Pull qemu headers into libkvm
[qemu-kvm/fedora.git] / qemu-kvm.c
blob2aeb17c344a370872ca0d5cf73a8b6f261a0d3d5
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-all.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 static uint64_t phys_ram_size;
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, (long)data);
110 void kvm_inject_interrupt(CPUState *env, int mask)
112 on_vcpu(env, inject_interrupt, (void *)(long)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 kvm_try_push_interrupts(void *opaque)
154 return kvm_arch_try_push_interrupts(opaque);
157 static void kvm_post_run(void *opaque, void *data)
159 CPUState *env = (CPUState *)data;
161 pthread_mutex_lock(&qemu_mutex);
162 kvm_arch_post_kvm_run(opaque, env);
165 static int kvm_pre_run(void *opaque, void *data)
167 CPUState *env = (CPUState *)data;
169 kvm_arch_pre_kvm_run(opaque, env);
171 if (env->exit_request)
172 return 1;
173 pthread_mutex_unlock(&qemu_mutex);
174 return 0;
177 static void kvm_do_load_registers(void *_env)
179 CPUState *env = _env;
181 kvm_arch_load_regs(env);
184 void kvm_load_registers(CPUState *env)
186 if (kvm_enabled() && qemu_system_ready)
187 on_vcpu(env, kvm_do_load_registers, env);
190 static void kvm_do_save_registers(void *_env)
192 CPUState *env = _env;
194 kvm_arch_save_regs(env);
197 void kvm_save_registers(CPUState *env)
199 if (kvm_enabled())
200 on_vcpu(env, kvm_do_save_registers, env);
203 int kvm_cpu_exec(CPUState *env)
205 int r;
207 r = kvm_run(env->kvm_cpu_state.vcpu_ctx, env);
208 if (r < 0) {
209 printf("kvm_run returned %d\n", r);
210 vm_stop(0);
213 return 0;
216 static int has_work(CPUState *env)
218 if (!vm_running || (env && env->kvm_cpu_state.stopped))
219 return 0;
220 if (!env->halted)
221 return 1;
222 return kvm_arch_has_work(env);
225 static void flush_queued_work(CPUState *env)
227 struct qemu_work_item *wi;
229 if (!env->kvm_cpu_state.queued_work_first)
230 return;
232 while ((wi = env->kvm_cpu_state.queued_work_first)) {
233 env->kvm_cpu_state.queued_work_first = wi->next;
234 wi->func(wi->data);
235 wi->done = true;
237 env->kvm_cpu_state.queued_work_last = NULL;
238 pthread_cond_broadcast(&qemu_work_cond);
241 static void kvm_main_loop_wait(CPUState *env, int timeout)
243 struct timespec ts;
244 int r, e;
245 siginfo_t siginfo;
246 sigset_t waitset;
248 pthread_mutex_unlock(&qemu_mutex);
250 ts.tv_sec = timeout / 1000;
251 ts.tv_nsec = (timeout % 1000) * 1000000;
252 sigemptyset(&waitset);
253 sigaddset(&waitset, SIG_IPI);
255 r = sigtimedwait(&waitset, &siginfo, &ts);
256 e = errno;
258 pthread_mutex_lock(&qemu_mutex);
260 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
261 printf("sigtimedwait: %s\n", strerror(e));
262 exit(1);
265 cpu_single_env = env;
266 flush_queued_work(env);
268 if (env->kvm_cpu_state.stop) {
269 env->kvm_cpu_state.stop = 0;
270 env->kvm_cpu_state.stopped = 1;
271 pthread_cond_signal(&qemu_pause_cond);
274 env->kvm_cpu_state.signalled = 0;
277 static int all_threads_paused(void)
279 CPUState *penv = first_cpu;
281 while (penv) {
282 if (penv->kvm_cpu_state.stop)
283 return 0;
284 penv = (CPUState *)penv->next_cpu;
287 return 1;
290 static void pause_all_threads(void)
292 CPUState *penv = first_cpu;
294 while (penv) {
295 if (penv != cpu_single_env) {
296 penv->kvm_cpu_state.stop = 1;
297 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
298 } else {
299 penv->kvm_cpu_state.stop = 0;
300 penv->kvm_cpu_state.stopped = 1;
301 cpu_exit(penv);
303 penv = (CPUState *)penv->next_cpu;
306 while (!all_threads_paused())
307 qemu_cond_wait(&qemu_pause_cond);
310 static void resume_all_threads(void)
312 CPUState *penv = first_cpu;
314 assert(!cpu_single_env);
316 while (penv) {
317 penv->kvm_cpu_state.stop = 0;
318 penv->kvm_cpu_state.stopped = 0;
319 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
320 penv = (CPUState *)penv->next_cpu;
324 static void kvm_vm_state_change_handler(void *context, int running, int reason)
326 if (running)
327 resume_all_threads();
328 else
329 pause_all_threads();
332 static void update_regs_for_sipi(CPUState *env)
334 kvm_arch_update_regs_for_sipi(env);
335 env->kvm_cpu_state.sipi_needed = 0;
338 static void update_regs_for_init(CPUState *env)
340 #ifdef TARGET_I386
341 SegmentCache cs = env->segs[R_CS];
342 #endif
344 cpu_reset(env);
346 #ifdef TARGET_I386
347 /* restore SIPI vector */
348 if(env->kvm_cpu_state.sipi_needed)
349 env->segs[R_CS] = cs;
350 #endif
352 env->kvm_cpu_state.init = 0;
353 kvm_arch_load_regs(env);
356 static void setup_kernel_sigmask(CPUState *env)
358 sigset_t set;
360 sigemptyset(&set);
361 sigaddset(&set, SIGUSR2);
362 sigaddset(&set, SIGIO);
363 sigaddset(&set, SIGALRM);
364 sigprocmask(SIG_BLOCK, &set, NULL);
366 sigprocmask(SIG_BLOCK, NULL, &set);
367 sigdelset(&set, SIG_IPI);
369 kvm_set_signal_mask(env->kvm_cpu_state.vcpu_ctx, &set);
372 static void qemu_kvm_system_reset(void)
374 CPUState *penv = first_cpu;
376 pause_all_threads();
378 qemu_system_reset();
380 while (penv) {
381 kvm_arch_cpu_reset(penv);
382 penv = (CPUState *)penv->next_cpu;
385 resume_all_threads();
388 static int kvm_main_loop_cpu(CPUState *env)
390 setup_kernel_sigmask(env);
392 pthread_mutex_lock(&qemu_mutex);
393 if (kvm_irqchip_in_kernel(kvm_context))
394 env->halted = 0;
396 kvm_qemu_init_env(env);
397 #ifdef TARGET_I386
398 kvm_tpr_vcpu_start(env);
399 #endif
401 cpu_single_env = env;
402 kvm_load_registers(env);
404 while (1) {
405 while (!has_work(env))
406 kvm_main_loop_wait(env, 1000);
407 if (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI))
408 env->halted = 0;
409 if (!kvm_irqchip_in_kernel(kvm_context)) {
410 if (env->kvm_cpu_state.init)
411 update_regs_for_init(env);
412 if (env->kvm_cpu_state.sipi_needed)
413 update_regs_for_sipi(env);
415 if (!env->halted && !env->kvm_cpu_state.init)
416 kvm_cpu_exec(env);
417 env->exit_request = 0;
418 env->exception_index = EXCP_INTERRUPT;
419 kvm_main_loop_wait(env, 0);
421 pthread_mutex_unlock(&qemu_mutex);
422 return 0;
425 static void *ap_main_loop(void *_env)
427 CPUState *env = _env;
428 sigset_t signals;
429 struct ioperm_data *data = NULL;
431 current_env = env;
432 env->thread_id = kvm_get_thread_id();
433 sigfillset(&signals);
434 sigprocmask(SIG_BLOCK, &signals, NULL);
435 env->kvm_cpu_state.vcpu_ctx = kvm_create_vcpu(kvm_context, env->cpu_index);
437 #ifdef USE_KVM_DEVICE_ASSIGNMENT
438 /* do ioperm for io ports of assigned devices */
439 LIST_FOREACH(data, &ioperm_head, entries)
440 on_vcpu(env, kvm_arch_do_ioperm, data);
441 #endif
443 /* signal VCPU creation */
444 pthread_mutex_lock(&qemu_mutex);
445 current_env->kvm_cpu_state.created = 1;
446 pthread_cond_signal(&qemu_vcpu_cond);
448 /* and wait for machine initialization */
449 while (!qemu_system_ready)
450 qemu_cond_wait(&qemu_system_cond);
451 pthread_mutex_unlock(&qemu_mutex);
453 kvm_main_loop_cpu(env);
454 return NULL;
457 void kvm_init_vcpu(CPUState *env)
459 pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
461 while (env->kvm_cpu_state.created == 0)
462 qemu_cond_wait(&qemu_vcpu_cond);
465 int kvm_vcpu_inited(CPUState *env)
467 return env->kvm_cpu_state.created;
470 int kvm_init_ap(void)
472 #ifdef TARGET_I386
473 kvm_tpr_opt_setup();
474 #endif
475 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
477 signal(SIG_IPI, sig_ipi_handler);
478 return 0;
481 void qemu_kvm_notify_work(void)
483 uint64_t value = 1;
484 char buffer[8];
485 size_t offset = 0;
487 if (io_thread_fd == -1)
488 return;
490 memcpy(buffer, &value, sizeof(value));
492 while (offset < 8) {
493 ssize_t len;
495 len = write(io_thread_fd, buffer + offset, 8 - offset);
496 if (len == -1 && errno == EINTR)
497 continue;
499 if (len <= 0)
500 break;
502 offset += len;
505 if (offset != 8)
506 fprintf(stderr, "failed to notify io thread\n");
509 /* If we have signalfd, we mask out the signals we want to handle and then
510 * use signalfd to listen for them. We rely on whatever the current signal
511 * handler is to dispatch the signals when we receive them.
514 static void sigfd_handler(void *opaque)
516 int fd = (unsigned long)opaque;
517 struct qemu_signalfd_siginfo info;
518 struct sigaction action;
519 ssize_t len;
521 while (1) {
522 do {
523 len = read(fd, &info, sizeof(info));
524 } while (len == -1 && errno == EINTR);
526 if (len == -1 && errno == EAGAIN)
527 break;
529 if (len != sizeof(info)) {
530 printf("read from sigfd returned %zd: %m\n", len);
531 return;
534 sigaction(info.ssi_signo, NULL, &action);
535 if (action.sa_handler)
536 action.sa_handler(info.ssi_signo);
541 /* Used to break IO thread out of select */
542 static void io_thread_wakeup(void *opaque)
544 int fd = (unsigned long)opaque;
545 char buffer[8];
546 size_t offset = 0;
548 while (offset < 8) {
549 ssize_t len;
551 len = read(fd, buffer + offset, 8 - offset);
552 if (len == -1 && errno == EINTR)
553 continue;
555 if (len <= 0)
556 break;
558 offset += len;
562 int kvm_main_loop(void)
564 int fds[2];
565 sigset_t mask;
566 int sigfd;
568 io_thread = pthread_self();
569 qemu_system_ready = 1;
571 if (qemu_eventfd(fds) == -1) {
572 fprintf(stderr, "failed to create eventfd\n");
573 return -errno;
576 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
577 (void *)(unsigned long)fds[0]);
579 io_thread_fd = fds[1];
581 sigemptyset(&mask);
582 sigaddset(&mask, SIGIO);
583 sigaddset(&mask, SIGALRM);
584 sigprocmask(SIG_BLOCK, &mask, NULL);
586 sigfd = qemu_signalfd(&mask);
587 if (sigfd == -1) {
588 fprintf(stderr, "failed to create signalfd\n");
589 return -errno;
592 fcntl(sigfd, F_SETFL, O_NONBLOCK);
594 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
595 (void *)(unsigned long)sigfd);
597 pthread_cond_broadcast(&qemu_system_cond);
599 io_thread_sigfd = sigfd;
600 cpu_single_env = NULL;
602 while (1) {
603 main_loop_wait(1000);
604 if (qemu_shutdown_requested()) {
605 if (qemu_no_shutdown()) {
606 vm_stop(0);
607 } else
608 break;
609 } else if (qemu_powerdown_requested())
610 qemu_system_powerdown();
611 else if (qemu_reset_requested())
612 qemu_kvm_system_reset();
613 else if (kvm_debug_cpu_requested) {
614 gdb_set_stop_cpu(kvm_debug_cpu_requested);
615 vm_stop(EXCP_DEBUG);
616 kvm_debug_cpu_requested = NULL;
620 pause_all_threads();
621 pthread_mutex_unlock(&qemu_mutex);
623 return 0;
626 #ifdef KVM_CAP_SET_GUEST_DEBUG
627 static int kvm_debug(void *opaque, void *data,
628 struct kvm_debug_exit_arch *arch_info)
630 int handle = kvm_arch_debug(arch_info);
631 CPUState *env = data;
633 if (handle) {
634 kvm_debug_cpu_requested = env;
635 env->kvm_cpu_state.stopped = 1;
637 return handle;
639 #endif
641 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
643 *data = cpu_inb(0, addr);
644 return 0;
647 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
649 *data = cpu_inw(0, addr);
650 return 0;
653 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
655 *data = cpu_inl(0, addr);
656 return 0;
659 #define PM_IO_BASE 0xb000
661 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
663 if (addr == 0xb2) {
664 switch (data) {
665 case 0: {
666 cpu_outb(0, 0xb3, 0);
667 break;
669 case 0xf0: {
670 unsigned x;
672 /* enable acpi */
673 x = cpu_inw(0, PM_IO_BASE + 4);
674 x &= ~1;
675 cpu_outw(0, PM_IO_BASE + 4, x);
676 break;
678 case 0xf1: {
679 unsigned x;
681 /* enable acpi */
682 x = cpu_inw(0, PM_IO_BASE + 4);
683 x |= 1;
684 cpu_outw(0, PM_IO_BASE + 4, x);
685 break;
687 default:
688 break;
690 return 0;
692 cpu_outb(0, addr, data);
693 return 0;
696 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
698 cpu_outw(0, addr, data);
699 return 0;
702 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
704 cpu_outl(0, addr, data);
705 return 0;
708 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
710 cpu_physical_memory_rw(addr, data, len, 0);
711 return 0;
714 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
716 cpu_physical_memory_rw(addr, data, len, 1);
717 return 0;
720 static int kvm_io_window(void *opaque)
722 return 1;
726 static int kvm_halt(void *opaque, kvm_vcpu_context_t vcpu)
728 return kvm_arch_halt(opaque, vcpu);
731 static int kvm_shutdown(void *opaque, void *data)
733 CPUState *env = (CPUState *)data;
735 /* stop the current vcpu from going back to guest mode */
736 env->kvm_cpu_state.stopped = 1;
738 qemu_system_reset_request();
739 return 1;
742 static int handle_unhandled(kvm_context_t kvm, kvm_vcpu_context_t vcpu,
743 uint64_t reason)
745 fprintf(stderr, "kvm: unhandled exit %"PRIx64"\n", reason);
746 return -EINVAL;
749 static struct kvm_callbacks qemu_kvm_ops = {
750 #ifdef KVM_CAP_SET_GUEST_DEBUG
751 .debug = kvm_debug,
752 #endif
753 .inb = kvm_inb,
754 .inw = kvm_inw,
755 .inl = kvm_inl,
756 .outb = kvm_outb,
757 .outw = kvm_outw,
758 .outl = kvm_outl,
759 .mmio_read = kvm_mmio_read,
760 .mmio_write = kvm_mmio_write,
761 .halt = kvm_halt,
762 .shutdown = kvm_shutdown,
763 .io_window = kvm_io_window,
764 .try_push_interrupts = kvm_try_push_interrupts,
765 #ifdef KVM_CAP_USER_NMI
766 .push_nmi = kvm_arch_push_nmi,
767 #endif
768 .post_kvm_run = kvm_post_run,
769 .pre_kvm_run = kvm_pre_run,
770 #ifdef TARGET_I386
771 .tpr_access = handle_tpr_access,
772 #endif
773 #ifdef TARGET_PPC
774 .powerpc_dcr_read = handle_powerpc_dcr_read,
775 .powerpc_dcr_write = handle_powerpc_dcr_write,
776 #endif
777 .unhandled = handle_unhandled,
780 int kvm_qemu_init()
782 /* Try to initialize kvm */
783 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
784 if (!kvm_context) {
785 return -1;
787 pthread_mutex_lock(&qemu_mutex);
789 return 0;
792 #ifdef TARGET_I386
793 static int destroy_region_works = 0;
794 #endif
797 #if !defined(TARGET_I386)
798 int kvm_arch_init_irq_routing(void)
800 return 0;
802 #endif
804 int kvm_qemu_create_context(void)
806 int r;
808 if (!kvm_irqchip) {
809 kvm_disable_irqchip_creation(kvm_context);
811 if (!kvm_pit) {
812 kvm_disable_pit_creation(kvm_context);
814 if (kvm_create(kvm_context, 0, NULL) < 0) {
815 kvm_qemu_destroy();
816 return -1;
818 r = kvm_arch_qemu_create_context();
819 if(r <0)
820 kvm_qemu_destroy();
821 if (kvm_pit && !kvm_pit_reinject) {
822 if (kvm_reinject_control(kvm_context, 0)) {
823 fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
824 return -1;
827 #ifdef TARGET_I386
828 destroy_region_works = kvm_destroy_memory_region_works(kvm_context);
829 #endif
831 r = kvm_arch_init_irq_routing();
832 if (r < 0) {
833 return r;
836 return 0;
839 void kvm_qemu_destroy(void)
841 kvm_finalize(kvm_context);
844 #ifdef TARGET_I386
845 static int must_use_aliases_source(target_phys_addr_t addr)
847 if (destroy_region_works)
848 return false;
849 if (addr == 0xa0000 || addr == 0xa8000)
850 return true;
851 return false;
854 static int must_use_aliases_target(target_phys_addr_t addr)
856 if (destroy_region_works)
857 return false;
858 if (addr >= 0xe0000000 && addr < 0x100000000ull)
859 return true;
860 return false;
863 static struct mapping {
864 target_phys_addr_t phys;
865 ram_addr_t ram;
866 ram_addr_t len;
867 } mappings[50];
868 static int nr_mappings;
870 static struct mapping *find_ram_mapping(ram_addr_t ram_addr)
872 struct mapping *p;
874 for (p = mappings; p < mappings + nr_mappings; ++p) {
875 if (p->ram <= ram_addr && ram_addr < p->ram + p->len) {
876 return p;
879 return NULL;
882 static struct mapping *find_mapping(target_phys_addr_t start_addr)
884 struct mapping *p;
886 for (p = mappings; p < mappings + nr_mappings; ++p) {
887 if (p->phys <= start_addr && start_addr < p->phys + p->len) {
888 return p;
891 return NULL;
894 static void drop_mapping(target_phys_addr_t start_addr)
896 struct mapping *p = find_mapping(start_addr);
898 if (p)
899 *p = mappings[--nr_mappings];
901 #endif
903 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
904 unsigned long size,
905 unsigned long phys_offset)
907 int r = 0;
908 unsigned long area_flags;
909 #ifdef TARGET_I386
910 struct mapping *p;
911 #endif
913 if (start_addr + size > phys_ram_size) {
914 phys_ram_size = start_addr + size;
917 phys_offset &= ~IO_MEM_ROM;
918 area_flags = phys_offset & ~TARGET_PAGE_MASK;
920 if (area_flags != IO_MEM_RAM) {
921 #ifdef TARGET_I386
922 if (must_use_aliases_source(start_addr)) {
923 kvm_destroy_memory_alias(kvm_context, start_addr);
924 return;
926 if (must_use_aliases_target(start_addr))
927 return;
928 #endif
929 while (size > 0) {
930 p = find_mapping(start_addr);
931 if (p) {
932 kvm_unregister_memory_area(kvm_context, p->phys, p->len);
933 drop_mapping(p->phys);
935 start_addr += TARGET_PAGE_SIZE;
936 if (size > TARGET_PAGE_SIZE) {
937 size -= TARGET_PAGE_SIZE;
938 } else {
939 size = 0;
942 return;
945 r = kvm_is_containing_region(kvm_context, start_addr, size);
946 if (r)
947 return;
949 if (area_flags >= TLB_MMIO)
950 return;
952 #ifdef TARGET_I386
953 if (must_use_aliases_source(start_addr)) {
954 p = find_ram_mapping(phys_offset);
955 if (p) {
956 kvm_create_memory_alias(kvm_context, start_addr, size,
957 p->phys + (phys_offset - p->ram));
959 return;
961 #endif
963 r = kvm_register_phys_mem(kvm_context, start_addr,
964 qemu_get_ram_ptr(phys_offset),
965 size, 0);
966 if (r < 0) {
967 printf("kvm_cpu_register_physical_memory: failed\n");
968 exit(1);
971 #ifdef TARGET_I386
972 drop_mapping(start_addr);
973 p = &mappings[nr_mappings++];
974 p->phys = start_addr;
975 p->ram = phys_offset;
976 p->len = size;
977 #endif
979 return;
982 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
983 target_phys_addr_t size,
984 unsigned long phys_offset)
986 kvm_unregister_memory_area(kvm_context, start_addr, size);
989 int kvm_setup_guest_memory(void *area, unsigned long size)
991 int ret = 0;
993 #ifdef MADV_DONTFORK
994 if (kvm_enabled() && !kvm_has_sync_mmu())
995 ret = madvise(area, size, MADV_DONTFORK);
996 #endif
998 if (ret)
999 perror ("madvise");
1001 return ret;
1004 int kvm_qemu_check_extension(int ext)
1006 return kvm_check_extension(kvm_context, ext);
1009 int kvm_qemu_init_env(CPUState *cenv)
1011 return kvm_arch_qemu_init_env(cenv);
1014 #ifdef KVM_CAP_SET_GUEST_DEBUG
1015 struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
1016 TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);
1018 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc)
1020 struct kvm_sw_breakpoint *bp;
1022 TAILQ_FOREACH(bp, &kvm_sw_breakpoints, entry) {
1023 if (bp->pc == pc)
1024 return bp;
1026 return NULL;
1029 struct kvm_set_guest_debug_data {
1030 struct kvm_guest_debug dbg;
1031 int err;
1034 static void kvm_invoke_set_guest_debug(void *data)
1036 struct kvm_set_guest_debug_data *dbg_data = data;
1038 dbg_data->err = kvm_set_guest_debug(cpu_single_env->kvm_cpu_state.vcpu_ctx,
1039 &dbg_data->dbg);
1042 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1044 struct kvm_set_guest_debug_data data;
1046 data.dbg.control = 0;
1047 if (env->singlestep_enabled)
1048 data.dbg.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1050 kvm_arch_update_guest_debug(env, &data.dbg);
1051 data.dbg.control |= reinject_trap;
1053 on_vcpu(env, kvm_invoke_set_guest_debug, &data);
1054 return data.err;
1057 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1058 target_ulong len, int type)
1060 struct kvm_sw_breakpoint *bp;
1061 CPUState *env;
1062 int err;
1064 if (type == GDB_BREAKPOINT_SW) {
1065 bp = kvm_find_sw_breakpoint(addr);
1066 if (bp) {
1067 bp->use_count++;
1068 return 0;
1071 bp = qemu_malloc(sizeof(struct kvm_sw_breakpoint));
1072 if (!bp)
1073 return -ENOMEM;
1075 bp->pc = addr;
1076 bp->use_count = 1;
1077 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1078 if (err) {
1079 free(bp);
1080 return err;
1083 TAILQ_INSERT_HEAD(&kvm_sw_breakpoints, bp, entry);
1084 } else {
1085 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1086 if (err)
1087 return err;
1090 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1091 err = kvm_update_guest_debug(env, 0);
1092 if (err)
1093 return err;
1095 return 0;
1098 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1099 target_ulong len, int type)
1101 struct kvm_sw_breakpoint *bp;
1102 CPUState *env;
1103 int err;
1105 if (type == GDB_BREAKPOINT_SW) {
1106 bp = kvm_find_sw_breakpoint(addr);
1107 if (!bp)
1108 return -ENOENT;
1110 if (bp->use_count > 1) {
1111 bp->use_count--;
1112 return 0;
1115 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1116 if (err)
1117 return err;
1119 TAILQ_REMOVE(&kvm_sw_breakpoints, bp, entry);
1120 qemu_free(bp);
1121 } else {
1122 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1123 if (err)
1124 return err;
1127 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1128 err = kvm_update_guest_debug(env, 0);
1129 if (err)
1130 return err;
1132 return 0;
1135 void kvm_remove_all_breakpoints(CPUState *current_env)
1137 struct kvm_sw_breakpoint *bp, *next;
1138 CPUState *env;
1140 TAILQ_FOREACH_SAFE(bp, &kvm_sw_breakpoints, entry, next) {
1141 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1142 /* Try harder to find a CPU that currently sees the breakpoint. */
1143 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1144 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0)
1145 break;
1149 kvm_arch_remove_all_hw_breakpoints();
1151 for (env = first_cpu; env != NULL; env = env->next_cpu)
1152 kvm_update_guest_debug(env, 0);
1155 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1157 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1159 return -EINVAL;
1162 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1163 target_ulong len, int type)
1165 return -EINVAL;
1168 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1169 target_ulong len, int type)
1171 return -EINVAL;
1174 void kvm_remove_all_breakpoints(CPUState *current_env)
1177 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1180 * dirty pages logging
1182 /* FIXME: use unsigned long pointer instead of unsigned char */
1183 unsigned char *kvm_dirty_bitmap = NULL;
1184 int kvm_physical_memory_set_dirty_tracking(int enable)
1186 int r = 0;
1188 if (!kvm_enabled())
1189 return 0;
1191 if (enable) {
1192 if (!kvm_dirty_bitmap) {
1193 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
1194 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
1195 if (kvm_dirty_bitmap == NULL) {
1196 perror("Failed to allocate dirty pages bitmap");
1197 r=-1;
1199 else {
1200 r = kvm_dirty_pages_log_enable_all(kvm_context);
1204 else {
1205 if (kvm_dirty_bitmap) {
1206 r = kvm_dirty_pages_log_reset(kvm_context);
1207 qemu_free(kvm_dirty_bitmap);
1208 kvm_dirty_bitmap = NULL;
1211 return r;
1214 /* get kvm's dirty pages bitmap and update qemu's */
1215 static int kvm_get_dirty_pages_log_range(unsigned long start_addr,
1216 unsigned char *bitmap,
1217 unsigned long offset,
1218 unsigned long mem_size)
1220 unsigned int i, j, n=0;
1221 unsigned char c;
1222 unsigned long page_number, addr, addr1;
1223 ram_addr_t ram_addr;
1224 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
1227 * bitmap-traveling is faster than memory-traveling (for addr...)
1228 * especially when most of the memory is not dirty.
1230 for (i=0; i<len; i++) {
1231 c = bitmap[i];
1232 while (c>0) {
1233 j = ffsl(c) - 1;
1234 c &= ~(1u<<j);
1235 page_number = i * 8 + j;
1236 addr1 = page_number * TARGET_PAGE_SIZE;
1237 addr = offset + addr1;
1238 ram_addr = cpu_get_physical_page_desc(addr);
1239 cpu_physical_memory_set_dirty(ram_addr);
1240 n++;
1243 return 0;
1245 static int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
1246 void *bitmap, void *opaque)
1248 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
1252 * get kvm's dirty pages bitmap and update qemu's
1253 * we only care about physical ram, which resides in slots 0 and 3
1255 int kvm_update_dirty_pages_log(void)
1257 int r = 0;
1260 r = kvm_get_dirty_pages_range(kvm_context, 0, -1UL,
1261 kvm_dirty_bitmap, NULL,
1262 kvm_get_dirty_bitmap_cb);
1263 return r;
1266 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
1267 int log)
1269 if (log)
1270 kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
1271 else {
1272 #ifdef TARGET_I386
1273 if (must_use_aliases_target(start))
1274 return;
1275 #endif
1276 kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
1280 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
1282 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
1283 unsigned int brsize = BITMAP_SIZE(ram_size);
1284 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
1285 unsigned int extra_bytes = (extra_pages +7)/8;
1286 unsigned int hole_start = BITMAP_SIZE(0xa0000);
1287 unsigned int hole_end = BITMAP_SIZE(0xc0000);
1289 memset(bitmap, 0xFF, brsize + extra_bytes);
1290 memset(bitmap + hole_start, 0, hole_end - hole_start);
1291 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
1293 return 0;
1296 #ifdef KVM_CAP_IRQCHIP
1298 int kvm_set_irq(int irq, int level, int *status)
1300 return kvm_set_irq_level(kvm_context, irq, level, status);
1303 #endif
1305 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1307 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1310 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1311 unsigned long size, int log, int writable)
1313 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1316 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1317 unsigned long size)
1319 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1322 void kvm_mutex_unlock(void)
1324 assert(!cpu_single_env);
1325 pthread_mutex_unlock(&qemu_mutex);
1328 void kvm_mutex_lock(void)
1330 pthread_mutex_lock(&qemu_mutex);
1331 cpu_single_env = NULL;
1334 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1336 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1339 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1340 unsigned int size)
1342 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);
1345 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1347 return kvm_register_coalesced_mmio(kvm_context, start, size);
1350 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1352 return kvm_unregister_coalesced_mmio(kvm_context, start, size);
1355 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1356 void kvm_add_ioperm_data(struct ioperm_data *data)
1358 LIST_INSERT_HEAD(&ioperm_head, data, entries);
1361 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num)
1363 struct ioperm_data *data;
1365 data = LIST_FIRST(&ioperm_head);
1366 while (data) {
1367 struct ioperm_data *next = LIST_NEXT(data, entries);
1369 if (data->start_port == start_port && data->num == num) {
1370 LIST_REMOVE(data, entries);
1371 qemu_free(data);
1374 data = next;
1378 void kvm_ioperm(CPUState *env, void *data)
1380 if (kvm_enabled() && qemu_system_ready)
1381 on_vcpu(env, kvm_arch_do_ioperm, data);
1384 #endif
1386 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
1388 #ifndef TARGET_IA64
1389 void *buf;
1391 #ifdef TARGET_I386
1392 if (must_use_aliases_source(start_addr))
1393 return 0;
1394 #endif
1396 buf = qemu_malloc((end_addr - start_addr) / 8 + 2);
1397 kvm_get_dirty_pages_range(kvm_context, start_addr, end_addr - start_addr,
1398 buf, NULL, kvm_get_dirty_bitmap_cb);
1399 qemu_free(buf);
1400 #endif
1401 return 0;
1404 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len)
1406 #ifdef TARGET_I386
1407 if (must_use_aliases_source(phys_addr))
1408 return 0;
1409 #endif
1411 #ifndef TARGET_IA64
1412 kvm_qemu_log_memory(phys_addr, len, 1);
1413 #endif
1414 return 0;
1417 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len)
1419 #ifdef TARGET_I386
1420 if (must_use_aliases_source(phys_addr))
1421 return 0;
1422 #endif
1424 #ifndef TARGET_IA64
1425 kvm_qemu_log_memory(phys_addr, len, 0);
1426 #endif
1427 return 0;
1430 /* hack: both libkvm and upstream qemu define kvm_has_sync_mmu(), differently */
1431 #undef kvm_has_sync_mmu
1432 int qemu_kvm_has_sync_mmu(void)
1434 return kvm_has_sync_mmu(kvm_context);
1437 void qemu_kvm_cpu_stop(CPUState *env)
1439 if (kvm_enabled())
1440 env->kvm_cpu_state.stopped = 1;