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"
27 /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
28 #define PAGE_SIZE TARGET_PAGE_SIZE
33 #define dprintf(fmt, ...) \
34 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
36 #define dprintf(fmt, ...) \
40 typedef struct KVMSlot
42 target_phys_addr_t start_addr
;
43 ram_addr_t memory_size
;
44 ram_addr_t phys_offset
;
49 typedef struct kvm_dirty_log KVMDirtyLog
;
61 static KVMState
*kvm_state
;
63 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
67 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
68 /* KVM private memory slots */
71 if (s
->slots
[i
].memory_size
== 0)
78 static KVMSlot
*kvm_lookup_slot(KVMState
*s
, target_phys_addr_t start_addr
)
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
))
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
;
113 dprintf("kvm_init_vcpu\n");
115 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
117 dprintf("kvm_create_vcpu failed\n");
124 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
126 dprintf("KVM_GET_VCPU_MMAP_SIZE failed\n");
130 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
132 if (env
->kvm_run
== MAP_FAILED
) {
134 dprintf("mmap'ing vcpu state failed\n");
138 ret
= kvm_arch_init_vcpu(env
);
144 int kvm_sync_vcpus(void)
148 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
151 ret
= kvm_arch_put_registers(env
);
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
,
166 KVMState
*s
= kvm_state
;
167 KVMSlot
*mem
= kvm_lookup_slot(s
, phys_addr
);
169 dprintf("invalid parameters %llx-%llx\n", phys_addr
, end_addr
);
173 flags
= (mem
->flags
& ~mask
) | flags
;
174 /* Nothing changed, no need to issue ioctl */
175 if (flags
== mem
->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
;
209 KVMSlot
*mem
= kvm_lookup_slot(s
, start_addr
);
210 unsigned long alloc_size
;
212 target_phys_addr_t phys_addr
= start_addr
;
214 dprintf("sync addr: %llx into %lx\n", start_addr
, mem
->phys_offset
);
216 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
220 alloc_size
= mem
->memory_size
>> TARGET_PAGE_BITS
/ sizeof(d
.dirty_bitmap
);
221 d
.dirty_bitmap
= qemu_mallocz(alloc_size
);
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
);
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
);
242 qemu_free(d
.dirty_bitmap
);
245 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
248 #ifdef KVM_CAP_COALESCED_MMIO
249 KVMState
*s
= kvm_state
;
251 if (s
->coalesced_mmio
) {
252 struct kvm_coalesced_mmio_zone zone
;
257 ret
= kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
264 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
267 #ifdef KVM_CAP_COALESCED_MMIO
268 KVMState
*s
= kvm_state
;
270 if (s
->coalesced_mmio
) {
271 struct kvm_coalesced_mmio_zone zone
;
276 ret
= kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
283 int kvm_init(int smp_cpus
)
292 s
= qemu_mallocz(sizeof(KVMState
));
294 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++)
295 s
->slots
[i
].slot
= i
;
298 s
->fd
= open("/dev/kvm", O_RDWR
);
300 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
305 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
306 if (ret
< KVM_API_VERSION
) {
309 fprintf(stderr
, "kvm version too old\n");
313 if (ret
> KVM_API_VERSION
) {
315 fprintf(stderr
, "kvm version not supported\n");
319 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
323 /* initially, KVM allocated its own memory and we had to jump through
324 * hooks to make phys_ram_base point to this. Modern versions of KVM
325 * just use a user allocated buffer so we can use phys_ram_base
326 * unmodified. Make sure we have a sufficiently modern version of KVM.
328 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
332 fprintf(stderr
, "kvm does not support KVM_CAP_USER_MEMORY\n");
336 /* There was a nasty bug in < kvm-80 that prevents memory slots from being
337 * destroyed properly. Since we rely on this capability, refuse to work
338 * with any kernel without this capability. */
339 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
,
340 KVM_CAP_DESTROY_MEMORY_REGION_WORKS
);
346 "KVM kernel module broken (DESTROY_MEMORY_REGION)\n"
347 "Please upgrade to at least kvm-81.\n");
351 s
->coalesced_mmio
= 0;
352 #ifdef KVM_CAP_COALESCED_MMIO
353 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
355 s
->coalesced_mmio
= ret
;
358 ret
= kvm_arch_init(s
, smp_cpus
);
378 static int kvm_handle_io(CPUState
*env
, uint16_t port
, void *data
,
379 int direction
, int size
, uint32_t count
)
384 for (i
= 0; i
< count
; i
++) {
385 if (direction
== KVM_EXIT_IO_IN
) {
388 stb_p(ptr
, cpu_inb(env
, port
));
391 stw_p(ptr
, cpu_inw(env
, port
));
394 stl_p(ptr
, cpu_inl(env
, port
));
400 cpu_outb(env
, port
, ldub_p(ptr
));
403 cpu_outw(env
, port
, lduw_p(ptr
));
406 cpu_outl(env
, port
, ldl_p(ptr
));
417 static void kvm_run_coalesced_mmio(CPUState
*env
, struct kvm_run
*run
)
419 #ifdef KVM_CAP_COALESCED_MMIO
420 KVMState
*s
= kvm_state
;
421 if (s
->coalesced_mmio
) {
422 struct kvm_coalesced_mmio_ring
*ring
;
424 ring
= (void *)run
+ (s
->coalesced_mmio
* TARGET_PAGE_SIZE
);
425 while (ring
->first
!= ring
->last
) {
426 struct kvm_coalesced_mmio
*ent
;
428 ent
= &ring
->coalesced_mmio
[ring
->first
];
430 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
431 /* FIXME smp_wmb() */
432 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
438 int kvm_cpu_exec(CPUState
*env
)
440 struct kvm_run
*run
= env
->kvm_run
;
443 dprintf("kvm_cpu_exec()\n");
446 kvm_arch_pre_run(env
, run
);
448 if (env
->exit_request
) {
449 dprintf("interrupt exit requested\n");
454 ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
455 kvm_arch_post_run(env
, run
);
457 if (ret
== -EINTR
|| ret
== -EAGAIN
) {
458 dprintf("io window exit\n");
464 dprintf("kvm run failed %s\n", strerror(-ret
));
468 kvm_run_coalesced_mmio(env
, run
);
470 ret
= 0; /* exit loop */
471 switch (run
->exit_reason
) {
473 dprintf("handle_io\n");
474 ret
= kvm_handle_io(env
, run
->io
.port
,
475 (uint8_t *)run
+ run
->io
.data_offset
,
481 dprintf("handle_mmio\n");
482 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
488 case KVM_EXIT_IRQ_WINDOW_OPEN
:
489 dprintf("irq_window_open\n");
491 case KVM_EXIT_SHUTDOWN
:
492 dprintf("shutdown\n");
493 qemu_system_reset_request();
496 case KVM_EXIT_UNKNOWN
:
497 dprintf("kvm_exit_unknown\n");
499 case KVM_EXIT_FAIL_ENTRY
:
500 dprintf("kvm_exit_fail_entry\n");
502 case KVM_EXIT_EXCEPTION
:
503 dprintf("kvm_exit_exception\n");
506 dprintf("kvm_exit_debug\n");
509 dprintf("kvm_arch_handle_exit\n");
510 ret
= kvm_arch_handle_exit(env
, run
);
515 if (env
->exit_request
) {
516 env
->exit_request
= 0;
517 env
->exception_index
= EXCP_INTERRUPT
;
523 void kvm_set_phys_mem(target_phys_addr_t start_addr
,
525 ram_addr_t phys_offset
)
527 KVMState
*s
= kvm_state
;
528 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
531 /* KVM does not support read-only slots */
532 phys_offset
&= ~IO_MEM_ROM
;
534 mem
= kvm_lookup_slot(s
, start_addr
);
536 if ((flags
== IO_MEM_UNASSIGNED
) || (flags
>= TLB_MMIO
)) {
537 mem
->memory_size
= 0;
538 mem
->start_addr
= start_addr
;
539 mem
->phys_offset
= 0;
542 kvm_set_user_memory_region(s
, mem
);
543 } else if (start_addr
>= mem
->start_addr
&&
544 (start_addr
+ size
) <= (mem
->start_addr
+
547 target_phys_addr_t mem_start
;
548 ram_addr_t mem_size
, mem_offset
;
551 if ((phys_offset
- (start_addr
- mem
->start_addr
)) ==
555 /* unregister whole slot */
556 memcpy(&slot
, mem
, sizeof(slot
));
557 mem
->memory_size
= 0;
558 kvm_set_user_memory_region(s
, mem
);
560 /* register prefix slot */
561 mem_start
= slot
.start_addr
;
562 mem_size
= start_addr
- slot
.start_addr
;
563 mem_offset
= slot
.phys_offset
;
565 kvm_set_phys_mem(mem_start
, mem_size
, mem_offset
);
567 /* register new slot */
568 kvm_set_phys_mem(start_addr
, size
, phys_offset
);
570 /* register suffix slot */
571 mem_start
= start_addr
+ size
;
572 mem_offset
+= mem_size
+ size
;
573 mem_size
= slot
.memory_size
- mem_size
- size
;
575 kvm_set_phys_mem(mem_start
, mem_size
, mem_offset
);
579 printf("Registering overlapping slot\n");
583 /* KVM does not need to know about this memory */
584 if (flags
>= IO_MEM_UNASSIGNED
)
587 mem
= kvm_alloc_slot(s
);
588 mem
->memory_size
= size
;
589 mem
->start_addr
= start_addr
;
590 mem
->phys_offset
= phys_offset
;
593 kvm_set_user_memory_region(s
, mem
);
594 /* FIXME deal with errors */
597 int kvm_ioctl(KVMState
*s
, int type
, ...)
604 arg
= va_arg(ap
, void *);
607 ret
= ioctl(s
->fd
, type
, arg
);
614 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
621 arg
= va_arg(ap
, void *);
624 ret
= ioctl(s
->vmfd
, type
, arg
);
631 int kvm_vcpu_ioctl(CPUState
*env
, int type
, ...)
638 arg
= va_arg(ap
, void *);
641 ret
= ioctl(env
->kvm_fd
, type
, arg
);
648 int kvm_has_sync_mmu(void)
650 #ifdef KVM_CAP_SYNC_MMU
651 KVMState
*s
= kvm_state
;
653 if (kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_SYNC_MMU
) > 0)