3 #include "config-host.h"
15 #include <sys/utsname.h>
17 extern void perror(const char *s
);
19 kvm_context_t kvm_context
;
23 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
24 pthread_cond_t qemu_aio_cond
= PTHREAD_COND_INITIALIZER
;
25 __thread
struct vcpu_info
*vcpu
;
27 struct qemu_kvm_signal_table
{
32 static struct qemu_kvm_signal_table io_signal_table
;
34 #define SIG_IPI (SIGRTMIN+4)
46 CPUState
*qemu_kvm_cpu_env(int index
)
48 return vcpu_info
[index
].env
;
51 static void sig_ipi_handler(int n
)
55 void kvm_update_interrupt_request(CPUState
*env
)
57 if (env
&& vcpu
&& env
!= vcpu
->env
) {
58 if (vcpu_info
[env
->cpu_index
].signalled
)
60 vcpu_info
[env
->cpu_index
].signalled
= 1;
61 if (vcpu_info
[env
->cpu_index
].thread
)
62 pthread_kill(vcpu_info
[env
->cpu_index
].thread
, SIG_IPI
);
66 void kvm_update_after_sipi(CPUState
*env
)
68 vcpu_info
[env
->cpu_index
].sipi_needed
= 1;
69 kvm_update_interrupt_request(env
);
72 void kvm_apic_init(CPUState
*env
)
74 if (env
->cpu_index
!= 0)
75 vcpu_info
[env
->cpu_index
].init
= 1;
76 kvm_update_interrupt_request(env
);
81 static int try_push_interrupts(void *opaque
)
83 return kvm_arch_try_push_interrupts(opaque
);
86 static void post_kvm_run(void *opaque
, int vcpu
)
89 pthread_mutex_lock(&qemu_mutex
);
90 kvm_arch_post_kvm_run(opaque
, vcpu
);
93 static int pre_kvm_run(void *opaque
, int vcpu
)
95 CPUState
*env
= cpu_single_env
;
97 kvm_arch_pre_kvm_run(opaque
, vcpu
);
99 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
101 pthread_mutex_unlock(&qemu_mutex
);
105 void kvm_load_registers(CPUState
*env
)
108 kvm_arch_load_regs(env
);
111 void kvm_save_registers(CPUState
*env
)
114 kvm_arch_save_regs(env
);
117 int kvm_cpu_exec(CPUState
*env
)
121 r
= kvm_run(kvm_context
, env
->cpu_index
);
123 printf("kvm_run returned %d\n", r
);
130 extern int vm_running
;
132 static int has_work(CPUState
*env
)
136 if (!(env
->hflags
& HF_HALTED_MASK
))
138 return kvm_arch_has_work(env
);
141 static int kvm_eat_signal(CPUState
*env
, int timeout
)
148 ts
.tv_sec
= timeout
/ 1000;
149 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
150 r
= sigtimedwait(&io_signal_table
.sigset
, &siginfo
, &ts
);
151 if (r
== -1 && (errno
== EAGAIN
|| errno
== EINTR
) && !timeout
)
154 pthread_mutex_lock(&qemu_mutex
);
156 cpu_single_env
= vcpu
->env
;
157 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
158 printf("sigtimedwait: %s\n", strerror(e
));
162 sigaction(siginfo
.si_signo
, NULL
, &sa
);
163 sa
.sa_handler(siginfo
.si_signo
);
164 if (siginfo
.si_signo
== SIGUSR2
)
165 pthread_cond_signal(&qemu_aio_cond
);
168 pthread_mutex_unlock(&qemu_mutex
);
174 static void kvm_eat_signals(CPUState
*env
, int timeout
)
178 while (kvm_eat_signal(env
, 0))
181 r
= kvm_eat_signal(env
, timeout
);
183 while (kvm_eat_signal(env
, 0))
187 * we call select() even if no signal was received, to account for
188 * for which there is no signal handler installed.
190 pthread_mutex_lock(&qemu_mutex
);
191 cpu_single_env
= vcpu
->env
;
193 pthread_mutex_unlock(&qemu_mutex
);
196 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
198 pthread_mutex_unlock(&qemu_mutex
);
199 if (env
->cpu_index
== 0)
200 kvm_eat_signals(env
, timeout
);
202 if (!kvm_irqchip_in_kernel(kvm_context
) &&
203 (timeout
|| vcpu_info
[env
->cpu_index
].stopped
)) {
209 sigaddset(&set
, SIG_IPI
);
219 sigaddset(&set
, SIG_IPI
);
220 sigtimedwait(&set
, &siginfo
, &ts
);
222 if (vcpu_info
[env
->cpu_index
].stop
) {
223 vcpu_info
[env
->cpu_index
].stop
= 0;
224 vcpu_info
[env
->cpu_index
].stopped
= 1;
225 pthread_kill(vcpu_info
[0].thread
, SIG_IPI
);
229 pthread_mutex_lock(&qemu_mutex
);
230 cpu_single_env
= env
;
231 vcpu_info
[env
->cpu_index
].signalled
= 0;
234 static int all_threads_paused(void)
238 for (i
= 1; i
< smp_cpus
; ++i
)
239 if (vcpu_info
[i
].stopped
)
244 static void pause_other_threads(void)
248 for (i
= 1; i
< smp_cpus
; ++i
) {
249 vcpu_info
[i
].stop
= 1;
250 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
252 while (!all_threads_paused())
253 kvm_eat_signals(vcpu
->env
, 0);
256 static void resume_other_threads(void)
260 for (i
= 1; i
< smp_cpus
; ++i
) {
261 vcpu_info
[i
].stop
= 0;
262 vcpu_info
[i
].stopped
= 0;
263 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
267 static void kvm_vm_state_change_handler(void *context
, int running
)
270 resume_other_threads();
272 pause_other_threads();
275 static void update_regs_for_sipi(CPUState
*env
)
277 kvm_arch_update_regs_for_sipi(env
);
278 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
279 vcpu_info
[env
->cpu_index
].init
= 0;
282 static void update_regs_for_init(CPUState
*env
)
285 kvm_arch_load_regs(env
);
288 static void setup_kernel_sigmask(CPUState
*env
)
292 sigprocmask(SIG_BLOCK
, NULL
, &set
);
293 sigdelset(&set
, SIG_IPI
);
294 if (env
->cpu_index
== 0)
295 sigandset(&set
, &set
, &io_signal_table
.negsigset
);
297 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
300 static int kvm_main_loop_cpu(CPUState
*env
)
302 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
304 setup_kernel_sigmask(env
);
305 pthread_mutex_lock(&qemu_mutex
);
307 kvm_qemu_init_env(env
);
308 env
->ready_for_interrupt_injection
= 1;
310 cpu_single_env
= env
;
312 kvm_tpr_opt_setup(env
);
315 while (!has_work(env
))
316 kvm_main_loop_wait(env
, 10);
317 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
318 env
->hflags
&= ~HF_HALTED_MASK
;
319 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
320 update_regs_for_sipi(env
);
321 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
322 update_regs_for_init(env
);
323 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
325 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
326 kvm_main_loop_wait(env
, 0);
327 if (qemu_shutdown_requested())
329 else if (qemu_powerdown_requested())
330 qemu_system_powerdown();
331 else if (qemu_reset_requested()) {
332 env
->interrupt_request
= 0;
334 kvm_arch_load_regs(env
);
337 pthread_mutex_unlock(&qemu_mutex
);
341 static void *ap_main_loop(void *_env
)
343 CPUState
*env
= _env
;
346 vcpu
= &vcpu_info
[env
->cpu_index
];
348 sigfillset(&signals
);
349 //sigdelset(&signals, SIG_IPI);
350 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
351 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
352 kvm_qemu_init_env(env
);
353 if (kvm_irqchip_in_kernel(kvm_context
))
354 env
->hflags
&= ~HF_HALTED_MASK
;
355 kvm_main_loop_cpu(env
);
359 static void qemu_kvm_init_signal_table(struct qemu_kvm_signal_table
*sigtab
)
361 sigemptyset(&sigtab
->sigset
);
362 sigfillset(&sigtab
->negsigset
);
365 static void kvm_add_signal(struct qemu_kvm_signal_table
*sigtab
, int signum
)
367 sigaddset(&sigtab
->sigset
, signum
);
368 sigdelset(&sigtab
->negsigset
, signum
);
371 int kvm_init_ap(void)
373 CPUState
*env
= first_cpu
->next_cpu
;
376 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
377 qemu_kvm_init_signal_table(&io_signal_table
);
378 kvm_add_signal(&io_signal_table
, SIGIO
);
379 kvm_add_signal(&io_signal_table
, SIGALRM
);
380 kvm_add_signal(&io_signal_table
, SIGUSR2
);
381 kvm_add_signal(&io_signal_table
, SIG_IPI
);
382 sigprocmask(SIG_BLOCK
, &io_signal_table
.sigset
, NULL
);
384 vcpu
= &vcpu_info
[0];
385 vcpu
->env
= first_cpu
;
386 signal(SIG_IPI
, sig_ipi_handler
);
387 for (i
= 1; i
< smp_cpus
; ++i
) {
388 pthread_create(&vcpu_info
[i
].thread
, NULL
, ap_main_loop
, env
);
394 int kvm_main_loop(void)
396 vcpu_info
[0].thread
= pthread_self();
397 pthread_mutex_unlock(&qemu_mutex
);
398 return kvm_main_loop_cpu(first_cpu
);
401 static int kvm_debug(void *opaque
, int vcpu
)
403 CPUState
*env
= cpu_single_env
;
405 env
->exception_index
= EXCP_DEBUG
;
409 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
411 *data
= cpu_inb(0, addr
);
415 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
417 *data
= cpu_inw(0, addr
);
421 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
423 *data
= cpu_inl(0, addr
);
427 #define PM_IO_BASE 0xb000
429 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
434 cpu_outb(0, 0xb3, 0);
441 x
= cpu_inw(0, PM_IO_BASE
+ 4);
443 cpu_outw(0, PM_IO_BASE
+ 4, x
);
450 x
= cpu_inw(0, PM_IO_BASE
+ 4);
452 cpu_outw(0, PM_IO_BASE
+ 4, x
);
460 cpu_outb(0, addr
, data
);
464 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
466 cpu_outw(0, addr
, data
);
470 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
472 cpu_outl(0, addr
, data
);
476 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
478 cpu_physical_memory_rw(addr
, data
, len
, 0);
482 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
484 cpu_physical_memory_rw(addr
, data
, len
, 1);
488 static int kvm_io_window(void *opaque
)
494 static int kvm_halt(void *opaque
, int vcpu
)
496 return kvm_arch_halt(opaque
, vcpu
);
499 static int kvm_shutdown(void *opaque
, int vcpu
)
501 qemu_system_reset_request();
505 static struct kvm_callbacks qemu_kvm_ops
= {
513 .mmio_read
= kvm_mmio_read
,
514 .mmio_write
= kvm_mmio_write
,
516 .shutdown
= kvm_shutdown
,
517 .io_window
= kvm_io_window
,
518 .try_push_interrupts
= try_push_interrupts
,
519 .post_kvm_run
= post_kvm_run
,
520 .pre_kvm_run
= pre_kvm_run
,
522 .tpr_access
= handle_tpr_access
,
525 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
526 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
532 /* Try to initialize kvm */
533 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
537 pthread_mutex_lock(&qemu_mutex
);
542 int kvm_qemu_create_context(void)
546 kvm_disable_irqchip_creation(kvm_context
);
548 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
552 r
= kvm_arch_qemu_create_context();
558 void kvm_qemu_destroy(void)
560 kvm_finalize(kvm_context
);
563 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
565 unsigned long phys_offset
)
567 #ifdef KVM_CAP_USER_MEMORY
570 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
572 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
573 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
576 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
578 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
579 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
580 phys_ram_base
+ phys_offset
,
583 if (phys_offset
& IO_MEM_ROM
) {
584 phys_offset
&= ~IO_MEM_ROM
;
585 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
587 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
588 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
589 phys_ram_base
+ phys_offset
,
593 printf("kvm_cpu_register_physical_memory: failed\n");
599 if (phys_offset
& IO_MEM_ROM
) {
600 phys_offset
&= ~IO_MEM_ROM
;
601 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
605 int kvm_qemu_check_extension(int ext
)
607 return kvm_check_extension(kvm_context
, ext
);
610 int kvm_qemu_init_env(CPUState
*cenv
)
612 return kvm_arch_qemu_init_env(cenv
);
615 int kvm_update_debugger(CPUState
*env
)
617 struct kvm_debug_guest dbg
;
621 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
623 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
624 dbg
.breakpoints
[i
].enabled
= 1;
625 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
627 dbg
.singlestep
= env
->singlestep_enabled
;
629 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
634 * dirty pages logging
636 /* FIXME: use unsigned long pointer instead of unsigned char */
637 unsigned char *kvm_dirty_bitmap
= NULL
;
638 int kvm_physical_memory_set_dirty_tracking(int enable
)
646 if (!kvm_dirty_bitmap
) {
647 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
648 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
649 if (kvm_dirty_bitmap
== NULL
) {
650 perror("Failed to allocate dirty pages bitmap");
654 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
659 if (kvm_dirty_bitmap
) {
660 r
= kvm_dirty_pages_log_reset(kvm_context
);
661 qemu_free(kvm_dirty_bitmap
);
662 kvm_dirty_bitmap
= NULL
;
668 /* get kvm's dirty pages bitmap and update qemu's */
669 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
670 unsigned char *bitmap
,
672 unsigned long mem_size
)
674 unsigned int i
, j
, n
=0;
676 unsigned page_number
, addr
, addr1
;
677 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
680 * bitmap-traveling is faster than memory-traveling (for addr...)
681 * especially when most of the memory is not dirty.
683 for (i
=0; i
<len
; i
++) {
688 page_number
= i
* 8 + j
;
689 addr1
= page_number
* TARGET_PAGE_SIZE
;
690 addr
= offset
+ addr1
;
691 cpu_physical_memory_set_dirty(addr
);
697 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
698 void *bitmap
, void *opaque
)
700 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
704 * get kvm's dirty pages bitmap and update qemu's
705 * we only care about physical ram, which resides in slots 0 and 3
707 int kvm_update_dirty_pages_log(void)
712 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
713 kvm_dirty_bitmap
, NULL
,
714 kvm_get_dirty_bitmap_cb
);
718 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
720 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
721 unsigned int brsize
= BITMAP_SIZE(ram_size
);
722 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
723 unsigned int extra_bytes
= (extra_pages
+7)/8;
724 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
725 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
727 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
728 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
729 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
734 #ifdef KVM_CAP_IRQCHIP
736 int kvm_set_irq(int irq
, int level
)
738 return kvm_set_irq_level(kvm_context
, irq
, level
);
743 void qemu_kvm_aio_wait_start(void)
747 void qemu_kvm_aio_wait(void)
749 if (!cpu_single_env
|| cpu_single_env
->cpu_index
== 0) {
750 pthread_mutex_unlock(&qemu_mutex
);
751 kvm_eat_signal(cpu_single_env
, 1000);
752 pthread_mutex_lock(&qemu_mutex
);
754 pthread_cond_wait(&qemu_aio_cond
, &qemu_mutex
);
758 void qemu_kvm_aio_wait_end(void)
762 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
764 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
767 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
768 unsigned long size
, int log
, int writable
)
770 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
773 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
776 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);