kvm: testsuite: test simultaneous irq injection
[qemu-kvm/fedora.git] / qemu-kvm.c
blob45b70e8d017d870d6c2737d8bfec259da72b0455
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 false 0
32 #define true 1
34 extern void perror(const char *s);
36 kvm_context_t kvm_context;
38 extern int smp_cpus;
40 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_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 CPUState *current_env;
47 static int qemu_system_ready;
49 #define SIG_IPI (SIGRTMIN+4)
51 pthread_t io_thread;
52 static int io_thread_fd = -1;
53 static int io_thread_sigfd = -1;
55 static int kvm_debug_stop_requested;
57 /* The list of ioperm_data */
58 static LIST_HEAD(, ioperm_data) ioperm_head;
60 static inline unsigned long kvm_get_thread_id(void)
62 return syscall(SYS_gettid);
65 static void qemu_cond_wait(pthread_cond_t *cond)
67 CPUState *env = cpu_single_env;
68 static const struct timespec ts = {
69 .tv_sec = 0,
70 .tv_nsec = 100000,
73 pthread_cond_timedwait(cond, &qemu_mutex, &ts);
74 cpu_single_env = env;
77 static void sig_ipi_handler(int n)
81 static void on_vcpu(CPUState *env, void (*func)(void *data), void *data)
83 struct qemu_work_item wi;
85 if (env == current_env) {
86 func(data);
87 return;
90 wi.func = func;
91 wi.data = data;
92 if (!env->kvm_cpu_state.queued_work_first)
93 env->kvm_cpu_state.queued_work_first = &wi;
94 else
95 env->kvm_cpu_state.queued_work_last->next = &wi;
96 env->kvm_cpu_state.queued_work_last = &wi;
97 wi.next = NULL;
98 wi.done = false;
100 pthread_kill(env->kvm_cpu_state.thread, SIG_IPI);
101 while (!wi.done)
102 qemu_cond_wait(&qemu_work_cond);
105 static void inject_interrupt(void *data)
107 cpu_interrupt(current_env, (int)data);
110 void kvm_inject_interrupt(CPUState *env, int mask)
112 on_vcpu(env, inject_interrupt, (void *)mask);
115 void kvm_update_interrupt_request(CPUState *env)
117 int signal = 0;
119 if (env) {
120 if (current_env && !current_env->kvm_cpu_state.created)
121 signal = 1;
123 * Testing for created here is really redundant
125 if (current_env && current_env->kvm_cpu_state.created &&
126 env != current_env && !env->kvm_cpu_state.signalled)
127 signal = 1;
129 if (signal) {
130 env->kvm_cpu_state.signalled = 1;
131 if (env->kvm_cpu_state.thread)
132 pthread_kill(env->kvm_cpu_state.thread, SIG_IPI);
137 void kvm_update_after_sipi(CPUState *env)
139 env->kvm_cpu_state.sipi_needed = 1;
140 kvm_update_interrupt_request(env);
143 void kvm_apic_init(CPUState *env)
145 if (env->cpu_index != 0)
146 env->kvm_cpu_state.init = 1;
147 kvm_update_interrupt_request(env);
150 #include <signal.h>
152 static int try_push_interrupts(void *opaque)
154 return kvm_arch_try_push_interrupts(opaque);
157 static int try_push_nmi(void *opaque)
159 return kvm_arch_try_push_nmi(opaque);
162 static void post_kvm_run(void *opaque, void *data)
164 CPUState *env = (CPUState *)data;
166 pthread_mutex_lock(&qemu_mutex);
167 kvm_arch_post_kvm_run(opaque, env);
170 static int pre_kvm_run(void *opaque, void *data)
172 CPUState *env = (CPUState *)data;
174 kvm_arch_pre_kvm_run(opaque, env);
176 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
177 return 1;
178 pthread_mutex_unlock(&qemu_mutex);
179 return 0;
182 static void kvm_do_load_registers(void *_env)
184 CPUState *env = _env;
186 kvm_arch_load_regs(env);
189 void kvm_load_registers(CPUState *env)
191 if (kvm_enabled() && qemu_system_ready)
192 on_vcpu(env, kvm_do_load_registers, env);
195 static void kvm_do_save_registers(void *_env)
197 CPUState *env = _env;
199 kvm_arch_save_regs(env);
202 void kvm_save_registers(CPUState *env)
204 if (kvm_enabled())
205 on_vcpu(env, kvm_do_save_registers, env);
208 int kvm_cpu_exec(CPUState *env)
210 int r;
212 r = kvm_run(kvm_context, env->cpu_index, env);
213 if (r < 0) {
214 printf("kvm_run returned %d\n", r);
215 exit(1);
218 return 0;
221 extern int vm_running;
223 static int has_work(CPUState *env)
225 if (!vm_running || (env && env->kvm_cpu_state.stopped))
226 return 0;
227 if (!env->halted)
228 return 1;
229 return kvm_arch_has_work(env);
232 static void flush_queued_work(CPUState *env)
234 struct qemu_work_item *wi;
236 if (!env->kvm_cpu_state.queued_work_first)
237 return;
239 while ((wi = env->kvm_cpu_state.queued_work_first)) {
240 env->kvm_cpu_state.queued_work_first = wi->next;
241 wi->func(wi->data);
242 wi->done = true;
244 env->kvm_cpu_state.queued_work_last = NULL;
245 pthread_cond_broadcast(&qemu_work_cond);
248 static void kvm_main_loop_wait(CPUState *env, int timeout)
250 struct timespec ts;
251 int r, e;
252 siginfo_t siginfo;
253 sigset_t waitset;
255 pthread_mutex_unlock(&qemu_mutex);
257 ts.tv_sec = timeout / 1000;
258 ts.tv_nsec = (timeout % 1000) * 1000000;
259 sigemptyset(&waitset);
260 sigaddset(&waitset, SIG_IPI);
262 r = sigtimedwait(&waitset, &siginfo, &ts);
263 e = errno;
265 pthread_mutex_lock(&qemu_mutex);
267 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
268 printf("sigtimedwait: %s\n", strerror(e));
269 exit(1);
272 cpu_single_env = env;
273 flush_queued_work(env);
275 if (env->kvm_cpu_state.stop) {
276 env->kvm_cpu_state.stop = 0;
277 env->kvm_cpu_state.stopped = 1;
278 pthread_cond_signal(&qemu_pause_cond);
281 env->kvm_cpu_state.signalled = 0;
284 static int all_threads_paused(void)
286 CPUState *penv = first_cpu;
288 while (penv) {
289 if (penv->kvm_cpu_state.stop)
290 return 0;
291 penv = (CPUState *)penv->next_cpu;
294 return 1;
297 static void pause_all_threads(void)
299 CPUState *penv = first_cpu;
301 assert(!cpu_single_env);
303 while (penv) {
304 penv->kvm_cpu_state.stop = 1;
305 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
306 penv = (CPUState *)penv->next_cpu;
309 while (!all_threads_paused())
310 qemu_cond_wait(&qemu_pause_cond);
313 static void resume_all_threads(void)
315 CPUState *penv = first_cpu;
317 assert(!cpu_single_env);
319 while (penv) {
320 penv->kvm_cpu_state.stop = 0;
321 penv->kvm_cpu_state.stopped = 0;
322 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
323 penv = (CPUState *)penv->next_cpu;
327 static void kvm_vm_state_change_handler(void *context, int running)
329 if (running)
330 resume_all_threads();
331 else
332 pause_all_threads();
335 static void update_regs_for_sipi(CPUState *env)
337 kvm_arch_update_regs_for_sipi(env);
338 env->kvm_cpu_state.sipi_needed = 0;
341 static void update_regs_for_init(CPUState *env)
343 #ifdef TARGET_I386
344 SegmentCache cs = env->segs[R_CS];
345 #endif
347 cpu_reset(env);
349 #ifdef TARGET_I386
350 /* restore SIPI vector */
351 if(env->kvm_cpu_state.sipi_needed)
352 env->segs[R_CS] = cs;
353 #endif
355 env->kvm_cpu_state.init = 0;
356 kvm_arch_load_regs(env);
359 static void setup_kernel_sigmask(CPUState *env)
361 sigset_t set;
363 sigemptyset(&set);
364 sigaddset(&set, SIGUSR2);
365 sigaddset(&set, SIGIO);
366 sigaddset(&set, SIGALRM);
367 sigprocmask(SIG_BLOCK, &set, NULL);
369 sigprocmask(SIG_BLOCK, NULL, &set);
370 sigdelset(&set, SIG_IPI);
372 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
375 void qemu_kvm_system_reset(void)
377 CPUState *penv = first_cpu;
379 pause_all_threads();
381 qemu_system_reset();
383 while (penv) {
384 kvm_arch_cpu_reset(penv);
385 penv = (CPUState *)penv->next_cpu;
388 resume_all_threads();
391 static int kvm_main_loop_cpu(CPUState *env)
393 setup_kernel_sigmask(env);
395 pthread_mutex_lock(&qemu_mutex);
396 if (kvm_irqchip_in_kernel(kvm_context))
397 env->halted = 0;
399 kvm_qemu_init_env(env);
400 #ifdef TARGET_I386
401 kvm_tpr_vcpu_start(env);
402 #endif
404 cpu_single_env = env;
405 kvm_load_registers(env);
407 while (1) {
408 while (!has_work(env))
409 kvm_main_loop_wait(env, 1000);
410 if (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI))
411 env->halted = 0;
412 if (!kvm_irqchip_in_kernel(kvm_context)) {
413 if (env->kvm_cpu_state.init)
414 update_regs_for_init(env);
415 if (env->kvm_cpu_state.sipi_needed)
416 update_regs_for_sipi(env);
418 if (!env->halted && !env->kvm_cpu_state.init)
419 kvm_cpu_exec(env);
420 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
421 kvm_main_loop_wait(env, 0);
423 pthread_mutex_unlock(&qemu_mutex);
424 return 0;
427 static void *ap_main_loop(void *_env)
429 CPUState *env = _env;
430 sigset_t signals;
431 struct ioperm_data *data = NULL;
433 current_env = env;
434 env->thread_id = kvm_get_thread_id();
435 sigfillset(&signals);
436 sigprocmask(SIG_BLOCK, &signals, NULL);
437 kvm_create_vcpu(kvm_context, env->cpu_index);
438 kvm_qemu_init_env(env);
440 #ifdef USE_KVM_DEVICE_ASSIGNMENT
441 /* do ioperm for io ports of assigned devices */
442 LIST_FOREACH(data, &ioperm_head, entries)
443 on_vcpu(env, kvm_arch_do_ioperm, data);
444 #endif
446 /* signal VCPU creation */
447 pthread_mutex_lock(&qemu_mutex);
448 current_env->kvm_cpu_state.created = 1;
449 pthread_cond_signal(&qemu_vcpu_cond);
451 /* and wait for machine initialization */
452 while (!qemu_system_ready)
453 qemu_cond_wait(&qemu_system_cond);
454 pthread_mutex_unlock(&qemu_mutex);
456 kvm_main_loop_cpu(env);
457 return NULL;
460 void kvm_init_vcpu(CPUState *env)
462 int cpu = env->cpu_index;
463 pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
465 while (env->kvm_cpu_state.created == 0)
466 qemu_cond_wait(&qemu_vcpu_cond);
469 int kvm_init_ap(void)
471 #ifdef TARGET_I386
472 kvm_tpr_opt_setup();
473 #endif
474 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
476 signal(SIG_IPI, sig_ipi_handler);
477 return 0;
480 void qemu_kvm_notify_work(void)
482 uint64_t value = 1;
483 char buffer[8];
484 size_t offset = 0;
486 if (io_thread_fd == -1)
487 return;
489 memcpy(buffer, &value, sizeof(value));
491 while (offset < 8) {
492 ssize_t len;
494 len = write(io_thread_fd, buffer + offset, 8 - offset);
495 if (len == -1 && errno == EINTR)
496 continue;
498 if (len <= 0)
499 break;
501 offset += len;
504 if (offset != 8)
505 fprintf(stderr, "failed to notify io thread\n");
508 /* If we have signalfd, we mask out the signals we want to handle and then
509 * use signalfd to listen for them. We rely on whatever the current signal
510 * handler is to dispatch the signals when we receive them.
513 static void sigfd_handler(void *opaque)
515 int fd = (unsigned long)opaque;
516 struct qemu_signalfd_siginfo info;
517 struct sigaction action;
518 ssize_t len;
520 while (1) {
521 do {
522 len = read(fd, &info, sizeof(info));
523 } while (len == -1 && errno == EINTR);
525 if (len == -1 && errno == EAGAIN)
526 break;
528 if (len != sizeof(info)) {
529 printf("read from sigfd returned %ld: %m\n", len);
530 return;
533 sigaction(info.ssi_signo, NULL, &action);
534 if (action.sa_handler)
535 action.sa_handler(info.ssi_signo);
540 /* Used to break IO thread out of select */
541 static void io_thread_wakeup(void *opaque)
543 int fd = (unsigned long)opaque;
544 char buffer[8];
545 size_t offset = 0;
547 while (offset < 8) {
548 ssize_t len;
550 len = read(fd, buffer + offset, 8 - offset);
551 if (len == -1 && errno == EINTR)
552 continue;
554 if (len <= 0)
555 break;
557 offset += len;
561 int kvm_main_loop(void)
563 int fds[2];
564 sigset_t mask;
565 int sigfd;
567 io_thread = pthread_self();
568 qemu_system_ready = 1;
570 if (qemu_eventfd(fds) == -1) {
571 fprintf(stderr, "failed to create eventfd\n");
572 return -errno;
575 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
576 (void *)(unsigned long)fds[0]);
578 io_thread_fd = fds[1];
580 sigemptyset(&mask);
581 sigaddset(&mask, SIGIO);
582 sigaddset(&mask, SIGALRM);
583 sigprocmask(SIG_BLOCK, &mask, NULL);
585 sigfd = qemu_signalfd(&mask);
586 if (sigfd == -1) {
587 fprintf(stderr, "failed to create signalfd\n");
588 return -errno;
591 fcntl(sigfd, F_SETFL, O_NONBLOCK);
593 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
594 (void *)(unsigned long)sigfd);
596 pthread_cond_broadcast(&qemu_system_cond);
598 io_thread_sigfd = sigfd;
599 cpu_single_env = NULL;
601 while (1) {
602 main_loop_wait(1000);
603 if (qemu_shutdown_requested())
604 break;
605 else if (qemu_powerdown_requested())
606 qemu_system_powerdown();
607 else if (qemu_reset_requested())
608 qemu_kvm_system_reset();
609 else if (kvm_debug_stop_requested) {
610 vm_stop(EXCP_DEBUG);
611 kvm_debug_stop_requested = 0;
615 pause_all_threads();
616 pthread_mutex_unlock(&qemu_mutex);
618 return 0;
621 static int kvm_debug(void *opaque, void *data)
623 struct CPUState *env = (struct CPUState *)data;
625 kvm_debug_stop_requested = 1;
626 env->kvm_cpu_state.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, void *data)
722 struct CPUState *env = (struct CPUState *)data;
724 /* stop the current vcpu from going back to guest mode */
725 env->kvm_cpu_state.stopped = 1;
727 qemu_system_reset_request();
728 return 1;
731 static struct kvm_callbacks qemu_kvm_ops = {
732 .debug = kvm_debug,
733 .inb = kvm_inb,
734 .inw = kvm_inw,
735 .inl = kvm_inl,
736 .outb = kvm_outb,
737 .outw = kvm_outw,
738 .outl = kvm_outl,
739 .mmio_read = kvm_mmio_read,
740 .mmio_write = kvm_mmio_write,
741 .halt = kvm_halt,
742 .shutdown = kvm_shutdown,
743 .io_window = kvm_io_window,
744 .try_push_interrupts = try_push_interrupts,
745 .try_push_nmi = try_push_nmi,
746 .post_kvm_run = post_kvm_run,
747 .pre_kvm_run = pre_kvm_run,
748 #ifdef TARGET_I386
749 .tpr_access = handle_tpr_access,
750 #endif
751 #ifdef TARGET_PPC
752 .powerpc_dcr_read = handle_powerpc_dcr_read,
753 .powerpc_dcr_write = handle_powerpc_dcr_write,
754 #endif
757 int kvm_qemu_init()
759 /* Try to initialize kvm */
760 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
761 if (!kvm_context) {
762 return -1;
764 pthread_mutex_lock(&qemu_mutex);
766 return 0;
769 int kvm_qemu_create_context(void)
771 int r;
772 if (!kvm_irqchip) {
773 kvm_disable_irqchip_creation(kvm_context);
775 if (!kvm_pit) {
776 kvm_disable_pit_creation(kvm_context);
778 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
779 kvm_qemu_destroy();
780 return -1;
782 r = kvm_arch_qemu_create_context();
783 if(r <0)
784 kvm_qemu_destroy();
785 return 0;
788 void kvm_qemu_destroy(void)
790 kvm_finalize(kvm_context);
793 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
794 unsigned long size,
795 unsigned long phys_offset)
797 int r = 0;
798 unsigned long area_flags = phys_offset & ~TARGET_PAGE_MASK;
800 phys_offset &= ~IO_MEM_ROM;
802 if (area_flags == IO_MEM_UNASSIGNED) {
803 kvm_unregister_memory_area(kvm_context, start_addr, size);
804 return;
807 r = kvm_is_containing_region(kvm_context, start_addr, size);
808 if (r)
809 return;
811 if (area_flags >= TLB_MMIO)
812 return;
814 r = kvm_register_phys_mem(kvm_context, start_addr,
815 phys_ram_base + phys_offset,
816 size, 0);
817 if (r < 0) {
818 printf("kvm_cpu_register_physical_memory: failed\n");
819 exit(1);
821 return;
824 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
825 target_phys_addr_t size,
826 unsigned long phys_offset)
828 kvm_unregister_memory_area(kvm_context, start_addr, size);
831 int kvm_setup_guest_memory(void *area, unsigned long size)
833 int ret = 0;
835 #ifdef MADV_DONTFORK
836 if (kvm_enabled() && !kvm_has_sync_mmu(kvm_context))
837 ret = madvise(area, size, MADV_DONTFORK);
838 #endif
840 if (ret)
841 perror ("madvise");
843 return ret;
846 int kvm_qemu_check_extension(int ext)
848 return kvm_check_extension(kvm_context, ext);
851 int kvm_qemu_init_env(CPUState *cenv)
853 return kvm_arch_qemu_init_env(cenv);
856 struct kvm_guest_debug_data {
857 struct kvm_debug_guest dbg;
858 int err;
861 void kvm_invoke_guest_debug(void *data)
863 struct kvm_guest_debug_data *dbg_data = data;
865 dbg_data->err = kvm_guest_debug(kvm_context, cpu_single_env->cpu_index,
866 &dbg_data->dbg);
869 int kvm_update_debugger(CPUState *env)
871 struct kvm_guest_debug_data data;
872 int i;
874 memset(data.dbg.breakpoints, 0, sizeof(data.dbg.breakpoints));
876 data.dbg.enabled = 0;
877 if (env->nb_breakpoints || env->singlestep_enabled) {
878 data.dbg.enabled = 1;
879 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
880 data.dbg.breakpoints[i].enabled = 1;
881 data.dbg.breakpoints[i].address = env->breakpoints[i];
883 data.dbg.singlestep = env->singlestep_enabled;
885 on_vcpu(env, kvm_invoke_guest_debug, &data);
886 return data.err;
891 * dirty pages logging
893 /* FIXME: use unsigned long pointer instead of unsigned char */
894 unsigned char *kvm_dirty_bitmap = NULL;
895 int kvm_physical_memory_set_dirty_tracking(int enable)
897 int r = 0;
899 if (!kvm_enabled())
900 return 0;
902 if (enable) {
903 if (!kvm_dirty_bitmap) {
904 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
905 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
906 if (kvm_dirty_bitmap == NULL) {
907 perror("Failed to allocate dirty pages bitmap");
908 r=-1;
910 else {
911 r = kvm_dirty_pages_log_enable_all(kvm_context);
915 else {
916 if (kvm_dirty_bitmap) {
917 r = kvm_dirty_pages_log_reset(kvm_context);
918 qemu_free(kvm_dirty_bitmap);
919 kvm_dirty_bitmap = NULL;
922 return r;
925 /* get kvm's dirty pages bitmap and update qemu's */
926 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
927 unsigned char *bitmap,
928 unsigned int offset,
929 unsigned long mem_size)
931 unsigned int i, j, n=0;
932 unsigned char c;
933 unsigned page_number, addr, addr1;
934 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
937 * bitmap-traveling is faster than memory-traveling (for addr...)
938 * especially when most of the memory is not dirty.
940 for (i=0; i<len; i++) {
941 c = bitmap[i];
942 while (c>0) {
943 j = ffsl(c) - 1;
944 c &= ~(1u<<j);
945 page_number = i * 8 + j;
946 addr1 = page_number * TARGET_PAGE_SIZE;
947 addr = offset + addr1;
948 cpu_physical_memory_set_dirty(addr);
949 n++;
952 return 0;
954 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
955 void *bitmap, void *opaque)
957 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
961 * get kvm's dirty pages bitmap and update qemu's
962 * we only care about physical ram, which resides in slots 0 and 3
964 int kvm_update_dirty_pages_log(void)
966 int r = 0;
969 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
970 kvm_dirty_bitmap, NULL,
971 kvm_get_dirty_bitmap_cb);
972 return r;
975 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
976 int log)
978 if (log)
979 kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
980 else
981 kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
984 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
986 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
987 unsigned int brsize = BITMAP_SIZE(ram_size);
988 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
989 unsigned int extra_bytes = (extra_pages +7)/8;
990 unsigned int hole_start = BITMAP_SIZE(0xa0000);
991 unsigned int hole_end = BITMAP_SIZE(0xc0000);
993 memset(bitmap, 0xFF, brsize + extra_bytes);
994 memset(bitmap + hole_start, 0, hole_end - hole_start);
995 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
997 return 0;
1000 #ifdef KVM_CAP_IRQCHIP
1002 int kvm_set_irq(int irq, int level)
1004 return kvm_set_irq_level(kvm_context, irq, level);
1007 #endif
1009 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1011 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1014 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1015 unsigned long size, int log, int writable)
1017 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1020 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1021 unsigned long size)
1023 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1026 void kvm_mutex_unlock(void)
1028 assert(!cpu_single_env);
1029 pthread_mutex_unlock(&qemu_mutex);
1032 void kvm_mutex_lock(void)
1034 pthread_mutex_lock(&qemu_mutex);
1035 cpu_single_env = NULL;
1038 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1040 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1043 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1044 unsigned int size)
1046 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);
1049 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1050 void kvm_add_ioperm_data(struct ioperm_data *data)
1052 LIST_INSERT_HEAD(&ioperm_head, data, entries);
1055 void kvm_ioperm(CPUState *env, void *data)
1057 if (kvm_enabled() && qemu_system_ready)
1058 on_vcpu(env, kvm_arch_do_ioperm, data);
1060 #endif