Merge branch 'master' of git://git.sv.gnu.org/qemu
[qemu-kvm/fedora.git] / qemu-kvm.c
blobfd957d955d2d5d26642c4975cd6d9a6bebc9760e
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 try_push_interrupts(void *opaque)
154 return kvm_arch_try_push_interrupts(opaque);
157 static void post_kvm_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 pre_kvm_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(kvm_context, env->cpu_index, env);
208 if (r < 0) {
209 printf("kvm_run returned %d\n", r);
210 exit(1);
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(kvm_context, env->cpu_index, &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 kvm_create_vcpu(kvm_context, env->cpu_index);
436 kvm_qemu_init_env(env);
438 #ifdef USE_KVM_DEVICE_ASSIGNMENT
439 /* do ioperm for io ports of assigned devices */
440 LIST_FOREACH(data, &ioperm_head, entries)
441 on_vcpu(env, kvm_arch_do_ioperm, data);
442 #endif
444 /* signal VCPU creation */
445 pthread_mutex_lock(&qemu_mutex);
446 current_env->kvm_cpu_state.created = 1;
447 pthread_cond_signal(&qemu_vcpu_cond);
449 /* and wait for machine initialization */
450 while (!qemu_system_ready)
451 qemu_cond_wait(&qemu_system_cond);
452 pthread_mutex_unlock(&qemu_mutex);
454 kvm_main_loop_cpu(env);
455 return NULL;
458 void kvm_init_vcpu(CPUState *env)
460 pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
462 while (env->kvm_cpu_state.created == 0)
463 qemu_cond_wait(&qemu_vcpu_cond);
466 int kvm_init_ap(void)
468 #ifdef TARGET_I386
469 kvm_tpr_opt_setup();
470 #endif
471 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
473 signal(SIG_IPI, sig_ipi_handler);
474 return 0;
477 void qemu_kvm_notify_work(void)
479 uint64_t value = 1;
480 char buffer[8];
481 size_t offset = 0;
483 if (io_thread_fd == -1)
484 return;
486 memcpy(buffer, &value, sizeof(value));
488 while (offset < 8) {
489 ssize_t len;
491 len = write(io_thread_fd, buffer + offset, 8 - offset);
492 if (len == -1 && errno == EINTR)
493 continue;
495 if (len <= 0)
496 break;
498 offset += len;
501 if (offset != 8)
502 fprintf(stderr, "failed to notify io thread\n");
505 /* If we have signalfd, we mask out the signals we want to handle and then
506 * use signalfd to listen for them. We rely on whatever the current signal
507 * handler is to dispatch the signals when we receive them.
510 static void sigfd_handler(void *opaque)
512 int fd = (unsigned long)opaque;
513 struct qemu_signalfd_siginfo info;
514 struct sigaction action;
515 ssize_t len;
517 while (1) {
518 do {
519 len = read(fd, &info, sizeof(info));
520 } while (len == -1 && errno == EINTR);
522 if (len == -1 && errno == EAGAIN)
523 break;
525 if (len != sizeof(info)) {
526 printf("read from sigfd returned %zd: %m\n", len);
527 return;
530 sigaction(info.ssi_signo, NULL, &action);
531 if (action.sa_handler)
532 action.sa_handler(info.ssi_signo);
537 /* Used to break IO thread out of select */
538 static void io_thread_wakeup(void *opaque)
540 int fd = (unsigned long)opaque;
541 char buffer[8];
542 size_t offset = 0;
544 while (offset < 8) {
545 ssize_t len;
547 len = read(fd, buffer + offset, 8 - offset);
548 if (len == -1 && errno == EINTR)
549 continue;
551 if (len <= 0)
552 break;
554 offset += len;
558 int kvm_main_loop(void)
560 int fds[2];
561 sigset_t mask;
562 int sigfd;
564 io_thread = pthread_self();
565 qemu_system_ready = 1;
567 if (qemu_eventfd(fds) == -1) {
568 fprintf(stderr, "failed to create eventfd\n");
569 return -errno;
572 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
573 (void *)(unsigned long)fds[0]);
575 io_thread_fd = fds[1];
577 sigemptyset(&mask);
578 sigaddset(&mask, SIGIO);
579 sigaddset(&mask, SIGALRM);
580 sigprocmask(SIG_BLOCK, &mask, NULL);
582 sigfd = qemu_signalfd(&mask);
583 if (sigfd == -1) {
584 fprintf(stderr, "failed to create signalfd\n");
585 return -errno;
588 fcntl(sigfd, F_SETFL, O_NONBLOCK);
590 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
591 (void *)(unsigned long)sigfd);
593 pthread_cond_broadcast(&qemu_system_cond);
595 io_thread_sigfd = sigfd;
596 cpu_single_env = NULL;
598 while (1) {
599 main_loop_wait(1000);
600 if (qemu_shutdown_requested()) {
601 if (qemu_no_shutdown()) {
602 vm_stop(0);
603 } else
604 break;
605 } else if (qemu_powerdown_requested())
606 qemu_system_powerdown();
607 else if (qemu_reset_requested())
608 qemu_kvm_system_reset();
609 else if (kvm_debug_cpu_requested) {
610 gdb_set_stop_cpu(kvm_debug_cpu_requested);
611 vm_stop(EXCP_DEBUG);
612 kvm_debug_cpu_requested = NULL;
616 pause_all_threads();
617 pthread_mutex_unlock(&qemu_mutex);
619 return 0;
622 #ifdef KVM_CAP_SET_GUEST_DEBUG
623 static int kvm_debug(void *opaque, void *data,
624 struct kvm_debug_exit_arch *arch_info)
626 int handle = kvm_arch_debug(arch_info);
627 CPUState *env = data;
629 if (handle) {
630 kvm_debug_cpu_requested = env;
631 env->kvm_cpu_state.stopped = 1;
633 return handle;
635 #endif
637 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
639 *data = cpu_inb(0, addr);
640 return 0;
643 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
645 *data = cpu_inw(0, addr);
646 return 0;
649 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
651 *data = cpu_inl(0, addr);
652 return 0;
655 #define PM_IO_BASE 0xb000
657 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
659 if (addr == 0xb2) {
660 switch (data) {
661 case 0: {
662 cpu_outb(0, 0xb3, 0);
663 break;
665 case 0xf0: {
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 case 0xf1: {
675 unsigned x;
677 /* enable acpi */
678 x = cpu_inw(0, PM_IO_BASE + 4);
679 x |= 1;
680 cpu_outw(0, PM_IO_BASE + 4, x);
681 break;
683 default:
684 break;
686 return 0;
688 cpu_outb(0, addr, data);
689 return 0;
692 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
694 cpu_outw(0, addr, data);
695 return 0;
698 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
700 cpu_outl(0, addr, data);
701 return 0;
704 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
706 cpu_physical_memory_rw(addr, data, len, 0);
707 return 0;
710 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
712 cpu_physical_memory_rw(addr, data, len, 1);
713 return 0;
716 static int kvm_io_window(void *opaque)
718 return 1;
722 static int kvm_halt(void *opaque, int vcpu)
724 return kvm_arch_halt(opaque, vcpu);
727 static int kvm_shutdown(void *opaque, void *data)
729 CPUState *env = (CPUState *)data;
731 /* stop the current vcpu from going back to guest mode */
732 env->kvm_cpu_state.stopped = 1;
734 qemu_system_reset_request();
735 return 1;
738 static struct kvm_callbacks qemu_kvm_ops = {
739 #ifdef KVM_CAP_SET_GUEST_DEBUG
740 .debug = kvm_debug,
741 #endif
742 .inb = kvm_inb,
743 .inw = kvm_inw,
744 .inl = kvm_inl,
745 .outb = kvm_outb,
746 .outw = kvm_outw,
747 .outl = kvm_outl,
748 .mmio_read = kvm_mmio_read,
749 .mmio_write = kvm_mmio_write,
750 .halt = kvm_halt,
751 .shutdown = kvm_shutdown,
752 .io_window = kvm_io_window,
753 .try_push_interrupts = try_push_interrupts,
754 #ifdef KVM_CAP_USER_NMI
755 .push_nmi = kvm_arch_push_nmi,
756 #endif
757 .post_kvm_run = post_kvm_run,
758 .pre_kvm_run = pre_kvm_run,
759 #ifdef TARGET_I386
760 .tpr_access = handle_tpr_access,
761 #endif
762 #ifdef TARGET_PPC
763 .powerpc_dcr_read = handle_powerpc_dcr_read,
764 .powerpc_dcr_write = handle_powerpc_dcr_write,
765 #endif
768 int kvm_qemu_init()
770 /* Try to initialize kvm */
771 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
772 if (!kvm_context) {
773 return -1;
775 pthread_mutex_lock(&qemu_mutex);
777 return 0;
780 #ifdef TARGET_I386
781 static int destroy_region_works = 0;
782 #endif
785 #if !defined(TARGET_I386)
786 int kvm_arch_init_irq_routing(void)
788 return 0;
790 #endif
792 int kvm_qemu_create_context(void)
794 int r;
796 if (!kvm_irqchip) {
797 kvm_disable_irqchip_creation(kvm_context);
799 if (!kvm_pit) {
800 kvm_disable_pit_creation(kvm_context);
802 if (kvm_create(kvm_context, 0, NULL) < 0) {
803 kvm_qemu_destroy();
804 return -1;
806 r = kvm_arch_qemu_create_context();
807 if(r <0)
808 kvm_qemu_destroy();
809 if (kvm_pit && !kvm_pit_reinject) {
810 if (kvm_reinject_control(kvm_context, 0)) {
811 fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
812 return -1;
815 #ifdef TARGET_I386
816 destroy_region_works = kvm_destroy_memory_region_works(kvm_context);
817 #endif
819 r = kvm_arch_init_irq_routing();
820 if (r < 0) {
821 return r;
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;
897 #ifdef TARGET_I386
898 struct mapping *p;
899 #endif
901 if (start_addr + size > phys_ram_size) {
902 phys_ram_size = start_addr + size;
905 phys_offset &= ~IO_MEM_ROM;
906 area_flags = phys_offset & ~TARGET_PAGE_MASK;
908 if (area_flags != IO_MEM_RAM) {
909 #ifdef TARGET_I386
910 if (must_use_aliases_source(start_addr)) {
911 kvm_destroy_memory_alias(kvm_context, start_addr);
912 return;
914 if (must_use_aliases_target(start_addr))
915 return;
916 #endif
917 while (size > 0) {
918 p = find_mapping(start_addr);
919 if (p) {
920 kvm_unregister_memory_area(kvm_context, p->phys, p->len);
921 drop_mapping(p->phys);
923 start_addr += TARGET_PAGE_SIZE;
924 if (size > TARGET_PAGE_SIZE) {
925 size -= TARGET_PAGE_SIZE;
926 } else {
927 size = 0;
930 return;
933 r = kvm_is_containing_region(kvm_context, start_addr, size);
934 if (r)
935 return;
937 if (area_flags >= TLB_MMIO)
938 return;
940 #ifdef TARGET_I386
941 if (must_use_aliases_source(start_addr)) {
942 p = find_ram_mapping(phys_offset);
943 if (p) {
944 kvm_create_memory_alias(kvm_context, start_addr, size,
945 p->phys + (phys_offset - p->ram));
947 return;
949 #endif
951 r = kvm_register_phys_mem(kvm_context, start_addr,
952 qemu_get_ram_ptr(phys_offset),
953 size, 0);
954 if (r < 0) {
955 printf("kvm_cpu_register_physical_memory: failed\n");
956 exit(1);
959 #ifdef TARGET_I386
960 drop_mapping(start_addr);
961 p = &mappings[nr_mappings++];
962 p->phys = start_addr;
963 p->ram = phys_offset;
964 p->len = size;
965 #endif
967 return;
970 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
971 target_phys_addr_t size,
972 unsigned long phys_offset)
974 kvm_unregister_memory_area(kvm_context, start_addr, size);
977 int kvm_setup_guest_memory(void *area, unsigned long size)
979 int ret = 0;
981 #ifdef MADV_DONTFORK
982 if (kvm_enabled() && !kvm_has_sync_mmu())
983 ret = madvise(area, size, MADV_DONTFORK);
984 #endif
986 if (ret)
987 perror ("madvise");
989 return ret;
992 int kvm_qemu_check_extension(int ext)
994 return kvm_check_extension(kvm_context, ext);
997 int kvm_qemu_init_env(CPUState *cenv)
999 return kvm_arch_qemu_init_env(cenv);
1002 #ifdef KVM_CAP_SET_GUEST_DEBUG
1003 struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
1004 TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);
1006 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc)
1008 struct kvm_sw_breakpoint *bp;
1010 TAILQ_FOREACH(bp, &kvm_sw_breakpoints, entry) {
1011 if (bp->pc == pc)
1012 return bp;
1014 return NULL;
1017 struct kvm_set_guest_debug_data {
1018 struct kvm_guest_debug dbg;
1019 int err;
1022 static void kvm_invoke_set_guest_debug(void *data)
1024 struct kvm_set_guest_debug_data *dbg_data = data;
1026 dbg_data->err = kvm_set_guest_debug(kvm_context, cpu_single_env->cpu_index,
1027 &dbg_data->dbg);
1030 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1032 struct kvm_set_guest_debug_data data;
1034 data.dbg.control = 0;
1035 if (env->singlestep_enabled)
1036 data.dbg.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1038 kvm_arch_update_guest_debug(env, &data.dbg);
1039 data.dbg.control |= reinject_trap;
1041 on_vcpu(env, kvm_invoke_set_guest_debug, &data);
1042 return data.err;
1045 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1046 target_ulong len, int type)
1048 struct kvm_sw_breakpoint *bp;
1049 CPUState *env;
1050 int err;
1052 if (type == GDB_BREAKPOINT_SW) {
1053 bp = kvm_find_sw_breakpoint(addr);
1054 if (bp) {
1055 bp->use_count++;
1056 return 0;
1059 bp = qemu_malloc(sizeof(struct kvm_sw_breakpoint));
1060 if (!bp)
1061 return -ENOMEM;
1063 bp->pc = addr;
1064 bp->use_count = 1;
1065 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1066 if (err) {
1067 free(bp);
1068 return err;
1071 TAILQ_INSERT_HEAD(&kvm_sw_breakpoints, bp, entry);
1072 } else {
1073 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1074 if (err)
1075 return err;
1078 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1079 err = kvm_update_guest_debug(env, 0);
1080 if (err)
1081 return err;
1083 return 0;
1086 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1087 target_ulong len, int type)
1089 struct kvm_sw_breakpoint *bp;
1090 CPUState *env;
1091 int err;
1093 if (type == GDB_BREAKPOINT_SW) {
1094 bp = kvm_find_sw_breakpoint(addr);
1095 if (!bp)
1096 return -ENOENT;
1098 if (bp->use_count > 1) {
1099 bp->use_count--;
1100 return 0;
1103 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1104 if (err)
1105 return err;
1107 TAILQ_REMOVE(&kvm_sw_breakpoints, bp, entry);
1108 qemu_free(bp);
1109 } else {
1110 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1111 if (err)
1112 return err;
1115 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1116 err = kvm_update_guest_debug(env, 0);
1117 if (err)
1118 return err;
1120 return 0;
1123 void kvm_remove_all_breakpoints(CPUState *current_env)
1125 struct kvm_sw_breakpoint *bp, *next;
1126 CPUState *env;
1128 TAILQ_FOREACH_SAFE(bp, &kvm_sw_breakpoints, entry, next) {
1129 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1130 /* Try harder to find a CPU that currently sees the breakpoint. */
1131 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1132 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0)
1133 break;
1137 kvm_arch_remove_all_hw_breakpoints();
1139 for (env = first_cpu; env != NULL; env = env->next_cpu)
1140 kvm_update_guest_debug(env, 0);
1143 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1145 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1147 return -EINVAL;
1150 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1151 target_ulong len, int type)
1153 return -EINVAL;
1156 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1157 target_ulong len, int type)
1159 return -EINVAL;
1162 void kvm_remove_all_breakpoints(CPUState *current_env)
1165 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1168 * dirty pages logging
1170 /* FIXME: use unsigned long pointer instead of unsigned char */
1171 unsigned char *kvm_dirty_bitmap = NULL;
1172 int kvm_physical_memory_set_dirty_tracking(int enable)
1174 int r = 0;
1176 if (!kvm_enabled())
1177 return 0;
1179 if (enable) {
1180 if (!kvm_dirty_bitmap) {
1181 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
1182 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
1183 if (kvm_dirty_bitmap == NULL) {
1184 perror("Failed to allocate dirty pages bitmap");
1185 r=-1;
1187 else {
1188 r = kvm_dirty_pages_log_enable_all(kvm_context);
1192 else {
1193 if (kvm_dirty_bitmap) {
1194 r = kvm_dirty_pages_log_reset(kvm_context);
1195 qemu_free(kvm_dirty_bitmap);
1196 kvm_dirty_bitmap = NULL;
1199 return r;
1202 /* get kvm's dirty pages bitmap and update qemu's */
1203 static int kvm_get_dirty_pages_log_range(unsigned long start_addr,
1204 unsigned char *bitmap,
1205 unsigned long offset,
1206 unsigned long mem_size)
1208 unsigned int i, j, n=0;
1209 unsigned char c;
1210 unsigned long page_number, addr, addr1;
1211 ram_addr_t ram_addr;
1212 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
1215 * bitmap-traveling is faster than memory-traveling (for addr...)
1216 * especially when most of the memory is not dirty.
1218 for (i=0; i<len; i++) {
1219 c = bitmap[i];
1220 while (c>0) {
1221 j = ffsl(c) - 1;
1222 c &= ~(1u<<j);
1223 page_number = i * 8 + j;
1224 addr1 = page_number * TARGET_PAGE_SIZE;
1225 addr = offset + addr1;
1226 ram_addr = cpu_get_physical_page_desc(addr);
1227 cpu_physical_memory_set_dirty(ram_addr);
1228 n++;
1231 return 0;
1233 static int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
1234 void *bitmap, void *opaque)
1236 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
1240 * get kvm's dirty pages bitmap and update qemu's
1241 * we only care about physical ram, which resides in slots 0 and 3
1243 int kvm_update_dirty_pages_log(void)
1245 int r = 0;
1248 r = kvm_get_dirty_pages_range(kvm_context, 0, -1UL,
1249 kvm_dirty_bitmap, NULL,
1250 kvm_get_dirty_bitmap_cb);
1251 return r;
1254 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
1255 int log)
1257 if (log)
1258 kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
1259 else {
1260 #ifdef TARGET_I386
1261 if (must_use_aliases_target(start))
1262 return;
1263 #endif
1264 kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
1268 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
1270 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
1271 unsigned int brsize = BITMAP_SIZE(ram_size);
1272 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
1273 unsigned int extra_bytes = (extra_pages +7)/8;
1274 unsigned int hole_start = BITMAP_SIZE(0xa0000);
1275 unsigned int hole_end = BITMAP_SIZE(0xc0000);
1277 memset(bitmap, 0xFF, brsize + extra_bytes);
1278 memset(bitmap + hole_start, 0, hole_end - hole_start);
1279 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
1281 return 0;
1284 #ifdef KVM_CAP_IRQCHIP
1286 int kvm_set_irq(int irq, int level, int *status)
1288 return kvm_set_irq_level(kvm_context, irq, level, status);
1291 #endif
1293 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1295 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1298 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1299 unsigned long size, int log, int writable)
1301 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1304 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1305 unsigned long size)
1307 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1310 void kvm_mutex_unlock(void)
1312 assert(!cpu_single_env);
1313 pthread_mutex_unlock(&qemu_mutex);
1316 void kvm_mutex_lock(void)
1318 pthread_mutex_lock(&qemu_mutex);
1319 cpu_single_env = NULL;
1322 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1324 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1327 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1328 unsigned int size)
1330 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);
1333 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1335 return kvm_register_coalesced_mmio(kvm_context, start, size);
1338 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1340 return kvm_unregister_coalesced_mmio(kvm_context, start, size);
1343 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1344 void kvm_add_ioperm_data(struct ioperm_data *data)
1346 LIST_INSERT_HEAD(&ioperm_head, data, entries);
1349 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num)
1351 struct ioperm_data *data;
1353 data = LIST_FIRST(&ioperm_head);
1354 while (data) {
1355 struct ioperm_data *next = LIST_NEXT(data, entries);
1357 if (data->start_port == start_port && data->num == num) {
1358 LIST_REMOVE(data, entries);
1359 qemu_free(data);
1362 data = next;
1366 void kvm_ioperm(CPUState *env, void *data)
1368 if (kvm_enabled() && qemu_system_ready)
1369 on_vcpu(env, kvm_arch_do_ioperm, data);
1372 #endif
1374 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
1376 #ifndef TARGET_IA64
1377 void *buf;
1379 #ifdef TARGET_I386
1380 if (must_use_aliases_source(start_addr))
1381 return 0;
1382 #endif
1384 buf = qemu_malloc((end_addr - start_addr) / 8 + 2);
1385 kvm_get_dirty_pages_range(kvm_context, start_addr, end_addr - start_addr,
1386 buf, NULL, kvm_get_dirty_bitmap_cb);
1387 qemu_free(buf);
1388 #endif
1389 return 0;
1392 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len)
1394 #ifdef TARGET_I386
1395 if (must_use_aliases_source(phys_addr))
1396 return 0;
1397 #endif
1399 #ifndef TARGET_IA64
1400 kvm_qemu_log_memory(phys_addr, len, 1);
1401 #endif
1402 return 0;
1405 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len)
1407 #ifdef TARGET_I386
1408 if (must_use_aliases_source(phys_addr))
1409 return 0;
1410 #endif
1412 #ifndef TARGET_IA64
1413 kvm_qemu_log_memory(phys_addr, len, 0);
1414 #endif
1415 return 0;
1418 /* hack: both libkvm and upstream qemu define kvm_has_sync_mmu(), differently */
1419 #undef kvm_has_sync_mmu
1420 int qemu_kvm_has_sync_mmu(void)
1422 return kvm_has_sync_mmu(kvm_context);
1425 void qemu_kvm_cpu_stop(CPUState *env)
1427 if (kvm_enabled())
1428 env->kvm_cpu_state.stopped = 1;
1431 void kvm_arch_get_registers(CPUState *env)
1433 kvm_save_registers(env);
1434 kvm_save_mpstate(env);
1437 void kvm_arch_put_registers(CPUState *env)
1439 kvm_load_registers(env);
1440 kvm_load_mpstate(env);
1444 void cpu_synchronize_state(CPUState *env, int modified)
1446 if (kvm_enabled()) {
1447 if (modified)
1448 kvm_arch_put_registers(env);
1449 else
1450 kvm_arch_get_registers(env);