]> git.proxmox.com Git - mirror_qemu.git/blame - kvm-all.c
MAINTAINERS: Add qemu-ppc to all ppc target stuff
[mirror_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"
85199474 24#include "qemu-barrier.h"
05330448 25#include "sysemu.h"
d33a1810 26#include "hw/hw.h"
e22a25c9 27#include "gdbstub.h"
05330448 28#include "kvm.h"
8369e01c 29#include "bswap.h"
a01672d3 30#include "memory.h"
05330448 31
d2f2b8a7
SH
32/* This check must be after config-host.h is included */
33#ifdef CONFIG_EVENTFD
34#include <sys/eventfd.h>
35#endif
36
f65ed4c1
AL
37/* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
38#define PAGE_SIZE TARGET_PAGE_SIZE
39
05330448
AL
40//#define DEBUG_KVM
41
42#ifdef DEBUG_KVM
8c0d577e 43#define DPRINTF(fmt, ...) \
05330448
AL
44 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
45#else
8c0d577e 46#define DPRINTF(fmt, ...) \
05330448
AL
47 do { } while (0)
48#endif
49
34fc643f
AL
50typedef struct KVMSlot
51{
c227f099
AL
52 target_phys_addr_t start_addr;
53 ram_addr_t memory_size;
9f213ed9 54 void *ram;
34fc643f
AL
55 int slot;
56 int flags;
57} KVMSlot;
05330448 58
5832d1f2
AL
59typedef struct kvm_dirty_log KVMDirtyLog;
60
05330448
AL
61struct KVMState
62{
63 KVMSlot slots[32];
64 int fd;
65 int vmfd;
f65ed4c1 66 int coalesced_mmio;
62a2744c 67 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
1cae88b9 68 bool coalesced_flush_in_progress;
e69917e2 69 int broken_set_mem_region;
4495d6a7 70 int migration_log;
a0fb002c 71 int vcpu_events;
b0b1d690 72 int robust_singlestep;
ff44f1a3 73 int debugregs;
e22a25c9
AL
74#ifdef KVM_CAP_SET_GUEST_DEBUG
75 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
76#endif
6f725c13
GC
77 int irqchip_in_kernel;
78 int pit_in_kernel;
f1665b21 79 int xsave, xcrs;
d2f2b8a7 80 int many_ioeventfds;
05330448
AL
81};
82
6a7af8cb 83KVMState *kvm_state;
05330448 84
94a8d39a
JK
85static const KVMCapabilityInfo kvm_required_capabilites[] = {
86 KVM_CAP_INFO(USER_MEMORY),
87 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
88 KVM_CAP_LAST_INFO
89};
90
05330448
AL
91static KVMSlot *kvm_alloc_slot(KVMState *s)
92{
93 int i;
94
95 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
a426e122 96 if (s->slots[i].memory_size == 0) {
05330448 97 return &s->slots[i];
a426e122 98 }
05330448
AL
99 }
100
d3f8d37f
AL
101 fprintf(stderr, "%s: no free slot available\n", __func__);
102 abort();
103}
104
105static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
c227f099
AL
106 target_phys_addr_t start_addr,
107 target_phys_addr_t end_addr)
d3f8d37f
AL
108{
109 int i;
110
111 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
112 KVMSlot *mem = &s->slots[i];
113
114 if (start_addr == mem->start_addr &&
115 end_addr == mem->start_addr + mem->memory_size) {
116 return mem;
117 }
118 }
119
05330448
AL
120 return NULL;
121}
122
6152e2ae
AL
123/*
124 * Find overlapping slot with lowest start address
125 */
126static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
c227f099
AL
127 target_phys_addr_t start_addr,
128 target_phys_addr_t end_addr)
05330448 129{
6152e2ae 130 KVMSlot *found = NULL;
05330448
AL
131 int i;
132
133 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
134 KVMSlot *mem = &s->slots[i];
135
6152e2ae
AL
136 if (mem->memory_size == 0 ||
137 (found && found->start_addr < mem->start_addr)) {
138 continue;
139 }
140
141 if (end_addr > mem->start_addr &&
142 start_addr < mem->start_addr + mem->memory_size) {
143 found = mem;
144 }
05330448
AL
145 }
146
6152e2ae 147 return found;
05330448
AL
148}
149
9f213ed9
AK
150int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
151 target_phys_addr_t *phys_addr)
983dfc3b
HY
152{
153 int i;
154
155 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
156 KVMSlot *mem = &s->slots[i];
157
9f213ed9
AK
158 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
159 *phys_addr = mem->start_addr + (ram - mem->ram);
983dfc3b
HY
160 return 1;
161 }
162 }
163
164 return 0;
165}
166
5832d1f2
AL
167static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
168{
169 struct kvm_userspace_memory_region mem;
170
171 mem.slot = slot->slot;
172 mem.guest_phys_addr = slot->start_addr;
173 mem.memory_size = slot->memory_size;
9f213ed9 174 mem.userspace_addr = (unsigned long)slot->ram;
5832d1f2 175 mem.flags = slot->flags;
4495d6a7
JK
176 if (s->migration_log) {
177 mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
178 }
5832d1f2
AL
179 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
180}
181
8d2ba1fb
JK
182static void kvm_reset_vcpu(void *opaque)
183{
184 CPUState *env = opaque;
185
caa5af0f 186 kvm_arch_reset_vcpu(env);
8d2ba1fb 187}
5832d1f2 188
6f725c13
GC
189int kvm_irqchip_in_kernel(void)
190{
191 return kvm_state->irqchip_in_kernel;
192}
193
194int kvm_pit_in_kernel(void)
195{
196 return kvm_state->pit_in_kernel;
197}
198
05330448
AL
199int kvm_init_vcpu(CPUState *env)
200{
201 KVMState *s = kvm_state;
202 long mmap_size;
203 int ret;
204
8c0d577e 205 DPRINTF("kvm_init_vcpu\n");
05330448 206
984b5181 207 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
05330448 208 if (ret < 0) {
8c0d577e 209 DPRINTF("kvm_create_vcpu failed\n");
05330448
AL
210 goto err;
211 }
212
213 env->kvm_fd = ret;
214 env->kvm_state = s;
d841b6c4 215 env->kvm_vcpu_dirty = 1;
05330448
AL
216
217 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
218 if (mmap_size < 0) {
748a680b 219 ret = mmap_size;
8c0d577e 220 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
05330448
AL
221 goto err;
222 }
223
224 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
225 env->kvm_fd, 0);
226 if (env->kvm_run == MAP_FAILED) {
227 ret = -errno;
8c0d577e 228 DPRINTF("mmap'ing vcpu state failed\n");
05330448
AL
229 goto err;
230 }
231
a426e122
JK
232 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
233 s->coalesced_mmio_ring =
234 (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
235 }
62a2744c 236
05330448 237 ret = kvm_arch_init_vcpu(env);
8d2ba1fb 238 if (ret == 0) {
a08d4367 239 qemu_register_reset(kvm_reset_vcpu, env);
caa5af0f 240 kvm_arch_reset_vcpu(env);
8d2ba1fb 241 }
05330448
AL
242err:
243 return ret;
244}
245
5832d1f2
AL
246/*
247 * dirty pages logging control
248 */
25254bbc
MT
249
250static int kvm_mem_flags(KVMState *s, bool log_dirty)
251{
252 return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
253}
254
255static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
5832d1f2
AL
256{
257 KVMState *s = kvm_state;
25254bbc 258 int flags, mask = KVM_MEM_LOG_DIRTY_PAGES;
4495d6a7
JK
259 int old_flags;
260
4495d6a7 261 old_flags = mem->flags;
5832d1f2 262
25254bbc 263 flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
5832d1f2
AL
264 mem->flags = flags;
265
4495d6a7
JK
266 /* If nothing changed effectively, no need to issue ioctl */
267 if (s->migration_log) {
268 flags |= KVM_MEM_LOG_DIRTY_PAGES;
269 }
25254bbc 270
4495d6a7 271 if (flags == old_flags) {
25254bbc 272 return 0;
4495d6a7
JK
273 }
274
5832d1f2
AL
275 return kvm_set_user_memory_region(s, mem);
276}
277
25254bbc
MT
278static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
279 ram_addr_t size, bool log_dirty)
280{
281 KVMState *s = kvm_state;
282 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
283
284 if (mem == NULL) {
285 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
286 TARGET_FMT_plx "\n", __func__, phys_addr,
287 (target_phys_addr_t)(phys_addr + size - 1));
288 return -EINVAL;
289 }
290 return kvm_slot_dirty_pages_log_change(mem, log_dirty);
291}
292
a01672d3
AK
293static void kvm_log_start(MemoryListener *listener,
294 MemoryRegionSection *section)
5832d1f2 295{
a01672d3
AK
296 int r;
297
298 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
299 section->size, true);
300 if (r < 0) {
301 abort();
302 }
5832d1f2
AL
303}
304
a01672d3
AK
305static void kvm_log_stop(MemoryListener *listener,
306 MemoryRegionSection *section)
5832d1f2 307{
a01672d3
AK
308 int r;
309
310 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
311 section->size, false);
312 if (r < 0) {
313 abort();
314 }
5832d1f2
AL
315}
316
7b8f3b78 317static int kvm_set_migration_log(int enable)
4495d6a7
JK
318{
319 KVMState *s = kvm_state;
320 KVMSlot *mem;
321 int i, err;
322
323 s->migration_log = enable;
324
325 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
326 mem = &s->slots[i];
327
70fedd76
AW
328 if (!mem->memory_size) {
329 continue;
330 }
4495d6a7
JK
331 if (!!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) == enable) {
332 continue;
333 }
334 err = kvm_set_user_memory_region(s, mem);
335 if (err) {
336 return err;
337 }
338 }
339 return 0;
340}
341
8369e01c 342/* get kvm's dirty pages bitmap and update qemu's */
ffcde12f
AK
343static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
344 unsigned long *bitmap)
96c1606b 345{
8369e01c 346 unsigned int i, j;
aa90fec7
BH
347 unsigned long page_number, c;
348 target_phys_addr_t addr, addr1;
ffcde12f 349 unsigned int len = ((section->size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
8369e01c
MT
350
351 /*
352 * bitmap-traveling is faster than memory-traveling (for addr...)
353 * especially when most of the memory is not dirty.
354 */
355 for (i = 0; i < len; i++) {
356 if (bitmap[i] != 0) {
357 c = leul_to_cpu(bitmap[i]);
358 do {
359 j = ffsl(c) - 1;
360 c &= ~(1ul << j);
361 page_number = i * HOST_LONG_BITS + j;
362 addr1 = page_number * TARGET_PAGE_SIZE;
ffcde12f
AK
363 addr = section->offset_within_region + addr1;
364 memory_region_set_dirty(section->mr, addr);
8369e01c
MT
365 } while (c != 0);
366 }
367 }
368 return 0;
96c1606b
AG
369}
370
8369e01c
MT
371#define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
372
5832d1f2
AL
373/**
374 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
375 * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty().
376 * This means all bits are set to dirty.
377 *
d3f8d37f 378 * @start_add: start of logged region.
5832d1f2
AL
379 * @end_addr: end of logged region.
380 */
ffcde12f 381static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
5832d1f2
AL
382{
383 KVMState *s = kvm_state;
151f7749 384 unsigned long size, allocated_size = 0;
151f7749
JK
385 KVMDirtyLog d;
386 KVMSlot *mem;
387 int ret = 0;
ffcde12f
AK
388 target_phys_addr_t start_addr = section->offset_within_address_space;
389 target_phys_addr_t end_addr = start_addr + section->size;
5832d1f2 390
151f7749
JK
391 d.dirty_bitmap = NULL;
392 while (start_addr < end_addr) {
393 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
394 if (mem == NULL) {
395 break;
396 }
5832d1f2 397
51b0c606
MT
398 /* XXX bad kernel interface alert
399 * For dirty bitmap, kernel allocates array of size aligned to
400 * bits-per-long. But for case when the kernel is 64bits and
401 * the userspace is 32bits, userspace can't align to the same
402 * bits-per-long, since sizeof(long) is different between kernel
403 * and user space. This way, userspace will provide buffer which
404 * may be 4 bytes less than the kernel will use, resulting in
405 * userspace memory corruption (which is not detectable by valgrind
406 * too, in most cases).
407 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
408 * a hope that sizeof(long) wont become >8 any time soon.
409 */
410 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
411 /*HOST_LONG_BITS*/ 64) / 8;
151f7749 412 if (!d.dirty_bitmap) {
7267c094 413 d.dirty_bitmap = g_malloc(size);
151f7749 414 } else if (size > allocated_size) {
7267c094 415 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
151f7749
JK
416 }
417 allocated_size = size;
418 memset(d.dirty_bitmap, 0, allocated_size);
5832d1f2 419
151f7749 420 d.slot = mem->slot;
5832d1f2 421
6e489f3f 422 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
8c0d577e 423 DPRINTF("ioctl failed %d\n", errno);
151f7749
JK
424 ret = -1;
425 break;
426 }
5832d1f2 427
ffcde12f 428 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
8369e01c 429 start_addr = mem->start_addr + mem->memory_size;
5832d1f2 430 }
7267c094 431 g_free(d.dirty_bitmap);
151f7749
JK
432
433 return ret;
5832d1f2
AL
434}
435
c227f099 436int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
f65ed4c1
AL
437{
438 int ret = -ENOSYS;
f65ed4c1
AL
439 KVMState *s = kvm_state;
440
441 if (s->coalesced_mmio) {
442 struct kvm_coalesced_mmio_zone zone;
443
444 zone.addr = start;
445 zone.size = size;
446
447 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
448 }
f65ed4c1
AL
449
450 return ret;
451}
452
c227f099 453int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
f65ed4c1
AL
454{
455 int ret = -ENOSYS;
f65ed4c1
AL
456 KVMState *s = kvm_state;
457
458 if (s->coalesced_mmio) {
459 struct kvm_coalesced_mmio_zone zone;
460
461 zone.addr = start;
462 zone.size = size;
463
464 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
465 }
f65ed4c1
AL
466
467 return ret;
468}
469
ad7b8b33
AL
470int kvm_check_extension(KVMState *s, unsigned int extension)
471{
472 int ret;
473
474 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
475 if (ret < 0) {
476 ret = 0;
477 }
478
479 return ret;
480}
481
d2f2b8a7
SH
482static int kvm_check_many_ioeventfds(void)
483{
d0dcac83
SH
484 /* Userspace can use ioeventfd for io notification. This requires a host
485 * that supports eventfd(2) and an I/O thread; since eventfd does not
486 * support SIGIO it cannot interrupt the vcpu.
487 *
488 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
d2f2b8a7
SH
489 * can avoid creating too many ioeventfds.
490 */
12d4536f 491#if defined(CONFIG_EVENTFD)
d2f2b8a7
SH
492 int ioeventfds[7];
493 int i, ret = 0;
494 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
495 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
496 if (ioeventfds[i] < 0) {
497 break;
498 }
499 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
500 if (ret < 0) {
501 close(ioeventfds[i]);
502 break;
503 }
504 }
505
506 /* Decide whether many devices are supported or not */
507 ret = i == ARRAY_SIZE(ioeventfds);
508
509 while (i-- > 0) {
510 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
511 close(ioeventfds[i]);
512 }
513 return ret;
514#else
515 return 0;
516#endif
517}
518
94a8d39a
JK
519static const KVMCapabilityInfo *
520kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
521{
522 while (list->name) {
523 if (!kvm_check_extension(s, list->value)) {
524 return list;
525 }
526 list++;
527 }
528 return NULL;
529}
530
a01672d3 531static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
46dbef6a
MT
532{
533 KVMState *s = kvm_state;
46dbef6a
MT
534 KVMSlot *mem, old;
535 int err;
a01672d3
AK
536 MemoryRegion *mr = section->mr;
537 bool log_dirty = memory_region_is_logging(mr);
538 target_phys_addr_t start_addr = section->offset_within_address_space;
539 ram_addr_t size = section->size;
9f213ed9 540 void *ram = NULL;
46dbef6a 541
14542fea
GN
542 /* kvm works in page size chunks, but the function may be called
543 with sub-page size and unaligned start address. */
544 size = TARGET_PAGE_ALIGN(size);
545 start_addr = TARGET_PAGE_ALIGN(start_addr);
46dbef6a 546
a01672d3
AK
547 if (!memory_region_is_ram(mr)) {
548 return;
9f213ed9
AK
549 }
550
a01672d3
AK
551 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region;
552
46dbef6a
MT
553 while (1) {
554 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
555 if (!mem) {
556 break;
557 }
558
a01672d3 559 if (add && start_addr >= mem->start_addr &&
46dbef6a 560 (start_addr + size <= mem->start_addr + mem->memory_size) &&
9f213ed9 561 (ram - start_addr == mem->ram - mem->start_addr)) {
46dbef6a 562 /* The new slot fits into the existing one and comes with
25254bbc
MT
563 * identical parameters - update flags and done. */
564 kvm_slot_dirty_pages_log_change(mem, log_dirty);
46dbef6a
MT
565 return;
566 }
567
568 old = *mem;
569
3fbffb62
AK
570 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
571 kvm_physical_sync_dirty_bitmap(section);
572 }
573
46dbef6a
MT
574 /* unregister the overlapping slot */
575 mem->memory_size = 0;
576 err = kvm_set_user_memory_region(s, mem);
577 if (err) {
578 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
579 __func__, strerror(-err));
580 abort();
581 }
582
583 /* Workaround for older KVM versions: we can't join slots, even not by
584 * unregistering the previous ones and then registering the larger
585 * slot. We have to maintain the existing fragmentation. Sigh.
586 *
587 * This workaround assumes that the new slot starts at the same
588 * address as the first existing one. If not or if some overlapping
589 * slot comes around later, we will fail (not seen in practice so far)
590 * - and actually require a recent KVM version. */
591 if (s->broken_set_mem_region &&
a01672d3 592 old.start_addr == start_addr && old.memory_size < size && add) {
46dbef6a
MT
593 mem = kvm_alloc_slot(s);
594 mem->memory_size = old.memory_size;
595 mem->start_addr = old.start_addr;
9f213ed9 596 mem->ram = old.ram;
25254bbc 597 mem->flags = kvm_mem_flags(s, log_dirty);
46dbef6a
MT
598
599 err = kvm_set_user_memory_region(s, mem);
600 if (err) {
601 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
602 strerror(-err));
603 abort();
604 }
605
606 start_addr += old.memory_size;
9f213ed9 607 ram += old.memory_size;
46dbef6a
MT
608 size -= old.memory_size;
609 continue;
610 }
611
612 /* register prefix slot */
613 if (old.start_addr < start_addr) {
614 mem = kvm_alloc_slot(s);
615 mem->memory_size = start_addr - old.start_addr;
616 mem->start_addr = old.start_addr;
9f213ed9 617 mem->ram = old.ram;
25254bbc 618 mem->flags = kvm_mem_flags(s, log_dirty);
46dbef6a
MT
619
620 err = kvm_set_user_memory_region(s, mem);
621 if (err) {
622 fprintf(stderr, "%s: error registering prefix slot: %s\n",
623 __func__, strerror(-err));
d4d6868f
AG
624#ifdef TARGET_PPC
625 fprintf(stderr, "%s: This is probably because your kernel's " \
626 "PAGE_SIZE is too big. Please try to use 4k " \
627 "PAGE_SIZE!\n", __func__);
628#endif
46dbef6a
MT
629 abort();
630 }
631 }
632
633 /* register suffix slot */
634 if (old.start_addr + old.memory_size > start_addr + size) {
635 ram_addr_t size_delta;
636
637 mem = kvm_alloc_slot(s);
638 mem->start_addr = start_addr + size;
639 size_delta = mem->start_addr - old.start_addr;
640 mem->memory_size = old.memory_size - size_delta;
9f213ed9 641 mem->ram = old.ram + size_delta;
25254bbc 642 mem->flags = kvm_mem_flags(s, log_dirty);
46dbef6a
MT
643
644 err = kvm_set_user_memory_region(s, mem);
645 if (err) {
646 fprintf(stderr, "%s: error registering suffix slot: %s\n",
647 __func__, strerror(-err));
648 abort();
649 }
650 }
651 }
652
653 /* in case the KVM bug workaround already "consumed" the new slot */
a426e122 654 if (!size) {
46dbef6a 655 return;
a426e122 656 }
a01672d3 657 if (!add) {
46dbef6a 658 return;
a426e122 659 }
46dbef6a
MT
660 mem = kvm_alloc_slot(s);
661 mem->memory_size = size;
662 mem->start_addr = start_addr;
9f213ed9 663 mem->ram = ram;
25254bbc 664 mem->flags = kvm_mem_flags(s, log_dirty);
46dbef6a
MT
665
666 err = kvm_set_user_memory_region(s, mem);
667 if (err) {
668 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
669 strerror(-err));
670 abort();
671 }
672}
673
a01672d3
AK
674static void kvm_region_add(MemoryListener *listener,
675 MemoryRegionSection *section)
676{
677 kvm_set_phys_mem(section, true);
678}
679
680static void kvm_region_del(MemoryListener *listener,
681 MemoryRegionSection *section)
682{
683 kvm_set_phys_mem(section, false);
684}
685
686static void kvm_log_sync(MemoryListener *listener,
687 MemoryRegionSection *section)
7b8f3b78 688{
a01672d3
AK
689 int r;
690
ffcde12f 691 r = kvm_physical_sync_dirty_bitmap(section);
a01672d3
AK
692 if (r < 0) {
693 abort();
694 }
7b8f3b78
MT
695}
696
a01672d3 697static void kvm_log_global_start(struct MemoryListener *listener)
7b8f3b78 698{
a01672d3
AK
699 int r;
700
701 r = kvm_set_migration_log(1);
702 assert(r >= 0);
7b8f3b78
MT
703}
704
a01672d3 705static void kvm_log_global_stop(struct MemoryListener *listener)
7b8f3b78 706{
a01672d3
AK
707 int r;
708
709 r = kvm_set_migration_log(0);
710 assert(r >= 0);
7b8f3b78
MT
711}
712
a01672d3
AK
713static MemoryListener kvm_memory_listener = {
714 .region_add = kvm_region_add,
715 .region_del = kvm_region_del,
e5896b12
AP
716 .log_start = kvm_log_start,
717 .log_stop = kvm_log_stop,
a01672d3
AK
718 .log_sync = kvm_log_sync,
719 .log_global_start = kvm_log_global_start,
720 .log_global_stop = kvm_log_global_stop,
7b8f3b78
MT
721};
722
aa7f74d1
JK
723static void kvm_handle_interrupt(CPUState *env, int mask)
724{
725 env->interrupt_request |= mask;
726
727 if (!qemu_cpu_is_self(env)) {
728 qemu_cpu_kick(env);
729 }
730}
731
cad1e282 732int kvm_init(void)
05330448 733{
168ccc11
JK
734 static const char upgrade_note[] =
735 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
736 "(see http://sourceforge.net/projects/kvm).\n";
05330448 737 KVMState *s;
94a8d39a 738 const KVMCapabilityInfo *missing_cap;
05330448
AL
739 int ret;
740 int i;
741
7267c094 742 s = g_malloc0(sizeof(KVMState));
05330448 743
e22a25c9 744#ifdef KVM_CAP_SET_GUEST_DEBUG
72cf2d4f 745 QTAILQ_INIT(&s->kvm_sw_breakpoints);
e22a25c9 746#endif
a426e122 747 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
05330448 748 s->slots[i].slot = i;
a426e122 749 }
05330448 750 s->vmfd = -1;
40ff6d7e 751 s->fd = qemu_open("/dev/kvm", O_RDWR);
05330448
AL
752 if (s->fd == -1) {
753 fprintf(stderr, "Could not access KVM kernel module: %m\n");
754 ret = -errno;
755 goto err;
756 }
757
758 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
759 if (ret < KVM_API_VERSION) {
a426e122 760 if (ret > 0) {
05330448 761 ret = -EINVAL;
a426e122 762 }
05330448
AL
763 fprintf(stderr, "kvm version too old\n");
764 goto err;
765 }
766
767 if (ret > KVM_API_VERSION) {
768 ret = -EINVAL;
769 fprintf(stderr, "kvm version not supported\n");
770 goto err;
771 }
772
773 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
0104dcac
AG
774 if (s->vmfd < 0) {
775#ifdef TARGET_S390X
776 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
777 "your host kernel command line\n");
778#endif
db9eae1c 779 ret = s->vmfd;
05330448 780 goto err;
0104dcac 781 }
05330448 782
94a8d39a
JK
783 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
784 if (!missing_cap) {
785 missing_cap =
786 kvm_check_extension_list(s, kvm_arch_required_capabilities);
05330448 787 }
94a8d39a 788 if (missing_cap) {
ad7b8b33 789 ret = -EINVAL;
94a8d39a
JK
790 fprintf(stderr, "kvm does not support %s\n%s",
791 missing_cap->name, upgrade_note);
d85dc283
AL
792 goto err;
793 }
794
ad7b8b33 795 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
f65ed4c1 796
e69917e2 797 s->broken_set_mem_region = 1;
14a09518 798 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
e69917e2
JK
799 if (ret > 0) {
800 s->broken_set_mem_region = 0;
801 }
e69917e2 802
a0fb002c
JK
803#ifdef KVM_CAP_VCPU_EVENTS
804 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
805#endif
806
b0b1d690
JK
807 s->robust_singlestep =
808 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
b0b1d690 809
ff44f1a3
JK
810#ifdef KVM_CAP_DEBUGREGS
811 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
812#endif
813
f1665b21
SY
814#ifdef KVM_CAP_XSAVE
815 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
816#endif
817
f1665b21
SY
818#ifdef KVM_CAP_XCRS
819 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
820#endif
821
cad1e282 822 ret = kvm_arch_init(s);
a426e122 823 if (ret < 0) {
05330448 824 goto err;
a426e122 825 }
05330448
AL
826
827 kvm_state = s;
a01672d3 828 memory_listener_register(&kvm_memory_listener);
05330448 829
d2f2b8a7
SH
830 s->many_ioeventfds = kvm_check_many_ioeventfds();
831
aa7f74d1
JK
832 cpu_interrupt_handler = kvm_handle_interrupt;
833
05330448
AL
834 return 0;
835
836err:
837 if (s) {
db9eae1c 838 if (s->vmfd >= 0) {
05330448 839 close(s->vmfd);
a426e122
JK
840 }
841 if (s->fd != -1) {
05330448 842 close(s->fd);
a426e122 843 }
05330448 844 }
7267c094 845 g_free(s);
05330448
AL
846
847 return ret;
848}
849
b30e93e9
JK
850static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
851 uint32_t count)
05330448
AL
852{
853 int i;
854 uint8_t *ptr = data;
855
856 for (i = 0; i < count; i++) {
857 if (direction == KVM_EXIT_IO_IN) {
858 switch (size) {
859 case 1:
afcea8cb 860 stb_p(ptr, cpu_inb(port));
05330448
AL
861 break;
862 case 2:
afcea8cb 863 stw_p(ptr, cpu_inw(port));
05330448
AL
864 break;
865 case 4:
afcea8cb 866 stl_p(ptr, cpu_inl(port));
05330448
AL
867 break;
868 }
869 } else {
870 switch (size) {
871 case 1:
afcea8cb 872 cpu_outb(port, ldub_p(ptr));
05330448
AL
873 break;
874 case 2:
afcea8cb 875 cpu_outw(port, lduw_p(ptr));
05330448
AL
876 break;
877 case 4:
afcea8cb 878 cpu_outl(port, ldl_p(ptr));
05330448
AL
879 break;
880 }
881 }
882
883 ptr += size;
884 }
05330448
AL
885}
886
73aaec4a 887static int kvm_handle_internal_error(CPUState *env, struct kvm_run *run)
7c80eef8 888{
bb44e0d1 889 fprintf(stderr, "KVM internal error.");
7c80eef8
MT
890 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
891 int i;
892
bb44e0d1 893 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
7c80eef8
MT
894 for (i = 0; i < run->internal.ndata; ++i) {
895 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
896 i, (uint64_t)run->internal.data[i]);
897 }
bb44e0d1
JK
898 } else {
899 fprintf(stderr, "\n");
7c80eef8 900 }
7c80eef8
MT
901 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
902 fprintf(stderr, "emulation failure\n");
a426e122 903 if (!kvm_arch_stop_on_emulation_error(env)) {
f5c848ee 904 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
d73cd8f4 905 return EXCP_INTERRUPT;
a426e122 906 }
7c80eef8
MT
907 }
908 /* FIXME: Should trigger a qmp message to let management know
909 * something went wrong.
910 */
73aaec4a 911 return -1;
7c80eef8 912}
7c80eef8 913
62a2744c 914void kvm_flush_coalesced_mmio_buffer(void)
f65ed4c1 915{
f65ed4c1 916 KVMState *s = kvm_state;
1cae88b9
AK
917
918 if (s->coalesced_flush_in_progress) {
919 return;
920 }
921
922 s->coalesced_flush_in_progress = true;
923
62a2744c
SY
924 if (s->coalesced_mmio_ring) {
925 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
f65ed4c1
AL
926 while (ring->first != ring->last) {
927 struct kvm_coalesced_mmio *ent;
928
929 ent = &ring->coalesced_mmio[ring->first];
930
931 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
85199474 932 smp_wmb();
f65ed4c1
AL
933 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
934 }
935 }
1cae88b9
AK
936
937 s->coalesced_flush_in_progress = false;
f65ed4c1
AL
938}
939
2705d56a 940static void do_kvm_cpu_synchronize_state(void *_env)
4c0960c0 941{
2705d56a
JK
942 CPUState *env = _env;
943
9ded2744 944 if (!env->kvm_vcpu_dirty) {
4c0960c0 945 kvm_arch_get_registers(env);
9ded2744 946 env->kvm_vcpu_dirty = 1;
4c0960c0
AK
947 }
948}
949
2705d56a
JK
950void kvm_cpu_synchronize_state(CPUState *env)
951{
a426e122 952 if (!env->kvm_vcpu_dirty) {
2705d56a 953 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
a426e122 954 }
2705d56a
JK
955}
956
ea375f9a
JK
957void kvm_cpu_synchronize_post_reset(CPUState *env)
958{
959 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
960 env->kvm_vcpu_dirty = 0;
961}
962
963void kvm_cpu_synchronize_post_init(CPUState *env)
964{
965 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
966 env->kvm_vcpu_dirty = 0;
967}
968
05330448
AL
969int kvm_cpu_exec(CPUState *env)
970{
971 struct kvm_run *run = env->kvm_run;
7cbb533f 972 int ret, run_ret;
05330448 973
8c0d577e 974 DPRINTF("kvm_cpu_exec()\n");
05330448 975
99036865 976 if (kvm_arch_process_async_events(env)) {
9ccfac9e 977 env->exit_request = 0;
6792a57b 978 return EXCP_HLT;
9ccfac9e 979 }
0af691d7 980
6792a57b
JK
981 cpu_single_env = env;
982
9ccfac9e 983 do {
9ded2744 984 if (env->kvm_vcpu_dirty) {
ea375f9a 985 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
9ded2744 986 env->kvm_vcpu_dirty = 0;
4c0960c0
AK
987 }
988
8c14c173 989 kvm_arch_pre_run(env, run);
9ccfac9e
JK
990 if (env->exit_request) {
991 DPRINTF("interrupt exit requested\n");
992 /*
993 * KVM requires us to reenter the kernel after IO exits to complete
994 * instruction emulation. This self-signal will ensure that we
995 * leave ASAP again.
996 */
997 qemu_cpu_kick_self();
998 }
273faf1b 999 cpu_single_env = NULL;
d549db5a 1000 qemu_mutex_unlock_iothread();
9ccfac9e 1001
7cbb533f 1002 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
9ccfac9e 1003
d549db5a 1004 qemu_mutex_lock_iothread();
273faf1b 1005 cpu_single_env = env;
05330448
AL
1006 kvm_arch_post_run(env, run);
1007
b0c883b5
JK
1008 kvm_flush_coalesced_mmio_buffer();
1009
7cbb533f 1010 if (run_ret < 0) {
dc77d341
JK
1011 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1012 DPRINTF("io window exit\n");
d73cd8f4 1013 ret = EXCP_INTERRUPT;
dc77d341
JK
1014 break;
1015 }
7b011fbc
ME
1016 fprintf(stderr, "error: kvm run failed %s\n",
1017 strerror(-run_ret));
05330448
AL
1018 abort();
1019 }
1020
05330448
AL
1021 switch (run->exit_reason) {
1022 case KVM_EXIT_IO:
8c0d577e 1023 DPRINTF("handle_io\n");
b30e93e9
JK
1024 kvm_handle_io(run->io.port,
1025 (uint8_t *)run + run->io.data_offset,
1026 run->io.direction,
1027 run->io.size,
1028 run->io.count);
d73cd8f4 1029 ret = 0;
05330448
AL
1030 break;
1031 case KVM_EXIT_MMIO:
8c0d577e 1032 DPRINTF("handle_mmio\n");
05330448
AL
1033 cpu_physical_memory_rw(run->mmio.phys_addr,
1034 run->mmio.data,
1035 run->mmio.len,
1036 run->mmio.is_write);
d73cd8f4 1037 ret = 0;
05330448
AL
1038 break;
1039 case KVM_EXIT_IRQ_WINDOW_OPEN:
8c0d577e 1040 DPRINTF("irq_window_open\n");
d73cd8f4 1041 ret = EXCP_INTERRUPT;
05330448
AL
1042 break;
1043 case KVM_EXIT_SHUTDOWN:
8c0d577e 1044 DPRINTF("shutdown\n");
05330448 1045 qemu_system_reset_request();
d73cd8f4 1046 ret = EXCP_INTERRUPT;
05330448
AL
1047 break;
1048 case KVM_EXIT_UNKNOWN:
bb44e0d1
JK
1049 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1050 (uint64_t)run->hw.hardware_exit_reason);
73aaec4a 1051 ret = -1;
05330448 1052 break;
7c80eef8 1053 case KVM_EXIT_INTERNAL_ERROR:
73aaec4a 1054 ret = kvm_handle_internal_error(env, run);
7c80eef8 1055 break;
05330448 1056 default:
8c0d577e 1057 DPRINTF("kvm_arch_handle_exit\n");
05330448
AL
1058 ret = kvm_arch_handle_exit(env, run);
1059 break;
1060 }
d73cd8f4 1061 } while (ret == 0);
05330448 1062
73aaec4a 1063 if (ret < 0) {
f5c848ee 1064 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
0461d5a6 1065 vm_stop(RUN_STATE_INTERNAL_ERROR);
becfc390
AL
1066 }
1067
6792a57b
JK
1068 env->exit_request = 0;
1069 cpu_single_env = NULL;
05330448
AL
1070 return ret;
1071}
1072
984b5181 1073int kvm_ioctl(KVMState *s, int type, ...)
05330448
AL
1074{
1075 int ret;
984b5181
AL
1076 void *arg;
1077 va_list ap;
05330448 1078
984b5181
AL
1079 va_start(ap, type);
1080 arg = va_arg(ap, void *);
1081 va_end(ap);
1082
1083 ret = ioctl(s->fd, type, arg);
a426e122 1084 if (ret == -1) {
05330448 1085 ret = -errno;
a426e122 1086 }
05330448
AL
1087 return ret;
1088}
1089
984b5181 1090int kvm_vm_ioctl(KVMState *s, int type, ...)
05330448
AL
1091{
1092 int ret;
984b5181
AL
1093 void *arg;
1094 va_list ap;
1095
1096 va_start(ap, type);
1097 arg = va_arg(ap, void *);
1098 va_end(ap);
05330448 1099
984b5181 1100 ret = ioctl(s->vmfd, type, arg);
a426e122 1101 if (ret == -1) {
05330448 1102 ret = -errno;
a426e122 1103 }
05330448
AL
1104 return ret;
1105}
1106
984b5181 1107int kvm_vcpu_ioctl(CPUState *env, int type, ...)
05330448
AL
1108{
1109 int ret;
984b5181
AL
1110 void *arg;
1111 va_list ap;
1112
1113 va_start(ap, type);
1114 arg = va_arg(ap, void *);
1115 va_end(ap);
05330448 1116
984b5181 1117 ret = ioctl(env->kvm_fd, type, arg);
a426e122 1118 if (ret == -1) {
05330448 1119 ret = -errno;
a426e122 1120 }
05330448
AL
1121 return ret;
1122}
bd322087
AL
1123
1124int kvm_has_sync_mmu(void)
1125{
94a8d39a 1126 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
bd322087 1127}
e22a25c9 1128
a0fb002c
JK
1129int kvm_has_vcpu_events(void)
1130{
1131 return kvm_state->vcpu_events;
1132}
1133
b0b1d690
JK
1134int kvm_has_robust_singlestep(void)
1135{
1136 return kvm_state->robust_singlestep;
1137}
1138
ff44f1a3
JK
1139int kvm_has_debugregs(void)
1140{
1141 return kvm_state->debugregs;
1142}
1143
f1665b21
SY
1144int kvm_has_xsave(void)
1145{
1146 return kvm_state->xsave;
1147}
1148
1149int kvm_has_xcrs(void)
1150{
1151 return kvm_state->xcrs;
1152}
1153
d2f2b8a7
SH
1154int kvm_has_many_ioeventfds(void)
1155{
1156 if (!kvm_enabled()) {
1157 return 0;
1158 }
1159 return kvm_state->many_ioeventfds;
1160}
1161
6f0437e8
JK
1162void kvm_setup_guest_memory(void *start, size_t size)
1163{
1164 if (!kvm_has_sync_mmu()) {
e78815a5 1165 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
6f0437e8
JK
1166
1167 if (ret) {
e78815a5
AF
1168 perror("qemu_madvise");
1169 fprintf(stderr,
1170 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
6f0437e8
JK
1171 exit(1);
1172 }
6f0437e8
JK
1173 }
1174}
1175
e22a25c9
AL
1176#ifdef KVM_CAP_SET_GUEST_DEBUG
1177struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
1178 target_ulong pc)
1179{
1180 struct kvm_sw_breakpoint *bp;
1181
72cf2d4f 1182 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
a426e122 1183 if (bp->pc == pc) {
e22a25c9 1184 return bp;
a426e122 1185 }
e22a25c9
AL
1186 }
1187 return NULL;
1188}
1189
1190int kvm_sw_breakpoints_active(CPUState *env)
1191{
72cf2d4f 1192 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
e22a25c9
AL
1193}
1194
452e4751
GC
1195struct kvm_set_guest_debug_data {
1196 struct kvm_guest_debug dbg;
1197 CPUState *env;
1198 int err;
1199};
1200
1201static void kvm_invoke_set_guest_debug(void *data)
1202{
1203 struct kvm_set_guest_debug_data *dbg_data = data;
b3807725
JK
1204 CPUState *env = dbg_data->env;
1205
b3807725 1206 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
452e4751
GC
1207}
1208
e22a25c9
AL
1209int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1210{
452e4751 1211 struct kvm_set_guest_debug_data data;
e22a25c9 1212
b0b1d690 1213 data.dbg.control = reinject_trap;
e22a25c9 1214
b0b1d690
JK
1215 if (env->singlestep_enabled) {
1216 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1217 }
452e4751 1218 kvm_arch_update_guest_debug(env, &data.dbg);
452e4751 1219 data.env = env;
e22a25c9 1220
be41cbe0 1221 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
452e4751 1222 return data.err;
e22a25c9
AL
1223}
1224
1225int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1226 target_ulong len, int type)
1227{
1228 struct kvm_sw_breakpoint *bp;
1229 CPUState *env;
1230 int err;
1231
1232 if (type == GDB_BREAKPOINT_SW) {
1233 bp = kvm_find_sw_breakpoint(current_env, addr);
1234 if (bp) {
1235 bp->use_count++;
1236 return 0;
1237 }
1238
7267c094 1239 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
a426e122 1240 if (!bp) {
e22a25c9 1241 return -ENOMEM;
a426e122 1242 }
e22a25c9
AL
1243
1244 bp->pc = addr;
1245 bp->use_count = 1;
1246 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1247 if (err) {
7267c094 1248 g_free(bp);
e22a25c9
AL
1249 return err;
1250 }
1251
72cf2d4f 1252 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
e22a25c9
AL
1253 bp, entry);
1254 } else {
1255 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
a426e122 1256 if (err) {
e22a25c9 1257 return err;
a426e122 1258 }
e22a25c9
AL
1259 }
1260
1261 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1262 err = kvm_update_guest_debug(env, 0);
a426e122 1263 if (err) {
e22a25c9 1264 return err;
a426e122 1265 }
e22a25c9
AL
1266 }
1267 return 0;
1268}
1269
1270int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1271 target_ulong len, int type)
1272{
1273 struct kvm_sw_breakpoint *bp;
1274 CPUState *env;
1275 int err;
1276
1277 if (type == GDB_BREAKPOINT_SW) {
1278 bp = kvm_find_sw_breakpoint(current_env, addr);
a426e122 1279 if (!bp) {
e22a25c9 1280 return -ENOENT;
a426e122 1281 }
e22a25c9
AL
1282
1283 if (bp->use_count > 1) {
1284 bp->use_count--;
1285 return 0;
1286 }
1287
1288 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
a426e122 1289 if (err) {
e22a25c9 1290 return err;
a426e122 1291 }
e22a25c9 1292
72cf2d4f 1293 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
7267c094 1294 g_free(bp);
e22a25c9
AL
1295 } else {
1296 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
a426e122 1297 if (err) {
e22a25c9 1298 return err;
a426e122 1299 }
e22a25c9
AL
1300 }
1301
1302 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1303 err = kvm_update_guest_debug(env, 0);
a426e122 1304 if (err) {
e22a25c9 1305 return err;
a426e122 1306 }
e22a25c9
AL
1307 }
1308 return 0;
1309}
1310
1311void kvm_remove_all_breakpoints(CPUState *current_env)
1312{
1313 struct kvm_sw_breakpoint *bp, *next;
1314 KVMState *s = current_env->kvm_state;
1315 CPUState *env;
1316
72cf2d4f 1317 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
e22a25c9
AL
1318 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1319 /* Try harder to find a CPU that currently sees the breakpoint. */
1320 for (env = first_cpu; env != NULL; env = env->next_cpu) {
a426e122 1321 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
e22a25c9 1322 break;
a426e122 1323 }
e22a25c9
AL
1324 }
1325 }
1326 }
1327 kvm_arch_remove_all_hw_breakpoints();
1328
a426e122 1329 for (env = first_cpu; env != NULL; env = env->next_cpu) {
e22a25c9 1330 kvm_update_guest_debug(env, 0);
a426e122 1331 }
e22a25c9
AL
1332}
1333
1334#else /* !KVM_CAP_SET_GUEST_DEBUG */
1335
1336int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1337{
1338 return -EINVAL;
1339}
1340
1341int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1342 target_ulong len, int type)
1343{
1344 return -EINVAL;
1345}
1346
1347int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1348 target_ulong len, int type)
1349{
1350 return -EINVAL;
1351}
1352
1353void kvm_remove_all_breakpoints(CPUState *current_env)
1354{
1355}
1356#endif /* !KVM_CAP_SET_GUEST_DEBUG */
cc84de95
MT
1357
1358int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset)
1359{
1360 struct kvm_signal_mask *sigmask;
1361 int r;
1362
a426e122 1363 if (!sigset) {
cc84de95 1364 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
a426e122 1365 }
cc84de95 1366
7267c094 1367 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
cc84de95
MT
1368
1369 sigmask->len = 8;
1370 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1371 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
7267c094 1372 g_free(sigmask);
cc84de95
MT
1373
1374 return r;
1375}
ca821806 1376
44f1a3d8
CM
1377int kvm_set_ioeventfd_mmio_long(int fd, uint32_t addr, uint32_t val, bool assign)
1378{
44f1a3d8
CM
1379 int ret;
1380 struct kvm_ioeventfd iofd;
1381
1382 iofd.datamatch = val;
1383 iofd.addr = addr;
1384 iofd.len = 4;
1385 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1386 iofd.fd = fd;
1387
1388 if (!kvm_enabled()) {
1389 return -ENOSYS;
1390 }
1391
1392 if (!assign) {
1393 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1394 }
1395
1396 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1397
1398 if (ret < 0) {
1399 return -errno;
1400 }
1401
1402 return 0;
44f1a3d8
CM
1403}
1404
ca821806
MT
1405int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1406{
1407 struct kvm_ioeventfd kick = {
1408 .datamatch = val,
1409 .addr = addr,
1410 .len = 2,
1411 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1412 .fd = fd,
1413 };
1414 int r;
a426e122 1415 if (!kvm_enabled()) {
ca821806 1416 return -ENOSYS;
a426e122
JK
1417 }
1418 if (!assign) {
ca821806 1419 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
a426e122 1420 }
ca821806 1421 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
a426e122 1422 if (r < 0) {
ca821806 1423 return r;
a426e122 1424 }
ca821806 1425 return 0;
98c8573e 1426}
a1b87fe0
JK
1427
1428int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr)
1429{
1430 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1431}
1432
1433int kvm_on_sigbus(int code, void *addr)
1434{
1435 return kvm_arch_on_sigbus(code, addr);
1436}