kvm: external module: DEBUGCTLMSR compatibility
[qemu-kvm/fedora.git] / qemu-kvm.c
blob1d07650ce81ec79ed04009078600dfa2e75ca9f1
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;
84 static const struct timespec ts = {
85 .tv_sec = 0,
86 .tv_nsec = 100000,
89 pthread_cond_timedwait(cond, &qemu_mutex, &ts);
90 /* If we're the I/O thread, some other thread may be waiting for aio
91 * completion */
92 if (!vcpu)
93 qemu_aio_poll();
94 cpu_single_env = env;
97 CPUState *qemu_kvm_cpu_env(int index)
99 return vcpu_info[index].env;
102 static void sig_ipi_handler(int n)
106 static void on_vcpu(CPUState *env, void (*func)(void *data), void *data)
108 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
109 struct qemu_kvm_work_item wi;
111 if (vi == vcpu) {
112 func(data);
113 return;
116 wi.func = func;
117 wi.data = data;
118 if (!vi->queued_work_first)
119 vi->queued_work_first = &wi;
120 else
121 vi->queued_work_last->next = &wi;
122 vi->queued_work_last = &wi;
123 wi.next = NULL;
124 wi.done = false;
126 pthread_kill(vi->thread, SIG_IPI);
127 while (!wi.done)
128 qemu_cond_wait(&qemu_work_cond);
131 void kvm_update_interrupt_request(CPUState *env)
133 int signal = 0;
135 if (env) {
136 if (!vcpu)
137 signal = 1;
138 if (vcpu && env != vcpu->env && !vcpu_info[env->cpu_index].signalled)
139 signal = 1;
141 if (signal) {
142 vcpu_info[env->cpu_index].signalled = 1;
143 if (vcpu_info[env->cpu_index].thread)
144 pthread_kill(vcpu_info[env->cpu_index].thread, SIG_IPI);
149 void kvm_update_after_sipi(CPUState *env)
151 vcpu_info[env->cpu_index].sipi_needed = 1;
152 kvm_update_interrupt_request(env);
155 void kvm_apic_init(CPUState *env)
157 if (env->cpu_index != 0)
158 vcpu_info[env->cpu_index].init = 1;
159 kvm_update_interrupt_request(env);
162 #include <signal.h>
164 static int try_push_interrupts(void *opaque)
166 return kvm_arch_try_push_interrupts(opaque);
169 static void post_kvm_run(void *opaque, int vcpu)
172 pthread_mutex_lock(&qemu_mutex);
173 kvm_arch_post_kvm_run(opaque, vcpu);
176 static int pre_kvm_run(void *opaque, int vcpu)
178 CPUState *env = qemu_kvm_cpu_env(vcpu);
180 kvm_arch_pre_kvm_run(opaque, vcpu);
182 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
183 return 1;
184 pthread_mutex_unlock(&qemu_mutex);
185 return 0;
188 static void kvm_do_load_registers(void *_env)
190 CPUState *env = _env;
192 kvm_arch_load_regs(env);
195 void kvm_load_registers(CPUState *env)
197 if (kvm_enabled() && qemu_system_ready)
198 on_vcpu(env, kvm_do_load_registers, env);
201 static void kvm_do_save_registers(void *_env)
203 CPUState *env = _env;
205 kvm_arch_save_regs(env);
208 void kvm_save_registers(CPUState *env)
210 if (kvm_enabled())
211 on_vcpu(env, kvm_do_save_registers, env);
214 int kvm_cpu_exec(CPUState *env)
216 int r;
218 r = kvm_run(kvm_context, env->cpu_index);
219 if (r < 0) {
220 printf("kvm_run returned %d\n", r);
221 exit(1);
224 return 0;
227 extern int vm_running;
229 static int has_work(CPUState *env)
231 if (!vm_running || (env && vcpu_info[env->cpu_index].stopped))
232 return 0;
233 if (!env->halted)
234 return 1;
235 return kvm_arch_has_work(env);
238 static void flush_queued_work(CPUState *env)
240 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
241 struct qemu_kvm_work_item *wi;
243 if (!vi->queued_work_first)
244 return;
246 while ((wi = vi->queued_work_first)) {
247 vi->queued_work_first = wi->next;
248 wi->func(wi->data);
249 wi->done = true;
251 vi->queued_work_last = NULL;
252 pthread_cond_broadcast(&qemu_work_cond);
255 static void kvm_main_loop_wait(CPUState *env, int timeout)
257 struct timespec ts;
258 int r, e;
259 siginfo_t siginfo;
260 sigset_t waitset;
262 pthread_mutex_unlock(&qemu_mutex);
264 ts.tv_sec = timeout / 1000;
265 ts.tv_nsec = (timeout % 1000) * 1000000;
266 sigemptyset(&waitset);
267 sigaddset(&waitset, SIG_IPI);
269 r = sigtimedwait(&waitset, &siginfo, &ts);
270 e = errno;
272 pthread_mutex_lock(&qemu_mutex);
274 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
275 printf("sigtimedwait: %s\n", strerror(e));
276 exit(1);
279 cpu_single_env = env;
280 flush_queued_work(env);
282 if (vcpu_info[env->cpu_index].stop) {
283 vcpu_info[env->cpu_index].stop = 0;
284 vcpu_info[env->cpu_index].stopped = 1;
285 pthread_cond_signal(&qemu_pause_cond);
288 vcpu_info[env->cpu_index].signalled = 0;
291 static int all_threads_paused(void)
293 int i;
295 for (i = 0; i < smp_cpus; ++i)
296 if (vcpu_info[i].stop)
297 return 0;
298 return 1;
301 static void pause_all_threads(void)
303 int i;
305 assert(!cpu_single_env);
307 for (i = 0; i < smp_cpus; ++i) {
308 vcpu_info[i].stop = 1;
309 pthread_kill(vcpu_info[i].thread, SIG_IPI);
311 while (!all_threads_paused())
312 qemu_cond_wait(&qemu_pause_cond);
315 static void resume_all_threads(void)
317 int i;
319 assert(!cpu_single_env);
321 for (i = 0; i < smp_cpus; ++i) {
322 vcpu_info[i].stop = 0;
323 vcpu_info[i].stopped = 0;
324 pthread_kill(vcpu_info[i].thread, SIG_IPI);
328 static void kvm_vm_state_change_handler(void *context, int running)
330 if (running)
331 resume_all_threads();
332 else
333 pause_all_threads();
336 static void update_regs_for_sipi(CPUState *env)
338 kvm_arch_update_regs_for_sipi(env);
339 vcpu_info[env->cpu_index].sipi_needed = 0;
340 vcpu_info[env->cpu_index].init = 0;
343 static void update_regs_for_init(CPUState *env)
345 cpu_reset(env);
346 kvm_arch_load_regs(env);
349 static void setup_kernel_sigmask(CPUState *env)
351 sigset_t set;
353 sigemptyset(&set);
354 sigaddset(&set, SIGUSR2);
355 sigaddset(&set, SIGIO);
356 sigaddset(&set, SIGALRM);
357 sigprocmask(SIG_BLOCK, &set, NULL);
359 sigprocmask(SIG_BLOCK, NULL, &set);
360 sigdelset(&set, SIG_IPI);
362 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
365 void qemu_kvm_system_reset(void)
367 int i;
369 pause_all_threads();
371 qemu_system_reset();
373 for (i = 0; i < smp_cpus; ++i)
374 kvm_arch_cpu_reset(vcpu_info[i].env);
376 resume_all_threads();
379 static int kvm_main_loop_cpu(CPUState *env)
381 struct vcpu_info *info = &vcpu_info[env->cpu_index];
383 setup_kernel_sigmask(env);
385 pthread_mutex_lock(&qemu_mutex);
386 if (kvm_irqchip_in_kernel(kvm_context))
387 env->halted = 0;
389 kvm_qemu_init_env(env);
390 #ifdef TARGET_I386
391 kvm_tpr_vcpu_start(env);
392 #endif
394 cpu_single_env = env;
395 kvm_load_registers(env);
397 while (1) {
398 while (!has_work(env))
399 kvm_main_loop_wait(env, 1000);
400 if (env->interrupt_request & CPU_INTERRUPT_HARD)
401 env->halted = 0;
402 if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
403 update_regs_for_sipi(env);
404 if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
405 update_regs_for_init(env);
406 if (!env->halted && !info->init)
407 kvm_cpu_exec(env);
408 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
409 kvm_main_loop_wait(env, 0);
411 pthread_mutex_unlock(&qemu_mutex);
412 return 0;
415 static void *ap_main_loop(void *_env)
417 CPUState *env = _env;
418 sigset_t signals;
420 vcpu = &vcpu_info[env->cpu_index];
421 vcpu->env = env;
422 vcpu->env->thread_id = kvm_get_thread_id();
423 sigfillset(&signals);
424 sigprocmask(SIG_BLOCK, &signals, NULL);
425 kvm_create_vcpu(kvm_context, env->cpu_index);
426 kvm_qemu_init_env(env);
428 /* signal VCPU creation */
429 pthread_mutex_lock(&qemu_mutex);
430 vcpu->created = 1;
431 pthread_cond_signal(&qemu_vcpu_cond);
433 /* and wait for machine initialization */
434 while (!qemu_system_ready)
435 qemu_cond_wait(&qemu_system_cond);
436 pthread_mutex_unlock(&qemu_mutex);
438 kvm_main_loop_cpu(env);
439 return NULL;
442 void kvm_init_new_ap(int cpu, CPUState *env)
444 pthread_create(&vcpu_info[cpu].thread, NULL, ap_main_loop, env);
446 while (vcpu_info[cpu].created == 0)
447 qemu_cond_wait(&qemu_vcpu_cond);
450 int kvm_init_ap(void)
452 #ifdef TARGET_I386
453 kvm_tpr_opt_setup();
454 #endif
455 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
457 signal(SIG_IPI, sig_ipi_handler);
458 return 0;
461 void qemu_kvm_notify_work(void)
463 uint64_t value = 1;
464 char buffer[8];
465 size_t offset = 0;
467 if (io_thread_fd == -1)
468 return;
470 memcpy(buffer, &value, sizeof(value));
472 while (offset < 8) {
473 ssize_t len;
475 len = write(io_thread_fd, buffer + offset, 8 - offset);
476 if (len == -1 && errno == EINTR)
477 continue;
479 if (len <= 0)
480 break;
482 offset += len;
485 if (offset != 8)
486 fprintf(stderr, "failed to notify io thread\n");
489 /* If we have signalfd, we mask out the signals we want to handle and then
490 * use signalfd to listen for them. We rely on whatever the current signal
491 * handler is to dispatch the signals when we receive them.
494 static void sigfd_handler(void *opaque)
496 int fd = (unsigned long)opaque;
497 struct signalfd_siginfo info;
498 struct sigaction action;
499 ssize_t len;
501 while (1) {
502 do {
503 len = read(fd, &info, sizeof(info));
504 } while (len == -1 && errno == EINTR);
506 if (len == -1 && errno == EAGAIN)
507 break;
509 if (len != sizeof(info)) {
510 printf("read from sigfd returned %ld: %m\n", len);
511 return;
514 sigaction(info.ssi_signo, NULL, &action);
515 if (action.sa_handler)
516 action.sa_handler(info.ssi_signo);
518 if (info.ssi_signo == SIGUSR2) {
519 pthread_cond_signal(&qemu_aio_cond);
524 /* Used to break IO thread out of select */
525 static void io_thread_wakeup(void *opaque)
527 int fd = (unsigned long)opaque;
528 char buffer[8];
529 size_t offset = 0;
531 while (offset < 8) {
532 ssize_t len;
534 len = read(fd, buffer + offset, 8 - offset);
535 if (len == -1 && errno == EINTR)
536 continue;
538 if (len <= 0)
539 break;
541 offset += len;
545 int kvm_main_loop(void)
547 int fds[2];
548 sigset_t mask;
549 int sigfd;
551 io_thread = pthread_self();
552 qemu_system_ready = 1;
554 if (kvm_eventfd(fds) == -1) {
555 fprintf(stderr, "failed to create eventfd\n");
556 return -errno;
559 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
560 (void *)(unsigned long)fds[0]);
562 io_thread_fd = fds[1];
564 sigemptyset(&mask);
565 sigaddset(&mask, SIGIO);
566 sigaddset(&mask, SIGALRM);
567 sigaddset(&mask, SIGUSR2);
568 sigprocmask(SIG_BLOCK, &mask, NULL);
570 sigfd = kvm_signalfd(&mask);
571 if (sigfd == -1) {
572 fprintf(stderr, "failed to create signalfd\n");
573 return -errno;
576 fcntl(sigfd, F_SETFL, O_NONBLOCK);
578 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
579 (void *)(unsigned long)sigfd);
581 pthread_cond_broadcast(&qemu_system_cond);
583 io_thread_sigfd = sigfd;
584 cpu_single_env = NULL;
586 while (1) {
587 main_loop_wait(1000);
588 if (qemu_shutdown_requested())
589 break;
590 else if (qemu_powerdown_requested())
591 qemu_system_powerdown();
592 else if (qemu_reset_requested())
593 qemu_kvm_system_reset();
594 else if (kvm_debug_stop_requested) {
595 vm_stop(EXCP_DEBUG);
596 kvm_debug_stop_requested = 0;
600 pause_all_threads();
601 pthread_mutex_unlock(&qemu_mutex);
603 return 0;
606 static int kvm_debug(void *opaque, int vcpu)
608 CPUState *env = cpu_single_env;
610 kvm_debug_stop_requested = 1;
611 vcpu_info[vcpu].stopped = 1;
612 return 1;
615 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
617 *data = cpu_inb(0, addr);
618 return 0;
621 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
623 *data = cpu_inw(0, addr);
624 return 0;
627 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
629 *data = cpu_inl(0, addr);
630 return 0;
633 #define PM_IO_BASE 0xb000
635 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
637 if (addr == 0xb2) {
638 switch (data) {
639 case 0: {
640 cpu_outb(0, 0xb3, 0);
641 break;
643 case 0xf0: {
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 case 0xf1: {
653 unsigned x;
655 /* enable acpi */
656 x = cpu_inw(0, PM_IO_BASE + 4);
657 x |= 1;
658 cpu_outw(0, PM_IO_BASE + 4, x);
659 break;
661 default:
662 break;
664 return 0;
666 cpu_outb(0, addr, data);
667 return 0;
670 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
672 cpu_outw(0, addr, data);
673 return 0;
676 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
678 cpu_outl(0, addr, data);
679 return 0;
682 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
684 cpu_physical_memory_rw(addr, data, len, 0);
685 return 0;
688 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
690 cpu_physical_memory_rw(addr, data, len, 1);
691 return 0;
694 static int kvm_io_window(void *opaque)
696 return 1;
700 static int kvm_halt(void *opaque, int vcpu)
702 return kvm_arch_halt(opaque, vcpu);
705 static int kvm_shutdown(void *opaque, int vcpu)
707 /* stop the current vcpu from going back to guest mode */
708 vcpu_info[cpu_single_env->cpu_index].stopped = 1;
710 qemu_system_reset_request();
711 return 1;
714 static struct kvm_callbacks qemu_kvm_ops = {
715 .debug = kvm_debug,
716 .inb = kvm_inb,
717 .inw = kvm_inw,
718 .inl = kvm_inl,
719 .outb = kvm_outb,
720 .outw = kvm_outw,
721 .outl = kvm_outl,
722 .mmio_read = kvm_mmio_read,
723 .mmio_write = kvm_mmio_write,
724 .halt = kvm_halt,
725 .shutdown = kvm_shutdown,
726 .io_window = kvm_io_window,
727 .try_push_interrupts = try_push_interrupts,
728 .post_kvm_run = post_kvm_run,
729 .pre_kvm_run = pre_kvm_run,
730 #ifdef TARGET_I386
731 .tpr_access = handle_tpr_access,
732 #endif
733 #ifdef TARGET_PPC
734 .powerpc_dcr_read = handle_powerpc_dcr_read,
735 .powerpc_dcr_write = handle_powerpc_dcr_write,
736 #endif
739 int kvm_qemu_init()
741 /* Try to initialize kvm */
742 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
743 if (!kvm_context) {
744 return -1;
746 pthread_mutex_lock(&qemu_mutex);
748 return 0;
751 int kvm_qemu_create_context(void)
753 int r;
754 if (!kvm_irqchip) {
755 kvm_disable_irqchip_creation(kvm_context);
757 if (!kvm_pit) {
758 kvm_disable_pit_creation(kvm_context);
760 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
761 kvm_qemu_destroy();
762 return -1;
764 r = kvm_arch_qemu_create_context();
765 if(r <0)
766 kvm_qemu_destroy();
767 return 0;
770 void kvm_qemu_destroy(void)
772 kvm_finalize(kvm_context);
775 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
776 unsigned long size,
777 unsigned long phys_offset)
779 #ifdef KVM_CAP_USER_MEMORY
780 int r = 0;
782 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
783 if (r) {
784 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
785 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
786 if (r)
787 return;
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 (phys_offset & IO_MEM_ROM) {
796 phys_offset &= ~IO_MEM_ROM;
797 r = kvm_is_intersecting_mem(kvm_context, start_addr);
798 if (r)
799 kvm_create_mem_hole(kvm_context, start_addr, size);
800 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
801 phys_ram_base + phys_offset,
802 size, 0);
804 if (r < 0) {
805 printf("kvm_cpu_register_physical_memory: failed\n");
806 exit(1);
808 return;
810 #endif
811 if (phys_offset & IO_MEM_ROM) {
812 phys_offset &= ~IO_MEM_ROM;
813 memcpy(phys_ram_base + start_addr, phys_ram_base + phys_offset, size);
817 int kvm_qemu_check_extension(int ext)
819 return kvm_check_extension(kvm_context, ext);
822 int kvm_qemu_init_env(CPUState *cenv)
824 return kvm_arch_qemu_init_env(cenv);
827 struct kvm_guest_debug_data {
828 struct kvm_debug_guest dbg;
829 int err;
832 void kvm_invoke_guest_debug(void *data)
834 struct kvm_guest_debug_data *dbg_data = data;
836 dbg_data->err = kvm_guest_debug(kvm_context, cpu_single_env->cpu_index,
837 &dbg_data->dbg);
840 int kvm_update_debugger(CPUState *env)
842 struct kvm_guest_debug_data data;
843 int i;
845 memset(data.dbg.breakpoints, 0, sizeof(data.dbg.breakpoints));
847 data.dbg.enabled = 0;
848 if (env->nb_breakpoints || env->singlestep_enabled) {
849 data.dbg.enabled = 1;
850 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
851 data.dbg.breakpoints[i].enabled = 1;
852 data.dbg.breakpoints[i].address = env->breakpoints[i];
854 data.dbg.singlestep = env->singlestep_enabled;
856 on_vcpu(env, kvm_invoke_guest_debug, &data);
857 return data.err;
862 * dirty pages logging
864 /* FIXME: use unsigned long pointer instead of unsigned char */
865 unsigned char *kvm_dirty_bitmap = NULL;
866 int kvm_physical_memory_set_dirty_tracking(int enable)
868 int r = 0;
870 if (!kvm_enabled())
871 return 0;
873 if (enable) {
874 if (!kvm_dirty_bitmap) {
875 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
876 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
877 if (kvm_dirty_bitmap == NULL) {
878 perror("Failed to allocate dirty pages bitmap");
879 r=-1;
881 else {
882 r = kvm_dirty_pages_log_enable_all(kvm_context);
886 else {
887 if (kvm_dirty_bitmap) {
888 r = kvm_dirty_pages_log_reset(kvm_context);
889 qemu_free(kvm_dirty_bitmap);
890 kvm_dirty_bitmap = NULL;
893 return r;
896 /* get kvm's dirty pages bitmap and update qemu's */
897 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
898 unsigned char *bitmap,
899 unsigned int offset,
900 unsigned long mem_size)
902 unsigned int i, j, n=0;
903 unsigned char c;
904 unsigned page_number, addr, addr1;
905 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
908 * bitmap-traveling is faster than memory-traveling (for addr...)
909 * especially when most of the memory is not dirty.
911 for (i=0; i<len; i++) {
912 c = bitmap[i];
913 while (c>0) {
914 j = ffsl(c) - 1;
915 c &= ~(1u<<j);
916 page_number = i * 8 + j;
917 addr1 = page_number * TARGET_PAGE_SIZE;
918 addr = offset + addr1;
919 cpu_physical_memory_set_dirty(addr);
920 n++;
923 return 0;
925 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
926 void *bitmap, void *opaque)
928 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
932 * get kvm's dirty pages bitmap and update qemu's
933 * we only care about physical ram, which resides in slots 0 and 3
935 int kvm_update_dirty_pages_log(void)
937 int r = 0;
940 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
941 kvm_dirty_bitmap, NULL,
942 kvm_get_dirty_bitmap_cb);
943 return r;
946 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
948 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
949 unsigned int brsize = BITMAP_SIZE(ram_size);
950 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
951 unsigned int extra_bytes = (extra_pages +7)/8;
952 unsigned int hole_start = BITMAP_SIZE(0xa0000);
953 unsigned int hole_end = BITMAP_SIZE(0xc0000);
955 memset(bitmap, 0xFF, brsize + extra_bytes);
956 memset(bitmap + hole_start, 0, hole_end - hole_start);
957 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
959 return 0;
962 #ifdef KVM_CAP_IRQCHIP
964 int kvm_set_irq(int irq, int level)
966 return kvm_set_irq_level(kvm_context, irq, level);
969 #endif
971 void qemu_kvm_aio_wait_start(void)
975 void qemu_kvm_aio_wait(void)
977 if (!cpu_single_env) {
978 if (io_thread_sigfd != -1) {
979 fd_set rfds;
980 int ret;
982 FD_ZERO(&rfds);
983 FD_SET(io_thread_sigfd, &rfds);
985 /* this is a rare case where we do want to hold qemu_mutex
986 * while sleeping. We cannot allow anything else to run
987 * right now. */
988 ret = select(io_thread_sigfd + 1, &rfds, NULL, NULL, NULL);
989 if (ret > 0 && FD_ISSET(io_thread_sigfd, &rfds))
990 sigfd_handler((void *)(unsigned long)io_thread_sigfd);
992 qemu_aio_poll();
993 } else
994 qemu_cond_wait(&qemu_aio_cond);
997 void qemu_kvm_aio_wait_end(void)
1001 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1003 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1006 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1007 unsigned long size, int log, int writable)
1009 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1012 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1013 unsigned long size)
1015 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1018 void kvm_mutex_unlock(void)
1020 assert(!cpu_single_env);
1021 pthread_mutex_unlock(&qemu_mutex);
1024 void kvm_mutex_lock(void)
1026 pthread_mutex_lock(&qemu_mutex);
1027 cpu_single_env = NULL;
1030 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1032 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1035 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1036 unsigned int size)
1038 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);