use KVM_UPSTREAM for ppc.
[qemu-kvm/fedora.git] / qemu-kvm.c
blob63f3a00839457f98055763de4189fc3f6943851d
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, (long)data);
108 void kvm_inject_interrupt(CPUState *env, int mask)
110 on_vcpu(env, inject_interrupt, (void *)(long)mask);
113 void kvm_update_interrupt_request(CPUState *env)
115 int signal = 0;
117 if (env) {
118 if (!current_env || !current_env->kvm_cpu_state.created)
119 signal = 1;
121 * Testing for created here is really redundant
123 if (current_env && current_env->kvm_cpu_state.created &&
124 env != current_env && !env->kvm_cpu_state.signalled)
125 signal = 1;
127 if (signal) {
128 env->kvm_cpu_state.signalled = 1;
129 if (env->kvm_cpu_state.thread)
130 pthread_kill(env->kvm_cpu_state.thread, SIG_IPI);
135 void kvm_update_after_sipi(CPUState *env)
137 env->kvm_cpu_state.sipi_needed = 1;
138 kvm_update_interrupt_request(env);
141 void kvm_apic_init(CPUState *env)
143 if (env->cpu_index != 0)
144 env->kvm_cpu_state.init = 1;
145 kvm_update_interrupt_request(env);
148 #include <signal.h>
150 static int try_push_interrupts(void *opaque)
152 return kvm_arch_try_push_interrupts(opaque);
155 static void post_kvm_run(void *opaque, void *data)
157 CPUState *env = (CPUState *)data;
159 pthread_mutex_lock(&qemu_mutex);
160 kvm_arch_post_kvm_run(opaque, env);
163 static int pre_kvm_run(void *opaque, void *data)
165 CPUState *env = (CPUState *)data;
167 kvm_arch_pre_kvm_run(opaque, env);
169 if (env->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 while (penv) {
293 if (penv != cpu_single_env) {
294 penv->kvm_cpu_state.stop = 1;
295 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
296 } else {
297 penv->kvm_cpu_state.stop = 0;
298 penv->kvm_cpu_state.stopped = 1;
299 cpu_interrupt(penv, CPU_INTERRUPT_EXIT);
301 penv = (CPUState *)penv->next_cpu;
304 while (!all_threads_paused())
305 qemu_cond_wait(&qemu_pause_cond);
308 static void resume_all_threads(void)
310 CPUState *penv = first_cpu;
312 assert(!cpu_single_env);
314 while (penv) {
315 penv->kvm_cpu_state.stop = 0;
316 penv->kvm_cpu_state.stopped = 0;
317 pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
318 penv = (CPUState *)penv->next_cpu;
322 static void kvm_vm_state_change_handler(void *context, int running, int reason)
324 if (running)
325 resume_all_threads();
326 else
327 pause_all_threads();
330 static void update_regs_for_sipi(CPUState *env)
332 kvm_arch_update_regs_for_sipi(env);
333 env->kvm_cpu_state.sipi_needed = 0;
336 static void update_regs_for_init(CPUState *env)
338 #ifdef TARGET_I386
339 SegmentCache cs = env->segs[R_CS];
340 #endif
342 cpu_reset(env);
344 #ifdef TARGET_I386
345 /* restore SIPI vector */
346 if(env->kvm_cpu_state.sipi_needed)
347 env->segs[R_CS] = cs;
348 #endif
350 env->kvm_cpu_state.init = 0;
351 kvm_arch_load_regs(env);
354 static void setup_kernel_sigmask(CPUState *env)
356 sigset_t set;
358 sigemptyset(&set);
359 sigaddset(&set, SIGUSR2);
360 sigaddset(&set, SIGIO);
361 sigaddset(&set, SIGALRM);
362 sigprocmask(SIG_BLOCK, &set, NULL);
364 sigprocmask(SIG_BLOCK, NULL, &set);
365 sigdelset(&set, SIG_IPI);
367 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
370 static void qemu_kvm_system_reset(void)
372 CPUState *penv = first_cpu;
374 pause_all_threads();
376 qemu_system_reset();
378 while (penv) {
379 kvm_arch_cpu_reset(penv);
380 penv = (CPUState *)penv->next_cpu;
383 resume_all_threads();
386 static int kvm_main_loop_cpu(CPUState *env)
388 setup_kernel_sigmask(env);
390 pthread_mutex_lock(&qemu_mutex);
391 if (kvm_irqchip_in_kernel(kvm_context))
392 env->halted = 0;
394 kvm_qemu_init_env(env);
395 #ifdef TARGET_I386
396 kvm_tpr_vcpu_start(env);
397 #endif
399 cpu_single_env = env;
400 kvm_load_registers(env);
402 while (1) {
403 while (!has_work(env))
404 kvm_main_loop_wait(env, 1000);
405 if (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI))
406 env->halted = 0;
407 if (!kvm_irqchip_in_kernel(kvm_context)) {
408 if (env->kvm_cpu_state.init)
409 update_regs_for_init(env);
410 if (env->kvm_cpu_state.sipi_needed)
411 update_regs_for_sipi(env);
413 if (!env->halted && !env->kvm_cpu_state.init)
414 kvm_cpu_exec(env);
415 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
416 kvm_main_loop_wait(env, 0);
418 pthread_mutex_unlock(&qemu_mutex);
419 return 0;
422 static void *ap_main_loop(void *_env)
424 CPUState *env = _env;
425 sigset_t signals;
426 struct ioperm_data *data = NULL;
428 current_env = env;
429 env->thread_id = kvm_get_thread_id();
430 sigfillset(&signals);
431 sigprocmask(SIG_BLOCK, &signals, NULL);
432 kvm_create_vcpu(kvm_context, env->cpu_index);
433 kvm_qemu_init_env(env);
435 #ifdef USE_KVM_DEVICE_ASSIGNMENT
436 /* do ioperm for io ports of assigned devices */
437 LIST_FOREACH(data, &ioperm_head, entries)
438 on_vcpu(env, kvm_arch_do_ioperm, data);
439 #endif
441 /* signal VCPU creation */
442 pthread_mutex_lock(&qemu_mutex);
443 current_env->kvm_cpu_state.created = 1;
444 pthread_cond_signal(&qemu_vcpu_cond);
446 /* and wait for machine initialization */
447 while (!qemu_system_ready)
448 qemu_cond_wait(&qemu_system_cond);
449 pthread_mutex_unlock(&qemu_mutex);
451 kvm_main_loop_cpu(env);
452 return NULL;
455 void kvm_init_vcpu(CPUState *env)
457 pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
459 while (env->kvm_cpu_state.created == 0)
460 qemu_cond_wait(&qemu_vcpu_cond);
463 int kvm_init_ap(void)
465 #ifdef TARGET_I386
466 kvm_tpr_opt_setup();
467 #endif
468 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
470 signal(SIG_IPI, sig_ipi_handler);
471 return 0;
474 void qemu_kvm_notify_work(void)
476 uint64_t value = 1;
477 char buffer[8];
478 size_t offset = 0;
480 if (io_thread_fd == -1)
481 return;
483 memcpy(buffer, &value, sizeof(value));
485 while (offset < 8) {
486 ssize_t len;
488 len = write(io_thread_fd, buffer + offset, 8 - offset);
489 if (len == -1 && errno == EINTR)
490 continue;
492 if (len <= 0)
493 break;
495 offset += len;
498 if (offset != 8)
499 fprintf(stderr, "failed to notify io thread\n");
502 /* If we have signalfd, we mask out the signals we want to handle and then
503 * use signalfd to listen for them. We rely on whatever the current signal
504 * handler is to dispatch the signals when we receive them.
507 static void sigfd_handler(void *opaque)
509 int fd = (unsigned long)opaque;
510 struct qemu_signalfd_siginfo info;
511 struct sigaction action;
512 ssize_t len;
514 while (1) {
515 do {
516 len = read(fd, &info, sizeof(info));
517 } while (len == -1 && errno == EINTR);
519 if (len == -1 && errno == EAGAIN)
520 break;
522 if (len != sizeof(info)) {
523 printf("read from sigfd returned %ld: %m\n", len);
524 return;
527 sigaction(info.ssi_signo, NULL, &action);
528 if (action.sa_handler)
529 action.sa_handler(info.ssi_signo);
534 /* Used to break IO thread out of select */
535 static void io_thread_wakeup(void *opaque)
537 int fd = (unsigned long)opaque;
538 char buffer[8];
539 size_t offset = 0;
541 while (offset < 8) {
542 ssize_t len;
544 len = read(fd, buffer + offset, 8 - offset);
545 if (len == -1 && errno == EINTR)
546 continue;
548 if (len <= 0)
549 break;
551 offset += len;
555 int kvm_main_loop(void)
557 int fds[2];
558 sigset_t mask;
559 int sigfd;
561 io_thread = pthread_self();
562 qemu_system_ready = 1;
564 if (qemu_eventfd(fds) == -1) {
565 fprintf(stderr, "failed to create eventfd\n");
566 return -errno;
569 qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
570 (void *)(unsigned long)fds[0]);
572 io_thread_fd = fds[1];
574 sigemptyset(&mask);
575 sigaddset(&mask, SIGIO);
576 sigaddset(&mask, SIGALRM);
577 sigprocmask(SIG_BLOCK, &mask, NULL);
579 sigfd = qemu_signalfd(&mask);
580 if (sigfd == -1) {
581 fprintf(stderr, "failed to create signalfd\n");
582 return -errno;
585 fcntl(sigfd, F_SETFL, O_NONBLOCK);
587 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
588 (void *)(unsigned long)sigfd);
590 pthread_cond_broadcast(&qemu_system_cond);
592 io_thread_sigfd = sigfd;
593 cpu_single_env = NULL;
595 while (1) {
596 main_loop_wait(1000);
597 if (qemu_shutdown_requested()) {
598 if (qemu_no_shutdown()) {
599 vm_stop(0);
600 } else
601 break;
602 } else if (qemu_powerdown_requested())
603 qemu_system_powerdown();
604 else if (qemu_reset_requested())
605 qemu_kvm_system_reset();
606 #ifdef CONFIG_GDBSTUB
607 else if (kvm_debug_cpu_requested) {
608 gdb_set_stop_cpu(kvm_debug_cpu_requested);
609 vm_stop(EXCP_DEBUG);
610 kvm_debug_cpu_requested = NULL;
612 #endif
615 pause_all_threads();
616 pthread_mutex_unlock(&qemu_mutex);
618 return 0;
621 #ifdef KVM_CAP_SET_GUEST_DEBUG
622 static int kvm_debug(void *opaque, void *data,
623 struct kvm_debug_exit_arch *arch_info)
625 int handle = kvm_arch_debug(arch_info);
626 struct CPUState *env = data;
628 if (handle) {
629 kvm_debug_cpu_requested = env;
630 env->kvm_cpu_state.stopped = 1;
632 return handle;
634 #endif
636 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
638 *data = cpu_inb(0, addr);
639 return 0;
642 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
644 *data = cpu_inw(0, addr);
645 return 0;
648 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
650 *data = cpu_inl(0, addr);
651 return 0;
654 #define PM_IO_BASE 0xb000
656 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
658 if (addr == 0xb2) {
659 switch (data) {
660 case 0: {
661 cpu_outb(0, 0xb3, 0);
662 break;
664 case 0xf0: {
665 unsigned x;
667 /* enable acpi */
668 x = cpu_inw(0, PM_IO_BASE + 4);
669 x &= ~1;
670 cpu_outw(0, PM_IO_BASE + 4, x);
671 break;
673 case 0xf1: {
674 unsigned x;
676 /* enable acpi */
677 x = cpu_inw(0, PM_IO_BASE + 4);
678 x |= 1;
679 cpu_outw(0, PM_IO_BASE + 4, x);
680 break;
682 default:
683 break;
685 return 0;
687 cpu_outb(0, addr, data);
688 return 0;
691 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
693 cpu_outw(0, addr, data);
694 return 0;
697 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
699 cpu_outl(0, addr, data);
700 return 0;
703 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
705 cpu_physical_memory_rw(addr, data, len, 0);
706 return 0;
709 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
711 cpu_physical_memory_rw(addr, data, len, 1);
712 return 0;
715 static int kvm_io_window(void *opaque)
717 return 1;
721 static int kvm_halt(void *opaque, int vcpu)
723 return kvm_arch_halt(opaque, vcpu);
726 static int kvm_shutdown(void *opaque, void *data)
728 struct CPUState *env = (struct CPUState *)data;
730 /* stop the current vcpu from going back to guest mode */
731 env->kvm_cpu_state.stopped = 1;
733 qemu_system_reset_request();
734 return 1;
737 static struct kvm_callbacks qemu_kvm_ops = {
738 #ifdef KVM_CAP_SET_GUEST_DEBUG
739 .debug = kvm_debug,
740 #endif
741 .inb = kvm_inb,
742 .inw = kvm_inw,
743 .inl = kvm_inl,
744 .outb = kvm_outb,
745 .outw = kvm_outw,
746 .outl = kvm_outl,
747 .mmio_read = kvm_mmio_read,
748 .mmio_write = kvm_mmio_write,
749 .halt = kvm_halt,
750 .shutdown = kvm_shutdown,
751 .io_window = kvm_io_window,
752 .try_push_interrupts = try_push_interrupts,
753 #ifdef KVM_CAP_USER_NMI
754 .push_nmi = kvm_arch_push_nmi,
755 #endif
756 .post_kvm_run = post_kvm_run,
757 .pre_kvm_run = pre_kvm_run,
758 #ifdef TARGET_I386
759 .tpr_access = handle_tpr_access,
760 #endif
761 #ifdef TARGET_PPC
762 .powerpc_dcr_read = handle_powerpc_dcr_read,
763 .powerpc_dcr_write = handle_powerpc_dcr_write,
764 #endif
767 int kvm_qemu_init()
769 /* Try to initialize kvm */
770 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
771 if (!kvm_context) {
772 return -1;
774 pthread_mutex_lock(&qemu_mutex);
776 return 0;
779 #ifdef TARGET_I386
780 static int destroy_region_works = 0;
781 #endif
783 int kvm_qemu_create_context(void)
785 int r;
786 int i;
788 if (!kvm_irqchip) {
789 kvm_disable_irqchip_creation(kvm_context);
791 if (!kvm_pit) {
792 kvm_disable_pit_creation(kvm_context);
794 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
795 kvm_qemu_destroy();
796 return -1;
798 r = kvm_arch_qemu_create_context();
799 if(r <0)
800 kvm_qemu_destroy();
801 if (kvm_pit && !kvm_pit_reinject) {
802 if (kvm_reinject_control(kvm_context, 0)) {
803 fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
804 return -1;
807 #ifdef TARGET_I386
808 destroy_region_works = kvm_destroy_memory_region_works(kvm_context);
809 #endif
811 if (kvm_irqchip && kvm_has_gsi_routing(kvm_context)) {
812 kvm_clear_gsi_routes(kvm_context);
813 for (i = 0; i < 8; ++i) {
814 if (i == 2)
815 continue;
816 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_MASTER, i);
817 if (r < 0)
818 return r;
820 for (i = 8; i < 16; ++i) {
821 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
822 if (r < 0)
823 return r;
825 for (i = 0; i < 24; ++i) {
826 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_IOAPIC, i);
827 if (r < 0)
828 return r;
830 kvm_commit_irq_routes(kvm_context);
832 return 0;
835 void kvm_qemu_destroy(void)
837 kvm_finalize(kvm_context);
840 #ifdef TARGET_I386
841 static int must_use_aliases_source(target_phys_addr_t addr)
843 if (destroy_region_works)
844 return false;
845 if (addr == 0xa0000 || addr == 0xa8000)
846 return true;
847 return false;
850 static int must_use_aliases_target(target_phys_addr_t addr)
852 if (destroy_region_works)
853 return false;
854 if (addr >= 0xe0000000 && addr < 0x100000000ull)
855 return true;
856 return false;
859 static struct mapping {
860 target_phys_addr_t phys;
861 ram_addr_t ram;
862 ram_addr_t len;
863 } mappings[50];
864 static int nr_mappings;
866 static struct mapping *find_ram_mapping(ram_addr_t ram_addr)
868 struct mapping *p;
870 for (p = mappings; p < mappings + nr_mappings; ++p) {
871 if (p->ram <= ram_addr && ram_addr < p->ram + p->len) {
872 return p;
875 return NULL;
878 static struct mapping *find_mapping(target_phys_addr_t start_addr)
880 struct mapping *p;
882 for (p = mappings; p < mappings + nr_mappings; ++p) {
883 if (p->phys <= start_addr && start_addr < p->phys + p->len) {
884 return p;
887 return NULL;
890 static void drop_mapping(target_phys_addr_t start_addr)
892 struct mapping *p = find_mapping(start_addr);
894 if (p)
895 *p = mappings[--nr_mappings];
897 #endif
899 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
900 unsigned long size,
901 unsigned long phys_offset)
903 int r = 0;
904 unsigned long area_flags;
905 #ifdef TARGET_I386
906 struct mapping *p;
907 #endif
909 phys_offset &= ~IO_MEM_ROM;
910 area_flags = phys_offset & ~TARGET_PAGE_MASK;
912 if (area_flags != IO_MEM_RAM) {
913 #ifdef TARGET_I386
914 if (must_use_aliases_source(start_addr)) {
915 kvm_destroy_memory_alias(kvm_context, start_addr);
916 return;
918 if (must_use_aliases_target(start_addr))
919 return;
920 #endif
921 while (size > 0) {
922 p = find_mapping(start_addr);
923 if (p) {
924 kvm_unregister_memory_area(kvm_context, p->phys, p->len);
925 drop_mapping(p->phys);
927 start_addr += TARGET_PAGE_SIZE;
928 if (size > TARGET_PAGE_SIZE) {
929 size -= TARGET_PAGE_SIZE;
930 } else {
931 size = 0;
934 return;
937 r = kvm_is_containing_region(kvm_context, start_addr, size);
938 if (r)
939 return;
941 if (area_flags >= TLB_MMIO)
942 return;
944 #ifdef TARGET_I386
945 if (must_use_aliases_source(start_addr)) {
946 p = find_ram_mapping(phys_offset);
947 if (p) {
948 kvm_create_memory_alias(kvm_context, start_addr, size,
949 p->phys + (phys_offset - p->ram));
951 return;
953 #endif
955 r = kvm_register_phys_mem(kvm_context, start_addr,
956 phys_ram_base + phys_offset,
957 size, 0);
958 if (r < 0) {
959 printf("kvm_cpu_register_physical_memory: failed\n");
960 exit(1);
963 #ifdef TARGET_I386
964 drop_mapping(start_addr);
965 p = &mappings[nr_mappings++];
966 p->phys = start_addr;
967 p->ram = phys_offset;
968 p->len = size;
969 #endif
971 return;
974 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr,
975 target_phys_addr_t size,
976 unsigned long phys_offset)
978 kvm_unregister_memory_area(kvm_context, start_addr, size);
981 int kvm_setup_guest_memory(void *area, unsigned long size)
983 int ret = 0;
985 #ifdef MADV_DONTFORK
986 if (kvm_enabled() && !kvm_has_sync_mmu())
987 ret = madvise(area, size, MADV_DONTFORK);
988 #endif
990 if (ret)
991 perror ("madvise");
993 return ret;
996 int kvm_qemu_check_extension(int ext)
998 return kvm_check_extension(kvm_context, ext);
1001 int kvm_qemu_init_env(CPUState *cenv)
1003 return kvm_arch_qemu_init_env(cenv);
1006 #ifdef KVM_CAP_SET_GUEST_DEBUG
1007 struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
1008 TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);
1010 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc)
1012 struct kvm_sw_breakpoint *bp;
1014 TAILQ_FOREACH(bp, &kvm_sw_breakpoints, entry) {
1015 if (bp->pc == pc)
1016 return bp;
1018 return NULL;
1021 struct kvm_set_guest_debug_data {
1022 struct kvm_guest_debug dbg;
1023 int err;
1026 static void kvm_invoke_set_guest_debug(void *data)
1028 struct kvm_set_guest_debug_data *dbg_data = data;
1030 dbg_data->err = kvm_set_guest_debug(kvm_context, cpu_single_env->cpu_index,
1031 &dbg_data->dbg);
1034 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1036 struct kvm_set_guest_debug_data data;
1038 data.dbg.control = 0;
1039 if (env->singlestep_enabled)
1040 data.dbg.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1042 kvm_arch_update_guest_debug(env, &data.dbg);
1043 data.dbg.control |= reinject_trap;
1045 on_vcpu(env, kvm_invoke_set_guest_debug, &data);
1046 return data.err;
1049 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1050 target_ulong len, int type)
1052 struct kvm_sw_breakpoint *bp;
1053 CPUState *env;
1054 int err;
1056 if (type == GDB_BREAKPOINT_SW) {
1057 bp = kvm_find_sw_breakpoint(addr);
1058 if (bp) {
1059 bp->use_count++;
1060 return 0;
1063 bp = qemu_malloc(sizeof(struct kvm_sw_breakpoint));
1064 if (!bp)
1065 return -ENOMEM;
1067 bp->pc = addr;
1068 bp->use_count = 1;
1069 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1070 if (err) {
1071 free(bp);
1072 return err;
1075 TAILQ_INSERT_HEAD(&kvm_sw_breakpoints, bp, entry);
1076 } else {
1077 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1078 if (err)
1079 return err;
1082 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1083 err = kvm_update_guest_debug(env, 0);
1084 if (err)
1085 return err;
1087 return 0;
1090 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1091 target_ulong len, int type)
1093 struct kvm_sw_breakpoint *bp;
1094 CPUState *env;
1095 int err;
1097 if (type == GDB_BREAKPOINT_SW) {
1098 bp = kvm_find_sw_breakpoint(addr);
1099 if (!bp)
1100 return -ENOENT;
1102 if (bp->use_count > 1) {
1103 bp->use_count--;
1104 return 0;
1107 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1108 if (err)
1109 return err;
1111 TAILQ_REMOVE(&kvm_sw_breakpoints, bp, entry);
1112 qemu_free(bp);
1113 } else {
1114 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1115 if (err)
1116 return err;
1119 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1120 err = kvm_update_guest_debug(env, 0);
1121 if (err)
1122 return err;
1124 return 0;
1127 void kvm_remove_all_breakpoints(CPUState *current_env)
1129 struct kvm_sw_breakpoint *bp, *next;
1130 CPUState *env;
1132 TAILQ_FOREACH_SAFE(bp, &kvm_sw_breakpoints, entry, next) {
1133 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1134 /* Try harder to find a CPU that currently sees the breakpoint. */
1135 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1136 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0)
1137 break;
1141 kvm_arch_remove_all_hw_breakpoints();
1143 for (env = first_cpu; env != NULL; env = env->next_cpu)
1144 kvm_update_guest_debug(env, 0);
1147 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1149 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
1151 return -EINVAL;
1154 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
1155 target_ulong len, int type)
1157 return -EINVAL;
1160 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
1161 target_ulong len, int type)
1163 return -EINVAL;
1166 void kvm_remove_all_breakpoints(CPUState *current_env)
1169 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1172 * dirty pages logging
1174 /* FIXME: use unsigned long pointer instead of unsigned char */
1175 unsigned char *kvm_dirty_bitmap = NULL;
1176 int kvm_physical_memory_set_dirty_tracking(int enable)
1178 int r = 0;
1180 if (!kvm_enabled())
1181 return 0;
1183 if (enable) {
1184 if (!kvm_dirty_bitmap) {
1185 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
1186 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
1187 if (kvm_dirty_bitmap == NULL) {
1188 perror("Failed to allocate dirty pages bitmap");
1189 r=-1;
1191 else {
1192 r = kvm_dirty_pages_log_enable_all(kvm_context);
1196 else {
1197 if (kvm_dirty_bitmap) {
1198 r = kvm_dirty_pages_log_reset(kvm_context);
1199 qemu_free(kvm_dirty_bitmap);
1200 kvm_dirty_bitmap = NULL;
1203 return r;
1206 /* get kvm's dirty pages bitmap and update qemu's */
1207 static int kvm_get_dirty_pages_log_range(unsigned long start_addr,
1208 unsigned char *bitmap,
1209 unsigned long offset,
1210 unsigned long mem_size)
1212 unsigned int i, j, n=0;
1213 unsigned char c;
1214 unsigned long page_number, addr, addr1;
1215 ram_addr_t ram_addr;
1216 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
1219 * bitmap-traveling is faster than memory-traveling (for addr...)
1220 * especially when most of the memory is not dirty.
1222 for (i=0; i<len; i++) {
1223 c = bitmap[i];
1224 while (c>0) {
1225 j = ffsl(c) - 1;
1226 c &= ~(1u<<j);
1227 page_number = i * 8 + j;
1228 addr1 = page_number * TARGET_PAGE_SIZE;
1229 addr = offset + addr1;
1230 ram_addr = cpu_get_physical_page_desc(addr);
1231 cpu_physical_memory_set_dirty(ram_addr);
1232 n++;
1235 return 0;
1237 static int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
1238 void *bitmap, void *opaque)
1240 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
1244 * get kvm's dirty pages bitmap and update qemu's
1245 * we only care about physical ram, which resides in slots 0 and 3
1247 int kvm_update_dirty_pages_log(void)
1249 int r = 0;
1252 r = kvm_get_dirty_pages_range(kvm_context, 0, -1ULL,
1253 kvm_dirty_bitmap, NULL,
1254 kvm_get_dirty_bitmap_cb);
1255 return r;
1258 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
1259 int log)
1261 if (log)
1262 kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
1263 else {
1264 #ifdef TARGET_I386
1265 if (must_use_aliases_target(start))
1266 return;
1267 #endif
1268 kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
1272 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
1274 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
1275 unsigned int brsize = BITMAP_SIZE(ram_size);
1276 unsigned int extra_pages = (phys_ram_size - ram_size) / TARGET_PAGE_SIZE;
1277 unsigned int extra_bytes = (extra_pages +7)/8;
1278 unsigned int hole_start = BITMAP_SIZE(0xa0000);
1279 unsigned int hole_end = BITMAP_SIZE(0xc0000);
1281 memset(bitmap, 0xFF, brsize + extra_bytes);
1282 memset(bitmap + hole_start, 0, hole_end - hole_start);
1283 memset(bitmap + brsize + extra_bytes, 0, bsize - brsize - extra_bytes);
1285 return 0;
1288 #ifdef KVM_CAP_IRQCHIP
1290 int kvm_set_irq(int irq, int level, int *status)
1292 return kvm_set_irq_level(kvm_context, irq, level, status);
1295 #endif
1297 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
1299 return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
1302 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
1303 unsigned long size, int log, int writable)
1305 return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
1308 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
1309 unsigned long size)
1311 kvm_destroy_phys_mem(kvm_context, start_addr, size);
1314 void kvm_mutex_unlock(void)
1316 assert(!cpu_single_env);
1317 pthread_mutex_unlock(&qemu_mutex);
1320 void kvm_mutex_lock(void)
1322 pthread_mutex_lock(&qemu_mutex);
1323 cpu_single_env = NULL;
1326 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr, unsigned int size)
1328 return kvm_register_coalesced_mmio(kvm_context, addr, size);
1331 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,
1332 unsigned int size)
1334 return kvm_unregister_coalesced_mmio(kvm_context, addr, size);
1337 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1339 return kvm_register_coalesced_mmio(kvm_context, start, size);
1342 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
1344 return kvm_unregister_coalesced_mmio(kvm_context, start, size);
1347 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1348 void kvm_add_ioperm_data(struct ioperm_data *data)
1350 LIST_INSERT_HEAD(&ioperm_head, data, entries);
1353 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num)
1355 struct ioperm_data *data;
1357 data = LIST_FIRST(&ioperm_head);
1358 while (data) {
1359 struct ioperm_data *next = LIST_NEXT(data, entries);
1361 if (data->start_port == start_port && data->num == num) {
1362 LIST_REMOVE(data, entries);
1363 qemu_free(data);
1366 data = next;
1370 void kvm_ioperm(CPUState *env, void *data)
1372 if (kvm_enabled() && qemu_system_ready)
1373 on_vcpu(env, kvm_arch_do_ioperm, data);
1376 #endif
1378 void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
1380 #ifndef TARGET_IA64
1381 void *buf;
1383 #ifdef TARGET_I386
1384 if (must_use_aliases_source(start_addr))
1385 return;
1386 #endif
1388 buf = qemu_malloc((end_addr - start_addr) / 8 + 2);
1389 kvm_get_dirty_pages_range(kvm_context, start_addr, end_addr - start_addr,
1390 buf, NULL, kvm_get_dirty_bitmap_cb);
1391 qemu_free(buf);
1392 #endif
1395 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len)
1397 #ifdef TARGET_I386
1398 if (must_use_aliases_source(phys_addr))
1399 return 0;
1400 #endif
1401 kvm_qemu_log_memory(phys_addr, len, 1);
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
1411 kvm_qemu_log_memory(phys_addr, len, 0);
1412 return 0;
1415 /* hack: both libkvm and upstream qemu define kvm_has_sync_mmu(), differently */
1416 #undef kvm_has_sync_mmu
1417 int qemu_kvm_has_sync_mmu(void)
1419 return kvm_has_sync_mmu(kvm_context);
1422 void qemu_kvm_cpu_stop(CPUState *env)
1424 if (kvm_enabled())
1425 env->kvm_cpu_state.stopped = 1;