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