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
;
113 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
114 KVM_CAP_INFO(USER_MEMORY
),
115 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
119 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
123 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
124 if (s
->slots
[i
].memory_size
== 0) {
129 fprintf(stderr
, "%s: no free slot available\n", __func__
);
133 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
139 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
140 KVMSlot
*mem
= &s
->slots
[i
];
142 if (start_addr
== mem
->start_addr
&&
143 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
152 * Find overlapping slot with lowest start address
154 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
158 KVMSlot
*found
= NULL
;
161 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
162 KVMSlot
*mem
= &s
->slots
[i
];
164 if (mem
->memory_size
== 0 ||
165 (found
&& found
->start_addr
< mem
->start_addr
)) {
169 if (end_addr
> mem
->start_addr
&&
170 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
178 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
183 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
184 KVMSlot
*mem
= &s
->slots
[i
];
186 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
187 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
195 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
197 struct kvm_userspace_memory_region mem
;
199 mem
.slot
= slot
->slot
;
200 mem
.guest_phys_addr
= slot
->start_addr
;
201 mem
.memory_size
= slot
->memory_size
;
202 mem
.userspace_addr
= (unsigned long)slot
->ram
;
203 mem
.flags
= slot
->flags
;
204 if (s
->migration_log
) {
205 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
207 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
210 static void kvm_reset_vcpu(void *opaque
)
212 CPUState
*cpu
= opaque
;
214 kvm_arch_reset_vcpu(cpu
);
217 int kvm_init_vcpu(CPUState
*cpu
)
219 KVMState
*s
= kvm_state
;
223 DPRINTF("kvm_init_vcpu\n");
225 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, (void *)kvm_arch_vcpu_id(cpu
));
227 DPRINTF("kvm_create_vcpu failed\n");
233 cpu
->kvm_vcpu_dirty
= true;
235 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
238 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
242 cpu
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
244 if (cpu
->kvm_run
== MAP_FAILED
) {
246 DPRINTF("mmap'ing vcpu state failed\n");
250 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
251 s
->coalesced_mmio_ring
=
252 (void *)cpu
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
255 ret
= kvm_arch_init_vcpu(cpu
);
257 qemu_register_reset(kvm_reset_vcpu
, cpu
);
258 kvm_arch_reset_vcpu(cpu
);
265 * dirty pages logging control
268 static int kvm_mem_flags(KVMState
*s
, bool log_dirty
)
270 return log_dirty
? KVM_MEM_LOG_DIRTY_PAGES
: 0;
273 static int kvm_slot_dirty_pages_log_change(KVMSlot
*mem
, bool log_dirty
)
275 KVMState
*s
= kvm_state
;
276 int flags
, mask
= KVM_MEM_LOG_DIRTY_PAGES
;
279 old_flags
= mem
->flags
;
281 flags
= (mem
->flags
& ~mask
) | kvm_mem_flags(s
, log_dirty
);
284 /* If nothing changed effectively, no need to issue ioctl */
285 if (s
->migration_log
) {
286 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
289 if (flags
== old_flags
) {
293 return kvm_set_user_memory_region(s
, mem
);
296 static int kvm_dirty_pages_log_change(hwaddr phys_addr
,
297 ram_addr_t size
, bool log_dirty
)
299 KVMState
*s
= kvm_state
;
300 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
303 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
304 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
305 (hwaddr
)(phys_addr
+ size
- 1));
308 return kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
311 static void kvm_log_start(MemoryListener
*listener
,
312 MemoryRegionSection
*section
)
316 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
317 section
->size
, true);
323 static void kvm_log_stop(MemoryListener
*listener
,
324 MemoryRegionSection
*section
)
328 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
329 section
->size
, false);
335 static int kvm_set_migration_log(int enable
)
337 KVMState
*s
= kvm_state
;
341 s
->migration_log
= enable
;
343 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
346 if (!mem
->memory_size
) {
349 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
352 err
= kvm_set_user_memory_region(s
, mem
);
360 /* get kvm's dirty pages bitmap and update qemu's */
361 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
362 unsigned long *bitmap
)
365 unsigned long page_number
, c
;
367 unsigned int len
= ((section
->size
/ getpagesize()) + HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
368 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
371 * bitmap-traveling is faster than memory-traveling (for addr...)
372 * especially when most of the memory is not dirty.
374 for (i
= 0; i
< len
; i
++) {
375 if (bitmap
[i
] != 0) {
376 c
= leul_to_cpu(bitmap
[i
]);
380 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
381 addr1
= page_number
* TARGET_PAGE_SIZE
;
382 addr
= section
->offset_within_region
+ addr1
;
383 memory_region_set_dirty(section
->mr
, addr
,
384 TARGET_PAGE_SIZE
* hpratio
);
391 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
394 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
395 * This function updates qemu's dirty bitmap using
396 * memory_region_set_dirty(). This means all bits are set
399 * @start_add: start of logged region.
400 * @end_addr: end of logged region.
402 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection
*section
)
404 KVMState
*s
= kvm_state
;
405 unsigned long size
, allocated_size
= 0;
409 hwaddr start_addr
= section
->offset_within_address_space
;
410 hwaddr end_addr
= start_addr
+ section
->size
;
412 d
.dirty_bitmap
= NULL
;
413 while (start_addr
< end_addr
) {
414 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
419 /* XXX bad kernel interface alert
420 * For dirty bitmap, kernel allocates array of size aligned to
421 * bits-per-long. But for case when the kernel is 64bits and
422 * the userspace is 32bits, userspace can't align to the same
423 * bits-per-long, since sizeof(long) is different between kernel
424 * and user space. This way, userspace will provide buffer which
425 * may be 4 bytes less than the kernel will use, resulting in
426 * userspace memory corruption (which is not detectable by valgrind
427 * too, in most cases).
428 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
429 * a hope that sizeof(long) wont become >8 any time soon.
431 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
432 /*HOST_LONG_BITS*/ 64) / 8;
433 if (!d
.dirty_bitmap
) {
434 d
.dirty_bitmap
= g_malloc(size
);
435 } else if (size
> allocated_size
) {
436 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
438 allocated_size
= size
;
439 memset(d
.dirty_bitmap
, 0, allocated_size
);
443 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
444 DPRINTF("ioctl failed %d\n", errno
);
449 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
450 start_addr
= mem
->start_addr
+ mem
->memory_size
;
452 g_free(d
.dirty_bitmap
);
457 static void kvm_coalesce_mmio_region(MemoryListener
*listener
,
458 MemoryRegionSection
*secion
,
459 hwaddr start
, hwaddr size
)
461 KVMState
*s
= kvm_state
;
463 if (s
->coalesced_mmio
) {
464 struct kvm_coalesced_mmio_zone zone
;
470 (void)kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
474 static void kvm_uncoalesce_mmio_region(MemoryListener
*listener
,
475 MemoryRegionSection
*secion
,
476 hwaddr start
, hwaddr size
)
478 KVMState
*s
= kvm_state
;
480 if (s
->coalesced_mmio
) {
481 struct kvm_coalesced_mmio_zone zone
;
487 (void)kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
491 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
495 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
503 static int kvm_check_many_ioeventfds(void)
505 /* Userspace can use ioeventfd for io notification. This requires a host
506 * that supports eventfd(2) and an I/O thread; since eventfd does not
507 * support SIGIO it cannot interrupt the vcpu.
509 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
510 * can avoid creating too many ioeventfds.
512 #if defined(CONFIG_EVENTFD)
515 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
516 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
517 if (ioeventfds
[i
] < 0) {
520 ret
= kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, true);
522 close(ioeventfds
[i
]);
527 /* Decide whether many devices are supported or not */
528 ret
= i
== ARRAY_SIZE(ioeventfds
);
531 kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, false);
532 close(ioeventfds
[i
]);
540 static const KVMCapabilityInfo
*
541 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
544 if (!kvm_check_extension(s
, list
->value
)) {
552 static void kvm_set_phys_mem(MemoryRegionSection
*section
, bool add
)
554 KVMState
*s
= kvm_state
;
557 MemoryRegion
*mr
= section
->mr
;
558 bool log_dirty
= memory_region_is_logging(mr
);
559 hwaddr start_addr
= section
->offset_within_address_space
;
560 ram_addr_t size
= section
->size
;
564 /* kvm works in page size chunks, but the function may be called
565 with sub-page size and unaligned start address. */
566 delta
= TARGET_PAGE_ALIGN(size
) - size
;
572 size
&= TARGET_PAGE_MASK
;
573 if (!size
|| (start_addr
& ~TARGET_PAGE_MASK
)) {
577 if (!memory_region_is_ram(mr
)) {
581 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
584 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
589 if (add
&& start_addr
>= mem
->start_addr
&&
590 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
591 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
592 /* The new slot fits into the existing one and comes with
593 * identical parameters - update flags and done. */
594 kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
600 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
601 kvm_physical_sync_dirty_bitmap(section
);
604 /* unregister the overlapping slot */
605 mem
->memory_size
= 0;
606 err
= kvm_set_user_memory_region(s
, mem
);
608 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
609 __func__
, strerror(-err
));
613 /* Workaround for older KVM versions: we can't join slots, even not by
614 * unregistering the previous ones and then registering the larger
615 * slot. We have to maintain the existing fragmentation. Sigh.
617 * This workaround assumes that the new slot starts at the same
618 * address as the first existing one. If not or if some overlapping
619 * slot comes around later, we will fail (not seen in practice so far)
620 * - and actually require a recent KVM version. */
621 if (s
->broken_set_mem_region
&&
622 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
623 mem
= kvm_alloc_slot(s
);
624 mem
->memory_size
= old
.memory_size
;
625 mem
->start_addr
= old
.start_addr
;
627 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
629 err
= kvm_set_user_memory_region(s
, mem
);
631 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
636 start_addr
+= old
.memory_size
;
637 ram
+= old
.memory_size
;
638 size
-= old
.memory_size
;
642 /* register prefix slot */
643 if (old
.start_addr
< start_addr
) {
644 mem
= kvm_alloc_slot(s
);
645 mem
->memory_size
= start_addr
- old
.start_addr
;
646 mem
->start_addr
= old
.start_addr
;
648 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
650 err
= kvm_set_user_memory_region(s
, mem
);
652 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
653 __func__
, strerror(-err
));
655 fprintf(stderr
, "%s: This is probably because your kernel's " \
656 "PAGE_SIZE is too big. Please try to use 4k " \
657 "PAGE_SIZE!\n", __func__
);
663 /* register suffix slot */
664 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
665 ram_addr_t size_delta
;
667 mem
= kvm_alloc_slot(s
);
668 mem
->start_addr
= start_addr
+ size
;
669 size_delta
= mem
->start_addr
- old
.start_addr
;
670 mem
->memory_size
= old
.memory_size
- size_delta
;
671 mem
->ram
= old
.ram
+ size_delta
;
672 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
674 err
= kvm_set_user_memory_region(s
, mem
);
676 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
677 __func__
, strerror(-err
));
683 /* in case the KVM bug workaround already "consumed" the new slot */
690 mem
= kvm_alloc_slot(s
);
691 mem
->memory_size
= size
;
692 mem
->start_addr
= start_addr
;
694 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
696 err
= kvm_set_user_memory_region(s
, mem
);
698 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
704 static void kvm_region_add(MemoryListener
*listener
,
705 MemoryRegionSection
*section
)
707 kvm_set_phys_mem(section
, true);
710 static void kvm_region_del(MemoryListener
*listener
,
711 MemoryRegionSection
*section
)
713 kvm_set_phys_mem(section
, false);
716 static void kvm_log_sync(MemoryListener
*listener
,
717 MemoryRegionSection
*section
)
721 r
= kvm_physical_sync_dirty_bitmap(section
);
727 static void kvm_log_global_start(struct MemoryListener
*listener
)
731 r
= kvm_set_migration_log(1);
735 static void kvm_log_global_stop(struct MemoryListener
*listener
)
739 r
= kvm_set_migration_log(0);
743 static void kvm_mem_ioeventfd_add(MemoryListener
*listener
,
744 MemoryRegionSection
*section
,
745 bool match_data
, uint64_t data
,
748 int fd
= event_notifier_get_fd(e
);
751 assert(match_data
&& section
->size
<= 8);
753 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
754 data
, true, section
->size
);
760 static void kvm_mem_ioeventfd_del(MemoryListener
*listener
,
761 MemoryRegionSection
*section
,
762 bool match_data
, uint64_t data
,
765 int fd
= event_notifier_get_fd(e
);
768 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
769 data
, false, section
->size
);
775 static void kvm_io_ioeventfd_add(MemoryListener
*listener
,
776 MemoryRegionSection
*section
,
777 bool match_data
, uint64_t data
,
780 int fd
= event_notifier_get_fd(e
);
783 assert(match_data
&& section
->size
== 2);
785 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
792 static void kvm_io_ioeventfd_del(MemoryListener
*listener
,
793 MemoryRegionSection
*section
,
794 bool match_data
, uint64_t data
,
798 int fd
= event_notifier_get_fd(e
);
801 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
808 static MemoryListener kvm_memory_listener
= {
809 .region_add
= kvm_region_add
,
810 .region_del
= kvm_region_del
,
811 .log_start
= kvm_log_start
,
812 .log_stop
= kvm_log_stop
,
813 .log_sync
= kvm_log_sync
,
814 .log_global_start
= kvm_log_global_start
,
815 .log_global_stop
= kvm_log_global_stop
,
816 .eventfd_add
= kvm_mem_ioeventfd_add
,
817 .eventfd_del
= kvm_mem_ioeventfd_del
,
818 .coalesced_mmio_add
= kvm_coalesce_mmio_region
,
819 .coalesced_mmio_del
= kvm_uncoalesce_mmio_region
,
823 static MemoryListener kvm_io_listener
= {
824 .eventfd_add
= kvm_io_ioeventfd_add
,
825 .eventfd_del
= kvm_io_ioeventfd_del
,
829 static void kvm_handle_interrupt(CPUArchState
*env
, int mask
)
831 CPUState
*cpu
= ENV_GET_CPU(env
);
833 env
->interrupt_request
|= mask
;
835 if (!qemu_cpu_is_self(cpu
)) {
840 int kvm_set_irq(KVMState
*s
, int irq
, int level
)
842 struct kvm_irq_level event
;
845 assert(kvm_async_interrupts_enabled());
849 ret
= kvm_vm_ioctl(s
, s
->irq_set_ioctl
, &event
);
851 perror("kvm_set_irq");
855 return (s
->irq_set_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
858 #ifdef KVM_CAP_IRQ_ROUTING
859 typedef struct KVMMSIRoute
{
860 struct kvm_irq_routing_entry kroute
;
861 QTAILQ_ENTRY(KVMMSIRoute
) entry
;
864 static void set_gsi(KVMState
*s
, unsigned int gsi
)
866 s
->used_gsi_bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
869 static void clear_gsi(KVMState
*s
, unsigned int gsi
)
871 s
->used_gsi_bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
874 static void kvm_init_irq_routing(KVMState
*s
)
878 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
);
880 unsigned int gsi_bits
, i
;
882 /* Round up so we can search ints using ffs */
883 gsi_bits
= ALIGN(gsi_count
, 32);
884 s
->used_gsi_bitmap
= g_malloc0(gsi_bits
/ 8);
885 s
->gsi_count
= gsi_count
;
887 /* Mark any over-allocated bits as already in use */
888 for (i
= gsi_count
; i
< gsi_bits
; i
++) {
893 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
894 s
->nr_allocated_irq_routes
= 0;
896 if (!s
->direct_msi
) {
897 for (i
= 0; i
< KVM_MSI_HASHTAB_SIZE
; i
++) {
898 QTAILQ_INIT(&s
->msi_hashtab
[i
]);
902 kvm_arch_init_irq_routing(s
);
905 static void kvm_irqchip_commit_routes(KVMState
*s
)
909 s
->irq_routes
->flags
= 0;
910 ret
= kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
914 static void kvm_add_routing_entry(KVMState
*s
,
915 struct kvm_irq_routing_entry
*entry
)
917 struct kvm_irq_routing_entry
*new;
920 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
921 n
= s
->nr_allocated_irq_routes
* 2;
925 size
= sizeof(struct kvm_irq_routing
);
926 size
+= n
* sizeof(*new);
927 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
928 s
->nr_allocated_irq_routes
= n
;
930 n
= s
->irq_routes
->nr
++;
931 new = &s
->irq_routes
->entries
[n
];
932 memset(new, 0, sizeof(*new));
933 new->gsi
= entry
->gsi
;
934 new->type
= entry
->type
;
935 new->flags
= entry
->flags
;
938 set_gsi(s
, entry
->gsi
);
940 kvm_irqchip_commit_routes(s
);
943 static int kvm_update_routing_entry(KVMState
*s
,
944 struct kvm_irq_routing_entry
*new_entry
)
946 struct kvm_irq_routing_entry
*entry
;
949 for (n
= 0; n
< s
->irq_routes
->nr
; n
++) {
950 entry
= &s
->irq_routes
->entries
[n
];
951 if (entry
->gsi
!= new_entry
->gsi
) {
955 entry
->type
= new_entry
->type
;
956 entry
->flags
= new_entry
->flags
;
957 entry
->u
= new_entry
->u
;
959 kvm_irqchip_commit_routes(s
);
967 void kvm_irqchip_add_irq_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
969 struct kvm_irq_routing_entry e
;
971 assert(pin
< s
->gsi_count
);
974 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
976 e
.u
.irqchip
.irqchip
= irqchip
;
977 e
.u
.irqchip
.pin
= pin
;
978 kvm_add_routing_entry(s
, &e
);
981 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
983 struct kvm_irq_routing_entry
*e
;
986 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
987 e
= &s
->irq_routes
->entries
[i
];
988 if (e
->gsi
== virq
) {
990 *e
= s
->irq_routes
->entries
[s
->irq_routes
->nr
];
996 static unsigned int kvm_hash_msi(uint32_t data
)
998 /* This is optimized for IA32 MSI layout. However, no other arch shall
999 * repeat the mistake of not providing a direct MSI injection API. */
1003 static void kvm_flush_dynamic_msi_routes(KVMState
*s
)
1005 KVMMSIRoute
*route
, *next
;
1008 for (hash
= 0; hash
< KVM_MSI_HASHTAB_SIZE
; hash
++) {
1009 QTAILQ_FOREACH_SAFE(route
, &s
->msi_hashtab
[hash
], entry
, next
) {
1010 kvm_irqchip_release_virq(s
, route
->kroute
.gsi
);
1011 QTAILQ_REMOVE(&s
->msi_hashtab
[hash
], route
, entry
);
1017 static int kvm_irqchip_get_virq(KVMState
*s
)
1019 uint32_t *word
= s
->used_gsi_bitmap
;
1020 int max_words
= ALIGN(s
->gsi_count
, 32) / 32;
1025 /* Return the lowest unused GSI in the bitmap */
1026 for (i
= 0; i
< max_words
; i
++) {
1027 bit
= ffs(~word
[i
]);
1032 return bit
- 1 + i
* 32;
1034 if (!s
->direct_msi
&& retry
) {
1036 kvm_flush_dynamic_msi_routes(s
);
1043 static KVMMSIRoute
*kvm_lookup_msi_route(KVMState
*s
, MSIMessage msg
)
1045 unsigned int hash
= kvm_hash_msi(msg
.data
);
1048 QTAILQ_FOREACH(route
, &s
->msi_hashtab
[hash
], entry
) {
1049 if (route
->kroute
.u
.msi
.address_lo
== (uint32_t)msg
.address
&&
1050 route
->kroute
.u
.msi
.address_hi
== (msg
.address
>> 32) &&
1051 route
->kroute
.u
.msi
.data
== msg
.data
) {
1058 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1063 if (s
->direct_msi
) {
1064 msi
.address_lo
= (uint32_t)msg
.address
;
1065 msi
.address_hi
= msg
.address
>> 32;
1066 msi
.data
= msg
.data
;
1068 memset(msi
.pad
, 0, sizeof(msi
.pad
));
1070 return kvm_vm_ioctl(s
, KVM_SIGNAL_MSI
, &msi
);
1073 route
= kvm_lookup_msi_route(s
, msg
);
1077 virq
= kvm_irqchip_get_virq(s
);
1082 route
= g_malloc(sizeof(KVMMSIRoute
));
1083 route
->kroute
.gsi
= virq
;
1084 route
->kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1085 route
->kroute
.flags
= 0;
1086 route
->kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1087 route
->kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1088 route
->kroute
.u
.msi
.data
= msg
.data
;
1090 kvm_add_routing_entry(s
, &route
->kroute
);
1092 QTAILQ_INSERT_TAIL(&s
->msi_hashtab
[kvm_hash_msi(msg
.data
)], route
,
1096 assert(route
->kroute
.type
== KVM_IRQ_ROUTING_MSI
);
1098 return kvm_set_irq(s
, route
->kroute
.gsi
, 1);
1101 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1103 struct kvm_irq_routing_entry kroute
;
1106 if (!kvm_gsi_routing_enabled()) {
1110 virq
= kvm_irqchip_get_virq(s
);
1116 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1118 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1119 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1120 kroute
.u
.msi
.data
= msg
.data
;
1122 kvm_add_routing_entry(s
, &kroute
);
1127 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
)
1129 struct kvm_irq_routing_entry kroute
;
1131 if (!kvm_irqchip_in_kernel()) {
1136 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1138 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1139 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1140 kroute
.u
.msi
.data
= msg
.data
;
1142 return kvm_update_routing_entry(s
, &kroute
);
1145 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1147 struct kvm_irqfd irqfd
= {
1150 .flags
= assign
? 0 : KVM_IRQFD_FLAG_DEASSIGN
,
1153 if (!kvm_irqfds_enabled()) {
1157 return kvm_vm_ioctl(s
, KVM_IRQFD
, &irqfd
);
1160 #else /* !KVM_CAP_IRQ_ROUTING */
1162 static void kvm_init_irq_routing(KVMState
*s
)
1166 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1170 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1175 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1180 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1185 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
)
1189 #endif /* !KVM_CAP_IRQ_ROUTING */
1191 int kvm_irqchip_add_irqfd_notifier(KVMState
*s
, EventNotifier
*n
, int virq
)
1193 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
), virq
, true);
1196 int kvm_irqchip_remove_irqfd_notifier(KVMState
*s
, EventNotifier
*n
, int virq
)
1198 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
), virq
, false);
1201 static int kvm_irqchip_create(KVMState
*s
)
1203 QemuOptsList
*list
= qemu_find_opts("machine");
1206 if (QTAILQ_EMPTY(&list
->head
) ||
1207 !qemu_opt_get_bool(QTAILQ_FIRST(&list
->head
),
1208 "kernel_irqchip", true) ||
1209 !kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
1213 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
1215 fprintf(stderr
, "Create kernel irqchip failed\n");
1219 kvm_kernel_irqchip
= true;
1220 /* If we have an in-kernel IRQ chip then we must have asynchronous
1221 * interrupt delivery (though the reverse is not necessarily true)
1223 kvm_async_interrupts_allowed
= true;
1225 kvm_init_irq_routing(s
);
1230 static int kvm_max_vcpus(KVMState
*s
)
1234 /* Find number of supported CPUs using the recommended
1235 * procedure from the kernel API documentation to cope with
1236 * older kernels that may be missing capabilities.
1238 ret
= kvm_check_extension(s
, KVM_CAP_MAX_VCPUS
);
1242 ret
= kvm_check_extension(s
, KVM_CAP_NR_VCPUS
);
1252 static const char upgrade_note
[] =
1253 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1254 "(see http://sourceforge.net/projects/kvm).\n";
1256 const KVMCapabilityInfo
*missing_cap
;
1261 s
= g_malloc0(sizeof(KVMState
));
1264 * On systems where the kernel can support different base page
1265 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1266 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1267 * page size for the system though.
1269 assert(TARGET_PAGE_SIZE
<= getpagesize());
1271 #ifdef KVM_CAP_SET_GUEST_DEBUG
1272 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
1274 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
1275 s
->slots
[i
].slot
= i
;
1278 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
1280 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1285 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1286 if (ret
< KVM_API_VERSION
) {
1290 fprintf(stderr
, "kvm version too old\n");
1294 if (ret
> KVM_API_VERSION
) {
1296 fprintf(stderr
, "kvm version not supported\n");
1300 max_vcpus
= kvm_max_vcpus(s
);
1301 if (smp_cpus
> max_vcpus
) {
1303 fprintf(stderr
, "Number of SMP cpus requested (%d) exceeds max cpus "
1304 "supported by KVM (%d)\n", smp_cpus
, max_vcpus
);
1308 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
1311 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
1312 "your host kernel command line\n");
1318 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1321 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1325 fprintf(stderr
, "kvm does not support %s\n%s",
1326 missing_cap
->name
, upgrade_note
);
1330 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1332 s
->broken_set_mem_region
= 1;
1333 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1335 s
->broken_set_mem_region
= 0;
1338 #ifdef KVM_CAP_VCPU_EVENTS
1339 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1342 s
->robust_singlestep
=
1343 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1345 #ifdef KVM_CAP_DEBUGREGS
1346 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1349 #ifdef KVM_CAP_XSAVE
1350 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1354 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1357 #ifdef KVM_CAP_PIT_STATE2
1358 s
->pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1361 #ifdef KVM_CAP_IRQ_ROUTING
1362 s
->direct_msi
= (kvm_check_extension(s
, KVM_CAP_SIGNAL_MSI
) > 0);
1365 s
->intx_set_mask
= kvm_check_extension(s
, KVM_CAP_PCI_2_3
);
1367 s
->irq_set_ioctl
= KVM_IRQ_LINE
;
1368 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
1369 s
->irq_set_ioctl
= KVM_IRQ_LINE_STATUS
;
1372 ret
= kvm_arch_init(s
);
1377 ret
= kvm_irqchip_create(s
);
1383 memory_listener_register(&kvm_memory_listener
, &address_space_memory
);
1384 memory_listener_register(&kvm_io_listener
, &address_space_io
);
1386 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1388 cpu_interrupt_handler
= kvm_handle_interrupt
;
1404 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1408 uint8_t *ptr
= data
;
1410 for (i
= 0; i
< count
; i
++) {
1411 if (direction
== KVM_EXIT_IO_IN
) {
1414 stb_p(ptr
, cpu_inb(port
));
1417 stw_p(ptr
, cpu_inw(port
));
1420 stl_p(ptr
, cpu_inl(port
));
1426 cpu_outb(port
, ldub_p(ptr
));
1429 cpu_outw(port
, lduw_p(ptr
));
1432 cpu_outl(port
, ldl_p(ptr
));
1441 static int kvm_handle_internal_error(CPUArchState
*env
, struct kvm_run
*run
)
1443 CPUState
*cpu
= ENV_GET_CPU(env
);
1445 fprintf(stderr
, "KVM internal error.");
1446 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1449 fprintf(stderr
, " Suberror: %d\n", run
->internal
.suberror
);
1450 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1451 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1452 i
, (uint64_t)run
->internal
.data
[i
]);
1455 fprintf(stderr
, "\n");
1457 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1458 fprintf(stderr
, "emulation failure\n");
1459 if (!kvm_arch_stop_on_emulation_error(cpu
)) {
1460 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1461 return EXCP_INTERRUPT
;
1464 /* FIXME: Should trigger a qmp message to let management know
1465 * something went wrong.
1470 void kvm_flush_coalesced_mmio_buffer(void)
1472 KVMState
*s
= kvm_state
;
1474 if (s
->coalesced_flush_in_progress
) {
1478 s
->coalesced_flush_in_progress
= true;
1480 if (s
->coalesced_mmio_ring
) {
1481 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1482 while (ring
->first
!= ring
->last
) {
1483 struct kvm_coalesced_mmio
*ent
;
1485 ent
= &ring
->coalesced_mmio
[ring
->first
];
1487 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1489 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1493 s
->coalesced_flush_in_progress
= false;
1496 static void do_kvm_cpu_synchronize_state(void *arg
)
1498 CPUState
*cpu
= arg
;
1500 if (!cpu
->kvm_vcpu_dirty
) {
1501 kvm_arch_get_registers(cpu
);
1502 cpu
->kvm_vcpu_dirty
= true;
1506 void kvm_cpu_synchronize_state(CPUArchState
*env
)
1508 CPUState
*cpu
= ENV_GET_CPU(env
);
1510 if (!cpu
->kvm_vcpu_dirty
) {
1511 run_on_cpu(cpu
, do_kvm_cpu_synchronize_state
, cpu
);
1515 void kvm_cpu_synchronize_post_reset(CPUArchState
*env
)
1517 CPUState
*cpu
= ENV_GET_CPU(env
);
1519 kvm_arch_put_registers(cpu
, KVM_PUT_RESET_STATE
);
1520 cpu
->kvm_vcpu_dirty
= false;
1523 void kvm_cpu_synchronize_post_init(CPUArchState
*env
)
1525 CPUState
*cpu
= ENV_GET_CPU(env
);
1527 kvm_arch_put_registers(cpu
, KVM_PUT_FULL_STATE
);
1528 cpu
->kvm_vcpu_dirty
= false;
1531 int kvm_cpu_exec(CPUArchState
*env
)
1533 CPUState
*cpu
= ENV_GET_CPU(env
);
1534 struct kvm_run
*run
= cpu
->kvm_run
;
1537 DPRINTF("kvm_cpu_exec()\n");
1539 if (kvm_arch_process_async_events(cpu
)) {
1540 env
->exit_request
= 0;
1545 if (cpu
->kvm_vcpu_dirty
) {
1546 kvm_arch_put_registers(cpu
, KVM_PUT_RUNTIME_STATE
);
1547 cpu
->kvm_vcpu_dirty
= false;
1550 kvm_arch_pre_run(cpu
, run
);
1551 if (env
->exit_request
) {
1552 DPRINTF("interrupt exit requested\n");
1554 * KVM requires us to reenter the kernel after IO exits to complete
1555 * instruction emulation. This self-signal will ensure that we
1558 qemu_cpu_kick_self();
1560 qemu_mutex_unlock_iothread();
1562 run_ret
= kvm_vcpu_ioctl(cpu
, KVM_RUN
, 0);
1564 qemu_mutex_lock_iothread();
1565 kvm_arch_post_run(cpu
, run
);
1568 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1569 DPRINTF("io window exit\n");
1570 ret
= EXCP_INTERRUPT
;
1573 fprintf(stderr
, "error: kvm run failed %s\n",
1574 strerror(-run_ret
));
1578 switch (run
->exit_reason
) {
1580 DPRINTF("handle_io\n");
1581 kvm_handle_io(run
->io
.port
,
1582 (uint8_t *)run
+ run
->io
.data_offset
,
1589 DPRINTF("handle_mmio\n");
1590 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1593 run
->mmio
.is_write
);
1596 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1597 DPRINTF("irq_window_open\n");
1598 ret
= EXCP_INTERRUPT
;
1600 case KVM_EXIT_SHUTDOWN
:
1601 DPRINTF("shutdown\n");
1602 qemu_system_reset_request();
1603 ret
= EXCP_INTERRUPT
;
1605 case KVM_EXIT_UNKNOWN
:
1606 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1607 (uint64_t)run
->hw
.hardware_exit_reason
);
1610 case KVM_EXIT_INTERNAL_ERROR
:
1611 ret
= kvm_handle_internal_error(env
, run
);
1614 DPRINTF("kvm_arch_handle_exit\n");
1615 ret
= kvm_arch_handle_exit(cpu
, run
);
1621 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1622 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1625 env
->exit_request
= 0;
1629 int kvm_ioctl(KVMState
*s
, int type
, ...)
1636 arg
= va_arg(ap
, void *);
1639 ret
= ioctl(s
->fd
, type
, arg
);
1646 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1653 arg
= va_arg(ap
, void *);
1656 ret
= ioctl(s
->vmfd
, type
, arg
);
1663 int kvm_vcpu_ioctl(CPUState
*cpu
, int type
, ...)
1670 arg
= va_arg(ap
, void *);
1673 ret
= ioctl(cpu
->kvm_fd
, type
, arg
);
1680 int kvm_has_sync_mmu(void)
1682 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
1685 int kvm_has_vcpu_events(void)
1687 return kvm_state
->vcpu_events
;
1690 int kvm_has_robust_singlestep(void)
1692 return kvm_state
->robust_singlestep
;
1695 int kvm_has_debugregs(void)
1697 return kvm_state
->debugregs
;
1700 int kvm_has_xsave(void)
1702 return kvm_state
->xsave
;
1705 int kvm_has_xcrs(void)
1707 return kvm_state
->xcrs
;
1710 int kvm_has_pit_state2(void)
1712 return kvm_state
->pit_state2
;
1715 int kvm_has_many_ioeventfds(void)
1717 if (!kvm_enabled()) {
1720 return kvm_state
->many_ioeventfds
;
1723 int kvm_has_gsi_routing(void)
1725 #ifdef KVM_CAP_IRQ_ROUTING
1726 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1732 int kvm_has_intx_set_mask(void)
1734 return kvm_state
->intx_set_mask
;
1737 void *kvm_vmalloc(ram_addr_t size
)
1742 mem
= kvm_arch_vmalloc(size
);
1747 return qemu_vmalloc(size
);
1750 void kvm_setup_guest_memory(void *start
, size_t size
)
1752 #ifdef CONFIG_VALGRIND_H
1753 VALGRIND_MAKE_MEM_DEFINED(start
, size
);
1755 if (!kvm_has_sync_mmu()) {
1756 int ret
= qemu_madvise(start
, size
, QEMU_MADV_DONTFORK
);
1759 perror("qemu_madvise");
1761 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1767 #ifdef KVM_CAP_SET_GUEST_DEBUG
1768 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*cpu
,
1771 struct kvm_sw_breakpoint
*bp
;
1773 QTAILQ_FOREACH(bp
, &cpu
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1781 int kvm_sw_breakpoints_active(CPUState
*cpu
)
1783 return !QTAILQ_EMPTY(&cpu
->kvm_state
->kvm_sw_breakpoints
);
1786 struct kvm_set_guest_debug_data
{
1787 struct kvm_guest_debug dbg
;
1792 static void kvm_invoke_set_guest_debug(void *data
)
1794 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1796 dbg_data
->err
= kvm_vcpu_ioctl(dbg_data
->cpu
, KVM_SET_GUEST_DEBUG
,
1800 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1802 CPUState
*cpu
= ENV_GET_CPU(env
);
1803 struct kvm_set_guest_debug_data data
;
1805 data
.dbg
.control
= reinject_trap
;
1807 if (env
->singlestep_enabled
) {
1808 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1810 kvm_arch_update_guest_debug(cpu
, &data
.dbg
);
1813 run_on_cpu(cpu
, kvm_invoke_set_guest_debug
, &data
);
1817 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1818 target_ulong len
, int type
)
1820 CPUState
*current_cpu
= ENV_GET_CPU(current_env
);
1821 struct kvm_sw_breakpoint
*bp
;
1825 if (type
== GDB_BREAKPOINT_SW
) {
1826 bp
= kvm_find_sw_breakpoint(current_cpu
, addr
);
1832 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1839 err
= kvm_arch_insert_sw_breakpoint(current_cpu
, bp
);
1845 QTAILQ_INSERT_HEAD(¤t_cpu
->kvm_state
->kvm_sw_breakpoints
,
1848 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1854 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1855 err
= kvm_update_guest_debug(env
, 0);
1863 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1864 target_ulong len
, int type
)
1866 CPUState
*current_cpu
= ENV_GET_CPU(current_env
);
1867 struct kvm_sw_breakpoint
*bp
;
1871 if (type
== GDB_BREAKPOINT_SW
) {
1872 bp
= kvm_find_sw_breakpoint(current_cpu
, addr
);
1877 if (bp
->use_count
> 1) {
1882 err
= kvm_arch_remove_sw_breakpoint(current_cpu
, bp
);
1887 QTAILQ_REMOVE(¤t_cpu
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1890 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1896 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1897 err
= kvm_update_guest_debug(env
, 0);
1905 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1907 CPUState
*current_cpu
= ENV_GET_CPU(current_env
);
1908 struct kvm_sw_breakpoint
*bp
, *next
;
1909 KVMState
*s
= current_cpu
->kvm_state
;
1913 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1914 if (kvm_arch_remove_sw_breakpoint(current_cpu
, bp
) != 0) {
1915 /* Try harder to find a CPU that currently sees the breakpoint. */
1916 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1917 cpu
= ENV_GET_CPU(env
);
1918 if (kvm_arch_remove_sw_breakpoint(cpu
, bp
) == 0) {
1923 QTAILQ_REMOVE(&s
->kvm_sw_breakpoints
, bp
, entry
);
1926 kvm_arch_remove_all_hw_breakpoints();
1928 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1929 kvm_update_guest_debug(env
, 0);
1933 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1935 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1940 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1941 target_ulong len
, int type
)
1946 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1947 target_ulong len
, int type
)
1952 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1955 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1957 int kvm_set_signal_mask(CPUArchState
*env
, const sigset_t
*sigset
)
1959 CPUState
*cpu
= ENV_GET_CPU(env
);
1960 struct kvm_signal_mask
*sigmask
;
1964 return kvm_vcpu_ioctl(cpu
, KVM_SET_SIGNAL_MASK
, NULL
);
1967 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1970 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1971 r
= kvm_vcpu_ioctl(cpu
, KVM_SET_SIGNAL_MASK
, sigmask
);
1977 int kvm_set_ioeventfd_mmio(int fd
, uint32_t addr
, uint32_t val
, bool assign
,
1981 struct kvm_ioeventfd iofd
;
1983 iofd
.datamatch
= val
;
1986 iofd
.flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
;
1989 if (!kvm_enabled()) {
1994 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1997 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
2006 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
2008 struct kvm_ioeventfd kick
= {
2012 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
2016 if (!kvm_enabled()) {
2020 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
2022 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
2029 int kvm_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
)
2031 return kvm_arch_on_sigbus_vcpu(cpu
, code
, addr
);
2034 int kvm_on_sigbus(int code
, void *addr
)
2036 return kvm_arch_on_sigbus(code
, addr
);