Use signalfd() in io-thread
[qemu-kvm/fedora.git] / qemu-kvm.c
blob11a54285db2fecef5de295a4eab9668cc1c66f2b
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 <string.h>
16 #include "hw/hw.h"
17 #include "sysemu.h"
18 #include "qemu-common.h"
19 #include "console.h"
20 #include "block.h"
22 #include "qemu-kvm.h"
23 #include <libkvm.h>
24 #include <pthread.h>
25 #include <sys/utsname.h>
26 #include <sys/syscall.h>
28 extern void perror(const char *s);
30 kvm_context_t kvm_context;
32 extern int smp_cpus;
34 static int qemu_kvm_reset_requested;
36 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
37 pthread_cond_t qemu_aio_cond = PTHREAD_COND_INITIALIZER;
38 pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
39 pthread_cond_t qemu_system_cond = PTHREAD_COND_INITIALIZER;
40 pthread_cond_t qemu_pause_cond = PTHREAD_COND_INITIALIZER;
41 __thread struct vcpu_info *vcpu;
43 static int qemu_system_ready;
45 #define SIG_IPI (SIGRTMIN+4)
47 struct vcpu_info {
48 CPUState *env;
49 int sipi_needed;
50 int init;
51 pthread_t thread;
52 int signalled;
53 int stop;
54 int stopped;
55 int reload_regs;
56 int created;
57 } vcpu_info[256];
59 pthread_t io_thread;
60 static int io_thread_fd = -1;
61 static int io_thread_sigfd = -1;
63 static inline unsigned long kvm_get_thread_id(void)
65 return syscall(SYS_gettid);
68 CPUState *qemu_kvm_cpu_env(int index)
70 return vcpu_info[index].env;
73 static void sig_ipi_handler(int n)
77 void kvm_update_interrupt_request(CPUState *env)
79 int signal = 0;
81 if (env) {
82 if (!vcpu)
83 signal = 1;
84 if (vcpu && env != vcpu->env && !vcpu_info[env->cpu_index].signalled)
85 signal = 1;
87 if (signal) {
88 vcpu_info[env->cpu_index].signalled = 1;
89 if (vcpu_info[env->cpu_index].thread)
90 pthread_kill(vcpu_info[env->cpu_index].thread, SIG_IPI);
95 void kvm_update_after_sipi(CPUState *env)
97 vcpu_info[env->cpu_index].sipi_needed = 1;
98 kvm_update_interrupt_request(env);
101 void kvm_apic_init(CPUState *env)
103 if (env->cpu_index != 0)
104 vcpu_info[env->cpu_index].init = 1;
105 kvm_update_interrupt_request(env);
108 #include <signal.h>
110 static int try_push_interrupts(void *opaque)
112 return kvm_arch_try_push_interrupts(opaque);
115 static void post_kvm_run(void *opaque, int vcpu)
118 pthread_mutex_lock(&qemu_mutex);
119 kvm_arch_post_kvm_run(opaque, vcpu);
122 static int pre_kvm_run(void *opaque, int vcpu)
124 CPUState *env = qemu_kvm_cpu_env(vcpu);
126 kvm_arch_pre_kvm_run(opaque, vcpu);
128 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
129 return 1;
130 pthread_mutex_unlock(&qemu_mutex);
131 return 0;
134 void kvm_load_registers(CPUState *env)
136 if (kvm_enabled())
137 kvm_arch_load_regs(env);
140 void kvm_save_registers(CPUState *env)
142 if (kvm_enabled())
143 kvm_arch_save_regs(env);
146 int kvm_cpu_exec(CPUState *env)
148 int r;
150 r = kvm_run(kvm_context, env->cpu_index);
151 if (r < 0) {
152 printf("kvm_run returned %d\n", r);
153 exit(1);
156 return 0;
159 extern int vm_running;
161 static int has_work(CPUState *env)
163 if (!vm_running || (env && vcpu_info[env->cpu_index].stopped))
164 return 0;
165 if (!(env->hflags & HF_HALTED_MASK))
166 return 1;
167 return kvm_arch_has_work(env);
170 static int kvm_eat_signal(CPUState *env, int timeout)
172 struct timespec ts;
173 int r, e, ret = 0;
174 siginfo_t siginfo;
175 sigset_t waitset;
177 ts.tv_sec = timeout / 1000;
178 ts.tv_nsec = (timeout % 1000) * 1000000;
179 sigemptyset(&waitset);
180 sigaddset(&waitset, SIG_IPI);
182 r = sigtimedwait(&waitset, &siginfo, &ts);
183 if (r == -1 && (errno == EAGAIN || errno == EINTR) && !timeout)
184 return 0;
185 e = errno;
187 pthread_mutex_lock(&qemu_mutex);
188 if (env && vcpu)
189 cpu_single_env = vcpu->env;
190 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
191 printf("sigtimedwait: %s\n", strerror(e));
192 exit(1);
194 if (r != -1)
195 ret = 1;
197 if (env && vcpu_info[env->cpu_index].stop) {
198 vcpu_info[env->cpu_index].stop = 0;
199 vcpu_info[env->cpu_index].stopped = 1;
200 pthread_cond_signal(&qemu_pause_cond);
202 pthread_mutex_unlock(&qemu_mutex);
204 return ret;
208 static void kvm_eat_signals(CPUState *env, int timeout)
210 int r = 0;
212 while (kvm_eat_signal(env, 0))
213 r = 1;
214 if (!r && timeout) {
215 r = kvm_eat_signal(env, timeout);
216 if (r)
217 while (kvm_eat_signal(env, 0))
222 static void kvm_main_loop_wait(CPUState *env, int timeout)
224 pthread_mutex_unlock(&qemu_mutex);
225 kvm_eat_signals(env, timeout);
226 pthread_mutex_lock(&qemu_mutex);
227 cpu_single_env = env;
228 vcpu_info[env->cpu_index].signalled = 0;
231 static int all_threads_paused(void)
233 int i;
235 for (i = 0; i < smp_cpus; ++i)
236 if (vcpu_info[i].stop)
237 return 0;
238 return 1;
241 static void pause_all_threads(void)
243 int i;
245 for (i = 0; i < smp_cpus; ++i) {
246 vcpu_info[i].stop = 1;
247 pthread_kill(vcpu_info[i].thread, SIG_IPI);
249 while (!all_threads_paused()) {
250 CPUState *env = cpu_single_env;
251 pthread_cond_wait(&qemu_pause_cond, &qemu_mutex);
252 cpu_single_env = env;
256 static void resume_all_threads(void)
258 int i;
260 for (i = 0; i < smp_cpus; ++i) {
261 vcpu_info[i].stop = 0;
262 vcpu_info[i].stopped = 0;
263 pthread_kill(vcpu_info[i].thread, SIG_IPI);
267 static void kvm_vm_state_change_handler(void *context, int running)
269 if (running)
270 resume_all_threads();
271 else
272 pause_all_threads();
275 static void update_regs_for_sipi(CPUState *env)
277 kvm_arch_update_regs_for_sipi(env);
278 vcpu_info[env->cpu_index].sipi_needed = 0;
279 vcpu_info[env->cpu_index].init = 0;
282 static void update_regs_for_init(CPUState *env)
284 cpu_reset(env);
285 kvm_arch_load_regs(env);
288 static void setup_kernel_sigmask(CPUState *env)
290 sigset_t set;
292 sigemptyset(&set);
293 sigaddset(&set, SIGUSR2);
294 sigaddset(&set, SIGIO);
295 sigaddset(&set, SIGALRM);
296 sigprocmask(SIG_BLOCK, &set, NULL);
298 sigprocmask(SIG_BLOCK, NULL, &set);
299 sigdelset(&set, SIG_IPI);
301 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
304 void qemu_kvm_system_reset_request(void)
306 int i;
308 for (i = 0; i < smp_cpus; ++i) {
309 vcpu_info[i].reload_regs = 1;
310 pthread_kill(vcpu_info[i].thread, SIG_IPI);
312 qemu_system_reset();
315 static int kvm_main_loop_cpu(CPUState *env)
317 struct vcpu_info *info = &vcpu_info[env->cpu_index];
319 setup_kernel_sigmask(env);
321 pthread_mutex_lock(&qemu_mutex);
322 if (kvm_irqchip_in_kernel(kvm_context))
323 env->hflags &= ~HF_HALTED_MASK;
325 kvm_qemu_init_env(env);
326 env->ready_for_interrupt_injection = 1;
327 #ifdef TARGET_I386
328 kvm_tpr_vcpu_start(env);
329 #endif
331 cpu_single_env = env;
332 while (1) {
333 while (!has_work(env))
334 kvm_main_loop_wait(env, 1000);
335 if (env->interrupt_request & CPU_INTERRUPT_HARD)
336 env->hflags &= ~HF_HALTED_MASK;
337 if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
338 update_regs_for_sipi(env);
339 if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
340 update_regs_for_init(env);
341 if (!(env->hflags & HF_HALTED_MASK) && !info->init)
342 kvm_cpu_exec(env);
343 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
344 kvm_main_loop_wait(env, 0);
345 if (info->reload_regs) {
346 info->reload_regs = 0;
347 if (env->cpu_index == 0) /* ap needs to be placed in INIT */
348 kvm_arch_load_regs(env);
351 pthread_mutex_unlock(&qemu_mutex);
352 return 0;
355 static void *ap_main_loop(void *_env)
357 CPUState *env = _env;
358 sigset_t signals;
360 vcpu = &vcpu_info[env->cpu_index];
361 vcpu->env = env;
362 vcpu->env->thread_id = kvm_get_thread_id();
363 sigfillset(&signals);
364 sigprocmask(SIG_BLOCK, &signals, NULL);
365 kvm_create_vcpu(kvm_context, env->cpu_index);
366 kvm_qemu_init_env(env);
368 /* signal VCPU creation */
369 pthread_mutex_lock(&qemu_mutex);
370 vcpu->created = 1;
371 pthread_cond_signal(&qemu_vcpu_cond);
373 /* and wait for machine initialization */
374 while (!qemu_system_ready)
375 pthread_cond_wait(&qemu_system_cond, &qemu_mutex);
376 pthread_mutex_unlock(&qemu_mutex);
378 kvm_main_loop_cpu(env);
379 return NULL;
382 void kvm_init_new_ap(int cpu, CPUState *env)
384 pthread_create(&vcpu_info[cpu].thread, NULL, ap_main_loop, env);
386 while (vcpu_info[cpu].created == 0)
387 pthread_cond_wait(&qemu_vcpu_cond, &qemu_mutex);
390 int kvm_init_ap(void)
392 #ifdef TARGET_I386
393 kvm_tpr_opt_setup();
394 #endif
395 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
397 signal(SIG_IPI, sig_ipi_handler);
398 return 0;
401 void qemu_kvm_notify_work(void)
403 uint64_t value = 1;
404 char buffer[8];
405 size_t offset = 0;
407 if (io_thread_fd == -1)
408 return;
410 memcpy(buffer, &value, sizeof(value));
412 while (offset < 8) {
413 ssize_t len;
415 len = write(io_thread_fd, buffer + offset, 8 - offset);
416 if (len == -1 && errno == EINTR)
417 continue;
419 if (len <= 0)
420 break;
422 offset += len;
425 if (offset != 8)
426 fprintf(stderr, "failed to notify io thread\n");
429 static int received_signal;
431 /* QEMU relies on periodically breaking out of select via EINTR to poll for IO
432 and timer signals. Since we're now using a file descriptor to handle
433 signals, select() won't be interrupted by a signal. We need to forcefully
434 break the select() loop when a signal is received hence
435 kvm_check_received_signal(). */
437 int kvm_check_received_signal(void)
439 if (received_signal) {
440 received_signal = 0;
441 return 1;
444 return 0;
447 /* If we have signalfd, we mask out the signals we want to handle and then
448 * use signalfd to listen for them. We rely on whatever the current signal
449 * handler is to dispatch the signals when we receive them.
452 static void sigfd_handler(void *opaque)
454 int fd = (unsigned long)opaque;
455 struct signalfd_siginfo info;
456 struct sigaction action;
457 ssize_t len;
459 while (1) {
460 do {
461 len = read(fd, &info, sizeof(info));
462 } while (len == -1 && errno == EINTR);
464 if (len == -1 && errno == EAGAIN)
465 break;
467 if (len != sizeof(info)) {
468 printf("read from sigfd returned %ld: %m\n", len);
469 return;
472 sigaction(info.ssi_signo, NULL, &action);
473 if (action.sa_handler)
474 action.sa_handler(info.ssi_signo);
476 if (info.ssi_signo == SIGUSR2) {
477 pthread_cond_signal(&qemu_aio_cond);
481 received_signal = 1;
484 /* Used to break IO thread out of select */
485 static void io_thread_wakeup(void *opaque)
487 int fd = (unsigned long)opaque;
488 char buffer[8];
489 size_t offset = 0;
491 while (offset < 8) {
492 ssize_t len;
494 len = read(fd, buffer + offset, 8 - offset);
495 if (len == -1 && errno == EINTR)
496 continue;
498 if (len <= 0)
499 break;
501 offset += len;
504 received_signal = 1;
507 int kvm_main_loop(void)
509 int fds[2];
510 sigset_t mask;
511 int sigfd;
513 io_thread = pthread_self();
514 qemu_system_ready = 1;
516 if (kvm_eventfd(fds) == -1) {
517 fprintf(stderr, "failed to create eventfd\n");
518 return -errno;
521 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
522 (void *)(unsigned long)fds[0]);
524 io_thread_fd = fds[1];
526 sigemptyset(&mask);
527 sigaddset(&mask, SIGIO);
528 sigaddset(&mask, SIGALRM);
529 sigaddset(&mask, SIGUSR2);
530 sigprocmask(SIG_BLOCK, &mask, NULL);
532 sigfd = kvm_signalfd(&mask);
533 if (sigfd == -1) {
534 fprintf(stderr, "failed to create signalfd\n");
535 return -errno;
538 fcntl(sigfd, F_SETFL, O_NONBLOCK);
540 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
541 (void *)(unsigned long)sigfd);
543 pthread_cond_broadcast(&qemu_system_cond);
545 io_thread_sigfd = sigfd;
546 cpu_single_env = NULL;
548 while (1) {
549 main_loop_wait(1000);
550 if (qemu_shutdown_requested())
551 break;
552 else if (qemu_powerdown_requested())
553 qemu_system_powerdown();
554 else if (qemu_reset_requested()) {
555 pthread_kill(vcpu_info[0].thread, SIG_IPI);
556 qemu_kvm_reset_requested = 1;
560 pause_all_threads();
561 pthread_mutex_unlock(&qemu_mutex);
563 return 0;
566 static int kvm_debug(void *opaque, int vcpu)
568 CPUState *env = cpu_single_env;
570 env->exception_index = EXCP_DEBUG;
571 return 1;
574 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
576 *data = cpu_inb(0, addr);
577 return 0;
580 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
582 *data = cpu_inw(0, addr);
583 return 0;
586 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
588 *data = cpu_inl(0, addr);
589 return 0;
592 #define PM_IO_BASE 0xb000
594 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
596 if (addr == 0xb2) {
597 switch (data) {
598 case 0: {
599 cpu_outb(0, 0xb3, 0);
600 break;
602 case 0xf0: {
603 unsigned x;
605 /* enable acpi */
606 x = cpu_inw(0, PM_IO_BASE + 4);
607 x &= ~1;
608 cpu_outw(0, PM_IO_BASE + 4, x);
609 break;
611 case 0xf1: {
612 unsigned x;
614 /* enable acpi */
615 x = cpu_inw(0, PM_IO_BASE + 4);
616 x |= 1;
617 cpu_outw(0, PM_IO_BASE + 4, x);
618 break;
620 default:
621 break;
623 return 0;
625 cpu_outb(0, addr, data);
626 return 0;
629 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
631 cpu_outw(0, addr, data);
632 return 0;
635 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
637 cpu_outl(0, addr, data);
638 return 0;
641 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
643 cpu_physical_memory_rw(addr, data, len, 0);
644 return 0;
647 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
649 cpu_physical_memory_rw(addr, data, len, 1);
650 return 0;
653 static int kvm_io_window(void *opaque)
655 return 1;
659 static int kvm_halt(void *opaque, int vcpu)
661 return kvm_arch_halt(opaque, vcpu);
664 static int kvm_shutdown(void *opaque, int vcpu)
666 qemu_system_reset_request();
667 return 1;
670 static struct kvm_callbacks qemu_kvm_ops = {
671 .debug = kvm_debug,
672 .inb = kvm_inb,
673 .inw = kvm_inw,
674 .inl = kvm_inl,
675 .outb = kvm_outb,
676 .outw = kvm_outw,
677 .outl = kvm_outl,
678 .mmio_read = kvm_mmio_read,
679 .mmio_write = kvm_mmio_write,
680 .halt = kvm_halt,
681 .shutdown = kvm_shutdown,
682 .io_window = kvm_io_window,
683 .try_push_interrupts = try_push_interrupts,
684 .post_kvm_run = post_kvm_run,
685 .pre_kvm_run = pre_kvm_run,
686 #ifdef TARGET_I386
687 .tpr_access = handle_tpr_access,
688 #endif
689 #ifdef TARGET_PPC
690 .powerpc_dcr_read = handle_powerpc_dcr_read,
691 .powerpc_dcr_write = handle_powerpc_dcr_write,
692 #endif
695 int kvm_qemu_init()
697 /* Try to initialize kvm */
698 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
699 if (!kvm_context) {
700 return -1;
702 pthread_mutex_lock(&qemu_mutex);
704 return 0;
707 int kvm_qemu_create_context(void)
709 int r;
710 if (!kvm_irqchip) {
711 kvm_disable_irqchip_creation(kvm_context);
713 if (!kvm_pit) {
714 kvm_disable_pit_creation(kvm_context);
716 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
717 kvm_qemu_destroy();
718 return -1;
720 r = kvm_arch_qemu_create_context();
721 if(r <0)
722 kvm_qemu_destroy();
723 return 0;
726 void kvm_qemu_destroy(void)
728 kvm_finalize(kvm_context);
731 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
732 unsigned long size,
733 unsigned long phys_offset)
735 #ifdef KVM_CAP_USER_MEMORY
736 int r = 0;
738 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
739 if (r) {
740 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
741 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
742 if (r)
743 return;
744 r = kvm_is_intersecting_mem(kvm_context, start_addr);
745 if (r)
746 kvm_create_mem_hole(kvm_context, start_addr, size);
747 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
748 phys_ram_base + phys_offset,
749 size, 0);
751 if (phys_offset & IO_MEM_ROM) {
752 phys_offset &= ~IO_MEM_ROM;
753 r = kvm_is_intersecting_mem(kvm_context, start_addr);
754 if (r)
755 kvm_create_mem_hole(kvm_context, start_addr, size);
756 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
757 phys_ram_base + phys_offset,
758 size, 0);
760 if (r < 0) {
761 printf("kvm_cpu_register_physical_memory: failed\n");
762 exit(1);
764 return;
766 #endif
767 if (phys_offset & IO_MEM_ROM) {
768 phys_offset &= ~IO_MEM_ROM;
769 memcpy(phys_ram_base + start_addr, phys_ram_base + phys_offset, size);
773 int kvm_qemu_check_extension(int ext)
775 return kvm_check_extension(kvm_context, ext);
778 int kvm_qemu_init_env(CPUState *cenv)
780 return kvm_arch_qemu_init_env(cenv);
783 int kvm_update_debugger(CPUState *env)
785 struct kvm_debug_guest dbg;
786 int i;
788 dbg.enabled = 0;
789 if (env->nb_breakpoints || env->singlestep_enabled) {
790 dbg.enabled = 1;
791 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
792 dbg.breakpoints[i].enabled = 1;
793 dbg.breakpoints[i].address = env->breakpoints[i];
795 dbg.singlestep = env->singlestep_enabled;
797 return kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
802 * dirty pages logging
804 /* FIXME: use unsigned long pointer instead of unsigned char */
805 unsigned char *kvm_dirty_bitmap = NULL;
806 int kvm_physical_memory_set_dirty_tracking(int enable)
808 int r = 0;
810 if (!kvm_enabled())
811 return 0;
813 if (enable) {
814 if (!kvm_dirty_bitmap) {
815 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
816 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
817 if (kvm_dirty_bitmap == NULL) {
818 perror("Failed to allocate dirty pages bitmap");
819 r=-1;
821 else {
822 r = kvm_dirty_pages_log_enable_all(kvm_context);
826 else {
827 if (kvm_dirty_bitmap) {
828 r = kvm_dirty_pages_log_reset(kvm_context);
829 qemu_free(kvm_dirty_bitmap);
830 kvm_dirty_bitmap = NULL;
833 return r;
836 /* get kvm's dirty pages bitmap and update qemu's */
837 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
838 unsigned char *bitmap,
839 unsigned int offset,
840 unsigned long mem_size)
842 unsigned int i, j, n=0;
843 unsigned char c;
844 unsigned page_number, addr, addr1;
845 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
848 * bitmap-traveling is faster than memory-traveling (for addr...)
849 * especially when most of the memory is not dirty.
851 for (i=0; i<len; i++) {
852 c = bitmap[i];
853 while (c>0) {
854 j = ffsl(c) - 1;
855 c &= ~(1u<<j);
856 page_number = i * 8 + j;
857 addr1 = page_number * TARGET_PAGE_SIZE;
858 addr = offset + addr1;
859 cpu_physical_memory_set_dirty(addr);
860 n++;
863 return 0;
865 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
866 void *bitmap, void *opaque)
868 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
872 * get kvm's dirty pages bitmap and update qemu's
873 * we only care about physical ram, which resides in slots 0 and 3
875 int kvm_update_dirty_pages_log(void)
877 int r = 0;
880 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
881 kvm_dirty_bitmap, NULL,
882 kvm_get_dirty_bitmap_cb);
883 return r;
886 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
888 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
889 unsigned int brsize = BITMAP_SIZE(ram_size);
890 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
891 unsigned int extra_bytes = (extra_pages +7)/8;
892 unsigned int hole_start = BITMAP_SIZE(0xa0000);
893 unsigned int hole_end = BITMAP_SIZE(0xc0000);
895 memset(bitmap, 0xFF, brsize + extra_bytes);
896 memset(bitmap + hole_start, 0, hole_end - hole_start);
897 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
899 return 0;
902 #ifdef KVM_CAP_IRQCHIP
904 int kvm_set_irq(int irq, int level)
906 return kvm_set_irq_level(kvm_context, irq, level);
909 #endif
911 void qemu_kvm_aio_wait_start(void)
915 void qemu_kvm_aio_wait(void)
917 CPUState *cpu_single = cpu_single_env;
919 if (!cpu_single_env) {
920 if (io_thread_sigfd != -1) {
921 fd_set rfds;
922 int ret;
924 FD_ZERO(&rfds);
925 FD_SET(io_thread_sigfd, &rfds);
927 /* this is a rare case where we do want to hold qemu_mutex
928 * while sleeping. We cannot allow anything else to run
929 * right now. */
930 ret = select(io_thread_sigfd + 1, &rfds, NULL, NULL, NULL);
931 if (ret > 0 && FD_ISSET(io_thread_sigfd, &rfds))
932 sigfd_handler((void *)(unsigned long)io_thread_sigfd);
934 qemu_aio_poll();
935 } else {
936 pthread_cond_wait(&qemu_aio_cond, &qemu_mutex);
937 cpu_single_env = cpu_single;
941 void qemu_kvm_aio_wait_end(void)
945 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
947 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
950 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
951 unsigned long size, int log, int writable)
953 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
956 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
957 unsigned long size)
959 kvm_destroy_phys_mem(kvm_context, start_addr, size);
962 void kvm_mutex_unlock(void)
964 pthread_mutex_unlock(&qemu_mutex);
967 void kvm_mutex_lock(void)
969 pthread_mutex_lock(&qemu_mutex);
970 cpu_single_env = NULL;