]> git.proxmox.com Git - qemu.git/blame - kvm-all.c
Don't wrap I2C registers addresses on PXA270.
[qemu.git] / kvm-all.c
CommitLineData
05330448
AL
1/*
2 * QEMU KVM support
3 *
4 * Copyright IBM, Corp. 2008
5832d1f2 5 * Red Hat, Inc. 2008
05330448
AL
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
5832d1f2 9 * Glauber Costa <gcosta@redhat.com>
05330448
AL
10 *
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.
13 *
14 */
15
16#include <sys/types.h>
17#include <sys/ioctl.h>
18#include <sys/mman.h>
984b5181 19#include <stdarg.h>
05330448
AL
20
21#include <linux/kvm.h>
22
23#include "qemu-common.h"
24#include "sysemu.h"
25#include "kvm.h"
26
27//#define DEBUG_KVM
28
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
36
34fc643f
AL
37typedef struct KVMSlot
38{
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;
05330448 45
5832d1f2
AL
46typedef struct kvm_dirty_log KVMDirtyLog;
47
05330448
AL
48int kvm_allowed = 0;
49
50struct KVMState
51{
52 KVMSlot slots[32];
53 int fd;
54 int vmfd;
55};
56
57static KVMState *kvm_state;
58
59static KVMSlot *kvm_alloc_slot(KVMState *s)
60{
61 int i;
62
63 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
62d60e8c
AL
64 /* KVM private memory slots */
65 if (i >= 8 && i < 12)
66 continue;
05330448
AL
67 if (s->slots[i].memory_size == 0)
68 return &s->slots[i];
69 }
70
71 return NULL;
72}
73
74static KVMSlot *kvm_lookup_slot(KVMState *s, target_phys_addr_t start_addr)
75{
76 int i;
77
78 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
79 KVMSlot *mem = &s->slots[i];
80
34fc643f
AL
81 if (start_addr >= mem->start_addr &&
82 start_addr < (mem->start_addr + mem->memory_size))
05330448
AL
83 return mem;
84 }
85
86 return NULL;
87}
88
5832d1f2
AL
89static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
90{
91 struct kvm_userspace_memory_region mem;
92
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;
98
99 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
100}
101
102
05330448
AL
103int kvm_init_vcpu(CPUState *env)
104{
105 KVMState *s = kvm_state;
106 long mmap_size;
107 int ret;
108
109 dprintf("kvm_init_vcpu\n");
110
984b5181 111 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
05330448
AL
112 if (ret < 0) {
113 dprintf("kvm_create_vcpu failed\n");
114 goto err;
115 }
116
117 env->kvm_fd = ret;
118 env->kvm_state = s;
119
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;
124 }
125
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;
132 }
133
134 ret = kvm_arch_init_vcpu(env);
135
136err:
137 return ret;
138}
139
5832d1f2
AL
140/*
141 * dirty pages logging control
142 */
143static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr, target_phys_addr_t end_addr,
144 unsigned flags,
145 unsigned mask)
146{
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;
152 }
153
154 flags = (mem->flags & ~mask) | flags;
155 /* Nothing changed, no need to issue ioctl */
156 if (flags == mem->flags)
157 return 0;
158
159 mem->flags = flags;
160
161 return kvm_set_user_memory_region(s, mem);
162}
163
164int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t end_addr)
165{
166 return kvm_dirty_pages_log_change(phys_addr, end_addr,
167 KVM_MEM_LOG_DIRTY_PAGES,
168 KVM_MEM_LOG_DIRTY_PAGES);
169}
170
171int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t end_addr)
172{
173 return kvm_dirty_pages_log_change(phys_addr, end_addr,
174 0,
175 KVM_MEM_LOG_DIRTY_PAGES);
176}
177
178/**
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.
182 *
183 * @start_add: start of logged region. This is what we use to search the memslot
184 * @end_addr: end of logged region.
185 */
186void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
187{
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;
194
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;
199 }
200
201 alloc_size = mem->memory_size >> TARGET_PAGE_BITS / sizeof(d.dirty_bitmap);
202 d.dirty_bitmap = qemu_mallocz(alloc_size);
203
204 if (d.dirty_bitmap == NULL) {
205 dprintf("Could not allocate dirty bitmap\n");
206 return;
207 }
208
209 d.slot = mem->slot;
210 dprintf("slot %d, phys_addr %llx, uaddr: %llx\n",
211 d.slot, mem->start_addr, mem->phys_offset);
212
213 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
214 dprintf("ioctl failed %d\n", errno);
215 goto out;
216 }
217
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);
226 }
227out:
228 qemu_free(d.dirty_bitmap);
229}
230
05330448
AL
231int kvm_init(int smp_cpus)
232{
233 KVMState *s;
234 int ret;
235 int i;
236
237 if (smp_cpus > 1)
238 return -EINVAL;
239
240 s = qemu_mallocz(sizeof(KVMState));
241 if (s == NULL)
242 return -ENOMEM;
243
244 for (i = 0; i < ARRAY_SIZE(s->slots); i++)
245 s->slots[i].slot = i;
246
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;
253 }
254
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;
261 }
262
263 if (ret > KVM_API_VERSION) {
264 ret = -EINVAL;
265 fprintf(stderr, "kvm version not supported\n");
266 goto err;
267 }
268
269 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
270 if (s->vmfd < 0)
271 goto err;
272
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.
277 */
984b5181 278 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY);
05330448
AL
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;
284 }
285
286 ret = kvm_arch_init(s, smp_cpus);
287 if (ret < 0)
288 goto err;
289
290 kvm_state = s;
291
292 return 0;
293
294err:
295 if (s) {
296 if (s->vmfd != -1)
297 close(s->vmfd);
298 if (s->fd != -1)
299 close(s->fd);
300 }
301 qemu_free(s);
302
303 return ret;
304}
305
306static int kvm_handle_io(CPUState *env, uint16_t port, void *data,
307 int direction, int size, uint32_t count)
308{
309 int i;
310 uint8_t *ptr = data;
311
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;
324 }
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;
336 }
337 }
338
339 ptr += size;
340 }
341
342 return 1;
343}
344
345int kvm_cpu_exec(CPUState *env)
346{
347 struct kvm_run *run = env->kvm_run;
348 int ret;
349
350 dprintf("kvm_cpu_exec()\n");
351
352 do {
353 kvm_arch_pre_run(env, run);
354
355 if ((env->interrupt_request & CPU_INTERRUPT_EXIT)) {
356 dprintf("interrupt exit requested\n");
357 ret = 0;
358 break;
359 }
360
361 ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
362 kvm_arch_post_run(env, run);
363
364 if (ret == -EINTR || ret == -EAGAIN) {
365 dprintf("io window exit\n");
366 ret = 0;
367 break;
368 }
369
370 if (ret < 0) {
371 dprintf("kvm run failed %s\n", strerror(-ret));
372 abort();
373 }
374
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;
417 }
418 } while (ret > 0);
419
becfc390
AL
420 if ((env->interrupt_request & CPU_INTERRUPT_EXIT)) {
421 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
422 env->exception_index = EXCP_INTERRUPT;
423 }
424
05330448
AL
425 return ret;
426}
427
428void kvm_set_phys_mem(target_phys_addr_t start_addr,
429 ram_addr_t size,
430 ram_addr_t phys_offset)
431{
432 KVMState *s = kvm_state;
433 ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
434 KVMSlot *mem;
435
436 /* KVM does not support read-only slots */
437 phys_offset &= ~IO_MEM_ROM;
438
439 mem = kvm_lookup_slot(s, start_addr);
440 if (mem) {
a3d6841f 441 if ((flags == IO_MEM_UNASSIGNED) || (flags >= TLB_MMIO)) {
05330448 442 mem->memory_size = 0;
34fc643f
AL
443 mem->start_addr = start_addr;
444 mem->phys_offset = 0;
05330448
AL
445 mem->flags = 0;
446
34fc643f
AL
447 kvm_set_user_memory_region(s, mem);
448 } else if (start_addr >= mem->start_addr &&
449 (start_addr + size) <= (mem->start_addr +
62d60e8c
AL
450 mem->memory_size)) {
451 KVMSlot slot;
452 target_phys_addr_t mem_start;
453 ram_addr_t mem_size, mem_offset;
454
455 /* Not splitting */
34fc643f
AL
456 if ((phys_offset - (start_addr - mem->start_addr)) ==
457 mem->phys_offset)
62d60e8c
AL
458 return;
459
460 /* unregister whole slot */
461 memcpy(&slot, mem, sizeof(slot));
462 mem->memory_size = 0;
34fc643f 463 kvm_set_user_memory_region(s, mem);
62d60e8c
AL
464
465 /* register prefix slot */
34fc643f
AL
466 mem_start = slot.start_addr;
467 mem_size = start_addr - slot.start_addr;
468 mem_offset = slot.phys_offset;
62d60e8c
AL
469 if (mem_size)
470 kvm_set_phys_mem(mem_start, mem_size, mem_offset);
471
472 /* register new slot */
473 kvm_set_phys_mem(start_addr, size, phys_offset);
474
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);
481
05330448 482 return;
62d60e8c
AL
483 } else {
484 printf("Registering overlapping slot\n");
485 abort();
486 }
05330448 487 }
05330448
AL
488 /* KVM does not need to know about this memory */
489 if (flags >= IO_MEM_UNASSIGNED)
490 return;
491
492 mem = kvm_alloc_slot(s);
493 mem->memory_size = size;
34fc643f
AL
494 mem->start_addr = start_addr;
495 mem->phys_offset = phys_offset;
05330448
AL
496 mem->flags = 0;
497
34fc643f 498 kvm_set_user_memory_region(s, mem);
05330448
AL
499 /* FIXME deal with errors */
500}
501
984b5181 502int kvm_ioctl(KVMState *s, int type, ...)
05330448
AL
503{
504 int ret;
984b5181
AL
505 void *arg;
506 va_list ap;
05330448 507
984b5181
AL
508 va_start(ap, type);
509 arg = va_arg(ap, void *);
510 va_end(ap);
511
512 ret = ioctl(s->fd, type, arg);
05330448
AL
513 if (ret == -1)
514 ret = -errno;
515
516 return ret;
517}
518
984b5181 519int kvm_vm_ioctl(KVMState *s, int type, ...)
05330448
AL
520{
521 int ret;
984b5181
AL
522 void *arg;
523 va_list ap;
524
525 va_start(ap, type);
526 arg = va_arg(ap, void *);
527 va_end(ap);
05330448 528
984b5181 529 ret = ioctl(s->vmfd, type, arg);
05330448
AL
530 if (ret == -1)
531 ret = -errno;
532
533 return ret;
534}
535
984b5181 536int kvm_vcpu_ioctl(CPUState *env, int type, ...)
05330448
AL
537{
538 int ret;
984b5181
AL
539 void *arg;
540 va_list ap;
541
542 va_start(ap, type);
543 arg = va_arg(ap, void *);
544 va_end(ap);
05330448 545
984b5181 546 ret = ioctl(env->kvm_fd, type, arg);
05330448
AL
547 if (ret == -1)
548 ret = -errno;
549
550 return ret;
551}
bd322087
AL
552
553int kvm_has_sync_mmu(void)
554{
555 KVMState *s = kvm_state;
556
557#ifdef KVM_CAP_SYNC_MMU
558 if (kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_SYNC_MMU) > 0)
559 return 1;
560#endif
561
562 return 0;
563}