target-ppc: memory load/store rework
[qemu/mini2440/sniper_sniper_test.git] / kvm-all.c
blob69ca46b1af3b1e8e888689198a830ddb30e66547
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 //#define DEBUG_KVM
29 #ifdef DEBUG_KVM
30 #define dprintf(fmt, ...) \
31 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
32 #else
33 #define dprintf(fmt, ...) \
34 do { } while (0)
35 #endif
37 typedef struct KVMSlot
39 target_phys_addr_t start_addr;
40 ram_addr_t memory_size;
41 ram_addr_t phys_offset;
42 int slot;
43 int flags;
44 } KVMSlot;
46 typedef struct kvm_dirty_log KVMDirtyLog;
48 int kvm_allowed = 0;
50 struct KVMState
52 KVMSlot slots[32];
53 int fd;
54 int vmfd;
57 static KVMState *kvm_state;
59 static KVMSlot *kvm_alloc_slot(KVMState *s)
61 int i;
63 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
64 /* KVM private memory slots */
65 if (i >= 8 && i < 12)
66 continue;
67 if (s->slots[i].memory_size == 0)
68 return &s->slots[i];
71 return NULL;
74 static KVMSlot *kvm_lookup_slot(KVMState *s, target_phys_addr_t start_addr)
76 int i;
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))
83 return mem;
86 return NULL;
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;
106 long mmap_size;
107 int ret;
109 dprintf("kvm_init_vcpu\n");
111 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
112 if (ret < 0) {
113 dprintf("kvm_create_vcpu failed\n");
114 goto err;
117 env->kvm_fd = ret;
118 env->kvm_state = s;
120 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
121 if (mmap_size < 0) {
122 dprintf("KVM_GET_VCPU_MMAP_SIZE failed\n");
123 goto err;
126 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
127 env->kvm_fd, 0);
128 if (env->kvm_run == MAP_FAILED) {
129 ret = -errno;
130 dprintf("mmap'ing vcpu state failed\n");
131 goto err;
134 ret = kvm_arch_init_vcpu(env);
136 err:
137 return ret;
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,
144 unsigned flags,
145 unsigned mask)
147 KVMState *s = kvm_state;
148 KVMSlot *mem = kvm_lookup_slot(s, phys_addr);
149 if (mem == NULL) {
150 dprintf("invalid parameters %llx-%llx\n", phys_addr, end_addr);
151 return -EINVAL;
154 flags = (mem->flags & ~mask) | flags;
155 /* Nothing changed, no need to issue ioctl */
156 if (flags == mem->flags)
157 return 0;
159 mem->flags = 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;
189 KVMDirtyLog d;
190 KVMSlot *mem = kvm_lookup_slot(s, start_addr);
191 unsigned long alloc_size;
192 ram_addr_t addr;
193 target_phys_addr_t phys_addr = start_addr;
195 dprintf("sync addr: %llx into %lx\n", start_addr, mem->phys_offset);
196 if (mem == NULL) {
197 fprintf(stderr, "BUG: %s: invalid parameters\n", __func__);
198 return;
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");
206 return;
209 d.slot = mem->slot;
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);
215 goto out;
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);
227 out:
228 qemu_free(d.dirty_bitmap);
231 int kvm_init(int smp_cpus)
233 KVMState *s;
234 int ret;
235 int i;
237 if (smp_cpus > 1)
238 return -EINVAL;
240 s = qemu_mallocz(sizeof(KVMState));
241 if (s == NULL)
242 return -ENOMEM;
244 for (i = 0; i < ARRAY_SIZE(s->slots); i++)
245 s->slots[i].slot = i;
247 s->vmfd = -1;
248 s->fd = open("/dev/kvm", O_RDWR);
249 if (s->fd == -1) {
250 fprintf(stderr, "Could not access KVM kernel module: %m\n");
251 ret = -errno;
252 goto err;
255 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
256 if (ret < KVM_API_VERSION) {
257 if (ret > 0)
258 ret = -EINVAL;
259 fprintf(stderr, "kvm version too old\n");
260 goto err;
263 if (ret > KVM_API_VERSION) {
264 ret = -EINVAL;
265 fprintf(stderr, "kvm version not supported\n");
266 goto err;
269 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
270 if (s->vmfd < 0)
271 goto err;
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);
279 if (ret <= 0) {
280 if (ret == 0)
281 ret = -EINVAL;
282 fprintf(stderr, "kvm does not support KVM_CAP_USER_MEMORY\n");
283 goto err;
286 ret = kvm_arch_init(s, smp_cpus);
287 if (ret < 0)
288 goto err;
290 kvm_state = s;
292 return 0;
294 err:
295 if (s) {
296 if (s->vmfd != -1)
297 close(s->vmfd);
298 if (s->fd != -1)
299 close(s->fd);
301 qemu_free(s);
303 return ret;
306 static int kvm_handle_io(CPUState *env, uint16_t port, void *data,
307 int direction, int size, uint32_t count)
309 int i;
310 uint8_t *ptr = data;
312 for (i = 0; i < count; i++) {
313 if (direction == KVM_EXIT_IO_IN) {
314 switch (size) {
315 case 1:
316 stb_p(ptr, cpu_inb(env, port));
317 break;
318 case 2:
319 stw_p(ptr, cpu_inw(env, port));
320 break;
321 case 4:
322 stl_p(ptr, cpu_inl(env, port));
323 break;
325 } else {
326 switch (size) {
327 case 1:
328 cpu_outb(env, port, ldub_p(ptr));
329 break;
330 case 2:
331 cpu_outw(env, port, lduw_p(ptr));
332 break;
333 case 4:
334 cpu_outl(env, port, ldl_p(ptr));
335 break;
339 ptr += size;
342 return 1;
345 int kvm_cpu_exec(CPUState *env)
347 struct kvm_run *run = env->kvm_run;
348 int ret;
350 dprintf("kvm_cpu_exec()\n");
352 do {
353 kvm_arch_pre_run(env, run);
355 if ((env->interrupt_request & CPU_INTERRUPT_EXIT)) {
356 dprintf("interrupt exit requested\n");
357 ret = 0;
358 break;
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");
366 ret = 0;
367 break;
370 if (ret < 0) {
371 dprintf("kvm run failed %s\n", strerror(-ret));
372 abort();
375 ret = 0; /* exit loop */
376 switch (run->exit_reason) {
377 case KVM_EXIT_IO:
378 dprintf("handle_io\n");
379 ret = kvm_handle_io(env, run->io.port,
380 (uint8_t *)run + run->io.data_offset,
381 run->io.direction,
382 run->io.size,
383 run->io.count);
384 break;
385 case KVM_EXIT_MMIO:
386 dprintf("handle_mmio\n");
387 cpu_physical_memory_rw(run->mmio.phys_addr,
388 run->mmio.data,
389 run->mmio.len,
390 run->mmio.is_write);
391 ret = 1;
392 break;
393 case KVM_EXIT_IRQ_WINDOW_OPEN:
394 dprintf("irq_window_open\n");
395 break;
396 case KVM_EXIT_SHUTDOWN:
397 dprintf("shutdown\n");
398 qemu_system_reset_request();
399 ret = 1;
400 break;
401 case KVM_EXIT_UNKNOWN:
402 dprintf("kvm_exit_unknown\n");
403 break;
404 case KVM_EXIT_FAIL_ENTRY:
405 dprintf("kvm_exit_fail_entry\n");
406 break;
407 case KVM_EXIT_EXCEPTION:
408 dprintf("kvm_exit_exception\n");
409 break;
410 case KVM_EXIT_DEBUG:
411 dprintf("kvm_exit_debug\n");
412 break;
413 default:
414 dprintf("kvm_arch_handle_exit\n");
415 ret = kvm_arch_handle_exit(env, run);
416 break;
418 } while (ret > 0);
420 if ((env->interrupt_request & CPU_INTERRUPT_EXIT)) {
421 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
422 env->exception_index = EXCP_INTERRUPT;
425 return ret;
428 void kvm_set_phys_mem(target_phys_addr_t start_addr,
429 ram_addr_t size,
430 ram_addr_t phys_offset)
432 KVMState *s = kvm_state;
433 ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
434 KVMSlot *mem;
436 /* KVM does not support read-only slots */
437 phys_offset &= ~IO_MEM_ROM;
439 mem = kvm_lookup_slot(s, start_addr);
440 if (mem) {
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;
445 mem->flags = 0;
447 kvm_set_user_memory_region(s, mem);
448 } else if (start_addr >= mem->start_addr &&
449 (start_addr + size) <= (mem->start_addr +
450 mem->memory_size)) {
451 KVMSlot slot;
452 target_phys_addr_t mem_start;
453 ram_addr_t mem_size, mem_offset;
455 /* Not splitting */
456 if ((phys_offset - (start_addr - mem->start_addr)) ==
457 mem->phys_offset)
458 return;
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;
469 if (mem_size)
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;
479 if (mem_size)
480 kvm_set_phys_mem(mem_start, mem_size, mem_offset);
482 return;
483 } else {
484 printf("Registering overlapping slot\n");
485 abort();
488 /* KVM does not need to know about this memory */
489 if (flags >= IO_MEM_UNASSIGNED)
490 return;
492 mem = kvm_alloc_slot(s);
493 mem->memory_size = size;
494 mem->start_addr = start_addr;
495 mem->phys_offset = phys_offset;
496 mem->flags = 0;
498 kvm_set_user_memory_region(s, mem);
499 /* FIXME deal with errors */
502 int kvm_ioctl(KVMState *s, int type, ...)
504 int ret;
505 void *arg;
506 va_list ap;
508 va_start(ap, type);
509 arg = va_arg(ap, void *);
510 va_end(ap);
512 ret = ioctl(s->fd, type, arg);
513 if (ret == -1)
514 ret = -errno;
516 return ret;
519 int kvm_vm_ioctl(KVMState *s, int type, ...)
521 int ret;
522 void *arg;
523 va_list ap;
525 va_start(ap, type);
526 arg = va_arg(ap, void *);
527 va_end(ap);
529 ret = ioctl(s->vmfd, type, arg);
530 if (ret == -1)
531 ret = -errno;
533 return ret;
536 int kvm_vcpu_ioctl(CPUState *env, int type, ...)
538 int ret;
539 void *arg;
540 va_list ap;
542 va_start(ap, type);
543 arg = va_arg(ap, void *);
544 va_end(ap);
546 ret = ioctl(env->kvm_fd, type, arg);
547 if (ret == -1)
548 ret = -errno;
550 return ret;
553 int kvm_has_sync_mmu(void)
555 KVMState *s = kvm_state;
557 #ifdef KVM_CAP_SYNC_MMU
558 if (kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_SYNC_MMU) > 0)
559 return 1;
560 #endif
562 return 0;