| 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 "exec/cpu-common.h" |
| 20 | #include "accel/accel-cpu-ops.h" |
| 21 | #include "system/kvm.h" |
| 22 | #include "system/kvm_int.h" |
| 23 | #include "system/runstate.h" |
| 24 | #include "system/cpus.h" |
| 25 | #include "qemu/guest-random.h" |
| 26 | #include "qapi/error.h" |
| 27 | |
| 28 | #include <linux/kvm.h> |
| 29 | #include "kvm-cpus.h" |
| 30 | |
| 31 | static void *kvm_vcpu_thread_fn(void *arg) |
| 32 | { |
| 33 | CPUState *cpu = arg; |
| 34 | int r; |
| 35 | |
| 36 | rcu_register_thread(); |
| 37 | |
| 38 | bql_lock(); |
| 39 | qemu_thread_get_self(cpu->thread); |
| 40 | cpu->thread_id = qemu_get_thread_id(); |
| 41 | current_cpu = cpu; |
| 42 | |
| 43 | r = kvm_init_vcpu(cpu, &error_fatal); |
| 44 | kvm_init_cpu_signals(cpu); |
| 45 | |
| 46 | /* signal CPU creation */ |
| 47 | cpu_thread_signal_created(cpu); |
| 48 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
| 49 | |
| 50 | do { |
| 51 | qemu_process_cpu_events(cpu); |
| 52 | |
| 53 | if (cpu_can_run(cpu)) { |
| 54 | r = kvm_cpu_exec(cpu); |
| 55 | if (r == EXCP_DEBUG) { |
| 56 | cpu_handle_guest_debug(cpu); |
| 57 | } |
| 58 | } |
| 59 | } while (!cpu->unplug || cpu_can_run(cpu)); |
| 60 | |
| 61 | kvm_destroy_vcpu(cpu); |
| 62 | cpu_thread_signal_destroyed(cpu); |
| 63 | bql_unlock(); |
| 64 | rcu_unregister_thread(); |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | static void kvm_start_vcpu_thread(CPUState *cpu) |
| 69 | { |
| 70 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 71 | |
| 72 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM", |
| 73 | cpu->cpu_index); |
| 74 | qemu_thread_create(cpu->thread, thread_name, kvm_vcpu_thread_fn, |
| 75 | cpu, QEMU_THREAD_JOINABLE); |
| 76 | } |
| 77 | |
| 78 | static bool kvm_vcpu_thread_is_idle(CPUState *cpu) |
| 79 | { |
| 80 | return !kvm_halt_in_kernel(); |
| 81 | } |
| 82 | |
| 83 | static bool kvm_cpus_are_resettable(void) |
| 84 | { |
| 85 | return !kvm_enabled() || !kvm_state->guest_state_protected; |
| 86 | } |
| 87 | |
| 88 | #ifdef TARGET_KVM_HAVE_GUEST_DEBUG |
| 89 | static int kvm_update_guest_debug_ops(CPUState *cpu) |
| 90 | { |
| 91 | return kvm_update_guest_debug(cpu, 0); |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | static void kvm_accel_ops_class_init(ObjectClass *oc, const void *data) |
| 96 | { |
| 97 | AccelOpsClass *ops = ACCEL_OPS_CLASS(oc); |
| 98 | |
| 99 | ops->create_vcpu_thread = kvm_start_vcpu_thread; |
| 100 | ops->cpu_thread_is_idle = kvm_vcpu_thread_is_idle; |
| 101 | ops->cpus_are_resettable = kvm_cpus_are_resettable; |
| 102 | ops->synchronize_post_reset = kvm_cpu_synchronize_post_reset; |
| 103 | ops->synchronize_post_init = kvm_cpu_synchronize_post_init; |
| 104 | ops->synchronize_state = kvm_cpu_synchronize_state; |
| 105 | ops->synchronize_pre_loadvm = kvm_cpu_synchronize_pre_loadvm; |
| 106 | ops->handle_interrupt = generic_handle_interrupt; |
| 107 | |
| 108 | #ifdef TARGET_KVM_HAVE_GUEST_DEBUG |
| 109 | ops->update_guest_debug = kvm_update_guest_debug_ops; |
| 110 | ops->insert_gdbstub_breakpoint = kvm_insert_gdbstub_breakpoint; |
| 111 | ops->remove_gdbstub_breakpoint = kvm_remove_gdbstub_breakpoint; |
| 112 | ops->remove_all_gdbstub_breakpoints = kvm_remove_all_gdbstub_breakpoints; |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | static const TypeInfo kvm_accel_ops_type = { |
| 117 | .name = ACCEL_OPS_NAME("kvm"), |
| 118 | |
| 119 | .parent = TYPE_ACCEL_OPS, |
| 120 | .class_init = kvm_accel_ops_class_init, |
| 121 | .abstract = true, |
| 122 | }; |
| 123 | |
| 124 | static void kvm_accel_ops_register_types(void) |
| 125 | { |
| 126 | type_register_static(&kvm_accel_ops_type); |
| 127 | } |
| 128 | type_init(kvm_accel_ops_register_types); |