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
;
330 kvm_tpr_opt_setup(env
);
332 while (!has_work(env
))
333 kvm_main_loop_wait(env
, 10);
334 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
335 env
->hflags
&= ~HF_HALTED_MASK
;
336 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
337 update_regs_for_sipi(env
);
338 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
339 update_regs_for_init(env
);
340 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
342 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
343 kvm_main_loop_wait(env
, 0);
344 if (qemu_shutdown_requested())
346 else if (qemu_powerdown_requested())
347 qemu_system_powerdown();
348 else if (qemu_reset_requested()) {
349 env
->interrupt_request
= 0;
351 kvm_arch_load_regs(env
);
354 pthread_mutex_unlock(&qemu_mutex
);
358 static void *ap_main_loop(void *_env
)
360 CPUState
*env
= _env
;
364 sigfillset(&signals
);
365 //sigdelset(&signals, SIG_IPI);
366 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
367 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
368 kvm_qemu_init_env(env
);
369 if (kvm_irqchip_in_kernel(kvm_context
))
370 env
->hflags
&= ~HF_HALTED_MASK
;
371 kvm_main_loop_cpu(env
);
375 static void kvm_add_signal(int signum
)
377 sigaddset(&io_sigset
, signum
);
378 sigdelset(&io_negsigset
, signum
);
379 sigprocmask(SIG_BLOCK
, &io_sigset
, NULL
);
382 int kvm_init_ap(void)
384 CPUState
*env
= first_cpu
->next_cpu
;
387 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
388 sigemptyset(&io_sigset
);
389 sigfillset(&io_negsigset
);
390 kvm_add_signal(SIGIO
);
391 kvm_add_signal(SIGALRM
);
392 kvm_add_signal(SIGUSR2
);
393 if (!kvm_irqchip_in_kernel(kvm_context
))
394 kvm_add_signal(SIG_IPI
);
396 vcpu_env
= first_cpu
;
397 signal(SIG_IPI
, sig_ipi_handler
);
398 for (i
= 1; i
< smp_cpus
; ++i
) {
399 pthread_create(&vcpu_info
[i
].thread
, NULL
, ap_main_loop
, env
);
405 int kvm_main_loop(void)
407 vcpu_info
[0].thread
= pthread_self();
408 return kvm_main_loop_cpu(first_cpu
);
411 static int kvm_debug(void *opaque
, int vcpu
)
413 CPUState
*env
= cpu_single_env
;
415 env
->exception_index
= EXCP_DEBUG
;
419 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
421 *data
= cpu_inb(0, addr
);
425 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
427 *data
= cpu_inw(0, addr
);
431 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
433 *data
= cpu_inl(0, addr
);
437 #define PM_IO_BASE 0xb000
439 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
444 cpu_outb(0, 0xb3, 0);
451 x
= cpu_inw(0, PM_IO_BASE
+ 4);
453 cpu_outw(0, PM_IO_BASE
+ 4, x
);
460 x
= cpu_inw(0, PM_IO_BASE
+ 4);
462 cpu_outw(0, PM_IO_BASE
+ 4, x
);
470 cpu_outb(0, addr
, data
);
474 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
476 cpu_outw(0, addr
, data
);
480 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
482 cpu_outl(0, addr
, data
);
486 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
488 cpu_physical_memory_rw(addr
, data
, len
, 0);
492 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
494 cpu_physical_memory_rw(addr
, data
, len
, 1);
498 static int kvm_io_window(void *opaque
)
504 static int kvm_halt(void *opaque
, int vcpu
)
506 return kvm_arch_halt(opaque
, vcpu
);
509 static int kvm_shutdown(void *opaque
, int vcpu
)
511 qemu_system_reset_request();
515 static int handle_tpr_access(void *opaque
, int vcpu
,
516 uint64_t rip
, int is_write
)
518 kvm_tpr_access_report(cpu_single_env
, rip
, is_write
);
522 static struct kvm_callbacks qemu_kvm_ops
= {
530 .mmio_read
= kvm_mmio_read
,
531 .mmio_write
= kvm_mmio_write
,
533 .shutdown
= kvm_shutdown
,
534 .io_window
= kvm_io_window
,
535 .try_push_interrupts
= try_push_interrupts
,
536 .post_kvm_run
= post_kvm_run
,
537 .pre_kvm_run
= pre_kvm_run
,
538 .tpr_access
= handle_tpr_access
,
543 /* Try to initialize kvm */
544 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
552 int kvm_qemu_create_context(void)
556 kvm_disable_irqchip_creation(kvm_context
);
558 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
562 r
= kvm_arch_qemu_create_context();
568 void kvm_qemu_destroy(void)
570 kvm_finalize(kvm_context
);
573 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
575 unsigned long phys_offset
)
577 #ifdef KVM_CAP_USER_MEMORY
580 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
582 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
583 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
586 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
588 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
589 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
590 phys_ram_base
+ phys_offset
,
593 if (phys_offset
& IO_MEM_ROM
) {
594 phys_offset
&= ~IO_MEM_ROM
;
595 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
597 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
598 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
599 phys_ram_base
+ phys_offset
,
603 printf("kvm_cpu_register_physical_memory: failed\n");
609 if (phys_offset
& IO_MEM_ROM
) {
610 phys_offset
&= ~IO_MEM_ROM
;
611 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
615 int kvm_qemu_check_extension(int ext
)
617 return kvm_check_extension(kvm_context
, ext
);
620 int kvm_qemu_init_env(CPUState
*cenv
)
622 return kvm_arch_qemu_init_env(cenv
);
625 int kvm_update_debugger(CPUState
*env
)
627 struct kvm_debug_guest dbg
;
631 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
633 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
634 dbg
.breakpoints
[i
].enabled
= 1;
635 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
637 dbg
.singlestep
= env
->singlestep_enabled
;
639 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
644 * dirty pages logging
646 /* FIXME: use unsigned long pointer instead of unsigned char */
647 unsigned char *kvm_dirty_bitmap
= NULL
;
648 int kvm_physical_memory_set_dirty_tracking(int enable
)
656 if (!kvm_dirty_bitmap
) {
657 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
658 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
659 if (kvm_dirty_bitmap
== NULL
) {
660 perror("Failed to allocate dirty pages bitmap");
664 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
669 if (kvm_dirty_bitmap
) {
670 r
= kvm_dirty_pages_log_reset(kvm_context
);
671 qemu_free(kvm_dirty_bitmap
);
672 kvm_dirty_bitmap
= NULL
;
678 /* get kvm's dirty pages bitmap and update qemu's */
679 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
680 unsigned char *bitmap
,
682 unsigned long mem_size
)
684 unsigned int i
, j
, n
=0;
686 unsigned page_number
, addr
, addr1
;
687 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
690 * bitmap-traveling is faster than memory-traveling (for addr...)
691 * especially when most of the memory is not dirty.
693 for (i
=0; i
<len
; i
++) {
698 page_number
= i
* 8 + j
;
699 addr1
= page_number
* TARGET_PAGE_SIZE
;
700 addr
= offset
+ addr1
;
701 cpu_physical_memory_set_dirty(addr
);
707 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
708 void *bitmap
, void *opaque
)
710 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
714 * get kvm's dirty pages bitmap and update qemu's
715 * we only care about physical ram, which resides in slots 0 and 3
717 int kvm_update_dirty_pages_log(void)
722 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
723 kvm_dirty_bitmap
, NULL
,
724 kvm_get_dirty_bitmap_cb
);
728 int kvm_get_phys_ram_bitmap_cb(unsigned long start
, unsigned long len
,
729 void *local_bitmap
, void *qemu_bitmap
)
731 unsigned int bsize
= ((len
/TARGET_PAGE_SIZE
) + 7) / 8;
732 unsigned int offset
= ((start
/TARGET_PAGE_SIZE
) + 7) / 8;
734 memcpy(qemu_bitmap
+ offset
, local_bitmap
, bsize
);
739 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
743 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
745 local_bitmap
= qemu_malloc(bsize
);
747 fprintf(stderr
, "could not allocate memory for phys_page bitmap\n");
751 r
= kvm_get_mem_map_range(kvm_context
, 0, phys_ram_size
,
752 local_bitmap
, bitmap
,
753 kvm_get_phys_ram_bitmap_cb
);
755 qemu_free(local_bitmap
);
759 #ifdef KVM_CAP_IRQCHIP
761 int kvm_set_irq(int irq
, int level
)
763 return kvm_set_irq_level(kvm_context
, irq
, level
);