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
;
82 /* The man page (and posix) say ioctl numbers are signed int, but
83 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
84 * unsigned, and treating them as signed here can break things */
85 unsigned irqchip_inject_ioctl
;
86 #ifdef KVM_CAP_IRQ_ROUTING
87 struct kvm_irq_routing
*irq_routes
;
88 int nr_allocated_irq_routes
;
89 uint32_t *used_gsi_bitmap
;
95 bool kvm_kernel_irqchip
;
97 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
98 KVM_CAP_INFO(USER_MEMORY
),
99 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
103 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
107 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
108 if (s
->slots
[i
].memory_size
== 0) {
113 fprintf(stderr
, "%s: no free slot available\n", __func__
);
117 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
118 target_phys_addr_t start_addr
,
119 target_phys_addr_t end_addr
)
123 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
124 KVMSlot
*mem
= &s
->slots
[i
];
126 if (start_addr
== mem
->start_addr
&&
127 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
136 * Find overlapping slot with lowest start address
138 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
139 target_phys_addr_t start_addr
,
140 target_phys_addr_t end_addr
)
142 KVMSlot
*found
= NULL
;
145 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
146 KVMSlot
*mem
= &s
->slots
[i
];
148 if (mem
->memory_size
== 0 ||
149 (found
&& found
->start_addr
< mem
->start_addr
)) {
153 if (end_addr
> mem
->start_addr
&&
154 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
162 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
163 target_phys_addr_t
*phys_addr
)
167 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
168 KVMSlot
*mem
= &s
->slots
[i
];
170 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
171 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
179 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
181 struct kvm_userspace_memory_region mem
;
183 mem
.slot
= slot
->slot
;
184 mem
.guest_phys_addr
= slot
->start_addr
;
185 mem
.memory_size
= slot
->memory_size
;
186 mem
.userspace_addr
= (unsigned long)slot
->ram
;
187 mem
.flags
= slot
->flags
;
188 if (s
->migration_log
) {
189 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
191 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
194 static void kvm_reset_vcpu(void *opaque
)
196 CPUArchState
*env
= opaque
;
198 kvm_arch_reset_vcpu(env
);
201 int kvm_pit_in_kernel(void)
203 return kvm_state
->pit_in_kernel
;
206 int kvm_init_vcpu(CPUArchState
*env
)
208 KVMState
*s
= kvm_state
;
212 DPRINTF("kvm_init_vcpu\n");
214 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
216 DPRINTF("kvm_create_vcpu failed\n");
222 env
->kvm_vcpu_dirty
= 1;
224 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
227 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
231 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
233 if (env
->kvm_run
== MAP_FAILED
) {
235 DPRINTF("mmap'ing vcpu state failed\n");
239 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
240 s
->coalesced_mmio_ring
=
241 (void *)env
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
244 ret
= kvm_arch_init_vcpu(env
);
246 qemu_register_reset(kvm_reset_vcpu
, env
);
247 kvm_arch_reset_vcpu(env
);
254 * dirty pages logging control
257 static int kvm_mem_flags(KVMState
*s
, bool log_dirty
)
259 return log_dirty
? KVM_MEM_LOG_DIRTY_PAGES
: 0;
262 static int kvm_slot_dirty_pages_log_change(KVMSlot
*mem
, bool log_dirty
)
264 KVMState
*s
= kvm_state
;
265 int flags
, mask
= KVM_MEM_LOG_DIRTY_PAGES
;
268 old_flags
= mem
->flags
;
270 flags
= (mem
->flags
& ~mask
) | kvm_mem_flags(s
, log_dirty
);
273 /* If nothing changed effectively, no need to issue ioctl */
274 if (s
->migration_log
) {
275 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
278 if (flags
== old_flags
) {
282 return kvm_set_user_memory_region(s
, mem
);
285 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr
,
286 ram_addr_t size
, bool log_dirty
)
288 KVMState
*s
= kvm_state
;
289 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
292 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
293 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
294 (target_phys_addr_t
)(phys_addr
+ size
- 1));
297 return kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
300 static void kvm_log_start(MemoryListener
*listener
,
301 MemoryRegionSection
*section
)
305 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
306 section
->size
, true);
312 static void kvm_log_stop(MemoryListener
*listener
,
313 MemoryRegionSection
*section
)
317 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
318 section
->size
, false);
324 static int kvm_set_migration_log(int enable
)
326 KVMState
*s
= kvm_state
;
330 s
->migration_log
= enable
;
332 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
335 if (!mem
->memory_size
) {
338 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
341 err
= kvm_set_user_memory_region(s
, mem
);
349 /* get kvm's dirty pages bitmap and update qemu's */
350 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
351 unsigned long *bitmap
)
354 unsigned long page_number
, c
;
355 target_phys_addr_t addr
, addr1
;
356 unsigned int len
= ((section
->size
/ TARGET_PAGE_SIZE
) + HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
359 * bitmap-traveling is faster than memory-traveling (for addr...)
360 * especially when most of the memory is not dirty.
362 for (i
= 0; i
< len
; i
++) {
363 if (bitmap
[i
] != 0) {
364 c
= leul_to_cpu(bitmap
[i
]);
368 page_number
= i
* HOST_LONG_BITS
+ j
;
369 addr1
= page_number
* TARGET_PAGE_SIZE
;
370 addr
= section
->offset_within_region
+ addr1
;
371 memory_region_set_dirty(section
->mr
, addr
, TARGET_PAGE_SIZE
);
378 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
381 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
382 * This function updates qemu's dirty bitmap using
383 * memory_region_set_dirty(). This means all bits are set
386 * @start_add: start of logged region.
387 * @end_addr: end of logged region.
389 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection
*section
)
391 KVMState
*s
= kvm_state
;
392 unsigned long size
, allocated_size
= 0;
396 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
397 target_phys_addr_t end_addr
= start_addr
+ section
->size
;
399 d
.dirty_bitmap
= NULL
;
400 while (start_addr
< end_addr
) {
401 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
406 /* XXX bad kernel interface alert
407 * For dirty bitmap, kernel allocates array of size aligned to
408 * bits-per-long. But for case when the kernel is 64bits and
409 * the userspace is 32bits, userspace can't align to the same
410 * bits-per-long, since sizeof(long) is different between kernel
411 * and user space. This way, userspace will provide buffer which
412 * may be 4 bytes less than the kernel will use, resulting in
413 * userspace memory corruption (which is not detectable by valgrind
414 * too, in most cases).
415 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
416 * a hope that sizeof(long) wont become >8 any time soon.
418 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
419 /*HOST_LONG_BITS*/ 64) / 8;
420 if (!d
.dirty_bitmap
) {
421 d
.dirty_bitmap
= g_malloc(size
);
422 } else if (size
> allocated_size
) {
423 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
425 allocated_size
= size
;
426 memset(d
.dirty_bitmap
, 0, allocated_size
);
430 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
431 DPRINTF("ioctl failed %d\n", errno
);
436 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
437 start_addr
= mem
->start_addr
+ mem
->memory_size
;
439 g_free(d
.dirty_bitmap
);
444 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
447 KVMState
*s
= kvm_state
;
449 if (s
->coalesced_mmio
) {
450 struct kvm_coalesced_mmio_zone zone
;
456 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
462 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
465 KVMState
*s
= kvm_state
;
467 if (s
->coalesced_mmio
) {
468 struct kvm_coalesced_mmio_zone zone
;
474 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
480 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
484 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
492 static int kvm_check_many_ioeventfds(void)
494 /* Userspace can use ioeventfd for io notification. This requires a host
495 * that supports eventfd(2) and an I/O thread; since eventfd does not
496 * support SIGIO it cannot interrupt the vcpu.
498 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
499 * can avoid creating too many ioeventfds.
501 #if defined(CONFIG_EVENTFD)
504 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
505 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
506 if (ioeventfds
[i
] < 0) {
509 ret
= kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, true);
511 close(ioeventfds
[i
]);
516 /* Decide whether many devices are supported or not */
517 ret
= i
== ARRAY_SIZE(ioeventfds
);
520 kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, false);
521 close(ioeventfds
[i
]);
529 static const KVMCapabilityInfo
*
530 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
533 if (!kvm_check_extension(s
, list
->value
)) {
541 static void kvm_set_phys_mem(MemoryRegionSection
*section
, bool add
)
543 KVMState
*s
= kvm_state
;
546 MemoryRegion
*mr
= section
->mr
;
547 bool log_dirty
= memory_region_is_logging(mr
);
548 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
549 ram_addr_t size
= section
->size
;
553 /* kvm works in page size chunks, but the function may be called
554 with sub-page size and unaligned start address. */
555 delta
= TARGET_PAGE_ALIGN(size
) - size
;
561 size
&= TARGET_PAGE_MASK
;
562 if (!size
|| (start_addr
& ~TARGET_PAGE_MASK
)) {
566 if (!memory_region_is_ram(mr
)) {
570 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
573 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
578 if (add
&& start_addr
>= mem
->start_addr
&&
579 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
580 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
581 /* The new slot fits into the existing one and comes with
582 * identical parameters - update flags and done. */
583 kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
589 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
590 kvm_physical_sync_dirty_bitmap(section
);
593 /* unregister the overlapping slot */
594 mem
->memory_size
= 0;
595 err
= kvm_set_user_memory_region(s
, mem
);
597 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
598 __func__
, strerror(-err
));
602 /* Workaround for older KVM versions: we can't join slots, even not by
603 * unregistering the previous ones and then registering the larger
604 * slot. We have to maintain the existing fragmentation. Sigh.
606 * This workaround assumes that the new slot starts at the same
607 * address as the first existing one. If not or if some overlapping
608 * slot comes around later, we will fail (not seen in practice so far)
609 * - and actually require a recent KVM version. */
610 if (s
->broken_set_mem_region
&&
611 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
612 mem
= kvm_alloc_slot(s
);
613 mem
->memory_size
= old
.memory_size
;
614 mem
->start_addr
= old
.start_addr
;
616 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
618 err
= kvm_set_user_memory_region(s
, mem
);
620 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
625 start_addr
+= old
.memory_size
;
626 ram
+= old
.memory_size
;
627 size
-= old
.memory_size
;
631 /* register prefix slot */
632 if (old
.start_addr
< start_addr
) {
633 mem
= kvm_alloc_slot(s
);
634 mem
->memory_size
= start_addr
- old
.start_addr
;
635 mem
->start_addr
= old
.start_addr
;
637 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
639 err
= kvm_set_user_memory_region(s
, mem
);
641 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
642 __func__
, strerror(-err
));
644 fprintf(stderr
, "%s: This is probably because your kernel's " \
645 "PAGE_SIZE is too big. Please try to use 4k " \
646 "PAGE_SIZE!\n", __func__
);
652 /* register suffix slot */
653 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
654 ram_addr_t size_delta
;
656 mem
= kvm_alloc_slot(s
);
657 mem
->start_addr
= start_addr
+ size
;
658 size_delta
= mem
->start_addr
- old
.start_addr
;
659 mem
->memory_size
= old
.memory_size
- size_delta
;
660 mem
->ram
= old
.ram
+ size_delta
;
661 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
663 err
= kvm_set_user_memory_region(s
, mem
);
665 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
666 __func__
, strerror(-err
));
672 /* in case the KVM bug workaround already "consumed" the new slot */
679 mem
= kvm_alloc_slot(s
);
680 mem
->memory_size
= size
;
681 mem
->start_addr
= start_addr
;
683 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
685 err
= kvm_set_user_memory_region(s
, mem
);
687 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
693 static void kvm_begin(MemoryListener
*listener
)
697 static void kvm_commit(MemoryListener
*listener
)
701 static void kvm_region_add(MemoryListener
*listener
,
702 MemoryRegionSection
*section
)
704 kvm_set_phys_mem(section
, true);
707 static void kvm_region_del(MemoryListener
*listener
,
708 MemoryRegionSection
*section
)
710 kvm_set_phys_mem(section
, false);
713 static void kvm_region_nop(MemoryListener
*listener
,
714 MemoryRegionSection
*section
)
718 static void kvm_log_sync(MemoryListener
*listener
,
719 MemoryRegionSection
*section
)
723 r
= kvm_physical_sync_dirty_bitmap(section
);
729 static void kvm_log_global_start(struct MemoryListener
*listener
)
733 r
= kvm_set_migration_log(1);
737 static void kvm_log_global_stop(struct MemoryListener
*listener
)
741 r
= kvm_set_migration_log(0);
745 static void kvm_mem_ioeventfd_add(MemoryRegionSection
*section
,
746 bool match_data
, uint64_t data
, int fd
)
750 assert(match_data
&& section
->size
== 4);
752 r
= kvm_set_ioeventfd_mmio_long(fd
, section
->offset_within_address_space
,
759 static void kvm_mem_ioeventfd_del(MemoryRegionSection
*section
,
760 bool match_data
, uint64_t data
, int fd
)
764 r
= kvm_set_ioeventfd_mmio_long(fd
, section
->offset_within_address_space
,
771 static void kvm_io_ioeventfd_add(MemoryRegionSection
*section
,
772 bool match_data
, uint64_t data
, int fd
)
776 assert(match_data
&& section
->size
== 2);
778 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
785 static void kvm_io_ioeventfd_del(MemoryRegionSection
*section
,
786 bool match_data
, uint64_t data
, int fd
)
791 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
798 static void kvm_eventfd_add(MemoryListener
*listener
,
799 MemoryRegionSection
*section
,
800 bool match_data
, uint64_t data
, int fd
)
802 if (section
->address_space
== get_system_memory()) {
803 kvm_mem_ioeventfd_add(section
, match_data
, data
, fd
);
805 kvm_io_ioeventfd_add(section
, match_data
, data
, fd
);
809 static void kvm_eventfd_del(MemoryListener
*listener
,
810 MemoryRegionSection
*section
,
811 bool match_data
, uint64_t data
, int fd
)
813 if (section
->address_space
== get_system_memory()) {
814 kvm_mem_ioeventfd_del(section
, match_data
, data
, fd
);
816 kvm_io_ioeventfd_del(section
, match_data
, data
, fd
);
820 static MemoryListener kvm_memory_listener
= {
822 .commit
= kvm_commit
,
823 .region_add
= kvm_region_add
,
824 .region_del
= kvm_region_del
,
825 .region_nop
= kvm_region_nop
,
826 .log_start
= kvm_log_start
,
827 .log_stop
= kvm_log_stop
,
828 .log_sync
= kvm_log_sync
,
829 .log_global_start
= kvm_log_global_start
,
830 .log_global_stop
= kvm_log_global_stop
,
831 .eventfd_add
= kvm_eventfd_add
,
832 .eventfd_del
= kvm_eventfd_del
,
836 static void kvm_handle_interrupt(CPUArchState
*env
, int mask
)
838 env
->interrupt_request
|= mask
;
840 if (!qemu_cpu_is_self(env
)) {
845 int kvm_irqchip_set_irq(KVMState
*s
, int irq
, int level
)
847 struct kvm_irq_level event
;
850 assert(kvm_irqchip_in_kernel());
854 ret
= kvm_vm_ioctl(s
, s
->irqchip_inject_ioctl
, &event
);
856 perror("kvm_set_irqchip_line");
860 return (s
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
863 #ifdef KVM_CAP_IRQ_ROUTING
864 static void set_gsi(KVMState
*s
, unsigned int gsi
)
866 assert(gsi
< s
->max_gsi
);
868 s
->used_gsi_bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
871 static void kvm_init_irq_routing(KVMState
*s
)
875 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
);
877 unsigned int gsi_bits
, i
;
879 /* Round up so we can search ints using ffs */
880 gsi_bits
= (gsi_count
+ 31) / 32;
881 s
->used_gsi_bitmap
= g_malloc0(gsi_bits
/ 8);
882 s
->max_gsi
= gsi_bits
;
884 /* Mark any over-allocated bits as already in use */
885 for (i
= gsi_count
; i
< gsi_bits
; i
++) {
890 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
891 s
->nr_allocated_irq_routes
= 0;
893 kvm_arch_init_irq_routing(s
);
896 static void kvm_add_routing_entry(KVMState
*s
,
897 struct kvm_irq_routing_entry
*entry
)
899 struct kvm_irq_routing_entry
*new;
902 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
903 n
= s
->nr_allocated_irq_routes
* 2;
907 size
= sizeof(struct kvm_irq_routing
);
908 size
+= n
* sizeof(*new);
909 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
910 s
->nr_allocated_irq_routes
= n
;
912 n
= s
->irq_routes
->nr
++;
913 new = &s
->irq_routes
->entries
[n
];
914 memset(new, 0, sizeof(*new));
915 new->gsi
= entry
->gsi
;
916 new->type
= entry
->type
;
917 new->flags
= entry
->flags
;
920 set_gsi(s
, entry
->gsi
);
923 void kvm_irqchip_add_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
925 struct kvm_irq_routing_entry e
;
928 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
930 e
.u
.irqchip
.irqchip
= irqchip
;
931 e
.u
.irqchip
.pin
= pin
;
932 kvm_add_routing_entry(s
, &e
);
935 int kvm_irqchip_commit_routes(KVMState
*s
)
937 s
->irq_routes
->flags
= 0;
938 return kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
941 #else /* !KVM_CAP_IRQ_ROUTING */
943 static void kvm_init_irq_routing(KVMState
*s
)
946 #endif /* !KVM_CAP_IRQ_ROUTING */
948 static int kvm_irqchip_create(KVMState
*s
)
950 QemuOptsList
*list
= qemu_find_opts("machine");
953 if (QTAILQ_EMPTY(&list
->head
) ||
954 !qemu_opt_get_bool(QTAILQ_FIRST(&list
->head
),
955 "kernel_irqchip", false) ||
956 !kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
960 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
962 fprintf(stderr
, "Create kernel irqchip failed\n");
966 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
967 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
968 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
970 kvm_kernel_irqchip
= true;
972 kvm_init_irq_routing(s
);
979 static const char upgrade_note
[] =
980 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
981 "(see http://sourceforge.net/projects/kvm).\n";
983 const KVMCapabilityInfo
*missing_cap
;
987 s
= g_malloc0(sizeof(KVMState
));
989 #ifdef KVM_CAP_SET_GUEST_DEBUG
990 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
992 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
993 s
->slots
[i
].slot
= i
;
996 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
998 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1003 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1004 if (ret
< KVM_API_VERSION
) {
1008 fprintf(stderr
, "kvm version too old\n");
1012 if (ret
> KVM_API_VERSION
) {
1014 fprintf(stderr
, "kvm version not supported\n");
1018 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
1021 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
1022 "your host kernel command line\n");
1028 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1031 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1035 fprintf(stderr
, "kvm does not support %s\n%s",
1036 missing_cap
->name
, upgrade_note
);
1040 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1042 s
->broken_set_mem_region
= 1;
1043 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1045 s
->broken_set_mem_region
= 0;
1048 #ifdef KVM_CAP_VCPU_EVENTS
1049 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1052 s
->robust_singlestep
=
1053 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1055 #ifdef KVM_CAP_DEBUGREGS
1056 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1059 #ifdef KVM_CAP_XSAVE
1060 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1064 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1067 #ifdef KVM_CAP_PIT_STATE2
1068 s
->pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1071 ret
= kvm_arch_init(s
);
1076 ret
= kvm_irqchip_create(s
);
1082 memory_listener_register(&kvm_memory_listener
, NULL
);
1084 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1086 cpu_interrupt_handler
= kvm_handle_interrupt
;
1104 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1108 uint8_t *ptr
= data
;
1110 for (i
= 0; i
< count
; i
++) {
1111 if (direction
== KVM_EXIT_IO_IN
) {
1114 stb_p(ptr
, cpu_inb(port
));
1117 stw_p(ptr
, cpu_inw(port
));
1120 stl_p(ptr
, cpu_inl(port
));
1126 cpu_outb(port
, ldub_p(ptr
));
1129 cpu_outw(port
, lduw_p(ptr
));
1132 cpu_outl(port
, ldl_p(ptr
));
1141 static int kvm_handle_internal_error(CPUArchState
*env
, struct kvm_run
*run
)
1143 fprintf(stderr
, "KVM internal error.");
1144 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1147 fprintf(stderr
, " Suberror: %d\n", run
->internal
.suberror
);
1148 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1149 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1150 i
, (uint64_t)run
->internal
.data
[i
]);
1153 fprintf(stderr
, "\n");
1155 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1156 fprintf(stderr
, "emulation failure\n");
1157 if (!kvm_arch_stop_on_emulation_error(env
)) {
1158 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1159 return EXCP_INTERRUPT
;
1162 /* FIXME: Should trigger a qmp message to let management know
1163 * something went wrong.
1168 void kvm_flush_coalesced_mmio_buffer(void)
1170 KVMState
*s
= kvm_state
;
1172 if (s
->coalesced_flush_in_progress
) {
1176 s
->coalesced_flush_in_progress
= true;
1178 if (s
->coalesced_mmio_ring
) {
1179 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1180 while (ring
->first
!= ring
->last
) {
1181 struct kvm_coalesced_mmio
*ent
;
1183 ent
= &ring
->coalesced_mmio
[ring
->first
];
1185 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1187 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1191 s
->coalesced_flush_in_progress
= false;
1194 static void do_kvm_cpu_synchronize_state(void *_env
)
1196 CPUArchState
*env
= _env
;
1198 if (!env
->kvm_vcpu_dirty
) {
1199 kvm_arch_get_registers(env
);
1200 env
->kvm_vcpu_dirty
= 1;
1204 void kvm_cpu_synchronize_state(CPUArchState
*env
)
1206 if (!env
->kvm_vcpu_dirty
) {
1207 run_on_cpu(env
, do_kvm_cpu_synchronize_state
, env
);
1211 void kvm_cpu_synchronize_post_reset(CPUArchState
*env
)
1213 kvm_arch_put_registers(env
, KVM_PUT_RESET_STATE
);
1214 env
->kvm_vcpu_dirty
= 0;
1217 void kvm_cpu_synchronize_post_init(CPUArchState
*env
)
1219 kvm_arch_put_registers(env
, KVM_PUT_FULL_STATE
);
1220 env
->kvm_vcpu_dirty
= 0;
1223 int kvm_cpu_exec(CPUArchState
*env
)
1225 struct kvm_run
*run
= env
->kvm_run
;
1228 DPRINTF("kvm_cpu_exec()\n");
1230 if (kvm_arch_process_async_events(env
)) {
1231 env
->exit_request
= 0;
1236 if (env
->kvm_vcpu_dirty
) {
1237 kvm_arch_put_registers(env
, KVM_PUT_RUNTIME_STATE
);
1238 env
->kvm_vcpu_dirty
= 0;
1241 kvm_arch_pre_run(env
, run
);
1242 if (env
->exit_request
) {
1243 DPRINTF("interrupt exit requested\n");
1245 * KVM requires us to reenter the kernel after IO exits to complete
1246 * instruction emulation. This self-signal will ensure that we
1249 qemu_cpu_kick_self();
1251 qemu_mutex_unlock_iothread();
1253 run_ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
1255 qemu_mutex_lock_iothread();
1256 kvm_arch_post_run(env
, run
);
1258 kvm_flush_coalesced_mmio_buffer();
1261 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1262 DPRINTF("io window exit\n");
1263 ret
= EXCP_INTERRUPT
;
1266 fprintf(stderr
, "error: kvm run failed %s\n",
1267 strerror(-run_ret
));
1271 switch (run
->exit_reason
) {
1273 DPRINTF("handle_io\n");
1274 kvm_handle_io(run
->io
.port
,
1275 (uint8_t *)run
+ run
->io
.data_offset
,
1282 DPRINTF("handle_mmio\n");
1283 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1286 run
->mmio
.is_write
);
1289 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1290 DPRINTF("irq_window_open\n");
1291 ret
= EXCP_INTERRUPT
;
1293 case KVM_EXIT_SHUTDOWN
:
1294 DPRINTF("shutdown\n");
1295 qemu_system_reset_request();
1296 ret
= EXCP_INTERRUPT
;
1298 case KVM_EXIT_UNKNOWN
:
1299 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1300 (uint64_t)run
->hw
.hardware_exit_reason
);
1303 case KVM_EXIT_INTERNAL_ERROR
:
1304 ret
= kvm_handle_internal_error(env
, run
);
1307 DPRINTF("kvm_arch_handle_exit\n");
1308 ret
= kvm_arch_handle_exit(env
, run
);
1314 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1315 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1318 env
->exit_request
= 0;
1322 int kvm_ioctl(KVMState
*s
, int type
, ...)
1329 arg
= va_arg(ap
, void *);
1332 ret
= ioctl(s
->fd
, type
, arg
);
1339 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1346 arg
= va_arg(ap
, void *);
1349 ret
= ioctl(s
->vmfd
, type
, arg
);
1356 int kvm_vcpu_ioctl(CPUArchState
*env
, int type
, ...)
1363 arg
= va_arg(ap
, void *);
1366 ret
= ioctl(env
->kvm_fd
, type
, arg
);
1373 int kvm_has_sync_mmu(void)
1375 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
1378 int kvm_has_vcpu_events(void)
1380 return kvm_state
->vcpu_events
;
1383 int kvm_has_robust_singlestep(void)
1385 return kvm_state
->robust_singlestep
;
1388 int kvm_has_debugregs(void)
1390 return kvm_state
->debugregs
;
1393 int kvm_has_xsave(void)
1395 return kvm_state
->xsave
;
1398 int kvm_has_xcrs(void)
1400 return kvm_state
->xcrs
;
1403 int kvm_has_pit_state2(void)
1405 return kvm_state
->pit_state2
;
1408 int kvm_has_many_ioeventfds(void)
1410 if (!kvm_enabled()) {
1413 return kvm_state
->many_ioeventfds
;
1416 int kvm_has_gsi_routing(void)
1418 #ifdef KVM_CAP_IRQ_ROUTING
1419 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1425 int kvm_allows_irq0_override(void)
1427 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1430 void kvm_setup_guest_memory(void *start
, size_t size
)
1432 if (!kvm_has_sync_mmu()) {
1433 int ret
= qemu_madvise(start
, size
, QEMU_MADV_DONTFORK
);
1436 perror("qemu_madvise");
1438 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1444 #ifdef KVM_CAP_SET_GUEST_DEBUG
1445 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUArchState
*env
,
1448 struct kvm_sw_breakpoint
*bp
;
1450 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1458 int kvm_sw_breakpoints_active(CPUArchState
*env
)
1460 return !QTAILQ_EMPTY(&env
->kvm_state
->kvm_sw_breakpoints
);
1463 struct kvm_set_guest_debug_data
{
1464 struct kvm_guest_debug dbg
;
1469 static void kvm_invoke_set_guest_debug(void *data
)
1471 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1472 CPUArchState
*env
= dbg_data
->env
;
1474 dbg_data
->err
= kvm_vcpu_ioctl(env
, KVM_SET_GUEST_DEBUG
, &dbg_data
->dbg
);
1477 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1479 struct kvm_set_guest_debug_data data
;
1481 data
.dbg
.control
= reinject_trap
;
1483 if (env
->singlestep_enabled
) {
1484 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1486 kvm_arch_update_guest_debug(env
, &data
.dbg
);
1489 run_on_cpu(env
, kvm_invoke_set_guest_debug
, &data
);
1493 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1494 target_ulong len
, int type
)
1496 struct kvm_sw_breakpoint
*bp
;
1500 if (type
== GDB_BREAKPOINT_SW
) {
1501 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1507 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1514 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1520 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1523 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1529 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1530 err
= kvm_update_guest_debug(env
, 0);
1538 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1539 target_ulong len
, int type
)
1541 struct kvm_sw_breakpoint
*bp
;
1545 if (type
== GDB_BREAKPOINT_SW
) {
1546 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1551 if (bp
->use_count
> 1) {
1556 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1561 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1564 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1570 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1571 err
= kvm_update_guest_debug(env
, 0);
1579 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1581 struct kvm_sw_breakpoint
*bp
, *next
;
1582 KVMState
*s
= current_env
->kvm_state
;
1585 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1586 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
1587 /* Try harder to find a CPU that currently sees the breakpoint. */
1588 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1589 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0) {
1595 kvm_arch_remove_all_hw_breakpoints();
1597 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1598 kvm_update_guest_debug(env
, 0);
1602 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1604 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1609 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1610 target_ulong len
, int type
)
1615 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1616 target_ulong len
, int type
)
1621 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1624 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1626 int kvm_set_signal_mask(CPUArchState
*env
, const sigset_t
*sigset
)
1628 struct kvm_signal_mask
*sigmask
;
1632 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1635 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1638 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1639 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1645 int kvm_set_ioeventfd_mmio_long(int fd
, uint32_t addr
, uint32_t val
, bool assign
)
1648 struct kvm_ioeventfd iofd
;
1650 iofd
.datamatch
= val
;
1653 iofd
.flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
;
1656 if (!kvm_enabled()) {
1661 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1664 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
1673 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1675 struct kvm_ioeventfd kick
= {
1679 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1683 if (!kvm_enabled()) {
1687 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1689 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1696 int kvm_on_sigbus_vcpu(CPUArchState
*env
, int code
, void *addr
)
1698 return kvm_arch_on_sigbus_vcpu(env
, code
, addr
);
1701 int kvm_on_sigbus(int code
, void *addr
)
1703 return kvm_arch_on_sigbus(code
, addr
);