1 /* 2 * QEMU KVM support 3 * 4 * Copyright IBM, Corp. 2008 5 * Red Hat, Inc. 2008 6 * 7 * Authors: 8 * Anthony Liguori <aliguori@us.ibm.com> 9 * Glauber Costa <gcosta@redhat.com> 10 * 11 * This work is licensed under the terms of the GNU GPL, version 2 or later. 12 * See the COPYING file in the top-level directory. 13 * 14 */ 15 16 #include "qemu/osdep.h" 17 #include "qemu/error-report.h" 18 #include "qemu/main-loop.h" 19 #include "system/accel-ops.h" 20 #include "system/kvm.h" 21 #include "system/kvm_int.h" 22 #include "system/runstate.h" 23 #include "system/cpus.h" 24 #include "qemu/guest-random.h" 25 #include "qapi/error.h" 26 27 #include <linux/kvm.h> 28 #include "kvm-cpus.h" 29 30 static void *kvm_vcpu_thread_fn(void *arg) 31 { 32 CPUState *cpu = arg; 33 int r; 34 35 rcu_register_thread(); 36 37 bql_lock(); 38 qemu_thread_get_self(cpu->thread); 39 cpu->thread_id = qemu_get_thread_id(); 40 current_cpu = cpu; 41 42 r = kvm_init_vcpu(cpu, &error_fatal); 43 kvm_init_cpu_signals(cpu); 44 45 /* signal CPU creation */ 46 cpu_thread_signal_created(cpu); 47 qemu_guest_random_seed_thread_part2(cpu->random_seed); 48 49 do { 50 if (cpu_can_run(cpu)) { 51 r = kvm_cpu_exec(cpu); 52 if (r == EXCP_DEBUG) { 53 cpu_handle_guest_debug(cpu); 54 } 55 } 56 qemu_wait_io_event(cpu); 57 } while (!cpu->unplug || cpu_can_run(cpu)); 58 59 kvm_destroy_vcpu(cpu); 60 cpu_thread_signal_destroyed(cpu); 61 bql_unlock(); 62 rcu_unregister_thread(); 63 return NULL; 64 } 65 66 static void kvm_start_vcpu_thread(CPUState *cpu) 67 { 68 char thread_name[VCPU_THREAD_NAME_SIZE]; 69 70 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM", 71 cpu->cpu_index); 72 qemu_thread_create(cpu->thread, thread_name, kvm_vcpu_thread_fn, 73 cpu, QEMU_THREAD_JOINABLE); 74 } 75 76 static bool kvm_vcpu_thread_is_idle(CPUState *cpu) 77 { 78 return !kvm_halt_in_kernel(); 79 } 80 81 static bool kvm_cpus_are_resettable(void) 82 { 83 return !kvm_enabled() || !kvm_state->guest_state_protected; 84 } 85 86 #ifdef TARGET_KVM_HAVE_GUEST_DEBUG 87 static int kvm_update_guest_debug_ops(CPUState *cpu) 88 { 89 return kvm_update_guest_debug(cpu, 0); 90 } 91 #endif 92 93 static void kvm_accel_ops_class_init(ObjectClass *oc, void *data) 94 { 95 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc); 96 97 ops->create_vcpu_thread = kvm_start_vcpu_thread; 98 ops->cpu_thread_is_idle = kvm_vcpu_thread_is_idle; 99 ops->cpus_are_resettable = kvm_cpus_are_resettable; 100 ops->synchronize_post_reset = kvm_cpu_synchronize_post_reset; 101 ops->synchronize_post_init = kvm_cpu_synchronize_post_init; 102 ops->synchronize_state = kvm_cpu_synchronize_state; 103 ops->synchronize_pre_loadvm = kvm_cpu_synchronize_pre_loadvm; 104 105 #ifdef TARGET_KVM_HAVE_GUEST_DEBUG 106 ops->update_guest_debug = kvm_update_guest_debug_ops; 107 ops->supports_guest_debug = kvm_supports_guest_debug; 108 ops->insert_breakpoint = kvm_insert_breakpoint; 109 ops->remove_breakpoint = kvm_remove_breakpoint; 110 ops->remove_all_breakpoints = kvm_remove_all_breakpoints; 111 #endif 112 } 113 114 static const TypeInfo kvm_accel_ops_type = { 115 .name = ACCEL_OPS_NAME("kvm"), 116 117 .parent = TYPE_ACCEL_OPS, 118 .class_init = kvm_accel_ops_class_init, 119 .abstract = true, 120 }; 121 122 static void kvm_accel_ops_register_types(void) 123 { 124 type_register_static(&kvm_accel_ops_type); 125 } 126 type_init(kvm_accel_ops_register_types); 127