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"
26 #include "qapi/error.h"
28 #include "hw/pci/msi.h"
29 #include "hw/pci/msix.h"
30 #include "hw/s390x/adapter.h"
31 #include "exec/gdbstub.h"
32 #include "sysemu/kvm_int.h"
33 #include "sysemu/cpus.h"
34 #include "qemu/bswap.h"
35 #include "exec/memory.h"
36 #include "exec/ram_addr.h"
37 #include "exec/address-spaces.h"
38 #include "qemu/event_notifier.h"
39 #include "trace-root.h"
42 #include "hw/boards.h"
44 /* This check must be after config-host.h is included */
46 #include <sys/eventfd.h>
49 /* KVM uses PAGE_SIZE in its definition of KVM_COALESCED_MMIO_MAX. We
50 * need to use the real host PAGE_SIZE, as that's what KVM will use.
52 #define PAGE_SIZE getpagesize()
57 #define DPRINTF(fmt, ...) \
58 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
60 #define DPRINTF(fmt, ...) \
64 #define KVM_MSI_HASHTAB_SIZE 256
66 struct KVMParkedVcpu
{
67 unsigned long vcpu_id
;
69 QLIST_ENTRY(KVMParkedVcpu
) node
;
74 AccelState parent_obj
;
80 struct kvm_coalesced_mmio_ring
*coalesced_mmio_ring
;
81 bool coalesced_flush_in_progress
;
82 int broken_set_mem_region
;
84 int robust_singlestep
;
86 #ifdef KVM_CAP_SET_GUEST_DEBUG
87 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
;
91 /* The man page (and posix) say ioctl numbers are signed int, but
92 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
93 * unsigned, and treating them as signed here can break things */
94 unsigned irq_set_ioctl
;
95 unsigned int sigmask_len
;
97 #ifdef KVM_CAP_IRQ_ROUTING
98 struct kvm_irq_routing
*irq_routes
;
99 int nr_allocated_irq_routes
;
100 unsigned long *used_gsi_bitmap
;
101 unsigned int gsi_count
;
102 QTAILQ_HEAD(msi_hashtab
, KVMMSIRoute
) msi_hashtab
[KVM_MSI_HASHTAB_SIZE
];
104 KVMMemoryListener memory_listener
;
105 QLIST_HEAD(, KVMParkedVcpu
) kvm_parked_vcpus
;
109 bool kvm_kernel_irqchip
;
110 bool kvm_split_irqchip
;
111 bool kvm_async_interrupts_allowed
;
112 bool kvm_halt_in_kernel_allowed
;
113 bool kvm_eventfds_allowed
;
114 bool kvm_irqfds_allowed
;
115 bool kvm_resamplefds_allowed
;
116 bool kvm_msi_via_irqfd_allowed
;
117 bool kvm_gsi_routing_allowed
;
118 bool kvm_gsi_direct_mapping
;
120 bool kvm_readonly_mem_allowed
;
121 bool kvm_vm_attributes_allowed
;
122 bool kvm_direct_msi_allowed
;
123 bool kvm_ioeventfd_any_length_allowed
;
124 bool kvm_msi_use_devid
;
125 static bool kvm_immediate_exit
;
127 static const KVMCapabilityInfo kvm_required_capabilites
[] = {
128 KVM_CAP_INFO(USER_MEMORY
),
129 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS
),
133 int kvm_get_max_memslots(void)
135 KVMState
*s
= KVM_STATE(current_machine
->accelerator
);
140 static KVMSlot
*kvm_get_free_slot(KVMMemoryListener
*kml
)
142 KVMState
*s
= kvm_state
;
145 for (i
= 0; i
< s
->nr_slots
; i
++) {
146 if (kml
->slots
[i
].memory_size
== 0) {
147 return &kml
->slots
[i
];
154 bool kvm_has_free_slot(MachineState
*ms
)
156 KVMState
*s
= KVM_STATE(ms
->accelerator
);
158 return kvm_get_free_slot(&s
->memory_listener
);
161 static KVMSlot
*kvm_alloc_slot(KVMMemoryListener
*kml
)
163 KVMSlot
*slot
= kvm_get_free_slot(kml
);
169 fprintf(stderr
, "%s: no free slot available\n", __func__
);
173 static KVMSlot
*kvm_lookup_matching_slot(KVMMemoryListener
*kml
,
177 KVMState
*s
= kvm_state
;
180 for (i
= 0; i
< s
->nr_slots
; i
++) {
181 KVMSlot
*mem
= &kml
->slots
[i
];
183 if (start_addr
== mem
->start_addr
&&
184 end_addr
== mem
->start_addr
+ mem
->memory_size
) {
193 * Find overlapping slot with lowest start address
195 static KVMSlot
*kvm_lookup_overlapping_slot(KVMMemoryListener
*kml
,
199 KVMState
*s
= kvm_state
;
200 KVMSlot
*found
= NULL
;
203 for (i
= 0; i
< s
->nr_slots
; i
++) {
204 KVMSlot
*mem
= &kml
->slots
[i
];
206 if (mem
->memory_size
== 0 ||
207 (found
&& found
->start_addr
< mem
->start_addr
)) {
211 if (end_addr
> mem
->start_addr
&&
212 start_addr
< mem
->start_addr
+ mem
->memory_size
) {
220 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram
,
223 KVMMemoryListener
*kml
= &s
->memory_listener
;
226 for (i
= 0; i
< s
->nr_slots
; i
++) {
227 KVMSlot
*mem
= &kml
->slots
[i
];
229 if (ram
>= mem
->ram
&& ram
< mem
->ram
+ mem
->memory_size
) {
230 *phys_addr
= mem
->start_addr
+ (ram
- mem
->ram
);
238 static int kvm_set_user_memory_region(KVMMemoryListener
*kml
, KVMSlot
*slot
)
240 KVMState
*s
= kvm_state
;
241 struct kvm_userspace_memory_region mem
;
243 mem
.slot
= slot
->slot
| (kml
->as_id
<< 16);
244 mem
.guest_phys_addr
= slot
->start_addr
;
245 mem
.userspace_addr
= (unsigned long)slot
->ram
;
246 mem
.flags
= slot
->flags
;
248 if (slot
->memory_size
&& mem
.flags
& KVM_MEM_READONLY
) {
249 /* Set the slot size to 0 before setting the slot to the desired
250 * value. This is needed based on KVM commit 75d61fbc. */
252 kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
254 mem
.memory_size
= slot
->memory_size
;
255 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
258 int kvm_destroy_vcpu(CPUState
*cpu
)
260 KVMState
*s
= kvm_state
;
262 struct KVMParkedVcpu
*vcpu
= NULL
;
265 DPRINTF("kvm_destroy_vcpu\n");
267 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
270 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
274 ret
= munmap(cpu
->kvm_run
, mmap_size
);
279 vcpu
= g_malloc0(sizeof(*vcpu
));
280 vcpu
->vcpu_id
= kvm_arch_vcpu_id(cpu
);
281 vcpu
->kvm_fd
= cpu
->kvm_fd
;
282 QLIST_INSERT_HEAD(&kvm_state
->kvm_parked_vcpus
, vcpu
, node
);
287 static int kvm_get_vcpu(KVMState
*s
, unsigned long vcpu_id
)
289 struct KVMParkedVcpu
*cpu
;
291 QLIST_FOREACH(cpu
, &s
->kvm_parked_vcpus
, node
) {
292 if (cpu
->vcpu_id
== vcpu_id
) {
295 QLIST_REMOVE(cpu
, node
);
296 kvm_fd
= cpu
->kvm_fd
;
302 return kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, (void *)vcpu_id
);
305 int kvm_init_vcpu(CPUState
*cpu
)
307 KVMState
*s
= kvm_state
;
311 DPRINTF("kvm_init_vcpu\n");
313 ret
= kvm_get_vcpu(s
, kvm_arch_vcpu_id(cpu
));
315 DPRINTF("kvm_create_vcpu failed\n");
321 cpu
->kvm_vcpu_dirty
= true;
323 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
326 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
330 cpu
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
332 if (cpu
->kvm_run
== MAP_FAILED
) {
334 DPRINTF("mmap'ing vcpu state failed\n");
338 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
) {
339 s
->coalesced_mmio_ring
=
340 (void *)cpu
->kvm_run
+ s
->coalesced_mmio
* PAGE_SIZE
;
343 ret
= kvm_arch_init_vcpu(cpu
);
349 * dirty pages logging control
352 static int kvm_mem_flags(MemoryRegion
*mr
)
354 bool readonly
= mr
->readonly
|| memory_region_is_romd(mr
);
357 if (memory_region_get_dirty_log_mask(mr
) != 0) {
358 flags
|= KVM_MEM_LOG_DIRTY_PAGES
;
360 if (readonly
&& kvm_readonly_mem_allowed
) {
361 flags
|= KVM_MEM_READONLY
;
366 static int kvm_slot_update_flags(KVMMemoryListener
*kml
, KVMSlot
*mem
,
371 old_flags
= mem
->flags
;
372 mem
->flags
= kvm_mem_flags(mr
);
374 /* If nothing changed effectively, no need to issue ioctl */
375 if (mem
->flags
== old_flags
) {
379 return kvm_set_user_memory_region(kml
, mem
);
382 static int kvm_section_update_flags(KVMMemoryListener
*kml
,
383 MemoryRegionSection
*section
)
385 hwaddr phys_addr
= section
->offset_within_address_space
;
386 ram_addr_t size
= int128_get64(section
->size
);
387 KVMSlot
*mem
= kvm_lookup_matching_slot(kml
, phys_addr
, phys_addr
+ size
);
392 return kvm_slot_update_flags(kml
, mem
, section
->mr
);
396 static void kvm_log_start(MemoryListener
*listener
,
397 MemoryRegionSection
*section
,
400 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
407 r
= kvm_section_update_flags(kml
, section
);
413 static void kvm_log_stop(MemoryListener
*listener
,
414 MemoryRegionSection
*section
,
417 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
424 r
= kvm_section_update_flags(kml
, section
);
430 /* get kvm's dirty pages bitmap and update qemu's */
431 static int kvm_get_dirty_pages_log_range(MemoryRegionSection
*section
,
432 unsigned long *bitmap
)
434 ram_addr_t start
= section
->offset_within_region
+
435 memory_region_get_ram_addr(section
->mr
);
436 ram_addr_t pages
= int128_get64(section
->size
) / getpagesize();
438 cpu_physical_memory_set_dirty_lebitmap(bitmap
, start
, pages
);
442 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
445 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
446 * This function updates qemu's dirty bitmap using
447 * memory_region_set_dirty(). This means all bits are set
450 * @start_add: start of logged region.
451 * @end_addr: end of logged region.
453 static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener
*kml
,
454 MemoryRegionSection
*section
)
456 KVMState
*s
= kvm_state
;
457 unsigned long size
, allocated_size
= 0;
458 struct kvm_dirty_log d
= {};
461 hwaddr start_addr
= section
->offset_within_address_space
;
462 hwaddr end_addr
= start_addr
+ int128_get64(section
->size
);
464 d
.dirty_bitmap
= NULL
;
465 while (start_addr
< end_addr
) {
466 mem
= kvm_lookup_overlapping_slot(kml
, start_addr
, end_addr
);
471 /* XXX bad kernel interface alert
472 * For dirty bitmap, kernel allocates array of size aligned to
473 * bits-per-long. But for case when the kernel is 64bits and
474 * the userspace is 32bits, userspace can't align to the same
475 * bits-per-long, since sizeof(long) is different between kernel
476 * and user space. This way, userspace will provide buffer which
477 * may be 4 bytes less than the kernel will use, resulting in
478 * userspace memory corruption (which is not detectable by valgrind
479 * too, in most cases).
480 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
481 * a hope that sizeof(long) won't become >8 any time soon.
483 size
= ALIGN(((mem
->memory_size
) >> TARGET_PAGE_BITS
),
484 /*HOST_LONG_BITS*/ 64) / 8;
485 if (!d
.dirty_bitmap
) {
486 d
.dirty_bitmap
= g_malloc(size
);
487 } else if (size
> allocated_size
) {
488 d
.dirty_bitmap
= g_realloc(d
.dirty_bitmap
, size
);
490 allocated_size
= size
;
491 memset(d
.dirty_bitmap
, 0, allocated_size
);
493 d
.slot
= mem
->slot
| (kml
->as_id
<< 16);
494 if (kvm_vm_ioctl(s
, KVM_GET_DIRTY_LOG
, &d
) == -1) {
495 DPRINTF("ioctl failed %d\n", errno
);
500 kvm_get_dirty_pages_log_range(section
, d
.dirty_bitmap
);
501 start_addr
= mem
->start_addr
+ mem
->memory_size
;
503 g_free(d
.dirty_bitmap
);
508 static void kvm_coalesce_mmio_region(MemoryListener
*listener
,
509 MemoryRegionSection
*secion
,
510 hwaddr start
, hwaddr size
)
512 KVMState
*s
= kvm_state
;
514 if (s
->coalesced_mmio
) {
515 struct kvm_coalesced_mmio_zone zone
;
521 (void)kvm_vm_ioctl(s
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
525 static void kvm_uncoalesce_mmio_region(MemoryListener
*listener
,
526 MemoryRegionSection
*secion
,
527 hwaddr start
, hwaddr size
)
529 KVMState
*s
= kvm_state
;
531 if (s
->coalesced_mmio
) {
532 struct kvm_coalesced_mmio_zone zone
;
538 (void)kvm_vm_ioctl(s
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
542 int kvm_check_extension(KVMState
*s
, unsigned int extension
)
546 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
554 int kvm_vm_check_extension(KVMState
*s
, unsigned int extension
)
558 ret
= kvm_vm_ioctl(s
, KVM_CHECK_EXTENSION
, extension
);
560 /* VM wide version not implemented, use global one instead */
561 ret
= kvm_check_extension(s
, extension
);
567 static uint32_t adjust_ioeventfd_endianness(uint32_t val
, uint32_t size
)
569 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
570 /* The kernel expects ioeventfd values in HOST_WORDS_BIGENDIAN
571 * endianness, but the memory core hands them in target endianness.
572 * For example, PPC is always treated as big-endian even if running
573 * on KVM and on PPC64LE. Correct here.
587 static int kvm_set_ioeventfd_mmio(int fd
, hwaddr addr
, uint32_t val
,
588 bool assign
, uint32_t size
, bool datamatch
)
591 struct kvm_ioeventfd iofd
= {
592 .datamatch
= datamatch
? adjust_ioeventfd_endianness(val
, size
) : 0,
599 if (!kvm_enabled()) {
604 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DATAMATCH
;
607 iofd
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
610 ret
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &iofd
);
619 static int kvm_set_ioeventfd_pio(int fd
, uint16_t addr
, uint16_t val
,
620 bool assign
, uint32_t size
, bool datamatch
)
622 struct kvm_ioeventfd kick
= {
623 .datamatch
= datamatch
? adjust_ioeventfd_endianness(val
, size
) : 0,
625 .flags
= KVM_IOEVENTFD_FLAG_PIO
,
630 if (!kvm_enabled()) {
634 kick
.flags
|= KVM_IOEVENTFD_FLAG_DATAMATCH
;
637 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
639 r
= kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
647 static int kvm_check_many_ioeventfds(void)
649 /* Userspace can use ioeventfd for io notification. This requires a host
650 * that supports eventfd(2) and an I/O thread; since eventfd does not
651 * support SIGIO it cannot interrupt the vcpu.
653 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
654 * can avoid creating too many ioeventfds.
656 #if defined(CONFIG_EVENTFD)
659 for (i
= 0; i
< ARRAY_SIZE(ioeventfds
); i
++) {
660 ioeventfds
[i
] = eventfd(0, EFD_CLOEXEC
);
661 if (ioeventfds
[i
] < 0) {
664 ret
= kvm_set_ioeventfd_pio(ioeventfds
[i
], 0, i
, true, 2, true);
666 close(ioeventfds
[i
]);
671 /* Decide whether many devices are supported or not */
672 ret
= i
== ARRAY_SIZE(ioeventfds
);
675 kvm_set_ioeventfd_pio(ioeventfds
[i
], 0, i
, false, 2, true);
676 close(ioeventfds
[i
]);
684 static const KVMCapabilityInfo
*
685 kvm_check_extension_list(KVMState
*s
, const KVMCapabilityInfo
*list
)
688 if (!kvm_check_extension(s
, list
->value
)) {
696 static void kvm_set_phys_mem(KVMMemoryListener
*kml
,
697 MemoryRegionSection
*section
, bool add
)
699 KVMState
*s
= kvm_state
;
702 MemoryRegion
*mr
= section
->mr
;
703 bool writeable
= !mr
->readonly
&& !mr
->rom_device
;
704 hwaddr start_addr
= section
->offset_within_address_space
;
705 ram_addr_t size
= int128_get64(section
->size
);
709 /* kvm works in page size chunks, but the function may be called
710 with sub-page size and unaligned start address. Pad the start
711 address to next and truncate size to previous page boundary. */
712 delta
= qemu_real_host_page_size
- (start_addr
& ~qemu_real_host_page_mask
);
713 delta
&= ~qemu_real_host_page_mask
;
719 size
&= qemu_real_host_page_mask
;
720 if (!size
|| (start_addr
& ~qemu_real_host_page_mask
)) {
724 if (!memory_region_is_ram(mr
)) {
725 if (writeable
|| !kvm_readonly_mem_allowed
) {
727 } else if (!mr
->romd_mode
) {
728 /* If the memory device is not in romd_mode, then we actually want
729 * to remove the kvm memory slot so all accesses will trap. */
734 ram
= memory_region_get_ram_ptr(mr
) + section
->offset_within_region
+ delta
;
737 mem
= kvm_lookup_overlapping_slot(kml
, start_addr
, start_addr
+ size
);
742 if (add
&& start_addr
>= mem
->start_addr
&&
743 (start_addr
+ size
<= mem
->start_addr
+ mem
->memory_size
) &&
744 (ram
- start_addr
== mem
->ram
- mem
->start_addr
)) {
745 /* The new slot fits into the existing one and comes with
746 * identical parameters - update flags and done. */
747 kvm_slot_update_flags(kml
, mem
, mr
);
753 if (mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
) {
754 kvm_physical_sync_dirty_bitmap(kml
, section
);
757 /* unregister the overlapping slot */
758 mem
->memory_size
= 0;
759 err
= kvm_set_user_memory_region(kml
, mem
);
761 fprintf(stderr
, "%s: error unregistering overlapping slot: %s\n",
762 __func__
, strerror(-err
));
766 /* Workaround for older KVM versions: we can't join slots, even not by
767 * unregistering the previous ones and then registering the larger
768 * slot. We have to maintain the existing fragmentation. Sigh.
770 * This workaround assumes that the new slot starts at the same
771 * address as the first existing one. If not or if some overlapping
772 * slot comes around later, we will fail (not seen in practice so far)
773 * - and actually require a recent KVM version. */
774 if (s
->broken_set_mem_region
&&
775 old
.start_addr
== start_addr
&& old
.memory_size
< size
&& add
) {
776 mem
= kvm_alloc_slot(kml
);
777 mem
->memory_size
= old
.memory_size
;
778 mem
->start_addr
= old
.start_addr
;
780 mem
->flags
= kvm_mem_flags(mr
);
782 err
= kvm_set_user_memory_region(kml
, mem
);
784 fprintf(stderr
, "%s: error updating slot: %s\n", __func__
,
789 start_addr
+= old
.memory_size
;
790 ram
+= old
.memory_size
;
791 size
-= old
.memory_size
;
795 /* register prefix slot */
796 if (old
.start_addr
< start_addr
) {
797 mem
= kvm_alloc_slot(kml
);
798 mem
->memory_size
= start_addr
- old
.start_addr
;
799 mem
->start_addr
= old
.start_addr
;
801 mem
->flags
= kvm_mem_flags(mr
);
803 err
= kvm_set_user_memory_region(kml
, mem
);
805 fprintf(stderr
, "%s: error registering prefix slot: %s\n",
806 __func__
, strerror(-err
));
808 fprintf(stderr
, "%s: This is probably because your kernel's " \
809 "PAGE_SIZE is too big. Please try to use 4k " \
810 "PAGE_SIZE!\n", __func__
);
816 /* register suffix slot */
817 if (old
.start_addr
+ old
.memory_size
> start_addr
+ size
) {
818 ram_addr_t size_delta
;
820 mem
= kvm_alloc_slot(kml
);
821 mem
->start_addr
= start_addr
+ size
;
822 size_delta
= mem
->start_addr
- old
.start_addr
;
823 mem
->memory_size
= old
.memory_size
- size_delta
;
824 mem
->ram
= old
.ram
+ size_delta
;
825 mem
->flags
= kvm_mem_flags(mr
);
827 err
= kvm_set_user_memory_region(kml
, mem
);
829 fprintf(stderr
, "%s: error registering suffix slot: %s\n",
830 __func__
, strerror(-err
));
836 /* in case the KVM bug workaround already "consumed" the new slot */
843 mem
= kvm_alloc_slot(kml
);
844 mem
->memory_size
= size
;
845 mem
->start_addr
= start_addr
;
847 mem
->flags
= kvm_mem_flags(mr
);
849 err
= kvm_set_user_memory_region(kml
, mem
);
851 fprintf(stderr
, "%s: error registering slot: %s\n", __func__
,
857 static void kvm_region_add(MemoryListener
*listener
,
858 MemoryRegionSection
*section
)
860 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
862 memory_region_ref(section
->mr
);
863 kvm_set_phys_mem(kml
, section
, true);
866 static void kvm_region_del(MemoryListener
*listener
,
867 MemoryRegionSection
*section
)
869 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
871 kvm_set_phys_mem(kml
, section
, false);
872 memory_region_unref(section
->mr
);
875 static void kvm_log_sync(MemoryListener
*listener
,
876 MemoryRegionSection
*section
)
878 KVMMemoryListener
*kml
= container_of(listener
, KVMMemoryListener
, listener
);
881 r
= kvm_physical_sync_dirty_bitmap(kml
, section
);
887 static void kvm_mem_ioeventfd_add(MemoryListener
*listener
,
888 MemoryRegionSection
*section
,
889 bool match_data
, uint64_t data
,
892 int fd
= event_notifier_get_fd(e
);
895 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
896 data
, true, int128_get64(section
->size
),
899 fprintf(stderr
, "%s: error adding ioeventfd: %s\n",
900 __func__
, strerror(-r
));
905 static void kvm_mem_ioeventfd_del(MemoryListener
*listener
,
906 MemoryRegionSection
*section
,
907 bool match_data
, uint64_t data
,
910 int fd
= event_notifier_get_fd(e
);
913 r
= kvm_set_ioeventfd_mmio(fd
, section
->offset_within_address_space
,
914 data
, false, int128_get64(section
->size
),
921 static void kvm_io_ioeventfd_add(MemoryListener
*listener
,
922 MemoryRegionSection
*section
,
923 bool match_data
, uint64_t data
,
926 int fd
= event_notifier_get_fd(e
);
929 r
= kvm_set_ioeventfd_pio(fd
, section
->offset_within_address_space
,
930 data
, true, int128_get64(section
->size
),
933 fprintf(stderr
, "%s: error adding ioeventfd: %s\n",
934 __func__
, strerror(-r
));
939 static void kvm_io_ioeventfd_del(MemoryListener
*listener
,
940 MemoryRegionSection
*section
,
941 bool match_data
, uint64_t data
,
945 int fd
= event_notifier_get_fd(e
);
948 r
= kvm_set_ioeventfd_pio(fd
, section
->offset_within_address_space
,
949 data
, false, int128_get64(section
->size
),
956 void kvm_memory_listener_register(KVMState
*s
, KVMMemoryListener
*kml
,
957 AddressSpace
*as
, int as_id
)
961 kml
->slots
= g_malloc0(s
->nr_slots
* sizeof(KVMSlot
));
964 for (i
= 0; i
< s
->nr_slots
; i
++) {
965 kml
->slots
[i
].slot
= i
;
968 kml
->listener
.region_add
= kvm_region_add
;
969 kml
->listener
.region_del
= kvm_region_del
;
970 kml
->listener
.log_start
= kvm_log_start
;
971 kml
->listener
.log_stop
= kvm_log_stop
;
972 kml
->listener
.log_sync
= kvm_log_sync
;
973 kml
->listener
.priority
= 10;
975 memory_listener_register(&kml
->listener
, as
);
978 static MemoryListener kvm_io_listener
= {
979 .eventfd_add
= kvm_io_ioeventfd_add
,
980 .eventfd_del
= kvm_io_ioeventfd_del
,
984 static void kvm_handle_interrupt(CPUState
*cpu
, int mask
)
986 cpu
->interrupt_request
|= mask
;
988 if (!qemu_cpu_is_self(cpu
)) {
993 int kvm_set_irq(KVMState
*s
, int irq
, int level
)
995 struct kvm_irq_level event
;
998 assert(kvm_async_interrupts_enabled());
1000 event
.level
= level
;
1002 ret
= kvm_vm_ioctl(s
, s
->irq_set_ioctl
, &event
);
1004 perror("kvm_set_irq");
1008 return (s
->irq_set_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
1011 #ifdef KVM_CAP_IRQ_ROUTING
1012 typedef struct KVMMSIRoute
{
1013 struct kvm_irq_routing_entry kroute
;
1014 QTAILQ_ENTRY(KVMMSIRoute
) entry
;
1017 static void set_gsi(KVMState
*s
, unsigned int gsi
)
1019 set_bit(gsi
, s
->used_gsi_bitmap
);
1022 static void clear_gsi(KVMState
*s
, unsigned int gsi
)
1024 clear_bit(gsi
, s
->used_gsi_bitmap
);
1027 void kvm_init_irq_routing(KVMState
*s
)
1031 gsi_count
= kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
) - 1;
1032 if (gsi_count
> 0) {
1033 /* Round up so we can search ints using ffs */
1034 s
->used_gsi_bitmap
= bitmap_new(gsi_count
);
1035 s
->gsi_count
= gsi_count
;
1038 s
->irq_routes
= g_malloc0(sizeof(*s
->irq_routes
));
1039 s
->nr_allocated_irq_routes
= 0;
1041 if (!kvm_direct_msi_allowed
) {
1042 for (i
= 0; i
< KVM_MSI_HASHTAB_SIZE
; i
++) {
1043 QTAILQ_INIT(&s
->msi_hashtab
[i
]);
1047 kvm_arch_init_irq_routing(s
);
1050 void kvm_irqchip_commit_routes(KVMState
*s
)
1054 if (kvm_gsi_direct_mapping()) {
1058 if (!kvm_gsi_routing_enabled()) {
1062 s
->irq_routes
->flags
= 0;
1063 trace_kvm_irqchip_commit_routes();
1064 ret
= kvm_vm_ioctl(s
, KVM_SET_GSI_ROUTING
, s
->irq_routes
);
1068 static void kvm_add_routing_entry(KVMState
*s
,
1069 struct kvm_irq_routing_entry
*entry
)
1071 struct kvm_irq_routing_entry
*new;
1074 if (s
->irq_routes
->nr
== s
->nr_allocated_irq_routes
) {
1075 n
= s
->nr_allocated_irq_routes
* 2;
1079 size
= sizeof(struct kvm_irq_routing
);
1080 size
+= n
* sizeof(*new);
1081 s
->irq_routes
= g_realloc(s
->irq_routes
, size
);
1082 s
->nr_allocated_irq_routes
= n
;
1084 n
= s
->irq_routes
->nr
++;
1085 new = &s
->irq_routes
->entries
[n
];
1089 set_gsi(s
, entry
->gsi
);
1092 static int kvm_update_routing_entry(KVMState
*s
,
1093 struct kvm_irq_routing_entry
*new_entry
)
1095 struct kvm_irq_routing_entry
*entry
;
1098 for (n
= 0; n
< s
->irq_routes
->nr
; n
++) {
1099 entry
= &s
->irq_routes
->entries
[n
];
1100 if (entry
->gsi
!= new_entry
->gsi
) {
1104 if(!memcmp(entry
, new_entry
, sizeof *entry
)) {
1108 *entry
= *new_entry
;
1116 void kvm_irqchip_add_irq_route(KVMState
*s
, int irq
, int irqchip
, int pin
)
1118 struct kvm_irq_routing_entry e
= {};
1120 assert(pin
< s
->gsi_count
);
1123 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1125 e
.u
.irqchip
.irqchip
= irqchip
;
1126 e
.u
.irqchip
.pin
= pin
;
1127 kvm_add_routing_entry(s
, &e
);
1130 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1132 struct kvm_irq_routing_entry
*e
;
1135 if (kvm_gsi_direct_mapping()) {
1139 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
1140 e
= &s
->irq_routes
->entries
[i
];
1141 if (e
->gsi
== virq
) {
1142 s
->irq_routes
->nr
--;
1143 *e
= s
->irq_routes
->entries
[s
->irq_routes
->nr
];
1147 kvm_arch_release_virq_post(virq
);
1148 trace_kvm_irqchip_release_virq(virq
);
1151 static unsigned int kvm_hash_msi(uint32_t data
)
1153 /* This is optimized for IA32 MSI layout. However, no other arch shall
1154 * repeat the mistake of not providing a direct MSI injection API. */
1158 static void kvm_flush_dynamic_msi_routes(KVMState
*s
)
1160 KVMMSIRoute
*route
, *next
;
1163 for (hash
= 0; hash
< KVM_MSI_HASHTAB_SIZE
; hash
++) {
1164 QTAILQ_FOREACH_SAFE(route
, &s
->msi_hashtab
[hash
], entry
, next
) {
1165 kvm_irqchip_release_virq(s
, route
->kroute
.gsi
);
1166 QTAILQ_REMOVE(&s
->msi_hashtab
[hash
], route
, entry
);
1172 static int kvm_irqchip_get_virq(KVMState
*s
)
1177 * PIC and IOAPIC share the first 16 GSI numbers, thus the available
1178 * GSI numbers are more than the number of IRQ route. Allocating a GSI
1179 * number can succeed even though a new route entry cannot be added.
1180 * When this happens, flush dynamic MSI entries to free IRQ route entries.
1182 if (!kvm_direct_msi_allowed
&& s
->irq_routes
->nr
== s
->gsi_count
) {
1183 kvm_flush_dynamic_msi_routes(s
);
1186 /* Return the lowest unused GSI in the bitmap */
1187 next_virq
= find_first_zero_bit(s
->used_gsi_bitmap
, s
->gsi_count
);
1188 if (next_virq
>= s
->gsi_count
) {
1195 static KVMMSIRoute
*kvm_lookup_msi_route(KVMState
*s
, MSIMessage msg
)
1197 unsigned int hash
= kvm_hash_msi(msg
.data
);
1200 QTAILQ_FOREACH(route
, &s
->msi_hashtab
[hash
], entry
) {
1201 if (route
->kroute
.u
.msi
.address_lo
== (uint32_t)msg
.address
&&
1202 route
->kroute
.u
.msi
.address_hi
== (msg
.address
>> 32) &&
1203 route
->kroute
.u
.msi
.data
== le32_to_cpu(msg
.data
)) {
1210 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1215 if (kvm_direct_msi_allowed
) {
1216 msi
.address_lo
= (uint32_t)msg
.address
;
1217 msi
.address_hi
= msg
.address
>> 32;
1218 msi
.data
= le32_to_cpu(msg
.data
);
1220 memset(msi
.pad
, 0, sizeof(msi
.pad
));
1222 return kvm_vm_ioctl(s
, KVM_SIGNAL_MSI
, &msi
);
1225 route
= kvm_lookup_msi_route(s
, msg
);
1229 virq
= kvm_irqchip_get_virq(s
);
1234 route
= g_malloc0(sizeof(KVMMSIRoute
));
1235 route
->kroute
.gsi
= virq
;
1236 route
->kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1237 route
->kroute
.flags
= 0;
1238 route
->kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1239 route
->kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1240 route
->kroute
.u
.msi
.data
= le32_to_cpu(msg
.data
);
1242 kvm_add_routing_entry(s
, &route
->kroute
);
1243 kvm_irqchip_commit_routes(s
);
1245 QTAILQ_INSERT_TAIL(&s
->msi_hashtab
[kvm_hash_msi(msg
.data
)], route
,
1249 assert(route
->kroute
.type
== KVM_IRQ_ROUTING_MSI
);
1251 return kvm_set_irq(s
, route
->kroute
.gsi
, 1);
1254 int kvm_irqchip_add_msi_route(KVMState
*s
, int vector
, PCIDevice
*dev
)
1256 struct kvm_irq_routing_entry kroute
= {};
1258 MSIMessage msg
= {0, 0};
1261 msg
= pci_get_msi_message(dev
, vector
);
1264 if (kvm_gsi_direct_mapping()) {
1265 return kvm_arch_msi_data_to_gsi(msg
.data
);
1268 if (!kvm_gsi_routing_enabled()) {
1272 virq
= kvm_irqchip_get_virq(s
);
1278 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1280 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1281 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1282 kroute
.u
.msi
.data
= le32_to_cpu(msg
.data
);
1283 if (kvm_msi_devid_required()) {
1284 kroute
.flags
= KVM_MSI_VALID_DEVID
;
1285 kroute
.u
.msi
.devid
= pci_requester_id(dev
);
1287 if (kvm_arch_fixup_msi_route(&kroute
, msg
.address
, msg
.data
, dev
)) {
1288 kvm_irqchip_release_virq(s
, virq
);
1292 trace_kvm_irqchip_add_msi_route(dev
? dev
->name
: (char *)"N/A",
1295 kvm_add_routing_entry(s
, &kroute
);
1296 kvm_arch_add_msi_route_post(&kroute
, vector
, dev
);
1297 kvm_irqchip_commit_routes(s
);
1302 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
,
1305 struct kvm_irq_routing_entry kroute
= {};
1307 if (kvm_gsi_direct_mapping()) {
1311 if (!kvm_irqchip_in_kernel()) {
1316 kroute
.type
= KVM_IRQ_ROUTING_MSI
;
1318 kroute
.u
.msi
.address_lo
= (uint32_t)msg
.address
;
1319 kroute
.u
.msi
.address_hi
= msg
.address
>> 32;
1320 kroute
.u
.msi
.data
= le32_to_cpu(msg
.data
);
1321 if (kvm_msi_devid_required()) {
1322 kroute
.flags
= KVM_MSI_VALID_DEVID
;
1323 kroute
.u
.msi
.devid
= pci_requester_id(dev
);
1325 if (kvm_arch_fixup_msi_route(&kroute
, msg
.address
, msg
.data
, dev
)) {
1329 trace_kvm_irqchip_update_msi_route(virq
);
1331 return kvm_update_routing_entry(s
, &kroute
);
1334 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int rfd
, int virq
,
1337 struct kvm_irqfd irqfd
= {
1340 .flags
= assign
? 0 : KVM_IRQFD_FLAG_DEASSIGN
,
1344 irqfd
.flags
|= KVM_IRQFD_FLAG_RESAMPLE
;
1345 irqfd
.resamplefd
= rfd
;
1348 if (!kvm_irqfds_enabled()) {
1352 return kvm_vm_ioctl(s
, KVM_IRQFD
, &irqfd
);
1355 int kvm_irqchip_add_adapter_route(KVMState
*s
, AdapterInfo
*adapter
)
1357 struct kvm_irq_routing_entry kroute
= {};
1360 if (!kvm_gsi_routing_enabled()) {
1364 virq
= kvm_irqchip_get_virq(s
);
1370 kroute
.type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
1372 kroute
.u
.adapter
.summary_addr
= adapter
->summary_addr
;
1373 kroute
.u
.adapter
.ind_addr
= adapter
->ind_addr
;
1374 kroute
.u
.adapter
.summary_offset
= adapter
->summary_offset
;
1375 kroute
.u
.adapter
.ind_offset
= adapter
->ind_offset
;
1376 kroute
.u
.adapter
.adapter_id
= adapter
->adapter_id
;
1378 kvm_add_routing_entry(s
, &kroute
);
1383 int kvm_irqchip_add_hv_sint_route(KVMState
*s
, uint32_t vcpu
, uint32_t sint
)
1385 struct kvm_irq_routing_entry kroute
= {};
1388 if (!kvm_gsi_routing_enabled()) {
1391 if (!kvm_check_extension(s
, KVM_CAP_HYPERV_SYNIC
)) {
1394 virq
= kvm_irqchip_get_virq(s
);
1400 kroute
.type
= KVM_IRQ_ROUTING_HV_SINT
;
1402 kroute
.u
.hv_sint
.vcpu
= vcpu
;
1403 kroute
.u
.hv_sint
.sint
= sint
;
1405 kvm_add_routing_entry(s
, &kroute
);
1406 kvm_irqchip_commit_routes(s
);
1411 #else /* !KVM_CAP_IRQ_ROUTING */
1413 void kvm_init_irq_routing(KVMState
*s
)
1417 void kvm_irqchip_release_virq(KVMState
*s
, int virq
)
1421 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
)
1426 int kvm_irqchip_add_msi_route(KVMState
*s
, int vector
, PCIDevice
*dev
)
1431 int kvm_irqchip_add_adapter_route(KVMState
*s
, AdapterInfo
*adapter
)
1436 int kvm_irqchip_add_hv_sint_route(KVMState
*s
, uint32_t vcpu
, uint32_t sint
)
1441 static int kvm_irqchip_assign_irqfd(KVMState
*s
, int fd
, int virq
, bool assign
)
1446 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
)
1450 #endif /* !KVM_CAP_IRQ_ROUTING */
1452 int kvm_irqchip_add_irqfd_notifier_gsi(KVMState
*s
, EventNotifier
*n
,
1453 EventNotifier
*rn
, int virq
)
1455 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
),
1456 rn
? event_notifier_get_fd(rn
) : -1, virq
, true);
1459 int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState
*s
, EventNotifier
*n
,
1462 return kvm_irqchip_assign_irqfd(s
, event_notifier_get_fd(n
), -1, virq
,
1466 int kvm_irqchip_add_irqfd_notifier(KVMState
*s
, EventNotifier
*n
,
1467 EventNotifier
*rn
, qemu_irq irq
)
1470 gboolean found
= g_hash_table_lookup_extended(s
->gsimap
, irq
, &key
, &gsi
);
1475 return kvm_irqchip_add_irqfd_notifier_gsi(s
, n
, rn
, GPOINTER_TO_INT(gsi
));
1478 int kvm_irqchip_remove_irqfd_notifier(KVMState
*s
, EventNotifier
*n
,
1482 gboolean found
= g_hash_table_lookup_extended(s
->gsimap
, irq
, &key
, &gsi
);
1487 return kvm_irqchip_remove_irqfd_notifier_gsi(s
, n
, GPOINTER_TO_INT(gsi
));
1490 void kvm_irqchip_set_qemuirq_gsi(KVMState
*s
, qemu_irq irq
, int gsi
)
1492 g_hash_table_insert(s
->gsimap
, irq
, GINT_TO_POINTER(gsi
));
1495 static void kvm_irqchip_create(MachineState
*machine
, KVMState
*s
)
1499 if (kvm_check_extension(s
, KVM_CAP_IRQCHIP
)) {
1501 } else if (kvm_check_extension(s
, KVM_CAP_S390_IRQCHIP
)) {
1502 ret
= kvm_vm_enable_cap(s
, KVM_CAP_S390_IRQCHIP
, 0);
1504 fprintf(stderr
, "Enable kernel irqchip failed: %s\n", strerror(-ret
));
1511 /* First probe and see if there's a arch-specific hook to create the
1512 * in-kernel irqchip for us */
1513 ret
= kvm_arch_irqchip_create(machine
, s
);
1515 if (machine_kernel_irqchip_split(machine
)) {
1516 perror("Split IRQ chip mode not supported.");
1519 ret
= kvm_vm_ioctl(s
, KVM_CREATE_IRQCHIP
);
1523 fprintf(stderr
, "Create kernel irqchip failed: %s\n", strerror(-ret
));
1527 kvm_kernel_irqchip
= true;
1528 /* If we have an in-kernel IRQ chip then we must have asynchronous
1529 * interrupt delivery (though the reverse is not necessarily true)
1531 kvm_async_interrupts_allowed
= true;
1532 kvm_halt_in_kernel_allowed
= true;
1534 kvm_init_irq_routing(s
);
1536 s
->gsimap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1539 /* Find number of supported CPUs using the recommended
1540 * procedure from the kernel API documentation to cope with
1541 * older kernels that may be missing capabilities.
1543 static int kvm_recommended_vcpus(KVMState
*s
)
1545 int ret
= kvm_check_extension(s
, KVM_CAP_NR_VCPUS
);
1546 return (ret
) ? ret
: 4;
1549 static int kvm_max_vcpus(KVMState
*s
)
1551 int ret
= kvm_check_extension(s
, KVM_CAP_MAX_VCPUS
);
1552 return (ret
) ? ret
: kvm_recommended_vcpus(s
);
1555 static int kvm_max_vcpu_id(KVMState
*s
)
1557 int ret
= kvm_check_extension(s
, KVM_CAP_MAX_VCPU_ID
);
1558 return (ret
) ? ret
: kvm_max_vcpus(s
);
1561 bool kvm_vcpu_id_is_valid(int vcpu_id
)
1563 KVMState
*s
= KVM_STATE(current_machine
->accelerator
);
1564 return vcpu_id
>= 0 && vcpu_id
< kvm_max_vcpu_id(s
);
1567 static int kvm_init(MachineState
*ms
)
1569 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
1570 static const char upgrade_note
[] =
1571 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1572 "(see http://sourceforge.net/projects/kvm).\n";
1577 { "SMP", smp_cpus
},
1578 { "hotpluggable", max_cpus
},
1581 int soft_vcpus_limit
, hard_vcpus_limit
;
1583 const KVMCapabilityInfo
*missing_cap
;
1586 const char *kvm_type
;
1588 s
= KVM_STATE(ms
->accelerator
);
1591 * On systems where the kernel can support different base page
1592 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1593 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1594 * page size for the system though.
1596 assert(TARGET_PAGE_SIZE
<= getpagesize());
1600 #ifdef KVM_CAP_SET_GUEST_DEBUG
1601 QTAILQ_INIT(&s
->kvm_sw_breakpoints
);
1603 QLIST_INIT(&s
->kvm_parked_vcpus
);
1605 s
->fd
= qemu_open("/dev/kvm", O_RDWR
);
1607 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
1612 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
1613 if (ret
< KVM_API_VERSION
) {
1617 fprintf(stderr
, "kvm version too old\n");
1621 if (ret
> KVM_API_VERSION
) {
1623 fprintf(stderr
, "kvm version not supported\n");
1627 kvm_immediate_exit
= kvm_check_extension(s
, KVM_CAP_IMMEDIATE_EXIT
);
1628 s
->nr_slots
= kvm_check_extension(s
, KVM_CAP_NR_MEMSLOTS
);
1630 /* If unspecified, use the default value */
1635 /* check the vcpu limits */
1636 soft_vcpus_limit
= kvm_recommended_vcpus(s
);
1637 hard_vcpus_limit
= kvm_max_vcpus(s
);
1640 if (nc
->num
> soft_vcpus_limit
) {
1642 "Warning: Number of %s cpus requested (%d) exceeds "
1643 "the recommended cpus supported by KVM (%d)\n",
1644 nc
->name
, nc
->num
, soft_vcpus_limit
);
1646 if (nc
->num
> hard_vcpus_limit
) {
1647 fprintf(stderr
, "Number of %s cpus requested (%d) exceeds "
1648 "the maximum cpus supported by KVM (%d)\n",
1649 nc
->name
, nc
->num
, hard_vcpus_limit
);
1656 kvm_type
= qemu_opt_get(qemu_get_machine_opts(), "kvm-type");
1658 type
= mc
->kvm_type(kvm_type
);
1659 } else if (kvm_type
) {
1661 fprintf(stderr
, "Invalid argument kvm-type=%s\n", kvm_type
);
1666 ret
= kvm_ioctl(s
, KVM_CREATE_VM
, type
);
1667 } while (ret
== -EINTR
);
1670 fprintf(stderr
, "ioctl(KVM_CREATE_VM) failed: %d %s\n", -ret
,
1674 if (ret
== -EINVAL
) {
1676 "Host kernel setup problem detected. Please verify:\n");
1677 fprintf(stderr
, "- for kernels supporting the switch_amode or"
1678 " user_mode parameters, whether\n");
1680 " user space is running in primary address space\n");
1682 "- for kernels supporting the vm.allocate_pgste sysctl, "
1683 "whether it is enabled\n");
1690 missing_cap
= kvm_check_extension_list(s
, kvm_required_capabilites
);
1693 kvm_check_extension_list(s
, kvm_arch_required_capabilities
);
1697 fprintf(stderr
, "kvm does not support %s\n%s",
1698 missing_cap
->name
, upgrade_note
);
1702 s
->coalesced_mmio
= kvm_check_extension(s
, KVM_CAP_COALESCED_MMIO
);
1704 s
->broken_set_mem_region
= 1;
1705 ret
= kvm_check_extension(s
, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS
);
1707 s
->broken_set_mem_region
= 0;
1710 #ifdef KVM_CAP_VCPU_EVENTS
1711 s
->vcpu_events
= kvm_check_extension(s
, KVM_CAP_VCPU_EVENTS
);
1714 s
->robust_singlestep
=
1715 kvm_check_extension(s
, KVM_CAP_X86_ROBUST_SINGLESTEP
);
1717 #ifdef KVM_CAP_DEBUGREGS
1718 s
->debugregs
= kvm_check_extension(s
, KVM_CAP_DEBUGREGS
);
1721 #ifdef KVM_CAP_IRQ_ROUTING
1722 kvm_direct_msi_allowed
= (kvm_check_extension(s
, KVM_CAP_SIGNAL_MSI
) > 0);
1725 s
->intx_set_mask
= kvm_check_extension(s
, KVM_CAP_PCI_2_3
);
1727 s
->irq_set_ioctl
= KVM_IRQ_LINE
;
1728 if (kvm_check_extension(s
, KVM_CAP_IRQ_INJECT_STATUS
)) {
1729 s
->irq_set_ioctl
= KVM_IRQ_LINE_STATUS
;
1732 #ifdef KVM_CAP_READONLY_MEM
1733 kvm_readonly_mem_allowed
=
1734 (kvm_check_extension(s
, KVM_CAP_READONLY_MEM
) > 0);
1737 kvm_eventfds_allowed
=
1738 (kvm_check_extension(s
, KVM_CAP_IOEVENTFD
) > 0);
1740 kvm_irqfds_allowed
=
1741 (kvm_check_extension(s
, KVM_CAP_IRQFD
) > 0);
1743 kvm_resamplefds_allowed
=
1744 (kvm_check_extension(s
, KVM_CAP_IRQFD_RESAMPLE
) > 0);
1746 kvm_vm_attributes_allowed
=
1747 (kvm_check_extension(s
, KVM_CAP_VM_ATTRIBUTES
) > 0);
1749 kvm_ioeventfd_any_length_allowed
=
1750 (kvm_check_extension(s
, KVM_CAP_IOEVENTFD_ANY_LENGTH
) > 0);
1754 ret
= kvm_arch_init(ms
, s
);
1759 if (machine_kernel_irqchip_allowed(ms
)) {
1760 kvm_irqchip_create(ms
, s
);
1763 if (kvm_eventfds_allowed
) {
1764 s
->memory_listener
.listener
.eventfd_add
= kvm_mem_ioeventfd_add
;
1765 s
->memory_listener
.listener
.eventfd_del
= kvm_mem_ioeventfd_del
;
1767 s
->memory_listener
.listener
.coalesced_mmio_add
= kvm_coalesce_mmio_region
;
1768 s
->memory_listener
.listener
.coalesced_mmio_del
= kvm_uncoalesce_mmio_region
;
1770 kvm_memory_listener_register(s
, &s
->memory_listener
,
1771 &address_space_memory
, 0);
1772 memory_listener_register(&kvm_io_listener
,
1775 s
->many_ioeventfds
= kvm_check_many_ioeventfds();
1777 cpu_interrupt_handler
= kvm_handle_interrupt
;
1789 g_free(s
->memory_listener
.slots
);
1794 void kvm_set_sigmask_len(KVMState
*s
, unsigned int sigmask_len
)
1796 s
->sigmask_len
= sigmask_len
;
1799 static void kvm_handle_io(uint16_t port
, MemTxAttrs attrs
, void *data
, int direction
,
1800 int size
, uint32_t count
)
1803 uint8_t *ptr
= data
;
1805 for (i
= 0; i
< count
; i
++) {
1806 address_space_rw(&address_space_io
, port
, attrs
,
1808 direction
== KVM_EXIT_IO_OUT
);
1813 static int kvm_handle_internal_error(CPUState
*cpu
, struct kvm_run
*run
)
1815 fprintf(stderr
, "KVM internal error. Suberror: %d\n",
1816 run
->internal
.suberror
);
1818 if (kvm_check_extension(kvm_state
, KVM_CAP_INTERNAL_ERROR_DATA
)) {
1821 for (i
= 0; i
< run
->internal
.ndata
; ++i
) {
1822 fprintf(stderr
, "extra data[%d]: %"PRIx64
"\n",
1823 i
, (uint64_t)run
->internal
.data
[i
]);
1826 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
) {
1827 fprintf(stderr
, "emulation failure\n");
1828 if (!kvm_arch_stop_on_emulation_error(cpu
)) {
1829 cpu_dump_state(cpu
, stderr
, fprintf
, CPU_DUMP_CODE
);
1830 return EXCP_INTERRUPT
;
1833 /* FIXME: Should trigger a qmp message to let management know
1834 * something went wrong.
1839 void kvm_flush_coalesced_mmio_buffer(void)
1841 KVMState
*s
= kvm_state
;
1843 if (s
->coalesced_flush_in_progress
) {
1847 s
->coalesced_flush_in_progress
= true;
1849 if (s
->coalesced_mmio_ring
) {
1850 struct kvm_coalesced_mmio_ring
*ring
= s
->coalesced_mmio_ring
;
1851 while (ring
->first
!= ring
->last
) {
1852 struct kvm_coalesced_mmio
*ent
;
1854 ent
= &ring
->coalesced_mmio
[ring
->first
];
1856 cpu_physical_memory_write(ent
->phys_addr
, ent
->data
, ent
->len
);
1858 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1862 s
->coalesced_flush_in_progress
= false;
1865 static void do_kvm_cpu_synchronize_state(CPUState
*cpu
, run_on_cpu_data arg
)
1867 if (!cpu
->kvm_vcpu_dirty
) {
1868 kvm_arch_get_registers(cpu
);
1869 cpu
->kvm_vcpu_dirty
= true;
1873 void kvm_cpu_synchronize_state(CPUState
*cpu
)
1875 if (!cpu
->kvm_vcpu_dirty
) {
1876 run_on_cpu(cpu
, do_kvm_cpu_synchronize_state
, RUN_ON_CPU_NULL
);
1880 static void do_kvm_cpu_synchronize_post_reset(CPUState
*cpu
, run_on_cpu_data arg
)
1882 kvm_arch_put_registers(cpu
, KVM_PUT_RESET_STATE
);
1883 cpu
->kvm_vcpu_dirty
= false;
1886 void kvm_cpu_synchronize_post_reset(CPUState
*cpu
)
1888 run_on_cpu(cpu
, do_kvm_cpu_synchronize_post_reset
, RUN_ON_CPU_NULL
);
1891 static void do_kvm_cpu_synchronize_post_init(CPUState
*cpu
, run_on_cpu_data arg
)
1893 kvm_arch_put_registers(cpu
, KVM_PUT_FULL_STATE
);
1894 cpu
->kvm_vcpu_dirty
= false;
1897 void kvm_cpu_synchronize_post_init(CPUState
*cpu
)
1899 run_on_cpu(cpu
, do_kvm_cpu_synchronize_post_init
, RUN_ON_CPU_NULL
);
1902 static void do_kvm_cpu_synchronize_pre_loadvm(CPUState
*cpu
, run_on_cpu_data arg
)
1904 cpu
->kvm_vcpu_dirty
= true;
1907 void kvm_cpu_synchronize_pre_loadvm(CPUState
*cpu
)
1909 run_on_cpu(cpu
, do_kvm_cpu_synchronize_pre_loadvm
, RUN_ON_CPU_NULL
);
1912 #ifdef KVM_HAVE_MCE_INJECTION
1913 static __thread
void *pending_sigbus_addr
;
1914 static __thread
int pending_sigbus_code
;
1915 static __thread
bool have_sigbus_pending
;
1918 static void kvm_cpu_kick(CPUState
*cpu
)
1920 atomic_set(&cpu
->kvm_run
->immediate_exit
, 1);
1923 static void kvm_cpu_kick_self(void)
1925 if (kvm_immediate_exit
) {
1926 kvm_cpu_kick(current_cpu
);
1928 qemu_cpu_kick_self();
1932 static void kvm_eat_signals(CPUState
*cpu
)
1934 struct timespec ts
= { 0, 0 };
1940 if (kvm_immediate_exit
) {
1941 atomic_set(&cpu
->kvm_run
->immediate_exit
, 0);
1942 /* Write kvm_run->immediate_exit before the cpu->exit_request
1943 * write in kvm_cpu_exec.
1949 sigemptyset(&waitset
);
1950 sigaddset(&waitset
, SIG_IPI
);
1953 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
1954 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
1955 perror("sigtimedwait");
1959 r
= sigpending(&chkset
);
1961 perror("sigpending");
1964 } while (sigismember(&chkset
, SIG_IPI
));
1967 int kvm_cpu_exec(CPUState
*cpu
)
1969 struct kvm_run
*run
= cpu
->kvm_run
;
1972 DPRINTF("kvm_cpu_exec()\n");
1974 if (kvm_arch_process_async_events(cpu
)) {
1975 atomic_set(&cpu
->exit_request
, 0);
1979 qemu_mutex_unlock_iothread();
1984 if (cpu
->kvm_vcpu_dirty
) {
1985 kvm_arch_put_registers(cpu
, KVM_PUT_RUNTIME_STATE
);
1986 cpu
->kvm_vcpu_dirty
= false;
1989 kvm_arch_pre_run(cpu
, run
);
1990 if (atomic_read(&cpu
->exit_request
)) {
1991 DPRINTF("interrupt exit requested\n");
1993 * KVM requires us to reenter the kernel after IO exits to complete
1994 * instruction emulation. This self-signal will ensure that we
1997 kvm_cpu_kick_self();
2000 /* Read cpu->exit_request before KVM_RUN reads run->immediate_exit.
2001 * Matching barrier in kvm_eat_signals.
2005 run_ret
= kvm_vcpu_ioctl(cpu
, KVM_RUN
, 0);
2007 attrs
= kvm_arch_post_run(cpu
, run
);
2009 #ifdef KVM_HAVE_MCE_INJECTION
2010 if (unlikely(have_sigbus_pending
)) {
2011 qemu_mutex_lock_iothread();
2012 kvm_arch_on_sigbus_vcpu(cpu
, pending_sigbus_code
,
2013 pending_sigbus_addr
);
2014 have_sigbus_pending
= false;
2015 qemu_mutex_unlock_iothread();
2020 if (run_ret
== -EINTR
|| run_ret
== -EAGAIN
) {
2021 DPRINTF("io window exit\n");
2022 kvm_eat_signals(cpu
);
2023 ret
= EXCP_INTERRUPT
;
2026 fprintf(stderr
, "error: kvm run failed %s\n",
2027 strerror(-run_ret
));
2029 if (run_ret
== -EBUSY
) {
2031 "This is probably because your SMT is enabled.\n"
2032 "VCPU can only run on primary threads with all "
2033 "secondary threads offline.\n");
2040 trace_kvm_run_exit(cpu
->cpu_index
, run
->exit_reason
);
2041 switch (run
->exit_reason
) {
2043 DPRINTF("handle_io\n");
2044 /* Called outside BQL */
2045 kvm_handle_io(run
->io
.port
, attrs
,
2046 (uint8_t *)run
+ run
->io
.data_offset
,
2053 DPRINTF("handle_mmio\n");
2054 /* Called outside BQL */
2055 address_space_rw(&address_space_memory
,
2056 run
->mmio
.phys_addr
, attrs
,
2059 run
->mmio
.is_write
);
2062 case KVM_EXIT_IRQ_WINDOW_OPEN
:
2063 DPRINTF("irq_window_open\n");
2064 ret
= EXCP_INTERRUPT
;
2066 case KVM_EXIT_SHUTDOWN
:
2067 DPRINTF("shutdown\n");
2068 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
2069 ret
= EXCP_INTERRUPT
;
2071 case KVM_EXIT_UNKNOWN
:
2072 fprintf(stderr
, "KVM: unknown exit, hardware reason %" PRIx64
"\n",
2073 (uint64_t)run
->hw
.hardware_exit_reason
);
2076 case KVM_EXIT_INTERNAL_ERROR
:
2077 ret
= kvm_handle_internal_error(cpu
, run
);
2079 case KVM_EXIT_SYSTEM_EVENT
:
2080 switch (run
->system_event
.type
) {
2081 case KVM_SYSTEM_EVENT_SHUTDOWN
:
2082 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
2083 ret
= EXCP_INTERRUPT
;
2085 case KVM_SYSTEM_EVENT_RESET
:
2086 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
2087 ret
= EXCP_INTERRUPT
;
2089 case KVM_SYSTEM_EVENT_CRASH
:
2090 kvm_cpu_synchronize_state(cpu
);
2091 qemu_mutex_lock_iothread();
2092 qemu_system_guest_panicked(cpu_get_crash_info(cpu
));
2093 qemu_mutex_unlock_iothread();
2097 DPRINTF("kvm_arch_handle_exit\n");
2098 ret
= kvm_arch_handle_exit(cpu
, run
);
2103 DPRINTF("kvm_arch_handle_exit\n");
2104 ret
= kvm_arch_handle_exit(cpu
, run
);
2109 qemu_mutex_lock_iothread();
2112 cpu_dump_state(cpu
, stderr
, fprintf
, CPU_DUMP_CODE
);
2113 vm_stop(RUN_STATE_INTERNAL_ERROR
);
2116 atomic_set(&cpu
->exit_request
, 0);
2120 int kvm_ioctl(KVMState
*s
, int type
, ...)
2127 arg
= va_arg(ap
, void *);
2130 trace_kvm_ioctl(type
, arg
);
2131 ret
= ioctl(s
->fd
, type
, arg
);
2138 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
2145 arg
= va_arg(ap
, void *);
2148 trace_kvm_vm_ioctl(type
, arg
);
2149 ret
= ioctl(s
->vmfd
, type
, arg
);
2156 int kvm_vcpu_ioctl(CPUState
*cpu
, int type
, ...)
2163 arg
= va_arg(ap
, void *);
2166 trace_kvm_vcpu_ioctl(cpu
->cpu_index
, type
, arg
);
2167 ret
= ioctl(cpu
->kvm_fd
, type
, arg
);
2174 int kvm_device_ioctl(int fd
, int type
, ...)
2181 arg
= va_arg(ap
, void *);
2184 trace_kvm_device_ioctl(fd
, type
, arg
);
2185 ret
= ioctl(fd
, type
, arg
);
2192 int kvm_vm_check_attr(KVMState
*s
, uint32_t group
, uint64_t attr
)
2195 struct kvm_device_attr attribute
= {
2200 if (!kvm_vm_attributes_allowed
) {
2204 ret
= kvm_vm_ioctl(s
, KVM_HAS_DEVICE_ATTR
, &attribute
);
2205 /* kvm returns 0 on success for HAS_DEVICE_ATTR */
2209 int kvm_device_check_attr(int dev_fd
, uint32_t group
, uint64_t attr
)
2211 struct kvm_device_attr attribute
= {
2217 return kvm_device_ioctl(dev_fd
, KVM_HAS_DEVICE_ATTR
, &attribute
) ? 0 : 1;
2220 int kvm_device_access(int fd
, int group
, uint64_t attr
,
2221 void *val
, bool write
, Error
**errp
)
2223 struct kvm_device_attr kvmattr
;
2227 kvmattr
.group
= group
;
2228 kvmattr
.attr
= attr
;
2229 kvmattr
.addr
= (uintptr_t)val
;
2231 err
= kvm_device_ioctl(fd
,
2232 write
? KVM_SET_DEVICE_ATTR
: KVM_GET_DEVICE_ATTR
,
2235 error_setg_errno(errp
, -err
,
2236 "KVM_%s_DEVICE_ATTR failed: Group %d "
2237 "attr 0x%016" PRIx64
,
2238 write
? "SET" : "GET", group
, attr
);
2243 /* Return 1 on success, 0 on failure */
2244 int kvm_has_sync_mmu(void)
2246 return kvm_check_extension(kvm_state
, KVM_CAP_SYNC_MMU
);
2249 int kvm_has_vcpu_events(void)
2251 return kvm_state
->vcpu_events
;
2254 int kvm_has_robust_singlestep(void)
2256 return kvm_state
->robust_singlestep
;
2259 int kvm_has_debugregs(void)
2261 return kvm_state
->debugregs
;
2264 int kvm_has_many_ioeventfds(void)
2266 if (!kvm_enabled()) {
2269 return kvm_state
->many_ioeventfds
;
2272 int kvm_has_gsi_routing(void)
2274 #ifdef KVM_CAP_IRQ_ROUTING
2275 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
2281 int kvm_has_intx_set_mask(void)
2283 return kvm_state
->intx_set_mask
;
2286 #ifdef KVM_CAP_SET_GUEST_DEBUG
2287 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*cpu
,
2290 struct kvm_sw_breakpoint
*bp
;
2292 QTAILQ_FOREACH(bp
, &cpu
->kvm_state
->kvm_sw_breakpoints
, entry
) {
2300 int kvm_sw_breakpoints_active(CPUState
*cpu
)
2302 return !QTAILQ_EMPTY(&cpu
->kvm_state
->kvm_sw_breakpoints
);
2305 struct kvm_set_guest_debug_data
{
2306 struct kvm_guest_debug dbg
;
2310 static void kvm_invoke_set_guest_debug(CPUState
*cpu
, run_on_cpu_data data
)
2312 struct kvm_set_guest_debug_data
*dbg_data
=
2313 (struct kvm_set_guest_debug_data
*) data
.host_ptr
;
2315 dbg_data
->err
= kvm_vcpu_ioctl(cpu
, KVM_SET_GUEST_DEBUG
,
2319 int kvm_update_guest_debug(CPUState
*cpu
, unsigned long reinject_trap
)
2321 struct kvm_set_guest_debug_data data
;
2323 data
.dbg
.control
= reinject_trap
;
2325 if (cpu
->singlestep_enabled
) {
2326 data
.dbg
.control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
2328 kvm_arch_update_guest_debug(cpu
, &data
.dbg
);
2330 run_on_cpu(cpu
, kvm_invoke_set_guest_debug
,
2331 RUN_ON_CPU_HOST_PTR(&data
));
2335 int kvm_insert_breakpoint(CPUState
*cpu
, target_ulong addr
,
2336 target_ulong len
, int type
)
2338 struct kvm_sw_breakpoint
*bp
;
2341 if (type
== GDB_BREAKPOINT_SW
) {
2342 bp
= kvm_find_sw_breakpoint(cpu
, addr
);
2348 bp
= g_malloc(sizeof(struct kvm_sw_breakpoint
));
2351 err
= kvm_arch_insert_sw_breakpoint(cpu
, bp
);
2357 QTAILQ_INSERT_HEAD(&cpu
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
2359 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
2366 err
= kvm_update_guest_debug(cpu
, 0);
2374 int kvm_remove_breakpoint(CPUState
*cpu
, target_ulong addr
,
2375 target_ulong len
, int type
)
2377 struct kvm_sw_breakpoint
*bp
;
2380 if (type
== GDB_BREAKPOINT_SW
) {
2381 bp
= kvm_find_sw_breakpoint(cpu
, addr
);
2386 if (bp
->use_count
> 1) {
2391 err
= kvm_arch_remove_sw_breakpoint(cpu
, bp
);
2396 QTAILQ_REMOVE(&cpu
->kvm_state
->kvm_sw_breakpoints
, bp
, entry
);
2399 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
2406 err
= kvm_update_guest_debug(cpu
, 0);
2414 void kvm_remove_all_breakpoints(CPUState
*cpu
)
2416 struct kvm_sw_breakpoint
*bp
, *next
;
2417 KVMState
*s
= cpu
->kvm_state
;
2420 QTAILQ_FOREACH_SAFE(bp
, &s
->kvm_sw_breakpoints
, entry
, next
) {
2421 if (kvm_arch_remove_sw_breakpoint(cpu
, bp
) != 0) {
2422 /* Try harder to find a CPU that currently sees the breakpoint. */
2423 CPU_FOREACH(tmpcpu
) {
2424 if (kvm_arch_remove_sw_breakpoint(tmpcpu
, bp
) == 0) {
2429 QTAILQ_REMOVE(&s
->kvm_sw_breakpoints
, bp
, entry
);
2432 kvm_arch_remove_all_hw_breakpoints();
2435 kvm_update_guest_debug(cpu
, 0);
2439 #else /* !KVM_CAP_SET_GUEST_DEBUG */
2441 int kvm_update_guest_debug(CPUState
*cpu
, unsigned long reinject_trap
)
2446 int kvm_insert_breakpoint(CPUState
*cpu
, target_ulong addr
,
2447 target_ulong len
, int type
)
2452 int kvm_remove_breakpoint(CPUState
*cpu
, target_ulong addr
,
2453 target_ulong len
, int type
)
2458 void kvm_remove_all_breakpoints(CPUState
*cpu
)
2461 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
2463 static int kvm_set_signal_mask(CPUState
*cpu
, const sigset_t
*sigset
)
2465 KVMState
*s
= kvm_state
;
2466 struct kvm_signal_mask
*sigmask
;
2469 sigmask
= g_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
2471 sigmask
->len
= s
->sigmask_len
;
2472 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
2473 r
= kvm_vcpu_ioctl(cpu
, KVM_SET_SIGNAL_MASK
, sigmask
);
2479 static void kvm_ipi_signal(int sig
)
2482 assert(kvm_immediate_exit
);
2483 kvm_cpu_kick(current_cpu
);
2487 void kvm_init_cpu_signals(CPUState
*cpu
)
2491 struct sigaction sigact
;
2493 memset(&sigact
, 0, sizeof(sigact
));
2494 sigact
.sa_handler
= kvm_ipi_signal
;
2495 sigaction(SIG_IPI
, &sigact
, NULL
);
2497 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2498 #if defined KVM_HAVE_MCE_INJECTION
2499 sigdelset(&set
, SIGBUS
);
2500 pthread_sigmask(SIG_SETMASK
, &set
, NULL
);
2502 sigdelset(&set
, SIG_IPI
);
2503 if (kvm_immediate_exit
) {
2504 r
= pthread_sigmask(SIG_SETMASK
, &set
, NULL
);
2506 r
= kvm_set_signal_mask(cpu
, &set
);
2509 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
2514 /* Called asynchronously in VCPU thread. */
2515 int kvm_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
)
2517 #ifdef KVM_HAVE_MCE_INJECTION
2518 if (have_sigbus_pending
) {
2521 have_sigbus_pending
= true;
2522 pending_sigbus_addr
= addr
;
2523 pending_sigbus_code
= code
;
2524 atomic_set(&cpu
->exit_request
, 1);
2531 /* Called synchronously (via signalfd) in main thread. */
2532 int kvm_on_sigbus(int code
, void *addr
)
2534 #ifdef KVM_HAVE_MCE_INJECTION
2535 /* Action required MCE kills the process if SIGBUS is blocked. Because
2536 * that's what happens in the I/O thread, where we handle MCE via signalfd,
2537 * we can only get action optional here.
2539 assert(code
!= BUS_MCEERR_AR
);
2540 kvm_arch_on_sigbus_vcpu(first_cpu
, code
, addr
);
2547 int kvm_create_device(KVMState
*s
, uint64_t type
, bool test
)
2550 struct kvm_create_device create_dev
;
2552 create_dev
.type
= type
;
2554 create_dev
.flags
= test
? KVM_CREATE_DEVICE_TEST
: 0;
2556 if (!kvm_check_extension(s
, KVM_CAP_DEVICE_CTRL
)) {
2560 ret
= kvm_vm_ioctl(s
, KVM_CREATE_DEVICE
, &create_dev
);
2565 return test
? 0 : create_dev
.fd
;
2568 bool kvm_device_supported(int vmfd
, uint64_t type
)
2570 struct kvm_create_device create_dev
= {
2573 .flags
= KVM_CREATE_DEVICE_TEST
,
2576 if (ioctl(vmfd
, KVM_CHECK_EXTENSION
, KVM_CAP_DEVICE_CTRL
) <= 0) {
2580 return (ioctl(vmfd
, KVM_CREATE_DEVICE
, &create_dev
) >= 0);
2583 int kvm_set_one_reg(CPUState
*cs
, uint64_t id
, void *source
)
2585 struct kvm_one_reg reg
;
2589 reg
.addr
= (uintptr_t) source
;
2590 r
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
2592 trace_kvm_failed_reg_set(id
, strerror(-r
));
2597 int kvm_get_one_reg(CPUState
*cs
, uint64_t id
, void *target
)
2599 struct kvm_one_reg reg
;
2603 reg
.addr
= (uintptr_t) target
;
2604 r
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
2606 trace_kvm_failed_reg_get(id
, strerror(-r
));
2611 static void kvm_accel_class_init(ObjectClass
*oc
, void *data
)
2613 AccelClass
*ac
= ACCEL_CLASS(oc
);
2615 ac
->init_machine
= kvm_init
;
2616 ac
->allowed
= &kvm_allowed
;
2619 static const TypeInfo kvm_accel_type
= {
2620 .name
= TYPE_KVM_ACCEL
,
2621 .parent
= TYPE_ACCEL
,
2622 .class_init
= kvm_accel_class_init
,
2623 .instance_size
= sizeof(KVMState
),
2626 static void kvm_type_init(void)
2628 type_register_static(&kvm_accel_type
);
2631 type_init(kvm_type_init
);