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"
25 #include "qemu-option.h"
26 #include "qemu-config.h"
34 #include "exec-memory.h"
36 /* This check must be after config-host.h is included */
38 #include <sys/eventfd.h>
41 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
42 #define PAGE_SIZE TARGET_PAGE_SIZE
47 #define DPRINTF(fmt, ...) \
48 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
50 #define DPRINTF(fmt, ...) \
54 #define KVM_MSI_HASHTAB_SIZE 256
56 typedef struct KVMSlot
58 target_phys_addr_t start_addr
;
59 ram_addr_t memory_size
;
65 typedef struct kvm_dirty_log KVMDirtyLog
;
73 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
74 bool coalesced_flush_in_progress
;
75 int broken_set_mem_region
;
78 int robust_singlestep
;
80 #ifdef KVM_CAP_SET_GUEST_DEBUG
81 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
86 /* The man page (and posix) say ioctl numbers are signed int, but
87 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
88 * unsigned, and treating them as signed here can break things */
89 unsigned irqchip_inject_ioctl
;
90 #ifdef KVM_CAP_IRQ_ROUTING
91 struct kvm_irq_routing
*irq_routes
;
92 int nr_allocated_irq_routes
;
93 uint32_t *used_gsi_bitmap
;
94 unsigned int gsi_count
;
95 QTAILQ_HEAD(msi_hashtab
, KVMMSIRoute
) msi_hashtab
[KVM_MSI_HASHTAB_SIZE
];
101 bool kvm_kernel_irqchip
;
103 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
104 KVM_CAP_INFO(USER_MEMORY
),
105 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
109 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
113 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
114 if (s
->slots
[i
].memory_size
== 0) {
119 fprintf(stderr
, "%s: no free slot available\n", __func__
);
123 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
124 target_phys_addr_t start_addr
,
125 target_phys_addr_t end_addr
)
129 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
130 KVMSlot
*mem
= &s
->slots
[i
];
132 if (start_addr
== mem
->start_addr
&&
133 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
142 * Find overlapping slot with lowest start address
144 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
145 target_phys_addr_t start_addr
,
146 target_phys_addr_t end_addr
)
148 KVMSlot
*found
= NULL
;
151 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
152 KVMSlot
*mem
= &s
->slots
[i
];
154 if (mem
->memory_size
== 0 ||
155 (found
&& found
->start_addr
< mem
->start_addr
)) {
159 if (end_addr
> mem
->start_addr
&&
160 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
168 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
169 target_phys_addr_t
*phys_addr
)
173 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
174 KVMSlot
*mem
= &s
->slots
[i
];
176 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
177 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
185 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
187 struct kvm_userspace_memory_region mem
;
189 mem
.slot
= slot
->slot
;
190 mem
.guest_phys_addr
= slot
->start_addr
;
191 mem
.memory_size
= slot
->memory_size
;
192 mem
.userspace_addr
= (unsigned long)slot
->ram
;
193 mem
.flags
= slot
->flags
;
194 if (s
->migration_log
) {
195 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
197 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
200 static void kvm_reset_vcpu(void *opaque
)
202 CPUArchState
*env
= opaque
;
204 kvm_arch_reset_vcpu(env
);
207 int kvm_init_vcpu(CPUArchState
*env
)
209 KVMState
*s
= kvm_state
;
213 DPRINTF("kvm_init_vcpu\n");
215 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
217 DPRINTF("kvm_create_vcpu failed\n");
223 env
->kvm_vcpu_dirty
= 1;
225 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
228 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
232 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
234 if (env
->kvm_run
== MAP_FAILED
) {
236 DPRINTF("mmap'ing vcpu state failed\n");
240 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
241 s
->coalesced_mmio_ring
=
242 (void *)env
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
245 ret
= kvm_arch_init_vcpu(env
);
247 qemu_register_reset(kvm_reset_vcpu
, env
);
248 kvm_arch_reset_vcpu(env
);
255 * dirty pages logging control
258 static int kvm_mem_flags(KVMState
*s
, bool log_dirty
)
260 return log_dirty
? KVM_MEM_LOG_DIRTY_PAGES
: 0;
263 static int kvm_slot_dirty_pages_log_change(KVMSlot
*mem
, bool log_dirty
)
265 KVMState
*s
= kvm_state
;
266 int flags
, mask
= KVM_MEM_LOG_DIRTY_PAGES
;
269 old_flags
= mem
->flags
;
271 flags
= (mem
->flags
& ~mask
) | kvm_mem_flags(s
, log_dirty
);
274 /* If nothing changed effectively, no need to issue ioctl */
275 if (s
->migration_log
) {
276 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
279 if (flags
== old_flags
) {
283 return kvm_set_user_memory_region(s
, mem
);
286 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr
,
287 ram_addr_t size
, bool log_dirty
)
289 KVMState
*s
= kvm_state
;
290 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
293 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
294 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
295 (target_phys_addr_t
)(phys_addr
+ size
- 1));
298 return kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
301 static void kvm_log_start(MemoryListener
*listener
,
302 MemoryRegionSection
*section
)
306 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
307 section
->size
, true);
313 static void kvm_log_stop(MemoryListener
*listener
,
314 MemoryRegionSection
*section
)
318 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
319 section
->size
, false);
325 static int kvm_set_migration_log(int enable
)
327 KVMState
*s
= kvm_state
;
331 s
->migration_log
= enable
;
333 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
336 if (!mem
->memory_size
) {
339 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
342 err
= kvm_set_user_memory_region(s
, mem
);
350 /* get kvm's dirty pages bitmap and update qemu's */
351 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
352 unsigned long *bitmap
)
355 unsigned long page_number
, c
;
356 target_phys_addr_t addr
, addr1
;
357 unsigned int len
= ((section
->size
/ TARGET_PAGE_SIZE
) + HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
358 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
361 * bitmap-traveling is faster than memory-traveling (for addr...)
362 * especially when most of the memory is not dirty.
364 for (i
= 0; i
< len
; i
++) {
365 if (bitmap
[i
] != 0) {
366 c
= leul_to_cpu(bitmap
[i
]);
370 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
371 addr1
= page_number
* TARGET_PAGE_SIZE
;
372 addr
= section
->offset_within_region
+ addr1
;
373 memory_region_set_dirty(section
->mr
, addr
,
374 TARGET_PAGE_SIZE
* hpratio
);
381 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
384 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
385 * This function updates qemu's dirty bitmap using
386 * memory_region_set_dirty(). This means all bits are set
389 * @start_add: start of logged region.
390 * @end_addr: end of logged region.
392 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection
*section
)
394 KVMState
*s
= kvm_state
;
395 unsigned long size
, allocated_size
= 0;
399 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
400 target_phys_addr_t end_addr
= start_addr
+ section
->size
;
402 d
.dirty_bitmap
= NULL
;
403 while (start_addr
< end_addr
) {
404 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
409 /* XXX bad kernel interface alert
410 * For dirty bitmap, kernel allocates array of size aligned to
411 * bits-per-long. But for case when the kernel is 64bits and
412 * the userspace is 32bits, userspace can't align to the same
413 * bits-per-long, since sizeof(long) is different between kernel
414 * and user space. This way, userspace will provide buffer which
415 * may be 4 bytes less than the kernel will use, resulting in
416 * userspace memory corruption (which is not detectable by valgrind
417 * too, in most cases).
418 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
419 * a hope that sizeof(long) wont become >8 any time soon.
421 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
422 /*HOST_LONG_BITS*/ 64) / 8;
423 if (!d
.dirty_bitmap
) {
424 d
.dirty_bitmap
= g_malloc(size
);
425 } else if (size
> allocated_size
) {
426 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
428 allocated_size
= size
;
429 memset(d
.dirty_bitmap
, 0, allocated_size
);
433 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
434 DPRINTF("ioctl failed %d\n", errno
);
439 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
440 start_addr
= mem
->start_addr
+ mem
->memory_size
;
442 g_free(d
.dirty_bitmap
);
447 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
450 KVMState
*s
= kvm_state
;
452 if (s
->coalesced_mmio
) {
453 struct kvm_coalesced_mmio_zone zone
;
459 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
465 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
468 KVMState
*s
= kvm_state
;
470 if (s
->coalesced_mmio
) {
471 struct kvm_coalesced_mmio_zone zone
;
477 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
483 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
487 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
495 static int kvm_check_many_ioeventfds(void)
497 /* Userspace can use ioeventfd for io notification. This requires a host
498 * that supports eventfd(2) and an I/O thread; since eventfd does not
499 * support SIGIO it cannot interrupt the vcpu.
501 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
502 * can avoid creating too many ioeventfds.
504 #if defined(CONFIG_EVENTFD)
507 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
508 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
509 if (ioeventfds
[i
] < 0) {
512 ret
= kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, true);
514 close(ioeventfds
[i
]);
519 /* Decide whether many devices are supported or not */
520 ret
= i
== ARRAY_SIZE(ioeventfds
);
523 kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, false);
524 close(ioeventfds
[i
]);
532 static const KVMCapabilityInfo
*
533 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
536 if (!kvm_check_extension(s
, list
->value
)) {
544 static void kvm_set_phys_mem(MemoryRegionSection
*section
, bool add
)
546 KVMState
*s
= kvm_state
;
549 MemoryRegion
*mr
= section
->mr
;
550 bool log_dirty
= memory_region_is_logging(mr
);
551 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
552 ram_addr_t size
= section
->size
;
556 /* kvm works in page size chunks, but the function may be called
557 with sub-page size and unaligned start address. */
558 delta
= TARGET_PAGE_ALIGN(size
) - size
;
564 size
&= TARGET_PAGE_MASK
;
565 if (!size
|| (start_addr
& ~TARGET_PAGE_MASK
)) {
569 if (!memory_region_is_ram(mr
)) {
573 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
576 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
581 if (add
&& start_addr
>= mem
->start_addr
&&
582 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
583 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
584 /* The new slot fits into the existing one and comes with
585 * identical parameters - update flags and done. */
586 kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
592 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
593 kvm_physical_sync_dirty_bitmap(section
);
596 /* unregister the overlapping slot */
597 mem
->memory_size
= 0;
598 err
= kvm_set_user_memory_region(s
, mem
);
600 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
601 __func__
, strerror(-err
));
605 /* Workaround for older KVM versions: we can't join slots, even not by
606 * unregistering the previous ones and then registering the larger
607 * slot. We have to maintain the existing fragmentation. Sigh.
609 * This workaround assumes that the new slot starts at the same
610 * address as the first existing one. If not or if some overlapping
611 * slot comes around later, we will fail (not seen in practice so far)
612 * - and actually require a recent KVM version. */
613 if (s
->broken_set_mem_region
&&
614 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
615 mem
= kvm_alloc_slot(s
);
616 mem
->memory_size
= old
.memory_size
;
617 mem
->start_addr
= old
.start_addr
;
619 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
621 err
= kvm_set_user_memory_region(s
, mem
);
623 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
628 start_addr
+= old
.memory_size
;
629 ram
+= old
.memory_size
;
630 size
-= old
.memory_size
;
634 /* register prefix slot */
635 if (old
.start_addr
< start_addr
) {
636 mem
= kvm_alloc_slot(s
);
637 mem
->memory_size
= start_addr
- old
.start_addr
;
638 mem
->start_addr
= old
.start_addr
;
640 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
642 err
= kvm_set_user_memory_region(s
, mem
);
644 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
645 __func__
, strerror(-err
));
647 fprintf(stderr
, "%s: This is probably because your kernel's " \
648 "PAGE_SIZE is too big. Please try to use 4k " \
649 "PAGE_SIZE!\n", __func__
);
655 /* register suffix slot */
656 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
657 ram_addr_t size_delta
;
659 mem
= kvm_alloc_slot(s
);
660 mem
->start_addr
= start_addr
+ size
;
661 size_delta
= mem
->start_addr
- old
.start_addr
;
662 mem
->memory_size
= old
.memory_size
- size_delta
;
663 mem
->ram
= old
.ram
+ size_delta
;
664 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
666 err
= kvm_set_user_memory_region(s
, mem
);
668 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
669 __func__
, strerror(-err
));
675 /* in case the KVM bug workaround already "consumed" the new slot */
682 mem
= kvm_alloc_slot(s
);
683 mem
->memory_size
= size
;
684 mem
->start_addr
= start_addr
;
686 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
688 err
= kvm_set_user_memory_region(s
, mem
);
690 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
696 static void kvm_begin(MemoryListener
*listener
)
700 static void kvm_commit(MemoryListener
*listener
)
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_region_nop(MemoryListener
*listener
,
717 MemoryRegionSection
*section
)
721 static void kvm_log_sync(MemoryListener
*listener
,
722 MemoryRegionSection
*section
)
726 r
= kvm_physical_sync_dirty_bitmap(section
);
732 static void kvm_log_global_start(struct MemoryListener
*listener
)
736 r
= kvm_set_migration_log(1);
740 static void kvm_log_global_stop(struct MemoryListener
*listener
)
744 r
= kvm_set_migration_log(0);
748 static void kvm_mem_ioeventfd_add(MemoryRegionSection
*section
,
749 bool match_data
, uint64_t data
, int fd
)
753 assert(match_data
&& section
->size
<= 8);
755 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
756 data
, true, section
->size
);
762 static void kvm_mem_ioeventfd_del(MemoryRegionSection
*section
,
763 bool match_data
, uint64_t data
, int fd
)
767 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
768 data
, false, section
->size
);
774 static void kvm_io_ioeventfd_add(MemoryRegionSection
*section
,
775 bool match_data
, uint64_t data
, int fd
)
779 assert(match_data
&& section
->size
== 2);
781 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
788 static void kvm_io_ioeventfd_del(MemoryRegionSection
*section
,
789 bool match_data
, uint64_t data
, int fd
)
794 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
801 static void kvm_eventfd_add(MemoryListener
*listener
,
802 MemoryRegionSection
*section
,
803 bool match_data
, uint64_t data
, int fd
)
805 if (section
->address_space
== get_system_memory()) {
806 kvm_mem_ioeventfd_add(section
, match_data
, data
, fd
);
808 kvm_io_ioeventfd_add(section
, match_data
, data
, fd
);
812 static void kvm_eventfd_del(MemoryListener
*listener
,
813 MemoryRegionSection
*section
,
814 bool match_data
, uint64_t data
, int fd
)
816 if (section
->address_space
== get_system_memory()) {
817 kvm_mem_ioeventfd_del(section
, match_data
, data
, fd
);
819 kvm_io_ioeventfd_del(section
, match_data
, data
, fd
);
823 static MemoryListener kvm_memory_listener
= {
825 .commit
= kvm_commit
,
826 .region_add
= kvm_region_add
,
827 .region_del
= kvm_region_del
,
828 .region_nop
= kvm_region_nop
,
829 .log_start
= kvm_log_start
,
830 .log_stop
= kvm_log_stop
,
831 .log_sync
= kvm_log_sync
,
832 .log_global_start
= kvm_log_global_start
,
833 .log_global_stop
= kvm_log_global_stop
,
834 .eventfd_add
= kvm_eventfd_add
,
835 .eventfd_del
= kvm_eventfd_del
,
839 static void kvm_handle_interrupt(CPUArchState
*env
, int mask
)
841 env
->interrupt_request
|= mask
;
843 if (!qemu_cpu_is_self(env
)) {
848 int kvm_irqchip_set_irq(KVMState
*s
, int irq
, int level
)
850 struct kvm_irq_level event
;
853 assert(kvm_irqchip_in_kernel());
857 ret
= kvm_vm_ioctl(s
, s
->irqchip_inject_ioctl
, &event
);
859 perror("kvm_set_irqchip_line");
863 return (s
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
866 #ifdef KVM_CAP_IRQ_ROUTING
867 typedef struct KVMMSIRoute
{
868 struct kvm_irq_routing_entry kroute
;
869 QTAILQ_ENTRY(KVMMSIRoute
) entry
;
872 static void set_gsi(KVMState
*s
, unsigned int gsi
)
874 s
->used_gsi_bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
877 static void clear_gsi(KVMState
*s
, unsigned int gsi
)
879 s
->used_gsi_bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
882 static void kvm_init_irq_routing(KVMState
*s
)
886 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
);
888 unsigned int gsi_bits
, i
;
890 /* Round up so we can search ints using ffs */
891 gsi_bits
= ALIGN(gsi_count
, 32);
892 s
->used_gsi_bitmap
= g_malloc0(gsi_bits
/ 8);
893 s
->gsi_count
= gsi_count
;
895 /* Mark any over-allocated bits as already in use */
896 for (i
= gsi_count
; i
< gsi_bits
; i
++) {
901 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
902 s
->nr_allocated_irq_routes
= 0;
904 if (!s
->direct_msi
) {
905 for (i
= 0; i
< KVM_MSI_HASHTAB_SIZE
; i
++) {
906 QTAILQ_INIT(&s
->msi_hashtab
[i
]);
910 kvm_arch_init_irq_routing(s
);
913 static void kvm_irqchip_commit_routes(KVMState
*s
)
917 s
->irq_routes
->flags
= 0;
918 ret
= kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
922 static void kvm_add_routing_entry(KVMState
*s
,
923 struct kvm_irq_routing_entry
*entry
)
925 struct kvm_irq_routing_entry
*new;
928 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
929 n
= s
->nr_allocated_irq_routes
* 2;
933 size
= sizeof(struct kvm_irq_routing
);
934 size
+= n
* sizeof(*new);
935 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
936 s
->nr_allocated_irq_routes
= n
;
938 n
= s
->irq_routes
->nr
++;
939 new = &s
->irq_routes
->entries
[n
];
940 memset(new, 0, sizeof(*new));
941 new->gsi
= entry
->gsi
;
942 new->type
= entry
->type
;
943 new->flags
= entry
->flags
;
946 set_gsi(s
, entry
->gsi
);
948 kvm_irqchip_commit_routes(s
);
951 void kvm_irqchip_add_irq_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
953 struct kvm_irq_routing_entry e
;
955 assert(pin
< s
->gsi_count
);
958 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
960 e
.u
.irqchip
.irqchip
= irqchip
;
961 e
.u
.irqchip
.pin
= pin
;
962 kvm_add_routing_entry(s
, &e
);
965 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
967 struct kvm_irq_routing_entry
*e
;
970 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
971 e
= &s
->irq_routes
->entries
[i
];
972 if (e
->gsi
== virq
) {
974 *e
= s
->irq_routes
->entries
[s
->irq_routes
->nr
];
979 kvm_irqchip_commit_routes(s
);
982 static unsigned int kvm_hash_msi(uint32_t data
)
984 /* This is optimized for IA32 MSI layout. However, no other arch shall
985 * repeat the mistake of not providing a direct MSI injection API. */
989 static void kvm_flush_dynamic_msi_routes(KVMState
*s
)
991 KVMMSIRoute
*route
, *next
;
994 for (hash
= 0; hash
< KVM_MSI_HASHTAB_SIZE
; hash
++) {
995 QTAILQ_FOREACH_SAFE(route
, &s
->msi_hashtab
[hash
], entry
, next
) {
996 kvm_irqchip_release_virq(s
, route
->kroute
.gsi
);
997 QTAILQ_REMOVE(&s
->msi_hashtab
[hash
], route
, entry
);
1003 static int kvm_irqchip_get_virq(KVMState
*s
)
1005 uint32_t *word
= s
->used_gsi_bitmap
;
1006 int max_words
= ALIGN(s
->gsi_count
, 32) / 32;
1011 /* Return the lowest unused GSI in the bitmap */
1012 for (i
= 0; i
< max_words
; i
++) {
1013 bit
= ffs(~word
[i
]);
1018 return bit
- 1 + i
* 32;
1020 if (!s
->direct_msi
&& retry
) {
1022 kvm_flush_dynamic_msi_routes(s
);
1029 static KVMMSIRoute
*kvm_lookup_msi_route(KVMState
*s
, MSIMessage msg
)
1031 unsigned int hash
= kvm_hash_msi(msg
.data
);
1034 QTAILQ_FOREACH(route
, &s
->msi_hashtab
[hash
], entry
) {
1035 if (route
->kroute
.u
.msi
.address_lo
== (uint32_t)msg
.address
&&
1036 route
->kroute
.u
.msi
.address_hi
== (msg
.address
>> 32) &&
1037 route
->kroute
.u
.msi
.data
== msg
.data
) {
1044 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1049 if (s
->direct_msi
) {
1050 msi
.address_lo
= (uint32_t)msg
.address
;
1051 msi
.address_hi
= msg
.address
>> 32;
1052 msi
.data
= msg
.data
;
1054 memset(msi
.pad
, 0, sizeof(msi
.pad
));
1056 return kvm_vm_ioctl(s
, KVM_SIGNAL_MSI
, &msi
);
1059 route
= kvm_lookup_msi_route(s
, msg
);
1063 virq
= kvm_irqchip_get_virq(s
);
1068 route
= g_malloc(sizeof(KVMMSIRoute
));
1069 route
->kroute
.gsi
= virq
;
1070 route
->kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1071 route
->kroute
.flags
= 0;
1072 route
->kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1073 route
->kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1074 route
->kroute
.u
.msi
.data
= msg
.data
;
1076 kvm_add_routing_entry(s
, &route
->kroute
);
1078 QTAILQ_INSERT_TAIL(&s
->msi_hashtab
[kvm_hash_msi(msg
.data
)], route
,
1082 assert(route
->kroute
.type
== KVM_IRQ_ROUTING_MSI
);
1084 return kvm_irqchip_set_irq(s
, route
->kroute
.gsi
, 1);
1087 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1089 struct kvm_irq_routing_entry kroute
;
1092 if (!kvm_irqchip_in_kernel()) {
1096 virq
= kvm_irqchip_get_virq(s
);
1102 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1104 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1105 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1106 kroute
.u
.msi
.data
= msg
.data
;
1108 kvm_add_routing_entry(s
, &kroute
);
1113 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1115 struct kvm_irqfd irqfd
= {
1118 .flags
= assign
? 0 : KVM_IRQFD_FLAG_DEASSIGN
,
1121 if (!kvm_irqchip_in_kernel()) {
1125 return kvm_vm_ioctl(s
, KVM_IRQFD
, &irqfd
);
1128 #else /* !KVM_CAP_IRQ_ROUTING */
1130 static void kvm_init_irq_routing(KVMState
*s
)
1134 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1138 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1143 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1148 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1152 #endif /* !KVM_CAP_IRQ_ROUTING */
1154 int kvm_irqchip_add_irqfd(KVMState
*s
, int fd
, int virq
)
1156 return kvm_irqchip_assign_irqfd(s
, fd
, virq
, true);
1159 int kvm_irqchip_remove_irqfd(KVMState
*s
, int fd
, int virq
)
1161 return kvm_irqchip_assign_irqfd(s
, fd
, virq
, false);
1164 static int kvm_irqchip_create(KVMState
*s
)
1166 QemuOptsList
*list
= qemu_find_opts("machine");
1169 if (QTAILQ_EMPTY(&list
->head
) ||
1170 !qemu_opt_get_bool(QTAILQ_FIRST(&list
->head
),
1171 "kernel_irqchip", true) ||
1172 !kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
1176 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
1178 fprintf(stderr
, "Create kernel irqchip failed\n");
1182 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
1183 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
1184 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
1186 kvm_kernel_irqchip
= true;
1188 kvm_init_irq_routing(s
);
1195 static const char upgrade_note
[] =
1196 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1197 "(see http://sourceforge.net/projects/kvm).\n";
1199 const KVMCapabilityInfo
*missing_cap
;
1203 s
= g_malloc0(sizeof(KVMState
));
1206 * On systems where the kernel can support different base page
1207 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1208 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1209 * page size for the system though.
1211 assert(TARGET_PAGE_SIZE
<= getpagesize());
1213 #ifdef KVM_CAP_SET_GUEST_DEBUG
1214 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
1216 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
1217 s
->slots
[i
].slot
= i
;
1220 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
1222 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1227 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1228 if (ret
< KVM_API_VERSION
) {
1232 fprintf(stderr
, "kvm version too old\n");
1236 if (ret
> KVM_API_VERSION
) {
1238 fprintf(stderr
, "kvm version not supported\n");
1242 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
1245 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
1246 "your host kernel command line\n");
1252 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1255 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1259 fprintf(stderr
, "kvm does not support %s\n%s",
1260 missing_cap
->name
, upgrade_note
);
1264 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1266 s
->broken_set_mem_region
= 1;
1267 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1269 s
->broken_set_mem_region
= 0;
1272 #ifdef KVM_CAP_VCPU_EVENTS
1273 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1276 s
->robust_singlestep
=
1277 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1279 #ifdef KVM_CAP_DEBUGREGS
1280 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1283 #ifdef KVM_CAP_XSAVE
1284 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1288 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1291 #ifdef KVM_CAP_PIT_STATE2
1292 s
->pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1295 #ifdef KVM_CAP_IRQ_ROUTING
1296 s
->direct_msi
= (kvm_check_extension(s
, KVM_CAP_SIGNAL_MSI
) > 0);
1299 ret
= kvm_arch_init(s
);
1304 ret
= kvm_irqchip_create(s
);
1310 memory_listener_register(&kvm_memory_listener
, NULL
);
1312 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1314 cpu_interrupt_handler
= kvm_handle_interrupt
;
1332 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1336 uint8_t *ptr
= data
;
1338 for (i
= 0; i
< count
; i
++) {
1339 if (direction
== KVM_EXIT_IO_IN
) {
1342 stb_p(ptr
, cpu_inb(port
));
1345 stw_p(ptr
, cpu_inw(port
));
1348 stl_p(ptr
, cpu_inl(port
));
1354 cpu_outb(port
, ldub_p(ptr
));
1357 cpu_outw(port
, lduw_p(ptr
));
1360 cpu_outl(port
, ldl_p(ptr
));
1369 static int kvm_handle_internal_error(CPUArchState
*env
, struct kvm_run
*run
)
1371 fprintf(stderr
, "KVM internal error.");
1372 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1375 fprintf(stderr
, " Suberror: %d\n", run
->internal
.suberror
);
1376 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1377 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1378 i
, (uint64_t)run
->internal
.data
[i
]);
1381 fprintf(stderr
, "\n");
1383 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1384 fprintf(stderr
, "emulation failure\n");
1385 if (!kvm_arch_stop_on_emulation_error(env
)) {
1386 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1387 return EXCP_INTERRUPT
;
1390 /* FIXME: Should trigger a qmp message to let management know
1391 * something went wrong.
1396 void kvm_flush_coalesced_mmio_buffer(void)
1398 KVMState
*s
= kvm_state
;
1400 if (s
->coalesced_flush_in_progress
) {
1404 s
->coalesced_flush_in_progress
= true;
1406 if (s
->coalesced_mmio_ring
) {
1407 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1408 while (ring
->first
!= ring
->last
) {
1409 struct kvm_coalesced_mmio
*ent
;
1411 ent
= &ring
->coalesced_mmio
[ring
->first
];
1413 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1415 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1419 s
->coalesced_flush_in_progress
= false;
1422 static void do_kvm_cpu_synchronize_state(void *_env
)
1424 CPUArchState
*env
= _env
;
1426 if (!env
->kvm_vcpu_dirty
) {
1427 kvm_arch_get_registers(env
);
1428 env
->kvm_vcpu_dirty
= 1;
1432 void kvm_cpu_synchronize_state(CPUArchState
*env
)
1434 if (!env
->kvm_vcpu_dirty
) {
1435 run_on_cpu(env
, do_kvm_cpu_synchronize_state
, env
);
1439 void kvm_cpu_synchronize_post_reset(CPUArchState
*env
)
1441 kvm_arch_put_registers(env
, KVM_PUT_RESET_STATE
);
1442 env
->kvm_vcpu_dirty
= 0;
1445 void kvm_cpu_synchronize_post_init(CPUArchState
*env
)
1447 kvm_arch_put_registers(env
, KVM_PUT_FULL_STATE
);
1448 env
->kvm_vcpu_dirty
= 0;
1451 int kvm_cpu_exec(CPUArchState
*env
)
1453 struct kvm_run
*run
= env
->kvm_run
;
1456 DPRINTF("kvm_cpu_exec()\n");
1458 if (kvm_arch_process_async_events(env
)) {
1459 env
->exit_request
= 0;
1464 if (env
->kvm_vcpu_dirty
) {
1465 kvm_arch_put_registers(env
, KVM_PUT_RUNTIME_STATE
);
1466 env
->kvm_vcpu_dirty
= 0;
1469 kvm_arch_pre_run(env
, run
);
1470 if (env
->exit_request
) {
1471 DPRINTF("interrupt exit requested\n");
1473 * KVM requires us to reenter the kernel after IO exits to complete
1474 * instruction emulation. This self-signal will ensure that we
1477 qemu_cpu_kick_self();
1479 qemu_mutex_unlock_iothread();
1481 run_ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
1483 qemu_mutex_lock_iothread();
1484 kvm_arch_post_run(env
, run
);
1486 kvm_flush_coalesced_mmio_buffer();
1489 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1490 DPRINTF("io window exit\n");
1491 ret
= EXCP_INTERRUPT
;
1494 fprintf(stderr
, "error: kvm run failed %s\n",
1495 strerror(-run_ret
));
1499 switch (run
->exit_reason
) {
1501 DPRINTF("handle_io\n");
1502 kvm_handle_io(run
->io
.port
,
1503 (uint8_t *)run
+ run
->io
.data_offset
,
1510 DPRINTF("handle_mmio\n");
1511 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1514 run
->mmio
.is_write
);
1517 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1518 DPRINTF("irq_window_open\n");
1519 ret
= EXCP_INTERRUPT
;
1521 case KVM_EXIT_SHUTDOWN
:
1522 DPRINTF("shutdown\n");
1523 qemu_system_reset_request();
1524 ret
= EXCP_INTERRUPT
;
1526 case KVM_EXIT_UNKNOWN
:
1527 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1528 (uint64_t)run
->hw
.hardware_exit_reason
);
1531 case KVM_EXIT_INTERNAL_ERROR
:
1532 ret
= kvm_handle_internal_error(env
, run
);
1535 DPRINTF("kvm_arch_handle_exit\n");
1536 ret
= kvm_arch_handle_exit(env
, run
);
1542 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1543 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1546 env
->exit_request
= 0;
1550 int kvm_ioctl(KVMState
*s
, int type
, ...)
1557 arg
= va_arg(ap
, void *);
1560 ret
= ioctl(s
->fd
, type
, arg
);
1567 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1574 arg
= va_arg(ap
, void *);
1577 ret
= ioctl(s
->vmfd
, type
, arg
);
1584 int kvm_vcpu_ioctl(CPUArchState
*env
, int type
, ...)
1591 arg
= va_arg(ap
, void *);
1594 ret
= ioctl(env
->kvm_fd
, type
, arg
);
1601 int kvm_has_sync_mmu(void)
1603 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
1606 int kvm_has_vcpu_events(void)
1608 return kvm_state
->vcpu_events
;
1611 int kvm_has_robust_singlestep(void)
1613 return kvm_state
->robust_singlestep
;
1616 int kvm_has_debugregs(void)
1618 return kvm_state
->debugregs
;
1621 int kvm_has_xsave(void)
1623 return kvm_state
->xsave
;
1626 int kvm_has_xcrs(void)
1628 return kvm_state
->xcrs
;
1631 int kvm_has_pit_state2(void)
1633 return kvm_state
->pit_state2
;
1636 int kvm_has_many_ioeventfds(void)
1638 if (!kvm_enabled()) {
1641 return kvm_state
->many_ioeventfds
;
1644 int kvm_has_gsi_routing(void)
1646 #ifdef KVM_CAP_IRQ_ROUTING
1647 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1653 int kvm_allows_irq0_override(void)
1655 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1658 void *kvm_vmalloc(ram_addr_t size
)
1663 mem
= kvm_arch_vmalloc(size
);
1668 return qemu_vmalloc(size
);
1671 void kvm_setup_guest_memory(void *start
, size_t size
)
1673 if (!kvm_has_sync_mmu()) {
1674 int ret
= qemu_madvise(start
, size
, QEMU_MADV_DONTFORK
);
1677 perror("qemu_madvise");
1679 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1685 #ifdef KVM_CAP_SET_GUEST_DEBUG
1686 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUArchState
*env
,
1689 struct kvm_sw_breakpoint
*bp
;
1691 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1699 int kvm_sw_breakpoints_active(CPUArchState
*env
)
1701 return !QTAILQ_EMPTY(&env
->kvm_state
->kvm_sw_breakpoints
);
1704 struct kvm_set_guest_debug_data
{
1705 struct kvm_guest_debug dbg
;
1710 static void kvm_invoke_set_guest_debug(void *data
)
1712 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1713 CPUArchState
*env
= dbg_data
->env
;
1715 dbg_data
->err
= kvm_vcpu_ioctl(env
, KVM_SET_GUEST_DEBUG
, &dbg_data
->dbg
);
1718 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1720 struct kvm_set_guest_debug_data data
;
1722 data
.dbg
.control
= reinject_trap
;
1724 if (env
->singlestep_enabled
) {
1725 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1727 kvm_arch_update_guest_debug(env
, &data
.dbg
);
1730 run_on_cpu(env
, kvm_invoke_set_guest_debug
, &data
);
1734 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1735 target_ulong len
, int type
)
1737 struct kvm_sw_breakpoint
*bp
;
1741 if (type
== GDB_BREAKPOINT_SW
) {
1742 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1748 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1755 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1761 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1764 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1770 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1771 err
= kvm_update_guest_debug(env
, 0);
1779 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1780 target_ulong len
, int type
)
1782 struct kvm_sw_breakpoint
*bp
;
1786 if (type
== GDB_BREAKPOINT_SW
) {
1787 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1792 if (bp
->use_count
> 1) {
1797 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1802 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1805 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1811 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1812 err
= kvm_update_guest_debug(env
, 0);
1820 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1822 struct kvm_sw_breakpoint
*bp
, *next
;
1823 KVMState
*s
= current_env
->kvm_state
;
1826 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1827 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
1828 /* Try harder to find a CPU that currently sees the breakpoint. */
1829 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1830 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0) {
1836 kvm_arch_remove_all_hw_breakpoints();
1838 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1839 kvm_update_guest_debug(env
, 0);
1843 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1845 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1850 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1851 target_ulong len
, int type
)
1856 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1857 target_ulong len
, int type
)
1862 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1865 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1867 int kvm_set_signal_mask(CPUArchState
*env
, const sigset_t
*sigset
)
1869 struct kvm_signal_mask
*sigmask
;
1873 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1876 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1879 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1880 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1886 int kvm_set_ioeventfd_mmio(int fd
, uint32_t addr
, uint32_t val
, bool assign
,
1890 struct kvm_ioeventfd iofd
;
1892 iofd
.datamatch
= val
;
1895 iofd
.flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
;
1898 if (!kvm_enabled()) {
1903 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1906 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
1915 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1917 struct kvm_ioeventfd kick
= {
1921 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1925 if (!kvm_enabled()) {
1929 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1931 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1938 int kvm_on_sigbus_vcpu(CPUArchState
*env
, int code
, void *addr
)
1940 return kvm_arch_on_sigbus_vcpu(env
, code
, addr
);
1943 int kvm_on_sigbus(int code
, void *addr
)
1945 return kvm_arch_on_sigbus(code
, addr
);