virtio-pci: add missing 'static'
[qemu/opensuse.git] / kvm-all.c
blob9b73ccfbecb12d6fd08e1bc9ccc54868cfb4271d
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;
351 unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
354 * bitmap-traveling is faster than memory-traveling (for addr...)
355 * especially when most of the memory is not dirty.
357 for (i = 0; i < len; i++) {
358 if (bitmap[i] != 0) {
359 c = leul_to_cpu(bitmap[i]);
360 do {
361 j = ffsl(c) - 1;
362 c &= ~(1ul << j);
363 page_number = (i * HOST_LONG_BITS + j) * hpratio;
364 addr1 = page_number * TARGET_PAGE_SIZE;
365 addr = section->offset_within_region + addr1;
366 memory_region_set_dirty(section->mr, addr,
367 TARGET_PAGE_SIZE * hpratio);
368 } while (c != 0);
371 return 0;
374 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
377 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
378 * This function updates qemu's dirty bitmap using
379 * memory_region_set_dirty(). This means all bits are set
380 * to dirty.
382 * @start_add: start of logged region.
383 * @end_addr: end of logged region.
385 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
387 KVMState *s = kvm_state;
388 unsigned long size, allocated_size = 0;
389 KVMDirtyLog d;
390 KVMSlot *mem;
391 int ret = 0;
392 target_phys_addr_t start_addr = section->offset_within_address_space;
393 target_phys_addr_t end_addr = start_addr + section->size;
395 d.dirty_bitmap = NULL;
396 while (start_addr < end_addr) {
397 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
398 if (mem == NULL) {
399 break;
402 /* XXX bad kernel interface alert
403 * For dirty bitmap, kernel allocates array of size aligned to
404 * bits-per-long. But for case when the kernel is 64bits and
405 * the userspace is 32bits, userspace can't align to the same
406 * bits-per-long, since sizeof(long) is different between kernel
407 * and user space. This way, userspace will provide buffer which
408 * may be 4 bytes less than the kernel will use, resulting in
409 * userspace memory corruption (which is not detectable by valgrind
410 * too, in most cases).
411 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
412 * a hope that sizeof(long) wont become >8 any time soon.
414 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
415 /*HOST_LONG_BITS*/ 64) / 8;
416 if (!d.dirty_bitmap) {
417 d.dirty_bitmap = g_malloc(size);
418 } else if (size > allocated_size) {
419 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
421 allocated_size = size;
422 memset(d.dirty_bitmap, 0, allocated_size);
424 d.slot = mem->slot;
426 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
427 DPRINTF("ioctl failed %d\n", errno);
428 ret = -1;
429 break;
432 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
433 start_addr = mem->start_addr + mem->memory_size;
435 g_free(d.dirty_bitmap);
437 return ret;
440 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
442 int ret = -ENOSYS;
443 KVMState *s = kvm_state;
445 if (s->coalesced_mmio) {
446 struct kvm_coalesced_mmio_zone zone;
448 zone.addr = start;
449 zone.size = size;
450 zone.pad = 0;
452 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
455 return ret;
458 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
460 int ret = -ENOSYS;
461 KVMState *s = kvm_state;
463 if (s->coalesced_mmio) {
464 struct kvm_coalesced_mmio_zone zone;
466 zone.addr = start;
467 zone.size = size;
468 zone.pad = 0;
470 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
473 return ret;
476 int kvm_check_extension(KVMState *s, unsigned int extension)
478 int ret;
480 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
481 if (ret < 0) {
482 ret = 0;
485 return ret;
488 static int kvm_check_many_ioeventfds(void)
490 /* Userspace can use ioeventfd for io notification. This requires a host
491 * that supports eventfd(2) and an I/O thread; since eventfd does not
492 * support SIGIO it cannot interrupt the vcpu.
494 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
495 * can avoid creating too many ioeventfds.
497 #if defined(CONFIG_EVENTFD)
498 int ioeventfds[7];
499 int i, ret = 0;
500 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
501 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
502 if (ioeventfds[i] < 0) {
503 break;
505 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
506 if (ret < 0) {
507 close(ioeventfds[i]);
508 break;
512 /* Decide whether many devices are supported or not */
513 ret = i == ARRAY_SIZE(ioeventfds);
515 while (i-- > 0) {
516 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
517 close(ioeventfds[i]);
519 return ret;
520 #else
521 return 0;
522 #endif
525 static const KVMCapabilityInfo *
526 kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
528 while (list->name) {
529 if (!kvm_check_extension(s, list->value)) {
530 return list;
532 list++;
534 return NULL;
537 static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
539 KVMState *s = kvm_state;
540 KVMSlot *mem, old;
541 int err;
542 MemoryRegion *mr = section->mr;
543 bool log_dirty = memory_region_is_logging(mr);
544 target_phys_addr_t start_addr = section->offset_within_address_space;
545 ram_addr_t size = section->size;
546 void *ram = NULL;
547 unsigned delta;
549 /* kvm works in page size chunks, but the function may be called
550 with sub-page size and unaligned start address. */
551 delta = TARGET_PAGE_ALIGN(size) - size;
552 if (delta > size) {
553 return;
555 start_addr += delta;
556 size -= delta;
557 size &= TARGET_PAGE_MASK;
558 if (!size || (start_addr & ~TARGET_PAGE_MASK)) {
559 return;
562 if (!memory_region_is_ram(mr)) {
563 return;
566 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
568 while (1) {
569 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
570 if (!mem) {
571 break;
574 if (add && start_addr >= mem->start_addr &&
575 (start_addr + size <= mem->start_addr + mem->memory_size) &&
576 (ram - start_addr == mem->ram - mem->start_addr)) {
577 /* The new slot fits into the existing one and comes with
578 * identical parameters - update flags and done. */
579 kvm_slot_dirty_pages_log_change(mem, log_dirty);
580 return;
583 old = *mem;
585 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
586 kvm_physical_sync_dirty_bitmap(section);
589 /* unregister the overlapping slot */
590 mem->memory_size = 0;
591 err = kvm_set_user_memory_region(s, mem);
592 if (err) {
593 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
594 __func__, strerror(-err));
595 abort();
598 /* Workaround for older KVM versions: we can't join slots, even not by
599 * unregistering the previous ones and then registering the larger
600 * slot. We have to maintain the existing fragmentation. Sigh.
602 * This workaround assumes that the new slot starts at the same
603 * address as the first existing one. If not or if some overlapping
604 * slot comes around later, we will fail (not seen in practice so far)
605 * - and actually require a recent KVM version. */
606 if (s->broken_set_mem_region &&
607 old.start_addr == start_addr && old.memory_size < size && add) {
608 mem = kvm_alloc_slot(s);
609 mem->memory_size = old.memory_size;
610 mem->start_addr = old.start_addr;
611 mem->ram = old.ram;
612 mem->flags = kvm_mem_flags(s, log_dirty);
614 err = kvm_set_user_memory_region(s, mem);
615 if (err) {
616 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
617 strerror(-err));
618 abort();
621 start_addr += old.memory_size;
622 ram += old.memory_size;
623 size -= old.memory_size;
624 continue;
627 /* register prefix slot */
628 if (old.start_addr < start_addr) {
629 mem = kvm_alloc_slot(s);
630 mem->memory_size = start_addr - old.start_addr;
631 mem->start_addr = old.start_addr;
632 mem->ram = old.ram;
633 mem->flags = kvm_mem_flags(s, log_dirty);
635 err = kvm_set_user_memory_region(s, mem);
636 if (err) {
637 fprintf(stderr, "%s: error registering prefix slot: %s\n",
638 __func__, strerror(-err));
639 #ifdef TARGET_PPC
640 fprintf(stderr, "%s: This is probably because your kernel's " \
641 "PAGE_SIZE is too big. Please try to use 4k " \
642 "PAGE_SIZE!\n", __func__);
643 #endif
644 abort();
648 /* register suffix slot */
649 if (old.start_addr + old.memory_size > start_addr + size) {
650 ram_addr_t size_delta;
652 mem = kvm_alloc_slot(s);
653 mem->start_addr = start_addr + size;
654 size_delta = mem->start_addr - old.start_addr;
655 mem->memory_size = old.memory_size - size_delta;
656 mem->ram = old.ram + size_delta;
657 mem->flags = kvm_mem_flags(s, log_dirty);
659 err = kvm_set_user_memory_region(s, mem);
660 if (err) {
661 fprintf(stderr, "%s: error registering suffix slot: %s\n",
662 __func__, strerror(-err));
663 abort();
668 /* in case the KVM bug workaround already "consumed" the new slot */
669 if (!size) {
670 return;
672 if (!add) {
673 return;
675 mem = kvm_alloc_slot(s);
676 mem->memory_size = size;
677 mem->start_addr = start_addr;
678 mem->ram = ram;
679 mem->flags = kvm_mem_flags(s, log_dirty);
681 err = kvm_set_user_memory_region(s, mem);
682 if (err) {
683 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
684 strerror(-err));
685 abort();
689 static void kvm_begin(MemoryListener *listener)
693 static void kvm_commit(MemoryListener *listener)
697 static void kvm_region_add(MemoryListener *listener,
698 MemoryRegionSection *section)
700 kvm_set_phys_mem(section, true);
703 static void kvm_region_del(MemoryListener *listener,
704 MemoryRegionSection *section)
706 kvm_set_phys_mem(section, false);
709 static void kvm_region_nop(MemoryListener *listener,
710 MemoryRegionSection *section)
714 static void kvm_log_sync(MemoryListener *listener,
715 MemoryRegionSection *section)
717 int r;
719 r = kvm_physical_sync_dirty_bitmap(section);
720 if (r < 0) {
721 abort();
725 static void kvm_log_global_start(struct MemoryListener *listener)
727 int r;
729 r = kvm_set_migration_log(1);
730 assert(r >= 0);
733 static void kvm_log_global_stop(struct MemoryListener *listener)
735 int r;
737 r = kvm_set_migration_log(0);
738 assert(r >= 0);
741 static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
742 bool match_data, uint64_t data, int fd)
744 int r;
746 assert(match_data && section->size <= 8);
748 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
749 data, true, section->size);
750 if (r < 0) {
751 abort();
755 static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
756 bool match_data, uint64_t data, int fd)
758 int r;
760 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
761 data, false, section->size);
762 if (r < 0) {
763 abort();
767 static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
768 bool match_data, uint64_t data, int fd)
770 int r;
772 assert(match_data && section->size == 2);
774 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
775 data, true);
776 if (r < 0) {
777 abort();
781 static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
782 bool match_data, uint64_t data, int fd)
785 int r;
787 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
788 data, false);
789 if (r < 0) {
790 abort();
794 static void kvm_eventfd_add(MemoryListener *listener,
795 MemoryRegionSection *section,
796 bool match_data, uint64_t data, int fd)
798 if (section->address_space == get_system_memory()) {
799 kvm_mem_ioeventfd_add(section, match_data, data, fd);
800 } else {
801 kvm_io_ioeventfd_add(section, match_data, data, fd);
805 static void kvm_eventfd_del(MemoryListener *listener,
806 MemoryRegionSection *section,
807 bool match_data, uint64_t data, int fd)
809 if (section->address_space == get_system_memory()) {
810 kvm_mem_ioeventfd_del(section, match_data, data, fd);
811 } else {
812 kvm_io_ioeventfd_del(section, match_data, data, fd);
816 static MemoryListener kvm_memory_listener = {
817 .begin = kvm_begin,
818 .commit = kvm_commit,
819 .region_add = kvm_region_add,
820 .region_del = kvm_region_del,
821 .region_nop = kvm_region_nop,
822 .log_start = kvm_log_start,
823 .log_stop = kvm_log_stop,
824 .log_sync = kvm_log_sync,
825 .log_global_start = kvm_log_global_start,
826 .log_global_stop = kvm_log_global_stop,
827 .eventfd_add = kvm_eventfd_add,
828 .eventfd_del = kvm_eventfd_del,
829 .priority = 10,
832 static void kvm_handle_interrupt(CPUArchState *env, int mask)
834 env->interrupt_request |= mask;
836 if (!qemu_cpu_is_self(env)) {
837 qemu_cpu_kick(env);
841 int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
843 struct kvm_irq_level event;
844 int ret;
846 assert(kvm_irqchip_in_kernel());
848 event.level = level;
849 event.irq = irq;
850 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
851 if (ret < 0) {
852 perror("kvm_set_irqchip_line");
853 abort();
856 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
859 #ifdef KVM_CAP_IRQ_ROUTING
860 static void set_gsi(KVMState *s, unsigned int gsi)
862 assert(gsi < s->max_gsi);
864 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
867 static void kvm_init_irq_routing(KVMState *s)
869 int gsi_count;
871 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
872 if (gsi_count > 0) {
873 unsigned int gsi_bits, i;
875 /* Round up so we can search ints using ffs */
876 gsi_bits = ALIGN(gsi_count, 32);
877 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
878 s->max_gsi = gsi_bits;
880 /* Mark any over-allocated bits as already in use */
881 for (i = gsi_count; i < gsi_bits; i++) {
882 set_gsi(s, i);
886 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
887 s->nr_allocated_irq_routes = 0;
889 kvm_arch_init_irq_routing(s);
892 static void kvm_add_routing_entry(KVMState *s,
893 struct kvm_irq_routing_entry *entry)
895 struct kvm_irq_routing_entry *new;
896 int n, size;
898 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
899 n = s->nr_allocated_irq_routes * 2;
900 if (n < 64) {
901 n = 64;
903 size = sizeof(struct kvm_irq_routing);
904 size += n * sizeof(*new);
905 s->irq_routes = g_realloc(s->irq_routes, size);
906 s->nr_allocated_irq_routes = n;
908 n = s->irq_routes->nr++;
909 new = &s->irq_routes->entries[n];
910 memset(new, 0, sizeof(*new));
911 new->gsi = entry->gsi;
912 new->type = entry->type;
913 new->flags = entry->flags;
914 new->u = entry->u;
916 set_gsi(s, entry->gsi);
919 void kvm_irqchip_add_route(KVMState *s, int irq, int irqchip, int pin)
921 struct kvm_irq_routing_entry e;
923 e.gsi = irq;
924 e.type = KVM_IRQ_ROUTING_IRQCHIP;
925 e.flags = 0;
926 e.u.irqchip.irqchip = irqchip;
927 e.u.irqchip.pin = pin;
928 kvm_add_routing_entry(s, &e);
931 int kvm_irqchip_commit_routes(KVMState *s)
933 s->irq_routes->flags = 0;
934 return kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
937 #else /* !KVM_CAP_IRQ_ROUTING */
939 static void kvm_init_irq_routing(KVMState *s)
942 #endif /* !KVM_CAP_IRQ_ROUTING */
944 static int kvm_irqchip_create(KVMState *s)
946 QemuOptsList *list = qemu_find_opts("machine");
947 int ret;
949 if (QTAILQ_EMPTY(&list->head) ||
950 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
951 "kernel_irqchip", false) ||
952 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
953 return 0;
956 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
957 if (ret < 0) {
958 fprintf(stderr, "Create kernel irqchip failed\n");
959 return ret;
962 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
963 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
964 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
966 kvm_kernel_irqchip = true;
968 kvm_init_irq_routing(s);
970 return 0;
973 int kvm_init(void)
975 static const char upgrade_note[] =
976 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
977 "(see http://sourceforge.net/projects/kvm).\n";
978 KVMState *s;
979 const KVMCapabilityInfo *missing_cap;
980 int ret;
981 int i;
983 s = g_malloc0(sizeof(KVMState));
986 * On systems where the kernel can support different base page
987 * sizes, host page size may be different from TARGET_PAGE_SIZE,
988 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
989 * page size for the system though.
991 assert(TARGET_PAGE_SIZE <= getpagesize());
993 #ifdef KVM_CAP_SET_GUEST_DEBUG
994 QTAILQ_INIT(&s->kvm_sw_breakpoints);
995 #endif
996 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
997 s->slots[i].slot = i;
999 s->vmfd = -1;
1000 s->fd = qemu_open("/dev/kvm", O_RDWR);
1001 if (s->fd == -1) {
1002 fprintf(stderr, "Could not access KVM kernel module: %m\n");
1003 ret = -errno;
1004 goto err;
1007 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
1008 if (ret < KVM_API_VERSION) {
1009 if (ret > 0) {
1010 ret = -EINVAL;
1012 fprintf(stderr, "kvm version too old\n");
1013 goto err;
1016 if (ret > KVM_API_VERSION) {
1017 ret = -EINVAL;
1018 fprintf(stderr, "kvm version not supported\n");
1019 goto err;
1022 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
1023 if (s->vmfd < 0) {
1024 #ifdef TARGET_S390X
1025 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1026 "your host kernel command line\n");
1027 #endif
1028 ret = s->vmfd;
1029 goto err;
1032 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1033 if (!missing_cap) {
1034 missing_cap =
1035 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1037 if (missing_cap) {
1038 ret = -EINVAL;
1039 fprintf(stderr, "kvm does not support %s\n%s",
1040 missing_cap->name, upgrade_note);
1041 goto err;
1044 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
1046 s->broken_set_mem_region = 1;
1047 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
1048 if (ret > 0) {
1049 s->broken_set_mem_region = 0;
1052 #ifdef KVM_CAP_VCPU_EVENTS
1053 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1054 #endif
1056 s->robust_singlestep =
1057 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
1059 #ifdef KVM_CAP_DEBUGREGS
1060 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1061 #endif
1063 #ifdef KVM_CAP_XSAVE
1064 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1065 #endif
1067 #ifdef KVM_CAP_XCRS
1068 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1069 #endif
1071 #ifdef KVM_CAP_PIT_STATE2
1072 s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1073 #endif
1075 ret = kvm_arch_init(s);
1076 if (ret < 0) {
1077 goto err;
1080 ret = kvm_irqchip_create(s);
1081 if (ret < 0) {
1082 goto err;
1085 kvm_state = s;
1086 memory_listener_register(&kvm_memory_listener, NULL);
1088 s->many_ioeventfds = kvm_check_many_ioeventfds();
1090 cpu_interrupt_handler = kvm_handle_interrupt;
1092 return 0;
1094 err:
1095 if (s) {
1096 if (s->vmfd >= 0) {
1097 close(s->vmfd);
1099 if (s->fd != -1) {
1100 close(s->fd);
1103 g_free(s);
1105 return ret;
1108 static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1109 uint32_t count)
1111 int i;
1112 uint8_t *ptr = data;
1114 for (i = 0; i < count; i++) {
1115 if (direction == KVM_EXIT_IO_IN) {
1116 switch (size) {
1117 case 1:
1118 stb_p(ptr, cpu_inb(port));
1119 break;
1120 case 2:
1121 stw_p(ptr, cpu_inw(port));
1122 break;
1123 case 4:
1124 stl_p(ptr, cpu_inl(port));
1125 break;
1127 } else {
1128 switch (size) {
1129 case 1:
1130 cpu_outb(port, ldub_p(ptr));
1131 break;
1132 case 2:
1133 cpu_outw(port, lduw_p(ptr));
1134 break;
1135 case 4:
1136 cpu_outl(port, ldl_p(ptr));
1137 break;
1141 ptr += size;
1145 static int kvm_handle_internal_error(CPUArchState *env, struct kvm_run *run)
1147 fprintf(stderr, "KVM internal error.");
1148 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1149 int i;
1151 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
1152 for (i = 0; i < run->internal.ndata; ++i) {
1153 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1154 i, (uint64_t)run->internal.data[i]);
1156 } else {
1157 fprintf(stderr, "\n");
1159 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1160 fprintf(stderr, "emulation failure\n");
1161 if (!kvm_arch_stop_on_emulation_error(env)) {
1162 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1163 return EXCP_INTERRUPT;
1166 /* FIXME: Should trigger a qmp message to let management know
1167 * something went wrong.
1169 return -1;
1172 void kvm_flush_coalesced_mmio_buffer(void)
1174 KVMState *s = kvm_state;
1176 if (s->coalesced_flush_in_progress) {
1177 return;
1180 s->coalesced_flush_in_progress = true;
1182 if (s->coalesced_mmio_ring) {
1183 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
1184 while (ring->first != ring->last) {
1185 struct kvm_coalesced_mmio *ent;
1187 ent = &ring->coalesced_mmio[ring->first];
1189 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
1190 smp_wmb();
1191 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1195 s->coalesced_flush_in_progress = false;
1198 static void do_kvm_cpu_synchronize_state(void *_env)
1200 CPUArchState *env = _env;
1202 if (!env->kvm_vcpu_dirty) {
1203 kvm_arch_get_registers(env);
1204 env->kvm_vcpu_dirty = 1;
1208 void kvm_cpu_synchronize_state(CPUArchState *env)
1210 if (!env->kvm_vcpu_dirty) {
1211 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
1215 void kvm_cpu_synchronize_post_reset(CPUArchState *env)
1217 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1218 env->kvm_vcpu_dirty = 0;
1221 void kvm_cpu_synchronize_post_init(CPUArchState *env)
1223 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1224 env->kvm_vcpu_dirty = 0;
1227 int kvm_cpu_exec(CPUArchState *env)
1229 struct kvm_run *run = env->kvm_run;
1230 int ret, run_ret;
1232 DPRINTF("kvm_cpu_exec()\n");
1234 if (kvm_arch_process_async_events(env)) {
1235 env->exit_request = 0;
1236 return EXCP_HLT;
1239 do {
1240 if (env->kvm_vcpu_dirty) {
1241 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
1242 env->kvm_vcpu_dirty = 0;
1245 kvm_arch_pre_run(env, run);
1246 if (env->exit_request) {
1247 DPRINTF("interrupt exit requested\n");
1249 * KVM requires us to reenter the kernel after IO exits to complete
1250 * instruction emulation. This self-signal will ensure that we
1251 * leave ASAP again.
1253 qemu_cpu_kick_self();
1255 qemu_mutex_unlock_iothread();
1257 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
1259 qemu_mutex_lock_iothread();
1260 kvm_arch_post_run(env, run);
1262 kvm_flush_coalesced_mmio_buffer();
1264 if (run_ret < 0) {
1265 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1266 DPRINTF("io window exit\n");
1267 ret = EXCP_INTERRUPT;
1268 break;
1270 fprintf(stderr, "error: kvm run failed %s\n",
1271 strerror(-run_ret));
1272 abort();
1275 switch (run->exit_reason) {
1276 case KVM_EXIT_IO:
1277 DPRINTF("handle_io\n");
1278 kvm_handle_io(run->io.port,
1279 (uint8_t *)run + run->io.data_offset,
1280 run->io.direction,
1281 run->io.size,
1282 run->io.count);
1283 ret = 0;
1284 break;
1285 case KVM_EXIT_MMIO:
1286 DPRINTF("handle_mmio\n");
1287 cpu_physical_memory_rw(run->mmio.phys_addr,
1288 run->mmio.data,
1289 run->mmio.len,
1290 run->mmio.is_write);
1291 ret = 0;
1292 break;
1293 case KVM_EXIT_IRQ_WINDOW_OPEN:
1294 DPRINTF("irq_window_open\n");
1295 ret = EXCP_INTERRUPT;
1296 break;
1297 case KVM_EXIT_SHUTDOWN:
1298 DPRINTF("shutdown\n");
1299 qemu_system_reset_request();
1300 ret = EXCP_INTERRUPT;
1301 break;
1302 case KVM_EXIT_UNKNOWN:
1303 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1304 (uint64_t)run->hw.hardware_exit_reason);
1305 ret = -1;
1306 break;
1307 case KVM_EXIT_INTERNAL_ERROR:
1308 ret = kvm_handle_internal_error(env, run);
1309 break;
1310 default:
1311 DPRINTF("kvm_arch_handle_exit\n");
1312 ret = kvm_arch_handle_exit(env, run);
1313 break;
1315 } while (ret == 0);
1317 if (ret < 0) {
1318 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1319 vm_stop(RUN_STATE_INTERNAL_ERROR);
1322 env->exit_request = 0;
1323 return ret;
1326 int kvm_ioctl(KVMState *s, int type, ...)
1328 int ret;
1329 void *arg;
1330 va_list ap;
1332 va_start(ap, type);
1333 arg = va_arg(ap, void *);
1334 va_end(ap);
1336 ret = ioctl(s->fd, type, arg);
1337 if (ret == -1) {
1338 ret = -errno;
1340 return ret;
1343 int kvm_vm_ioctl(KVMState *s, int type, ...)
1345 int ret;
1346 void *arg;
1347 va_list ap;
1349 va_start(ap, type);
1350 arg = va_arg(ap, void *);
1351 va_end(ap);
1353 ret = ioctl(s->vmfd, type, arg);
1354 if (ret == -1) {
1355 ret = -errno;
1357 return ret;
1360 int kvm_vcpu_ioctl(CPUArchState *env, int type, ...)
1362 int ret;
1363 void *arg;
1364 va_list ap;
1366 va_start(ap, type);
1367 arg = va_arg(ap, void *);
1368 va_end(ap);
1370 ret = ioctl(env->kvm_fd, type, arg);
1371 if (ret == -1) {
1372 ret = -errno;
1374 return ret;
1377 int kvm_has_sync_mmu(void)
1379 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
1382 int kvm_has_vcpu_events(void)
1384 return kvm_state->vcpu_events;
1387 int kvm_has_robust_singlestep(void)
1389 return kvm_state->robust_singlestep;
1392 int kvm_has_debugregs(void)
1394 return kvm_state->debugregs;
1397 int kvm_has_xsave(void)
1399 return kvm_state->xsave;
1402 int kvm_has_xcrs(void)
1404 return kvm_state->xcrs;
1407 int kvm_has_pit_state2(void)
1409 return kvm_state->pit_state2;
1412 int kvm_has_many_ioeventfds(void)
1414 if (!kvm_enabled()) {
1415 return 0;
1417 return kvm_state->many_ioeventfds;
1420 int kvm_has_gsi_routing(void)
1422 #ifdef KVM_CAP_IRQ_ROUTING
1423 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
1424 #else
1425 return false;
1426 #endif
1429 int kvm_allows_irq0_override(void)
1431 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1434 void kvm_setup_guest_memory(void *start, size_t size)
1436 if (!kvm_has_sync_mmu()) {
1437 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
1439 if (ret) {
1440 perror("qemu_madvise");
1441 fprintf(stderr,
1442 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1443 exit(1);
1448 #ifdef KVM_CAP_SET_GUEST_DEBUG
1449 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
1450 target_ulong pc)
1452 struct kvm_sw_breakpoint *bp;
1454 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
1455 if (bp->pc == pc) {
1456 return bp;
1459 return NULL;
1462 int kvm_sw_breakpoints_active(CPUArchState *env)
1464 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
1467 struct kvm_set_guest_debug_data {
1468 struct kvm_guest_debug dbg;
1469 CPUArchState *env;
1470 int err;
1473 static void kvm_invoke_set_guest_debug(void *data)
1475 struct kvm_set_guest_debug_data *dbg_data = data;
1476 CPUArchState *env = dbg_data->env;
1478 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
1481 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1483 struct kvm_set_guest_debug_data data;
1485 data.dbg.control = reinject_trap;
1487 if (env->singlestep_enabled) {
1488 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1490 kvm_arch_update_guest_debug(env, &data.dbg);
1491 data.env = env;
1493 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
1494 return data.err;
1497 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1498 target_ulong len, int type)
1500 struct kvm_sw_breakpoint *bp;
1501 CPUArchState *env;
1502 int err;
1504 if (type == GDB_BREAKPOINT_SW) {
1505 bp = kvm_find_sw_breakpoint(current_env, addr);
1506 if (bp) {
1507 bp->use_count++;
1508 return 0;
1511 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
1512 if (!bp) {
1513 return -ENOMEM;
1516 bp->pc = addr;
1517 bp->use_count = 1;
1518 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1519 if (err) {
1520 g_free(bp);
1521 return err;
1524 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
1525 bp, entry);
1526 } else {
1527 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1528 if (err) {
1529 return err;
1533 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1534 err = kvm_update_guest_debug(env, 0);
1535 if (err) {
1536 return err;
1539 return 0;
1542 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1543 target_ulong len, int type)
1545 struct kvm_sw_breakpoint *bp;
1546 CPUArchState *env;
1547 int err;
1549 if (type == GDB_BREAKPOINT_SW) {
1550 bp = kvm_find_sw_breakpoint(current_env, addr);
1551 if (!bp) {
1552 return -ENOENT;
1555 if (bp->use_count > 1) {
1556 bp->use_count--;
1557 return 0;
1560 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1561 if (err) {
1562 return err;
1565 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
1566 g_free(bp);
1567 } else {
1568 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1569 if (err) {
1570 return err;
1574 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1575 err = kvm_update_guest_debug(env, 0);
1576 if (err) {
1577 return err;
1580 return 0;
1583 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1585 struct kvm_sw_breakpoint *bp, *next;
1586 KVMState *s = current_env->kvm_state;
1587 CPUArchState *env;
1589 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
1590 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1591 /* Try harder to find a CPU that currently sees the breakpoint. */
1592 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1593 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
1594 break;
1599 kvm_arch_remove_all_hw_breakpoints();
1601 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1602 kvm_update_guest_debug(env, 0);
1606 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1608 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1610 return -EINVAL;
1613 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1614 target_ulong len, int type)
1616 return -EINVAL;
1619 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1620 target_ulong len, int type)
1622 return -EINVAL;
1625 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1628 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1630 int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset)
1632 struct kvm_signal_mask *sigmask;
1633 int r;
1635 if (!sigset) {
1636 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
1639 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
1641 sigmask->len = 8;
1642 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1643 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
1644 g_free(sigmask);
1646 return r;
1649 int kvm_set_ioeventfd_mmio(int fd, uint32_t addr, uint32_t val, bool assign,
1650 uint32_t size)
1652 int ret;
1653 struct kvm_ioeventfd iofd;
1655 iofd.datamatch = val;
1656 iofd.addr = addr;
1657 iofd.len = size;
1658 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1659 iofd.fd = fd;
1661 if (!kvm_enabled()) {
1662 return -ENOSYS;
1665 if (!assign) {
1666 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1669 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1671 if (ret < 0) {
1672 return -errno;
1675 return 0;
1678 int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1680 struct kvm_ioeventfd kick = {
1681 .datamatch = val,
1682 .addr = addr,
1683 .len = 2,
1684 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1685 .fd = fd,
1687 int r;
1688 if (!kvm_enabled()) {
1689 return -ENOSYS;
1691 if (!assign) {
1692 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1694 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1695 if (r < 0) {
1696 return r;
1698 return 0;
1701 int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr)
1703 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1706 int kvm_on_sigbus(int code, void *addr)
1708 return kvm_arch_on_sigbus(code, addr);