2 * Kernel-based Virtual Machine control library
4 * This library provides an API to control the kvm hardware virtualization
7 * Copyright (C) 2006 Qumranet
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the GNU LGPL license, version 2.
18 #define __user /* temporary, until installed via make headers_install */
21 #include <linux/kvm.h>
23 #define EXPECTED_KVM_API_VERSION 12
25 #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
26 #error libkvm: userspace and kernel version mismatch
36 #include <sys/ioctl.h>
40 #if defined(__x86_64__) || defined(__i386__)
48 #if defined(__powerpc__)
49 #include "kvm-powerpc.h"
56 //#define DEBUG_MEMREG
58 #define DPRINTF(fmt, args...) \
59 do { fprintf(stderr, "%s:%d " fmt , __func__, __LINE__, ##args); } while (0)
61 #define DPRINTF(fmt, args...) do {} while (0)
65 int kvm_abi
= EXPECTED_KVM_API_VERSION
;
69 unsigned long phys_addr
;
71 unsigned long userspace_addr
;
76 struct slot_info slots
[KVM_MAX_NUM_MEM_REGIONS
];
82 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
86 int get_free_slot(kvm_context_t kvm
)
91 #if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
92 tss_ext
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
98 * on older kernels where the set tss ioctl is not supprted we must save
99 * slot 0 to hold the extended memory, as the vmx will use the last 3
100 * pages of this slot.
107 for (; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
113 void register_slot(int slot
, unsigned long phys_addr
, unsigned long len
,
114 unsigned long userspace_addr
, unsigned flags
)
116 slots
[slot
].phys_addr
= phys_addr
;
117 slots
[slot
].len
= len
;
118 slots
[slot
].userspace_addr
= userspace_addr
;
119 slots
[slot
].flags
= flags
;
122 void free_slot(int slot
)
125 slots
[slot
].logging_count
= 0;
128 int get_slot(unsigned long phys_addr
)
132 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
133 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
134 (slots
[i
].phys_addr
+ slots
[i
].len
-1) >= phys_addr
)
140 /* Returns -1 if this slot is not totally contained on any other,
141 * and the number of the slot otherwise */
142 int get_container_slot(uint64_t phys_addr
, unsigned long size
)
146 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
147 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
148 (slots
[i
].phys_addr
+ slots
[i
].len
) >= phys_addr
+ size
)
153 int kvm_is_containing_region(kvm_context_t kvm
, unsigned long phys_addr
, unsigned long size
)
155 int slot
= get_container_slot(phys_addr
, size
);
162 * dirty pages logging control
164 static int kvm_dirty_pages_log_change(kvm_context_t kvm
,
165 unsigned long phys_addr
,
170 int slot
= get_slot(phys_addr
);
173 fprintf(stderr
, "BUG: %s: invalid parameters\n", __FUNCTION__
);
177 flags
= (slots
[slot
].flags
& ~mask
) | flags
;
178 if (flags
== slots
[slot
].flags
)
180 slots
[slot
].flags
= flags
;
183 struct kvm_userspace_memory_region mem
= {
185 .memory_size
= slots
[slot
].len
,
186 .guest_phys_addr
= slots
[slot
].phys_addr
,
187 .userspace_addr
= slots
[slot
].userspace_addr
,
188 .flags
= slots
[slot
].flags
,
192 DPRINTF("slot %d start %llx len %llx flags %x\n",
197 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &mem
);
199 fprintf(stderr
, "%s: %m\n", __FUNCTION__
);
204 static int kvm_dirty_pages_log_change_all(kvm_context_t kvm
,
205 int (*change
)(kvm_context_t kvm
,
211 for (i
=r
=0; i
<KVM_MAX_NUM_MEM_REGIONS
&& r
==0; i
++) {
213 r
= change(kvm
, slots
[i
].phys_addr
, slots
[i
].len
);
218 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm
,
222 int slot
= get_slot(phys_addr
);
224 DPRINTF("start %"PRIx64
" len %"PRIx64
"\n", phys_addr
, len
);
226 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
230 if (slots
[slot
].logging_count
++)
233 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
,
234 KVM_MEM_LOG_DIRTY_PAGES
,
235 KVM_MEM_LOG_DIRTY_PAGES
);
238 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm
,
242 int slot
= get_slot(phys_addr
);
245 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
249 if (--slots
[slot
].logging_count
)
252 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
,
254 KVM_MEM_LOG_DIRTY_PAGES
);
258 * Enable dirty page logging for all memory regions
260 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
)
262 if (kvm
->dirty_pages_log_all
)
264 kvm
->dirty_pages_log_all
= 1;
265 return kvm_dirty_pages_log_change_all(kvm
,
266 kvm_dirty_pages_log_enable_slot
);
270 * Enable dirty page logging only for memory regions that were created with
271 * dirty logging enabled (disable for all other memory regions).
273 int kvm_dirty_pages_log_reset(kvm_context_t kvm
)
275 if (!kvm
->dirty_pages_log_all
)
277 kvm
->dirty_pages_log_all
= 0;
278 return kvm_dirty_pages_log_change_all(kvm
,
279 kvm_dirty_pages_log_disable_slot
);
283 kvm_context_t
kvm_init(struct kvm_callbacks
*callbacks
,
290 fd
= open("/dev/kvm", O_RDWR
);
292 perror("open /dev/kvm");
295 r
= ioctl(fd
, KVM_GET_API_VERSION
, 0);
297 fprintf(stderr
, "kvm kernel version too old: "
298 "KVM_GET_API_VERSION ioctl not supported\n");
301 if (r
< EXPECTED_KVM_API_VERSION
) {
302 fprintf(stderr
, "kvm kernel version too old: "
303 "We expect API version %d or newer, but got "
305 EXPECTED_KVM_API_VERSION
, r
);
308 if (r
> EXPECTED_KVM_API_VERSION
) {
309 fprintf(stderr
, "kvm userspace version too old\n");
313 kvm_page_size
= getpagesize();
314 kvm
= malloc(sizeof(*kvm
));
317 memset(kvm
, 0, sizeof(*kvm
));
320 kvm
->callbacks
= callbacks
;
321 kvm
->opaque
= opaque
;
322 kvm
->dirty_pages_log_all
= 0;
323 kvm
->no_irqchip_creation
= 0;
324 kvm
->no_pit_creation
= 0;
332 void kvm_finalize(kvm_context_t kvm
)
334 if (kvm
->vcpu_fd
[0] != -1)
335 close(kvm
->vcpu_fd
[0]);
336 if (kvm
->vm_fd
!= -1)
342 void kvm_disable_irqchip_creation(kvm_context_t kvm
)
344 kvm
->no_irqchip_creation
= 1;
347 void kvm_disable_pit_creation(kvm_context_t kvm
)
349 kvm
->no_pit_creation
= 1;
352 int kvm_create_vcpu(kvm_context_t kvm
, int slot
)
357 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_VCPU
, slot
);
360 fprintf(stderr
, "kvm_create_vcpu: %m\n");
363 kvm
->vcpu_fd
[slot
] = r
;
364 mmap_size
= ioctl(kvm
->fd
, KVM_GET_VCPU_MMAP_SIZE
, 0);
365 if (mmap_size
== -1) {
367 fprintf(stderr
, "get vcpu mmap size: %m\n");
370 kvm
->run
[slot
] = mmap(NULL
, mmap_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
371 kvm
->vcpu_fd
[slot
], 0);
372 if (kvm
->run
[slot
] == MAP_FAILED
) {
374 fprintf(stderr
, "mmap vcpu area: %m\n");
380 int kvm_create_vm(kvm_context_t kvm
)
384 kvm
->vcpu_fd
[0] = -1;
386 fd
= ioctl(fd
, KVM_CREATE_VM
, 0);
388 fprintf(stderr
, "kvm_create_vm: %m\n");
395 static int kvm_create_default_phys_mem(kvm_context_t kvm
,
396 unsigned long phys_mem_bytes
,
399 #ifdef KVM_CAP_USER_MEMORY
400 int r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
403 fprintf(stderr
, "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n");
405 #error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported
410 int kvm_check_extension(kvm_context_t kvm
, int ext
)
414 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, ext
);
420 void kvm_create_irqchip(kvm_context_t kvm
)
424 kvm
->irqchip_in_kernel
= 0;
425 #ifdef KVM_CAP_IRQCHIP
426 if (!kvm
->no_irqchip_creation
) {
427 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_IRQCHIP
);
428 if (r
> 0) { /* kernel irqchip supported */
429 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_IRQCHIP
);
431 kvm
->irqchip_in_kernel
= 1;
433 fprintf(stderr
, "Create kernel PIC irqchip failed\n");
439 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
, void **vm_mem
)
443 r
= kvm_create_vm(kvm
);
446 r
= kvm_arch_create(kvm
, phys_mem_bytes
, vm_mem
);
450 r
= kvm_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
453 kvm_create_irqchip(kvm
);
459 void *kvm_create_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
460 unsigned long len
, int log
, int writable
)
463 int prot
= PROT_READ
;
465 struct kvm_userspace_memory_region memory
= {
467 .guest_phys_addr
= phys_start
,
468 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
474 #if !defined(__s390__)
475 ptr
= mmap(NULL
, len
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, -1, 0);
477 ptr
= mmap(LIBKVM_S390_ORIGIN
, len
, prot
| PROT_EXEC
,
478 MAP_FIXED
| MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
480 if (ptr
== MAP_FAILED
) {
481 fprintf(stderr
, "%s: %s", __func__
, strerror(errno
));
487 memory
.userspace_addr
= (unsigned long)ptr
;
488 memory
.slot
= get_free_slot(kvm
);
489 DPRINTF("slot %d start %llx len %llx flags %x\n",
491 memory
.guest_phys_addr
,
494 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
496 fprintf(stderr
, "%s: %s", __func__
, strerror(errno
));
499 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
500 memory
.userspace_addr
, memory
.flags
);
505 int kvm_register_phys_mem(kvm_context_t kvm
,
506 unsigned long phys_start
, void *userspace_addr
,
507 unsigned long len
, int log
)
510 struct kvm_userspace_memory_region memory
= {
512 .guest_phys_addr
= phys_start
,
513 .userspace_addr
= (unsigned long)(intptr_t)userspace_addr
,
514 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
518 memory
.slot
= get_free_slot(kvm
);
519 DPRINTF("memory: gpa: %llx, size: %llx, uaddr: %llx, slot: %x, flags: %lx\n",
520 memory
.guest_phys_addr
, memory
.memory_size
,
521 memory
.userspace_addr
, memory
.slot
, memory
.flags
);
522 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
524 fprintf(stderr
, "create_userspace_phys_mem: %s\n", strerror(errno
));
527 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
528 memory
.userspace_addr
, memory
.flags
);
533 /* destroy/free a whole slot.
534 * phys_start, len and slot are the params passed to kvm_create_phys_mem()
536 void kvm_destroy_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
541 struct kvm_userspace_memory_region memory
= {
543 .guest_phys_addr
= phys_start
,
548 slot
= get_slot(phys_start
);
550 if ((slot
>= KVM_MAX_NUM_MEM_REGIONS
) || (slot
== -1)) {
551 fprintf(stderr
, "BUG: %s: invalid parameters (slot=%d)\n",
555 if (phys_start
!= slots
[slot
].phys_addr
) {
557 "WARNING: %s: phys_start is 0x%lx expecting 0x%lx\n",
558 __FUNCTION__
, phys_start
, slots
[slot
].phys_addr
);
559 phys_start
= slots
[slot
].phys_addr
;
563 DPRINTF("slot %d start %llx len %llx flags %x\n",
565 memory
.guest_phys_addr
,
568 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
570 fprintf(stderr
, "destroy_userspace_phys_mem: %s",
575 free_slot(memory
.slot
);
578 void kvm_unregister_memory_area(kvm_context_t kvm
, uint64_t phys_addr
, unsigned long size
)
581 int slot
= get_container_slot(phys_addr
, size
);
584 DPRINTF("Unregistering memory region %llx (%lx)\n", phys_addr
, size
);
585 kvm_destroy_phys_mem(kvm
, phys_addr
, size
);
590 static int kvm_get_map(kvm_context_t kvm
, int ioctl_num
, int slot
, void *buf
)
593 struct kvm_dirty_log log
= {
597 log
.dirty_bitmap
= buf
;
599 r
= ioctl(kvm
->vm_fd
, ioctl_num
, &log
);
605 int kvm_get_dirty_pages(kvm_context_t kvm
, unsigned long phys_addr
, void *buf
)
609 slot
= get_slot(phys_addr
);
610 return kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, slot
, buf
);
613 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
614 #define BITMAP_SIZE(m) (ALIGN(((m)/PAGE_SIZE), sizeof(long) * 8) / 8)
616 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
617 unsigned long len
, void *buf
, void *opaque
,
618 int (*cb
)(unsigned long start
, unsigned long len
,
619 void*bitmap
, void *opaque
))
623 unsigned long end_addr
= phys_addr
+ len
;
625 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
626 if ((slots
[i
].len
&& (uint64_t)slots
[i
].phys_addr
>= phys_addr
)
627 && ((uint64_t)slots
[i
].phys_addr
+ slots
[i
].len
<= end_addr
)) {
628 r
= kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, i
, buf
);
631 r
= cb(slots
[i
].phys_addr
, slots
[i
].len
, buf
, opaque
);
639 #ifdef KVM_CAP_IRQCHIP
641 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
)
643 struct kvm_irq_level event
;
646 if (!kvm
->irqchip_in_kernel
)
650 r
= ioctl(kvm
->vm_fd
, KVM_IRQ_LINE
, &event
);
652 perror("kvm_set_irq_level");
656 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
660 if (!kvm
->irqchip_in_kernel
)
662 r
= ioctl(kvm
->vm_fd
, KVM_GET_IRQCHIP
, chip
);
665 perror("kvm_get_irqchip\n");
670 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
674 if (!kvm
->irqchip_in_kernel
)
676 r
= ioctl(kvm
->vm_fd
, KVM_SET_IRQCHIP
, chip
);
679 perror("kvm_set_irqchip\n");
686 static int handle_io(kvm_context_t kvm
, struct kvm_run
*run
, int vcpu
)
688 uint16_t addr
= run
->io
.port
;
691 void *p
= (void *)run
+ run
->io
.data_offset
;
693 for (i
= 0; i
< run
->io
.count
; ++i
) {
694 switch (run
->io
.direction
) {
696 switch (run
->io
.size
) {
698 r
= kvm
->callbacks
->inb(kvm
->opaque
, addr
, p
);
701 r
= kvm
->callbacks
->inw(kvm
->opaque
, addr
, p
);
704 r
= kvm
->callbacks
->inl(kvm
->opaque
, addr
, p
);
707 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
711 case KVM_EXIT_IO_OUT
:
712 switch (run
->io
.size
) {
714 r
= kvm
->callbacks
->outb(kvm
->opaque
, addr
,
718 r
= kvm
->callbacks
->outw(kvm
->opaque
, addr
,
722 r
= kvm
->callbacks
->outl(kvm
->opaque
, addr
,
726 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
731 fprintf(stderr
, "bad I/O direction %d\n", run
->io
.direction
);
741 int handle_debug(kvm_context_t kvm
, int vcpu
, void *env
)
743 #ifdef KVM_CAP_SET_GUEST_DEBUG
744 struct kvm_run
*run
= kvm
->run
[vcpu
];
746 return kvm
->callbacks
->debug(kvm
->opaque
, env
, &run
->debug
.arch
);
752 int kvm_get_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
754 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_REGS
, regs
);
757 int kvm_set_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
759 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_REGS
, regs
);
762 int kvm_get_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
764 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_FPU
, fpu
);
767 int kvm_set_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
769 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_FPU
, fpu
);
772 int kvm_get_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
774 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_SREGS
, sregs
);
777 int kvm_set_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
779 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SREGS
, sregs
);
782 #ifdef KVM_CAP_MP_STATE
783 int kvm_get_mpstate(kvm_context_t kvm
, int vcpu
, struct kvm_mp_state
*mp_state
)
787 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
789 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_MP_STATE
, mp_state
);
793 int kvm_set_mpstate(kvm_context_t kvm
, int vcpu
, struct kvm_mp_state
*mp_state
)
797 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
799 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_MP_STATE
, mp_state
);
804 static int handle_mmio(kvm_context_t kvm
, struct kvm_run
*kvm_run
)
806 unsigned long addr
= kvm_run
->mmio
.phys_addr
;
807 void *data
= kvm_run
->mmio
.data
;
809 /* hack: Red Hat 7.1 generates these weird accesses. */
810 if ((addr
> 0xa0000-4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
813 if (kvm_run
->mmio
.is_write
)
814 return kvm
->callbacks
->mmio_write(kvm
->opaque
, addr
, data
,
817 return kvm
->callbacks
->mmio_read(kvm
->opaque
, addr
, data
,
821 int handle_io_window(kvm_context_t kvm
)
823 return kvm
->callbacks
->io_window(kvm
->opaque
);
826 int handle_halt(kvm_context_t kvm
, int vcpu
)
828 return kvm
->callbacks
->halt(kvm
->opaque
, vcpu
);
831 int handle_shutdown(kvm_context_t kvm
, void *env
)
833 return kvm
->callbacks
->shutdown(kvm
->opaque
, env
);
836 int try_push_interrupts(kvm_context_t kvm
)
838 return kvm
->callbacks
->try_push_interrupts(kvm
->opaque
);
841 static inline void push_nmi(kvm_context_t kvm
)
843 #ifdef KVM_CAP_USER_NMI
844 kvm
->callbacks
->push_nmi(kvm
->opaque
);
845 #endif /* KVM_CAP_USER_NMI */
848 void post_kvm_run(kvm_context_t kvm
, void *env
)
850 kvm
->callbacks
->post_kvm_run(kvm
->opaque
, env
);
853 int pre_kvm_run(kvm_context_t kvm
, void *env
)
855 return kvm
->callbacks
->pre_kvm_run(kvm
->opaque
, env
);
858 int kvm_get_interrupt_flag(kvm_context_t kvm
, int vcpu
)
860 struct kvm_run
*run
= kvm
->run
[vcpu
];
865 int kvm_is_ready_for_interrupt_injection(kvm_context_t kvm
, int vcpu
)
867 struct kvm_run
*run
= kvm
->run
[vcpu
];
869 return run
->ready_for_interrupt_injection
;
872 int kvm_run(kvm_context_t kvm
, int vcpu
, void *env
)
875 int fd
= kvm
->vcpu_fd
[vcpu
];
876 struct kvm_run
*run
= kvm
->run
[vcpu
];
880 #if !defined(__s390__)
881 if (!kvm
->irqchip_in_kernel
)
882 run
->request_interrupt_window
= try_push_interrupts(kvm
);
884 r
= pre_kvm_run(kvm
, env
);
887 r
= ioctl(fd
, KVM_RUN
, 0);
889 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
891 post_kvm_run(kvm
, env
);
892 fprintf(stderr
, "kvm_run: %s\n", strerror(-r
));
896 post_kvm_run(kvm
, env
);
898 #if defined(KVM_CAP_COALESCED_MMIO)
899 if (kvm
->coalesced_mmio
) {
900 struct kvm_coalesced_mmio_ring
*ring
= (void *)run
+
901 kvm
->coalesced_mmio
* PAGE_SIZE
;
902 while (ring
->first
!= ring
->last
) {
903 kvm
->callbacks
->mmio_write(kvm
->opaque
,
904 ring
->coalesced_mmio
[ring
->first
].phys_addr
,
905 &ring
->coalesced_mmio
[ring
->first
].data
[0],
906 ring
->coalesced_mmio
[ring
->first
].len
);
908 ring
->first
= (ring
->first
+ 1) %
909 KVM_COALESCED_MMIO_MAX
;
914 #if !defined(__s390__)
916 r
= handle_io_window(kvm
);
921 switch (run
->exit_reason
) {
922 case KVM_EXIT_UNKNOWN
:
923 fprintf(stderr
, "unhandled vm exit: 0x%x vcpu_id %d\n",
924 (unsigned)run
->hw
.hardware_exit_reason
, vcpu
);
925 kvm_show_regs(kvm
, vcpu
);
928 case KVM_EXIT_FAIL_ENTRY
:
929 fprintf(stderr
, "kvm_run: failed entry, reason %u\n",
930 (unsigned)run
->fail_entry
.hardware_entry_failure_reason
& 0xffff);
931 kvm_show_regs(kvm
, vcpu
);
934 case KVM_EXIT_EXCEPTION
:
935 fprintf(stderr
, "exception %d (%x)\n",
938 kvm_show_regs(kvm
, vcpu
);
939 kvm_show_code(kvm
, vcpu
);
943 r
= handle_io(kvm
, run
, vcpu
);
946 r
= handle_debug(kvm
, vcpu
, env
);
949 r
= handle_mmio(kvm
, run
);
952 r
= handle_halt(kvm
, vcpu
);
954 case KVM_EXIT_IRQ_WINDOW_OPEN
:
956 case KVM_EXIT_SHUTDOWN
:
957 r
= handle_shutdown(kvm
, env
);
959 #if defined(__s390__)
960 case KVM_EXIT_S390_SIEIC
:
961 r
= kvm
->callbacks
->s390_handle_intercept(kvm
, vcpu
,
964 case KVM_EXIT_S390_RESET
:
965 r
= kvm
->callbacks
->s390_handle_reset(kvm
, vcpu
, run
);
969 if (kvm_arch_run(run
, kvm
, vcpu
)) {
970 fprintf(stderr
, "unhandled vm exit: 0x%x\n",
972 kvm_show_regs(kvm
, vcpu
);
984 int kvm_inject_irq(kvm_context_t kvm
, int vcpu
, unsigned irq
)
986 struct kvm_interrupt intr
;
989 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_INTERRUPT
, &intr
);
992 #ifdef KVM_CAP_SET_GUEST_DEBUG
993 int kvm_set_guest_debug(kvm_context_t kvm
, int vcpu
, struct kvm_guest_debug
*dbg
)
995 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_GUEST_DEBUG
, dbg
);
999 int kvm_set_signal_mask(kvm_context_t kvm
, int vcpu
, const sigset_t
*sigset
)
1001 struct kvm_signal_mask
*sigmask
;
1005 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, NULL
);
1010 sigmask
= malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1015 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1016 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, sigmask
);
1023 int kvm_irqchip_in_kernel(kvm_context_t kvm
)
1025 return kvm
->irqchip_in_kernel
;
1028 int kvm_pit_in_kernel(kvm_context_t kvm
)
1030 return kvm
->pit_in_kernel
;
1033 int kvm_has_sync_mmu(kvm_context_t kvm
)
1036 #ifdef KVM_CAP_SYNC_MMU
1037 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SYNC_MMU
);
1042 int kvm_inject_nmi(kvm_context_t kvm
, int vcpu
)
1044 #ifdef KVM_CAP_USER_NMI
1045 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_NMI
);
1051 int kvm_init_coalesced_mmio(kvm_context_t kvm
)
1054 kvm
->coalesced_mmio
= 0;
1055 #ifdef KVM_CAP_COALESCED_MMIO
1056 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
1058 kvm
->coalesced_mmio
= r
;
1065 int kvm_register_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1067 #ifdef KVM_CAP_COALESCED_MMIO
1068 struct kvm_coalesced_mmio_zone zone
;
1071 if (kvm
->coalesced_mmio
) {
1076 r
= ioctl(kvm
->vm_fd
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
1078 perror("kvm_register_coalesced_mmio_zone");
1087 int kvm_unregister_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1089 #ifdef KVM_CAP_COALESCED_MMIO
1090 struct kvm_coalesced_mmio_zone zone
;
1093 if (kvm
->coalesced_mmio
) {
1098 r
= ioctl(kvm
->vm_fd
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
1100 perror("kvm_unregister_coalesced_mmio_zone");
1103 DPRINTF("Unregistered coalesced mmio region for %llx (%lx)\n", addr
, size
);
1110 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1111 int kvm_assign_pci_device(kvm_context_t kvm
,
1112 struct kvm_assigned_pci_dev
*assigned_dev
)
1116 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
1123 int kvm_assign_irq(kvm_context_t kvm
,
1124 struct kvm_assigned_irq
*assigned_irq
)
1128 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_IRQ
, assigned_irq
);
1136 int kvm_destroy_memory_region_works(kvm_context_t kvm
)
1140 #ifdef KVM_CAP_DESTROY_MEMORY_REGION_WORKS
1141 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
,
1142 KVM_CAP_DESTROY_MEMORY_REGION_WORKS
);