4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
9 #include "config-host.h"
15 #include "qemu-common.h"
22 #include "libkvm-all.h"
26 #include <sys/utsname.h>
27 #include <sys/syscall.h>
29 #include <sys/ioctl.h>
34 #define EXPECTED_KVM_API_VERSION 12
36 #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
37 #error libkvm: userspace and kernel version mismatch
43 int kvm_pit_reinject
= 1;
45 kvm_context_t kvm_context
;
47 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
48 pthread_cond_t qemu_vcpu_cond
= PTHREAD_COND_INITIALIZER
;
49 pthread_cond_t qemu_system_cond
= PTHREAD_COND_INITIALIZER
;
50 pthread_cond_t qemu_pause_cond
= PTHREAD_COND_INITIALIZER
;
51 pthread_cond_t qemu_work_cond
= PTHREAD_COND_INITIALIZER
;
52 __thread CPUState
*current_env
;
54 static int qemu_system_ready
;
56 #define SIG_IPI (SIGRTMIN+4)
59 static int io_thread_fd
= -1;
60 static int io_thread_sigfd
= -1;
62 static CPUState
*kvm_debug_cpu_requested
;
64 static uint64_t phys_ram_size
;
66 /* The list of ioperm_data */
67 static LIST_HEAD(, ioperm_data
) ioperm_head
;
69 //#define DEBUG_MEMREG
71 #define DPRINTF(fmt, args...) \
72 do { fprintf(stderr, "%s:%d " fmt , __func__, __LINE__, ##args); } while (0)
74 #define DPRINTF(fmt, args...) do {} while (0)
77 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
79 int kvm_abi
= EXPECTED_KVM_API_VERSION
;
82 static inline void set_gsi(kvm_context_t kvm
, unsigned int gsi
)
84 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
86 if (gsi
< kvm
->max_gsi
)
87 bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
89 DPRINTF("Invalid GSI %d\n");
92 static inline void clear_gsi(kvm_context_t kvm
, unsigned int gsi
)
94 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
96 if (gsi
< kvm
->max_gsi
)
97 bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
99 DPRINTF("Invalid GSI %d\n");
103 unsigned long phys_addr
;
105 unsigned long userspace_addr
;
110 struct slot_info slots
[KVM_MAX_NUM_MEM_REGIONS
];
112 static void init_slots(void)
116 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
120 static int get_free_slot(kvm_context_t kvm
)
125 #if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
126 tss_ext
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
132 * on older kernels where the set tss ioctl is not supprted we must save
133 * slot 0 to hold the extended memory, as the vmx will use the last 3
134 * pages of this slot.
141 for (; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
147 static void register_slot(int slot
, unsigned long phys_addr
, unsigned long len
,
148 unsigned long userspace_addr
, unsigned flags
)
150 slots
[slot
].phys_addr
= phys_addr
;
151 slots
[slot
].len
= len
;
152 slots
[slot
].userspace_addr
= userspace_addr
;
153 slots
[slot
].flags
= flags
;
156 static void free_slot(int slot
)
159 slots
[slot
].logging_count
= 0;
162 static int get_slot(unsigned long phys_addr
)
166 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
167 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
168 (slots
[i
].phys_addr
+ slots
[i
].len
-1) >= phys_addr
)
174 /* Returns -1 if this slot is not totally contained on any other,
175 * and the number of the slot otherwise */
176 static int get_container_slot(uint64_t phys_addr
, unsigned long size
)
180 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
181 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
182 (slots
[i
].phys_addr
+ slots
[i
].len
) >= phys_addr
+ size
)
187 int kvm_is_containing_region(kvm_context_t kvm
, unsigned long phys_addr
, unsigned long size
)
189 int slot
= get_container_slot(phys_addr
, size
);
196 * dirty pages logging control
198 static int kvm_dirty_pages_log_change(kvm_context_t kvm
,
199 unsigned long phys_addr
,
204 int slot
= get_slot(phys_addr
);
207 fprintf(stderr
, "BUG: %s: invalid parameters\n", __FUNCTION__
);
211 flags
= (slots
[slot
].flags
& ~mask
) | flags
;
212 if (flags
== slots
[slot
].flags
)
214 slots
[slot
].flags
= flags
;
217 struct kvm_userspace_memory_region mem
= {
219 .memory_size
= slots
[slot
].len
,
220 .guest_phys_addr
= slots
[slot
].phys_addr
,
221 .userspace_addr
= slots
[slot
].userspace_addr
,
222 .flags
= slots
[slot
].flags
,
226 DPRINTF("slot %d start %llx len %llx flags %x\n",
231 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &mem
);
233 fprintf(stderr
, "%s: %m\n", __FUNCTION__
);
238 static int kvm_dirty_pages_log_change_all(kvm_context_t kvm
,
239 int (*change
)(kvm_context_t kvm
,
245 for (i
=r
=0; i
<KVM_MAX_NUM_MEM_REGIONS
&& r
==0; i
++) {
247 r
= change(kvm
, slots
[i
].phys_addr
, slots
[i
].len
);
252 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm
,
256 int slot
= get_slot(phys_addr
);
258 DPRINTF("start %"PRIx64
" len %"PRIx64
"\n", phys_addr
, len
);
260 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
264 if (slots
[slot
].logging_count
++)
267 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
,
268 KVM_MEM_LOG_DIRTY_PAGES
,
269 KVM_MEM_LOG_DIRTY_PAGES
);
272 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm
,
276 int slot
= get_slot(phys_addr
);
279 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
283 if (--slots
[slot
].logging_count
)
286 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
,
288 KVM_MEM_LOG_DIRTY_PAGES
);
292 * Enable dirty page logging for all memory regions
294 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
)
296 if (kvm
->dirty_pages_log_all
)
298 kvm
->dirty_pages_log_all
= 1;
299 return kvm_dirty_pages_log_change_all(kvm
,
300 kvm_dirty_pages_log_enable_slot
);
304 * Enable dirty page logging only for memory regions that were created with
305 * dirty logging enabled (disable for all other memory regions).
307 int kvm_dirty_pages_log_reset(kvm_context_t kvm
)
309 if (!kvm
->dirty_pages_log_all
)
311 kvm
->dirty_pages_log_all
= 0;
312 return kvm_dirty_pages_log_change_all(kvm
,
313 kvm_dirty_pages_log_disable_slot
);
317 kvm_context_t
kvm_init(struct kvm_callbacks
*callbacks
,
324 fd
= open("/dev/kvm", O_RDWR
);
326 perror("open /dev/kvm");
329 r
= ioctl(fd
, KVM_GET_API_VERSION
, 0);
331 fprintf(stderr
, "kvm kernel version too old: "
332 "KVM_GET_API_VERSION ioctl not supported\n");
335 if (r
< EXPECTED_KVM_API_VERSION
) {
336 fprintf(stderr
, "kvm kernel version too old: "
337 "We expect API version %d or newer, but got "
339 EXPECTED_KVM_API_VERSION
, r
);
342 if (r
> EXPECTED_KVM_API_VERSION
) {
343 fprintf(stderr
, "kvm userspace version too old\n");
347 kvm_page_size
= getpagesize();
348 kvm
= malloc(sizeof(*kvm
));
351 memset(kvm
, 0, sizeof(*kvm
));
354 kvm
->callbacks
= callbacks
;
355 kvm
->opaque
= opaque
;
356 kvm
->dirty_pages_log_all
= 0;
357 kvm
->no_irqchip_creation
= 0;
358 kvm
->no_pit_creation
= 0;
360 gsi_count
= kvm_get_gsi_count(kvm
);
364 /* Round up so we can search ints using ffs */
365 gsi_bits
= ALIGN(gsi_count
, 32);
366 kvm
->used_gsi_bitmap
= malloc(gsi_bits
/ 8);
367 if (!kvm
->used_gsi_bitmap
)
369 memset(kvm
->used_gsi_bitmap
, 0, gsi_bits
/ 8);
370 kvm
->max_gsi
= gsi_bits
;
372 /* Mark any over-allocated bits as already in use */
373 for (i
= gsi_count
; i
< gsi_bits
; i
++)
383 void kvm_finalize(kvm_context_t kvm
)
386 if (kvm->vcpu_fd[0] != -1)
387 close(kvm->vcpu_fd[0]);
388 if (kvm->vm_fd != -1)
395 void kvm_disable_irqchip_creation(kvm_context_t kvm
)
397 kvm
->no_irqchip_creation
= 1;
400 void kvm_disable_pit_creation(kvm_context_t kvm
)
402 kvm
->no_pit_creation
= 1;
405 kvm_vcpu_context_t
kvm_create_vcpu(kvm_context_t kvm
, int id
)
409 kvm_vcpu_context_t vcpu_ctx
= malloc(sizeof(struct kvm_vcpu_context
));
419 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_VCPU
, id
);
421 fprintf(stderr
, "kvm_create_vcpu: %m\n");
425 mmap_size
= ioctl(kvm
->fd
, KVM_GET_VCPU_MMAP_SIZE
, 0);
426 if (mmap_size
== -1) {
427 fprintf(stderr
, "get vcpu mmap size: %m\n");
430 vcpu_ctx
->run
= mmap(NULL
, mmap_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
432 if (vcpu_ctx
->run
== MAP_FAILED
) {
433 fprintf(stderr
, "mmap vcpu area: %m\n");
444 int kvm_create_vm(kvm_context_t kvm
)
448 #ifdef KVM_CAP_IRQ_ROUTING
449 kvm
->irq_routes
= malloc(sizeof(*kvm
->irq_routes
));
450 if (!kvm
->irq_routes
)
452 memset(kvm
->irq_routes
, 0, sizeof(*kvm
->irq_routes
));
453 kvm
->nr_allocated_irq_routes
= 0;
456 fd
= ioctl(fd
, KVM_CREATE_VM
, 0);
458 fprintf(stderr
, "kvm_create_vm: %m\n");
465 static int kvm_create_default_phys_mem(kvm_context_t kvm
,
466 unsigned long phys_mem_bytes
,
469 #ifdef KVM_CAP_USER_MEMORY
470 int r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
473 fprintf(stderr
, "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n");
475 #error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported
480 int kvm_check_extension(kvm_context_t kvm
, int ext
)
484 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, ext
);
490 void kvm_create_irqchip(kvm_context_t kvm
)
494 kvm
->irqchip_in_kernel
= 0;
495 #ifdef KVM_CAP_IRQCHIP
496 if (!kvm
->no_irqchip_creation
) {
497 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_IRQCHIP
);
498 if (r
> 0) { /* kernel irqchip supported */
499 r
= ioctl(kvm
->vm_fd
, KVM_CREATE_IRQCHIP
);
501 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
502 #if defined(KVM_CAP_IRQ_INJECT_STATUS) && defined(KVM_IRQ_LINE_STATUS)
503 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
,
504 KVM_CAP_IRQ_INJECT_STATUS
);
506 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
508 kvm
->irqchip_in_kernel
= 1;
511 fprintf(stderr
, "Create kernel PIC irqchip failed\n");
517 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
, void **vm_mem
)
521 r
= kvm_create_vm(kvm
);
524 r
= kvm_arch_create(kvm
, phys_mem_bytes
, vm_mem
);
528 r
= kvm_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
531 kvm_create_irqchip(kvm
);
537 void *kvm_create_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
538 unsigned long len
, int log
, int writable
)
541 int prot
= PROT_READ
;
543 struct kvm_userspace_memory_region memory
= {
545 .guest_phys_addr
= phys_start
,
546 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
552 #if !defined(__s390__)
553 ptr
= mmap(NULL
, len
, prot
, MAP_ANONYMOUS
| MAP_SHARED
, -1, 0);
555 ptr
= mmap(LIBKVM_S390_ORIGIN
, len
, prot
| PROT_EXEC
,
556 MAP_FIXED
| MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
558 if (ptr
== MAP_FAILED
) {
559 fprintf(stderr
, "%s: %s", __func__
, strerror(errno
));
565 memory
.userspace_addr
= (unsigned long)ptr
;
566 memory
.slot
= get_free_slot(kvm
);
567 DPRINTF("slot %d start %llx len %llx flags %x\n",
569 memory
.guest_phys_addr
,
572 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
574 fprintf(stderr
, "%s: %s", __func__
, strerror(errno
));
577 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
578 memory
.userspace_addr
, memory
.flags
);
583 int kvm_register_phys_mem(kvm_context_t kvm
,
584 unsigned long phys_start
, void *userspace_addr
,
585 unsigned long len
, int log
)
588 struct kvm_userspace_memory_region memory
= {
590 .guest_phys_addr
= phys_start
,
591 .userspace_addr
= (unsigned long)(intptr_t)userspace_addr
,
592 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
596 memory
.slot
= get_free_slot(kvm
);
597 DPRINTF("memory: gpa: %llx, size: %llx, uaddr: %llx, slot: %x, flags: %lx\n",
598 memory
.guest_phys_addr
, memory
.memory_size
,
599 memory
.userspace_addr
, memory
.slot
, memory
.flags
);
600 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
602 fprintf(stderr
, "create_userspace_phys_mem: %s\n", strerror(errno
));
605 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
606 memory
.userspace_addr
, memory
.flags
);
611 /* destroy/free a whole slot.
612 * phys_start, len and slot are the params passed to kvm_create_phys_mem()
614 void kvm_destroy_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
619 struct kvm_userspace_memory_region memory
= {
621 .guest_phys_addr
= phys_start
,
626 slot
= get_slot(phys_start
);
628 if ((slot
>= KVM_MAX_NUM_MEM_REGIONS
) || (slot
== -1)) {
629 fprintf(stderr
, "BUG: %s: invalid parameters (slot=%d)\n",
633 if (phys_start
!= slots
[slot
].phys_addr
) {
635 "WARNING: %s: phys_start is 0x%lx expecting 0x%lx\n",
636 __FUNCTION__
, phys_start
, slots
[slot
].phys_addr
);
637 phys_start
= slots
[slot
].phys_addr
;
641 DPRINTF("slot %d start %llx len %llx flags %x\n",
643 memory
.guest_phys_addr
,
646 r
= ioctl(kvm
->vm_fd
, KVM_SET_USER_MEMORY_REGION
, &memory
);
648 fprintf(stderr
, "destroy_userspace_phys_mem: %s",
653 free_slot(memory
.slot
);
656 void kvm_unregister_memory_area(kvm_context_t kvm
, uint64_t phys_addr
, unsigned long size
)
659 int slot
= get_container_slot(phys_addr
, size
);
662 DPRINTF("Unregistering memory region %llx (%lx)\n", phys_addr
, size
);
663 kvm_destroy_phys_mem(kvm
, phys_addr
, size
);
668 static int kvm_get_map(kvm_context_t kvm
, int ioctl_num
, int slot
, void *buf
)
671 struct kvm_dirty_log log
= {
675 log
.dirty_bitmap
= buf
;
677 r
= ioctl(kvm
->vm_fd
, ioctl_num
, &log
);
683 int kvm_get_dirty_pages(kvm_context_t kvm
, unsigned long phys_addr
, void *buf
)
687 slot
= get_slot(phys_addr
);
688 return kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, slot
, buf
);
691 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
692 unsigned long len
, void *opaque
,
693 int (*cb
)(unsigned long start
, unsigned long len
,
694 void*bitmap
, void *opaque
))
698 unsigned long end_addr
= phys_addr
+ len
;
701 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
702 if ((slots
[i
].len
&& (uint64_t)slots
[i
].phys_addr
>= phys_addr
)
703 && ((uint64_t)slots
[i
].phys_addr
+ slots
[i
].len
<= end_addr
)) {
704 buf
= qemu_malloc((slots
[i
].len
/ 4096 + 7) / 8 + 2);
705 r
= kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, i
, buf
);
710 r
= cb(slots
[i
].phys_addr
, slots
[i
].len
, buf
, opaque
);
719 #ifdef KVM_CAP_IRQCHIP
721 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
, int *status
)
723 struct kvm_irq_level event
;
726 if (!kvm
->irqchip_in_kernel
)
730 r
= ioctl(kvm
->vm_fd
, kvm
->irqchip_inject_ioctl
, &event
);
732 perror("kvm_set_irq_level");
735 #ifdef KVM_CAP_IRQ_INJECT_STATUS
736 *status
= (kvm
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ?
746 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
750 if (!kvm
->irqchip_in_kernel
)
752 r
= ioctl(kvm
->vm_fd
, KVM_GET_IRQCHIP
, chip
);
755 perror("kvm_get_irqchip\n");
760 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
764 if (!kvm
->irqchip_in_kernel
)
766 r
= ioctl(kvm
->vm_fd
, KVM_SET_IRQCHIP
, chip
);
769 perror("kvm_set_irqchip\n");
776 static int handle_io(kvm_vcpu_context_t vcpu
)
778 struct kvm_run
*run
= vcpu
->run
;
779 kvm_context_t kvm
= vcpu
->kvm
;
780 uint16_t addr
= run
->io
.port
;
783 void *p
= (void *)run
+ run
->io
.data_offset
;
785 for (i
= 0; i
< run
->io
.count
; ++i
) {
786 switch (run
->io
.direction
) {
788 switch (run
->io
.size
) {
790 r
= kvm
->callbacks
->inb(kvm
->opaque
, addr
, p
);
793 r
= kvm
->callbacks
->inw(kvm
->opaque
, addr
, p
);
796 r
= kvm
->callbacks
->inl(kvm
->opaque
, addr
, p
);
799 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
803 case KVM_EXIT_IO_OUT
:
804 switch (run
->io
.size
) {
806 r
= kvm
->callbacks
->outb(kvm
->opaque
, addr
,
810 r
= kvm
->callbacks
->outw(kvm
->opaque
, addr
,
814 r
= kvm
->callbacks
->outl(kvm
->opaque
, addr
,
818 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
823 fprintf(stderr
, "bad I/O direction %d\n", run
->io
.direction
);
833 int handle_debug(kvm_vcpu_context_t vcpu
, void *env
)
835 #ifdef KVM_CAP_SET_GUEST_DEBUG
836 struct kvm_run
*run
= vcpu
->run
;
837 kvm_context_t kvm
= vcpu
->kvm
;
839 return kvm
->callbacks
->debug(kvm
->opaque
, env
, &run
->debug
.arch
);
845 int kvm_get_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
)
847 return ioctl(vcpu
->fd
, KVM_GET_REGS
, regs
);
850 int kvm_set_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
)
852 return ioctl(vcpu
->fd
, KVM_SET_REGS
, regs
);
855 int kvm_get_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
)
857 return ioctl(vcpu
->fd
, KVM_GET_FPU
, fpu
);
860 int kvm_set_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
)
862 return ioctl(vcpu
->fd
, KVM_SET_FPU
, fpu
);
865 int kvm_get_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*sregs
)
867 return ioctl(vcpu
->fd
, KVM_GET_SREGS
, sregs
);
870 int kvm_set_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*sregs
)
872 return ioctl(vcpu
->fd
, KVM_SET_SREGS
, sregs
);
875 #ifdef KVM_CAP_MP_STATE
876 int kvm_get_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
)
880 r
= ioctl(vcpu
->kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
882 return ioctl(vcpu
->fd
, KVM_GET_MP_STATE
, mp_state
);
886 int kvm_set_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
)
890 r
= ioctl(vcpu
->kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
892 return ioctl(vcpu
->fd
, KVM_SET_MP_STATE
, mp_state
);
897 static int handle_mmio(kvm_vcpu_context_t vcpu
)
899 unsigned long addr
= vcpu
->run
->mmio
.phys_addr
;
900 kvm_context_t kvm
= vcpu
->kvm
;
901 struct kvm_run
*kvm_run
= vcpu
->run
;
902 void *data
= kvm_run
->mmio
.data
;
904 /* hack: Red Hat 7.1 generates these weird accesses. */
905 if ((addr
> 0xa0000-4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
908 if (kvm_run
->mmio
.is_write
)
909 return kvm
->callbacks
->mmio_write(kvm
->opaque
, addr
, data
,
912 return kvm
->callbacks
->mmio_read(kvm
->opaque
, addr
, data
,
916 int handle_io_window(kvm_context_t kvm
)
918 return kvm
->callbacks
->io_window(kvm
->opaque
);
921 int handle_halt(kvm_vcpu_context_t vcpu
)
923 return vcpu
->kvm
->callbacks
->halt(vcpu
->kvm
->opaque
, vcpu
);
926 int handle_shutdown(kvm_context_t kvm
, void *env
)
928 return kvm
->callbacks
->shutdown(kvm
->opaque
, env
);
931 int try_push_interrupts(kvm_context_t kvm
)
933 return kvm
->callbacks
->try_push_interrupts(kvm
->opaque
);
936 static inline void push_nmi(kvm_context_t kvm
)
938 #ifdef KVM_CAP_USER_NMI
939 kvm
->callbacks
->push_nmi(kvm
->opaque
);
940 #endif /* KVM_CAP_USER_NMI */
943 void post_kvm_run(kvm_context_t kvm
, void *env
)
945 kvm
->callbacks
->post_kvm_run(kvm
->opaque
, env
);
948 int pre_kvm_run(kvm_context_t kvm
, void *env
)
950 return kvm
->callbacks
->pre_kvm_run(kvm
->opaque
, env
);
953 int kvm_get_interrupt_flag(kvm_vcpu_context_t vcpu
)
955 return vcpu
->run
->if_flag
;
958 int kvm_is_ready_for_interrupt_injection(kvm_vcpu_context_t vcpu
)
960 return vcpu
->run
->ready_for_interrupt_injection
;
963 int kvm_run(kvm_vcpu_context_t vcpu
, void *env
)
967 struct kvm_run
*run
= vcpu
->run
;
968 kvm_context_t kvm
= vcpu
->kvm
;
972 #if !defined(__s390__)
973 if (!kvm
->irqchip_in_kernel
)
974 run
->request_interrupt_window
= try_push_interrupts(kvm
);
976 r
= pre_kvm_run(kvm
, env
);
979 r
= ioctl(fd
, KVM_RUN
, 0);
981 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
983 post_kvm_run(kvm
, env
);
984 fprintf(stderr
, "kvm_run: %s\n", strerror(-r
));
988 post_kvm_run(kvm
, env
);
990 #if defined(KVM_CAP_COALESCED_MMIO)
991 if (kvm
->coalesced_mmio
) {
992 struct kvm_coalesced_mmio_ring
*ring
= (void *)run
+
993 kvm
->coalesced_mmio
* PAGE_SIZE
;
994 while (ring
->first
!= ring
->last
) {
995 kvm
->callbacks
->mmio_write(kvm
->opaque
,
996 ring
->coalesced_mmio
[ring
->first
].phys_addr
,
997 &ring
->coalesced_mmio
[ring
->first
].data
[0],
998 ring
->coalesced_mmio
[ring
->first
].len
);
1000 ring
->first
= (ring
->first
+ 1) %
1001 KVM_COALESCED_MMIO_MAX
;
1006 #if !defined(__s390__)
1008 r
= handle_io_window(kvm
);
1013 switch (run
->exit_reason
) {
1014 case KVM_EXIT_UNKNOWN
:
1015 r
= kvm
->callbacks
->unhandled(kvm
, vcpu
,
1016 run
->hw
.hardware_exit_reason
);
1018 case KVM_EXIT_FAIL_ENTRY
:
1019 r
= kvm
->callbacks
->unhandled(kvm
, vcpu
,
1020 run
->fail_entry
.hardware_entry_failure_reason
);
1022 case KVM_EXIT_EXCEPTION
:
1023 fprintf(stderr
, "exception %d (%x)\n",
1025 run
->ex
.error_code
);
1026 kvm_show_regs(vcpu
);
1027 kvm_show_code(vcpu
);
1031 r
= handle_io(vcpu
);
1033 case KVM_EXIT_DEBUG
:
1034 r
= handle_debug(vcpu
, env
);
1037 r
= handle_mmio(vcpu
);
1040 r
= handle_halt(vcpu
);
1042 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1044 case KVM_EXIT_SHUTDOWN
:
1045 r
= handle_shutdown(kvm
, env
);
1047 #if defined(__s390__)
1048 case KVM_EXIT_S390_SIEIC
:
1049 r
= kvm
->callbacks
->s390_handle_intercept(kvm
, vcpu
,
1052 case KVM_EXIT_S390_RESET
:
1053 r
= kvm
->callbacks
->s390_handle_reset(kvm
, vcpu
, run
);
1057 if (kvm_arch_run(vcpu
)) {
1058 fprintf(stderr
, "unhandled vm exit: 0x%x\n",
1060 kvm_show_regs(vcpu
);
1072 int kvm_inject_irq(kvm_vcpu_context_t vcpu
, unsigned irq
)
1074 struct kvm_interrupt intr
;
1077 return ioctl(vcpu
->fd
, KVM_INTERRUPT
, &intr
);
1080 #ifdef KVM_CAP_SET_GUEST_DEBUG
1081 int kvm_set_guest_debug(kvm_vcpu_context_t vcpu
, struct kvm_guest_debug
*dbg
)
1083 return ioctl(vcpu
->fd
, KVM_SET_GUEST_DEBUG
, dbg
);
1087 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu
, const sigset_t
*sigset
)
1089 struct kvm_signal_mask
*sigmask
;
1093 r
= ioctl(vcpu
->fd
, KVM_SET_SIGNAL_MASK
, NULL
);
1098 sigmask
= malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1103 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1104 r
= ioctl(vcpu
->fd
, KVM_SET_SIGNAL_MASK
, sigmask
);
1111 int kvm_irqchip_in_kernel(kvm_context_t kvm
)
1113 return kvm
->irqchip_in_kernel
;
1116 int kvm_pit_in_kernel(kvm_context_t kvm
)
1118 return kvm
->pit_in_kernel
;
1121 int kvm_has_sync_mmu(void)
1124 #ifdef KVM_CAP_SYNC_MMU
1125 r
= ioctl(kvm_context
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SYNC_MMU
);
1130 int kvm_inject_nmi(kvm_vcpu_context_t vcpu
)
1132 #ifdef KVM_CAP_USER_NMI
1133 return ioctl(vcpu
->fd
, KVM_NMI
);
1139 int kvm_init_coalesced_mmio(kvm_context_t kvm
)
1142 kvm
->coalesced_mmio
= 0;
1143 #ifdef KVM_CAP_COALESCED_MMIO
1144 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
1146 kvm
->coalesced_mmio
= r
;
1153 int kvm_register_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1155 #ifdef KVM_CAP_COALESCED_MMIO
1156 struct kvm_coalesced_mmio_zone zone
;
1159 if (kvm
->coalesced_mmio
) {
1164 r
= ioctl(kvm
->vm_fd
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
1166 perror("kvm_register_coalesced_mmio_zone");
1175 int kvm_unregister_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1177 #ifdef KVM_CAP_COALESCED_MMIO
1178 struct kvm_coalesced_mmio_zone zone
;
1181 if (kvm
->coalesced_mmio
) {
1186 r
= ioctl(kvm
->vm_fd
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
1188 perror("kvm_unregister_coalesced_mmio_zone");
1191 DPRINTF("Unregistered coalesced mmio region for %llx (%lx)\n", addr
, size
);
1198 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1199 int kvm_assign_pci_device(kvm_context_t kvm
,
1200 struct kvm_assigned_pci_dev
*assigned_dev
)
1204 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
1211 static int kvm_old_assign_irq(kvm_context_t kvm
,
1212 struct kvm_assigned_irq
*assigned_irq
)
1216 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_IRQ
, assigned_irq
);
1223 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
1224 int kvm_assign_irq(kvm_context_t kvm
,
1225 struct kvm_assigned_irq
*assigned_irq
)
1229 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_ASSIGN_DEV_IRQ
);
1231 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_DEV_IRQ
, assigned_irq
);
1237 return kvm_old_assign_irq(kvm
, assigned_irq
);
1240 int kvm_deassign_irq(kvm_context_t kvm
,
1241 struct kvm_assigned_irq
*assigned_irq
)
1245 ret
= ioctl(kvm
->vm_fd
, KVM_DEASSIGN_DEV_IRQ
, assigned_irq
);
1252 int kvm_assign_irq(kvm_context_t kvm
,
1253 struct kvm_assigned_irq
*assigned_irq
)
1255 return kvm_old_assign_irq(kvm
, assigned_irq
);
1260 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1261 int kvm_deassign_pci_device(kvm_context_t kvm
,
1262 struct kvm_assigned_pci_dev
*assigned_dev
)
1266 ret
= ioctl(kvm
->vm_fd
, KVM_DEASSIGN_PCI_DEVICE
, assigned_dev
);
1274 int kvm_destroy_memory_region_works(kvm_context_t kvm
)
1278 #ifdef KVM_CAP_DESTROY_MEMORY_REGION_WORKS
1279 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
,
1280 KVM_CAP_DESTROY_MEMORY_REGION_WORKS
);
1287 int kvm_reinject_control(kvm_context_t kvm
, int pit_reinject
)
1289 #ifdef KVM_CAP_REINJECT_CONTROL
1291 struct kvm_reinject_control control
;
1293 control
.pit_reinject
= pit_reinject
;
1295 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_REINJECT_CONTROL
);
1297 r
= ioctl(kvm
->vm_fd
, KVM_REINJECT_CONTROL
, &control
);
1306 int kvm_has_gsi_routing(kvm_context_t kvm
)
1310 #ifdef KVM_CAP_IRQ_ROUTING
1311 r
= kvm_check_extension(kvm
, KVM_CAP_IRQ_ROUTING
);
1316 int kvm_get_gsi_count(kvm_context_t kvm
)
1318 #ifdef KVM_CAP_IRQ_ROUTING
1319 return kvm_check_extension(kvm
, KVM_CAP_IRQ_ROUTING
);
1325 int kvm_clear_gsi_routes(kvm_context_t kvm
)
1327 #ifdef KVM_CAP_IRQ_ROUTING
1328 kvm
->irq_routes
->nr
= 0;
1335 int kvm_add_routing_entry(kvm_context_t kvm
,
1336 struct kvm_irq_routing_entry
* entry
)
1338 #ifdef KVM_CAP_IRQ_ROUTING
1339 struct kvm_irq_routing
*z
;
1340 struct kvm_irq_routing_entry
*new;
1343 if (kvm
->irq_routes
->nr
== kvm
->nr_allocated_irq_routes
) {
1344 n
= kvm
->nr_allocated_irq_routes
* 2;
1347 size
= sizeof(struct kvm_irq_routing
);
1348 size
+= n
* sizeof(*new);
1349 z
= realloc(kvm
->irq_routes
, size
);
1352 kvm
->nr_allocated_irq_routes
= n
;
1353 kvm
->irq_routes
= z
;
1355 n
= kvm
->irq_routes
->nr
++;
1356 new = &kvm
->irq_routes
->entries
[n
];
1357 memset(new, 0, sizeof(*new));
1358 new->gsi
= entry
->gsi
;
1359 new->type
= entry
->type
;
1360 new->flags
= entry
->flags
;
1363 set_gsi(kvm
, entry
->gsi
);
1371 int kvm_add_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1373 #ifdef KVM_CAP_IRQ_ROUTING
1374 struct kvm_irq_routing_entry e
;
1377 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1379 e
.u
.irqchip
.irqchip
= irqchip
;
1380 e
.u
.irqchip
.pin
= pin
;
1381 return kvm_add_routing_entry(kvm
, &e
);
1387 int kvm_del_routing_entry(kvm_context_t kvm
,
1388 struct kvm_irq_routing_entry
* entry
)
1390 #ifdef KVM_CAP_IRQ_ROUTING
1391 struct kvm_irq_routing_entry
*e
, *p
;
1392 int i
, gsi
, found
= 0;
1396 for (i
= 0; i
< kvm
->irq_routes
->nr
; ++i
) {
1397 e
= &kvm
->irq_routes
->entries
[i
];
1398 if (e
->type
== entry
->type
1402 case KVM_IRQ_ROUTING_IRQCHIP
: {
1403 if (e
->u
.irqchip
.irqchip
==
1404 entry
->u
.irqchip
.irqchip
1405 && e
->u
.irqchip
.pin
==
1406 entry
->u
.irqchip
.pin
) {
1407 p
= &kvm
->irq_routes
->
1408 entries
[--kvm
->irq_routes
->nr
];
1414 case KVM_IRQ_ROUTING_MSI
: {
1415 if (e
->u
.msi
.address_lo
==
1416 entry
->u
.msi
.address_lo
1417 && e
->u
.msi
.address_hi
==
1418 entry
->u
.msi
.address_hi
1419 && e
->u
.msi
.data
== entry
->u
.msi
.data
) {
1420 p
= &kvm
->irq_routes
->
1421 entries
[--kvm
->irq_routes
->nr
];
1431 /* If there are no other users of this GSI
1432 * mark it available in the bitmap */
1433 for (i
= 0; i
< kvm
->irq_routes
->nr
; i
++) {
1434 e
= &kvm
->irq_routes
->entries
[i
];
1438 if (i
== kvm
->irq_routes
->nr
)
1439 clear_gsi(kvm
, gsi
);
1451 int kvm_del_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1453 #ifdef KVM_CAP_IRQ_ROUTING
1454 struct kvm_irq_routing_entry e
;
1457 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1459 e
.u
.irqchip
.irqchip
= irqchip
;
1460 e
.u
.irqchip
.pin
= pin
;
1461 return kvm_del_routing_entry(kvm
, &e
);
1467 int kvm_commit_irq_routes(kvm_context_t kvm
)
1469 #ifdef KVM_CAP_IRQ_ROUTING
1472 kvm
->irq_routes
->flags
= 0;
1473 r
= ioctl(kvm
->vm_fd
, KVM_SET_GSI_ROUTING
, kvm
->irq_routes
);
1482 int kvm_get_irq_route_gsi(kvm_context_t kvm
)
1485 uint32_t *buf
= kvm
->used_gsi_bitmap
;
1487 /* Return the lowest unused GSI in the bitmap */
1488 for (i
= 0; i
< kvm
->max_gsi
/ 32; i
++) {
1493 return bit
- 1 + i
* 32;
1499 #ifdef KVM_CAP_DEVICE_MSIX
1500 int kvm_assign_set_msix_nr(kvm_context_t kvm
,
1501 struct kvm_assigned_msix_nr
*msix_nr
)
1505 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_SET_MSIX_NR
, msix_nr
);
1512 int kvm_assign_set_msix_entry(kvm_context_t kvm
,
1513 struct kvm_assigned_msix_entry
*entry
)
1517 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_SET_MSIX_ENTRY
, entry
);
1525 #if defined(KVM_CAP_IRQFD) && defined(CONFIG_eventfd)
1527 #include <sys/eventfd.h>
1529 static int _kvm_irqfd(kvm_context_t kvm
, int fd
, int gsi
, int flags
)
1532 struct kvm_irqfd data
= {
1538 r
= ioctl(kvm
->vm_fd
, KVM_IRQFD
, &data
);
1544 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1549 if (!kvm_check_extension(kvm
, KVM_CAP_IRQFD
))
1556 r
= _kvm_irqfd(kvm
, fd
, gsi
, 0);
1565 #else /* KVM_CAP_IRQFD */
1567 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1572 #endif /* KVM_CAP_IRQFD */
1573 static inline unsigned long kvm_get_thread_id(void)
1575 return syscall(SYS_gettid
);
1578 static void qemu_cond_wait(pthread_cond_t
*cond
)
1580 CPUState
*env
= cpu_single_env
;
1581 static const struct timespec ts
= {
1586 pthread_cond_timedwait(cond
, &qemu_mutex
, &ts
);
1587 cpu_single_env
= env
;
1590 static void sig_ipi_handler(int n
)
1594 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
1596 struct qemu_work_item wi
;
1598 if (env
== current_env
) {
1605 if (!env
->kvm_cpu_state
.queued_work_first
)
1606 env
->kvm_cpu_state
.queued_work_first
= &wi
;
1608 env
->kvm_cpu_state
.queued_work_last
->next
= &wi
;
1609 env
->kvm_cpu_state
.queued_work_last
= &wi
;
1613 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1615 qemu_cond_wait(&qemu_work_cond
);
1618 static void inject_interrupt(void *data
)
1620 cpu_interrupt(current_env
, (long)data
);
1623 void kvm_inject_interrupt(CPUState
*env
, int mask
)
1625 on_vcpu(env
, inject_interrupt
, (void *)(long)mask
);
1628 void kvm_update_interrupt_request(CPUState
*env
)
1633 if (!current_env
|| !current_env
->kvm_cpu_state
.created
)
1636 * Testing for created here is really redundant
1638 if (current_env
&& current_env
->kvm_cpu_state
.created
&&
1639 env
!= current_env
&& !env
->kvm_cpu_state
.signalled
)
1643 env
->kvm_cpu_state
.signalled
= 1;
1644 if (env
->kvm_cpu_state
.thread
)
1645 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1652 static int kvm_try_push_interrupts(void *opaque
)
1654 return kvm_arch_try_push_interrupts(opaque
);
1657 static void kvm_post_run(void *opaque
, void *data
)
1659 CPUState
*env
= (CPUState
*)data
;
1661 pthread_mutex_lock(&qemu_mutex
);
1662 kvm_arch_post_kvm_run(opaque
, env
);
1665 static int kvm_pre_run(void *opaque
, void *data
)
1667 CPUState
*env
= (CPUState
*)data
;
1669 kvm_arch_pre_kvm_run(opaque
, env
);
1671 pthread_mutex_unlock(&qemu_mutex
);
1675 static void kvm_do_load_registers(void *_env
)
1677 CPUState
*env
= _env
;
1679 kvm_arch_load_regs(env
);
1682 void kvm_load_registers(CPUState
*env
)
1684 if (kvm_enabled() && qemu_system_ready
)
1685 on_vcpu(env
, kvm_do_load_registers
, env
);
1688 static void kvm_do_save_registers(void *_env
)
1690 CPUState
*env
= _env
;
1692 kvm_arch_save_regs(env
);
1695 void kvm_save_registers(CPUState
*env
)
1698 on_vcpu(env
, kvm_do_save_registers
, env
);
1701 static void kvm_do_load_mpstate(void *_env
)
1703 CPUState
*env
= _env
;
1705 kvm_arch_load_mpstate(env
);
1708 void kvm_load_mpstate(CPUState
*env
)
1710 if (kvm_enabled() && qemu_system_ready
)
1711 on_vcpu(env
, kvm_do_load_mpstate
, env
);
1714 static void kvm_do_save_mpstate(void *_env
)
1716 CPUState
*env
= _env
;
1718 kvm_arch_save_mpstate(env
);
1719 env
->halted
= (env
->mp_state
== KVM_MP_STATE_HALTED
);
1722 void kvm_save_mpstate(CPUState
*env
)
1725 on_vcpu(env
, kvm_do_save_mpstate
, env
);
1728 int kvm_cpu_exec(CPUState
*env
)
1732 r
= kvm_run(env
->kvm_cpu_state
.vcpu_ctx
, env
);
1734 printf("kvm_run returned %d\n", r
);
1741 static int is_cpu_stopped(CPUState
*env
)
1743 return !vm_running
|| env
->kvm_cpu_state
.stopped
;
1746 static void flush_queued_work(CPUState
*env
)
1748 struct qemu_work_item
*wi
;
1750 if (!env
->kvm_cpu_state
.queued_work_first
)
1753 while ((wi
= env
->kvm_cpu_state
.queued_work_first
)) {
1754 env
->kvm_cpu_state
.queued_work_first
= wi
->next
;
1758 env
->kvm_cpu_state
.queued_work_last
= NULL
;
1759 pthread_cond_broadcast(&qemu_work_cond
);
1762 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
1769 pthread_mutex_unlock(&qemu_mutex
);
1771 ts
.tv_sec
= timeout
/ 1000;
1772 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
1773 sigemptyset(&waitset
);
1774 sigaddset(&waitset
, SIG_IPI
);
1776 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
1779 pthread_mutex_lock(&qemu_mutex
);
1781 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
1782 printf("sigtimedwait: %s\n", strerror(e
));
1786 cpu_single_env
= env
;
1787 flush_queued_work(env
);
1789 if (env
->kvm_cpu_state
.stop
) {
1790 env
->kvm_cpu_state
.stop
= 0;
1791 env
->kvm_cpu_state
.stopped
= 1;
1792 pthread_cond_signal(&qemu_pause_cond
);
1795 env
->kvm_cpu_state
.signalled
= 0;
1798 static int all_threads_paused(void)
1800 CPUState
*penv
= first_cpu
;
1803 if (penv
->kvm_cpu_state
.stop
)
1805 penv
= (CPUState
*)penv
->next_cpu
;
1811 static void pause_all_threads(void)
1813 CPUState
*penv
= first_cpu
;
1816 if (penv
!= cpu_single_env
) {
1817 penv
->kvm_cpu_state
.stop
= 1;
1818 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1820 penv
->kvm_cpu_state
.stop
= 0;
1821 penv
->kvm_cpu_state
.stopped
= 1;
1824 penv
= (CPUState
*)penv
->next_cpu
;
1827 while (!all_threads_paused())
1828 qemu_cond_wait(&qemu_pause_cond
);
1831 static void resume_all_threads(void)
1833 CPUState
*penv
= first_cpu
;
1835 assert(!cpu_single_env
);
1838 penv
->kvm_cpu_state
.stop
= 0;
1839 penv
->kvm_cpu_state
.stopped
= 0;
1840 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1841 penv
= (CPUState
*)penv
->next_cpu
;
1845 static void kvm_vm_state_change_handler(void *context
, int running
, int reason
)
1848 resume_all_threads();
1850 pause_all_threads();
1853 static void setup_kernel_sigmask(CPUState
*env
)
1858 sigaddset(&set
, SIGUSR2
);
1859 sigaddset(&set
, SIGIO
);
1860 sigaddset(&set
, SIGALRM
);
1861 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1863 sigprocmask(SIG_BLOCK
, NULL
, &set
);
1864 sigdelset(&set
, SIG_IPI
);
1866 kvm_set_signal_mask(env
->kvm_cpu_state
.vcpu_ctx
, &set
);
1869 static void qemu_kvm_system_reset(void)
1871 CPUState
*penv
= first_cpu
;
1873 pause_all_threads();
1875 qemu_system_reset();
1878 kvm_arch_cpu_reset(penv
);
1879 penv
= (CPUState
*)penv
->next_cpu
;
1882 resume_all_threads();
1885 static void process_irqchip_events(CPUState
*env
)
1887 kvm_arch_process_irqchip_events(env
);
1888 if (kvm_arch_has_work(env
))
1892 static int kvm_main_loop_cpu(CPUState
*env
)
1894 setup_kernel_sigmask(env
);
1896 pthread_mutex_lock(&qemu_mutex
);
1898 kvm_qemu_init_env(env
);
1900 kvm_tpr_vcpu_start(env
);
1903 cpu_single_env
= env
;
1904 kvm_arch_load_regs(env
);
1907 int run_cpu
= !is_cpu_stopped(env
);
1908 if (run_cpu
&& !kvm_irqchip_in_kernel(kvm_context
)) {
1909 process_irqchip_events(env
);
1910 run_cpu
= !env
->halted
;
1913 kvm_main_loop_wait(env
, 0);
1916 kvm_main_loop_wait(env
, 1000);
1919 pthread_mutex_unlock(&qemu_mutex
);
1923 static void *ap_main_loop(void *_env
)
1925 CPUState
*env
= _env
;
1927 struct ioperm_data
*data
= NULL
;
1930 env
->thread_id
= kvm_get_thread_id();
1931 sigfillset(&signals
);
1932 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
1933 env
->kvm_cpu_state
.vcpu_ctx
= kvm_create_vcpu(kvm_context
, env
->cpu_index
);
1935 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1936 /* do ioperm for io ports of assigned devices */
1937 LIST_FOREACH(data
, &ioperm_head
, entries
)
1938 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
1941 /* signal VCPU creation */
1942 pthread_mutex_lock(&qemu_mutex
);
1943 current_env
->kvm_cpu_state
.created
= 1;
1944 pthread_cond_signal(&qemu_vcpu_cond
);
1946 /* and wait for machine initialization */
1947 while (!qemu_system_ready
)
1948 qemu_cond_wait(&qemu_system_cond
);
1949 pthread_mutex_unlock(&qemu_mutex
);
1951 kvm_main_loop_cpu(env
);
1955 void kvm_init_vcpu(CPUState
*env
)
1957 pthread_create(&env
->kvm_cpu_state
.thread
, NULL
, ap_main_loop
, env
);
1959 while (env
->kvm_cpu_state
.created
== 0)
1960 qemu_cond_wait(&qemu_vcpu_cond
);
1963 int kvm_vcpu_inited(CPUState
*env
)
1965 return env
->kvm_cpu_state
.created
;
1968 int kvm_init_ap(void)
1971 kvm_tpr_opt_setup();
1973 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
1975 signal(SIG_IPI
, sig_ipi_handler
);
1979 void qemu_kvm_notify_work(void)
1985 if (io_thread_fd
== -1)
1988 memcpy(buffer
, &value
, sizeof(value
));
1990 while (offset
< 8) {
1993 len
= write(io_thread_fd
, buffer
+ offset
, 8 - offset
);
1994 if (len
== -1 && errno
== EINTR
)
2004 fprintf(stderr
, "failed to notify io thread\n");
2007 /* If we have signalfd, we mask out the signals we want to handle and then
2008 * use signalfd to listen for them. We rely on whatever the current signal
2009 * handler is to dispatch the signals when we receive them.
2012 static void sigfd_handler(void *opaque
)
2014 int fd
= (unsigned long)opaque
;
2015 struct qemu_signalfd_siginfo info
;
2016 struct sigaction action
;
2021 len
= read(fd
, &info
, sizeof(info
));
2022 } while (len
== -1 && errno
== EINTR
);
2024 if (len
== -1 && errno
== EAGAIN
)
2027 if (len
!= sizeof(info
)) {
2028 printf("read from sigfd returned %zd: %m\n", len
);
2032 sigaction(info
.ssi_signo
, NULL
, &action
);
2033 if (action
.sa_handler
)
2034 action
.sa_handler(info
.ssi_signo
);
2039 /* Used to break IO thread out of select */
2040 static void io_thread_wakeup(void *opaque
)
2042 int fd
= (unsigned long)opaque
;
2046 while (offset
< 8) {
2049 len
= read(fd
, buffer
+ offset
, 8 - offset
);
2050 if (len
== -1 && errno
== EINTR
)
2060 int kvm_main_loop(void)
2066 io_thread
= pthread_self();
2067 qemu_system_ready
= 1;
2069 if (qemu_eventfd(fds
) == -1) {
2070 fprintf(stderr
, "failed to create eventfd\n");
2074 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
2075 (void *)(unsigned long)fds
[0]);
2077 io_thread_fd
= fds
[1];
2080 sigaddset(&mask
, SIGIO
);
2081 sigaddset(&mask
, SIGALRM
);
2082 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
2084 sigfd
= qemu_signalfd(&mask
);
2086 fprintf(stderr
, "failed to create signalfd\n");
2090 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
2092 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
2093 (void *)(unsigned long)sigfd
);
2095 pthread_cond_broadcast(&qemu_system_cond
);
2097 io_thread_sigfd
= sigfd
;
2098 cpu_single_env
= NULL
;
2101 main_loop_wait(1000);
2102 if (qemu_shutdown_requested()) {
2103 if (qemu_no_shutdown()) {
2107 } else if (qemu_powerdown_requested())
2108 qemu_system_powerdown();
2109 else if (qemu_reset_requested())
2110 qemu_kvm_system_reset();
2111 else if (kvm_debug_cpu_requested
) {
2112 gdb_set_stop_cpu(kvm_debug_cpu_requested
);
2113 vm_stop(EXCP_DEBUG
);
2114 kvm_debug_cpu_requested
= NULL
;
2118 pause_all_threads();
2119 pthread_mutex_unlock(&qemu_mutex
);
2124 #ifdef KVM_CAP_SET_GUEST_DEBUG
2125 static int kvm_debug(void *opaque
, void *data
,
2126 struct kvm_debug_exit_arch
*arch_info
)
2128 int handle
= kvm_arch_debug(arch_info
);
2129 CPUState
*env
= data
;
2132 kvm_debug_cpu_requested
= env
;
2133 env
->kvm_cpu_state
.stopped
= 1;
2139 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
2141 *data
= cpu_inb(0, addr
);
2145 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
2147 *data
= cpu_inw(0, addr
);
2151 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
2153 *data
= cpu_inl(0, addr
);
2157 #define PM_IO_BASE 0xb000
2159 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
2164 cpu_outb(0, 0xb3, 0);
2171 x
= cpu_inw(0, PM_IO_BASE
+ 4);
2173 cpu_outw(0, PM_IO_BASE
+ 4, x
);
2180 x
= cpu_inw(0, PM_IO_BASE
+ 4);
2182 cpu_outw(0, PM_IO_BASE
+ 4, x
);
2190 cpu_outb(0, addr
, data
);
2194 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
2196 cpu_outw(0, addr
, data
);
2200 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
2202 cpu_outl(0, addr
, data
);
2206 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
2208 cpu_physical_memory_rw(addr
, data
, len
, 0);
2212 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
2214 cpu_physical_memory_rw(addr
, data
, len
, 1);
2218 static int kvm_io_window(void *opaque
)
2224 static int kvm_halt(void *opaque
, kvm_vcpu_context_t vcpu
)
2226 return kvm_arch_halt(opaque
, vcpu
);
2229 static int kvm_shutdown(void *opaque
, void *data
)
2231 CPUState
*env
= (CPUState
*)data
;
2233 /* stop the current vcpu from going back to guest mode */
2234 env
->kvm_cpu_state
.stopped
= 1;
2236 qemu_system_reset_request();
2240 static int handle_unhandled(kvm_context_t kvm
, kvm_vcpu_context_t vcpu
,
2243 fprintf(stderr
, "kvm: unhandled exit %"PRIx64
"\n", reason
);
2247 static struct kvm_callbacks qemu_kvm_ops
= {
2248 #ifdef KVM_CAP_SET_GUEST_DEBUG
2257 .mmio_read
= kvm_mmio_read
,
2258 .mmio_write
= kvm_mmio_write
,
2260 .shutdown
= kvm_shutdown
,
2261 .io_window
= kvm_io_window
,
2262 .try_push_interrupts
= kvm_try_push_interrupts
,
2263 #ifdef KVM_CAP_USER_NMI
2264 .push_nmi
= kvm_arch_push_nmi
,
2266 .post_kvm_run
= kvm_post_run
,
2267 .pre_kvm_run
= kvm_pre_run
,
2269 .tpr_access
= handle_tpr_access
,
2271 .unhandled
= handle_unhandled
,
2276 /* Try to initialize kvm */
2277 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
2281 pthread_mutex_lock(&qemu_mutex
);
2287 static int destroy_region_works
= 0;
2291 #if !defined(TARGET_I386)
2292 int kvm_arch_init_irq_routing(void)
2298 int kvm_qemu_create_context(void)
2303 kvm_disable_irqchip_creation(kvm_context
);
2306 kvm_disable_pit_creation(kvm_context
);
2308 if (kvm_create(kvm_context
, 0, NULL
) < 0) {
2312 r
= kvm_arch_qemu_create_context();
2315 if (kvm_pit
&& !kvm_pit_reinject
) {
2316 if (kvm_reinject_control(kvm_context
, 0)) {
2317 fprintf(stderr
, "failure to disable in-kernel PIT reinjection\n");
2322 destroy_region_works
= kvm_destroy_memory_region_works(kvm_context
);
2325 r
= kvm_arch_init_irq_routing();
2333 void kvm_qemu_destroy(void)
2335 kvm_finalize(kvm_context
);
2339 static int must_use_aliases_source(target_phys_addr_t addr
)
2341 if (destroy_region_works
)
2343 if (addr
== 0xa0000 || addr
== 0xa8000)
2348 static int must_use_aliases_target(target_phys_addr_t addr
)
2350 if (destroy_region_works
)
2352 if (addr
>= 0xe0000000 && addr
< 0x100000000ull
)
2357 static struct mapping
{
2358 target_phys_addr_t phys
;
2362 static int nr_mappings
;
2364 static struct mapping
*find_ram_mapping(ram_addr_t ram_addr
)
2368 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
2369 if (p
->ram
<= ram_addr
&& ram_addr
< p
->ram
+ p
->len
) {
2376 static struct mapping
*find_mapping(target_phys_addr_t start_addr
)
2380 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
2381 if (p
->phys
<= start_addr
&& start_addr
< p
->phys
+ p
->len
) {
2388 static void drop_mapping(target_phys_addr_t start_addr
)
2390 struct mapping
*p
= find_mapping(start_addr
);
2393 *p
= mappings
[--nr_mappings
];
2397 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
2399 unsigned long phys_offset
)
2402 unsigned long area_flags
;
2407 if (start_addr
+ size
> phys_ram_size
) {
2408 phys_ram_size
= start_addr
+ size
;
2411 phys_offset
&= ~IO_MEM_ROM
;
2412 area_flags
= phys_offset
& ~TARGET_PAGE_MASK
;
2414 if (area_flags
!= IO_MEM_RAM
) {
2416 if (must_use_aliases_source(start_addr
)) {
2417 kvm_destroy_memory_alias(kvm_context
, start_addr
);
2420 if (must_use_aliases_target(start_addr
))
2424 p
= find_mapping(start_addr
);
2426 kvm_unregister_memory_area(kvm_context
, p
->phys
, p
->len
);
2427 drop_mapping(p
->phys
);
2429 start_addr
+= TARGET_PAGE_SIZE
;
2430 if (size
> TARGET_PAGE_SIZE
) {
2431 size
-= TARGET_PAGE_SIZE
;
2439 r
= kvm_is_containing_region(kvm_context
, start_addr
, size
);
2443 if (area_flags
>= TLB_MMIO
)
2447 if (must_use_aliases_source(start_addr
)) {
2448 p
= find_ram_mapping(phys_offset
);
2450 kvm_create_memory_alias(kvm_context
, start_addr
, size
,
2451 p
->phys
+ (phys_offset
- p
->ram
));
2457 r
= kvm_register_phys_mem(kvm_context
, start_addr
,
2458 qemu_get_ram_ptr(phys_offset
),
2461 printf("kvm_cpu_register_physical_memory: failed\n");
2466 drop_mapping(start_addr
);
2467 p
= &mappings
[nr_mappings
++];
2468 p
->phys
= start_addr
;
2469 p
->ram
= phys_offset
;
2476 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr
,
2477 target_phys_addr_t size
,
2478 unsigned long phys_offset
)
2480 kvm_unregister_memory_area(kvm_context
, start_addr
, size
);
2483 int kvm_setup_guest_memory(void *area
, unsigned long size
)
2487 #ifdef MADV_DONTFORK
2488 if (kvm_enabled() && !kvm_has_sync_mmu())
2489 ret
= madvise(area
, size
, MADV_DONTFORK
);
2498 int kvm_qemu_check_extension(int ext
)
2500 return kvm_check_extension(kvm_context
, ext
);
2503 int kvm_qemu_init_env(CPUState
*cenv
)
2505 return kvm_arch_qemu_init_env(cenv
);
2508 #ifdef KVM_CAP_SET_GUEST_DEBUG
2509 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
=
2510 TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints
);
2512 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(target_ulong pc
)
2514 struct kvm_sw_breakpoint
*bp
;
2516 TAILQ_FOREACH(bp
, &kvm_sw_breakpoints
, entry
) {
2523 struct kvm_set_guest_debug_data
{
2524 struct kvm_guest_debug dbg
;
2528 static void kvm_invoke_set_guest_debug(void *data
)
2530 struct kvm_set_guest_debug_data
*dbg_data
= data
;
2532 dbg_data
->err
= kvm_set_guest_debug(cpu_single_env
->kvm_cpu_state
.vcpu_ctx
,
2536 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
2538 struct kvm_set_guest_debug_data data
;
2540 data
.dbg
.control
= 0;
2541 if (env
->singlestep_enabled
)
2542 data
.dbg
.control
= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
2544 kvm_arch_update_guest_debug(env
, &data
.dbg
);
2545 data
.dbg
.control
|= reinject_trap
;
2547 on_vcpu(env
, kvm_invoke_set_guest_debug
, &data
);
2551 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
2552 target_ulong len
, int type
)
2554 struct kvm_sw_breakpoint
*bp
;
2558 if (type
== GDB_BREAKPOINT_SW
) {
2559 bp
= kvm_find_sw_breakpoint(addr
);
2565 bp
= qemu_malloc(sizeof(struct kvm_sw_breakpoint
));
2571 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
2577 TAILQ_INSERT_HEAD(&kvm_sw_breakpoints
, bp
, entry
);
2579 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
2584 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2585 err
= kvm_update_guest_debug(env
, 0);
2592 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
2593 target_ulong len
, int type
)
2595 struct kvm_sw_breakpoint
*bp
;
2599 if (type
== GDB_BREAKPOINT_SW
) {
2600 bp
= kvm_find_sw_breakpoint(addr
);
2604 if (bp
->use_count
> 1) {
2609 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
2613 TAILQ_REMOVE(&kvm_sw_breakpoints
, bp
, entry
);
2616 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
2621 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2622 err
= kvm_update_guest_debug(env
, 0);
2629 void kvm_remove_all_breakpoints(CPUState
*current_env
)
2631 struct kvm_sw_breakpoint
*bp
, *next
;
2634 TAILQ_FOREACH_SAFE(bp
, &kvm_sw_breakpoints
, entry
, next
) {
2635 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
2636 /* Try harder to find a CPU that currently sees the breakpoint. */
2637 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2638 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0)
2643 kvm_arch_remove_all_hw_breakpoints();
2645 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2646 kvm_update_guest_debug(env
, 0);
2649 #else /* !KVM_CAP_SET_GUEST_DEBUG */
2651 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
2656 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
2657 target_ulong len
, int type
)
2662 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
2663 target_ulong len
, int type
)
2668 void kvm_remove_all_breakpoints(CPUState
*current_env
)
2671 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
2674 * dirty pages logging
2676 /* FIXME: use unsigned long pointer instead of unsigned char */
2677 unsigned char *kvm_dirty_bitmap
= NULL
;
2678 int kvm_physical_memory_set_dirty_tracking(int enable
)
2686 if (!kvm_dirty_bitmap
) {
2687 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
2688 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
2689 if (kvm_dirty_bitmap
== NULL
) {
2690 perror("Failed to allocate dirty pages bitmap");
2694 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
2699 if (kvm_dirty_bitmap
) {
2700 r
= kvm_dirty_pages_log_reset(kvm_context
);
2701 qemu_free(kvm_dirty_bitmap
);
2702 kvm_dirty_bitmap
= NULL
;
2708 /* get kvm's dirty pages bitmap and update qemu's */
2709 static int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
2710 unsigned char *bitmap
,
2711 unsigned long offset
,
2712 unsigned long mem_size
)
2714 unsigned int i
, j
, n
=0;
2716 unsigned long page_number
, addr
, addr1
;
2717 ram_addr_t ram_addr
;
2718 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
2721 * bitmap-traveling is faster than memory-traveling (for addr...)
2722 * especially when most of the memory is not dirty.
2724 for (i
=0; i
<len
; i
++) {
2729 page_number
= i
* 8 + j
;
2730 addr1
= page_number
* TARGET_PAGE_SIZE
;
2731 addr
= offset
+ addr1
;
2732 ram_addr
= cpu_get_physical_page_desc(addr
);
2733 cpu_physical_memory_set_dirty(ram_addr
);
2739 static int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
2740 void *bitmap
, void *opaque
)
2742 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
2746 * get kvm's dirty pages bitmap and update qemu's
2747 * we only care about physical ram, which resides in slots 0 and 3
2749 int kvm_update_dirty_pages_log(void)
2754 r
= kvm_get_dirty_pages_range(kvm_context
, 0, -1UL,
2756 kvm_get_dirty_bitmap_cb
);
2760 void kvm_qemu_log_memory(target_phys_addr_t start
, target_phys_addr_t size
,
2764 kvm_dirty_pages_log_enable_slot(kvm_context
, start
, size
);
2767 if (must_use_aliases_target(start
))
2770 kvm_dirty_pages_log_disable_slot(kvm_context
, start
, size
);
2774 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
2776 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
2777 unsigned int brsize
= BITMAP_SIZE(ram_size
);
2778 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
2779 unsigned int extra_bytes
= (extra_pages
+7)/8;
2780 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
2781 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
2783 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
2784 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
2785 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
2790 #ifdef KVM_CAP_IRQCHIP
2792 int kvm_set_irq(int irq
, int level
, int *status
)
2794 return kvm_set_irq_level(kvm_context
, irq
, level
, status
);
2799 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
2801 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
2804 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
2805 unsigned long size
, int log
, int writable
)
2807 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
2810 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
2813 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);
2816 void kvm_mutex_unlock(void)
2818 assert(!cpu_single_env
);
2819 pthread_mutex_unlock(&qemu_mutex
);
2822 void kvm_mutex_lock(void)
2824 pthread_mutex_lock(&qemu_mutex
);
2825 cpu_single_env
= NULL
;
2828 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr
, unsigned int size
)
2830 return kvm_register_coalesced_mmio(kvm_context
, addr
, size
);
2833 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr
,
2836 return kvm_unregister_coalesced_mmio(kvm_context
, addr
, size
);
2839 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
2841 return kvm_register_coalesced_mmio(kvm_context
, start
, size
);
2844 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
2846 return kvm_unregister_coalesced_mmio(kvm_context
, start
, size
);
2849 #ifdef USE_KVM_DEVICE_ASSIGNMENT
2850 void kvm_add_ioperm_data(struct ioperm_data
*data
)
2852 LIST_INSERT_HEAD(&ioperm_head
, data
, entries
);
2855 void kvm_remove_ioperm_data(unsigned long start_port
, unsigned long num
)
2857 struct ioperm_data
*data
;
2859 data
= LIST_FIRST(&ioperm_head
);
2861 struct ioperm_data
*next
= LIST_NEXT(data
, entries
);
2863 if (data
->start_port
== start_port
&& data
->num
== num
) {
2864 LIST_REMOVE(data
, entries
);
2872 void kvm_ioperm(CPUState
*env
, void *data
)
2874 if (kvm_enabled() && qemu_system_ready
)
2875 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
2880 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
, target_phys_addr_t end_addr
)
2885 if (must_use_aliases_source(start_addr
))
2889 kvm_get_dirty_pages_range(kvm_context
, start_addr
, end_addr
- start_addr
,
2890 NULL
, kvm_get_dirty_bitmap_cb
);
2895 int kvm_log_start(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
2898 if (must_use_aliases_source(phys_addr
))
2903 kvm_qemu_log_memory(phys_addr
, len
, 1);
2908 int kvm_log_stop(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
2911 if (must_use_aliases_source(phys_addr
))
2916 kvm_qemu_log_memory(phys_addr
, len
, 0);
2921 void qemu_kvm_cpu_stop(CPUState
*env
)
2924 env
->kvm_cpu_state
.stopped
= 1;