Make --mem-path memory allocation depend on mmu notifiers
[qemu-kvm/fedora.git] / qemu-kvm.c
blob5ff63adc02a5e6b7601879de8722c36745806d7d
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 struct 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, (int)data);
108 void kvm_inject_interrupt(CPUState *env, int mask)
110 on_vcpu(env, inject_interrupt, (void *)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->interrupt_request & CPU_INTERRUPT_EXIT)
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 static void pause_all_threads(void)
290 CPUState *penv = first_cpu;
292 assert(!cpu_single_env);
294 while (penv) {
295 penv->kvm_cpu_state.stop = 1;
296 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
297 penv = (CPUState *)penv->next_cpu;
300 while (!all_threads_paused())
301 qemu_cond_wait(&qemu_pause_cond);
304 static void resume_all_threads(void)
306 CPUState *penv = first_cpu;
308 assert(!cpu_single_env);
310 while (penv) {
311 penv->kvm_cpu_state.stop = 0;
312 penv->kvm_cpu_state.stopped = 0;
313 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
314 penv = (CPUState *)penv->next_cpu;
318 static void kvm_vm_state_change_handler(void *context, int running)
320 if (running)
321 resume_all_threads();
322 else
323 pause_all_threads();
326 static void update_regs_for_sipi(CPUState *env)
328 kvm_arch_update_regs_for_sipi(env);
329 env->kvm_cpu_state.sipi_needed = 0;
332 static void update_regs_for_init(CPUState *env)
334 #ifdef TARGET_I386
335 SegmentCache cs = env->segs[R_CS];
336 #endif
338 cpu_reset(env);
340 #ifdef TARGET_I386
341 /* restore SIPI vector */
342 if(env->kvm_cpu_state.sipi_needed)
343 env->segs[R_CS] = cs;
344 #endif
346 env->kvm_cpu_state.init = 0;
347 kvm_arch_load_regs(env);
350 static void setup_kernel_sigmask(CPUState *env)
352 sigset_t set;
354 sigemptyset(&set);
355 sigaddset(&set, SIGUSR2);
356 sigaddset(&set, SIGIO);
357 sigaddset(&set, SIGALRM);
358 sigprocmask(SIG_BLOCK, &set, NULL);
360 sigprocmask(SIG_BLOCK, NULL, &set);
361 sigdelset(&set, SIG_IPI);
363 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
366 static void qemu_kvm_system_reset(void)
368 CPUState *penv = first_cpu;
370 pause_all_threads();
372 qemu_system_reset();
374 while (penv) {
375 kvm_arch_cpu_reset(penv);
376 penv = (CPUState *)penv->next_cpu;
379 resume_all_threads();
382 static int kvm_main_loop_cpu(CPUState *env)
384 setup_kernel_sigmask(env);
386 pthread_mutex_lock(&qemu_mutex);
387 if (kvm_irqchip_in_kernel(kvm_context))
388 env->halted = 0;
390 kvm_qemu_init_env(env);
391 #ifdef TARGET_I386
392 kvm_tpr_vcpu_start(env);
393 #endif
395 cpu_single_env = env;
396 kvm_load_registers(env);
398 while (1) {
399 while (!has_work(env))
400 kvm_main_loop_wait(env, 1000);
401 if (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI))
402 env->halted = 0;
403 if (!kvm_irqchip_in_kernel(kvm_context)) {
404 if (env->kvm_cpu_state.init)
405 update_regs_for_init(env);
406 if (env->kvm_cpu_state.sipi_needed)
407 update_regs_for_sipi(env);
409 if (!env->halted && !env->kvm_cpu_state.init)
410 kvm_cpu_exec(env);
411 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
412 kvm_main_loop_wait(env, 0);
414 pthread_mutex_unlock(&qemu_mutex);
415 return 0;
418 static void *ap_main_loop(void *_env)
420 CPUState *env = _env;
421 sigset_t signals;
422 struct ioperm_data *data = NULL;
424 current_env = env;
425 env->thread_id = kvm_get_thread_id();
426 sigfillset(&signals);
427 sigprocmask(SIG_BLOCK, &signals, NULL);
428 kvm_create_vcpu(kvm_context, env->cpu_index);
429 kvm_qemu_init_env(env);
431 #ifdef USE_KVM_DEVICE_ASSIGNMENT
432 /* do ioperm for io ports of assigned devices */
433 LIST_FOREACH(data, &ioperm_head, entries)
434 on_vcpu(env, kvm_arch_do_ioperm, data);
435 #endif
437 /* signal VCPU creation */
438 pthread_mutex_lock(&qemu_mutex);
439 current_env->kvm_cpu_state.created = 1;
440 pthread_cond_signal(&qemu_vcpu_cond);
442 /* and wait for machine initialization */
443 while (!qemu_system_ready)
444 qemu_cond_wait(&qemu_system_cond);
445 pthread_mutex_unlock(&qemu_mutex);
447 kvm_main_loop_cpu(env);
448 return NULL;
451 void kvm_init_vcpu(CPUState *env)
453 pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
455 while (env->kvm_cpu_state.created == 0)
456 qemu_cond_wait(&qemu_vcpu_cond);
459 int kvm_init_ap(void)
461 #ifdef TARGET_I386
462 kvm_tpr_opt_setup();
463 #endif
464 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
466 signal(SIG_IPI, sig_ipi_handler);
467 return 0;
470 void qemu_kvm_notify_work(void)
472 uint64_t value = 1;
473 char buffer[8];
474 size_t offset = 0;
476 if (io_thread_fd == -1)
477 return;
479 memcpy(buffer, &value, sizeof(value));
481 while (offset < 8) {
482 ssize_t len;
484 len = write(io_thread_fd, buffer + offset, 8 - offset);
485 if (len == -1 && errno == EINTR)
486 continue;
488 if (len <= 0)
489 break;
491 offset += len;
494 if (offset != 8)
495 fprintf(stderr, "failed to notify io thread\n");
498 /* If we have signalfd, we mask out the signals we want to handle and then
499 * use signalfd to listen for them. We rely on whatever the current signal
500 * handler is to dispatch the signals when we receive them.
503 static void sigfd_handler(void *opaque)
505 int fd = (unsigned long)opaque;
506 struct qemu_signalfd_siginfo info;
507 struct sigaction action;
508 ssize_t len;
510 while (1) {
511 do {
512 len = read(fd, &info, sizeof(info));
513 } while (len == -1 && errno == EINTR);
515 if (len == -1 && errno == EAGAIN)
516 break;
518 if (len != sizeof(info)) {
519 printf("read from sigfd returned %ld: %m\n", len);
520 return;
523 sigaction(info.ssi_signo, NULL, &action);
524 if (action.sa_handler)
525 action.sa_handler(info.ssi_signo);
530 /* Used to break IO thread out of select */
531 static void io_thread_wakeup(void *opaque)
533 int fd = (unsigned long)opaque;
534 char buffer[8];
535 size_t offset = 0;
537 while (offset < 8) {
538 ssize_t len;
540 len = read(fd, buffer + offset, 8 - offset);
541 if (len == -1 && errno == EINTR)
542 continue;
544 if (len <= 0)
545 break;
547 offset += len;
551 int kvm_main_loop(void)
553 int fds[2];
554 sigset_t mask;
555 int sigfd;
557 io_thread = pthread_self();
558 qemu_system_ready = 1;
560 if (qemu_eventfd(fds) == -1) {
561 fprintf(stderr, "failed to create eventfd\n");
562 return -errno;
565 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
566 (void *)(unsigned long)fds[0]);
568 io_thread_fd = fds[1];
570 sigemptyset(&mask);
571 sigaddset(&mask, SIGIO);
572 sigaddset(&mask, SIGALRM);
573 sigprocmask(SIG_BLOCK, &mask, NULL);
575 sigfd = qemu_signalfd(&mask);
576 if (sigfd == -1) {
577 fprintf(stderr, "failed to create signalfd\n");
578 return -errno;
581 fcntl(sigfd, F_SETFL, O_NONBLOCK);
583 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
584 (void *)(unsigned long)sigfd);
586 pthread_cond_broadcast(&qemu_system_cond);
588 io_thread_sigfd = sigfd;
589 cpu_single_env = NULL;
591 while (1) {
592 main_loop_wait(1000);
593 if (qemu_shutdown_requested())
594 break;
595 else if (qemu_powerdown_requested())
596 qemu_system_powerdown();
597 else if (qemu_reset_requested())
598 qemu_kvm_system_reset();
599 #ifdef CONFIG_GDBSTUB
600 else if (kvm_debug_cpu_requested) {
601 gdb_set_stop_cpu(kvm_debug_cpu_requested);
602 vm_stop(EXCP_DEBUG);
603 kvm_debug_cpu_requested = NULL;
605 #endif
608 pause_all_threads();
609 pthread_mutex_unlock(&qemu_mutex);
611 return 0;
614 #ifdef KVM_CAP_SET_GUEST_DEBUG
615 int kvm_debug(void *opaque, void *data, struct kvm_debug_exit_arch *arch_info)
617 int handle = kvm_arch_debug(arch_info);
618 struct CPUState *env = data;
620 if (handle) {
621 kvm_debug_cpu_requested = env;
622 env->kvm_cpu_state.stopped = 1;
624 return handle;
626 #endif
628 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
630 *data = cpu_inb(0, addr);
631 return 0;
634 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
636 *data = cpu_inw(0, addr);
637 return 0;
640 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
642 *data = cpu_inl(0, addr);
643 return 0;
646 #define PM_IO_BASE 0xb000
648 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
650 if (addr == 0xb2) {
651 switch (data) {
652 case 0: {
653 cpu_outb(0, 0xb3, 0);
654 break;
656 case 0xf0: {
657 unsigned x;
659 /* enable acpi */
660 x = cpu_inw(0, PM_IO_BASE + 4);
661 x &= ~1;
662 cpu_outw(0, PM_IO_BASE + 4, x);
663 break;
665 case 0xf1: {
666 unsigned x;
668 /* enable acpi */
669 x = cpu_inw(0, PM_IO_BASE + 4);
670 x |= 1;
671 cpu_outw(0, PM_IO_BASE + 4, x);
672 break;
674 default:
675 break;
677 return 0;
679 cpu_outb(0, addr, data);
680 return 0;
683 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
685 cpu_outw(0, addr, data);
686 return 0;
689 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
691 cpu_outl(0, addr, data);
692 return 0;
695 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
697 cpu_physical_memory_rw(addr, data, len, 0);
698 return 0;
701 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
703 cpu_physical_memory_rw(addr, data, len, 1);
704 return 0;
707 static int kvm_io_window(void *opaque)
709 return 1;
713 static int kvm_halt(void *opaque, int vcpu)
715 return kvm_arch_halt(opaque, vcpu);
718 static int kvm_shutdown(void *opaque, void *data)
720 struct CPUState *env = (struct CPUState *)data;
722 /* stop the current vcpu from going back to guest mode */
723 env->kvm_cpu_state.stopped = 1;
725 qemu_system_reset_request();
726 return 1;
729 static struct kvm_callbacks qemu_kvm_ops = {
730 #ifdef KVM_CAP_SET_GUEST_DEBUG
731 .debug = kvm_debug,
732 #endif
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 #ifdef KVM_CAP_USER_NMI
746 .push_nmi = kvm_arch_push_nmi,
747 #endif
748 .post_kvm_run = post_kvm_run,
749 .pre_kvm_run = pre_kvm_run,
750 #ifdef TARGET_I386
751 .tpr_access = handle_tpr_access,
752 #endif
753 #ifdef TARGET_PPC
754 .powerpc_dcr_read = handle_powerpc_dcr_read,
755 .powerpc_dcr_write = handle_powerpc_dcr_write,
756 #endif
759 int kvm_qemu_init()
761 /* Try to initialize kvm */
762 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
763 if (!kvm_context) {
764 return -1;
766 pthread_mutex_lock(&qemu_mutex);
768 return 0;
771 #ifdef TARGET_I386
772 static int destroy_region_works = 0;
773 #endif
775 int kvm_qemu_create_context(void)
777 int r;
778 int i;
780 if (!kvm_irqchip) {
781 kvm_disable_irqchip_creation(kvm_context);
783 if (!kvm_pit) {
784 kvm_disable_pit_creation(kvm_context);
786 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
787 kvm_qemu_destroy();
788 return -1;
790 r = kvm_arch_qemu_create_context();
791 if(r <0)
792 kvm_qemu_destroy();
793 if (kvm_pit && !kvm_pit_reinject) {
794 if (kvm_reinject_control(kvm_context, 0)) {
795 fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
796 return -1;
799 #ifdef TARGET_I386
800 destroy_region_works = kvm_destroy_memory_region_works(kvm_context);
801 #endif
803 if (kvm_irqchip && kvm_has_gsi_routing(kvm_context)) {
804 kvm_clear_gsi_routes(kvm_context);
805 for (i = 0; i < 8; ++i) {
806 if (i == 2)
807 continue;
808 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_MASTER, i);
809 if (r < 0)
810 return r;
812 for (i = 8; i < 16; ++i) {
813 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
814 if (r < 0)
815 return r;
817 for (i = 0; i < 24; ++i) {
818 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_IOAPIC, i);
819 if (r < 0)
820 return r;
822 kvm_commit_irq_routes(kvm_context);
824 return 0;
827 void kvm_qemu_destroy(void)
829 kvm_finalize(kvm_context);
832 #ifdef TARGET_I386
833 static int must_use_aliases_source(target_phys_addr_t addr)
835 if (destroy_region_works)
836 return false;
837 if (addr == 0xa0000 || addr == 0xa8000)
838 return true;
839 return false;
842 static int must_use_aliases_target(target_phys_addr_t addr)
844 if (destroy_region_works)
845 return false;
846 if (addr >= 0xe0000000 && addr < 0x100000000ull)
847 return true;
848 return false;
851 static struct mapping {
852 target_phys_addr_t phys;
853 ram_addr_t ram;
854 ram_addr_t len;
855 } mappings[50];
856 static int nr_mappings;
858 static struct mapping *find_ram_mapping(ram_addr_t ram_addr)
860 struct mapping *p;
862 for (p = mappings; p < mappings + nr_mappings; ++p) {
863 if (p->ram <= ram_addr && ram_addr < p->ram + p->len) {
864 return p;
867 return NULL;
870 static struct mapping *find_mapping(target_phys_addr_t start_addr)
872 struct mapping *p;
874 for (p = mappings; p < mappings + nr_mappings; ++p) {
875 if (p->phys <= start_addr && start_addr < p->phys + p->len) {
876 return p;
879 return NULL;
882 static void drop_mapping(target_phys_addr_t start_addr)
884 struct mapping *p = find_mapping(start_addr);
886 if (p)
887 *p = mappings[--nr_mappings];
889 #endif
891 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
892 unsigned long size,
893 unsigned long phys_offset)
895 int r = 0;
896 unsigned long area_flags = phys_offset & ~TARGET_PAGE_MASK;
897 #ifdef TARGET_I386
898 struct mapping *p;
899 #endif
901 phys_offset &= ~IO_MEM_ROM;
903 if (area_flags == IO_MEM_UNASSIGNED) {
904 #ifdef TARGET_I386
905 if (must_use_aliases_source(start_addr)) {
906 kvm_destroy_memory_alias(kvm_context, start_addr);
907 return;
909 if (must_use_aliases_target(start_addr))
910 return;
911 #endif
912 kvm_unregister_memory_area(kvm_context, start_addr, size);
913 return;
916 r = kvm_is_containing_region(kvm_context, start_addr, size);
917 if (r)
918 return;
920 if (area_flags >= TLB_MMIO)
921 return;
923 #ifdef TARGET_I386
924 if (must_use_aliases_source(start_addr)) {
925 p = find_ram_mapping(phys_offset);
926 if (p) {
927 kvm_create_memory_alias(kvm_context, start_addr, size,
928 p->phys + (phys_offset - p->ram));
930 return;
932 #endif
934 r = kvm_register_phys_mem(kvm_context, start_addr,
935 phys_ram_base + phys_offset,
936 size, 0);
937 if (r < 0) {
938 printf("kvm_cpu_register_physical_memory: failed\n");
939 exit(1);
942 #ifdef TARGET_I386
943 drop_mapping(start_addr);
944 p = &mappings[nr_mappings++];
945 p->phys = start_addr;
946 p->ram = phys_offset;
947 p->len = size;
948 #endif
950 return;
953 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
954 target_phys_addr_t size,
955 unsigned long phys_offset)
957 kvm_unregister_memory_area(kvm_context, start_addr, size);
960 int kvm_setup_guest_memory(void *area, unsigned long size)
962 int ret = 0;
964 #ifdef MADV_DONTFORK
965 if (kvm_enabled() && !kvm_has_sync_mmu())
966 ret = madvise(area, size, MADV_DONTFORK);
967 #endif
969 if (ret)
970 perror ("madvise");
972 return ret;
975 int kvm_qemu_check_extension(int ext)
977 return kvm_check_extension(kvm_context, ext);
980 int kvm_qemu_init_env(CPUState *cenv)
982 return kvm_arch_qemu_init_env(cenv);
985 #ifdef KVM_CAP_SET_GUEST_DEBUG
986 struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
987 TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);
989 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc)
991 struct kvm_sw_breakpoint *bp;
993 TAILQ_FOREACH(bp, &kvm_sw_breakpoints, entry) {
994 if (bp->pc == pc)
995 return bp;
997 return NULL;
1000 struct kvm_set_guest_debug_data {
1001 struct kvm_guest_debug dbg;
1002 int err;
1005 void kvm_invoke_set_guest_debug(void *data)
1007 struct kvm_set_guest_debug_data *dbg_data = data;
1009 dbg_data->err = kvm_set_guest_debug(kvm_context, cpu_single_env->cpu_index,
1010 &dbg_data->dbg);
1013 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1015 struct kvm_set_guest_debug_data data;
1017 data.dbg.control = 0;
1018 if (env->singlestep_enabled)
1019 data.dbg.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1021 kvm_arch_update_guest_debug(env, &data.dbg);
1022 data.dbg.control |= reinject_trap;
1024 on_vcpu(env, kvm_invoke_set_guest_debug, &data);
1025 return data.err;
1028 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1029 target_ulong len, int type)
1031 struct kvm_sw_breakpoint *bp;
1032 CPUState *env;
1033 int err;
1035 if (type == GDB_BREAKPOINT_SW) {
1036 bp = kvm_find_sw_breakpoint(addr);
1037 if (bp) {
1038 bp->use_count++;
1039 return 0;
1042 bp = qemu_malloc(sizeof(struct kvm_sw_breakpoint));
1043 if (!bp)
1044 return -ENOMEM;
1046 bp->pc = addr;
1047 bp->use_count = 1;
1048 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1049 if (err) {
1050 free(bp);
1051 return err;
1054 TAILQ_INSERT_HEAD(&kvm_sw_breakpoints, bp, entry);
1055 } else {
1056 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1057 if (err)
1058 return err;
1061 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1062 err = kvm_update_guest_debug(env, 0);
1063 if (err)
1064 return err;
1066 return 0;
1069 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1070 target_ulong len, int type)
1072 struct kvm_sw_breakpoint *bp;
1073 CPUState *env;
1074 int err;
1076 if (type == GDB_BREAKPOINT_SW) {
1077 bp = kvm_find_sw_breakpoint(addr);
1078 if (!bp)
1079 return -ENOENT;
1081 if (bp->use_count > 1) {
1082 bp->use_count--;
1083 return 0;
1086 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1087 if (err)
1088 return err;
1090 TAILQ_REMOVE(&kvm_sw_breakpoints, bp, entry);
1091 qemu_free(bp);
1092 } else {
1093 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1094 if (err)
1095 return err;
1098 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1099 err = kvm_update_guest_debug(env, 0);
1100 if (err)
1101 return err;
1103 return 0;
1106 void kvm_remove_all_breakpoints(CPUState *current_env)
1108 struct kvm_sw_breakpoint *bp, *next;
1109 CPUState *env;
1111 TAILQ_FOREACH_SAFE(bp, &kvm_sw_breakpoints, entry, next) {
1112 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1113 /* Try harder to find a CPU that currently sees the breakpoint. */
1114 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1115 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0)
1116 break;
1120 kvm_arch_remove_all_hw_breakpoints();
1122 for (env = first_cpu; env != NULL; env = env->next_cpu)
1123 kvm_update_guest_debug(env, 0);
1126 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1128 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1130 return -EINVAL;
1133 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1134 target_ulong len, int type)
1136 return -EINVAL;
1139 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1140 target_ulong len, int type)
1142 return -EINVAL;
1145 void kvm_remove_all_breakpoints(CPUState *current_env)
1148 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1151 * dirty pages logging
1153 /* FIXME: use unsigned long pointer instead of unsigned char */
1154 unsigned char *kvm_dirty_bitmap = NULL;
1155 int kvm_physical_memory_set_dirty_tracking(int enable)
1157 int r = 0;
1159 if (!kvm_enabled())
1160 return 0;
1162 if (enable) {
1163 if (!kvm_dirty_bitmap) {
1164 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
1165 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
1166 if (kvm_dirty_bitmap == NULL) {
1167 perror("Failed to allocate dirty pages bitmap");
1168 r=-1;
1170 else {
1171 r = kvm_dirty_pages_log_enable_all(kvm_context);
1175 else {
1176 if (kvm_dirty_bitmap) {
1177 r = kvm_dirty_pages_log_reset(kvm_context);
1178 qemu_free(kvm_dirty_bitmap);
1179 kvm_dirty_bitmap = NULL;
1182 return r;
1185 /* get kvm's dirty pages bitmap and update qemu's */
1186 static int kvm_get_dirty_pages_log_range(unsigned long start_addr,
1187 unsigned char *bitmap,
1188 unsigned int offset,
1189 unsigned long mem_size)
1191 unsigned int i, j, n=0;
1192 unsigned char c;
1193 unsigned long page_number, addr, addr1;
1194 ram_addr_t ram_addr;
1195 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
1198 * bitmap-traveling is faster than memory-traveling (for addr...)
1199 * especially when most of the memory is not dirty.
1201 for (i=0; i<len; i++) {
1202 c = bitmap[i];
1203 while (c>0) {
1204 j = ffsl(c) - 1;
1205 c &= ~(1u<<j);
1206 page_number = i * 8 + j;
1207 addr1 = page_number * TARGET_PAGE_SIZE;
1208 addr = offset + addr1;
1209 ram_addr = cpu_get_physical_page_desc(addr);
1210 cpu_physical_memory_set_dirty(ram_addr);
1211 n++;
1214 return 0;
1216 static int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
1217 void *bitmap, void *opaque)
1219 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
1223 * get kvm's dirty pages bitmap and update qemu's
1224 * we only care about physical ram, which resides in slots 0 and 3
1226 int kvm_update_dirty_pages_log(void)
1228 int r = 0;
1231 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
1232 kvm_dirty_bitmap, NULL,
1233 kvm_get_dirty_bitmap_cb);
1234 return r;
1237 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
1238 int log)
1240 if (log)
1241 kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
1242 else {
1243 #ifdef TARGET_I386
1244 if (must_use_aliases_target(start))
1245 return;
1246 #endif
1247 kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
1251 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
1253 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
1254 unsigned int brsize = BITMAP_SIZE(ram_size);
1255 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
1256 unsigned int extra_bytes = (extra_pages +7)/8;
1257 unsigned int hole_start = BITMAP_SIZE(0xa0000);
1258 unsigned int hole_end = BITMAP_SIZE(0xc0000);
1260 memset(bitmap, 0xFF, brsize + extra_bytes);
1261 memset(bitmap + hole_start, 0, hole_end - hole_start);
1262 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
1264 return 0;
1267 #ifdef KVM_CAP_IRQCHIP
1269 int kvm_set_irq(int irq, int level)
1271 return kvm_set_irq_level(kvm_context, irq, level);
1274 #endif
1276 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1278 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1281 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1282 unsigned long size, int log, int writable)
1284 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1287 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1288 unsigned long size)
1290 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1293 void kvm_mutex_unlock(void)
1295 assert(!cpu_single_env);
1296 pthread_mutex_unlock(&qemu_mutex);
1299 void kvm_mutex_lock(void)
1301 pthread_mutex_lock(&qemu_mutex);
1302 cpu_single_env = NULL;
1305 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1307 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1310 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1311 unsigned int size)
1313 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);
1316 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1318 return kvm_register_coalesced_mmio(kvm_context, start, size);
1321 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1323 return kvm_unregister_coalesced_mmio(kvm_context, start, size);
1326 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1327 void kvm_add_ioperm_data(struct ioperm_data *data)
1329 LIST_INSERT_HEAD(&ioperm_head, data, entries);
1332 void kvm_ioperm(CPUState *env, void *data)
1334 if (kvm_enabled() && qemu_system_ready)
1335 on_vcpu(env, kvm_arch_do_ioperm, data);
1338 #endif
1340 void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
1342 #ifndef TARGET_IA64
1343 void *buf;
1345 #ifdef TARGET_I386
1346 if (must_use_aliases_source(start_addr))
1347 return;
1348 #endif
1350 buf = qemu_malloc((end_addr - start_addr) / 8 + 2);
1351 kvm_get_dirty_pages_range(kvm_context, start_addr, end_addr - start_addr,
1352 buf, NULL, kvm_get_dirty_bitmap_cb);
1353 qemu_free(buf);
1354 #endif
1357 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len)
1359 #ifdef TARGET_I386
1360 if (must_use_aliases_source(phys_addr))
1361 return 0;
1362 #endif
1363 kvm_qemu_log_memory(phys_addr, len, 1);
1364 return 0;
1367 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len)
1369 #ifdef TARGET_I386
1370 if (must_use_aliases_source(phys_addr))
1371 return 0;
1372 #endif
1373 kvm_qemu_log_memory(phys_addr, len, 0);
1374 return 0;
1377 /* hack: both libkvm and upstream qemu define kvm_has_sync_mmu(), differently */
1378 #undef kvm_has_sync_mmu
1379 int qemu_kvm_has_sync_mmu(void)
1381 return kvm_has_sync_mmu(kvm_context);
1384 void qemu_kvm_cpu_stop(CPUState *env)
1386 if (kvm_enabled())
1387 env->kvm_cpu_state.stopped = 1;