Coalesced MMIO support (core)
[qemu-kvm/markmc.git] / qemu-kvm.c
blob837228ef5304e6548f472226c7fcbc04798b2480
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"
23 #include "qemu-kvm.h"
24 #include <libkvm.h>
25 #include <pthread.h>
26 #include <sys/utsname.h>
27 #include <sys/syscall.h>
29 #define bool _Bool
30 #define false 0
31 #define true 1
33 extern void perror(const char *s);
35 kvm_context_t kvm_context;
37 extern int smp_cpus;
39 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
40 pthread_cond_t qemu_aio_cond = PTHREAD_COND_INITIALIZER;
41 pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
42 pthread_cond_t qemu_system_cond = PTHREAD_COND_INITIALIZER;
43 pthread_cond_t qemu_pause_cond = PTHREAD_COND_INITIALIZER;
44 pthread_cond_t qemu_work_cond = PTHREAD_COND_INITIALIZER;
45 __thread struct vcpu_info *vcpu;
47 static int qemu_system_ready;
49 #define SIG_IPI (SIGRTMIN+4)
51 struct qemu_kvm_work_item {
52 struct qemu_kvm_work_item *next;
53 void (*func)(void *data);
54 void *data;
55 bool done;
58 struct vcpu_info {
59 CPUState *env;
60 int sipi_needed;
61 int init;
62 pthread_t thread;
63 int signalled;
64 int stop;
65 int stopped;
66 int created;
67 struct qemu_kvm_work_item *queued_work_first, *queued_work_last;
68 } vcpu_info[256];
70 pthread_t io_thread;
71 static int io_thread_fd = -1;
72 static int io_thread_sigfd = -1;
74 static int kvm_debug_stop_requested;
76 static inline unsigned long kvm_get_thread_id(void)
78 return syscall(SYS_gettid);
81 static void qemu_cond_wait(pthread_cond_t *cond)
83 CPUState *env = cpu_single_env;
85 pthread_cond_wait(cond, &qemu_mutex);
86 cpu_single_env = env;
89 CPUState *qemu_kvm_cpu_env(int index)
91 return vcpu_info[index].env;
94 static void sig_ipi_handler(int n)
98 static void on_vcpu(CPUState *env, void (*func)(void *data), void *data)
100 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
101 struct qemu_kvm_work_item wi;
103 if (vi == vcpu) {
104 func(data);
105 return;
108 wi.func = func;
109 wi.data = data;
110 if (!vi->queued_work_first)
111 vi->queued_work_first = &wi;
112 else
113 vi->queued_work_last->next = &wi;
114 vi->queued_work_last = &wi;
115 wi.next = NULL;
116 wi.done = false;
118 pthread_kill(vi->thread, SIG_IPI);
119 while (!wi.done)
120 qemu_cond_wait(&qemu_work_cond);
123 void kvm_update_interrupt_request(CPUState *env)
125 int signal = 0;
127 if (env) {
128 if (!vcpu)
129 signal = 1;
130 if (vcpu && env != vcpu->env && !vcpu_info[env->cpu_index].signalled)
131 signal = 1;
133 if (signal) {
134 vcpu_info[env->cpu_index].signalled = 1;
135 if (vcpu_info[env->cpu_index].thread)
136 pthread_kill(vcpu_info[env->cpu_index].thread, SIG_IPI);
141 void kvm_update_after_sipi(CPUState *env)
143 vcpu_info[env->cpu_index].sipi_needed = 1;
144 kvm_update_interrupt_request(env);
147 void kvm_apic_init(CPUState *env)
149 if (env->cpu_index != 0)
150 vcpu_info[env->cpu_index].init = 1;
151 kvm_update_interrupt_request(env);
154 #include <signal.h>
156 static int try_push_interrupts(void *opaque)
158 return kvm_arch_try_push_interrupts(opaque);
161 static void post_kvm_run(void *opaque, int vcpu)
164 pthread_mutex_lock(&qemu_mutex);
165 kvm_arch_post_kvm_run(opaque, vcpu);
168 static int pre_kvm_run(void *opaque, int vcpu)
170 CPUState *env = qemu_kvm_cpu_env(vcpu);
172 kvm_arch_pre_kvm_run(opaque, vcpu);
174 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
175 return 1;
176 pthread_mutex_unlock(&qemu_mutex);
177 return 0;
180 static void kvm_do_load_registers(void *_env)
182 CPUState *env = _env;
184 kvm_arch_load_regs(env);
187 void kvm_load_registers(CPUState *env)
189 if (kvm_enabled())
190 on_vcpu(env, kvm_do_load_registers, env);
193 static void kvm_do_save_registers(void *_env)
195 CPUState *env = _env;
197 kvm_arch_save_regs(env);
200 void kvm_save_registers(CPUState *env)
202 if (kvm_enabled())
203 on_vcpu(env, kvm_do_save_registers, env);
206 int kvm_cpu_exec(CPUState *env)
208 int r;
210 r = kvm_run(kvm_context, env->cpu_index);
211 if (r < 0) {
212 printf("kvm_run returned %d\n", r);
213 exit(1);
216 return 0;
219 extern int vm_running;
221 static int has_work(CPUState *env)
223 if (!vm_running || (env && vcpu_info[env->cpu_index].stopped))
224 return 0;
225 if (!(env->hflags & HF_HALTED_MASK))
226 return 1;
227 return kvm_arch_has_work(env);
230 static void flush_queued_work(CPUState *env)
232 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
233 struct qemu_kvm_work_item *wi;
235 if (!vi->queued_work_first)
236 return;
238 while ((wi = vi->queued_work_first)) {
239 vi->queued_work_first = wi->next;
240 wi->func(wi->data);
241 wi->done = true;
243 vi->queued_work_last = NULL;
244 pthread_cond_broadcast(&qemu_work_cond);
247 static void kvm_main_loop_wait(CPUState *env, int timeout)
249 struct timespec ts;
250 int r, e;
251 siginfo_t siginfo;
252 sigset_t waitset;
254 pthread_mutex_unlock(&qemu_mutex);
256 ts.tv_sec = timeout / 1000;
257 ts.tv_nsec = (timeout % 1000) * 1000000;
258 sigemptyset(&waitset);
259 sigaddset(&waitset, SIG_IPI);
261 r = sigtimedwait(&waitset, &siginfo, &ts);
262 e = errno;
264 pthread_mutex_lock(&qemu_mutex);
266 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
267 printf("sigtimedwait: %s\n", strerror(e));
268 exit(1);
271 cpu_single_env = env;
272 flush_queued_work(env);
274 if (vcpu_info[env->cpu_index].stop) {
275 vcpu_info[env->cpu_index].stop = 0;
276 vcpu_info[env->cpu_index].stopped = 1;
277 pthread_cond_signal(&qemu_pause_cond);
280 vcpu_info[env->cpu_index].signalled = 0;
283 static int all_threads_paused(void)
285 int i;
287 for (i = 0; i < smp_cpus; ++i)
288 if (vcpu_info[i].stop)
289 return 0;
290 return 1;
293 static void pause_all_threads(void)
295 int i;
297 assert(!cpu_single_env);
299 for (i = 0; i < smp_cpus; ++i) {
300 vcpu_info[i].stop = 1;
301 pthread_kill(vcpu_info[i].thread, SIG_IPI);
303 while (!all_threads_paused())
304 qemu_cond_wait(&qemu_pause_cond);
307 static void resume_all_threads(void)
309 int i;
311 assert(!cpu_single_env);
313 for (i = 0; i < smp_cpus; ++i) {
314 vcpu_info[i].stop = 0;
315 vcpu_info[i].stopped = 0;
316 pthread_kill(vcpu_info[i].thread, SIG_IPI);
320 static void kvm_vm_state_change_handler(void *context, int running)
322 if (running)
323 resume_all_threads();
324 else
325 pause_all_threads();
328 static void update_regs_for_sipi(CPUState *env)
330 kvm_arch_update_regs_for_sipi(env);
331 vcpu_info[env->cpu_index].sipi_needed = 0;
332 vcpu_info[env->cpu_index].init = 0;
335 static void update_regs_for_init(CPUState *env)
337 cpu_reset(env);
338 kvm_arch_load_regs(env);
341 static void setup_kernel_sigmask(CPUState *env)
343 sigset_t set;
345 sigemptyset(&set);
346 sigaddset(&set, SIGUSR2);
347 sigaddset(&set, SIGIO);
348 sigaddset(&set, SIGALRM);
349 sigprocmask(SIG_BLOCK, &set, NULL);
351 sigprocmask(SIG_BLOCK, NULL, &set);
352 sigdelset(&set, SIG_IPI);
354 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
357 void qemu_kvm_system_reset(void)
359 int i;
361 pause_all_threads();
363 qemu_system_reset();
365 for (i = 0; i < smp_cpus; ++i)
366 kvm_arch_cpu_reset(vcpu_info[i].env);
368 resume_all_threads();
371 static int kvm_main_loop_cpu(CPUState *env)
373 struct vcpu_info *info = &vcpu_info[env->cpu_index];
375 setup_kernel_sigmask(env);
377 pthread_mutex_lock(&qemu_mutex);
378 if (kvm_irqchip_in_kernel(kvm_context))
379 env->hflags &= ~HF_HALTED_MASK;
381 kvm_qemu_init_env(env);
382 env->ready_for_interrupt_injection = 1;
383 #ifdef TARGET_I386
384 kvm_tpr_vcpu_start(env);
385 #endif
387 cpu_single_env = env;
388 while (1) {
389 while (!has_work(env))
390 kvm_main_loop_wait(env, 1000);
391 if (env->interrupt_request & CPU_INTERRUPT_HARD)
392 env->hflags &= ~HF_HALTED_MASK;
393 if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
394 update_regs_for_sipi(env);
395 if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
396 update_regs_for_init(env);
397 if (!(env->hflags & HF_HALTED_MASK) && !info->init)
398 kvm_cpu_exec(env);
399 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
400 kvm_main_loop_wait(env, 0);
402 pthread_mutex_unlock(&qemu_mutex);
403 return 0;
406 static void *ap_main_loop(void *_env)
408 CPUState *env = _env;
409 sigset_t signals;
411 vcpu = &vcpu_info[env->cpu_index];
412 vcpu->env = env;
413 vcpu->env->thread_id = kvm_get_thread_id();
414 sigfillset(&signals);
415 sigprocmask(SIG_BLOCK, &signals, NULL);
416 kvm_create_vcpu(kvm_context, env->cpu_index);
417 kvm_qemu_init_env(env);
419 /* signal VCPU creation */
420 pthread_mutex_lock(&qemu_mutex);
421 vcpu->created = 1;
422 pthread_cond_signal(&qemu_vcpu_cond);
424 /* and wait for machine initialization */
425 while (!qemu_system_ready)
426 qemu_cond_wait(&qemu_system_cond);
427 pthread_mutex_unlock(&qemu_mutex);
429 kvm_main_loop_cpu(env);
430 return NULL;
433 void kvm_init_new_ap(int cpu, CPUState *env)
435 pthread_create(&vcpu_info[cpu].thread, NULL, ap_main_loop, env);
437 while (vcpu_info[cpu].created == 0)
438 qemu_cond_wait(&qemu_vcpu_cond);
441 int kvm_init_ap(void)
443 #ifdef TARGET_I386
444 kvm_tpr_opt_setup();
445 #endif
446 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
448 signal(SIG_IPI, sig_ipi_handler);
449 return 0;
452 void qemu_kvm_notify_work(void)
454 uint64_t value = 1;
455 char buffer[8];
456 size_t offset = 0;
458 if (io_thread_fd == -1)
459 return;
461 memcpy(buffer, &value, sizeof(value));
463 while (offset < 8) {
464 ssize_t len;
466 len = write(io_thread_fd, buffer + offset, 8 - offset);
467 if (len == -1 && errno == EINTR)
468 continue;
470 if (len <= 0)
471 break;
473 offset += len;
476 if (offset != 8)
477 fprintf(stderr, "failed to notify io thread\n");
480 /* If we have signalfd, we mask out the signals we want to handle and then
481 * use signalfd to listen for them. We rely on whatever the current signal
482 * handler is to dispatch the signals when we receive them.
485 static void sigfd_handler(void *opaque)
487 int fd = (unsigned long)opaque;
488 struct signalfd_siginfo info;
489 struct sigaction action;
490 ssize_t len;
492 while (1) {
493 do {
494 len = read(fd, &info, sizeof(info));
495 } while (len == -1 && errno == EINTR);
497 if (len == -1 && errno == EAGAIN)
498 break;
500 if (len != sizeof(info)) {
501 printf("read from sigfd returned %ld: %m\n", len);
502 return;
505 sigaction(info.ssi_signo, NULL, &action);
506 if (action.sa_handler)
507 action.sa_handler(info.ssi_signo);
509 if (info.ssi_signo == SIGUSR2) {
510 pthread_cond_signal(&qemu_aio_cond);
515 /* Used to break IO thread out of select */
516 static void io_thread_wakeup(void *opaque)
518 int fd = (unsigned long)opaque;
519 char buffer[8];
520 size_t offset = 0;
522 while (offset < 8) {
523 ssize_t len;
525 len = read(fd, buffer + offset, 8 - offset);
526 if (len == -1 && errno == EINTR)
527 continue;
529 if (len <= 0)
530 break;
532 offset += len;
536 int kvm_main_loop(void)
538 int fds[2];
539 sigset_t mask;
540 int sigfd;
542 io_thread = pthread_self();
543 qemu_system_ready = 1;
545 if (kvm_eventfd(fds) == -1) {
546 fprintf(stderr, "failed to create eventfd\n");
547 return -errno;
550 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
551 (void *)(unsigned long)fds[0]);
553 io_thread_fd = fds[1];
555 sigemptyset(&mask);
556 sigaddset(&mask, SIGIO);
557 sigaddset(&mask, SIGALRM);
558 sigaddset(&mask, SIGUSR2);
559 sigprocmask(SIG_BLOCK, &mask, NULL);
561 sigfd = kvm_signalfd(&mask);
562 if (sigfd == -1) {
563 fprintf(stderr, "failed to create signalfd\n");
564 return -errno;
567 fcntl(sigfd, F_SETFL, O_NONBLOCK);
569 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
570 (void *)(unsigned long)sigfd);
572 pthread_cond_broadcast(&qemu_system_cond);
574 io_thread_sigfd = sigfd;
575 cpu_single_env = NULL;
577 while (1) {
578 main_loop_wait(1000);
579 if (qemu_shutdown_requested())
580 break;
581 else if (qemu_powerdown_requested())
582 qemu_system_powerdown();
583 else if (qemu_reset_requested())
584 qemu_kvm_system_reset();
585 else if (kvm_debug_stop_requested) {
586 vm_stop(EXCP_DEBUG);
587 kvm_debug_stop_requested = 0;
591 pause_all_threads();
592 pthread_mutex_unlock(&qemu_mutex);
594 return 0;
597 static int kvm_debug(void *opaque, int vcpu)
599 CPUState *env = cpu_single_env;
601 kvm_debug_stop_requested = 1;
602 vcpu_info[vcpu].stopped = 1;
603 return 1;
606 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
608 *data = cpu_inb(0, addr);
609 return 0;
612 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
614 *data = cpu_inw(0, addr);
615 return 0;
618 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
620 *data = cpu_inl(0, addr);
621 return 0;
624 #define PM_IO_BASE 0xb000
626 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
628 if (addr == 0xb2) {
629 switch (data) {
630 case 0: {
631 cpu_outb(0, 0xb3, 0);
632 break;
634 case 0xf0: {
635 unsigned x;
637 /* enable acpi */
638 x = cpu_inw(0, PM_IO_BASE + 4);
639 x &= ~1;
640 cpu_outw(0, PM_IO_BASE + 4, x);
641 break;
643 case 0xf1: {
644 unsigned x;
646 /* enable acpi */
647 x = cpu_inw(0, PM_IO_BASE + 4);
648 x |= 1;
649 cpu_outw(0, PM_IO_BASE + 4, x);
650 break;
652 default:
653 break;
655 return 0;
657 cpu_outb(0, addr, data);
658 return 0;
661 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
663 cpu_outw(0, addr, data);
664 return 0;
667 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
669 cpu_outl(0, addr, data);
670 return 0;
673 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
675 cpu_physical_memory_rw(addr, data, len, 0);
676 return 0;
679 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
681 cpu_physical_memory_rw(addr, data, len, 1);
682 return 0;
685 static int kvm_io_window(void *opaque)
687 return 1;
691 static int kvm_halt(void *opaque, int vcpu)
693 return kvm_arch_halt(opaque, vcpu);
696 static int kvm_shutdown(void *opaque, int vcpu)
698 /* stop the current vcpu from going back to guest mode */
699 vcpu_info[cpu_single_env->cpu_index].stopped = 1;
701 qemu_system_reset_request();
702 return 1;
705 static struct kvm_callbacks qemu_kvm_ops = {
706 .debug = kvm_debug,
707 .inb = kvm_inb,
708 .inw = kvm_inw,
709 .inl = kvm_inl,
710 .outb = kvm_outb,
711 .outw = kvm_outw,
712 .outl = kvm_outl,
713 .mmio_read = kvm_mmio_read,
714 .mmio_write = kvm_mmio_write,
715 .halt = kvm_halt,
716 .shutdown = kvm_shutdown,
717 .io_window = kvm_io_window,
718 .try_push_interrupts = try_push_interrupts,
719 .post_kvm_run = post_kvm_run,
720 .pre_kvm_run = pre_kvm_run,
721 #ifdef TARGET_I386
722 .tpr_access = handle_tpr_access,
723 #endif
724 #ifdef TARGET_PPC
725 .powerpc_dcr_read = handle_powerpc_dcr_read,
726 .powerpc_dcr_write = handle_powerpc_dcr_write,
727 #endif
730 int kvm_qemu_init()
732 /* Try to initialize kvm */
733 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
734 if (!kvm_context) {
735 return -1;
737 pthread_mutex_lock(&qemu_mutex);
739 return 0;
742 int kvm_qemu_create_context(void)
744 int r;
745 if (!kvm_irqchip) {
746 kvm_disable_irqchip_creation(kvm_context);
748 if (!kvm_pit) {
749 kvm_disable_pit_creation(kvm_context);
751 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
752 kvm_qemu_destroy();
753 return -1;
755 r = kvm_arch_qemu_create_context();
756 if(r <0)
757 kvm_qemu_destroy();
758 return 0;
761 void kvm_qemu_destroy(void)
763 kvm_finalize(kvm_context);
766 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
767 unsigned long size,
768 unsigned long phys_offset)
770 #ifdef KVM_CAP_USER_MEMORY
771 int r = 0;
773 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
774 if (r) {
775 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
776 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
777 if (r)
778 return;
779 r = kvm_is_intersecting_mem(kvm_context, start_addr);
780 if (r)
781 kvm_create_mem_hole(kvm_context, start_addr, size);
782 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
783 phys_ram_base + phys_offset,
784 size, 0);
786 if (phys_offset & IO_MEM_ROM) {
787 phys_offset &= ~IO_MEM_ROM;
788 r = kvm_is_intersecting_mem(kvm_context, start_addr);
789 if (r)
790 kvm_create_mem_hole(kvm_context, start_addr, size);
791 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
792 phys_ram_base + phys_offset,
793 size, 0);
795 if (r < 0) {
796 printf("kvm_cpu_register_physical_memory: failed\n");
797 exit(1);
799 return;
801 #endif
802 if (phys_offset & IO_MEM_ROM) {
803 phys_offset &= ~IO_MEM_ROM;
804 memcpy(phys_ram_base + start_addr, phys_ram_base + phys_offset, size);
808 int kvm_qemu_check_extension(int ext)
810 return kvm_check_extension(kvm_context, ext);
813 int kvm_qemu_init_env(CPUState *cenv)
815 return kvm_arch_qemu_init_env(cenv);
818 struct kvm_guest_debug_data {
819 struct kvm_debug_guest dbg;
820 int err;
823 void kvm_invoke_guest_debug(void *data)
825 struct kvm_guest_debug_data *dbg_data = data;
827 dbg_data->err = kvm_guest_debug(kvm_context, cpu_single_env->cpu_index,
828 &dbg_data->dbg);
831 int kvm_update_debugger(CPUState *env)
833 struct kvm_guest_debug_data data;
834 int i;
836 memset(data.dbg.breakpoints, 0, sizeof(data.dbg.breakpoints));
838 data.dbg.enabled = 0;
839 if (env->nb_breakpoints || env->singlestep_enabled) {
840 data.dbg.enabled = 1;
841 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
842 data.dbg.breakpoints[i].enabled = 1;
843 data.dbg.breakpoints[i].address = env->breakpoints[i];
845 data.dbg.singlestep = env->singlestep_enabled;
847 on_vcpu(env, kvm_invoke_guest_debug, &data);
848 return data.err;
853 * dirty pages logging
855 /* FIXME: use unsigned long pointer instead of unsigned char */
856 unsigned char *kvm_dirty_bitmap = NULL;
857 int kvm_physical_memory_set_dirty_tracking(int enable)
859 int r = 0;
861 if (!kvm_enabled())
862 return 0;
864 if (enable) {
865 if (!kvm_dirty_bitmap) {
866 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
867 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
868 if (kvm_dirty_bitmap == NULL) {
869 perror("Failed to allocate dirty pages bitmap");
870 r=-1;
872 else {
873 r = kvm_dirty_pages_log_enable_all(kvm_context);
877 else {
878 if (kvm_dirty_bitmap) {
879 r = kvm_dirty_pages_log_reset(kvm_context);
880 qemu_free(kvm_dirty_bitmap);
881 kvm_dirty_bitmap = NULL;
884 return r;
887 /* get kvm's dirty pages bitmap and update qemu's */
888 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
889 unsigned char *bitmap,
890 unsigned int offset,
891 unsigned long mem_size)
893 unsigned int i, j, n=0;
894 unsigned char c;
895 unsigned page_number, addr, addr1;
896 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
899 * bitmap-traveling is faster than memory-traveling (for addr...)
900 * especially when most of the memory is not dirty.
902 for (i=0; i<len; i++) {
903 c = bitmap[i];
904 while (c>0) {
905 j = ffsl(c) - 1;
906 c &= ~(1u<<j);
907 page_number = i * 8 + j;
908 addr1 = page_number * TARGET_PAGE_SIZE;
909 addr = offset + addr1;
910 cpu_physical_memory_set_dirty(addr);
911 n++;
914 return 0;
916 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
917 void *bitmap, void *opaque)
919 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
923 * get kvm's dirty pages bitmap and update qemu's
924 * we only care about physical ram, which resides in slots 0 and 3
926 int kvm_update_dirty_pages_log(void)
928 int r = 0;
931 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
932 kvm_dirty_bitmap, NULL,
933 kvm_get_dirty_bitmap_cb);
934 return r;
937 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
939 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
940 unsigned int brsize = BITMAP_SIZE(ram_size);
941 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
942 unsigned int extra_bytes = (extra_pages +7)/8;
943 unsigned int hole_start = BITMAP_SIZE(0xa0000);
944 unsigned int hole_end = BITMAP_SIZE(0xc0000);
946 memset(bitmap, 0xFF, brsize + extra_bytes);
947 memset(bitmap + hole_start, 0, hole_end - hole_start);
948 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
950 return 0;
953 #ifdef KVM_CAP_IRQCHIP
955 int kvm_set_irq(int irq, int level)
957 return kvm_set_irq_level(kvm_context, irq, level);
960 #endif
962 void qemu_kvm_aio_wait_start(void)
966 void qemu_kvm_aio_wait(void)
968 if (!cpu_single_env) {
969 if (io_thread_sigfd != -1) {
970 fd_set rfds;
971 int ret;
973 FD_ZERO(&rfds);
974 FD_SET(io_thread_sigfd, &rfds);
976 /* this is a rare case where we do want to hold qemu_mutex
977 * while sleeping. We cannot allow anything else to run
978 * right now. */
979 ret = select(io_thread_sigfd + 1, &rfds, NULL, NULL, NULL);
980 if (ret > 0 && FD_ISSET(io_thread_sigfd, &rfds))
981 sigfd_handler((void *)(unsigned long)io_thread_sigfd);
983 qemu_aio_poll();
984 } else
985 qemu_cond_wait(&qemu_aio_cond);
988 void qemu_kvm_aio_wait_end(void)
992 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
994 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
997 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
998 unsigned long size, int log, int writable)
1000 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1003 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1004 unsigned long size)
1006 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1009 void kvm_mutex_unlock(void)
1011 assert(!cpu_single_env);
1012 pthread_mutex_unlock(&qemu_mutex);
1015 void kvm_mutex_lock(void)
1017 pthread_mutex_lock(&qemu_mutex);
1018 cpu_single_env = NULL;
1021 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1023 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1026 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1027 unsigned int size)
1029 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);