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"
51 int kvm_abi
= EXPECTED_KVM_API_VERSION
;
55 unsigned long phys_addr
;
58 unsigned long userspace_addr
;
62 struct slot_info slots
[KVM_MAX_NUM_MEM_REGIONS
];
68 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
72 int get_free_slot(kvm_context_t kvm
)
77 #ifdef KVM_CAP_SET_TSS_ADDR
78 tss_ext
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
84 * on older kernels where the set tss ioctl is not supprted we must save
85 * slot 0 to hold the extended memory, as the vmx will use the last 3
93 for (; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
99 void register_slot(int slot
, unsigned long phys_addr
, unsigned long len
,
100 int user_alloc
, unsigned long userspace_addr
, unsigned flags
)
102 slots
[slot
].phys_addr
= phys_addr
;
103 slots
[slot
].len
= len
;
104 slots
[slot
].user_alloc
= user_alloc
;
105 slots
[slot
].userspace_addr
= userspace_addr
;
106 slots
[slot
].flags
= flags
;
109 void free_slot(int slot
)
114 int get_slot(unsigned long phys_addr
)
118 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
119 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
120 (slots
[i
].phys_addr
+ slots
[i
].len
-1) >= phys_addr
)
126 int get_intersecting_slot(unsigned long phys_addr
)
130 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
131 if (slots
[i
].len
&& slots
[i
].phys_addr
< phys_addr
&&
132 (slots
[i
].phys_addr
+ slots
[i
].len
) > phys_addr
)
138 * dirty pages logging control
140 static int kvm_dirty_pages_log_change(kvm_context_t kvm
, unsigned long phys_addr
146 slot
= get_slot(phys_addr
);
148 fprintf(stderr
, "BUG: %s: invalid parameters\n", __FUNCTION__
);
151 flag
|= slots
[slot
].flags
;
152 #ifdef KVM_CAP_USER_MEMORY
153 if (slots
[slot
].user_alloc
) {
154 struct kvm_userspace_memory_region mem
= {
156 .memory_size
= slots
[slot
].len
,
157 .guest_phys_addr
= slots
[slot
].phys_addr
,
158 .userspace_addr
= slots
[slot
].userspace_addr
,
161 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &mem
);
164 if (!slots
[slot
].user_alloc
) {
165 struct kvm_memory_region mem
= {
167 .memory_size
= slots
[slot
].len
,
168 .guest_phys_addr
= slots
[slot
].phys_addr
,
171 r
= ioctl(kvm
->vm_fd
, KVM_SET_MEMORY_REGION
, &mem
);
174 fprintf(stderr
, "%s: %m\n", __FUNCTION__
);
178 static int kvm_dirty_pages_log_change_all(kvm_context_t kvm
, __u32 flag
)
182 for (i
=r
=0; i
<KVM_MAX_NUM_MEM_REGIONS
&& r
==0; i
++) {
184 r
= kvm_dirty_pages_log_change(kvm
, slots
[i
].phys_addr
,
191 * Enable dirty page logging for all memory regions
193 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
)
195 if (kvm
->dirty_pages_log_all
)
197 kvm
->dirty_pages_log_all
= 1;
198 return kvm_dirty_pages_log_change_all(kvm
, KVM_MEM_LOG_DIRTY_PAGES
);
202 * Enable dirty page logging only for memory regions that were created with
203 * dirty logging enabled (disable for all other memory regions).
205 int kvm_dirty_pages_log_reset(kvm_context_t kvm
)
207 if (!kvm
->dirty_pages_log_all
)
209 kvm
->dirty_pages_log_all
= 0;
210 return kvm_dirty_pages_log_change_all(kvm
, 0);
214 kvm_context_t
kvm_init(struct kvm_callbacks
*callbacks
,
221 fd
= open("/dev/kvm", O_RDWR
);
223 perror("open /dev/kvm");
226 r
= ioctl(fd
, KVM_GET_API_VERSION
, 0);
228 fprintf(stderr
, "kvm kernel version too old: "
229 "KVM_GET_API_VERSION ioctl not supported\n");
232 if (r
< EXPECTED_KVM_API_VERSION
) {
233 fprintf(stderr
, "kvm kernel version too old: "
234 "We expect API version %d or newer, but got "
236 EXPECTED_KVM_API_VERSION
, r
);
239 if (r
> EXPECTED_KVM_API_VERSION
) {
240 fprintf(stderr
, "kvm userspace version too old\n");
244 kvm_page_size
= getpagesize();
245 kvm
= malloc(sizeof(*kvm
));
248 kvm
->callbacks
= callbacks
;
249 kvm
->opaque
= opaque
;
250 kvm
->dirty_pages_log_all
= 0;
251 kvm
->no_irqchip_creation
= 0;
259 void kvm_finalize(kvm_context_t kvm
)
261 if (kvm
->vcpu_fd
[0] != -1)
262 close(kvm
->vcpu_fd
[0]);
263 if (kvm
->vm_fd
!= -1)
269 void kvm_disable_irqchip_creation(kvm_context_t kvm
)
271 kvm
->no_irqchip_creation
= 1;
274 int kvm_create_vcpu(kvm_context_t kvm
, int slot
)
279 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_VCPU
, slot
);
282 fprintf(stderr
, "kvm_create_vcpu: %m\n");
285 kvm
->vcpu_fd
[slot
] = r
;
286 mmap_size
= ioctl(kvm
->fd
, KVM_GET_VCPU_MMAP_SIZE
, 0);
287 if (mmap_size
== -1) {
289 fprintf(stderr
, "get vcpu mmap size: %m\n");
292 kvm
->run
[slot
] = mmap(NULL
, mmap_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
293 kvm
->vcpu_fd
[slot
], 0);
294 if (kvm
->run
[slot
] == MAP_FAILED
) {
296 fprintf(stderr
, "mmap vcpu area: %m\n");
302 int kvm_create_vm(kvm_context_t kvm
)
306 kvm
->vcpu_fd
[0] = -1;
308 fd
= ioctl(fd
, KVM_CREATE_VM
, 0);
310 fprintf(stderr
, "kvm_create_vm: %m\n");
317 static int kvm_create_default_phys_mem(kvm_context_t kvm
,
318 unsigned long phys_mem_bytes
,
321 unsigned long memory
= (phys_mem_bytes
+ PAGE_SIZE
- 1) & PAGE_MASK
;
324 #ifdef KVM_CAP_USER_MEMORY
325 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
330 r
= kvm_alloc_kernel_memory(kvm
, memory
, vm_mem
);
334 r
= kvm_arch_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
338 kvm
->physical_memory
= *vm_mem
;
342 int kvm_check_extension(kvm_context_t kvm
, int ext
)
346 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, ext
);
352 void kvm_create_irqchip(kvm_context_t kvm
)
356 kvm
->irqchip_in_kernel
= 0;
357 #ifdef KVM_CAP_IRQCHIP
358 if (!kvm
->no_irqchip_creation
) {
359 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_IRQCHIP
);
360 if (r
> 0) { /* kernel irqchip supported */
361 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_IRQCHIP
);
363 kvm
->irqchip_in_kernel
= 1;
365 printf("Create kernel PIC irqchip failed\n");
371 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
, void **vm_mem
)
375 r
= kvm_create_vm(kvm
);
378 r
= kvm_arch_create(kvm
, phys_mem_bytes
, vm_mem
);
382 r
= kvm_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
385 kvm_create_irqchip(kvm
);
386 r
= kvm_create_vcpu(kvm
, 0);
394 #ifdef KVM_CAP_USER_MEMORY
396 void *kvm_create_userspace_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
397 unsigned long len
, int log
, int writable
)
400 int prot
= PROT_READ
;
402 struct kvm_userspace_memory_region memory
= {
404 .guest_phys_addr
= phys_start
,
405 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
411 ptr
= mmap(NULL
, len
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, -1, 0);
412 if (ptr
== MAP_FAILED
) {
413 fprintf(stderr
, "create_userspace_phys_mem: %s", strerror(errno
));
419 memory
.userspace_addr
= (unsigned long)ptr
;
420 memory
.slot
= get_free_slot(kvm
);
421 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
423 fprintf(stderr
, "create_userspace_phys_mem: %s", strerror(errno
));
426 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
427 1, memory
.userspace_addr
, memory
.flags
);
434 void *kvm_create_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
435 unsigned long len
, int log
, int writable
)
437 #ifdef KVM_CAP_USER_MEMORY
440 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
442 return kvm_create_userspace_phys_mem(kvm
, phys_start
, len
,
446 return kvm_create_kernel_phys_mem(kvm
, phys_start
, len
,
450 int kvm_is_intersecting_mem(kvm_context_t kvm
, unsigned long phys_start
)
452 return get_intersecting_slot(phys_start
) != -1;
455 int kvm_is_allocated_mem(kvm_context_t kvm
, unsigned long phys_start
,
460 slot
= get_slot(phys_start
);
463 if (slots
[slot
].len
== len
)
468 int kvm_create_mem_hole(kvm_context_t kvm
, unsigned long phys_start
,
471 #ifdef KVM_CAP_USER_MEMORY
474 struct kvm_userspace_memory_region rmslot
;
475 struct kvm_userspace_memory_region newslot1
;
476 struct kvm_userspace_memory_region newslot2
;
478 len
= (len
+ PAGE_SIZE
- 1) & PAGE_MASK
;
480 slot
= get_intersecting_slot(phys_start
);
481 /* no need to create hole, as there is already hole */
485 memset(&rmslot
, 0, sizeof(struct kvm_userspace_memory_region
));
486 memset(&newslot1
, 0, sizeof(struct kvm_userspace_memory_region
));
487 memset(&newslot2
, 0, sizeof(struct kvm_userspace_memory_region
));
489 rmslot
.guest_phys_addr
= slots
[slot
].phys_addr
;
492 newslot1
.guest_phys_addr
= slots
[slot
].phys_addr
;
493 newslot1
.memory_size
= phys_start
- slots
[slot
].phys_addr
;
494 newslot1
.slot
= slot
;
495 newslot1
.userspace_addr
= slots
[slot
].userspace_addr
;
496 newslot1
.flags
= slots
[slot
].flags
;
498 newslot2
.guest_phys_addr
= newslot1
.guest_phys_addr
+
499 newslot1
.memory_size
+ len
;
500 newslot2
.memory_size
= slots
[slot
].phys_addr
+
501 slots
[slot
].len
- newslot2
.guest_phys_addr
;
502 newslot2
.userspace_addr
= newslot1
.userspace_addr
+
503 newslot1
.memory_size
;
504 newslot2
.slot
= get_free_slot(kvm
);
505 newslot2
.flags
= newslot1
.flags
;
507 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &rmslot
);
509 fprintf(stderr
, "kvm_create_mem_hole: %s\n", strerror(errno
));
514 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &newslot1
);
516 fprintf(stderr
, "kvm_create_mem_hole: %s\n", strerror(errno
));
519 register_slot(newslot1
.slot
, newslot1
.guest_phys_addr
,
520 newslot1
.memory_size
, 1, newslot1
.userspace_addr
,
523 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &newslot2
);
525 fprintf(stderr
, "kvm_create_mem_hole: %s\n", strerror(errno
));
528 register_slot(newslot2
.slot
, newslot2
.guest_phys_addr
,
529 newslot2
.memory_size
, 1, newslot2
.userspace_addr
,
535 int kvm_register_userspace_phys_mem(kvm_context_t kvm
,
536 unsigned long phys_start
, void *userspace_addr
,
537 unsigned long len
, int log
)
540 #ifdef KVM_CAP_USER_MEMORY
541 struct kvm_userspace_memory_region memory
= {
543 .guest_phys_addr
= phys_start
,
544 .userspace_addr
= (intptr_t)userspace_addr
,
545 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
549 if (!kvm
->physical_memory
)
550 kvm
->physical_memory
= userspace_addr
- phys_start
;
552 memory
.slot
= get_free_slot(kvm
);
553 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
555 fprintf(stderr
, "create_userspace_phys_mem: %s\n", strerror(errno
));
558 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
559 1, memory
.userspace_addr
, memory
.flags
);
567 /* destroy/free a whole slot.
568 * phys_start, len and slot are the params passed to kvm_create_phys_mem()
570 void kvm_destroy_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
575 slot
= get_slot(phys_start
);
577 if (slot
>= KVM_MAX_NUM_MEM_REGIONS
) {
578 fprintf(stderr
, "BUG: %s: invalid parameters (slot=%d)\n",
582 if (phys_start
!= slots
[slot
].phys_addr
) {
584 "WARNING: %s: phys_start is 0x%lx expecting 0x%lx\n",
585 __FUNCTION__
, phys_start
, slots
[slot
].phys_addr
);
586 phys_start
= slots
[slot
].phys_addr
;
588 kvm_create_phys_mem(kvm
, phys_start
, 0, 0, 0);
591 static int kvm_get_map(kvm_context_t kvm
, int ioctl_num
, int slot
, void *buf
)
594 struct kvm_dirty_log log
= {
598 log
.dirty_bitmap
= buf
;
600 r
= ioctl(kvm
->vm_fd
, ioctl_num
, &log
);
606 int kvm_get_dirty_pages(kvm_context_t kvm
, unsigned long phys_addr
, void *buf
)
610 slot
= get_slot(phys_addr
);
611 return kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, slot
, buf
);
614 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
615 #define BITMAP_SIZE(m) (ALIGN(((m)/PAGE_SIZE), sizeof(long) * 8) / 8)
617 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
618 unsigned long len
, void *buf
, void *opaque
,
619 int (*cb
)(unsigned long start
, unsigned long len
,
620 void*bitmap
, void *opaque
))
624 unsigned long end_addr
= phys_addr
+ len
;
626 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
627 if ((slots
[i
].len
&& slots
[i
].phys_addr
>= phys_addr
) &&
628 (slots
[i
].phys_addr
+ slots
[i
].len
<= end_addr
)) {
629 r
= kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, i
, buf
);
632 r
= cb(slots
[i
].phys_addr
, slots
[i
].len
, buf
, opaque
);
640 #ifdef KVM_CAP_IRQCHIP
642 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
)
644 struct kvm_irq_level event
;
647 if (!kvm
->irqchip_in_kernel
)
651 r
= ioctl(kvm
->vm_fd
, KVM_IRQ_LINE
, &event
);
653 perror("kvm_set_irq_level");
657 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
661 if (!kvm
->irqchip_in_kernel
)
663 r
= ioctl(kvm
->vm_fd
, KVM_GET_IRQCHIP
, chip
);
666 perror("kvm_get_irqchip\n");
671 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
675 if (!kvm
->irqchip_in_kernel
)
677 r
= ioctl(kvm
->vm_fd
, KVM_SET_IRQCHIP
, chip
);
680 perror("kvm_set_irqchip\n");
687 static int handle_io(kvm_context_t kvm
, struct kvm_run
*run
, int vcpu
)
689 uint16_t addr
= run
->io
.port
;
692 void *p
= (void *)run
+ run
->io
.data_offset
;
694 for (i
= 0; i
< run
->io
.count
; ++i
) {
695 switch (run
->io
.direction
) {
697 switch (run
->io
.size
) {
699 r
= kvm
->callbacks
->inb(kvm
->opaque
, addr
, p
);
702 r
= kvm
->callbacks
->inw(kvm
->opaque
, addr
, p
);
705 r
= kvm
->callbacks
->inl(kvm
->opaque
, addr
, p
);
708 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
712 case KVM_EXIT_IO_OUT
:
713 switch (run
->io
.size
) {
715 r
= kvm
->callbacks
->outb(kvm
->opaque
, addr
,
719 r
= kvm
->callbacks
->outw(kvm
->opaque
, addr
,
723 r
= kvm
->callbacks
->outl(kvm
->opaque
, addr
,
727 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
732 fprintf(stderr
, "bad I/O direction %d\n", run
->io
.direction
);
742 int handle_debug(kvm_context_t kvm
, int vcpu
)
744 return kvm
->callbacks
->debug(kvm
->opaque
, vcpu
);
747 int kvm_get_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
749 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_REGS
, regs
);
752 int kvm_set_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
)
754 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_REGS
, regs
);
757 int kvm_get_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
759 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_FPU
, fpu
);
762 int kvm_set_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
)
764 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_FPU
, fpu
);
767 int kvm_get_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
769 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_GET_SREGS
, sregs
);
772 int kvm_set_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*sregs
)
774 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SREGS
, sregs
);
777 static int handle_mmio(kvm_context_t kvm
, struct kvm_run
*kvm_run
)
779 unsigned long addr
= kvm_run
->mmio
.phys_addr
;
780 void *data
= kvm_run
->mmio
.data
;
782 /* hack: Red Hat 7.1 generates these weird accesses. */
783 if ((addr
> 0xa0000-4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
786 if (kvm_run
->mmio
.is_write
)
787 return kvm
->callbacks
->mmio_write(kvm
->opaque
, addr
, data
,
790 return kvm
->callbacks
->mmio_read(kvm
->opaque
, addr
, data
,
794 int handle_io_window(kvm_context_t kvm
)
796 return kvm
->callbacks
->io_window(kvm
->opaque
);
799 int handle_halt(kvm_context_t kvm
, int vcpu
)
801 return kvm
->callbacks
->halt(kvm
->opaque
, vcpu
);
804 int handle_shutdown(kvm_context_t kvm
, int vcpu
)
806 return kvm
->callbacks
->shutdown(kvm
->opaque
, vcpu
);
809 int try_push_interrupts(kvm_context_t kvm
)
811 return kvm
->callbacks
->try_push_interrupts(kvm
->opaque
);
814 void post_kvm_run(kvm_context_t kvm
, int vcpu
)
816 kvm
->callbacks
->post_kvm_run(kvm
->opaque
, vcpu
);
819 int pre_kvm_run(kvm_context_t kvm
, int vcpu
)
821 return kvm
->callbacks
->pre_kvm_run(kvm
->opaque
, vcpu
);
824 int kvm_get_interrupt_flag(kvm_context_t kvm
, int vcpu
)
826 struct kvm_run
*run
= kvm
->run
[vcpu
];
831 int kvm_is_ready_for_interrupt_injection(kvm_context_t kvm
, int vcpu
)
833 struct kvm_run
*run
= kvm
->run
[vcpu
];
835 return run
->ready_for_interrupt_injection
;
838 int kvm_run(kvm_context_t kvm
, int vcpu
)
841 int fd
= kvm
->vcpu_fd
[vcpu
];
842 struct kvm_run
*run
= kvm
->run
[vcpu
];
845 if (!kvm
->irqchip_in_kernel
)
846 run
->request_interrupt_window
= try_push_interrupts(kvm
);
847 r
= pre_kvm_run(kvm
, vcpu
);
850 r
= ioctl(fd
, KVM_RUN
, 0);
851 post_kvm_run(kvm
, vcpu
);
853 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
855 printf("kvm_run: %m\n");
859 r
= handle_io_window(kvm
);
863 switch (run
->exit_reason
) {
864 case KVM_EXIT_UNKNOWN
:
865 fprintf(stderr
, "unhandled vm exit: 0x%x vcpu_id %d\n",
866 (unsigned)run
->hw
.hardware_exit_reason
, vcpu
);
867 kvm_show_regs(kvm
, vcpu
);
870 case KVM_EXIT_FAIL_ENTRY
:
871 fprintf(stderr
, "kvm_run: failed entry, reason %u\n",
872 (unsigned)run
->fail_entry
.hardware_entry_failure_reason
& 0xffff);
875 case KVM_EXIT_EXCEPTION
:
876 fprintf(stderr
, "exception %d (%x)\n",
879 kvm_show_regs(kvm
, vcpu
);
880 kvm_show_code(kvm
, vcpu
);
884 r
= handle_io(kvm
, run
, vcpu
);
887 r
= handle_debug(kvm
, vcpu
);
890 r
= handle_mmio(kvm
, run
);
893 r
= handle_halt(kvm
, vcpu
);
895 case KVM_EXIT_IRQ_WINDOW_OPEN
:
897 case KVM_EXIT_SHUTDOWN
:
898 r
= handle_shutdown(kvm
, vcpu
);
901 if (kvm_arch_run(run
, kvm
, vcpu
)) {
902 fprintf(stderr
, "unhandled vm exit: 0x%x\n",
904 kvm_show_regs(kvm
, vcpu
);
916 int kvm_inject_irq(kvm_context_t kvm
, int vcpu
, unsigned irq
)
918 struct kvm_interrupt intr
;
921 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_INTERRUPT
, &intr
);
924 int kvm_guest_debug(kvm_context_t kvm
, int vcpu
, struct kvm_debug_guest
*dbg
)
926 return ioctl(kvm
->vcpu_fd
[vcpu
], KVM_DEBUG_GUEST
, dbg
);
929 int kvm_set_signal_mask(kvm_context_t kvm
, int vcpu
, const sigset_t
*sigset
)
931 struct kvm_signal_mask
*sigmask
;
935 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, NULL
);
940 sigmask
= malloc(sizeof(*sigmask
) + sizeof(*sigset
));
945 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
946 r
= ioctl(kvm
->vcpu_fd
[vcpu
], KVM_SET_SIGNAL_MASK
, sigmask
);
953 int kvm_irqchip_in_kernel(kvm_context_t kvm
)
955 return kvm
->irqchip_in_kernel
;