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"
30 /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
31 #define PAGE_SIZE TARGET_PAGE_SIZE
36 #define DPRINTF(fmt, ...) \
37 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
39 #define DPRINTF(fmt, ...) \
43 typedef struct KVMSlot
45 target_phys_addr_t start_addr
;
46 ram_addr_t memory_size
;
47 ram_addr_t phys_offset
;
52 typedef struct kvm_dirty_log KVMDirtyLog
;
60 #ifdef KVM_CAP_COALESCED_MMIO
61 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
63 int broken_set_mem_region
;
66 int robust_singlestep
;
67 #ifdef KVM_CAP_SET_GUEST_DEBUG
68 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
70 int irqchip_in_kernel
;
74 static KVMState
*kvm_state
;
76 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
80 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
81 /* KVM private memory slots */
84 if (s
->slots
[i
].memory_size
== 0)
88 fprintf(stderr
, "%s: no free slot available\n", __func__
);
92 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
93 target_phys_addr_t start_addr
,
94 target_phys_addr_t end_addr
)
98 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
99 KVMSlot
*mem
= &s
->slots
[i
];
101 if (start_addr
== mem
->start_addr
&&
102 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
111 * Find overlapping slot with lowest start address
113 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
114 target_phys_addr_t start_addr
,
115 target_phys_addr_t end_addr
)
117 KVMSlot
*found
= NULL
;
120 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
121 KVMSlot
*mem
= &s
->slots
[i
];
123 if (mem
->memory_size
== 0 ||
124 (found
&& found
->start_addr
< mem
->start_addr
)) {
128 if (end_addr
> mem
->start_addr
&&
129 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
137 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
139 struct kvm_userspace_memory_region mem
;
141 mem
.slot
= slot
->slot
;
142 mem
.guest_phys_addr
= slot
->start_addr
;
143 mem
.memory_size
= slot
->memory_size
;
144 mem
.userspace_addr
= (unsigned long)qemu_get_ram_ptr(slot
->phys_offset
);
145 mem
.flags
= slot
->flags
;
146 if (s
->migration_log
) {
147 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
149 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
152 static void kvm_reset_vcpu(void *opaque
)
154 CPUState
*env
= opaque
;
156 kvm_arch_reset_vcpu(env
);
159 int kvm_irqchip_in_kernel(void)
161 return kvm_state
->irqchip_in_kernel
;
164 int kvm_pit_in_kernel(void)
166 return kvm_state
->pit_in_kernel
;
170 int kvm_init_vcpu(CPUState
*env
)
172 KVMState
*s
= kvm_state
;
176 DPRINTF("kvm_init_vcpu\n");
178 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
180 DPRINTF("kvm_create_vcpu failed\n");
187 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
189 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
193 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
195 if (env
->kvm_run
== MAP_FAILED
) {
197 DPRINTF("mmap'ing vcpu state failed\n");
201 #ifdef KVM_CAP_COALESCED_MMIO
202 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
)
203 s
->coalesced_mmio_ring
= (void *) env
->kvm_run
+
204 s
->coalesced_mmio
* PAGE_SIZE
;
207 ret
= kvm_arch_init_vcpu(env
);
209 qemu_register_reset(kvm_reset_vcpu
, env
);
210 kvm_arch_reset_vcpu(env
);
217 * dirty pages logging control
219 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr
,
220 ram_addr_t size
, int flags
, int mask
)
222 KVMState
*s
= kvm_state
;
223 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
227 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
228 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
229 (target_phys_addr_t
)(phys_addr
+ size
- 1));
233 old_flags
= mem
->flags
;
235 flags
= (mem
->flags
& ~mask
) | flags
;
238 /* If nothing changed effectively, no need to issue ioctl */
239 if (s
->migration_log
) {
240 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
242 if (flags
== old_flags
) {
246 return kvm_set_user_memory_region(s
, mem
);
249 int kvm_log_start(target_phys_addr_t phys_addr
, ram_addr_t size
)
251 return kvm_dirty_pages_log_change(phys_addr
, size
,
252 KVM_MEM_LOG_DIRTY_PAGES
,
253 KVM_MEM_LOG_DIRTY_PAGES
);
256 int kvm_log_stop(target_phys_addr_t phys_addr
, ram_addr_t size
)
258 return kvm_dirty_pages_log_change(phys_addr
, size
,
260 KVM_MEM_LOG_DIRTY_PAGES
);
263 static int kvm_set_migration_log(int enable
)
265 KVMState
*s
= kvm_state
;
269 s
->migration_log
= enable
;
271 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
274 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
277 err
= kvm_set_user_memory_region(s
, mem
);
285 static int test_le_bit(unsigned long nr
, unsigned char *addr
)
287 return (addr
[nr
>> 3] >> (nr
& 7)) & 1;
291 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
292 * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty().
293 * This means all bits are set to dirty.
295 * @start_add: start of logged region.
296 * @end_addr: end of logged region.
298 static int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
,
299 target_phys_addr_t end_addr
)
301 KVMState
*s
= kvm_state
;
302 unsigned long size
, allocated_size
= 0;
303 target_phys_addr_t phys_addr
;
309 d
.dirty_bitmap
= NULL
;
310 while (start_addr
< end_addr
) {
311 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
316 size
= ((mem
->memory_size
>> TARGET_PAGE_BITS
) + 7) / 8;
317 if (!d
.dirty_bitmap
) {
318 d
.dirty_bitmap
= qemu_malloc(size
);
319 } else if (size
> allocated_size
) {
320 d
.dirty_bitmap
= qemu_realloc(d
.dirty_bitmap
, size
);
322 allocated_size
= size
;
323 memset(d
.dirty_bitmap
, 0, allocated_size
);
327 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
328 DPRINTF("ioctl failed %d\n", errno
);
333 for (phys_addr
= mem
->start_addr
, addr
= mem
->phys_offset
;
334 phys_addr
< mem
->start_addr
+ mem
->memory_size
;
335 phys_addr
+= TARGET_PAGE_SIZE
, addr
+= TARGET_PAGE_SIZE
) {
336 unsigned char *bitmap
= (unsigned char *)d
.dirty_bitmap
;
337 unsigned nr
= (phys_addr
- mem
->start_addr
) >> TARGET_PAGE_BITS
;
339 if (test_le_bit(nr
, bitmap
)) {
340 cpu_physical_memory_set_dirty(addr
);
343 start_addr
= phys_addr
;
345 qemu_free(d
.dirty_bitmap
);
350 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
353 #ifdef KVM_CAP_COALESCED_MMIO
354 KVMState
*s
= kvm_state
;
356 if (s
->coalesced_mmio
) {
357 struct kvm_coalesced_mmio_zone zone
;
362 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
369 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
372 #ifdef KVM_CAP_COALESCED_MMIO
373 KVMState
*s
= kvm_state
;
375 if (s
->coalesced_mmio
) {
376 struct kvm_coalesced_mmio_zone zone
;
381 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
388 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
392 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
400 static void kvm_set_phys_mem(target_phys_addr_t start_addr
,
402 ram_addr_t phys_offset
)
404 KVMState
*s
= kvm_state
;
405 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
409 if (start_addr
& ~TARGET_PAGE_MASK
) {
410 if (flags
>= IO_MEM_UNASSIGNED
) {
411 if (!kvm_lookup_overlapping_slot(s
, start_addr
,
412 start_addr
+ size
)) {
415 fprintf(stderr
, "Unaligned split of a KVM memory slot\n");
417 fprintf(stderr
, "Only page-aligned memory slots supported\n");
422 /* KVM does not support read-only slots */
423 phys_offset
&= ~IO_MEM_ROM
;
426 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
431 if (flags
< IO_MEM_UNASSIGNED
&& start_addr
>= mem
->start_addr
&&
432 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
433 (phys_offset
- start_addr
== mem
->phys_offset
- mem
->start_addr
)) {
434 /* The new slot fits into the existing one and comes with
435 * identical parameters - nothing to be done. */
441 /* unregister the overlapping slot */
442 mem
->memory_size
= 0;
443 err
= kvm_set_user_memory_region(s
, mem
);
445 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
446 __func__
, strerror(-err
));
450 /* Workaround for older KVM versions: we can't join slots, even not by
451 * unregistering the previous ones and then registering the larger
452 * slot. We have to maintain the existing fragmentation. Sigh.
454 * This workaround assumes that the new slot starts at the same
455 * address as the first existing one. If not or if some overlapping
456 * slot comes around later, we will fail (not seen in practice so far)
457 * - and actually require a recent KVM version. */
458 if (s
->broken_set_mem_region
&&
459 old
.start_addr
== start_addr
&& old
.memory_size
< size
&&
460 flags
< IO_MEM_UNASSIGNED
) {
461 mem
= kvm_alloc_slot(s
);
462 mem
->memory_size
= old
.memory_size
;
463 mem
->start_addr
= old
.start_addr
;
464 mem
->phys_offset
= old
.phys_offset
;
467 err
= kvm_set_user_memory_region(s
, mem
);
469 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
474 start_addr
+= old
.memory_size
;
475 phys_offset
+= old
.memory_size
;
476 size
-= old
.memory_size
;
480 /* register prefix slot */
481 if (old
.start_addr
< start_addr
) {
482 mem
= kvm_alloc_slot(s
);
483 mem
->memory_size
= start_addr
- old
.start_addr
;
484 mem
->start_addr
= old
.start_addr
;
485 mem
->phys_offset
= old
.phys_offset
;
488 err
= kvm_set_user_memory_region(s
, mem
);
490 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
491 __func__
, strerror(-err
));
496 /* register suffix slot */
497 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
498 ram_addr_t size_delta
;
500 mem
= kvm_alloc_slot(s
);
501 mem
->start_addr
= start_addr
+ size
;
502 size_delta
= mem
->start_addr
- old
.start_addr
;
503 mem
->memory_size
= old
.memory_size
- size_delta
;
504 mem
->phys_offset
= old
.phys_offset
+ size_delta
;
507 err
= kvm_set_user_memory_region(s
, mem
);
509 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
510 __func__
, strerror(-err
));
516 /* in case the KVM bug workaround already "consumed" the new slot */
520 /* KVM does not need to know about this memory */
521 if (flags
>= IO_MEM_UNASSIGNED
)
524 mem
= kvm_alloc_slot(s
);
525 mem
->memory_size
= size
;
526 mem
->start_addr
= start_addr
;
527 mem
->phys_offset
= phys_offset
;
530 err
= kvm_set_user_memory_region(s
, mem
);
532 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
538 static void kvm_client_set_memory(struct CPUPhysMemoryClient
*client
,
539 target_phys_addr_t start_addr
,
541 ram_addr_t phys_offset
)
543 kvm_set_phys_mem(start_addr
, size
, phys_offset
);
546 static int kvm_client_sync_dirty_bitmap(struct CPUPhysMemoryClient
*client
,
547 target_phys_addr_t start_addr
,
548 target_phys_addr_t end_addr
)
550 return kvm_physical_sync_dirty_bitmap(start_addr
, end_addr
);
553 static int kvm_client_migration_log(struct CPUPhysMemoryClient
*client
,
556 return kvm_set_migration_log(enable
);
559 static CPUPhysMemoryClient kvm_cpu_phys_memory_client
= {
560 .set_memory
= kvm_client_set_memory
,
561 .sync_dirty_bitmap
= kvm_client_sync_dirty_bitmap
,
562 .migration_log
= kvm_client_migration_log
,
565 int kvm_init(int smp_cpus
)
567 static const char upgrade_note
[] =
568 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
569 "(see http://sourceforge.net/projects/kvm).\n";
575 fprintf(stderr
, "No SMP KVM support, use '-smp 1'\n");
579 s
= qemu_mallocz(sizeof(KVMState
));
581 #ifdef KVM_CAP_SET_GUEST_DEBUG
582 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
584 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++)
585 s
->slots
[i
].slot
= i
;
588 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
590 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
595 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
596 if (ret
< KVM_API_VERSION
) {
599 fprintf(stderr
, "kvm version too old\n");
603 if (ret
> KVM_API_VERSION
) {
605 fprintf(stderr
, "kvm version not supported\n");
609 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
612 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
613 "your host kernel command line\n");
618 /* initially, KVM allocated its own memory and we had to jump through
619 * hooks to make phys_ram_base point to this. Modern versions of KVM
620 * just use a user allocated buffer so we can use regular pages
621 * unmodified. Make sure we have a sufficiently modern version of KVM.
623 if (!kvm_check_extension(s
, KVM_CAP_USER_MEMORY
)) {
625 fprintf(stderr
, "kvm does not support KVM_CAP_USER_MEMORY\n%s",
630 /* There was a nasty bug in < kvm-80 that prevents memory slots from being
631 * destroyed properly. Since we rely on this capability, refuse to work
632 * with any kernel without this capability. */
633 if (!kvm_check_extension(s
, KVM_CAP_DESTROY_MEMORY_REGION_WORKS
)) {
637 "KVM kernel module broken (DESTROY_MEMORY_REGION).\n%s",
642 s
->coalesced_mmio
= 0;
643 #ifdef KVM_CAP_COALESCED_MMIO
644 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
645 s
->coalesced_mmio_ring
= NULL
;
648 s
->broken_set_mem_region
= 1;
649 #ifdef KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
650 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
652 s
->broken_set_mem_region
= 0;
657 #ifdef KVM_CAP_VCPU_EVENTS
658 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
661 s
->robust_singlestep
= 0;
662 #ifdef KVM_CAP_X86_ROBUST_SINGLESTEP
663 s
->robust_singlestep
=
664 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
667 ret
= kvm_arch_init(s
, smp_cpus
);
672 cpu_register_phys_memory_client(&kvm_cpu_phys_memory_client
);
688 static int kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
694 for (i
= 0; i
< count
; i
++) {
695 if (direction
== KVM_EXIT_IO_IN
) {
698 stb_p(ptr
, cpu_inb(port
));
701 stw_p(ptr
, cpu_inw(port
));
704 stl_p(ptr
, cpu_inl(port
));
710 cpu_outb(port
, ldub_p(ptr
));
713 cpu_outw(port
, lduw_p(ptr
));
716 cpu_outl(port
, ldl_p(ptr
));
727 void kvm_flush_coalesced_mmio_buffer(void)
729 #ifdef KVM_CAP_COALESCED_MMIO
730 KVMState
*s
= kvm_state
;
731 if (s
->coalesced_mmio_ring
) {
732 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
733 while (ring
->first
!= ring
->last
) {
734 struct kvm_coalesced_mmio
*ent
;
736 ent
= &ring
->coalesced_mmio
[ring
->first
];
738 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
740 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
746 void kvm_cpu_synchronize_state(CPUState
*env
)
748 if (!env
->kvm_vcpu_dirty
) {
749 kvm_arch_get_registers(env
);
750 env
->kvm_vcpu_dirty
= 1;
754 void kvm_cpu_synchronize_post_reset(CPUState
*env
)
756 kvm_arch_put_registers(env
, KVM_PUT_RESET_STATE
);
757 env
->kvm_vcpu_dirty
= 0;
760 void kvm_cpu_synchronize_post_init(CPUState
*env
)
762 kvm_arch_put_registers(env
, KVM_PUT_FULL_STATE
);
763 env
->kvm_vcpu_dirty
= 0;
766 int kvm_cpu_exec(CPUState
*env
)
768 struct kvm_run
*run
= env
->kvm_run
;
771 DPRINTF("kvm_cpu_exec()\n");
774 #ifndef CONFIG_IOTHREAD
775 if (env
->exit_request
) {
776 DPRINTF("interrupt exit requested\n");
782 if (env
->kvm_vcpu_dirty
) {
783 kvm_arch_put_registers(env
, KVM_PUT_RUNTIME_STATE
);
784 env
->kvm_vcpu_dirty
= 0;
787 kvm_arch_pre_run(env
, run
);
788 qemu_mutex_unlock_iothread();
789 ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
790 qemu_mutex_lock_iothread();
791 kvm_arch_post_run(env
, run
);
793 if (ret
== -EINTR
|| ret
== -EAGAIN
) {
795 DPRINTF("io window exit\n");
801 DPRINTF("kvm run failed %s\n", strerror(-ret
));
805 kvm_flush_coalesced_mmio_buffer();
807 ret
= 0; /* exit loop */
808 switch (run
->exit_reason
) {
810 DPRINTF("handle_io\n");
811 ret
= kvm_handle_io(run
->io
.port
,
812 (uint8_t *)run
+ run
->io
.data_offset
,
818 DPRINTF("handle_mmio\n");
819 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
825 case KVM_EXIT_IRQ_WINDOW_OPEN
:
826 DPRINTF("irq_window_open\n");
828 case KVM_EXIT_SHUTDOWN
:
829 DPRINTF("shutdown\n");
830 qemu_system_reset_request();
833 case KVM_EXIT_UNKNOWN
:
834 DPRINTF("kvm_exit_unknown\n");
836 case KVM_EXIT_FAIL_ENTRY
:
837 DPRINTF("kvm_exit_fail_entry\n");
839 case KVM_EXIT_EXCEPTION
:
840 DPRINTF("kvm_exit_exception\n");
843 DPRINTF("kvm_exit_debug\n");
844 #ifdef KVM_CAP_SET_GUEST_DEBUG
845 if (kvm_arch_debug(&run
->debug
.arch
)) {
846 gdb_set_stop_cpu(env
);
848 env
->exception_index
= EXCP_DEBUG
;
851 /* re-enter, this exception was guest-internal */
853 #endif /* KVM_CAP_SET_GUEST_DEBUG */
856 DPRINTF("kvm_arch_handle_exit\n");
857 ret
= kvm_arch_handle_exit(env
, run
);
862 if (env
->exit_request
) {
863 env
->exit_request
= 0;
864 env
->exception_index
= EXCP_INTERRUPT
;
870 int kvm_ioctl(KVMState
*s
, int type
, ...)
877 arg
= va_arg(ap
, void *);
880 ret
= ioctl(s
->fd
, type
, arg
);
887 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
894 arg
= va_arg(ap
, void *);
897 ret
= ioctl(s
->vmfd
, type
, arg
);
904 int kvm_vcpu_ioctl(CPUState
*env
, int type
, ...)
911 arg
= va_arg(ap
, void *);
914 ret
= ioctl(env
->kvm_fd
, type
, arg
);
921 int kvm_has_sync_mmu(void)
923 #ifdef KVM_CAP_SYNC_MMU
924 KVMState
*s
= kvm_state
;
926 return kvm_check_extension(s
, KVM_CAP_SYNC_MMU
);
932 int kvm_has_vcpu_events(void)
934 return kvm_state
->vcpu_events
;
937 int kvm_has_robust_singlestep(void)
939 return kvm_state
->robust_singlestep
;
942 void kvm_setup_guest_memory(void *start
, size_t size
)
944 if (!kvm_has_sync_mmu()) {
946 int ret
= madvise(start
, size
, MADV_DONTFORK
);
954 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
960 #ifdef KVM_CAP_SET_GUEST_DEBUG
961 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
963 #ifdef CONFIG_IOTHREAD
964 if (env
!= cpu_single_env
) {
971 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*env
,
974 struct kvm_sw_breakpoint
*bp
;
976 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
983 int kvm_sw_breakpoints_active(CPUState
*env
)
985 return !QTAILQ_EMPTY(&env
->kvm_state
->kvm_sw_breakpoints
);
988 struct kvm_set_guest_debug_data
{
989 struct kvm_guest_debug dbg
;
994 static void kvm_invoke_set_guest_debug(void *data
)
996 struct kvm_set_guest_debug_data
*dbg_data
= data
;
997 CPUState
*env
= dbg_data
->env
;
999 dbg_data
->err
= kvm_vcpu_ioctl(env
, KVM_SET_GUEST_DEBUG
, &dbg_data
->dbg
);
1002 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
1004 struct kvm_set_guest_debug_data data
;
1006 data
.dbg
.control
= reinject_trap
;
1008 if (env
->singlestep_enabled
) {
1009 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1011 kvm_arch_update_guest_debug(env
, &data
.dbg
);
1014 on_vcpu(env
, kvm_invoke_set_guest_debug
, &data
);
1018 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
1019 target_ulong len
, int type
)
1021 struct kvm_sw_breakpoint
*bp
;
1025 if (type
== GDB_BREAKPOINT_SW
) {
1026 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1032 bp
= qemu_malloc(sizeof(struct kvm_sw_breakpoint
));
1038 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1044 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1047 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1052 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1053 err
= kvm_update_guest_debug(env
, 0);
1060 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
1061 target_ulong len
, int type
)
1063 struct kvm_sw_breakpoint
*bp
;
1067 if (type
== GDB_BREAKPOINT_SW
) {
1068 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1072 if (bp
->use_count
> 1) {
1077 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1081 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1084 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1089 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1090 err
= kvm_update_guest_debug(env
, 0);
1097 void kvm_remove_all_breakpoints(CPUState
*current_env
)
1099 struct kvm_sw_breakpoint
*bp
, *next
;
1100 KVMState
*s
= current_env
->kvm_state
;
1103 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1104 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
1105 /* Try harder to find a CPU that currently sees the breakpoint. */
1106 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1107 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0)
1112 kvm_arch_remove_all_hw_breakpoints();
1114 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1115 kvm_update_guest_debug(env
, 0);
1118 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1120 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
1125 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
1126 target_ulong len
, int type
)
1131 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
1132 target_ulong len
, int type
)
1137 void kvm_remove_all_breakpoints(CPUState
*current_env
)
1140 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1142 int kvm_set_signal_mask(CPUState
*env
, const sigset_t
*sigset
)
1144 struct kvm_signal_mask
*sigmask
;
1148 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1150 sigmask
= qemu_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1153 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1154 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1160 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1162 #ifdef KVM_IOEVENTFD
1163 struct kvm_ioeventfd kick
= {
1167 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1174 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1175 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);