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"
22 #include <sys/utsname.h>
23 #include <sys/syscall.h>
25 extern void perror(const char *s
);
27 kvm_context_t kvm_context
;
31 static int qemu_kvm_reset_requested
;
33 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
34 pthread_cond_t qemu_aio_cond
= PTHREAD_COND_INITIALIZER
;
35 pthread_cond_t qemu_vcpu_cond
= PTHREAD_COND_INITIALIZER
;
36 pthread_cond_t qemu_system_cond
= PTHREAD_COND_INITIALIZER
;
37 __thread
struct vcpu_info
*vcpu
;
39 static int qemu_system_ready
;
41 struct qemu_kvm_signal_table
{
46 static struct qemu_kvm_signal_table io_signal_table
;
47 static struct qemu_kvm_signal_table vcpu_signal_table
;
49 #define SIG_IPI (SIGRTMIN+4)
65 static inline unsigned long kvm_get_thread_id(void)
67 return syscall(SYS_gettid
);
70 CPUState
*qemu_kvm_cpu_env(int index
)
72 return vcpu_info
[index
].env
;
75 static void sig_ipi_handler(int n
)
79 void kvm_update_interrupt_request(CPUState
*env
)
86 if (vcpu
&& env
!= vcpu
->env
&& !vcpu_info
[env
->cpu_index
].signalled
)
90 vcpu_info
[env
->cpu_index
].signalled
= 1;
91 if (vcpu_info
[env
->cpu_index
].thread
)
92 pthread_kill(vcpu_info
[env
->cpu_index
].thread
, SIG_IPI
);
97 void kvm_update_after_sipi(CPUState
*env
)
99 vcpu_info
[env
->cpu_index
].sipi_needed
= 1;
100 kvm_update_interrupt_request(env
);
103 void kvm_apic_init(CPUState
*env
)
105 if (env
->cpu_index
!= 0)
106 vcpu_info
[env
->cpu_index
].init
= 1;
107 kvm_update_interrupt_request(env
);
112 static int try_push_interrupts(void *opaque
)
114 return kvm_arch_try_push_interrupts(opaque
);
117 static void post_kvm_run(void *opaque
, int vcpu
)
120 pthread_mutex_lock(&qemu_mutex
);
121 kvm_arch_post_kvm_run(opaque
, vcpu
);
124 static int pre_kvm_run(void *opaque
, int vcpu
)
126 CPUState
*env
= qemu_kvm_cpu_env(vcpu
);
128 kvm_arch_pre_kvm_run(opaque
, vcpu
);
130 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
132 pthread_mutex_unlock(&qemu_mutex
);
136 void kvm_load_registers(CPUState
*env
)
139 kvm_arch_load_regs(env
);
142 void kvm_save_registers(CPUState
*env
)
145 kvm_arch_save_regs(env
);
148 int kvm_cpu_exec(CPUState
*env
)
152 r
= kvm_run(kvm_context
, env
->cpu_index
);
154 printf("kvm_run returned %d\n", r
);
161 extern int vm_running
;
163 static int has_work(CPUState
*env
)
165 if (!vm_running
|| (env
&& vcpu_info
[env
->cpu_index
].stopped
))
167 if (!(env
->hflags
& HF_HALTED_MASK
))
169 return kvm_arch_has_work(env
);
172 static int kvm_process_signal(int si_signo
)
178 pthread_cond_signal(&qemu_aio_cond
);
182 sigaction(si_signo
, NULL
, &sa
);
183 sa
.sa_handler(si_signo
);
190 static int kvm_eat_signal(struct qemu_kvm_signal_table
*waitset
, CPUState
*env
,
197 ts
.tv_sec
= timeout
/ 1000;
198 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
199 r
= sigtimedwait(&waitset
->sigset
, &siginfo
, &ts
);
200 if (r
== -1 && (errno
== EAGAIN
|| errno
== EINTR
) && !timeout
)
203 pthread_mutex_lock(&qemu_mutex
);
205 cpu_single_env
= vcpu
->env
;
206 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
207 printf("sigtimedwait: %s\n", strerror(e
));
211 ret
= kvm_process_signal(siginfo
.si_signo
);
213 if (env
&& vcpu_info
[env
->cpu_index
].stop
) {
214 vcpu_info
[env
->cpu_index
].stop
= 0;
215 vcpu_info
[env
->cpu_index
].stopped
= 1;
216 pthread_kill(io_thread
, SIGUSR1
);
218 pthread_mutex_unlock(&qemu_mutex
);
224 static void kvm_eat_signals(CPUState
*env
, int timeout
)
227 struct qemu_kvm_signal_table
*waitset
= &vcpu_signal_table
;
229 while (kvm_eat_signal(waitset
, env
, 0))
232 r
= kvm_eat_signal(waitset
, env
, timeout
);
234 while (kvm_eat_signal(waitset
, env
, 0))
239 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
241 pthread_mutex_unlock(&qemu_mutex
);
242 kvm_eat_signals(env
, timeout
);
243 pthread_mutex_lock(&qemu_mutex
);
244 cpu_single_env
= env
;
245 vcpu_info
[env
->cpu_index
].signalled
= 0;
248 static int all_threads_paused(void)
252 for (i
= 0; i
< smp_cpus
; ++i
)
253 if (vcpu_info
[i
].stop
)
258 static void pause_all_threads(void)
262 for (i
= 0; i
< smp_cpus
; ++i
) {
263 vcpu_info
[i
].stop
= 1;
264 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
266 while (!all_threads_paused()) {
267 pthread_mutex_unlock(&qemu_mutex
);
268 kvm_eat_signal(&io_signal_table
, NULL
, 1000);
269 pthread_mutex_lock(&qemu_mutex
);
270 cpu_single_env
= NULL
;
274 static void resume_all_threads(void)
278 for (i
= 0; i
< smp_cpus
; ++i
) {
279 vcpu_info
[i
].stop
= 0;
280 vcpu_info
[i
].stopped
= 0;
281 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
285 static void kvm_vm_state_change_handler(void *context
, int running
)
288 resume_all_threads();
293 static void update_regs_for_sipi(CPUState
*env
)
295 kvm_arch_update_regs_for_sipi(env
);
296 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
297 vcpu_info
[env
->cpu_index
].init
= 0;
300 static void update_regs_for_init(CPUState
*env
)
303 kvm_arch_load_regs(env
);
306 static void setup_kernel_sigmask(CPUState
*env
)
310 sigprocmask(SIG_BLOCK
, NULL
, &set
);
311 sigdelset(&set
, SIG_IPI
);
313 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
316 void qemu_kvm_system_reset_request(void)
320 for (i
= 0; i
< smp_cpus
; ++i
) {
321 vcpu_info
[i
].reload_regs
= 1;
322 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
327 static int kvm_main_loop_cpu(CPUState
*env
)
329 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
331 setup_kernel_sigmask(env
);
333 pthread_mutex_lock(&qemu_mutex
);
334 if (kvm_irqchip_in_kernel(kvm_context
))
335 env
->hflags
&= ~HF_HALTED_MASK
;
337 kvm_qemu_init_env(env
);
338 env
->ready_for_interrupt_injection
= 1;
340 kvm_tpr_vcpu_start(env
);
343 cpu_single_env
= env
;
345 while (!has_work(env
))
346 kvm_main_loop_wait(env
, 10);
347 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
348 env
->hflags
&= ~HF_HALTED_MASK
;
349 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
350 update_regs_for_sipi(env
);
351 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
352 update_regs_for_init(env
);
353 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
355 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
356 kvm_main_loop_wait(env
, 0);
357 if (info
->reload_regs
) {
358 info
->reload_regs
= 0;
359 if (env
->cpu_index
== 0) /* ap needs to be placed in INIT */
360 kvm_arch_load_regs(env
);
363 pthread_mutex_unlock(&qemu_mutex
);
367 static void *ap_main_loop(void *_env
)
369 CPUState
*env
= _env
;
372 vcpu
= &vcpu_info
[env
->cpu_index
];
374 vcpu
->env
->thread_id
= kvm_get_thread_id();
375 sigfillset(&signals
);
376 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
377 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
378 kvm_qemu_init_env(env
);
380 /* signal VCPU creation */
381 pthread_mutex_lock(&qemu_mutex
);
383 pthread_cond_signal(&qemu_vcpu_cond
);
385 /* and wait for machine initialization */
386 while (!qemu_system_ready
)
387 pthread_cond_wait(&qemu_system_cond
, &qemu_mutex
);
388 pthread_mutex_unlock(&qemu_mutex
);
390 kvm_main_loop_cpu(env
);
394 static void qemu_kvm_init_signal_table(struct qemu_kvm_signal_table
*sigtab
)
396 sigemptyset(&sigtab
->sigset
);
397 sigfillset(&sigtab
->negsigset
);
400 static void kvm_add_signal(struct qemu_kvm_signal_table
*sigtab
, int signum
)
402 sigaddset(&sigtab
->sigset
, signum
);
403 sigdelset(&sigtab
->negsigset
, signum
);
406 void kvm_init_new_ap(int cpu
, CPUState
*env
)
408 pthread_create(&vcpu_info
[cpu
].thread
, NULL
, ap_main_loop
, env
);
410 while (vcpu_info
[cpu
].created
== 0)
411 pthread_cond_wait(&qemu_vcpu_cond
, &qemu_mutex
);
414 static void qemu_kvm_init_signal_tables(void)
416 qemu_kvm_init_signal_table(&io_signal_table
);
417 qemu_kvm_init_signal_table(&vcpu_signal_table
);
419 kvm_add_signal(&io_signal_table
, SIGIO
);
420 kvm_add_signal(&io_signal_table
, SIGALRM
);
421 kvm_add_signal(&io_signal_table
, SIGUSR1
);
422 kvm_add_signal(&io_signal_table
, SIGUSR2
);
424 kvm_add_signal(&vcpu_signal_table
, SIG_IPI
);
426 sigprocmask(SIG_BLOCK
, &io_signal_table
.sigset
, NULL
);
429 int kvm_init_ap(void)
434 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
435 qemu_kvm_init_signal_tables();
437 signal(SIG_IPI
, sig_ipi_handler
);
441 void qemu_kvm_notify_work(void)
444 pthread_kill(io_thread
, SIGUSR1
);
448 * The IO thread has all signals that inform machine events
449 * blocked (io_signal_table), so it won't get interrupted
450 * while processing in main_loop_wait().
453 int kvm_main_loop(void)
455 io_thread
= pthread_self();
456 qemu_system_ready
= 1;
457 pthread_mutex_unlock(&qemu_mutex
);
459 pthread_cond_broadcast(&qemu_system_cond
);
462 kvm_eat_signal(&io_signal_table
, NULL
, 1000);
463 pthread_mutex_lock(&qemu_mutex
);
464 cpu_single_env
= NULL
;
466 if (qemu_shutdown_requested())
468 else if (qemu_powerdown_requested())
469 qemu_system_powerdown();
470 else if (qemu_reset_requested()) {
471 pthread_kill(vcpu_info
[0].thread
, SIG_IPI
);
472 qemu_kvm_reset_requested
= 1;
474 pthread_mutex_unlock(&qemu_mutex
);
478 pthread_mutex_unlock(&qemu_mutex
);
483 static int kvm_debug(void *opaque
, int vcpu
)
485 CPUState
*env
= cpu_single_env
;
487 env
->exception_index
= EXCP_DEBUG
;
491 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
493 *data
= cpu_inb(0, addr
);
497 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
499 *data
= cpu_inw(0, addr
);
503 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
505 *data
= cpu_inl(0, addr
);
509 #define PM_IO_BASE 0xb000
511 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
516 cpu_outb(0, 0xb3, 0);
523 x
= cpu_inw(0, PM_IO_BASE
+ 4);
525 cpu_outw(0, PM_IO_BASE
+ 4, x
);
532 x
= cpu_inw(0, PM_IO_BASE
+ 4);
534 cpu_outw(0, PM_IO_BASE
+ 4, x
);
542 cpu_outb(0, addr
, data
);
546 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
548 cpu_outw(0, addr
, data
);
552 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
554 cpu_outl(0, addr
, data
);
558 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
560 cpu_physical_memory_rw(addr
, data
, len
, 0);
564 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
566 cpu_physical_memory_rw(addr
, data
, len
, 1);
570 static int kvm_io_window(void *opaque
)
576 static int kvm_halt(void *opaque
, int vcpu
)
578 return kvm_arch_halt(opaque
, vcpu
);
581 static int kvm_shutdown(void *opaque
, int vcpu
)
583 qemu_system_reset_request();
587 static struct kvm_callbacks qemu_kvm_ops
= {
595 .mmio_read
= kvm_mmio_read
,
596 .mmio_write
= kvm_mmio_write
,
598 .shutdown
= kvm_shutdown
,
599 .io_window
= kvm_io_window
,
600 .try_push_interrupts
= try_push_interrupts
,
601 .post_kvm_run
= post_kvm_run
,
602 .pre_kvm_run
= pre_kvm_run
,
604 .tpr_access
= handle_tpr_access
,
607 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
608 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
614 /* Try to initialize kvm */
615 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
619 pthread_mutex_lock(&qemu_mutex
);
624 int kvm_qemu_create_context(void)
628 kvm_disable_irqchip_creation(kvm_context
);
631 kvm_disable_pit_creation(kvm_context
);
633 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
637 r
= kvm_arch_qemu_create_context();
643 void kvm_qemu_destroy(void)
645 kvm_finalize(kvm_context
);
648 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
650 unsigned long phys_offset
)
652 #ifdef KVM_CAP_USER_MEMORY
655 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
657 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
658 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
661 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
663 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
664 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
665 phys_ram_base
+ phys_offset
,
668 if (phys_offset
& IO_MEM_ROM
) {
669 phys_offset
&= ~IO_MEM_ROM
;
670 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
672 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
673 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
674 phys_ram_base
+ phys_offset
,
678 printf("kvm_cpu_register_physical_memory: failed\n");
684 if (phys_offset
& IO_MEM_ROM
) {
685 phys_offset
&= ~IO_MEM_ROM
;
686 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
690 int kvm_qemu_check_extension(int ext
)
692 return kvm_check_extension(kvm_context
, ext
);
695 int kvm_qemu_init_env(CPUState
*cenv
)
697 return kvm_arch_qemu_init_env(cenv
);
700 int kvm_update_debugger(CPUState
*env
)
702 struct kvm_debug_guest dbg
;
706 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
708 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
709 dbg
.breakpoints
[i
].enabled
= 1;
710 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
712 dbg
.singlestep
= env
->singlestep_enabled
;
714 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
719 * dirty pages logging
721 /* FIXME: use unsigned long pointer instead of unsigned char */
722 unsigned char *kvm_dirty_bitmap
= NULL
;
723 int kvm_physical_memory_set_dirty_tracking(int enable
)
731 if (!kvm_dirty_bitmap
) {
732 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
733 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
734 if (kvm_dirty_bitmap
== NULL
) {
735 perror("Failed to allocate dirty pages bitmap");
739 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
744 if (kvm_dirty_bitmap
) {
745 r
= kvm_dirty_pages_log_reset(kvm_context
);
746 qemu_free(kvm_dirty_bitmap
);
747 kvm_dirty_bitmap
= NULL
;
753 /* get kvm's dirty pages bitmap and update qemu's */
754 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
755 unsigned char *bitmap
,
757 unsigned long mem_size
)
759 unsigned int i
, j
, n
=0;
761 unsigned page_number
, addr
, addr1
;
762 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
765 * bitmap-traveling is faster than memory-traveling (for addr...)
766 * especially when most of the memory is not dirty.
768 for (i
=0; i
<len
; i
++) {
773 page_number
= i
* 8 + j
;
774 addr1
= page_number
* TARGET_PAGE_SIZE
;
775 addr
= offset
+ addr1
;
776 cpu_physical_memory_set_dirty(addr
);
782 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
783 void *bitmap
, void *opaque
)
785 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
789 * get kvm's dirty pages bitmap and update qemu's
790 * we only care about physical ram, which resides in slots 0 and 3
792 int kvm_update_dirty_pages_log(void)
797 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
798 kvm_dirty_bitmap
, NULL
,
799 kvm_get_dirty_bitmap_cb
);
803 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
805 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
806 unsigned int brsize
= BITMAP_SIZE(ram_size
);
807 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
808 unsigned int extra_bytes
= (extra_pages
+7)/8;
809 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
810 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
812 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
813 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
814 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
819 #ifdef KVM_CAP_IRQCHIP
821 int kvm_set_irq(int irq
, int level
)
823 return kvm_set_irq_level(kvm_context
, irq
, level
);
828 void qemu_kvm_aio_wait_start(void)
832 void qemu_kvm_aio_wait(void)
834 CPUState
*cpu_single
= cpu_single_env
;
836 if (!cpu_single_env
) {
837 pthread_mutex_unlock(&qemu_mutex
);
838 kvm_eat_signal(&io_signal_table
, NULL
, 1000);
839 pthread_mutex_lock(&qemu_mutex
);
840 cpu_single_env
= NULL
;
842 pthread_cond_wait(&qemu_aio_cond
, &qemu_mutex
);
843 cpu_single_env
= cpu_single
;
847 void qemu_kvm_aio_wait_end(void)
851 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
853 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
856 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
857 unsigned long size
, int log
, int writable
)
859 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
862 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
865 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);