master
c 113 lines 2.82 KB
Raw
1 /*
2 * QEMU Windows Hypervisor Platform accelerator (WHPX)
3 *
4 * Copyright Microsoft Corp. 2017
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 *
9 */
10
11 #include "qemu/osdep.h"
12 #include "system/kvm_int.h"
13 #include "qemu/main-loop.h"
14 #include "accel/accel-cpu-ops.h"
15 #include "system/cpus.h"
16 #include "qemu/guest-random.h"
17
18 #include "system/whpx.h"
19 #include "system/whpx-internal.h"
20 #include "system/whpx-all.h"
21 #include "system/whpx-accel-ops.h"
22
23 static void *whpx_cpu_thread_fn(void *arg)
24 {
25 CPUState *cpu = arg;
26 int r;
27
28 rcu_register_thread();
29
30 bql_lock();
31 qemu_thread_get_self(cpu->thread);
32 cpu->thread_id = qemu_get_thread_id();
33 current_cpu = cpu;
34
35 r = whpx_init_vcpu(cpu);
36 if (r < 0) {
37 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
38 exit(1);
39 }
40
41 /* signal CPU creation */
42 cpu_thread_signal_created(cpu);
43 qemu_guest_random_seed_thread_part2(cpu->random_seed);
44
45 do {
46 qemu_process_cpu_events(cpu);
47
48 if (cpu_can_run(cpu)) {
49 r = whpx_vcpu_exec(cpu);
50 if (r == EXCP_DEBUG) {
51 cpu_handle_guest_debug(cpu);
52 }
53 }
54 } while (!cpu->unplug || cpu_can_run(cpu));
55
56 whpx_destroy_vcpu(cpu);
57 cpu_thread_signal_destroyed(cpu);
58 bql_unlock();
59 rcu_unregister_thread();
60 return NULL;
61 }
62
63 static void whpx_start_vcpu_thread(CPUState *cpu)
64 {
65 char thread_name[VCPU_THREAD_NAME_SIZE];
66
67 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
68 cpu->cpu_index);
69 qemu_thread_create(cpu->thread, thread_name, whpx_cpu_thread_fn,
70 cpu, QEMU_THREAD_JOINABLE);
71 }
72
73 static void whpx_kick_vcpu_thread(CPUState *cpu)
74 {
75 if (!qemu_cpu_is_self(cpu)) {
76 whpx_vcpu_kick(cpu);
77 }
78 }
79
80 static bool whpx_vcpu_thread_is_idle(CPUState *cpu)
81 {
82 return !whpx_irqchip_in_kernel();
83 }
84
85
86 static void whpx_accel_ops_class_init(ObjectClass *oc, const void *data)
87 {
88 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
89
90 ops->create_vcpu_thread = whpx_start_vcpu_thread;
91 ops->kick_vcpu_thread = whpx_kick_vcpu_thread;
92 ops->cpu_thread_is_idle = whpx_vcpu_thread_is_idle;
93 ops->handle_interrupt = generic_handle_interrupt;
94
95 ops->synchronize_post_reset = whpx_cpu_synchronize_post_reset;
96 ops->synchronize_post_init = whpx_cpu_synchronize_post_init;
97 ops->synchronize_state = whpx_cpu_synchronize_state;
98 ops->synchronize_pre_loadvm = whpx_cpu_synchronize_pre_loadvm;
99 }
100
101 static const TypeInfo whpx_accel_ops_type = {
102 .name = ACCEL_OPS_NAME("whpx"),
103
104 .parent = TYPE_ACCEL_OPS,
105 .class_init = whpx_accel_ops_class_init,
106 .abstract = true,
107 };
108
109 static void whpx_accel_ops_register_types(void)
110 {
111 type_register_static(&whpx_accel_ops_type);
112 }
113 type_init(whpx_accel_ops_register_types);