3 #include "config-host.h"
6 #define KVM_ALLOWED_DEFAULT 1
8 #define KVM_ALLOWED_DEFAULT 0
11 int kvm_allowed
= KVM_ALLOWED_DEFAULT
;
23 #include <sys/utsname.h>
25 extern void perror(const char *s
);
27 kvm_context_t kvm_context
;
31 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
32 __thread CPUState
*vcpu_env
;
34 static sigset_t io_sigset
, io_negsigset
;
38 #define SIG_IPI (SIGRTMIN+4)
49 static void sig_ipi_handler(int n
)
53 void kvm_update_interrupt_request(CPUState
*env
)
55 if (env
&& env
!= vcpu_env
) {
56 if (vcpu_info
[env
->cpu_index
].signalled
)
58 vcpu_info
[env
->cpu_index
].signalled
= 1;
59 if (vcpu_info
[env
->cpu_index
].thread
)
60 pthread_kill(vcpu_info
[env
->cpu_index
].thread
, SIG_IPI
);
64 void kvm_update_after_sipi(CPUState
*env
)
66 vcpu_info
[env
->cpu_index
].sipi_needed
= 1;
67 kvm_update_interrupt_request(env
);
70 * the qemu bios waits using a busy loop that's much too short for
71 * kvm. add a wait after the first sipi.
74 static int first_sipi
= 1;
83 void kvm_apic_init(CPUState
*env
)
85 if (env
->cpu_index
!= 0)
86 vcpu_info
[env
->cpu_index
].init
= 1;
87 kvm_update_interrupt_request(env
);
92 static int try_push_interrupts(void *opaque
)
94 return kvm_arch_try_push_interrupts(opaque
);
97 static void post_kvm_run(void *opaque
, int vcpu
)
100 pthread_mutex_lock(&qemu_mutex
);
101 kvm_arch_post_kvm_run(opaque
, vcpu
);
104 static int pre_kvm_run(void *opaque
, int vcpu
)
106 CPUState
*env
= cpu_single_env
;
108 if (env
->cpu_index
== 0 && wait_hack
) {
113 pthread_mutex_unlock(&qemu_mutex
);
114 for (i
= 0; i
< 10; ++i
)
116 pthread_mutex_lock(&qemu_mutex
);
119 kvm_arch_pre_kvm_run(opaque
, vcpu
);
121 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
123 pthread_mutex_unlock(&qemu_mutex
);
127 void kvm_load_registers(CPUState
*env
)
130 kvm_arch_load_regs(env
);
133 void kvm_save_registers(CPUState
*env
)
136 kvm_arch_save_regs(env
);
139 int kvm_cpu_exec(CPUState
*env
)
143 r
= kvm_run(kvm_context
, env
->cpu_index
);
145 printf("kvm_run returned %d\n", r
);
152 extern int vm_running
;
154 static int has_work(CPUState
*env
)
158 if (!(env
->hflags
& HF_HALTED_MASK
))
160 return kvm_arch_has_work(env
);
163 static int kvm_eat_signal(CPUState
*env
, int timeout
)
170 ts
.tv_sec
= timeout
/ 1000;
171 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
172 r
= sigtimedwait(&io_sigset
, &siginfo
, &ts
);
173 if (r
== -1 && (errno
== EAGAIN
|| errno
== EINTR
) && !timeout
)
176 pthread_mutex_lock(&qemu_mutex
);
177 cpu_single_env
= vcpu_env
;
178 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
179 printf("sigtimedwait: %s\n", strerror(e
));
183 sigaction(siginfo
.si_signo
, NULL
, &sa
);
184 sa
.sa_handler(siginfo
.si_signo
);
187 pthread_mutex_unlock(&qemu_mutex
);
193 static void kvm_eat_signals(CPUState
*env
, int timeout
)
197 while (kvm_eat_signal(env
, 0))
200 r
= kvm_eat_signal(env
, timeout
);
202 while (kvm_eat_signal(env
, 0))
206 * we call select() even if no signal was received, to account for
207 * for which there is no signal handler installed.
209 pthread_mutex_lock(&qemu_mutex
);
210 cpu_single_env
= vcpu_env
;
212 pthread_mutex_unlock(&qemu_mutex
);
215 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
217 pthread_mutex_unlock(&qemu_mutex
);
218 if (env
->cpu_index
== 0)
219 kvm_eat_signals(env
, timeout
);
221 if (!kvm_irqchip_in_kernel(kvm_context
) &&
222 (timeout
|| vcpu_info
[env
->cpu_index
].stopped
)) {
228 sigaddset(&set
, SIG_IPI
);
238 sigaddset(&set
, SIG_IPI
);
239 sigtimedwait(&set
, &siginfo
, &ts
);
241 if (vcpu_info
[env
->cpu_index
].stop
) {
242 vcpu_info
[env
->cpu_index
].stop
= 0;
243 vcpu_info
[env
->cpu_index
].stopped
= 1;
244 pthread_kill(vcpu_info
[0].thread
, SIG_IPI
);
248 pthread_mutex_lock(&qemu_mutex
);
249 cpu_single_env
= env
;
250 vcpu_info
[env
->cpu_index
].signalled
= 0;
253 static int all_threads_paused(void)
257 for (i
= 1; i
< smp_cpus
; ++i
)
258 if (vcpu_info
[i
].stopped
)
263 static void pause_other_threads(void)
267 for (i
= 1; i
< smp_cpus
; ++i
) {
268 vcpu_info
[i
].stop
= 1;
269 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
271 while (!all_threads_paused())
272 kvm_eat_signals(vcpu_env
, 0);
275 static void resume_other_threads(void)
279 for (i
= 1; i
< smp_cpus
; ++i
) {
280 vcpu_info
[i
].stop
= 0;
281 vcpu_info
[i
].stopped
= 0;
282 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
286 static void kvm_vm_state_change_handler(void *context
, int running
)
289 resume_other_threads();
291 pause_other_threads();
294 static void update_regs_for_sipi(CPUState
*env
)
296 kvm_arch_update_regs_for_sipi(env
);
297 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
298 vcpu_info
[env
->cpu_index
].init
= 0;
301 static void update_regs_for_init(CPUState
*env
)
304 kvm_arch_load_regs(env
);
307 static void setup_kernel_sigmask(CPUState
*env
)
311 sigprocmask(SIG_BLOCK
, NULL
, &set
);
312 sigdelset(&set
, SIG_IPI
);
313 if (env
->cpu_index
== 0)
314 sigandset(&set
, &set
, &io_negsigset
);
316 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
319 static int kvm_main_loop_cpu(CPUState
*env
)
321 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
323 setup_kernel_sigmask(env
);
324 pthread_mutex_lock(&qemu_mutex
);
326 kvm_qemu_init_env(env
);
327 env
->ready_for_interrupt_injection
= 1;
329 cpu_single_env
= env
;
331 while (!has_work(env
))
332 kvm_main_loop_wait(env
, 10);
333 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
334 env
->hflags
&= ~HF_HALTED_MASK
;
335 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
336 update_regs_for_sipi(env
);
337 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
338 update_regs_for_init(env
);
339 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
341 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
342 kvm_main_loop_wait(env
, 0);
343 if (qemu_shutdown_requested())
345 else if (qemu_powerdown_requested())
346 qemu_system_powerdown();
347 else if (qemu_reset_requested()) {
348 env
->interrupt_request
= 0;
350 kvm_arch_load_regs(env
);
353 pthread_mutex_unlock(&qemu_mutex
);
357 static void *ap_main_loop(void *_env
)
359 CPUState
*env
= _env
;
363 sigfillset(&signals
);
364 //sigdelset(&signals, SIG_IPI);
365 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
366 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
367 kvm_qemu_init_env(env
);
368 if (kvm_irqchip_in_kernel(kvm_context
))
369 env
->hflags
&= ~HF_HALTED_MASK
;
370 kvm_main_loop_cpu(env
);
374 static void kvm_add_signal(int signum
)
376 sigaddset(&io_sigset
, signum
);
377 sigdelset(&io_negsigset
, signum
);
378 sigprocmask(SIG_BLOCK
, &io_sigset
, NULL
);
381 int kvm_init_ap(void)
383 CPUState
*env
= first_cpu
->next_cpu
;
386 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
387 sigemptyset(&io_sigset
);
388 sigfillset(&io_negsigset
);
389 kvm_add_signal(SIGIO
);
390 kvm_add_signal(SIGALRM
);
391 kvm_add_signal(SIGUSR2
);
392 if (!kvm_irqchip_in_kernel(kvm_context
))
393 kvm_add_signal(SIG_IPI
);
395 vcpu_env
= first_cpu
;
396 signal(SIG_IPI
, sig_ipi_handler
);
397 for (i
= 1; i
< smp_cpus
; ++i
) {
398 pthread_create(&vcpu_info
[i
].thread
, NULL
, ap_main_loop
, env
);
404 int kvm_main_loop(void)
406 vcpu_info
[0].thread
= pthread_self();
407 return kvm_main_loop_cpu(first_cpu
);
410 static int kvm_debug(void *opaque
, int vcpu
)
412 CPUState
*env
= cpu_single_env
;
414 env
->exception_index
= EXCP_DEBUG
;
418 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
420 *data
= cpu_inb(0, addr
);
424 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
426 *data
= cpu_inw(0, addr
);
430 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
432 *data
= cpu_inl(0, addr
);
436 #define PM_IO_BASE 0xb000
438 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
443 cpu_outb(0, 0xb3, 0);
450 x
= cpu_inw(0, PM_IO_BASE
+ 4);
452 cpu_outw(0, PM_IO_BASE
+ 4, x
);
459 x
= cpu_inw(0, PM_IO_BASE
+ 4);
461 cpu_outw(0, PM_IO_BASE
+ 4, x
);
469 cpu_outb(0, addr
, data
);
473 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
475 cpu_outw(0, addr
, data
);
479 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
481 cpu_outl(0, addr
, data
);
485 static int kvm_mmio_read(void *opaque
, uint64_t addr
,
486 uint8_t *data
, int len
, int is_write
)
488 cpu_physical_memory_rw(addr
, data
, len
, 0);
492 static int kvm_mmio_write(void *opaque
, uint64_t addr
,
493 uint8_t *data
, int len
, int is_write
)
495 cpu_physical_memory_rw(addr
, data
, len
, 1);
499 static int kvm_io_window(void *opaque
)
505 static int kvm_halt(void *opaque
, int vcpu
)
507 return kvm_arch_halt(opaque
, vcpu
);
510 static int kvm_shutdown(void *opaque
, int vcpu
)
512 qemu_system_reset_request();
516 static struct kvm_callbacks qemu_kvm_ops
= {
524 .mmio_read
= kvm_mmio_read
,
525 .mmio_write
= kvm_mmio_write
,
527 .shutdown
= kvm_shutdown
,
528 .io_window
= kvm_io_window
,
529 .try_push_interrupts
= try_push_interrupts
,
530 .post_kvm_run
= post_kvm_run
,
531 .pre_kvm_run
= pre_kvm_run
,
536 /* Try to initialize kvm */
537 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
545 int kvm_qemu_create_context(void)
549 kvm_disable_irqchip_creation(kvm_context
);
551 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
555 r
= kvm_arch_qemu_create_context();
561 void kvm_qemu_destroy(void)
563 kvm_finalize(kvm_context
);
566 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
568 unsigned long phys_offset
)
570 #ifdef KVM_CAP_USER_MEMORY
573 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
575 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
576 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
579 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
581 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
582 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
583 phys_ram_base
+ phys_offset
,
586 if (phys_offset
& IO_MEM_ROM
) {
587 phys_offset
&= ~IO_MEM_ROM
;
588 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
590 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
591 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
592 phys_ram_base
+ phys_offset
,
596 printf("kvm_cpu_register_physical_memory: failed\n");
602 if (phys_offset
& IO_MEM_ROM
) {
603 phys_offset
&= ~IO_MEM_ROM
;
604 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
608 int kvm_qemu_check_extension(int ext
)
610 return kvm_check_extension(kvm_context
, ext
);
613 int kvm_qemu_init_env(CPUState
*cenv
)
615 return kvm_arch_qemu_init_env(cenv
);
618 int kvm_update_debugger(CPUState
*env
)
620 struct kvm_debug_guest dbg
;
624 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
626 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
627 dbg
.breakpoints
[i
].enabled
= 1;
628 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
630 dbg
.singlestep
= env
->singlestep_enabled
;
632 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
637 * dirty pages logging
639 /* FIXME: use unsigned long pointer instead of unsigned char */
640 unsigned char *kvm_dirty_bitmap
= NULL
;
641 int kvm_physical_memory_set_dirty_tracking(int enable
)
649 if (!kvm_dirty_bitmap
) {
650 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
651 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
652 if (kvm_dirty_bitmap
== NULL
) {
653 perror("Failed to allocate dirty pages bitmap");
657 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
662 if (kvm_dirty_bitmap
) {
663 r
= kvm_dirty_pages_log_reset(kvm_context
);
664 qemu_free(kvm_dirty_bitmap
);
665 kvm_dirty_bitmap
= NULL
;
671 /* get kvm's dirty pages bitmap and update qemu's */
672 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
673 unsigned char *bitmap
,
675 unsigned long mem_size
)
677 unsigned int i
, j
, n
=0;
679 unsigned page_number
, addr
, addr1
;
680 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
683 * bitmap-traveling is faster than memory-traveling (for addr...)
684 * especially when most of the memory is not dirty.
686 for (i
=0; i
<len
; i
++) {
691 page_number
= i
* 8 + j
;
692 addr1
= page_number
* TARGET_PAGE_SIZE
;
693 addr
= offset
+ addr1
;
694 cpu_physical_memory_set_dirty(addr
);
700 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
701 void *bitmap
, void *opaque
)
703 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
707 * get kvm's dirty pages bitmap and update qemu's
708 * we only care about physical ram, which resides in slots 0 and 3
710 int kvm_update_dirty_pages_log(void)
715 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
716 kvm_dirty_bitmap
, NULL
,
717 kvm_get_dirty_bitmap_cb
);
721 int kvm_get_phys_ram_bitmap_cb(unsigned long start
, unsigned long len
,
722 void *local_bitmap
, void *qemu_bitmap
)
724 unsigned int bsize
= ((len
/TARGET_PAGE_SIZE
) + 7) / 8;
725 unsigned int offset
= ((start
/TARGET_PAGE_SIZE
) + 7) / 8;
727 memcpy(qemu_bitmap
+ offset
, local_bitmap
, bsize
);
732 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
736 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
738 local_bitmap
= qemu_malloc(bsize
);
740 fprintf(stderr
, "could not allocate memory for phys_page bitmap\n");
744 r
= kvm_get_mem_map_range(kvm_context
, 0, phys_ram_size
,
745 local_bitmap
, bitmap
,
746 kvm_get_phys_ram_bitmap_cb
);
748 qemu_free(local_bitmap
);
752 #ifdef KVM_CAP_IRQCHIP
754 int kvm_set_irq(int irq
, int level
)
756 return kvm_set_irq_level(kvm_context
, irq
, level
);