Merge remote-tracking branch 'qemu-kvm/uq/master' into staging
[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);
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 cpu_physical_memory_set_dirty().
383 * This means all bits are set to dirty.
384 *
385 * @start_add: start of logged region.
386 * @end_addr: end of logged region.
387 */
388 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
389 {
390 KVMState *s = kvm_state;
391 unsigned long size, allocated_size = 0;
392 KVMDirtyLog d;
393 KVMSlot *mem;
394 int ret = 0;
395 target_phys_addr_t start_addr = section->offset_within_address_space;
396 target_phys_addr_t end_addr = start_addr + section->size;
397
398 d.dirty_bitmap = NULL;
399 while (start_addr < end_addr) {
400 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
401 if (mem == NULL) {
402 break;
403 }
404
405 /* XXX bad kernel interface alert
406 * For dirty bitmap, kernel allocates array of size aligned to
407 * bits-per-long. But for case when the kernel is 64bits and
408 * the userspace is 32bits, userspace can't align to the same
409 * bits-per-long, since sizeof(long) is different between kernel
410 * and user space. This way, userspace will provide buffer which
411 * may be 4 bytes less than the kernel will use, resulting in
412 * userspace memory corruption (which is not detectable by valgrind
413 * too, in most cases).
414 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
415 * a hope that sizeof(long) wont become >8 any time soon.
416 */
417 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
418 /*HOST_LONG_BITS*/ 64) / 8;
419 if (!d.dirty_bitmap) {
420 d.dirty_bitmap = g_malloc(size);
421 } else if (size > allocated_size) {
422 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
423 }
424 allocated_size = size;
425 memset(d.dirty_bitmap, 0, allocated_size);
426
427 d.slot = mem->slot;
428
429 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
430 DPRINTF("ioctl failed %d\n", errno);
431 ret = -1;
432 break;
433 }
434
435 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
436 start_addr = mem->start_addr + mem->memory_size;
437 }
438 g_free(d.dirty_bitmap);
439
440 return ret;
441 }
442
443 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
444 {
445 int ret = -ENOSYS;
446 KVMState *s = kvm_state;
447
448 if (s->coalesced_mmio) {
449 struct kvm_coalesced_mmio_zone zone;
450
451 zone.addr = start;
452 zone.size = size;
453
454 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
455 }
456
457 return ret;
458 }
459
460 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
461 {
462 int ret = -ENOSYS;
463 KVMState *s = kvm_state;
464
465 if (s->coalesced_mmio) {
466 struct kvm_coalesced_mmio_zone zone;
467
468 zone.addr = start;
469 zone.size = size;
470
471 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
472 }
473
474 return ret;
475 }
476
477 int kvm_check_extension(KVMState *s, unsigned int extension)
478 {
479 int ret;
480
481 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
482 if (ret < 0) {
483 ret = 0;
484 }
485
486 return ret;
487 }
488
489 static int kvm_check_many_ioeventfds(void)
490 {
491 /* Userspace can use ioeventfd for io notification. This requires a host
492 * that supports eventfd(2) and an I/O thread; since eventfd does not
493 * support SIGIO it cannot interrupt the vcpu.
494 *
495 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
496 * can avoid creating too many ioeventfds.
497 */
498 #if defined(CONFIG_EVENTFD)
499 int ioeventfds[7];
500 int i, ret = 0;
501 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
502 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
503 if (ioeventfds[i] < 0) {
504 break;
505 }
506 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
507 if (ret < 0) {
508 close(ioeventfds[i]);
509 break;
510 }
511 }
512
513 /* Decide whether many devices are supported or not */
514 ret = i == ARRAY_SIZE(ioeventfds);
515
516 while (i-- > 0) {
517 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
518 close(ioeventfds[i]);
519 }
520 return ret;
521 #else
522 return 0;
523 #endif
524 }
525
526 static const KVMCapabilityInfo *
527 kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
528 {
529 while (list->name) {
530 if (!kvm_check_extension(s, list->value)) {
531 return list;
532 }
533 list++;
534 }
535 return NULL;
536 }
537
538 static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
539 {
540 KVMState *s = kvm_state;
541 KVMSlot *mem, old;
542 int err;
543 MemoryRegion *mr = section->mr;
544 bool log_dirty = memory_region_is_logging(mr);
545 target_phys_addr_t start_addr = section->offset_within_address_space;
546 ram_addr_t size = section->size;
547 void *ram = NULL;
548
549 /* kvm works in page size chunks, but the function may be called
550 with sub-page size and unaligned start address. */
551 size = TARGET_PAGE_ALIGN(size);
552 start_addr = TARGET_PAGE_ALIGN(start_addr);
553
554 if (!memory_region_is_ram(mr)) {
555 return;
556 }
557
558 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region;
559
560 while (1) {
561 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
562 if (!mem) {
563 break;
564 }
565
566 if (add && start_addr >= mem->start_addr &&
567 (start_addr + size <= mem->start_addr + mem->memory_size) &&
568 (ram - start_addr == mem->ram - mem->start_addr)) {
569 /* The new slot fits into the existing one and comes with
570 * identical parameters - update flags and done. */
571 kvm_slot_dirty_pages_log_change(mem, log_dirty);
572 return;
573 }
574
575 old = *mem;
576
577 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
578 kvm_physical_sync_dirty_bitmap(section);
579 }
580
581 /* unregister the overlapping slot */
582 mem->memory_size = 0;
583 err = kvm_set_user_memory_region(s, mem);
584 if (err) {
585 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
586 __func__, strerror(-err));
587 abort();
588 }
589
590 /* Workaround for older KVM versions: we can't join slots, even not by
591 * unregistering the previous ones and then registering the larger
592 * slot. We have to maintain the existing fragmentation. Sigh.
593 *
594 * This workaround assumes that the new slot starts at the same
595 * address as the first existing one. If not or if some overlapping
596 * slot comes around later, we will fail (not seen in practice so far)
597 * - and actually require a recent KVM version. */
598 if (s->broken_set_mem_region &&
599 old.start_addr == start_addr && old.memory_size < size && add) {
600 mem = kvm_alloc_slot(s);
601 mem->memory_size = old.memory_size;
602 mem->start_addr = old.start_addr;
603 mem->ram = old.ram;
604 mem->flags = kvm_mem_flags(s, log_dirty);
605
606 err = kvm_set_user_memory_region(s, mem);
607 if (err) {
608 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
609 strerror(-err));
610 abort();
611 }
612
613 start_addr += old.memory_size;
614 ram += old.memory_size;
615 size -= old.memory_size;
616 continue;
617 }
618
619 /* register prefix slot */
620 if (old.start_addr < start_addr) {
621 mem = kvm_alloc_slot(s);
622 mem->memory_size = start_addr - old.start_addr;
623 mem->start_addr = old.start_addr;
624 mem->ram = old.ram;
625 mem->flags = kvm_mem_flags(s, log_dirty);
626
627 err = kvm_set_user_memory_region(s, mem);
628 if (err) {
629 fprintf(stderr, "%s: error registering prefix slot: %s\n",
630 __func__, strerror(-err));
631 #ifdef TARGET_PPC
632 fprintf(stderr, "%s: This is probably because your kernel's " \
633 "PAGE_SIZE is too big. Please try to use 4k " \
634 "PAGE_SIZE!\n", __func__);
635 #endif
636 abort();
637 }
638 }
639
640 /* register suffix slot */
641 if (old.start_addr + old.memory_size > start_addr + size) {
642 ram_addr_t size_delta;
643
644 mem = kvm_alloc_slot(s);
645 mem->start_addr = start_addr + size;
646 size_delta = mem->start_addr - old.start_addr;
647 mem->memory_size = old.memory_size - size_delta;
648 mem->ram = old.ram + size_delta;
649 mem->flags = kvm_mem_flags(s, log_dirty);
650
651 err = kvm_set_user_memory_region(s, mem);
652 if (err) {
653 fprintf(stderr, "%s: error registering suffix slot: %s\n",
654 __func__, strerror(-err));
655 abort();
656 }
657 }
658 }
659
660 /* in case the KVM bug workaround already "consumed" the new slot */
661 if (!size) {
662 return;
663 }
664 if (!add) {
665 return;
666 }
667 mem = kvm_alloc_slot(s);
668 mem->memory_size = size;
669 mem->start_addr = start_addr;
670 mem->ram = ram;
671 mem->flags = kvm_mem_flags(s, log_dirty);
672
673 err = kvm_set_user_memory_region(s, mem);
674 if (err) {
675 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
676 strerror(-err));
677 abort();
678 }
679 }
680
681 static void kvm_region_add(MemoryListener *listener,
682 MemoryRegionSection *section)
683 {
684 kvm_set_phys_mem(section, true);
685 }
686
687 static void kvm_region_del(MemoryListener *listener,
688 MemoryRegionSection *section)
689 {
690 kvm_set_phys_mem(section, false);
691 }
692
693 static void kvm_log_sync(MemoryListener *listener,
694 MemoryRegionSection *section)
695 {
696 int r;
697
698 r = kvm_physical_sync_dirty_bitmap(section);
699 if (r < 0) {
700 abort();
701 }
702 }
703
704 static void kvm_log_global_start(struct MemoryListener *listener)
705 {
706 int r;
707
708 r = kvm_set_migration_log(1);
709 assert(r >= 0);
710 }
711
712 static void kvm_log_global_stop(struct MemoryListener *listener)
713 {
714 int r;
715
716 r = kvm_set_migration_log(0);
717 assert(r >= 0);
718 }
719
720 static MemoryListener kvm_memory_listener = {
721 .region_add = kvm_region_add,
722 .region_del = kvm_region_del,
723 .log_start = kvm_log_start,
724 .log_stop = kvm_log_stop,
725 .log_sync = kvm_log_sync,
726 .log_global_start = kvm_log_global_start,
727 .log_global_stop = kvm_log_global_stop,
728 };
729
730 static void kvm_handle_interrupt(CPUState *env, int mask)
731 {
732 env->interrupt_request |= mask;
733
734 if (!qemu_cpu_is_self(env)) {
735 qemu_cpu_kick(env);
736 }
737 }
738
739 int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
740 {
741 struct kvm_irq_level event;
742 int ret;
743
744 assert(s->irqchip_in_kernel);
745
746 event.level = level;
747 event.irq = irq;
748 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
749 if (ret < 0) {
750 perror("kvm_set_irqchip_line");
751 abort();
752 }
753
754 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
755 }
756
757 #ifdef KVM_CAP_IRQ_ROUTING
758 static void set_gsi(KVMState *s, unsigned int gsi)
759 {
760 assert(gsi < s->max_gsi);
761
762 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
763 }
764
765 static void kvm_init_irq_routing(KVMState *s)
766 {
767 int gsi_count;
768
769 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
770 if (gsi_count > 0) {
771 unsigned int gsi_bits, i;
772
773 /* Round up so we can search ints using ffs */
774 gsi_bits = (gsi_count + 31) / 32;
775 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
776 s->max_gsi = gsi_bits;
777
778 /* Mark any over-allocated bits as already in use */
779 for (i = gsi_count; i < gsi_bits; i++) {
780 set_gsi(s, i);
781 }
782 }
783
784 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
785 s->nr_allocated_irq_routes = 0;
786
787 kvm_arch_init_irq_routing(s);
788 }
789
790 static void kvm_add_routing_entry(KVMState *s,
791 struct kvm_irq_routing_entry *entry)
792 {
793 struct kvm_irq_routing_entry *new;
794 int n, size;
795
796 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
797 n = s->nr_allocated_irq_routes * 2;
798 if (n < 64) {
799 n = 64;
800 }
801 size = sizeof(struct kvm_irq_routing);
802 size += n * sizeof(*new);
803 s->irq_routes = g_realloc(s->irq_routes, size);
804 s->nr_allocated_irq_routes = n;
805 }
806 n = s->irq_routes->nr++;
807 new = &s->irq_routes->entries[n];
808 memset(new, 0, sizeof(*new));
809 new->gsi = entry->gsi;
810 new->type = entry->type;
811 new->flags = entry->flags;
812 new->u = entry->u;
813
814 set_gsi(s, entry->gsi);
815 }
816
817 void kvm_irqchip_add_route(KVMState *s, int irq, int irqchip, int pin)
818 {
819 struct kvm_irq_routing_entry e;
820
821 e.gsi = irq;
822 e.type = KVM_IRQ_ROUTING_IRQCHIP;
823 e.flags = 0;
824 e.u.irqchip.irqchip = irqchip;
825 e.u.irqchip.pin = pin;
826 kvm_add_routing_entry(s, &e);
827 }
828
829 int kvm_irqchip_commit_routes(KVMState *s)
830 {
831 s->irq_routes->flags = 0;
832 return kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
833 }
834
835 #else /* !KVM_CAP_IRQ_ROUTING */
836
837 static void kvm_init_irq_routing(KVMState *s)
838 {
839 }
840 #endif /* !KVM_CAP_IRQ_ROUTING */
841
842 static int kvm_irqchip_create(KVMState *s)
843 {
844 QemuOptsList *list = qemu_find_opts("machine");
845 int ret;
846
847 if (QTAILQ_EMPTY(&list->head) ||
848 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
849 "kernel_irqchip", false) ||
850 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
851 return 0;
852 }
853
854 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
855 if (ret < 0) {
856 fprintf(stderr, "Create kernel irqchip failed\n");
857 return ret;
858 }
859
860 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
861 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
862 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
863 }
864 s->irqchip_in_kernel = 1;
865
866 kvm_init_irq_routing(s);
867
868 return 0;
869 }
870
871 int kvm_init(void)
872 {
873 static const char upgrade_note[] =
874 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
875 "(see http://sourceforge.net/projects/kvm).\n";
876 KVMState *s;
877 const KVMCapabilityInfo *missing_cap;
878 int ret;
879 int i;
880
881 s = g_malloc0(sizeof(KVMState));
882
883 #ifdef KVM_CAP_SET_GUEST_DEBUG
884 QTAILQ_INIT(&s->kvm_sw_breakpoints);
885 #endif
886 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
887 s->slots[i].slot = i;
888 }
889 s->vmfd = -1;
890 s->fd = qemu_open("/dev/kvm", O_RDWR);
891 if (s->fd == -1) {
892 fprintf(stderr, "Could not access KVM kernel module: %m\n");
893 ret = -errno;
894 goto err;
895 }
896
897 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
898 if (ret < KVM_API_VERSION) {
899 if (ret > 0) {
900 ret = -EINVAL;
901 }
902 fprintf(stderr, "kvm version too old\n");
903 goto err;
904 }
905
906 if (ret > KVM_API_VERSION) {
907 ret = -EINVAL;
908 fprintf(stderr, "kvm version not supported\n");
909 goto err;
910 }
911
912 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
913 if (s->vmfd < 0) {
914 #ifdef TARGET_S390X
915 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
916 "your host kernel command line\n");
917 #endif
918 ret = s->vmfd;
919 goto err;
920 }
921
922 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
923 if (!missing_cap) {
924 missing_cap =
925 kvm_check_extension_list(s, kvm_arch_required_capabilities);
926 }
927 if (missing_cap) {
928 ret = -EINVAL;
929 fprintf(stderr, "kvm does not support %s\n%s",
930 missing_cap->name, upgrade_note);
931 goto err;
932 }
933
934 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
935
936 s->broken_set_mem_region = 1;
937 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
938 if (ret > 0) {
939 s->broken_set_mem_region = 0;
940 }
941
942 #ifdef KVM_CAP_VCPU_EVENTS
943 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
944 #endif
945
946 s->robust_singlestep =
947 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
948
949 #ifdef KVM_CAP_DEBUGREGS
950 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
951 #endif
952
953 #ifdef KVM_CAP_XSAVE
954 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
955 #endif
956
957 #ifdef KVM_CAP_XCRS
958 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
959 #endif
960
961 ret = kvm_arch_init(s);
962 if (ret < 0) {
963 goto err;
964 }
965
966 ret = kvm_irqchip_create(s);
967 if (ret < 0) {
968 goto err;
969 }
970
971 kvm_state = s;
972 memory_listener_register(&kvm_memory_listener);
973
974 s->many_ioeventfds = kvm_check_many_ioeventfds();
975
976 cpu_interrupt_handler = kvm_handle_interrupt;
977
978 return 0;
979
980 err:
981 if (s) {
982 if (s->vmfd >= 0) {
983 close(s->vmfd);
984 }
985 if (s->fd != -1) {
986 close(s->fd);
987 }
988 }
989 g_free(s);
990
991 return ret;
992 }
993
994 static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
995 uint32_t count)
996 {
997 int i;
998 uint8_t *ptr = data;
999
1000 for (i = 0; i < count; i++) {
1001 if (direction == KVM_EXIT_IO_IN) {
1002 switch (size) {
1003 case 1:
1004 stb_p(ptr, cpu_inb(port));
1005 break;
1006 case 2:
1007 stw_p(ptr, cpu_inw(port));
1008 break;
1009 case 4:
1010 stl_p(ptr, cpu_inl(port));
1011 break;
1012 }
1013 } else {
1014 switch (size) {
1015 case 1:
1016 cpu_outb(port, ldub_p(ptr));
1017 break;
1018 case 2:
1019 cpu_outw(port, lduw_p(ptr));
1020 break;
1021 case 4:
1022 cpu_outl(port, ldl_p(ptr));
1023 break;
1024 }
1025 }
1026
1027 ptr += size;
1028 }
1029 }
1030
1031 static int kvm_handle_internal_error(CPUState *env, struct kvm_run *run)
1032 {
1033 fprintf(stderr, "KVM internal error.");
1034 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1035 int i;
1036
1037 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
1038 for (i = 0; i < run->internal.ndata; ++i) {
1039 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1040 i, (uint64_t)run->internal.data[i]);
1041 }
1042 } else {
1043 fprintf(stderr, "\n");
1044 }
1045 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1046 fprintf(stderr, "emulation failure\n");
1047 if (!kvm_arch_stop_on_emulation_error(env)) {
1048 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1049 return EXCP_INTERRUPT;
1050 }
1051 }
1052 /* FIXME: Should trigger a qmp message to let management know
1053 * something went wrong.
1054 */
1055 return -1;
1056 }
1057
1058 void kvm_flush_coalesced_mmio_buffer(void)
1059 {
1060 KVMState *s = kvm_state;
1061
1062 if (s->coalesced_flush_in_progress) {
1063 return;
1064 }
1065
1066 s->coalesced_flush_in_progress = true;
1067
1068 if (s->coalesced_mmio_ring) {
1069 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
1070 while (ring->first != ring->last) {
1071 struct kvm_coalesced_mmio *ent;
1072
1073 ent = &ring->coalesced_mmio[ring->first];
1074
1075 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
1076 smp_wmb();
1077 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1078 }
1079 }
1080
1081 s->coalesced_flush_in_progress = false;
1082 }
1083
1084 static void do_kvm_cpu_synchronize_state(void *_env)
1085 {
1086 CPUState *env = _env;
1087
1088 if (!env->kvm_vcpu_dirty) {
1089 kvm_arch_get_registers(env);
1090 env->kvm_vcpu_dirty = 1;
1091 }
1092 }
1093
1094 void kvm_cpu_synchronize_state(CPUState *env)
1095 {
1096 if (!env->kvm_vcpu_dirty) {
1097 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
1098 }
1099 }
1100
1101 void kvm_cpu_synchronize_post_reset(CPUState *env)
1102 {
1103 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1104 env->kvm_vcpu_dirty = 0;
1105 }
1106
1107 void kvm_cpu_synchronize_post_init(CPUState *env)
1108 {
1109 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1110 env->kvm_vcpu_dirty = 0;
1111 }
1112
1113 int kvm_cpu_exec(CPUState *env)
1114 {
1115 struct kvm_run *run = env->kvm_run;
1116 int ret, run_ret;
1117
1118 DPRINTF("kvm_cpu_exec()\n");
1119
1120 if (kvm_arch_process_async_events(env)) {
1121 env->exit_request = 0;
1122 return EXCP_HLT;
1123 }
1124
1125 cpu_single_env = env;
1126
1127 do {
1128 if (env->kvm_vcpu_dirty) {
1129 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
1130 env->kvm_vcpu_dirty = 0;
1131 }
1132
1133 kvm_arch_pre_run(env, run);
1134 if (env->exit_request) {
1135 DPRINTF("interrupt exit requested\n");
1136 /*
1137 * KVM requires us to reenter the kernel after IO exits to complete
1138 * instruction emulation. This self-signal will ensure that we
1139 * leave ASAP again.
1140 */
1141 qemu_cpu_kick_self();
1142 }
1143 cpu_single_env = NULL;
1144 qemu_mutex_unlock_iothread();
1145
1146 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
1147
1148 qemu_mutex_lock_iothread();
1149 cpu_single_env = env;
1150 kvm_arch_post_run(env, run);
1151
1152 kvm_flush_coalesced_mmio_buffer();
1153
1154 if (run_ret < 0) {
1155 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1156 DPRINTF("io window exit\n");
1157 ret = EXCP_INTERRUPT;
1158 break;
1159 }
1160 fprintf(stderr, "error: kvm run failed %s\n",
1161 strerror(-run_ret));
1162 abort();
1163 }
1164
1165 switch (run->exit_reason) {
1166 case KVM_EXIT_IO:
1167 DPRINTF("handle_io\n");
1168 kvm_handle_io(run->io.port,
1169 (uint8_t *)run + run->io.data_offset,
1170 run->io.direction,
1171 run->io.size,
1172 run->io.count);
1173 ret = 0;
1174 break;
1175 case KVM_EXIT_MMIO:
1176 DPRINTF("handle_mmio\n");
1177 cpu_physical_memory_rw(run->mmio.phys_addr,
1178 run->mmio.data,
1179 run->mmio.len,
1180 run->mmio.is_write);
1181 ret = 0;
1182 break;
1183 case KVM_EXIT_IRQ_WINDOW_OPEN:
1184 DPRINTF("irq_window_open\n");
1185 ret = EXCP_INTERRUPT;
1186 break;
1187 case KVM_EXIT_SHUTDOWN:
1188 DPRINTF("shutdown\n");
1189 qemu_system_reset_request();
1190 ret = EXCP_INTERRUPT;
1191 break;
1192 case KVM_EXIT_UNKNOWN:
1193 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1194 (uint64_t)run->hw.hardware_exit_reason);
1195 ret = -1;
1196 break;
1197 case KVM_EXIT_INTERNAL_ERROR:
1198 ret = kvm_handle_internal_error(env, run);
1199 break;
1200 default:
1201 DPRINTF("kvm_arch_handle_exit\n");
1202 ret = kvm_arch_handle_exit(env, run);
1203 break;
1204 }
1205 } while (ret == 0);
1206
1207 if (ret < 0) {
1208 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1209 vm_stop(RUN_STATE_INTERNAL_ERROR);
1210 }
1211
1212 env->exit_request = 0;
1213 cpu_single_env = NULL;
1214 return ret;
1215 }
1216
1217 int kvm_ioctl(KVMState *s, int type, ...)
1218 {
1219 int ret;
1220 void *arg;
1221 va_list ap;
1222
1223 va_start(ap, type);
1224 arg = va_arg(ap, void *);
1225 va_end(ap);
1226
1227 ret = ioctl(s->fd, type, arg);
1228 if (ret == -1) {
1229 ret = -errno;
1230 }
1231 return ret;
1232 }
1233
1234 int kvm_vm_ioctl(KVMState *s, int type, ...)
1235 {
1236 int ret;
1237 void *arg;
1238 va_list ap;
1239
1240 va_start(ap, type);
1241 arg = va_arg(ap, void *);
1242 va_end(ap);
1243
1244 ret = ioctl(s->vmfd, type, arg);
1245 if (ret == -1) {
1246 ret = -errno;
1247 }
1248 return ret;
1249 }
1250
1251 int kvm_vcpu_ioctl(CPUState *env, int type, ...)
1252 {
1253 int ret;
1254 void *arg;
1255 va_list ap;
1256
1257 va_start(ap, type);
1258 arg = va_arg(ap, void *);
1259 va_end(ap);
1260
1261 ret = ioctl(env->kvm_fd, type, arg);
1262 if (ret == -1) {
1263 ret = -errno;
1264 }
1265 return ret;
1266 }
1267
1268 int kvm_has_sync_mmu(void)
1269 {
1270 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
1271 }
1272
1273 int kvm_has_vcpu_events(void)
1274 {
1275 return kvm_state->vcpu_events;
1276 }
1277
1278 int kvm_has_robust_singlestep(void)
1279 {
1280 return kvm_state->robust_singlestep;
1281 }
1282
1283 int kvm_has_debugregs(void)
1284 {
1285 return kvm_state->debugregs;
1286 }
1287
1288 int kvm_has_xsave(void)
1289 {
1290 return kvm_state->xsave;
1291 }
1292
1293 int kvm_has_xcrs(void)
1294 {
1295 return kvm_state->xcrs;
1296 }
1297
1298 int kvm_has_many_ioeventfds(void)
1299 {
1300 if (!kvm_enabled()) {
1301 return 0;
1302 }
1303 return kvm_state->many_ioeventfds;
1304 }
1305
1306 int kvm_has_gsi_routing(void)
1307 {
1308 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
1309 }
1310
1311 int kvm_allows_irq0_override(void)
1312 {
1313 return !kvm_enabled() || !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1314 }
1315
1316 void kvm_setup_guest_memory(void *start, size_t size)
1317 {
1318 if (!kvm_has_sync_mmu()) {
1319 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
1320
1321 if (ret) {
1322 perror("qemu_madvise");
1323 fprintf(stderr,
1324 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1325 exit(1);
1326 }
1327 }
1328 }
1329
1330 #ifdef KVM_CAP_SET_GUEST_DEBUG
1331 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
1332 target_ulong pc)
1333 {
1334 struct kvm_sw_breakpoint *bp;
1335
1336 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
1337 if (bp->pc == pc) {
1338 return bp;
1339 }
1340 }
1341 return NULL;
1342 }
1343
1344 int kvm_sw_breakpoints_active(CPUState *env)
1345 {
1346 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
1347 }
1348
1349 struct kvm_set_guest_debug_data {
1350 struct kvm_guest_debug dbg;
1351 CPUState *env;
1352 int err;
1353 };
1354
1355 static void kvm_invoke_set_guest_debug(void *data)
1356 {
1357 struct kvm_set_guest_debug_data *dbg_data = data;
1358 CPUState *env = dbg_data->env;
1359
1360 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
1361 }
1362
1363 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1364 {
1365 struct kvm_set_guest_debug_data data;
1366
1367 data.dbg.control = reinject_trap;
1368
1369 if (env->singlestep_enabled) {
1370 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1371 }
1372 kvm_arch_update_guest_debug(env, &data.dbg);
1373 data.env = env;
1374
1375 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
1376 return data.err;
1377 }
1378
1379 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1380 target_ulong len, int type)
1381 {
1382 struct kvm_sw_breakpoint *bp;
1383 CPUState *env;
1384 int err;
1385
1386 if (type == GDB_BREAKPOINT_SW) {
1387 bp = kvm_find_sw_breakpoint(current_env, addr);
1388 if (bp) {
1389 bp->use_count++;
1390 return 0;
1391 }
1392
1393 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
1394 if (!bp) {
1395 return -ENOMEM;
1396 }
1397
1398 bp->pc = addr;
1399 bp->use_count = 1;
1400 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1401 if (err) {
1402 g_free(bp);
1403 return err;
1404 }
1405
1406 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
1407 bp, entry);
1408 } else {
1409 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1410 if (err) {
1411 return err;
1412 }
1413 }
1414
1415 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1416 err = kvm_update_guest_debug(env, 0);
1417 if (err) {
1418 return err;
1419 }
1420 }
1421 return 0;
1422 }
1423
1424 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1425 target_ulong len, int type)
1426 {
1427 struct kvm_sw_breakpoint *bp;
1428 CPUState *env;
1429 int err;
1430
1431 if (type == GDB_BREAKPOINT_SW) {
1432 bp = kvm_find_sw_breakpoint(current_env, addr);
1433 if (!bp) {
1434 return -ENOENT;
1435 }
1436
1437 if (bp->use_count > 1) {
1438 bp->use_count--;
1439 return 0;
1440 }
1441
1442 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1443 if (err) {
1444 return err;
1445 }
1446
1447 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
1448 g_free(bp);
1449 } else {
1450 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1451 if (err) {
1452 return err;
1453 }
1454 }
1455
1456 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1457 err = kvm_update_guest_debug(env, 0);
1458 if (err) {
1459 return err;
1460 }
1461 }
1462 return 0;
1463 }
1464
1465 void kvm_remove_all_breakpoints(CPUState *current_env)
1466 {
1467 struct kvm_sw_breakpoint *bp, *next;
1468 KVMState *s = current_env->kvm_state;
1469 CPUState *env;
1470
1471 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
1472 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1473 /* Try harder to find a CPU that currently sees the breakpoint. */
1474 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1475 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
1476 break;
1477 }
1478 }
1479 }
1480 }
1481 kvm_arch_remove_all_hw_breakpoints();
1482
1483 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1484 kvm_update_guest_debug(env, 0);
1485 }
1486 }
1487
1488 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1489
1490 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1491 {
1492 return -EINVAL;
1493 }
1494
1495 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1496 target_ulong len, int type)
1497 {
1498 return -EINVAL;
1499 }
1500
1501 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1502 target_ulong len, int type)
1503 {
1504 return -EINVAL;
1505 }
1506
1507 void kvm_remove_all_breakpoints(CPUState *current_env)
1508 {
1509 }
1510 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1511
1512 int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset)
1513 {
1514 struct kvm_signal_mask *sigmask;
1515 int r;
1516
1517 if (!sigset) {
1518 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
1519 }
1520
1521 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
1522
1523 sigmask->len = 8;
1524 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1525 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
1526 g_free(sigmask);
1527
1528 return r;
1529 }
1530
1531 int kvm_set_ioeventfd_mmio_long(int fd, uint32_t addr, uint32_t val, bool assign)
1532 {
1533 int ret;
1534 struct kvm_ioeventfd iofd;
1535
1536 iofd.datamatch = val;
1537 iofd.addr = addr;
1538 iofd.len = 4;
1539 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1540 iofd.fd = fd;
1541
1542 if (!kvm_enabled()) {
1543 return -ENOSYS;
1544 }
1545
1546 if (!assign) {
1547 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1548 }
1549
1550 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1551
1552 if (ret < 0) {
1553 return -errno;
1554 }
1555
1556 return 0;
1557 }
1558
1559 int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1560 {
1561 struct kvm_ioeventfd kick = {
1562 .datamatch = val,
1563 .addr = addr,
1564 .len = 2,
1565 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1566 .fd = fd,
1567 };
1568 int r;
1569 if (!kvm_enabled()) {
1570 return -ENOSYS;
1571 }
1572 if (!assign) {
1573 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1574 }
1575 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1576 if (r < 0) {
1577 return r;
1578 }
1579 return 0;
1580 }
1581
1582 int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr)
1583 {
1584 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1585 }
1586
1587 int kvm_on_sigbus(int code, void *addr)
1588 {
1589 return kvm_arch_on_sigbus(code, addr);
1590 }