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)
64 #define MIN(x,y) ((x) < (y) ? (x) : (y))
65 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
67 int kvm_abi
= EXPECTED_KVM_API_VERSION
;
70 static inline void set_gsi(kvm_context_t kvm
, unsigned int gsi
)
72 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
74 if (gsi
< kvm
->max_gsi
)
75 bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
77 DPRINTF("Invalid GSI %d\n");
80 static inline void clear_gsi(kvm_context_t kvm
, unsigned int gsi
)
82 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
84 if (gsi
< kvm
->max_gsi
)
85 bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
87 DPRINTF("Invalid GSI %d\n");
91 unsigned long phys_addr
;
93 unsigned long userspace_addr
;
98 struct slot_info slots
[KVM_MAX_NUM_MEM_REGIONS
];
100 static void init_slots(void)
104 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
108 static int get_free_slot(kvm_context_t kvm
)
113 #if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
114 tss_ext
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
120 * on older kernels where the set tss ioctl is not supprted we must save
121 * slot 0 to hold the extended memory, as the vmx will use the last 3
122 * pages of this slot.
129 for (; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
135 static void register_slot(int slot
, unsigned long phys_addr
, unsigned long len
,
136 unsigned long userspace_addr
, unsigned flags
)
138 slots
[slot
].phys_addr
= phys_addr
;
139 slots
[slot
].len
= len
;
140 slots
[slot
].userspace_addr
= userspace_addr
;
141 slots
[slot
].flags
= flags
;
144 static void free_slot(int slot
)
147 slots
[slot
].logging_count
= 0;
150 static int get_slot(unsigned long phys_addr
)
154 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
155 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
156 (slots
[i
].phys_addr
+ slots
[i
].len
-1) >= phys_addr
)
162 /* Returns -1 if this slot is not totally contained on any other,
163 * and the number of the slot otherwise */
164 static int get_container_slot(uint64_t phys_addr
, unsigned long size
)
168 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
169 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
170 (slots
[i
].phys_addr
+ slots
[i
].len
) >= phys_addr
+ size
)
175 int kvm_is_containing_region(kvm_context_t kvm
, unsigned long phys_addr
, unsigned long size
)
177 int slot
= get_container_slot(phys_addr
, size
);
184 * dirty pages logging control
186 static int kvm_dirty_pages_log_change(kvm_context_t kvm
,
187 unsigned long phys_addr
,
192 int slot
= get_slot(phys_addr
);
195 fprintf(stderr
, "BUG: %s: invalid parameters\n", __FUNCTION__
);
199 flags
= (slots
[slot
].flags
& ~mask
) | flags
;
200 if (flags
== slots
[slot
].flags
)
202 slots
[slot
].flags
= flags
;
205 struct kvm_userspace_memory_region mem
= {
207 .memory_size
= slots
[slot
].len
,
208 .guest_phys_addr
= slots
[slot
].phys_addr
,
209 .userspace_addr
= slots
[slot
].userspace_addr
,
210 .flags
= slots
[slot
].flags
,
214 DPRINTF("slot %d start %llx len %llx flags %x\n",
219 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &mem
);
221 fprintf(stderr
, "%s: %m\n", __FUNCTION__
);
226 static int kvm_dirty_pages_log_change_all(kvm_context_t kvm
,
227 int (*change
)(kvm_context_t kvm
,
233 for (i
=r
=0; i
<KVM_MAX_NUM_MEM_REGIONS
&& r
==0; i
++) {
235 r
= change(kvm
, slots
[i
].phys_addr
, slots
[i
].len
);
240 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm
,
244 int slot
= get_slot(phys_addr
);
246 DPRINTF("start %"PRIx64
" len %"PRIx64
"\n", phys_addr
, len
);
248 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
252 if (slots
[slot
].logging_count
++)
255 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
,
256 KVM_MEM_LOG_DIRTY_PAGES
,
257 KVM_MEM_LOG_DIRTY_PAGES
);
260 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm
,
264 int slot
= get_slot(phys_addr
);
267 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
271 if (--slots
[slot
].logging_count
)
274 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
,
276 KVM_MEM_LOG_DIRTY_PAGES
);
280 * Enable dirty page logging for all memory regions
282 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
)
284 if (kvm
->dirty_pages_log_all
)
286 kvm
->dirty_pages_log_all
= 1;
287 return kvm_dirty_pages_log_change_all(kvm
,
288 kvm_dirty_pages_log_enable_slot
);
292 * Enable dirty page logging only for memory regions that were created with
293 * dirty logging enabled (disable for all other memory regions).
295 int kvm_dirty_pages_log_reset(kvm_context_t kvm
)
297 if (!kvm
->dirty_pages_log_all
)
299 kvm
->dirty_pages_log_all
= 0;
300 return kvm_dirty_pages_log_change_all(kvm
,
301 kvm_dirty_pages_log_disable_slot
);
305 kvm_context_t
kvm_init(struct kvm_callbacks
*callbacks
,
312 fd
= open("/dev/kvm", O_RDWR
);
314 perror("open /dev/kvm");
317 r
= ioctl(fd
, KVM_GET_API_VERSION
, 0);
319 fprintf(stderr
, "kvm kernel version too old: "
320 "KVM_GET_API_VERSION ioctl not supported\n");
323 if (r
< EXPECTED_KVM_API_VERSION
) {
324 fprintf(stderr
, "kvm kernel version too old: "
325 "We expect API version %d or newer, but got "
327 EXPECTED_KVM_API_VERSION
, r
);
330 if (r
> EXPECTED_KVM_API_VERSION
) {
331 fprintf(stderr
, "kvm userspace version too old\n");
335 kvm_page_size
= getpagesize();
336 kvm
= malloc(sizeof(*kvm
));
339 memset(kvm
, 0, sizeof(*kvm
));
342 kvm
->callbacks
= callbacks
;
343 kvm
->opaque
= opaque
;
344 kvm
->dirty_pages_log_all
= 0;
345 kvm
->no_irqchip_creation
= 0;
346 kvm
->no_pit_creation
= 0;
348 gsi_count
= kvm_get_gsi_count(kvm
);
352 /* Round up so we can search ints using ffs */
353 gsi_bits
= ALIGN(gsi_count
, 32);
354 kvm
->used_gsi_bitmap
= malloc(gsi_bits
/ 8);
355 if (!kvm
->used_gsi_bitmap
)
357 memset(kvm
->used_gsi_bitmap
, 0, gsi_bits
/ 8);
358 kvm
->max_gsi
= gsi_bits
;
360 /* Mark any over-allocated bits as already in use */
361 for (i
= gsi_count
; i
< gsi_bits
; i
++)
371 void kvm_finalize(kvm_context_t kvm
)
373 if (kvm
->vcpu_fd
[0] != -1)
374 close(kvm
->vcpu_fd
[0]);
375 if (kvm
->vm_fd
!= -1)
381 void kvm_disable_irqchip_creation(kvm_context_t kvm
)
383 kvm
->no_irqchip_creation
= 1;
386 void kvm_disable_pit_creation(kvm_context_t kvm
)
388 kvm
->no_pit_creation
= 1;
391 int kvm_create_vcpu(kvm_context_t kvm
, int slot
)
396 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_VCPU
, slot
);
399 fprintf(stderr
, "kvm_create_vcpu: %m\n");
402 kvm
->vcpu_fd
[slot
] = r
;
403 mmap_size
= ioctl(kvm
->fd
, KVM_GET_VCPU_MMAP_SIZE
, 0);
404 if (mmap_size
== -1) {
406 fprintf(stderr
, "get vcpu mmap size: %m\n");
409 kvm
->run
[slot
] = mmap(NULL
, mmap_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
410 kvm
->vcpu_fd
[slot
], 0);
411 if (kvm
->run
[slot
] == MAP_FAILED
) {
413 fprintf(stderr
, "mmap vcpu area: %m\n");
419 int kvm_create_vm(kvm_context_t kvm
)
423 #ifdef KVM_CAP_IRQ_ROUTING
424 kvm
->irq_routes
= malloc(sizeof(*kvm
->irq_routes
));
425 if (!kvm
->irq_routes
)
427 memset(kvm
->irq_routes
, 0, sizeof(*kvm
->irq_routes
));
428 kvm
->nr_allocated_irq_routes
= 0;
431 kvm
->vcpu_fd
[0] = -1;
433 fd
= ioctl(fd
, KVM_CREATE_VM
, 0);
435 fprintf(stderr
, "kvm_create_vm: %m\n");
442 static int kvm_create_default_phys_mem(kvm_context_t kvm
,
443 unsigned long phys_mem_bytes
,
446 #ifdef KVM_CAP_USER_MEMORY
447 int r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
450 fprintf(stderr
, "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n");
452 #error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported
457 int kvm_check_extension(kvm_context_t kvm
, int ext
)
461 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, ext
);
467 void kvm_create_irqchip(kvm_context_t kvm
)
471 kvm
->irqchip_in_kernel
= 0;
472 #ifdef KVM_CAP_IRQCHIP
473 if (!kvm
->no_irqchip_creation
) {
474 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_IRQCHIP
);
475 if (r
> 0) { /* kernel irqchip supported */
476 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_IRQCHIP
);
478 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
479 #if defined(KVM_CAP_IRQ_INJECT_STATUS) && defined(KVM_IRQ_LINE_STATUS)
480 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
,
481 KVM_CAP_IRQ_INJECT_STATUS
);
483 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
485 kvm
->irqchip_in_kernel
= 1;
488 fprintf(stderr
, "Create kernel PIC irqchip failed\n");
494 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
, void **vm_mem
)
498 r
= kvm_create_vm(kvm
);
501 r
= kvm_arch_create(kvm
, phys_mem_bytes
, vm_mem
);
505 r
= kvm_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
508 kvm_create_irqchip(kvm
);
514 void *kvm_create_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
515 unsigned long len
, int log
, int writable
)
518 int prot
= PROT_READ
;
520 struct kvm_userspace_memory_region memory
= {
522 .guest_phys_addr
= phys_start
,
523 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
529 #if !defined(__s390__)
530 ptr
= mmap(NULL
, len
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, -1, 0);
532 ptr
= mmap(LIBKVM_S390_ORIGIN
, len
, prot
| PROT_EXEC
,
533 MAP_FIXED
| MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
535 if (ptr
== MAP_FAILED
) {
536 fprintf(stderr
, "%s: %s", __func__
, strerror(errno
));
542 memory
.userspace_addr
= (unsigned long)ptr
;
543 memory
.slot
= get_free_slot(kvm
);
544 DPRINTF("slot %d start %llx len %llx flags %x\n",
546 memory
.guest_phys_addr
,
549 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
551 fprintf(stderr
, "%s: %s", __func__
, strerror(errno
));
554 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
555 memory
.userspace_addr
, memory
.flags
);
560 int kvm_register_phys_mem(kvm_context_t kvm
,
561 unsigned long phys_start
, void *userspace_addr
,
562 unsigned long len
, int log
)
565 struct kvm_userspace_memory_region memory
= {
567 .guest_phys_addr
= phys_start
,
568 .userspace_addr
= (unsigned long)(intptr_t)userspace_addr
,
569 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
573 memory
.slot
= get_free_slot(kvm
);
574 DPRINTF("memory: gpa: %llx, size: %llx, uaddr: %llx, slot: %x, flags: %lx\n",
575 memory
.guest_phys_addr
, memory
.memory_size
,
576 memory
.userspace_addr
, memory
.slot
, memory
.flags
);
577 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
579 fprintf(stderr
, "create_userspace_phys_mem: %s\n", strerror(errno
));
582 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
583 memory
.userspace_addr
, memory
.flags
);
588 /* destroy/free a whole slot.
589 * phys_start, len and slot are the params passed to kvm_create_phys_mem()
591 void kvm_destroy_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
596 struct kvm_userspace_memory_region memory
= {
598 .guest_phys_addr
= phys_start
,
603 slot
= get_slot(phys_start
);
605 if ((slot
>= KVM_MAX_NUM_MEM_REGIONS
) || (slot
== -1)) {
606 fprintf(stderr
, "BUG: %s: invalid parameters (slot=%d)\n",
610 if (phys_start
!= slots
[slot
].phys_addr
) {
612 "WARNING: %s: phys_start is 0x%lx expecting 0x%lx\n",
613 __FUNCTION__
, phys_start
, slots
[slot
].phys_addr
);
614 phys_start
= slots
[slot
].phys_addr
;
618 DPRINTF("slot %d start %llx len %llx flags %x\n",
620 memory
.guest_phys_addr
,
623 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
625 fprintf(stderr
, "destroy_userspace_phys_mem: %s",
630 free_slot(memory
.slot
);
633 void kvm_unregister_memory_area(kvm_context_t kvm
, uint64_t phys_addr
, unsigned long size
)
636 int slot
= get_container_slot(phys_addr
, size
);
639 DPRINTF("Unregistering memory region %llx (%lx)\n", phys_addr
, size
);
640 kvm_destroy_phys_mem(kvm
, phys_addr
, size
);
645 static int kvm_get_map(kvm_context_t kvm
, int ioctl_num
, int slot
, void *buf
)
648 struct kvm_dirty_log log
= {
652 log
.dirty_bitmap
= buf
;
654 r
= ioctl(kvm
->vm_fd
, ioctl_num
, &log
);
660 int kvm_get_dirty_pages(kvm_context_t kvm
, unsigned long phys_addr
, void *buf
)
664 slot
= get_slot(phys_addr
);
665 return kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, slot
, buf
);
668 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
669 unsigned long len
, void *buf
, void *opaque
,
670 int (*cb
)(unsigned long start
, unsigned long len
,
671 void*bitmap
, void *opaque
))
675 unsigned long end_addr
= phys_addr
+ len
;
677 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
678 if ((slots
[i
].len
&& (uint64_t)slots
[i
].phys_addr
>= phys_addr
)
679 && ((uint64_t)slots
[i
].phys_addr
+ slots
[i
].len
<= end_addr
)) {
680 r
= kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, i
, buf
);
683 r
= cb(slots
[i
].phys_addr
, slots
[i
].len
, buf
, opaque
);
691 #ifdef KVM_CAP_IRQCHIP
693 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
, int *status
)
695 struct kvm_irq_level event
;
698 if (!kvm
->irqchip_in_kernel
)
702 r
= ioctl(kvm
->vm_fd
, kvm
->irqchip_inject_ioctl
, &event
);
704 perror("kvm_set_irq_level");
707 #ifdef KVM_CAP_IRQ_INJECT_STATUS
708 *status
= (kvm
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ?
718 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
722 if (!kvm
->irqchip_in_kernel
)
724 r
= ioctl(kvm
->vm_fd
, KVM_GET_IRQCHIP
, chip
);
727 perror("kvm_get_irqchip\n");
732 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
736 if (!kvm
->irqchip_in_kernel
)
738 r
= ioctl(kvm
->vm_fd
, KVM_SET_IRQCHIP
, chip
);
741 perror("kvm_set_irqchip\n");
748 static int handle_io(kvm_context_t kvm
, struct kvm_run
*run
, int vcpu
)
750 uint16_t addr
= run
->io
.port
;
753 void *p
= (void *)run
+ run
->io
.data_offset
;
755 for (i
= 0; i
< run
->io
.count
; ++i
) {
756 switch (run
->io
.direction
) {
758 switch (run
->io
.size
) {
760 r
= kvm
->callbacks
->inb(kvm
->opaque
, addr
, p
);
763 r
= kvm
->callbacks
->inw(kvm
->opaque
, addr
, p
);
766 r
= kvm
->callbacks
->inl(kvm
->opaque
, addr
, p
);
769 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
773 case KVM_EXIT_IO_OUT
:
774 switch (run
->io
.size
) {
776 r
= kvm
->callbacks
->outb(kvm
->opaque
, addr
,
780 r
= kvm
->callbacks
->outw(kvm
->opaque
, addr
,
784 r
= kvm
->callbacks
->outl(kvm
->opaque
, addr
,
788 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
793 fprintf(stderr
, "bad I/O direction %d\n", run
->io
.direction
);
803 int handle_debug(kvm_context_t kvm
, int vcpu
, void *env
)
805 #ifdef KVM_CAP_SET_GUEST_DEBUG
806 struct kvm_run
*run
= kvm
->run
[vcpu
];
808 return kvm
->callbacks
->debug(kvm
->opaque
, env
, &run
->debug
.arch
);
814 int kvm_get_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
816 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_REGS
, regs
);
819 int kvm_set_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
821 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_REGS
, regs
);
824 int kvm_get_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
826 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_FPU
, fpu
);
829 int kvm_set_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
831 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_FPU
, fpu
);
834 int kvm_get_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
836 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_SREGS
, sregs
);
839 int kvm_set_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
841 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SREGS
, sregs
);
844 #ifdef KVM_CAP_MP_STATE
845 int kvm_get_mpstate(kvm_context_t kvm
, int vcpu
, struct kvm_mp_state
*mp_state
)
849 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
851 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_MP_STATE
, mp_state
);
855 int kvm_set_mpstate(kvm_context_t kvm
, int vcpu
, struct kvm_mp_state
*mp_state
)
859 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
861 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_MP_STATE
, mp_state
);
866 static int handle_mmio(kvm_context_t kvm
, struct kvm_run
*kvm_run
)
868 unsigned long addr
= kvm_run
->mmio
.phys_addr
;
869 void *data
= kvm_run
->mmio
.data
;
871 /* hack: Red Hat 7.1 generates these weird accesses. */
872 if ((addr
> 0xa0000-4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
875 if (kvm_run
->mmio
.is_write
)
876 return kvm
->callbacks
->mmio_write(kvm
->opaque
, addr
, data
,
879 return kvm
->callbacks
->mmio_read(kvm
->opaque
, addr
, data
,
883 int handle_io_window(kvm_context_t kvm
)
885 return kvm
->callbacks
->io_window(kvm
->opaque
);
888 int handle_halt(kvm_context_t kvm
, int vcpu
)
890 return kvm
->callbacks
->halt(kvm
->opaque
, vcpu
);
893 int handle_shutdown(kvm_context_t kvm
, void *env
)
895 return kvm
->callbacks
->shutdown(kvm
->opaque
, env
);
898 int try_push_interrupts(kvm_context_t kvm
)
900 return kvm
->callbacks
->try_push_interrupts(kvm
->opaque
);
903 static inline void push_nmi(kvm_context_t kvm
)
905 #ifdef KVM_CAP_USER_NMI
906 kvm
->callbacks
->push_nmi(kvm
->opaque
);
907 #endif /* KVM_CAP_USER_NMI */
910 void post_kvm_run(kvm_context_t kvm
, void *env
)
912 kvm
->callbacks
->post_kvm_run(kvm
->opaque
, env
);
915 int pre_kvm_run(kvm_context_t kvm
, void *env
)
917 return kvm
->callbacks
->pre_kvm_run(kvm
->opaque
, env
);
920 int kvm_get_interrupt_flag(kvm_context_t kvm
, int vcpu
)
922 struct kvm_run
*run
= kvm
->run
[vcpu
];
927 int kvm_is_ready_for_interrupt_injection(kvm_context_t kvm
, int vcpu
)
929 struct kvm_run
*run
= kvm
->run
[vcpu
];
931 return run
->ready_for_interrupt_injection
;
934 int kvm_run(kvm_context_t kvm
, int vcpu
, void *env
)
937 int fd
= kvm
->vcpu_fd
[vcpu
];
938 struct kvm_run
*run
= kvm
->run
[vcpu
];
942 #if !defined(__s390__)
943 if (!kvm
->irqchip_in_kernel
)
944 run
->request_interrupt_window
= try_push_interrupts(kvm
);
946 r
= pre_kvm_run(kvm
, env
);
949 r
= ioctl(fd
, KVM_RUN
, 0);
951 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
953 post_kvm_run(kvm
, env
);
954 fprintf(stderr
, "kvm_run: %s\n", strerror(-r
));
958 post_kvm_run(kvm
, env
);
960 #if defined(KVM_CAP_COALESCED_MMIO)
961 if (kvm
->coalesced_mmio
) {
962 struct kvm_coalesced_mmio_ring
*ring
= (void *)run
+
963 kvm
->coalesced_mmio
* PAGE_SIZE
;
964 while (ring
->first
!= ring
->last
) {
965 kvm
->callbacks
->mmio_write(kvm
->opaque
,
966 ring
->coalesced_mmio
[ring
->first
].phys_addr
,
967 &ring
->coalesced_mmio
[ring
->first
].data
[0],
968 ring
->coalesced_mmio
[ring
->first
].len
);
970 ring
->first
= (ring
->first
+ 1) %
971 KVM_COALESCED_MMIO_MAX
;
976 #if !defined(__s390__)
978 r
= handle_io_window(kvm
);
983 switch (run
->exit_reason
) {
984 case KVM_EXIT_UNKNOWN
:
985 fprintf(stderr
, "unhandled vm exit: 0x%x vcpu_id %d\n",
986 (unsigned)run
->hw
.hardware_exit_reason
, vcpu
);
987 kvm_show_regs(kvm
, vcpu
);
990 case KVM_EXIT_FAIL_ENTRY
:
991 fprintf(stderr
, "kvm_run: failed entry, reason %u\n",
992 (unsigned)run
->fail_entry
.hardware_entry_failure_reason
& 0xffff);
993 kvm_show_regs(kvm
, vcpu
);
996 case KVM_EXIT_EXCEPTION
:
997 fprintf(stderr
, "exception %d (%x)\n",
1000 kvm_show_regs(kvm
, vcpu
);
1001 kvm_show_code(kvm
, vcpu
);
1005 r
= handle_io(kvm
, run
, vcpu
);
1007 case KVM_EXIT_DEBUG
:
1008 r
= handle_debug(kvm
, vcpu
, env
);
1011 r
= handle_mmio(kvm
, run
);
1014 r
= handle_halt(kvm
, vcpu
);
1016 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1018 case KVM_EXIT_SHUTDOWN
:
1019 r
= handle_shutdown(kvm
, env
);
1021 #if defined(__s390__)
1022 case KVM_EXIT_S390_SIEIC
:
1023 r
= kvm
->callbacks
->s390_handle_intercept(kvm
, vcpu
,
1026 case KVM_EXIT_S390_RESET
:
1027 r
= kvm
->callbacks
->s390_handle_reset(kvm
, vcpu
, run
);
1031 if (kvm_arch_run(run
, kvm
, vcpu
)) {
1032 fprintf(stderr
, "unhandled vm exit: 0x%x\n",
1034 kvm_show_regs(kvm
, vcpu
);
1046 int kvm_inject_irq(kvm_context_t kvm
, int vcpu
, unsigned irq
)
1048 struct kvm_interrupt intr
;
1051 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_INTERRUPT
, &intr
);
1054 #ifdef KVM_CAP_SET_GUEST_DEBUG
1055 int kvm_set_guest_debug(kvm_context_t kvm
, int vcpu
, struct kvm_guest_debug
*dbg
)
1057 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_GUEST_DEBUG
, dbg
);
1061 int kvm_set_signal_mask(kvm_context_t kvm
, int vcpu
, const sigset_t
*sigset
)
1063 struct kvm_signal_mask
*sigmask
;
1067 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, NULL
);
1072 sigmask
= malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1077 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1078 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, sigmask
);
1085 int kvm_irqchip_in_kernel(kvm_context_t kvm
)
1087 return kvm
->irqchip_in_kernel
;
1090 int kvm_pit_in_kernel(kvm_context_t kvm
)
1092 return kvm
->pit_in_kernel
;
1095 int kvm_has_sync_mmu(kvm_context_t kvm
)
1098 #ifdef KVM_CAP_SYNC_MMU
1099 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SYNC_MMU
);
1104 int kvm_inject_nmi(kvm_context_t kvm
, int vcpu
)
1106 #ifdef KVM_CAP_USER_NMI
1107 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_NMI
);
1113 int kvm_init_coalesced_mmio(kvm_context_t kvm
)
1116 kvm
->coalesced_mmio
= 0;
1117 #ifdef KVM_CAP_COALESCED_MMIO
1118 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
1120 kvm
->coalesced_mmio
= r
;
1127 int kvm_register_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1129 #ifdef KVM_CAP_COALESCED_MMIO
1130 struct kvm_coalesced_mmio_zone zone
;
1133 if (kvm
->coalesced_mmio
) {
1138 r
= ioctl(kvm
->vm_fd
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
1140 perror("kvm_register_coalesced_mmio_zone");
1149 int kvm_unregister_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1151 #ifdef KVM_CAP_COALESCED_MMIO
1152 struct kvm_coalesced_mmio_zone zone
;
1155 if (kvm
->coalesced_mmio
) {
1160 r
= ioctl(kvm
->vm_fd
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
1162 perror("kvm_unregister_coalesced_mmio_zone");
1165 DPRINTF("Unregistered coalesced mmio region for %llx (%lx)\n", addr
, size
);
1172 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1173 int kvm_assign_pci_device(kvm_context_t kvm
,
1174 struct kvm_assigned_pci_dev
*assigned_dev
)
1178 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
1185 static int kvm_old_assign_irq(kvm_context_t kvm
,
1186 struct kvm_assigned_irq
*assigned_irq
)
1190 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_IRQ
, assigned_irq
);
1197 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
1198 int kvm_assign_irq(kvm_context_t kvm
,
1199 struct kvm_assigned_irq
*assigned_irq
)
1203 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_ASSIGN_DEV_IRQ
);
1205 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_DEV_IRQ
, assigned_irq
);
1211 return kvm_old_assign_irq(kvm
, assigned_irq
);
1214 int kvm_deassign_irq(kvm_context_t kvm
,
1215 struct kvm_assigned_irq
*assigned_irq
)
1219 ret
= ioctl(kvm
->vm_fd
, KVM_DEASSIGN_DEV_IRQ
, assigned_irq
);
1226 int kvm_assign_irq(kvm_context_t kvm
,
1227 struct kvm_assigned_irq
*assigned_irq
)
1229 return kvm_old_assign_irq(kvm
, assigned_irq
);
1234 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1235 int kvm_deassign_pci_device(kvm_context_t kvm
,
1236 struct kvm_assigned_pci_dev
*assigned_dev
)
1240 ret
= ioctl(kvm
->vm_fd
, KVM_DEASSIGN_PCI_DEVICE
, assigned_dev
);
1248 int kvm_destroy_memory_region_works(kvm_context_t kvm
)
1252 #ifdef KVM_CAP_DESTROY_MEMORY_REGION_WORKS
1253 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
,
1254 KVM_CAP_DESTROY_MEMORY_REGION_WORKS
);
1261 int kvm_reinject_control(kvm_context_t kvm
, int pit_reinject
)
1263 #ifdef KVM_CAP_REINJECT_CONTROL
1265 struct kvm_reinject_control control
;
1267 control
.pit_reinject
= pit_reinject
;
1269 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_REINJECT_CONTROL
);
1271 r
= ioctl(kvm
->vm_fd
, KVM_REINJECT_CONTROL
, &control
);
1280 int kvm_has_gsi_routing(kvm_context_t kvm
)
1284 #ifdef KVM_CAP_IRQ_ROUTING
1285 r
= kvm_check_extension(kvm
, KVM_CAP_IRQ_ROUTING
);
1290 int kvm_get_gsi_count(kvm_context_t kvm
)
1292 #ifdef KVM_CAP_IRQ_ROUTING
1293 return kvm_check_extension(kvm
, KVM_CAP_IRQ_ROUTING
);
1299 int kvm_clear_gsi_routes(kvm_context_t kvm
)
1301 #ifdef KVM_CAP_IRQ_ROUTING
1302 kvm
->irq_routes
->nr
= 0;
1309 int kvm_add_routing_entry(kvm_context_t kvm
,
1310 struct kvm_irq_routing_entry
* entry
)
1312 #ifdef KVM_CAP_IRQ_ROUTING
1313 struct kvm_irq_routing
*z
;
1314 struct kvm_irq_routing_entry
*new;
1317 if (kvm
->irq_routes
->nr
== kvm
->nr_allocated_irq_routes
) {
1318 n
= kvm
->nr_allocated_irq_routes
* 2;
1321 size
= sizeof(struct kvm_irq_routing
);
1322 size
+= n
* sizeof(*new);
1323 z
= realloc(kvm
->irq_routes
, size
);
1326 kvm
->nr_allocated_irq_routes
= n
;
1327 kvm
->irq_routes
= z
;
1329 n
= kvm
->irq_routes
->nr
++;
1330 new = &kvm
->irq_routes
->entries
[n
];
1331 memset(new, 0, sizeof(*new));
1332 new->gsi
= entry
->gsi
;
1333 new->type
= entry
->type
;
1334 new->flags
= entry
->flags
;
1337 set_gsi(kvm
, entry
->gsi
);
1345 int kvm_add_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1347 #ifdef KVM_CAP_IRQ_ROUTING
1348 struct kvm_irq_routing_entry e
;
1351 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1353 e
.u
.irqchip
.irqchip
= irqchip
;
1354 e
.u
.irqchip
.pin
= pin
;
1355 return kvm_add_routing_entry(kvm
, &e
);
1361 int kvm_del_routing_entry(kvm_context_t kvm
,
1362 struct kvm_irq_routing_entry
* entry
)
1364 #ifdef KVM_CAP_IRQ_ROUTING
1365 struct kvm_irq_routing_entry
*e
, *p
;
1366 int i
, gsi
, found
= 0;
1370 for (i
= 0; i
< kvm
->irq_routes
->nr
; ++i
) {
1371 e
= &kvm
->irq_routes
->entries
[i
];
1372 if (e
->type
== entry
->type
1376 case KVM_IRQ_ROUTING_IRQCHIP
: {
1377 if (e
->u
.irqchip
.irqchip
==
1378 entry
->u
.irqchip
.irqchip
1379 && e
->u
.irqchip
.pin
==
1380 entry
->u
.irqchip
.pin
) {
1381 p
= &kvm
->irq_routes
->
1382 entries
[--kvm
->irq_routes
->nr
];
1388 case KVM_IRQ_ROUTING_MSI
: {
1389 if (e
->u
.msi
.address_lo
==
1390 entry
->u
.msi
.address_lo
1391 && e
->u
.msi
.address_hi
==
1392 entry
->u
.msi
.address_hi
1393 && e
->u
.msi
.data
== entry
->u
.msi
.data
) {
1394 p
= &kvm
->irq_routes
->
1395 entries
[--kvm
->irq_routes
->nr
];
1405 /* If there are no other users of this GSI
1406 * mark it available in the bitmap */
1407 for (i
= 0; i
< kvm
->irq_routes
->nr
; i
++) {
1408 e
= &kvm
->irq_routes
->entries
[i
];
1412 if (i
== kvm
->irq_routes
->nr
)
1413 clear_gsi(kvm
, gsi
);
1425 int kvm_del_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1427 #ifdef KVM_CAP_IRQ_ROUTING
1428 struct kvm_irq_routing_entry e
;
1431 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1433 e
.u
.irqchip
.irqchip
= irqchip
;
1434 e
.u
.irqchip
.pin
= pin
;
1435 return kvm_del_routing_entry(kvm
, &e
);
1441 int kvm_commit_irq_routes(kvm_context_t kvm
)
1443 #ifdef KVM_CAP_IRQ_ROUTING
1446 kvm
->irq_routes
->flags
= 0;
1447 r
= ioctl(kvm
->vm_fd
, KVM_SET_GSI_ROUTING
, kvm
->irq_routes
);
1456 int kvm_get_irq_route_gsi(kvm_context_t kvm
)
1459 uint32_t *buf
= kvm
->used_gsi_bitmap
;
1461 /* Return the lowest unused GSI in the bitmap */
1462 for (i
= 0; i
< kvm
->max_gsi
/ 32; i
++) {
1467 return bit
- 1 + i
* 32;
1473 #ifdef KVM_CAP_DEVICE_MSIX
1474 int kvm_assign_set_msix_nr(kvm_context_t kvm
,
1475 struct kvm_assigned_msix_nr
*msix_nr
)
1479 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_SET_MSIX_NR
, msix_nr
);
1486 int kvm_assign_set_msix_entry(kvm_context_t kvm
,
1487 struct kvm_assigned_msix_entry
*entry
)
1491 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_SET_MSIX_ENTRY
, entry
);