Clean up KVM/QEMU interaction
[qemu-kvm/fedora.git] / qemu-kvm.c
blob3f12eda37f6b3dca7ead0261a6b022b8046be638
2 #include "config.h"
3 #include "config-host.h"
5 int kvm_allowed = 1;
6 int kvm_irqchip = 1;
8 #include <string.h>
9 #include "hw/hw.h"
10 #include "sysemu.h"
12 #include "qemu-kvm.h"
13 #include <libkvm.h>
14 #include <pthread.h>
15 #include <sys/utsname.h>
17 extern void perror(const char *s);
19 kvm_context_t kvm_context;
21 extern int smp_cpus;
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 {
28 sigset_t sigset;
29 sigset_t negsigset;
32 static struct qemu_kvm_signal_table io_signal_table;
34 #define SIG_IPI (SIGRTMIN+4)
36 struct vcpu_info {
37 CPUState *env;
38 int sipi_needed;
39 int init;
40 pthread_t thread;
41 int signalled;
42 int stop;
43 int stopped;
44 } vcpu_info[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)
59 return;
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);
79 #include <signal.h>
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)
100 return 1;
101 pthread_mutex_unlock(&qemu_mutex);
102 return 0;
105 void kvm_load_registers(CPUState *env)
107 if (kvm_enabled())
108 kvm_arch_load_regs(env);
111 void kvm_save_registers(CPUState *env)
113 if (kvm_enabled())
114 kvm_arch_save_regs(env);
117 int kvm_cpu_exec(CPUState *env)
119 int r;
121 r = kvm_run(kvm_context, env->cpu_index);
122 if (r < 0) {
123 printf("kvm_run returned %d\n", r);
124 exit(1);
127 return 0;
130 extern int vm_running;
132 static int has_work(CPUState *env)
134 if (!vm_running)
135 return 0;
136 if (!(env->hflags & HF_HALTED_MASK))
137 return 1;
138 return kvm_arch_has_work(env);
141 static int kvm_eat_signal(CPUState *env, int timeout)
143 struct timespec ts;
144 int r, e, ret = 0;
145 siginfo_t siginfo;
146 struct sigaction sa;
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)
152 return 0;
153 e = errno;
154 pthread_mutex_lock(&qemu_mutex);
155 if (vcpu)
156 cpu_single_env = vcpu->env;
157 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
158 printf("sigtimedwait: %s\n", strerror(e));
159 exit(1);
161 if (r != -1) {
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);
166 ret = 1;
168 pthread_mutex_unlock(&qemu_mutex);
170 return ret;
174 static void kvm_eat_signals(CPUState *env, int timeout)
176 int r = 0;
178 while (kvm_eat_signal(env, 0))
179 r = 1;
180 if (!r && timeout) {
181 r = kvm_eat_signal(env, timeout);
182 if (r)
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;
192 main_loop_wait(0);
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);
201 else {
202 if (!kvm_irqchip_in_kernel(kvm_context) &&
203 (timeout || vcpu_info[env->cpu_index].stopped)) {
204 sigset_t set;
205 int n;
207 paused:
208 sigemptyset(&set);
209 sigaddset(&set, SIG_IPI);
210 sigwait(&set, &n);
211 } else {
212 struct timespec ts;
213 siginfo_t siginfo;
214 sigset_t set;
216 ts.tv_sec = 0;
217 ts.tv_nsec = 0;
218 sigemptyset(&set);
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);
226 goto paused;
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)
236 int i;
238 for (i = 1; i < smp_cpus; ++i)
239 if (vcpu_info[i].stopped)
240 return 0;
241 return 1;
244 static void pause_other_threads(void)
246 int i;
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)
258 int i;
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)
269 if (running)
270 resume_other_threads();
271 else
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)
284 cpu_reset(env);
285 kvm_arch_load_regs(env);
288 static void setup_kernel_sigmask(CPUState *env)
290 sigset_t set;
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;
311 #ifdef TARGET_I386
312 kvm_tpr_opt_setup(env);
313 #endif
314 while (1) {
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)
324 kvm_cpu_exec(env);
325 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
326 kvm_main_loop_wait(env, 0);
327 if (qemu_shutdown_requested())
328 break;
329 else if (qemu_powerdown_requested())
330 qemu_system_powerdown();
331 else if (qemu_reset_requested()) {
332 env->interrupt_request = 0;
333 qemu_system_reset();
334 kvm_arch_load_regs(env);
337 pthread_mutex_unlock(&qemu_mutex);
338 return 0;
341 static void *ap_main_loop(void *_env)
343 CPUState *env = _env;
344 sigset_t signals;
346 vcpu = &vcpu_info[env->cpu_index];
347 vcpu->env = env;
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);
356 return NULL;
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;
374 int i;
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);
389 env = env->next_cpu;
391 return 0;
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;
406 return 1;
409 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
411 *data = cpu_inb(0, addr);
412 return 0;
415 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
417 *data = cpu_inw(0, addr);
418 return 0;
421 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
423 *data = cpu_inl(0, addr);
424 return 0;
427 #define PM_IO_BASE 0xb000
429 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
431 if (addr == 0xb2) {
432 switch (data) {
433 case 0: {
434 cpu_outb(0, 0xb3, 0);
435 break;
437 case 0xf0: {
438 unsigned x;
440 /* enable acpi */
441 x = cpu_inw(0, PM_IO_BASE + 4);
442 x &= ~1;
443 cpu_outw(0, PM_IO_BASE + 4, x);
444 break;
446 case 0xf1: {
447 unsigned x;
449 /* enable acpi */
450 x = cpu_inw(0, PM_IO_BASE + 4);
451 x |= 1;
452 cpu_outw(0, PM_IO_BASE + 4, x);
453 break;
455 default:
456 break;
458 return 0;
460 cpu_outb(0, addr, data);
461 return 0;
464 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
466 cpu_outw(0, addr, data);
467 return 0;
470 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
472 cpu_outl(0, addr, data);
473 return 0;
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);
479 return 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);
485 return 0;
488 static int kvm_io_window(void *opaque)
490 return 1;
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();
502 return 1;
505 static struct kvm_callbacks qemu_kvm_ops = {
506 .debug = kvm_debug,
507 .inb = kvm_inb,
508 .inw = kvm_inw,
509 .inl = kvm_inl,
510 .outb = kvm_outb,
511 .outw = kvm_outw,
512 .outl = kvm_outl,
513 .mmio_read = kvm_mmio_read,
514 .mmio_write = kvm_mmio_write,
515 .halt = kvm_halt,
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,
521 #ifdef TARGET_I386
522 .tpr_access = handle_tpr_access,
523 #endif
524 #ifdef TARGET_PPC
525 .powerpc_dcr_read = handle_powerpc_dcr_read,
526 .powerpc_dcr_write = handle_powerpc_dcr_write,
527 #endif
530 int kvm_qemu_init()
532 /* Try to initialize kvm */
533 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
534 if (!kvm_context) {
535 return -1;
537 pthread_mutex_lock(&qemu_mutex);
539 return 0;
542 int kvm_qemu_create_context(void)
544 int r;
545 if (!kvm_irqchip) {
546 kvm_disable_irqchip_creation(kvm_context);
548 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
549 kvm_qemu_destroy();
550 return -1;
552 r = kvm_arch_qemu_create_context();
553 if(r <0)
554 kvm_qemu_destroy();
555 return 0;
558 void kvm_qemu_destroy(void)
560 kvm_finalize(kvm_context);
563 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
564 unsigned long size,
565 unsigned long phys_offset)
567 #ifdef KVM_CAP_USER_MEMORY
568 int r = 0;
570 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
571 if (r) {
572 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
573 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
574 if (r)
575 return;
576 r = kvm_is_intersecting_mem(kvm_context, start_addr);
577 if (r)
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,
581 size, 0);
583 if (phys_offset & IO_MEM_ROM) {
584 phys_offset &= ~IO_MEM_ROM;
585 r = kvm_is_intersecting_mem(kvm_context, start_addr);
586 if (r)
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,
590 size, 0);
592 if (r < 0) {
593 printf("kvm_cpu_register_physical_memory: failed\n");
594 exit(1);
596 return;
598 #endif
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;
618 int i;
620 dbg.enabled = 0;
621 if (env->nb_breakpoints || env->singlestep_enabled) {
622 dbg.enabled = 1;
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)
640 int r = 0;
642 if (!kvm_enabled())
643 return 0;
645 if (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");
651 r=-1;
653 else {
654 r = kvm_dirty_pages_log_enable_all(kvm_context);
658 else {
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;
665 return r;
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,
671 unsigned int offset,
672 unsigned long mem_size)
674 unsigned int i, j, n=0;
675 unsigned char c;
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++) {
684 c = bitmap[i];
685 while (c>0) {
686 j = ffsl(c) - 1;
687 c &= ~(1u<<j);
688 page_number = i * 8 + j;
689 addr1 = page_number * TARGET_PAGE_SIZE;
690 addr = offset + addr1;
691 cpu_physical_memory_set_dirty(addr);
692 n++;
695 return 0;
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)
709 int r = 0;
712 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
713 kvm_dirty_bitmap, NULL,
714 kvm_get_dirty_bitmap_cb);
715 return r;
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);
731 return 0;
734 #ifdef KVM_CAP_IRQCHIP
736 int kvm_set_irq(int irq, int level)
738 return kvm_set_irq_level(kvm_context, irq, level);
741 #endif
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);
753 } else {
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,
774 unsigned long size)
776 kvm_destroy_phys_mem(kvm_context, start_addr, size);