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 pthread_cond_t qemu_aio_cond
= PTHREAD_COND_INITIALIZER
;
33 __thread CPUState
*vcpu_env
;
35 static sigset_t io_sigset
, io_negsigset
;
37 #define SIG_IPI (SIGRTMIN+4)
48 static void sig_ipi_handler(int n
)
52 void kvm_update_interrupt_request(CPUState
*env
)
54 if (env
&& env
!= vcpu_env
) {
55 if (vcpu_info
[env
->cpu_index
].signalled
)
57 vcpu_info
[env
->cpu_index
].signalled
= 1;
58 if (vcpu_info
[env
->cpu_index
].thread
)
59 pthread_kill(vcpu_info
[env
->cpu_index
].thread
, SIG_IPI
);
63 void kvm_update_after_sipi(CPUState
*env
)
65 vcpu_info
[env
->cpu_index
].sipi_needed
= 1;
66 kvm_update_interrupt_request(env
);
69 void kvm_apic_init(CPUState
*env
)
71 if (env
->cpu_index
!= 0)
72 vcpu_info
[env
->cpu_index
].init
= 1;
73 kvm_update_interrupt_request(env
);
78 static int try_push_interrupts(void *opaque
)
80 return kvm_arch_try_push_interrupts(opaque
);
83 static void post_kvm_run(void *opaque
, int vcpu
)
86 pthread_mutex_lock(&qemu_mutex
);
87 kvm_arch_post_kvm_run(opaque
, vcpu
);
90 static int pre_kvm_run(void *opaque
, int vcpu
)
92 CPUState
*env
= cpu_single_env
;
94 kvm_arch_pre_kvm_run(opaque
, vcpu
);
96 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
98 pthread_mutex_unlock(&qemu_mutex
);
102 void kvm_load_registers(CPUState
*env
)
105 kvm_arch_load_regs(env
);
108 void kvm_save_registers(CPUState
*env
)
111 kvm_arch_save_regs(env
);
114 int kvm_cpu_exec(CPUState
*env
)
118 r
= kvm_run(kvm_context
, env
->cpu_index
);
120 printf("kvm_run returned %d\n", r
);
127 extern int vm_running
;
129 static int has_work(CPUState
*env
)
133 if (!(env
->hflags
& HF_HALTED_MASK
))
135 return kvm_arch_has_work(env
);
138 static int kvm_eat_signal(CPUState
*env
, int timeout
)
145 ts
.tv_sec
= timeout
/ 1000;
146 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
147 r
= sigtimedwait(&io_sigset
, &siginfo
, &ts
);
148 if (r
== -1 && (errno
== EAGAIN
|| errno
== EINTR
) && !timeout
)
151 pthread_mutex_lock(&qemu_mutex
);
152 cpu_single_env
= vcpu_env
;
153 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
154 printf("sigtimedwait: %s\n", strerror(e
));
158 sigaction(siginfo
.si_signo
, NULL
, &sa
);
159 sa
.sa_handler(siginfo
.si_signo
);
160 if (siginfo
.si_signo
== SIGUSR2
)
161 pthread_cond_signal(&qemu_aio_cond
);
164 pthread_mutex_unlock(&qemu_mutex
);
170 static void kvm_eat_signals(CPUState
*env
, int timeout
)
174 while (kvm_eat_signal(env
, 0))
177 r
= kvm_eat_signal(env
, timeout
);
179 while (kvm_eat_signal(env
, 0))
183 * we call select() even if no signal was received, to account for
184 * for which there is no signal handler installed.
186 pthread_mutex_lock(&qemu_mutex
);
187 cpu_single_env
= vcpu_env
;
189 pthread_mutex_unlock(&qemu_mutex
);
192 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
194 pthread_mutex_unlock(&qemu_mutex
);
195 if (env
->cpu_index
== 0)
196 kvm_eat_signals(env
, timeout
);
198 if (!kvm_irqchip_in_kernel(kvm_context
) &&
199 (timeout
|| vcpu_info
[env
->cpu_index
].stopped
)) {
205 sigaddset(&set
, SIG_IPI
);
215 sigaddset(&set
, SIG_IPI
);
216 sigtimedwait(&set
, &siginfo
, &ts
);
218 if (vcpu_info
[env
->cpu_index
].stop
) {
219 vcpu_info
[env
->cpu_index
].stop
= 0;
220 vcpu_info
[env
->cpu_index
].stopped
= 1;
221 pthread_kill(vcpu_info
[0].thread
, SIG_IPI
);
225 pthread_mutex_lock(&qemu_mutex
);
226 cpu_single_env
= env
;
227 vcpu_info
[env
->cpu_index
].signalled
= 0;
230 static int all_threads_paused(void)
234 for (i
= 1; i
< smp_cpus
; ++i
)
235 if (vcpu_info
[i
].stopped
)
240 static void pause_other_threads(void)
244 for (i
= 1; i
< smp_cpus
; ++i
) {
245 vcpu_info
[i
].stop
= 1;
246 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
248 while (!all_threads_paused())
249 kvm_eat_signals(vcpu_env
, 0);
252 static void resume_other_threads(void)
256 for (i
= 1; i
< smp_cpus
; ++i
) {
257 vcpu_info
[i
].stop
= 0;
258 vcpu_info
[i
].stopped
= 0;
259 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
263 static void kvm_vm_state_change_handler(void *context
, int running
)
266 resume_other_threads();
268 pause_other_threads();
271 static void update_regs_for_sipi(CPUState
*env
)
273 kvm_arch_update_regs_for_sipi(env
);
274 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
275 vcpu_info
[env
->cpu_index
].init
= 0;
278 static void update_regs_for_init(CPUState
*env
)
281 kvm_arch_load_regs(env
);
284 static void setup_kernel_sigmask(CPUState
*env
)
288 sigprocmask(SIG_BLOCK
, NULL
, &set
);
289 sigdelset(&set
, SIG_IPI
);
290 if (env
->cpu_index
== 0)
291 sigandset(&set
, &set
, &io_negsigset
);
293 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
296 static int kvm_main_loop_cpu(CPUState
*env
)
298 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
300 setup_kernel_sigmask(env
);
301 pthread_mutex_lock(&qemu_mutex
);
303 kvm_qemu_init_env(env
);
304 env
->ready_for_interrupt_injection
= 1;
306 cpu_single_env
= env
;
308 kvm_tpr_opt_setup(env
);
311 while (!has_work(env
))
312 kvm_main_loop_wait(env
, 10);
313 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
314 env
->hflags
&= ~HF_HALTED_MASK
;
315 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
316 update_regs_for_sipi(env
);
317 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
318 update_regs_for_init(env
);
319 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
321 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
322 kvm_main_loop_wait(env
, 0);
323 if (qemu_shutdown_requested())
325 else if (qemu_powerdown_requested())
326 qemu_system_powerdown();
327 else if (qemu_reset_requested()) {
328 env
->interrupt_request
= 0;
330 kvm_arch_load_regs(env
);
333 pthread_mutex_unlock(&qemu_mutex
);
337 static void *ap_main_loop(void *_env
)
339 CPUState
*env
= _env
;
343 sigfillset(&signals
);
344 //sigdelset(&signals, SIG_IPI);
345 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
346 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
347 kvm_qemu_init_env(env
);
348 if (kvm_irqchip_in_kernel(kvm_context
))
349 env
->hflags
&= ~HF_HALTED_MASK
;
350 kvm_main_loop_cpu(env
);
354 static void kvm_add_signal(int signum
)
356 sigaddset(&io_sigset
, signum
);
357 sigdelset(&io_negsigset
, signum
);
358 sigprocmask(SIG_BLOCK
, &io_sigset
, NULL
);
361 int kvm_init_ap(void)
363 CPUState
*env
= first_cpu
->next_cpu
;
366 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
367 sigemptyset(&io_sigset
);
368 sigfillset(&io_negsigset
);
369 kvm_add_signal(SIGIO
);
370 kvm_add_signal(SIGALRM
);
371 kvm_add_signal(SIGUSR2
);
372 kvm_add_signal(SIG_IPI
);
374 vcpu_env
= first_cpu
;
375 signal(SIG_IPI
, sig_ipi_handler
);
376 for (i
= 1; i
< smp_cpus
; ++i
) {
377 pthread_create(&vcpu_info
[i
].thread
, NULL
, ap_main_loop
, env
);
383 int kvm_main_loop(void)
385 vcpu_info
[0].thread
= pthread_self();
386 pthread_mutex_unlock(&qemu_mutex
);
387 return kvm_main_loop_cpu(first_cpu
);
390 static int kvm_debug(void *opaque
, int vcpu
)
392 CPUState
*env
= cpu_single_env
;
394 env
->exception_index
= EXCP_DEBUG
;
398 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
400 *data
= cpu_inb(0, addr
);
404 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
406 *data
= cpu_inw(0, addr
);
410 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
412 *data
= cpu_inl(0, addr
);
416 #define PM_IO_BASE 0xb000
418 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
423 cpu_outb(0, 0xb3, 0);
430 x
= cpu_inw(0, PM_IO_BASE
+ 4);
432 cpu_outw(0, PM_IO_BASE
+ 4, x
);
439 x
= cpu_inw(0, PM_IO_BASE
+ 4);
441 cpu_outw(0, PM_IO_BASE
+ 4, x
);
449 cpu_outb(0, addr
, data
);
453 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
455 cpu_outw(0, addr
, data
);
459 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
461 cpu_outl(0, addr
, data
);
465 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
467 cpu_physical_memory_rw(addr
, data
, len
, 0);
471 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
473 cpu_physical_memory_rw(addr
, data
, len
, 1);
477 static int kvm_io_window(void *opaque
)
483 static int kvm_halt(void *opaque
, int vcpu
)
485 return kvm_arch_halt(opaque
, vcpu
);
488 static int kvm_shutdown(void *opaque
, int vcpu
)
490 qemu_system_reset_request();
494 static struct kvm_callbacks qemu_kvm_ops
= {
502 .mmio_read
= kvm_mmio_read
,
503 .mmio_write
= kvm_mmio_write
,
505 .shutdown
= kvm_shutdown
,
506 .io_window
= kvm_io_window
,
507 .try_push_interrupts
= try_push_interrupts
,
508 .post_kvm_run
= post_kvm_run
,
509 .pre_kvm_run
= pre_kvm_run
,
511 .tpr_access
= handle_tpr_access
,
514 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
515 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
521 /* Try to initialize kvm */
522 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
526 pthread_mutex_lock(&qemu_mutex
);
531 int kvm_qemu_create_context(void)
535 kvm_disable_irqchip_creation(kvm_context
);
537 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
541 r
= kvm_arch_qemu_create_context();
547 void kvm_qemu_destroy(void)
549 kvm_finalize(kvm_context
);
552 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
554 unsigned long phys_offset
)
556 #ifdef KVM_CAP_USER_MEMORY
559 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
561 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
562 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
565 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
567 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
568 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
569 phys_ram_base
+ phys_offset
,
572 if (phys_offset
& IO_MEM_ROM
) {
573 phys_offset
&= ~IO_MEM_ROM
;
574 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
576 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
577 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
578 phys_ram_base
+ phys_offset
,
582 printf("kvm_cpu_register_physical_memory: failed\n");
588 if (phys_offset
& IO_MEM_ROM
) {
589 phys_offset
&= ~IO_MEM_ROM
;
590 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
594 int kvm_qemu_check_extension(int ext
)
596 return kvm_check_extension(kvm_context
, ext
);
599 int kvm_qemu_init_env(CPUState
*cenv
)
601 return kvm_arch_qemu_init_env(cenv
);
604 int kvm_update_debugger(CPUState
*env
)
606 struct kvm_debug_guest dbg
;
610 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
612 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
613 dbg
.breakpoints
[i
].enabled
= 1;
614 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
616 dbg
.singlestep
= env
->singlestep_enabled
;
618 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
623 * dirty pages logging
625 /* FIXME: use unsigned long pointer instead of unsigned char */
626 unsigned char *kvm_dirty_bitmap
= NULL
;
627 int kvm_physical_memory_set_dirty_tracking(int enable
)
635 if (!kvm_dirty_bitmap
) {
636 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
637 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
638 if (kvm_dirty_bitmap
== NULL
) {
639 perror("Failed to allocate dirty pages bitmap");
643 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
648 if (kvm_dirty_bitmap
) {
649 r
= kvm_dirty_pages_log_reset(kvm_context
);
650 qemu_free(kvm_dirty_bitmap
);
651 kvm_dirty_bitmap
= NULL
;
657 /* get kvm's dirty pages bitmap and update qemu's */
658 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
659 unsigned char *bitmap
,
661 unsigned long mem_size
)
663 unsigned int i
, j
, n
=0;
665 unsigned page_number
, addr
, addr1
;
666 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
669 * bitmap-traveling is faster than memory-traveling (for addr...)
670 * especially when most of the memory is not dirty.
672 for (i
=0; i
<len
; i
++) {
677 page_number
= i
* 8 + j
;
678 addr1
= page_number
* TARGET_PAGE_SIZE
;
679 addr
= offset
+ addr1
;
680 cpu_physical_memory_set_dirty(addr
);
686 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
687 void *bitmap
, void *opaque
)
689 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
693 * get kvm's dirty pages bitmap and update qemu's
694 * we only care about physical ram, which resides in slots 0 and 3
696 int kvm_update_dirty_pages_log(void)
701 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
702 kvm_dirty_bitmap
, NULL
,
703 kvm_get_dirty_bitmap_cb
);
707 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
709 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
710 unsigned int brsize
= BITMAP_SIZE(ram_size
);
711 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
712 unsigned int extra_bytes
= (extra_pages
+7)/8;
713 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
714 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
716 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
717 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
718 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
723 #ifdef KVM_CAP_IRQCHIP
725 int kvm_set_irq(int irq
, int level
)
727 return kvm_set_irq_level(kvm_context
, irq
, level
);
732 void qemu_kvm_aio_wait_start(void)
736 void qemu_kvm_aio_wait(void)
738 if (!cpu_single_env
|| cpu_single_env
->cpu_index
== 0) {
739 pthread_mutex_unlock(&qemu_mutex
);
740 kvm_eat_signal(cpu_single_env
, 1000);
741 pthread_mutex_lock(&qemu_mutex
);
743 pthread_cond_wait(&qemu_aio_cond
, &qemu_mutex
);
747 void qemu_kvm_aio_wait_end(void)