qemu-option: qemu_opt_parse(): use error_set()
[qemu/ar7.git] / kvm-all.c
blob489ee53ad283125f8115a2be5b2c0c314f2afeac
1 /*
2 * QEMU KVM support
4 * Copyright IBM, Corp. 2008
5 * Red Hat, Inc. 2008
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Glauber Costa <gcosta@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
19 #include <stdarg.h>
21 #include <linux/kvm.h>
23 #include "qemu-common.h"
24 #include "qemu-barrier.h"
25 #include "sysemu.h"
26 #include "hw/hw.h"
27 #include "hw/msi.h"
28 #include "gdbstub.h"
29 #include "kvm.h"
30 #include "bswap.h"
31 #include "memory.h"
32 #include "exec-memory.h"
34 /* This check must be after config-host.h is included */
35 #ifdef CONFIG_EVENTFD
36 #include <sys/eventfd.h>
37 #endif
39 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
40 #define PAGE_SIZE TARGET_PAGE_SIZE
42 //#define DEBUG_KVM
44 #ifdef DEBUG_KVM
45 #define DPRINTF(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #else
48 #define DPRINTF(fmt, ...) \
49 do { } while (0)
50 #endif
52 #define KVM_MSI_HASHTAB_SIZE 256
54 typedef struct KVMSlot
56 target_phys_addr_t start_addr;
57 ram_addr_t memory_size;
58 void *ram;
59 int slot;
60 int flags;
61 } KVMSlot;
63 typedef struct kvm_dirty_log KVMDirtyLog;
65 typedef struct KVMMSIRoute {
66 struct kvm_irq_routing_entry kroute;
67 QTAILQ_ENTRY(KVMMSIRoute) entry;
68 } KVMMSIRoute;
70 struct KVMState
72 KVMSlot slots[32];
73 int fd;
74 int vmfd;
75 int coalesced_mmio;
76 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
77 bool coalesced_flush_in_progress;
78 int broken_set_mem_region;
79 int migration_log;
80 int vcpu_events;
81 int robust_singlestep;
82 int debugregs;
83 #ifdef KVM_CAP_SET_GUEST_DEBUG
84 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
85 #endif
86 int pit_state2;
87 int xsave, xcrs;
88 int many_ioeventfds;
89 /* The man page (and posix) say ioctl numbers are signed int, but
90 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
91 * unsigned, and treating them as signed here can break things */
92 unsigned irqchip_inject_ioctl;
93 #ifdef KVM_CAP_IRQ_ROUTING
94 struct kvm_irq_routing *irq_routes;
95 int nr_allocated_irq_routes;
96 uint32_t *used_gsi_bitmap;
97 unsigned int gsi_count;
98 QTAILQ_HEAD(msi_hashtab, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE];
99 bool direct_msi;
100 #endif
103 KVMState *kvm_state;
104 bool kvm_kernel_irqchip;
106 static const KVMCapabilityInfo kvm_required_capabilites[] = {
107 KVM_CAP_INFO(USER_MEMORY),
108 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
109 KVM_CAP_LAST_INFO
112 static KVMSlot *kvm_alloc_slot(KVMState *s)
114 int i;
116 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
117 if (s->slots[i].memory_size == 0) {
118 return &s->slots[i];
122 fprintf(stderr, "%s: no free slot available\n", __func__);
123 abort();
126 static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
127 target_phys_addr_t start_addr,
128 target_phys_addr_t end_addr)
130 int i;
132 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
133 KVMSlot *mem = &s->slots[i];
135 if (start_addr == mem->start_addr &&
136 end_addr == mem->start_addr + mem->memory_size) {
137 return mem;
141 return NULL;
145 * Find overlapping slot with lowest start address
147 static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
148 target_phys_addr_t start_addr,
149 target_phys_addr_t end_addr)
151 KVMSlot *found = NULL;
152 int i;
154 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
155 KVMSlot *mem = &s->slots[i];
157 if (mem->memory_size == 0 ||
158 (found && found->start_addr < mem->start_addr)) {
159 continue;
162 if (end_addr > mem->start_addr &&
163 start_addr < mem->start_addr + mem->memory_size) {
164 found = mem;
168 return found;
171 int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
172 target_phys_addr_t *phys_addr)
174 int i;
176 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
177 KVMSlot *mem = &s->slots[i];
179 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
180 *phys_addr = mem->start_addr + (ram - mem->ram);
181 return 1;
185 return 0;
188 static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
190 struct kvm_userspace_memory_region mem;
192 mem.slot = slot->slot;
193 mem.guest_phys_addr = slot->start_addr;
194 mem.memory_size = slot->memory_size;
195 mem.userspace_addr = (unsigned long)slot->ram;
196 mem.flags = slot->flags;
197 if (s->migration_log) {
198 mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
200 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
203 static void kvm_reset_vcpu(void *opaque)
205 CPUArchState *env = opaque;
207 kvm_arch_reset_vcpu(env);
210 int kvm_init_vcpu(CPUArchState *env)
212 KVMState *s = kvm_state;
213 long mmap_size;
214 int ret;
216 DPRINTF("kvm_init_vcpu\n");
218 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
219 if (ret < 0) {
220 DPRINTF("kvm_create_vcpu failed\n");
221 goto err;
224 env->kvm_fd = ret;
225 env->kvm_state = s;
226 env->kvm_vcpu_dirty = 1;
228 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
229 if (mmap_size < 0) {
230 ret = mmap_size;
231 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
232 goto err;
235 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
236 env->kvm_fd, 0);
237 if (env->kvm_run == MAP_FAILED) {
238 ret = -errno;
239 DPRINTF("mmap'ing vcpu state failed\n");
240 goto err;
243 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
244 s->coalesced_mmio_ring =
245 (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
248 ret = kvm_arch_init_vcpu(env);
249 if (ret == 0) {
250 qemu_register_reset(kvm_reset_vcpu, env);
251 kvm_arch_reset_vcpu(env);
253 err:
254 return ret;
258 * dirty pages logging control
261 static int kvm_mem_flags(KVMState *s, bool log_dirty)
263 return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
266 static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
268 KVMState *s = kvm_state;
269 int flags, mask = KVM_MEM_LOG_DIRTY_PAGES;
270 int old_flags;
272 old_flags = mem->flags;
274 flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
275 mem->flags = flags;
277 /* If nothing changed effectively, no need to issue ioctl */
278 if (s->migration_log) {
279 flags |= KVM_MEM_LOG_DIRTY_PAGES;
282 if (flags == old_flags) {
283 return 0;
286 return kvm_set_user_memory_region(s, mem);
289 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
290 ram_addr_t size, bool log_dirty)
292 KVMState *s = kvm_state;
293 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
295 if (mem == NULL) {
296 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
297 TARGET_FMT_plx "\n", __func__, phys_addr,
298 (target_phys_addr_t)(phys_addr + size - 1));
299 return -EINVAL;
301 return kvm_slot_dirty_pages_log_change(mem, log_dirty);
304 static void kvm_log_start(MemoryListener *listener,
305 MemoryRegionSection *section)
307 int r;
309 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
310 section->size, true);
311 if (r < 0) {
312 abort();
316 static void kvm_log_stop(MemoryListener *listener,
317 MemoryRegionSection *section)
319 int r;
321 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
322 section->size, false);
323 if (r < 0) {
324 abort();
328 static int kvm_set_migration_log(int enable)
330 KVMState *s = kvm_state;
331 KVMSlot *mem;
332 int i, err;
334 s->migration_log = enable;
336 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
337 mem = &s->slots[i];
339 if (!mem->memory_size) {
340 continue;
342 if (!!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) == enable) {
343 continue;
345 err = kvm_set_user_memory_region(s, mem);
346 if (err) {
347 return err;
350 return 0;
353 /* get kvm's dirty pages bitmap and update qemu's */
354 static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
355 unsigned long *bitmap)
357 unsigned int i, j;
358 unsigned long page_number, c;
359 target_phys_addr_t addr, addr1;
360 unsigned int len = ((section->size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
361 unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
364 * bitmap-traveling is faster than memory-traveling (for addr...)
365 * especially when most of the memory is not dirty.
367 for (i = 0; i < len; i++) {
368 if (bitmap[i] != 0) {
369 c = leul_to_cpu(bitmap[i]);
370 do {
371 j = ffsl(c) - 1;
372 c &= ~(1ul << j);
373 page_number = (i * HOST_LONG_BITS + j) * hpratio;
374 addr1 = page_number * TARGET_PAGE_SIZE;
375 addr = section->offset_within_region + addr1;
376 memory_region_set_dirty(section->mr, addr,
377 TARGET_PAGE_SIZE * hpratio);
378 } while (c != 0);
381 return 0;
384 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
387 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
388 * This function updates qemu's dirty bitmap using
389 * memory_region_set_dirty(). This means all bits are set
390 * to dirty.
392 * @start_add: start of logged region.
393 * @end_addr: end of logged region.
395 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
397 KVMState *s = kvm_state;
398 unsigned long size, allocated_size = 0;
399 KVMDirtyLog d;
400 KVMSlot *mem;
401 int ret = 0;
402 target_phys_addr_t start_addr = section->offset_within_address_space;
403 target_phys_addr_t end_addr = start_addr + section->size;
405 d.dirty_bitmap = NULL;
406 while (start_addr < end_addr) {
407 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
408 if (mem == NULL) {
409 break;
412 /* XXX bad kernel interface alert
413 * For dirty bitmap, kernel allocates array of size aligned to
414 * bits-per-long. But for case when the kernel is 64bits and
415 * the userspace is 32bits, userspace can't align to the same
416 * bits-per-long, since sizeof(long) is different between kernel
417 * and user space. This way, userspace will provide buffer which
418 * may be 4 bytes less than the kernel will use, resulting in
419 * userspace memory corruption (which is not detectable by valgrind
420 * too, in most cases).
421 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
422 * a hope that sizeof(long) wont become >8 any time soon.
424 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
425 /*HOST_LONG_BITS*/ 64) / 8;
426 if (!d.dirty_bitmap) {
427 d.dirty_bitmap = g_malloc(size);
428 } else if (size > allocated_size) {
429 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
431 allocated_size = size;
432 memset(d.dirty_bitmap, 0, allocated_size);
434 d.slot = mem->slot;
436 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
437 DPRINTF("ioctl failed %d\n", errno);
438 ret = -1;
439 break;
442 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
443 start_addr = mem->start_addr + mem->memory_size;
445 g_free(d.dirty_bitmap);
447 return ret;
450 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
452 int ret = -ENOSYS;
453 KVMState *s = kvm_state;
455 if (s->coalesced_mmio) {
456 struct kvm_coalesced_mmio_zone zone;
458 zone.addr = start;
459 zone.size = size;
460 zone.pad = 0;
462 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
465 return ret;
468 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
470 int ret = -ENOSYS;
471 KVMState *s = kvm_state;
473 if (s->coalesced_mmio) {
474 struct kvm_coalesced_mmio_zone zone;
476 zone.addr = start;
477 zone.size = size;
478 zone.pad = 0;
480 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
483 return ret;
486 int kvm_check_extension(KVMState *s, unsigned int extension)
488 int ret;
490 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
491 if (ret < 0) {
492 ret = 0;
495 return ret;
498 static int kvm_check_many_ioeventfds(void)
500 /* Userspace can use ioeventfd for io notification. This requires a host
501 * that supports eventfd(2) and an I/O thread; since eventfd does not
502 * support SIGIO it cannot interrupt the vcpu.
504 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
505 * can avoid creating too many ioeventfds.
507 #if defined(CONFIG_EVENTFD)
508 int ioeventfds[7];
509 int i, ret = 0;
510 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
511 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
512 if (ioeventfds[i] < 0) {
513 break;
515 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
516 if (ret < 0) {
517 close(ioeventfds[i]);
518 break;
522 /* Decide whether many devices are supported or not */
523 ret = i == ARRAY_SIZE(ioeventfds);
525 while (i-- > 0) {
526 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
527 close(ioeventfds[i]);
529 return ret;
530 #else
531 return 0;
532 #endif
535 static const KVMCapabilityInfo *
536 kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
538 while (list->name) {
539 if (!kvm_check_extension(s, list->value)) {
540 return list;
542 list++;
544 return NULL;
547 static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
549 KVMState *s = kvm_state;
550 KVMSlot *mem, old;
551 int err;
552 MemoryRegion *mr = section->mr;
553 bool log_dirty = memory_region_is_logging(mr);
554 target_phys_addr_t start_addr = section->offset_within_address_space;
555 ram_addr_t size = section->size;
556 void *ram = NULL;
557 unsigned delta;
559 /* kvm works in page size chunks, but the function may be called
560 with sub-page size and unaligned start address. */
561 delta = TARGET_PAGE_ALIGN(size) - size;
562 if (delta > size) {
563 return;
565 start_addr += delta;
566 size -= delta;
567 size &= TARGET_PAGE_MASK;
568 if (!size || (start_addr & ~TARGET_PAGE_MASK)) {
569 return;
572 if (!memory_region_is_ram(mr)) {
573 return;
576 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
578 while (1) {
579 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
580 if (!mem) {
581 break;
584 if (add && start_addr >= mem->start_addr &&
585 (start_addr + size <= mem->start_addr + mem->memory_size) &&
586 (ram - start_addr == mem->ram - mem->start_addr)) {
587 /* The new slot fits into the existing one and comes with
588 * identical parameters - update flags and done. */
589 kvm_slot_dirty_pages_log_change(mem, log_dirty);
590 return;
593 old = *mem;
595 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
596 kvm_physical_sync_dirty_bitmap(section);
599 /* unregister the overlapping slot */
600 mem->memory_size = 0;
601 err = kvm_set_user_memory_region(s, mem);
602 if (err) {
603 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
604 __func__, strerror(-err));
605 abort();
608 /* Workaround for older KVM versions: we can't join slots, even not by
609 * unregistering the previous ones and then registering the larger
610 * slot. We have to maintain the existing fragmentation. Sigh.
612 * This workaround assumes that the new slot starts at the same
613 * address as the first existing one. If not or if some overlapping
614 * slot comes around later, we will fail (not seen in practice so far)
615 * - and actually require a recent KVM version. */
616 if (s->broken_set_mem_region &&
617 old.start_addr == start_addr && old.memory_size < size && add) {
618 mem = kvm_alloc_slot(s);
619 mem->memory_size = old.memory_size;
620 mem->start_addr = old.start_addr;
621 mem->ram = old.ram;
622 mem->flags = kvm_mem_flags(s, log_dirty);
624 err = kvm_set_user_memory_region(s, mem);
625 if (err) {
626 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
627 strerror(-err));
628 abort();
631 start_addr += old.memory_size;
632 ram += old.memory_size;
633 size -= old.memory_size;
634 continue;
637 /* register prefix slot */
638 if (old.start_addr < start_addr) {
639 mem = kvm_alloc_slot(s);
640 mem->memory_size = start_addr - old.start_addr;
641 mem->start_addr = old.start_addr;
642 mem->ram = old.ram;
643 mem->flags = kvm_mem_flags(s, log_dirty);
645 err = kvm_set_user_memory_region(s, mem);
646 if (err) {
647 fprintf(stderr, "%s: error registering prefix slot: %s\n",
648 __func__, strerror(-err));
649 #ifdef TARGET_PPC
650 fprintf(stderr, "%s: This is probably because your kernel's " \
651 "PAGE_SIZE is too big. Please try to use 4k " \
652 "PAGE_SIZE!\n", __func__);
653 #endif
654 abort();
658 /* register suffix slot */
659 if (old.start_addr + old.memory_size > start_addr + size) {
660 ram_addr_t size_delta;
662 mem = kvm_alloc_slot(s);
663 mem->start_addr = start_addr + size;
664 size_delta = mem->start_addr - old.start_addr;
665 mem->memory_size = old.memory_size - size_delta;
666 mem->ram = old.ram + size_delta;
667 mem->flags = kvm_mem_flags(s, log_dirty);
669 err = kvm_set_user_memory_region(s, mem);
670 if (err) {
671 fprintf(stderr, "%s: error registering suffix slot: %s\n",
672 __func__, strerror(-err));
673 abort();
678 /* in case the KVM bug workaround already "consumed" the new slot */
679 if (!size) {
680 return;
682 if (!add) {
683 return;
685 mem = kvm_alloc_slot(s);
686 mem->memory_size = size;
687 mem->start_addr = start_addr;
688 mem->ram = ram;
689 mem->flags = kvm_mem_flags(s, log_dirty);
691 err = kvm_set_user_memory_region(s, mem);
692 if (err) {
693 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
694 strerror(-err));
695 abort();
699 static void kvm_begin(MemoryListener *listener)
703 static void kvm_commit(MemoryListener *listener)
707 static void kvm_region_add(MemoryListener *listener,
708 MemoryRegionSection *section)
710 kvm_set_phys_mem(section, true);
713 static void kvm_region_del(MemoryListener *listener,
714 MemoryRegionSection *section)
716 kvm_set_phys_mem(section, false);
719 static void kvm_region_nop(MemoryListener *listener,
720 MemoryRegionSection *section)
724 static void kvm_log_sync(MemoryListener *listener,
725 MemoryRegionSection *section)
727 int r;
729 r = kvm_physical_sync_dirty_bitmap(section);
730 if (r < 0) {
731 abort();
735 static void kvm_log_global_start(struct MemoryListener *listener)
737 int r;
739 r = kvm_set_migration_log(1);
740 assert(r >= 0);
743 static void kvm_log_global_stop(struct MemoryListener *listener)
745 int r;
747 r = kvm_set_migration_log(0);
748 assert(r >= 0);
751 static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
752 bool match_data, uint64_t data, int fd)
754 int r;
756 assert(match_data && section->size <= 8);
758 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
759 data, true, section->size);
760 if (r < 0) {
761 abort();
765 static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
766 bool match_data, uint64_t data, int fd)
768 int r;
770 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
771 data, false, section->size);
772 if (r < 0) {
773 abort();
777 static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
778 bool match_data, uint64_t data, int fd)
780 int r;
782 assert(match_data && section->size == 2);
784 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
785 data, true);
786 if (r < 0) {
787 abort();
791 static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
792 bool match_data, uint64_t data, int fd)
795 int r;
797 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
798 data, false);
799 if (r < 0) {
800 abort();
804 static void kvm_eventfd_add(MemoryListener *listener,
805 MemoryRegionSection *section,
806 bool match_data, uint64_t data, int fd)
808 if (section->address_space == get_system_memory()) {
809 kvm_mem_ioeventfd_add(section, match_data, data, fd);
810 } else {
811 kvm_io_ioeventfd_add(section, match_data, data, fd);
815 static void kvm_eventfd_del(MemoryListener *listener,
816 MemoryRegionSection *section,
817 bool match_data, uint64_t data, int fd)
819 if (section->address_space == get_system_memory()) {
820 kvm_mem_ioeventfd_del(section, match_data, data, fd);
821 } else {
822 kvm_io_ioeventfd_del(section, match_data, data, fd);
826 static MemoryListener kvm_memory_listener = {
827 .begin = kvm_begin,
828 .commit = kvm_commit,
829 .region_add = kvm_region_add,
830 .region_del = kvm_region_del,
831 .region_nop = kvm_region_nop,
832 .log_start = kvm_log_start,
833 .log_stop = kvm_log_stop,
834 .log_sync = kvm_log_sync,
835 .log_global_start = kvm_log_global_start,
836 .log_global_stop = kvm_log_global_stop,
837 .eventfd_add = kvm_eventfd_add,
838 .eventfd_del = kvm_eventfd_del,
839 .priority = 10,
842 static void kvm_handle_interrupt(CPUArchState *env, int mask)
844 env->interrupt_request |= mask;
846 if (!qemu_cpu_is_self(env)) {
847 qemu_cpu_kick(env);
851 int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
853 struct kvm_irq_level event;
854 int ret;
856 assert(kvm_irqchip_in_kernel());
858 event.level = level;
859 event.irq = irq;
860 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
861 if (ret < 0) {
862 perror("kvm_set_irqchip_line");
863 abort();
866 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
869 #ifdef KVM_CAP_IRQ_ROUTING
870 static void set_gsi(KVMState *s, unsigned int gsi)
872 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
875 static void clear_gsi(KVMState *s, unsigned int gsi)
877 s->used_gsi_bitmap[gsi / 32] &= ~(1U << (gsi % 32));
880 static void kvm_init_irq_routing(KVMState *s)
882 int gsi_count, i;
884 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
885 if (gsi_count > 0) {
886 unsigned int gsi_bits, i;
888 /* Round up so we can search ints using ffs */
889 gsi_bits = ALIGN(gsi_count, 32);
890 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
891 s->gsi_count = gsi_count;
893 /* Mark any over-allocated bits as already in use */
894 for (i = gsi_count; i < gsi_bits; i++) {
895 set_gsi(s, i);
899 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
900 s->nr_allocated_irq_routes = 0;
902 if (!s->direct_msi) {
903 for (i = 0; i < KVM_MSI_HASHTAB_SIZE; i++) {
904 QTAILQ_INIT(&s->msi_hashtab[i]);
908 kvm_arch_init_irq_routing(s);
911 static void kvm_irqchip_commit_routes(KVMState *s)
913 int ret;
915 s->irq_routes->flags = 0;
916 ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
917 assert(ret == 0);
920 static void kvm_add_routing_entry(KVMState *s,
921 struct kvm_irq_routing_entry *entry)
923 struct kvm_irq_routing_entry *new;
924 int n, size;
926 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
927 n = s->nr_allocated_irq_routes * 2;
928 if (n < 64) {
929 n = 64;
931 size = sizeof(struct kvm_irq_routing);
932 size += n * sizeof(*new);
933 s->irq_routes = g_realloc(s->irq_routes, size);
934 s->nr_allocated_irq_routes = n;
936 n = s->irq_routes->nr++;
937 new = &s->irq_routes->entries[n];
938 memset(new, 0, sizeof(*new));
939 new->gsi = entry->gsi;
940 new->type = entry->type;
941 new->flags = entry->flags;
942 new->u = entry->u;
944 set_gsi(s, entry->gsi);
946 kvm_irqchip_commit_routes(s);
949 void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin)
951 struct kvm_irq_routing_entry e;
953 assert(pin < s->gsi_count);
955 e.gsi = irq;
956 e.type = KVM_IRQ_ROUTING_IRQCHIP;
957 e.flags = 0;
958 e.u.irqchip.irqchip = irqchip;
959 e.u.irqchip.pin = pin;
960 kvm_add_routing_entry(s, &e);
963 void kvm_irqchip_release_virq(KVMState *s, int virq)
965 struct kvm_irq_routing_entry *e;
966 int i;
968 for (i = 0; i < s->irq_routes->nr; i++) {
969 e = &s->irq_routes->entries[i];
970 if (e->gsi == virq) {
971 s->irq_routes->nr--;
972 *e = s->irq_routes->entries[s->irq_routes->nr];
975 clear_gsi(s, virq);
977 kvm_irqchip_commit_routes(s);
980 static unsigned int kvm_hash_msi(uint32_t data)
982 /* This is optimized for IA32 MSI layout. However, no other arch shall
983 * repeat the mistake of not providing a direct MSI injection API. */
984 return data & 0xff;
987 static void kvm_flush_dynamic_msi_routes(KVMState *s)
989 KVMMSIRoute *route, *next;
990 unsigned int hash;
992 for (hash = 0; hash < KVM_MSI_HASHTAB_SIZE; hash++) {
993 QTAILQ_FOREACH_SAFE(route, &s->msi_hashtab[hash], entry, next) {
994 kvm_irqchip_release_virq(s, route->kroute.gsi);
995 QTAILQ_REMOVE(&s->msi_hashtab[hash], route, entry);
996 g_free(route);
1001 static int kvm_irqchip_get_virq(KVMState *s)
1003 uint32_t *word = s->used_gsi_bitmap;
1004 int max_words = ALIGN(s->gsi_count, 32) / 32;
1005 int i, bit;
1006 bool retry = true;
1008 again:
1009 /* Return the lowest unused GSI in the bitmap */
1010 for (i = 0; i < max_words; i++) {
1011 bit = ffs(~word[i]);
1012 if (!bit) {
1013 continue;
1016 return bit - 1 + i * 32;
1018 if (!s->direct_msi && retry) {
1019 retry = false;
1020 kvm_flush_dynamic_msi_routes(s);
1021 goto again;
1023 return -ENOSPC;
1027 static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
1029 unsigned int hash = kvm_hash_msi(msg.data);
1030 KVMMSIRoute *route;
1032 QTAILQ_FOREACH(route, &s->msi_hashtab[hash], entry) {
1033 if (route->kroute.u.msi.address_lo == (uint32_t)msg.address &&
1034 route->kroute.u.msi.address_hi == (msg.address >> 32) &&
1035 route->kroute.u.msi.data == msg.data) {
1036 return route;
1039 return NULL;
1042 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1044 struct kvm_msi msi;
1045 KVMMSIRoute *route;
1047 if (s->direct_msi) {
1048 msi.address_lo = (uint32_t)msg.address;
1049 msi.address_hi = msg.address >> 32;
1050 msi.data = msg.data;
1051 msi.flags = 0;
1052 memset(msi.pad, 0, sizeof(msi.pad));
1054 return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
1057 route = kvm_lookup_msi_route(s, msg);
1058 if (!route) {
1059 int virq;
1061 virq = kvm_irqchip_get_virq(s);
1062 if (virq < 0) {
1063 return virq;
1066 route = g_malloc(sizeof(KVMMSIRoute));
1067 route->kroute.gsi = virq;
1068 route->kroute.type = KVM_IRQ_ROUTING_MSI;
1069 route->kroute.flags = 0;
1070 route->kroute.u.msi.address_lo = (uint32_t)msg.address;
1071 route->kroute.u.msi.address_hi = msg.address >> 32;
1072 route->kroute.u.msi.data = msg.data;
1074 kvm_add_routing_entry(s, &route->kroute);
1076 QTAILQ_INSERT_TAIL(&s->msi_hashtab[kvm_hash_msi(msg.data)], route,
1077 entry);
1080 assert(route->kroute.type == KVM_IRQ_ROUTING_MSI);
1082 return kvm_irqchip_set_irq(s, route->kroute.gsi, 1);
1085 int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1087 struct kvm_irq_routing_entry kroute;
1088 int virq;
1090 if (!kvm_irqchip_in_kernel()) {
1091 return -ENOSYS;
1094 virq = kvm_irqchip_get_virq(s);
1095 if (virq < 0) {
1096 return virq;
1099 kroute.gsi = virq;
1100 kroute.type = KVM_IRQ_ROUTING_MSI;
1101 kroute.flags = 0;
1102 kroute.u.msi.address_lo = (uint32_t)msg.address;
1103 kroute.u.msi.address_hi = msg.address >> 32;
1104 kroute.u.msi.data = msg.data;
1106 kvm_add_routing_entry(s, &kroute);
1108 return virq;
1111 static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1113 struct kvm_irqfd irqfd = {
1114 .fd = fd,
1115 .gsi = virq,
1116 .flags = assign ? 0 : KVM_IRQFD_FLAG_DEASSIGN,
1119 if (!kvm_irqchip_in_kernel()) {
1120 return -ENOSYS;
1123 return kvm_vm_ioctl(s, KVM_IRQFD, &irqfd);
1126 #else /* !KVM_CAP_IRQ_ROUTING */
1128 static void kvm_init_irq_routing(KVMState *s)
1132 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1134 abort();
1137 int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1139 abort();
1142 static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1144 abort();
1146 #endif /* !KVM_CAP_IRQ_ROUTING */
1148 int kvm_irqchip_add_irqfd(KVMState *s, int fd, int virq)
1150 return kvm_irqchip_assign_irqfd(s, fd, virq, true);
1153 int kvm_irqchip_remove_irqfd(KVMState *s, int fd, int virq)
1155 return kvm_irqchip_assign_irqfd(s, fd, virq, false);
1158 static int kvm_irqchip_create(KVMState *s)
1160 QemuOptsList *list = qemu_find_opts("machine");
1161 int ret;
1163 if (QTAILQ_EMPTY(&list->head) ||
1164 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
1165 "kernel_irqchip", true) ||
1166 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
1167 return 0;
1170 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
1171 if (ret < 0) {
1172 fprintf(stderr, "Create kernel irqchip failed\n");
1173 return ret;
1176 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
1177 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
1178 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
1180 kvm_kernel_irqchip = true;
1182 kvm_init_irq_routing(s);
1184 return 0;
1187 int kvm_init(void)
1189 static const char upgrade_note[] =
1190 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1191 "(see http://sourceforge.net/projects/kvm).\n";
1192 KVMState *s;
1193 const KVMCapabilityInfo *missing_cap;
1194 int ret;
1195 int i;
1197 s = g_malloc0(sizeof(KVMState));
1200 * On systems where the kernel can support different base page
1201 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1202 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1203 * page size for the system though.
1205 assert(TARGET_PAGE_SIZE <= getpagesize());
1207 #ifdef KVM_CAP_SET_GUEST_DEBUG
1208 QTAILQ_INIT(&s->kvm_sw_breakpoints);
1209 #endif
1210 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
1211 s->slots[i].slot = i;
1213 s->vmfd = -1;
1214 s->fd = qemu_open("/dev/kvm", O_RDWR);
1215 if (s->fd == -1) {
1216 fprintf(stderr, "Could not access KVM kernel module: %m\n");
1217 ret = -errno;
1218 goto err;
1221 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
1222 if (ret < KVM_API_VERSION) {
1223 if (ret > 0) {
1224 ret = -EINVAL;
1226 fprintf(stderr, "kvm version too old\n");
1227 goto err;
1230 if (ret > KVM_API_VERSION) {
1231 ret = -EINVAL;
1232 fprintf(stderr, "kvm version not supported\n");
1233 goto err;
1236 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
1237 if (s->vmfd < 0) {
1238 #ifdef TARGET_S390X
1239 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1240 "your host kernel command line\n");
1241 #endif
1242 ret = s->vmfd;
1243 goto err;
1246 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1247 if (!missing_cap) {
1248 missing_cap =
1249 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1251 if (missing_cap) {
1252 ret = -EINVAL;
1253 fprintf(stderr, "kvm does not support %s\n%s",
1254 missing_cap->name, upgrade_note);
1255 goto err;
1258 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
1260 s->broken_set_mem_region = 1;
1261 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
1262 if (ret > 0) {
1263 s->broken_set_mem_region = 0;
1266 #ifdef KVM_CAP_VCPU_EVENTS
1267 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1268 #endif
1270 s->robust_singlestep =
1271 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
1273 #ifdef KVM_CAP_DEBUGREGS
1274 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1275 #endif
1277 #ifdef KVM_CAP_XSAVE
1278 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1279 #endif
1281 #ifdef KVM_CAP_XCRS
1282 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1283 #endif
1285 #ifdef KVM_CAP_PIT_STATE2
1286 s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1287 #endif
1289 s->direct_msi = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0);
1291 ret = kvm_arch_init(s);
1292 if (ret < 0) {
1293 goto err;
1296 ret = kvm_irqchip_create(s);
1297 if (ret < 0) {
1298 goto err;
1301 kvm_state = s;
1302 memory_listener_register(&kvm_memory_listener, NULL);
1304 s->many_ioeventfds = kvm_check_many_ioeventfds();
1306 cpu_interrupt_handler = kvm_handle_interrupt;
1308 return 0;
1310 err:
1311 if (s) {
1312 if (s->vmfd >= 0) {
1313 close(s->vmfd);
1315 if (s->fd != -1) {
1316 close(s->fd);
1319 g_free(s);
1321 return ret;
1324 static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1325 uint32_t count)
1327 int i;
1328 uint8_t *ptr = data;
1330 for (i = 0; i < count; i++) {
1331 if (direction == KVM_EXIT_IO_IN) {
1332 switch (size) {
1333 case 1:
1334 stb_p(ptr, cpu_inb(port));
1335 break;
1336 case 2:
1337 stw_p(ptr, cpu_inw(port));
1338 break;
1339 case 4:
1340 stl_p(ptr, cpu_inl(port));
1341 break;
1343 } else {
1344 switch (size) {
1345 case 1:
1346 cpu_outb(port, ldub_p(ptr));
1347 break;
1348 case 2:
1349 cpu_outw(port, lduw_p(ptr));
1350 break;
1351 case 4:
1352 cpu_outl(port, ldl_p(ptr));
1353 break;
1357 ptr += size;
1361 static int kvm_handle_internal_error(CPUArchState *env, struct kvm_run *run)
1363 fprintf(stderr, "KVM internal error.");
1364 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1365 int i;
1367 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
1368 for (i = 0; i < run->internal.ndata; ++i) {
1369 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1370 i, (uint64_t)run->internal.data[i]);
1372 } else {
1373 fprintf(stderr, "\n");
1375 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1376 fprintf(stderr, "emulation failure\n");
1377 if (!kvm_arch_stop_on_emulation_error(env)) {
1378 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1379 return EXCP_INTERRUPT;
1382 /* FIXME: Should trigger a qmp message to let management know
1383 * something went wrong.
1385 return -1;
1388 void kvm_flush_coalesced_mmio_buffer(void)
1390 KVMState *s = kvm_state;
1392 if (s->coalesced_flush_in_progress) {
1393 return;
1396 s->coalesced_flush_in_progress = true;
1398 if (s->coalesced_mmio_ring) {
1399 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
1400 while (ring->first != ring->last) {
1401 struct kvm_coalesced_mmio *ent;
1403 ent = &ring->coalesced_mmio[ring->first];
1405 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
1406 smp_wmb();
1407 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1411 s->coalesced_flush_in_progress = false;
1414 static void do_kvm_cpu_synchronize_state(void *_env)
1416 CPUArchState *env = _env;
1418 if (!env->kvm_vcpu_dirty) {
1419 kvm_arch_get_registers(env);
1420 env->kvm_vcpu_dirty = 1;
1424 void kvm_cpu_synchronize_state(CPUArchState *env)
1426 if (!env->kvm_vcpu_dirty) {
1427 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
1431 void kvm_cpu_synchronize_post_reset(CPUArchState *env)
1433 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1434 env->kvm_vcpu_dirty = 0;
1437 void kvm_cpu_synchronize_post_init(CPUArchState *env)
1439 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1440 env->kvm_vcpu_dirty = 0;
1443 int kvm_cpu_exec(CPUArchState *env)
1445 struct kvm_run *run = env->kvm_run;
1446 int ret, run_ret;
1448 DPRINTF("kvm_cpu_exec()\n");
1450 if (kvm_arch_process_async_events(env)) {
1451 env->exit_request = 0;
1452 return EXCP_HLT;
1455 do {
1456 if (env->kvm_vcpu_dirty) {
1457 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
1458 env->kvm_vcpu_dirty = 0;
1461 kvm_arch_pre_run(env, run);
1462 if (env->exit_request) {
1463 DPRINTF("interrupt exit requested\n");
1465 * KVM requires us to reenter the kernel after IO exits to complete
1466 * instruction emulation. This self-signal will ensure that we
1467 * leave ASAP again.
1469 qemu_cpu_kick_self();
1471 qemu_mutex_unlock_iothread();
1473 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
1475 qemu_mutex_lock_iothread();
1476 kvm_arch_post_run(env, run);
1478 kvm_flush_coalesced_mmio_buffer();
1480 if (run_ret < 0) {
1481 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1482 DPRINTF("io window exit\n");
1483 ret = EXCP_INTERRUPT;
1484 break;
1486 fprintf(stderr, "error: kvm run failed %s\n",
1487 strerror(-run_ret));
1488 abort();
1491 switch (run->exit_reason) {
1492 case KVM_EXIT_IO:
1493 DPRINTF("handle_io\n");
1494 kvm_handle_io(run->io.port,
1495 (uint8_t *)run + run->io.data_offset,
1496 run->io.direction,
1497 run->io.size,
1498 run->io.count);
1499 ret = 0;
1500 break;
1501 case KVM_EXIT_MMIO:
1502 DPRINTF("handle_mmio\n");
1503 cpu_physical_memory_rw(run->mmio.phys_addr,
1504 run->mmio.data,
1505 run->mmio.len,
1506 run->mmio.is_write);
1507 ret = 0;
1508 break;
1509 case KVM_EXIT_IRQ_WINDOW_OPEN:
1510 DPRINTF("irq_window_open\n");
1511 ret = EXCP_INTERRUPT;
1512 break;
1513 case KVM_EXIT_SHUTDOWN:
1514 DPRINTF("shutdown\n");
1515 qemu_system_reset_request();
1516 ret = EXCP_INTERRUPT;
1517 break;
1518 case KVM_EXIT_UNKNOWN:
1519 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1520 (uint64_t)run->hw.hardware_exit_reason);
1521 ret = -1;
1522 break;
1523 case KVM_EXIT_INTERNAL_ERROR:
1524 ret = kvm_handle_internal_error(env, run);
1525 break;
1526 default:
1527 DPRINTF("kvm_arch_handle_exit\n");
1528 ret = kvm_arch_handle_exit(env, run);
1529 break;
1531 } while (ret == 0);
1533 if (ret < 0) {
1534 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1535 vm_stop(RUN_STATE_INTERNAL_ERROR);
1538 env->exit_request = 0;
1539 return ret;
1542 int kvm_ioctl(KVMState *s, int type, ...)
1544 int ret;
1545 void *arg;
1546 va_list ap;
1548 va_start(ap, type);
1549 arg = va_arg(ap, void *);
1550 va_end(ap);
1552 ret = ioctl(s->fd, type, arg);
1553 if (ret == -1) {
1554 ret = -errno;
1556 return ret;
1559 int kvm_vm_ioctl(KVMState *s, int type, ...)
1561 int ret;
1562 void *arg;
1563 va_list ap;
1565 va_start(ap, type);
1566 arg = va_arg(ap, void *);
1567 va_end(ap);
1569 ret = ioctl(s->vmfd, type, arg);
1570 if (ret == -1) {
1571 ret = -errno;
1573 return ret;
1576 int kvm_vcpu_ioctl(CPUArchState *env, int type, ...)
1578 int ret;
1579 void *arg;
1580 va_list ap;
1582 va_start(ap, type);
1583 arg = va_arg(ap, void *);
1584 va_end(ap);
1586 ret = ioctl(env->kvm_fd, type, arg);
1587 if (ret == -1) {
1588 ret = -errno;
1590 return ret;
1593 int kvm_has_sync_mmu(void)
1595 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
1598 int kvm_has_vcpu_events(void)
1600 return kvm_state->vcpu_events;
1603 int kvm_has_robust_singlestep(void)
1605 return kvm_state->robust_singlestep;
1608 int kvm_has_debugregs(void)
1610 return kvm_state->debugregs;
1613 int kvm_has_xsave(void)
1615 return kvm_state->xsave;
1618 int kvm_has_xcrs(void)
1620 return kvm_state->xcrs;
1623 int kvm_has_pit_state2(void)
1625 return kvm_state->pit_state2;
1628 int kvm_has_many_ioeventfds(void)
1630 if (!kvm_enabled()) {
1631 return 0;
1633 return kvm_state->many_ioeventfds;
1636 int kvm_has_gsi_routing(void)
1638 #ifdef KVM_CAP_IRQ_ROUTING
1639 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
1640 #else
1641 return false;
1642 #endif
1645 int kvm_allows_irq0_override(void)
1647 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1650 void kvm_setup_guest_memory(void *start, size_t size)
1652 if (!kvm_has_sync_mmu()) {
1653 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
1655 if (ret) {
1656 perror("qemu_madvise");
1657 fprintf(stderr,
1658 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1659 exit(1);
1664 #ifdef KVM_CAP_SET_GUEST_DEBUG
1665 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
1666 target_ulong pc)
1668 struct kvm_sw_breakpoint *bp;
1670 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
1671 if (bp->pc == pc) {
1672 return bp;
1675 return NULL;
1678 int kvm_sw_breakpoints_active(CPUArchState *env)
1680 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
1683 struct kvm_set_guest_debug_data {
1684 struct kvm_guest_debug dbg;
1685 CPUArchState *env;
1686 int err;
1689 static void kvm_invoke_set_guest_debug(void *data)
1691 struct kvm_set_guest_debug_data *dbg_data = data;
1692 CPUArchState *env = dbg_data->env;
1694 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
1697 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1699 struct kvm_set_guest_debug_data data;
1701 data.dbg.control = reinject_trap;
1703 if (env->singlestep_enabled) {
1704 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1706 kvm_arch_update_guest_debug(env, &data.dbg);
1707 data.env = env;
1709 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
1710 return data.err;
1713 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1714 target_ulong len, int type)
1716 struct kvm_sw_breakpoint *bp;
1717 CPUArchState *env;
1718 int err;
1720 if (type == GDB_BREAKPOINT_SW) {
1721 bp = kvm_find_sw_breakpoint(current_env, addr);
1722 if (bp) {
1723 bp->use_count++;
1724 return 0;
1727 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
1728 if (!bp) {
1729 return -ENOMEM;
1732 bp->pc = addr;
1733 bp->use_count = 1;
1734 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1735 if (err) {
1736 g_free(bp);
1737 return err;
1740 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
1741 bp, entry);
1742 } else {
1743 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1744 if (err) {
1745 return err;
1749 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1750 err = kvm_update_guest_debug(env, 0);
1751 if (err) {
1752 return err;
1755 return 0;
1758 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1759 target_ulong len, int type)
1761 struct kvm_sw_breakpoint *bp;
1762 CPUArchState *env;
1763 int err;
1765 if (type == GDB_BREAKPOINT_SW) {
1766 bp = kvm_find_sw_breakpoint(current_env, addr);
1767 if (!bp) {
1768 return -ENOENT;
1771 if (bp->use_count > 1) {
1772 bp->use_count--;
1773 return 0;
1776 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1777 if (err) {
1778 return err;
1781 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
1782 g_free(bp);
1783 } else {
1784 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1785 if (err) {
1786 return err;
1790 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1791 err = kvm_update_guest_debug(env, 0);
1792 if (err) {
1793 return err;
1796 return 0;
1799 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1801 struct kvm_sw_breakpoint *bp, *next;
1802 KVMState *s = current_env->kvm_state;
1803 CPUArchState *env;
1805 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
1806 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1807 /* Try harder to find a CPU that currently sees the breakpoint. */
1808 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1809 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
1810 break;
1815 kvm_arch_remove_all_hw_breakpoints();
1817 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1818 kvm_update_guest_debug(env, 0);
1822 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1824 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1826 return -EINVAL;
1829 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1830 target_ulong len, int type)
1832 return -EINVAL;
1835 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1836 target_ulong len, int type)
1838 return -EINVAL;
1841 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1844 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1846 int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset)
1848 struct kvm_signal_mask *sigmask;
1849 int r;
1851 if (!sigset) {
1852 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
1855 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
1857 sigmask->len = 8;
1858 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1859 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
1860 g_free(sigmask);
1862 return r;
1865 int kvm_set_ioeventfd_mmio(int fd, uint32_t addr, uint32_t val, bool assign,
1866 uint32_t size)
1868 int ret;
1869 struct kvm_ioeventfd iofd;
1871 iofd.datamatch = val;
1872 iofd.addr = addr;
1873 iofd.len = size;
1874 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1875 iofd.fd = fd;
1877 if (!kvm_enabled()) {
1878 return -ENOSYS;
1881 if (!assign) {
1882 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1885 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1887 if (ret < 0) {
1888 return -errno;
1891 return 0;
1894 int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1896 struct kvm_ioeventfd kick = {
1897 .datamatch = val,
1898 .addr = addr,
1899 .len = 2,
1900 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1901 .fd = fd,
1903 int r;
1904 if (!kvm_enabled()) {
1905 return -ENOSYS;
1907 if (!assign) {
1908 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1910 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1911 if (r < 0) {
1912 return r;
1914 return 0;
1917 int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr)
1919 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1922 int kvm_on_sigbus(int code, void *addr)
1924 return kvm_arch_on_sigbus(code, addr);