Get current register values for monitor info cpu command
[qemu-kvm/fedora.git] / qemu-kvm.c
blob7c1d6ec35a322100ca4ec2ebe2ac8bb82a21b0bb
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 inline unsigned long kvm_get_thread_id(void)
76 return syscall(SYS_gettid);
79 static void qemu_cond_wait(pthread_cond_t *cond)
81 CPUState *env = cpu_single_env;
83 pthread_cond_wait(cond, &qemu_mutex);
84 cpu_single_env = env;
87 CPUState *qemu_kvm_cpu_env(int index)
89 return vcpu_info[index].env;
92 static void sig_ipi_handler(int n)
96 static void on_vcpu(CPUState *env, void (*func)(void *data), void *data)
98 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
99 struct qemu_kvm_work_item wi;
101 if (vi == vcpu) {
102 func(data);
103 return;
106 wi.func = func;
107 wi.data = data;
108 if (!vi->queued_work_first)
109 vi->queued_work_first = &wi;
110 else
111 vi->queued_work_last->next = &wi;
112 vi->queued_work_last = &wi;
113 wi.next = NULL;
114 wi.done = false;
116 pthread_kill(vi->thread, SIG_IPI);
117 while (!wi.done)
118 qemu_cond_wait(&qemu_work_cond);
121 void kvm_update_interrupt_request(CPUState *env)
123 int signal = 0;
125 if (env) {
126 if (!vcpu)
127 signal = 1;
128 if (vcpu && env != vcpu->env && !vcpu_info[env->cpu_index].signalled)
129 signal = 1;
131 if (signal) {
132 vcpu_info[env->cpu_index].signalled = 1;
133 if (vcpu_info[env->cpu_index].thread)
134 pthread_kill(vcpu_info[env->cpu_index].thread, SIG_IPI);
139 void kvm_update_after_sipi(CPUState *env)
141 vcpu_info[env->cpu_index].sipi_needed = 1;
142 kvm_update_interrupt_request(env);
145 void kvm_apic_init(CPUState *env)
147 if (env->cpu_index != 0)
148 vcpu_info[env->cpu_index].init = 1;
149 kvm_update_interrupt_request(env);
152 #include <signal.h>
154 static int try_push_interrupts(void *opaque)
156 return kvm_arch_try_push_interrupts(opaque);
159 static void post_kvm_run(void *opaque, int vcpu)
162 pthread_mutex_lock(&qemu_mutex);
163 kvm_arch_post_kvm_run(opaque, vcpu);
166 static int pre_kvm_run(void *opaque, int vcpu)
168 CPUState *env = qemu_kvm_cpu_env(vcpu);
170 kvm_arch_pre_kvm_run(opaque, vcpu);
172 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
173 return 1;
174 pthread_mutex_unlock(&qemu_mutex);
175 return 0;
178 void kvm_load_registers(CPUState *env)
180 if (kvm_enabled())
181 kvm_arch_load_regs(env);
184 void kvm_save_registers(CPUState *env)
186 if (kvm_enabled())
187 kvm_arch_save_regs(env);
190 int kvm_cpu_exec(CPUState *env)
192 int r;
194 r = kvm_run(kvm_context, env->cpu_index);
195 if (r < 0) {
196 printf("kvm_run returned %d\n", r);
197 exit(1);
200 return 0;
203 extern int vm_running;
205 static int has_work(CPUState *env)
207 if (!vm_running || (env && vcpu_info[env->cpu_index].stopped))
208 return 0;
209 if (!(env->hflags & HF_HALTED_MASK))
210 return 1;
211 return kvm_arch_has_work(env);
214 static void flush_queued_work(CPUState *env)
216 struct vcpu_info *vi = &vcpu_info[env->cpu_index];
217 struct qemu_kvm_work_item *wi;
219 if (!vi->queued_work_first)
220 return;
222 while ((wi = vi->queued_work_first)) {
223 vi->queued_work_first = wi->next;
224 wi->func(wi->data);
225 wi->done = true;
227 vi->queued_work_last = NULL;
228 pthread_cond_broadcast(&qemu_work_cond);
231 static void kvm_main_loop_wait(CPUState *env, int timeout)
233 struct timespec ts;
234 int r, e;
235 siginfo_t siginfo;
236 sigset_t waitset;
238 pthread_mutex_unlock(&qemu_mutex);
240 ts.tv_sec = timeout / 1000;
241 ts.tv_nsec = (timeout % 1000) * 1000000;
242 sigemptyset(&waitset);
243 sigaddset(&waitset, SIG_IPI);
245 r = sigtimedwait(&waitset, &siginfo, &ts);
246 e = errno;
248 pthread_mutex_lock(&qemu_mutex);
250 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
251 printf("sigtimedwait: %s\n", strerror(e));
252 exit(1);
256 flush_queued_work(env);
258 if (vcpu_info[env->cpu_index].stop) {
259 vcpu_info[env->cpu_index].stop = 0;
260 vcpu_info[env->cpu_index].stopped = 1;
261 pthread_cond_signal(&qemu_pause_cond);
263 cpu_single_env = env;
265 vcpu_info[env->cpu_index].signalled = 0;
268 static int all_threads_paused(void)
270 int i;
272 for (i = 0; i < smp_cpus; ++i)
273 if (vcpu_info[i].stop)
274 return 0;
275 return 1;
278 static void pause_all_threads(void)
280 int i;
282 assert(!cpu_single_env);
284 for (i = 0; i < smp_cpus; ++i) {
285 vcpu_info[i].stop = 1;
286 pthread_kill(vcpu_info[i].thread, SIG_IPI);
288 while (!all_threads_paused())
289 qemu_cond_wait(&qemu_pause_cond);
292 static void resume_all_threads(void)
294 int i;
296 assert(!cpu_single_env);
298 for (i = 0; i < smp_cpus; ++i) {
299 vcpu_info[i].stop = 0;
300 vcpu_info[i].stopped = 0;
301 pthread_kill(vcpu_info[i].thread, SIG_IPI);
305 static void kvm_vm_state_change_handler(void *context, int running)
307 if (running)
308 resume_all_threads();
309 else
310 pause_all_threads();
313 static void update_regs_for_sipi(CPUState *env)
315 kvm_arch_update_regs_for_sipi(env);
316 vcpu_info[env->cpu_index].sipi_needed = 0;
317 vcpu_info[env->cpu_index].init = 0;
320 static void update_regs_for_init(CPUState *env)
322 cpu_reset(env);
323 kvm_arch_load_regs(env);
326 static void setup_kernel_sigmask(CPUState *env)
328 sigset_t set;
330 sigemptyset(&set);
331 sigaddset(&set, SIGUSR2);
332 sigaddset(&set, SIGIO);
333 sigaddset(&set, SIGALRM);
334 sigprocmask(SIG_BLOCK, &set, NULL);
336 sigprocmask(SIG_BLOCK, NULL, &set);
337 sigdelset(&set, SIG_IPI);
339 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
342 void qemu_kvm_system_reset(void)
344 int i;
346 pause_all_threads();
348 qemu_system_reset();
350 for (i = 0; i < smp_cpus; ++i)
351 kvm_arch_cpu_reset(vcpu_info[i].env);
353 resume_all_threads();
356 static int kvm_main_loop_cpu(CPUState *env)
358 struct vcpu_info *info = &vcpu_info[env->cpu_index];
360 setup_kernel_sigmask(env);
362 pthread_mutex_lock(&qemu_mutex);
363 if (kvm_irqchip_in_kernel(kvm_context))
364 env->hflags &= ~HF_HALTED_MASK;
366 kvm_qemu_init_env(env);
367 env->ready_for_interrupt_injection = 1;
368 #ifdef TARGET_I386
369 kvm_tpr_vcpu_start(env);
370 #endif
372 cpu_single_env = env;
373 while (1) {
374 while (!has_work(env))
375 kvm_main_loop_wait(env, 1000);
376 if (env->interrupt_request & CPU_INTERRUPT_HARD)
377 env->hflags &= ~HF_HALTED_MASK;
378 if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
379 update_regs_for_sipi(env);
380 if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
381 update_regs_for_init(env);
382 if (!(env->hflags & HF_HALTED_MASK) && !info->init)
383 kvm_cpu_exec(env);
384 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
385 kvm_main_loop_wait(env, 0);
387 pthread_mutex_unlock(&qemu_mutex);
388 return 0;
391 static void *ap_main_loop(void *_env)
393 CPUState *env = _env;
394 sigset_t signals;
396 vcpu = &vcpu_info[env->cpu_index];
397 vcpu->env = env;
398 vcpu->env->thread_id = kvm_get_thread_id();
399 sigfillset(&signals);
400 sigprocmask(SIG_BLOCK, &signals, NULL);
401 kvm_create_vcpu(kvm_context, env->cpu_index);
402 kvm_qemu_init_env(env);
404 /* signal VCPU creation */
405 pthread_mutex_lock(&qemu_mutex);
406 vcpu->created = 1;
407 pthread_cond_signal(&qemu_vcpu_cond);
409 /* and wait for machine initialization */
410 while (!qemu_system_ready)
411 qemu_cond_wait(&qemu_system_cond);
412 pthread_mutex_unlock(&qemu_mutex);
414 kvm_main_loop_cpu(env);
415 return NULL;
418 void kvm_init_new_ap(int cpu, CPUState *env)
420 pthread_create(&vcpu_info[cpu].thread, NULL, ap_main_loop, env);
422 while (vcpu_info[cpu].created == 0)
423 qemu_cond_wait(&qemu_vcpu_cond);
426 int kvm_init_ap(void)
428 #ifdef TARGET_I386
429 kvm_tpr_opt_setup();
430 #endif
431 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
433 signal(SIG_IPI, sig_ipi_handler);
434 return 0;
437 void qemu_kvm_notify_work(void)
439 uint64_t value = 1;
440 char buffer[8];
441 size_t offset = 0;
443 if (io_thread_fd == -1)
444 return;
446 memcpy(buffer, &value, sizeof(value));
448 while (offset < 8) {
449 ssize_t len;
451 len = write(io_thread_fd, buffer + offset, 8 - offset);
452 if (len == -1 && errno == EINTR)
453 continue;
455 if (len <= 0)
456 break;
458 offset += len;
461 if (offset != 8)
462 fprintf(stderr, "failed to notify io thread\n");
465 /* If we have signalfd, we mask out the signals we want to handle and then
466 * use signalfd to listen for them. We rely on whatever the current signal
467 * handler is to dispatch the signals when we receive them.
470 static void sigfd_handler(void *opaque)
472 int fd = (unsigned long)opaque;
473 struct signalfd_siginfo info;
474 struct sigaction action;
475 ssize_t len;
477 while (1) {
478 do {
479 len = read(fd, &info, sizeof(info));
480 } while (len == -1 && errno == EINTR);
482 if (len == -1 && errno == EAGAIN)
483 break;
485 if (len != sizeof(info)) {
486 printf("read from sigfd returned %ld: %m\n", len);
487 return;
490 sigaction(info.ssi_signo, NULL, &action);
491 if (action.sa_handler)
492 action.sa_handler(info.ssi_signo);
494 if (info.ssi_signo == SIGUSR2) {
495 pthread_cond_signal(&qemu_aio_cond);
500 /* Used to break IO thread out of select */
501 static void io_thread_wakeup(void *opaque)
503 int fd = (unsigned long)opaque;
504 char buffer[8];
505 size_t offset = 0;
507 while (offset < 8) {
508 ssize_t len;
510 len = read(fd, buffer + offset, 8 - offset);
511 if (len == -1 && errno == EINTR)
512 continue;
514 if (len <= 0)
515 break;
517 offset += len;
521 int kvm_main_loop(void)
523 int fds[2];
524 sigset_t mask;
525 int sigfd;
527 io_thread = pthread_self();
528 qemu_system_ready = 1;
530 if (kvm_eventfd(fds) == -1) {
531 fprintf(stderr, "failed to create eventfd\n");
532 return -errno;
535 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
536 (void *)(unsigned long)fds[0]);
538 io_thread_fd = fds[1];
540 sigemptyset(&mask);
541 sigaddset(&mask, SIGIO);
542 sigaddset(&mask, SIGALRM);
543 sigaddset(&mask, SIGUSR2);
544 sigprocmask(SIG_BLOCK, &mask, NULL);
546 sigfd = kvm_signalfd(&mask);
547 if (sigfd == -1) {
548 fprintf(stderr, "failed to create signalfd\n");
549 return -errno;
552 fcntl(sigfd, F_SETFL, O_NONBLOCK);
554 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
555 (void *)(unsigned long)sigfd);
557 pthread_cond_broadcast(&qemu_system_cond);
559 io_thread_sigfd = sigfd;
560 cpu_single_env = NULL;
562 while (1) {
563 main_loop_wait(1000);
564 if (qemu_shutdown_requested())
565 break;
566 else if (qemu_powerdown_requested())
567 qemu_system_powerdown();
568 else if (qemu_reset_requested())
569 qemu_kvm_system_reset();
572 pause_all_threads();
573 pthread_mutex_unlock(&qemu_mutex);
575 return 0;
578 static int kvm_debug(void *opaque, int vcpu)
580 CPUState *env = cpu_single_env;
582 env->exception_index = EXCP_DEBUG;
583 return 1;
586 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
588 *data = cpu_inb(0, addr);
589 return 0;
592 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
594 *data = cpu_inw(0, addr);
595 return 0;
598 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
600 *data = cpu_inl(0, addr);
601 return 0;
604 #define PM_IO_BASE 0xb000
606 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
608 if (addr == 0xb2) {
609 switch (data) {
610 case 0: {
611 cpu_outb(0, 0xb3, 0);
612 break;
614 case 0xf0: {
615 unsigned x;
617 /* enable acpi */
618 x = cpu_inw(0, PM_IO_BASE + 4);
619 x &= ~1;
620 cpu_outw(0, PM_IO_BASE + 4, x);
621 break;
623 case 0xf1: {
624 unsigned x;
626 /* enable acpi */
627 x = cpu_inw(0, PM_IO_BASE + 4);
628 x |= 1;
629 cpu_outw(0, PM_IO_BASE + 4, x);
630 break;
632 default:
633 break;
635 return 0;
637 cpu_outb(0, addr, data);
638 return 0;
641 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
643 cpu_outw(0, addr, data);
644 return 0;
647 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
649 cpu_outl(0, addr, data);
650 return 0;
653 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
655 cpu_physical_memory_rw(addr, data, len, 0);
656 return 0;
659 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
661 cpu_physical_memory_rw(addr, data, len, 1);
662 return 0;
665 static int kvm_io_window(void *opaque)
667 return 1;
671 static int kvm_halt(void *opaque, int vcpu)
673 return kvm_arch_halt(opaque, vcpu);
676 static int kvm_shutdown(void *opaque, int vcpu)
678 /* stop the current vcpu from going back to guest mode */
679 vcpu_info[cpu_single_env->cpu_index].stopped = 1;
681 qemu_system_reset_request();
682 return 1;
685 static struct kvm_callbacks qemu_kvm_ops = {
686 .debug = kvm_debug,
687 .inb = kvm_inb,
688 .inw = kvm_inw,
689 .inl = kvm_inl,
690 .outb = kvm_outb,
691 .outw = kvm_outw,
692 .outl = kvm_outl,
693 .mmio_read = kvm_mmio_read,
694 .mmio_write = kvm_mmio_write,
695 .halt = kvm_halt,
696 .shutdown = kvm_shutdown,
697 .io_window = kvm_io_window,
698 .try_push_interrupts = try_push_interrupts,
699 .post_kvm_run = post_kvm_run,
700 .pre_kvm_run = pre_kvm_run,
701 #ifdef TARGET_I386
702 .tpr_access = handle_tpr_access,
703 #endif
704 #ifdef TARGET_PPC
705 .powerpc_dcr_read = handle_powerpc_dcr_read,
706 .powerpc_dcr_write = handle_powerpc_dcr_write,
707 #endif
710 int kvm_qemu_init()
712 /* Try to initialize kvm */
713 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
714 if (!kvm_context) {
715 return -1;
717 pthread_mutex_lock(&qemu_mutex);
719 return 0;
722 int kvm_qemu_create_context(void)
724 int r;
725 if (!kvm_irqchip) {
726 kvm_disable_irqchip_creation(kvm_context);
728 if (!kvm_pit) {
729 kvm_disable_pit_creation(kvm_context);
731 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
732 kvm_qemu_destroy();
733 return -1;
735 r = kvm_arch_qemu_create_context();
736 if(r <0)
737 kvm_qemu_destroy();
738 return 0;
741 void kvm_qemu_destroy(void)
743 kvm_finalize(kvm_context);
746 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
747 unsigned long size,
748 unsigned long phys_offset)
750 #ifdef KVM_CAP_USER_MEMORY
751 int r = 0;
753 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
754 if (r) {
755 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
756 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
757 if (r)
758 return;
759 r = kvm_is_intersecting_mem(kvm_context, start_addr);
760 if (r)
761 kvm_create_mem_hole(kvm_context, start_addr, size);
762 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
763 phys_ram_base + phys_offset,
764 size, 0);
766 if (phys_offset & IO_MEM_ROM) {
767 phys_offset &= ~IO_MEM_ROM;
768 r = kvm_is_intersecting_mem(kvm_context, start_addr);
769 if (r)
770 kvm_create_mem_hole(kvm_context, start_addr, size);
771 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
772 phys_ram_base + phys_offset,
773 size, 0);
775 if (r < 0) {
776 printf("kvm_cpu_register_physical_memory: failed\n");
777 exit(1);
779 return;
781 #endif
782 if (phys_offset & IO_MEM_ROM) {
783 phys_offset &= ~IO_MEM_ROM;
784 memcpy(phys_ram_base + start_addr, phys_ram_base + phys_offset, size);
788 int kvm_qemu_check_extension(int ext)
790 return kvm_check_extension(kvm_context, ext);
793 int kvm_qemu_init_env(CPUState *cenv)
795 return kvm_arch_qemu_init_env(cenv);
798 int kvm_update_debugger(CPUState *env)
800 struct kvm_debug_guest dbg;
801 int i;
803 memset(dbg.breakpoints, 0, sizeof(dbg.breakpoints));
805 dbg.enabled = 0;
806 if (env->nb_breakpoints || env->singlestep_enabled) {
807 dbg.enabled = 1;
808 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
809 dbg.breakpoints[i].enabled = 1;
810 dbg.breakpoints[i].address = env->breakpoints[i];
812 dbg.singlestep = env->singlestep_enabled;
814 return kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
819 * dirty pages logging
821 /* FIXME: use unsigned long pointer instead of unsigned char */
822 unsigned char *kvm_dirty_bitmap = NULL;
823 int kvm_physical_memory_set_dirty_tracking(int enable)
825 int r = 0;
827 if (!kvm_enabled())
828 return 0;
830 if (enable) {
831 if (!kvm_dirty_bitmap) {
832 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
833 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
834 if (kvm_dirty_bitmap == NULL) {
835 perror("Failed to allocate dirty pages bitmap");
836 r=-1;
838 else {
839 r = kvm_dirty_pages_log_enable_all(kvm_context);
843 else {
844 if (kvm_dirty_bitmap) {
845 r = kvm_dirty_pages_log_reset(kvm_context);
846 qemu_free(kvm_dirty_bitmap);
847 kvm_dirty_bitmap = NULL;
850 return r;
853 /* get kvm's dirty pages bitmap and update qemu's */
854 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
855 unsigned char *bitmap,
856 unsigned int offset,
857 unsigned long mem_size)
859 unsigned int i, j, n=0;
860 unsigned char c;
861 unsigned page_number, addr, addr1;
862 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
865 * bitmap-traveling is faster than memory-traveling (for addr...)
866 * especially when most of the memory is not dirty.
868 for (i=0; i<len; i++) {
869 c = bitmap[i];
870 while (c>0) {
871 j = ffsl(c) - 1;
872 c &= ~(1u<<j);
873 page_number = i * 8 + j;
874 addr1 = page_number * TARGET_PAGE_SIZE;
875 addr = offset + addr1;
876 cpu_physical_memory_set_dirty(addr);
877 n++;
880 return 0;
882 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
883 void *bitmap, void *opaque)
885 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
889 * get kvm's dirty pages bitmap and update qemu's
890 * we only care about physical ram, which resides in slots 0 and 3
892 int kvm_update_dirty_pages_log(void)
894 int r = 0;
897 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
898 kvm_dirty_bitmap, NULL,
899 kvm_get_dirty_bitmap_cb);
900 return r;
903 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
905 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
906 unsigned int brsize = BITMAP_SIZE(ram_size);
907 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
908 unsigned int extra_bytes = (extra_pages +7)/8;
909 unsigned int hole_start = BITMAP_SIZE(0xa0000);
910 unsigned int hole_end = BITMAP_SIZE(0xc0000);
912 memset(bitmap, 0xFF, brsize + extra_bytes);
913 memset(bitmap + hole_start, 0, hole_end - hole_start);
914 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
916 return 0;
919 #ifdef KVM_CAP_IRQCHIP
921 int kvm_set_irq(int irq, int level)
923 return kvm_set_irq_level(kvm_context, irq, level);
926 #endif
928 void qemu_kvm_aio_wait_start(void)
932 void qemu_kvm_aio_wait(void)
934 if (!cpu_single_env) {
935 if (io_thread_sigfd != -1) {
936 fd_set rfds;
937 int ret;
939 FD_ZERO(&rfds);
940 FD_SET(io_thread_sigfd, &rfds);
942 /* this is a rare case where we do want to hold qemu_mutex
943 * while sleeping. We cannot allow anything else to run
944 * right now. */
945 ret = select(io_thread_sigfd + 1, &rfds, NULL, NULL, NULL);
946 if (ret > 0 && FD_ISSET(io_thread_sigfd, &rfds))
947 sigfd_handler((void *)(unsigned long)io_thread_sigfd);
949 qemu_aio_poll();
950 } else
951 qemu_cond_wait(&qemu_aio_cond);
954 void qemu_kvm_aio_wait_end(void)
958 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
960 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
963 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
964 unsigned long size, int log, int writable)
966 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
969 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
970 unsigned long size)
972 kvm_destroy_phys_mem(kvm_context, start_addr, size);
975 void kvm_mutex_unlock(void)
977 assert(!cpu_single_env);
978 pthread_mutex_unlock(&qemu_mutex);
981 void kvm_mutex_lock(void)
983 pthread_mutex_lock(&qemu_mutex);
984 cpu_single_env = NULL;