4 * Copyright IBM, Corp. 2008
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>
21 #include <linux/kvm.h>
23 #include "qemu-common.h"
24 #include "qemu-barrier.h"
31 #include "exec-memory.h"
33 /* This check must be after config-host.h is included */
35 #include <sys/eventfd.h>
38 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
39 #define PAGE_SIZE TARGET_PAGE_SIZE
44 #define DPRINTF(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #define DPRINTF(fmt, ...) \
51 typedef struct KVMSlot
53 target_phys_addr_t start_addr
;
54 ram_addr_t memory_size
;
60 typedef struct kvm_dirty_log KVMDirtyLog
;
68 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
69 bool coalesced_flush_in_progress
;
70 int broken_set_mem_region
;
73 int robust_singlestep
;
75 #ifdef KVM_CAP_SET_GUEST_DEBUG
76 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
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
;
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
),
102 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
106 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
107 if (s
->slots
[i
].memory_size
== 0) {
112 fprintf(stderr
, "%s: no free slot available\n", __func__
);
116 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
117 target_phys_addr_t start_addr
,
118 target_phys_addr_t end_addr
)
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
) {
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
;
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
)) {
152 if (end_addr
> mem
->start_addr
&&
153 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
161 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
162 target_phys_addr_t
*phys_addr
)
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
);
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
;
206 DPRINTF("kvm_init_vcpu\n");
208 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
210 DPRINTF("kvm_create_vcpu failed\n");
216 env
->kvm_vcpu_dirty
= 1;
218 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
221 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
225 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
227 if (env
->kvm_run
== MAP_FAILED
) {
229 DPRINTF("mmap'ing vcpu state failed\n");
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
);
240 qemu_register_reset(kvm_reset_vcpu
, env
);
241 kvm_arch_reset_vcpu(env
);
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
;
262 old_flags
= mem
->flags
;
264 flags
= (mem
->flags
& ~mask
) | kvm_mem_flags(s
, log_dirty
);
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
) {
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
);
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));
291 return kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
294 static void kvm_log_start(MemoryListener
*listener
,
295 MemoryRegionSection
*section
)
299 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
300 section
->size
, true);
306 static void kvm_log_stop(MemoryListener
*listener
,
307 MemoryRegionSection
*section
)
311 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
312 section
->size
, false);
318 static int kvm_set_migration_log(int enable
)
320 KVMState
*s
= kvm_state
;
324 s
->migration_log
= enable
;
326 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
329 if (!mem
->memory_size
) {
332 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
335 err
= kvm_set_user_memory_region(s
, mem
);
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
)
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
]);
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
);
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
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;
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
);
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
);
424 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
425 DPRINTF("ioctl failed %d\n", errno
);
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
);
438 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
441 KVMState
*s
= kvm_state
;
443 if (s
->coalesced_mmio
) {
444 struct kvm_coalesced_mmio_zone zone
;
450 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
456 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
459 KVMState
*s
= kvm_state
;
461 if (s
->coalesced_mmio
) {
462 struct kvm_coalesced_mmio_zone zone
;
468 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
474 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
478 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
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)
498 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
499 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
500 if (ioeventfds
[i
] < 0) {
503 ret
= kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, true);
505 close(ioeventfds
[i
]);
510 /* Decide whether many devices are supported or not */
511 ret
= i
== ARRAY_SIZE(ioeventfds
);
514 kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, false);
515 close(ioeventfds
[i
]);
523 static const KVMCapabilityInfo
*
524 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
527 if (!kvm_check_extension(s
, list
->value
)) {
535 static void kvm_set_phys_mem(MemoryRegionSection
*section
, bool add
)
537 KVMState
*s
= kvm_state
;
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
;
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
;
555 size
&= TARGET_PAGE_MASK
;
556 if (!size
|| (start_addr
& ~TARGET_PAGE_MASK
)) {
560 if (!memory_region_is_ram(mr
)) {
564 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
567 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
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
);
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
);
591 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
592 __func__
, strerror(-err
));
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
;
610 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
612 err
= kvm_set_user_memory_region(s
, mem
);
614 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
619 start_addr
+= old
.memory_size
;
620 ram
+= old
.memory_size
;
621 size
-= old
.memory_size
;
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
;
631 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
633 err
= kvm_set_user_memory_region(s
, mem
);
635 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
636 __func__
, strerror(-err
));
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__
);
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
);
659 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
660 __func__
, strerror(-err
));
666 /* in case the KVM bug workaround already "consumed" the new slot */
673 mem
= kvm_alloc_slot(s
);
674 mem
->memory_size
= size
;
675 mem
->start_addr
= start_addr
;
677 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
679 err
= kvm_set_user_memory_region(s
, mem
);
681 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
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
)
717 r
= kvm_physical_sync_dirty_bitmap(section
);
723 static void kvm_log_global_start(struct MemoryListener
*listener
)
727 r
= kvm_set_migration_log(1);
731 static void kvm_log_global_stop(struct MemoryListener
*listener
)
735 r
= kvm_set_migration_log(0);
739 static void kvm_mem_ioeventfd_add(MemoryRegionSection
*section
,
740 bool match_data
, uint64_t data
, int fd
)
744 assert(match_data
&& section
->size
<= 8);
746 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
747 data
, true, section
->size
);
753 static void kvm_mem_ioeventfd_del(MemoryRegionSection
*section
,
754 bool match_data
, uint64_t data
, int fd
)
758 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
759 data
, false, section
->size
);
765 static void kvm_io_ioeventfd_add(MemoryRegionSection
*section
,
766 bool match_data
, uint64_t data
, int fd
)
770 assert(match_data
&& section
->size
== 2);
772 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
779 static void kvm_io_ioeventfd_del(MemoryRegionSection
*section
,
780 bool match_data
, uint64_t data
, int fd
)
785 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
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
);
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
);
810 kvm_io_ioeventfd_del(section
, match_data
, data
, fd
);
814 static MemoryListener kvm_memory_listener
= {
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
,
830 static void kvm_handle_interrupt(CPUArchState
*env
, int mask
)
832 env
->interrupt_request
|= mask
;
834 if (!qemu_cpu_is_self(env
)) {
839 int kvm_irqchip_set_irq(KVMState
*s
, int irq
, int level
)
841 struct kvm_irq_level event
;
844 assert(kvm_irqchip_in_kernel());
848 ret
= kvm_vm_ioctl(s
, s
->irqchip_inject_ioctl
, &event
);
850 perror("kvm_set_irqchip_line");
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
)
869 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
);
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
++) {
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;
896 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
897 n
= s
->nr_allocated_irq_routes
* 2;
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
;
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
;
922 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
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");
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
)) {
954 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
956 fprintf(stderr
, "Create kernel irqchip failed\n");
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
);
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";
977 const KVMCapabilityInfo
*missing_cap
;
981 s
= g_malloc0(sizeof(KVMState
));
983 #ifdef KVM_CAP_SET_GUEST_DEBUG
984 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
986 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
987 s
->slots
[i
].slot
= i
;
990 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
992 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
997 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
998 if (ret
< KVM_API_VERSION
) {
1002 fprintf(stderr
, "kvm version too old\n");
1006 if (ret
> KVM_API_VERSION
) {
1008 fprintf(stderr
, "kvm version not supported\n");
1012 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
1015 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
1016 "your host kernel command line\n");
1022 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1025 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1029 fprintf(stderr
, "kvm does not support %s\n%s",
1030 missing_cap
->name
, upgrade_note
);
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
);
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
);
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
);
1053 #ifdef KVM_CAP_XSAVE
1054 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1058 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1061 #ifdef KVM_CAP_PIT_STATE2
1062 s
->pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1065 ret
= kvm_arch_init(s
);
1070 ret
= kvm_irqchip_create(s
);
1076 memory_listener_register(&kvm_memory_listener
, NULL
);
1078 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1080 cpu_interrupt_handler
= kvm_handle_interrupt
;
1098 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1102 uint8_t *ptr
= data
;
1104 for (i
= 0; i
< count
; i
++) {
1105 if (direction
== KVM_EXIT_IO_IN
) {
1108 stb_p(ptr
, cpu_inb(port
));
1111 stw_p(ptr
, cpu_inw(port
));
1114 stl_p(ptr
, cpu_inl(port
));
1120 cpu_outb(port
, ldub_p(ptr
));
1123 cpu_outw(port
, lduw_p(ptr
));
1126 cpu_outl(port
, ldl_p(ptr
));
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
)) {
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
]);
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.
1162 void kvm_flush_coalesced_mmio_buffer(void)
1164 KVMState
*s
= kvm_state
;
1166 if (s
->coalesced_flush_in_progress
) {
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
);
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
;
1222 DPRINTF("kvm_cpu_exec()\n");
1224 if (kvm_arch_process_async_events(env
)) {
1225 env
->exit_request
= 0;
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
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();
1255 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1256 DPRINTF("io window exit\n");
1257 ret
= EXCP_INTERRUPT
;
1260 fprintf(stderr
, "error: kvm run failed %s\n",
1261 strerror(-run_ret
));
1265 switch (run
->exit_reason
) {
1267 DPRINTF("handle_io\n");
1268 kvm_handle_io(run
->io
.port
,
1269 (uint8_t *)run
+ run
->io
.data_offset
,
1276 DPRINTF("handle_mmio\n");
1277 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1280 run
->mmio
.is_write
);
1283 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1284 DPRINTF("irq_window_open\n");
1285 ret
= EXCP_INTERRUPT
;
1287 case KVM_EXIT_SHUTDOWN
:
1288 DPRINTF("shutdown\n");
1289 qemu_system_reset_request();
1290 ret
= EXCP_INTERRUPT
;
1292 case KVM_EXIT_UNKNOWN
:
1293 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1294 (uint64_t)run
->hw
.hardware_exit_reason
);
1297 case KVM_EXIT_INTERNAL_ERROR
:
1298 ret
= kvm_handle_internal_error(env
, run
);
1301 DPRINTF("kvm_arch_handle_exit\n");
1302 ret
= kvm_arch_handle_exit(env
, run
);
1308 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1309 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1312 env
->exit_request
= 0;
1316 int kvm_ioctl(KVMState
*s
, int type
, ...)
1323 arg
= va_arg(ap
, void *);
1326 ret
= ioctl(s
->fd
, type
, arg
);
1333 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1340 arg
= va_arg(ap
, void *);
1343 ret
= ioctl(s
->vmfd
, type
, arg
);
1350 int kvm_vcpu_ioctl(CPUArchState
*env
, int type
, ...)
1357 arg
= va_arg(ap
, void *);
1360 ret
= ioctl(env
->kvm_fd
, type
, arg
);
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()) {
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
);
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
);
1430 perror("qemu_madvise");
1432 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1438 #ifdef KVM_CAP_SET_GUEST_DEBUG
1439 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUArchState
*env
,
1442 struct kvm_sw_breakpoint
*bp
;
1444 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
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
;
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
);
1483 run_on_cpu(env
, kvm_invoke_set_guest_debug
, &data
);
1487 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1488 target_ulong len
, int type
)
1490 struct kvm_sw_breakpoint
*bp
;
1494 if (type
== GDB_BREAKPOINT_SW
) {
1495 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1501 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1508 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1514 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1517 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1523 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1524 err
= kvm_update_guest_debug(env
, 0);
1532 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1533 target_ulong len
, int type
)
1535 struct kvm_sw_breakpoint
*bp
;
1539 if (type
== GDB_BREAKPOINT_SW
) {
1540 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1545 if (bp
->use_count
> 1) {
1550 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1555 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1558 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1564 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1565 err
= kvm_update_guest_debug(env
, 0);
1573 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1575 struct kvm_sw_breakpoint
*bp
, *next
;
1576 KVMState
*s
= current_env
->kvm_state
;
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) {
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
)
1603 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1604 target_ulong len
, int type
)
1609 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1610 target_ulong len
, int type
)
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
;
1626 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1629 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1632 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1633 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1639 int kvm_set_ioeventfd_mmio(int fd
, uint32_t addr
, uint32_t val
, bool assign
,
1643 struct kvm_ioeventfd iofd
;
1645 iofd
.datamatch
= val
;
1648 iofd
.flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
;
1651 if (!kvm_enabled()) {
1656 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1659 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
1668 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1670 struct kvm_ioeventfd kick
= {
1674 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1678 if (!kvm_enabled()) {
1682 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1684 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
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
);