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"
25 #include <sys/utsname.h>
26 #include <sys/syscall.h>
28 #include <sys/ioctl.h>
30 #include <sys/prctl.h>
36 #define PR_MCE_KILL 33
40 #define BUS_MCEERR_AR 4
43 #define BUS_MCEERR_AO 5
46 #define EXPECTED_KVM_API_VERSION 12
48 #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
49 #error libkvm: userspace and kernel version mismatch
55 int kvm_pit_reinject
= 1;
60 kvm_context_t kvm_context
;
62 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
63 pthread_cond_t qemu_vcpu_cond
= PTHREAD_COND_INITIALIZER
;
64 pthread_cond_t qemu_system_cond
= PTHREAD_COND_INITIALIZER
;
65 pthread_cond_t qemu_pause_cond
= PTHREAD_COND_INITIALIZER
;
66 pthread_cond_t qemu_work_cond
= PTHREAD_COND_INITIALIZER
;
67 __thread CPUState
*current_env
;
69 static int qemu_system_ready
;
71 #define SIG_IPI (SIGRTMIN+4)
74 static int io_thread_fd
= -1;
75 static int io_thread_sigfd
= -1;
77 static CPUState
*kvm_debug_cpu_requested
;
79 static uint64_t phys_ram_size
;
81 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
82 /* The list of ioperm_data */
83 static QLIST_HEAD(, ioperm_data
) ioperm_head
;
86 //#define DEBUG_MEMREG
88 #define DPRINTF(fmt, args...) \
89 do { fprintf(stderr, "%s:%d " fmt , __func__, __LINE__, ##args); } while (0)
91 #define DPRINTF(fmt, args...) do {} while (0)
94 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
96 int kvm_abi
= EXPECTED_KVM_API_VERSION
;
99 #ifdef KVM_CAP_SET_GUEST_DEBUG
100 static int kvm_debug(CPUState
*env
,
101 struct kvm_debug_exit_arch
*arch_info
)
103 int handle
= kvm_arch_debug(arch_info
);
106 kvm_debug_cpu_requested
= env
;
113 static int handle_unhandled(uint64_t reason
)
115 fprintf(stderr
, "kvm: unhandled exit %" PRIx64
"\n", reason
);
120 static inline void set_gsi(kvm_context_t kvm
, unsigned int gsi
)
122 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
124 if (gsi
< kvm
->max_gsi
)
125 bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
127 DPRINTF("Invalid GSI %d\n");
130 static inline void clear_gsi(kvm_context_t kvm
, unsigned int gsi
)
132 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
134 if (gsi
< kvm
->max_gsi
)
135 bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
137 DPRINTF("Invalid GSI %d\n");
141 unsigned long phys_addr
;
143 unsigned long userspace_addr
;
148 struct slot_info slots
[KVM_MAX_NUM_MEM_REGIONS
];
150 static void init_slots(void)
154 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
158 static int get_free_slot(kvm_context_t kvm
)
163 #if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
164 tss_ext
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
170 * on older kernels where the set tss ioctl is not supprted we must save
171 * slot 0 to hold the extended memory, as the vmx will use the last 3
172 * pages of this slot.
179 for (; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
185 static void register_slot(int slot
, unsigned long phys_addr
,
186 unsigned long len
, unsigned long userspace_addr
,
189 slots
[slot
].phys_addr
= phys_addr
;
190 slots
[slot
].len
= len
;
191 slots
[slot
].userspace_addr
= userspace_addr
;
192 slots
[slot
].flags
= flags
;
195 static void free_slot(int slot
)
198 slots
[slot
].logging_count
= 0;
201 static int get_slot(unsigned long phys_addr
)
205 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
206 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
207 (slots
[i
].phys_addr
+ slots
[i
].len
- 1) >= phys_addr
)
213 /* Returns -1 if this slot is not totally contained on any other,
214 * and the number of the slot otherwise */
215 static int get_container_slot(uint64_t phys_addr
, unsigned long size
)
219 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
)
220 if (slots
[i
].len
&& slots
[i
].phys_addr
<= phys_addr
&&
221 (slots
[i
].phys_addr
+ slots
[i
].len
) >= phys_addr
+ size
)
226 int kvm_is_containing_region(kvm_context_t kvm
, unsigned long phys_addr
,
229 int slot
= get_container_slot(phys_addr
, size
);
236 * dirty pages logging control
238 static int kvm_dirty_pages_log_change(kvm_context_t kvm
,
239 unsigned long phys_addr
, unsigned flags
,
243 int slot
= get_slot(phys_addr
);
246 fprintf(stderr
, "BUG: %s: invalid parameters\n", __FUNCTION__
);
250 flags
= (slots
[slot
].flags
& ~mask
) | flags
;
251 if (flags
== slots
[slot
].flags
)
253 slots
[slot
].flags
= flags
;
256 struct kvm_userspace_memory_region mem
= {
258 .memory_size
= slots
[slot
].len
,
259 .guest_phys_addr
= slots
[slot
].phys_addr
,
260 .userspace_addr
= slots
[slot
].userspace_addr
,
261 .flags
= slots
[slot
].flags
,
265 DPRINTF("slot %d start %llx len %llx flags %x\n",
266 mem
.slot
, mem
.guest_phys_addr
, mem
.memory_size
, mem
.flags
);
267 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_USER_MEMORY_REGION
, &mem
);
269 fprintf(stderr
, "%s: %m\n", __FUNCTION__
);
274 static int kvm_dirty_pages_log_change_all(kvm_context_t kvm
,
275 int (*change
)(kvm_context_t kvm
,
281 for (i
= r
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
&& r
== 0; i
++) {
283 r
= change(kvm
, slots
[i
].phys_addr
, slots
[i
].len
);
288 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm
, uint64_t phys_addr
,
291 int slot
= get_slot(phys_addr
);
293 DPRINTF("start %" PRIx64
" len %" PRIx64
"\n", phys_addr
, len
);
295 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
299 if (slots
[slot
].logging_count
++)
302 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
,
303 KVM_MEM_LOG_DIRTY_PAGES
,
304 KVM_MEM_LOG_DIRTY_PAGES
);
307 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm
, uint64_t phys_addr
,
310 int slot
= get_slot(phys_addr
);
313 fprintf(stderr
, "BUG: %s: invalid parameters\n", __func__
);
317 if (--slots
[slot
].logging_count
)
320 return kvm_dirty_pages_log_change(kvm
, slots
[slot
].phys_addr
, 0,
321 KVM_MEM_LOG_DIRTY_PAGES
);
325 * Enable dirty page logging for all memory regions
327 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
)
329 if (kvm
->dirty_pages_log_all
)
331 kvm
->dirty_pages_log_all
= 1;
332 return kvm_dirty_pages_log_change_all(kvm
, kvm_dirty_pages_log_enable_slot
);
336 * Enable dirty page logging only for memory regions that were created with
337 * dirty logging enabled (disable for all other memory regions).
339 int kvm_dirty_pages_log_reset(kvm_context_t kvm
)
341 if (!kvm
->dirty_pages_log_all
)
343 kvm
->dirty_pages_log_all
= 0;
344 return kvm_dirty_pages_log_change_all(kvm
,
345 kvm_dirty_pages_log_disable_slot
);
349 static int kvm_create_context(void);
351 int kvm_init(int smp_cpus
)
357 fd
= open("/dev/kvm", O_RDWR
);
359 perror("open /dev/kvm");
362 r
= ioctl(fd
, KVM_GET_API_VERSION
, 0);
365 "kvm kernel version too old: "
366 "KVM_GET_API_VERSION ioctl not supported\n");
369 if (r
< EXPECTED_KVM_API_VERSION
) {
370 fprintf(stderr
, "kvm kernel version too old: "
371 "We expect API version %d or newer, but got "
372 "version %d\n", EXPECTED_KVM_API_VERSION
, r
);
375 if (r
> EXPECTED_KVM_API_VERSION
) {
376 fprintf(stderr
, "kvm userspace version too old\n");
380 kvm_page_size
= getpagesize();
381 kvm_state
= qemu_mallocz(sizeof(*kvm_state
));
382 kvm_context
= &kvm_state
->kvm_context
;
385 kvm_state
->vmfd
= -1;
386 kvm_context
->opaque
= cpu_single_env
;
387 kvm_context
->dirty_pages_log_all
= 0;
388 kvm_context
->no_irqchip_creation
= 0;
389 kvm_context
->no_pit_creation
= 0;
391 #ifdef KVM_CAP_SET_GUEST_DEBUG
392 QTAILQ_INIT(&kvm_state
->kvm_sw_breakpoints
);
395 gsi_count
= kvm_get_gsi_count(kvm_context
);
399 /* Round up so we can search ints using ffs */
400 gsi_bits
= ALIGN(gsi_count
, 32);
401 kvm_context
->used_gsi_bitmap
= qemu_mallocz(gsi_bits
/ 8);
402 kvm_context
->max_gsi
= gsi_bits
;
404 /* Mark any over-allocated bits as already in use */
405 for (i
= gsi_count
; i
< gsi_bits
; i
++)
406 set_gsi(kvm_context
, i
);
409 pthread_mutex_lock(&qemu_mutex
);
410 return kvm_create_context();
417 static void kvm_finalize(KVMState
*s
)
420 if (kvm->vcpu_fd[0] != -1)
421 close(kvm->vcpu_fd[0]);
422 if (kvm->vm_fd != -1)
429 void kvm_disable_irqchip_creation(kvm_context_t kvm
)
431 kvm
->no_irqchip_creation
= 1;
434 void kvm_disable_pit_creation(kvm_context_t kvm
)
436 kvm
->no_pit_creation
= 1;
439 kvm_vcpu_context_t
kvm_create_vcpu(CPUState
*env
, int id
)
443 kvm_vcpu_context_t vcpu_ctx
= qemu_malloc(sizeof(struct kvm_vcpu_context
));
445 r
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_VCPU
, id
);
447 fprintf(stderr
, "kvm_create_vcpu: %m\n");
453 env
->kvm_state
= kvm_state
;
455 mmap_size
= kvm_ioctl(kvm_state
, KVM_GET_VCPU_MMAP_SIZE
, 0);
457 fprintf(stderr
, "get vcpu mmap size: %m\n");
461 mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, vcpu_ctx
->fd
,
463 if (env
->kvm_run
== MAP_FAILED
) {
464 fprintf(stderr
, "mmap vcpu area: %m\n");
476 static int kvm_set_boot_vcpu_id(kvm_context_t kvm
, uint32_t id
)
478 #ifdef KVM_CAP_SET_BOOT_CPU_ID
479 int r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_BOOT_CPU_ID
);
481 return kvm_vm_ioctl(kvm_state
, KVM_SET_BOOT_CPU_ID
, id
);
488 int kvm_create_vm(kvm_context_t kvm
)
491 #ifdef KVM_CAP_IRQ_ROUTING
492 kvm
->irq_routes
= qemu_mallocz(sizeof(*kvm
->irq_routes
));
493 kvm
->nr_allocated_irq_routes
= 0;
496 fd
= kvm_ioctl(kvm_state
, KVM_CREATE_VM
, 0);
498 fprintf(stderr
, "kvm_create_vm: %m\n");
501 kvm_state
->vmfd
= fd
;
505 static int kvm_create_default_phys_mem(kvm_context_t kvm
,
506 unsigned long phys_mem_bytes
,
509 #ifdef KVM_CAP_USER_MEMORY
510 int r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
514 "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n");
516 #error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported
521 void kvm_create_irqchip(kvm_context_t kvm
)
525 kvm
->irqchip_in_kernel
= 0;
526 #ifdef KVM_CAP_IRQCHIP
527 if (!kvm
->no_irqchip_creation
) {
528 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_IRQCHIP
);
529 if (r
> 0) { /* kernel irqchip supported */
530 r
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_IRQCHIP
);
532 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
533 #if defined(KVM_CAP_IRQ_INJECT_STATUS) && defined(KVM_IRQ_LINE_STATUS)
534 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
,
535 KVM_CAP_IRQ_INJECT_STATUS
);
537 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
539 kvm
->irqchip_in_kernel
= 1;
541 fprintf(stderr
, "Create kernel PIC irqchip failed\n");
545 kvm_state
->irqchip_in_kernel
= kvm
->irqchip_in_kernel
;
548 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
, void **vm_mem
)
552 r
= kvm_create_vm(kvm
);
555 r
= kvm_arch_create(kvm
, phys_mem_bytes
, vm_mem
);
559 r
= kvm_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
562 kvm_create_irqchip(kvm
);
568 int kvm_register_phys_mem(kvm_context_t kvm
,
569 unsigned long phys_start
, void *userspace_addr
,
570 unsigned long len
, int log
)
573 struct kvm_userspace_memory_region memory
= {
575 .guest_phys_addr
= phys_start
,
576 .userspace_addr
= (unsigned long) (uintptr_t) userspace_addr
,
577 .flags
= log
? KVM_MEM_LOG_DIRTY_PAGES
: 0,
581 memory
.slot
= get_free_slot(kvm
);
583 ("memory: gpa: %llx, size: %llx, uaddr: %llx, slot: %x, flags: %lx\n",
584 memory
.guest_phys_addr
, memory
.memory_size
, memory
.userspace_addr
,
585 memory
.slot
, memory
.flags
);
586 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_USER_MEMORY_REGION
, &memory
);
588 fprintf(stderr
, "create_userspace_phys_mem: %s\n", strerror(-r
));
591 register_slot(memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
592 memory
.userspace_addr
, memory
.flags
);
597 /* destroy/free a whole slot.
598 * phys_start, len and slot are the params passed to kvm_create_phys_mem()
600 void kvm_destroy_phys_mem(kvm_context_t kvm
, unsigned long phys_start
,
605 struct kvm_userspace_memory_region memory
= {
607 .guest_phys_addr
= phys_start
,
612 slot
= get_slot(phys_start
);
614 if ((slot
>= KVM_MAX_NUM_MEM_REGIONS
) || (slot
== -1)) {
615 fprintf(stderr
, "BUG: %s: invalid parameters (slot=%d)\n", __FUNCTION__
,
619 if (phys_start
!= slots
[slot
].phys_addr
) {
621 "WARNING: %s: phys_start is 0x%lx expecting 0x%lx\n",
622 __FUNCTION__
, phys_start
, slots
[slot
].phys_addr
);
623 phys_start
= slots
[slot
].phys_addr
;
627 DPRINTF("slot %d start %llx len %llx flags %x\n",
628 memory
.slot
, memory
.guest_phys_addr
, memory
.memory_size
,
630 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_USER_MEMORY_REGION
, &memory
);
632 fprintf(stderr
, "destroy_userspace_phys_mem: %s", strerror(-r
));
636 free_slot(memory
.slot
);
639 void kvm_unregister_memory_area(kvm_context_t kvm
, uint64_t phys_addr
,
643 int slot
= get_container_slot(phys_addr
, size
);
646 DPRINTF("Unregistering memory region %llx (%lx)\n", phys_addr
, size
);
647 kvm_destroy_phys_mem(kvm
, phys_addr
, size
);
652 static int kvm_get_map(kvm_context_t kvm
, int ioctl_num
, int slot
, void *buf
)
655 struct kvm_dirty_log log
= {
659 log
.dirty_bitmap
= buf
;
661 r
= kvm_vm_ioctl(kvm_state
, ioctl_num
, &log
);
667 int kvm_get_dirty_pages(kvm_context_t kvm
, unsigned long phys_addr
, void *buf
)
671 slot
= get_slot(phys_addr
);
672 return kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, slot
, buf
);
675 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
676 unsigned long len
, void *opaque
,
677 int (*cb
)(unsigned long start
,
678 unsigned long len
, void *bitmap
,
683 unsigned long end_addr
= phys_addr
+ len
;
686 for (i
= 0; i
< KVM_MAX_NUM_MEM_REGIONS
; ++i
) {
687 if ((slots
[i
].len
&& (uint64_t) slots
[i
].phys_addr
>= phys_addr
)
688 && ((uint64_t) slots
[i
].phys_addr
+ slots
[i
].len
<= end_addr
)) {
689 buf
= qemu_malloc(BITMAP_SIZE(slots
[i
].len
));
690 r
= kvm_get_map(kvm
, KVM_GET_DIRTY_LOG
, i
, buf
);
695 r
= cb(slots
[i
].phys_addr
, slots
[i
].len
, buf
, opaque
);
704 #ifdef KVM_CAP_IRQCHIP
706 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
, int *status
)
708 struct kvm_irq_level event
;
711 if (!kvm
->irqchip_in_kernel
)
715 r
= kvm_vm_ioctl(kvm_state
, kvm
->irqchip_inject_ioctl
, &event
);
717 perror("kvm_set_irq_level");
720 #ifdef KVM_CAP_IRQ_INJECT_STATUS
722 (kvm
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
731 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
735 if (!kvm
->irqchip_in_kernel
)
737 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_IRQCHIP
, chip
);
739 perror("kvm_get_irqchip\n");
744 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
748 if (!kvm
->irqchip_in_kernel
)
750 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_IRQCHIP
, chip
);
752 perror("kvm_set_irqchip\n");
759 static int handle_io(CPUState
*env
)
761 struct kvm_run
*run
= env
->kvm_run
;
762 uint16_t addr
= run
->io
.port
;
764 void *p
= (void *) run
+ run
->io
.data_offset
;
766 for (i
= 0; i
< run
->io
.count
; ++i
) {
767 switch (run
->io
.direction
) {
769 switch (run
->io
.size
) {
771 *(uint8_t *) p
= cpu_inb(addr
);
774 *(uint16_t *) p
= cpu_inw(addr
);
777 *(uint32_t *) p
= cpu_inl(addr
);
780 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
784 case KVM_EXIT_IO_OUT
:
785 switch (run
->io
.size
) {
787 cpu_outb(addr
, *(uint8_t *) p
);
790 cpu_outw(addr
, *(uint16_t *) p
);
793 cpu_outl(addr
, *(uint32_t *) p
);
796 fprintf(stderr
, "bad I/O size %d\n", run
->io
.size
);
801 fprintf(stderr
, "bad I/O direction %d\n", run
->io
.direction
);
811 static int handle_debug(CPUState
*env
)
813 #ifdef KVM_CAP_SET_GUEST_DEBUG
814 struct kvm_run
*run
= env
->kvm_run
;
816 return kvm_debug(env
, &run
->debug
.arch
);
822 int kvm_get_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
)
824 return ioctl(vcpu
->fd
, KVM_GET_REGS
, regs
);
827 int kvm_set_regs(kvm_vcpu_context_t vcpu
, struct kvm_regs
*regs
)
829 return ioctl(vcpu
->fd
, KVM_SET_REGS
, regs
);
832 int kvm_get_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
)
834 return ioctl(vcpu
->fd
, KVM_GET_FPU
, fpu
);
837 int kvm_set_fpu(kvm_vcpu_context_t vcpu
, struct kvm_fpu
*fpu
)
839 return ioctl(vcpu
->fd
, KVM_SET_FPU
, fpu
);
842 int kvm_get_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*sregs
)
844 return ioctl(vcpu
->fd
, KVM_GET_SREGS
, sregs
);
847 int kvm_set_sregs(kvm_vcpu_context_t vcpu
, struct kvm_sregs
*sregs
)
849 return ioctl(vcpu
->fd
, KVM_SET_SREGS
, sregs
);
852 #ifdef KVM_CAP_MP_STATE
853 int kvm_get_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
)
857 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
859 return ioctl(vcpu
->fd
, KVM_GET_MP_STATE
, mp_state
);
863 int kvm_set_mpstate(kvm_vcpu_context_t vcpu
, struct kvm_mp_state
*mp_state
)
867 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
869 return ioctl(vcpu
->fd
, KVM_SET_MP_STATE
, mp_state
);
874 static int handle_mmio(CPUState
*env
)
876 unsigned long addr
= env
->kvm_run
->mmio
.phys_addr
;
877 struct kvm_run
*kvm_run
= env
->kvm_run
;
878 void *data
= kvm_run
->mmio
.data
;
880 /* hack: Red Hat 7.1 generates these weird accesses. */
881 if ((addr
> 0xa0000 - 4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
884 cpu_physical_memory_rw(addr
, data
, kvm_run
->mmio
.len
, kvm_run
->mmio
.is_write
);
888 int handle_io_window(kvm_context_t kvm
)
893 int handle_shutdown(kvm_context_t kvm
, CPUState
*env
)
895 /* stop the current vcpu from going back to guest mode */
898 qemu_system_reset_request();
902 static inline void push_nmi(kvm_context_t kvm
)
904 #ifdef KVM_CAP_USER_NMI
905 kvm_arch_push_nmi(kvm
->opaque
);
906 #endif /* KVM_CAP_USER_NMI */
909 void post_kvm_run(kvm_context_t kvm
, CPUState
*env
)
911 pthread_mutex_lock(&qemu_mutex
);
912 kvm_arch_post_kvm_run(kvm
->opaque
, env
);
915 int pre_kvm_run(kvm_context_t kvm
, CPUState
*env
)
917 kvm_arch_pre_kvm_run(kvm
->opaque
, env
);
919 pthread_mutex_unlock(&qemu_mutex
);
923 int kvm_get_interrupt_flag(CPUState
*env
)
925 return env
->kvm_run
->if_flag
;
928 int kvm_is_ready_for_interrupt_injection(CPUState
*env
)
930 return env
->kvm_run
->ready_for_interrupt_injection
;
933 int kvm_run(kvm_vcpu_context_t vcpu
, void *env
)
937 CPUState
*_env
= env
;
938 kvm_context_t kvm
= &_env
->kvm_state
->kvm_context
;
939 struct kvm_run
*run
= _env
->kvm_run
;
943 #if !defined(__s390__)
944 if (!kvm
->irqchip_in_kernel
)
945 run
->request_interrupt_window
= kvm_arch_try_push_interrupts(env
);
948 if (_env
->kvm_cpu_state
.regs_modified
) {
949 kvm_arch_put_registers(_env
);
950 _env
->kvm_cpu_state
.regs_modified
= 0;
953 r
= pre_kvm_run(kvm
, env
);
956 r
= ioctl(fd
, KVM_RUN
, 0);
958 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
960 post_kvm_run(kvm
, env
);
961 fprintf(stderr
, "kvm_run: %s\n", strerror(-r
));
965 post_kvm_run(kvm
, env
);
967 #if defined(KVM_CAP_COALESCED_MMIO)
968 if (kvm_state
->coalesced_mmio
) {
969 struct kvm_coalesced_mmio_ring
*ring
=
970 (void *) run
+ kvm_state
->coalesced_mmio
* PAGE_SIZE
;
971 while (ring
->first
!= ring
->last
) {
972 cpu_physical_memory_rw(ring
->coalesced_mmio
[ring
->first
].phys_addr
,
973 &ring
->coalesced_mmio
[ring
->first
].data
[0],
974 ring
->coalesced_mmio
[ring
->first
].len
, 1);
976 ring
->first
= (ring
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
981 #if !defined(__s390__)
983 r
= handle_io_window(kvm
);
988 switch (run
->exit_reason
) {
989 case KVM_EXIT_UNKNOWN
:
990 r
= handle_unhandled(run
->hw
.hardware_exit_reason
);
992 case KVM_EXIT_FAIL_ENTRY
:
993 r
= handle_unhandled(run
->fail_entry
.hardware_entry_failure_reason
);
995 case KVM_EXIT_EXCEPTION
:
996 fprintf(stderr
, "exception %d (%x)\n", run
->ex
.exception
,
1005 case KVM_EXIT_DEBUG
:
1006 r
= handle_debug(env
);
1009 r
= handle_mmio(env
);
1012 r
= kvm_arch_halt(vcpu
);
1014 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1016 case KVM_EXIT_SHUTDOWN
:
1017 r
= handle_shutdown(kvm
, env
);
1019 #if defined(__s390__)
1020 case KVM_EXIT_S390_SIEIC
:
1021 r
= kvm_s390_handle_intercept(kvm
, vcpu
, run
);
1023 case KVM_EXIT_S390_RESET
:
1024 r
= kvm_s390_handle_reset(kvm
, vcpu
, run
);
1027 case KVM_EXIT_INTERNAL_ERROR
:
1028 fprintf(stderr
, "KVM internal error. Suberror: %d\n",
1029 run
->internal
.suberror
);
1030 kvm_show_regs(vcpu
);
1031 if (run
->internal
.suberror
== KVM_INTERNAL_ERROR_EMULATION
)
1032 fprintf(stderr
, "emulation failure, check dmesg for details\n");
1036 if (kvm_arch_run(env
)) {
1037 fprintf(stderr
, "unhandled vm exit: 0x%x\n", run
->exit_reason
);
1038 kvm_show_regs(vcpu
);
1050 int kvm_inject_irq(kvm_vcpu_context_t vcpu
, unsigned irq
)
1052 struct kvm_interrupt intr
;
1055 return ioctl(vcpu
->fd
, KVM_INTERRUPT
, &intr
);
1058 #ifdef KVM_CAP_SET_GUEST_DEBUG
1059 int kvm_set_guest_debug(kvm_vcpu_context_t vcpu
, struct kvm_guest_debug
*dbg
)
1061 return ioctl(vcpu
->fd
, KVM_SET_GUEST_DEBUG
, dbg
);
1065 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu
, const sigset_t
*sigset
)
1067 struct kvm_signal_mask
*sigmask
;
1071 r
= ioctl(vcpu
->fd
, KVM_SET_SIGNAL_MASK
, NULL
);
1076 sigmask
= qemu_malloc(sizeof(*sigmask
) + sizeof(*sigset
));
1079 memcpy(sigmask
->sigset
, sigset
, sizeof(*sigset
));
1080 r
= ioctl(vcpu
->fd
, KVM_SET_SIGNAL_MASK
, sigmask
);
1087 int kvm_pit_in_kernel(kvm_context_t kvm
)
1089 return kvm
->pit_in_kernel
;
1092 int kvm_inject_nmi(kvm_vcpu_context_t vcpu
)
1094 #ifdef KVM_CAP_USER_NMI
1095 return ioctl(vcpu
->fd
, KVM_NMI
);
1101 int kvm_init_coalesced_mmio(kvm_context_t kvm
)
1104 kvm_state
->coalesced_mmio
= 0;
1105 #ifdef KVM_CAP_COALESCED_MMIO
1106 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
1108 kvm_state
->coalesced_mmio
= r
;
1115 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1116 int kvm_assign_pci_device(kvm_context_t kvm
,
1117 struct kvm_assigned_pci_dev
*assigned_dev
)
1119 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
1122 static int kvm_old_assign_irq(kvm_context_t kvm
,
1123 struct kvm_assigned_irq
*assigned_irq
)
1125 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_IRQ
, assigned_irq
);
1128 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
1129 int kvm_assign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
)
1133 ret
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_ASSIGN_DEV_IRQ
);
1135 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_DEV_IRQ
, assigned_irq
);
1138 return kvm_old_assign_irq(kvm
, assigned_irq
);
1141 int kvm_deassign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
)
1143 return kvm_vm_ioctl(kvm_state
, KVM_DEASSIGN_DEV_IRQ
, assigned_irq
);
1146 int kvm_assign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
)
1148 return kvm_old_assign_irq(kvm
, assigned_irq
);
1153 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1154 int kvm_deassign_pci_device(kvm_context_t kvm
,
1155 struct kvm_assigned_pci_dev
*assigned_dev
)
1157 return kvm_vm_ioctl(kvm_state
, KVM_DEASSIGN_PCI_DEVICE
, assigned_dev
);
1161 int kvm_destroy_memory_region_works(kvm_context_t kvm
)
1165 #ifdef KVM_CAP_DESTROY_MEMORY_REGION_WORKS
1167 kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
,
1168 KVM_CAP_DESTROY_MEMORY_REGION_WORKS
);
1175 int kvm_reinject_control(kvm_context_t kvm
, int pit_reinject
)
1177 #ifdef KVM_CAP_REINJECT_CONTROL
1179 struct kvm_reinject_control control
;
1181 control
.pit_reinject
= pit_reinject
;
1183 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_REINJECT_CONTROL
);
1185 return kvm_vm_ioctl(kvm_state
, KVM_REINJECT_CONTROL
, &control
);
1191 int kvm_has_gsi_routing(kvm_context_t kvm
)
1195 #ifdef KVM_CAP_IRQ_ROUTING
1196 r
= kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1201 int kvm_get_gsi_count(kvm_context_t kvm
)
1203 #ifdef KVM_CAP_IRQ_ROUTING
1204 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
1210 int kvm_clear_gsi_routes(kvm_context_t kvm
)
1212 #ifdef KVM_CAP_IRQ_ROUTING
1213 kvm
->irq_routes
->nr
= 0;
1220 int kvm_add_routing_entry(kvm_context_t kvm
,
1221 struct kvm_irq_routing_entry
*entry
)
1223 #ifdef KVM_CAP_IRQ_ROUTING
1224 struct kvm_irq_routing
*z
;
1225 struct kvm_irq_routing_entry
*new;
1228 if (kvm
->irq_routes
->nr
== kvm
->nr_allocated_irq_routes
) {
1229 n
= kvm
->nr_allocated_irq_routes
* 2;
1232 size
= sizeof(struct kvm_irq_routing
);
1233 size
+= n
* sizeof(*new);
1234 z
= realloc(kvm
->irq_routes
, size
);
1237 kvm
->nr_allocated_irq_routes
= n
;
1238 kvm
->irq_routes
= z
;
1240 n
= kvm
->irq_routes
->nr
++;
1241 new = &kvm
->irq_routes
->entries
[n
];
1242 memset(new, 0, sizeof(*new));
1243 new->gsi
= entry
->gsi
;
1244 new->type
= entry
->type
;
1245 new->flags
= entry
->flags
;
1248 set_gsi(kvm
, entry
->gsi
);
1256 int kvm_add_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1258 #ifdef KVM_CAP_IRQ_ROUTING
1259 struct kvm_irq_routing_entry e
;
1262 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1264 e
.u
.irqchip
.irqchip
= irqchip
;
1265 e
.u
.irqchip
.pin
= pin
;
1266 return kvm_add_routing_entry(kvm
, &e
);
1272 int kvm_del_routing_entry(kvm_context_t kvm
,
1273 struct kvm_irq_routing_entry
*entry
)
1275 #ifdef KVM_CAP_IRQ_ROUTING
1276 struct kvm_irq_routing_entry
*e
, *p
;
1277 int i
, gsi
, found
= 0;
1281 for (i
= 0; i
< kvm
->irq_routes
->nr
; ++i
) {
1282 e
= &kvm
->irq_routes
->entries
[i
];
1283 if (e
->type
== entry
->type
&& e
->gsi
== gsi
) {
1285 case KVM_IRQ_ROUTING_IRQCHIP
:{
1286 if (e
->u
.irqchip
.irqchip
==
1287 entry
->u
.irqchip
.irqchip
1288 && e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
1289 p
= &kvm
->irq_routes
->entries
[--kvm
->irq_routes
->nr
];
1295 case KVM_IRQ_ROUTING_MSI
:{
1296 if (e
->u
.msi
.address_lo
==
1297 entry
->u
.msi
.address_lo
1298 && e
->u
.msi
.address_hi
==
1299 entry
->u
.msi
.address_hi
1300 && e
->u
.msi
.data
== entry
->u
.msi
.data
) {
1301 p
= &kvm
->irq_routes
->entries
[--kvm
->irq_routes
->nr
];
1311 /* If there are no other users of this GSI
1312 * mark it available in the bitmap */
1313 for (i
= 0; i
< kvm
->irq_routes
->nr
; i
++) {
1314 e
= &kvm
->irq_routes
->entries
[i
];
1318 if (i
== kvm
->irq_routes
->nr
)
1319 clear_gsi(kvm
, gsi
);
1331 int kvm_update_routing_entry(kvm_context_t kvm
,
1332 struct kvm_irq_routing_entry
*entry
,
1333 struct kvm_irq_routing_entry
*newentry
)
1335 #ifdef KVM_CAP_IRQ_ROUTING
1336 struct kvm_irq_routing_entry
*e
;
1339 if (entry
->gsi
!= newentry
->gsi
|| entry
->type
!= newentry
->type
) {
1343 for (i
= 0; i
< kvm
->irq_routes
->nr
; ++i
) {
1344 e
= &kvm
->irq_routes
->entries
[i
];
1345 if (e
->type
!= entry
->type
|| e
->gsi
!= entry
->gsi
) {
1349 case KVM_IRQ_ROUTING_IRQCHIP
:
1350 if (e
->u
.irqchip
.irqchip
== entry
->u
.irqchip
.irqchip
&&
1351 e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
1352 memcpy(&e
->u
.irqchip
, &newentry
->u
.irqchip
,
1353 sizeof e
->u
.irqchip
);
1357 case KVM_IRQ_ROUTING_MSI
:
1358 if (e
->u
.msi
.address_lo
== entry
->u
.msi
.address_lo
&&
1359 e
->u
.msi
.address_hi
== entry
->u
.msi
.address_hi
&&
1360 e
->u
.msi
.data
== entry
->u
.msi
.data
) {
1361 memcpy(&e
->u
.msi
, &newentry
->u
.msi
, sizeof e
->u
.msi
);
1375 int kvm_del_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
1377 #ifdef KVM_CAP_IRQ_ROUTING
1378 struct kvm_irq_routing_entry e
;
1381 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
1383 e
.u
.irqchip
.irqchip
= irqchip
;
1384 e
.u
.irqchip
.pin
= pin
;
1385 return kvm_del_routing_entry(kvm
, &e
);
1391 int kvm_commit_irq_routes(kvm_context_t kvm
)
1393 #ifdef KVM_CAP_IRQ_ROUTING
1394 kvm
->irq_routes
->flags
= 0;
1395 return kvm_vm_ioctl(kvm_state
, KVM_SET_GSI_ROUTING
, kvm
->irq_routes
);
1401 int kvm_get_irq_route_gsi(kvm_context_t kvm
)
1404 uint32_t *buf
= kvm
->used_gsi_bitmap
;
1406 /* Return the lowest unused GSI in the bitmap */
1407 for (i
= 0; i
< kvm
->max_gsi
/ 32; i
++) {
1412 return bit
- 1 + i
* 32;
1418 #ifdef KVM_CAP_DEVICE_MSIX
1419 int kvm_assign_set_msix_nr(kvm_context_t kvm
,
1420 struct kvm_assigned_msix_nr
*msix_nr
)
1422 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_SET_MSIX_NR
, msix_nr
);
1425 int kvm_assign_set_msix_entry(kvm_context_t kvm
,
1426 struct kvm_assigned_msix_entry
*entry
)
1428 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_SET_MSIX_ENTRY
, entry
);
1432 #if defined(KVM_CAP_IRQFD) && defined(CONFIG_EVENTFD)
1434 #include <sys/eventfd.h>
1436 static int _kvm_irqfd(kvm_context_t kvm
, int fd
, int gsi
, int flags
)
1438 struct kvm_irqfd data
= {
1444 return kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &data
);
1447 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1452 if (!kvm_check_extension(kvm_state
, KVM_CAP_IRQFD
))
1459 r
= _kvm_irqfd(kvm
, fd
, gsi
, 0);
1468 #else /* KVM_CAP_IRQFD */
1470 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1475 #endif /* KVM_CAP_IRQFD */
1476 static inline unsigned long kvm_get_thread_id(void)
1478 return syscall(SYS_gettid
);
1481 static void qemu_cond_wait(pthread_cond_t
*cond
)
1483 CPUState
*env
= cpu_single_env
;
1485 pthread_cond_wait(cond
, &qemu_mutex
);
1486 cpu_single_env
= env
;
1489 static void sig_ipi_handler(int n
)
1493 static void hardware_memory_error(void)
1495 fprintf(stderr
, "Hardware memory error!\n");
1499 static void sigbus_reraise(void)
1502 struct sigaction action
;
1504 memset(&action
, 0, sizeof(action
));
1505 action
.sa_handler
= SIG_DFL
;
1506 if (!sigaction(SIGBUS
, &action
, NULL
)) {
1509 sigaddset(&set
, SIGBUS
);
1510 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
1512 perror("Failed to re-raise SIGBUS!\n");
1516 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
1519 #if defined(KVM_CAP_MCE) && defined(TARGET_I386)
1520 if (first_cpu
->mcg_cap
&& siginfo
->ssi_addr
1521 && siginfo
->ssi_code
== BUS_MCEERR_AO
) {
1523 unsigned long paddr
;
1526 /* Hope we are lucky for AO MCE */
1527 if (do_qemu_ram_addr_from_host((void *)(intptr_t)siginfo
->ssi_addr
,
1529 fprintf(stderr
, "Hardware memory error for memory used by "
1530 "QEMU itself instead of guest system!: %llx\n",
1531 (unsigned long long)siginfo
->ssi_addr
);
1534 status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1535 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1537 kvm_inject_x86_mce(first_cpu
, 9, status
,
1538 MCG_STATUS_MCIP
| MCG_STATUS_RIPV
, paddr
,
1539 (MCM_ADDR_PHYS
<< 6) | 0xc, 1);
1540 for (cenv
= first_cpu
->next_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1541 kvm_inject_x86_mce(cenv
, 1, MCI_STATUS_VAL
| MCI_STATUS_UC
,
1542 MCG_STATUS_MCIP
| MCG_STATUS_RIPV
, 0, 0, 1);
1546 if (siginfo
->ssi_code
== BUS_MCEERR_AO
)
1548 else if (siginfo
->ssi_code
== BUS_MCEERR_AR
)
1549 hardware_memory_error();
1555 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
1557 struct qemu_work_item wi
;
1559 if (env
== current_env
) {
1566 if (!env
->kvm_cpu_state
.queued_work_first
)
1567 env
->kvm_cpu_state
.queued_work_first
= &wi
;
1569 env
->kvm_cpu_state
.queued_work_last
->next
= &wi
;
1570 env
->kvm_cpu_state
.queued_work_last
= &wi
;
1574 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1576 qemu_cond_wait(&qemu_work_cond
);
1579 void kvm_arch_get_registers(CPUState
*env
)
1581 kvm_arch_save_regs(env
);
1584 static void do_kvm_cpu_synchronize_state(void *_env
)
1586 CPUState
*env
= _env
;
1587 if (!env
->kvm_cpu_state
.regs_modified
) {
1588 kvm_arch_get_registers(env
);
1589 env
->kvm_cpu_state
.regs_modified
= 1;
1593 void kvm_cpu_synchronize_state(CPUState
*env
)
1595 if (!env
->kvm_cpu_state
.regs_modified
)
1596 on_vcpu(env
, do_kvm_cpu_synchronize_state
, env
);
1599 static void inject_interrupt(void *data
)
1601 cpu_interrupt(current_env
, (long) data
);
1604 void kvm_inject_interrupt(CPUState
*env
, int mask
)
1606 on_vcpu(env
, inject_interrupt
, (void *) (long) mask
);
1609 void kvm_update_interrupt_request(CPUState
*env
)
1614 if (!current_env
|| !current_env
->created
)
1617 * Testing for created here is really redundant
1619 if (current_env
&& current_env
->created
&&
1620 env
!= current_env
&& !env
->kvm_cpu_state
.signalled
)
1624 env
->kvm_cpu_state
.signalled
= 1;
1625 if (env
->kvm_cpu_state
.thread
)
1626 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1631 static void kvm_do_load_registers(void *_env
)
1633 CPUState
*env
= _env
;
1635 kvm_arch_load_regs(env
);
1638 void kvm_load_registers(CPUState
*env
)
1640 if (kvm_enabled() && qemu_system_ready
)
1641 on_vcpu(env
, kvm_do_load_registers
, env
);
1644 static void kvm_do_save_registers(void *_env
)
1646 CPUState
*env
= _env
;
1648 kvm_arch_save_regs(env
);
1651 void kvm_save_registers(CPUState
*env
)
1654 on_vcpu(env
, kvm_do_save_registers
, env
);
1657 static void kvm_do_load_mpstate(void *_env
)
1659 CPUState
*env
= _env
;
1661 kvm_arch_load_mpstate(env
);
1664 void kvm_load_mpstate(CPUState
*env
)
1666 if (kvm_enabled() && qemu_system_ready
)
1667 on_vcpu(env
, kvm_do_load_mpstate
, env
);
1670 static void kvm_do_save_mpstate(void *_env
)
1672 CPUState
*env
= _env
;
1674 kvm_arch_save_mpstate(env
);
1675 #ifdef KVM_CAP_MP_STATE
1676 if (kvm_irqchip_in_kernel())
1677 env
->halted
= (env
->mp_state
== KVM_MP_STATE_HALTED
);
1681 void kvm_save_mpstate(CPUState
*env
)
1684 on_vcpu(env
, kvm_do_save_mpstate
, env
);
1687 int kvm_cpu_exec(CPUState
*env
)
1691 r
= kvm_run(env
->kvm_cpu_state
.vcpu_ctx
, env
);
1693 printf("kvm_run returned %d\n", r
);
1700 static int is_cpu_stopped(CPUState
*env
)
1702 return !vm_running
|| env
->stopped
;
1705 static void flush_queued_work(CPUState
*env
)
1707 struct qemu_work_item
*wi
;
1709 if (!env
->kvm_cpu_state
.queued_work_first
)
1712 while ((wi
= env
->kvm_cpu_state
.queued_work_first
)) {
1713 env
->kvm_cpu_state
.queued_work_first
= wi
->next
;
1717 env
->kvm_cpu_state
.queued_work_last
= NULL
;
1718 pthread_cond_broadcast(&qemu_work_cond
);
1721 static void kvm_on_sigbus(CPUState
*env
, siginfo_t
*siginfo
)
1723 #if defined(KVM_CAP_MCE) && defined(TARGET_I386)
1724 struct kvm_x86_mce mce
= {
1727 unsigned long paddr
;
1730 if (env
->mcg_cap
&& siginfo
->si_addr
1731 && (siginfo
->si_code
== BUS_MCEERR_AR
1732 || siginfo
->si_code
== BUS_MCEERR_AO
)) {
1733 if (siginfo
->si_code
== BUS_MCEERR_AR
) {
1734 /* Fake an Intel architectural Data Load SRAR UCR */
1735 mce
.status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1736 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1737 | MCI_STATUS_AR
| 0x134;
1738 mce
.misc
= (MCM_ADDR_PHYS
<< 6) | 0xc;
1739 mce
.mcg_status
= MCG_STATUS_MCIP
| MCG_STATUS_EIPV
;
1741 /* Fake an Intel architectural Memory scrubbing UCR */
1742 mce
.status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1743 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1745 mce
.misc
= (MCM_ADDR_PHYS
<< 6) | 0xc;
1746 mce
.mcg_status
= MCG_STATUS_MCIP
| MCG_STATUS_RIPV
;
1748 if (do_qemu_ram_addr_from_host((void *)siginfo
->si_addr
, &paddr
)) {
1749 fprintf(stderr
, "Hardware memory error for memory used by "
1750 "QEMU itself instaed of guest system!\n");
1751 /* Hope we are lucky for AO MCE */
1752 if (siginfo
->si_code
== BUS_MCEERR_AO
)
1755 hardware_memory_error();
1758 r
= kvm_set_mce(env
->kvm_cpu_state
.vcpu_ctx
, &mce
);
1760 fprintf(stderr
, "kvm_set_mce: %s\n", strerror(errno
));
1766 if (siginfo
->si_code
== BUS_MCEERR_AO
)
1768 else if (siginfo
->si_code
== BUS_MCEERR_AR
)
1769 hardware_memory_error();
1775 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
1783 ts
.tv_sec
= timeout
/ 1000;
1784 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
1785 sigemptyset(&waitset
);
1786 sigaddset(&waitset
, SIG_IPI
);
1787 sigaddset(&waitset
, SIGBUS
);
1790 pthread_mutex_unlock(&qemu_mutex
);
1792 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
1795 pthread_mutex_lock(&qemu_mutex
);
1797 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
1798 printf("sigtimedwait: %s\n", strerror(e
));
1804 kvm_on_sigbus(env
, &siginfo
);
1810 r
= sigpending(&chkset
);
1812 printf("sigpending: %s\n", strerror(e
));
1815 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
1817 cpu_single_env
= env
;
1818 flush_queued_work(env
);
1823 pthread_cond_signal(&qemu_pause_cond
);
1826 env
->kvm_cpu_state
.signalled
= 0;
1829 static int all_threads_paused(void)
1831 CPUState
*penv
= first_cpu
;
1836 penv
= (CPUState
*) penv
->next_cpu
;
1842 static void pause_all_threads(void)
1844 CPUState
*penv
= first_cpu
;
1847 if (penv
!= cpu_single_env
) {
1849 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1855 penv
= (CPUState
*) penv
->next_cpu
;
1858 while (!all_threads_paused())
1859 qemu_cond_wait(&qemu_pause_cond
);
1862 static void resume_all_threads(void)
1864 CPUState
*penv
= first_cpu
;
1866 assert(!cpu_single_env
);
1871 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1872 penv
= (CPUState
*) penv
->next_cpu
;
1876 static void kvm_vm_state_change_handler(void *context
, int running
, int reason
)
1879 resume_all_threads();
1881 pause_all_threads();
1884 static void setup_kernel_sigmask(CPUState
*env
)
1889 sigaddset(&set
, SIGUSR2
);
1890 sigaddset(&set
, SIGIO
);
1891 sigaddset(&set
, SIGALRM
);
1892 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1894 sigprocmask(SIG_BLOCK
, NULL
, &set
);
1895 sigdelset(&set
, SIG_IPI
);
1896 sigdelset(&set
, SIGBUS
);
1898 kvm_set_signal_mask(env
->kvm_cpu_state
.vcpu_ctx
, &set
);
1901 static void qemu_kvm_system_reset(void)
1903 CPUState
*penv
= first_cpu
;
1905 pause_all_threads();
1907 qemu_system_reset();
1910 kvm_arch_cpu_reset(penv
);
1911 penv
= (CPUState
*) penv
->next_cpu
;
1914 resume_all_threads();
1917 static void process_irqchip_events(CPUState
*env
)
1919 kvm_arch_process_irqchip_events(env
);
1920 if (kvm_arch_has_work(env
))
1924 static int kvm_main_loop_cpu(CPUState
*env
)
1927 int run_cpu
= !is_cpu_stopped(env
);
1928 if (run_cpu
&& !kvm_irqchip_in_kernel()) {
1929 process_irqchip_events(env
);
1930 run_cpu
= !env
->halted
;
1933 kvm_main_loop_wait(env
, 0);
1936 kvm_main_loop_wait(env
, 1000);
1939 pthread_mutex_unlock(&qemu_mutex
);
1943 static void *ap_main_loop(void *_env
)
1945 CPUState
*env
= _env
;
1947 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1948 struct ioperm_data
*data
= NULL
;
1952 env
->thread_id
= kvm_get_thread_id();
1953 sigfillset(&signals
);
1954 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
1955 env
->kvm_cpu_state
.vcpu_ctx
= kvm_create_vcpu(env
, env
->cpu_index
);
1957 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1958 /* do ioperm for io ports of assigned devices */
1959 QLIST_FOREACH(data
, &ioperm_head
, entries
)
1960 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
1963 setup_kernel_sigmask(env
);
1965 pthread_mutex_lock(&qemu_mutex
);
1966 cpu_single_env
= env
;
1968 kvm_arch_init_vcpu(env
);
1970 kvm_tpr_vcpu_start(env
);
1973 kvm_arch_load_regs(env
);
1975 /* signal VCPU creation */
1976 current_env
->created
= 1;
1977 pthread_cond_signal(&qemu_vcpu_cond
);
1979 /* and wait for machine initialization */
1980 while (!qemu_system_ready
)
1981 qemu_cond_wait(&qemu_system_cond
);
1983 /* re-initialize cpu_single_env after re-acquiring qemu_mutex */
1984 cpu_single_env
= env
;
1986 kvm_main_loop_cpu(env
);
1990 void kvm_init_vcpu(CPUState
*env
)
1992 pthread_create(&env
->kvm_cpu_state
.thread
, NULL
, ap_main_loop
, env
);
1994 while (env
->created
== 0)
1995 qemu_cond_wait(&qemu_vcpu_cond
);
1998 int kvm_vcpu_inited(CPUState
*env
)
2000 return env
->created
;
2004 void kvm_hpet_disable_kpit(void)
2006 struct kvm_pit_state2 ps2
;
2008 kvm_get_pit2(kvm_context
, &ps2
);
2009 ps2
.flags
|= KVM_PIT_FLAGS_HPET_LEGACY
;
2010 kvm_set_pit2(kvm_context
, &ps2
);
2013 void kvm_hpet_enable_kpit(void)
2015 struct kvm_pit_state2 ps2
;
2017 kvm_get_pit2(kvm_context
, &ps2
);
2018 ps2
.flags
&= ~KVM_PIT_FLAGS_HPET_LEGACY
;
2019 kvm_set_pit2(kvm_context
, &ps2
);
2023 int kvm_init_ap(void)
2025 struct sigaction action
;
2028 kvm_tpr_opt_setup();
2030 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
2032 signal(SIG_IPI
, sig_ipi_handler
);
2034 memset(&action
, 0, sizeof(action
));
2035 action
.sa_flags
= SA_SIGINFO
;
2036 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
2037 sigaction(SIGBUS
, &action
, NULL
);
2038 prctl(PR_MCE_KILL
, 1, 1);
2042 void qemu_kvm_notify_work(void)
2048 if (io_thread_fd
== -1)
2051 memcpy(buffer
, &value
, sizeof(value
));
2053 while (offset
< 8) {
2056 len
= write(io_thread_fd
, buffer
+ offset
, 8 - offset
);
2057 if (len
== -1 && errno
== EINTR
)
2060 /* In case we have a pipe, there is not reason to insist writing
2063 if (len
== -1 && errno
== EAGAIN
)
2073 /* If we have signalfd, we mask out the signals we want to handle and then
2074 * use signalfd to listen for them. We rely on whatever the current signal
2075 * handler is to dispatch the signals when we receive them.
2078 static void sigfd_handler(void *opaque
)
2080 int fd
= (unsigned long) opaque
;
2081 struct qemu_signalfd_siginfo info
;
2082 struct sigaction action
;
2087 len
= read(fd
, &info
, sizeof(info
));
2088 } while (len
== -1 && errno
== EINTR
);
2090 if (len
== -1 && errno
== EAGAIN
)
2093 if (len
!= sizeof(info
)) {
2094 printf("read from sigfd returned %zd: %m\n", len
);
2098 sigaction(info
.ssi_signo
, NULL
, &action
);
2099 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
)
2100 action
.sa_sigaction(info
.ssi_signo
,
2101 (siginfo_t
*)&info
, NULL
);
2102 else if (action
.sa_handler
)
2103 action
.sa_handler(info
.ssi_signo
);
2108 /* Used to break IO thread out of select */
2109 static void io_thread_wakeup(void *opaque
)
2111 int fd
= (unsigned long) opaque
;
2114 /* Drain the pipe/(eventfd) */
2118 len
= read(fd
, buffer
, sizeof(buffer
));
2119 if (len
== -1 && errno
== EINTR
)
2127 int kvm_main_loop(void)
2133 io_thread
= pthread_self();
2134 qemu_system_ready
= 1;
2136 if (qemu_eventfd(fds
) == -1) {
2137 fprintf(stderr
, "failed to create eventfd\n");
2141 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
2142 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
2144 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
2145 (void *)(unsigned long) fds
[0]);
2147 io_thread_fd
= fds
[1];
2150 sigaddset(&mask
, SIGIO
);
2151 sigaddset(&mask
, SIGALRM
);
2152 sigaddset(&mask
, SIGBUS
);
2153 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
2155 sigfd
= qemu_signalfd(&mask
);
2157 fprintf(stderr
, "failed to create signalfd\n");
2161 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
2163 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
2164 (void *)(unsigned long) sigfd
);
2166 pthread_cond_broadcast(&qemu_system_cond
);
2168 io_thread_sigfd
= sigfd
;
2169 cpu_single_env
= NULL
;
2172 main_loop_wait(1000);
2173 if (qemu_shutdown_requested()) {
2174 if (qemu_no_shutdown()) {
2178 } else if (qemu_powerdown_requested())
2179 qemu_irq_raise(qemu_system_powerdown
);
2180 else if (qemu_reset_requested())
2181 qemu_kvm_system_reset();
2182 else if (kvm_debug_cpu_requested
) {
2183 gdb_set_stop_cpu(kvm_debug_cpu_requested
);
2184 vm_stop(EXCP_DEBUG
);
2185 kvm_debug_cpu_requested
= NULL
;
2189 pause_all_threads();
2190 pthread_mutex_unlock(&qemu_mutex
);
2196 static int destroy_region_works
= 0;
2200 #if !defined(TARGET_I386)
2201 int kvm_arch_init_irq_routing(void)
2209 static int kvm_create_context(void)
2214 kvm_disable_irqchip_creation(kvm_context
);
2217 kvm_disable_pit_creation(kvm_context
);
2219 if (kvm_create(kvm_context
, 0, NULL
) < 0) {
2220 kvm_finalize(kvm_state
);
2223 r
= kvm_arch_qemu_create_context();
2225 kvm_finalize(kvm_state
);
2226 if (kvm_pit
&& !kvm_pit_reinject
) {
2227 if (kvm_reinject_control(kvm_context
, 0)) {
2228 fprintf(stderr
, "failure to disable in-kernel PIT reinjection\n");
2233 destroy_region_works
= kvm_destroy_memory_region_works(kvm_context
);
2236 r
= kvm_arch_init_irq_routing();
2243 if (!qemu_kvm_has_gsi_routing()) {
2246 /* if kernel can't do irq routing, interrupt source
2247 * override 0->2 can not be set up as required by hpet,
2251 } else if (!qemu_kvm_has_pit_state2()) {
2263 static int must_use_aliases_source(target_phys_addr_t addr
)
2265 if (destroy_region_works
)
2267 if (addr
== 0xa0000 || addr
== 0xa8000)
2272 static int must_use_aliases_target(target_phys_addr_t addr
)
2274 if (destroy_region_works
)
2276 if (addr
>= 0xe0000000 && addr
< 0x100000000ull
)
2281 static struct mapping
{
2282 target_phys_addr_t phys
;
2286 static int nr_mappings
;
2288 static struct mapping
*find_ram_mapping(ram_addr_t ram_addr
)
2292 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
2293 if (p
->ram
<= ram_addr
&& ram_addr
< p
->ram
+ p
->len
) {
2300 static struct mapping
*find_mapping(target_phys_addr_t start_addr
)
2304 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
2305 if (p
->phys
<= start_addr
&& start_addr
< p
->phys
+ p
->len
) {
2312 static void drop_mapping(target_phys_addr_t start_addr
)
2314 struct mapping
*p
= find_mapping(start_addr
);
2317 *p
= mappings
[--nr_mappings
];
2321 void kvm_set_phys_mem(target_phys_addr_t start_addr
, ram_addr_t size
,
2322 ram_addr_t phys_offset
)
2325 unsigned long area_flags
;
2330 if (start_addr
+ size
> phys_ram_size
) {
2331 phys_ram_size
= start_addr
+ size
;
2334 phys_offset
&= ~IO_MEM_ROM
;
2335 area_flags
= phys_offset
& ~TARGET_PAGE_MASK
;
2337 if (area_flags
!= IO_MEM_RAM
) {
2339 if (must_use_aliases_source(start_addr
)) {
2340 kvm_destroy_memory_alias(kvm_context
, start_addr
);
2343 if (must_use_aliases_target(start_addr
))
2347 p
= find_mapping(start_addr
);
2349 kvm_unregister_memory_area(kvm_context
, p
->phys
, p
->len
);
2350 drop_mapping(p
->phys
);
2352 start_addr
+= TARGET_PAGE_SIZE
;
2353 if (size
> TARGET_PAGE_SIZE
) {
2354 size
-= TARGET_PAGE_SIZE
;
2362 r
= kvm_is_containing_region(kvm_context
, start_addr
, size
);
2366 if (area_flags
>= TLB_MMIO
)
2370 if (must_use_aliases_source(start_addr
)) {
2371 p
= find_ram_mapping(phys_offset
);
2373 kvm_create_memory_alias(kvm_context
, start_addr
, size
,
2374 p
->phys
+ (phys_offset
- p
->ram
));
2380 r
= kvm_register_phys_mem(kvm_context
, start_addr
,
2381 qemu_get_ram_ptr(phys_offset
), size
, 0);
2383 printf("kvm_cpu_register_physical_memory: failed\n");
2387 drop_mapping(start_addr
);
2388 p
= &mappings
[nr_mappings
++];
2389 p
->phys
= start_addr
;
2390 p
->ram
= phys_offset
;
2397 int kvm_setup_guest_memory(void *area
, unsigned long size
)
2401 #ifdef MADV_DONTFORK
2402 if (kvm_enabled() && !kvm_has_sync_mmu())
2403 ret
= madvise(area
, size
, MADV_DONTFORK
);
2412 #ifdef KVM_CAP_SET_GUEST_DEBUG
2414 struct kvm_set_guest_debug_data
{
2415 struct kvm_guest_debug dbg
;
2419 static void kvm_invoke_set_guest_debug(void *data
)
2421 struct kvm_set_guest_debug_data
*dbg_data
= data
;
2423 if (cpu_single_env
->kvm_cpu_state
.regs_modified
) {
2424 kvm_arch_put_registers(cpu_single_env
);
2425 cpu_single_env
->kvm_cpu_state
.regs_modified
= 0;
2428 kvm_set_guest_debug(cpu_single_env
->kvm_cpu_state
.vcpu_ctx
,
2432 int kvm_update_guest_debug(CPUState
*env
, unsigned long reinject_trap
)
2434 struct kvm_set_guest_debug_data data
;
2436 data
.dbg
.control
= 0;
2437 if (env
->singlestep_enabled
)
2438 data
.dbg
.control
= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
2440 kvm_arch_update_guest_debug(env
, &data
.dbg
);
2441 data
.dbg
.control
|= reinject_trap
;
2443 on_vcpu(env
, kvm_invoke_set_guest_debug
, &data
);
2450 * dirty pages logging
2452 /* FIXME: use unsigned long pointer instead of unsigned char */
2453 unsigned char *kvm_dirty_bitmap
= NULL
;
2454 int kvm_physical_memory_set_dirty_tracking(int enable
)
2462 if (!kvm_dirty_bitmap
) {
2463 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
2464 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
2465 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
2468 if (kvm_dirty_bitmap
) {
2469 r
= kvm_dirty_pages_log_reset(kvm_context
);
2470 qemu_free(kvm_dirty_bitmap
);
2471 kvm_dirty_bitmap
= NULL
;
2477 /* get kvm's dirty pages bitmap and update qemu's */
2478 static int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
2479 unsigned char *bitmap
,
2480 unsigned long offset
,
2481 unsigned long mem_size
)
2483 unsigned int i
, j
, n
= 0;
2485 unsigned long page_number
, addr
, addr1
;
2486 ram_addr_t ram_addr
;
2487 unsigned int len
= ((mem_size
/ TARGET_PAGE_SIZE
) + 7) / 8;
2490 * bitmap-traveling is faster than memory-traveling (for addr...)
2491 * especially when most of the memory is not dirty.
2493 for (i
= 0; i
< len
; i
++) {
2498 page_number
= i
* 8 + j
;
2499 addr1
= page_number
* TARGET_PAGE_SIZE
;
2500 addr
= offset
+ addr1
;
2501 ram_addr
= cpu_get_physical_page_desc(addr
);
2502 cpu_physical_memory_set_dirty(ram_addr
);
2509 static int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
2510 void *bitmap
, void *opaque
)
2512 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
2516 * get kvm's dirty pages bitmap and update qemu's
2517 * we only care about physical ram, which resides in slots 0 and 3
2519 int kvm_update_dirty_pages_log(void)
2524 r
= kvm_get_dirty_pages_range(kvm_context
, 0, -1UL, NULL
,
2525 kvm_get_dirty_bitmap_cb
);
2529 void kvm_qemu_log_memory(target_phys_addr_t start
, target_phys_addr_t size
,
2533 kvm_dirty_pages_log_enable_slot(kvm_context
, start
, size
);
2536 if (must_use_aliases_target(start
))
2539 kvm_dirty_pages_log_disable_slot(kvm_context
, start
, size
);
2543 #ifdef KVM_CAP_IRQCHIP
2545 int kvm_set_irq(int irq
, int level
, int *status
)
2547 return kvm_set_irq_level(kvm_context
, irq
, level
, status
);
2552 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
2554 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
2557 void kvm_mutex_unlock(void)
2559 assert(!cpu_single_env
);
2560 pthread_mutex_unlock(&qemu_mutex
);
2563 void kvm_mutex_lock(void)
2565 pthread_mutex_lock(&qemu_mutex
);
2566 cpu_single_env
= NULL
;
2569 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
2570 void kvm_add_ioperm_data(struct ioperm_data
*data
)
2572 QLIST_INSERT_HEAD(&ioperm_head
, data
, entries
);
2575 void kvm_remove_ioperm_data(unsigned long start_port
, unsigned long num
)
2577 struct ioperm_data
*data
;
2579 data
= QLIST_FIRST(&ioperm_head
);
2581 struct ioperm_data
*next
= QLIST_NEXT(data
, entries
);
2583 if (data
->start_port
== start_port
&& data
->num
== num
) {
2584 QLIST_REMOVE(data
, entries
);
2592 void kvm_ioperm(CPUState
*env
, void *data
)
2594 if (kvm_enabled() && qemu_system_ready
)
2595 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
2600 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
,
2601 target_phys_addr_t end_addr
)
2606 if (must_use_aliases_source(start_addr
))
2610 kvm_get_dirty_pages_range(kvm_context
, start_addr
,
2611 end_addr
- start_addr
, NULL
,
2612 kvm_get_dirty_bitmap_cb
);
2617 int kvm_log_start(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
2620 if (must_use_aliases_source(phys_addr
))
2625 kvm_qemu_log_memory(phys_addr
, len
, 1);
2630 int kvm_log_stop(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
2633 if (must_use_aliases_source(phys_addr
))
2638 kvm_qemu_log_memory(phys_addr
, len
, 0);
2643 int kvm_set_boot_cpu_id(uint32_t id
)
2645 return kvm_set_boot_vcpu_id(kvm_context
, id
);
2650 struct kvm_x86_mce_data
{
2652 struct kvm_x86_mce
*mce
;
2656 static void kvm_do_inject_x86_mce(void *_data
)
2658 struct kvm_x86_mce_data
*data
= _data
;
2661 r
= kvm_set_mce(data
->env
->kvm_cpu_state
.vcpu_ctx
, data
->mce
);
2663 perror("kvm_set_mce FAILED");
2664 if (data
->abort_on_error
)
2670 void kvm_inject_x86_mce(CPUState
*cenv
, int bank
, uint64_t status
,
2671 uint64_t mcg_status
, uint64_t addr
, uint64_t misc
,
2675 struct kvm_x86_mce mce
= {
2678 .mcg_status
= mcg_status
,
2682 struct kvm_x86_mce_data data
= {
2685 .abort_on_error
= abort_on_error
,
2688 if (!cenv
->mcg_cap
) {
2689 fprintf(stderr
, "MCE support is not enabled!\n");
2692 on_vcpu(cenv
, kvm_do_inject_x86_mce
, &data
);