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>
39 #if defined(__x86_64__) || defined(__i386__)
47 #if defined(__powerpc__)
48 #include "kvm-powerpc.h"
55 int kvm_abi
= EXPECTED_KVM_API_VERSION
;
59 unsigned long phys_addr
;
62 unsigned long userspace_addr
;
66 struct slot_info slots
[KVM_MAX_NUM_MEM_REGIONS
];
72 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
76 int get_free_slot(kvm_context_t kvm
)
81 #if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
82 tss_ext
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
88 * on older kernels where the set tss ioctl is not supprted we must save
89 * slot 0 to hold the extended memory, as the vmx will use the last 3
97 for (; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
103 void register_slot(int slot
, unsigned long phys_addr
, unsigned long len
,
104 int user_alloc
, unsigned long userspace_addr
, unsigned flags
)
106 slots
[slot
].phys_addr
= phys_addr
;
107 slots
[slot
].len
= len
;
108 slots
[slot
].user_alloc
= user_alloc
;
109 slots
[slot
].userspace_addr
= userspace_addr
;
110 slots
[slot
].flags
= flags
;
113 void free_slot(int slot
)
118 int get_slot(unsigned long phys_addr
)
122 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
123 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
124 (slots
[i
].phys_addr
+ slots
[i
].len
-1) >= phys_addr
)
130 int get_intersecting_slot(unsigned long phys_addr
)
134 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
135 if (slots
[i
].len
&& slots
[i
].phys_addr
< phys_addr
&&
136 (slots
[i
].phys_addr
+ slots
[i
].len
) > phys_addr
)
142 * dirty pages logging control
144 static int kvm_dirty_pages_log_change(kvm_context_t kvm
, unsigned long phys_addr
150 slot
= get_slot(phys_addr
);
152 fprintf(stderr
, "BUG: %s: invalid parameters\n", __FUNCTION__
);
155 flag
|= slots
[slot
].flags
;
156 #ifdef KVM_CAP_USER_MEMORY
157 if (slots
[slot
].user_alloc
) {
158 struct kvm_userspace_memory_region mem
= {
160 .memory_size
= slots
[slot
].len
,
161 .guest_phys_addr
= slots
[slot
].phys_addr
,
162 .userspace_addr
= slots
[slot
].userspace_addr
,
165 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &mem
);
168 if (!slots
[slot
].user_alloc
) {
169 struct kvm_memory_region mem
= {
171 .memory_size
= slots
[slot
].len
,
172 .guest_phys_addr
= slots
[slot
].phys_addr
,
175 r
= ioctl(kvm
->vm_fd
, KVM_SET_MEMORY_REGION
, &mem
);
178 fprintf(stderr
, "%s: %m\n", __FUNCTION__
);
182 static int kvm_dirty_pages_log_change_all(kvm_context_t kvm
, __u32 flag
)
186 for (i
=r
=0; i
<KVM_MAX_NUM_MEM_REGIONS
&& r
==0; i
++) {
188 r
= kvm_dirty_pages_log_change(kvm
, slots
[i
].phys_addr
,
195 * Enable dirty page logging for all memory regions
197 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
)
199 if (kvm
->dirty_pages_log_all
)
201 kvm
->dirty_pages_log_all
= 1;
202 return kvm_dirty_pages_log_change_all(kvm
, KVM_MEM_LOG_DIRTY_PAGES
);
206 * Enable dirty page logging only for memory regions that were created with
207 * dirty logging enabled (disable for all other memory regions).
209 int kvm_dirty_pages_log_reset(kvm_context_t kvm
)
211 if (!kvm
->dirty_pages_log_all
)
213 kvm
->dirty_pages_log_all
= 0;
214 return kvm_dirty_pages_log_change_all(kvm
, 0);
218 kvm_context_t
kvm_init(struct kvm_callbacks
*callbacks
,
225 fd
= open("/dev/kvm", O_RDWR
);
227 perror("open /dev/kvm");
230 r
= ioctl(fd
, KVM_GET_API_VERSION
, 0);
232 fprintf(stderr
, "kvm kernel version too old: "
233 "KVM_GET_API_VERSION ioctl not supported\n");
236 if (r
< EXPECTED_KVM_API_VERSION
) {
237 fprintf(stderr
, "kvm kernel version too old: "
238 "We expect API version %d or newer, but got "
240 EXPECTED_KVM_API_VERSION
, r
);
243 if (r
> EXPECTED_KVM_API_VERSION
) {
244 fprintf(stderr
, "kvm userspace version too old\n");
248 kvm_page_size
= getpagesize();
249 kvm
= malloc(sizeof(*kvm
));
252 kvm
->callbacks
= callbacks
;
253 kvm
->opaque
= opaque
;
254 kvm
->dirty_pages_log_all
= 0;
255 kvm
->no_irqchip_creation
= 0;
256 kvm
->no_pit_creation
= 0;
264 void kvm_finalize(kvm_context_t kvm
)
266 if (kvm
->vcpu_fd
[0] != -1)
267 close(kvm
->vcpu_fd
[0]);
268 if (kvm
->vm_fd
!= -1)
274 void kvm_disable_irqchip_creation(kvm_context_t kvm
)
276 kvm
->no_irqchip_creation
= 1;
279 void kvm_disable_pit_creation(kvm_context_t kvm
)
281 kvm
->no_pit_creation
= 1;
284 int kvm_create_vcpu(kvm_context_t kvm
, int slot
)
289 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_VCPU
, slot
);
292 fprintf(stderr
, "kvm_create_vcpu: %m\n");
295 kvm
->vcpu_fd
[slot
] = r
;
296 mmap_size
= ioctl(kvm
->fd
, KVM_GET_VCPU_MMAP_SIZE
, 0);
297 if (mmap_size
== -1) {
299 fprintf(stderr
, "get vcpu mmap size: %m\n");
302 kvm
->run
[slot
] = mmap(NULL
, mmap_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
303 kvm
->vcpu_fd
[slot
], 0);
304 if (kvm
->run
[slot
] == MAP_FAILED
) {
306 fprintf(stderr
, "mmap vcpu area: %m\n");
312 int kvm_create_vm(kvm_context_t kvm
)
316 kvm
->vcpu_fd
[0] = -1;
318 fd
= ioctl(fd
, KVM_CREATE_VM
, 0);
320 fprintf(stderr
, "kvm_create_vm: %m\n");
327 static int kvm_create_default_phys_mem(kvm_context_t kvm
,
328 unsigned long phys_mem_bytes
,
331 unsigned long memory
= (phys_mem_bytes
+ PAGE_SIZE
- 1) & PAGE_MASK
;
334 #ifdef KVM_CAP_USER_MEMORY
335 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
340 r
= kvm_alloc_kernel_memory(kvm
, memory
, vm_mem
);
344 r
= kvm_arch_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
351 int kvm_check_extension(kvm_context_t kvm
, int ext
)
355 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, ext
);
361 void kvm_create_irqchip(kvm_context_t kvm
)
365 kvm
->irqchip_in_kernel
= 0;
366 #ifdef KVM_CAP_IRQCHIP
367 if (!kvm
->no_irqchip_creation
) {
368 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_IRQCHIP
);
369 if (r
> 0) { /* kernel irqchip supported */
370 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_IRQCHIP
);
372 kvm
->irqchip_in_kernel
= 1;
374 fprintf(stderr
, "Create kernel PIC irqchip failed\n");
380 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
, void **vm_mem
)
384 r
= kvm_create_vm(kvm
);
387 r
= kvm_arch_create(kvm
, phys_mem_bytes
, vm_mem
);
391 r
= kvm_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
394 kvm_create_irqchip(kvm
);
400 #ifdef KVM_CAP_USER_MEMORY
402 void *kvm_create_userspace_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
403 unsigned long len
, int log
, int writable
)
406 int prot
= PROT_READ
;
408 struct kvm_userspace_memory_region memory
= {
410 .guest_phys_addr
= phys_start
,
411 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
417 #if !defined(__s390__)
418 ptr
= mmap(NULL
, len
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, -1, 0);
420 ptr
= mmap(LIBKVM_S390_ORIGIN
, len
, prot
| PROT_EXEC
,
421 MAP_FIXED
| MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
423 if (ptr
== MAP_FAILED
) {
424 fprintf(stderr
, "create_userspace_phys_mem: %s", strerror(errno
));
430 memory
.userspace_addr
= (unsigned long)ptr
;
431 memory
.slot
= get_free_slot(kvm
);
432 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
434 fprintf(stderr
, "create_userspace_phys_mem: %s", strerror(errno
));
437 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
438 1, memory
.userspace_addr
, memory
.flags
);
443 void kvm_destroy_userspace_phys_mem(kvm_context_t kvm
,
444 unsigned long phys_start
)
447 struct kvm_userspace_memory_region memory
= {
449 .guest_phys_addr
= phys_start
,
453 memory
.userspace_addr
= 0;
454 memory
.slot
= get_slot(phys_start
);
456 if (memory
.slot
== -1)
459 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
461 fprintf(stderr
, "destroy_userspace_phys_mem: %s",
466 free_slot(memory
.slot
);
471 void *kvm_create_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
472 unsigned long len
, int log
, int writable
)
474 #ifdef KVM_CAP_USER_MEMORY
477 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
479 return kvm_create_userspace_phys_mem(kvm
, phys_start
, len
,
483 return kvm_create_kernel_phys_mem(kvm
, phys_start
, len
,
487 int kvm_is_intersecting_mem(kvm_context_t kvm
, unsigned long phys_start
)
489 return get_intersecting_slot(phys_start
) != -1;
492 int kvm_is_allocated_mem(kvm_context_t kvm
, unsigned long phys_start
,
497 slot
= get_slot(phys_start
);
500 if (slots
[slot
].len
== len
)
505 int kvm_create_mem_hole(kvm_context_t kvm
, unsigned long phys_start
,
508 #ifdef KVM_CAP_USER_MEMORY
511 struct kvm_userspace_memory_region rmslot
;
512 struct kvm_userspace_memory_region newslot1
;
513 struct kvm_userspace_memory_region newslot2
;
515 len
= (len
+ PAGE_SIZE
- 1) & PAGE_MASK
;
517 slot
= get_intersecting_slot(phys_start
);
518 /* no need to create hole, as there is already hole */
522 memset(&rmslot
, 0, sizeof(struct kvm_userspace_memory_region
));
523 memset(&newslot1
, 0, sizeof(struct kvm_userspace_memory_region
));
524 memset(&newslot2
, 0, sizeof(struct kvm_userspace_memory_region
));
526 rmslot
.guest_phys_addr
= slots
[slot
].phys_addr
;
529 newslot1
.guest_phys_addr
= slots
[slot
].phys_addr
;
530 newslot1
.memory_size
= phys_start
- slots
[slot
].phys_addr
;
531 newslot1
.slot
= slot
;
532 newslot1
.userspace_addr
= slots
[slot
].userspace_addr
;
533 newslot1
.flags
= slots
[slot
].flags
;
535 newslot2
.guest_phys_addr
= newslot1
.guest_phys_addr
+
536 newslot1
.memory_size
+ len
;
537 newslot2
.memory_size
= slots
[slot
].phys_addr
+
538 slots
[slot
].len
- newslot2
.guest_phys_addr
;
539 newslot2
.userspace_addr
= newslot1
.userspace_addr
+
540 newslot1
.memory_size
;
541 newslot2
.slot
= get_free_slot(kvm
);
542 newslot2
.flags
= newslot1
.flags
;
544 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &rmslot
);
546 fprintf(stderr
, "kvm_create_mem_hole: %s\n", strerror(errno
));
551 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &newslot1
);
553 fprintf(stderr
, "kvm_create_mem_hole: %s\n", strerror(errno
));
556 register_slot(newslot1
.slot
, newslot1
.guest_phys_addr
,
557 newslot1
.memory_size
, 1, newslot1
.userspace_addr
,
560 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &newslot2
);
562 fprintf(stderr
, "kvm_create_mem_hole: %s\n", strerror(errno
));
565 register_slot(newslot2
.slot
, newslot2
.guest_phys_addr
,
566 newslot2
.memory_size
, 1, newslot2
.userspace_addr
,
572 int kvm_register_userspace_phys_mem(kvm_context_t kvm
,
573 unsigned long phys_start
, void *userspace_addr
,
574 unsigned long len
, int log
)
577 #ifdef KVM_CAP_USER_MEMORY
578 struct kvm_userspace_memory_region memory
= {
580 .guest_phys_addr
= phys_start
,
581 .userspace_addr
= (unsigned long)(intptr_t)userspace_addr
,
582 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
586 memory
.slot
= get_free_slot(kvm
);
587 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
589 fprintf(stderr
, "create_userspace_phys_mem: %s\n", strerror(errno
));
592 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
593 1, memory
.userspace_addr
, memory
.flags
);
601 /* destroy/free a whole slot.
602 * phys_start, len and slot are the params passed to kvm_create_phys_mem()
604 void kvm_destroy_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
609 slot
= get_slot(phys_start
);
611 if (slot
>= KVM_MAX_NUM_MEM_REGIONS
) {
612 fprintf(stderr
, "BUG: %s: invalid parameters (slot=%d)\n",
616 if (phys_start
!= slots
[slot
].phys_addr
) {
618 "WARNING: %s: phys_start is 0x%lx expecting 0x%lx\n",
619 __FUNCTION__
, phys_start
, slots
[slot
].phys_addr
);
620 phys_start
= slots
[slot
].phys_addr
;
623 #ifdef KVM_CAP_USER_MEMORY
624 if (ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
) > 0)
625 kvm_destroy_userspace_phys_mem(kvm
, phys_start
);
628 kvm_create_kernel_phys_mem(kvm
, phys_start
, 0, 0, 0);
631 static int kvm_get_map(kvm_context_t kvm
, int ioctl_num
, int slot
, void *buf
)
634 struct kvm_dirty_log log
= {
638 log
.dirty_bitmap
= buf
;
640 r
= ioctl(kvm
->vm_fd
, ioctl_num
, &log
);
646 int kvm_get_dirty_pages(kvm_context_t kvm
, unsigned long phys_addr
, void *buf
)
650 slot
= get_slot(phys_addr
);
651 return kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, slot
, buf
);
654 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
655 #define BITMAP_SIZE(m) (ALIGN(((m)/PAGE_SIZE), sizeof(long) * 8) / 8)
657 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
658 unsigned long len
, void *buf
, void *opaque
,
659 int (*cb
)(unsigned long start
, unsigned long len
,
660 void*bitmap
, void *opaque
))
664 unsigned long end_addr
= phys_addr
+ len
;
666 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
667 if ((slots
[i
].len
&& slots
[i
].phys_addr
>= phys_addr
) &&
668 (slots
[i
].phys_addr
+ slots
[i
].len
<= end_addr
)) {
669 r
= kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, i
, buf
);
672 r
= cb(slots
[i
].phys_addr
, slots
[i
].len
, buf
, opaque
);
680 #ifdef KVM_CAP_IRQCHIP
682 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
)
684 struct kvm_irq_level event
;
687 if (!kvm
->irqchip_in_kernel
)
691 r
= ioctl(kvm
->vm_fd
, KVM_IRQ_LINE
, &event
);
693 perror("kvm_set_irq_level");
697 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
701 if (!kvm
->irqchip_in_kernel
)
703 r
= ioctl(kvm
->vm_fd
, KVM_GET_IRQCHIP
, chip
);
706 perror("kvm_get_irqchip\n");
711 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
715 if (!kvm
->irqchip_in_kernel
)
717 r
= ioctl(kvm
->vm_fd
, KVM_SET_IRQCHIP
, chip
);
720 perror("kvm_set_irqchip\n");
727 static int handle_io(kvm_context_t kvm
, struct kvm_run
*run
, int vcpu
)
729 uint16_t addr
= run
->io
.port
;
732 void *p
= (void *)run
+ run
->io
.data_offset
;
734 for (i
= 0; i
< run
->io
.count
; ++i
) {
735 switch (run
->io
.direction
) {
737 switch (run
->io
.size
) {
739 r
= kvm
->callbacks
->inb(kvm
->opaque
, addr
, p
);
742 r
= kvm
->callbacks
->inw(kvm
->opaque
, addr
, p
);
745 r
= kvm
->callbacks
->inl(kvm
->opaque
, addr
, p
);
748 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
752 case KVM_EXIT_IO_OUT
:
753 switch (run
->io
.size
) {
755 r
= kvm
->callbacks
->outb(kvm
->opaque
, addr
,
759 r
= kvm
->callbacks
->outw(kvm
->opaque
, addr
,
763 r
= kvm
->callbacks
->outl(kvm
->opaque
, addr
,
767 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
772 fprintf(stderr
, "bad I/O direction %d\n", run
->io
.direction
);
782 int handle_debug(kvm_context_t kvm
, int vcpu
)
784 return kvm
->callbacks
->debug(kvm
->opaque
, vcpu
);
787 int kvm_get_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
789 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_REGS
, regs
);
792 int kvm_set_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
794 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_REGS
, regs
);
797 int kvm_get_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
799 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_FPU
, fpu
);
802 int kvm_set_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
804 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_FPU
, fpu
);
807 int kvm_get_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
809 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_SREGS
, sregs
);
812 int kvm_set_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
814 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SREGS
, sregs
);
817 #ifdef KVM_CAP_MP_STATE
818 int kvm_get_mpstate(kvm_context_t kvm
, int vcpu
, struct kvm_mp_state
*mp_state
)
822 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
824 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_MP_STATE
, mp_state
);
828 int kvm_set_mpstate(kvm_context_t kvm
, int vcpu
, struct kvm_mp_state
*mp_state
)
832 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
834 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_MP_STATE
, mp_state
);
839 static int handle_mmio(kvm_context_t kvm
, struct kvm_run
*kvm_run
)
841 unsigned long addr
= kvm_run
->mmio
.phys_addr
;
842 void *data
= kvm_run
->mmio
.data
;
844 /* hack: Red Hat 7.1 generates these weird accesses. */
845 if ((addr
> 0xa0000-4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
848 if (kvm_run
->mmio
.is_write
)
849 return kvm
->callbacks
->mmio_write(kvm
->opaque
, addr
, data
,
852 return kvm
->callbacks
->mmio_read(kvm
->opaque
, addr
, data
,
856 int handle_io_window(kvm_context_t kvm
)
858 return kvm
->callbacks
->io_window(kvm
->opaque
);
861 int handle_halt(kvm_context_t kvm
, int vcpu
)
863 return kvm
->callbacks
->halt(kvm
->opaque
, vcpu
);
866 int handle_shutdown(kvm_context_t kvm
, int vcpu
)
868 return kvm
->callbacks
->shutdown(kvm
->opaque
, vcpu
);
871 int try_push_interrupts(kvm_context_t kvm
)
873 return kvm
->callbacks
->try_push_interrupts(kvm
->opaque
);
876 void post_kvm_run(kvm_context_t kvm
, int vcpu
)
878 kvm
->callbacks
->post_kvm_run(kvm
->opaque
, vcpu
);
881 int pre_kvm_run(kvm_context_t kvm
, int vcpu
)
883 return kvm
->callbacks
->pre_kvm_run(kvm
->opaque
, vcpu
);
886 int kvm_get_interrupt_flag(kvm_context_t kvm
, int vcpu
)
888 struct kvm_run
*run
= kvm
->run
[vcpu
];
893 int kvm_is_ready_for_interrupt_injection(kvm_context_t kvm
, int vcpu
)
895 struct kvm_run
*run
= kvm
->run
[vcpu
];
897 return run
->ready_for_interrupt_injection
;
900 int kvm_run(kvm_context_t kvm
, int vcpu
)
903 int fd
= kvm
->vcpu_fd
[vcpu
];
904 struct kvm_run
*run
= kvm
->run
[vcpu
];
907 #if !defined(__s390__)
908 if (!kvm
->irqchip_in_kernel
)
909 run
->request_interrupt_window
= try_push_interrupts(kvm
);
911 r
= pre_kvm_run(kvm
, vcpu
);
914 r
= ioctl(fd
, KVM_RUN
, 0);
916 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
918 post_kvm_run(kvm
, vcpu
);
919 fprintf(stderr
, "kvm_run: %s\n", strerror(-r
));
923 post_kvm_run(kvm
, vcpu
);
925 #if defined(KVM_CAP_COALESCED_MMIO)
926 if (kvm
->coalesced_mmio
) {
927 struct kvm_coalesced_mmio_ring
*ring
= (void *)run
+
928 kvm
->coalesced_mmio
* PAGE_SIZE
;
929 while (ring
->first
!= ring
->last
) {
930 kvm
->callbacks
->mmio_write(kvm
->opaque
,
931 ring
->coalesced_mmio
[ring
->first
].phys_addr
,
932 &ring
->coalesced_mmio
[ring
->first
].data
[0],
933 ring
->coalesced_mmio
[ring
->first
].len
);
935 ring
->first
= (ring
->first
+ 1) %
936 KVM_COALESCED_MMIO_MAX
;
941 #if !defined(__s390__)
943 r
= handle_io_window(kvm
);
948 switch (run
->exit_reason
) {
949 case KVM_EXIT_UNKNOWN
:
950 fprintf(stderr
, "unhandled vm exit: 0x%x vcpu_id %d\n",
951 (unsigned)run
->hw
.hardware_exit_reason
, vcpu
);
952 kvm_show_regs(kvm
, vcpu
);
955 case KVM_EXIT_FAIL_ENTRY
:
956 fprintf(stderr
, "kvm_run: failed entry, reason %u\n",
957 (unsigned)run
->fail_entry
.hardware_entry_failure_reason
& 0xffff);
960 case KVM_EXIT_EXCEPTION
:
961 fprintf(stderr
, "exception %d (%x)\n",
964 kvm_show_regs(kvm
, vcpu
);
965 kvm_show_code(kvm
, vcpu
);
969 r
= handle_io(kvm
, run
, vcpu
);
972 r
= handle_debug(kvm
, vcpu
);
975 r
= handle_mmio(kvm
, run
);
978 r
= handle_halt(kvm
, vcpu
);
980 case KVM_EXIT_IRQ_WINDOW_OPEN
:
982 case KVM_EXIT_SHUTDOWN
:
983 r
= handle_shutdown(kvm
, vcpu
);
985 #if defined(__s390__)
986 case KVM_EXIT_S390_SIEIC
:
987 r
= kvm
->callbacks
->s390_handle_intercept(kvm
, vcpu
,
990 case KVM_EXIT_S390_RESET
:
991 r
= kvm
->callbacks
->s390_handle_reset(kvm
, vcpu
, run
);
994 if (kvm_arch_run(run
, kvm
, vcpu
)) {
995 fprintf(stderr
, "unhandled vm exit: 0x%x\n",
997 kvm_show_regs(kvm
, vcpu
);
1009 int kvm_inject_irq(kvm_context_t kvm
, int vcpu
, unsigned irq
)
1011 struct kvm_interrupt intr
;
1014 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_INTERRUPT
, &intr
);
1017 int kvm_guest_debug(kvm_context_t kvm
, int vcpu
, struct kvm_debug_guest
*dbg
)
1019 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_DEBUG_GUEST
, dbg
);
1022 int kvm_set_signal_mask(kvm_context_t kvm
, int vcpu
, const sigset_t
*sigset
)
1024 struct kvm_signal_mask
*sigmask
;
1028 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, NULL
);
1033 sigmask
= malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1038 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1039 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, sigmask
);
1046 int kvm_irqchip_in_kernel(kvm_context_t kvm
)
1048 return kvm
->irqchip_in_kernel
;
1051 int kvm_pit_in_kernel(kvm_context_t kvm
)
1053 return kvm
->pit_in_kernel
;
1056 int kvm_has_sync_mmu(kvm_context_t kvm
)
1059 #ifdef KVM_CAP_SYNC_MMU
1060 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SYNC_MMU
);
1065 int kvm_init_coalesced_mmio(kvm_context_t kvm
)
1068 kvm
->coalesced_mmio
= 0;
1069 #ifdef KVM_CAP_COALESCED_MMIO
1070 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
1072 kvm
->coalesced_mmio
= r
;
1079 int kvm_register_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1081 #ifdef KVM_CAP_COALESCED_MMIO
1082 struct kvm_coalesced_mmio_zone zone
;
1085 if (kvm
->coalesced_mmio
) {
1090 r
= ioctl(kvm
->vm_fd
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
1092 perror("kvm_register_coalesced_mmio_zone");
1101 int kvm_unregister_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1103 #ifdef KVM_CAP_COALESCED_MMIO
1104 struct kvm_coalesced_mmio_zone zone
;
1107 if (kvm
->coalesced_mmio
) {
1112 r
= ioctl(kvm
->vm_fd
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
1114 perror("kvm_unregister_coalesced_mmio_zone");