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"
19 #include "qemu-common.h"
26 #include <sys/utsname.h>
27 #include <sys/syscall.h>
33 extern void perror(const char *s
);
35 kvm_context_t kvm_context
;
39 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
40 pthread_cond_t qemu_aio_cond
= PTHREAD_COND_INITIALIZER
;
41 pthread_cond_t qemu_vcpu_cond
= PTHREAD_COND_INITIALIZER
;
42 pthread_cond_t qemu_system_cond
= PTHREAD_COND_INITIALIZER
;
43 pthread_cond_t qemu_pause_cond
= PTHREAD_COND_INITIALIZER
;
44 pthread_cond_t qemu_work_cond
= PTHREAD_COND_INITIALIZER
;
45 __thread
struct vcpu_info
*vcpu
;
47 static int qemu_system_ready
;
49 #define SIG_IPI (SIGRTMIN+4)
51 struct qemu_kvm_work_item
{
52 struct qemu_kvm_work_item
*next
;
53 void (*func
)(void *data
);
67 struct qemu_kvm_work_item
*queued_work_first
, *queued_work_last
;
71 static int io_thread_fd
= -1;
72 static int io_thread_sigfd
= -1;
74 static inline unsigned long kvm_get_thread_id(void)
76 return syscall(SYS_gettid
);
79 static void qemu_cond_wait(pthread_cond_t
*cond
)
81 CPUState
*env
= cpu_single_env
;
83 pthread_cond_wait(cond
, &qemu_mutex
);
87 CPUState
*qemu_kvm_cpu_env(int index
)
89 return vcpu_info
[index
].env
;
92 static void sig_ipi_handler(int n
)
96 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
98 struct vcpu_info
*vi
= &vcpu_info
[env
->cpu_index
];
99 struct qemu_kvm_work_item wi
;
108 if (!vi
->queued_work_first
)
109 vi
->queued_work_first
= &wi
;
111 vi
->queued_work_last
->next
= &wi
;
112 vi
->queued_work_last
= &wi
;
116 pthread_kill(vi
->thread
, SIG_IPI
);
118 qemu_cond_wait(&qemu_work_cond
);
121 void kvm_update_interrupt_request(CPUState
*env
)
128 if (vcpu
&& env
!= vcpu
->env
&& !vcpu_info
[env
->cpu_index
].signalled
)
132 vcpu_info
[env
->cpu_index
].signalled
= 1;
133 if (vcpu_info
[env
->cpu_index
].thread
)
134 pthread_kill(vcpu_info
[env
->cpu_index
].thread
, SIG_IPI
);
139 void kvm_update_after_sipi(CPUState
*env
)
141 vcpu_info
[env
->cpu_index
].sipi_needed
= 1;
142 kvm_update_interrupt_request(env
);
145 void kvm_apic_init(CPUState
*env
)
147 if (env
->cpu_index
!= 0)
148 vcpu_info
[env
->cpu_index
].init
= 1;
149 kvm_update_interrupt_request(env
);
154 static int try_push_interrupts(void *opaque
)
156 return kvm_arch_try_push_interrupts(opaque
);
159 static void post_kvm_run(void *opaque
, int vcpu
)
162 pthread_mutex_lock(&qemu_mutex
);
163 kvm_arch_post_kvm_run(opaque
, vcpu
);
166 static int pre_kvm_run(void *opaque
, int vcpu
)
168 CPUState
*env
= qemu_kvm_cpu_env(vcpu
);
170 kvm_arch_pre_kvm_run(opaque
, vcpu
);
172 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
174 pthread_mutex_unlock(&qemu_mutex
);
178 static void kvm_do_load_registers(void *_env
)
180 CPUState
*env
= _env
;
182 kvm_arch_load_regs(env
);
185 void kvm_load_registers(CPUState
*env
)
188 on_vcpu(env
->cpu_index
, kvm_do_load_registers
, env
);
191 static void kvm_do_save_registers(void *_env
)
193 CPUState
*env
= _env
;
195 kvm_arch_save_regs(env
);
198 void kvm_save_registers(CPUState
*env
)
201 on_vcpu(env
, kvm_do_save_registers
, env
);
204 int kvm_cpu_exec(CPUState
*env
)
208 r
= kvm_run(kvm_context
, env
->cpu_index
);
210 printf("kvm_run returned %d\n", r
);
217 extern int vm_running
;
219 static int has_work(CPUState
*env
)
221 if (!vm_running
|| (env
&& vcpu_info
[env
->cpu_index
].stopped
))
223 if (!(env
->hflags
& HF_HALTED_MASK
))
225 return kvm_arch_has_work(env
);
228 static void flush_queued_work(CPUState
*env
)
230 struct vcpu_info
*vi
= &vcpu_info
[env
->cpu_index
];
231 struct qemu_kvm_work_item
*wi
;
233 if (!vi
->queued_work_first
)
236 while ((wi
= vi
->queued_work_first
)) {
237 vi
->queued_work_first
= wi
->next
;
241 vi
->queued_work_last
= NULL
;
242 pthread_cond_broadcast(&qemu_work_cond
);
245 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
252 pthread_mutex_unlock(&qemu_mutex
);
254 ts
.tv_sec
= timeout
/ 1000;
255 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
256 sigemptyset(&waitset
);
257 sigaddset(&waitset
, SIG_IPI
);
259 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
262 pthread_mutex_lock(&qemu_mutex
);
264 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
265 printf("sigtimedwait: %s\n", strerror(e
));
270 flush_queued_work(env
);
272 if (vcpu_info
[env
->cpu_index
].stop
) {
273 vcpu_info
[env
->cpu_index
].stop
= 0;
274 vcpu_info
[env
->cpu_index
].stopped
= 1;
275 pthread_cond_signal(&qemu_pause_cond
);
277 cpu_single_env
= env
;
279 vcpu_info
[env
->cpu_index
].signalled
= 0;
282 static int all_threads_paused(void)
286 for (i
= 0; i
< smp_cpus
; ++i
)
287 if (vcpu_info
[i
].stop
)
292 static void pause_all_threads(void)
296 assert(!cpu_single_env
);
298 for (i
= 0; i
< smp_cpus
; ++i
) {
299 vcpu_info
[i
].stop
= 1;
300 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
302 while (!all_threads_paused())
303 qemu_cond_wait(&qemu_pause_cond
);
306 static void resume_all_threads(void)
310 assert(!cpu_single_env
);
312 for (i
= 0; i
< smp_cpus
; ++i
) {
313 vcpu_info
[i
].stop
= 0;
314 vcpu_info
[i
].stopped
= 0;
315 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
319 static void kvm_vm_state_change_handler(void *context
, int running
)
322 resume_all_threads();
327 static void update_regs_for_sipi(CPUState
*env
)
329 kvm_arch_update_regs_for_sipi(env
);
330 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
331 vcpu_info
[env
->cpu_index
].init
= 0;
334 static void update_regs_for_init(CPUState
*env
)
337 kvm_arch_load_regs(env
);
340 static void setup_kernel_sigmask(CPUState
*env
)
345 sigaddset(&set
, SIGUSR2
);
346 sigaddset(&set
, SIGIO
);
347 sigaddset(&set
, SIGALRM
);
348 sigprocmask(SIG_BLOCK
, &set
, NULL
);
350 sigprocmask(SIG_BLOCK
, NULL
, &set
);
351 sigdelset(&set
, SIG_IPI
);
353 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
356 void qemu_kvm_system_reset(void)
364 for (i
= 0; i
< smp_cpus
; ++i
)
365 kvm_arch_cpu_reset(vcpu_info
[i
].env
);
367 resume_all_threads();
370 static int kvm_main_loop_cpu(CPUState
*env
)
372 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
374 setup_kernel_sigmask(env
);
376 pthread_mutex_lock(&qemu_mutex
);
377 if (kvm_irqchip_in_kernel(kvm_context
))
378 env
->hflags
&= ~HF_HALTED_MASK
;
380 kvm_qemu_init_env(env
);
381 env
->ready_for_interrupt_injection
= 1;
383 kvm_tpr_vcpu_start(env
);
386 cpu_single_env
= env
;
388 while (!has_work(env
))
389 kvm_main_loop_wait(env
, 1000);
390 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
391 env
->hflags
&= ~HF_HALTED_MASK
;
392 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
393 update_regs_for_sipi(env
);
394 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
395 update_regs_for_init(env
);
396 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
398 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
399 kvm_main_loop_wait(env
, 0);
401 pthread_mutex_unlock(&qemu_mutex
);
405 static void *ap_main_loop(void *_env
)
407 CPUState
*env
= _env
;
410 vcpu
= &vcpu_info
[env
->cpu_index
];
412 vcpu
->env
->thread_id
= kvm_get_thread_id();
413 sigfillset(&signals
);
414 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
415 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
416 kvm_qemu_init_env(env
);
418 /* signal VCPU creation */
419 pthread_mutex_lock(&qemu_mutex
);
421 pthread_cond_signal(&qemu_vcpu_cond
);
423 /* and wait for machine initialization */
424 while (!qemu_system_ready
)
425 qemu_cond_wait(&qemu_system_cond
);
426 pthread_mutex_unlock(&qemu_mutex
);
428 kvm_main_loop_cpu(env
);
432 void kvm_init_new_ap(int cpu
, CPUState
*env
)
434 pthread_create(&vcpu_info
[cpu
].thread
, NULL
, ap_main_loop
, env
);
436 while (vcpu_info
[cpu
].created
== 0)
437 qemu_cond_wait(&qemu_vcpu_cond
);
440 int kvm_init_ap(void)
445 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
447 signal(SIG_IPI
, sig_ipi_handler
);
451 void qemu_kvm_notify_work(void)
457 if (io_thread_fd
== -1)
460 memcpy(buffer
, &value
, sizeof(value
));
465 len
= write(io_thread_fd
, buffer
+ offset
, 8 - offset
);
466 if (len
== -1 && errno
== EINTR
)
476 fprintf(stderr
, "failed to notify io thread\n");
479 /* If we have signalfd, we mask out the signals we want to handle and then
480 * use signalfd to listen for them. We rely on whatever the current signal
481 * handler is to dispatch the signals when we receive them.
484 static void sigfd_handler(void *opaque
)
486 int fd
= (unsigned long)opaque
;
487 struct signalfd_siginfo info
;
488 struct sigaction action
;
493 len
= read(fd
, &info
, sizeof(info
));
494 } while (len
== -1 && errno
== EINTR
);
496 if (len
== -1 && errno
== EAGAIN
)
499 if (len
!= sizeof(info
)) {
500 printf("read from sigfd returned %ld: %m\n", len
);
504 sigaction(info
.ssi_signo
, NULL
, &action
);
505 if (action
.sa_handler
)
506 action
.sa_handler(info
.ssi_signo
);
508 if (info
.ssi_signo
== SIGUSR2
) {
509 pthread_cond_signal(&qemu_aio_cond
);
514 /* Used to break IO thread out of select */
515 static void io_thread_wakeup(void *opaque
)
517 int fd
= (unsigned long)opaque
;
524 len
= read(fd
, buffer
+ offset
, 8 - offset
);
525 if (len
== -1 && errno
== EINTR
)
535 int kvm_main_loop(void)
541 io_thread
= pthread_self();
542 qemu_system_ready
= 1;
544 if (kvm_eventfd(fds
) == -1) {
545 fprintf(stderr
, "failed to create eventfd\n");
549 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
550 (void *)(unsigned long)fds
[0]);
552 io_thread_fd
= fds
[1];
555 sigaddset(&mask
, SIGIO
);
556 sigaddset(&mask
, SIGALRM
);
557 sigaddset(&mask
, SIGUSR2
);
558 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
560 sigfd
= kvm_signalfd(&mask
);
562 fprintf(stderr
, "failed to create signalfd\n");
566 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
568 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
569 (void *)(unsigned long)sigfd
);
571 pthread_cond_broadcast(&qemu_system_cond
);
573 io_thread_sigfd
= sigfd
;
574 cpu_single_env
= NULL
;
577 main_loop_wait(1000);
578 if (qemu_shutdown_requested())
580 else if (qemu_powerdown_requested())
581 qemu_system_powerdown();
582 else if (qemu_reset_requested())
583 qemu_kvm_system_reset();
587 pthread_mutex_unlock(&qemu_mutex
);
592 static int kvm_debug(void *opaque
, int vcpu
)
594 CPUState
*env
= cpu_single_env
;
596 env
->exception_index
= EXCP_DEBUG
;
600 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
602 *data
= cpu_inb(0, addr
);
606 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
608 *data
= cpu_inw(0, addr
);
612 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
614 *data
= cpu_inl(0, addr
);
618 #define PM_IO_BASE 0xb000
620 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
625 cpu_outb(0, 0xb3, 0);
632 x
= cpu_inw(0, PM_IO_BASE
+ 4);
634 cpu_outw(0, PM_IO_BASE
+ 4, x
);
641 x
= cpu_inw(0, PM_IO_BASE
+ 4);
643 cpu_outw(0, PM_IO_BASE
+ 4, x
);
651 cpu_outb(0, addr
, data
);
655 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
657 cpu_outw(0, addr
, data
);
661 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
663 cpu_outl(0, addr
, data
);
667 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
669 cpu_physical_memory_rw(addr
, data
, len
, 0);
673 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
675 cpu_physical_memory_rw(addr
, data
, len
, 1);
679 static int kvm_io_window(void *opaque
)
685 static int kvm_halt(void *opaque
, int vcpu
)
687 return kvm_arch_halt(opaque
, vcpu
);
690 static int kvm_shutdown(void *opaque
, int vcpu
)
692 /* stop the current vcpu from going back to guest mode */
693 vcpu_info
[cpu_single_env
->cpu_index
].stopped
= 1;
695 qemu_system_reset_request();
699 static struct kvm_callbacks qemu_kvm_ops
= {
707 .mmio_read
= kvm_mmio_read
,
708 .mmio_write
= kvm_mmio_write
,
710 .shutdown
= kvm_shutdown
,
711 .io_window
= kvm_io_window
,
712 .try_push_interrupts
= try_push_interrupts
,
713 .post_kvm_run
= post_kvm_run
,
714 .pre_kvm_run
= pre_kvm_run
,
716 .tpr_access
= handle_tpr_access
,
719 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
720 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
726 /* Try to initialize kvm */
727 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
731 pthread_mutex_lock(&qemu_mutex
);
736 int kvm_qemu_create_context(void)
740 kvm_disable_irqchip_creation(kvm_context
);
743 kvm_disable_pit_creation(kvm_context
);
745 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
749 r
= kvm_arch_qemu_create_context();
755 void kvm_qemu_destroy(void)
757 kvm_finalize(kvm_context
);
760 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
762 unsigned long phys_offset
)
764 #ifdef KVM_CAP_USER_MEMORY
767 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
769 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
770 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
773 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
775 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
776 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
777 phys_ram_base
+ phys_offset
,
780 if (phys_offset
& IO_MEM_ROM
) {
781 phys_offset
&= ~IO_MEM_ROM
;
782 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
784 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
785 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
786 phys_ram_base
+ phys_offset
,
790 printf("kvm_cpu_register_physical_memory: failed\n");
796 if (phys_offset
& IO_MEM_ROM
) {
797 phys_offset
&= ~IO_MEM_ROM
;
798 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
802 int kvm_qemu_check_extension(int ext
)
804 return kvm_check_extension(kvm_context
, ext
);
807 int kvm_qemu_init_env(CPUState
*cenv
)
809 return kvm_arch_qemu_init_env(cenv
);
812 int kvm_update_debugger(CPUState
*env
)
814 struct kvm_debug_guest dbg
;
817 memset(dbg
.breakpoints
, 0, sizeof(dbg
.breakpoints
));
820 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
822 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
823 dbg
.breakpoints
[i
].enabled
= 1;
824 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
826 dbg
.singlestep
= env
->singlestep_enabled
;
828 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
833 * dirty pages logging
835 /* FIXME: use unsigned long pointer instead of unsigned char */
836 unsigned char *kvm_dirty_bitmap
= NULL
;
837 int kvm_physical_memory_set_dirty_tracking(int enable
)
845 if (!kvm_dirty_bitmap
) {
846 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
847 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
848 if (kvm_dirty_bitmap
== NULL
) {
849 perror("Failed to allocate dirty pages bitmap");
853 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
858 if (kvm_dirty_bitmap
) {
859 r
= kvm_dirty_pages_log_reset(kvm_context
);
860 qemu_free(kvm_dirty_bitmap
);
861 kvm_dirty_bitmap
= NULL
;
867 /* get kvm's dirty pages bitmap and update qemu's */
868 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
869 unsigned char *bitmap
,
871 unsigned long mem_size
)
873 unsigned int i
, j
, n
=0;
875 unsigned page_number
, addr
, addr1
;
876 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
879 * bitmap-traveling is faster than memory-traveling (for addr...)
880 * especially when most of the memory is not dirty.
882 for (i
=0; i
<len
; i
++) {
887 page_number
= i
* 8 + j
;
888 addr1
= page_number
* TARGET_PAGE_SIZE
;
889 addr
= offset
+ addr1
;
890 cpu_physical_memory_set_dirty(addr
);
896 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
897 void *bitmap
, void *opaque
)
899 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
903 * get kvm's dirty pages bitmap and update qemu's
904 * we only care about physical ram, which resides in slots 0 and 3
906 int kvm_update_dirty_pages_log(void)
911 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
912 kvm_dirty_bitmap
, NULL
,
913 kvm_get_dirty_bitmap_cb
);
917 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
919 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
920 unsigned int brsize
= BITMAP_SIZE(ram_size
);
921 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
922 unsigned int extra_bytes
= (extra_pages
+7)/8;
923 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
924 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
926 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
927 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
928 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
933 #ifdef KVM_CAP_IRQCHIP
935 int kvm_set_irq(int irq
, int level
)
937 return kvm_set_irq_level(kvm_context
, irq
, level
);
942 void qemu_kvm_aio_wait_start(void)
946 void qemu_kvm_aio_wait(void)
948 if (!cpu_single_env
) {
949 if (io_thread_sigfd
!= -1) {
954 FD_SET(io_thread_sigfd
, &rfds
);
956 /* this is a rare case where we do want to hold qemu_mutex
957 * while sleeping. We cannot allow anything else to run
959 ret
= select(io_thread_sigfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
960 if (ret
> 0 && FD_ISSET(io_thread_sigfd
, &rfds
))
961 sigfd_handler((void *)(unsigned long)io_thread_sigfd
);
965 qemu_cond_wait(&qemu_aio_cond
);
968 void qemu_kvm_aio_wait_end(void)
972 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
974 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
977 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
978 unsigned long size
, int log
, int writable
)
980 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
983 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
986 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);
989 void kvm_mutex_unlock(void)
991 assert(!cpu_single_env
);
992 pthread_mutex_unlock(&qemu_mutex
);
995 void kvm_mutex_lock(void)
997 pthread_mutex_lock(&qemu_mutex
);
998 cpu_single_env
= NULL
;