Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging
[qemu/ar7.git] / accel / kvm / kvm-accel-ops.c
blobfbf4fe34975d046788218e0c279c78f1282d7abe
1 /*
2 * QEMU KVM support
4 * Copyright IBM, Corp. 2008
5 * Red Hat, Inc. 2008
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Glauber Costa <gcosta@redhat.com>
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.
16 #include "qemu/osdep.h"
17 #include "qemu/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "sysemu/kvm.h"
20 #include "sysemu/kvm_int.h"
21 #include "sysemu/runstate.h"
22 #include "sysemu/cpus.h"
23 #include "qemu/guest-random.h"
24 #include "qapi/error.h"
26 #include <linux/kvm.h>
27 #include "kvm-cpus.h"
29 static void *kvm_vcpu_thread_fn(void *arg)
31 CPUState *cpu = arg;
32 int r;
34 rcu_register_thread();
36 qemu_mutex_lock_iothread();
37 qemu_thread_get_self(cpu->thread);
38 cpu->thread_id = qemu_get_thread_id();
39 cpu->can_do_io = 1;
40 current_cpu = cpu;
42 r = kvm_init_vcpu(cpu, &error_fatal);
43 kvm_init_cpu_signals(cpu);
45 /* signal CPU creation */
46 cpu_thread_signal_created(cpu);
47 qemu_guest_random_seed_thread_part2(cpu->random_seed);
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);
56 qemu_wait_io_event(cpu);
57 } while (!cpu->unplug || cpu_can_run(cpu));
59 kvm_destroy_vcpu(cpu);
60 cpu_thread_signal_destroyed(cpu);
61 qemu_mutex_unlock_iothread();
62 rcu_unregister_thread();
63 return NULL;
66 static void kvm_start_vcpu_thread(CPUState *cpu)
68 char thread_name[VCPU_THREAD_NAME_SIZE];
70 cpu->thread = g_malloc0(sizeof(QemuThread));
71 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
72 qemu_cond_init(cpu->halt_cond);
73 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
74 cpu->cpu_index);
75 qemu_thread_create(cpu->thread, thread_name, kvm_vcpu_thread_fn,
76 cpu, QEMU_THREAD_JOINABLE);
79 static bool kvm_vcpu_thread_is_idle(CPUState *cpu)
81 return !kvm_halt_in_kernel();
84 static bool kvm_cpus_are_resettable(void)
86 return !kvm_enabled() || kvm_cpu_check_are_resettable();
89 static void kvm_accel_ops_class_init(ObjectClass *oc, void *data)
91 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
93 ops->create_vcpu_thread = kvm_start_vcpu_thread;
94 ops->cpu_thread_is_idle = kvm_vcpu_thread_is_idle;
95 ops->cpus_are_resettable = kvm_cpus_are_resettable;
96 ops->synchronize_post_reset = kvm_cpu_synchronize_post_reset;
97 ops->synchronize_post_init = kvm_cpu_synchronize_post_init;
98 ops->synchronize_state = kvm_cpu_synchronize_state;
99 ops->synchronize_pre_loadvm = kvm_cpu_synchronize_pre_loadvm;
101 #ifdef KVM_CAP_SET_GUEST_DEBUG
102 ops->supports_guest_debug = kvm_supports_guest_debug;
103 ops->insert_breakpoint = kvm_insert_breakpoint;
104 ops->remove_breakpoint = kvm_remove_breakpoint;
105 ops->remove_all_breakpoints = kvm_remove_all_breakpoints;
106 #endif
109 static const TypeInfo kvm_accel_ops_type = {
110 .name = ACCEL_OPS_NAME("kvm"),
112 .parent = TYPE_ACCEL_OPS,
113 .class_init = kvm_accel_ops_class_init,
114 .abstract = true,
117 static void kvm_accel_ops_register_types(void)
119 type_register_static(&kvm_accel_ops_type);
121 type_init(kvm_accel_ops_register_types);