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 *buf
, void *opaque
,
693 int (*cb
)(unsigned long start
, unsigned long len
,
694 void*bitmap
, void *opaque
))
698 unsigned long end_addr
= phys_addr
+ len
;
700 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
701 if ((slots
[i
].len
&& (uint64_t)slots
[i
].phys_addr
>= phys_addr
)
702 && ((uint64_t)slots
[i
].phys_addr
+ slots
[i
].len
<= end_addr
)) {
703 r
= kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, i
, buf
);
706 r
= cb(slots
[i
].phys_addr
, slots
[i
].len
, buf
, opaque
);
714 #ifdef KVM_CAP_IRQCHIP
716 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
, int *status
)
718 struct kvm_irq_level event
;
721 if (!kvm
->irqchip_in_kernel
)
725 r
= ioctl(kvm
->vm_fd
, kvm
->irqchip_inject_ioctl
, &event
);
727 perror("kvm_set_irq_level");
730 #ifdef KVM_CAP_IRQ_INJECT_STATUS
731 *status
= (kvm
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ?
741 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
745 if (!kvm
->irqchip_in_kernel
)
747 r
= ioctl(kvm
->vm_fd
, KVM_GET_IRQCHIP
, chip
);
750 perror("kvm_get_irqchip\n");
755 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
759 if (!kvm
->irqchip_in_kernel
)
761 r
= ioctl(kvm
->vm_fd
, KVM_SET_IRQCHIP
, chip
);
764 perror("kvm_set_irqchip\n");
771 static int handle_io(kvm_vcpu_context_t vcpu
)
773 struct kvm_run
*run
= vcpu
->run
;
774 kvm_context_t kvm
= vcpu
->kvm
;
775 uint16_t addr
= run
->io
.port
;
778 void *p
= (void *)run
+ run
->io
.data_offset
;
780 for (i
= 0; i
< run
->io
.count
; ++i
) {
781 switch (run
->io
.direction
) {
783 switch (run
->io
.size
) {
785 r
= kvm
->callbacks
->inb(kvm
->opaque
, addr
, p
);
788 r
= kvm
->callbacks
->inw(kvm
->opaque
, addr
, p
);
791 r
= kvm
->callbacks
->inl(kvm
->opaque
, addr
, p
);
794 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
798 case KVM_EXIT_IO_OUT
:
799 switch (run
->io
.size
) {
801 r
= kvm
->callbacks
->outb(kvm
->opaque
, addr
,
805 r
= kvm
->callbacks
->outw(kvm
->opaque
, addr
,
809 r
= kvm
->callbacks
->outl(kvm
->opaque
, addr
,
813 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
818 fprintf(stderr
, "bad I/O direction %d\n", run
->io
.direction
);
828 int handle_debug(kvm_vcpu_context_t vcpu
, void *env
)
830 #ifdef KVM_CAP_SET_GUEST_DEBUG
831 struct kvm_run
*run
= vcpu
->run
;
832 kvm_context_t kvm
= vcpu
->kvm
;
834 return kvm
->callbacks
->debug(kvm
->opaque
, env
, &run
->debug
.arch
);
840 int kvm_get_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
)
842 return ioctl(vcpu
->fd
, KVM_GET_REGS
, regs
);
845 int kvm_set_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
)
847 return ioctl(vcpu
->fd
, KVM_SET_REGS
, regs
);
850 int kvm_get_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
)
852 return ioctl(vcpu
->fd
, KVM_GET_FPU
, fpu
);
855 int kvm_set_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
)
857 return ioctl(vcpu
->fd
, KVM_SET_FPU
, fpu
);
860 int kvm_get_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*sregs
)
862 return ioctl(vcpu
->fd
, KVM_GET_SREGS
, sregs
);
865 int kvm_set_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*sregs
)
867 return ioctl(vcpu
->fd
, KVM_SET_SREGS
, sregs
);
870 #ifdef KVM_CAP_MP_STATE
871 int kvm_get_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
)
875 r
= ioctl(vcpu
->kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
877 return ioctl(vcpu
->fd
, KVM_GET_MP_STATE
, mp_state
);
881 int kvm_set_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
)
885 r
= ioctl(vcpu
->kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
887 return ioctl(vcpu
->fd
, KVM_SET_MP_STATE
, mp_state
);
892 static int handle_mmio(kvm_vcpu_context_t vcpu
)
894 unsigned long addr
= vcpu
->run
->mmio
.phys_addr
;
895 kvm_context_t kvm
= vcpu
->kvm
;
896 struct kvm_run
*kvm_run
= vcpu
->run
;
897 void *data
= kvm_run
->mmio
.data
;
899 /* hack: Red Hat 7.1 generates these weird accesses. */
900 if ((addr
> 0xa0000-4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
903 if (kvm_run
->mmio
.is_write
)
904 return kvm
->callbacks
->mmio_write(kvm
->opaque
, addr
, data
,
907 return kvm
->callbacks
->mmio_read(kvm
->opaque
, addr
, data
,
911 int handle_io_window(kvm_context_t kvm
)
913 return kvm
->callbacks
->io_window(kvm
->opaque
);
916 int handle_halt(kvm_vcpu_context_t vcpu
)
918 return vcpu
->kvm
->callbacks
->halt(vcpu
->kvm
->opaque
, vcpu
);
921 int handle_shutdown(kvm_context_t kvm
, void *env
)
923 return kvm
->callbacks
->shutdown(kvm
->opaque
, env
);
926 int try_push_interrupts(kvm_context_t kvm
)
928 return kvm
->callbacks
->try_push_interrupts(kvm
->opaque
);
931 static inline void push_nmi(kvm_context_t kvm
)
933 #ifdef KVM_CAP_USER_NMI
934 kvm
->callbacks
->push_nmi(kvm
->opaque
);
935 #endif /* KVM_CAP_USER_NMI */
938 void post_kvm_run(kvm_context_t kvm
, void *env
)
940 kvm
->callbacks
->post_kvm_run(kvm
->opaque
, env
);
943 int pre_kvm_run(kvm_context_t kvm
, void *env
)
945 return kvm
->callbacks
->pre_kvm_run(kvm
->opaque
, env
);
948 int kvm_get_interrupt_flag(kvm_vcpu_context_t vcpu
)
950 return vcpu
->run
->if_flag
;
953 int kvm_is_ready_for_interrupt_injection(kvm_vcpu_context_t vcpu
)
955 return vcpu
->run
->ready_for_interrupt_injection
;
958 int kvm_run(kvm_vcpu_context_t vcpu
, void *env
)
962 struct kvm_run
*run
= vcpu
->run
;
963 kvm_context_t kvm
= vcpu
->kvm
;
967 #if !defined(__s390__)
968 if (!kvm
->irqchip_in_kernel
)
969 run
->request_interrupt_window
= try_push_interrupts(kvm
);
971 r
= pre_kvm_run(kvm
, env
);
974 r
= ioctl(fd
, KVM_RUN
, 0);
976 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
978 post_kvm_run(kvm
, env
);
979 fprintf(stderr
, "kvm_run: %s\n", strerror(-r
));
983 post_kvm_run(kvm
, env
);
985 #if defined(KVM_CAP_COALESCED_MMIO)
986 if (kvm
->coalesced_mmio
) {
987 struct kvm_coalesced_mmio_ring
*ring
= (void *)run
+
988 kvm
->coalesced_mmio
* PAGE_SIZE
;
989 while (ring
->first
!= ring
->last
) {
990 kvm
->callbacks
->mmio_write(kvm
->opaque
,
991 ring
->coalesced_mmio
[ring
->first
].phys_addr
,
992 &ring
->coalesced_mmio
[ring
->first
].data
[0],
993 ring
->coalesced_mmio
[ring
->first
].len
);
995 ring
->first
= (ring
->first
+ 1) %
996 KVM_COALESCED_MMIO_MAX
;
1001 #if !defined(__s390__)
1003 r
= handle_io_window(kvm
);
1008 switch (run
->exit_reason
) {
1009 case KVM_EXIT_UNKNOWN
:
1010 r
= kvm
->callbacks
->unhandled(kvm
, vcpu
,
1011 run
->hw
.hardware_exit_reason
);
1013 case KVM_EXIT_FAIL_ENTRY
:
1014 r
= kvm
->callbacks
->unhandled(kvm
, vcpu
,
1015 run
->fail_entry
.hardware_entry_failure_reason
);
1017 case KVM_EXIT_EXCEPTION
:
1018 fprintf(stderr
, "exception %d (%x)\n",
1020 run
->ex
.error_code
);
1021 kvm_show_regs(vcpu
);
1022 kvm_show_code(vcpu
);
1026 r
= handle_io(vcpu
);
1028 case KVM_EXIT_DEBUG
:
1029 r
= handle_debug(vcpu
, env
);
1032 r
= handle_mmio(vcpu
);
1035 r
= handle_halt(vcpu
);
1037 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1039 case KVM_EXIT_SHUTDOWN
:
1040 r
= handle_shutdown(kvm
, env
);
1042 #if defined(__s390__)
1043 case KVM_EXIT_S390_SIEIC
:
1044 r
= kvm
->callbacks
->s390_handle_intercept(kvm
, vcpu
,
1047 case KVM_EXIT_S390_RESET
:
1048 r
= kvm
->callbacks
->s390_handle_reset(kvm
, vcpu
, run
);
1052 if (kvm_arch_run(vcpu
)) {
1053 fprintf(stderr
, "unhandled vm exit: 0x%x\n",
1055 kvm_show_regs(vcpu
);
1067 int kvm_inject_irq(kvm_vcpu_context_t vcpu
, unsigned irq
)
1069 struct kvm_interrupt intr
;
1072 return ioctl(vcpu
->fd
, KVM_INTERRUPT
, &intr
);
1075 #ifdef KVM_CAP_SET_GUEST_DEBUG
1076 int kvm_set_guest_debug(kvm_vcpu_context_t vcpu
, struct kvm_guest_debug
*dbg
)
1078 return ioctl(vcpu
->fd
, KVM_SET_GUEST_DEBUG
, dbg
);
1082 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu
, const sigset_t
*sigset
)
1084 struct kvm_signal_mask
*sigmask
;
1088 r
= ioctl(vcpu
->fd
, KVM_SET_SIGNAL_MASK
, NULL
);
1093 sigmask
= malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1098 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1099 r
= ioctl(vcpu
->fd
, KVM_SET_SIGNAL_MASK
, sigmask
);
1106 int kvm_irqchip_in_kernel(kvm_context_t kvm
)
1108 return kvm
->irqchip_in_kernel
;
1111 int kvm_pit_in_kernel(kvm_context_t kvm
)
1113 return kvm
->pit_in_kernel
;
1116 int kvm_has_sync_mmu(void)
1119 #ifdef KVM_CAP_SYNC_MMU
1120 r
= ioctl(kvm_context
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_SYNC_MMU
);
1125 int kvm_inject_nmi(kvm_vcpu_context_t vcpu
)
1127 #ifdef KVM_CAP_USER_NMI
1128 return ioctl(vcpu
->fd
, KVM_NMI
);
1134 int kvm_init_coalesced_mmio(kvm_context_t kvm
)
1137 kvm
->coalesced_mmio
= 0;
1138 #ifdef KVM_CAP_COALESCED_MMIO
1139 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
1141 kvm
->coalesced_mmio
= r
;
1148 int kvm_register_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1150 #ifdef KVM_CAP_COALESCED_MMIO
1151 struct kvm_coalesced_mmio_zone zone
;
1154 if (kvm
->coalesced_mmio
) {
1159 r
= ioctl(kvm
->vm_fd
, KVM_REGISTER_COALESCED_MMIO
, &zone
);
1161 perror("kvm_register_coalesced_mmio_zone");
1170 int kvm_unregister_coalesced_mmio(kvm_context_t kvm
, uint64_t addr
, uint32_t size
)
1172 #ifdef KVM_CAP_COALESCED_MMIO
1173 struct kvm_coalesced_mmio_zone zone
;
1176 if (kvm
->coalesced_mmio
) {
1181 r
= ioctl(kvm
->vm_fd
, KVM_UNREGISTER_COALESCED_MMIO
, &zone
);
1183 perror("kvm_unregister_coalesced_mmio_zone");
1186 DPRINTF("Unregistered coalesced mmio region for %llx (%lx)\n", addr
, size
);
1193 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1194 int kvm_assign_pci_device(kvm_context_t kvm
,
1195 struct kvm_assigned_pci_dev
*assigned_dev
)
1199 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
1206 static int kvm_old_assign_irq(kvm_context_t kvm
,
1207 struct kvm_assigned_irq
*assigned_irq
)
1211 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_IRQ
, assigned_irq
);
1218 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
1219 int kvm_assign_irq(kvm_context_t kvm
,
1220 struct kvm_assigned_irq
*assigned_irq
)
1224 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_ASSIGN_DEV_IRQ
);
1226 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_DEV_IRQ
, assigned_irq
);
1232 return kvm_old_assign_irq(kvm
, assigned_irq
);
1235 int kvm_deassign_irq(kvm_context_t kvm
,
1236 struct kvm_assigned_irq
*assigned_irq
)
1240 ret
= ioctl(kvm
->vm_fd
, KVM_DEASSIGN_DEV_IRQ
, assigned_irq
);
1247 int kvm_assign_irq(kvm_context_t kvm
,
1248 struct kvm_assigned_irq
*assigned_irq
)
1250 return kvm_old_assign_irq(kvm
, assigned_irq
);
1255 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1256 int kvm_deassign_pci_device(kvm_context_t kvm
,
1257 struct kvm_assigned_pci_dev
*assigned_dev
)
1261 ret
= ioctl(kvm
->vm_fd
, KVM_DEASSIGN_PCI_DEVICE
, assigned_dev
);
1269 int kvm_destroy_memory_region_works(kvm_context_t kvm
)
1273 #ifdef KVM_CAP_DESTROY_MEMORY_REGION_WORKS
1274 ret
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
,
1275 KVM_CAP_DESTROY_MEMORY_REGION_WORKS
);
1282 int kvm_reinject_control(kvm_context_t kvm
, int pit_reinject
)
1284 #ifdef KVM_CAP_REINJECT_CONTROL
1286 struct kvm_reinject_control control
;
1288 control
.pit_reinject
= pit_reinject
;
1290 r
= ioctl(kvm
->fd
, KVM_CHECK_EXTENSION
, KVM_CAP_REINJECT_CONTROL
);
1292 r
= ioctl(kvm
->vm_fd
, KVM_REINJECT_CONTROL
, &control
);
1301 int kvm_has_gsi_routing(kvm_context_t kvm
)
1305 #ifdef KVM_CAP_IRQ_ROUTING
1306 r
= kvm_check_extension(kvm
, KVM_CAP_IRQ_ROUTING
);
1311 int kvm_get_gsi_count(kvm_context_t kvm
)
1313 #ifdef KVM_CAP_IRQ_ROUTING
1314 return kvm_check_extension(kvm
, KVM_CAP_IRQ_ROUTING
);
1320 int kvm_clear_gsi_routes(kvm_context_t kvm
)
1322 #ifdef KVM_CAP_IRQ_ROUTING
1323 kvm
->irq_routes
->nr
= 0;
1330 int kvm_add_routing_entry(kvm_context_t kvm
,
1331 struct kvm_irq_routing_entry
* entry
)
1333 #ifdef KVM_CAP_IRQ_ROUTING
1334 struct kvm_irq_routing
*z
;
1335 struct kvm_irq_routing_entry
*new;
1338 if (kvm
->irq_routes
->nr
== kvm
->nr_allocated_irq_routes
) {
1339 n
= kvm
->nr_allocated_irq_routes
* 2;
1342 size
= sizeof(struct kvm_irq_routing
);
1343 size
+= n
* sizeof(*new);
1344 z
= realloc(kvm
->irq_routes
, size
);
1347 kvm
->nr_allocated_irq_routes
= n
;
1348 kvm
->irq_routes
= z
;
1350 n
= kvm
->irq_routes
->nr
++;
1351 new = &kvm
->irq_routes
->entries
[n
];
1352 memset(new, 0, sizeof(*new));
1353 new->gsi
= entry
->gsi
;
1354 new->type
= entry
->type
;
1355 new->flags
= entry
->flags
;
1358 set_gsi(kvm
, entry
->gsi
);
1366 int kvm_add_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1368 #ifdef KVM_CAP_IRQ_ROUTING
1369 struct kvm_irq_routing_entry e
;
1372 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1374 e
.u
.irqchip
.irqchip
= irqchip
;
1375 e
.u
.irqchip
.pin
= pin
;
1376 return kvm_add_routing_entry(kvm
, &e
);
1382 int kvm_del_routing_entry(kvm_context_t kvm
,
1383 struct kvm_irq_routing_entry
* entry
)
1385 #ifdef KVM_CAP_IRQ_ROUTING
1386 struct kvm_irq_routing_entry
*e
, *p
;
1387 int i
, gsi
, found
= 0;
1391 for (i
= 0; i
< kvm
->irq_routes
->nr
; ++i
) {
1392 e
= &kvm
->irq_routes
->entries
[i
];
1393 if (e
->type
== entry
->type
1397 case KVM_IRQ_ROUTING_IRQCHIP
: {
1398 if (e
->u
.irqchip
.irqchip
==
1399 entry
->u
.irqchip
.irqchip
1400 && e
->u
.irqchip
.pin
==
1401 entry
->u
.irqchip
.pin
) {
1402 p
= &kvm
->irq_routes
->
1403 entries
[--kvm
->irq_routes
->nr
];
1409 case KVM_IRQ_ROUTING_MSI
: {
1410 if (e
->u
.msi
.address_lo
==
1411 entry
->u
.msi
.address_lo
1412 && e
->u
.msi
.address_hi
==
1413 entry
->u
.msi
.address_hi
1414 && e
->u
.msi
.data
== entry
->u
.msi
.data
) {
1415 p
= &kvm
->irq_routes
->
1416 entries
[--kvm
->irq_routes
->nr
];
1426 /* If there are no other users of this GSI
1427 * mark it available in the bitmap */
1428 for (i
= 0; i
< kvm
->irq_routes
->nr
; i
++) {
1429 e
= &kvm
->irq_routes
->entries
[i
];
1433 if (i
== kvm
->irq_routes
->nr
)
1434 clear_gsi(kvm
, gsi
);
1446 int kvm_del_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1448 #ifdef KVM_CAP_IRQ_ROUTING
1449 struct kvm_irq_routing_entry e
;
1452 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1454 e
.u
.irqchip
.irqchip
= irqchip
;
1455 e
.u
.irqchip
.pin
= pin
;
1456 return kvm_del_routing_entry(kvm
, &e
);
1462 int kvm_commit_irq_routes(kvm_context_t kvm
)
1464 #ifdef KVM_CAP_IRQ_ROUTING
1467 kvm
->irq_routes
->flags
= 0;
1468 r
= ioctl(kvm
->vm_fd
, KVM_SET_GSI_ROUTING
, kvm
->irq_routes
);
1477 int kvm_get_irq_route_gsi(kvm_context_t kvm
)
1480 uint32_t *buf
= kvm
->used_gsi_bitmap
;
1482 /* Return the lowest unused GSI in the bitmap */
1483 for (i
= 0; i
< kvm
->max_gsi
/ 32; i
++) {
1488 return bit
- 1 + i
* 32;
1494 #ifdef KVM_CAP_DEVICE_MSIX
1495 int kvm_assign_set_msix_nr(kvm_context_t kvm
,
1496 struct kvm_assigned_msix_nr
*msix_nr
)
1500 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_SET_MSIX_NR
, msix_nr
);
1507 int kvm_assign_set_msix_entry(kvm_context_t kvm
,
1508 struct kvm_assigned_msix_entry
*entry
)
1512 ret
= ioctl(kvm
->vm_fd
, KVM_ASSIGN_SET_MSIX_ENTRY
, entry
);
1520 #if defined(KVM_CAP_IRQFD) && defined(CONFIG_eventfd)
1522 #include <sys/eventfd.h>
1524 static int _kvm_irqfd(kvm_context_t kvm
, int fd
, int gsi
, int flags
)
1527 struct kvm_irqfd data
= {
1533 r
= ioctl(kvm
->vm_fd
, KVM_IRQFD
, &data
);
1539 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1544 if (!kvm_check_extension(kvm
, KVM_CAP_IRQFD
))
1551 r
= _kvm_irqfd(kvm
, fd
, gsi
, 0);
1560 #else /* KVM_CAP_IRQFD */
1562 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1567 #endif /* KVM_CAP_IRQFD */
1568 static inline unsigned long kvm_get_thread_id(void)
1570 return syscall(SYS_gettid
);
1573 static void qemu_cond_wait(pthread_cond_t
*cond
)
1575 CPUState
*env
= cpu_single_env
;
1576 static const struct timespec ts
= {
1581 pthread_cond_timedwait(cond
, &qemu_mutex
, &ts
);
1582 cpu_single_env
= env
;
1585 static void sig_ipi_handler(int n
)
1589 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
1591 struct qemu_work_item wi
;
1593 if (env
== current_env
) {
1600 if (!env
->kvm_cpu_state
.queued_work_first
)
1601 env
->kvm_cpu_state
.queued_work_first
= &wi
;
1603 env
->kvm_cpu_state
.queued_work_last
->next
= &wi
;
1604 env
->kvm_cpu_state
.queued_work_last
= &wi
;
1608 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1610 qemu_cond_wait(&qemu_work_cond
);
1613 static void inject_interrupt(void *data
)
1615 cpu_interrupt(current_env
, (long)data
);
1618 void kvm_inject_interrupt(CPUState
*env
, int mask
)
1620 on_vcpu(env
, inject_interrupt
, (void *)(long)mask
);
1623 void kvm_update_interrupt_request(CPUState
*env
)
1628 if (!current_env
|| !current_env
->kvm_cpu_state
.created
)
1631 * Testing for created here is really redundant
1633 if (current_env
&& current_env
->kvm_cpu_state
.created
&&
1634 env
!= current_env
&& !env
->kvm_cpu_state
.signalled
)
1638 env
->kvm_cpu_state
.signalled
= 1;
1639 if (env
->kvm_cpu_state
.thread
)
1640 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1645 void kvm_update_after_sipi(CPUState
*env
)
1647 env
->kvm_cpu_state
.sipi_needed
= 1;
1648 kvm_update_interrupt_request(env
);
1651 void kvm_apic_init(CPUState
*env
)
1653 if (env
->cpu_index
!= 0)
1654 env
->kvm_cpu_state
.init
= 1;
1655 kvm_update_interrupt_request(env
);
1660 static int kvm_try_push_interrupts(void *opaque
)
1662 return kvm_arch_try_push_interrupts(opaque
);
1665 static void kvm_post_run(void *opaque
, void *data
)
1667 CPUState
*env
= (CPUState
*)data
;
1669 pthread_mutex_lock(&qemu_mutex
);
1670 kvm_arch_post_kvm_run(opaque
, env
);
1673 static int kvm_pre_run(void *opaque
, void *data
)
1675 CPUState
*env
= (CPUState
*)data
;
1677 kvm_arch_pre_kvm_run(opaque
, env
);
1679 if (env
->exit_request
)
1681 pthread_mutex_unlock(&qemu_mutex
);
1685 static void kvm_do_load_registers(void *_env
)
1687 CPUState
*env
= _env
;
1689 kvm_arch_load_regs(env
);
1692 void kvm_load_registers(CPUState
*env
)
1694 if (kvm_enabled() && qemu_system_ready
)
1695 on_vcpu(env
, kvm_do_load_registers
, env
);
1698 static void kvm_do_save_registers(void *_env
)
1700 CPUState
*env
= _env
;
1702 kvm_arch_save_regs(env
);
1705 void kvm_save_registers(CPUState
*env
)
1708 on_vcpu(env
, kvm_do_save_registers
, env
);
1711 int kvm_cpu_exec(CPUState
*env
)
1715 r
= kvm_run(env
->kvm_cpu_state
.vcpu_ctx
, env
);
1717 printf("kvm_run returned %d\n", r
);
1724 static int has_work(CPUState
*env
)
1726 if (!vm_running
|| (env
&& env
->kvm_cpu_state
.stopped
))
1728 if (kvm_irqchip_in_kernel(kvm_context
))
1732 return kvm_arch_has_work(env
);
1735 static void flush_queued_work(CPUState
*env
)
1737 struct qemu_work_item
*wi
;
1739 if (!env
->kvm_cpu_state
.queued_work_first
)
1742 while ((wi
= env
->kvm_cpu_state
.queued_work_first
)) {
1743 env
->kvm_cpu_state
.queued_work_first
= wi
->next
;
1747 env
->kvm_cpu_state
.queued_work_last
= NULL
;
1748 pthread_cond_broadcast(&qemu_work_cond
);
1751 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
1758 pthread_mutex_unlock(&qemu_mutex
);
1760 ts
.tv_sec
= timeout
/ 1000;
1761 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
1762 sigemptyset(&waitset
);
1763 sigaddset(&waitset
, SIG_IPI
);
1765 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
1768 pthread_mutex_lock(&qemu_mutex
);
1770 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
1771 printf("sigtimedwait: %s\n", strerror(e
));
1775 cpu_single_env
= env
;
1776 flush_queued_work(env
);
1778 if (env
->kvm_cpu_state
.stop
) {
1779 env
->kvm_cpu_state
.stop
= 0;
1780 env
->kvm_cpu_state
.stopped
= 1;
1781 pthread_cond_signal(&qemu_pause_cond
);
1784 env
->kvm_cpu_state
.signalled
= 0;
1787 static int all_threads_paused(void)
1789 CPUState
*penv
= first_cpu
;
1792 if (penv
->kvm_cpu_state
.stop
)
1794 penv
= (CPUState
*)penv
->next_cpu
;
1800 static void pause_all_threads(void)
1802 CPUState
*penv
= first_cpu
;
1805 if (penv
!= cpu_single_env
) {
1806 penv
->kvm_cpu_state
.stop
= 1;
1807 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1809 penv
->kvm_cpu_state
.stop
= 0;
1810 penv
->kvm_cpu_state
.stopped
= 1;
1813 penv
= (CPUState
*)penv
->next_cpu
;
1816 while (!all_threads_paused())
1817 qemu_cond_wait(&qemu_pause_cond
);
1820 static void resume_all_threads(void)
1822 CPUState
*penv
= first_cpu
;
1824 assert(!cpu_single_env
);
1827 penv
->kvm_cpu_state
.stop
= 0;
1828 penv
->kvm_cpu_state
.stopped
= 0;
1829 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1830 penv
= (CPUState
*)penv
->next_cpu
;
1834 static void kvm_vm_state_change_handler(void *context
, int running
, int reason
)
1837 resume_all_threads();
1839 pause_all_threads();
1842 static void update_regs_for_sipi(CPUState
*env
)
1844 kvm_arch_update_regs_for_sipi(env
);
1845 env
->kvm_cpu_state
.sipi_needed
= 0;
1848 static void update_regs_for_init(CPUState
*env
)
1851 SegmentCache cs
= env
->segs
[R_CS
];
1857 /* restore SIPI vector */
1858 if(env
->kvm_cpu_state
.sipi_needed
)
1859 env
->segs
[R_CS
] = cs
;
1862 env
->kvm_cpu_state
.init
= 0;
1863 kvm_arch_load_regs(env
);
1866 static void setup_kernel_sigmask(CPUState
*env
)
1871 sigaddset(&set
, SIGUSR2
);
1872 sigaddset(&set
, SIGIO
);
1873 sigaddset(&set
, SIGALRM
);
1874 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1876 sigprocmask(SIG_BLOCK
, NULL
, &set
);
1877 sigdelset(&set
, SIG_IPI
);
1879 kvm_set_signal_mask(env
->kvm_cpu_state
.vcpu_ctx
, &set
);
1882 static void qemu_kvm_system_reset(void)
1884 CPUState
*penv
= first_cpu
;
1886 pause_all_threads();
1888 qemu_system_reset();
1891 kvm_arch_cpu_reset(penv
);
1892 penv
= (CPUState
*)penv
->next_cpu
;
1895 resume_all_threads();
1898 static int kvm_main_loop_cpu(CPUState
*env
)
1900 setup_kernel_sigmask(env
);
1902 pthread_mutex_lock(&qemu_mutex
);
1904 kvm_qemu_init_env(env
);
1906 kvm_tpr_vcpu_start(env
);
1909 cpu_single_env
= env
;
1910 kvm_arch_load_regs(env
);
1913 while (!has_work(env
))
1914 kvm_main_loop_wait(env
, 1000);
1915 if (env
->interrupt_request
& (CPU_INTERRUPT_HARD
| CPU_INTERRUPT_NMI
))
1917 if (!kvm_irqchip_in_kernel(kvm_context
)) {
1918 if (env
->kvm_cpu_state
.init
)
1919 update_regs_for_init(env
);
1920 if (env
->kvm_cpu_state
.sipi_needed
)
1921 update_regs_for_sipi(env
);
1923 if (!env
->halted
|| kvm_irqchip_in_kernel(kvm_context
))
1925 env
->exit_request
= 0;
1926 env
->exception_index
= EXCP_INTERRUPT
;
1927 kvm_main_loop_wait(env
, 0);
1929 pthread_mutex_unlock(&qemu_mutex
);
1933 static void *ap_main_loop(void *_env
)
1935 CPUState
*env
= _env
;
1937 struct ioperm_data
*data
= NULL
;
1940 env
->thread_id
= kvm_get_thread_id();
1941 sigfillset(&signals
);
1942 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
1943 env
->kvm_cpu_state
.vcpu_ctx
= kvm_create_vcpu(kvm_context
, env
->cpu_index
);
1945 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1946 /* do ioperm for io ports of assigned devices */
1947 LIST_FOREACH(data
, &ioperm_head
, entries
)
1948 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
1951 /* signal VCPU creation */
1952 pthread_mutex_lock(&qemu_mutex
);
1953 current_env
->kvm_cpu_state
.created
= 1;
1954 pthread_cond_signal(&qemu_vcpu_cond
);
1956 /* and wait for machine initialization */
1957 while (!qemu_system_ready
)
1958 qemu_cond_wait(&qemu_system_cond
);
1959 pthread_mutex_unlock(&qemu_mutex
);
1961 kvm_main_loop_cpu(env
);
1965 void kvm_init_vcpu(CPUState
*env
)
1967 pthread_create(&env
->kvm_cpu_state
.thread
, NULL
, ap_main_loop
, env
);
1969 while (env
->kvm_cpu_state
.created
== 0)
1970 qemu_cond_wait(&qemu_vcpu_cond
);
1973 int kvm_vcpu_inited(CPUState
*env
)
1975 return env
->kvm_cpu_state
.created
;
1978 int kvm_init_ap(void)
1981 kvm_tpr_opt_setup();
1983 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
1985 signal(SIG_IPI
, sig_ipi_handler
);
1989 void qemu_kvm_notify_work(void)
1995 if (io_thread_fd
== -1)
1998 memcpy(buffer
, &value
, sizeof(value
));
2000 while (offset
< 8) {
2003 len
= write(io_thread_fd
, buffer
+ offset
, 8 - offset
);
2004 if (len
== -1 && errno
== EINTR
)
2014 fprintf(stderr
, "failed to notify io thread\n");
2017 /* If we have signalfd, we mask out the signals we want to handle and then
2018 * use signalfd to listen for them. We rely on whatever the current signal
2019 * handler is to dispatch the signals when we receive them.
2022 static void sigfd_handler(void *opaque
)
2024 int fd
= (unsigned long)opaque
;
2025 struct qemu_signalfd_siginfo info
;
2026 struct sigaction action
;
2031 len
= read(fd
, &info
, sizeof(info
));
2032 } while (len
== -1 && errno
== EINTR
);
2034 if (len
== -1 && errno
== EAGAIN
)
2037 if (len
!= sizeof(info
)) {
2038 printf("read from sigfd returned %zd: %m\n", len
);
2042 sigaction(info
.ssi_signo
, NULL
, &action
);
2043 if (action
.sa_handler
)
2044 action
.sa_handler(info
.ssi_signo
);
2049 /* Used to break IO thread out of select */
2050 static void io_thread_wakeup(void *opaque
)
2052 int fd
= (unsigned long)opaque
;
2056 while (offset
< 8) {
2059 len
= read(fd
, buffer
+ offset
, 8 - offset
);
2060 if (len
== -1 && errno
== EINTR
)
2070 int kvm_main_loop(void)
2076 io_thread
= pthread_self();
2077 qemu_system_ready
= 1;
2079 if (qemu_eventfd(fds
) == -1) {
2080 fprintf(stderr
, "failed to create eventfd\n");
2084 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
2085 (void *)(unsigned long)fds
[0]);
2087 io_thread_fd
= fds
[1];
2090 sigaddset(&mask
, SIGIO
);
2091 sigaddset(&mask
, SIGALRM
);
2092 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
2094 sigfd
= qemu_signalfd(&mask
);
2096 fprintf(stderr
, "failed to create signalfd\n");
2100 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
2102 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
2103 (void *)(unsigned long)sigfd
);
2105 pthread_cond_broadcast(&qemu_system_cond
);
2107 io_thread_sigfd
= sigfd
;
2108 cpu_single_env
= NULL
;
2111 main_loop_wait(1000);
2112 if (qemu_shutdown_requested()) {
2113 if (qemu_no_shutdown()) {
2117 } else if (qemu_powerdown_requested())
2118 qemu_system_powerdown();
2119 else if (qemu_reset_requested())
2120 qemu_kvm_system_reset();
2121 else if (kvm_debug_cpu_requested
) {
2122 gdb_set_stop_cpu(kvm_debug_cpu_requested
);
2123 vm_stop(EXCP_DEBUG
);
2124 kvm_debug_cpu_requested
= NULL
;
2128 pause_all_threads();
2129 pthread_mutex_unlock(&qemu_mutex
);
2134 #ifdef KVM_CAP_SET_GUEST_DEBUG
2135 static int kvm_debug(void *opaque
, void *data
,
2136 struct kvm_debug_exit_arch
*arch_info
)
2138 int handle
= kvm_arch_debug(arch_info
);
2139 CPUState
*env
= data
;
2142 kvm_debug_cpu_requested
= env
;
2143 env
->kvm_cpu_state
.stopped
= 1;
2149 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
2151 *data
= cpu_inb(0, addr
);
2155 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
2157 *data
= cpu_inw(0, addr
);
2161 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
2163 *data
= cpu_inl(0, addr
);
2167 #define PM_IO_BASE 0xb000
2169 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
2174 cpu_outb(0, 0xb3, 0);
2181 x
= cpu_inw(0, PM_IO_BASE
+ 4);
2183 cpu_outw(0, PM_IO_BASE
+ 4, x
);
2190 x
= cpu_inw(0, PM_IO_BASE
+ 4);
2192 cpu_outw(0, PM_IO_BASE
+ 4, x
);
2200 cpu_outb(0, addr
, data
);
2204 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
2206 cpu_outw(0, addr
, data
);
2210 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
2212 cpu_outl(0, addr
, data
);
2216 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
2218 cpu_physical_memory_rw(addr
, data
, len
, 0);
2222 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
2224 cpu_physical_memory_rw(addr
, data
, len
, 1);
2228 static int kvm_io_window(void *opaque
)
2234 static int kvm_halt(void *opaque
, kvm_vcpu_context_t vcpu
)
2236 return kvm_arch_halt(opaque
, vcpu
);
2239 static int kvm_shutdown(void *opaque
, void *data
)
2241 CPUState
*env
= (CPUState
*)data
;
2243 /* stop the current vcpu from going back to guest mode */
2244 env
->kvm_cpu_state
.stopped
= 1;
2246 qemu_system_reset_request();
2250 static int handle_unhandled(kvm_context_t kvm
, kvm_vcpu_context_t vcpu
,
2253 fprintf(stderr
, "kvm: unhandled exit %"PRIx64
"\n", reason
);
2257 static struct kvm_callbacks qemu_kvm_ops
= {
2258 #ifdef KVM_CAP_SET_GUEST_DEBUG
2267 .mmio_read
= kvm_mmio_read
,
2268 .mmio_write
= kvm_mmio_write
,
2270 .shutdown
= kvm_shutdown
,
2271 .io_window
= kvm_io_window
,
2272 .try_push_interrupts
= kvm_try_push_interrupts
,
2273 #ifdef KVM_CAP_USER_NMI
2274 .push_nmi
= kvm_arch_push_nmi
,
2276 .post_kvm_run
= kvm_post_run
,
2277 .pre_kvm_run
= kvm_pre_run
,
2279 .tpr_access
= handle_tpr_access
,
2282 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
2283 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
2285 .unhandled
= handle_unhandled
,
2290 /* Try to initialize kvm */
2291 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
2295 pthread_mutex_lock(&qemu_mutex
);
2301 static int destroy_region_works
= 0;
2305 #if !defined(TARGET_I386)
2306 int kvm_arch_init_irq_routing(void)
2312 int kvm_qemu_create_context(void)
2317 kvm_disable_irqchip_creation(kvm_context
);
2320 kvm_disable_pit_creation(kvm_context
);
2322 if (kvm_create(kvm_context
, 0, NULL
) < 0) {
2326 r
= kvm_arch_qemu_create_context();
2329 if (kvm_pit
&& !kvm_pit_reinject
) {
2330 if (kvm_reinject_control(kvm_context
, 0)) {
2331 fprintf(stderr
, "failure to disable in-kernel PIT reinjection\n");
2336 destroy_region_works
= kvm_destroy_memory_region_works(kvm_context
);
2339 r
= kvm_arch_init_irq_routing();
2347 void kvm_qemu_destroy(void)
2349 kvm_finalize(kvm_context
);
2353 static int must_use_aliases_source(target_phys_addr_t addr
)
2355 if (destroy_region_works
)
2357 if (addr
== 0xa0000 || addr
== 0xa8000)
2362 static int must_use_aliases_target(target_phys_addr_t addr
)
2364 if (destroy_region_works
)
2366 if (addr
>= 0xe0000000 && addr
< 0x100000000ull
)
2371 static struct mapping
{
2372 target_phys_addr_t phys
;
2376 static int nr_mappings
;
2378 static struct mapping
*find_ram_mapping(ram_addr_t ram_addr
)
2382 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
2383 if (p
->ram
<= ram_addr
&& ram_addr
< p
->ram
+ p
->len
) {
2390 static struct mapping
*find_mapping(target_phys_addr_t start_addr
)
2394 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
2395 if (p
->phys
<= start_addr
&& start_addr
< p
->phys
+ p
->len
) {
2402 static void drop_mapping(target_phys_addr_t start_addr
)
2404 struct mapping
*p
= find_mapping(start_addr
);
2407 *p
= mappings
[--nr_mappings
];
2411 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
2413 unsigned long phys_offset
)
2416 unsigned long area_flags
;
2421 if (start_addr
+ size
> phys_ram_size
) {
2422 phys_ram_size
= start_addr
+ size
;
2425 phys_offset
&= ~IO_MEM_ROM
;
2426 area_flags
= phys_offset
& ~TARGET_PAGE_MASK
;
2428 if (area_flags
!= IO_MEM_RAM
) {
2430 if (must_use_aliases_source(start_addr
)) {
2431 kvm_destroy_memory_alias(kvm_context
, start_addr
);
2434 if (must_use_aliases_target(start_addr
))
2438 p
= find_mapping(start_addr
);
2440 kvm_unregister_memory_area(kvm_context
, p
->phys
, p
->len
);
2441 drop_mapping(p
->phys
);
2443 start_addr
+= TARGET_PAGE_SIZE
;
2444 if (size
> TARGET_PAGE_SIZE
) {
2445 size
-= TARGET_PAGE_SIZE
;
2453 r
= kvm_is_containing_region(kvm_context
, start_addr
, size
);
2457 if (area_flags
>= TLB_MMIO
)
2461 if (must_use_aliases_source(start_addr
)) {
2462 p
= find_ram_mapping(phys_offset
);
2464 kvm_create_memory_alias(kvm_context
, start_addr
, size
,
2465 p
->phys
+ (phys_offset
- p
->ram
));
2471 r
= kvm_register_phys_mem(kvm_context
, start_addr
,
2472 qemu_get_ram_ptr(phys_offset
),
2475 printf("kvm_cpu_register_physical_memory: failed\n");
2480 drop_mapping(start_addr
);
2481 p
= &mappings
[nr_mappings
++];
2482 p
->phys
= start_addr
;
2483 p
->ram
= phys_offset
;
2490 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr
,
2491 target_phys_addr_t size
,
2492 unsigned long phys_offset
)
2494 kvm_unregister_memory_area(kvm_context
, start_addr
, size
);
2497 int kvm_setup_guest_memory(void *area
, unsigned long size
)
2501 #ifdef MADV_DONTFORK
2502 if (kvm_enabled() && !kvm_has_sync_mmu())
2503 ret
= madvise(area
, size
, MADV_DONTFORK
);
2512 int kvm_qemu_check_extension(int ext
)
2514 return kvm_check_extension(kvm_context
, ext
);
2517 int kvm_qemu_init_env(CPUState
*cenv
)
2519 return kvm_arch_qemu_init_env(cenv
);
2522 #ifdef KVM_CAP_SET_GUEST_DEBUG
2523 struct kvm_sw_breakpoint_head kvm_sw_breakpoints
=
2524 TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints
);
2526 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(target_ulong pc
)
2528 struct kvm_sw_breakpoint
*bp
;
2530 TAILQ_FOREACH(bp
, &kvm_sw_breakpoints
, entry
) {
2537 struct kvm_set_guest_debug_data
{
2538 struct kvm_guest_debug dbg
;
2542 static void kvm_invoke_set_guest_debug(void *data
)
2544 struct kvm_set_guest_debug_data
*dbg_data
= data
;
2546 dbg_data
->err
= kvm_set_guest_debug(cpu_single_env
->kvm_cpu_state
.vcpu_ctx
,
2550 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
2552 struct kvm_set_guest_debug_data data
;
2554 data
.dbg
.control
= 0;
2555 if (env
->singlestep_enabled
)
2556 data
.dbg
.control
= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
2558 kvm_arch_update_guest_debug(env
, &data
.dbg
);
2559 data
.dbg
.control
|= reinject_trap
;
2561 on_vcpu(env
, kvm_invoke_set_guest_debug
, &data
);
2565 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
2566 target_ulong len
, int type
)
2568 struct kvm_sw_breakpoint
*bp
;
2572 if (type
== GDB_BREAKPOINT_SW
) {
2573 bp
= kvm_find_sw_breakpoint(addr
);
2579 bp
= qemu_malloc(sizeof(struct kvm_sw_breakpoint
));
2585 err
= kvm_arch_insert_sw_breakpoint(current_env
, bp
);
2591 TAILQ_INSERT_HEAD(&kvm_sw_breakpoints
, bp
, entry
);
2593 err
= kvm_arch_insert_hw_breakpoint(addr
, len
, type
);
2598 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2599 err
= kvm_update_guest_debug(env
, 0);
2606 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
2607 target_ulong len
, int type
)
2609 struct kvm_sw_breakpoint
*bp
;
2613 if (type
== GDB_BREAKPOINT_SW
) {
2614 bp
= kvm_find_sw_breakpoint(addr
);
2618 if (bp
->use_count
> 1) {
2623 err
= kvm_arch_remove_sw_breakpoint(current_env
, bp
);
2627 TAILQ_REMOVE(&kvm_sw_breakpoints
, bp
, entry
);
2630 err
= kvm_arch_remove_hw_breakpoint(addr
, len
, type
);
2635 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2636 err
= kvm_update_guest_debug(env
, 0);
2643 void kvm_remove_all_breakpoints(CPUState
*current_env
)
2645 struct kvm_sw_breakpoint
*bp
, *next
;
2648 TAILQ_FOREACH_SAFE(bp
, &kvm_sw_breakpoints
, entry
, next
) {
2649 if (kvm_arch_remove_sw_breakpoint(current_env
, bp
) != 0) {
2650 /* Try harder to find a CPU that currently sees the breakpoint. */
2651 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2652 if (kvm_arch_remove_sw_breakpoint(env
, bp
) == 0)
2657 kvm_arch_remove_all_hw_breakpoints();
2659 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2660 kvm_update_guest_debug(env
, 0);
2663 #else /* !KVM_CAP_SET_GUEST_DEBUG */
2665 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
2670 int kvm_insert_breakpoint(CPUState
*current_env
, target_ulong addr
,
2671 target_ulong len
, int type
)
2676 int kvm_remove_breakpoint(CPUState
*current_env
, target_ulong addr
,
2677 target_ulong len
, int type
)
2682 void kvm_remove_all_breakpoints(CPUState
*current_env
)
2685 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
2688 * dirty pages logging
2690 /* FIXME: use unsigned long pointer instead of unsigned char */
2691 unsigned char *kvm_dirty_bitmap
= NULL
;
2692 int kvm_physical_memory_set_dirty_tracking(int enable
)
2700 if (!kvm_dirty_bitmap
) {
2701 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
2702 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
2703 if (kvm_dirty_bitmap
== NULL
) {
2704 perror("Failed to allocate dirty pages bitmap");
2708 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
2713 if (kvm_dirty_bitmap
) {
2714 r
= kvm_dirty_pages_log_reset(kvm_context
);
2715 qemu_free(kvm_dirty_bitmap
);
2716 kvm_dirty_bitmap
= NULL
;
2722 /* get kvm's dirty pages bitmap and update qemu's */
2723 static int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
2724 unsigned char *bitmap
,
2725 unsigned long offset
,
2726 unsigned long mem_size
)
2728 unsigned int i
, j
, n
=0;
2730 unsigned long page_number
, addr
, addr1
;
2731 ram_addr_t ram_addr
;
2732 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
2735 * bitmap-traveling is faster than memory-traveling (for addr...)
2736 * especially when most of the memory is not dirty.
2738 for (i
=0; i
<len
; i
++) {
2743 page_number
= i
* 8 + j
;
2744 addr1
= page_number
* TARGET_PAGE_SIZE
;
2745 addr
= offset
+ addr1
;
2746 ram_addr
= cpu_get_physical_page_desc(addr
);
2747 cpu_physical_memory_set_dirty(ram_addr
);
2753 static int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
2754 void *bitmap
, void *opaque
)
2756 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
2760 * get kvm's dirty pages bitmap and update qemu's
2761 * we only care about physical ram, which resides in slots 0 and 3
2763 int kvm_update_dirty_pages_log(void)
2768 r
= kvm_get_dirty_pages_range(kvm_context
, 0, -1UL,
2769 kvm_dirty_bitmap
, NULL
,
2770 kvm_get_dirty_bitmap_cb
);
2774 void kvm_qemu_log_memory(target_phys_addr_t start
, target_phys_addr_t size
,
2778 kvm_dirty_pages_log_enable_slot(kvm_context
, start
, size
);
2781 if (must_use_aliases_target(start
))
2784 kvm_dirty_pages_log_disable_slot(kvm_context
, start
, size
);
2788 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
2790 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
2791 unsigned int brsize
= BITMAP_SIZE(ram_size
);
2792 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
2793 unsigned int extra_bytes
= (extra_pages
+7)/8;
2794 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
2795 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
2797 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
2798 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
2799 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
2804 #ifdef KVM_CAP_IRQCHIP
2806 int kvm_set_irq(int irq
, int level
, int *status
)
2808 return kvm_set_irq_level(kvm_context
, irq
, level
, status
);
2813 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
2815 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
2818 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
2819 unsigned long size
, int log
, int writable
)
2821 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
2824 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
2827 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);
2830 void kvm_mutex_unlock(void)
2832 assert(!cpu_single_env
);
2833 pthread_mutex_unlock(&qemu_mutex
);
2836 void kvm_mutex_lock(void)
2838 pthread_mutex_lock(&qemu_mutex
);
2839 cpu_single_env
= NULL
;
2842 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr
, unsigned int size
)
2844 return kvm_register_coalesced_mmio(kvm_context
, addr
, size
);
2847 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr
,
2850 return kvm_unregister_coalesced_mmio(kvm_context
, addr
, size
);
2853 int kvm_coalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
2855 return kvm_register_coalesced_mmio(kvm_context
, start
, size
);
2858 int kvm_uncoalesce_mmio_region(target_phys_addr_t start
, ram_addr_t size
)
2860 return kvm_unregister_coalesced_mmio(kvm_context
, start
, size
);
2863 #ifdef USE_KVM_DEVICE_ASSIGNMENT
2864 void kvm_add_ioperm_data(struct ioperm_data
*data
)
2866 LIST_INSERT_HEAD(&ioperm_head
, data
, entries
);
2869 void kvm_remove_ioperm_data(unsigned long start_port
, unsigned long num
)
2871 struct ioperm_data
*data
;
2873 data
= LIST_FIRST(&ioperm_head
);
2875 struct ioperm_data
*next
= LIST_NEXT(data
, entries
);
2877 if (data
->start_port
== start_port
&& data
->num
== num
) {
2878 LIST_REMOVE(data
, entries
);
2886 void kvm_ioperm(CPUState
*env
, void *data
)
2888 if (kvm_enabled() && qemu_system_ready
)
2889 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
2894 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
, target_phys_addr_t end_addr
)
2900 if (must_use_aliases_source(start_addr
))
2904 buf
= qemu_malloc((end_addr
- start_addr
) / 8 + 2);
2905 kvm_get_dirty_pages_range(kvm_context
, start_addr
, end_addr
- start_addr
,
2906 buf
, NULL
, kvm_get_dirty_bitmap_cb
);
2912 int kvm_log_start(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
2915 if (must_use_aliases_source(phys_addr
))
2920 kvm_qemu_log_memory(phys_addr
, len
, 1);
2925 int kvm_log_stop(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
2928 if (must_use_aliases_source(phys_addr
))
2933 kvm_qemu_log_memory(phys_addr
, len
, 0);
2938 void qemu_kvm_cpu_stop(CPUState
*env
)
2941 env
->kvm_cpu_state
.stopped
= 1;