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 void kvm_load_registers(CPUState
*env
)
181 kvm_arch_load_regs(env
);
184 void kvm_save_registers(CPUState
*env
)
187 kvm_arch_save_regs(env
);
190 int kvm_cpu_exec(CPUState
*env
)
194 r
= kvm_run(kvm_context
, env
->cpu_index
);
196 printf("kvm_run returned %d\n", r
);
203 extern int vm_running
;
205 static int has_work(CPUState
*env
)
207 if (!vm_running
|| (env
&& vcpu_info
[env
->cpu_index
].stopped
))
209 if (!(env
->hflags
& HF_HALTED_MASK
))
211 return kvm_arch_has_work(env
);
214 static void flush_queued_work(CPUState
*env
)
216 struct vcpu_info
*vi
= &vcpu_info
[env
->cpu_index
];
217 struct qemu_kvm_work_item
*wi
;
219 if (!vi
->queued_work_first
)
222 while ((wi
= vi
->queued_work_first
)) {
223 vi
->queued_work_first
= wi
->next
;
227 vi
->queued_work_last
= NULL
;
228 pthread_cond_broadcast(&qemu_work_cond
);
231 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
238 pthread_mutex_unlock(&qemu_mutex
);
240 ts
.tv_sec
= timeout
/ 1000;
241 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
242 sigemptyset(&waitset
);
243 sigaddset(&waitset
, SIG_IPI
);
245 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
248 pthread_mutex_lock(&qemu_mutex
);
250 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
251 printf("sigtimedwait: %s\n", strerror(e
));
256 flush_queued_work(env
);
258 if (vcpu_info
[env
->cpu_index
].stop
) {
259 vcpu_info
[env
->cpu_index
].stop
= 0;
260 vcpu_info
[env
->cpu_index
].stopped
= 1;
261 pthread_cond_signal(&qemu_pause_cond
);
263 cpu_single_env
= env
;
265 vcpu_info
[env
->cpu_index
].signalled
= 0;
268 static int all_threads_paused(void)
272 for (i
= 0; i
< smp_cpus
; ++i
)
273 if (vcpu_info
[i
].stop
)
278 static void pause_all_threads(void)
282 assert(!cpu_single_env
);
284 for (i
= 0; i
< smp_cpus
; ++i
) {
285 vcpu_info
[i
].stop
= 1;
286 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
288 while (!all_threads_paused())
289 qemu_cond_wait(&qemu_pause_cond
);
292 static void resume_all_threads(void)
296 assert(!cpu_single_env
);
298 for (i
= 0; i
< smp_cpus
; ++i
) {
299 vcpu_info
[i
].stop
= 0;
300 vcpu_info
[i
].stopped
= 0;
301 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
305 static void kvm_vm_state_change_handler(void *context
, int running
)
308 resume_all_threads();
313 static void update_regs_for_sipi(CPUState
*env
)
315 kvm_arch_update_regs_for_sipi(env
);
316 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
317 vcpu_info
[env
->cpu_index
].init
= 0;
320 static void update_regs_for_init(CPUState
*env
)
323 kvm_arch_load_regs(env
);
326 static void setup_kernel_sigmask(CPUState
*env
)
331 sigaddset(&set
, SIGUSR2
);
332 sigaddset(&set
, SIGIO
);
333 sigaddset(&set
, SIGALRM
);
334 sigprocmask(SIG_BLOCK
, &set
, NULL
);
336 sigprocmask(SIG_BLOCK
, NULL
, &set
);
337 sigdelset(&set
, SIG_IPI
);
339 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
342 void qemu_kvm_system_reset(void)
350 for (i
= 0; i
< smp_cpus
; ++i
)
351 kvm_arch_cpu_reset(vcpu_info
[i
].env
);
353 resume_all_threads();
356 static int kvm_main_loop_cpu(CPUState
*env
)
358 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
360 setup_kernel_sigmask(env
);
362 pthread_mutex_lock(&qemu_mutex
);
363 if (kvm_irqchip_in_kernel(kvm_context
))
364 env
->hflags
&= ~HF_HALTED_MASK
;
366 kvm_qemu_init_env(env
);
367 env
->ready_for_interrupt_injection
= 1;
369 kvm_tpr_vcpu_start(env
);
372 cpu_single_env
= env
;
374 while (!has_work(env
))
375 kvm_main_loop_wait(env
, 1000);
376 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
377 env
->hflags
&= ~HF_HALTED_MASK
;
378 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
379 update_regs_for_sipi(env
);
380 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
381 update_regs_for_init(env
);
382 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
384 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
385 kvm_main_loop_wait(env
, 0);
387 pthread_mutex_unlock(&qemu_mutex
);
391 static void *ap_main_loop(void *_env
)
393 CPUState
*env
= _env
;
396 vcpu
= &vcpu_info
[env
->cpu_index
];
398 vcpu
->env
->thread_id
= kvm_get_thread_id();
399 sigfillset(&signals
);
400 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
401 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
402 kvm_qemu_init_env(env
);
404 /* signal VCPU creation */
405 pthread_mutex_lock(&qemu_mutex
);
407 pthread_cond_signal(&qemu_vcpu_cond
);
409 /* and wait for machine initialization */
410 while (!qemu_system_ready
)
411 qemu_cond_wait(&qemu_system_cond
);
412 pthread_mutex_unlock(&qemu_mutex
);
414 kvm_main_loop_cpu(env
);
418 void kvm_init_new_ap(int cpu
, CPUState
*env
)
420 pthread_create(&vcpu_info
[cpu
].thread
, NULL
, ap_main_loop
, env
);
422 while (vcpu_info
[cpu
].created
== 0)
423 qemu_cond_wait(&qemu_vcpu_cond
);
426 int kvm_init_ap(void)
431 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
433 signal(SIG_IPI
, sig_ipi_handler
);
437 void qemu_kvm_notify_work(void)
443 if (io_thread_fd
== -1)
446 memcpy(buffer
, &value
, sizeof(value
));
451 len
= write(io_thread_fd
, buffer
+ offset
, 8 - offset
);
452 if (len
== -1 && errno
== EINTR
)
462 fprintf(stderr
, "failed to notify io thread\n");
465 /* If we have signalfd, we mask out the signals we want to handle and then
466 * use signalfd to listen for them. We rely on whatever the current signal
467 * handler is to dispatch the signals when we receive them.
470 static void sigfd_handler(void *opaque
)
472 int fd
= (unsigned long)opaque
;
473 struct signalfd_siginfo info
;
474 struct sigaction action
;
479 len
= read(fd
, &info
, sizeof(info
));
480 } while (len
== -1 && errno
== EINTR
);
482 if (len
== -1 && errno
== EAGAIN
)
485 if (len
!= sizeof(info
)) {
486 printf("read from sigfd returned %ld: %m\n", len
);
490 sigaction(info
.ssi_signo
, NULL
, &action
);
491 if (action
.sa_handler
)
492 action
.sa_handler(info
.ssi_signo
);
494 if (info
.ssi_signo
== SIGUSR2
) {
495 pthread_cond_signal(&qemu_aio_cond
);
500 /* Used to break IO thread out of select */
501 static void io_thread_wakeup(void *opaque
)
503 int fd
= (unsigned long)opaque
;
510 len
= read(fd
, buffer
+ offset
, 8 - offset
);
511 if (len
== -1 && errno
== EINTR
)
521 int kvm_main_loop(void)
527 io_thread
= pthread_self();
528 qemu_system_ready
= 1;
530 if (kvm_eventfd(fds
) == -1) {
531 fprintf(stderr
, "failed to create eventfd\n");
535 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
536 (void *)(unsigned long)fds
[0]);
538 io_thread_fd
= fds
[1];
541 sigaddset(&mask
, SIGIO
);
542 sigaddset(&mask
, SIGALRM
);
543 sigaddset(&mask
, SIGUSR2
);
544 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
546 sigfd
= kvm_signalfd(&mask
);
548 fprintf(stderr
, "failed to create signalfd\n");
552 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
554 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
555 (void *)(unsigned long)sigfd
);
557 pthread_cond_broadcast(&qemu_system_cond
);
559 io_thread_sigfd
= sigfd
;
560 cpu_single_env
= NULL
;
563 main_loop_wait(1000);
564 if (qemu_shutdown_requested())
566 else if (qemu_powerdown_requested())
567 qemu_system_powerdown();
568 else if (qemu_reset_requested())
569 qemu_kvm_system_reset();
573 pthread_mutex_unlock(&qemu_mutex
);
578 static int kvm_debug(void *opaque
, int vcpu
)
580 CPUState
*env
= cpu_single_env
;
582 env
->exception_index
= EXCP_DEBUG
;
586 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
588 *data
= cpu_inb(0, addr
);
592 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
594 *data
= cpu_inw(0, addr
);
598 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
600 *data
= cpu_inl(0, addr
);
604 #define PM_IO_BASE 0xb000
606 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
611 cpu_outb(0, 0xb3, 0);
618 x
= cpu_inw(0, PM_IO_BASE
+ 4);
620 cpu_outw(0, PM_IO_BASE
+ 4, x
);
627 x
= cpu_inw(0, PM_IO_BASE
+ 4);
629 cpu_outw(0, PM_IO_BASE
+ 4, x
);
637 cpu_outb(0, addr
, data
);
641 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
643 cpu_outw(0, addr
, data
);
647 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
649 cpu_outl(0, addr
, data
);
653 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
655 cpu_physical_memory_rw(addr
, data
, len
, 0);
659 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
661 cpu_physical_memory_rw(addr
, data
, len
, 1);
665 static int kvm_io_window(void *opaque
)
671 static int kvm_halt(void *opaque
, int vcpu
)
673 return kvm_arch_halt(opaque
, vcpu
);
676 static int kvm_shutdown(void *opaque
, int vcpu
)
678 /* stop the current vcpu from going back to guest mode */
679 vcpu_info
[cpu_single_env
->cpu_index
].stopped
= 1;
681 qemu_system_reset_request();
685 static struct kvm_callbacks qemu_kvm_ops
= {
693 .mmio_read
= kvm_mmio_read
,
694 .mmio_write
= kvm_mmio_write
,
696 .shutdown
= kvm_shutdown
,
697 .io_window
= kvm_io_window
,
698 .try_push_interrupts
= try_push_interrupts
,
699 .post_kvm_run
= post_kvm_run
,
700 .pre_kvm_run
= pre_kvm_run
,
702 .tpr_access
= handle_tpr_access
,
705 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
706 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
712 /* Try to initialize kvm */
713 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
717 pthread_mutex_lock(&qemu_mutex
);
722 int kvm_qemu_create_context(void)
726 kvm_disable_irqchip_creation(kvm_context
);
729 kvm_disable_pit_creation(kvm_context
);
731 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
735 r
= kvm_arch_qemu_create_context();
741 void kvm_qemu_destroy(void)
743 kvm_finalize(kvm_context
);
746 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
748 unsigned long phys_offset
)
750 #ifdef KVM_CAP_USER_MEMORY
753 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
755 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
756 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
759 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
761 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
762 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
763 phys_ram_base
+ phys_offset
,
766 if (phys_offset
& IO_MEM_ROM
) {
767 phys_offset
&= ~IO_MEM_ROM
;
768 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
770 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
771 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
772 phys_ram_base
+ phys_offset
,
776 printf("kvm_cpu_register_physical_memory: failed\n");
782 if (phys_offset
& IO_MEM_ROM
) {
783 phys_offset
&= ~IO_MEM_ROM
;
784 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
788 int kvm_qemu_check_extension(int ext
)
790 return kvm_check_extension(kvm_context
, ext
);
793 int kvm_qemu_init_env(CPUState
*cenv
)
795 return kvm_arch_qemu_init_env(cenv
);
798 int kvm_update_debugger(CPUState
*env
)
800 struct kvm_debug_guest dbg
;
803 memset(dbg
.breakpoints
, 0, sizeof(dbg
.breakpoints
));
806 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
808 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
809 dbg
.breakpoints
[i
].enabled
= 1;
810 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
812 dbg
.singlestep
= env
->singlestep_enabled
;
814 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
819 * dirty pages logging
821 /* FIXME: use unsigned long pointer instead of unsigned char */
822 unsigned char *kvm_dirty_bitmap
= NULL
;
823 int kvm_physical_memory_set_dirty_tracking(int enable
)
831 if (!kvm_dirty_bitmap
) {
832 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
833 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
834 if (kvm_dirty_bitmap
== NULL
) {
835 perror("Failed to allocate dirty pages bitmap");
839 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
844 if (kvm_dirty_bitmap
) {
845 r
= kvm_dirty_pages_log_reset(kvm_context
);
846 qemu_free(kvm_dirty_bitmap
);
847 kvm_dirty_bitmap
= NULL
;
853 /* get kvm's dirty pages bitmap and update qemu's */
854 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
855 unsigned char *bitmap
,
857 unsigned long mem_size
)
859 unsigned int i
, j
, n
=0;
861 unsigned page_number
, addr
, addr1
;
862 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
865 * bitmap-traveling is faster than memory-traveling (for addr...)
866 * especially when most of the memory is not dirty.
868 for (i
=0; i
<len
; i
++) {
873 page_number
= i
* 8 + j
;
874 addr1
= page_number
* TARGET_PAGE_SIZE
;
875 addr
= offset
+ addr1
;
876 cpu_physical_memory_set_dirty(addr
);
882 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
883 void *bitmap
, void *opaque
)
885 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
889 * get kvm's dirty pages bitmap and update qemu's
890 * we only care about physical ram, which resides in slots 0 and 3
892 int kvm_update_dirty_pages_log(void)
897 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
898 kvm_dirty_bitmap
, NULL
,
899 kvm_get_dirty_bitmap_cb
);
903 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
905 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
906 unsigned int brsize
= BITMAP_SIZE(ram_size
);
907 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
908 unsigned int extra_bytes
= (extra_pages
+7)/8;
909 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
910 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
912 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
913 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
914 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
919 #ifdef KVM_CAP_IRQCHIP
921 int kvm_set_irq(int irq
, int level
)
923 return kvm_set_irq_level(kvm_context
, irq
, level
);
928 void qemu_kvm_aio_wait_start(void)
932 void qemu_kvm_aio_wait(void)
934 if (!cpu_single_env
) {
935 if (io_thread_sigfd
!= -1) {
940 FD_SET(io_thread_sigfd
, &rfds
);
942 /* this is a rare case where we do want to hold qemu_mutex
943 * while sleeping. We cannot allow anything else to run
945 ret
= select(io_thread_sigfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
946 if (ret
> 0 && FD_ISSET(io_thread_sigfd
, &rfds
))
947 sigfd_handler((void *)(unsigned long)io_thread_sigfd
);
951 qemu_cond_wait(&qemu_aio_cond
);
954 void qemu_kvm_aio_wait_end(void)
958 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
960 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
963 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
964 unsigned long size
, int log
, int writable
)
966 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
969 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
972 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);
975 void kvm_mutex_unlock(void)
977 assert(!cpu_single_env
);
978 pthread_mutex_unlock(&qemu_mutex
);
981 void kvm_mutex_lock(void)
983 pthread_mutex_lock(&qemu_mutex
);
984 cpu_single_env
= NULL
;