Handle INIT before SIPI.
[qemu-kvm/fedora.git] / qemu-kvm.c
blob67d1de9aa0bf64fc6555db476bc4d8f77aed973b
1 /*
2 * qemu/kvm integration
4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
7 */
8 #include "config.h"
9 #include "config-host.h"
11 int kvm_allowed = 1;
12 int kvm_irqchip = 1;
13 int kvm_pit = 1;
15 #include <assert.h>
16 #include <string.h>
17 #include "hw/hw.h"
18 #include "sysemu.h"
19 #include "qemu-common.h"
20 #include "console.h"
21 #include "block.h"
22 #include "compatfd.h"
24 #include "qemu-kvm.h"
25 #include <libkvm.h>
26 #include <pthread.h>
27 #include <sys/utsname.h>
28 #include <sys/syscall.h>
29 #include <sys/mman.h>
31 #define bool _Bool
32 #define false 0
33 #define true 1
35 extern void perror(const char *s);
37 kvm_context_t kvm_context;
39 extern int smp_cpus;
41 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
42 pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
43 pthread_cond_t qemu_system_cond = PTHREAD_COND_INITIALIZER;
44 pthread_cond_t qemu_pause_cond = PTHREAD_COND_INITIALIZER;
45 pthread_cond_t qemu_work_cond = PTHREAD_COND_INITIALIZER;
46 __thread struct vcpu_info *vcpu;
48 static int qemu_system_ready;
50 #define SIG_IPI (SIGRTMIN+4)
52 struct qemu_kvm_work_item {
53 struct qemu_kvm_work_item *next;
54 void (*func)(void *data);
55 void *data;
56 bool done;
59 struct vcpu_info {
60 CPUState *env;
61 int sipi_needed;
62 int init;
63 pthread_t thread;
64 int signalled;
65 int stop;
66 int stopped;
67 int created;
68 struct qemu_kvm_work_item *queued_work_first, *queued_work_last;
69 } vcpu_info[256];
71 pthread_t io_thread;
72 static int io_thread_fd = -1;
73 static int io_thread_sigfd = -1;
75 static int kvm_debug_stop_requested;
77 static inline unsigned long kvm_get_thread_id(void)
79 return syscall(SYS_gettid);
82 static void qemu_cond_wait(pthread_cond_t *cond)
84 CPUState *env = cpu_single_env;
85 static const struct timespec ts = {
86 .tv_sec = 0,
87 .tv_nsec = 100000,
90 pthread_cond_timedwait(cond, &qemu_mutex, &ts);
91 cpu_single_env = env;
94 CPUState *qemu_kvm_cpu_env(int index)
96 return vcpu_info[index].env;
99 static void sig_ipi_handler(int n)
103 static void on_vcpu(CPUState *env, void (*func)(void *data), void *data)
105 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
106 struct qemu_kvm_work_item wi;
108 if (vi == vcpu) {
109 func(data);
110 return;
113 wi.func = func;
114 wi.data = data;
115 if (!vi->queued_work_first)
116 vi->queued_work_first = &wi;
117 else
118 vi->queued_work_last->next = &wi;
119 vi->queued_work_last = &wi;
120 wi.next = NULL;
121 wi.done = false;
123 pthread_kill(vi->thread, SIG_IPI);
124 while (!wi.done)
125 qemu_cond_wait(&qemu_work_cond);
128 static void inject_interrupt(void *data)
130 cpu_interrupt(vcpu->env, (int)data);
133 void kvm_inject_interrupt(CPUState *env, int mask)
135 on_vcpu(env, inject_interrupt, (void *)mask);
138 void kvm_update_interrupt_request(CPUState *env)
140 int signal = 0;
142 if (env) {
143 if (!vcpu)
144 signal = 1;
145 if (vcpu && env != vcpu->env && !vcpu_info[env->cpu_index].signalled)
146 signal = 1;
148 if (signal) {
149 vcpu_info[env->cpu_index].signalled = 1;
150 if (vcpu_info[env->cpu_index].thread)
151 pthread_kill(vcpu_info[env->cpu_index].thread, SIG_IPI);
156 void kvm_update_after_sipi(CPUState *env)
158 vcpu_info[env->cpu_index].sipi_needed = 1;
159 kvm_update_interrupt_request(env);
162 void kvm_apic_init(CPUState *env)
164 if (env->cpu_index != 0)
165 vcpu_info[env->cpu_index].init = 1;
166 kvm_update_interrupt_request(env);
169 #include <signal.h>
171 static int try_push_interrupts(void *opaque)
173 return kvm_arch_try_push_interrupts(opaque);
176 static int try_push_nmi(void *opaque)
178 return kvm_arch_try_push_nmi(opaque);
181 static void post_kvm_run(void *opaque, int vcpu)
184 pthread_mutex_lock(&qemu_mutex);
185 kvm_arch_post_kvm_run(opaque, vcpu);
188 static int pre_kvm_run(void *opaque, int vcpu)
190 CPUState *env = qemu_kvm_cpu_env(vcpu);
192 kvm_arch_pre_kvm_run(opaque, vcpu);
194 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
195 return 1;
196 pthread_mutex_unlock(&qemu_mutex);
197 return 0;
200 static void kvm_do_load_registers(void *_env)
202 CPUState *env = _env;
204 kvm_arch_load_regs(env);
207 void kvm_load_registers(CPUState *env)
209 if (kvm_enabled() && qemu_system_ready)
210 on_vcpu(env, kvm_do_load_registers, env);
213 static void kvm_do_save_registers(void *_env)
215 CPUState *env = _env;
217 kvm_arch_save_regs(env);
220 void kvm_save_registers(CPUState *env)
222 if (kvm_enabled())
223 on_vcpu(env, kvm_do_save_registers, env);
226 int kvm_cpu_exec(CPUState *env)
228 int r;
230 r = kvm_run(kvm_context, env->cpu_index);
231 if (r < 0) {
232 printf("kvm_run returned %d\n", r);
233 exit(1);
236 return 0;
239 extern int vm_running;
241 static int has_work(CPUState *env)
243 if (!vm_running || (env && vcpu_info[env->cpu_index].stopped))
244 return 0;
245 if (!env->halted)
246 return 1;
247 return kvm_arch_has_work(env);
250 static void flush_queued_work(CPUState *env)
252 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
253 struct qemu_kvm_work_item *wi;
255 if (!vi->queued_work_first)
256 return;
258 while ((wi = vi->queued_work_first)) {
259 vi->queued_work_first = wi->next;
260 wi->func(wi->data);
261 wi->done = true;
263 vi->queued_work_last = NULL;
264 pthread_cond_broadcast(&qemu_work_cond);
267 static void kvm_main_loop_wait(CPUState *env, int timeout)
269 struct timespec ts;
270 int r, e;
271 siginfo_t siginfo;
272 sigset_t waitset;
274 pthread_mutex_unlock(&qemu_mutex);
276 ts.tv_sec = timeout / 1000;
277 ts.tv_nsec = (timeout % 1000) * 1000000;
278 sigemptyset(&waitset);
279 sigaddset(&waitset, SIG_IPI);
281 r = sigtimedwait(&waitset, &siginfo, &ts);
282 e = errno;
284 pthread_mutex_lock(&qemu_mutex);
286 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
287 printf("sigtimedwait: %s\n", strerror(e));
288 exit(1);
291 cpu_single_env = env;
292 flush_queued_work(env);
294 if (vcpu_info[env->cpu_index].stop) {
295 vcpu_info[env->cpu_index].stop = 0;
296 vcpu_info[env->cpu_index].stopped = 1;
297 pthread_cond_signal(&qemu_pause_cond);
300 vcpu_info[env->cpu_index].signalled = 0;
303 static int all_threads_paused(void)
305 int i;
307 for (i = 0; i < smp_cpus; ++i)
308 if (vcpu_info[i].stop)
309 return 0;
310 return 1;
313 static void pause_all_threads(void)
315 int i;
317 assert(!cpu_single_env);
319 for (i = 0; i < smp_cpus; ++i) {
320 vcpu_info[i].stop = 1;
321 pthread_kill(vcpu_info[i].thread, SIG_IPI);
323 while (!all_threads_paused())
324 qemu_cond_wait(&qemu_pause_cond);
327 static void resume_all_threads(void)
329 int i;
331 assert(!cpu_single_env);
333 for (i = 0; i < smp_cpus; ++i) {
334 vcpu_info[i].stop = 0;
335 vcpu_info[i].stopped = 0;
336 pthread_kill(vcpu_info[i].thread, SIG_IPI);
340 static void kvm_vm_state_change_handler(void *context, int running)
342 if (running)
343 resume_all_threads();
344 else
345 pause_all_threads();
348 static void update_regs_for_sipi(CPUState *env)
350 kvm_arch_update_regs_for_sipi(env);
351 vcpu_info[env->cpu_index].sipi_needed = 0;
354 static void update_regs_for_init(CPUState *env)
356 SegmentCache cs = env->segs[R_CS];
358 cpu_reset(env);
360 /* restore SIPI vector */
361 if(vcpu_info[env->cpu_index].sipi_needed)
362 env->segs[R_CS] = cs;
364 kvm_arch_load_regs(env);
365 vcpu_info[env->cpu_index].init = 0;
368 static void setup_kernel_sigmask(CPUState *env)
370 sigset_t set;
372 sigemptyset(&set);
373 sigaddset(&set, SIGUSR2);
374 sigaddset(&set, SIGIO);
375 sigaddset(&set, SIGALRM);
376 sigprocmask(SIG_BLOCK, &set, NULL);
378 sigprocmask(SIG_BLOCK, NULL, &set);
379 sigdelset(&set, SIG_IPI);
381 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
384 void qemu_kvm_system_reset(void)
386 int i;
388 pause_all_threads();
390 qemu_system_reset();
392 for (i = 0; i < smp_cpus; ++i)
393 kvm_arch_cpu_reset(vcpu_info[i].env);
395 resume_all_threads();
398 static int kvm_main_loop_cpu(CPUState *env)
400 struct vcpu_info *info = &vcpu_info[env->cpu_index];
402 setup_kernel_sigmask(env);
404 pthread_mutex_lock(&qemu_mutex);
405 if (kvm_irqchip_in_kernel(kvm_context))
406 env->halted = 0;
408 kvm_qemu_init_env(env);
409 #ifdef TARGET_I386
410 kvm_tpr_vcpu_start(env);
411 #endif
413 cpu_single_env = env;
414 kvm_load_registers(env);
416 while (1) {
417 while (!has_work(env))
418 kvm_main_loop_wait(env, 1000);
419 if (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI))
420 env->halted = 0;
421 if (!kvm_irqchip_in_kernel(kvm_context)) {
422 if (info->init)
423 update_regs_for_init(env);
424 if (info->sipi_needed)
425 update_regs_for_sipi(env);
427 if (!env->halted && !info->init)
428 kvm_cpu_exec(env);
429 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
430 kvm_main_loop_wait(env, 0);
432 pthread_mutex_unlock(&qemu_mutex);
433 return 0;
436 static void *ap_main_loop(void *_env)
438 CPUState *env = _env;
439 sigset_t signals;
441 vcpu = &vcpu_info[env->cpu_index];
442 vcpu->env = env;
443 vcpu->env->thread_id = kvm_get_thread_id();
444 sigfillset(&signals);
445 sigprocmask(SIG_BLOCK, &signals, NULL);
446 kvm_create_vcpu(kvm_context, env->cpu_index);
447 kvm_qemu_init_env(env);
449 /* signal VCPU creation */
450 pthread_mutex_lock(&qemu_mutex);
451 vcpu->created = 1;
452 pthread_cond_signal(&qemu_vcpu_cond);
454 /* and wait for machine initialization */
455 while (!qemu_system_ready)
456 qemu_cond_wait(&qemu_system_cond);
457 pthread_mutex_unlock(&qemu_mutex);
459 kvm_main_loop_cpu(env);
460 return NULL;
463 void kvm_init_new_ap(int cpu, CPUState *env)
465 pthread_create(&vcpu_info[cpu].thread, NULL, ap_main_loop, env);
467 while (vcpu_info[cpu].created == 0)
468 qemu_cond_wait(&qemu_vcpu_cond);
471 int kvm_init_ap(void)
473 #ifdef TARGET_I386
474 kvm_tpr_opt_setup();
475 #endif
476 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
478 signal(SIG_IPI, sig_ipi_handler);
479 return 0;
482 void qemu_kvm_notify_work(void)
484 uint64_t value = 1;
485 char buffer[8];
486 size_t offset = 0;
488 if (io_thread_fd == -1)
489 return;
491 memcpy(buffer, &value, sizeof(value));
493 while (offset < 8) {
494 ssize_t len;
496 len = write(io_thread_fd, buffer + offset, 8 - offset);
497 if (len == -1 && errno == EINTR)
498 continue;
500 if (len <= 0)
501 break;
503 offset += len;
506 if (offset != 8)
507 fprintf(stderr, "failed to notify io thread\n");
510 /* If we have signalfd, we mask out the signals we want to handle and then
511 * use signalfd to listen for them. We rely on whatever the current signal
512 * handler is to dispatch the signals when we receive them.
515 static void sigfd_handler(void *opaque)
517 int fd = (unsigned long)opaque;
518 struct qemu_signalfd_siginfo info;
519 struct sigaction action;
520 ssize_t len;
522 while (1) {
523 do {
524 len = read(fd, &info, sizeof(info));
525 } while (len == -1 && errno == EINTR);
527 if (len == -1 && errno == EAGAIN)
528 break;
530 if (len != sizeof(info)) {
531 printf("read from sigfd returned %ld: %m\n", len);
532 return;
535 sigaction(info.ssi_signo, NULL, &action);
536 if (action.sa_handler)
537 action.sa_handler(info.ssi_signo);
542 /* Used to break IO thread out of select */
543 static void io_thread_wakeup(void *opaque)
545 int fd = (unsigned long)opaque;
546 char buffer[8];
547 size_t offset = 0;
549 while (offset < 8) {
550 ssize_t len;
552 len = read(fd, buffer + offset, 8 - offset);
553 if (len == -1 && errno == EINTR)
554 continue;
556 if (len <= 0)
557 break;
559 offset += len;
563 int kvm_main_loop(void)
565 int fds[2];
566 sigset_t mask;
567 int sigfd;
569 io_thread = pthread_self();
570 qemu_system_ready = 1;
572 if (qemu_eventfd(fds) == -1) {
573 fprintf(stderr, "failed to create eventfd\n");
574 return -errno;
577 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
578 (void *)(unsigned long)fds[0]);
580 io_thread_fd = fds[1];
582 sigemptyset(&mask);
583 sigaddset(&mask, SIGIO);
584 sigaddset(&mask, SIGALRM);
585 sigprocmask(SIG_BLOCK, &mask, NULL);
587 sigfd = qemu_signalfd(&mask);
588 if (sigfd == -1) {
589 fprintf(stderr, "failed to create signalfd\n");
590 return -errno;
593 fcntl(sigfd, F_SETFL, O_NONBLOCK);
595 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
596 (void *)(unsigned long)sigfd);
598 pthread_cond_broadcast(&qemu_system_cond);
600 io_thread_sigfd = sigfd;
601 cpu_single_env = NULL;
603 while (1) {
604 main_loop_wait(1000);
605 if (qemu_shutdown_requested())
606 break;
607 else if (qemu_powerdown_requested())
608 qemu_system_powerdown();
609 else if (qemu_reset_requested())
610 qemu_kvm_system_reset();
611 else if (kvm_debug_stop_requested) {
612 vm_stop(EXCP_DEBUG);
613 kvm_debug_stop_requested = 0;
617 pause_all_threads();
618 pthread_mutex_unlock(&qemu_mutex);
620 return 0;
623 static int kvm_debug(void *opaque, int vcpu)
625 kvm_debug_stop_requested = 1;
626 vcpu_info[vcpu].stopped = 1;
627 return 1;
630 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
632 *data = cpu_inb(0, addr);
633 return 0;
636 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
638 *data = cpu_inw(0, addr);
639 return 0;
642 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
644 *data = cpu_inl(0, addr);
645 return 0;
648 #define PM_IO_BASE 0xb000
650 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
652 if (addr == 0xb2) {
653 switch (data) {
654 case 0: {
655 cpu_outb(0, 0xb3, 0);
656 break;
658 case 0xf0: {
659 unsigned x;
661 /* enable acpi */
662 x = cpu_inw(0, PM_IO_BASE + 4);
663 x &= ~1;
664 cpu_outw(0, PM_IO_BASE + 4, x);
665 break;
667 case 0xf1: {
668 unsigned x;
670 /* enable acpi */
671 x = cpu_inw(0, PM_IO_BASE + 4);
672 x |= 1;
673 cpu_outw(0, PM_IO_BASE + 4, x);
674 break;
676 default:
677 break;
679 return 0;
681 cpu_outb(0, addr, data);
682 return 0;
685 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
687 cpu_outw(0, addr, data);
688 return 0;
691 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
693 cpu_outl(0, addr, data);
694 return 0;
697 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
699 cpu_physical_memory_rw(addr, data, len, 0);
700 return 0;
703 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
705 cpu_physical_memory_rw(addr, data, len, 1);
706 return 0;
709 static int kvm_io_window(void *opaque)
711 return 1;
715 static int kvm_halt(void *opaque, int vcpu)
717 return kvm_arch_halt(opaque, vcpu);
720 static int kvm_shutdown(void *opaque, int vcpu)
722 /* stop the current vcpu from going back to guest mode */
723 vcpu_info[cpu_single_env->cpu_index].stopped = 1;
725 qemu_system_reset_request();
726 return 1;
729 static struct kvm_callbacks qemu_kvm_ops = {
730 .debug = kvm_debug,
731 .inb = kvm_inb,
732 .inw = kvm_inw,
733 .inl = kvm_inl,
734 .outb = kvm_outb,
735 .outw = kvm_outw,
736 .outl = kvm_outl,
737 .mmio_read = kvm_mmio_read,
738 .mmio_write = kvm_mmio_write,
739 .halt = kvm_halt,
740 .shutdown = kvm_shutdown,
741 .io_window = kvm_io_window,
742 .try_push_interrupts = try_push_interrupts,
743 .try_push_nmi = try_push_nmi,
744 .post_kvm_run = post_kvm_run,
745 .pre_kvm_run = pre_kvm_run,
746 #ifdef TARGET_I386
747 .tpr_access = handle_tpr_access,
748 #endif
749 #ifdef TARGET_PPC
750 .powerpc_dcr_read = handle_powerpc_dcr_read,
751 .powerpc_dcr_write = handle_powerpc_dcr_write,
752 #endif
755 int kvm_qemu_init()
757 /* Try to initialize kvm */
758 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
759 if (!kvm_context) {
760 return -1;
762 pthread_mutex_lock(&qemu_mutex);
764 return 0;
767 int kvm_qemu_create_context(void)
769 int r;
770 if (!kvm_irqchip) {
771 kvm_disable_irqchip_creation(kvm_context);
773 if (!kvm_pit) {
774 kvm_disable_pit_creation(kvm_context);
776 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
777 kvm_qemu_destroy();
778 return -1;
780 r = kvm_arch_qemu_create_context();
781 if(r <0)
782 kvm_qemu_destroy();
783 return 0;
786 void kvm_qemu_destroy(void)
788 kvm_finalize(kvm_context);
791 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
792 unsigned long size,
793 unsigned long phys_offset)
795 int r = 0;
796 unsigned long area_flags = phys_offset & ~TARGET_PAGE_MASK;
798 phys_offset &= ~IO_MEM_ROM;
800 if (area_flags == IO_MEM_UNASSIGNED) {
801 kvm_unregister_memory_area(kvm_context, start_addr, size);
802 return;
805 r = kvm_is_containing_region(kvm_context, start_addr, size);
806 if (r)
807 return;
809 if (area_flags >= TLB_MMIO)
810 return;
812 r = kvm_register_phys_mem(kvm_context, start_addr,
813 phys_ram_base + phys_offset,
814 size, 0);
815 if (r < 0) {
816 printf("kvm_cpu_register_physical_memory: failed\n");
817 exit(1);
819 return;
822 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
823 target_phys_addr_t size,
824 unsigned long phys_offset)
826 kvm_unregister_memory_area(kvm_context, start_addr, size);
829 int kvm_setup_guest_memory(void *area, unsigned long size)
831 int ret = 0;
833 #ifdef MADV_DONTFORK
834 if (kvm_enabled() && !kvm_has_sync_mmu(kvm_context))
835 ret = madvise(area, size, MADV_DONTFORK);
836 #endif
838 if (ret)
839 perror ("madvise");
841 return ret;
844 int kvm_qemu_check_extension(int ext)
846 return kvm_check_extension(kvm_context, ext);
849 int kvm_qemu_init_env(CPUState *cenv)
851 return kvm_arch_qemu_init_env(cenv);
854 struct kvm_guest_debug_data {
855 struct kvm_debug_guest dbg;
856 int err;
859 void kvm_invoke_guest_debug(void *data)
861 struct kvm_guest_debug_data *dbg_data = data;
863 dbg_data->err = kvm_guest_debug(kvm_context, cpu_single_env->cpu_index,
864 &dbg_data->dbg);
867 int kvm_update_debugger(CPUState *env)
869 struct kvm_guest_debug_data data;
870 int i;
872 memset(data.dbg.breakpoints, 0, sizeof(data.dbg.breakpoints));
874 data.dbg.enabled = 0;
875 if (env->nb_breakpoints || env->singlestep_enabled) {
876 data.dbg.enabled = 1;
877 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
878 data.dbg.breakpoints[i].enabled = 1;
879 data.dbg.breakpoints[i].address = env->breakpoints[i];
881 data.dbg.singlestep = env->singlestep_enabled;
883 on_vcpu(env, kvm_invoke_guest_debug, &data);
884 return data.err;
889 * dirty pages logging
891 /* FIXME: use unsigned long pointer instead of unsigned char */
892 unsigned char *kvm_dirty_bitmap = NULL;
893 int kvm_physical_memory_set_dirty_tracking(int enable)
895 int r = 0;
897 if (!kvm_enabled())
898 return 0;
900 if (enable) {
901 if (!kvm_dirty_bitmap) {
902 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
903 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
904 if (kvm_dirty_bitmap == NULL) {
905 perror("Failed to allocate dirty pages bitmap");
906 r=-1;
908 else {
909 r = kvm_dirty_pages_log_enable_all(kvm_context);
913 else {
914 if (kvm_dirty_bitmap) {
915 r = kvm_dirty_pages_log_reset(kvm_context);
916 qemu_free(kvm_dirty_bitmap);
917 kvm_dirty_bitmap = NULL;
920 return r;
923 /* get kvm's dirty pages bitmap and update qemu's */
924 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
925 unsigned char *bitmap,
926 unsigned int offset,
927 unsigned long mem_size)
929 unsigned int i, j, n=0;
930 unsigned char c;
931 unsigned page_number, addr, addr1;
932 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
935 * bitmap-traveling is faster than memory-traveling (for addr...)
936 * especially when most of the memory is not dirty.
938 for (i=0; i<len; i++) {
939 c = bitmap[i];
940 while (c>0) {
941 j = ffsl(c) - 1;
942 c &= ~(1u<<j);
943 page_number = i * 8 + j;
944 addr1 = page_number * TARGET_PAGE_SIZE;
945 addr = offset + addr1;
946 cpu_physical_memory_set_dirty(addr);
947 n++;
950 return 0;
952 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
953 void *bitmap, void *opaque)
955 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
959 * get kvm's dirty pages bitmap and update qemu's
960 * we only care about physical ram, which resides in slots 0 and 3
962 int kvm_update_dirty_pages_log(void)
964 int r = 0;
967 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
968 kvm_dirty_bitmap, NULL,
969 kvm_get_dirty_bitmap_cb);
970 return r;
973 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
974 int log)
976 if (log)
977 kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
978 else
979 kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
982 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
984 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
985 unsigned int brsize = BITMAP_SIZE(ram_size);
986 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
987 unsigned int extra_bytes = (extra_pages +7)/8;
988 unsigned int hole_start = BITMAP_SIZE(0xa0000);
989 unsigned int hole_end = BITMAP_SIZE(0xc0000);
991 memset(bitmap, 0xFF, brsize + extra_bytes);
992 memset(bitmap + hole_start, 0, hole_end - hole_start);
993 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
995 return 0;
998 #ifdef KVM_CAP_IRQCHIP
1000 int kvm_set_irq(int irq, int level)
1002 return kvm_set_irq_level(kvm_context, irq, level);
1005 #endif
1007 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1009 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1012 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1013 unsigned long size, int log, int writable)
1015 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1018 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1019 unsigned long size)
1021 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1024 void kvm_mutex_unlock(void)
1026 assert(!cpu_single_env);
1027 pthread_mutex_unlock(&qemu_mutex);
1030 void kvm_mutex_lock(void)
1032 pthread_mutex_lock(&qemu_mutex);
1033 cpu_single_env = NULL;
1036 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1038 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1041 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1042 unsigned int size)
1044 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);