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"
27 #include <sys/utsname.h>
28 #include <sys/syscall.h>
35 extern void perror(const char *s
);
37 kvm_context_t kvm_context
;
41 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
42 pthread_cond_t qemu_vcpu_cond
= PTHREAD_COND_INITIALIZER
;
43 pthread_cond_t qemu_system_cond
= PTHREAD_COND_INITIALIZER
;
44 pthread_cond_t qemu_pause_cond
= PTHREAD_COND_INITIALIZER
;
45 pthread_cond_t qemu_work_cond
= PTHREAD_COND_INITIALIZER
;
46 __thread
struct vcpu_info
*vcpu
;
48 static int qemu_system_ready
;
50 #define SIG_IPI (SIGRTMIN+4)
52 struct qemu_kvm_work_item
{
53 struct qemu_kvm_work_item
*next
;
54 void (*func
)(void *data
);
68 struct qemu_kvm_work_item
*queued_work_first
, *queued_work_last
;
72 static int io_thread_fd
= -1;
73 static int io_thread_sigfd
= -1;
75 static int kvm_debug_stop_requested
;
77 static inline unsigned long kvm_get_thread_id(void)
79 return syscall(SYS_gettid
);
82 static void qemu_cond_wait(pthread_cond_t
*cond
)
84 CPUState
*env
= cpu_single_env
;
85 static const struct timespec ts
= {
90 pthread_cond_timedwait(cond
, &qemu_mutex
, &ts
);
94 CPUState
*qemu_kvm_cpu_env(int index
)
96 return vcpu_info
[index
].env
;
99 static void sig_ipi_handler(int n
)
103 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
105 struct vcpu_info
*vi
= &vcpu_info
[env
->cpu_index
];
106 struct qemu_kvm_work_item wi
;
115 if (!vi
->queued_work_first
)
116 vi
->queued_work_first
= &wi
;
118 vi
->queued_work_last
->next
= &wi
;
119 vi
->queued_work_last
= &wi
;
123 pthread_kill(vi
->thread
, SIG_IPI
);
125 qemu_cond_wait(&qemu_work_cond
);
128 void kvm_update_interrupt_request(CPUState
*env
)
135 if (vcpu
&& env
!= vcpu
->env
&& !vcpu_info
[env
->cpu_index
].signalled
)
139 vcpu_info
[env
->cpu_index
].signalled
= 1;
140 if (vcpu_info
[env
->cpu_index
].thread
)
141 pthread_kill(vcpu_info
[env
->cpu_index
].thread
, SIG_IPI
);
146 void kvm_update_after_sipi(CPUState
*env
)
148 vcpu_info
[env
->cpu_index
].sipi_needed
= 1;
149 kvm_update_interrupt_request(env
);
152 void kvm_apic_init(CPUState
*env
)
154 if (env
->cpu_index
!= 0)
155 vcpu_info
[env
->cpu_index
].init
= 1;
156 kvm_update_interrupt_request(env
);
161 static int try_push_interrupts(void *opaque
)
163 return kvm_arch_try_push_interrupts(opaque
);
166 static void post_kvm_run(void *opaque
, int vcpu
)
169 pthread_mutex_lock(&qemu_mutex
);
170 kvm_arch_post_kvm_run(opaque
, vcpu
);
173 static int pre_kvm_run(void *opaque
, int vcpu
)
175 CPUState
*env
= qemu_kvm_cpu_env(vcpu
);
177 kvm_arch_pre_kvm_run(opaque
, vcpu
);
179 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
181 pthread_mutex_unlock(&qemu_mutex
);
185 static void kvm_do_load_registers(void *_env
)
187 CPUState
*env
= _env
;
189 kvm_arch_load_regs(env
);
192 void kvm_load_registers(CPUState
*env
)
194 if (kvm_enabled() && qemu_system_ready
)
195 on_vcpu(env
, kvm_do_load_registers
, env
);
198 static void kvm_do_save_registers(void *_env
)
200 CPUState
*env
= _env
;
202 kvm_arch_save_regs(env
);
205 void kvm_save_registers(CPUState
*env
)
208 on_vcpu(env
, kvm_do_save_registers
, env
);
211 int kvm_cpu_exec(CPUState
*env
)
215 r
= kvm_run(kvm_context
, env
->cpu_index
);
217 printf("kvm_run returned %d\n", r
);
224 extern int vm_running
;
226 static int has_work(CPUState
*env
)
228 if (!vm_running
|| (env
&& vcpu_info
[env
->cpu_index
].stopped
))
232 return kvm_arch_has_work(env
);
235 static void flush_queued_work(CPUState
*env
)
237 struct vcpu_info
*vi
= &vcpu_info
[env
->cpu_index
];
238 struct qemu_kvm_work_item
*wi
;
240 if (!vi
->queued_work_first
)
243 while ((wi
= vi
->queued_work_first
)) {
244 vi
->queued_work_first
= wi
->next
;
248 vi
->queued_work_last
= NULL
;
249 pthread_cond_broadcast(&qemu_work_cond
);
252 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
259 pthread_mutex_unlock(&qemu_mutex
);
261 ts
.tv_sec
= timeout
/ 1000;
262 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
263 sigemptyset(&waitset
);
264 sigaddset(&waitset
, SIG_IPI
);
266 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
269 pthread_mutex_lock(&qemu_mutex
);
271 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
272 printf("sigtimedwait: %s\n", strerror(e
));
276 cpu_single_env
= env
;
277 flush_queued_work(env
);
279 if (vcpu_info
[env
->cpu_index
].stop
) {
280 vcpu_info
[env
->cpu_index
].stop
= 0;
281 vcpu_info
[env
->cpu_index
].stopped
= 1;
282 pthread_cond_signal(&qemu_pause_cond
);
285 vcpu_info
[env
->cpu_index
].signalled
= 0;
288 static int all_threads_paused(void)
292 for (i
= 0; i
< smp_cpus
; ++i
)
293 if (vcpu_info
[i
].stop
)
298 static void pause_all_threads(void)
302 assert(!cpu_single_env
);
304 for (i
= 0; i
< smp_cpus
; ++i
) {
305 vcpu_info
[i
].stop
= 1;
306 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
308 while (!all_threads_paused())
309 qemu_cond_wait(&qemu_pause_cond
);
312 static void resume_all_threads(void)
316 assert(!cpu_single_env
);
318 for (i
= 0; i
< smp_cpus
; ++i
) {
319 vcpu_info
[i
].stop
= 0;
320 vcpu_info
[i
].stopped
= 0;
321 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
325 static void kvm_vm_state_change_handler(void *context
, int running
)
328 resume_all_threads();
333 static void update_regs_for_sipi(CPUState
*env
)
335 kvm_arch_update_regs_for_sipi(env
);
336 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
337 vcpu_info
[env
->cpu_index
].init
= 0;
340 static void update_regs_for_init(CPUState
*env
)
343 kvm_arch_load_regs(env
);
346 static void setup_kernel_sigmask(CPUState
*env
)
351 sigaddset(&set
, SIGUSR2
);
352 sigaddset(&set
, SIGIO
);
353 sigaddset(&set
, SIGALRM
);
354 sigprocmask(SIG_BLOCK
, &set
, NULL
);
356 sigprocmask(SIG_BLOCK
, NULL
, &set
);
357 sigdelset(&set
, SIG_IPI
);
359 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
362 void qemu_kvm_system_reset(void)
370 for (i
= 0; i
< smp_cpus
; ++i
)
371 kvm_arch_cpu_reset(vcpu_info
[i
].env
);
373 resume_all_threads();
376 static int kvm_main_loop_cpu(CPUState
*env
)
378 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
380 setup_kernel_sigmask(env
);
382 pthread_mutex_lock(&qemu_mutex
);
383 if (kvm_irqchip_in_kernel(kvm_context
))
386 kvm_qemu_init_env(env
);
388 kvm_tpr_vcpu_start(env
);
391 cpu_single_env
= env
;
392 kvm_load_registers(env
);
395 while (!has_work(env
))
396 kvm_main_loop_wait(env
, 1000);
397 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
399 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
400 update_regs_for_sipi(env
);
401 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
402 update_regs_for_init(env
);
403 if (!env
->halted
&& !info
->init
)
405 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
406 kvm_main_loop_wait(env
, 0);
408 pthread_mutex_unlock(&qemu_mutex
);
412 static void *ap_main_loop(void *_env
)
414 CPUState
*env
= _env
;
417 vcpu
= &vcpu_info
[env
->cpu_index
];
419 vcpu
->env
->thread_id
= kvm_get_thread_id();
420 sigfillset(&signals
);
421 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
422 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
423 kvm_qemu_init_env(env
);
425 /* signal VCPU creation */
426 pthread_mutex_lock(&qemu_mutex
);
428 pthread_cond_signal(&qemu_vcpu_cond
);
430 /* and wait for machine initialization */
431 while (!qemu_system_ready
)
432 qemu_cond_wait(&qemu_system_cond
);
433 pthread_mutex_unlock(&qemu_mutex
);
435 kvm_main_loop_cpu(env
);
439 void kvm_init_new_ap(int cpu
, CPUState
*env
)
441 pthread_create(&vcpu_info
[cpu
].thread
, NULL
, ap_main_loop
, env
);
443 while (vcpu_info
[cpu
].created
== 0)
444 qemu_cond_wait(&qemu_vcpu_cond
);
447 int kvm_init_ap(void)
452 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
454 signal(SIG_IPI
, sig_ipi_handler
);
458 void qemu_kvm_notify_work(void)
464 if (io_thread_fd
== -1)
467 memcpy(buffer
, &value
, sizeof(value
));
472 len
= write(io_thread_fd
, buffer
+ offset
, 8 - offset
);
473 if (len
== -1 && errno
== EINTR
)
483 fprintf(stderr
, "failed to notify io thread\n");
486 /* If we have signalfd, we mask out the signals we want to handle and then
487 * use signalfd to listen for them. We rely on whatever the current signal
488 * handler is to dispatch the signals when we receive them.
491 static void sigfd_handler(void *opaque
)
493 int fd
= (unsigned long)opaque
;
494 struct qemu_signalfd_siginfo info
;
495 struct sigaction action
;
500 len
= read(fd
, &info
, sizeof(info
));
501 } while (len
== -1 && errno
== EINTR
);
503 if (len
== -1 && errno
== EAGAIN
)
506 if (len
!= sizeof(info
)) {
507 printf("read from sigfd returned %ld: %m\n", len
);
511 sigaction(info
.ssi_signo
, NULL
, &action
);
512 if (action
.sa_handler
)
513 action
.sa_handler(info
.ssi_signo
);
518 /* Used to break IO thread out of select */
519 static void io_thread_wakeup(void *opaque
)
521 int fd
= (unsigned long)opaque
;
528 len
= read(fd
, buffer
+ offset
, 8 - offset
);
529 if (len
== -1 && errno
== EINTR
)
539 int kvm_main_loop(void)
545 io_thread
= pthread_self();
546 qemu_system_ready
= 1;
548 if (kvm_eventfd(fds
) == -1) {
549 fprintf(stderr
, "failed to create eventfd\n");
553 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
554 (void *)(unsigned long)fds
[0]);
556 io_thread_fd
= fds
[1];
559 sigaddset(&mask
, SIGIO
);
560 sigaddset(&mask
, SIGALRM
);
561 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
563 sigfd
= qemu_signalfd(&mask
);
565 fprintf(stderr
, "failed to create signalfd\n");
569 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
571 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
572 (void *)(unsigned long)sigfd
);
574 pthread_cond_broadcast(&qemu_system_cond
);
576 io_thread_sigfd
= sigfd
;
577 cpu_single_env
= NULL
;
580 main_loop_wait(1000);
581 if (qemu_shutdown_requested())
583 else if (qemu_powerdown_requested())
584 qemu_system_powerdown();
585 else if (qemu_reset_requested())
586 qemu_kvm_system_reset();
587 else if (kvm_debug_stop_requested
) {
589 kvm_debug_stop_requested
= 0;
594 pthread_mutex_unlock(&qemu_mutex
);
599 static int kvm_debug(void *opaque
, int vcpu
)
601 kvm_debug_stop_requested
= 1;
602 vcpu_info
[vcpu
].stopped
= 1;
606 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
608 *data
= cpu_inb(0, addr
);
612 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
614 *data
= cpu_inw(0, addr
);
618 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
620 *data
= cpu_inl(0, addr
);
624 #define PM_IO_BASE 0xb000
626 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
631 cpu_outb(0, 0xb3, 0);
638 x
= cpu_inw(0, PM_IO_BASE
+ 4);
640 cpu_outw(0, PM_IO_BASE
+ 4, x
);
647 x
= cpu_inw(0, PM_IO_BASE
+ 4);
649 cpu_outw(0, PM_IO_BASE
+ 4, x
);
657 cpu_outb(0, addr
, data
);
661 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
663 cpu_outw(0, addr
, data
);
667 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
669 cpu_outl(0, addr
, data
);
673 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
675 cpu_physical_memory_rw(addr
, data
, len
, 0);
679 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
681 cpu_physical_memory_rw(addr
, data
, len
, 1);
685 static int kvm_io_window(void *opaque
)
691 static int kvm_halt(void *opaque
, int vcpu
)
693 return kvm_arch_halt(opaque
, vcpu
);
696 static int kvm_shutdown(void *opaque
, int vcpu
)
698 /* stop the current vcpu from going back to guest mode */
699 vcpu_info
[cpu_single_env
->cpu_index
].stopped
= 1;
701 qemu_system_reset_request();
705 static struct kvm_callbacks qemu_kvm_ops
= {
713 .mmio_read
= kvm_mmio_read
,
714 .mmio_write
= kvm_mmio_write
,
716 .shutdown
= kvm_shutdown
,
717 .io_window
= kvm_io_window
,
718 .try_push_interrupts
= try_push_interrupts
,
719 .post_kvm_run
= post_kvm_run
,
720 .pre_kvm_run
= pre_kvm_run
,
722 .tpr_access
= handle_tpr_access
,
725 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
726 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
732 /* Try to initialize kvm */
733 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
737 pthread_mutex_lock(&qemu_mutex
);
742 int kvm_qemu_create_context(void)
746 kvm_disable_irqchip_creation(kvm_context
);
749 kvm_disable_pit_creation(kvm_context
);
751 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
755 r
= kvm_arch_qemu_create_context();
761 void kvm_qemu_destroy(void)
763 kvm_finalize(kvm_context
);
766 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
768 unsigned long phys_offset
)
771 unsigned long area_flags
= phys_offset
& ~TARGET_PAGE_MASK
;
773 phys_offset
&= ~IO_MEM_ROM
;
775 if (area_flags
== IO_MEM_UNASSIGNED
) {
776 kvm_unregister_memory_area(kvm_context
, start_addr
, size
);
780 r
= kvm_is_containing_region(kvm_context
, start_addr
, size
);
784 if (area_flags
>= TLB_MMIO
)
787 r
= kvm_register_phys_mem(kvm_context
, start_addr
,
788 phys_ram_base
+ phys_offset
,
791 printf("kvm_cpu_register_physical_memory: failed\n");
797 int kvm_setup_guest_memory(void *area
, unsigned long size
)
802 if (kvm_enabled() && !kvm_has_sync_mmu(kvm_context
))
803 ret
= madvise(area
, size
, MADV_DONTFORK
);
812 int kvm_qemu_check_extension(int ext
)
814 return kvm_check_extension(kvm_context
, ext
);
817 int kvm_qemu_init_env(CPUState
*cenv
)
819 return kvm_arch_qemu_init_env(cenv
);
822 struct kvm_guest_debug_data
{
823 struct kvm_debug_guest dbg
;
827 void kvm_invoke_guest_debug(void *data
)
829 struct kvm_guest_debug_data
*dbg_data
= data
;
831 dbg_data
->err
= kvm_guest_debug(kvm_context
, cpu_single_env
->cpu_index
,
835 int kvm_update_debugger(CPUState
*env
)
837 struct kvm_guest_debug_data data
;
840 memset(data
.dbg
.breakpoints
, 0, sizeof(data
.dbg
.breakpoints
));
842 data
.dbg
.enabled
= 0;
843 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
844 data
.dbg
.enabled
= 1;
845 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
846 data
.dbg
.breakpoints
[i
].enabled
= 1;
847 data
.dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
849 data
.dbg
.singlestep
= env
->singlestep_enabled
;
851 on_vcpu(env
, kvm_invoke_guest_debug
, &data
);
857 * dirty pages logging
859 /* FIXME: use unsigned long pointer instead of unsigned char */
860 unsigned char *kvm_dirty_bitmap
= NULL
;
861 int kvm_physical_memory_set_dirty_tracking(int enable
)
869 if (!kvm_dirty_bitmap
) {
870 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
871 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
872 if (kvm_dirty_bitmap
== NULL
) {
873 perror("Failed to allocate dirty pages bitmap");
877 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
882 if (kvm_dirty_bitmap
) {
883 r
= kvm_dirty_pages_log_reset(kvm_context
);
884 qemu_free(kvm_dirty_bitmap
);
885 kvm_dirty_bitmap
= NULL
;
891 /* get kvm's dirty pages bitmap and update qemu's */
892 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
893 unsigned char *bitmap
,
895 unsigned long mem_size
)
897 unsigned int i
, j
, n
=0;
899 unsigned page_number
, addr
, addr1
;
900 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
903 * bitmap-traveling is faster than memory-traveling (for addr...)
904 * especially when most of the memory is not dirty.
906 for (i
=0; i
<len
; i
++) {
911 page_number
= i
* 8 + j
;
912 addr1
= page_number
* TARGET_PAGE_SIZE
;
913 addr
= offset
+ addr1
;
914 cpu_physical_memory_set_dirty(addr
);
920 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
921 void *bitmap
, void *opaque
)
923 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
927 * get kvm's dirty pages bitmap and update qemu's
928 * we only care about physical ram, which resides in slots 0 and 3
930 int kvm_update_dirty_pages_log(void)
935 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
936 kvm_dirty_bitmap
, NULL
,
937 kvm_get_dirty_bitmap_cb
);
941 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
943 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
944 unsigned int brsize
= BITMAP_SIZE(ram_size
);
945 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
946 unsigned int extra_bytes
= (extra_pages
+7)/8;
947 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
948 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
950 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
951 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
952 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
957 #ifdef KVM_CAP_IRQCHIP
959 int kvm_set_irq(int irq
, int level
)
961 return kvm_set_irq_level(kvm_context
, irq
, level
);
966 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
968 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
971 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
972 unsigned long size
, int log
, int writable
)
974 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
977 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
980 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);
983 void kvm_mutex_unlock(void)
985 assert(!cpu_single_env
);
986 pthread_mutex_unlock(&qemu_mutex
);
989 void kvm_mutex_lock(void)
991 pthread_mutex_lock(&qemu_mutex
);
992 cpu_single_env
= NULL
;
995 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr
, unsigned int size
)
997 return kvm_register_coalesced_mmio(kvm_context
, addr
, size
);
1000 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr
,
1003 return kvm_unregister_coalesced_mmio(kvm_context
, addr
, size
);