Regenerate vgabios (first source drop)
[qemu-kvm/fedora.git] / qemu-kvm.c
blob9aee9035af4c7acbea7d2d6c3728da30fc5e90d7
2 #include "config.h"
3 #include "config-host.h"
5 #ifdef USE_KVM
6 #define KVM_ALLOWED_DEFAULT 1
7 #else
8 #define KVM_ALLOWED_DEFAULT 0
9 #endif
11 int kvm_allowed = KVM_ALLOWED_DEFAULT;
12 int kvm_irqchip = 1;
14 #ifdef USE_KVM
16 #include <string.h>
17 #include "hw/hw.h"
18 #include "sysemu.h"
20 #include "qemu-kvm.h"
21 #include <libkvm.h>
22 #include <pthread.h>
23 #include <sys/utsname.h>
25 extern void perror(const char *s);
27 kvm_context_t kvm_context;
29 extern int smp_cpus;
31 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
32 __thread CPUState *vcpu_env;
34 static sigset_t io_sigset, io_negsigset;
36 static int wait_hack;
38 #define SIG_IPI (SIGRTMIN+4)
40 struct vcpu_info {
41 int sipi_needed;
42 int init;
43 pthread_t thread;
44 int signalled;
45 int stop;
46 int stopped;
47 } vcpu_info[4];
49 static void sig_ipi_handler(int n)
53 void kvm_update_interrupt_request(CPUState *env)
55 if (env && env != vcpu_env) {
56 if (vcpu_info[env->cpu_index].signalled)
57 return;
58 vcpu_info[env->cpu_index].signalled = 1;
59 if (vcpu_info[env->cpu_index].thread)
60 pthread_kill(vcpu_info[env->cpu_index].thread, SIG_IPI);
64 void kvm_update_after_sipi(CPUState *env)
66 vcpu_info[env->cpu_index].sipi_needed = 1;
67 kvm_update_interrupt_request(env);
70 * the qemu bios waits using a busy loop that's much too short for
71 * kvm. add a wait after the first sipi.
74 static int first_sipi = 1;
76 if (first_sipi) {
77 wait_hack = 1;
78 first_sipi = 0;
83 void kvm_apic_init(CPUState *env)
85 if (env->cpu_index != 0)
86 vcpu_info[env->cpu_index].init = 1;
87 kvm_update_interrupt_request(env);
90 #include <signal.h>
92 static int try_push_interrupts(void *opaque)
94 return kvm_arch_try_push_interrupts(opaque);
97 static void post_kvm_run(void *opaque, int vcpu)
100 pthread_mutex_lock(&qemu_mutex);
101 kvm_arch_post_kvm_run(opaque, vcpu);
104 static int pre_kvm_run(void *opaque, int vcpu)
106 CPUState *env = cpu_single_env;
108 if (env->cpu_index == 0 && wait_hack) {
109 int i;
111 wait_hack = 0;
113 pthread_mutex_unlock(&qemu_mutex);
114 for (i = 0; i < 10; ++i)
115 usleep(1000);
116 pthread_mutex_lock(&qemu_mutex);
119 kvm_arch_pre_kvm_run(opaque, vcpu);
121 if (env->interrupt_request & CPU_INTERRUPT_EXIT)
122 return 1;
123 pthread_mutex_unlock(&qemu_mutex);
124 return 0;
127 void kvm_load_registers(CPUState *env)
129 if (kvm_allowed)
130 kvm_arch_load_regs(env);
133 void kvm_save_registers(CPUState *env)
135 if (kvm_allowed)
136 kvm_arch_save_regs(env);
139 int kvm_cpu_exec(CPUState *env)
141 int r;
143 r = kvm_run(kvm_context, env->cpu_index);
144 if (r < 0) {
145 printf("kvm_run returned %d\n", r);
146 exit(1);
149 return 0;
152 extern int vm_running;
154 static int has_work(CPUState *env)
156 if (!vm_running)
157 return 0;
158 if (!(env->hflags & HF_HALTED_MASK))
159 return 1;
160 return kvm_arch_has_work(env);
163 static int kvm_eat_signal(CPUState *env, int timeout)
165 struct timespec ts;
166 int r, e, ret = 0;
167 siginfo_t siginfo;
168 struct sigaction sa;
170 ts.tv_sec = timeout / 1000;
171 ts.tv_nsec = (timeout % 1000) * 1000000;
172 r = sigtimedwait(&io_sigset, &siginfo, &ts);
173 if (r == -1 && (errno == EAGAIN || errno == EINTR) && !timeout)
174 return 0;
175 e = errno;
176 pthread_mutex_lock(&qemu_mutex);
177 cpu_single_env = vcpu_env;
178 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
179 printf("sigtimedwait: %s\n", strerror(e));
180 exit(1);
182 if (r != -1) {
183 sigaction(siginfo.si_signo, NULL, &sa);
184 sa.sa_handler(siginfo.si_signo);
185 ret = 1;
187 pthread_mutex_unlock(&qemu_mutex);
189 return ret;
193 static void kvm_eat_signals(CPUState *env, int timeout)
195 int r = 0;
197 while (kvm_eat_signal(env, 0))
198 r = 1;
199 if (!r && timeout) {
200 r = kvm_eat_signal(env, timeout);
201 if (r)
202 while (kvm_eat_signal(env, 0))
206 * we call select() even if no signal was received, to account for
207 * for which there is no signal handler installed.
209 pthread_mutex_lock(&qemu_mutex);
210 cpu_single_env = vcpu_env;
211 main_loop_wait(0);
212 pthread_mutex_unlock(&qemu_mutex);
215 static void kvm_main_loop_wait(CPUState *env, int timeout)
217 pthread_mutex_unlock(&qemu_mutex);
218 if (env->cpu_index == 0)
219 kvm_eat_signals(env, timeout);
220 else {
221 if (!kvm_irqchip_in_kernel(kvm_context) &&
222 (timeout || vcpu_info[env->cpu_index].stopped)) {
223 sigset_t set;
224 int n;
226 paused:
227 sigemptyset(&set);
228 sigaddset(&set, SIG_IPI);
229 sigwait(&set, &n);
230 } else {
231 struct timespec ts;
232 siginfo_t siginfo;
233 sigset_t set;
235 ts.tv_sec = 0;
236 ts.tv_nsec = 0;
237 sigemptyset(&set);
238 sigaddset(&set, SIG_IPI);
239 sigtimedwait(&set, &siginfo, &ts);
241 if (vcpu_info[env->cpu_index].stop) {
242 vcpu_info[env->cpu_index].stop = 0;
243 vcpu_info[env->cpu_index].stopped = 1;
244 pthread_kill(vcpu_info[0].thread, SIG_IPI);
245 goto paused;
248 pthread_mutex_lock(&qemu_mutex);
249 cpu_single_env = env;
250 vcpu_info[env->cpu_index].signalled = 0;
253 static int all_threads_paused(void)
255 int i;
257 for (i = 1; i < smp_cpus; ++i)
258 if (vcpu_info[i].stopped)
259 return 0;
260 return 1;
263 static void pause_other_threads(void)
265 int i;
267 for (i = 1; i < smp_cpus; ++i) {
268 vcpu_info[i].stop = 1;
269 pthread_kill(vcpu_info[i].thread, SIG_IPI);
271 while (!all_threads_paused())
272 kvm_eat_signals(vcpu_env, 0);
275 static void resume_other_threads(void)
277 int i;
279 for (i = 1; i < smp_cpus; ++i) {
280 vcpu_info[i].stop = 0;
281 vcpu_info[i].stopped = 0;
282 pthread_kill(vcpu_info[i].thread, SIG_IPI);
286 static void kvm_vm_state_change_handler(void *context, int running)
288 if (running)
289 resume_other_threads();
290 else
291 pause_other_threads();
294 static void update_regs_for_sipi(CPUState *env)
296 kvm_arch_update_regs_for_sipi(env);
297 vcpu_info[env->cpu_index].sipi_needed = 0;
298 vcpu_info[env->cpu_index].init = 0;
301 static void update_regs_for_init(CPUState *env)
303 cpu_reset(env);
304 kvm_arch_load_regs(env);
307 static void setup_kernel_sigmask(CPUState *env)
309 sigset_t set;
311 sigprocmask(SIG_BLOCK, NULL, &set);
312 sigdelset(&set, SIG_IPI);
313 if (env->cpu_index == 0)
314 sigandset(&set, &set, &io_negsigset);
316 kvm_set_signal_mask(kvm_context, env->cpu_index, &set);
319 static int kvm_main_loop_cpu(CPUState *env)
321 struct vcpu_info *info = &vcpu_info[env->cpu_index];
323 setup_kernel_sigmask(env);
324 pthread_mutex_lock(&qemu_mutex);
326 kvm_qemu_init_env(env);
327 env->ready_for_interrupt_injection = 1;
329 cpu_single_env = env;
330 kvm_tpr_opt_setup(env);
331 while (1) {
332 while (!has_work(env))
333 kvm_main_loop_wait(env, 10);
334 if (env->interrupt_request & CPU_INTERRUPT_HARD)
335 env->hflags &= ~HF_HALTED_MASK;
336 if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
337 update_regs_for_sipi(env);
338 if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
339 update_regs_for_init(env);
340 if (!(env->hflags & HF_HALTED_MASK) && !info->init)
341 kvm_cpu_exec(env);
342 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
343 kvm_main_loop_wait(env, 0);
344 if (qemu_shutdown_requested())
345 break;
346 else if (qemu_powerdown_requested())
347 qemu_system_powerdown();
348 else if (qemu_reset_requested()) {
349 env->interrupt_request = 0;
350 qemu_system_reset();
351 kvm_arch_load_regs(env);
354 pthread_mutex_unlock(&qemu_mutex);
355 return 0;
358 static void *ap_main_loop(void *_env)
360 CPUState *env = _env;
361 sigset_t signals;
363 vcpu_env = env;
364 sigfillset(&signals);
365 //sigdelset(&signals, SIG_IPI);
366 sigprocmask(SIG_BLOCK, &signals, NULL);
367 kvm_create_vcpu(kvm_context, env->cpu_index);
368 kvm_qemu_init_env(env);
369 if (kvm_irqchip_in_kernel(kvm_context))
370 env->hflags &= ~HF_HALTED_MASK;
371 kvm_main_loop_cpu(env);
372 return NULL;
375 static void kvm_add_signal(int signum)
377 sigaddset(&io_sigset, signum);
378 sigdelset(&io_negsigset, signum);
379 sigprocmask(SIG_BLOCK, &io_sigset, NULL);
382 int kvm_init_ap(void)
384 CPUState *env = first_cpu->next_cpu;
385 int i;
387 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
388 sigemptyset(&io_sigset);
389 sigfillset(&io_negsigset);
390 kvm_add_signal(SIGIO);
391 kvm_add_signal(SIGALRM);
392 kvm_add_signal(SIGUSR2);
393 if (!kvm_irqchip_in_kernel(kvm_context))
394 kvm_add_signal(SIG_IPI);
396 vcpu_env = first_cpu;
397 signal(SIG_IPI, sig_ipi_handler);
398 for (i = 1; i < smp_cpus; ++i) {
399 pthread_create(&vcpu_info[i].thread, NULL, ap_main_loop, env);
400 env = env->next_cpu;
402 return 0;
405 int kvm_main_loop(void)
407 vcpu_info[0].thread = pthread_self();
408 return kvm_main_loop_cpu(first_cpu);
411 static int kvm_debug(void *opaque, int vcpu)
413 CPUState *env = cpu_single_env;
415 env->exception_index = EXCP_DEBUG;
416 return 1;
419 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
421 *data = cpu_inb(0, addr);
422 return 0;
425 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
427 *data = cpu_inw(0, addr);
428 return 0;
431 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
433 *data = cpu_inl(0, addr);
434 return 0;
437 #define PM_IO_BASE 0xb000
439 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
441 if (addr == 0xb2) {
442 switch (data) {
443 case 0: {
444 cpu_outb(0, 0xb3, 0);
445 break;
447 case 0xf0: {
448 unsigned x;
450 /* enable acpi */
451 x = cpu_inw(0, PM_IO_BASE + 4);
452 x &= ~1;
453 cpu_outw(0, PM_IO_BASE + 4, x);
454 break;
456 case 0xf1: {
457 unsigned x;
459 /* enable acpi */
460 x = cpu_inw(0, PM_IO_BASE + 4);
461 x |= 1;
462 cpu_outw(0, PM_IO_BASE + 4, x);
463 break;
465 default:
466 break;
468 return 0;
470 cpu_outb(0, addr, data);
471 return 0;
474 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
476 cpu_outw(0, addr, data);
477 return 0;
480 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
482 cpu_outl(0, addr, data);
483 return 0;
486 static int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t *data, int len)
488 cpu_physical_memory_rw(addr, data, len, 0);
489 return 0;
492 static int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)
494 cpu_physical_memory_rw(addr, data, len, 1);
495 return 0;
498 static int kvm_io_window(void *opaque)
500 return 1;
504 static int kvm_halt(void *opaque, int vcpu)
506 return kvm_arch_halt(opaque, vcpu);
509 static int kvm_shutdown(void *opaque, int vcpu)
511 qemu_system_reset_request();
512 return 1;
515 static int handle_tpr_access(void *opaque, int vcpu,
516 uint64_t rip, int is_write)
518 kvm_tpr_access_report(cpu_single_env, rip, is_write);
519 return 0;
522 static struct kvm_callbacks qemu_kvm_ops = {
523 .debug = kvm_debug,
524 .inb = kvm_inb,
525 .inw = kvm_inw,
526 .inl = kvm_inl,
527 .outb = kvm_outb,
528 .outw = kvm_outw,
529 .outl = kvm_outl,
530 .mmio_read = kvm_mmio_read,
531 .mmio_write = kvm_mmio_write,
532 .halt = kvm_halt,
533 .shutdown = kvm_shutdown,
534 .io_window = kvm_io_window,
535 .try_push_interrupts = try_push_interrupts,
536 .post_kvm_run = post_kvm_run,
537 .pre_kvm_run = pre_kvm_run,
538 .tpr_access = handle_tpr_access,
541 int kvm_qemu_init()
543 /* Try to initialize kvm */
544 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
545 if (!kvm_context) {
546 return -1;
549 return 0;
552 int kvm_qemu_create_context(void)
554 int r;
555 if (!kvm_irqchip) {
556 kvm_disable_irqchip_creation(kvm_context);
558 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
559 kvm_qemu_destroy();
560 return -1;
562 r = kvm_arch_qemu_create_context();
563 if(r <0)
564 kvm_qemu_destroy();
565 return 0;
568 void kvm_qemu_destroy(void)
570 kvm_finalize(kvm_context);
573 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
574 unsigned long size,
575 unsigned long phys_offset)
577 #ifdef KVM_CAP_USER_MEMORY
578 int r = 0;
580 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
581 if (r) {
582 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
583 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
584 if (r)
585 return;
586 r = kvm_is_intersecting_mem(kvm_context, start_addr);
587 if (r)
588 kvm_create_mem_hole(kvm_context, start_addr, size);
589 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
590 phys_ram_base + phys_offset,
591 size, 0);
593 if (phys_offset & IO_MEM_ROM) {
594 phys_offset &= ~IO_MEM_ROM;
595 r = kvm_is_intersecting_mem(kvm_context, start_addr);
596 if (r)
597 kvm_create_mem_hole(kvm_context, start_addr, size);
598 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
599 phys_ram_base + phys_offset,
600 size, 0);
602 if (r < 0) {
603 printf("kvm_cpu_register_physical_memory: failed\n");
604 exit(1);
606 return;
608 #endif
609 if (phys_offset & IO_MEM_ROM) {
610 phys_offset &= ~IO_MEM_ROM;
611 memcpy(phys_ram_base + start_addr, phys_ram_base + phys_offset, size);
615 int kvm_qemu_check_extension(int ext)
617 return kvm_check_extension(kvm_context, ext);
620 int kvm_qemu_init_env(CPUState *cenv)
622 return kvm_arch_qemu_init_env(cenv);
625 int kvm_update_debugger(CPUState *env)
627 struct kvm_debug_guest dbg;
628 int i;
630 dbg.enabled = 0;
631 if (env->nb_breakpoints || env->singlestep_enabled) {
632 dbg.enabled = 1;
633 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
634 dbg.breakpoints[i].enabled = 1;
635 dbg.breakpoints[i].address = env->breakpoints[i];
637 dbg.singlestep = env->singlestep_enabled;
639 return kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
644 * dirty pages logging
646 /* FIXME: use unsigned long pointer instead of unsigned char */
647 unsigned char *kvm_dirty_bitmap = NULL;
648 int kvm_physical_memory_set_dirty_tracking(int enable)
650 int r = 0;
652 if (!kvm_allowed)
653 return 0;
655 if (enable) {
656 if (!kvm_dirty_bitmap) {
657 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
658 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
659 if (kvm_dirty_bitmap == NULL) {
660 perror("Failed to allocate dirty pages bitmap");
661 r=-1;
663 else {
664 r = kvm_dirty_pages_log_enable_all(kvm_context);
668 else {
669 if (kvm_dirty_bitmap) {
670 r = kvm_dirty_pages_log_reset(kvm_context);
671 qemu_free(kvm_dirty_bitmap);
672 kvm_dirty_bitmap = NULL;
675 return r;
678 /* get kvm's dirty pages bitmap and update qemu's */
679 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
680 unsigned char *bitmap,
681 unsigned int offset,
682 unsigned long mem_size)
684 unsigned int i, j, n=0;
685 unsigned char c;
686 unsigned page_number, addr, addr1;
687 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
690 * bitmap-traveling is faster than memory-traveling (for addr...)
691 * especially when most of the memory is not dirty.
693 for (i=0; i<len; i++) {
694 c = bitmap[i];
695 while (c>0) {
696 j = ffsl(c) - 1;
697 c &= ~(1u<<j);
698 page_number = i * 8 + j;
699 addr1 = page_number * TARGET_PAGE_SIZE;
700 addr = offset + addr1;
701 cpu_physical_memory_set_dirty(addr);
702 n++;
705 return 0;
707 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
708 void *bitmap, void *opaque)
710 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
714 * get kvm's dirty pages bitmap and update qemu's
715 * we only care about physical ram, which resides in slots 0 and 3
717 int kvm_update_dirty_pages_log(void)
719 int r = 0;
722 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
723 kvm_dirty_bitmap, NULL,
724 kvm_get_dirty_bitmap_cb);
725 return r;
728 int kvm_get_phys_ram_bitmap_cb(unsigned long start, unsigned long len,
729 void *local_bitmap, void *qemu_bitmap)
731 unsigned int bsize = ((len/TARGET_PAGE_SIZE) + 7) / 8;
732 unsigned int offset = ((start/TARGET_PAGE_SIZE) + 7) / 8;
734 memcpy(qemu_bitmap + offset, local_bitmap, bsize);
736 return 0;
739 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
741 int r=0;
742 void *local_bitmap;
743 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
745 local_bitmap = qemu_malloc(bsize);
746 if (!local_bitmap) {
747 fprintf(stderr, "could not allocate memory for phys_page bitmap\n");
748 return 1;
751 r = kvm_get_mem_map_range(kvm_context, 0, phys_ram_size,
752 local_bitmap, bitmap,
753 kvm_get_phys_ram_bitmap_cb);
755 qemu_free(local_bitmap);
756 return r;
759 #ifdef KVM_CAP_IRQCHIP
761 int kvm_set_irq(int irq, int level)
763 return kvm_set_irq_level(kvm_context, irq, level);
766 #endif
768 #endif