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/atomic.h"
25 #include "qemu/option.h"
26 #include "qemu/config-file.h"
27 #include "sysemu/sysemu.h"
29 #include "hw/pci/msi.h"
30 #include "exec/gdbstub.h"
31 #include "sysemu/kvm.h"
32 #include "qemu/bswap.h"
33 #include "exec/memory.h"
34 #include "exec/address-spaces.h"
35 #include "qemu/event_notifier.h"
37 /* This check must be after config-host.h is included */
39 #include <sys/eventfd.h>
42 #ifdef CONFIG_VALGRIND_H
43 #include <valgrind/memcheck.h>
46 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
47 #define PAGE_SIZE TARGET_PAGE_SIZE
52 #define DPRINTF(fmt, ...) \
53 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
55 #define DPRINTF(fmt, ...) \
59 #define KVM_MSI_HASHTAB_SIZE 256
61 typedef struct KVMSlot
64 ram_addr_t memory_size
;
70 typedef struct kvm_dirty_log KVMDirtyLog
;
78 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
79 bool coalesced_flush_in_progress
;
80 int broken_set_mem_region
;
83 int robust_singlestep
;
85 #ifdef KVM_CAP_SET_GUEST_DEBUG
86 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
92 /* The man page (and posix) say ioctl numbers are signed int, but
93 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
94 * unsigned, and treating them as signed here can break things */
95 unsigned irq_set_ioctl
;
96 #ifdef KVM_CAP_IRQ_ROUTING
97 struct kvm_irq_routing
*irq_routes
;
98 int nr_allocated_irq_routes
;
99 uint32_t *used_gsi_bitmap
;
100 unsigned int gsi_count
;
101 QTAILQ_HEAD(msi_hashtab
, KVMMSIRoute
) msi_hashtab
[KVM_MSI_HASHTAB_SIZE
];
107 bool kvm_kernel_irqchip
;
108 bool kvm_async_interrupts_allowed
;
109 bool kvm_irqfds_allowed
;
110 bool kvm_msi_via_irqfd_allowed
;
111 bool kvm_gsi_routing_allowed
;
114 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
115 KVM_CAP_INFO(USER_MEMORY
),
116 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
120 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
124 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
125 if (s
->slots
[i
].memory_size
== 0) {
130 fprintf(stderr
, "%s: no free slot available\n", __func__
);
134 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
140 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
141 KVMSlot
*mem
= &s
->slots
[i
];
143 if (start_addr
== mem
->start_addr
&&
144 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
153 * Find overlapping slot with lowest start address
155 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
159 KVMSlot
*found
= NULL
;
162 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
163 KVMSlot
*mem
= &s
->slots
[i
];
165 if (mem
->memory_size
== 0 ||
166 (found
&& found
->start_addr
< mem
->start_addr
)) {
170 if (end_addr
> mem
->start_addr
&&
171 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
179 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
184 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
185 KVMSlot
*mem
= &s
->slots
[i
];
187 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
188 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
196 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
198 struct kvm_userspace_memory_region mem
;
200 mem
.slot
= slot
->slot
;
201 mem
.guest_phys_addr
= slot
->start_addr
;
202 mem
.memory_size
= slot
->memory_size
;
203 mem
.userspace_addr
= (unsigned long)slot
->ram
;
204 mem
.flags
= slot
->flags
;
205 if (s
->migration_log
) {
206 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
208 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
211 static void kvm_reset_vcpu(void *opaque
)
213 CPUState
*cpu
= opaque
;
215 kvm_arch_reset_vcpu(cpu
);
218 int kvm_init_vcpu(CPUState
*cpu
)
220 KVMState
*s
= kvm_state
;
224 DPRINTF("kvm_init_vcpu\n");
226 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, (void *)kvm_arch_vcpu_id(cpu
));
228 DPRINTF("kvm_create_vcpu failed\n");
234 cpu
->kvm_vcpu_dirty
= true;
236 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
239 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
243 cpu
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
245 if (cpu
->kvm_run
== MAP_FAILED
) {
247 DPRINTF("mmap'ing vcpu state failed\n");
251 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
252 s
->coalesced_mmio_ring
=
253 (void *)cpu
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
256 ret
= kvm_arch_init_vcpu(cpu
);
258 qemu_register_reset(kvm_reset_vcpu
, cpu
);
259 kvm_arch_reset_vcpu(cpu
);
266 * dirty pages logging control
269 static int kvm_mem_flags(KVMState
*s
, bool log_dirty
)
271 return log_dirty
? KVM_MEM_LOG_DIRTY_PAGES
: 0;
274 static int kvm_slot_dirty_pages_log_change(KVMSlot
*mem
, bool log_dirty
)
276 KVMState
*s
= kvm_state
;
277 int flags
, mask
= KVM_MEM_LOG_DIRTY_PAGES
;
280 old_flags
= mem
->flags
;
282 flags
= (mem
->flags
& ~mask
) | kvm_mem_flags(s
, log_dirty
);
285 /* If nothing changed effectively, no need to issue ioctl */
286 if (s
->migration_log
) {
287 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
290 if (flags
== old_flags
) {
294 return kvm_set_user_memory_region(s
, mem
);
297 static int kvm_dirty_pages_log_change(hwaddr phys_addr
,
298 ram_addr_t size
, bool log_dirty
)
300 KVMState
*s
= kvm_state
;
301 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
304 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
305 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
306 (hwaddr
)(phys_addr
+ size
- 1));
309 return kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
312 static void kvm_log_start(MemoryListener
*listener
,
313 MemoryRegionSection
*section
)
317 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
318 section
->size
, true);
324 static void kvm_log_stop(MemoryListener
*listener
,
325 MemoryRegionSection
*section
)
329 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
330 section
->size
, false);
336 static int kvm_set_migration_log(int enable
)
338 KVMState
*s
= kvm_state
;
342 s
->migration_log
= enable
;
344 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
347 if (!mem
->memory_size
) {
350 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
353 err
= kvm_set_user_memory_region(s
, mem
);
361 /* get kvm's dirty pages bitmap and update qemu's */
362 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
363 unsigned long *bitmap
)
366 unsigned long page_number
, c
;
368 unsigned int len
= ((section
->size
/ getpagesize()) + HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
369 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
372 * bitmap-traveling is faster than memory-traveling (for addr...)
373 * especially when most of the memory is not dirty.
375 for (i
= 0; i
< len
; i
++) {
376 if (bitmap
[i
] != 0) {
377 c
= leul_to_cpu(bitmap
[i
]);
381 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
382 addr1
= page_number
* TARGET_PAGE_SIZE
;
383 addr
= section
->offset_within_region
+ addr1
;
384 memory_region_set_dirty(section
->mr
, addr
,
385 TARGET_PAGE_SIZE
* hpratio
);
392 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
395 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
396 * This function updates qemu's dirty bitmap using
397 * memory_region_set_dirty(). This means all bits are set
400 * @start_add: start of logged region.
401 * @end_addr: end of logged region.
403 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection
*section
)
405 KVMState
*s
= kvm_state
;
406 unsigned long size
, allocated_size
= 0;
410 hwaddr start_addr
= section
->offset_within_address_space
;
411 hwaddr end_addr
= start_addr
+ section
->size
;
413 d
.dirty_bitmap
= NULL
;
414 while (start_addr
< end_addr
) {
415 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
420 /* XXX bad kernel interface alert
421 * For dirty bitmap, kernel allocates array of size aligned to
422 * bits-per-long. But for case when the kernel is 64bits and
423 * the userspace is 32bits, userspace can't align to the same
424 * bits-per-long, since sizeof(long) is different between kernel
425 * and user space. This way, userspace will provide buffer which
426 * may be 4 bytes less than the kernel will use, resulting in
427 * userspace memory corruption (which is not detectable by valgrind
428 * too, in most cases).
429 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
430 * a hope that sizeof(long) wont become >8 any time soon.
432 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
433 /*HOST_LONG_BITS*/ 64) / 8;
434 if (!d
.dirty_bitmap
) {
435 d
.dirty_bitmap
= g_malloc(size
);
436 } else if (size
> allocated_size
) {
437 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
439 allocated_size
= size
;
440 memset(d
.dirty_bitmap
, 0, allocated_size
);
444 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
445 DPRINTF("ioctl failed %d\n", errno
);
450 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
451 start_addr
= mem
->start_addr
+ mem
->memory_size
;
453 g_free(d
.dirty_bitmap
);
458 static void kvm_coalesce_mmio_region(MemoryListener
*listener
,
459 MemoryRegionSection
*secion
,
460 hwaddr start
, hwaddr size
)
462 KVMState
*s
= kvm_state
;
464 if (s
->coalesced_mmio
) {
465 struct kvm_coalesced_mmio_zone zone
;
471 (void)kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
475 static void kvm_uncoalesce_mmio_region(MemoryListener
*listener
,
476 MemoryRegionSection
*secion
,
477 hwaddr start
, hwaddr size
)
479 KVMState
*s
= kvm_state
;
481 if (s
->coalesced_mmio
) {
482 struct kvm_coalesced_mmio_zone zone
;
488 (void)kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
492 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
496 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
504 static int kvm_set_ioeventfd_mmio(int fd
, uint32_t addr
, uint32_t val
,
505 bool assign
, uint32_t size
, bool datamatch
)
508 struct kvm_ioeventfd iofd
;
510 iofd
.datamatch
= datamatch
? val
: 0;
516 if (!kvm_enabled()) {
521 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DATAMATCH
;
524 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
527 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
536 static int kvm_set_ioeventfd_pio(int fd
, uint16_t addr
, uint16_t val
,
537 bool assign
, uint32_t size
, bool datamatch
)
539 struct kvm_ioeventfd kick
= {
540 .datamatch
= datamatch
? val
: 0,
542 .flags
= KVM_IOEVENTFD_FLAG_PIO
,
547 if (!kvm_enabled()) {
551 kick
.flags
|= KVM_IOEVENTFD_FLAG_DATAMATCH
;
554 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
556 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
564 static int kvm_check_many_ioeventfds(void)
566 /* Userspace can use ioeventfd for io notification. This requires a host
567 * that supports eventfd(2) and an I/O thread; since eventfd does not
568 * support SIGIO it cannot interrupt the vcpu.
570 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
571 * can avoid creating too many ioeventfds.
573 #if defined(CONFIG_EVENTFD)
576 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
577 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
578 if (ioeventfds
[i
] < 0) {
581 ret
= kvm_set_ioeventfd_pio(ioeventfds
[i
], 0, i
, true, 2, true);
583 close(ioeventfds
[i
]);
588 /* Decide whether many devices are supported or not */
589 ret
= i
== ARRAY_SIZE(ioeventfds
);
592 kvm_set_ioeventfd_pio(ioeventfds
[i
], 0, i
, false, 2, true);
593 close(ioeventfds
[i
]);
601 static const KVMCapabilityInfo
*
602 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
605 if (!kvm_check_extension(s
, list
->value
)) {
613 static void kvm_set_phys_mem(MemoryRegionSection
*section
, bool add
)
615 KVMState
*s
= kvm_state
;
618 MemoryRegion
*mr
= section
->mr
;
619 bool log_dirty
= memory_region_is_logging(mr
);
620 hwaddr start_addr
= section
->offset_within_address_space
;
621 ram_addr_t size
= section
->size
;
625 /* kvm works in page size chunks, but the function may be called
626 with sub-page size and unaligned start address. */
627 delta
= TARGET_PAGE_ALIGN(size
) - size
;
633 size
&= TARGET_PAGE_MASK
;
634 if (!size
|| (start_addr
& ~TARGET_PAGE_MASK
)) {
638 if (!memory_region_is_ram(mr
)) {
642 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
645 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
650 if (add
&& start_addr
>= mem
->start_addr
&&
651 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
652 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
653 /* The new slot fits into the existing one and comes with
654 * identical parameters - update flags and done. */
655 kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
661 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
662 kvm_physical_sync_dirty_bitmap(section
);
665 /* unregister the overlapping slot */
666 mem
->memory_size
= 0;
667 err
= kvm_set_user_memory_region(s
, mem
);
669 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
670 __func__
, strerror(-err
));
674 /* Workaround for older KVM versions: we can't join slots, even not by
675 * unregistering the previous ones and then registering the larger
676 * slot. We have to maintain the existing fragmentation. Sigh.
678 * This workaround assumes that the new slot starts at the same
679 * address as the first existing one. If not or if some overlapping
680 * slot comes around later, we will fail (not seen in practice so far)
681 * - and actually require a recent KVM version. */
682 if (s
->broken_set_mem_region
&&
683 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
684 mem
= kvm_alloc_slot(s
);
685 mem
->memory_size
= old
.memory_size
;
686 mem
->start_addr
= old
.start_addr
;
688 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
690 err
= kvm_set_user_memory_region(s
, mem
);
692 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
697 start_addr
+= old
.memory_size
;
698 ram
+= old
.memory_size
;
699 size
-= old
.memory_size
;
703 /* register prefix slot */
704 if (old
.start_addr
< start_addr
) {
705 mem
= kvm_alloc_slot(s
);
706 mem
->memory_size
= start_addr
- old
.start_addr
;
707 mem
->start_addr
= old
.start_addr
;
709 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
711 err
= kvm_set_user_memory_region(s
, mem
);
713 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
714 __func__
, strerror(-err
));
716 fprintf(stderr
, "%s: This is probably because your kernel's " \
717 "PAGE_SIZE is too big. Please try to use 4k " \
718 "PAGE_SIZE!\n", __func__
);
724 /* register suffix slot */
725 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
726 ram_addr_t size_delta
;
728 mem
= kvm_alloc_slot(s
);
729 mem
->start_addr
= start_addr
+ size
;
730 size_delta
= mem
->start_addr
- old
.start_addr
;
731 mem
->memory_size
= old
.memory_size
- size_delta
;
732 mem
->ram
= old
.ram
+ size_delta
;
733 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
735 err
= kvm_set_user_memory_region(s
, mem
);
737 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
738 __func__
, strerror(-err
));
744 /* in case the KVM bug workaround already "consumed" the new slot */
751 mem
= kvm_alloc_slot(s
);
752 mem
->memory_size
= size
;
753 mem
->start_addr
= start_addr
;
755 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
757 err
= kvm_set_user_memory_region(s
, mem
);
759 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
765 static void kvm_region_add(MemoryListener
*listener
,
766 MemoryRegionSection
*section
)
768 kvm_set_phys_mem(section
, true);
771 static void kvm_region_del(MemoryListener
*listener
,
772 MemoryRegionSection
*section
)
774 kvm_set_phys_mem(section
, false);
777 static void kvm_log_sync(MemoryListener
*listener
,
778 MemoryRegionSection
*section
)
782 r
= kvm_physical_sync_dirty_bitmap(section
);
788 static void kvm_log_global_start(struct MemoryListener
*listener
)
792 r
= kvm_set_migration_log(1);
796 static void kvm_log_global_stop(struct MemoryListener
*listener
)
800 r
= kvm_set_migration_log(0);
804 static void kvm_mem_ioeventfd_add(MemoryListener
*listener
,
805 MemoryRegionSection
*section
,
806 bool match_data
, uint64_t data
,
809 int fd
= event_notifier_get_fd(e
);
812 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
813 data
, true, section
->size
, match_data
);
819 static void kvm_mem_ioeventfd_del(MemoryListener
*listener
,
820 MemoryRegionSection
*section
,
821 bool match_data
, uint64_t data
,
824 int fd
= event_notifier_get_fd(e
);
827 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
828 data
, false, section
->size
, match_data
);
834 static void kvm_io_ioeventfd_add(MemoryListener
*listener
,
835 MemoryRegionSection
*section
,
836 bool match_data
, uint64_t data
,
839 int fd
= event_notifier_get_fd(e
);
842 r
= kvm_set_ioeventfd_pio(fd
, section
->offset_within_address_space
,
843 data
, true, section
->size
, match_data
);
849 static void kvm_io_ioeventfd_del(MemoryListener
*listener
,
850 MemoryRegionSection
*section
,
851 bool match_data
, uint64_t data
,
855 int fd
= event_notifier_get_fd(e
);
858 r
= kvm_set_ioeventfd_pio(fd
, section
->offset_within_address_space
,
859 data
, false, section
->size
, match_data
);
865 static MemoryListener kvm_memory_listener
= {
866 .region_add
= kvm_region_add
,
867 .region_del
= kvm_region_del
,
868 .log_start
= kvm_log_start
,
869 .log_stop
= kvm_log_stop
,
870 .log_sync
= kvm_log_sync
,
871 .log_global_start
= kvm_log_global_start
,
872 .log_global_stop
= kvm_log_global_stop
,
873 .eventfd_add
= kvm_mem_ioeventfd_add
,
874 .eventfd_del
= kvm_mem_ioeventfd_del
,
875 .coalesced_mmio_add
= kvm_coalesce_mmio_region
,
876 .coalesced_mmio_del
= kvm_uncoalesce_mmio_region
,
880 static MemoryListener kvm_io_listener
= {
881 .eventfd_add
= kvm_io_ioeventfd_add
,
882 .eventfd_del
= kvm_io_ioeventfd_del
,
886 static void kvm_handle_interrupt(CPUState
*cpu
, int mask
)
888 cpu
->interrupt_request
|= mask
;
890 if (!qemu_cpu_is_self(cpu
)) {
895 int kvm_set_irq(KVMState
*s
, int irq
, int level
)
897 struct kvm_irq_level event
;
900 assert(kvm_async_interrupts_enabled());
904 ret
= kvm_vm_ioctl(s
, s
->irq_set_ioctl
, &event
);
906 perror("kvm_set_irq");
910 return (s
->irq_set_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
913 #ifdef KVM_CAP_IRQ_ROUTING
914 typedef struct KVMMSIRoute
{
915 struct kvm_irq_routing_entry kroute
;
916 QTAILQ_ENTRY(KVMMSIRoute
) entry
;
919 static void set_gsi(KVMState
*s
, unsigned int gsi
)
921 s
->used_gsi_bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
924 static void clear_gsi(KVMState
*s
, unsigned int gsi
)
926 s
->used_gsi_bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
929 static void kvm_init_irq_routing(KVMState
*s
)
933 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
);
935 unsigned int gsi_bits
, i
;
937 /* Round up so we can search ints using ffs */
938 gsi_bits
= ALIGN(gsi_count
, 32);
939 s
->used_gsi_bitmap
= g_malloc0(gsi_bits
/ 8);
940 s
->gsi_count
= gsi_count
;
942 /* Mark any over-allocated bits as already in use */
943 for (i
= gsi_count
; i
< gsi_bits
; i
++) {
948 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
949 s
->nr_allocated_irq_routes
= 0;
951 if (!s
->direct_msi
) {
952 for (i
= 0; i
< KVM_MSI_HASHTAB_SIZE
; i
++) {
953 QTAILQ_INIT(&s
->msi_hashtab
[i
]);
957 kvm_arch_init_irq_routing(s
);
960 static void kvm_irqchip_commit_routes(KVMState
*s
)
964 s
->irq_routes
->flags
= 0;
965 ret
= kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
969 static void kvm_add_routing_entry(KVMState
*s
,
970 struct kvm_irq_routing_entry
*entry
)
972 struct kvm_irq_routing_entry
*new;
975 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
976 n
= s
->nr_allocated_irq_routes
* 2;
980 size
= sizeof(struct kvm_irq_routing
);
981 size
+= n
* sizeof(*new);
982 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
983 s
->nr_allocated_irq_routes
= n
;
985 n
= s
->irq_routes
->nr
++;
986 new = &s
->irq_routes
->entries
[n
];
987 memset(new, 0, sizeof(*new));
988 new->gsi
= entry
->gsi
;
989 new->type
= entry
->type
;
990 new->flags
= entry
->flags
;
993 set_gsi(s
, entry
->gsi
);
995 kvm_irqchip_commit_routes(s
);
998 static int kvm_update_routing_entry(KVMState
*s
,
999 struct kvm_irq_routing_entry
*new_entry
)
1001 struct kvm_irq_routing_entry
*entry
;
1004 for (n
= 0; n
< s
->irq_routes
->nr
; n
++) {
1005 entry
= &s
->irq_routes
->entries
[n
];
1006 if (entry
->gsi
!= new_entry
->gsi
) {
1010 entry
->type
= new_entry
->type
;
1011 entry
->flags
= new_entry
->flags
;
1012 entry
->u
= new_entry
->u
;
1014 kvm_irqchip_commit_routes(s
);
1022 void kvm_irqchip_add_irq_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
1024 struct kvm_irq_routing_entry e
;
1026 assert(pin
< s
->gsi_count
);
1029 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1031 e
.u
.irqchip
.irqchip
= irqchip
;
1032 e
.u
.irqchip
.pin
= pin
;
1033 kvm_add_routing_entry(s
, &e
);
1036 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1038 struct kvm_irq_routing_entry
*e
;
1041 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
1042 e
= &s
->irq_routes
->entries
[i
];
1043 if (e
->gsi
== virq
) {
1044 s
->irq_routes
->nr
--;
1045 *e
= s
->irq_routes
->entries
[s
->irq_routes
->nr
];
1051 static unsigned int kvm_hash_msi(uint32_t data
)
1053 /* This is optimized for IA32 MSI layout. However, no other arch shall
1054 * repeat the mistake of not providing a direct MSI injection API. */
1058 static void kvm_flush_dynamic_msi_routes(KVMState
*s
)
1060 KVMMSIRoute
*route
, *next
;
1063 for (hash
= 0; hash
< KVM_MSI_HASHTAB_SIZE
; hash
++) {
1064 QTAILQ_FOREACH_SAFE(route
, &s
->msi_hashtab
[hash
], entry
, next
) {
1065 kvm_irqchip_release_virq(s
, route
->kroute
.gsi
);
1066 QTAILQ_REMOVE(&s
->msi_hashtab
[hash
], route
, entry
);
1072 static int kvm_irqchip_get_virq(KVMState
*s
)
1074 uint32_t *word
= s
->used_gsi_bitmap
;
1075 int max_words
= ALIGN(s
->gsi_count
, 32) / 32;
1080 /* Return the lowest unused GSI in the bitmap */
1081 for (i
= 0; i
< max_words
; i
++) {
1082 bit
= ffs(~word
[i
]);
1087 return bit
- 1 + i
* 32;
1089 if (!s
->direct_msi
&& retry
) {
1091 kvm_flush_dynamic_msi_routes(s
);
1098 static KVMMSIRoute
*kvm_lookup_msi_route(KVMState
*s
, MSIMessage msg
)
1100 unsigned int hash
= kvm_hash_msi(msg
.data
);
1103 QTAILQ_FOREACH(route
, &s
->msi_hashtab
[hash
], entry
) {
1104 if (route
->kroute
.u
.msi
.address_lo
== (uint32_t)msg
.address
&&
1105 route
->kroute
.u
.msi
.address_hi
== (msg
.address
>> 32) &&
1106 route
->kroute
.u
.msi
.data
== msg
.data
) {
1113 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1118 if (s
->direct_msi
) {
1119 msi
.address_lo
= (uint32_t)msg
.address
;
1120 msi
.address_hi
= msg
.address
>> 32;
1121 msi
.data
= msg
.data
;
1123 memset(msi
.pad
, 0, sizeof(msi
.pad
));
1125 return kvm_vm_ioctl(s
, KVM_SIGNAL_MSI
, &msi
);
1128 route
= kvm_lookup_msi_route(s
, msg
);
1132 virq
= kvm_irqchip_get_virq(s
);
1137 route
= g_malloc(sizeof(KVMMSIRoute
));
1138 route
->kroute
.gsi
= virq
;
1139 route
->kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1140 route
->kroute
.flags
= 0;
1141 route
->kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1142 route
->kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1143 route
->kroute
.u
.msi
.data
= msg
.data
;
1145 kvm_add_routing_entry(s
, &route
->kroute
);
1147 QTAILQ_INSERT_TAIL(&s
->msi_hashtab
[kvm_hash_msi(msg
.data
)], route
,
1151 assert(route
->kroute
.type
== KVM_IRQ_ROUTING_MSI
);
1153 return kvm_set_irq(s
, route
->kroute
.gsi
, 1);
1156 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1158 struct kvm_irq_routing_entry kroute
;
1161 if (!kvm_gsi_routing_enabled()) {
1165 virq
= kvm_irqchip_get_virq(s
);
1171 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1173 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1174 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1175 kroute
.u
.msi
.data
= msg
.data
;
1177 kvm_add_routing_entry(s
, &kroute
);
1182 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
)
1184 struct kvm_irq_routing_entry kroute
;
1186 if (!kvm_irqchip_in_kernel()) {
1191 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1193 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1194 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1195 kroute
.u
.msi
.data
= msg
.data
;
1197 return kvm_update_routing_entry(s
, &kroute
);
1200 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1202 struct kvm_irqfd irqfd
= {
1205 .flags
= assign
? 0 : KVM_IRQFD_FLAG_DEASSIGN
,
1208 if (!kvm_irqfds_enabled()) {
1212 return kvm_vm_ioctl(s
, KVM_IRQFD
, &irqfd
);
1215 #else /* !KVM_CAP_IRQ_ROUTING */
1217 static void kvm_init_irq_routing(KVMState
*s
)
1221 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1225 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1230 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1235 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1240 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
)
1244 #endif /* !KVM_CAP_IRQ_ROUTING */
1246 int kvm_irqchip_add_irqfd_notifier(KVMState
*s
, EventNotifier
*n
, int virq
)
1248 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
), virq
, true);
1251 int kvm_irqchip_remove_irqfd_notifier(KVMState
*s
, EventNotifier
*n
, int virq
)
1253 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
), virq
, false);
1256 static int kvm_irqchip_create(KVMState
*s
)
1258 QemuOptsList
*list
= qemu_find_opts("machine");
1261 if (QTAILQ_EMPTY(&list
->head
) ||
1262 !qemu_opt_get_bool(QTAILQ_FIRST(&list
->head
),
1263 "kernel_irqchip", true) ||
1264 !kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
1268 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
1270 fprintf(stderr
, "Create kernel irqchip failed\n");
1274 kvm_kernel_irqchip
= true;
1275 /* If we have an in-kernel IRQ chip then we must have asynchronous
1276 * interrupt delivery (though the reverse is not necessarily true)
1278 kvm_async_interrupts_allowed
= true;
1280 kvm_init_irq_routing(s
);
1285 static int kvm_max_vcpus(KVMState
*s
)
1289 /* Find number of supported CPUs using the recommended
1290 * procedure from the kernel API documentation to cope with
1291 * older kernels that may be missing capabilities.
1293 ret
= kvm_check_extension(s
, KVM_CAP_MAX_VCPUS
);
1297 ret
= kvm_check_extension(s
, KVM_CAP_NR_VCPUS
);
1307 static const char upgrade_note
[] =
1308 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1309 "(see http://sourceforge.net/projects/kvm).\n";
1311 const KVMCapabilityInfo
*missing_cap
;
1316 s
= g_malloc0(sizeof(KVMState
));
1319 * On systems where the kernel can support different base page
1320 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1321 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1322 * page size for the system though.
1324 assert(TARGET_PAGE_SIZE
<= getpagesize());
1326 #ifdef KVM_CAP_SET_GUEST_DEBUG
1327 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
1329 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
1330 s
->slots
[i
].slot
= i
;
1333 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
1335 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1340 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1341 if (ret
< KVM_API_VERSION
) {
1345 fprintf(stderr
, "kvm version too old\n");
1349 if (ret
> KVM_API_VERSION
) {
1351 fprintf(stderr
, "kvm version not supported\n");
1355 max_vcpus
= kvm_max_vcpus(s
);
1356 if (smp_cpus
> max_vcpus
) {
1358 fprintf(stderr
, "Number of SMP cpus requested (%d) exceeds max cpus "
1359 "supported by KVM (%d)\n", smp_cpus
, max_vcpus
);
1363 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
1366 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
1367 "your host kernel command line\n");
1373 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1376 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1380 fprintf(stderr
, "kvm does not support %s\n%s",
1381 missing_cap
->name
, upgrade_note
);
1385 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1387 s
->broken_set_mem_region
= 1;
1388 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1390 s
->broken_set_mem_region
= 0;
1393 #ifdef KVM_CAP_VCPU_EVENTS
1394 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1397 s
->robust_singlestep
=
1398 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1400 #ifdef KVM_CAP_DEBUGREGS
1401 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1404 #ifdef KVM_CAP_XSAVE
1405 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1409 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1412 #ifdef KVM_CAP_PIT_STATE2
1413 s
->pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1416 #ifdef KVM_CAP_IRQ_ROUTING
1417 s
->direct_msi
= (kvm_check_extension(s
, KVM_CAP_SIGNAL_MSI
) > 0);
1420 s
->intx_set_mask
= kvm_check_extension(s
, KVM_CAP_PCI_2_3
);
1422 s
->irq_set_ioctl
= KVM_IRQ_LINE
;
1423 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
1424 s
->irq_set_ioctl
= KVM_IRQ_LINE_STATUS
;
1427 ret
= kvm_arch_init(s
);
1432 ret
= kvm_irqchip_create(s
);
1438 memory_listener_register(&kvm_memory_listener
, &address_space_memory
);
1439 memory_listener_register(&kvm_io_listener
, &address_space_io
);
1441 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1443 cpu_interrupt_handler
= kvm_handle_interrupt
;
1459 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1463 uint8_t *ptr
= data
;
1465 for (i
= 0; i
< count
; i
++) {
1466 if (direction
== KVM_EXIT_IO_IN
) {
1469 stb_p(ptr
, cpu_inb(port
));
1472 stw_p(ptr
, cpu_inw(port
));
1475 stl_p(ptr
, cpu_inl(port
));
1481 cpu_outb(port
, ldub_p(ptr
));
1484 cpu_outw(port
, lduw_p(ptr
));
1487 cpu_outl(port
, ldl_p(ptr
));
1496 static int kvm_handle_internal_error(CPUArchState
*env
, struct kvm_run
*run
)
1498 CPUState
*cpu
= ENV_GET_CPU(env
);
1500 fprintf(stderr
, "KVM internal error.");
1501 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1504 fprintf(stderr
, " Suberror: %d\n", run
->internal
.suberror
);
1505 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1506 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1507 i
, (uint64_t)run
->internal
.data
[i
]);
1510 fprintf(stderr
, "\n");
1512 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1513 fprintf(stderr
, "emulation failure\n");
1514 if (!kvm_arch_stop_on_emulation_error(cpu
)) {
1515 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1516 return EXCP_INTERRUPT
;
1519 /* FIXME: Should trigger a qmp message to let management know
1520 * something went wrong.
1525 void kvm_flush_coalesced_mmio_buffer(void)
1527 KVMState
*s
= kvm_state
;
1529 if (s
->coalesced_flush_in_progress
) {
1533 s
->coalesced_flush_in_progress
= true;
1535 if (s
->coalesced_mmio_ring
) {
1536 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1537 while (ring
->first
!= ring
->last
) {
1538 struct kvm_coalesced_mmio
*ent
;
1540 ent
= &ring
->coalesced_mmio
[ring
->first
];
1542 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1544 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1548 s
->coalesced_flush_in_progress
= false;
1551 static void do_kvm_cpu_synchronize_state(void *arg
)
1553 CPUState
*cpu
= arg
;
1555 if (!cpu
->kvm_vcpu_dirty
) {
1556 kvm_arch_get_registers(cpu
);
1557 cpu
->kvm_vcpu_dirty
= true;
1561 void kvm_cpu_synchronize_state(CPUArchState
*env
)
1563 CPUState
*cpu
= ENV_GET_CPU(env
);
1565 if (!cpu
->kvm_vcpu_dirty
) {
1566 run_on_cpu(cpu
, do_kvm_cpu_synchronize_state
, cpu
);
1570 void kvm_cpu_synchronize_post_reset(CPUState
*cpu
)
1572 kvm_arch_put_registers(cpu
, KVM_PUT_RESET_STATE
);
1573 cpu
->kvm_vcpu_dirty
= false;
1576 void kvm_cpu_synchronize_post_init(CPUState
*cpu
)
1578 kvm_arch_put_registers(cpu
, KVM_PUT_FULL_STATE
);
1579 cpu
->kvm_vcpu_dirty
= false;
1582 int kvm_cpu_exec(CPUArchState
*env
)
1584 CPUState
*cpu
= ENV_GET_CPU(env
);
1585 struct kvm_run
*run
= cpu
->kvm_run
;
1588 DPRINTF("kvm_cpu_exec()\n");
1590 if (kvm_arch_process_async_events(cpu
)) {
1591 cpu
->exit_request
= 0;
1596 if (cpu
->kvm_vcpu_dirty
) {
1597 kvm_arch_put_registers(cpu
, KVM_PUT_RUNTIME_STATE
);
1598 cpu
->kvm_vcpu_dirty
= false;
1601 kvm_arch_pre_run(cpu
, run
);
1602 if (cpu
->exit_request
) {
1603 DPRINTF("interrupt exit requested\n");
1605 * KVM requires us to reenter the kernel after IO exits to complete
1606 * instruction emulation. This self-signal will ensure that we
1609 qemu_cpu_kick_self();
1611 qemu_mutex_unlock_iothread();
1613 run_ret
= kvm_vcpu_ioctl(cpu
, KVM_RUN
, 0);
1615 qemu_mutex_lock_iothread();
1616 kvm_arch_post_run(cpu
, run
);
1619 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1620 DPRINTF("io window exit\n");
1621 ret
= EXCP_INTERRUPT
;
1624 fprintf(stderr
, "error: kvm run failed %s\n",
1625 strerror(-run_ret
));
1629 switch (run
->exit_reason
) {
1631 DPRINTF("handle_io\n");
1632 kvm_handle_io(run
->io
.port
,
1633 (uint8_t *)run
+ run
->io
.data_offset
,
1640 DPRINTF("handle_mmio\n");
1641 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1644 run
->mmio
.is_write
);
1647 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1648 DPRINTF("irq_window_open\n");
1649 ret
= EXCP_INTERRUPT
;
1651 case KVM_EXIT_SHUTDOWN
:
1652 DPRINTF("shutdown\n");
1653 qemu_system_reset_request();
1654 ret
= EXCP_INTERRUPT
;
1656 case KVM_EXIT_UNKNOWN
:
1657 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1658 (uint64_t)run
->hw
.hardware_exit_reason
);
1661 case KVM_EXIT_INTERNAL_ERROR
:
1662 ret
= kvm_handle_internal_error(env
, run
);
1665 DPRINTF("kvm_arch_handle_exit\n");
1666 ret
= kvm_arch_handle_exit(cpu
, run
);
1672 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1673 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1676 cpu
->exit_request
= 0;
1680 int kvm_ioctl(KVMState
*s
, int type
, ...)
1687 arg
= va_arg(ap
, void *);
1690 ret
= ioctl(s
->fd
, type
, arg
);
1697 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1704 arg
= va_arg(ap
, void *);
1707 ret
= ioctl(s
->vmfd
, type
, arg
);
1714 int kvm_vcpu_ioctl(CPUState
*cpu
, int type
, ...)
1721 arg
= va_arg(ap
, void *);
1724 ret
= ioctl(cpu
->kvm_fd
, type
, arg
);
1731 int kvm_has_sync_mmu(void)
1733 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
1736 int kvm_has_vcpu_events(void)
1738 return kvm_state
->vcpu_events
;
1741 int kvm_has_robust_singlestep(void)
1743 return kvm_state
->robust_singlestep
;
1746 int kvm_has_debugregs(void)
1748 return kvm_state
->debugregs
;
1751 int kvm_has_xsave(void)
1753 return kvm_state
->xsave
;
1756 int kvm_has_xcrs(void)
1758 return kvm_state
->xcrs
;
1761 int kvm_has_pit_state2(void)
1763 return kvm_state
->pit_state2
;
1766 int kvm_has_many_ioeventfds(void)
1768 if (!kvm_enabled()) {
1771 return kvm_state
->many_ioeventfds
;
1774 int kvm_has_gsi_routing(void)
1776 #ifdef KVM_CAP_IRQ_ROUTING
1777 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1783 int kvm_has_intx_set_mask(void)
1785 return kvm_state
->intx_set_mask
;
1788 void *kvm_vmalloc(ram_addr_t size
)
1793 mem
= kvm_arch_vmalloc(size
);
1798 return qemu_vmalloc(size
);
1801 void kvm_setup_guest_memory(void *start
, size_t size
)
1803 #ifdef CONFIG_VALGRIND_H
1804 VALGRIND_MAKE_MEM_DEFINED(start
, size
);
1806 if (!kvm_has_sync_mmu()) {
1807 int ret
= qemu_madvise(start
, size
, QEMU_MADV_DONTFORK
);
1810 perror("qemu_madvise");
1812 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1818 #ifdef KVM_CAP_SET_GUEST_DEBUG
1819 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*cpu
,
1822 struct kvm_sw_breakpoint
*bp
;
1824 QTAILQ_FOREACH(bp
, &cpu
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1832 int kvm_sw_breakpoints_active(CPUState
*cpu
)
1834 return !QTAILQ_EMPTY(&cpu
->kvm_state
->kvm_sw_breakpoints
);
1837 struct kvm_set_guest_debug_data
{
1838 struct kvm_guest_debug dbg
;
1843 static void kvm_invoke_set_guest_debug(void *data
)
1845 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1847 dbg_data
->err
= kvm_vcpu_ioctl(dbg_data
->cpu
, KVM_SET_GUEST_DEBUG
,
1851 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1853 CPUState
*cpu
= ENV_GET_CPU(env
);
1854 struct kvm_set_guest_debug_data data
;
1856 data
.dbg
.control
= reinject_trap
;
1858 if (env
->singlestep_enabled
) {
1859 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1861 kvm_arch_update_guest_debug(cpu
, &data
.dbg
);
1864 run_on_cpu(cpu
, kvm_invoke_set_guest_debug
, &data
);
1868 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1869 target_ulong len
, int type
)
1871 CPUState
*current_cpu
= ENV_GET_CPU(current_env
);
1872 struct kvm_sw_breakpoint
*bp
;
1876 if (type
== GDB_BREAKPOINT_SW
) {
1877 bp
= kvm_find_sw_breakpoint(current_cpu
, addr
);
1883 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1890 err
= kvm_arch_insert_sw_breakpoint(current_cpu
, bp
);
1896 QTAILQ_INSERT_HEAD(¤t_cpu
->kvm_state
->kvm_sw_breakpoints
,
1899 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1905 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1906 err
= kvm_update_guest_debug(env
, 0);
1914 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1915 target_ulong len
, int type
)
1917 CPUState
*current_cpu
= ENV_GET_CPU(current_env
);
1918 struct kvm_sw_breakpoint
*bp
;
1922 if (type
== GDB_BREAKPOINT_SW
) {
1923 bp
= kvm_find_sw_breakpoint(current_cpu
, addr
);
1928 if (bp
->use_count
> 1) {
1933 err
= kvm_arch_remove_sw_breakpoint(current_cpu
, bp
);
1938 QTAILQ_REMOVE(¤t_cpu
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1941 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1947 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1948 err
= kvm_update_guest_debug(env
, 0);
1956 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1958 CPUState
*current_cpu
= ENV_GET_CPU(current_env
);
1959 struct kvm_sw_breakpoint
*bp
, *next
;
1960 KVMState
*s
= current_cpu
->kvm_state
;
1964 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1965 if (kvm_arch_remove_sw_breakpoint(current_cpu
, bp
) != 0) {
1966 /* Try harder to find a CPU that currently sees the breakpoint. */
1967 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1968 cpu
= ENV_GET_CPU(env
);
1969 if (kvm_arch_remove_sw_breakpoint(cpu
, bp
) == 0) {
1974 QTAILQ_REMOVE(&s
->kvm_sw_breakpoints
, bp
, entry
);
1977 kvm_arch_remove_all_hw_breakpoints();
1979 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1980 kvm_update_guest_debug(env
, 0);
1984 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1986 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1991 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1992 target_ulong len
, int type
)
1997 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1998 target_ulong len
, int type
)
2003 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
2006 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
2008 int kvm_set_signal_mask(CPUArchState
*env
, const sigset_t
*sigset
)
2010 CPUState
*cpu
= ENV_GET_CPU(env
);
2011 struct kvm_signal_mask
*sigmask
;
2015 return kvm_vcpu_ioctl(cpu
, KVM_SET_SIGNAL_MASK
, NULL
);
2018 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
2021 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
2022 r
= kvm_vcpu_ioctl(cpu
, KVM_SET_SIGNAL_MASK
, sigmask
);
2027 int kvm_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
)
2029 return kvm_arch_on_sigbus_vcpu(cpu
, code
, addr
);
2032 int kvm_on_sigbus(int code
, void *addr
)
2034 return kvm_arch_on_sigbus(code
, addr
);