kvm: libkvm: Add function kvm_create_kernel_phys_mem to libkvm-powerpc.c
[qemu-kvm/fedora.git] / qemu-kvm.c
blobdd8468691fbaebf3738647c65b069653f4acebed
2 #include "config.h"
3 #include "config-host.h"
5 #ifdef USE_KVM
6 #define KVM_ALLOWED_DEFAULT 1
7 #else
8 #define KVM_ALLOWED_DEFAULT 0
9 #endif
11 int kvm_allowed = KVM_ALLOWED_DEFAULT;
12 int kvm_irqchip = 1;
14 #ifdef USE_KVM
16 #include <string.h>
17 #include "hw/hw.h"
18 #include "sysemu.h"
20 #include "qemu-kvm.h"
21 #include <libkvm.h>
22 #include <pthread.h>
23 #include <sys/utsname.h>
25 extern void perror(const char *s);
27 kvm_context_t kvm_context;
29 extern int smp_cpus;
31 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
32 __thread CPUState *vcpu_env;
34 static sigset_t io_sigset, io_negsigset;
36 static int wait_hack;
38 #define SIG_IPI (SIGRTMIN+4)
40 struct vcpu_info {
41 int sipi_needed;
42 int init;
43 pthread_t thread;
44 int signalled;
45 int stop;
46 int stopped;
47 } vcpu_info[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)
57 return;
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;
76 if (first_sipi) {
77 wait_hack = 1;
78 first_sipi = 0;
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);
90 #include <signal.h>
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) {
109 int i;
111 wait_hack = 0;
113 pthread_mutex_unlock(&qemu_mutex);
114 for (i = 0; i < 10; ++i)
115 usleep(1000);
116 pthread_mutex_lock(&qemu_mutex);
119 kvm_arch_pre_kvm_run(opaque, vcpu);
121 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
122 return 1;
123 pthread_mutex_unlock(&qemu_mutex);
124 return 0;
127 void kvm_load_registers(CPUState *env)
129 if (kvm_allowed)
130 kvm_arch_load_regs(env);
133 void kvm_save_registers(CPUState *env)
135 if (kvm_allowed)
136 kvm_arch_save_regs(env);
139 int kvm_cpu_exec(CPUState *env)
141 int r;
143 r = kvm_run(kvm_context, env->cpu_index);
144 if (r < 0) {
145 printf("kvm_run returned %d\n", r);
146 exit(1);
149 return 0;
152 extern int vm_running;
154 static int has_work(CPUState *env)
156 if (!vm_running)
157 return 0;
158 if (!(env->hflags & HF_HALTED_MASK))
159 return 1;
160 return kvm_arch_has_work(env);
163 static int kvm_eat_signal(CPUState *env, int timeout)
165 struct timespec ts;
166 int r, e, ret = 0;
167 siginfo_t siginfo;
168 struct sigaction sa;
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)
174 return 0;
175 e = errno;
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));
180 exit(1);
182 if (r != -1) {
183 sigaction(siginfo.si_signo, NULL, &sa);
184 sa.sa_handler(siginfo.si_signo);
185 ret = 1;
187 pthread_mutex_unlock(&qemu_mutex);
189 return ret;
193 static void kvm_eat_signals(CPUState *env, int timeout)
195 int r = 0;
197 while (kvm_eat_signal(env, 0))
198 r = 1;
199 if (!r && timeout) {
200 r = kvm_eat_signal(env, timeout);
201 if (r)
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;
211 main_loop_wait(0);
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);
220 else {
221 if (!kvm_irqchip_in_kernel(kvm_context) &&
222 (timeout || vcpu_info[env->cpu_index].stopped)) {
223 sigset_t set;
224 int n;
226 paused:
227 sigemptyset(&set);
228 sigaddset(&set, SIG_IPI);
229 sigwait(&set, &n);
230 } else {
231 struct timespec ts;
232 siginfo_t siginfo;
233 sigset_t set;
235 ts.tv_sec = 0;
236 ts.tv_nsec = 0;
237 sigemptyset(&set);
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);
245 goto paused;
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)
255 int i;
257 for (i = 1; i < smp_cpus; ++i)
258 if (vcpu_info[i].stopped)
259 return 0;
260 return 1;
263 static void pause_other_threads(void)
265 int i;
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)
277 int i;
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)
288 if (running)
289 resume_other_threads();
290 else
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)
303 cpu_reset(env);
304 kvm_arch_load_regs(env);
307 static void setup_kernel_sigmask(CPUState *env)
309 sigset_t set;
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 #ifdef TARGET_I386
331 kvm_tpr_opt_setup(env);
332 #endif
333 while (1) {
334 while (!has_work(env))
335 kvm_main_loop_wait(env, 10);
336 if (env->interrupt_request & CPU_INTERRUPT_HARD)
337 env->hflags &= ~HF_HALTED_MASK;
338 if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
339 update_regs_for_sipi(env);
340 if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
341 update_regs_for_init(env);
342 if (!(env->hflags & HF_HALTED_MASK) && !info->init)
343 kvm_cpu_exec(env);
344 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
345 kvm_main_loop_wait(env, 0);
346 if (qemu_shutdown_requested())
347 break;
348 else if (qemu_powerdown_requested())
349 qemu_system_powerdown();
350 else if (qemu_reset_requested()) {
351 env->interrupt_request = 0;
352 qemu_system_reset();
353 kvm_arch_load_regs(env);
356 pthread_mutex_unlock(&qemu_mutex);
357 return 0;
360 static void *ap_main_loop(void *_env)
362 CPUState *env = _env;
363 sigset_t signals;
365 vcpu_env = env;
366 sigfillset(&signals);
367 //sigdelset(&signals, SIG_IPI);
368 sigprocmask(SIG_BLOCK, &signals, NULL);
369 kvm_create_vcpu(kvm_context, env->cpu_index);
370 kvm_qemu_init_env(env);
371 if (kvm_irqchip_in_kernel(kvm_context))
372 env->hflags &= ~HF_HALTED_MASK;
373 kvm_main_loop_cpu(env);
374 return NULL;
377 static void kvm_add_signal(int signum)
379 sigaddset(&io_sigset, signum);
380 sigdelset(&io_negsigset, signum);
381 sigprocmask(SIG_BLOCK, &io_sigset, NULL);
384 int kvm_init_ap(void)
386 CPUState *env = first_cpu->next_cpu;
387 int i;
389 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
390 sigemptyset(&io_sigset);
391 sigfillset(&io_negsigset);
392 kvm_add_signal(SIGIO);
393 kvm_add_signal(SIGALRM);
394 kvm_add_signal(SIGUSR2);
395 if (!kvm_irqchip_in_kernel(kvm_context))
396 kvm_add_signal(SIG_IPI);
398 vcpu_env = first_cpu;
399 signal(SIG_IPI, sig_ipi_handler);
400 for (i = 1; i < smp_cpus; ++i) {
401 pthread_create(&vcpu_info[i].thread, NULL, ap_main_loop, env);
402 env = env->next_cpu;
404 return 0;
407 int kvm_main_loop(void)
409 vcpu_info[0].thread = pthread_self();
410 return kvm_main_loop_cpu(first_cpu);
413 static int kvm_debug(void *opaque, int vcpu)
415 CPUState *env = cpu_single_env;
417 env->exception_index = EXCP_DEBUG;
418 return 1;
421 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
423 *data = cpu_inb(0, addr);
424 return 0;
427 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
429 *data = cpu_inw(0, addr);
430 return 0;
433 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
435 *data = cpu_inl(0, addr);
436 return 0;
439 #define PM_IO_BASE 0xb000
441 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
443 if (addr == 0xb2) {
444 switch (data) {
445 case 0: {
446 cpu_outb(0, 0xb3, 0);
447 break;
449 case 0xf0: {
450 unsigned x;
452 /* enable acpi */
453 x = cpu_inw(0, PM_IO_BASE + 4);
454 x &= ~1;
455 cpu_outw(0, PM_IO_BASE + 4, x);
456 break;
458 case 0xf1: {
459 unsigned x;
461 /* enable acpi */
462 x = cpu_inw(0, PM_IO_BASE + 4);
463 x |= 1;
464 cpu_outw(0, PM_IO_BASE + 4, x);
465 break;
467 default:
468 break;
470 return 0;
472 cpu_outb(0, addr, data);
473 return 0;
476 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
478 cpu_outw(0, addr, data);
479 return 0;
482 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
484 cpu_outl(0, addr, data);
485 return 0;
488 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
490 cpu_physical_memory_rw(addr, data, len, 0);
491 return 0;
494 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
496 cpu_physical_memory_rw(addr, data, len, 1);
497 return 0;
500 static int kvm_io_window(void *opaque)
502 return 1;
506 static int kvm_halt(void *opaque, int vcpu)
508 return kvm_arch_halt(opaque, vcpu);
511 static int kvm_shutdown(void *opaque, int vcpu)
513 qemu_system_reset_request();
514 return 1;
517 static struct kvm_callbacks qemu_kvm_ops = {
518 .debug = kvm_debug,
519 .inb = kvm_inb,
520 .inw = kvm_inw,
521 .inl = kvm_inl,
522 .outb = kvm_outb,
523 .outw = kvm_outw,
524 .outl = kvm_outl,
525 .mmio_read = kvm_mmio_read,
526 .mmio_write = kvm_mmio_write,
527 .halt = kvm_halt,
528 .shutdown = kvm_shutdown,
529 .io_window = kvm_io_window,
530 .try_push_interrupts = try_push_interrupts,
531 .post_kvm_run = post_kvm_run,
532 .pre_kvm_run = pre_kvm_run,
533 #ifdef TARGET_I386
534 .tpr_access = handle_tpr_access,
535 #endif
538 int kvm_qemu_init()
540 /* Try to initialize kvm */
541 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
542 if (!kvm_context) {
543 return -1;
546 return 0;
549 int kvm_qemu_create_context(void)
551 int r;
552 if (!kvm_irqchip) {
553 kvm_disable_irqchip_creation(kvm_context);
555 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
556 kvm_qemu_destroy();
557 return -1;
559 r = kvm_arch_qemu_create_context();
560 if(r <0)
561 kvm_qemu_destroy();
562 return 0;
565 void kvm_qemu_destroy(void)
567 kvm_finalize(kvm_context);
570 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
571 unsigned long size,
572 unsigned long phys_offset)
574 #ifdef KVM_CAP_USER_MEMORY
575 int r = 0;
577 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
578 if (r) {
579 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
580 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
581 if (r)
582 return;
583 r = kvm_is_intersecting_mem(kvm_context, start_addr);
584 if (r)
585 kvm_create_mem_hole(kvm_context, start_addr, size);
586 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
587 phys_ram_base + phys_offset,
588 size, 0);
590 if (phys_offset & IO_MEM_ROM) {
591 phys_offset &= ~IO_MEM_ROM;
592 r = kvm_is_intersecting_mem(kvm_context, start_addr);
593 if (r)
594 kvm_create_mem_hole(kvm_context, start_addr, size);
595 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
596 phys_ram_base + phys_offset,
597 size, 0);
599 if (r < 0) {
600 printf("kvm_cpu_register_physical_memory: failed\n");
601 exit(1);
603 return;
605 #endif
606 if (phys_offset & IO_MEM_ROM) {
607 phys_offset &= ~IO_MEM_ROM;
608 memcpy(phys_ram_base + start_addr, phys_ram_base + phys_offset, size);
612 int kvm_qemu_check_extension(int ext)
614 return kvm_check_extension(kvm_context, ext);
617 int kvm_qemu_init_env(CPUState *cenv)
619 return kvm_arch_qemu_init_env(cenv);
622 int kvm_update_debugger(CPUState *env)
624 struct kvm_debug_guest dbg;
625 int i;
627 dbg.enabled = 0;
628 if (env->nb_breakpoints || env->singlestep_enabled) {
629 dbg.enabled = 1;
630 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
631 dbg.breakpoints[i].enabled = 1;
632 dbg.breakpoints[i].address = env->breakpoints[i];
634 dbg.singlestep = env->singlestep_enabled;
636 return kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
641 * dirty pages logging
643 /* FIXME: use unsigned long pointer instead of unsigned char */
644 unsigned char *kvm_dirty_bitmap = NULL;
645 int kvm_physical_memory_set_dirty_tracking(int enable)
647 int r = 0;
649 if (!kvm_allowed)
650 return 0;
652 if (enable) {
653 if (!kvm_dirty_bitmap) {
654 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
655 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
656 if (kvm_dirty_bitmap == NULL) {
657 perror("Failed to allocate dirty pages bitmap");
658 r=-1;
660 else {
661 r = kvm_dirty_pages_log_enable_all(kvm_context);
665 else {
666 if (kvm_dirty_bitmap) {
667 r = kvm_dirty_pages_log_reset(kvm_context);
668 qemu_free(kvm_dirty_bitmap);
669 kvm_dirty_bitmap = NULL;
672 return r;
675 /* get kvm's dirty pages bitmap and update qemu's */
676 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
677 unsigned char *bitmap,
678 unsigned int offset,
679 unsigned long mem_size)
681 unsigned int i, j, n=0;
682 unsigned char c;
683 unsigned page_number, addr, addr1;
684 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
687 * bitmap-traveling is faster than memory-traveling (for addr...)
688 * especially when most of the memory is not dirty.
690 for (i=0; i<len; i++) {
691 c = bitmap[i];
692 while (c>0) {
693 j = ffsl(c) - 1;
694 c &= ~(1u<<j);
695 page_number = i * 8 + j;
696 addr1 = page_number * TARGET_PAGE_SIZE;
697 addr = offset + addr1;
698 cpu_physical_memory_set_dirty(addr);
699 n++;
702 return 0;
704 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
705 void *bitmap, void *opaque)
707 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
711 * get kvm's dirty pages bitmap and update qemu's
712 * we only care about physical ram, which resides in slots 0 and 3
714 int kvm_update_dirty_pages_log(void)
716 int r = 0;
719 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
720 kvm_dirty_bitmap, NULL,
721 kvm_get_dirty_bitmap_cb);
722 return r;
725 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
727 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
728 unsigned int brsize = BITMAP_SIZE(ram_size);
729 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
730 unsigned int extra_bytes = (extra_pages +7)/8;
731 unsigned int hole_start = BITMAP_SIZE(0xa0000);
732 unsigned int hole_end = BITMAP_SIZE(0xc0000);
734 memset(bitmap, 0xFF, brsize + extra_bytes);
735 memset(bitmap + hole_start, 0, hole_end - hole_start);
736 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
738 return 0;
741 #ifdef KVM_CAP_IRQCHIP
743 int kvm_set_irq(int irq, int level)
745 return kvm_set_irq_level(kvm_context, irq, level);
748 #endif
750 #endif