4 * Copyright IBM, Corp. 2008
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Glauber Costa <gcosta@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
16 #include "qemu/osdep.h"
17 #include <sys/ioctl.h>
19 #include <linux/kvm.h>
21 #include "qemu-common.h"
22 #include "qemu/atomic.h"
23 #include "qemu/option.h"
24 #include "qemu/config-file.h"
25 #include "qemu/error-report.h"
27 #include "hw/pci/msi.h"
28 #include "hw/pci/msix.h"
29 #include "hw/s390x/adapter.h"
30 #include "exec/gdbstub.h"
31 #include "sysemu/kvm_int.h"
32 #include "sysemu/cpus.h"
33 #include "qemu/bswap.h"
34 #include "exec/memory.h"
35 #include "exec/ram_addr.h"
36 #include "exec/address-spaces.h"
37 #include "qemu/event_notifier.h"
38 #include "trace-root.h"
41 #include "hw/boards.h"
43 /* This check must be after config-host.h is included */
45 #include <sys/eventfd.h>
48 /* KVM uses PAGE_SIZE in its definition of KVM_COALESCED_MMIO_MAX. We
49 * need to use the real host PAGE_SIZE, as that's what KVM will use.
51 #define PAGE_SIZE getpagesize()
56 #define DPRINTF(fmt, ...) \
57 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
59 #define DPRINTF(fmt, ...) \
63 #define KVM_MSI_HASHTAB_SIZE 256
65 struct KVMParkedVcpu
{
66 unsigned long vcpu_id
;
68 QLIST_ENTRY(KVMParkedVcpu
) node
;
73 AccelState parent_obj
;
79 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
80 bool coalesced_flush_in_progress
;
81 int broken_set_mem_region
;
83 int robust_singlestep
;
85 #ifdef KVM_CAP_SET_GUEST_DEBUG
86 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
90 /* The man page (and posix) say ioctl numbers are signed int, but
91 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
92 * unsigned, and treating them as signed here can break things */
93 unsigned irq_set_ioctl
;
94 unsigned int sigmask_len
;
96 #ifdef KVM_CAP_IRQ_ROUTING
97 struct kvm_irq_routing
*irq_routes
;
98 int nr_allocated_irq_routes
;
99 unsigned long *used_gsi_bitmap
;
100 unsigned int gsi_count
;
101 QTAILQ_HEAD(msi_hashtab
, KVMMSIRoute
) msi_hashtab
[KVM_MSI_HASHTAB_SIZE
];
103 KVMMemoryListener memory_listener
;
104 QLIST_HEAD(, KVMParkedVcpu
) kvm_parked_vcpus
;
108 bool kvm_kernel_irqchip
;
109 bool kvm_split_irqchip
;
110 bool kvm_async_interrupts_allowed
;
111 bool kvm_halt_in_kernel_allowed
;
112 bool kvm_eventfds_allowed
;
113 bool kvm_irqfds_allowed
;
114 bool kvm_resamplefds_allowed
;
115 bool kvm_msi_via_irqfd_allowed
;
116 bool kvm_gsi_routing_allowed
;
117 bool kvm_gsi_direct_mapping
;
119 bool kvm_readonly_mem_allowed
;
120 bool kvm_vm_attributes_allowed
;
121 bool kvm_direct_msi_allowed
;
122 bool kvm_ioeventfd_any_length_allowed
;
123 bool kvm_msi_use_devid
;
124 static bool kvm_immediate_exit
;
126 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
127 KVM_CAP_INFO(USER_MEMORY
),
128 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
132 int kvm_get_max_memslots(void)
134 KVMState
*s
= KVM_STATE(current_machine
->accelerator
);
139 static KVMSlot
*kvm_get_free_slot(KVMMemoryListener
*kml
)
141 KVMState
*s
= kvm_state
;
144 for (i
= 0; i
< s
->nr_slots
; i
++) {
145 if (kml
->slots
[i
].memory_size
== 0) {
146 return &kml
->slots
[i
];
153 bool kvm_has_free_slot(MachineState
*ms
)
155 KVMState
*s
= KVM_STATE(ms
->accelerator
);
157 return kvm_get_free_slot(&s
->memory_listener
);
160 static KVMSlot
*kvm_alloc_slot(KVMMemoryListener
*kml
)
162 KVMSlot
*slot
= kvm_get_free_slot(kml
);
168 fprintf(stderr
, "%s: no free slot available\n", __func__
);
172 static KVMSlot
*kvm_lookup_matching_slot(KVMMemoryListener
*kml
,
176 KVMState
*s
= kvm_state
;
179 for (i
= 0; i
< s
->nr_slots
; i
++) {
180 KVMSlot
*mem
= &kml
->slots
[i
];
182 if (start_addr
== mem
->start_addr
&&
183 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
192 * Find overlapping slot with lowest start address
194 static KVMSlot
*kvm_lookup_overlapping_slot(KVMMemoryListener
*kml
,
198 KVMState
*s
= kvm_state
;
199 KVMSlot
*found
= NULL
;
202 for (i
= 0; i
< s
->nr_slots
; i
++) {
203 KVMSlot
*mem
= &kml
->slots
[i
];
205 if (mem
->memory_size
== 0 ||
206 (found
&& found
->start_addr
< mem
->start_addr
)) {
210 if (end_addr
> mem
->start_addr
&&
211 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
219 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
222 KVMMemoryListener
*kml
= &s
->memory_listener
;
225 for (i
= 0; i
< s
->nr_slots
; i
++) {
226 KVMSlot
*mem
= &kml
->slots
[i
];
228 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
229 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
237 static int kvm_set_user_memory_region(KVMMemoryListener
*kml
, KVMSlot
*slot
)
239 KVMState
*s
= kvm_state
;
240 struct kvm_userspace_memory_region mem
;
242 mem
.slot
= slot
->slot
| (kml
->as_id
<< 16);
243 mem
.guest_phys_addr
= slot
->start_addr
;
244 mem
.userspace_addr
= (unsigned long)slot
->ram
;
245 mem
.flags
= slot
->flags
;
247 if (slot
->memory_size
&& mem
.flags
& KVM_MEM_READONLY
) {
248 /* Set the slot size to 0 before setting the slot to the desired
249 * value. This is needed based on KVM commit 75d61fbc. */
251 kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
253 mem
.memory_size
= slot
->memory_size
;
254 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
257 int kvm_destroy_vcpu(CPUState
*cpu
)
259 KVMState
*s
= kvm_state
;
261 struct KVMParkedVcpu
*vcpu
= NULL
;
264 DPRINTF("kvm_destroy_vcpu\n");
266 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
269 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
273 ret
= munmap(cpu
->kvm_run
, mmap_size
);
278 vcpu
= g_malloc0(sizeof(*vcpu
));
279 vcpu
->vcpu_id
= kvm_arch_vcpu_id(cpu
);
280 vcpu
->kvm_fd
= cpu
->kvm_fd
;
281 QLIST_INSERT_HEAD(&kvm_state
->kvm_parked_vcpus
, vcpu
, node
);
286 static int kvm_get_vcpu(KVMState
*s
, unsigned long vcpu_id
)
288 struct KVMParkedVcpu
*cpu
;
290 QLIST_FOREACH(cpu
, &s
->kvm_parked_vcpus
, node
) {
291 if (cpu
->vcpu_id
== vcpu_id
) {
294 QLIST_REMOVE(cpu
, node
);
295 kvm_fd
= cpu
->kvm_fd
;
301 return kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, (void *)vcpu_id
);
304 int kvm_init_vcpu(CPUState
*cpu
)
306 KVMState
*s
= kvm_state
;
310 DPRINTF("kvm_init_vcpu\n");
312 ret
= kvm_get_vcpu(s
, kvm_arch_vcpu_id(cpu
));
314 DPRINTF("kvm_create_vcpu failed\n");
320 cpu
->kvm_vcpu_dirty
= true;
322 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
325 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
329 cpu
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
331 if (cpu
->kvm_run
== MAP_FAILED
) {
333 DPRINTF("mmap'ing vcpu state failed\n");
337 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
338 s
->coalesced_mmio_ring
=
339 (void *)cpu
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
342 ret
= kvm_arch_init_vcpu(cpu
);
348 * dirty pages logging control
351 static int kvm_mem_flags(MemoryRegion
*mr
)
353 bool readonly
= mr
->readonly
|| memory_region_is_romd(mr
);
356 if (memory_region_get_dirty_log_mask(mr
) != 0) {
357 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
359 if (readonly
&& kvm_readonly_mem_allowed
) {
360 flags
|= KVM_MEM_READONLY
;
365 static int kvm_slot_update_flags(KVMMemoryListener
*kml
, KVMSlot
*mem
,
370 old_flags
= mem
->flags
;
371 mem
->flags
= kvm_mem_flags(mr
);
373 /* If nothing changed effectively, no need to issue ioctl */
374 if (mem
->flags
== old_flags
) {
378 return kvm_set_user_memory_region(kml
, mem
);
381 static int kvm_section_update_flags(KVMMemoryListener
*kml
,
382 MemoryRegionSection
*section
)
384 hwaddr phys_addr
= section
->offset_within_address_space
;
385 ram_addr_t size
= int128_get64(section
->size
);
386 KVMSlot
*mem
= kvm_lookup_matching_slot(kml
, phys_addr
, phys_addr
+ size
);
391 return kvm_slot_update_flags(kml
, mem
, section
->mr
);
395 static void kvm_log_start(MemoryListener
*listener
,
396 MemoryRegionSection
*section
,
399 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
406 r
= kvm_section_update_flags(kml
, section
);
412 static void kvm_log_stop(MemoryListener
*listener
,
413 MemoryRegionSection
*section
,
416 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
423 r
= kvm_section_update_flags(kml
, section
);
429 /* get kvm's dirty pages bitmap and update qemu's */
430 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
431 unsigned long *bitmap
)
433 ram_addr_t start
= section
->offset_within_region
+
434 memory_region_get_ram_addr(section
->mr
);
435 ram_addr_t pages
= int128_get64(section
->size
) / getpagesize();
437 cpu_physical_memory_set_dirty_lebitmap(bitmap
, start
, pages
);
441 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
444 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
445 * This function updates qemu's dirty bitmap using
446 * memory_region_set_dirty(). This means all bits are set
449 * @start_add: start of logged region.
450 * @end_addr: end of logged region.
452 static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener
*kml
,
453 MemoryRegionSection
*section
)
455 KVMState
*s
= kvm_state
;
456 unsigned long size
, allocated_size
= 0;
457 struct kvm_dirty_log d
= {};
460 hwaddr start_addr
= section
->offset_within_address_space
;
461 hwaddr end_addr
= start_addr
+ int128_get64(section
->size
);
463 d
.dirty_bitmap
= NULL
;
464 while (start_addr
< end_addr
) {
465 mem
= kvm_lookup_overlapping_slot(kml
, start_addr
, end_addr
);
470 /* XXX bad kernel interface alert
471 * For dirty bitmap, kernel allocates array of size aligned to
472 * bits-per-long. But for case when the kernel is 64bits and
473 * the userspace is 32bits, userspace can't align to the same
474 * bits-per-long, since sizeof(long) is different between kernel
475 * and user space. This way, userspace will provide buffer which
476 * may be 4 bytes less than the kernel will use, resulting in
477 * userspace memory corruption (which is not detectable by valgrind
478 * too, in most cases).
479 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
480 * a hope that sizeof(long) won't become >8 any time soon.
482 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
483 /*HOST_LONG_BITS*/ 64) / 8;
484 if (!d
.dirty_bitmap
) {
485 d
.dirty_bitmap
= g_malloc(size
);
486 } else if (size
> allocated_size
) {
487 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
489 allocated_size
= size
;
490 memset(d
.dirty_bitmap
, 0, allocated_size
);
492 d
.slot
= mem
->slot
| (kml
->as_id
<< 16);
493 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
494 DPRINTF("ioctl failed %d\n", errno
);
499 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
500 start_addr
= mem
->start_addr
+ mem
->memory_size
;
502 g_free(d
.dirty_bitmap
);
507 static void kvm_coalesce_mmio_region(MemoryListener
*listener
,
508 MemoryRegionSection
*secion
,
509 hwaddr start
, hwaddr size
)
511 KVMState
*s
= kvm_state
;
513 if (s
->coalesced_mmio
) {
514 struct kvm_coalesced_mmio_zone zone
;
520 (void)kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
524 static void kvm_uncoalesce_mmio_region(MemoryListener
*listener
,
525 MemoryRegionSection
*secion
,
526 hwaddr start
, hwaddr size
)
528 KVMState
*s
= kvm_state
;
530 if (s
->coalesced_mmio
) {
531 struct kvm_coalesced_mmio_zone zone
;
537 (void)kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
541 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
545 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
553 int kvm_vm_check_extension(KVMState
*s
, unsigned int extension
)
557 ret
= kvm_vm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
559 /* VM wide version not implemented, use global one instead */
560 ret
= kvm_check_extension(s
, extension
);
566 static uint32_t adjust_ioeventfd_endianness(uint32_t val
, uint32_t size
)
568 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
569 /* The kernel expects ioeventfd values in HOST_WORDS_BIGENDIAN
570 * endianness, but the memory core hands them in target endianness.
571 * For example, PPC is always treated as big-endian even if running
572 * on KVM and on PPC64LE. Correct here.
586 static int kvm_set_ioeventfd_mmio(int fd
, hwaddr addr
, uint32_t val
,
587 bool assign
, uint32_t size
, bool datamatch
)
590 struct kvm_ioeventfd iofd
= {
591 .datamatch
= datamatch
? adjust_ioeventfd_endianness(val
, size
) : 0,
598 if (!kvm_enabled()) {
603 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DATAMATCH
;
606 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
609 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
618 static int kvm_set_ioeventfd_pio(int fd
, uint16_t addr
, uint16_t val
,
619 bool assign
, uint32_t size
, bool datamatch
)
621 struct kvm_ioeventfd kick
= {
622 .datamatch
= datamatch
? adjust_ioeventfd_endianness(val
, size
) : 0,
624 .flags
= KVM_IOEVENTFD_FLAG_PIO
,
629 if (!kvm_enabled()) {
633 kick
.flags
|= KVM_IOEVENTFD_FLAG_DATAMATCH
;
636 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
638 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
646 static int kvm_check_many_ioeventfds(void)
648 /* Userspace can use ioeventfd for io notification. This requires a host
649 * that supports eventfd(2) and an I/O thread; since eventfd does not
650 * support SIGIO it cannot interrupt the vcpu.
652 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
653 * can avoid creating too many ioeventfds.
655 #if defined(CONFIG_EVENTFD)
658 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
659 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
660 if (ioeventfds
[i
] < 0) {
663 ret
= kvm_set_ioeventfd_pio(ioeventfds
[i
], 0, i
, true, 2, true);
665 close(ioeventfds
[i
]);
670 /* Decide whether many devices are supported or not */
671 ret
= i
== ARRAY_SIZE(ioeventfds
);
674 kvm_set_ioeventfd_pio(ioeventfds
[i
], 0, i
, false, 2, true);
675 close(ioeventfds
[i
]);
683 static const KVMCapabilityInfo
*
684 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
687 if (!kvm_check_extension(s
, list
->value
)) {
695 static void kvm_set_phys_mem(KVMMemoryListener
*kml
,
696 MemoryRegionSection
*section
, bool add
)
698 KVMState
*s
= kvm_state
;
701 MemoryRegion
*mr
= section
->mr
;
702 bool writeable
= !mr
->readonly
&& !mr
->rom_device
;
703 hwaddr start_addr
= section
->offset_within_address_space
;
704 ram_addr_t size
= int128_get64(section
->size
);
708 /* kvm works in page size chunks, but the function may be called
709 with sub-page size and unaligned start address. Pad the start
710 address to next and truncate size to previous page boundary. */
711 delta
= qemu_real_host_page_size
- (start_addr
& ~qemu_real_host_page_mask
);
712 delta
&= ~qemu_real_host_page_mask
;
718 size
&= qemu_real_host_page_mask
;
719 if (!size
|| (start_addr
& ~qemu_real_host_page_mask
)) {
723 if (!memory_region_is_ram(mr
)) {
724 if (writeable
|| !kvm_readonly_mem_allowed
) {
726 } else if (!mr
->romd_mode
) {
727 /* If the memory device is not in romd_mode, then we actually want
728 * to remove the kvm memory slot so all accesses will trap. */
733 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
736 mem
= kvm_lookup_overlapping_slot(kml
, start_addr
, start_addr
+ size
);
741 if (add
&& start_addr
>= mem
->start_addr
&&
742 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
743 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
744 /* The new slot fits into the existing one and comes with
745 * identical parameters - update flags and done. */
746 kvm_slot_update_flags(kml
, mem
, mr
);
752 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
753 kvm_physical_sync_dirty_bitmap(kml
, section
);
756 /* unregister the overlapping slot */
757 mem
->memory_size
= 0;
758 err
= kvm_set_user_memory_region(kml
, mem
);
760 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
761 __func__
, strerror(-err
));
765 /* Workaround for older KVM versions: we can't join slots, even not by
766 * unregistering the previous ones and then registering the larger
767 * slot. We have to maintain the existing fragmentation. Sigh.
769 * This workaround assumes that the new slot starts at the same
770 * address as the first existing one. If not or if some overlapping
771 * slot comes around later, we will fail (not seen in practice so far)
772 * - and actually require a recent KVM version. */
773 if (s
->broken_set_mem_region
&&
774 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
775 mem
= kvm_alloc_slot(kml
);
776 mem
->memory_size
= old
.memory_size
;
777 mem
->start_addr
= old
.start_addr
;
779 mem
->flags
= kvm_mem_flags(mr
);
781 err
= kvm_set_user_memory_region(kml
, mem
);
783 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
788 start_addr
+= old
.memory_size
;
789 ram
+= old
.memory_size
;
790 size
-= old
.memory_size
;
794 /* register prefix slot */
795 if (old
.start_addr
< start_addr
) {
796 mem
= kvm_alloc_slot(kml
);
797 mem
->memory_size
= start_addr
- old
.start_addr
;
798 mem
->start_addr
= old
.start_addr
;
800 mem
->flags
= kvm_mem_flags(mr
);
802 err
= kvm_set_user_memory_region(kml
, mem
);
804 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
805 __func__
, strerror(-err
));
807 fprintf(stderr
, "%s: This is probably because your kernel's " \
808 "PAGE_SIZE is too big. Please try to use 4k " \
809 "PAGE_SIZE!\n", __func__
);
815 /* register suffix slot */
816 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
817 ram_addr_t size_delta
;
819 mem
= kvm_alloc_slot(kml
);
820 mem
->start_addr
= start_addr
+ size
;
821 size_delta
= mem
->start_addr
- old
.start_addr
;
822 mem
->memory_size
= old
.memory_size
- size_delta
;
823 mem
->ram
= old
.ram
+ size_delta
;
824 mem
->flags
= kvm_mem_flags(mr
);
826 err
= kvm_set_user_memory_region(kml
, mem
);
828 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
829 __func__
, strerror(-err
));
835 /* in case the KVM bug workaround already "consumed" the new slot */
842 mem
= kvm_alloc_slot(kml
);
843 mem
->memory_size
= size
;
844 mem
->start_addr
= start_addr
;
846 mem
->flags
= kvm_mem_flags(mr
);
848 err
= kvm_set_user_memory_region(kml
, mem
);
850 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
856 static void kvm_region_add(MemoryListener
*listener
,
857 MemoryRegionSection
*section
)
859 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
861 memory_region_ref(section
->mr
);
862 kvm_set_phys_mem(kml
, section
, true);
865 static void kvm_region_del(MemoryListener
*listener
,
866 MemoryRegionSection
*section
)
868 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
870 kvm_set_phys_mem(kml
, section
, false);
871 memory_region_unref(section
->mr
);
874 static void kvm_log_sync(MemoryListener
*listener
,
875 MemoryRegionSection
*section
)
877 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
880 r
= kvm_physical_sync_dirty_bitmap(kml
, section
);
886 static void kvm_mem_ioeventfd_add(MemoryListener
*listener
,
887 MemoryRegionSection
*section
,
888 bool match_data
, uint64_t data
,
891 int fd
= event_notifier_get_fd(e
);
894 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
895 data
, true, int128_get64(section
->size
),
898 fprintf(stderr
, "%s: error adding ioeventfd: %s\n",
899 __func__
, strerror(-r
));
904 static void kvm_mem_ioeventfd_del(MemoryListener
*listener
,
905 MemoryRegionSection
*section
,
906 bool match_data
, uint64_t data
,
909 int fd
= event_notifier_get_fd(e
);
912 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
913 data
, false, int128_get64(section
->size
),
920 static void kvm_io_ioeventfd_add(MemoryListener
*listener
,
921 MemoryRegionSection
*section
,
922 bool match_data
, uint64_t data
,
925 int fd
= event_notifier_get_fd(e
);
928 r
= kvm_set_ioeventfd_pio(fd
, section
->offset_within_address_space
,
929 data
, true, int128_get64(section
->size
),
932 fprintf(stderr
, "%s: error adding ioeventfd: %s\n",
933 __func__
, strerror(-r
));
938 static void kvm_io_ioeventfd_del(MemoryListener
*listener
,
939 MemoryRegionSection
*section
,
940 bool match_data
, uint64_t data
,
944 int fd
= event_notifier_get_fd(e
);
947 r
= kvm_set_ioeventfd_pio(fd
, section
->offset_within_address_space
,
948 data
, false, int128_get64(section
->size
),
955 void kvm_memory_listener_register(KVMState
*s
, KVMMemoryListener
*kml
,
956 AddressSpace
*as
, int as_id
)
960 kml
->slots
= g_malloc0(s
->nr_slots
* sizeof(KVMSlot
));
963 for (i
= 0; i
< s
->nr_slots
; i
++) {
964 kml
->slots
[i
].slot
= i
;
967 kml
->listener
.region_add
= kvm_region_add
;
968 kml
->listener
.region_del
= kvm_region_del
;
969 kml
->listener
.log_start
= kvm_log_start
;
970 kml
->listener
.log_stop
= kvm_log_stop
;
971 kml
->listener
.log_sync
= kvm_log_sync
;
972 kml
->listener
.priority
= 10;
974 memory_listener_register(&kml
->listener
, as
);
977 static MemoryListener kvm_io_listener
= {
978 .eventfd_add
= kvm_io_ioeventfd_add
,
979 .eventfd_del
= kvm_io_ioeventfd_del
,
983 static void kvm_handle_interrupt(CPUState
*cpu
, int mask
)
985 cpu
->interrupt_request
|= mask
;
987 if (!qemu_cpu_is_self(cpu
)) {
992 int kvm_set_irq(KVMState
*s
, int irq
, int level
)
994 struct kvm_irq_level event
;
997 assert(kvm_async_interrupts_enabled());
1001 ret
= kvm_vm_ioctl(s
, s
->irq_set_ioctl
, &event
);
1003 perror("kvm_set_irq");
1007 return (s
->irq_set_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
1010 #ifdef KVM_CAP_IRQ_ROUTING
1011 typedef struct KVMMSIRoute
{
1012 struct kvm_irq_routing_entry kroute
;
1013 QTAILQ_ENTRY(KVMMSIRoute
) entry
;
1016 static void set_gsi(KVMState
*s
, unsigned int gsi
)
1018 set_bit(gsi
, s
->used_gsi_bitmap
);
1021 static void clear_gsi(KVMState
*s
, unsigned int gsi
)
1023 clear_bit(gsi
, s
->used_gsi_bitmap
);
1026 void kvm_init_irq_routing(KVMState
*s
)
1030 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
) - 1;
1031 if (gsi_count
> 0) {
1032 /* Round up so we can search ints using ffs */
1033 s
->used_gsi_bitmap
= bitmap_new(gsi_count
);
1034 s
->gsi_count
= gsi_count
;
1037 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
1038 s
->nr_allocated_irq_routes
= 0;
1040 if (!kvm_direct_msi_allowed
) {
1041 for (i
= 0; i
< KVM_MSI_HASHTAB_SIZE
; i
++) {
1042 QTAILQ_INIT(&s
->msi_hashtab
[i
]);
1046 kvm_arch_init_irq_routing(s
);
1049 void kvm_irqchip_commit_routes(KVMState
*s
)
1053 if (kvm_gsi_direct_mapping()) {
1057 if (!kvm_gsi_routing_enabled()) {
1061 s
->irq_routes
->flags
= 0;
1062 trace_kvm_irqchip_commit_routes();
1063 ret
= kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
1067 static void kvm_add_routing_entry(KVMState
*s
,
1068 struct kvm_irq_routing_entry
*entry
)
1070 struct kvm_irq_routing_entry
*new;
1073 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
1074 n
= s
->nr_allocated_irq_routes
* 2;
1078 size
= sizeof(struct kvm_irq_routing
);
1079 size
+= n
* sizeof(*new);
1080 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
1081 s
->nr_allocated_irq_routes
= n
;
1083 n
= s
->irq_routes
->nr
++;
1084 new = &s
->irq_routes
->entries
[n
];
1088 set_gsi(s
, entry
->gsi
);
1091 static int kvm_update_routing_entry(KVMState
*s
,
1092 struct kvm_irq_routing_entry
*new_entry
)
1094 struct kvm_irq_routing_entry
*entry
;
1097 for (n
= 0; n
< s
->irq_routes
->nr
; n
++) {
1098 entry
= &s
->irq_routes
->entries
[n
];
1099 if (entry
->gsi
!= new_entry
->gsi
) {
1103 if(!memcmp(entry
, new_entry
, sizeof *entry
)) {
1107 *entry
= *new_entry
;
1115 void kvm_irqchip_add_irq_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
1117 struct kvm_irq_routing_entry e
= {};
1119 assert(pin
< s
->gsi_count
);
1122 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1124 e
.u
.irqchip
.irqchip
= irqchip
;
1125 e
.u
.irqchip
.pin
= pin
;
1126 kvm_add_routing_entry(s
, &e
);
1129 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1131 struct kvm_irq_routing_entry
*e
;
1134 if (kvm_gsi_direct_mapping()) {
1138 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
1139 e
= &s
->irq_routes
->entries
[i
];
1140 if (e
->gsi
== virq
) {
1141 s
->irq_routes
->nr
--;
1142 *e
= s
->irq_routes
->entries
[s
->irq_routes
->nr
];
1146 kvm_arch_release_virq_post(virq
);
1149 static unsigned int kvm_hash_msi(uint32_t data
)
1151 /* This is optimized for IA32 MSI layout. However, no other arch shall
1152 * repeat the mistake of not providing a direct MSI injection API. */
1156 static void kvm_flush_dynamic_msi_routes(KVMState
*s
)
1158 KVMMSIRoute
*route
, *next
;
1161 for (hash
= 0; hash
< KVM_MSI_HASHTAB_SIZE
; hash
++) {
1162 QTAILQ_FOREACH_SAFE(route
, &s
->msi_hashtab
[hash
], entry
, next
) {
1163 kvm_irqchip_release_virq(s
, route
->kroute
.gsi
);
1164 QTAILQ_REMOVE(&s
->msi_hashtab
[hash
], route
, entry
);
1170 static int kvm_irqchip_get_virq(KVMState
*s
)
1175 * PIC and IOAPIC share the first 16 GSI numbers, thus the available
1176 * GSI numbers are more than the number of IRQ route. Allocating a GSI
1177 * number can succeed even though a new route entry cannot be added.
1178 * When this happens, flush dynamic MSI entries to free IRQ route entries.
1180 if (!kvm_direct_msi_allowed
&& s
->irq_routes
->nr
== s
->gsi_count
) {
1181 kvm_flush_dynamic_msi_routes(s
);
1184 /* Return the lowest unused GSI in the bitmap */
1185 next_virq
= find_first_zero_bit(s
->used_gsi_bitmap
, s
->gsi_count
);
1186 if (next_virq
>= s
->gsi_count
) {
1193 static KVMMSIRoute
*kvm_lookup_msi_route(KVMState
*s
, MSIMessage msg
)
1195 unsigned int hash
= kvm_hash_msi(msg
.data
);
1198 QTAILQ_FOREACH(route
, &s
->msi_hashtab
[hash
], entry
) {
1199 if (route
->kroute
.u
.msi
.address_lo
== (uint32_t)msg
.address
&&
1200 route
->kroute
.u
.msi
.address_hi
== (msg
.address
>> 32) &&
1201 route
->kroute
.u
.msi
.data
== le32_to_cpu(msg
.data
)) {
1208 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1213 if (kvm_direct_msi_allowed
) {
1214 msi
.address_lo
= (uint32_t)msg
.address
;
1215 msi
.address_hi
= msg
.address
>> 32;
1216 msi
.data
= le32_to_cpu(msg
.data
);
1218 memset(msi
.pad
, 0, sizeof(msi
.pad
));
1220 return kvm_vm_ioctl(s
, KVM_SIGNAL_MSI
, &msi
);
1223 route
= kvm_lookup_msi_route(s
, msg
);
1227 virq
= kvm_irqchip_get_virq(s
);
1232 route
= g_malloc0(sizeof(KVMMSIRoute
));
1233 route
->kroute
.gsi
= virq
;
1234 route
->kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1235 route
->kroute
.flags
= 0;
1236 route
->kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1237 route
->kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1238 route
->kroute
.u
.msi
.data
= le32_to_cpu(msg
.data
);
1240 kvm_add_routing_entry(s
, &route
->kroute
);
1241 kvm_irqchip_commit_routes(s
);
1243 QTAILQ_INSERT_TAIL(&s
->msi_hashtab
[kvm_hash_msi(msg
.data
)], route
,
1247 assert(route
->kroute
.type
== KVM_IRQ_ROUTING_MSI
);
1249 return kvm_set_irq(s
, route
->kroute
.gsi
, 1);
1252 int kvm_irqchip_add_msi_route(KVMState
*s
, int vector
, PCIDevice
*dev
)
1254 struct kvm_irq_routing_entry kroute
= {};
1256 MSIMessage msg
= {0, 0};
1259 msg
= pci_get_msi_message(dev
, vector
);
1262 if (kvm_gsi_direct_mapping()) {
1263 return kvm_arch_msi_data_to_gsi(msg
.data
);
1266 if (!kvm_gsi_routing_enabled()) {
1270 virq
= kvm_irqchip_get_virq(s
);
1276 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1278 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1279 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1280 kroute
.u
.msi
.data
= le32_to_cpu(msg
.data
);
1281 if (kvm_msi_devid_required()) {
1282 kroute
.flags
= KVM_MSI_VALID_DEVID
;
1283 kroute
.u
.msi
.devid
= pci_requester_id(dev
);
1285 if (kvm_arch_fixup_msi_route(&kroute
, msg
.address
, msg
.data
, dev
)) {
1286 kvm_irqchip_release_virq(s
, virq
);
1290 trace_kvm_irqchip_add_msi_route(virq
);
1292 kvm_add_routing_entry(s
, &kroute
);
1293 kvm_arch_add_msi_route_post(&kroute
, vector
, dev
);
1294 kvm_irqchip_commit_routes(s
);
1299 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
,
1302 struct kvm_irq_routing_entry kroute
= {};
1304 if (kvm_gsi_direct_mapping()) {
1308 if (!kvm_irqchip_in_kernel()) {
1313 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1315 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1316 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1317 kroute
.u
.msi
.data
= le32_to_cpu(msg
.data
);
1318 if (kvm_msi_devid_required()) {
1319 kroute
.flags
= KVM_MSI_VALID_DEVID
;
1320 kroute
.u
.msi
.devid
= pci_requester_id(dev
);
1322 if (kvm_arch_fixup_msi_route(&kroute
, msg
.address
, msg
.data
, dev
)) {
1326 trace_kvm_irqchip_update_msi_route(virq
);
1328 return kvm_update_routing_entry(s
, &kroute
);
1331 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int rfd
, int virq
,
1334 struct kvm_irqfd irqfd
= {
1337 .flags
= assign
? 0 : KVM_IRQFD_FLAG_DEASSIGN
,
1341 irqfd
.flags
|= KVM_IRQFD_FLAG_RESAMPLE
;
1342 irqfd
.resamplefd
= rfd
;
1345 if (!kvm_irqfds_enabled()) {
1349 return kvm_vm_ioctl(s
, KVM_IRQFD
, &irqfd
);
1352 int kvm_irqchip_add_adapter_route(KVMState
*s
, AdapterInfo
*adapter
)
1354 struct kvm_irq_routing_entry kroute
= {};
1357 if (!kvm_gsi_routing_enabled()) {
1361 virq
= kvm_irqchip_get_virq(s
);
1367 kroute
.type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
1369 kroute
.u
.adapter
.summary_addr
= adapter
->summary_addr
;
1370 kroute
.u
.adapter
.ind_addr
= adapter
->ind_addr
;
1371 kroute
.u
.adapter
.summary_offset
= adapter
->summary_offset
;
1372 kroute
.u
.adapter
.ind_offset
= adapter
->ind_offset
;
1373 kroute
.u
.adapter
.adapter_id
= adapter
->adapter_id
;
1375 kvm_add_routing_entry(s
, &kroute
);
1380 int kvm_irqchip_add_hv_sint_route(KVMState
*s
, uint32_t vcpu
, uint32_t sint
)
1382 struct kvm_irq_routing_entry kroute
= {};
1385 if (!kvm_gsi_routing_enabled()) {
1388 if (!kvm_check_extension(s
, KVM_CAP_HYPERV_SYNIC
)) {
1391 virq
= kvm_irqchip_get_virq(s
);
1397 kroute
.type
= KVM_IRQ_ROUTING_HV_SINT
;
1399 kroute
.u
.hv_sint
.vcpu
= vcpu
;
1400 kroute
.u
.hv_sint
.sint
= sint
;
1402 kvm_add_routing_entry(s
, &kroute
);
1403 kvm_irqchip_commit_routes(s
);
1408 #else /* !KVM_CAP_IRQ_ROUTING */
1410 void kvm_init_irq_routing(KVMState
*s
)
1414 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1418 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1423 int kvm_irqchip_add_msi_route(KVMState
*s
, int vector
, PCIDevice
*dev
)
1428 int kvm_irqchip_add_adapter_route(KVMState
*s
, AdapterInfo
*adapter
)
1433 int kvm_irqchip_add_hv_sint_route(KVMState
*s
, uint32_t vcpu
, uint32_t sint
)
1438 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1443 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
)
1447 #endif /* !KVM_CAP_IRQ_ROUTING */
1449 int kvm_irqchip_add_irqfd_notifier_gsi(KVMState
*s
, EventNotifier
*n
,
1450 EventNotifier
*rn
, int virq
)
1452 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
),
1453 rn
? event_notifier_get_fd(rn
) : -1, virq
, true);
1456 int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState
*s
, EventNotifier
*n
,
1459 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
), -1, virq
,
1463 int kvm_irqchip_add_irqfd_notifier(KVMState
*s
, EventNotifier
*n
,
1464 EventNotifier
*rn
, qemu_irq irq
)
1467 gboolean found
= g_hash_table_lookup_extended(s
->gsimap
, irq
, &key
, &gsi
);
1472 return kvm_irqchip_add_irqfd_notifier_gsi(s
, n
, rn
, GPOINTER_TO_INT(gsi
));
1475 int kvm_irqchip_remove_irqfd_notifier(KVMState
*s
, EventNotifier
*n
,
1479 gboolean found
= g_hash_table_lookup_extended(s
->gsimap
, irq
, &key
, &gsi
);
1484 return kvm_irqchip_remove_irqfd_notifier_gsi(s
, n
, GPOINTER_TO_INT(gsi
));
1487 void kvm_irqchip_set_qemuirq_gsi(KVMState
*s
, qemu_irq irq
, int gsi
)
1489 g_hash_table_insert(s
->gsimap
, irq
, GINT_TO_POINTER(gsi
));
1492 static void kvm_irqchip_create(MachineState
*machine
, KVMState
*s
)
1496 if (kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
1498 } else if (kvm_check_extension(s
, KVM_CAP_S390_IRQCHIP
)) {
1499 ret
= kvm_vm_enable_cap(s
, KVM_CAP_S390_IRQCHIP
, 0);
1501 fprintf(stderr
, "Enable kernel irqchip failed: %s\n", strerror(-ret
));
1508 /* First probe and see if there's a arch-specific hook to create the
1509 * in-kernel irqchip for us */
1510 ret
= kvm_arch_irqchip_create(machine
, s
);
1512 if (machine_kernel_irqchip_split(machine
)) {
1513 perror("Split IRQ chip mode not supported.");
1516 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
1520 fprintf(stderr
, "Create kernel irqchip failed: %s\n", strerror(-ret
));
1524 kvm_kernel_irqchip
= true;
1525 /* If we have an in-kernel IRQ chip then we must have asynchronous
1526 * interrupt delivery (though the reverse is not necessarily true)
1528 kvm_async_interrupts_allowed
= true;
1529 kvm_halt_in_kernel_allowed
= true;
1531 kvm_init_irq_routing(s
);
1533 s
->gsimap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1536 /* Find number of supported CPUs using the recommended
1537 * procedure from the kernel API documentation to cope with
1538 * older kernels that may be missing capabilities.
1540 static int kvm_recommended_vcpus(KVMState
*s
)
1542 int ret
= kvm_check_extension(s
, KVM_CAP_NR_VCPUS
);
1543 return (ret
) ? ret
: 4;
1546 static int kvm_max_vcpus(KVMState
*s
)
1548 int ret
= kvm_check_extension(s
, KVM_CAP_MAX_VCPUS
);
1549 return (ret
) ? ret
: kvm_recommended_vcpus(s
);
1552 static int kvm_max_vcpu_id(KVMState
*s
)
1554 int ret
= kvm_check_extension(s
, KVM_CAP_MAX_VCPU_ID
);
1555 return (ret
) ? ret
: kvm_max_vcpus(s
);
1558 bool kvm_vcpu_id_is_valid(int vcpu_id
)
1560 KVMState
*s
= KVM_STATE(current_machine
->accelerator
);
1561 return vcpu_id
>= 0 && vcpu_id
< kvm_max_vcpu_id(s
);
1564 static int kvm_init(MachineState
*ms
)
1566 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
1567 static const char upgrade_note
[] =
1568 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1569 "(see http://sourceforge.net/projects/kvm).\n";
1574 { "SMP", smp_cpus
},
1575 { "hotpluggable", max_cpus
},
1578 int soft_vcpus_limit
, hard_vcpus_limit
;
1580 const KVMCapabilityInfo
*missing_cap
;
1583 const char *kvm_type
;
1585 s
= KVM_STATE(ms
->accelerator
);
1588 * On systems where the kernel can support different base page
1589 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1590 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1591 * page size for the system though.
1593 assert(TARGET_PAGE_SIZE
<= getpagesize());
1597 #ifdef KVM_CAP_SET_GUEST_DEBUG
1598 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
1600 QLIST_INIT(&s
->kvm_parked_vcpus
);
1602 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
1604 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1609 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1610 if (ret
< KVM_API_VERSION
) {
1614 fprintf(stderr
, "kvm version too old\n");
1618 if (ret
> KVM_API_VERSION
) {
1620 fprintf(stderr
, "kvm version not supported\n");
1624 kvm_immediate_exit
= kvm_check_extension(s
, KVM_CAP_IMMEDIATE_EXIT
);
1625 s
->nr_slots
= kvm_check_extension(s
, KVM_CAP_NR_MEMSLOTS
);
1627 /* If unspecified, use the default value */
1632 /* check the vcpu limits */
1633 soft_vcpus_limit
= kvm_recommended_vcpus(s
);
1634 hard_vcpus_limit
= kvm_max_vcpus(s
);
1637 if (nc
->num
> soft_vcpus_limit
) {
1639 "Warning: Number of %s cpus requested (%d) exceeds "
1640 "the recommended cpus supported by KVM (%d)\n",
1641 nc
->name
, nc
->num
, soft_vcpus_limit
);
1643 if (nc
->num
> hard_vcpus_limit
) {
1644 fprintf(stderr
, "Number of %s cpus requested (%d) exceeds "
1645 "the maximum cpus supported by KVM (%d)\n",
1646 nc
->name
, nc
->num
, hard_vcpus_limit
);
1653 kvm_type
= qemu_opt_get(qemu_get_machine_opts(), "kvm-type");
1655 type
= mc
->kvm_type(kvm_type
);
1656 } else if (kvm_type
) {
1658 fprintf(stderr
, "Invalid argument kvm-type=%s\n", kvm_type
);
1663 ret
= kvm_ioctl(s
, KVM_CREATE_VM
, type
);
1664 } while (ret
== -EINTR
);
1667 fprintf(stderr
, "ioctl(KVM_CREATE_VM) failed: %d %s\n", -ret
,
1671 if (ret
== -EINVAL
) {
1673 "Host kernel setup problem detected. Please verify:\n");
1674 fprintf(stderr
, "- for kernels supporting the switch_amode or"
1675 " user_mode parameters, whether\n");
1677 " user space is running in primary address space\n");
1679 "- for kernels supporting the vm.allocate_pgste sysctl, "
1680 "whether it is enabled\n");
1687 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1690 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1694 fprintf(stderr
, "kvm does not support %s\n%s",
1695 missing_cap
->name
, upgrade_note
);
1699 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1701 s
->broken_set_mem_region
= 1;
1702 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1704 s
->broken_set_mem_region
= 0;
1707 #ifdef KVM_CAP_VCPU_EVENTS
1708 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1711 s
->robust_singlestep
=
1712 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1714 #ifdef KVM_CAP_DEBUGREGS
1715 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1718 #ifdef KVM_CAP_IRQ_ROUTING
1719 kvm_direct_msi_allowed
= (kvm_check_extension(s
, KVM_CAP_SIGNAL_MSI
) > 0);
1722 s
->intx_set_mask
= kvm_check_extension(s
, KVM_CAP_PCI_2_3
);
1724 s
->irq_set_ioctl
= KVM_IRQ_LINE
;
1725 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
1726 s
->irq_set_ioctl
= KVM_IRQ_LINE_STATUS
;
1729 #ifdef KVM_CAP_READONLY_MEM
1730 kvm_readonly_mem_allowed
=
1731 (kvm_check_extension(s
, KVM_CAP_READONLY_MEM
) > 0);
1734 kvm_eventfds_allowed
=
1735 (kvm_check_extension(s
, KVM_CAP_IOEVENTFD
) > 0);
1737 kvm_irqfds_allowed
=
1738 (kvm_check_extension(s
, KVM_CAP_IRQFD
) > 0);
1740 kvm_resamplefds_allowed
=
1741 (kvm_check_extension(s
, KVM_CAP_IRQFD_RESAMPLE
) > 0);
1743 kvm_vm_attributes_allowed
=
1744 (kvm_check_extension(s
, KVM_CAP_VM_ATTRIBUTES
) > 0);
1746 kvm_ioeventfd_any_length_allowed
=
1747 (kvm_check_extension(s
, KVM_CAP_IOEVENTFD_ANY_LENGTH
) > 0);
1749 ret
= kvm_arch_init(ms
, s
);
1754 if (machine_kernel_irqchip_allowed(ms
)) {
1755 kvm_irqchip_create(ms
, s
);
1760 if (kvm_eventfds_allowed
) {
1761 s
->memory_listener
.listener
.eventfd_add
= kvm_mem_ioeventfd_add
;
1762 s
->memory_listener
.listener
.eventfd_del
= kvm_mem_ioeventfd_del
;
1764 s
->memory_listener
.listener
.coalesced_mmio_add
= kvm_coalesce_mmio_region
;
1765 s
->memory_listener
.listener
.coalesced_mmio_del
= kvm_uncoalesce_mmio_region
;
1767 kvm_memory_listener_register(s
, &s
->memory_listener
,
1768 &address_space_memory
, 0);
1769 memory_listener_register(&kvm_io_listener
,
1772 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1774 cpu_interrupt_handler
= kvm_handle_interrupt
;
1786 g_free(s
->memory_listener
.slots
);
1791 void kvm_set_sigmask_len(KVMState
*s
, unsigned int sigmask_len
)
1793 s
->sigmask_len
= sigmask_len
;
1796 static void kvm_handle_io(uint16_t port
, MemTxAttrs attrs
, void *data
, int direction
,
1797 int size
, uint32_t count
)
1800 uint8_t *ptr
= data
;
1802 for (i
= 0; i
< count
; i
++) {
1803 address_space_rw(&address_space_io
, port
, attrs
,
1805 direction
== KVM_EXIT_IO_OUT
);
1810 static int kvm_handle_internal_error(CPUState
*cpu
, struct kvm_run
*run
)
1812 fprintf(stderr
, "KVM internal error. Suberror: %d\n",
1813 run
->internal
.suberror
);
1815 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1818 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1819 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1820 i
, (uint64_t)run
->internal
.data
[i
]);
1823 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1824 fprintf(stderr
, "emulation failure\n");
1825 if (!kvm_arch_stop_on_emulation_error(cpu
)) {
1826 cpu_dump_state(cpu
, stderr
, fprintf
, CPU_DUMP_CODE
);
1827 return EXCP_INTERRUPT
;
1830 /* FIXME: Should trigger a qmp message to let management know
1831 * something went wrong.
1836 void kvm_flush_coalesced_mmio_buffer(void)
1838 KVMState
*s
= kvm_state
;
1840 if (s
->coalesced_flush_in_progress
) {
1844 s
->coalesced_flush_in_progress
= true;
1846 if (s
->coalesced_mmio_ring
) {
1847 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1848 while (ring
->first
!= ring
->last
) {
1849 struct kvm_coalesced_mmio
*ent
;
1851 ent
= &ring
->coalesced_mmio
[ring
->first
];
1853 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1855 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1859 s
->coalesced_flush_in_progress
= false;
1862 static void do_kvm_cpu_synchronize_state(CPUState
*cpu
, run_on_cpu_data arg
)
1864 if (!cpu
->kvm_vcpu_dirty
) {
1865 kvm_arch_get_registers(cpu
);
1866 cpu
->kvm_vcpu_dirty
= true;
1870 void kvm_cpu_synchronize_state(CPUState
*cpu
)
1872 if (!cpu
->kvm_vcpu_dirty
) {
1873 run_on_cpu(cpu
, do_kvm_cpu_synchronize_state
, RUN_ON_CPU_NULL
);
1877 static void do_kvm_cpu_synchronize_post_reset(CPUState
*cpu
, run_on_cpu_data arg
)
1879 kvm_arch_put_registers(cpu
, KVM_PUT_RESET_STATE
);
1880 cpu
->kvm_vcpu_dirty
= false;
1883 void kvm_cpu_synchronize_post_reset(CPUState
*cpu
)
1885 run_on_cpu(cpu
, do_kvm_cpu_synchronize_post_reset
, RUN_ON_CPU_NULL
);
1888 static void do_kvm_cpu_synchronize_post_init(CPUState
*cpu
, run_on_cpu_data arg
)
1890 kvm_arch_put_registers(cpu
, KVM_PUT_FULL_STATE
);
1891 cpu
->kvm_vcpu_dirty
= false;
1894 void kvm_cpu_synchronize_post_init(CPUState
*cpu
)
1896 run_on_cpu(cpu
, do_kvm_cpu_synchronize_post_init
, RUN_ON_CPU_NULL
);
1899 #ifdef KVM_HAVE_MCE_INJECTION
1900 static __thread
void *pending_sigbus_addr
;
1901 static __thread
int pending_sigbus_code
;
1902 static __thread
bool have_sigbus_pending
;
1905 static void kvm_cpu_kick(CPUState
*cpu
)
1907 atomic_set(&cpu
->kvm_run
->immediate_exit
, 1);
1910 static void kvm_cpu_kick_self(void)
1912 if (kvm_immediate_exit
) {
1913 kvm_cpu_kick(current_cpu
);
1915 qemu_cpu_kick_self();
1919 static void kvm_eat_signals(CPUState
*cpu
)
1921 struct timespec ts
= { 0, 0 };
1927 if (kvm_immediate_exit
) {
1928 atomic_set(&cpu
->kvm_run
->immediate_exit
, 0);
1929 /* Write kvm_run->immediate_exit before the cpu->exit_request
1930 * write in kvm_cpu_exec.
1936 sigemptyset(&waitset
);
1937 sigaddset(&waitset
, SIG_IPI
);
1940 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
1941 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
1942 perror("sigtimedwait");
1946 r
= sigpending(&chkset
);
1948 perror("sigpending");
1951 } while (sigismember(&chkset
, SIG_IPI
));
1954 int kvm_cpu_exec(CPUState
*cpu
)
1956 struct kvm_run
*run
= cpu
->kvm_run
;
1959 DPRINTF("kvm_cpu_exec()\n");
1961 if (kvm_arch_process_async_events(cpu
)) {
1962 atomic_set(&cpu
->exit_request
, 0);
1966 qemu_mutex_unlock_iothread();
1971 if (cpu
->kvm_vcpu_dirty
) {
1972 kvm_arch_put_registers(cpu
, KVM_PUT_RUNTIME_STATE
);
1973 cpu
->kvm_vcpu_dirty
= false;
1976 kvm_arch_pre_run(cpu
, run
);
1977 if (atomic_read(&cpu
->exit_request
)) {
1978 DPRINTF("interrupt exit requested\n");
1980 * KVM requires us to reenter the kernel after IO exits to complete
1981 * instruction emulation. This self-signal will ensure that we
1984 kvm_cpu_kick_self();
1987 /* Read cpu->exit_request before KVM_RUN reads run->immediate_exit.
1988 * Matching barrier in kvm_eat_signals.
1992 run_ret
= kvm_vcpu_ioctl(cpu
, KVM_RUN
, 0);
1994 attrs
= kvm_arch_post_run(cpu
, run
);
1996 #ifdef KVM_HAVE_MCE_INJECTION
1997 if (unlikely(have_sigbus_pending
)) {
1998 qemu_mutex_lock_iothread();
1999 kvm_arch_on_sigbus_vcpu(cpu
, pending_sigbus_code
,
2000 pending_sigbus_addr
);
2001 have_sigbus_pending
= false;
2002 qemu_mutex_unlock_iothread();
2007 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
2008 DPRINTF("io window exit\n");
2009 kvm_eat_signals(cpu
);
2010 ret
= EXCP_INTERRUPT
;
2013 fprintf(stderr
, "error: kvm run failed %s\n",
2014 strerror(-run_ret
));
2016 if (run_ret
== -EBUSY
) {
2018 "This is probably because your SMT is enabled.\n"
2019 "VCPU can only run on primary threads with all "
2020 "secondary threads offline.\n");
2027 trace_kvm_run_exit(cpu
->cpu_index
, run
->exit_reason
);
2028 switch (run
->exit_reason
) {
2030 DPRINTF("handle_io\n");
2031 /* Called outside BQL */
2032 kvm_handle_io(run
->io
.port
, attrs
,
2033 (uint8_t *)run
+ run
->io
.data_offset
,
2040 DPRINTF("handle_mmio\n");
2041 /* Called outside BQL */
2042 address_space_rw(&address_space_memory
,
2043 run
->mmio
.phys_addr
, attrs
,
2046 run
->mmio
.is_write
);
2049 case KVM_EXIT_IRQ_WINDOW_OPEN
:
2050 DPRINTF("irq_window_open\n");
2051 ret
= EXCP_INTERRUPT
;
2053 case KVM_EXIT_SHUTDOWN
:
2054 DPRINTF("shutdown\n");
2055 qemu_system_reset_request();
2056 ret
= EXCP_INTERRUPT
;
2058 case KVM_EXIT_UNKNOWN
:
2059 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
2060 (uint64_t)run
->hw
.hardware_exit_reason
);
2063 case KVM_EXIT_INTERNAL_ERROR
:
2064 ret
= kvm_handle_internal_error(cpu
, run
);
2066 case KVM_EXIT_SYSTEM_EVENT
:
2067 switch (run
->system_event
.type
) {
2068 case KVM_SYSTEM_EVENT_SHUTDOWN
:
2069 qemu_system_shutdown_request();
2070 ret
= EXCP_INTERRUPT
;
2072 case KVM_SYSTEM_EVENT_RESET
:
2073 qemu_system_reset_request();
2074 ret
= EXCP_INTERRUPT
;
2076 case KVM_SYSTEM_EVENT_CRASH
:
2077 kvm_cpu_synchronize_state(cpu
);
2078 qemu_mutex_lock_iothread();
2079 qemu_system_guest_panicked(cpu_get_crash_info(cpu
));
2080 qemu_mutex_unlock_iothread();
2084 DPRINTF("kvm_arch_handle_exit\n");
2085 ret
= kvm_arch_handle_exit(cpu
, run
);
2090 DPRINTF("kvm_arch_handle_exit\n");
2091 ret
= kvm_arch_handle_exit(cpu
, run
);
2096 qemu_mutex_lock_iothread();
2099 cpu_dump_state(cpu
, stderr
, fprintf
, CPU_DUMP_CODE
);
2100 vm_stop(RUN_STATE_INTERNAL_ERROR
);
2103 atomic_set(&cpu
->exit_request
, 0);
2107 int kvm_ioctl(KVMState
*s
, int type
, ...)
2114 arg
= va_arg(ap
, void *);
2117 trace_kvm_ioctl(type
, arg
);
2118 ret
= ioctl(s
->fd
, type
, arg
);
2125 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
2132 arg
= va_arg(ap
, void *);
2135 trace_kvm_vm_ioctl(type
, arg
);
2136 ret
= ioctl(s
->vmfd
, type
, arg
);
2143 int kvm_vcpu_ioctl(CPUState
*cpu
, int type
, ...)
2150 arg
= va_arg(ap
, void *);
2153 trace_kvm_vcpu_ioctl(cpu
->cpu_index
, type
, arg
);
2154 ret
= ioctl(cpu
->kvm_fd
, type
, arg
);
2161 int kvm_device_ioctl(int fd
, int type
, ...)
2168 arg
= va_arg(ap
, void *);
2171 trace_kvm_device_ioctl(fd
, type
, arg
);
2172 ret
= ioctl(fd
, type
, arg
);
2179 int kvm_vm_check_attr(KVMState
*s
, uint32_t group
, uint64_t attr
)
2182 struct kvm_device_attr attribute
= {
2187 if (!kvm_vm_attributes_allowed
) {
2191 ret
= kvm_vm_ioctl(s
, KVM_HAS_DEVICE_ATTR
, &attribute
);
2192 /* kvm returns 0 on success for HAS_DEVICE_ATTR */
2196 int kvm_device_check_attr(int dev_fd
, uint32_t group
, uint64_t attr
)
2198 struct kvm_device_attr attribute
= {
2204 return kvm_device_ioctl(dev_fd
, KVM_HAS_DEVICE_ATTR
, &attribute
) ? 0 : 1;
2207 void kvm_device_access(int fd
, int group
, uint64_t attr
,
2208 void *val
, bool write
)
2210 struct kvm_device_attr kvmattr
;
2214 kvmattr
.group
= group
;
2215 kvmattr
.attr
= attr
;
2216 kvmattr
.addr
= (uintptr_t)val
;
2218 err
= kvm_device_ioctl(fd
,
2219 write
? KVM_SET_DEVICE_ATTR
: KVM_GET_DEVICE_ATTR
,
2222 error_report("KVM_%s_DEVICE_ATTR failed: %s",
2223 write
? "SET" : "GET", strerror(-err
));
2224 error_printf("Group %d attr 0x%016" PRIx64
"\n", group
, attr
);
2229 /* Return 1 on success, 0 on failure */
2230 int kvm_has_sync_mmu(void)
2232 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
2235 int kvm_has_vcpu_events(void)
2237 return kvm_state
->vcpu_events
;
2240 int kvm_has_robust_singlestep(void)
2242 return kvm_state
->robust_singlestep
;
2245 int kvm_has_debugregs(void)
2247 return kvm_state
->debugregs
;
2250 int kvm_has_many_ioeventfds(void)
2252 if (!kvm_enabled()) {
2255 return kvm_state
->many_ioeventfds
;
2258 int kvm_has_gsi_routing(void)
2260 #ifdef KVM_CAP_IRQ_ROUTING
2261 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
2267 int kvm_has_intx_set_mask(void)
2269 return kvm_state
->intx_set_mask
;
2272 #ifdef KVM_CAP_SET_GUEST_DEBUG
2273 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*cpu
,
2276 struct kvm_sw_breakpoint
*bp
;
2278 QTAILQ_FOREACH(bp
, &cpu
->kvm_state
->kvm_sw_breakpoints
, entry
) {
2286 int kvm_sw_breakpoints_active(CPUState
*cpu
)
2288 return !QTAILQ_EMPTY(&cpu
->kvm_state
->kvm_sw_breakpoints
);
2291 struct kvm_set_guest_debug_data
{
2292 struct kvm_guest_debug dbg
;
2296 static void kvm_invoke_set_guest_debug(CPUState
*cpu
, run_on_cpu_data data
)
2298 struct kvm_set_guest_debug_data
*dbg_data
=
2299 (struct kvm_set_guest_debug_data
*) data
.host_ptr
;
2301 dbg_data
->err
= kvm_vcpu_ioctl(cpu
, KVM_SET_GUEST_DEBUG
,
2305 int kvm_update_guest_debug(CPUState
*cpu
, unsigned long reinject_trap
)
2307 struct kvm_set_guest_debug_data data
;
2309 data
.dbg
.control
= reinject_trap
;
2311 if (cpu
->singlestep_enabled
) {
2312 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
2314 kvm_arch_update_guest_debug(cpu
, &data
.dbg
);
2316 run_on_cpu(cpu
, kvm_invoke_set_guest_debug
,
2317 RUN_ON_CPU_HOST_PTR(&data
));
2321 int kvm_insert_breakpoint(CPUState
*cpu
, target_ulong addr
,
2322 target_ulong len
, int type
)
2324 struct kvm_sw_breakpoint
*bp
;
2327 if (type
== GDB_BREAKPOINT_SW
) {
2328 bp
= kvm_find_sw_breakpoint(cpu
, addr
);
2334 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
2337 err
= kvm_arch_insert_sw_breakpoint(cpu
, bp
);
2343 QTAILQ_INSERT_HEAD(&cpu
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
2345 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
2352 err
= kvm_update_guest_debug(cpu
, 0);
2360 int kvm_remove_breakpoint(CPUState
*cpu
, target_ulong addr
,
2361 target_ulong len
, int type
)
2363 struct kvm_sw_breakpoint
*bp
;
2366 if (type
== GDB_BREAKPOINT_SW
) {
2367 bp
= kvm_find_sw_breakpoint(cpu
, addr
);
2372 if (bp
->use_count
> 1) {
2377 err
= kvm_arch_remove_sw_breakpoint(cpu
, bp
);
2382 QTAILQ_REMOVE(&cpu
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
2385 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
2392 err
= kvm_update_guest_debug(cpu
, 0);
2400 void kvm_remove_all_breakpoints(CPUState
*cpu
)
2402 struct kvm_sw_breakpoint
*bp
, *next
;
2403 KVMState
*s
= cpu
->kvm_state
;
2406 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
2407 if (kvm_arch_remove_sw_breakpoint(cpu
, bp
) != 0) {
2408 /* Try harder to find a CPU that currently sees the breakpoint. */
2409 CPU_FOREACH(tmpcpu
) {
2410 if (kvm_arch_remove_sw_breakpoint(tmpcpu
, bp
) == 0) {
2415 QTAILQ_REMOVE(&s
->kvm_sw_breakpoints
, bp
, entry
);
2418 kvm_arch_remove_all_hw_breakpoints();
2421 kvm_update_guest_debug(cpu
, 0);
2425 #else /* !KVM_CAP_SET_GUEST_DEBUG */
2427 int kvm_update_guest_debug(CPUState
*cpu
, unsigned long reinject_trap
)
2432 int kvm_insert_breakpoint(CPUState
*cpu
, target_ulong addr
,
2433 target_ulong len
, int type
)
2438 int kvm_remove_breakpoint(CPUState
*cpu
, target_ulong addr
,
2439 target_ulong len
, int type
)
2444 void kvm_remove_all_breakpoints(CPUState
*cpu
)
2447 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
2449 static int kvm_set_signal_mask(CPUState
*cpu
, const sigset_t
*sigset
)
2451 KVMState
*s
= kvm_state
;
2452 struct kvm_signal_mask
*sigmask
;
2455 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
2457 sigmask
->len
= s
->sigmask_len
;
2458 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
2459 r
= kvm_vcpu_ioctl(cpu
, KVM_SET_SIGNAL_MASK
, sigmask
);
2465 static void kvm_ipi_signal(int sig
)
2468 assert(kvm_immediate_exit
);
2469 kvm_cpu_kick(current_cpu
);
2473 void kvm_init_cpu_signals(CPUState
*cpu
)
2477 struct sigaction sigact
;
2479 memset(&sigact
, 0, sizeof(sigact
));
2480 sigact
.sa_handler
= kvm_ipi_signal
;
2481 sigaction(SIG_IPI
, &sigact
, NULL
);
2483 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2484 #if defined KVM_HAVE_MCE_INJECTION
2485 sigdelset(&set
, SIGBUS
);
2486 pthread_sigmask(SIG_SETMASK
, &set
, NULL
);
2488 sigdelset(&set
, SIG_IPI
);
2489 if (kvm_immediate_exit
) {
2490 r
= pthread_sigmask(SIG_SETMASK
, &set
, NULL
);
2492 r
= kvm_set_signal_mask(cpu
, &set
);
2495 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
2500 /* Called asynchronously in VCPU thread. */
2501 int kvm_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
)
2503 #ifdef KVM_HAVE_MCE_INJECTION
2504 if (have_sigbus_pending
) {
2507 have_sigbus_pending
= true;
2508 pending_sigbus_addr
= addr
;
2509 pending_sigbus_code
= code
;
2510 atomic_set(&cpu
->exit_request
, 1);
2517 /* Called synchronously (via signalfd) in main thread. */
2518 int kvm_on_sigbus(int code
, void *addr
)
2520 #ifdef KVM_HAVE_MCE_INJECTION
2521 /* Action required MCE kills the process if SIGBUS is blocked. Because
2522 * that's what happens in the I/O thread, where we handle MCE via signalfd,
2523 * we can only get action optional here.
2525 assert(code
!= BUS_MCEERR_AR
);
2526 kvm_arch_on_sigbus_vcpu(first_cpu
, code
, addr
);
2533 int kvm_create_device(KVMState
*s
, uint64_t type
, bool test
)
2536 struct kvm_create_device create_dev
;
2538 create_dev
.type
= type
;
2540 create_dev
.flags
= test
? KVM_CREATE_DEVICE_TEST
: 0;
2542 if (!kvm_check_extension(s
, KVM_CAP_DEVICE_CTRL
)) {
2546 ret
= kvm_vm_ioctl(s
, KVM_CREATE_DEVICE
, &create_dev
);
2551 return test
? 0 : create_dev
.fd
;
2554 bool kvm_device_supported(int vmfd
, uint64_t type
)
2556 struct kvm_create_device create_dev
= {
2559 .flags
= KVM_CREATE_DEVICE_TEST
,
2562 if (ioctl(vmfd
, KVM_CHECK_EXTENSION
, KVM_CAP_DEVICE_CTRL
) <= 0) {
2566 return (ioctl(vmfd
, KVM_CREATE_DEVICE
, &create_dev
) >= 0);
2569 int kvm_set_one_reg(CPUState
*cs
, uint64_t id
, void *source
)
2571 struct kvm_one_reg reg
;
2575 reg
.addr
= (uintptr_t) source
;
2576 r
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
2578 trace_kvm_failed_reg_set(id
, strerror(-r
));
2583 int kvm_get_one_reg(CPUState
*cs
, uint64_t id
, void *target
)
2585 struct kvm_one_reg reg
;
2589 reg
.addr
= (uintptr_t) target
;
2590 r
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
2592 trace_kvm_failed_reg_get(id
, strerror(-r
));
2597 static void kvm_accel_class_init(ObjectClass
*oc
, void *data
)
2599 AccelClass
*ac
= ACCEL_CLASS(oc
);
2601 ac
->init_machine
= kvm_init
;
2602 ac
->allowed
= &kvm_allowed
;
2605 static const TypeInfo kvm_accel_type
= {
2606 .name
= TYPE_KVM_ACCEL
,
2607 .parent
= TYPE_ACCEL
,
2608 .class_init
= kvm_accel_class_init
,
2609 .instance_size
= sizeof(KVMState
),
2612 static void kvm_type_init(void)
2614 type_register_static(&kvm_accel_type
);
2617 type_init(kvm_type_init
);