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"
26 #include <sys/utsname.h>
27 #include <sys/syscall.h>
29 #include <sys/ioctl.h>
31 #include <sys/prctl.h>
37 #define PR_MCE_KILL 33
41 #define BUS_MCEERR_AR 4
44 #define BUS_MCEERR_AO 5
47 #define EXPECTED_KVM_API_VERSION 12
49 #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
50 #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 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
80 /* The list of ioperm_data */
81 static QLIST_HEAD(, ioperm_data
) ioperm_head
;
84 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
86 int kvm_abi
= EXPECTED_KVM_API_VERSION
;
89 #ifdef KVM_CAP_SET_GUEST_DEBUG
90 static int kvm_debug(CPUState
*env
,
91 struct kvm_debug_exit_arch
*arch_info
)
93 int handle
= kvm_arch_debug(arch_info
);
96 kvm_debug_cpu_requested
= env
;
103 static int handle_unhandled(uint64_t reason
)
105 fprintf(stderr
, "kvm: unhandled exit %" PRIx64
"\n", reason
);
109 #define VMX_INVALID_GUEST_STATE 0x80000021
111 static int handle_failed_vmentry(uint64_t reason
)
113 fprintf(stderr
, "kvm: vm entry failed with error 0x%" PRIx64
"\n\n", reason
);
115 /* Perhaps we will need to check if this machine is intel since exit reason 0x21
116 has a different interpretation on SVM */
117 if (reason
== VMX_INVALID_GUEST_STATE
) {
118 fprintf(stderr
, "If you're runnning a guest on an Intel machine without\n");
119 fprintf(stderr
, "unrestricted mode support, the failure can be most likely\n");
120 fprintf(stderr
, "due to the guest entering an invalid state for Intel VT.\n");
121 fprintf(stderr
, "For example, the guest maybe running in big real mode\n");
122 fprintf(stderr
, "which is not supported on less recent Intel processors.\n\n");
128 static inline void set_gsi(kvm_context_t kvm
, unsigned int gsi
)
130 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
132 if (gsi
< kvm
->max_gsi
)
133 bitmap
[gsi
/ 32] |= 1U << (gsi
% 32);
135 DPRINTF("Invalid GSI %u\n", gsi
);
138 static inline void clear_gsi(kvm_context_t kvm
, unsigned int gsi
)
140 uint32_t *bitmap
= kvm
->used_gsi_bitmap
;
142 if (gsi
< kvm
->max_gsi
)
143 bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
145 DPRINTF("Invalid GSI %u\n", gsi
);
148 static int kvm_create_context(void);
150 int kvm_init(int smp_cpus
)
156 fd
= open("/dev/kvm", O_RDWR
);
158 perror("open /dev/kvm");
161 r
= ioctl(fd
, KVM_GET_API_VERSION
, 0);
164 "kvm kernel version too old: "
165 "KVM_GET_API_VERSION ioctl not supported\n");
168 if (r
< EXPECTED_KVM_API_VERSION
) {
169 fprintf(stderr
, "kvm kernel version too old: "
170 "We expect API version %d or newer, but got "
171 "version %d\n", EXPECTED_KVM_API_VERSION
, r
);
174 if (r
> EXPECTED_KVM_API_VERSION
) {
175 fprintf(stderr
, "kvm userspace version too old\n");
179 kvm_page_size
= getpagesize();
180 kvm_state
= qemu_mallocz(sizeof(*kvm_state
));
181 kvm_context
= &kvm_state
->kvm_context
;
184 kvm_state
->vmfd
= -1;
185 kvm_context
->opaque
= cpu_single_env
;
186 kvm_context
->dirty_pages_log_all
= 0;
187 kvm_context
->no_irqchip_creation
= 0;
188 kvm_context
->no_pit_creation
= 0;
190 #ifdef KVM_CAP_SET_GUEST_DEBUG
191 QTAILQ_INIT(&kvm_state
->kvm_sw_breakpoints
);
194 gsi_count
= kvm_get_gsi_count(kvm_context
);
198 /* Round up so we can search ints using ffs */
199 gsi_bits
= ALIGN(gsi_count
, 32);
200 kvm_context
->used_gsi_bitmap
= qemu_mallocz(gsi_bits
/ 8);
201 kvm_context
->max_gsi
= gsi_bits
;
203 /* Mark any over-allocated bits as already in use */
204 for (i
= gsi_count
; i
< gsi_bits
; i
++)
205 set_gsi(kvm_context
, i
);
208 kvm_cpu_register_phys_memory_client();
210 pthread_mutex_lock(&qemu_mutex
);
211 return kvm_create_context();
218 static void kvm_finalize(KVMState
*s
)
221 if (kvm->vcpu_fd[0] != -1)
222 close(kvm->vcpu_fd[0]);
223 if (kvm->vm_fd != -1)
230 void kvm_disable_irqchip_creation(kvm_context_t kvm
)
232 kvm
->no_irqchip_creation
= 1;
235 void kvm_disable_pit_creation(kvm_context_t kvm
)
237 kvm
->no_pit_creation
= 1;
240 static void kvm_reset_vcpu(void *opaque
)
242 CPUState
*env
= opaque
;
244 kvm_arch_cpu_reset(env
);
247 static void kvm_create_vcpu(CPUState
*env
, int id
)
251 KVMState
*s
= kvm_state
;
253 r
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_VCPU
, id
);
255 fprintf(stderr
, "kvm_create_vcpu: %m\n");
256 fprintf(stderr
, "Failed to create vCPU. Check the -smp parameter.\n");
261 env
->kvm_state
= kvm_state
;
263 mmap_size
= kvm_ioctl(kvm_state
, KVM_GET_VCPU_MMAP_SIZE
, 0);
265 fprintf(stderr
, "get vcpu mmap size: %m\n");
269 mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, env
->kvm_fd
,
271 if (env
->kvm_run
== MAP_FAILED
) {
272 fprintf(stderr
, "mmap vcpu area: %m\n");
276 #ifdef KVM_CAP_COALESCED_MMIO
277 if (s
->coalesced_mmio
&& !s
->coalesced_mmio_ring
)
278 s
->coalesced_mmio_ring
= (void *) env
->kvm_run
+
279 s
->coalesced_mmio
* PAGE_SIZE
;
282 r
= kvm_arch_init_vcpu(env
);
284 qemu_register_reset(kvm_reset_vcpu
, env
);
291 /* We're no good with semi-broken states. */
295 static int kvm_set_boot_vcpu_id(kvm_context_t kvm
, uint32_t id
)
297 #ifdef KVM_CAP_SET_BOOT_CPU_ID
298 int r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_BOOT_CPU_ID
);
300 return kvm_vm_ioctl(kvm_state
, KVM_SET_BOOT_CPU_ID
, id
);
307 int kvm_create_vm(kvm_context_t kvm
)
310 #ifdef KVM_CAP_IRQ_ROUTING
311 kvm
->irq_routes
= qemu_mallocz(sizeof(*kvm
->irq_routes
));
312 kvm
->nr_allocated_irq_routes
= 0;
315 fd
= kvm_ioctl(kvm_state
, KVM_CREATE_VM
, 0);
317 fprintf(stderr
, "kvm_create_vm: %m\n");
320 kvm_state
->vmfd
= fd
;
324 static int kvm_create_default_phys_mem(kvm_context_t kvm
,
325 unsigned long phys_mem_bytes
,
328 #ifdef KVM_CAP_USER_MEMORY
329 int r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
333 "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n");
335 #error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported
340 void kvm_create_irqchip(kvm_context_t kvm
)
344 kvm
->irqchip_in_kernel
= 0;
345 #ifdef KVM_CAP_IRQCHIP
346 if (!kvm
->no_irqchip_creation
) {
347 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_IRQCHIP
);
348 if (r
> 0) { /* kernel irqchip supported */
349 r
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_IRQCHIP
);
351 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE
;
352 #if defined(KVM_CAP_IRQ_INJECT_STATUS) && defined(KVM_IRQ_LINE_STATUS)
353 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
,
354 KVM_CAP_IRQ_INJECT_STATUS
);
356 kvm
->irqchip_inject_ioctl
= KVM_IRQ_LINE_STATUS
;
358 kvm
->irqchip_in_kernel
= 1;
360 fprintf(stderr
, "Create kernel PIC irqchip failed\n");
364 kvm_state
->irqchip_in_kernel
= kvm
->irqchip_in_kernel
;
367 int kvm_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
, void **vm_mem
)
371 r
= kvm_create_vm(kvm
);
374 r
= kvm_arch_create(kvm
, phys_mem_bytes
, vm_mem
);
377 for (i
= 0; i
< ARRAY_SIZE(kvm_state
->slots
); i
++)
378 kvm_state
->slots
[i
].slot
= i
;
380 r
= kvm_create_default_phys_mem(kvm
, phys_mem_bytes
, vm_mem
);
383 kvm_create_irqchip(kvm
);
388 #ifdef KVM_CAP_IRQCHIP
390 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
, int *status
)
392 struct kvm_irq_level event
;
395 if (!kvm
->irqchip_in_kernel
)
399 r
= kvm_vm_ioctl(kvm_state
, kvm
->irqchip_inject_ioctl
, &event
);
401 perror("kvm_set_irq_level");
404 #ifdef KVM_CAP_IRQ_INJECT_STATUS
406 (kvm
->irqchip_inject_ioctl
== KVM_IRQ_LINE
) ? 1 : event
.status
;
415 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
419 if (!kvm
->irqchip_in_kernel
)
421 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_IRQCHIP
, chip
);
423 perror("kvm_get_irqchip\n");
428 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
)
432 if (!kvm
->irqchip_in_kernel
)
434 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_IRQCHIP
, chip
);
436 perror("kvm_set_irqchip\n");
443 static int handle_debug(CPUState
*env
)
445 #ifdef KVM_CAP_SET_GUEST_DEBUG
446 struct kvm_run
*run
= env
->kvm_run
;
448 return kvm_debug(env
, &run
->debug
.arch
);
454 int kvm_get_regs(CPUState
*env
, struct kvm_regs
*regs
)
456 return kvm_vcpu_ioctl(env
, KVM_GET_REGS
, regs
);
459 int kvm_set_regs(CPUState
*env
, struct kvm_regs
*regs
)
461 return kvm_vcpu_ioctl(env
, KVM_SET_REGS
, regs
);
464 int kvm_get_fpu(CPUState
*env
, struct kvm_fpu
*fpu
)
466 return kvm_vcpu_ioctl(env
, KVM_GET_FPU
, fpu
);
469 int kvm_set_fpu(CPUState
*env
, struct kvm_fpu
*fpu
)
471 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, fpu
);
474 int kvm_get_sregs(CPUState
*env
, struct kvm_sregs
*sregs
)
476 return kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, sregs
);
479 int kvm_set_sregs(CPUState
*env
, struct kvm_sregs
*sregs
)
481 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, sregs
);
484 #ifdef KVM_CAP_MP_STATE
485 int kvm_get_mpstate(CPUState
*env
, struct kvm_mp_state
*mp_state
)
489 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
491 return kvm_vcpu_ioctl(env
, KVM_GET_MP_STATE
, mp_state
);
495 int kvm_set_mpstate(CPUState
*env
, struct kvm_mp_state
*mp_state
)
499 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_MP_STATE
);
501 return kvm_vcpu_ioctl(env
, KVM_SET_MP_STATE
, mp_state
);
507 int kvm_get_xsave(CPUState
*env
, struct kvm_xsave
*xsave
)
509 return kvm_vcpu_ioctl(env
, KVM_GET_XSAVE
, xsave
);
512 int kvm_set_xsave(CPUState
*env
, struct kvm_xsave
*xsave
)
514 return kvm_vcpu_ioctl(env
, KVM_SET_XSAVE
, xsave
);
519 int kvm_get_xcrs(CPUState
*env
, struct kvm_xcrs
*xcrs
)
521 return kvm_vcpu_ioctl(env
, KVM_GET_XCRS
, xcrs
);
524 int kvm_set_xcrs(CPUState
*env
, struct kvm_xcrs
*xcrs
)
526 return kvm_vcpu_ioctl(env
, KVM_SET_XCRS
, xcrs
);
530 static int handle_mmio(CPUState
*env
)
532 unsigned long addr
= env
->kvm_run
->mmio
.phys_addr
;
533 struct kvm_run
*kvm_run
= env
->kvm_run
;
534 void *data
= kvm_run
->mmio
.data
;
536 /* hack: Red Hat 7.1 generates these weird accesses. */
537 if ((addr
> 0xa0000 - 4 && addr
<= 0xa0000) && kvm_run
->mmio
.len
== 3)
540 cpu_physical_memory_rw(addr
, data
, kvm_run
->mmio
.len
, kvm_run
->mmio
.is_write
);
544 int handle_io_window(kvm_context_t kvm
)
549 int handle_shutdown(kvm_context_t kvm
, CPUState
*env
)
551 /* stop the current vcpu from going back to guest mode */
554 qemu_system_reset_request();
558 static inline void push_nmi(kvm_context_t kvm
)
560 #ifdef KVM_CAP_USER_NMI
561 kvm_arch_push_nmi(kvm
->opaque
);
562 #endif /* KVM_CAP_USER_NMI */
565 void post_kvm_run(kvm_context_t kvm
, CPUState
*env
)
567 pthread_mutex_lock(&qemu_mutex
);
568 kvm_arch_post_run(env
, env
->kvm_run
);
569 cpu_single_env
= env
;
572 int pre_kvm_run(kvm_context_t kvm
, CPUState
*env
)
574 kvm_arch_pre_run(env
, env
->kvm_run
);
576 pthread_mutex_unlock(&qemu_mutex
);
580 int kvm_is_ready_for_interrupt_injection(CPUState
*env
)
582 return env
->kvm_run
->ready_for_interrupt_injection
;
585 int kvm_run(CPUState
*env
)
588 kvm_context_t kvm
= &env
->kvm_state
->kvm_context
;
589 struct kvm_run
*run
= env
->kvm_run
;
590 int fd
= env
->kvm_fd
;
593 if (env
->kvm_vcpu_dirty
) {
594 kvm_arch_load_regs(env
, KVM_PUT_RUNTIME_STATE
);
595 env
->kvm_vcpu_dirty
= 0;
598 #if !defined(__s390__)
599 if (!kvm
->irqchip_in_kernel
)
600 run
->request_interrupt_window
= kvm_arch_try_push_interrupts(env
);
603 r
= pre_kvm_run(kvm
, env
);
606 if (env
->exit_request
) {
607 env
->exit_request
= 0;
608 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
610 r
= ioctl(fd
, KVM_RUN
, 0);
612 if (r
== -1 && errno
!= EINTR
&& errno
!= EAGAIN
) {
614 post_kvm_run(kvm
, env
);
615 fprintf(stderr
, "kvm_run: %s\n", strerror(-r
));
619 post_kvm_run(kvm
, env
);
621 kvm_flush_coalesced_mmio_buffer();
623 #if !defined(__s390__)
625 r
= handle_io_window(kvm
);
630 switch (run
->exit_reason
) {
631 case KVM_EXIT_UNKNOWN
:
632 r
= handle_unhandled(run
->hw
.hardware_exit_reason
);
634 case KVM_EXIT_FAIL_ENTRY
:
635 r
= handle_failed_vmentry(run
->fail_entry
.hardware_entry_failure_reason
);
637 case KVM_EXIT_EXCEPTION
:
638 fprintf(stderr
, "exception %d (%x)\n", run
->ex
.exception
,
645 r
= kvm_handle_io(run
->io
.port
,
646 (uint8_t *)run
+ run
->io
.data_offset
,
653 r
= handle_debug(env
);
656 r
= handle_mmio(env
);
659 r
= kvm_arch_halt(env
);
661 case KVM_EXIT_IRQ_WINDOW_OPEN
:
663 case KVM_EXIT_SHUTDOWN
:
664 r
= handle_shutdown(kvm
, env
);
666 #if defined(__s390__)
667 case KVM_EXIT_S390_SIEIC
:
668 r
= kvm_s390_handle_intercept(kvm
, env
, run
);
670 case KVM_EXIT_S390_RESET
:
671 r
= kvm_s390_handle_reset(kvm
, env
, run
);
674 case KVM_EXIT_INTERNAL_ERROR
:
675 kvm_handle_internal_error(env
, run
);
679 if (kvm_arch_run(env
)) {
680 fprintf(stderr
, "unhandled vm exit: 0x%x\n", run
->exit_reason
);
693 int kvm_inject_irq(CPUState
*env
, unsigned irq
)
695 struct kvm_interrupt intr
;
698 return kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
701 int kvm_inject_nmi(CPUState
*env
)
703 #ifdef KVM_CAP_USER_NMI
704 return kvm_vcpu_ioctl(env
, KVM_NMI
);
710 int kvm_init_coalesced_mmio(kvm_context_t kvm
)
713 kvm_state
->coalesced_mmio
= 0;
714 #ifdef KVM_CAP_COALESCED_MMIO
715 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_COALESCED_MMIO
);
717 kvm_state
->coalesced_mmio
= r
;
724 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
725 int kvm_assign_pci_device(kvm_context_t kvm
,
726 struct kvm_assigned_pci_dev
*assigned_dev
)
728 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
731 static int kvm_old_assign_irq(kvm_context_t kvm
,
732 struct kvm_assigned_irq
*assigned_irq
)
734 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_IRQ
, assigned_irq
);
737 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
738 int kvm_assign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
)
742 ret
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_ASSIGN_DEV_IRQ
);
744 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_DEV_IRQ
, assigned_irq
);
747 return kvm_old_assign_irq(kvm
, assigned_irq
);
750 int kvm_deassign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
)
752 return kvm_vm_ioctl(kvm_state
, KVM_DEASSIGN_DEV_IRQ
, assigned_irq
);
755 int kvm_assign_irq(kvm_context_t kvm
, struct kvm_assigned_irq
*assigned_irq
)
757 return kvm_old_assign_irq(kvm
, assigned_irq
);
762 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
763 int kvm_deassign_pci_device(kvm_context_t kvm
,
764 struct kvm_assigned_pci_dev
*assigned_dev
)
766 return kvm_vm_ioctl(kvm_state
, KVM_DEASSIGN_PCI_DEVICE
, assigned_dev
);
770 int kvm_reinject_control(kvm_context_t kvm
, int pit_reinject
)
772 #ifdef KVM_CAP_REINJECT_CONTROL
774 struct kvm_reinject_control control
;
776 control
.pit_reinject
= pit_reinject
;
778 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_REINJECT_CONTROL
);
780 return kvm_vm_ioctl(kvm_state
, KVM_REINJECT_CONTROL
, &control
);
786 int kvm_has_gsi_routing(kvm_context_t kvm
)
790 #ifdef KVM_CAP_IRQ_ROUTING
791 r
= kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
796 int kvm_get_gsi_count(kvm_context_t kvm
)
798 #ifdef KVM_CAP_IRQ_ROUTING
799 return kvm_check_extension(kvm_state
, KVM_CAP_IRQ_ROUTING
);
805 int kvm_clear_gsi_routes(kvm_context_t kvm
)
807 #ifdef KVM_CAP_IRQ_ROUTING
808 kvm
->irq_routes
->nr
= 0;
815 int kvm_add_routing_entry(kvm_context_t kvm
,
816 struct kvm_irq_routing_entry
*entry
)
818 #ifdef KVM_CAP_IRQ_ROUTING
819 struct kvm_irq_routing
*z
;
820 struct kvm_irq_routing_entry
*new;
823 if (kvm
->irq_routes
->nr
== kvm
->nr_allocated_irq_routes
) {
824 n
= kvm
->nr_allocated_irq_routes
* 2;
827 size
= sizeof(struct kvm_irq_routing
);
828 size
+= n
* sizeof(*new);
829 z
= realloc(kvm
->irq_routes
, size
);
832 kvm
->nr_allocated_irq_routes
= n
;
835 n
= kvm
->irq_routes
->nr
++;
836 new = &kvm
->irq_routes
->entries
[n
];
837 memset(new, 0, sizeof(*new));
838 new->gsi
= entry
->gsi
;
839 new->type
= entry
->type
;
840 new->flags
= entry
->flags
;
843 set_gsi(kvm
, entry
->gsi
);
851 int kvm_add_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
853 #ifdef KVM_CAP_IRQ_ROUTING
854 struct kvm_irq_routing_entry e
;
857 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
859 e
.u
.irqchip
.irqchip
= irqchip
;
860 e
.u
.irqchip
.pin
= pin
;
861 return kvm_add_routing_entry(kvm
, &e
);
867 int kvm_del_routing_entry(kvm_context_t kvm
,
868 struct kvm_irq_routing_entry
*entry
)
870 #ifdef KVM_CAP_IRQ_ROUTING
871 struct kvm_irq_routing_entry
*e
, *p
;
872 int i
, gsi
, found
= 0;
876 for (i
= 0; i
< kvm
->irq_routes
->nr
; ++i
) {
877 e
= &kvm
->irq_routes
->entries
[i
];
878 if (e
->type
== entry
->type
&& e
->gsi
== gsi
) {
880 case KVM_IRQ_ROUTING_IRQCHIP
:{
881 if (e
->u
.irqchip
.irqchip
==
882 entry
->u
.irqchip
.irqchip
883 && e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
884 p
= &kvm
->irq_routes
->entries
[--kvm
->irq_routes
->nr
];
890 case KVM_IRQ_ROUTING_MSI
:{
891 if (e
->u
.msi
.address_lo
==
892 entry
->u
.msi
.address_lo
893 && e
->u
.msi
.address_hi
==
894 entry
->u
.msi
.address_hi
895 && e
->u
.msi
.data
== entry
->u
.msi
.data
) {
896 p
= &kvm
->irq_routes
->entries
[--kvm
->irq_routes
->nr
];
906 /* If there are no other users of this GSI
907 * mark it available in the bitmap */
908 for (i
= 0; i
< kvm
->irq_routes
->nr
; i
++) {
909 e
= &kvm
->irq_routes
->entries
[i
];
913 if (i
== kvm
->irq_routes
->nr
)
926 int kvm_update_routing_entry(kvm_context_t kvm
,
927 struct kvm_irq_routing_entry
*entry
,
928 struct kvm_irq_routing_entry
*newentry
)
930 #ifdef KVM_CAP_IRQ_ROUTING
931 struct kvm_irq_routing_entry
*e
;
934 if (entry
->gsi
!= newentry
->gsi
|| entry
->type
!= newentry
->type
) {
938 for (i
= 0; i
< kvm
->irq_routes
->nr
; ++i
) {
939 e
= &kvm
->irq_routes
->entries
[i
];
940 if (e
->type
!= entry
->type
|| e
->gsi
!= entry
->gsi
) {
944 case KVM_IRQ_ROUTING_IRQCHIP
:
945 if (e
->u
.irqchip
.irqchip
== entry
->u
.irqchip
.irqchip
&&
946 e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
947 memcpy(&e
->u
.irqchip
, &newentry
->u
.irqchip
,
948 sizeof e
->u
.irqchip
);
952 case KVM_IRQ_ROUTING_MSI
:
953 if (e
->u
.msi
.address_lo
== entry
->u
.msi
.address_lo
&&
954 e
->u
.msi
.address_hi
== entry
->u
.msi
.address_hi
&&
955 e
->u
.msi
.data
== entry
->u
.msi
.data
) {
956 memcpy(&e
->u
.msi
, &newentry
->u
.msi
, sizeof e
->u
.msi
);
970 int kvm_del_irq_route(kvm_context_t kvm
, int gsi
, int irqchip
, int pin
)
972 #ifdef KVM_CAP_IRQ_ROUTING
973 struct kvm_irq_routing_entry e
;
976 e
.type
= KVM_IRQ_ROUTING_IRQCHIP
;
978 e
.u
.irqchip
.irqchip
= irqchip
;
979 e
.u
.irqchip
.pin
= pin
;
980 return kvm_del_routing_entry(kvm
, &e
);
986 int kvm_commit_irq_routes(kvm_context_t kvm
)
988 #ifdef KVM_CAP_IRQ_ROUTING
989 kvm
->irq_routes
->flags
= 0;
990 return kvm_vm_ioctl(kvm_state
, KVM_SET_GSI_ROUTING
, kvm
->irq_routes
);
996 int kvm_get_irq_route_gsi(kvm_context_t kvm
)
999 uint32_t *buf
= kvm
->used_gsi_bitmap
;
1001 /* Return the lowest unused GSI in the bitmap */
1002 for (i
= 0; i
< kvm
->max_gsi
/ 32; i
++) {
1007 return bit
- 1 + i
* 32;
1013 #ifdef KVM_CAP_DEVICE_MSIX
1014 int kvm_assign_set_msix_nr(kvm_context_t kvm
,
1015 struct kvm_assigned_msix_nr
*msix_nr
)
1017 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_SET_MSIX_NR
, msix_nr
);
1020 int kvm_assign_set_msix_entry(kvm_context_t kvm
,
1021 struct kvm_assigned_msix_entry
*entry
)
1023 return kvm_vm_ioctl(kvm_state
, KVM_ASSIGN_SET_MSIX_ENTRY
, entry
);
1027 #if defined(KVM_CAP_IRQFD) && defined(CONFIG_EVENTFD)
1029 #include <sys/eventfd.h>
1031 static int _kvm_irqfd(kvm_context_t kvm
, int fd
, int gsi
, int flags
)
1033 struct kvm_irqfd data
= {
1039 return kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &data
);
1042 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1047 if (!kvm_check_extension(kvm_state
, KVM_CAP_IRQFD
))
1054 r
= _kvm_irqfd(kvm
, fd
, gsi
, 0);
1063 #else /* KVM_CAP_IRQFD */
1065 int kvm_irqfd(kvm_context_t kvm
, int gsi
, int flags
)
1070 #endif /* KVM_CAP_IRQFD */
1071 unsigned long kvm_get_thread_id(void)
1073 return syscall(SYS_gettid
);
1076 static void qemu_cond_wait(pthread_cond_t
*cond
)
1078 CPUState
*env
= cpu_single_env
;
1080 pthread_cond_wait(cond
, &qemu_mutex
);
1081 cpu_single_env
= env
;
1084 static void sig_ipi_handler(int n
)
1088 static void hardware_memory_error(void)
1090 fprintf(stderr
, "Hardware memory error!\n");
1094 static void sigbus_reraise(void)
1097 struct sigaction action
;
1099 memset(&action
, 0, sizeof(action
));
1100 action
.sa_handler
= SIG_DFL
;
1101 if (!sigaction(SIGBUS
, &action
, NULL
)) {
1104 sigaddset(&set
, SIGBUS
);
1105 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
1107 perror("Failed to re-raise SIGBUS!\n");
1111 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
1114 #if defined(KVM_CAP_MCE) && defined(TARGET_I386)
1115 if (first_cpu
->mcg_cap
&& siginfo
->ssi_addr
1116 && siginfo
->ssi_code
== BUS_MCEERR_AO
) {
1118 unsigned long paddr
;
1121 /* Hope we are lucky for AO MCE */
1122 if (do_qemu_ram_addr_from_host((void *)(intptr_t)siginfo
->ssi_addr
,
1124 fprintf(stderr
, "Hardware memory error for memory used by "
1125 "QEMU itself instead of guest system!: %llx\n",
1126 (unsigned long long)siginfo
->ssi_addr
);
1129 status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1130 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1132 kvm_inject_x86_mce(first_cpu
, 9, status
,
1133 MCG_STATUS_MCIP
| MCG_STATUS_RIPV
, paddr
,
1134 (MCM_ADDR_PHYS
<< 6) | 0xc, 1);
1135 for (cenv
= first_cpu
->next_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1136 kvm_inject_x86_mce(cenv
, 1, MCI_STATUS_VAL
| MCI_STATUS_UC
,
1137 MCG_STATUS_MCIP
| MCG_STATUS_RIPV
, 0, 0, 1);
1141 if (siginfo
->ssi_code
== BUS_MCEERR_AO
)
1143 else if (siginfo
->ssi_code
== BUS_MCEERR_AR
)
1144 hardware_memory_error();
1150 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
1152 struct qemu_work_item wi
;
1154 if (env
== current_env
) {
1161 if (!env
->kvm_cpu_state
.queued_work_first
)
1162 env
->kvm_cpu_state
.queued_work_first
= &wi
;
1164 env
->kvm_cpu_state
.queued_work_last
->next
= &wi
;
1165 env
->kvm_cpu_state
.queued_work_last
= &wi
;
1169 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1171 qemu_cond_wait(&qemu_work_cond
);
1174 static void do_kvm_cpu_synchronize_state(void *_env
)
1176 CPUState
*env
= _env
;
1178 if (!env
->kvm_vcpu_dirty
) {
1179 kvm_arch_save_regs(env
);
1180 env
->kvm_vcpu_dirty
= 1;
1184 void kvm_cpu_synchronize_state(CPUState
*env
)
1186 if (!env
->kvm_vcpu_dirty
)
1187 on_vcpu(env
, do_kvm_cpu_synchronize_state
, env
);
1190 void kvm_cpu_synchronize_post_reset(CPUState
*env
)
1192 kvm_arch_load_regs(env
, KVM_PUT_RESET_STATE
);
1193 env
->kvm_vcpu_dirty
= 0;
1196 void kvm_cpu_synchronize_post_init(CPUState
*env
)
1198 kvm_arch_load_regs(env
, KVM_PUT_FULL_STATE
);
1199 env
->kvm_vcpu_dirty
= 0;
1202 static void inject_interrupt(void *data
)
1204 cpu_interrupt(current_env
, (long) data
);
1207 void kvm_inject_interrupt(CPUState
*env
, int mask
)
1209 on_vcpu(env
, inject_interrupt
, (void *) (long) mask
);
1212 void kvm_update_interrupt_request(CPUState
*env
)
1217 if (!current_env
|| !current_env
->created
)
1220 * Testing for created here is really redundant
1222 if (current_env
&& current_env
->created
&&
1223 env
!= current_env
&& !env
->kvm_cpu_state
.signalled
)
1227 env
->kvm_cpu_state
.signalled
= 1;
1228 if (env
->kvm_cpu_state
.thread
)
1229 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
1234 int kvm_cpu_exec(CPUState
*env
)
1240 printf("kvm_run returned %d\n", r
);
1247 int kvm_cpu_is_stopped(CPUState
*env
)
1249 return !vm_running
|| env
->stopped
;
1252 static void flush_queued_work(CPUState
*env
)
1254 struct qemu_work_item
*wi
;
1256 if (!env
->kvm_cpu_state
.queued_work_first
)
1259 while ((wi
= env
->kvm_cpu_state
.queued_work_first
)) {
1260 env
->kvm_cpu_state
.queued_work_first
= wi
->next
;
1264 env
->kvm_cpu_state
.queued_work_last
= NULL
;
1265 pthread_cond_broadcast(&qemu_work_cond
);
1268 static int kvm_mce_in_exception(CPUState
*env
)
1270 struct kvm_msr_entry msr_mcg_status
= {
1271 .index
= MSR_MCG_STATUS
,
1275 r
= kvm_get_msrs(env
, &msr_mcg_status
, 1);
1276 if (r
== -1 || r
== 0)
1278 return !!(msr_mcg_status
.data
& MCG_STATUS_MCIP
);
1281 static void kvm_on_sigbus(CPUState
*env
, siginfo_t
*siginfo
)
1283 #if defined(KVM_CAP_MCE) && defined(TARGET_I386)
1284 struct kvm_x86_mce mce
= {
1287 unsigned long paddr
;
1290 if (env
->mcg_cap
&& siginfo
->si_addr
1291 && (siginfo
->si_code
== BUS_MCEERR_AR
1292 || siginfo
->si_code
== BUS_MCEERR_AO
)) {
1293 if (siginfo
->si_code
== BUS_MCEERR_AR
) {
1294 /* Fake an Intel architectural Data Load SRAR UCR */
1295 mce
.status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1296 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1297 | MCI_STATUS_AR
| 0x134;
1298 mce
.misc
= (MCM_ADDR_PHYS
<< 6) | 0xc;
1299 mce
.mcg_status
= MCG_STATUS_MCIP
| MCG_STATUS_EIPV
;
1302 * If there is an MCE excpetion being processed, ignore
1305 r
= kvm_mce_in_exception(env
);
1307 fprintf(stderr
, "Failed to get MCE status\n");
1310 /* Fake an Intel architectural Memory scrubbing UCR */
1311 mce
.status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1312 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1314 mce
.misc
= (MCM_ADDR_PHYS
<< 6) | 0xc;
1315 mce
.mcg_status
= MCG_STATUS_MCIP
| MCG_STATUS_RIPV
;
1317 if (do_qemu_ram_addr_from_host((void *)siginfo
->si_addr
, &paddr
)) {
1318 fprintf(stderr
, "Hardware memory error for memory used by "
1319 "QEMU itself instaed of guest system!\n");
1320 /* Hope we are lucky for AO MCE */
1321 if (siginfo
->si_code
== BUS_MCEERR_AO
)
1324 hardware_memory_error();
1327 r
= kvm_set_mce(env
, &mce
);
1329 fprintf(stderr
, "kvm_set_mce: %s\n", strerror(errno
));
1335 if (siginfo
->si_code
== BUS_MCEERR_AO
)
1337 else if (siginfo
->si_code
== BUS_MCEERR_AR
)
1338 hardware_memory_error();
1344 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
1352 ts
.tv_sec
= timeout
/ 1000;
1353 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
1354 sigemptyset(&waitset
);
1355 sigaddset(&waitset
, SIG_IPI
);
1356 sigaddset(&waitset
, SIGBUS
);
1359 pthread_mutex_unlock(&qemu_mutex
);
1361 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
1364 pthread_mutex_lock(&qemu_mutex
);
1366 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
1367 printf("sigtimedwait: %s\n", strerror(e
));
1373 kvm_on_sigbus(env
, &siginfo
);
1379 r
= sigpending(&chkset
);
1381 printf("sigpending: %s\n", strerror(e
));
1384 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
1386 cpu_single_env
= env
;
1387 flush_queued_work(env
);
1392 pthread_cond_signal(&qemu_pause_cond
);
1395 env
->kvm_cpu_state
.signalled
= 0;
1398 static int all_threads_paused(void)
1400 CPUState
*penv
= first_cpu
;
1405 penv
= (CPUState
*) penv
->next_cpu
;
1411 static void pause_all_threads(void)
1413 CPUState
*penv
= first_cpu
;
1416 if (penv
!= cpu_single_env
) {
1418 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1424 penv
= (CPUState
*) penv
->next_cpu
;
1427 while (!all_threads_paused())
1428 qemu_cond_wait(&qemu_pause_cond
);
1431 static void resume_all_threads(void)
1433 CPUState
*penv
= first_cpu
;
1435 assert(!cpu_single_env
);
1440 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
1441 penv
= (CPUState
*) penv
->next_cpu
;
1445 static void kvm_vm_state_change_handler(void *context
, int running
, int reason
)
1448 resume_all_threads();
1450 pause_all_threads();
1453 static void setup_kernel_sigmask(CPUState
*env
)
1458 sigaddset(&set
, SIGUSR2
);
1459 sigaddset(&set
, SIGIO
);
1460 sigaddset(&set
, SIGALRM
);
1461 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1463 sigprocmask(SIG_BLOCK
, NULL
, &set
);
1464 sigdelset(&set
, SIG_IPI
);
1465 sigdelset(&set
, SIGBUS
);
1467 kvm_set_signal_mask(env
, &set
);
1470 static void qemu_kvm_system_reset(void)
1472 pause_all_threads();
1474 qemu_system_reset();
1476 resume_all_threads();
1479 static void process_irqchip_events(CPUState
*env
)
1481 kvm_arch_process_irqchip_events(env
);
1482 if (kvm_arch_has_work(env
))
1486 static int kvm_main_loop_cpu(CPUState
*env
)
1489 int run_cpu
= !kvm_cpu_is_stopped(env
);
1490 if (run_cpu
&& !kvm_irqchip_in_kernel()) {
1491 process_irqchip_events(env
);
1492 run_cpu
= !env
->halted
;
1496 kvm_main_loop_wait(env
, 0);
1498 kvm_main_loop_wait(env
, 1000);
1501 pthread_mutex_unlock(&qemu_mutex
);
1505 static void *ap_main_loop(void *_env
)
1507 CPUState
*env
= _env
;
1509 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1510 struct ioperm_data
*data
= NULL
;
1514 env
->thread_id
= kvm_get_thread_id();
1515 sigfillset(&signals
);
1516 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
1518 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1519 /* do ioperm for io ports of assigned devices */
1520 QLIST_FOREACH(data
, &ioperm_head
, entries
)
1521 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
1524 pthread_mutex_lock(&qemu_mutex
);
1525 cpu_single_env
= env
;
1527 kvm_create_vcpu(env
, env
->cpu_index
);
1528 setup_kernel_sigmask(env
);
1530 /* signal VCPU creation */
1531 current_env
->created
= 1;
1532 pthread_cond_signal(&qemu_vcpu_cond
);
1534 /* and wait for machine initialization */
1535 while (!qemu_system_ready
)
1536 qemu_cond_wait(&qemu_system_cond
);
1538 /* re-initialize cpu_single_env after re-acquiring qemu_mutex */
1539 cpu_single_env
= env
;
1541 kvm_main_loop_cpu(env
);
1545 int kvm_init_vcpu(CPUState
*env
)
1547 pthread_create(&env
->kvm_cpu_state
.thread
, NULL
, ap_main_loop
, env
);
1549 while (env
->created
== 0)
1550 qemu_cond_wait(&qemu_vcpu_cond
);
1555 int kvm_vcpu_inited(CPUState
*env
)
1557 return env
->created
;
1561 void kvm_hpet_disable_kpit(void)
1563 struct kvm_pit_state2 ps2
;
1565 kvm_get_pit2(kvm_context
, &ps2
);
1566 ps2
.flags
|= KVM_PIT_FLAGS_HPET_LEGACY
;
1567 kvm_set_pit2(kvm_context
, &ps2
);
1570 void kvm_hpet_enable_kpit(void)
1572 struct kvm_pit_state2 ps2
;
1574 kvm_get_pit2(kvm_context
, &ps2
);
1575 ps2
.flags
&= ~KVM_PIT_FLAGS_HPET_LEGACY
;
1576 kvm_set_pit2(kvm_context
, &ps2
);
1580 int kvm_init_ap(void)
1582 struct sigaction action
;
1584 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
1586 signal(SIG_IPI
, sig_ipi_handler
);
1588 memset(&action
, 0, sizeof(action
));
1589 action
.sa_flags
= SA_SIGINFO
;
1590 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
1591 sigaction(SIGBUS
, &action
, NULL
);
1592 prctl(PR_MCE_KILL
, 1, 1, 0, 0);
1596 void qemu_kvm_notify_work(void)
1598 /* Write 8 bytes to be compatible with eventfd. */
1599 static uint64_t val
= 1;
1602 if (io_thread_fd
== -1)
1606 ret
= write(io_thread_fd
, &val
, sizeof(val
));
1607 } while (ret
< 0 && errno
== EINTR
);
1609 /* EAGAIN is fine in case we have a pipe. */
1610 if (ret
< 0 && errno
!= EAGAIN
) {
1611 fprintf(stderr
, "qemu_kvm_notify_work: write() filed: %s\n",
1617 /* If we have signalfd, we mask out the signals we want to handle and then
1618 * use signalfd to listen for them. We rely on whatever the current signal
1619 * handler is to dispatch the signals when we receive them.
1622 static void sigfd_handler(void *opaque
)
1624 int fd
= (unsigned long) opaque
;
1625 struct qemu_signalfd_siginfo info
;
1626 struct sigaction action
;
1631 len
= read(fd
, &info
, sizeof(info
));
1632 } while (len
== -1 && errno
== EINTR
);
1634 if (len
== -1 && errno
== EAGAIN
)
1637 if (len
!= sizeof(info
)) {
1638 printf("read from sigfd returned %zd: %m\n", len
);
1642 sigaction(info
.ssi_signo
, NULL
, &action
);
1643 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
)
1644 action
.sa_sigaction(info
.ssi_signo
,
1645 (siginfo_t
*)&info
, NULL
);
1646 else if (action
.sa_handler
)
1647 action
.sa_handler(info
.ssi_signo
);
1652 /* Used to break IO thread out of select */
1653 static void io_thread_wakeup(void *opaque
)
1655 int fd
= (unsigned long) opaque
;
1659 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
1661 len
= read(fd
, buffer
, sizeof(buffer
));
1662 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
1665 int kvm_main_loop(void)
1671 io_thread
= pthread_self();
1672 qemu_system_ready
= 1;
1674 if (qemu_eventfd(fds
) == -1) {
1675 fprintf(stderr
, "failed to create eventfd\n");
1679 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1680 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1682 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
1683 (void *)(unsigned long) fds
[0]);
1685 io_thread_fd
= fds
[1];
1688 sigaddset(&mask
, SIGIO
);
1689 sigaddset(&mask
, SIGALRM
);
1690 sigaddset(&mask
, SIGBUS
);
1691 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
1693 sigfd
= qemu_signalfd(&mask
);
1695 fprintf(stderr
, "failed to create signalfd\n");
1699 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
1701 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
1702 (void *)(unsigned long) sigfd
);
1704 pthread_cond_broadcast(&qemu_system_cond
);
1706 io_thread_sigfd
= sigfd
;
1707 cpu_single_env
= NULL
;
1711 if (qemu_shutdown_requested()) {
1712 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1713 if (qemu_no_shutdown()) {
1717 } else if (qemu_powerdown_requested()) {
1718 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1719 qemu_irq_raise(qemu_system_powerdown
);
1720 } else if (qemu_reset_requested()) {
1721 qemu_kvm_system_reset();
1722 } else if (kvm_debug_cpu_requested
) {
1723 gdb_set_stop_cpu(kvm_debug_cpu_requested
);
1724 vm_stop(EXCP_DEBUG
);
1725 kvm_debug_cpu_requested
= NULL
;
1729 pause_all_threads();
1730 pthread_mutex_unlock(&qemu_mutex
);
1735 #if !defined(TARGET_I386)
1736 int kvm_arch_init_irq_routing(void)
1744 static int kvm_create_context(void)
1746 static const char upgrade_note
[] =
1747 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1748 "(see http://sourceforge.net/projects/kvm).\n";
1753 kvm_disable_irqchip_creation(kvm_context
);
1756 kvm_disable_pit_creation(kvm_context
);
1758 if (kvm_create(kvm_context
, 0, NULL
) < 0) {
1759 kvm_finalize(kvm_state
);
1762 r
= kvm_arch_qemu_create_context();
1764 kvm_finalize(kvm_state
);
1767 if (kvm_pit
&& !kvm_pit_reinject
) {
1768 if (kvm_reinject_control(kvm_context
, 0)) {
1769 fprintf(stderr
, "failure to disable in-kernel PIT reinjection\n");
1774 /* There was a nasty bug in < kvm-80 that prevents memory slots from being
1775 * destroyed properly. Since we rely on this capability, refuse to work
1776 * with any kernel without this capability. */
1777 if (!kvm_check_extension(kvm_state
, KVM_CAP_DESTROY_MEMORY_REGION_WORKS
)) {
1779 "KVM kernel module broken (DESTROY_MEMORY_REGION).\n%s",
1784 r
= kvm_arch_init_irq_routing();
1789 kvm_state
->vcpu_events
= 0;
1790 #ifdef KVM_CAP_VCPU_EVENTS
1791 kvm_state
->vcpu_events
= kvm_check_extension(kvm_state
, KVM_CAP_VCPU_EVENTS
);
1794 kvm_state
->debugregs
= 0;
1795 #ifdef KVM_CAP_DEBUGREGS
1796 kvm_state
->debugregs
= kvm_check_extension(kvm_state
, KVM_CAP_DEBUGREGS
);
1801 if (!qemu_kvm_has_gsi_routing()) {
1804 /* if kernel can't do irq routing, interrupt source
1805 * override 0->2 can not be set up as required by hpet,
1809 } else if (!qemu_kvm_has_pit_state2()) {
1820 #ifdef KVM_CAP_IRQCHIP
1822 int kvm_set_irq(int irq
, int level
, int *status
)
1824 return kvm_set_irq_level(kvm_context
, irq
, level
, status
);
1829 void kvm_mutex_unlock(void)
1831 assert(!cpu_single_env
);
1832 pthread_mutex_unlock(&qemu_mutex
);
1835 void kvm_mutex_lock(void)
1837 pthread_mutex_lock(&qemu_mutex
);
1838 cpu_single_env
= NULL
;
1841 void qemu_mutex_unlock_iothread(void)
1847 void qemu_mutex_lock_iothread(void)
1853 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1854 void kvm_add_ioperm_data(struct ioperm_data
*data
)
1856 QLIST_INSERT_HEAD(&ioperm_head
, data
, entries
);
1859 void kvm_remove_ioperm_data(unsigned long start_port
, unsigned long num
)
1861 struct ioperm_data
*data
;
1863 data
= QLIST_FIRST(&ioperm_head
);
1865 struct ioperm_data
*next
= QLIST_NEXT(data
, entries
);
1867 if (data
->start_port
== start_port
&& data
->num
== num
) {
1868 QLIST_REMOVE(data
, entries
);
1876 void kvm_ioperm(CPUState
*env
, void *data
)
1878 if (kvm_enabled() && qemu_system_ready
)
1879 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
1884 int kvm_set_boot_cpu_id(uint32_t id
)
1886 return kvm_set_boot_vcpu_id(kvm_context
, id
);
1891 struct kvm_x86_mce_data
{
1893 struct kvm_x86_mce
*mce
;
1897 static void kvm_do_inject_x86_mce(void *_data
)
1899 struct kvm_x86_mce_data
*data
= _data
;
1902 /* If there is an MCE excpetion being processed, ignore this SRAO MCE */
1903 r
= kvm_mce_in_exception(data
->env
);
1905 fprintf(stderr
, "Failed to get MCE status\n");
1906 else if (r
&& !(data
->mce
->status
& MCI_STATUS_AR
))
1908 r
= kvm_set_mce(data
->env
, data
->mce
);
1910 perror("kvm_set_mce FAILED");
1911 if (data
->abort_on_error
)
1917 void kvm_inject_x86_mce(CPUState
*cenv
, int bank
, uint64_t status
,
1918 uint64_t mcg_status
, uint64_t addr
, uint64_t misc
,
1922 struct kvm_x86_mce mce
= {
1925 .mcg_status
= mcg_status
,
1929 struct kvm_x86_mce_data data
= {
1932 .abort_on_error
= abort_on_error
,
1935 if (!cenv
->mcg_cap
) {
1936 fprintf(stderr
, "MCE support is not enabled!\n");
1939 on_vcpu(cenv
, kvm_do_inject_x86_mce
, &data
);