Use DVD-ROM as model name for INQUIRY and IDENTIFY DEVICE
[qemu-kvm/fedora.git] / qemu-kvm.c
blob77bc092a33ea5627cb795d4ed3b30c398b9d282b
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 while (1) {
331 while (!has_work(env))
332 kvm_main_loop_wait(env, 10);
333 if (env->interrupt_request & CPU_INTERRUPT_HARD)
334 env->hflags &= ~HF_HALTED_MASK;
335 if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
336 update_regs_for_sipi(env);
337 if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
338 update_regs_for_init(env);
339 if (!(env->hflags & HF_HALTED_MASK) && !info->init)
340 kvm_cpu_exec(env);
341 env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
342 kvm_main_loop_wait(env, 0);
343 if (qemu_shutdown_requested())
344 break;
345 else if (qemu_powerdown_requested())
346 qemu_system_powerdown();
347 else if (qemu_reset_requested()) {
348 env->interrupt_request = 0;
349 qemu_system_reset();
350 kvm_arch_load_regs(env);
353 pthread_mutex_unlock(&qemu_mutex);
354 return 0;
357 static void *ap_main_loop(void *_env)
359 CPUState *env = _env;
360 sigset_t signals;
362 vcpu_env = env;
363 sigfillset(&signals);
364 //sigdelset(&signals, SIG_IPI);
365 sigprocmask(SIG_BLOCK, &signals, NULL);
366 kvm_create_vcpu(kvm_context, env->cpu_index);
367 kvm_qemu_init_env(env);
368 if (kvm_irqchip_in_kernel(kvm_context))
369 env->hflags &= ~HF_HALTED_MASK;
370 kvm_main_loop_cpu(env);
371 return NULL;
374 static void kvm_add_signal(int signum)
376 sigaddset(&io_sigset, signum);
377 sigdelset(&io_negsigset, signum);
378 sigprocmask(SIG_BLOCK, &io_sigset, NULL);
381 int kvm_init_ap(void)
383 CPUState *env = first_cpu->next_cpu;
384 int i;
386 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
387 sigemptyset(&io_sigset);
388 sigfillset(&io_negsigset);
389 kvm_add_signal(SIGIO);
390 kvm_add_signal(SIGALRM);
391 kvm_add_signal(SIGUSR2);
392 if (!kvm_irqchip_in_kernel(kvm_context))
393 kvm_add_signal(SIG_IPI);
395 vcpu_env = first_cpu;
396 signal(SIG_IPI, sig_ipi_handler);
397 for (i = 1; i < smp_cpus; ++i) {
398 pthread_create(&vcpu_info[i].thread, NULL, ap_main_loop, env);
399 env = env->next_cpu;
401 return 0;
404 int kvm_main_loop(void)
406 vcpu_info[0].thread = pthread_self();
407 return kvm_main_loop_cpu(first_cpu);
410 static int kvm_debug(void *opaque, int vcpu)
412 CPUState *env = cpu_single_env;
414 env->exception_index = EXCP_DEBUG;
415 return 1;
418 static int kvm_inb(void *opaque, uint16_t addr, uint8_t *data)
420 *data = cpu_inb(0, addr);
421 return 0;
424 static int kvm_inw(void *opaque, uint16_t addr, uint16_t *data)
426 *data = cpu_inw(0, addr);
427 return 0;
430 static int kvm_inl(void *opaque, uint16_t addr, uint32_t *data)
432 *data = cpu_inl(0, addr);
433 return 0;
436 #define PM_IO_BASE 0xb000
438 static int kvm_outb(void *opaque, uint16_t addr, uint8_t data)
440 if (addr == 0xb2) {
441 switch (data) {
442 case 0: {
443 cpu_outb(0, 0xb3, 0);
444 break;
446 case 0xf0: {
447 unsigned x;
449 /* enable acpi */
450 x = cpu_inw(0, PM_IO_BASE + 4);
451 x &= ~1;
452 cpu_outw(0, PM_IO_BASE + 4, x);
453 break;
455 case 0xf1: {
456 unsigned x;
458 /* enable acpi */
459 x = cpu_inw(0, PM_IO_BASE + 4);
460 x |= 1;
461 cpu_outw(0, PM_IO_BASE + 4, x);
462 break;
464 default:
465 break;
467 return 0;
469 cpu_outb(0, addr, data);
470 return 0;
473 static int kvm_outw(void *opaque, uint16_t addr, uint16_t data)
475 cpu_outw(0, addr, data);
476 return 0;
479 static int kvm_outl(void *opaque, uint16_t addr, uint32_t data)
481 cpu_outl(0, addr, data);
482 return 0;
485 static int kvm_mmio_read(void *opaque, uint64_t addr,
486 uint8_t *data, int len, int is_write)
488 cpu_physical_memory_rw(addr, data, len, 0);
489 return 0;
492 static int kvm_mmio_write(void *opaque, uint64_t addr,
493 uint8_t *data, int len, int is_write)
495 cpu_physical_memory_rw(addr, data, len, 1);
496 return 0;
499 static int kvm_io_window(void *opaque)
501 return 1;
505 static int kvm_halt(void *opaque, int vcpu)
507 return kvm_arch_halt(opaque, vcpu);
510 static int kvm_shutdown(void *opaque, int vcpu)
512 qemu_system_reset_request();
513 return 1;
516 static struct kvm_callbacks qemu_kvm_ops = {
517 .debug = kvm_debug,
518 .inb = kvm_inb,
519 .inw = kvm_inw,
520 .inl = kvm_inl,
521 .outb = kvm_outb,
522 .outw = kvm_outw,
523 .outl = kvm_outl,
524 .mmio_read = kvm_mmio_read,
525 .mmio_write = kvm_mmio_write,
526 .halt = kvm_halt,
527 .shutdown = kvm_shutdown,
528 .io_window = kvm_io_window,
529 .try_push_interrupts = try_push_interrupts,
530 .post_kvm_run = post_kvm_run,
531 .pre_kvm_run = pre_kvm_run,
534 int kvm_qemu_init()
536 /* Try to initialize kvm */
537 kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
538 if (!kvm_context) {
539 return -1;
542 return 0;
545 int kvm_qemu_create_context(void)
547 int r;
548 if (!kvm_irqchip) {
549 kvm_disable_irqchip_creation(kvm_context);
551 if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
552 kvm_qemu_destroy();
553 return -1;
555 r = kvm_arch_qemu_create_context();
556 if(r <0)
557 kvm_qemu_destroy();
558 return 0;
561 void kvm_qemu_destroy(void)
563 kvm_finalize(kvm_context);
566 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
567 unsigned long size,
568 unsigned long phys_offset)
570 #ifdef KVM_CAP_USER_MEMORY
571 int r = 0;
573 r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY);
574 if (r) {
575 if (!(phys_offset & ~TARGET_PAGE_MASK)) {
576 r = kvm_is_allocated_mem(kvm_context, start_addr, size);
577 if (r)
578 return;
579 r = kvm_is_intersecting_mem(kvm_context, start_addr);
580 if (r)
581 kvm_create_mem_hole(kvm_context, start_addr, size);
582 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
583 phys_ram_base + phys_offset,
584 size, 0);
586 if (phys_offset & IO_MEM_ROM) {
587 phys_offset &= ~IO_MEM_ROM;
588 r = kvm_is_intersecting_mem(kvm_context, start_addr);
589 if (r)
590 kvm_create_mem_hole(kvm_context, start_addr, size);
591 r = kvm_register_userspace_phys_mem(kvm_context, start_addr,
592 phys_ram_base + phys_offset,
593 size, 0);
595 if (r < 0) {
596 printf("kvm_cpu_register_physical_memory: failed\n");
597 exit(1);
599 return;
601 #endif
602 if (phys_offset & IO_MEM_ROM) {
603 phys_offset &= ~IO_MEM_ROM;
604 memcpy(phys_ram_base + start_addr, phys_ram_base + phys_offset, size);
608 int kvm_qemu_check_extension(int ext)
610 return kvm_check_extension(kvm_context, ext);
613 int kvm_qemu_init_env(CPUState *cenv)
615 return kvm_arch_qemu_init_env(cenv);
618 int kvm_update_debugger(CPUState *env)
620 struct kvm_debug_guest dbg;
621 int i;
623 dbg.enabled = 0;
624 if (env->nb_breakpoints || env->singlestep_enabled) {
625 dbg.enabled = 1;
626 for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
627 dbg.breakpoints[i].enabled = 1;
628 dbg.breakpoints[i].address = env->breakpoints[i];
630 dbg.singlestep = env->singlestep_enabled;
632 return kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
637 * dirty pages logging
639 /* FIXME: use unsigned long pointer instead of unsigned char */
640 unsigned char *kvm_dirty_bitmap = NULL;
641 int kvm_physical_memory_set_dirty_tracking(int enable)
643 int r = 0;
645 if (!kvm_allowed)
646 return 0;
648 if (enable) {
649 if (!kvm_dirty_bitmap) {
650 unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
651 kvm_dirty_bitmap = qemu_malloc(bitmap_size);
652 if (kvm_dirty_bitmap == NULL) {
653 perror("Failed to allocate dirty pages bitmap");
654 r=-1;
656 else {
657 r = kvm_dirty_pages_log_enable_all(kvm_context);
661 else {
662 if (kvm_dirty_bitmap) {
663 r = kvm_dirty_pages_log_reset(kvm_context);
664 qemu_free(kvm_dirty_bitmap);
665 kvm_dirty_bitmap = NULL;
668 return r;
671 /* get kvm's dirty pages bitmap and update qemu's */
672 int kvm_get_dirty_pages_log_range(unsigned long start_addr,
673 unsigned char *bitmap,
674 unsigned int offset,
675 unsigned long mem_size)
677 unsigned int i, j, n=0;
678 unsigned char c;
679 unsigned page_number, addr, addr1;
680 unsigned int len = ((mem_size/TARGET_PAGE_SIZE) + 7) / 8;
683 * bitmap-traveling is faster than memory-traveling (for addr...)
684 * especially when most of the memory is not dirty.
686 for (i=0; i<len; i++) {
687 c = bitmap[i];
688 while (c>0) {
689 j = ffsl(c) - 1;
690 c &= ~(1u<<j);
691 page_number = i * 8 + j;
692 addr1 = page_number * TARGET_PAGE_SIZE;
693 addr = offset + addr1;
694 cpu_physical_memory_set_dirty(addr);
695 n++;
698 return 0;
700 int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
701 void *bitmap, void *opaque)
703 return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
707 * get kvm's dirty pages bitmap and update qemu's
708 * we only care about physical ram, which resides in slots 0 and 3
710 int kvm_update_dirty_pages_log(void)
712 int r = 0;
715 r = kvm_get_dirty_pages_range(kvm_context, 0, phys_ram_size,
716 kvm_dirty_bitmap, NULL,
717 kvm_get_dirty_bitmap_cb);
718 return r;
721 int kvm_get_phys_ram_bitmap_cb(unsigned long start, unsigned long len,
722 void *local_bitmap, void *qemu_bitmap)
724 unsigned int bsize = ((len/TARGET_PAGE_SIZE) + 7) / 8;
725 unsigned int offset = ((start/TARGET_PAGE_SIZE) + 7) / 8;
727 memcpy(qemu_bitmap + offset, local_bitmap, bsize);
729 return 0;
732 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap)
734 int r=0;
735 void *local_bitmap;
736 unsigned int bsize = BITMAP_SIZE(phys_ram_size);
738 local_bitmap = qemu_malloc(bsize);
739 if (!local_bitmap) {
740 fprintf(stderr, "could not allocate memory for phys_page bitmap\n");
741 return 1;
744 r = kvm_get_mem_map_range(kvm_context, 0, phys_ram_size,
745 local_bitmap, bitmap,
746 kvm_get_phys_ram_bitmap_cb);
748 qemu_free(local_bitmap);
749 return r;
752 #ifdef KVM_CAP_IRQCHIP
754 int kvm_set_irq(int irq, int level)
756 return kvm_set_irq_level(kvm_context, irq, level);
759 #endif
761 #endif