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
struct vcpu_info
*vcpu
;
35 struct qemu_kvm_signal_table
{
40 static struct qemu_kvm_signal_table io_signal_table
;
42 #define SIG_IPI (SIGRTMIN+4)
54 CPUState
*qemu_kvm_cpu_env(int index
)
56 return vcpu_info
[index
].env
;
59 static void sig_ipi_handler(int n
)
63 void kvm_update_interrupt_request(CPUState
*env
)
65 if (env
&& vcpu
&& env
!= vcpu
->env
) {
66 if (vcpu_info
[env
->cpu_index
].signalled
)
68 vcpu_info
[env
->cpu_index
].signalled
= 1;
69 if (vcpu_info
[env
->cpu_index
].thread
)
70 pthread_kill(vcpu_info
[env
->cpu_index
].thread
, SIG_IPI
);
74 void kvm_update_after_sipi(CPUState
*env
)
76 vcpu_info
[env
->cpu_index
].sipi_needed
= 1;
77 kvm_update_interrupt_request(env
);
80 void kvm_apic_init(CPUState
*env
)
82 if (env
->cpu_index
!= 0)
83 vcpu_info
[env
->cpu_index
].init
= 1;
84 kvm_update_interrupt_request(env
);
89 static int try_push_interrupts(void *opaque
)
91 return kvm_arch_try_push_interrupts(opaque
);
94 static void post_kvm_run(void *opaque
, int vcpu
)
97 pthread_mutex_lock(&qemu_mutex
);
98 kvm_arch_post_kvm_run(opaque
, vcpu
);
101 static int pre_kvm_run(void *opaque
, int vcpu
)
103 CPUState
*env
= cpu_single_env
;
105 kvm_arch_pre_kvm_run(opaque
, vcpu
);
107 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
109 pthread_mutex_unlock(&qemu_mutex
);
113 void kvm_load_registers(CPUState
*env
)
116 kvm_arch_load_regs(env
);
119 void kvm_save_registers(CPUState
*env
)
122 kvm_arch_save_regs(env
);
125 int kvm_cpu_exec(CPUState
*env
)
129 r
= kvm_run(kvm_context
, env
->cpu_index
);
131 printf("kvm_run returned %d\n", r
);
138 extern int vm_running
;
140 static int has_work(CPUState
*env
)
144 if (!(env
->hflags
& HF_HALTED_MASK
))
146 return kvm_arch_has_work(env
);
149 static int kvm_eat_signal(CPUState
*env
, int timeout
)
156 ts
.tv_sec
= timeout
/ 1000;
157 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
158 r
= sigtimedwait(&io_signal_table
.sigset
, &siginfo
, &ts
);
159 if (r
== -1 && (errno
== EAGAIN
|| errno
== EINTR
) && !timeout
)
162 pthread_mutex_lock(&qemu_mutex
);
164 cpu_single_env
= vcpu
->env
;
165 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
166 printf("sigtimedwait: %s\n", strerror(e
));
170 sigaction(siginfo
.si_signo
, NULL
, &sa
);
171 sa
.sa_handler(siginfo
.si_signo
);
172 if (siginfo
.si_signo
== SIGUSR2
)
173 pthread_cond_signal(&qemu_aio_cond
);
176 pthread_mutex_unlock(&qemu_mutex
);
182 static void kvm_eat_signals(CPUState
*env
, int timeout
)
186 while (kvm_eat_signal(env
, 0))
189 r
= kvm_eat_signal(env
, timeout
);
191 while (kvm_eat_signal(env
, 0))
195 * we call select() even if no signal was received, to account for
196 * for which there is no signal handler installed.
198 pthread_mutex_lock(&qemu_mutex
);
199 cpu_single_env
= vcpu
->env
;
201 pthread_mutex_unlock(&qemu_mutex
);
204 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
206 pthread_mutex_unlock(&qemu_mutex
);
207 if (env
->cpu_index
== 0)
208 kvm_eat_signals(env
, timeout
);
210 if (!kvm_irqchip_in_kernel(kvm_context
) &&
211 (timeout
|| vcpu_info
[env
->cpu_index
].stopped
)) {
217 sigaddset(&set
, SIG_IPI
);
227 sigaddset(&set
, SIG_IPI
);
228 sigtimedwait(&set
, &siginfo
, &ts
);
230 if (vcpu_info
[env
->cpu_index
].stop
) {
231 vcpu_info
[env
->cpu_index
].stop
= 0;
232 vcpu_info
[env
->cpu_index
].stopped
= 1;
233 pthread_kill(vcpu_info
[0].thread
, SIG_IPI
);
237 pthread_mutex_lock(&qemu_mutex
);
238 cpu_single_env
= env
;
239 vcpu_info
[env
->cpu_index
].signalled
= 0;
242 static int all_threads_paused(void)
246 for (i
= 1; i
< smp_cpus
; ++i
)
247 if (vcpu_info
[i
].stopped
)
252 static void pause_other_threads(void)
256 for (i
= 1; i
< smp_cpus
; ++i
) {
257 vcpu_info
[i
].stop
= 1;
258 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
260 while (!all_threads_paused())
261 kvm_eat_signals(vcpu
->env
, 0);
264 static void resume_other_threads(void)
268 for (i
= 1; i
< smp_cpus
; ++i
) {
269 vcpu_info
[i
].stop
= 0;
270 vcpu_info
[i
].stopped
= 0;
271 pthread_kill(vcpu_info
[i
].thread
, SIG_IPI
);
275 static void kvm_vm_state_change_handler(void *context
, int running
)
278 resume_other_threads();
280 pause_other_threads();
283 static void update_regs_for_sipi(CPUState
*env
)
285 kvm_arch_update_regs_for_sipi(env
);
286 vcpu_info
[env
->cpu_index
].sipi_needed
= 0;
287 vcpu_info
[env
->cpu_index
].init
= 0;
290 static void update_regs_for_init(CPUState
*env
)
293 kvm_arch_load_regs(env
);
296 static void setup_kernel_sigmask(CPUState
*env
)
300 sigprocmask(SIG_BLOCK
, NULL
, &set
);
301 sigdelset(&set
, SIG_IPI
);
302 if (env
->cpu_index
== 0)
303 sigandset(&set
, &set
, &io_signal_table
.negsigset
);
305 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
308 static int kvm_main_loop_cpu(CPUState
*env
)
310 struct vcpu_info
*info
= &vcpu_info
[env
->cpu_index
];
312 setup_kernel_sigmask(env
);
313 pthread_mutex_lock(&qemu_mutex
);
315 kvm_qemu_init_env(env
);
316 env
->ready_for_interrupt_injection
= 1;
318 cpu_single_env
= env
;
320 kvm_tpr_opt_setup(env
);
323 while (!has_work(env
))
324 kvm_main_loop_wait(env
, 10);
325 if (env
->interrupt_request
& CPU_INTERRUPT_HARD
)
326 env
->hflags
&= ~HF_HALTED_MASK
;
327 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->sipi_needed
)
328 update_regs_for_sipi(env
);
329 if (!kvm_irqchip_in_kernel(kvm_context
) && info
->init
)
330 update_regs_for_init(env
);
331 if (!(env
->hflags
& HF_HALTED_MASK
) && !info
->init
)
333 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
334 kvm_main_loop_wait(env
, 0);
335 if (qemu_shutdown_requested())
337 else if (qemu_powerdown_requested())
338 qemu_system_powerdown();
339 else if (qemu_reset_requested()) {
340 env
->interrupt_request
= 0;
342 kvm_arch_load_regs(env
);
345 pthread_mutex_unlock(&qemu_mutex
);
349 static void *ap_main_loop(void *_env
)
351 CPUState
*env
= _env
;
354 vcpu
= &vcpu_info
[env
->cpu_index
];
356 sigfillset(&signals
);
357 //sigdelset(&signals, SIG_IPI);
358 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
359 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
360 kvm_qemu_init_env(env
);
361 if (kvm_irqchip_in_kernel(kvm_context
))
362 env
->hflags
&= ~HF_HALTED_MASK
;
363 kvm_main_loop_cpu(env
);
367 static void qemu_kvm_init_signal_table(struct qemu_kvm_signal_table
*sigtab
)
369 sigemptyset(&sigtab
->sigset
);
370 sigfillset(&sigtab
->negsigset
);
373 static void kvm_add_signal(struct qemu_kvm_signal_table
*sigtab
, int signum
)
375 sigaddset(&sigtab
->sigset
, signum
);
376 sigdelset(&sigtab
->negsigset
, signum
);
379 int kvm_init_ap(void)
381 CPUState
*env
= first_cpu
->next_cpu
;
384 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
385 qemu_kvm_init_signal_table(&io_signal_table
);
386 kvm_add_signal(&io_signal_table
, SIGIO
);
387 kvm_add_signal(&io_signal_table
, SIGALRM
);
388 kvm_add_signal(&io_signal_table
, SIGUSR2
);
389 kvm_add_signal(&io_signal_table
, SIG_IPI
);
390 sigprocmask(SIG_BLOCK
, &io_signal_table
.sigset
, NULL
);
392 vcpu
= &vcpu_info
[0];
393 vcpu
->env
= first_cpu
;
394 signal(SIG_IPI
, sig_ipi_handler
);
395 for (i
= 1; i
< smp_cpus
; ++i
) {
396 pthread_create(&vcpu_info
[i
].thread
, NULL
, ap_main_loop
, env
);
402 int kvm_main_loop(void)
404 vcpu_info
[0].thread
= pthread_self();
405 pthread_mutex_unlock(&qemu_mutex
);
406 return kvm_main_loop_cpu(first_cpu
);
409 static int kvm_debug(void *opaque
, int vcpu
)
411 CPUState
*env
= cpu_single_env
;
413 env
->exception_index
= EXCP_DEBUG
;
417 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
419 *data
= cpu_inb(0, addr
);
423 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
425 *data
= cpu_inw(0, addr
);
429 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
431 *data
= cpu_inl(0, addr
);
435 #define PM_IO_BASE 0xb000
437 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
442 cpu_outb(0, 0xb3, 0);
449 x
= cpu_inw(0, PM_IO_BASE
+ 4);
451 cpu_outw(0, PM_IO_BASE
+ 4, x
);
458 x
= cpu_inw(0, PM_IO_BASE
+ 4);
460 cpu_outw(0, PM_IO_BASE
+ 4, x
);
468 cpu_outb(0, addr
, data
);
472 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
474 cpu_outw(0, addr
, data
);
478 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
480 cpu_outl(0, addr
, data
);
484 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
486 cpu_physical_memory_rw(addr
, data
, len
, 0);
490 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
492 cpu_physical_memory_rw(addr
, data
, len
, 1);
496 static int kvm_io_window(void *opaque
)
502 static int kvm_halt(void *opaque
, int vcpu
)
504 return kvm_arch_halt(opaque
, vcpu
);
507 static int kvm_shutdown(void *opaque
, int vcpu
)
509 qemu_system_reset_request();
513 static struct kvm_callbacks qemu_kvm_ops
= {
521 .mmio_read
= kvm_mmio_read
,
522 .mmio_write
= kvm_mmio_write
,
524 .shutdown
= kvm_shutdown
,
525 .io_window
= kvm_io_window
,
526 .try_push_interrupts
= try_push_interrupts
,
527 .post_kvm_run
= post_kvm_run
,
528 .pre_kvm_run
= pre_kvm_run
,
530 .tpr_access
= handle_tpr_access
,
533 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
534 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
540 /* Try to initialize kvm */
541 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
545 pthread_mutex_lock(&qemu_mutex
);
550 int kvm_qemu_create_context(void)
554 kvm_disable_irqchip_creation(kvm_context
);
556 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
560 r
= kvm_arch_qemu_create_context();
566 void kvm_qemu_destroy(void)
568 kvm_finalize(kvm_context
);
571 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
573 unsigned long phys_offset
)
575 #ifdef KVM_CAP_USER_MEMORY
578 r
= kvm_check_extension(kvm_context
, KVM_CAP_USER_MEMORY
);
580 if (!(phys_offset
& ~TARGET_PAGE_MASK
)) {
581 r
= kvm_is_allocated_mem(kvm_context
, start_addr
, size
);
584 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
586 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
587 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
588 phys_ram_base
+ phys_offset
,
591 if (phys_offset
& IO_MEM_ROM
) {
592 phys_offset
&= ~IO_MEM_ROM
;
593 r
= kvm_is_intersecting_mem(kvm_context
, start_addr
);
595 kvm_create_mem_hole(kvm_context
, start_addr
, size
);
596 r
= kvm_register_userspace_phys_mem(kvm_context
, start_addr
,
597 phys_ram_base
+ phys_offset
,
601 printf("kvm_cpu_register_physical_memory: failed\n");
607 if (phys_offset
& IO_MEM_ROM
) {
608 phys_offset
&= ~IO_MEM_ROM
;
609 memcpy(phys_ram_base
+ start_addr
, phys_ram_base
+ phys_offset
, size
);
613 int kvm_qemu_check_extension(int ext
)
615 return kvm_check_extension(kvm_context
, ext
);
618 int kvm_qemu_init_env(CPUState
*cenv
)
620 return kvm_arch_qemu_init_env(cenv
);
623 int kvm_update_debugger(CPUState
*env
)
625 struct kvm_debug_guest dbg
;
629 if (env
->nb_breakpoints
|| env
->singlestep_enabled
) {
631 for (i
= 0; i
< 4 && i
< env
->nb_breakpoints
; ++i
) {
632 dbg
.breakpoints
[i
].enabled
= 1;
633 dbg
.breakpoints
[i
].address
= env
->breakpoints
[i
];
635 dbg
.singlestep
= env
->singlestep_enabled
;
637 return kvm_guest_debug(kvm_context
, env
->cpu_index
, &dbg
);
642 * dirty pages logging
644 /* FIXME: use unsigned long pointer instead of unsigned char */
645 unsigned char *kvm_dirty_bitmap
= NULL
;
646 int kvm_physical_memory_set_dirty_tracking(int enable
)
654 if (!kvm_dirty_bitmap
) {
655 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
656 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
657 if (kvm_dirty_bitmap
== NULL
) {
658 perror("Failed to allocate dirty pages bitmap");
662 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
667 if (kvm_dirty_bitmap
) {
668 r
= kvm_dirty_pages_log_reset(kvm_context
);
669 qemu_free(kvm_dirty_bitmap
);
670 kvm_dirty_bitmap
= NULL
;
676 /* get kvm's dirty pages bitmap and update qemu's */
677 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
678 unsigned char *bitmap
,
680 unsigned long mem_size
)
682 unsigned int i
, j
, n
=0;
684 unsigned page_number
, addr
, addr1
;
685 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
688 * bitmap-traveling is faster than memory-traveling (for addr...)
689 * especially when most of the memory is not dirty.
691 for (i
=0; i
<len
; i
++) {
696 page_number
= i
* 8 + j
;
697 addr1
= page_number
* TARGET_PAGE_SIZE
;
698 addr
= offset
+ addr1
;
699 cpu_physical_memory_set_dirty(addr
);
705 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
706 void *bitmap
, void *opaque
)
708 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
712 * get kvm's dirty pages bitmap and update qemu's
713 * we only care about physical ram, which resides in slots 0 and 3
715 int kvm_update_dirty_pages_log(void)
720 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
721 kvm_dirty_bitmap
, NULL
,
722 kvm_get_dirty_bitmap_cb
);
726 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
728 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
729 unsigned int brsize
= BITMAP_SIZE(ram_size
);
730 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
731 unsigned int extra_bytes
= (extra_pages
+7)/8;
732 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
733 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
735 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
736 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
737 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
742 #ifdef KVM_CAP_IRQCHIP
744 int kvm_set_irq(int irq
, int level
)
746 return kvm_set_irq_level(kvm_context
, irq
, level
);
751 void qemu_kvm_aio_wait_start(void)
755 void qemu_kvm_aio_wait(void)
757 if (!cpu_single_env
|| cpu_single_env
->cpu_index
== 0) {
758 pthread_mutex_unlock(&qemu_mutex
);
759 kvm_eat_signal(cpu_single_env
, 1000);
760 pthread_mutex_lock(&qemu_mutex
);
762 pthread_cond_wait(&qemu_aio_cond
, &qemu_mutex
);
766 void qemu_kvm_aio_wait_end(void)