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
;
351 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
354 * bitmap-traveling is faster than memory-traveling (for addr...)
355 * especially when most of the memory is not dirty.
357 for (i
= 0; i
< len
; i
++) {
358 if (bitmap
[i
] != 0) {
359 c
= leul_to_cpu(bitmap
[i
]);
363 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
364 addr1
= page_number
* TARGET_PAGE_SIZE
;
365 addr
= section
->offset_within_region
+ addr1
;
366 memory_region_set_dirty(section
->mr
, addr
,
367 TARGET_PAGE_SIZE
* hpratio
);
374 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
377 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
378 * This function updates qemu's dirty bitmap using
379 * memory_region_set_dirty(). This means all bits are set
382 * @start_add: start of logged region.
383 * @end_addr: end of logged region.
385 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection
*section
)
387 KVMState
*s
= kvm_state
;
388 unsigned long size
, allocated_size
= 0;
392 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
393 target_phys_addr_t end_addr
= start_addr
+ section
->size
;
395 d
.dirty_bitmap
= NULL
;
396 while (start_addr
< end_addr
) {
397 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
402 /* XXX bad kernel interface alert
403 * For dirty bitmap, kernel allocates array of size aligned to
404 * bits-per-long. But for case when the kernel is 64bits and
405 * the userspace is 32bits, userspace can't align to the same
406 * bits-per-long, since sizeof(long) is different between kernel
407 * and user space. This way, userspace will provide buffer which
408 * may be 4 bytes less than the kernel will use, resulting in
409 * userspace memory corruption (which is not detectable by valgrind
410 * too, in most cases).
411 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
412 * a hope that sizeof(long) wont become >8 any time soon.
414 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
415 /*HOST_LONG_BITS*/ 64) / 8;
416 if (!d
.dirty_bitmap
) {
417 d
.dirty_bitmap
= g_malloc(size
);
418 } else if (size
> allocated_size
) {
419 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
421 allocated_size
= size
;
422 memset(d
.dirty_bitmap
, 0, allocated_size
);
426 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
427 DPRINTF("ioctl failed %d\n", errno
);
432 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
433 start_addr
= mem
->start_addr
+ mem
->memory_size
;
435 g_free(d
.dirty_bitmap
);
440 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
443 KVMState
*s
= kvm_state
;
445 if (s
->coalesced_mmio
) {
446 struct kvm_coalesced_mmio_zone zone
;
452 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
458 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
461 KVMState
*s
= kvm_state
;
463 if (s
->coalesced_mmio
) {
464 struct kvm_coalesced_mmio_zone zone
;
470 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
476 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
480 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
488 static int kvm_check_many_ioeventfds(void)
490 /* Userspace can use ioeventfd for io notification. This requires a host
491 * that supports eventfd(2) and an I/O thread; since eventfd does not
492 * support SIGIO it cannot interrupt the vcpu.
494 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
495 * can avoid creating too many ioeventfds.
497 #if defined(CONFIG_EVENTFD)
500 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
501 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
502 if (ioeventfds
[i
] < 0) {
505 ret
= kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, true);
507 close(ioeventfds
[i
]);
512 /* Decide whether many devices are supported or not */
513 ret
= i
== ARRAY_SIZE(ioeventfds
);
516 kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, false);
517 close(ioeventfds
[i
]);
525 static const KVMCapabilityInfo
*
526 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
529 if (!kvm_check_extension(s
, list
->value
)) {
537 static void kvm_set_phys_mem(MemoryRegionSection
*section
, bool add
)
539 KVMState
*s
= kvm_state
;
542 MemoryRegion
*mr
= section
->mr
;
543 bool log_dirty
= memory_region_is_logging(mr
);
544 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
545 ram_addr_t size
= section
->size
;
549 /* kvm works in page size chunks, but the function may be called
550 with sub-page size and unaligned start address. */
551 delta
= TARGET_PAGE_ALIGN(size
) - size
;
557 size
&= TARGET_PAGE_MASK
;
558 if (!size
|| (start_addr
& ~TARGET_PAGE_MASK
)) {
562 if (!memory_region_is_ram(mr
)) {
566 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
569 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
574 if (add
&& start_addr
>= mem
->start_addr
&&
575 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
576 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
577 /* The new slot fits into the existing one and comes with
578 * identical parameters - update flags and done. */
579 kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
585 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
586 kvm_physical_sync_dirty_bitmap(section
);
589 /* unregister the overlapping slot */
590 mem
->memory_size
= 0;
591 err
= kvm_set_user_memory_region(s
, mem
);
593 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
594 __func__
, strerror(-err
));
598 /* Workaround for older KVM versions: we can't join slots, even not by
599 * unregistering the previous ones and then registering the larger
600 * slot. We have to maintain the existing fragmentation. Sigh.
602 * This workaround assumes that the new slot starts at the same
603 * address as the first existing one. If not or if some overlapping
604 * slot comes around later, we will fail (not seen in practice so far)
605 * - and actually require a recent KVM version. */
606 if (s
->broken_set_mem_region
&&
607 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
608 mem
= kvm_alloc_slot(s
);
609 mem
->memory_size
= old
.memory_size
;
610 mem
->start_addr
= old
.start_addr
;
612 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
614 err
= kvm_set_user_memory_region(s
, mem
);
616 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
621 start_addr
+= old
.memory_size
;
622 ram
+= old
.memory_size
;
623 size
-= old
.memory_size
;
627 /* register prefix slot */
628 if (old
.start_addr
< start_addr
) {
629 mem
= kvm_alloc_slot(s
);
630 mem
->memory_size
= start_addr
- old
.start_addr
;
631 mem
->start_addr
= old
.start_addr
;
633 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
635 err
= kvm_set_user_memory_region(s
, mem
);
637 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
638 __func__
, strerror(-err
));
640 fprintf(stderr
, "%s: This is probably because your kernel's " \
641 "PAGE_SIZE is too big. Please try to use 4k " \
642 "PAGE_SIZE!\n", __func__
);
648 /* register suffix slot */
649 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
650 ram_addr_t size_delta
;
652 mem
= kvm_alloc_slot(s
);
653 mem
->start_addr
= start_addr
+ size
;
654 size_delta
= mem
->start_addr
- old
.start_addr
;
655 mem
->memory_size
= old
.memory_size
- size_delta
;
656 mem
->ram
= old
.ram
+ size_delta
;
657 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
659 err
= kvm_set_user_memory_region(s
, mem
);
661 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
662 __func__
, strerror(-err
));
668 /* in case the KVM bug workaround already "consumed" the new slot */
675 mem
= kvm_alloc_slot(s
);
676 mem
->memory_size
= size
;
677 mem
->start_addr
= start_addr
;
679 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
681 err
= kvm_set_user_memory_region(s
, mem
);
683 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
689 static void kvm_begin(MemoryListener
*listener
)
693 static void kvm_commit(MemoryListener
*listener
)
697 static void kvm_region_add(MemoryListener
*listener
,
698 MemoryRegionSection
*section
)
700 kvm_set_phys_mem(section
, true);
703 static void kvm_region_del(MemoryListener
*listener
,
704 MemoryRegionSection
*section
)
706 kvm_set_phys_mem(section
, false);
709 static void kvm_region_nop(MemoryListener
*listener
,
710 MemoryRegionSection
*section
)
714 static void kvm_log_sync(MemoryListener
*listener
,
715 MemoryRegionSection
*section
)
719 r
= kvm_physical_sync_dirty_bitmap(section
);
725 static void kvm_log_global_start(struct MemoryListener
*listener
)
729 r
= kvm_set_migration_log(1);
733 static void kvm_log_global_stop(struct MemoryListener
*listener
)
737 r
= kvm_set_migration_log(0);
741 static void kvm_mem_ioeventfd_add(MemoryRegionSection
*section
,
742 bool match_data
, uint64_t data
, int fd
)
746 assert(match_data
&& section
->size
<= 8);
748 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
749 data
, true, section
->size
);
755 static void kvm_mem_ioeventfd_del(MemoryRegionSection
*section
,
756 bool match_data
, uint64_t data
, int fd
)
760 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
761 data
, false, section
->size
);
767 static void kvm_io_ioeventfd_add(MemoryRegionSection
*section
,
768 bool match_data
, uint64_t data
, int fd
)
772 assert(match_data
&& section
->size
== 2);
774 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
781 static void kvm_io_ioeventfd_del(MemoryRegionSection
*section
,
782 bool match_data
, uint64_t data
, int fd
)
787 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
794 static void kvm_eventfd_add(MemoryListener
*listener
,
795 MemoryRegionSection
*section
,
796 bool match_data
, uint64_t data
, int fd
)
798 if (section
->address_space
== get_system_memory()) {
799 kvm_mem_ioeventfd_add(section
, match_data
, data
, fd
);
801 kvm_io_ioeventfd_add(section
, match_data
, data
, fd
);
805 static void kvm_eventfd_del(MemoryListener
*listener
,
806 MemoryRegionSection
*section
,
807 bool match_data
, uint64_t data
, int fd
)
809 if (section
->address_space
== get_system_memory()) {
810 kvm_mem_ioeventfd_del(section
, match_data
, data
, fd
);
812 kvm_io_ioeventfd_del(section
, match_data
, data
, fd
);
816 static MemoryListener kvm_memory_listener
= {
818 .commit
= kvm_commit
,
819 .region_add
= kvm_region_add
,
820 .region_del
= kvm_region_del
,
821 .region_nop
= kvm_region_nop
,
822 .log_start
= kvm_log_start
,
823 .log_stop
= kvm_log_stop
,
824 .log_sync
= kvm_log_sync
,
825 .log_global_start
= kvm_log_global_start
,
826 .log_global_stop
= kvm_log_global_stop
,
827 .eventfd_add
= kvm_eventfd_add
,
828 .eventfd_del
= kvm_eventfd_del
,
832 static void kvm_handle_interrupt(CPUArchState
*env
, int mask
)
834 env
->interrupt_request
|= mask
;
836 if (!qemu_cpu_is_self(env
)) {
841 int kvm_irqchip_set_irq(KVMState
*s
, int irq
, int level
)
843 struct kvm_irq_level event
;
846 assert(kvm_irqchip_in_kernel());
850 ret
= kvm_vm_ioctl(s
, s
->irqchip_inject_ioctl
, &event
);
852 perror("kvm_set_irqchip_line");
856 return (s
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
859 #ifdef KVM_CAP_IRQ_ROUTING
860 static void set_gsi(KVMState
*s
, unsigned int gsi
)
862 assert(gsi
< s
->max_gsi
);
864 s
->used_gsi_bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
867 static void kvm_init_irq_routing(KVMState
*s
)
871 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
);
873 unsigned int gsi_bits
, i
;
875 /* Round up so we can search ints using ffs */
876 gsi_bits
= ALIGN(gsi_count
, 32);
877 s
->used_gsi_bitmap
= g_malloc0(gsi_bits
/ 8);
878 s
->max_gsi
= gsi_bits
;
880 /* Mark any over-allocated bits as already in use */
881 for (i
= gsi_count
; i
< gsi_bits
; i
++) {
886 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
887 s
->nr_allocated_irq_routes
= 0;
889 kvm_arch_init_irq_routing(s
);
892 static void kvm_add_routing_entry(KVMState
*s
,
893 struct kvm_irq_routing_entry
*entry
)
895 struct kvm_irq_routing_entry
*new;
898 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
899 n
= s
->nr_allocated_irq_routes
* 2;
903 size
= sizeof(struct kvm_irq_routing
);
904 size
+= n
* sizeof(*new);
905 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
906 s
->nr_allocated_irq_routes
= n
;
908 n
= s
->irq_routes
->nr
++;
909 new = &s
->irq_routes
->entries
[n
];
910 memset(new, 0, sizeof(*new));
911 new->gsi
= entry
->gsi
;
912 new->type
= entry
->type
;
913 new->flags
= entry
->flags
;
916 set_gsi(s
, entry
->gsi
);
919 void kvm_irqchip_add_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
921 struct kvm_irq_routing_entry e
;
924 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
926 e
.u
.irqchip
.irqchip
= irqchip
;
927 e
.u
.irqchip
.pin
= pin
;
928 kvm_add_routing_entry(s
, &e
);
931 int kvm_irqchip_commit_routes(KVMState
*s
)
933 s
->irq_routes
->flags
= 0;
934 return kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
937 #else /* !KVM_CAP_IRQ_ROUTING */
939 static void kvm_init_irq_routing(KVMState
*s
)
942 #endif /* !KVM_CAP_IRQ_ROUTING */
944 static int kvm_irqchip_create(KVMState
*s
)
946 QemuOptsList
*list
= qemu_find_opts("machine");
949 if (QTAILQ_EMPTY(&list
->head
) ||
950 !qemu_opt_get_bool(QTAILQ_FIRST(&list
->head
),
951 "kernel_irqchip", false) ||
952 !kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
956 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
958 fprintf(stderr
, "Create kernel irqchip failed\n");
962 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
963 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
964 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
966 kvm_kernel_irqchip
= true;
968 kvm_init_irq_routing(s
);
975 static const char upgrade_note
[] =
976 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
977 "(see http://sourceforge.net/projects/kvm).\n";
979 const KVMCapabilityInfo
*missing_cap
;
983 s
= g_malloc0(sizeof(KVMState
));
986 * On systems where the kernel can support different base page
987 * sizes, host page size may be different from TARGET_PAGE_SIZE,
988 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
989 * page size for the system though.
991 assert(TARGET_PAGE_SIZE
<= getpagesize());
993 #ifdef KVM_CAP_SET_GUEST_DEBUG
994 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
996 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
997 s
->slots
[i
].slot
= i
;
1000 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
1002 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1007 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1008 if (ret
< KVM_API_VERSION
) {
1012 fprintf(stderr
, "kvm version too old\n");
1016 if (ret
> KVM_API_VERSION
) {
1018 fprintf(stderr
, "kvm version not supported\n");
1022 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
1025 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
1026 "your host kernel command line\n");
1032 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1035 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1039 fprintf(stderr
, "kvm does not support %s\n%s",
1040 missing_cap
->name
, upgrade_note
);
1044 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1046 s
->broken_set_mem_region
= 1;
1047 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1049 s
->broken_set_mem_region
= 0;
1052 #ifdef KVM_CAP_VCPU_EVENTS
1053 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1056 s
->robust_singlestep
=
1057 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1059 #ifdef KVM_CAP_DEBUGREGS
1060 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1063 #ifdef KVM_CAP_XSAVE
1064 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1068 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1071 #ifdef KVM_CAP_PIT_STATE2
1072 s
->pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1075 ret
= kvm_arch_init(s
);
1080 ret
= kvm_irqchip_create(s
);
1086 memory_listener_register(&kvm_memory_listener
, NULL
);
1088 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1090 cpu_interrupt_handler
= kvm_handle_interrupt
;
1108 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1112 uint8_t *ptr
= data
;
1114 for (i
= 0; i
< count
; i
++) {
1115 if (direction
== KVM_EXIT_IO_IN
) {
1118 stb_p(ptr
, cpu_inb(port
));
1121 stw_p(ptr
, cpu_inw(port
));
1124 stl_p(ptr
, cpu_inl(port
));
1130 cpu_outb(port
, ldub_p(ptr
));
1133 cpu_outw(port
, lduw_p(ptr
));
1136 cpu_outl(port
, ldl_p(ptr
));
1145 static int kvm_handle_internal_error(CPUArchState
*env
, struct kvm_run
*run
)
1147 fprintf(stderr
, "KVM internal error.");
1148 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1151 fprintf(stderr
, " Suberror: %d\n", run
->internal
.suberror
);
1152 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1153 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1154 i
, (uint64_t)run
->internal
.data
[i
]);
1157 fprintf(stderr
, "\n");
1159 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1160 fprintf(stderr
, "emulation failure\n");
1161 if (!kvm_arch_stop_on_emulation_error(env
)) {
1162 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1163 return EXCP_INTERRUPT
;
1166 /* FIXME: Should trigger a qmp message to let management know
1167 * something went wrong.
1172 void kvm_flush_coalesced_mmio_buffer(void)
1174 KVMState
*s
= kvm_state
;
1176 if (s
->coalesced_flush_in_progress
) {
1180 s
->coalesced_flush_in_progress
= true;
1182 if (s
->coalesced_mmio_ring
) {
1183 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1184 while (ring
->first
!= ring
->last
) {
1185 struct kvm_coalesced_mmio
*ent
;
1187 ent
= &ring
->coalesced_mmio
[ring
->first
];
1189 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1191 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1195 s
->coalesced_flush_in_progress
= false;
1198 static void do_kvm_cpu_synchronize_state(void *_env
)
1200 CPUArchState
*env
= _env
;
1202 if (!env
->kvm_vcpu_dirty
) {
1203 kvm_arch_get_registers(env
);
1204 env
->kvm_vcpu_dirty
= 1;
1208 void kvm_cpu_synchronize_state(CPUArchState
*env
)
1210 if (!env
->kvm_vcpu_dirty
) {
1211 run_on_cpu(env
, do_kvm_cpu_synchronize_state
, env
);
1215 void kvm_cpu_synchronize_post_reset(CPUArchState
*env
)
1217 kvm_arch_put_registers(env
, KVM_PUT_RESET_STATE
);
1218 env
->kvm_vcpu_dirty
= 0;
1221 void kvm_cpu_synchronize_post_init(CPUArchState
*env
)
1223 kvm_arch_put_registers(env
, KVM_PUT_FULL_STATE
);
1224 env
->kvm_vcpu_dirty
= 0;
1227 int kvm_cpu_exec(CPUArchState
*env
)
1229 struct kvm_run
*run
= env
->kvm_run
;
1232 DPRINTF("kvm_cpu_exec()\n");
1234 if (kvm_arch_process_async_events(env
)) {
1235 env
->exit_request
= 0;
1240 if (env
->kvm_vcpu_dirty
) {
1241 kvm_arch_put_registers(env
, KVM_PUT_RUNTIME_STATE
);
1242 env
->kvm_vcpu_dirty
= 0;
1245 kvm_arch_pre_run(env
, run
);
1246 if (env
->exit_request
) {
1247 DPRINTF("interrupt exit requested\n");
1249 * KVM requires us to reenter the kernel after IO exits to complete
1250 * instruction emulation. This self-signal will ensure that we
1253 qemu_cpu_kick_self();
1255 qemu_mutex_unlock_iothread();
1257 run_ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
1259 qemu_mutex_lock_iothread();
1260 kvm_arch_post_run(env
, run
);
1262 kvm_flush_coalesced_mmio_buffer();
1265 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1266 DPRINTF("io window exit\n");
1267 ret
= EXCP_INTERRUPT
;
1270 fprintf(stderr
, "error: kvm run failed %s\n",
1271 strerror(-run_ret
));
1275 switch (run
->exit_reason
) {
1277 DPRINTF("handle_io\n");
1278 kvm_handle_io(run
->io
.port
,
1279 (uint8_t *)run
+ run
->io
.data_offset
,
1286 DPRINTF("handle_mmio\n");
1287 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1290 run
->mmio
.is_write
);
1293 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1294 DPRINTF("irq_window_open\n");
1295 ret
= EXCP_INTERRUPT
;
1297 case KVM_EXIT_SHUTDOWN
:
1298 DPRINTF("shutdown\n");
1299 qemu_system_reset_request();
1300 ret
= EXCP_INTERRUPT
;
1302 case KVM_EXIT_UNKNOWN
:
1303 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1304 (uint64_t)run
->hw
.hardware_exit_reason
);
1307 case KVM_EXIT_INTERNAL_ERROR
:
1308 ret
= kvm_handle_internal_error(env
, run
);
1311 DPRINTF("kvm_arch_handle_exit\n");
1312 ret
= kvm_arch_handle_exit(env
, run
);
1318 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1319 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1322 env
->exit_request
= 0;
1326 int kvm_ioctl(KVMState
*s
, int type
, ...)
1333 arg
= va_arg(ap
, void *);
1336 ret
= ioctl(s
->fd
, type
, arg
);
1343 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1350 arg
= va_arg(ap
, void *);
1353 ret
= ioctl(s
->vmfd
, type
, arg
);
1360 int kvm_vcpu_ioctl(CPUArchState
*env
, int type
, ...)
1367 arg
= va_arg(ap
, void *);
1370 ret
= ioctl(env
->kvm_fd
, type
, arg
);
1377 int kvm_has_sync_mmu(void)
1379 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
1382 int kvm_has_vcpu_events(void)
1384 return kvm_state
->vcpu_events
;
1387 int kvm_has_robust_singlestep(void)
1389 return kvm_state
->robust_singlestep
;
1392 int kvm_has_debugregs(void)
1394 return kvm_state
->debugregs
;
1397 int kvm_has_xsave(void)
1399 return kvm_state
->xsave
;
1402 int kvm_has_xcrs(void)
1404 return kvm_state
->xcrs
;
1407 int kvm_has_pit_state2(void)
1409 return kvm_state
->pit_state2
;
1412 int kvm_has_many_ioeventfds(void)
1414 if (!kvm_enabled()) {
1417 return kvm_state
->many_ioeventfds
;
1420 int kvm_has_gsi_routing(void)
1422 #ifdef KVM_CAP_IRQ_ROUTING
1423 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1429 int kvm_allows_irq0_override(void)
1431 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1434 void kvm_setup_guest_memory(void *start
, size_t size
)
1436 if (!kvm_has_sync_mmu()) {
1437 int ret
= qemu_madvise(start
, size
, QEMU_MADV_DONTFORK
);
1440 perror("qemu_madvise");
1442 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1448 #ifdef KVM_CAP_SET_GUEST_DEBUG
1449 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUArchState
*env
,
1452 struct kvm_sw_breakpoint
*bp
;
1454 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1462 int kvm_sw_breakpoints_active(CPUArchState
*env
)
1464 return !QTAILQ_EMPTY(&env
->kvm_state
->kvm_sw_breakpoints
);
1467 struct kvm_set_guest_debug_data
{
1468 struct kvm_guest_debug dbg
;
1473 static void kvm_invoke_set_guest_debug(void *data
)
1475 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1476 CPUArchState
*env
= dbg_data
->env
;
1478 dbg_data
->err
= kvm_vcpu_ioctl(env
, KVM_SET_GUEST_DEBUG
, &dbg_data
->dbg
);
1481 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1483 struct kvm_set_guest_debug_data data
;
1485 data
.dbg
.control
= reinject_trap
;
1487 if (env
->singlestep_enabled
) {
1488 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1490 kvm_arch_update_guest_debug(env
, &data
.dbg
);
1493 run_on_cpu(env
, kvm_invoke_set_guest_debug
, &data
);
1497 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1498 target_ulong len
, int type
)
1500 struct kvm_sw_breakpoint
*bp
;
1504 if (type
== GDB_BREAKPOINT_SW
) {
1505 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1511 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1518 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1524 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1527 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1533 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1534 err
= kvm_update_guest_debug(env
, 0);
1542 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1543 target_ulong len
, int type
)
1545 struct kvm_sw_breakpoint
*bp
;
1549 if (type
== GDB_BREAKPOINT_SW
) {
1550 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1555 if (bp
->use_count
> 1) {
1560 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1565 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1568 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1574 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1575 err
= kvm_update_guest_debug(env
, 0);
1583 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1585 struct kvm_sw_breakpoint
*bp
, *next
;
1586 KVMState
*s
= current_env
->kvm_state
;
1589 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1590 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
1591 /* Try harder to find a CPU that currently sees the breakpoint. */
1592 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1593 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0) {
1599 kvm_arch_remove_all_hw_breakpoints();
1601 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1602 kvm_update_guest_debug(env
, 0);
1606 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1608 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1613 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1614 target_ulong len
, int type
)
1619 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1620 target_ulong len
, int type
)
1625 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1628 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1630 int kvm_set_signal_mask(CPUArchState
*env
, const sigset_t
*sigset
)
1632 struct kvm_signal_mask
*sigmask
;
1636 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1639 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1642 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1643 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1649 int kvm_set_ioeventfd_mmio(int fd
, uint32_t addr
, uint32_t val
, bool assign
,
1653 struct kvm_ioeventfd iofd
;
1655 iofd
.datamatch
= val
;
1658 iofd
.flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
;
1661 if (!kvm_enabled()) {
1666 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1669 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
1678 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1680 struct kvm_ioeventfd kick
= {
1684 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1688 if (!kvm_enabled()) {
1692 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1694 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1701 int kvm_on_sigbus_vcpu(CPUArchState
*env
, int code
, void *addr
)
1703 return kvm_arch_on_sigbus_vcpu(env
, code
, addr
);
1706 int kvm_on_sigbus(int code
, void *addr
)
1708 return kvm_arch_on_sigbus(code
, addr
);