Add bootindex support to usb-host and usb-redir
[qemu.git] / kvm-all.c
blobb8e9dc69de598dc2b8cd4774ccbb5cfe0861d86d
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 "gdbstub.h"
28 #include "kvm.h"
29 #include "bswap.h"
30 #include "memory.h"
31 #include "exec-memory.h"
33 /* This check must be after config-host.h is included */
34 #ifdef CONFIG_EVENTFD
35 #include <sys/eventfd.h>
36 #endif
38 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
39 #define PAGE_SIZE TARGET_PAGE_SIZE
41 //#define DEBUG_KVM
43 #ifdef DEBUG_KVM
44 #define DPRINTF(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46 #else
47 #define DPRINTF(fmt, ...) \
48 do { } while (0)
49 #endif
51 typedef struct KVMSlot
53 target_phys_addr_t start_addr;
54 ram_addr_t memory_size;
55 void *ram;
56 int slot;
57 int flags;
58 } KVMSlot;
60 typedef struct kvm_dirty_log KVMDirtyLog;
62 struct KVMState
64 KVMSlot slots[32];
65 int fd;
66 int vmfd;
67 int coalesced_mmio;
68 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
69 bool coalesced_flush_in_progress;
70 int broken_set_mem_region;
71 int migration_log;
72 int vcpu_events;
73 int robust_singlestep;
74 int debugregs;
75 #ifdef KVM_CAP_SET_GUEST_DEBUG
76 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
77 #endif
78 int pit_state2;
79 int xsave, xcrs;
80 int many_ioeventfds;
81 /* The man page (and posix) say ioctl numbers are signed int, but
82 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
83 * unsigned, and treating them as signed here can break things */
84 unsigned irqchip_inject_ioctl;
85 #ifdef KVM_CAP_IRQ_ROUTING
86 struct kvm_irq_routing *irq_routes;
87 int nr_allocated_irq_routes;
88 uint32_t *used_gsi_bitmap;
89 unsigned int max_gsi;
90 #endif
93 KVMState *kvm_state;
94 bool kvm_kernel_irqchip;
96 static const KVMCapabilityInfo kvm_required_capabilites[] = {
97 KVM_CAP_INFO(USER_MEMORY),
98 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
99 KVM_CAP_LAST_INFO
102 static KVMSlot *kvm_alloc_slot(KVMState *s)
104 int i;
106 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
107 if (s->slots[i].memory_size == 0) {
108 return &s->slots[i];
112 fprintf(stderr, "%s: no free slot available\n", __func__);
113 abort();
116 static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
117 target_phys_addr_t start_addr,
118 target_phys_addr_t end_addr)
120 int i;
122 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
123 KVMSlot *mem = &s->slots[i];
125 if (start_addr == mem->start_addr &&
126 end_addr == mem->start_addr + mem->memory_size) {
127 return mem;
131 return NULL;
135 * Find overlapping slot with lowest start address
137 static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
138 target_phys_addr_t start_addr,
139 target_phys_addr_t end_addr)
141 KVMSlot *found = NULL;
142 int i;
144 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
145 KVMSlot *mem = &s->slots[i];
147 if (mem->memory_size == 0 ||
148 (found && found->start_addr < mem->start_addr)) {
149 continue;
152 if (end_addr > mem->start_addr &&
153 start_addr < mem->start_addr + mem->memory_size) {
154 found = mem;
158 return found;
161 int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
162 target_phys_addr_t *phys_addr)
164 int i;
166 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
167 KVMSlot *mem = &s->slots[i];
169 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
170 *phys_addr = mem->start_addr + (ram - mem->ram);
171 return 1;
175 return 0;
178 static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
180 struct kvm_userspace_memory_region mem;
182 mem.slot = slot->slot;
183 mem.guest_phys_addr = slot->start_addr;
184 mem.memory_size = slot->memory_size;
185 mem.userspace_addr = (unsigned long)slot->ram;
186 mem.flags = slot->flags;
187 if (s->migration_log) {
188 mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
190 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
193 static void kvm_reset_vcpu(void *opaque)
195 CPUArchState *env = opaque;
197 kvm_arch_reset_vcpu(env);
200 int kvm_init_vcpu(CPUArchState *env)
202 KVMState *s = kvm_state;
203 long mmap_size;
204 int ret;
206 DPRINTF("kvm_init_vcpu\n");
208 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
209 if (ret < 0) {
210 DPRINTF("kvm_create_vcpu failed\n");
211 goto err;
214 env->kvm_fd = ret;
215 env->kvm_state = s;
216 env->kvm_vcpu_dirty = 1;
218 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
219 if (mmap_size < 0) {
220 ret = mmap_size;
221 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
222 goto err;
225 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
226 env->kvm_fd, 0);
227 if (env->kvm_run == MAP_FAILED) {
228 ret = -errno;
229 DPRINTF("mmap'ing vcpu state failed\n");
230 goto err;
233 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
234 s->coalesced_mmio_ring =
235 (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
238 ret = kvm_arch_init_vcpu(env);
239 if (ret == 0) {
240 qemu_register_reset(kvm_reset_vcpu, env);
241 kvm_arch_reset_vcpu(env);
243 err:
244 return ret;
248 * dirty pages logging control
251 static int kvm_mem_flags(KVMState *s, bool log_dirty)
253 return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
256 static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
258 KVMState *s = kvm_state;
259 int flags, mask = KVM_MEM_LOG_DIRTY_PAGES;
260 int old_flags;
262 old_flags = mem->flags;
264 flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
265 mem->flags = flags;
267 /* If nothing changed effectively, no need to issue ioctl */
268 if (s->migration_log) {
269 flags |= KVM_MEM_LOG_DIRTY_PAGES;
272 if (flags == old_flags) {
273 return 0;
276 return kvm_set_user_memory_region(s, mem);
279 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
280 ram_addr_t size, bool log_dirty)
282 KVMState *s = kvm_state;
283 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
285 if (mem == NULL) {
286 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
287 TARGET_FMT_plx "\n", __func__, phys_addr,
288 (target_phys_addr_t)(phys_addr + size - 1));
289 return -EINVAL;
291 return kvm_slot_dirty_pages_log_change(mem, log_dirty);
294 static void kvm_log_start(MemoryListener *listener,
295 MemoryRegionSection *section)
297 int r;
299 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
300 section->size, true);
301 if (r < 0) {
302 abort();
306 static void kvm_log_stop(MemoryListener *listener,
307 MemoryRegionSection *section)
309 int r;
311 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
312 section->size, false);
313 if (r < 0) {
314 abort();
318 static int kvm_set_migration_log(int enable)
320 KVMState *s = kvm_state;
321 KVMSlot *mem;
322 int i, err;
324 s->migration_log = enable;
326 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
327 mem = &s->slots[i];
329 if (!mem->memory_size) {
330 continue;
332 if (!!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) == enable) {
333 continue;
335 err = kvm_set_user_memory_region(s, mem);
336 if (err) {
337 return err;
340 return 0;
343 /* get kvm's dirty pages bitmap and update qemu's */
344 static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
345 unsigned long *bitmap)
347 unsigned int i, j;
348 unsigned long page_number, c;
349 target_phys_addr_t addr, addr1;
350 unsigned int len = ((section->size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
353 * bitmap-traveling is faster than memory-traveling (for addr...)
354 * especially when most of the memory is not dirty.
356 for (i = 0; i < len; i++) {
357 if (bitmap[i] != 0) {
358 c = leul_to_cpu(bitmap[i]);
359 do {
360 j = ffsl(c) - 1;
361 c &= ~(1ul << j);
362 page_number = i * HOST_LONG_BITS + j;
363 addr1 = page_number * TARGET_PAGE_SIZE;
364 addr = section->offset_within_region + addr1;
365 memory_region_set_dirty(section->mr, addr, TARGET_PAGE_SIZE);
366 } while (c != 0);
369 return 0;
372 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
375 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
376 * This function updates qemu's dirty bitmap using
377 * memory_region_set_dirty(). This means all bits are set
378 * to dirty.
380 * @start_add: start of logged region.
381 * @end_addr: end of logged region.
383 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
385 KVMState *s = kvm_state;
386 unsigned long size, allocated_size = 0;
387 KVMDirtyLog d;
388 KVMSlot *mem;
389 int ret = 0;
390 target_phys_addr_t start_addr = section->offset_within_address_space;
391 target_phys_addr_t end_addr = start_addr + section->size;
393 d.dirty_bitmap = NULL;
394 while (start_addr < end_addr) {
395 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
396 if (mem == NULL) {
397 break;
400 /* XXX bad kernel interface alert
401 * For dirty bitmap, kernel allocates array of size aligned to
402 * bits-per-long. But for case when the kernel is 64bits and
403 * the userspace is 32bits, userspace can't align to the same
404 * bits-per-long, since sizeof(long) is different between kernel
405 * and user space. This way, userspace will provide buffer which
406 * may be 4 bytes less than the kernel will use, resulting in
407 * userspace memory corruption (which is not detectable by valgrind
408 * too, in most cases).
409 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
410 * a hope that sizeof(long) wont become >8 any time soon.
412 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
413 /*HOST_LONG_BITS*/ 64) / 8;
414 if (!d.dirty_bitmap) {
415 d.dirty_bitmap = g_malloc(size);
416 } else if (size > allocated_size) {
417 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
419 allocated_size = size;
420 memset(d.dirty_bitmap, 0, allocated_size);
422 d.slot = mem->slot;
424 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
425 DPRINTF("ioctl failed %d\n", errno);
426 ret = -1;
427 break;
430 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
431 start_addr = mem->start_addr + mem->memory_size;
433 g_free(d.dirty_bitmap);
435 return ret;
438 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
440 int ret = -ENOSYS;
441 KVMState *s = kvm_state;
443 if (s->coalesced_mmio) {
444 struct kvm_coalesced_mmio_zone zone;
446 zone.addr = start;
447 zone.size = size;
448 zone.pad = 0;
450 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
453 return ret;
456 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
458 int ret = -ENOSYS;
459 KVMState *s = kvm_state;
461 if (s->coalesced_mmio) {
462 struct kvm_coalesced_mmio_zone zone;
464 zone.addr = start;
465 zone.size = size;
466 zone.pad = 0;
468 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
471 return ret;
474 int kvm_check_extension(KVMState *s, unsigned int extension)
476 int ret;
478 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
479 if (ret < 0) {
480 ret = 0;
483 return ret;
486 static int kvm_check_many_ioeventfds(void)
488 /* Userspace can use ioeventfd for io notification. This requires a host
489 * that supports eventfd(2) and an I/O thread; since eventfd does not
490 * support SIGIO it cannot interrupt the vcpu.
492 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
493 * can avoid creating too many ioeventfds.
495 #if defined(CONFIG_EVENTFD)
496 int ioeventfds[7];
497 int i, ret = 0;
498 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
499 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
500 if (ioeventfds[i] < 0) {
501 break;
503 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
504 if (ret < 0) {
505 close(ioeventfds[i]);
506 break;
510 /* Decide whether many devices are supported or not */
511 ret = i == ARRAY_SIZE(ioeventfds);
513 while (i-- > 0) {
514 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
515 close(ioeventfds[i]);
517 return ret;
518 #else
519 return 0;
520 #endif
523 static const KVMCapabilityInfo *
524 kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
526 while (list->name) {
527 if (!kvm_check_extension(s, list->value)) {
528 return list;
530 list++;
532 return NULL;
535 static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
537 KVMState *s = kvm_state;
538 KVMSlot *mem, old;
539 int err;
540 MemoryRegion *mr = section->mr;
541 bool log_dirty = memory_region_is_logging(mr);
542 target_phys_addr_t start_addr = section->offset_within_address_space;
543 ram_addr_t size = section->size;
544 void *ram = NULL;
545 unsigned delta;
547 /* kvm works in page size chunks, but the function may be called
548 with sub-page size and unaligned start address. */
549 delta = TARGET_PAGE_ALIGN(size) - size;
550 if (delta > size) {
551 return;
553 start_addr += delta;
554 size -= delta;
555 size &= TARGET_PAGE_MASK;
556 if (!size || (start_addr & ~TARGET_PAGE_MASK)) {
557 return;
560 if (!memory_region_is_ram(mr)) {
561 return;
564 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
566 while (1) {
567 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
568 if (!mem) {
569 break;
572 if (add && start_addr >= mem->start_addr &&
573 (start_addr + size <= mem->start_addr + mem->memory_size) &&
574 (ram - start_addr == mem->ram - mem->start_addr)) {
575 /* The new slot fits into the existing one and comes with
576 * identical parameters - update flags and done. */
577 kvm_slot_dirty_pages_log_change(mem, log_dirty);
578 return;
581 old = *mem;
583 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
584 kvm_physical_sync_dirty_bitmap(section);
587 /* unregister the overlapping slot */
588 mem->memory_size = 0;
589 err = kvm_set_user_memory_region(s, mem);
590 if (err) {
591 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
592 __func__, strerror(-err));
593 abort();
596 /* Workaround for older KVM versions: we can't join slots, even not by
597 * unregistering the previous ones and then registering the larger
598 * slot. We have to maintain the existing fragmentation. Sigh.
600 * This workaround assumes that the new slot starts at the same
601 * address as the first existing one. If not or if some overlapping
602 * slot comes around later, we will fail (not seen in practice so far)
603 * - and actually require a recent KVM version. */
604 if (s->broken_set_mem_region &&
605 old.start_addr == start_addr && old.memory_size < size && add) {
606 mem = kvm_alloc_slot(s);
607 mem->memory_size = old.memory_size;
608 mem->start_addr = old.start_addr;
609 mem->ram = old.ram;
610 mem->flags = kvm_mem_flags(s, log_dirty);
612 err = kvm_set_user_memory_region(s, mem);
613 if (err) {
614 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
615 strerror(-err));
616 abort();
619 start_addr += old.memory_size;
620 ram += old.memory_size;
621 size -= old.memory_size;
622 continue;
625 /* register prefix slot */
626 if (old.start_addr < start_addr) {
627 mem = kvm_alloc_slot(s);
628 mem->memory_size = start_addr - old.start_addr;
629 mem->start_addr = old.start_addr;
630 mem->ram = old.ram;
631 mem->flags = kvm_mem_flags(s, log_dirty);
633 err = kvm_set_user_memory_region(s, mem);
634 if (err) {
635 fprintf(stderr, "%s: error registering prefix slot: %s\n",
636 __func__, strerror(-err));
637 #ifdef TARGET_PPC
638 fprintf(stderr, "%s: This is probably because your kernel's " \
639 "PAGE_SIZE is too big. Please try to use 4k " \
640 "PAGE_SIZE!\n", __func__);
641 #endif
642 abort();
646 /* register suffix slot */
647 if (old.start_addr + old.memory_size > start_addr + size) {
648 ram_addr_t size_delta;
650 mem = kvm_alloc_slot(s);
651 mem->start_addr = start_addr + size;
652 size_delta = mem->start_addr - old.start_addr;
653 mem->memory_size = old.memory_size - size_delta;
654 mem->ram = old.ram + size_delta;
655 mem->flags = kvm_mem_flags(s, log_dirty);
657 err = kvm_set_user_memory_region(s, mem);
658 if (err) {
659 fprintf(stderr, "%s: error registering suffix slot: %s\n",
660 __func__, strerror(-err));
661 abort();
666 /* in case the KVM bug workaround already "consumed" the new slot */
667 if (!size) {
668 return;
670 if (!add) {
671 return;
673 mem = kvm_alloc_slot(s);
674 mem->memory_size = size;
675 mem->start_addr = start_addr;
676 mem->ram = ram;
677 mem->flags = kvm_mem_flags(s, log_dirty);
679 err = kvm_set_user_memory_region(s, mem);
680 if (err) {
681 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
682 strerror(-err));
683 abort();
687 static void kvm_begin(MemoryListener *listener)
691 static void kvm_commit(MemoryListener *listener)
695 static void kvm_region_add(MemoryListener *listener,
696 MemoryRegionSection *section)
698 kvm_set_phys_mem(section, true);
701 static void kvm_region_del(MemoryListener *listener,
702 MemoryRegionSection *section)
704 kvm_set_phys_mem(section, false);
707 static void kvm_region_nop(MemoryListener *listener,
708 MemoryRegionSection *section)
712 static void kvm_log_sync(MemoryListener *listener,
713 MemoryRegionSection *section)
715 int r;
717 r = kvm_physical_sync_dirty_bitmap(section);
718 if (r < 0) {
719 abort();
723 static void kvm_log_global_start(struct MemoryListener *listener)
725 int r;
727 r = kvm_set_migration_log(1);
728 assert(r >= 0);
731 static void kvm_log_global_stop(struct MemoryListener *listener)
733 int r;
735 r = kvm_set_migration_log(0);
736 assert(r >= 0);
739 static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
740 bool match_data, uint64_t data, int fd)
742 int r;
744 assert(match_data && section->size <= 8);
746 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
747 data, true, section->size);
748 if (r < 0) {
749 abort();
753 static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
754 bool match_data, uint64_t data, int fd)
756 int r;
758 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
759 data, false, section->size);
760 if (r < 0) {
761 abort();
765 static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
766 bool match_data, uint64_t data, int fd)
768 int r;
770 assert(match_data && section->size == 2);
772 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
773 data, true);
774 if (r < 0) {
775 abort();
779 static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
780 bool match_data, uint64_t data, int fd)
783 int r;
785 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
786 data, false);
787 if (r < 0) {
788 abort();
792 static void kvm_eventfd_add(MemoryListener *listener,
793 MemoryRegionSection *section,
794 bool match_data, uint64_t data, int fd)
796 if (section->address_space == get_system_memory()) {
797 kvm_mem_ioeventfd_add(section, match_data, data, fd);
798 } else {
799 kvm_io_ioeventfd_add(section, match_data, data, fd);
803 static void kvm_eventfd_del(MemoryListener *listener,
804 MemoryRegionSection *section,
805 bool match_data, uint64_t data, int fd)
807 if (section->address_space == get_system_memory()) {
808 kvm_mem_ioeventfd_del(section, match_data, data, fd);
809 } else {
810 kvm_io_ioeventfd_del(section, match_data, data, fd);
814 static MemoryListener kvm_memory_listener = {
815 .begin = kvm_begin,
816 .commit = kvm_commit,
817 .region_add = kvm_region_add,
818 .region_del = kvm_region_del,
819 .region_nop = kvm_region_nop,
820 .log_start = kvm_log_start,
821 .log_stop = kvm_log_stop,
822 .log_sync = kvm_log_sync,
823 .log_global_start = kvm_log_global_start,
824 .log_global_stop = kvm_log_global_stop,
825 .eventfd_add = kvm_eventfd_add,
826 .eventfd_del = kvm_eventfd_del,
827 .priority = 10,
830 static void kvm_handle_interrupt(CPUArchState *env, int mask)
832 env->interrupt_request |= mask;
834 if (!qemu_cpu_is_self(env)) {
835 qemu_cpu_kick(env);
839 int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
841 struct kvm_irq_level event;
842 int ret;
844 assert(kvm_irqchip_in_kernel());
846 event.level = level;
847 event.irq = irq;
848 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
849 if (ret < 0) {
850 perror("kvm_set_irqchip_line");
851 abort();
854 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
857 #ifdef KVM_CAP_IRQ_ROUTING
858 static void set_gsi(KVMState *s, unsigned int gsi)
860 assert(gsi < s->max_gsi);
862 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
865 static void kvm_init_irq_routing(KVMState *s)
867 int gsi_count;
869 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
870 if (gsi_count > 0) {
871 unsigned int gsi_bits, i;
873 /* Round up so we can search ints using ffs */
874 gsi_bits = ALIGN(gsi_count, 32);
875 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
876 s->max_gsi = gsi_bits;
878 /* Mark any over-allocated bits as already in use */
879 for (i = gsi_count; i < gsi_bits; i++) {
880 set_gsi(s, i);
884 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
885 s->nr_allocated_irq_routes = 0;
887 kvm_arch_init_irq_routing(s);
890 static void kvm_add_routing_entry(KVMState *s,
891 struct kvm_irq_routing_entry *entry)
893 struct kvm_irq_routing_entry *new;
894 int n, size;
896 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
897 n = s->nr_allocated_irq_routes * 2;
898 if (n < 64) {
899 n = 64;
901 size = sizeof(struct kvm_irq_routing);
902 size += n * sizeof(*new);
903 s->irq_routes = g_realloc(s->irq_routes, size);
904 s->nr_allocated_irq_routes = n;
906 n = s->irq_routes->nr++;
907 new = &s->irq_routes->entries[n];
908 memset(new, 0, sizeof(*new));
909 new->gsi = entry->gsi;
910 new->type = entry->type;
911 new->flags = entry->flags;
912 new->u = entry->u;
914 set_gsi(s, entry->gsi);
917 void kvm_irqchip_add_route(KVMState *s, int irq, int irqchip, int pin)
919 struct kvm_irq_routing_entry e;
921 e.gsi = irq;
922 e.type = KVM_IRQ_ROUTING_IRQCHIP;
923 e.flags = 0;
924 e.u.irqchip.irqchip = irqchip;
925 e.u.irqchip.pin = pin;
926 kvm_add_routing_entry(s, &e);
929 int kvm_irqchip_commit_routes(KVMState *s)
931 s->irq_routes->flags = 0;
932 return kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
935 #else /* !KVM_CAP_IRQ_ROUTING */
937 static void kvm_init_irq_routing(KVMState *s)
940 #endif /* !KVM_CAP_IRQ_ROUTING */
942 static int kvm_irqchip_create(KVMState *s)
944 QemuOptsList *list = qemu_find_opts("machine");
945 int ret;
947 if (QTAILQ_EMPTY(&list->head) ||
948 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
949 "kernel_irqchip", false) ||
950 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
951 return 0;
954 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
955 if (ret < 0) {
956 fprintf(stderr, "Create kernel irqchip failed\n");
957 return ret;
960 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
961 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
962 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
964 kvm_kernel_irqchip = true;
966 kvm_init_irq_routing(s);
968 return 0;
971 int kvm_init(void)
973 static const char upgrade_note[] =
974 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
975 "(see http://sourceforge.net/projects/kvm).\n";
976 KVMState *s;
977 const KVMCapabilityInfo *missing_cap;
978 int ret;
979 int i;
981 s = g_malloc0(sizeof(KVMState));
983 #ifdef KVM_CAP_SET_GUEST_DEBUG
984 QTAILQ_INIT(&s->kvm_sw_breakpoints);
985 #endif
986 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
987 s->slots[i].slot = i;
989 s->vmfd = -1;
990 s->fd = qemu_open("/dev/kvm", O_RDWR);
991 if (s->fd == -1) {
992 fprintf(stderr, "Could not access KVM kernel module: %m\n");
993 ret = -errno;
994 goto err;
997 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
998 if (ret < KVM_API_VERSION) {
999 if (ret > 0) {
1000 ret = -EINVAL;
1002 fprintf(stderr, "kvm version too old\n");
1003 goto err;
1006 if (ret > KVM_API_VERSION) {
1007 ret = -EINVAL;
1008 fprintf(stderr, "kvm version not supported\n");
1009 goto err;
1012 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
1013 if (s->vmfd < 0) {
1014 #ifdef TARGET_S390X
1015 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1016 "your host kernel command line\n");
1017 #endif
1018 ret = s->vmfd;
1019 goto err;
1022 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1023 if (!missing_cap) {
1024 missing_cap =
1025 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1027 if (missing_cap) {
1028 ret = -EINVAL;
1029 fprintf(stderr, "kvm does not support %s\n%s",
1030 missing_cap->name, upgrade_note);
1031 goto err;
1034 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
1036 s->broken_set_mem_region = 1;
1037 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
1038 if (ret > 0) {
1039 s->broken_set_mem_region = 0;
1042 #ifdef KVM_CAP_VCPU_EVENTS
1043 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1044 #endif
1046 s->robust_singlestep =
1047 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
1049 #ifdef KVM_CAP_DEBUGREGS
1050 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1051 #endif
1053 #ifdef KVM_CAP_XSAVE
1054 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1055 #endif
1057 #ifdef KVM_CAP_XCRS
1058 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1059 #endif
1061 #ifdef KVM_CAP_PIT_STATE2
1062 s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1063 #endif
1065 ret = kvm_arch_init(s);
1066 if (ret < 0) {
1067 goto err;
1070 ret = kvm_irqchip_create(s);
1071 if (ret < 0) {
1072 goto err;
1075 kvm_state = s;
1076 memory_listener_register(&kvm_memory_listener, NULL);
1078 s->many_ioeventfds = kvm_check_many_ioeventfds();
1080 cpu_interrupt_handler = kvm_handle_interrupt;
1082 return 0;
1084 err:
1085 if (s) {
1086 if (s->vmfd >= 0) {
1087 close(s->vmfd);
1089 if (s->fd != -1) {
1090 close(s->fd);
1093 g_free(s);
1095 return ret;
1098 static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1099 uint32_t count)
1101 int i;
1102 uint8_t *ptr = data;
1104 for (i = 0; i < count; i++) {
1105 if (direction == KVM_EXIT_IO_IN) {
1106 switch (size) {
1107 case 1:
1108 stb_p(ptr, cpu_inb(port));
1109 break;
1110 case 2:
1111 stw_p(ptr, cpu_inw(port));
1112 break;
1113 case 4:
1114 stl_p(ptr, cpu_inl(port));
1115 break;
1117 } else {
1118 switch (size) {
1119 case 1:
1120 cpu_outb(port, ldub_p(ptr));
1121 break;
1122 case 2:
1123 cpu_outw(port, lduw_p(ptr));
1124 break;
1125 case 4:
1126 cpu_outl(port, ldl_p(ptr));
1127 break;
1131 ptr += size;
1135 static int kvm_handle_internal_error(CPUArchState *env, struct kvm_run *run)
1137 fprintf(stderr, "KVM internal error.");
1138 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1139 int i;
1141 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
1142 for (i = 0; i < run->internal.ndata; ++i) {
1143 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1144 i, (uint64_t)run->internal.data[i]);
1146 } else {
1147 fprintf(stderr, "\n");
1149 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1150 fprintf(stderr, "emulation failure\n");
1151 if (!kvm_arch_stop_on_emulation_error(env)) {
1152 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1153 return EXCP_INTERRUPT;
1156 /* FIXME: Should trigger a qmp message to let management know
1157 * something went wrong.
1159 return -1;
1162 void kvm_flush_coalesced_mmio_buffer(void)
1164 KVMState *s = kvm_state;
1166 if (s->coalesced_flush_in_progress) {
1167 return;
1170 s->coalesced_flush_in_progress = true;
1172 if (s->coalesced_mmio_ring) {
1173 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
1174 while (ring->first != ring->last) {
1175 struct kvm_coalesced_mmio *ent;
1177 ent = &ring->coalesced_mmio[ring->first];
1179 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
1180 smp_wmb();
1181 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1185 s->coalesced_flush_in_progress = false;
1188 static void do_kvm_cpu_synchronize_state(void *_env)
1190 CPUArchState *env = _env;
1192 if (!env->kvm_vcpu_dirty) {
1193 kvm_arch_get_registers(env);
1194 env->kvm_vcpu_dirty = 1;
1198 void kvm_cpu_synchronize_state(CPUArchState *env)
1200 if (!env->kvm_vcpu_dirty) {
1201 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
1205 void kvm_cpu_synchronize_post_reset(CPUArchState *env)
1207 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1208 env->kvm_vcpu_dirty = 0;
1211 void kvm_cpu_synchronize_post_init(CPUArchState *env)
1213 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1214 env->kvm_vcpu_dirty = 0;
1217 int kvm_cpu_exec(CPUArchState *env)
1219 struct kvm_run *run = env->kvm_run;
1220 int ret, run_ret;
1222 DPRINTF("kvm_cpu_exec()\n");
1224 if (kvm_arch_process_async_events(env)) {
1225 env->exit_request = 0;
1226 return EXCP_HLT;
1229 do {
1230 if (env->kvm_vcpu_dirty) {
1231 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
1232 env->kvm_vcpu_dirty = 0;
1235 kvm_arch_pre_run(env, run);
1236 if (env->exit_request) {
1237 DPRINTF("interrupt exit requested\n");
1239 * KVM requires us to reenter the kernel after IO exits to complete
1240 * instruction emulation. This self-signal will ensure that we
1241 * leave ASAP again.
1243 qemu_cpu_kick_self();
1245 qemu_mutex_unlock_iothread();
1247 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
1249 qemu_mutex_lock_iothread();
1250 kvm_arch_post_run(env, run);
1252 kvm_flush_coalesced_mmio_buffer();
1254 if (run_ret < 0) {
1255 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1256 DPRINTF("io window exit\n");
1257 ret = EXCP_INTERRUPT;
1258 break;
1260 fprintf(stderr, "error: kvm run failed %s\n",
1261 strerror(-run_ret));
1262 abort();
1265 switch (run->exit_reason) {
1266 case KVM_EXIT_IO:
1267 DPRINTF("handle_io\n");
1268 kvm_handle_io(run->io.port,
1269 (uint8_t *)run + run->io.data_offset,
1270 run->io.direction,
1271 run->io.size,
1272 run->io.count);
1273 ret = 0;
1274 break;
1275 case KVM_EXIT_MMIO:
1276 DPRINTF("handle_mmio\n");
1277 cpu_physical_memory_rw(run->mmio.phys_addr,
1278 run->mmio.data,
1279 run->mmio.len,
1280 run->mmio.is_write);
1281 ret = 0;
1282 break;
1283 case KVM_EXIT_IRQ_WINDOW_OPEN:
1284 DPRINTF("irq_window_open\n");
1285 ret = EXCP_INTERRUPT;
1286 break;
1287 case KVM_EXIT_SHUTDOWN:
1288 DPRINTF("shutdown\n");
1289 qemu_system_reset_request();
1290 ret = EXCP_INTERRUPT;
1291 break;
1292 case KVM_EXIT_UNKNOWN:
1293 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1294 (uint64_t)run->hw.hardware_exit_reason);
1295 ret = -1;
1296 break;
1297 case KVM_EXIT_INTERNAL_ERROR:
1298 ret = kvm_handle_internal_error(env, run);
1299 break;
1300 default:
1301 DPRINTF("kvm_arch_handle_exit\n");
1302 ret = kvm_arch_handle_exit(env, run);
1303 break;
1305 } while (ret == 0);
1307 if (ret < 0) {
1308 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1309 vm_stop(RUN_STATE_INTERNAL_ERROR);
1312 env->exit_request = 0;
1313 return ret;
1316 int kvm_ioctl(KVMState *s, int type, ...)
1318 int ret;
1319 void *arg;
1320 va_list ap;
1322 va_start(ap, type);
1323 arg = va_arg(ap, void *);
1324 va_end(ap);
1326 ret = ioctl(s->fd, type, arg);
1327 if (ret == -1) {
1328 ret = -errno;
1330 return ret;
1333 int kvm_vm_ioctl(KVMState *s, int type, ...)
1335 int ret;
1336 void *arg;
1337 va_list ap;
1339 va_start(ap, type);
1340 arg = va_arg(ap, void *);
1341 va_end(ap);
1343 ret = ioctl(s->vmfd, type, arg);
1344 if (ret == -1) {
1345 ret = -errno;
1347 return ret;
1350 int kvm_vcpu_ioctl(CPUArchState *env, int type, ...)
1352 int ret;
1353 void *arg;
1354 va_list ap;
1356 va_start(ap, type);
1357 arg = va_arg(ap, void *);
1358 va_end(ap);
1360 ret = ioctl(env->kvm_fd, type, arg);
1361 if (ret == -1) {
1362 ret = -errno;
1364 return ret;
1367 int kvm_has_sync_mmu(void)
1369 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
1372 int kvm_has_vcpu_events(void)
1374 return kvm_state->vcpu_events;
1377 int kvm_has_robust_singlestep(void)
1379 return kvm_state->robust_singlestep;
1382 int kvm_has_debugregs(void)
1384 return kvm_state->debugregs;
1387 int kvm_has_xsave(void)
1389 return kvm_state->xsave;
1392 int kvm_has_xcrs(void)
1394 return kvm_state->xcrs;
1397 int kvm_has_pit_state2(void)
1399 return kvm_state->pit_state2;
1402 int kvm_has_many_ioeventfds(void)
1404 if (!kvm_enabled()) {
1405 return 0;
1407 return kvm_state->many_ioeventfds;
1410 int kvm_has_gsi_routing(void)
1412 #ifdef KVM_CAP_IRQ_ROUTING
1413 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
1414 #else
1415 return false;
1416 #endif
1419 int kvm_allows_irq0_override(void)
1421 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1424 void kvm_setup_guest_memory(void *start, size_t size)
1426 if (!kvm_has_sync_mmu()) {
1427 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
1429 if (ret) {
1430 perror("qemu_madvise");
1431 fprintf(stderr,
1432 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1433 exit(1);
1438 #ifdef KVM_CAP_SET_GUEST_DEBUG
1439 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
1440 target_ulong pc)
1442 struct kvm_sw_breakpoint *bp;
1444 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
1445 if (bp->pc == pc) {
1446 return bp;
1449 return NULL;
1452 int kvm_sw_breakpoints_active(CPUArchState *env)
1454 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
1457 struct kvm_set_guest_debug_data {
1458 struct kvm_guest_debug dbg;
1459 CPUArchState *env;
1460 int err;
1463 static void kvm_invoke_set_guest_debug(void *data)
1465 struct kvm_set_guest_debug_data *dbg_data = data;
1466 CPUArchState *env = dbg_data->env;
1468 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
1471 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1473 struct kvm_set_guest_debug_data data;
1475 data.dbg.control = reinject_trap;
1477 if (env->singlestep_enabled) {
1478 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1480 kvm_arch_update_guest_debug(env, &data.dbg);
1481 data.env = env;
1483 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
1484 return data.err;
1487 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1488 target_ulong len, int type)
1490 struct kvm_sw_breakpoint *bp;
1491 CPUArchState *env;
1492 int err;
1494 if (type == GDB_BREAKPOINT_SW) {
1495 bp = kvm_find_sw_breakpoint(current_env, addr);
1496 if (bp) {
1497 bp->use_count++;
1498 return 0;
1501 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
1502 if (!bp) {
1503 return -ENOMEM;
1506 bp->pc = addr;
1507 bp->use_count = 1;
1508 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1509 if (err) {
1510 g_free(bp);
1511 return err;
1514 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
1515 bp, entry);
1516 } else {
1517 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1518 if (err) {
1519 return err;
1523 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1524 err = kvm_update_guest_debug(env, 0);
1525 if (err) {
1526 return err;
1529 return 0;
1532 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1533 target_ulong len, int type)
1535 struct kvm_sw_breakpoint *bp;
1536 CPUArchState *env;
1537 int err;
1539 if (type == GDB_BREAKPOINT_SW) {
1540 bp = kvm_find_sw_breakpoint(current_env, addr);
1541 if (!bp) {
1542 return -ENOENT;
1545 if (bp->use_count > 1) {
1546 bp->use_count--;
1547 return 0;
1550 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1551 if (err) {
1552 return err;
1555 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
1556 g_free(bp);
1557 } else {
1558 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1559 if (err) {
1560 return err;
1564 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1565 err = kvm_update_guest_debug(env, 0);
1566 if (err) {
1567 return err;
1570 return 0;
1573 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1575 struct kvm_sw_breakpoint *bp, *next;
1576 KVMState *s = current_env->kvm_state;
1577 CPUArchState *env;
1579 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
1580 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1581 /* Try harder to find a CPU that currently sees the breakpoint. */
1582 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1583 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
1584 break;
1589 kvm_arch_remove_all_hw_breakpoints();
1591 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1592 kvm_update_guest_debug(env, 0);
1596 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1598 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1600 return -EINVAL;
1603 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1604 target_ulong len, int type)
1606 return -EINVAL;
1609 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1610 target_ulong len, int type)
1612 return -EINVAL;
1615 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1618 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1620 int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset)
1622 struct kvm_signal_mask *sigmask;
1623 int r;
1625 if (!sigset) {
1626 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
1629 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
1631 sigmask->len = 8;
1632 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1633 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
1634 g_free(sigmask);
1636 return r;
1639 int kvm_set_ioeventfd_mmio(int fd, uint32_t addr, uint32_t val, bool assign,
1640 uint32_t size)
1642 int ret;
1643 struct kvm_ioeventfd iofd;
1645 iofd.datamatch = val;
1646 iofd.addr = addr;
1647 iofd.len = size;
1648 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1649 iofd.fd = fd;
1651 if (!kvm_enabled()) {
1652 return -ENOSYS;
1655 if (!assign) {
1656 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1659 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1661 if (ret < 0) {
1662 return -errno;
1665 return 0;
1668 int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1670 struct kvm_ioeventfd kick = {
1671 .datamatch = val,
1672 .addr = addr,
1673 .len = 2,
1674 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1675 .fd = fd,
1677 int r;
1678 if (!kvm_enabled()) {
1679 return -ENOSYS;
1681 if (!assign) {
1682 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1684 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1685 if (r < 0) {
1686 return r;
1688 return 0;
1691 int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr)
1693 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1696 int kvm_on_sigbus(int code, void *addr)
1698 return kvm_arch_on_sigbus(code, addr);