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"
30 #define dprintf(fmt, ...) \
31 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
33 #define dprintf(fmt, ...) \
37 typedef struct KVMSlot
39 target_phys_addr_t start_addr
;
40 ram_addr_t memory_size
;
41 ram_addr_t phys_offset
;
46 typedef struct kvm_dirty_log KVMDirtyLog
;
57 static KVMState
*kvm_state
;
59 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
63 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
64 /* KVM private memory slots */
67 if (s
->slots
[i
].memory_size
== 0)
74 static KVMSlot
*kvm_lookup_slot(KVMState
*s
, target_phys_addr_t start_addr
)
78 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
79 KVMSlot
*mem
= &s
->slots
[i
];
81 if (start_addr
>= mem
->start_addr
&&
82 start_addr
< (mem
->start_addr
+ mem
->memory_size
))
89 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
91 struct kvm_userspace_memory_region mem
;
93 mem
.slot
= slot
->slot
;
94 mem
.guest_phys_addr
= slot
->start_addr
;
95 mem
.memory_size
= slot
->memory_size
;
96 mem
.userspace_addr
= (unsigned long)phys_ram_base
+ slot
->phys_offset
;
97 mem
.flags
= slot
->flags
;
99 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
103 int kvm_init_vcpu(CPUState
*env
)
105 KVMState
*s
= kvm_state
;
109 dprintf("kvm_init_vcpu\n");
111 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
113 dprintf("kvm_create_vcpu failed\n");
120 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
122 dprintf("KVM_GET_VCPU_MMAP_SIZE failed\n");
126 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
128 if (env
->kvm_run
== MAP_FAILED
) {
130 dprintf("mmap'ing vcpu state failed\n");
134 ret
= kvm_arch_init_vcpu(env
);
141 * dirty pages logging control
143 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr
, target_phys_addr_t end_addr
,
147 KVMState
*s
= kvm_state
;
148 KVMSlot
*mem
= kvm_lookup_slot(s
, phys_addr
);
150 dprintf("invalid parameters %llx-%llx\n", phys_addr
, end_addr
);
154 flags
= (mem
->flags
& ~mask
) | flags
;
155 /* Nothing changed, no need to issue ioctl */
156 if (flags
== mem
->flags
)
161 return kvm_set_user_memory_region(s
, mem
);
164 int kvm_log_start(target_phys_addr_t phys_addr
, target_phys_addr_t end_addr
)
166 return kvm_dirty_pages_log_change(phys_addr
, end_addr
,
167 KVM_MEM_LOG_DIRTY_PAGES
,
168 KVM_MEM_LOG_DIRTY_PAGES
);
171 int kvm_log_stop(target_phys_addr_t phys_addr
, target_phys_addr_t end_addr
)
173 return kvm_dirty_pages_log_change(phys_addr
, end_addr
,
175 KVM_MEM_LOG_DIRTY_PAGES
);
179 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
180 * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty().
181 * This means all bits are set to dirty.
183 * @start_add: start of logged region. This is what we use to search the memslot
184 * @end_addr: end of logged region.
186 void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
, target_phys_addr_t end_addr
)
188 KVMState
*s
= kvm_state
;
190 KVMSlot
*mem
= kvm_lookup_slot(s
, start_addr
);
191 unsigned long alloc_size
;
193 target_phys_addr_t phys_addr
= start_addr
;
195 dprintf("sync addr: %llx into %lx\n", start_addr
, mem
->phys_offset
);
197 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
201 alloc_size
= mem
->memory_size
>> TARGET_PAGE_BITS
/ sizeof(d
.dirty_bitmap
);
202 d
.dirty_bitmap
= qemu_mallocz(alloc_size
);
204 if (d
.dirty_bitmap
== NULL
) {
205 dprintf("Could not allocate dirty bitmap\n");
210 dprintf("slot %d, phys_addr %llx, uaddr: %llx\n",
211 d
.slot
, mem
->start_addr
, mem
->phys_offset
);
213 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
214 dprintf("ioctl failed %d\n", errno
);
218 phys_addr
= start_addr
;
219 for (addr
= mem
->phys_offset
; phys_addr
< end_addr
; phys_addr
+= TARGET_PAGE_SIZE
, addr
+= TARGET_PAGE_SIZE
) {
220 unsigned long *bitmap
= (unsigned long *)d
.dirty_bitmap
;
221 unsigned nr
= (phys_addr
- start_addr
) >> TARGET_PAGE_BITS
;
222 unsigned word
= nr
/ (sizeof(*bitmap
) * 8);
223 unsigned bit
= nr
% (sizeof(*bitmap
) * 8);
224 if ((bitmap
[word
] >> bit
) & 1)
225 cpu_physical_memory_set_dirty(addr
);
228 qemu_free(d
.dirty_bitmap
);
231 int kvm_init(int smp_cpus
)
240 s
= qemu_mallocz(sizeof(KVMState
));
244 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++)
245 s
->slots
[i
].slot
= i
;
248 s
->fd
= open("/dev/kvm", O_RDWR
);
250 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
255 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
256 if (ret
< KVM_API_VERSION
) {
259 fprintf(stderr
, "kvm version too old\n");
263 if (ret
> KVM_API_VERSION
) {
265 fprintf(stderr
, "kvm version not supported\n");
269 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
273 /* initially, KVM allocated its own memory and we had to jump through
274 * hooks to make phys_ram_base point to this. Modern versions of KVM
275 * just use a user allocated buffer so we can use phys_ram_base
276 * unmodified. Make sure we have a sufficiently modern version of KVM.
278 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
282 fprintf(stderr
, "kvm does not support KVM_CAP_USER_MEMORY\n");
286 ret
= kvm_arch_init(s
, smp_cpus
);
306 static int kvm_handle_io(CPUState
*env
, uint16_t port
, void *data
,
307 int direction
, int size
, uint32_t count
)
312 for (i
= 0; i
< count
; i
++) {
313 if (direction
== KVM_EXIT_IO_IN
) {
316 stb_p(ptr
, cpu_inb(env
, port
));
319 stw_p(ptr
, cpu_inw(env
, port
));
322 stl_p(ptr
, cpu_inl(env
, port
));
328 cpu_outb(env
, port
, ldub_p(ptr
));
331 cpu_outw(env
, port
, lduw_p(ptr
));
334 cpu_outl(env
, port
, ldl_p(ptr
));
345 int kvm_cpu_exec(CPUState
*env
)
347 struct kvm_run
*run
= env
->kvm_run
;
350 dprintf("kvm_cpu_exec()\n");
353 kvm_arch_pre_run(env
, run
);
355 if ((env
->interrupt_request
& CPU_INTERRUPT_EXIT
)) {
356 dprintf("interrupt exit requested\n");
361 ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
362 kvm_arch_post_run(env
, run
);
364 if (ret
== -EINTR
|| ret
== -EAGAIN
) {
365 dprintf("io window exit\n");
371 dprintf("kvm run failed %s\n", strerror(-ret
));
375 ret
= 0; /* exit loop */
376 switch (run
->exit_reason
) {
378 dprintf("handle_io\n");
379 ret
= kvm_handle_io(env
, run
->io
.port
,
380 (uint8_t *)run
+ run
->io
.data_offset
,
386 dprintf("handle_mmio\n");
387 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
393 case KVM_EXIT_IRQ_WINDOW_OPEN
:
394 dprintf("irq_window_open\n");
396 case KVM_EXIT_SHUTDOWN
:
397 dprintf("shutdown\n");
398 qemu_system_reset_request();
401 case KVM_EXIT_UNKNOWN
:
402 dprintf("kvm_exit_unknown\n");
404 case KVM_EXIT_FAIL_ENTRY
:
405 dprintf("kvm_exit_fail_entry\n");
407 case KVM_EXIT_EXCEPTION
:
408 dprintf("kvm_exit_exception\n");
411 dprintf("kvm_exit_debug\n");
414 dprintf("kvm_arch_handle_exit\n");
415 ret
= kvm_arch_handle_exit(env
, run
);
420 if ((env
->interrupt_request
& CPU_INTERRUPT_EXIT
)) {
421 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
422 env
->exception_index
= EXCP_INTERRUPT
;
428 void kvm_set_phys_mem(target_phys_addr_t start_addr
,
430 ram_addr_t phys_offset
)
432 KVMState
*s
= kvm_state
;
433 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
436 /* KVM does not support read-only slots */
437 phys_offset
&= ~IO_MEM_ROM
;
439 mem
= kvm_lookup_slot(s
, start_addr
);
441 if ((flags
== IO_MEM_UNASSIGNED
) || (flags
>= TLB_MMIO
)) {
442 mem
->memory_size
= 0;
443 mem
->start_addr
= start_addr
;
444 mem
->phys_offset
= 0;
447 kvm_set_user_memory_region(s
, mem
);
448 } else if (start_addr
>= mem
->start_addr
&&
449 (start_addr
+ size
) <= (mem
->start_addr
+
452 target_phys_addr_t mem_start
;
453 ram_addr_t mem_size
, mem_offset
;
456 if ((phys_offset
- (start_addr
- mem
->start_addr
)) ==
460 /* unregister whole slot */
461 memcpy(&slot
, mem
, sizeof(slot
));
462 mem
->memory_size
= 0;
463 kvm_set_user_memory_region(s
, mem
);
465 /* register prefix slot */
466 mem_start
= slot
.start_addr
;
467 mem_size
= start_addr
- slot
.start_addr
;
468 mem_offset
= slot
.phys_offset
;
470 kvm_set_phys_mem(mem_start
, mem_size
, mem_offset
);
472 /* register new slot */
473 kvm_set_phys_mem(start_addr
, size
, phys_offset
);
475 /* register suffix slot */
476 mem_start
= start_addr
+ size
;
477 mem_offset
+= mem_size
+ size
;
478 mem_size
= slot
.memory_size
- mem_size
- size
;
480 kvm_set_phys_mem(mem_start
, mem_size
, mem_offset
);
484 printf("Registering overlapping slot\n");
488 /* KVM does not need to know about this memory */
489 if (flags
>= IO_MEM_UNASSIGNED
)
492 mem
= kvm_alloc_slot(s
);
493 mem
->memory_size
= size
;
494 mem
->start_addr
= start_addr
;
495 mem
->phys_offset
= phys_offset
;
498 kvm_set_user_memory_region(s
, mem
);
499 /* FIXME deal with errors */
502 int kvm_ioctl(KVMState
*s
, int type
, ...)
509 arg
= va_arg(ap
, void *);
512 ret
= ioctl(s
->fd
, type
, arg
);
519 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
526 arg
= va_arg(ap
, void *);
529 ret
= ioctl(s
->vmfd
, type
, arg
);
536 int kvm_vcpu_ioctl(CPUState
*env
, int type
, ...)
543 arg
= va_arg(ap
, void *);
546 ret
= ioctl(env
->kvm_fd
, type
, arg
);