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"
31 /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
32 #define PAGE_SIZE TARGET_PAGE_SIZE
37 #define DPRINTF(fmt, ...) \
38 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
40 #define DPRINTF(fmt, ...) \
46 typedef struct KVMSlot
48 target_phys_addr_t start_addr
;
49 ram_addr_t memory_size
;
50 ram_addr_t phys_offset
;
55 typedef struct kvm_dirty_log KVMDirtyLog
;
63 #ifdef KVM_CAP_COALESCED_MMIO
64 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
66 int broken_set_mem_region
;
69 int robust_singlestep
;
71 #ifdef KVM_CAP_SET_GUEST_DEBUG
72 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
74 int irqchip_in_kernel
;
79 static KVMState
*kvm_state
;
83 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
87 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
88 /* KVM private memory slots */
91 if (s
->slots
[i
].memory_size
== 0)
95 fprintf(stderr
, "%s: no free slot available\n", __func__
);
99 static KVMSlot
*kvm_lookup_matching_slot(KVMState
*s
,
100 target_phys_addr_t start_addr
,
101 target_phys_addr_t end_addr
)
105 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
106 KVMSlot
*mem
= &s
->slots
[i
];
108 if (start_addr
== mem
->start_addr
&&
109 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
118 * Find overlapping slot with lowest start address
120 static KVMSlot
*kvm_lookup_overlapping_slot(KVMState
*s
,
121 target_phys_addr_t start_addr
,
122 target_phys_addr_t end_addr
)
124 KVMSlot
*found
= NULL
;
127 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
128 KVMSlot
*mem
= &s
->slots
[i
];
130 if (mem
->memory_size
== 0 ||
131 (found
&& found
->start_addr
< mem
->start_addr
)) {
135 if (end_addr
> mem
->start_addr
&&
136 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
144 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
146 struct kvm_userspace_memory_region mem
;
148 mem
.slot
= slot
->slot
;
149 mem
.guest_phys_addr
= slot
->start_addr
;
150 mem
.memory_size
= slot
->memory_size
;
151 mem
.userspace_addr
= (unsigned long)qemu_get_ram_ptr(slot
->phys_offset
);
152 mem
.flags
= slot
->flags
;
153 if (s
->migration_log
) {
154 mem
.flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
156 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
160 static void kvm_reset_vcpu(void *opaque
)
162 CPUState
*env
= opaque
;
164 kvm_arch_reset_vcpu(env
);
168 int kvm_irqchip_in_kernel(void)
170 return kvm_state
->irqchip_in_kernel
;
173 int kvm_pit_in_kernel(void)
175 return kvm_state
->pit_in_kernel
;
180 int kvm_init_vcpu(CPUState
*env
)
182 KVMState
*s
= kvm_state
;
186 DPRINTF("kvm_init_vcpu\n");
188 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
190 DPRINTF("kvm_create_vcpu failed\n");
197 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
199 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
203 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
205 if (env
->kvm_run
== MAP_FAILED
) {
207 DPRINTF("mmap'ing vcpu state failed\n");
211 #ifdef KVM_CAP_COALESCED_MMIO
212 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
)
213 s
->coalesced_mmio_ring
= (void *) env
->kvm_run
+
214 s
->coalesced_mmio
* PAGE_SIZE
;
217 ret
= kvm_arch_init_vcpu(env
);
219 qemu_register_reset(kvm_reset_vcpu
, env
);
220 kvm_arch_reset_vcpu(env
);
228 * dirty pages logging control
230 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr
,
231 ram_addr_t size
, int flags
, int mask
)
233 KVMState
*s
= kvm_state
;
234 KVMSlot
*mem
= kvm_lookup_matching_slot(s
, phys_addr
, phys_addr
+ size
);
238 fprintf(stderr
, "BUG: %s: invalid parameters " TARGET_FMT_plx
"-"
239 TARGET_FMT_plx
"\n", __func__
, phys_addr
,
240 (target_phys_addr_t
)(phys_addr
+ size
- 1));
244 old_flags
= mem
->flags
;
246 flags
= (mem
->flags
& ~mask
) | flags
;
249 /* If nothing changed effectively, no need to issue ioctl */
250 if (s
->migration_log
) {
251 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
253 if (flags
== old_flags
) {
257 return kvm_set_user_memory_region(s
, mem
);
260 int kvm_log_start(target_phys_addr_t phys_addr
, ram_addr_t size
)
262 return kvm_dirty_pages_log_change(phys_addr
, size
,
263 KVM_MEM_LOG_DIRTY_PAGES
,
264 KVM_MEM_LOG_DIRTY_PAGES
);
267 int kvm_log_stop(target_phys_addr_t phys_addr
, ram_addr_t size
)
269 return kvm_dirty_pages_log_change(phys_addr
, size
,
271 KVM_MEM_LOG_DIRTY_PAGES
);
274 static int kvm_set_migration_log(int enable
)
276 KVMState
*s
= kvm_state
;
280 s
->migration_log
= enable
;
282 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
285 if (!!(mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) == enable
) {
288 err
= kvm_set_user_memory_region(s
, mem
);
296 /* get kvm's dirty pages bitmap and update qemu's */
297 static int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
298 unsigned long *bitmap
,
299 unsigned long offset
,
300 unsigned long mem_size
)
303 unsigned long page_number
, addr
, addr1
, c
;
305 unsigned int len
= ((mem_size
/ TARGET_PAGE_SIZE
) + HOST_LONG_BITS
- 1) /
309 * bitmap-traveling is faster than memory-traveling (for addr...)
310 * especially when most of the memory is not dirty.
312 for (i
= 0; i
< len
; i
++) {
313 if (bitmap
[i
] != 0) {
314 c
= leul_to_cpu(bitmap
[i
]);
318 page_number
= i
* HOST_LONG_BITS
+ j
;
319 addr1
= page_number
* TARGET_PAGE_SIZE
;
320 addr
= offset
+ addr1
;
321 ram_addr
= cpu_get_physical_page_desc(addr
);
322 cpu_physical_memory_set_dirty(ram_addr
);
329 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
332 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
333 * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty().
334 * This means all bits are set to dirty.
336 * @start_add: start of logged region.
337 * @end_addr: end of logged region.
339 static int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
,
340 target_phys_addr_t end_addr
)
342 KVMState
*s
= kvm_state
;
343 unsigned long size
, allocated_size
= 0;
348 d
.dirty_bitmap
= NULL
;
349 while (start_addr
< end_addr
) {
350 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, end_addr
);
355 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
), HOST_LONG_BITS
) / 8;
356 if (!d
.dirty_bitmap
) {
357 d
.dirty_bitmap
= qemu_malloc(size
);
358 } else if (size
> allocated_size
) {
359 d
.dirty_bitmap
= qemu_realloc(d
.dirty_bitmap
, size
);
361 allocated_size
= size
;
362 memset(d
.dirty_bitmap
, 0, allocated_size
);
366 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
367 DPRINTF("ioctl failed %d\n", errno
);
372 kvm_get_dirty_pages_log_range(mem
->start_addr
, d
.dirty_bitmap
,
373 mem
->start_addr
, mem
->memory_size
);
374 start_addr
= mem
->start_addr
+ mem
->memory_size
;
376 qemu_free(d
.dirty_bitmap
);
381 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
384 #ifdef KVM_CAP_COALESCED_MMIO
385 KVMState
*s
= kvm_state
;
387 if (s
->coalesced_mmio
) {
388 struct kvm_coalesced_mmio_zone zone
;
393 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
400 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
403 #ifdef KVM_CAP_COALESCED_MMIO
404 KVMState
*s
= kvm_state
;
406 if (s
->coalesced_mmio
) {
407 struct kvm_coalesced_mmio_zone zone
;
412 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
419 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
423 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
431 static void kvm_set_phys_mem(target_phys_addr_t start_addr
,
433 ram_addr_t phys_offset
)
435 KVMState
*s
= kvm_state
;
436 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
440 if (start_addr
& ~TARGET_PAGE_MASK
) {
441 if (flags
>= IO_MEM_UNASSIGNED
) {
442 if (!kvm_lookup_overlapping_slot(s
, start_addr
,
443 start_addr
+ size
)) {
446 fprintf(stderr
, "Unaligned split of a KVM memory slot\n");
448 fprintf(stderr
, "Only page-aligned memory slots supported\n");
453 /* KVM does not support read-only slots */
454 phys_offset
&= ~IO_MEM_ROM
;
457 mem
= kvm_lookup_overlapping_slot(s
, start_addr
, start_addr
+ size
);
462 if (flags
< IO_MEM_UNASSIGNED
&& start_addr
>= mem
->start_addr
&&
463 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
464 (phys_offset
- start_addr
== mem
->phys_offset
- mem
->start_addr
)) {
465 /* The new slot fits into the existing one and comes with
466 * identical parameters - nothing to be done. */
472 /* unregister the overlapping slot */
473 mem
->memory_size
= 0;
474 err
= kvm_set_user_memory_region(s
, mem
);
476 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
477 __func__
, strerror(-err
));
481 /* Workaround for older KVM versions: we can't join slots, even not by
482 * unregistering the previous ones and then registering the larger
483 * slot. We have to maintain the existing fragmentation. Sigh.
485 * This workaround assumes that the new slot starts at the same
486 * address as the first existing one. If not or if some overlapping
487 * slot comes around later, we will fail (not seen in practice so far)
488 * - and actually require a recent KVM version. */
489 if (s
->broken_set_mem_region
&&
490 old
.start_addr
== start_addr
&& old
.memory_size
< size
&&
491 flags
< IO_MEM_UNASSIGNED
) {
492 mem
= kvm_alloc_slot(s
);
493 mem
->memory_size
= old
.memory_size
;
494 mem
->start_addr
= old
.start_addr
;
495 mem
->phys_offset
= old
.phys_offset
;
498 err
= kvm_set_user_memory_region(s
, mem
);
500 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
505 start_addr
+= old
.memory_size
;
506 phys_offset
+= old
.memory_size
;
507 size
-= old
.memory_size
;
511 /* register prefix slot */
512 if (old
.start_addr
< start_addr
) {
513 mem
= kvm_alloc_slot(s
);
514 mem
->memory_size
= start_addr
- old
.start_addr
;
515 mem
->start_addr
= old
.start_addr
;
516 mem
->phys_offset
= old
.phys_offset
;
519 err
= kvm_set_user_memory_region(s
, mem
);
521 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
522 __func__
, strerror(-err
));
527 /* register suffix slot */
528 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
529 ram_addr_t size_delta
;
531 mem
= kvm_alloc_slot(s
);
532 mem
->start_addr
= start_addr
+ size
;
533 size_delta
= mem
->start_addr
- old
.start_addr
;
534 mem
->memory_size
= old
.memory_size
- size_delta
;
535 mem
->phys_offset
= old
.phys_offset
+ size_delta
;
538 err
= kvm_set_user_memory_region(s
, mem
);
540 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
541 __func__
, strerror(-err
));
547 /* in case the KVM bug workaround already "consumed" the new slot */
551 /* KVM does not need to know about this memory */
552 if (flags
>= IO_MEM_UNASSIGNED
)
555 mem
= kvm_alloc_slot(s
);
556 mem
->memory_size
= size
;
557 mem
->start_addr
= start_addr
;
558 mem
->phys_offset
= phys_offset
;
561 err
= kvm_set_user_memory_region(s
, mem
);
563 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
569 static void kvm_client_set_memory(struct CPUPhysMemoryClient
*client
,
570 target_phys_addr_t start_addr
,
572 ram_addr_t phys_offset
)
574 kvm_set_phys_mem(start_addr
, size
, phys_offset
);
577 static int kvm_client_sync_dirty_bitmap(struct CPUPhysMemoryClient
*client
,
578 target_phys_addr_t start_addr
,
579 target_phys_addr_t end_addr
)
581 return kvm_physical_sync_dirty_bitmap(start_addr
, end_addr
);
584 static int kvm_client_migration_log(struct CPUPhysMemoryClient
*client
,
587 return kvm_set_migration_log(enable
);
590 static CPUPhysMemoryClient kvm_cpu_phys_memory_client
= {
591 .set_memory
= kvm_client_set_memory
,
592 .sync_dirty_bitmap
= kvm_client_sync_dirty_bitmap
,
593 .migration_log
= kvm_client_migration_log
,
597 void kvm_cpu_register_phys_memory_client(void)
599 cpu_register_phys_memory_client(&kvm_cpu_phys_memory_client
);
604 int kvm_init(int smp_cpus
)
606 static const char upgrade_note
[] =
607 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
608 "(see http://sourceforge.net/projects/kvm).\n";
613 s
= qemu_mallocz(sizeof(KVMState
));
615 #ifdef KVM_CAP_SET_GUEST_DEBUG
616 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
618 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++)
619 s
->slots
[i
].slot
= i
;
622 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
624 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
629 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
630 if (ret
< KVM_API_VERSION
) {
633 fprintf(stderr
, "kvm version too old\n");
637 if (ret
> KVM_API_VERSION
) {
639 fprintf(stderr
, "kvm version not supported\n");
643 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
646 fprintf(stderr
, "Please add the 'switch_amode' kernel parameter to "
647 "your host kernel command line\n");
652 /* initially, KVM allocated its own memory and we had to jump through
653 * hooks to make phys_ram_base point to this. Modern versions of KVM
654 * just use a user allocated buffer so we can use regular pages
655 * unmodified. Make sure we have a sufficiently modern version of KVM.
657 if (!kvm_check_extension(s
, KVM_CAP_USER_MEMORY
)) {
659 fprintf(stderr
, "kvm does not support KVM_CAP_USER_MEMORY\n%s",
664 /* There was a nasty bug in < kvm-80 that prevents memory slots from being
665 * destroyed properly. Since we rely on this capability, refuse to work
666 * with any kernel without this capability. */
667 if (!kvm_check_extension(s
, KVM_CAP_DESTROY_MEMORY_REGION_WORKS
)) {
671 "KVM kernel module broken (DESTROY_MEMORY_REGION).\n%s",
676 s
->coalesced_mmio
= 0;
677 #ifdef KVM_CAP_COALESCED_MMIO
678 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
679 s
->coalesced_mmio_ring
= NULL
;
682 s
->broken_set_mem_region
= 1;
683 #ifdef KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
684 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
686 s
->broken_set_mem_region
= 0;
691 #ifdef KVM_CAP_VCPU_EVENTS
692 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
695 s
->robust_singlestep
= 0;
696 #ifdef KVM_CAP_X86_ROBUST_SINGLESTEP
697 s
->robust_singlestep
=
698 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
702 #ifdef KVM_CAP_DEBUGREGS
703 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
708 s
->xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
713 s
->xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
716 ret
= kvm_arch_init(s
, smp_cpus
);
721 cpu_register_phys_memory_client(&kvm_cpu_phys_memory_client
);
738 static int kvm_handle_io(uint16_t port
, void *data
, int direction
, int size
,
744 for (i
= 0; i
< count
; i
++) {
745 if (direction
== KVM_EXIT_IO_IN
) {
748 stb_p(ptr
, cpu_inb(port
));
751 stw_p(ptr
, cpu_inw(port
));
754 stl_p(ptr
, cpu_inl(port
));
760 cpu_outb(port
, ldub_p(ptr
));
763 cpu_outw(port
, lduw_p(ptr
));
766 cpu_outl(port
, ldl_p(ptr
));
777 #ifdef KVM_CAP_INTERNAL_ERROR_DATA
778 static void kvm_handle_internal_error(CPUState
*env
, struct kvm_run
*run
)
781 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
784 fprintf(stderr
, "KVM internal error. Suberror: %d\n",
785 run
->internal
.suberror
);
787 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
788 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
789 i
, (uint64_t)run
->internal
.data
[i
]);
792 cpu_dump_state(env
, stderr
, fprintf
, 0);
793 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
794 fprintf(stderr
, "emulation failure\n");
795 if (!kvm_arch_stop_on_emulation_error(env
))
798 /* FIXME: Should trigger a qmp message to let management know
799 * something went wrong.
805 void kvm_flush_coalesced_mmio_buffer(void)
807 #ifdef KVM_CAP_COALESCED_MMIO
808 KVMState
*s
= kvm_state
;
809 if (s
->coalesced_mmio_ring
) {
810 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
811 while (ring
->first
!= ring
->last
) {
812 struct kvm_coalesced_mmio
*ent
;
814 ent
= &ring
->coalesced_mmio
[ring
->first
];
816 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
818 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
826 static void do_kvm_cpu_synchronize_state(void *_env
)
828 CPUState
*env
= _env
;
830 if (!env
->kvm_vcpu_dirty
) {
831 kvm_arch_get_registers(env
);
832 env
->kvm_vcpu_dirty
= 1;
836 void kvm_cpu_synchronize_state(CPUState
*env
)
838 if (!env
->kvm_vcpu_dirty
)
839 run_on_cpu(env
, do_kvm_cpu_synchronize_state
, env
);
842 void kvm_cpu_synchronize_post_reset(CPUState
*env
)
844 kvm_arch_put_registers(env
, KVM_PUT_RESET_STATE
);
845 env
->kvm_vcpu_dirty
= 0;
848 void kvm_cpu_synchronize_post_init(CPUState
*env
)
850 kvm_arch_put_registers(env
, KVM_PUT_FULL_STATE
);
851 env
->kvm_vcpu_dirty
= 0;
854 int kvm_cpu_exec(CPUState
*env
)
856 struct kvm_run
*run
= env
->kvm_run
;
859 DPRINTF("kvm_cpu_exec()\n");
862 #ifndef CONFIG_IOTHREAD
863 if (env
->exit_request
) {
864 DPRINTF("interrupt exit requested\n");
870 if (kvm_arch_process_irqchip_events(env
)) {
875 if (env
->kvm_vcpu_dirty
) {
876 kvm_arch_put_registers(env
, KVM_PUT_RUNTIME_STATE
);
877 env
->kvm_vcpu_dirty
= 0;
880 kvm_arch_pre_run(env
, run
);
881 cpu_single_env
= NULL
;
882 qemu_mutex_unlock_iothread();
883 ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
884 qemu_mutex_lock_iothread();
885 cpu_single_env
= env
;
886 kvm_arch_post_run(env
, run
);
888 if (ret
== -EINTR
|| ret
== -EAGAIN
) {
890 DPRINTF("io window exit\n");
896 DPRINTF("kvm run failed %s\n", strerror(-ret
));
900 kvm_flush_coalesced_mmio_buffer();
902 ret
= 0; /* exit loop */
903 switch (run
->exit_reason
) {
905 DPRINTF("handle_io\n");
906 ret
= kvm_handle_io(run
->io
.port
,
907 (uint8_t *)run
+ run
->io
.data_offset
,
913 DPRINTF("handle_mmio\n");
914 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
920 case KVM_EXIT_IRQ_WINDOW_OPEN
:
921 DPRINTF("irq_window_open\n");
923 case KVM_EXIT_SHUTDOWN
:
924 DPRINTF("shutdown\n");
925 qemu_system_reset_request();
928 case KVM_EXIT_UNKNOWN
:
929 DPRINTF("kvm_exit_unknown\n");
931 case KVM_EXIT_FAIL_ENTRY
:
932 DPRINTF("kvm_exit_fail_entry\n");
934 case KVM_EXIT_EXCEPTION
:
935 DPRINTF("kvm_exit_exception\n");
937 #ifdef KVM_CAP_INTERNAL_ERROR_DATA
938 case KVM_EXIT_INTERNAL_ERROR
:
939 kvm_handle_internal_error(env
, run
);
943 DPRINTF("kvm_exit_debug\n");
944 #ifdef KVM_CAP_SET_GUEST_DEBUG
945 if (kvm_arch_debug(&run
->debug
.arch
)) {
946 gdb_set_stop_cpu(env
);
948 env
->exception_index
= EXCP_DEBUG
;
951 /* re-enter, this exception was guest-internal */
953 #endif /* KVM_CAP_SET_GUEST_DEBUG */
956 DPRINTF("kvm_arch_handle_exit\n");
957 ret
= kvm_arch_handle_exit(env
, run
);
962 if (env
->exit_request
) {
963 env
->exit_request
= 0;
964 env
->exception_index
= EXCP_INTERRUPT
;
971 int kvm_ioctl(KVMState
*s
, int type
, ...)
978 arg
= va_arg(ap
, void *);
981 ret
= ioctl(s
->fd
, type
, arg
);
988 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
995 arg
= va_arg(ap
, void *);
998 ret
= ioctl(s
->vmfd
, type
, arg
);
1005 int kvm_vcpu_ioctl(CPUState
*env
, int type
, ...)
1012 arg
= va_arg(ap
, void *);
1015 ret
= ioctl(env
->kvm_fd
, type
, arg
);
1022 int kvm_has_sync_mmu(void)
1024 #ifdef KVM_CAP_SYNC_MMU
1025 KVMState
*s
= kvm_state
;
1027 return kvm_check_extension(s
, KVM_CAP_SYNC_MMU
);
1033 int kvm_has_vcpu_events(void)
1035 return kvm_state
->vcpu_events
;
1038 int kvm_has_robust_singlestep(void)
1040 return kvm_state
->robust_singlestep
;
1043 int kvm_has_debugregs(void)
1045 return kvm_state
->debugregs
;
1049 int kvm_has_xsave(void)
1051 return kvm_state
->xsave
;
1054 int kvm_has_xcrs(void)
1056 return kvm_state
->xcrs
;
1060 void kvm_setup_guest_memory(void *start
, size_t size
)
1062 if (!kvm_has_sync_mmu()) {
1063 #ifdef MADV_DONTFORK
1064 int ret
= madvise(start
, size
, MADV_DONTFORK
);
1072 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1078 #ifdef KVM_CAP_SET_GUEST_DEBUG
1079 #ifndef KVM_UPSTREAM
1080 #define run_on_cpu on_vcpu
1081 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
);
1082 #endif /* !KVM_UPSTREAM */
1084 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*env
,
1087 struct kvm_sw_breakpoint
*bp
;
1089 QTAILQ_FOREACH(bp
, &env
->kvm_state
->kvm_sw_breakpoints
, entry
) {
1096 int kvm_sw_breakpoints_active(CPUState
*env
)
1098 return !QTAILQ_EMPTY(&env
->kvm_state
->kvm_sw_breakpoints
);
1101 struct kvm_set_guest_debug_data
{
1102 struct kvm_guest_debug dbg
;
1107 static void kvm_invoke_set_guest_debug(void *data
)
1109 struct kvm_set_guest_debug_data
*dbg_data
= data
;
1110 CPUState
*env
= dbg_data
->env
;
1112 dbg_data
->err
= kvm_vcpu_ioctl(env
, KVM_SET_GUEST_DEBUG
, &dbg_data
->dbg
);
1115 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
1117 struct kvm_set_guest_debug_data data
;
1119 data
.dbg
.control
= reinject_trap
;
1121 if (env
->singlestep_enabled
) {
1122 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
1124 kvm_arch_update_guest_debug(env
, &data
.dbg
);
1127 run_on_cpu(env
, kvm_invoke_set_guest_debug
, &data
);
1131 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
1132 target_ulong len
, int type
)
1134 struct kvm_sw_breakpoint
*bp
;
1138 if (type
== GDB_BREAKPOINT_SW
) {
1139 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1145 bp
= qemu_malloc(sizeof(struct kvm_sw_breakpoint
));
1151 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
1157 QTAILQ_INSERT_HEAD(¤t_env
->kvm_state
->kvm_sw_breakpoints
,
1160 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
1165 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1166 err
= kvm_update_guest_debug(env
, 0);
1173 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
1174 target_ulong len
, int type
)
1176 struct kvm_sw_breakpoint
*bp
;
1180 if (type
== GDB_BREAKPOINT_SW
) {
1181 bp
= kvm_find_sw_breakpoint(current_env
, addr
);
1185 if (bp
->use_count
> 1) {
1190 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
1194 QTAILQ_REMOVE(¤t_env
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
1197 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
1202 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1203 err
= kvm_update_guest_debug(env
, 0);
1210 void kvm_remove_all_breakpoints(CPUState
*current_env
)
1212 struct kvm_sw_breakpoint
*bp
, *next
;
1213 KVMState
*s
= current_env
->kvm_state
;
1216 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
1217 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
1218 /* Try harder to find a CPU that currently sees the breakpoint. */
1219 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1220 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0)
1225 kvm_arch_remove_all_hw_breakpoints();
1227 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1228 kvm_update_guest_debug(env
, 0);
1231 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1233 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
1238 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
1239 target_ulong len
, int type
)
1244 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
1245 target_ulong len
, int type
)
1250 void kvm_remove_all_breakpoints(CPUState
*current_env
)
1253 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1255 int kvm_set_signal_mask(CPUState
*env
, const sigset_t
*sigset
)
1257 struct kvm_signal_mask
*sigmask
;
1261 return kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, NULL
);
1263 sigmask
= qemu_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1266 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1267 r
= kvm_vcpu_ioctl(env
, KVM_SET_SIGNAL_MASK
, sigmask
);
1273 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t addr
, uint16_t val
, bool assign
)
1275 #ifdef KVM_IOEVENTFD
1276 struct kvm_ioeventfd kick
= {
1280 .flags
= KVM_IOEVENTFD_FLAG_DATAMATCH
| KVM_IOEVENTFD_FLAG_PIO
,
1287 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1288 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1297 #if defined(KVM_IRQFD)
1298 int kvm_set_irqfd(int gsi
, int fd
, bool assigned
)
1300 struct kvm_irqfd irqfd
= {
1303 .flags
= assigned
? 0 : KVM_IRQFD_FLAG_DEASSIGN
,
1306 if (!kvm_enabled() || !kvm_irqchip_in_kernel())
1309 r
= kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &irqfd
);
1317 #include "qemu-kvm.c"