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 it's 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 int irqchip_inject_ioctl
;
82 #ifdef KVM_CAP_IRQ_ROUTING
83 struct kvm_irq_routing
*irq_routes
;
84 int nr_allocated_irq_routes
;
85 uint32_t *used_gsi_bitmap
;
91 bool kvm_kernel_irqchip
;
93 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
94 KVM_CAP_INFO(USER_MEMORY
),
95 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
99 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
103 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
104 if (s
->slots
[i
].memory_size
== 0) {
109 fprintf(stderr
, "%s: no free slot available\n", __func__
);
113 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
114 target_phys_addr_t start_addr
,
115 target_phys_addr_t end_addr
)
119 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
120 KVMSlot
*mem
= &s
->slots
[i
];
122 if (start_addr
== mem
->start_addr
&&
123 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
132 * Find overlapping slot with lowest start address
134 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
135 target_phys_addr_t start_addr
,
136 target_phys_addr_t end_addr
)
138 KVMSlot
*found
= NULL
;
141 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
142 KVMSlot
*mem
= &s
->slots
[i
];
144 if (mem
->memory_size
== 0 ||
145 (found
&& found
->start_addr
< mem
->start_addr
)) {
149 if (end_addr
> mem
->start_addr
&&
150 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
158 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
159 target_phys_addr_t
*phys_addr
)
163 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
164 KVMSlot
*mem
= &s
->slots
[i
];
166 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
167 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
175 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
177 struct kvm_userspace_memory_region mem
;
179 mem
.slot
= slot
->slot
;
180 mem
.guest_phys_addr
= slot
->start_addr
;
181 mem
.memory_size
= slot
->memory_size
;
182 mem
.userspace_addr
= (unsigned long)slot
->ram
;
183 mem
.flags
= slot
->flags
;
184 if (s
->migration_log
) {
185 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
187 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
190 static void kvm_reset_vcpu(void *opaque
)
192 CPUState
*env
= opaque
;
194 kvm_arch_reset_vcpu(env
);
197 int kvm_pit_in_kernel(void)
199 return kvm_state
->pit_in_kernel
;
202 int kvm_init_vcpu(CPUState
*env
)
204 KVMState
*s
= kvm_state
;
208 DPRINTF("kvm_init_vcpu\n");
210 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
212 DPRINTF("kvm_create_vcpu failed\n");
218 env
->kvm_vcpu_dirty
= 1;
220 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
223 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
227 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
229 if (env
->kvm_run
== MAP_FAILED
) {
231 DPRINTF("mmap'ing vcpu state failed\n");
235 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
236 s
->coalesced_mmio_ring
=
237 (void *)env
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
240 ret
= kvm_arch_init_vcpu(env
);
242 qemu_register_reset(kvm_reset_vcpu
, env
);
243 kvm_arch_reset_vcpu(env
);
250 * dirty pages logging control
253 static int kvm_mem_flags(KVMState
*s
, bool log_dirty
)
255 return log_dirty
? KVM_MEM_LOG_DIRTY_PAGES
: 0;
258 static int kvm_slot_dirty_pages_log_change(KVMSlot
*mem
, bool log_dirty
)
260 KVMState
*s
= kvm_state
;
261 int flags
, mask
= KVM_MEM_LOG_DIRTY_PAGES
;
264 old_flags
= mem
->flags
;
266 flags
= (mem
->flags
& ~mask
) | kvm_mem_flags(s
, log_dirty
);
269 /* If nothing changed effectively, no need to issue ioctl */
270 if (s
->migration_log
) {
271 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
274 if (flags
== old_flags
) {
278 return kvm_set_user_memory_region(s
, mem
);
281 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr
,
282 ram_addr_t size
, bool log_dirty
)
284 KVMState
*s
= kvm_state
;
285 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
288 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
289 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
290 (target_phys_addr_t
)(phys_addr
+ size
- 1));
293 return kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
296 static void kvm_log_start(MemoryListener
*listener
,
297 MemoryRegionSection
*section
)
301 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
302 section
->size
, true);
308 static void kvm_log_stop(MemoryListener
*listener
,
309 MemoryRegionSection
*section
)
313 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
314 section
->size
, false);
320 static int kvm_set_migration_log(int enable
)
322 KVMState
*s
= kvm_state
;
326 s
->migration_log
= enable
;
328 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
331 if (!mem
->memory_size
) {
334 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
337 err
= kvm_set_user_memory_region(s
, mem
);
345 /* get kvm's dirty pages bitmap and update qemu's */
346 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
347 unsigned long *bitmap
)
350 unsigned long page_number
, c
;
351 target_phys_addr_t addr
, addr1
;
352 unsigned int len
= ((section
->size
/ TARGET_PAGE_SIZE
) + HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
355 * bitmap-traveling is faster than memory-traveling (for addr...)
356 * especially when most of the memory is not dirty.
358 for (i
= 0; i
< len
; i
++) {
359 if (bitmap
[i
] != 0) {
360 c
= leul_to_cpu(bitmap
[i
]);
364 page_number
= i
* HOST_LONG_BITS
+ j
;
365 addr1
= page_number
* TARGET_PAGE_SIZE
;
366 addr
= section
->offset_within_region
+ addr1
;
367 memory_region_set_dirty(section
->mr
, addr
, TARGET_PAGE_SIZE
);
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
;
451 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
457 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
460 KVMState
*s
= kvm_state
;
462 if (s
->coalesced_mmio
) {
463 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
== 4);
746 r
= kvm_set_ioeventfd_mmio_long(fd
, section
->offset_within_address_space
,
753 static void kvm_mem_ioeventfd_del(MemoryRegionSection
*section
,
754 bool match_data
, uint64_t data
, int fd
)
758 r
= kvm_set_ioeventfd_mmio_long(fd
, section
->offset_within_address_space
,
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(CPUState
*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
= (gsi_count
+ 31) / 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 ret
= kvm_arch_init(s
);
1066 ret
= kvm_irqchip_create(s
);
1072 memory_listener_register(&kvm_memory_listener
, NULL
);
1074 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1076 cpu_interrupt_handler
= kvm_handle_interrupt
;
1094 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1098 uint8_t *ptr
= data
;
1100 for (i
= 0; i
< count
; i
++) {
1101 if (direction
== KVM_EXIT_IO_IN
) {
1104 stb_p(ptr
, cpu_inb(port
));
1107 stw_p(ptr
, cpu_inw(port
));
1110 stl_p(ptr
, cpu_inl(port
));
1116 cpu_outb(port
, ldub_p(ptr
));
1119 cpu_outw(port
, lduw_p(ptr
));
1122 cpu_outl(port
, ldl_p(ptr
));
1131 static int kvm_handle_internal_error(CPUState
*env
, struct kvm_run
*run
)
1133 fprintf(stderr
, "KVM internal error.");
1134 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1137 fprintf(stderr
, " Suberror: %d\n", run
->internal
.suberror
);
1138 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1139 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1140 i
, (uint64_t)run
->internal
.data
[i
]);
1143 fprintf(stderr
, "\n");
1145 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1146 fprintf(stderr
, "emulation failure\n");
1147 if (!kvm_arch_stop_on_emulation_error(env
)) {
1148 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1149 return EXCP_INTERRUPT
;
1152 /* FIXME: Should trigger a qmp message to let management know
1153 * something went wrong.
1158 void kvm_flush_coalesced_mmio_buffer(void)
1160 KVMState
*s
= kvm_state
;
1162 if (s
->coalesced_flush_in_progress
) {
1166 s
->coalesced_flush_in_progress
= true;
1168 if (s
->coalesced_mmio_ring
) {
1169 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1170 while (ring
->first
!= ring
->last
) {
1171 struct kvm_coalesced_mmio
*ent
;
1173 ent
= &ring
->coalesced_mmio
[ring
->first
];
1175 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1177 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1181 s
->coalesced_flush_in_progress
= false;
1184 static void do_kvm_cpu_synchronize_state(void *_env
)
1186 CPUState
*env
= _env
;
1188 if (!env
->kvm_vcpu_dirty
) {
1189 kvm_arch_get_registers(env
);
1190 env
->kvm_vcpu_dirty
= 1;
1194 void kvm_cpu_synchronize_state(CPUState
*env
)
1196 if (!env
->kvm_vcpu_dirty
) {
1197 run_on_cpu(env
, do_kvm_cpu_synchronize_state
, env
);
1201 void kvm_cpu_synchronize_post_reset(CPUState
*env
)
1203 kvm_arch_put_registers(env
, KVM_PUT_RESET_STATE
);
1204 env
->kvm_vcpu_dirty
= 0;
1207 void kvm_cpu_synchronize_post_init(CPUState
*env
)
1209 kvm_arch_put_registers(env
, KVM_PUT_FULL_STATE
);
1210 env
->kvm_vcpu_dirty
= 0;
1213 int kvm_cpu_exec(CPUState
*env
)
1215 struct kvm_run
*run
= env
->kvm_run
;
1218 DPRINTF("kvm_cpu_exec()\n");
1220 if (kvm_arch_process_async_events(env
)) {
1221 env
->exit_request
= 0;
1226 if (env
->kvm_vcpu_dirty
) {
1227 kvm_arch_put_registers(env
, KVM_PUT_RUNTIME_STATE
);
1228 env
->kvm_vcpu_dirty
= 0;
1231 kvm_arch_pre_run(env
, run
);
1232 if (env
->exit_request
) {
1233 DPRINTF("interrupt exit requested\n");
1235 * KVM requires us to reenter the kernel after IO exits to complete
1236 * instruction emulation. This self-signal will ensure that we
1239 qemu_cpu_kick_self();
1241 qemu_mutex_unlock_iothread();
1243 run_ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
1245 qemu_mutex_lock_iothread();
1246 kvm_arch_post_run(env
, run
);
1248 kvm_flush_coalesced_mmio_buffer();
1251 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1252 DPRINTF("io window exit\n");
1253 ret
= EXCP_INTERRUPT
;
1256 fprintf(stderr
, "error: kvm run failed %s\n",
1257 strerror(-run_ret
));
1261 switch (run
->exit_reason
) {
1263 DPRINTF("handle_io\n");
1264 kvm_handle_io(run
->io
.port
,
1265 (uint8_t *)run
+ run
->io
.data_offset
,
1272 DPRINTF("handle_mmio\n");
1273 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1276 run
->mmio
.is_write
);
1279 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1280 DPRINTF("irq_window_open\n");
1281 ret
= EXCP_INTERRUPT
;
1283 case KVM_EXIT_SHUTDOWN
:
1284 DPRINTF("shutdown\n");
1285 qemu_system_reset_request();
1286 ret
= EXCP_INTERRUPT
;
1288 case KVM_EXIT_UNKNOWN
:
1289 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1290 (uint64_t)run
->hw
.hardware_exit_reason
);
1293 case KVM_EXIT_INTERNAL_ERROR
:
1294 ret
= kvm_handle_internal_error(env
, run
);
1297 DPRINTF("kvm_arch_handle_exit\n");
1298 ret
= kvm_arch_handle_exit(env
, run
);
1304 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1305 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1308 env
->exit_request
= 0;
1312 int kvm_ioctl(KVMState
*s
, int type
, ...)
1319 arg
= va_arg(ap
, void *);
1322 ret
= ioctl(s
->fd
, type
, arg
);
1329 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1336 arg
= va_arg(ap
, void *);
1339 ret
= ioctl(s
->vmfd
, type
, arg
);
1346 int kvm_vcpu_ioctl(CPUState
*env
, int type
, ...)
1353 arg
= va_arg(ap
, void *);
1356 ret
= ioctl(env
->kvm_fd
, type
, arg
);
1363 int kvm_has_sync_mmu(void)
1365 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
1368 int kvm_has_vcpu_events(void)
1370 return kvm_state
->vcpu_events
;
1373 int kvm_has_robust_singlestep(void)
1375 return kvm_state
->robust_singlestep
;
1378 int kvm_has_debugregs(void)
1380 return kvm_state
->debugregs
;
1383 int kvm_has_xsave(void)
1385 return kvm_state
->xsave
;
1388 int kvm_has_xcrs(void)
1390 return kvm_state
->xcrs
;
1393 int kvm_has_many_ioeventfds(void)
1395 if (!kvm_enabled()) {
1398 return kvm_state
->many_ioeventfds
;
1401 int kvm_has_gsi_routing(void)
1403 #ifdef KVM_CAP_IRQ_ROUTING
1404 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1410 int kvm_allows_irq0_override(void)
1412 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1415 void kvm_setup_guest_memory(void *start
, size_t size
)
1417 if (!kvm_has_sync_mmu()) {
1418 int ret
= qemu_madvise(start
, size
, QEMU_MADV_DONTFORK
);
1421 perror("qemu_madvise");
1423 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1429 #ifdef KVM_CAP_SET_GUEST_DEBUG
1430 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*env
,
1433 struct kvm_sw_breakpoint
*bp
;
1435 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1443 int kvm_sw_breakpoints_active(CPUState
*env
)
1445 return !QTAILQ_EMPTY(&env
->kvm_state
->kvm_sw_breakpoints
);
1448 struct kvm_set_guest_debug_data
{
1449 struct kvm_guest_debug dbg
;
1454 static void kvm_invoke_set_guest_debug(void *data
)
1456 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1457 CPUState
*env
= dbg_data
->env
;
1459 dbg_data
->err
= kvm_vcpu_ioctl(env
, KVM_SET_GUEST_DEBUG
, &dbg_data
->dbg
);
1462 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
1464 struct kvm_set_guest_debug_data data
;
1466 data
.dbg
.control
= reinject_trap
;
1468 if (env
->singlestep_enabled
) {
1469 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1471 kvm_arch_update_guest_debug(env
, &data
.dbg
);
1474 run_on_cpu(env
, kvm_invoke_set_guest_debug
, &data
);
1478 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
1479 target_ulong len
, int type
)
1481 struct kvm_sw_breakpoint
*bp
;
1485 if (type
== GDB_BREAKPOINT_SW
) {
1486 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1492 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1499 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1505 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1508 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1514 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1515 err
= kvm_update_guest_debug(env
, 0);
1523 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
1524 target_ulong len
, int type
)
1526 struct kvm_sw_breakpoint
*bp
;
1530 if (type
== GDB_BREAKPOINT_SW
) {
1531 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1536 if (bp
->use_count
> 1) {
1541 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1546 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1549 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1555 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1556 err
= kvm_update_guest_debug(env
, 0);
1564 void kvm_remove_all_breakpoints(CPUState
*current_env
)
1566 struct kvm_sw_breakpoint
*bp
, *next
;
1567 KVMState
*s
= current_env
->kvm_state
;
1570 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1571 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
1572 /* Try harder to find a CPU that currently sees the breakpoint. */
1573 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1574 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0) {
1580 kvm_arch_remove_all_hw_breakpoints();
1582 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1583 kvm_update_guest_debug(env
, 0);
1587 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1589 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
1594 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
1595 target_ulong len
, int type
)
1600 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
1601 target_ulong len
, int type
)
1606 void kvm_remove_all_breakpoints(CPUState
*current_env
)
1609 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1611 int kvm_set_signal_mask(CPUState
*env
, const sigset_t
*sigset
)
1613 struct kvm_signal_mask
*sigmask
;
1617 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1620 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1623 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1624 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1630 int kvm_set_ioeventfd_mmio_long(int fd
, uint32_t addr
, uint32_t val
, bool assign
)
1633 struct kvm_ioeventfd iofd
;
1635 iofd
.datamatch
= val
;
1638 iofd
.flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
;
1641 if (!kvm_enabled()) {
1646 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1649 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
1658 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1660 struct kvm_ioeventfd kick
= {
1664 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1668 if (!kvm_enabled()) {
1672 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1674 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1681 int kvm_on_sigbus_vcpu(CPUState
*env
, int code
, void *addr
)
1683 return kvm_arch_on_sigbus_vcpu(env
, code
, addr
);
1686 int kvm_on_sigbus(int code
, void *addr
)
1688 return kvm_arch_on_sigbus(code
, addr
);