kvm: configure: run kernel configure even with --with-patched-kernel
[qemu-kvm/fedora.git] / qemu-kvm.c
blob4164368b2dda129f2bfacb1171fc4db9ff26369f
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 #include <assert.h>
12 #include <string.h>
13 #include "hw/hw.h"
14 #include "sysemu.h"
15 #include "qemu-common.h"
16 #include "console.h"
17 #include "block.h"
18 #include "compatfd.h"
19 #include "gdbstub.h"
21 #include "qemu-kvm.h"
22 #include <libkvm.h>
23 #include <pthread.h>
24 #include <sys/utsname.h>
25 #include <sys/syscall.h>
26 #include <sys/mman.h>
28 #define false 0
29 #define true 1
31 int kvm_allowed = 1;
32 int kvm_irqchip = 1;
33 int kvm_pit = 1;
34 int kvm_pit_reinject = 1;
35 int kvm_nested = 0;
36 kvm_context_t kvm_context;
38 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
39 pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
40 pthread_cond_t qemu_system_cond = PTHREAD_COND_INITIALIZER;
41 pthread_cond_t qemu_pause_cond = PTHREAD_COND_INITIALIZER;
42 pthread_cond_t qemu_work_cond = PTHREAD_COND_INITIALIZER;
43 __thread CPUState *current_env;
45 static int qemu_system_ready;
47 #define SIG_IPI (SIGRTMIN+4)
49 pthread_t io_thread;
50 static int io_thread_fd = -1;
51 static int io_thread_sigfd = -1;
53 static CPUState *kvm_debug_cpu_requested;
55 /* The list of ioperm_data */
56 static LIST_HEAD(, ioperm_data) ioperm_head;
58 static inline unsigned long kvm_get_thread_id(void)
60 return syscall(SYS_gettid);
63 static void qemu_cond_wait(pthread_cond_t *cond)
65 CPUState *env = cpu_single_env;
66 static const struct timespec ts = {
67 .tv_sec = 0,
68 .tv_nsec = 100000,
71 pthread_cond_timedwait(cond, &qemu_mutex, &ts);
72 cpu_single_env = env;
75 static void sig_ipi_handler(int n)
79 static void on_vcpu(CPUState *env, void (*func)(void *data), void *data)
81 struct qemu_work_item wi;
83 if (env == current_env) {
84 func(data);
85 return;
88 wi.func = func;
89 wi.data = data;
90 if (!env->kvm_cpu_state.queued_work_first)
91 env->kvm_cpu_state.queued_work_first = &wi;
92 else
93 env->kvm_cpu_state.queued_work_last->next = &wi;
94 env->kvm_cpu_state.queued_work_last = &wi;
95 wi.next = NULL;
96 wi.done = false;
98 pthread_kill(env->kvm_cpu_state.thread, SIG_IPI);
99 while (!wi.done)
100 qemu_cond_wait(&qemu_work_cond);
103 static void inject_interrupt(void *data)
105 cpu_interrupt(current_env, (long)data);
108 void kvm_inject_interrupt(CPUState *env, int mask)
110 on_vcpu(env, inject_interrupt, (void *)(long)mask);
113 void kvm_update_interrupt_request(CPUState *env)
115 int signal = 0;
117 if (env) {
118 if (!current_env || !current_env->kvm_cpu_state.created)
119 signal = 1;
121 * Testing for created here is really redundant
123 if (current_env && current_env->kvm_cpu_state.created &&
124 env != current_env && !env->kvm_cpu_state.signalled)
125 signal = 1;
127 if (signal) {
128 env->kvm_cpu_state.signalled = 1;
129 if (env->kvm_cpu_state.thread)
130 pthread_kill(env->kvm_cpu_state.thread, SIG_IPI);
135 void kvm_update_after_sipi(CPUState *env)
137 env->kvm_cpu_state.sipi_needed = 1;
138 kvm_update_interrupt_request(env);
141 void kvm_apic_init(CPUState *env)
143 if (env->cpu_index != 0)
144 env->kvm_cpu_state.init = 1;
145 kvm_update_interrupt_request(env);
148 #include <signal.h>
150 static int try_push_interrupts(void *opaque)
152 return kvm_arch_try_push_interrupts(opaque);
155 static void post_kvm_run(void *opaque, void *data)
157 CPUState *env = (CPUState *)data;
159 pthread_mutex_lock(&qemu_mutex);
160 kvm_arch_post_kvm_run(opaque, env);
163 static int pre_kvm_run(void *opaque, void *data)
165 CPUState *env = (CPUState *)data;
167 kvm_arch_pre_kvm_run(opaque, env);
169 if (env->exit_request)
170 return 1;
171 pthread_mutex_unlock(&qemu_mutex);
172 return 0;
175 static void kvm_do_load_registers(void *_env)
177 CPUState *env = _env;
179 kvm_arch_load_regs(env);
182 void kvm_load_registers(CPUState *env)
184 if (kvm_enabled() && qemu_system_ready)
185 on_vcpu(env, kvm_do_load_registers, env);
188 static void kvm_do_save_registers(void *_env)
190 CPUState *env = _env;
192 kvm_arch_save_regs(env);
195 void kvm_save_registers(CPUState *env)
197 if (kvm_enabled())
198 on_vcpu(env, kvm_do_save_registers, env);
201 int kvm_cpu_exec(CPUState *env)
203 int r;
205 r = kvm_run(kvm_context, env->cpu_index, env);
206 if (r < 0) {
207 printf("kvm_run returned %d\n", r);
208 exit(1);
211 return 0;
214 static int has_work(CPUState *env)
216 if (!vm_running || (env && env->kvm_cpu_state.stopped))
217 return 0;
218 if (!env->halted)
219 return 1;
220 return kvm_arch_has_work(env);
223 static void flush_queued_work(CPUState *env)
225 struct qemu_work_item *wi;
227 if (!env->kvm_cpu_state.queued_work_first)
228 return;
230 while ((wi = env->kvm_cpu_state.queued_work_first)) {
231 env->kvm_cpu_state.queued_work_first = wi->next;
232 wi->func(wi->data);
233 wi->done = true;
235 env->kvm_cpu_state.queued_work_last = NULL;
236 pthread_cond_broadcast(&qemu_work_cond);
239 static void kvm_main_loop_wait(CPUState *env, int timeout)
241 struct timespec ts;
242 int r, e;
243 siginfo_t siginfo;
244 sigset_t waitset;
246 pthread_mutex_unlock(&qemu_mutex);
248 ts.tv_sec = timeout / 1000;
249 ts.tv_nsec = (timeout % 1000) * 1000000;
250 sigemptyset(&waitset);
251 sigaddset(&waitset, SIG_IPI);
253 r = sigtimedwait(&waitset, &siginfo, &ts);
254 e = errno;
256 pthread_mutex_lock(&qemu_mutex);
258 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
259 printf("sigtimedwait: %s\n", strerror(e));
260 exit(1);
263 cpu_single_env = env;
264 flush_queued_work(env);
266 if (env->kvm_cpu_state.stop) {
267 env->kvm_cpu_state.stop = 0;
268 env->kvm_cpu_state.stopped = 1;
269 pthread_cond_signal(&qemu_pause_cond);
272 env->kvm_cpu_state.signalled = 0;
275 static int all_threads_paused(void)
277 CPUState *penv = first_cpu;
279 while (penv) {
280 if (penv->kvm_cpu_state.stop)
281 return 0;
282 penv = (CPUState *)penv->next_cpu;
285 return 1;
288 void qemu_kvm_pause_all_threads(void)
290 CPUState *penv = first_cpu;
292 while (penv) {
293 if (penv != cpu_single_env) {
294 penv->kvm_cpu_state.stop = 1;
295 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
296 } else {
297 penv->kvm_cpu_state.stop = 0;
298 penv->kvm_cpu_state.stopped = 1;
299 cpu_exit(penv);
301 penv = (CPUState *)penv->next_cpu;
304 while (!all_threads_paused())
305 qemu_cond_wait(&qemu_pause_cond);
308 void qemu_kvm_resume_all_threads(void)
310 CPUState *penv = first_cpu;
312 assert(!cpu_single_env);
314 while (penv) {
315 penv->kvm_cpu_state.stop = 0;
316 penv->kvm_cpu_state.stopped = 0;
317 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
318 penv = (CPUState *)penv->next_cpu;
322 static void update_regs_for_sipi(CPUState *env)
324 kvm_arch_update_regs_for_sipi(env);
325 env->kvm_cpu_state.sipi_needed = 0;
328 static void update_regs_for_init(CPUState *env)
330 #ifdef TARGET_I386
331 SegmentCache cs = env->segs[R_CS];
332 #endif
334 cpu_reset(env);
336 #ifdef TARGET_I386
337 /* restore SIPI vector */
338 if(env->kvm_cpu_state.sipi_needed)
339 env->segs[R_CS] = cs;
340 #endif
342 env->kvm_cpu_state.init = 0;
343 kvm_arch_load_regs(env);
346 static void setup_kernel_sigmask(CPUState *env)
348 sigset_t set;
350 sigemptyset(&set);
351 sigaddset(&set, SIGUSR2);
352 sigaddset(&set, SIGIO);
353 sigaddset(&set, SIGALRM);
354 sigprocmask(SIG_BLOCK, &set, NULL);
356 sigprocmask(SIG_BLOCK, NULL, &set);
357 sigdelset(&set, SIG_IPI);
359 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
362 static void qemu_kvm_system_reset(void)
364 CPUState *penv = first_cpu;
366 qemu_kvm_pause_all_threads();
368 qemu_system_reset();
370 while (penv) {
371 kvm_arch_cpu_reset(penv);
372 penv = (CPUState *)penv->next_cpu;
375 qemu_kvm_resume_all_threads();
378 static int kvm_main_loop_cpu(CPUState *env)
380 setup_kernel_sigmask(env);
382 pthread_mutex_lock(&qemu_mutex);
383 if (kvm_irqchip_in_kernel(kvm_context))
384 env->halted = 0;
386 kvm_qemu_init_env(env);
387 #ifdef TARGET_I386
388 kvm_tpr_vcpu_start(env);
389 #endif
391 cpu_single_env = env;
392 kvm_load_registers(env);
394 while (1) {
395 while (!has_work(env))
396 kvm_main_loop_wait(env, 1000);
397 if (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI))
398 env->halted = 0;
399 if (!kvm_irqchip_in_kernel(kvm_context)) {
400 if (env->kvm_cpu_state.init)
401 update_regs_for_init(env);
402 if (env->kvm_cpu_state.sipi_needed)
403 update_regs_for_sipi(env);
405 if (!env->halted && !env->kvm_cpu_state.init)
406 kvm_cpu_exec(env);
407 env->exit_request = 0;
408 env->exception_index = EXCP_INTERRUPT;
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;
419 struct ioperm_data *data = NULL;
421 current_env = env;
422 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 #ifdef USE_KVM_DEVICE_ASSIGNMENT
429 /* do ioperm for io ports of assigned devices */
430 LIST_FOREACH(data, &ioperm_head, entries)
431 on_vcpu(env, kvm_arch_do_ioperm, data);
432 #endif
434 /* signal VCPU creation */
435 pthread_mutex_lock(&qemu_mutex);
436 current_env->kvm_cpu_state.created = 1;
437 pthread_cond_signal(&qemu_vcpu_cond);
439 /* and wait for machine initialization */
440 while (!qemu_system_ready)
441 qemu_cond_wait(&qemu_system_cond);
442 pthread_mutex_unlock(&qemu_mutex);
444 kvm_main_loop_cpu(env);
445 return NULL;
448 void kvm_init_vcpu(CPUState *env)
450 pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
452 while (env->kvm_cpu_state.created == 0)
453 qemu_cond_wait(&qemu_vcpu_cond);
456 int kvm_init_ap(void)
458 #ifdef TARGET_I386
459 kvm_tpr_opt_setup();
460 #endif
462 signal(SIG_IPI, sig_ipi_handler);
463 return 0;
466 void qemu_kvm_notify_work(void)
468 uint64_t value = 1;
469 char buffer[8];
470 size_t offset = 0;
472 if (io_thread_fd == -1)
473 return;
475 memcpy(buffer, &value, sizeof(value));
477 while (offset < 8) {
478 ssize_t len;
480 len = write(io_thread_fd, buffer + offset, 8 - offset);
481 if (len == -1 && errno == EINTR)
482 continue;
484 if (len <= 0)
485 break;
487 offset += len;
490 if (offset != 8)
491 fprintf(stderr, "failed to notify io thread\n");
494 /* If we have signalfd, we mask out the signals we want to handle and then
495 * use signalfd to listen for them. We rely on whatever the current signal
496 * handler is to dispatch the signals when we receive them.
499 static void sigfd_handler(void *opaque)
501 int fd = (unsigned long)opaque;
502 struct qemu_signalfd_siginfo info;
503 struct sigaction action;
504 ssize_t len;
506 while (1) {
507 do {
508 len = read(fd, &info, sizeof(info));
509 } while (len == -1 && errno == EINTR);
511 if (len == -1 && errno == EAGAIN)
512 break;
514 if (len != sizeof(info)) {
515 printf("read from sigfd returned %ld: %m\n", len);
516 return;
519 sigaction(info.ssi_signo, NULL, &action);
520 if (action.sa_handler)
521 action.sa_handler(info.ssi_signo);
526 /* Used to break IO thread out of select */
527 static void io_thread_wakeup(void *opaque)
529 int fd = (unsigned long)opaque;
530 char buffer[8];
531 size_t offset = 0;
533 while (offset < 8) {
534 ssize_t len;
536 len = read(fd, buffer + offset, 8 - offset);
537 if (len == -1 && errno == EINTR)
538 continue;
540 if (len <= 0)
541 break;
543 offset += len;
547 int kvm_main_loop(void)
549 int fds[2];
550 sigset_t mask;
551 int sigfd;
553 io_thread = pthread_self();
554 qemu_system_ready = 1;
556 if (qemu_eventfd(fds) == -1) {
557 fprintf(stderr, "failed to create eventfd\n");
558 return -errno;
561 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
562 (void *)(unsigned long)fds[0]);
564 io_thread_fd = fds[1];
566 sigemptyset(&mask);
567 sigaddset(&mask, SIGIO);
568 sigaddset(&mask, SIGALRM);
569 sigprocmask(SIG_BLOCK, &mask, NULL);
571 sigfd = qemu_signalfd(&mask);
572 if (sigfd == -1) {
573 fprintf(stderr, "failed to create signalfd\n");
574 return -errno;
577 fcntl(sigfd, F_SETFL, O_NONBLOCK);
579 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
580 (void *)(unsigned long)sigfd);
582 pthread_cond_broadcast(&qemu_system_cond);
584 io_thread_sigfd = sigfd;
585 cpu_single_env = NULL;
587 while (1) {
588 main_loop_wait(1000);
589 if (qemu_shutdown_requested())
590 break;
591 else if (qemu_powerdown_requested())
592 qemu_system_powerdown();
593 else if (qemu_reset_requested())
594 qemu_kvm_system_reset();
595 #ifdef CONFIG_GDBSTUB
596 else if (kvm_debug_cpu_requested) {
597 gdb_set_stop_cpu(kvm_debug_cpu_requested);
598 vm_stop(EXCP_DEBUG);
599 kvm_debug_cpu_requested = NULL;
601 #endif
604 qemu_kvm_pause_all_threads();
605 pthread_mutex_unlock(&qemu_mutex);
607 return 0;
610 #ifdef KVM_CAP_SET_GUEST_DEBUG
611 static int kvm_debug(void *opaque, void *data,
612 struct kvm_debug_exit_arch *arch_info)
614 int handle = kvm_arch_debug(arch_info);
615 CPUState *env = data;
617 if (handle) {
618 kvm_debug_cpu_requested = env;
619 env->kvm_cpu_state.stopped = 1;
621 return handle;
623 #endif
625 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
627 *data = cpu_inb(0, addr);
628 return 0;
631 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
633 *data = cpu_inw(0, addr);
634 return 0;
637 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
639 *data = cpu_inl(0, addr);
640 return 0;
643 #define PM_IO_BASE 0xb000
645 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
647 if (addr == 0xb2) {
648 switch (data) {
649 case 0: {
650 cpu_outb(0, 0xb3, 0);
651 break;
653 case 0xf0: {
654 unsigned x;
656 /* enable acpi */
657 x = cpu_inw(0, PM_IO_BASE + 4);
658 x &= ~1;
659 cpu_outw(0, PM_IO_BASE + 4, x);
660 break;
662 case 0xf1: {
663 unsigned x;
665 /* enable acpi */
666 x = cpu_inw(0, PM_IO_BASE + 4);
667 x |= 1;
668 cpu_outw(0, PM_IO_BASE + 4, x);
669 break;
671 default:
672 break;
674 return 0;
676 cpu_outb(0, addr, data);
677 return 0;
680 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
682 cpu_outw(0, addr, data);
683 return 0;
686 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
688 cpu_outl(0, addr, data);
689 return 0;
692 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
694 cpu_physical_memory_rw(addr, data, len, 0);
695 return 0;
698 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
700 cpu_physical_memory_rw(addr, data, len, 1);
701 return 0;
704 static int kvm_io_window(void *opaque)
706 return 1;
710 static int kvm_halt(void *opaque, int vcpu)
712 return kvm_arch_halt(opaque, vcpu);
715 static int kvm_shutdown(void *opaque, void *data)
717 CPUState *env = (CPUState *)data;
719 /* stop the current vcpu from going back to guest mode */
720 env->kvm_cpu_state.stopped = 1;
722 qemu_system_reset_request();
723 return 1;
726 static struct kvm_callbacks qemu_kvm_ops = {
727 #ifdef KVM_CAP_SET_GUEST_DEBUG
728 .debug = kvm_debug,
729 #endif
730 .inb = kvm_inb,
731 .inw = kvm_inw,
732 .inl = kvm_inl,
733 .outb = kvm_outb,
734 .outw = kvm_outw,
735 .outl = kvm_outl,
736 .mmio_read = kvm_mmio_read,
737 .mmio_write = kvm_mmio_write,
738 .halt = kvm_halt,
739 .shutdown = kvm_shutdown,
740 .io_window = kvm_io_window,
741 .try_push_interrupts = try_push_interrupts,
742 #ifdef KVM_CAP_USER_NMI
743 .push_nmi = kvm_arch_push_nmi,
744 #endif
745 .post_kvm_run = post_kvm_run,
746 .pre_kvm_run = pre_kvm_run,
747 #ifdef TARGET_I386
748 .tpr_access = handle_tpr_access,
749 #endif
750 #ifdef TARGET_PPC
751 .powerpc_dcr_read = handle_powerpc_dcr_read,
752 .powerpc_dcr_write = handle_powerpc_dcr_write,
753 #endif
756 int kvm_qemu_init()
758 /* Try to initialize kvm */
759 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
760 if (!kvm_context) {
761 return -1;
763 pthread_mutex_lock(&qemu_mutex);
765 return 0;
768 #ifdef TARGET_I386
769 static int destroy_region_works = 0;
770 #endif
772 int kvm_qemu_create_context(void)
774 int r;
775 int i;
777 if (!kvm_irqchip) {
778 kvm_disable_irqchip_creation(kvm_context);
780 if (!kvm_pit) {
781 kvm_disable_pit_creation(kvm_context);
783 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
784 kvm_qemu_destroy();
785 return -1;
787 r = kvm_arch_qemu_create_context();
788 if(r <0)
789 kvm_qemu_destroy();
790 if (kvm_pit && !kvm_pit_reinject) {
791 if (kvm_reinject_control(kvm_context, 0)) {
792 fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
793 return -1;
796 #ifdef TARGET_I386
797 destroy_region_works = kvm_destroy_memory_region_works(kvm_context);
798 #endif
800 if (kvm_irqchip && kvm_has_gsi_routing(kvm_context)) {
801 kvm_clear_gsi_routes(kvm_context);
802 for (i = 0; i < 8; ++i) {
803 if (i == 2)
804 continue;
805 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_MASTER, i);
806 if (r < 0)
807 return r;
809 for (i = 8; i < 16; ++i) {
810 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
811 if (r < 0)
812 return r;
814 for (i = 0; i < 24; ++i) {
815 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_IOAPIC, i);
816 if (r < 0)
817 return r;
819 kvm_commit_irq_routes(kvm_context);
821 return 0;
824 void kvm_qemu_destroy(void)
826 kvm_finalize(kvm_context);
829 #ifdef TARGET_I386
830 static int must_use_aliases_source(target_phys_addr_t addr)
832 if (destroy_region_works)
833 return false;
834 if (addr == 0xa0000 || addr == 0xa8000)
835 return true;
836 return false;
839 static int must_use_aliases_target(target_phys_addr_t addr)
841 if (destroy_region_works)
842 return false;
843 if (addr >= 0xe0000000 && addr < 0x100000000ull)
844 return true;
845 return false;
848 static struct mapping {
849 target_phys_addr_t phys;
850 ram_addr_t ram;
851 ram_addr_t len;
852 } mappings[50];
853 static int nr_mappings;
855 static struct mapping *find_ram_mapping(ram_addr_t ram_addr)
857 struct mapping *p;
859 for (p = mappings; p < mappings + nr_mappings; ++p) {
860 if (p->ram <= ram_addr && ram_addr < p->ram + p->len) {
861 return p;
864 return NULL;
867 static struct mapping *find_mapping(target_phys_addr_t start_addr)
869 struct mapping *p;
871 for (p = mappings; p < mappings + nr_mappings; ++p) {
872 if (p->phys <= start_addr && start_addr < p->phys + p->len) {
873 return p;
876 return NULL;
879 static void drop_mapping(target_phys_addr_t start_addr)
881 struct mapping *p = find_mapping(start_addr);
883 if (p)
884 *p = mappings[--nr_mappings];
886 #endif
888 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
889 unsigned long size,
890 unsigned long phys_offset)
892 int r = 0;
893 unsigned long area_flags = phys_offset & ~TARGET_PAGE_MASK;
894 #ifdef TARGET_I386
895 struct mapping *p;
896 #endif
898 phys_offset &= ~IO_MEM_ROM;
900 if (area_flags == IO_MEM_UNASSIGNED) {
901 #ifdef TARGET_I386
902 if (must_use_aliases_source(start_addr)) {
903 kvm_destroy_memory_alias(kvm_context, start_addr);
904 return;
906 if (must_use_aliases_target(start_addr))
907 return;
908 #endif
909 kvm_unregister_memory_area(kvm_context, start_addr, size);
910 return;
913 r = kvm_is_containing_region(kvm_context, start_addr, size);
914 if (r)
915 return;
917 if (area_flags >= TLB_MMIO)
918 return;
920 #ifdef TARGET_I386
921 if (must_use_aliases_source(start_addr)) {
922 p = find_ram_mapping(phys_offset);
923 if (p) {
924 kvm_create_memory_alias(kvm_context, start_addr, size,
925 p->phys + (phys_offset - p->ram));
927 return;
929 #endif
931 r = kvm_register_phys_mem(kvm_context, start_addr,
932 phys_ram_base + phys_offset,
933 size, 0);
934 if (r < 0) {
935 printf("kvm_cpu_register_physical_memory: failed\n");
936 exit(1);
939 #ifdef TARGET_I386
940 drop_mapping(start_addr);
941 p = &mappings[nr_mappings++];
942 p->phys = start_addr;
943 p->ram = phys_offset;
944 p->len = size;
945 #endif
947 return;
950 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
951 target_phys_addr_t size,
952 unsigned long phys_offset)
954 kvm_unregister_memory_area(kvm_context, start_addr, size);
957 int kvm_setup_guest_memory(void *area, unsigned long size)
959 int ret = 0;
961 #ifdef MADV_DONTFORK
962 if (kvm_enabled() && !kvm_has_sync_mmu())
963 ret = madvise(area, size, MADV_DONTFORK);
964 #endif
966 if (ret)
967 perror ("madvise");
969 return ret;
972 int kvm_qemu_check_extension(int ext)
974 return kvm_check_extension(kvm_context, ext);
977 int kvm_qemu_init_env(CPUState *cenv)
979 return kvm_arch_qemu_init_env(cenv);
982 #ifdef KVM_CAP_SET_GUEST_DEBUG
983 struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
984 TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);
986 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc)
988 struct kvm_sw_breakpoint *bp;
990 TAILQ_FOREACH(bp, &kvm_sw_breakpoints, entry) {
991 if (bp->pc == pc)
992 return bp;
994 return NULL;
997 struct kvm_set_guest_debug_data {
998 struct kvm_guest_debug dbg;
999 int err;
1002 static void kvm_invoke_set_guest_debug(void *data)
1004 struct kvm_set_guest_debug_data *dbg_data = data;
1006 dbg_data->err = kvm_set_guest_debug(kvm_context, cpu_single_env->cpu_index,
1007 &dbg_data->dbg);
1010 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1012 struct kvm_set_guest_debug_data data;
1014 data.dbg.control = 0;
1015 if (env->singlestep_enabled)
1016 data.dbg.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1018 kvm_arch_update_guest_debug(env, &data.dbg);
1019 data.dbg.control |= reinject_trap;
1021 on_vcpu(env, kvm_invoke_set_guest_debug, &data);
1022 return data.err;
1025 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1026 target_ulong len, int type)
1028 struct kvm_sw_breakpoint *bp;
1029 CPUState *env;
1030 int err;
1032 if (type == GDB_BREAKPOINT_SW) {
1033 bp = kvm_find_sw_breakpoint(addr);
1034 if (bp) {
1035 bp->use_count++;
1036 return 0;
1039 bp = qemu_malloc(sizeof(struct kvm_sw_breakpoint));
1040 if (!bp)
1041 return -ENOMEM;
1043 bp->pc = addr;
1044 bp->use_count = 1;
1045 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1046 if (err) {
1047 free(bp);
1048 return err;
1051 TAILQ_INSERT_HEAD(&kvm_sw_breakpoints, bp, entry);
1052 } else {
1053 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1054 if (err)
1055 return err;
1058 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1059 err = kvm_update_guest_debug(env, 0);
1060 if (err)
1061 return err;
1063 return 0;
1066 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1067 target_ulong len, int type)
1069 struct kvm_sw_breakpoint *bp;
1070 CPUState *env;
1071 int err;
1073 if (type == GDB_BREAKPOINT_SW) {
1074 bp = kvm_find_sw_breakpoint(addr);
1075 if (!bp)
1076 return -ENOENT;
1078 if (bp->use_count > 1) {
1079 bp->use_count--;
1080 return 0;
1083 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1084 if (err)
1085 return err;
1087 TAILQ_REMOVE(&kvm_sw_breakpoints, bp, entry);
1088 qemu_free(bp);
1089 } else {
1090 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1091 if (err)
1092 return err;
1095 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1096 err = kvm_update_guest_debug(env, 0);
1097 if (err)
1098 return err;
1100 return 0;
1103 void kvm_remove_all_breakpoints(CPUState *current_env)
1105 struct kvm_sw_breakpoint *bp, *next;
1106 CPUState *env;
1108 TAILQ_FOREACH_SAFE(bp, &kvm_sw_breakpoints, entry, next) {
1109 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1110 /* Try harder to find a CPU that currently sees the breakpoint. */
1111 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1112 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0)
1113 break;
1117 kvm_arch_remove_all_hw_breakpoints();
1119 for (env = first_cpu; env != NULL; env = env->next_cpu)
1120 kvm_update_guest_debug(env, 0);
1123 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1125 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1127 return -EINVAL;
1130 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1131 target_ulong len, int type)
1133 return -EINVAL;
1136 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1137 target_ulong len, int type)
1139 return -EINVAL;
1142 void kvm_remove_all_breakpoints(CPUState *current_env)
1145 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1148 * dirty pages logging
1150 /* FIXME: use unsigned long pointer instead of unsigned char */
1151 unsigned char *kvm_dirty_bitmap = NULL;
1152 int kvm_physical_memory_set_dirty_tracking(int enable)
1154 int r = 0;
1156 if (!kvm_enabled())
1157 return 0;
1159 if (enable) {
1160 if (!kvm_dirty_bitmap) {
1161 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
1162 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
1163 if (kvm_dirty_bitmap == NULL) {
1164 perror("Failed to allocate dirty pages bitmap");
1165 r=-1;
1167 else {
1168 r = kvm_dirty_pages_log_enable_all(kvm_context);
1172 else {
1173 if (kvm_dirty_bitmap) {
1174 r = kvm_dirty_pages_log_reset(kvm_context);
1175 qemu_free(kvm_dirty_bitmap);
1176 kvm_dirty_bitmap = NULL;
1179 return r;
1182 /* get kvm's dirty pages bitmap and update qemu's */
1183 static int kvm_get_dirty_pages_log_range(unsigned long start_addr,
1184 unsigned char *bitmap,
1185 unsigned int offset,
1186 unsigned long mem_size)
1188 unsigned int i, j, n=0;
1189 unsigned char c;
1190 unsigned long page_number, addr, addr1;
1191 ram_addr_t ram_addr;
1192 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
1195 * bitmap-traveling is faster than memory-traveling (for addr...)
1196 * especially when most of the memory is not dirty.
1198 for (i=0; i<len; i++) {
1199 c = bitmap[i];
1200 while (c>0) {
1201 j = ffsl(c) - 1;
1202 c &= ~(1u<<j);
1203 page_number = i * 8 + j;
1204 addr1 = page_number * TARGET_PAGE_SIZE;
1205 addr = offset + addr1;
1206 ram_addr = cpu_get_physical_page_desc(addr);
1207 cpu_physical_memory_set_dirty(ram_addr);
1208 n++;
1211 return 0;
1213 static int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
1214 void *bitmap, void *opaque)
1216 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
1220 * get kvm's dirty pages bitmap and update qemu's
1221 * we only care about physical ram, which resides in slots 0 and 3
1223 int kvm_update_dirty_pages_log(void)
1225 int r = 0;
1228 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
1229 kvm_dirty_bitmap, NULL,
1230 kvm_get_dirty_bitmap_cb);
1231 return r;
1234 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
1235 int log)
1237 if (log)
1238 kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
1239 else {
1240 #ifdef TARGET_I386
1241 if (must_use_aliases_target(start))
1242 return;
1243 #endif
1244 kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
1248 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
1250 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
1251 unsigned int brsize = BITMAP_SIZE(ram_size);
1252 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
1253 unsigned int extra_bytes = (extra_pages +7)/8;
1254 unsigned int hole_start = BITMAP_SIZE(0xa0000);
1255 unsigned int hole_end = BITMAP_SIZE(0xc0000);
1257 memset(bitmap, 0xFF, brsize + extra_bytes);
1258 memset(bitmap + hole_start, 0, hole_end - hole_start);
1259 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
1261 return 0;
1264 #ifdef KVM_CAP_IRQCHIP
1266 int kvm_set_irq(int irq, int level, int *status)
1268 return kvm_set_irq_level(kvm_context, irq, level, status);
1271 #endif
1273 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1275 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1278 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1279 unsigned long size, int log, int writable)
1281 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1284 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1285 unsigned long size)
1287 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1290 void kvm_mutex_unlock(void)
1292 assert(!cpu_single_env);
1293 pthread_mutex_unlock(&qemu_mutex);
1296 void kvm_mutex_lock(void)
1298 pthread_mutex_lock(&qemu_mutex);
1299 cpu_single_env = NULL;
1302 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1304 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1307 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1308 unsigned int size)
1310 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);
1313 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1315 return kvm_register_coalesced_mmio(kvm_context, start, size);
1318 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1320 return kvm_unregister_coalesced_mmio(kvm_context, start, size);
1323 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1324 void kvm_add_ioperm_data(struct ioperm_data *data)
1326 LIST_INSERT_HEAD(&ioperm_head, data, entries);
1329 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num)
1331 struct ioperm_data *data;
1333 data = LIST_FIRST(&ioperm_head);
1334 while (data) {
1335 struct ioperm_data *next = LIST_NEXT(data, entries);
1337 if (data->start_port == start_port && data->num == num) {
1338 LIST_REMOVE(data, entries);
1339 qemu_free(data);
1342 data = next;
1346 void kvm_ioperm(CPUState *env, void *data)
1348 if (kvm_enabled() && qemu_system_ready)
1349 on_vcpu(env, kvm_arch_do_ioperm, data);
1352 #endif
1354 void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
1356 #ifndef TARGET_IA64
1357 void *buf;
1359 #ifdef TARGET_I386
1360 if (must_use_aliases_source(start_addr))
1361 return;
1362 #endif
1364 buf = qemu_malloc((end_addr - start_addr) / 8 + 2);
1365 kvm_get_dirty_pages_range(kvm_context, start_addr, end_addr - start_addr,
1366 buf, NULL, kvm_get_dirty_bitmap_cb);
1367 qemu_free(buf);
1368 #endif
1371 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len)
1373 #ifdef TARGET_I386
1374 if (must_use_aliases_source(phys_addr))
1375 return 0;
1376 #endif
1377 kvm_qemu_log_memory(phys_addr, len, 1);
1378 return 0;
1381 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len)
1383 #ifdef TARGET_I386
1384 if (must_use_aliases_source(phys_addr))
1385 return 0;
1386 #endif
1387 kvm_qemu_log_memory(phys_addr, len, 0);
1388 return 0;
1391 /* hack: both libkvm and upstream qemu define kvm_has_sync_mmu(), differently */
1392 #undef kvm_has_sync_mmu
1393 int qemu_kvm_has_sync_mmu(void)
1395 return kvm_has_sync_mmu(kvm_context);
1398 void qemu_kvm_cpu_stop(CPUState *env)
1400 if (kvm_enabled())
1401 env->kvm_cpu_state.stopped = 1;