use KVM_UPSTREAM for ppc.
[qemu-kvm/fedora.git] / kvm-all.c
blob541b200e3814095e1b9ee9fbe1ea9ad470a33ceb
1 /*
2 * QEMU KVM support
4 * Copyright IBM, Corp. 2008
5 * Red Hat, Inc. 2008
7 * Authors:
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>
18 #include <sys/mman.h>
19 #include <stdarg.h>
21 #include <linux/kvm.h>
23 #include "qemu-common.h"
24 #include "sysemu.h"
25 #include "kvm.h"
27 /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
28 #define PAGE_SIZE TARGET_PAGE_SIZE
30 //#define DEBUG_KVM
32 #ifdef DEBUG_KVM
33 #define dprintf(fmt, ...) \
34 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
35 #else
36 #define dprintf(fmt, ...) \
37 do { } while (0)
38 #endif
40 typedef struct KVMSlot
42 target_phys_addr_t start_addr;
43 ram_addr_t memory_size;
44 ram_addr_t phys_offset;
45 int slot;
46 int flags;
47 } KVMSlot;
49 typedef struct kvm_dirty_log KVMDirtyLog;
51 int kvm_allowed = 0;
53 struct KVMState
55 KVMSlot slots[32];
56 int fd;
57 int vmfd;
58 int coalesced_mmio;
61 static KVMState *kvm_state;
63 static KVMSlot *kvm_alloc_slot(KVMState *s)
65 int i;
67 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
68 /* KVM private memory slots */
69 if (i >= 8 && i < 12)
70 continue;
71 if (s->slots[i].memory_size == 0)
72 return &s->slots[i];
75 return NULL;
78 static KVMSlot *kvm_lookup_slot(KVMState *s, target_phys_addr_t start_addr)
80 int i;
82 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
83 KVMSlot *mem = &s->slots[i];
85 if (start_addr >= mem->start_addr &&
86 start_addr < (mem->start_addr + mem->memory_size))
87 return mem;
90 return NULL;
93 static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
95 struct kvm_userspace_memory_region mem;
97 mem.slot = slot->slot;
98 mem.guest_phys_addr = slot->start_addr;
99 mem.memory_size = slot->memory_size;
100 mem.userspace_addr = (unsigned long)phys_ram_base + slot->phys_offset;
101 mem.flags = slot->flags;
103 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
107 int kvm_init_vcpu(CPUState *env)
109 KVMState *s = kvm_state;
110 long mmap_size;
111 int ret;
113 dprintf("kvm_init_vcpu\n");
115 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
116 if (ret < 0) {
117 dprintf("kvm_create_vcpu failed\n");
118 goto err;
121 env->kvm_fd = ret;
122 env->kvm_state = s;
124 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
125 if (mmap_size < 0) {
126 dprintf("KVM_GET_VCPU_MMAP_SIZE failed\n");
127 goto err;
130 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
131 env->kvm_fd, 0);
132 if (env->kvm_run == MAP_FAILED) {
133 ret = -errno;
134 dprintf("mmap'ing vcpu state failed\n");
135 goto err;
138 ret = kvm_arch_init_vcpu(env);
140 err:
141 return ret;
144 int kvm_sync_vcpus(void)
146 CPUState *env;
148 for (env = first_cpu; env != NULL; env = env->next_cpu) {
149 int ret;
151 ret = kvm_arch_put_registers(env);
152 if (ret)
153 return ret;
156 return 0;
160 * dirty pages logging control
162 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr, target_phys_addr_t end_addr,
163 unsigned flags,
164 unsigned mask)
166 KVMState *s = kvm_state;
167 KVMSlot *mem = kvm_lookup_slot(s, phys_addr);
168 if (mem == NULL) {
169 dprintf("invalid parameters %llx-%llx\n", phys_addr, end_addr);
170 return -EINVAL;
173 flags = (mem->flags & ~mask) | flags;
174 /* Nothing changed, no need to issue ioctl */
175 if (flags == mem->flags)
176 return 0;
178 mem->flags = flags;
180 return kvm_set_user_memory_region(s, mem);
183 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t end_addr)
185 return kvm_dirty_pages_log_change(phys_addr, end_addr,
186 KVM_MEM_LOG_DIRTY_PAGES,
187 KVM_MEM_LOG_DIRTY_PAGES);
190 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t end_addr)
192 return kvm_dirty_pages_log_change(phys_addr, end_addr,
194 KVM_MEM_LOG_DIRTY_PAGES);
198 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
199 * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty().
200 * This means all bits are set to dirty.
202 * @start_add: start of logged region. This is what we use to search the memslot
203 * @end_addr: end of logged region.
205 void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
207 KVMState *s = kvm_state;
208 KVMDirtyLog d;
209 KVMSlot *mem = kvm_lookup_slot(s, start_addr);
210 unsigned long alloc_size;
211 ram_addr_t addr;
212 target_phys_addr_t phys_addr = start_addr;
214 dprintf("sync addr: %llx into %lx\n", start_addr, mem->phys_offset);
215 if (mem == NULL) {
216 fprintf(stderr, "BUG: %s: invalid parameters\n", __func__);
217 return;
220 alloc_size = mem->memory_size >> TARGET_PAGE_BITS / sizeof(d.dirty_bitmap);
221 d.dirty_bitmap = qemu_mallocz(alloc_size);
223 d.slot = mem->slot;
224 dprintf("slot %d, phys_addr %llx, uaddr: %llx\n",
225 d.slot, mem->start_addr, mem->phys_offset);
227 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
228 dprintf("ioctl failed %d\n", errno);
229 goto out;
232 phys_addr = start_addr;
233 for (addr = mem->phys_offset; phys_addr < end_addr; phys_addr+= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
234 unsigned long *bitmap = (unsigned long *)d.dirty_bitmap;
235 unsigned nr = (phys_addr - start_addr) >> TARGET_PAGE_BITS;
236 unsigned word = nr / (sizeof(*bitmap) * 8);
237 unsigned bit = nr % (sizeof(*bitmap) * 8);
238 if ((bitmap[word] >> bit) & 1)
239 cpu_physical_memory_set_dirty(addr);
241 out:
242 qemu_free(d.dirty_bitmap);
245 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
247 int ret = -ENOSYS;
248 #ifdef KVM_CAP_COALESCED_MMIO
249 KVMState *s = kvm_state;
251 if (s->coalesced_mmio) {
252 struct kvm_coalesced_mmio_zone zone;
254 zone.addr = start;
255 zone.size = size;
257 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
259 #endif
261 return ret;
264 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
266 int ret = -ENOSYS;
267 #ifdef KVM_CAP_COALESCED_MMIO
268 KVMState *s = kvm_state;
270 if (s->coalesced_mmio) {
271 struct kvm_coalesced_mmio_zone zone;
273 zone.addr = start;
274 zone.size = size;
276 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
278 #endif
280 return ret;
283 int kvm_check_extension(KVMState *s, unsigned int extension)
285 int ret;
287 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
288 if (ret < 0) {
289 ret = 0;
292 return ret;
295 int kvm_init(int smp_cpus)
297 KVMState *s;
298 int ret;
299 int i;
301 if (smp_cpus > 1) {
302 fprintf(stderr, "No SMP KVM support, use '-smp 1'\n");
303 return -EINVAL;
306 s = qemu_mallocz(sizeof(KVMState));
308 for (i = 0; i < ARRAY_SIZE(s->slots); i++)
309 s->slots[i].slot = i;
311 s->vmfd = -1;
312 s->fd = open("/dev/kvm", O_RDWR);
313 if (s->fd == -1) {
314 fprintf(stderr, "Could not access KVM kernel module: %m\n");
315 ret = -errno;
316 goto err;
319 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
320 if (ret < KVM_API_VERSION) {
321 if (ret > 0)
322 ret = -EINVAL;
323 fprintf(stderr, "kvm version too old\n");
324 goto err;
327 if (ret > KVM_API_VERSION) {
328 ret = -EINVAL;
329 fprintf(stderr, "kvm version not supported\n");
330 goto err;
333 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
334 if (s->vmfd < 0)
335 goto err;
337 /* initially, KVM allocated its own memory and we had to jump through
338 * hooks to make phys_ram_base point to this. Modern versions of KVM
339 * just use a user allocated buffer so we can use phys_ram_base
340 * unmodified. Make sure we have a sufficiently modern version of KVM.
342 if (!kvm_check_extension(s, KVM_CAP_USER_MEMORY)) {
343 ret = -EINVAL;
344 fprintf(stderr, "kvm does not support KVM_CAP_USER_MEMORY\n");
345 goto err;
348 /* There was a nasty bug in < kvm-80 that prevents memory slots from being
349 * destroyed properly. Since we rely on this capability, refuse to work
350 * with any kernel without this capability. */
351 if (!kvm_check_extension(s, KVM_CAP_DESTROY_MEMORY_REGION_WORKS)) {
352 ret = -EINVAL;
354 fprintf(stderr,
355 "KVM kernel module broken (DESTROY_MEMORY_REGION)\n"
356 "Please upgrade to at least kvm-81.\n");
357 goto err;
360 #ifdef KVM_CAP_COALESCED_MMIO
361 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
362 #else
363 s->coalesced_mmio = 0;
364 #endif
366 ret = kvm_arch_init(s, smp_cpus);
367 if (ret < 0)
368 goto err;
370 kvm_state = s;
372 return 0;
374 err:
375 if (s) {
376 if (s->vmfd != -1)
377 close(s->vmfd);
378 if (s->fd != -1)
379 close(s->fd);
381 qemu_free(s);
383 return ret;
386 static int kvm_handle_io(CPUState *env, uint16_t port, void *data,
387 int direction, int size, uint32_t count)
389 int i;
390 uint8_t *ptr = data;
392 for (i = 0; i < count; i++) {
393 if (direction == KVM_EXIT_IO_IN) {
394 switch (size) {
395 case 1:
396 stb_p(ptr, cpu_inb(env, port));
397 break;
398 case 2:
399 stw_p(ptr, cpu_inw(env, port));
400 break;
401 case 4:
402 stl_p(ptr, cpu_inl(env, port));
403 break;
405 } else {
406 switch (size) {
407 case 1:
408 cpu_outb(env, port, ldub_p(ptr));
409 break;
410 case 2:
411 cpu_outw(env, port, lduw_p(ptr));
412 break;
413 case 4:
414 cpu_outl(env, port, ldl_p(ptr));
415 break;
419 ptr += size;
422 return 1;
425 static void kvm_run_coalesced_mmio(CPUState *env, struct kvm_run *run)
427 #ifdef KVM_CAP_COALESCED_MMIO
428 KVMState *s = kvm_state;
429 if (s->coalesced_mmio) {
430 struct kvm_coalesced_mmio_ring *ring;
432 ring = (void *)run + (s->coalesced_mmio * TARGET_PAGE_SIZE);
433 while (ring->first != ring->last) {
434 struct kvm_coalesced_mmio *ent;
436 ent = &ring->coalesced_mmio[ring->first];
438 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
439 /* FIXME smp_wmb() */
440 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
443 #endif
446 int kvm_cpu_exec(CPUState *env)
448 struct kvm_run *run = env->kvm_run;
449 int ret;
451 dprintf("kvm_cpu_exec()\n");
453 do {
454 kvm_arch_pre_run(env, run);
456 if (env->exit_request) {
457 dprintf("interrupt exit requested\n");
458 ret = 0;
459 break;
462 ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
463 kvm_arch_post_run(env, run);
465 if (ret == -EINTR || ret == -EAGAIN) {
466 dprintf("io window exit\n");
467 ret = 0;
468 break;
471 if (ret < 0) {
472 dprintf("kvm run failed %s\n", strerror(-ret));
473 abort();
476 kvm_run_coalesced_mmio(env, run);
478 ret = 0; /* exit loop */
479 switch (run->exit_reason) {
480 case KVM_EXIT_IO:
481 dprintf("handle_io\n");
482 ret = kvm_handle_io(env, run->io.port,
483 (uint8_t *)run + run->io.data_offset,
484 run->io.direction,
485 run->io.size,
486 run->io.count);
487 break;
488 case KVM_EXIT_MMIO:
489 dprintf("handle_mmio\n");
490 cpu_physical_memory_rw(run->mmio.phys_addr,
491 run->mmio.data,
492 run->mmio.len,
493 run->mmio.is_write);
494 ret = 1;
495 break;
496 case KVM_EXIT_IRQ_WINDOW_OPEN:
497 dprintf("irq_window_open\n");
498 break;
499 case KVM_EXIT_SHUTDOWN:
500 dprintf("shutdown\n");
501 qemu_system_reset_request();
502 ret = 1;
503 break;
504 case KVM_EXIT_UNKNOWN:
505 dprintf("kvm_exit_unknown\n");
506 break;
507 case KVM_EXIT_FAIL_ENTRY:
508 dprintf("kvm_exit_fail_entry\n");
509 break;
510 case KVM_EXIT_EXCEPTION:
511 dprintf("kvm_exit_exception\n");
512 break;
513 case KVM_EXIT_DEBUG:
514 dprintf("kvm_exit_debug\n");
515 break;
516 default:
517 dprintf("kvm_arch_handle_exit\n");
518 ret = kvm_arch_handle_exit(env, run);
519 break;
521 } while (ret > 0);
523 if (env->exit_request) {
524 env->exit_request = 0;
525 env->exception_index = EXCP_INTERRUPT;
528 return ret;
531 void kvm_set_phys_mem(target_phys_addr_t start_addr,
532 ram_addr_t size,
533 ram_addr_t phys_offset)
535 KVMState *s = kvm_state;
536 ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
537 KVMSlot *mem;
539 /* KVM does not support read-only slots */
540 phys_offset &= ~IO_MEM_ROM;
542 mem = kvm_lookup_slot(s, start_addr);
543 if (mem) {
544 if ((flags == IO_MEM_UNASSIGNED) || (flags >= TLB_MMIO)) {
545 mem->memory_size = 0;
546 mem->start_addr = start_addr;
547 mem->phys_offset = 0;
548 mem->flags = 0;
550 kvm_set_user_memory_region(s, mem);
551 } else if (start_addr >= mem->start_addr &&
552 (start_addr + size) <= (mem->start_addr +
553 mem->memory_size)) {
554 KVMSlot slot;
555 target_phys_addr_t mem_start;
556 ram_addr_t mem_size, mem_offset;
558 /* Not splitting */
559 if ((phys_offset - (start_addr - mem->start_addr)) ==
560 mem->phys_offset)
561 return;
563 /* unregister whole slot */
564 memcpy(&slot, mem, sizeof(slot));
565 mem->memory_size = 0;
566 kvm_set_user_memory_region(s, mem);
568 /* register prefix slot */
569 mem_start = slot.start_addr;
570 mem_size = start_addr - slot.start_addr;
571 mem_offset = slot.phys_offset;
572 if (mem_size)
573 kvm_set_phys_mem(mem_start, mem_size, mem_offset);
575 /* register new slot */
576 kvm_set_phys_mem(start_addr, size, phys_offset);
578 /* register suffix slot */
579 mem_start = start_addr + size;
580 mem_offset += mem_size + size;
581 mem_size = slot.memory_size - mem_size - size;
582 if (mem_size)
583 kvm_set_phys_mem(mem_start, mem_size, mem_offset);
585 return;
586 } else {
587 printf("Registering overlapping slot\n");
588 abort();
591 /* KVM does not need to know about this memory */
592 if (flags >= IO_MEM_UNASSIGNED)
593 return;
595 mem = kvm_alloc_slot(s);
596 mem->memory_size = size;
597 mem->start_addr = start_addr;
598 mem->phys_offset = phys_offset;
599 mem->flags = 0;
601 kvm_set_user_memory_region(s, mem);
602 /* FIXME deal with errors */
605 int kvm_ioctl(KVMState *s, int type, ...)
607 int ret;
608 void *arg;
609 va_list ap;
611 va_start(ap, type);
612 arg = va_arg(ap, void *);
613 va_end(ap);
615 ret = ioctl(s->fd, type, arg);
616 if (ret == -1)
617 ret = -errno;
619 return ret;
622 int kvm_vm_ioctl(KVMState *s, int type, ...)
624 int ret;
625 void *arg;
626 va_list ap;
628 va_start(ap, type);
629 arg = va_arg(ap, void *);
630 va_end(ap);
632 ret = ioctl(s->vmfd, type, arg);
633 if (ret == -1)
634 ret = -errno;
636 return ret;
639 int kvm_vcpu_ioctl(CPUState *env, int type, ...)
641 int ret;
642 void *arg;
643 va_list ap;
645 va_start(ap, type);
646 arg = va_arg(ap, void *);
647 va_end(ap);
649 ret = ioctl(env->kvm_fd, type, arg);
650 if (ret == -1)
651 ret = -errno;
653 return ret;
656 int kvm_has_sync_mmu(void)
658 #ifdef KVM_CAP_SYNC_MMU
659 KVMState *s = kvm_state;
661 return kvm_check_extension(s, KVM_CAP_SYNC_MMU);
662 #else
663 return 0;
664 #endif
667 void kvm_setup_guest_memory(void *start, size_t size)
669 if (!kvm_has_sync_mmu()) {
670 #ifdef MADV_DONTFORK
671 int ret = madvise(start, size, MADV_DONTFORK);
673 if (ret) {
674 perror("madvice");
675 exit(1);
677 #else
678 fprintf(stderr,
679 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
680 exit(1);
681 #endif