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"
28 /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
29 #define PAGE_SIZE TARGET_PAGE_SIZE
34 #define dprintf(fmt, ...) \
35 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
37 #define dprintf(fmt, ...) \
41 typedef struct KVMSlot
43 target_phys_addr_t start_addr;
44 ram_addr_t memory_size;
45 ram_addr_t phys_offset;
50 typedef struct kvm_dirty_log KVMDirtyLog;
60 int broken_set_mem_region;
61 #ifdef KVM_CAP_SET_GUEST_DEBUG
62 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
66 static KVMState *kvm_state;
68 static KVMSlot *kvm_alloc_slot(KVMState *s)
72 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
73 /* KVM private memory slots */
76 if (s->slots[i].memory_size == 0)
80 fprintf(stderr, "%s: no free slot available\n", __func__);
84 static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
85 target_phys_addr_t start_addr,
86 target_phys_addr_t end_addr)
90 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
91 KVMSlot *mem = &s->slots[i];
93 if (start_addr == mem->start_addr &&
94 end_addr == mem->start_addr + mem->memory_size) {
103 * Find overlapping slot with lowest start address
105 static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
106 target_phys_addr_t start_addr,
107 target_phys_addr_t end_addr)
109 KVMSlot *found = NULL;
112 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
113 KVMSlot *mem = &s->slots[i];
115 if (mem->memory_size == 0 ||
116 (found && found->start_addr < mem->start_addr)) {
120 if (end_addr > mem->start_addr &&
121 start_addr < mem->start_addr + mem->memory_size) {
129 static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
131 struct kvm_userspace_memory_region mem;
133 mem.slot = slot->slot;
134 mem.guest_phys_addr = slot->start_addr;
135 mem.memory_size = slot->memory_size;
136 mem.userspace_addr = (unsigned long)qemu_get_ram_ptr(slot->phys_offset);
137 mem.flags = slot->flags;
139 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
143 int kvm_init_vcpu(CPUState *env)
145 KVMState *s = kvm_state;
149 dprintf("kvm_init_vcpu\n");
151 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
153 dprintf("kvm_create_vcpu failed\n");
160 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
162 dprintf("KVM_GET_VCPU_MMAP_SIZE failed\n");
166 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
168 if (env->kvm_run == MAP_FAILED) {
170 dprintf("mmap'ing vcpu state failed\n");
174 ret = kvm_arch_init_vcpu(env);
180 int kvm_sync_vcpus(void)
184 for (env = first_cpu; env != NULL; env = env->next_cpu) {
187 ret = kvm_arch_put_registers(env);
196 * dirty pages logging control
198 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
199 ram_addr_t size, unsigned flags,
202 KVMState *s = kvm_state;
203 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
205 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
206 TARGET_FMT_plx "\n", __func__, phys_addr,
207 phys_addr + size - 1);
211 flags = (mem->flags & ~mask) | flags;
212 /* Nothing changed, no need to issue ioctl */
213 if (flags == mem->flags)
218 return kvm_set_user_memory_region(s, mem);
221 int kvm_log_start(target_phys_addr_t phys_addr, ram_addr_t size)
223 return kvm_dirty_pages_log_change(phys_addr, size,
224 KVM_MEM_LOG_DIRTY_PAGES,
225 KVM_MEM_LOG_DIRTY_PAGES);
228 int kvm_log_stop(target_phys_addr_t phys_addr, ram_addr_t size)
230 return kvm_dirty_pages_log_change(phys_addr, size,
232 KVM_MEM_LOG_DIRTY_PAGES);
236 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
237 * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty().
238 * This means all bits are set to dirty.
240 * @start_add: start of logged region.
241 * @end_addr: end of logged region.
243 void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
244 target_phys_addr_t end_addr)
246 KVMState *s = kvm_state;
248 KVMSlot *mem = kvm_lookup_matching_slot(s, start_addr, end_addr);
249 unsigned long alloc_size;
251 target_phys_addr_t phys_addr = start_addr;
253 dprintf("sync addr: " TARGET_FMT_lx " into %lx\n", start_addr,
256 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
257 TARGET_FMT_plx "\n", __func__, phys_addr, end_addr - 1);
261 alloc_size = mem->memory_size >> TARGET_PAGE_BITS / sizeof(d.dirty_bitmap);
262 d.dirty_bitmap = qemu_mallocz(alloc_size);
265 dprintf("slot %d, phys_addr %llx, uaddr: %llx\n",
266 d.slot, mem->start_addr, mem->phys_offset);
268 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
269 dprintf("ioctl failed %d\n", errno);
273 phys_addr = start_addr;
274 for (addr = mem->phys_offset; phys_addr < end_addr; phys_addr+= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
275 unsigned long *bitmap = (unsigned long *)d.dirty_bitmap;
276 unsigned nr = (phys_addr - start_addr) >> TARGET_PAGE_BITS;
277 unsigned word = nr / (sizeof(*bitmap) * 8);
278 unsigned bit = nr % (sizeof(*bitmap) * 8);
279 if ((bitmap[word] >> bit) & 1)
280 cpu_physical_memory_set_dirty(addr);
283 qemu_free(d.dirty_bitmap);
286 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
289 #ifdef KVM_CAP_COALESCED_MMIO
290 KVMState *s = kvm_state;
292 if (s->coalesced_mmio) {
293 struct kvm_coalesced_mmio_zone zone;
298 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
305 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
308 #ifdef KVM_CAP_COALESCED_MMIO
309 KVMState *s = kvm_state;
311 if (s->coalesced_mmio) {
312 struct kvm_coalesced_mmio_zone zone;
317 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
324 int kvm_check_extension(KVMState *s, unsigned int extension)
328 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
336 int kvm_init(int smp_cpus)
343 fprintf(stderr, "No SMP KVM support, use '-smp 1'\n");
347 s = qemu_mallocz(sizeof(KVMState));
349 #ifdef KVM_CAP_SET_GUEST_DEBUG
350 TAILQ_INIT(&s->kvm_sw_breakpoints);
352 for (i = 0; i < ARRAY_SIZE(s->slots); i++)
353 s->slots[i].slot = i;
356 s->fd = open("/dev/kvm", O_RDWR);
358 fprintf(stderr, "Could not access KVM kernel module: %m\n");
363 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
364 if (ret < KVM_API_VERSION) {
367 fprintf(stderr, "kvm version too old\n");
371 if (ret > KVM_API_VERSION) {
373 fprintf(stderr, "kvm version not supported\n");
377 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
381 /* initially, KVM allocated its own memory and we had to jump through
382 * hooks to make phys_ram_base point to this. Modern versions of KVM
383 * just use a user allocated buffer so we can use regular pages
384 * unmodified. Make sure we have a sufficiently modern version of KVM.
386 if (!kvm_check_extension(s, KVM_CAP_USER_MEMORY)) {
388 fprintf(stderr, "kvm does not support KVM_CAP_USER_MEMORY\n");
392 /* There was a nasty bug in < kvm-80 that prevents memory slots from being
393 * destroyed properly. Since we rely on this capability, refuse to work
394 * with any kernel without this capability. */
395 if (!kvm_check_extension(s, KVM_CAP_DESTROY_MEMORY_REGION_WORKS)) {
399 "KVM kernel module broken (DESTROY_MEMORY_REGION)\n"
400 "Please upgrade to at least kvm-81.\n");
404 #ifdef KVM_CAP_COALESCED_MMIO
405 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
407 s->coalesced_mmio = 0;
410 s->broken_set_mem_region = 1;
411 #ifdef KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
412 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
414 s->broken_set_mem_region = 0;
418 ret = kvm_arch_init(s, smp_cpus);
438 static int kvm_handle_io(CPUState *env, uint16_t port, void *data,
439 int direction, int size, uint32_t count)
444 for (i = 0; i < count; i++) {
445 if (direction == KVM_EXIT_IO_IN) {
448 stb_p(ptr, cpu_inb(env, port));
451 stw_p(ptr, cpu_inw(env, port));
454 stl_p(ptr, cpu_inl(env, port));
460 cpu_outb(env, port, ldub_p(ptr));
463 cpu_outw(env, port, lduw_p(ptr));
466 cpu_outl(env, port, ldl_p(ptr));
477 static void kvm_run_coalesced_mmio(CPUState *env, struct kvm_run *run)
479 #ifdef KVM_CAP_COALESCED_MMIO
480 KVMState *s = kvm_state;
481 if (s->coalesced_mmio) {
482 struct kvm_coalesced_mmio_ring *ring;
484 ring = (void *)run + (s->coalesced_mmio * TARGET_PAGE_SIZE);
485 while (ring->first != ring->last) {
486 struct kvm_coalesced_mmio *ent;
488 ent = &ring->coalesced_mmio[ring->first];
490 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
491 /* FIXME smp_wmb() */
492 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
498 int kvm_cpu_exec(CPUState *env)
500 struct kvm_run *run = env->kvm_run;
503 dprintf("kvm_cpu_exec()\n");
506 kvm_arch_pre_run(env, run);
508 if (env->exit_request) {
509 dprintf("interrupt exit requested\n");
514 ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
515 kvm_arch_post_run(env, run);
517 if (ret == -EINTR || ret == -EAGAIN) {
518 dprintf("io window exit\n");
524 dprintf("kvm run failed %s\n", strerror(-ret));
528 kvm_run_coalesced_mmio(env, run);
530 ret = 0; /* exit loop */
531 switch (run->exit_reason) {
533 dprintf("handle_io\n");
534 ret = kvm_handle_io(env, run->io.port,
535 (uint8_t *)run + run->io.data_offset,
541 dprintf("handle_mmio\n");
542 cpu_physical_memory_rw(run->mmio.phys_addr,
548 case KVM_EXIT_IRQ_WINDOW_OPEN:
549 dprintf("irq_window_open\n");
551 case KVM_EXIT_SHUTDOWN:
552 dprintf("shutdown\n");
553 qemu_system_reset_request();
556 case KVM_EXIT_UNKNOWN:
557 dprintf("kvm_exit_unknown\n");
559 case KVM_EXIT_FAIL_ENTRY:
560 dprintf("kvm_exit_fail_entry\n");
562 case KVM_EXIT_EXCEPTION:
563 dprintf("kvm_exit_exception\n");
566 dprintf("kvm_exit_debug\n");
567 #ifdef KVM_CAP_SET_GUEST_DEBUG
568 if (kvm_arch_debug(&run->debug.arch)) {
569 gdb_set_stop_cpu(env);
571 env->exception_index = EXCP_DEBUG;
574 /* re-enter, this exception was guest-internal */
576 #endif /* KVM_CAP_SET_GUEST_DEBUG */
579 dprintf("kvm_arch_handle_exit\n");
580 ret = kvm_arch_handle_exit(env, run);
585 if (env->exit_request) {
586 env->exit_request = 0;
587 env->exception_index = EXCP_INTERRUPT;
593 void kvm_set_phys_mem(target_phys_addr_t start_addr,
595 ram_addr_t phys_offset)
597 KVMState *s = kvm_state;
598 ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
602 if (start_addr & ~TARGET_PAGE_MASK) {
603 if (flags >= IO_MEM_UNASSIGNED) {
604 if (!kvm_lookup_overlapping_slot(s, start_addr,
605 start_addr + size)) {
608 fprintf(stderr, "Unaligned split of a KVM memory slot\n");
610 fprintf(stderr, "Only page-aligned memory slots supported\n");
615 /* KVM does not support read-only slots */
616 phys_offset &= ~IO_MEM_ROM;
619 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
624 if (flags < IO_MEM_UNASSIGNED && start_addr >= mem->start_addr &&
625 (start_addr + size <= mem->start_addr + mem->memory_size) &&
626 (phys_offset - start_addr == mem->phys_offset - mem->start_addr)) {
627 /* The new slot fits into the existing one and comes with
628 * identical parameters - nothing to be done. */
634 /* unregister the overlapping slot */
635 mem->memory_size = 0;
636 err = kvm_set_user_memory_region(s, mem);
638 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
639 __func__, strerror(-err));
643 /* Workaround for older KVM versions: we can't join slots, even not by
644 * unregistering the previous ones and then registering the larger
645 * slot. We have to maintain the existing fragmentation. Sigh.
647 * This workaround assumes that the new slot starts at the same
648 * address as the first existing one. If not or if some overlapping
649 * slot comes around later, we will fail (not seen in practice so far)
650 * - and actually require a recent KVM version. */
651 if (s->broken_set_mem_region &&
652 old.start_addr == start_addr && old.memory_size < size &&
653 flags < IO_MEM_UNASSIGNED) {
654 mem = kvm_alloc_slot(s);
655 mem->memory_size = old.memory_size;
656 mem->start_addr = old.start_addr;
657 mem->phys_offset = old.phys_offset;
660 err = kvm_set_user_memory_region(s, mem);
662 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
667 start_addr += old.memory_size;
668 phys_offset += old.memory_size;
669 size -= old.memory_size;
673 /* register prefix slot */
674 if (old.start_addr < start_addr) {
675 mem = kvm_alloc_slot(s);
676 mem->memory_size = start_addr - old.start_addr;
677 mem->start_addr = old.start_addr;
678 mem->phys_offset = old.phys_offset;
681 err = kvm_set_user_memory_region(s, mem);
683 fprintf(stderr, "%s: error registering prefix slot: %s\n",
684 __func__, strerror(-err));
689 /* register suffix slot */
690 if (old.start_addr + old.memory_size > start_addr + size) {
691 ram_addr_t size_delta;
693 mem = kvm_alloc_slot(s);
694 mem->start_addr = start_addr + size;
695 size_delta = mem->start_addr - old.start_addr;
696 mem->memory_size = old.memory_size - size_delta;
697 mem->phys_offset = old.phys_offset + size_delta;
700 err = kvm_set_user_memory_region(s, mem);
702 fprintf(stderr, "%s: error registering suffix slot: %s\n",
703 __func__, strerror(-err));
709 /* in case the KVM bug workaround already "consumed" the new slot */
713 /* KVM does not need to know about this memory */
714 if (flags >= IO_MEM_UNASSIGNED)
717 mem = kvm_alloc_slot(s);
718 mem->memory_size = size;
719 mem->start_addr = start_addr;
720 mem->phys_offset = phys_offset;
723 err = kvm_set_user_memory_region(s, mem);
725 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
731 int kvm_ioctl(KVMState *s, int type, ...)
738 arg = va_arg(ap, void *);
741 ret = ioctl(s->fd, type, arg);
748 int kvm_vm_ioctl(KVMState *s, int type, ...)
755 arg = va_arg(ap, void *);
758 ret = ioctl(s->vmfd, type, arg);
765 int kvm_vcpu_ioctl(CPUState *env, int type, ...)
772 arg = va_arg(ap, void *);
775 ret = ioctl(env->kvm_fd, type, arg);
782 int kvm_has_sync_mmu(void)
784 #ifdef KVM_CAP_SYNC_MMU
785 KVMState *s = kvm_state;
787 return kvm_check_extension(s, KVM_CAP_SYNC_MMU);
793 void kvm_setup_guest_memory(void *start, size_t size)
795 if (!kvm_has_sync_mmu()) {
797 int ret = madvise(start, size, MADV_DONTFORK);
805 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
811 #ifdef KVM_CAP_SET_GUEST_DEBUG
812 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
815 struct kvm_sw_breakpoint *bp;
817 TAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
824 int kvm_sw_breakpoints_active(CPUState *env)
826 return !TAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
829 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
831 struct kvm_guest_debug dbg;
834 if (env->singlestep_enabled)
835 dbg.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
837 kvm_arch_update_guest_debug(env, &dbg);
838 dbg.control |= reinject_trap;
840 return kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg);
843 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
844 target_ulong len, int type)
846 struct kvm_sw_breakpoint *bp;
850 if (type == GDB_BREAKPOINT_SW) {
851 bp = kvm_find_sw_breakpoint(current_env, addr);
857 bp = qemu_malloc(sizeof(struct kvm_sw_breakpoint));
863 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
869 TAILQ_INSERT_HEAD(¤t_env->kvm_state->kvm_sw_breakpoints,
872 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
877 for (env = first_cpu; env != NULL; env = env->next_cpu) {
878 err = kvm_update_guest_debug(env, 0);
885 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
886 target_ulong len, int type)
888 struct kvm_sw_breakpoint *bp;
892 if (type == GDB_BREAKPOINT_SW) {
893 bp = kvm_find_sw_breakpoint(current_env, addr);
897 if (bp->use_count > 1) {
902 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
906 TAILQ_REMOVE(¤t_env->kvm_state->kvm_sw_breakpoints, bp, entry);
909 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
914 for (env = first_cpu; env != NULL; env = env->next_cpu) {
915 err = kvm_update_guest_debug(env, 0);
922 void kvm_remove_all_breakpoints(CPUState *current_env)
924 struct kvm_sw_breakpoint *bp, *next;
925 KVMState *s = current_env->kvm_state;
928 TAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
929 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
930 /* Try harder to find a CPU that currently sees the breakpoint. */
931 for (env = first_cpu; env != NULL; env = env->next_cpu) {
932 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0)
937 kvm_arch_remove_all_hw_breakpoints();
939 for (env = first_cpu; env != NULL; env = env->next_cpu)
940 kvm_update_guest_debug(env, 0);
943 #else /* !KVM_CAP_SET_GUEST_DEBUG */
945 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
950 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
951 target_ulong len, int type)
956 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
957 target_ulong len, int type)
962 void kvm_remove_all_breakpoints(CPUState *current_env)
965 #endif /* !KVM_CAP_SET_GUEST_DEBUG */