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"
35 #include "event_notifier.h"
37 /* This check must be after config-host.h is included */
39 #include <sys/eventfd.h>
42 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
43 #define PAGE_SIZE TARGET_PAGE_SIZE
48 #define DPRINTF(fmt, ...) \
49 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #define DPRINTF(fmt, ...) \
55 #define KVM_MSI_HASHTAB_SIZE 256
57 typedef struct KVMSlot
59 target_phys_addr_t start_addr
;
60 ram_addr_t memory_size
;
66 typedef struct kvm_dirty_log KVMDirtyLog
;
74 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
75 bool coalesced_flush_in_progress
;
76 int broken_set_mem_region
;
79 int robust_singlestep
;
81 #ifdef KVM_CAP_SET_GUEST_DEBUG
82 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
87 /* The man page (and posix) say ioctl numbers are signed int, but
88 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
89 * unsigned, and treating them as signed here can break things */
90 unsigned irqchip_inject_ioctl
;
91 #ifdef KVM_CAP_IRQ_ROUTING
92 struct kvm_irq_routing
*irq_routes
;
93 int nr_allocated_irq_routes
;
94 uint32_t *used_gsi_bitmap
;
95 unsigned int gsi_count
;
96 QTAILQ_HEAD(msi_hashtab
, KVMMSIRoute
) msi_hashtab
[KVM_MSI_HASHTAB_SIZE
];
102 bool kvm_kernel_irqchip
;
104 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
105 KVM_CAP_INFO(USER_MEMORY
),
106 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
110 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
114 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
115 if (s
->slots
[i
].memory_size
== 0) {
120 fprintf(stderr
, "%s: no free slot available\n", __func__
);
124 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
125 target_phys_addr_t start_addr
,
126 target_phys_addr_t end_addr
)
130 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
131 KVMSlot
*mem
= &s
->slots
[i
];
133 if (start_addr
== mem
->start_addr
&&
134 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
143 * Find overlapping slot with lowest start address
145 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
146 target_phys_addr_t start_addr
,
147 target_phys_addr_t end_addr
)
149 KVMSlot
*found
= NULL
;
152 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
153 KVMSlot
*mem
= &s
->slots
[i
];
155 if (mem
->memory_size
== 0 ||
156 (found
&& found
->start_addr
< mem
->start_addr
)) {
160 if (end_addr
> mem
->start_addr
&&
161 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
169 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
170 target_phys_addr_t
*phys_addr
)
174 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
175 KVMSlot
*mem
= &s
->slots
[i
];
177 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
178 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
186 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
188 struct kvm_userspace_memory_region mem
;
190 mem
.slot
= slot
->slot
;
191 mem
.guest_phys_addr
= slot
->start_addr
;
192 mem
.memory_size
= slot
->memory_size
;
193 mem
.userspace_addr
= (unsigned long)slot
->ram
;
194 mem
.flags
= slot
->flags
;
195 if (s
->migration_log
) {
196 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
198 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
201 static void kvm_reset_vcpu(void *opaque
)
203 CPUArchState
*env
= opaque
;
205 kvm_arch_reset_vcpu(env
);
208 int kvm_init_vcpu(CPUArchState
*env
)
210 KVMState
*s
= kvm_state
;
214 DPRINTF("kvm_init_vcpu\n");
216 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
218 DPRINTF("kvm_create_vcpu failed\n");
224 env
->kvm_vcpu_dirty
= 1;
226 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
229 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
233 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
235 if (env
->kvm_run
== MAP_FAILED
) {
237 DPRINTF("mmap'ing vcpu state failed\n");
241 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
242 s
->coalesced_mmio_ring
=
243 (void *)env
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
246 ret
= kvm_arch_init_vcpu(env
);
248 qemu_register_reset(kvm_reset_vcpu
, env
);
249 kvm_arch_reset_vcpu(env
);
256 * dirty pages logging control
259 static int kvm_mem_flags(KVMState
*s
, bool log_dirty
)
261 return log_dirty
? KVM_MEM_LOG_DIRTY_PAGES
: 0;
264 static int kvm_slot_dirty_pages_log_change(KVMSlot
*mem
, bool log_dirty
)
266 KVMState
*s
= kvm_state
;
267 int flags
, mask
= KVM_MEM_LOG_DIRTY_PAGES
;
270 old_flags
= mem
->flags
;
272 flags
= (mem
->flags
& ~mask
) | kvm_mem_flags(s
, log_dirty
);
275 /* If nothing changed effectively, no need to issue ioctl */
276 if (s
->migration_log
) {
277 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
280 if (flags
== old_flags
) {
284 return kvm_set_user_memory_region(s
, mem
);
287 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr
,
288 ram_addr_t size
, bool log_dirty
)
290 KVMState
*s
= kvm_state
;
291 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
294 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
295 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
296 (target_phys_addr_t
)(phys_addr
+ size
- 1));
299 return kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
302 static void kvm_log_start(MemoryListener
*listener
,
303 MemoryRegionSection
*section
)
307 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
308 section
->size
, true);
314 static void kvm_log_stop(MemoryListener
*listener
,
315 MemoryRegionSection
*section
)
319 r
= kvm_dirty_pages_log_change(section
->offset_within_address_space
,
320 section
->size
, false);
326 static int kvm_set_migration_log(int enable
)
328 KVMState
*s
= kvm_state
;
332 s
->migration_log
= enable
;
334 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
337 if (!mem
->memory_size
) {
340 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
343 err
= kvm_set_user_memory_region(s
, mem
);
351 /* get kvm's dirty pages bitmap and update qemu's */
352 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
353 unsigned long *bitmap
)
356 unsigned long page_number
, c
;
357 target_phys_addr_t addr
, addr1
;
358 unsigned int len
= ((section
->size
/ TARGET_PAGE_SIZE
) + HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
359 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
362 * bitmap-traveling is faster than memory-traveling (for addr...)
363 * especially when most of the memory is not dirty.
365 for (i
= 0; i
< len
; i
++) {
366 if (bitmap
[i
] != 0) {
367 c
= leul_to_cpu(bitmap
[i
]);
371 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
372 addr1
= page_number
* TARGET_PAGE_SIZE
;
373 addr
= section
->offset_within_region
+ addr1
;
374 memory_region_set_dirty(section
->mr
, addr
,
375 TARGET_PAGE_SIZE
* hpratio
);
382 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
385 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
386 * This function updates qemu's dirty bitmap using
387 * memory_region_set_dirty(). This means all bits are set
390 * @start_add: start of logged region.
391 * @end_addr: end of logged region.
393 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection
*section
)
395 KVMState
*s
= kvm_state
;
396 unsigned long size
, allocated_size
= 0;
400 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
401 target_phys_addr_t end_addr
= start_addr
+ section
->size
;
403 d
.dirty_bitmap
= NULL
;
404 while (start_addr
< end_addr
) {
405 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
410 /* XXX bad kernel interface alert
411 * For dirty bitmap, kernel allocates array of size aligned to
412 * bits-per-long. But for case when the kernel is 64bits and
413 * the userspace is 32bits, userspace can't align to the same
414 * bits-per-long, since sizeof(long) is different between kernel
415 * and user space. This way, userspace will provide buffer which
416 * may be 4 bytes less than the kernel will use, resulting in
417 * userspace memory corruption (which is not detectable by valgrind
418 * too, in most cases).
419 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
420 * a hope that sizeof(long) wont become >8 any time soon.
422 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
423 /*HOST_LONG_BITS*/ 64) / 8;
424 if (!d
.dirty_bitmap
) {
425 d
.dirty_bitmap
= g_malloc(size
);
426 } else if (size
> allocated_size
) {
427 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
429 allocated_size
= size
;
430 memset(d
.dirty_bitmap
, 0, allocated_size
);
434 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
435 DPRINTF("ioctl failed %d\n", errno
);
440 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
441 start_addr
= mem
->start_addr
+ mem
->memory_size
;
443 g_free(d
.dirty_bitmap
);
448 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
451 KVMState
*s
= kvm_state
;
453 if (s
->coalesced_mmio
) {
454 struct kvm_coalesced_mmio_zone zone
;
460 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
466 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
469 KVMState
*s
= kvm_state
;
471 if (s
->coalesced_mmio
) {
472 struct kvm_coalesced_mmio_zone zone
;
478 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
484 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
488 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
496 static int kvm_check_many_ioeventfds(void)
498 /* Userspace can use ioeventfd for io notification. This requires a host
499 * that supports eventfd(2) and an I/O thread; since eventfd does not
500 * support SIGIO it cannot interrupt the vcpu.
502 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
503 * can avoid creating too many ioeventfds.
505 #if defined(CONFIG_EVENTFD)
508 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
509 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
510 if (ioeventfds
[i
] < 0) {
513 ret
= kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, true);
515 close(ioeventfds
[i
]);
520 /* Decide whether many devices are supported or not */
521 ret
= i
== ARRAY_SIZE(ioeventfds
);
524 kvm_set_ioeventfd_pio_word(ioeventfds
[i
], 0, i
, false);
525 close(ioeventfds
[i
]);
533 static const KVMCapabilityInfo
*
534 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
537 if (!kvm_check_extension(s
, list
->value
)) {
545 static void kvm_set_phys_mem(MemoryRegionSection
*section
, bool add
)
547 KVMState
*s
= kvm_state
;
550 MemoryRegion
*mr
= section
->mr
;
551 bool log_dirty
= memory_region_is_logging(mr
);
552 target_phys_addr_t start_addr
= section
->offset_within_address_space
;
553 ram_addr_t size
= section
->size
;
557 /* kvm works in page size chunks, but the function may be called
558 with sub-page size and unaligned start address. */
559 delta
= TARGET_PAGE_ALIGN(size
) - size
;
565 size
&= TARGET_PAGE_MASK
;
566 if (!size
|| (start_addr
& ~TARGET_PAGE_MASK
)) {
570 if (!memory_region_is_ram(mr
)) {
574 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
577 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
582 if (add
&& start_addr
>= mem
->start_addr
&&
583 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
584 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
585 /* The new slot fits into the existing one and comes with
586 * identical parameters - update flags and done. */
587 kvm_slot_dirty_pages_log_change(mem
, log_dirty
);
593 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
594 kvm_physical_sync_dirty_bitmap(section
);
597 /* unregister the overlapping slot */
598 mem
->memory_size
= 0;
599 err
= kvm_set_user_memory_region(s
, mem
);
601 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
602 __func__
, strerror(-err
));
606 /* Workaround for older KVM versions: we can't join slots, even not by
607 * unregistering the previous ones and then registering the larger
608 * slot. We have to maintain the existing fragmentation. Sigh.
610 * This workaround assumes that the new slot starts at the same
611 * address as the first existing one. If not or if some overlapping
612 * slot comes around later, we will fail (not seen in practice so far)
613 * - and actually require a recent KVM version. */
614 if (s
->broken_set_mem_region
&&
615 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
616 mem
= kvm_alloc_slot(s
);
617 mem
->memory_size
= old
.memory_size
;
618 mem
->start_addr
= old
.start_addr
;
620 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
622 err
= kvm_set_user_memory_region(s
, mem
);
624 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
629 start_addr
+= old
.memory_size
;
630 ram
+= old
.memory_size
;
631 size
-= old
.memory_size
;
635 /* register prefix slot */
636 if (old
.start_addr
< start_addr
) {
637 mem
= kvm_alloc_slot(s
);
638 mem
->memory_size
= start_addr
- old
.start_addr
;
639 mem
->start_addr
= old
.start_addr
;
641 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
643 err
= kvm_set_user_memory_region(s
, mem
);
645 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
646 __func__
, strerror(-err
));
648 fprintf(stderr
, "%s: This is probably because your kernel's " \
649 "PAGE_SIZE is too big. Please try to use 4k " \
650 "PAGE_SIZE!\n", __func__
);
656 /* register suffix slot */
657 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
658 ram_addr_t size_delta
;
660 mem
= kvm_alloc_slot(s
);
661 mem
->start_addr
= start_addr
+ size
;
662 size_delta
= mem
->start_addr
- old
.start_addr
;
663 mem
->memory_size
= old
.memory_size
- size_delta
;
664 mem
->ram
= old
.ram
+ size_delta
;
665 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
667 err
= kvm_set_user_memory_region(s
, mem
);
669 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
670 __func__
, strerror(-err
));
676 /* in case the KVM bug workaround already "consumed" the new slot */
683 mem
= kvm_alloc_slot(s
);
684 mem
->memory_size
= size
;
685 mem
->start_addr
= start_addr
;
687 mem
->flags
= kvm_mem_flags(s
, log_dirty
);
689 err
= kvm_set_user_memory_region(s
, mem
);
691 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
697 static void kvm_begin(MemoryListener
*listener
)
701 static void kvm_commit(MemoryListener
*listener
)
705 static void kvm_region_add(MemoryListener
*listener
,
706 MemoryRegionSection
*section
)
708 kvm_set_phys_mem(section
, true);
711 static void kvm_region_del(MemoryListener
*listener
,
712 MemoryRegionSection
*section
)
714 kvm_set_phys_mem(section
, false);
717 static void kvm_region_nop(MemoryListener
*listener
,
718 MemoryRegionSection
*section
)
722 static void kvm_log_sync(MemoryListener
*listener
,
723 MemoryRegionSection
*section
)
727 r
= kvm_physical_sync_dirty_bitmap(section
);
733 static void kvm_log_global_start(struct MemoryListener
*listener
)
737 r
= kvm_set_migration_log(1);
741 static void kvm_log_global_stop(struct MemoryListener
*listener
)
745 r
= kvm_set_migration_log(0);
749 static void kvm_mem_ioeventfd_add(MemoryRegionSection
*section
,
750 bool match_data
, uint64_t data
, int fd
)
754 assert(match_data
&& section
->size
<= 8);
756 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
757 data
, true, section
->size
);
763 static void kvm_mem_ioeventfd_del(MemoryRegionSection
*section
,
764 bool match_data
, uint64_t data
, int fd
)
768 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
769 data
, false, section
->size
);
775 static void kvm_io_ioeventfd_add(MemoryRegionSection
*section
,
776 bool match_data
, uint64_t data
, int fd
)
780 assert(match_data
&& section
->size
== 2);
782 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
789 static void kvm_io_ioeventfd_del(MemoryRegionSection
*section
,
790 bool match_data
, uint64_t data
, int fd
)
795 r
= kvm_set_ioeventfd_pio_word(fd
, section
->offset_within_address_space
,
802 static void kvm_eventfd_add(MemoryListener
*listener
,
803 MemoryRegionSection
*section
,
804 bool match_data
, uint64_t data
,
807 if (section
->address_space
== get_system_memory()) {
808 kvm_mem_ioeventfd_add(section
, match_data
, data
,
809 event_notifier_get_fd(e
));
811 kvm_io_ioeventfd_add(section
, match_data
, data
,
812 event_notifier_get_fd(e
));
816 static void kvm_eventfd_del(MemoryListener
*listener
,
817 MemoryRegionSection
*section
,
818 bool match_data
, uint64_t data
,
821 if (section
->address_space
== get_system_memory()) {
822 kvm_mem_ioeventfd_del(section
, match_data
, data
,
823 event_notifier_get_fd(e
));
825 kvm_io_ioeventfd_del(section
, match_data
, data
,
826 event_notifier_get_fd(e
));
830 static MemoryListener kvm_memory_listener
= {
832 .commit
= kvm_commit
,
833 .region_add
= kvm_region_add
,
834 .region_del
= kvm_region_del
,
835 .region_nop
= kvm_region_nop
,
836 .log_start
= kvm_log_start
,
837 .log_stop
= kvm_log_stop
,
838 .log_sync
= kvm_log_sync
,
839 .log_global_start
= kvm_log_global_start
,
840 .log_global_stop
= kvm_log_global_stop
,
841 .eventfd_add
= kvm_eventfd_add
,
842 .eventfd_del
= kvm_eventfd_del
,
846 static void kvm_handle_interrupt(CPUArchState
*env
, int mask
)
848 env
->interrupt_request
|= mask
;
850 if (!qemu_cpu_is_self(env
)) {
855 int kvm_irqchip_set_irq(KVMState
*s
, int irq
, int level
)
857 struct kvm_irq_level event
;
860 assert(kvm_irqchip_in_kernel());
864 ret
= kvm_vm_ioctl(s
, s
->irqchip_inject_ioctl
, &event
);
866 perror("kvm_set_irqchip_line");
870 return (s
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
873 #ifdef KVM_CAP_IRQ_ROUTING
874 typedef struct KVMMSIRoute
{
875 struct kvm_irq_routing_entry kroute
;
876 QTAILQ_ENTRY(KVMMSIRoute
) entry
;
879 static void set_gsi(KVMState
*s
, unsigned int gsi
)
881 s
->used_gsi_bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
884 static void clear_gsi(KVMState
*s
, unsigned int gsi
)
886 s
->used_gsi_bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
889 static void kvm_init_irq_routing(KVMState
*s
)
893 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
);
895 unsigned int gsi_bits
, i
;
897 /* Round up so we can search ints using ffs */
898 gsi_bits
= ALIGN(gsi_count
, 32);
899 s
->used_gsi_bitmap
= g_malloc0(gsi_bits
/ 8);
900 s
->gsi_count
= gsi_count
;
902 /* Mark any over-allocated bits as already in use */
903 for (i
= gsi_count
; i
< gsi_bits
; i
++) {
908 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
909 s
->nr_allocated_irq_routes
= 0;
911 if (!s
->direct_msi
) {
912 for (i
= 0; i
< KVM_MSI_HASHTAB_SIZE
; i
++) {
913 QTAILQ_INIT(&s
->msi_hashtab
[i
]);
917 kvm_arch_init_irq_routing(s
);
920 static void kvm_irqchip_commit_routes(KVMState
*s
)
924 s
->irq_routes
->flags
= 0;
925 ret
= kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
929 static void kvm_add_routing_entry(KVMState
*s
,
930 struct kvm_irq_routing_entry
*entry
)
932 struct kvm_irq_routing_entry
*new;
935 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
936 n
= s
->nr_allocated_irq_routes
* 2;
940 size
= sizeof(struct kvm_irq_routing
);
941 size
+= n
* sizeof(*new);
942 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
943 s
->nr_allocated_irq_routes
= n
;
945 n
= s
->irq_routes
->nr
++;
946 new = &s
->irq_routes
->entries
[n
];
947 memset(new, 0, sizeof(*new));
948 new->gsi
= entry
->gsi
;
949 new->type
= entry
->type
;
950 new->flags
= entry
->flags
;
953 set_gsi(s
, entry
->gsi
);
955 kvm_irqchip_commit_routes(s
);
958 void kvm_irqchip_add_irq_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
960 struct kvm_irq_routing_entry e
;
962 assert(pin
< s
->gsi_count
);
965 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
967 e
.u
.irqchip
.irqchip
= irqchip
;
968 e
.u
.irqchip
.pin
= pin
;
969 kvm_add_routing_entry(s
, &e
);
972 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
974 struct kvm_irq_routing_entry
*e
;
977 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
978 e
= &s
->irq_routes
->entries
[i
];
979 if (e
->gsi
== virq
) {
981 *e
= s
->irq_routes
->entries
[s
->irq_routes
->nr
];
986 kvm_irqchip_commit_routes(s
);
989 static unsigned int kvm_hash_msi(uint32_t data
)
991 /* This is optimized for IA32 MSI layout. However, no other arch shall
992 * repeat the mistake of not providing a direct MSI injection API. */
996 static void kvm_flush_dynamic_msi_routes(KVMState
*s
)
998 KVMMSIRoute
*route
, *next
;
1001 for (hash
= 0; hash
< KVM_MSI_HASHTAB_SIZE
; hash
++) {
1002 QTAILQ_FOREACH_SAFE(route
, &s
->msi_hashtab
[hash
], entry
, next
) {
1003 kvm_irqchip_release_virq(s
, route
->kroute
.gsi
);
1004 QTAILQ_REMOVE(&s
->msi_hashtab
[hash
], route
, entry
);
1010 static int kvm_irqchip_get_virq(KVMState
*s
)
1012 uint32_t *word
= s
->used_gsi_bitmap
;
1013 int max_words
= ALIGN(s
->gsi_count
, 32) / 32;
1018 /* Return the lowest unused GSI in the bitmap */
1019 for (i
= 0; i
< max_words
; i
++) {
1020 bit
= ffs(~word
[i
]);
1025 return bit
- 1 + i
* 32;
1027 if (!s
->direct_msi
&& retry
) {
1029 kvm_flush_dynamic_msi_routes(s
);
1036 static KVMMSIRoute
*kvm_lookup_msi_route(KVMState
*s
, MSIMessage msg
)
1038 unsigned int hash
= kvm_hash_msi(msg
.data
);
1041 QTAILQ_FOREACH(route
, &s
->msi_hashtab
[hash
], entry
) {
1042 if (route
->kroute
.u
.msi
.address_lo
== (uint32_t)msg
.address
&&
1043 route
->kroute
.u
.msi
.address_hi
== (msg
.address
>> 32) &&
1044 route
->kroute
.u
.msi
.data
== msg
.data
) {
1051 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1056 if (s
->direct_msi
) {
1057 msi
.address_lo
= (uint32_t)msg
.address
;
1058 msi
.address_hi
= msg
.address
>> 32;
1059 msi
.data
= msg
.data
;
1061 memset(msi
.pad
, 0, sizeof(msi
.pad
));
1063 return kvm_vm_ioctl(s
, KVM_SIGNAL_MSI
, &msi
);
1066 route
= kvm_lookup_msi_route(s
, msg
);
1070 virq
= kvm_irqchip_get_virq(s
);
1075 route
= g_malloc(sizeof(KVMMSIRoute
));
1076 route
->kroute
.gsi
= virq
;
1077 route
->kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1078 route
->kroute
.flags
= 0;
1079 route
->kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1080 route
->kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1081 route
->kroute
.u
.msi
.data
= msg
.data
;
1083 kvm_add_routing_entry(s
, &route
->kroute
);
1085 QTAILQ_INSERT_TAIL(&s
->msi_hashtab
[kvm_hash_msi(msg
.data
)], route
,
1089 assert(route
->kroute
.type
== KVM_IRQ_ROUTING_MSI
);
1091 return kvm_irqchip_set_irq(s
, route
->kroute
.gsi
, 1);
1094 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1096 struct kvm_irq_routing_entry kroute
;
1099 if (!kvm_irqchip_in_kernel()) {
1103 virq
= kvm_irqchip_get_virq(s
);
1109 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1111 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1112 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1113 kroute
.u
.msi
.data
= msg
.data
;
1115 kvm_add_routing_entry(s
, &kroute
);
1120 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1122 struct kvm_irqfd irqfd
= {
1125 .flags
= assign
? 0 : KVM_IRQFD_FLAG_DEASSIGN
,
1128 if (!kvm_irqchip_in_kernel()) {
1132 return kvm_vm_ioctl(s
, KVM_IRQFD
, &irqfd
);
1135 #else /* !KVM_CAP_IRQ_ROUTING */
1137 static void kvm_init_irq_routing(KVMState
*s
)
1141 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1145 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1150 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
)
1155 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1159 #endif /* !KVM_CAP_IRQ_ROUTING */
1161 int kvm_irqchip_add_irqfd(KVMState
*s
, int fd
, int virq
)
1163 return kvm_irqchip_assign_irqfd(s
, fd
, virq
, true);
1166 int kvm_irqchip_add_irq_notifier(KVMState
*s
, EventNotifier
*n
, int virq
)
1168 return kvm_irqchip_add_irqfd(s
, event_notifier_get_fd(n
), virq
);
1171 int kvm_irqchip_remove_irqfd(KVMState
*s
, int fd
, int virq
)
1173 return kvm_irqchip_assign_irqfd(s
, fd
, virq
, false);
1176 int kvm_irqchip_remove_irq_notifier(KVMState
*s
, EventNotifier
*n
, int virq
)
1178 return kvm_irqchip_remove_irqfd(s
, event_notifier_get_fd(n
), virq
);
1181 static int kvm_irqchip_create(KVMState
*s
)
1183 QemuOptsList
*list
= qemu_find_opts("machine");
1186 if (QTAILQ_EMPTY(&list
->head
) ||
1187 !qemu_opt_get_bool(QTAILQ_FIRST(&list
->head
),
1188 "kernel_irqchip", true) ||
1189 !kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
1193 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
1195 fprintf(stderr
, "Create kernel irqchip failed\n");
1199 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
1200 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
1201 s
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
1203 kvm_kernel_irqchip
= true;
1205 kvm_init_irq_routing(s
);
1212 static const char upgrade_note
[] =
1213 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1214 "(see http://sourceforge.net/projects/kvm).\n";
1216 const KVMCapabilityInfo
*missing_cap
;
1220 s
= g_malloc0(sizeof(KVMState
));
1223 * On systems where the kernel can support different base page
1224 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1225 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1226 * page size for the system though.
1228 assert(TARGET_PAGE_SIZE
<= getpagesize());
1230 #ifdef KVM_CAP_SET_GUEST_DEBUG
1231 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
1233 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
1234 s
->slots
[i
].slot
= i
;
1237 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
1239 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1244 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1245 if (ret
< KVM_API_VERSION
) {
1249 fprintf(stderr
, "kvm version too old\n");
1253 if (ret
> KVM_API_VERSION
) {
1255 fprintf(stderr
, "kvm version not supported\n");
1259 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
1262 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
1263 "your host kernel command line\n");
1269 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1272 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1276 fprintf(stderr
, "kvm does not support %s\n%s",
1277 missing_cap
->name
, upgrade_note
);
1281 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1283 s
->broken_set_mem_region
= 1;
1284 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1286 s
->broken_set_mem_region
= 0;
1289 #ifdef KVM_CAP_VCPU_EVENTS
1290 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1293 s
->robust_singlestep
=
1294 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1296 #ifdef KVM_CAP_DEBUGREGS
1297 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1300 #ifdef KVM_CAP_XSAVE
1301 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1305 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1308 #ifdef KVM_CAP_PIT_STATE2
1309 s
->pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1312 #ifdef KVM_CAP_IRQ_ROUTING
1313 s
->direct_msi
= (kvm_check_extension(s
, KVM_CAP_SIGNAL_MSI
) > 0);
1316 ret
= kvm_arch_init(s
);
1321 ret
= kvm_irqchip_create(s
);
1327 memory_listener_register(&kvm_memory_listener
, NULL
);
1329 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1331 cpu_interrupt_handler
= kvm_handle_interrupt
;
1349 static void kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
1353 uint8_t *ptr
= data
;
1355 for (i
= 0; i
< count
; i
++) {
1356 if (direction
== KVM_EXIT_IO_IN
) {
1359 stb_p(ptr
, cpu_inb(port
));
1362 stw_p(ptr
, cpu_inw(port
));
1365 stl_p(ptr
, cpu_inl(port
));
1371 cpu_outb(port
, ldub_p(ptr
));
1374 cpu_outw(port
, lduw_p(ptr
));
1377 cpu_outl(port
, ldl_p(ptr
));
1386 static int kvm_handle_internal_error(CPUArchState
*env
, struct kvm_run
*run
)
1388 fprintf(stderr
, "KVM internal error.");
1389 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1392 fprintf(stderr
, " Suberror: %d\n", run
->internal
.suberror
);
1393 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1394 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1395 i
, (uint64_t)run
->internal
.data
[i
]);
1398 fprintf(stderr
, "\n");
1400 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1401 fprintf(stderr
, "emulation failure\n");
1402 if (!kvm_arch_stop_on_emulation_error(env
)) {
1403 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1404 return EXCP_INTERRUPT
;
1407 /* FIXME: Should trigger a qmp message to let management know
1408 * something went wrong.
1413 void kvm_flush_coalesced_mmio_buffer(void)
1415 KVMState
*s
= kvm_state
;
1417 if (s
->coalesced_flush_in_progress
) {
1421 s
->coalesced_flush_in_progress
= true;
1423 if (s
->coalesced_mmio_ring
) {
1424 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1425 while (ring
->first
!= ring
->last
) {
1426 struct kvm_coalesced_mmio
*ent
;
1428 ent
= &ring
->coalesced_mmio
[ring
->first
];
1430 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1432 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1436 s
->coalesced_flush_in_progress
= false;
1439 static void do_kvm_cpu_synchronize_state(void *_env
)
1441 CPUArchState
*env
= _env
;
1443 if (!env
->kvm_vcpu_dirty
) {
1444 kvm_arch_get_registers(env
);
1445 env
->kvm_vcpu_dirty
= 1;
1449 void kvm_cpu_synchronize_state(CPUArchState
*env
)
1451 if (!env
->kvm_vcpu_dirty
) {
1452 run_on_cpu(env
, do_kvm_cpu_synchronize_state
, env
);
1456 void kvm_cpu_synchronize_post_reset(CPUArchState
*env
)
1458 kvm_arch_put_registers(env
, KVM_PUT_RESET_STATE
);
1459 env
->kvm_vcpu_dirty
= 0;
1462 void kvm_cpu_synchronize_post_init(CPUArchState
*env
)
1464 kvm_arch_put_registers(env
, KVM_PUT_FULL_STATE
);
1465 env
->kvm_vcpu_dirty
= 0;
1468 int kvm_cpu_exec(CPUArchState
*env
)
1470 struct kvm_run
*run
= env
->kvm_run
;
1473 DPRINTF("kvm_cpu_exec()\n");
1475 if (kvm_arch_process_async_events(env
)) {
1476 env
->exit_request
= 0;
1481 if (env
->kvm_vcpu_dirty
) {
1482 kvm_arch_put_registers(env
, KVM_PUT_RUNTIME_STATE
);
1483 env
->kvm_vcpu_dirty
= 0;
1486 kvm_arch_pre_run(env
, run
);
1487 if (env
->exit_request
) {
1488 DPRINTF("interrupt exit requested\n");
1490 * KVM requires us to reenter the kernel after IO exits to complete
1491 * instruction emulation. This self-signal will ensure that we
1494 qemu_cpu_kick_self();
1496 qemu_mutex_unlock_iothread();
1498 run_ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
1500 qemu_mutex_lock_iothread();
1501 kvm_arch_post_run(env
, run
);
1503 kvm_flush_coalesced_mmio_buffer();
1506 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
1507 DPRINTF("io window exit\n");
1508 ret
= EXCP_INTERRUPT
;
1511 fprintf(stderr
, "error: kvm run failed %s\n",
1512 strerror(-run_ret
));
1516 switch (run
->exit_reason
) {
1518 DPRINTF("handle_io\n");
1519 kvm_handle_io(run
->io
.port
,
1520 (uint8_t *)run
+ run
->io
.data_offset
,
1527 DPRINTF("handle_mmio\n");
1528 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
1531 run
->mmio
.is_write
);
1534 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1535 DPRINTF("irq_window_open\n");
1536 ret
= EXCP_INTERRUPT
;
1538 case KVM_EXIT_SHUTDOWN
:
1539 DPRINTF("shutdown\n");
1540 qemu_system_reset_request();
1541 ret
= EXCP_INTERRUPT
;
1543 case KVM_EXIT_UNKNOWN
:
1544 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
1545 (uint64_t)run
->hw
.hardware_exit_reason
);
1548 case KVM_EXIT_INTERNAL_ERROR
:
1549 ret
= kvm_handle_internal_error(env
, run
);
1552 DPRINTF("kvm_arch_handle_exit\n");
1553 ret
= kvm_arch_handle_exit(env
, run
);
1559 cpu_dump_state(env
, stderr
, fprintf
, CPU_DUMP_CODE
);
1560 vm_stop(RUN_STATE_INTERNAL_ERROR
);
1563 env
->exit_request
= 0;
1567 int kvm_ioctl(KVMState
*s
, int type
, ...)
1574 arg
= va_arg(ap
, void *);
1577 ret
= ioctl(s
->fd
, type
, arg
);
1584 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
1591 arg
= va_arg(ap
, void *);
1594 ret
= ioctl(s
->vmfd
, type
, arg
);
1601 int kvm_vcpu_ioctl(CPUArchState
*env
, int type
, ...)
1608 arg
= va_arg(ap
, void *);
1611 ret
= ioctl(env
->kvm_fd
, type
, arg
);
1618 int kvm_has_sync_mmu(void)
1620 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
1623 int kvm_has_vcpu_events(void)
1625 return kvm_state
->vcpu_events
;
1628 int kvm_has_robust_singlestep(void)
1630 return kvm_state
->robust_singlestep
;
1633 int kvm_has_debugregs(void)
1635 return kvm_state
->debugregs
;
1638 int kvm_has_xsave(void)
1640 return kvm_state
->xsave
;
1643 int kvm_has_xcrs(void)
1645 return kvm_state
->xcrs
;
1648 int kvm_has_pit_state2(void)
1650 return kvm_state
->pit_state2
;
1653 int kvm_has_many_ioeventfds(void)
1655 if (!kvm_enabled()) {
1658 return kvm_state
->many_ioeventfds
;
1661 int kvm_has_gsi_routing(void)
1663 #ifdef KVM_CAP_IRQ_ROUTING
1664 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1670 int kvm_allows_irq0_override(void)
1672 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1675 void *kvm_vmalloc(ram_addr_t size
)
1680 mem
= kvm_arch_vmalloc(size
);
1685 return qemu_vmalloc(size
);
1688 void kvm_setup_guest_memory(void *start
, size_t size
)
1690 if (!kvm_has_sync_mmu()) {
1691 int ret
= qemu_madvise(start
, size
, QEMU_MADV_DONTFORK
);
1694 perror("qemu_madvise");
1696 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1702 #ifdef KVM_CAP_SET_GUEST_DEBUG
1703 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUArchState
*env
,
1706 struct kvm_sw_breakpoint
*bp
;
1708 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1716 int kvm_sw_breakpoints_active(CPUArchState
*env
)
1718 return !QTAILQ_EMPTY(&env
->kvm_state
->kvm_sw_breakpoints
);
1721 struct kvm_set_guest_debug_data
{
1722 struct kvm_guest_debug dbg
;
1727 static void kvm_invoke_set_guest_debug(void *data
)
1729 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1730 CPUArchState
*env
= dbg_data
->env
;
1732 dbg_data
->err
= kvm_vcpu_ioctl(env
, KVM_SET_GUEST_DEBUG
, &dbg_data
->dbg
);
1735 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1737 struct kvm_set_guest_debug_data data
;
1739 data
.dbg
.control
= reinject_trap
;
1741 if (env
->singlestep_enabled
) {
1742 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1744 kvm_arch_update_guest_debug(env
, &data
.dbg
);
1747 run_on_cpu(env
, kvm_invoke_set_guest_debug
, &data
);
1751 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1752 target_ulong len
, int type
)
1754 struct kvm_sw_breakpoint
*bp
;
1758 if (type
== GDB_BREAKPOINT_SW
) {
1759 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1765 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
1772 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1778 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1781 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1787 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1788 err
= kvm_update_guest_debug(env
, 0);
1796 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1797 target_ulong len
, int type
)
1799 struct kvm_sw_breakpoint
*bp
;
1803 if (type
== GDB_BREAKPOINT_SW
) {
1804 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1809 if (bp
->use_count
> 1) {
1814 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1819 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1822 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1828 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1829 err
= kvm_update_guest_debug(env
, 0);
1837 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1839 struct kvm_sw_breakpoint
*bp
, *next
;
1840 KVMState
*s
= current_env
->kvm_state
;
1843 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1844 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
1845 /* Try harder to find a CPU that currently sees the breakpoint. */
1846 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1847 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0) {
1853 kvm_arch_remove_all_hw_breakpoints();
1855 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1856 kvm_update_guest_debug(env
, 0);
1860 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1862 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
)
1867 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1868 target_ulong len
, int type
)
1873 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
1874 target_ulong len
, int type
)
1879 void kvm_remove_all_breakpoints(CPUArchState
*current_env
)
1882 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1884 int kvm_set_signal_mask(CPUArchState
*env
, const sigset_t
*sigset
)
1886 struct kvm_signal_mask
*sigmask
;
1890 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1893 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1896 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1897 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1903 int kvm_set_ioeventfd_mmio(int fd
, uint32_t addr
, uint32_t val
, bool assign
,
1907 struct kvm_ioeventfd iofd
;
1909 iofd
.datamatch
= val
;
1912 iofd
.flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
;
1915 if (!kvm_enabled()) {
1920 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1923 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
1932 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1934 struct kvm_ioeventfd kick
= {
1938 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1942 if (!kvm_enabled()) {
1946 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1948 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1955 int kvm_on_sigbus_vcpu(CPUArchState
*env
, int code
, void *addr
)
1957 return kvm_arch_on_sigbus_vcpu(env
, code
, addr
);
1960 int kvm_on_sigbus(int code
, void *addr
)
1962 return kvm_arch_on_sigbus(code
, addr
);