2 * QEMU Windows Hypervisor Platform accelerator (WHPX)
4 * Copyright Microsoft Corp. 2017
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.
11 #include "qemu/osdep.h"
12 #include "sysemu/kvm_int.h"
13 #include "qemu/main-loop.h"
14 #include "sysemu/cpus.h"
15 #include "qemu/guest-random.h"
17 #include "sysemu/whpx.h"
18 #include "whpx-cpus.h"
20 #include <WinHvPlatform.h>
21 #include <WinHvEmulation.h>
23 static void *whpx_cpu_thread_fn(void *arg
)
28 rcu_register_thread();
30 qemu_mutex_lock_iothread();
31 qemu_thread_get_self(cpu
->thread
);
32 cpu
->thread_id
= qemu_get_thread_id();
35 r
= whpx_init_vcpu(cpu
);
37 fprintf(stderr
, "whpx_init_vcpu failed: %s\n", strerror(-r
));
41 /* signal CPU creation */
42 cpu_thread_signal_created(cpu
);
43 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
46 if (cpu_can_run(cpu
)) {
47 r
= whpx_vcpu_exec(cpu
);
48 if (r
== EXCP_DEBUG
) {
49 cpu_handle_guest_debug(cpu
);
52 while (cpu_thread_is_idle(cpu
)) {
53 qemu_cond_wait_iothread(cpu
->halt_cond
);
55 qemu_wait_io_event_common(cpu
);
56 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
58 whpx_destroy_vcpu(cpu
);
59 cpu_thread_signal_destroyed(cpu
);
60 qemu_mutex_unlock_iothread();
61 rcu_unregister_thread();
65 static void whpx_start_vcpu_thread(CPUState
*cpu
)
67 char thread_name
[VCPU_THREAD_NAME_SIZE
];
69 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
70 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
71 qemu_cond_init(cpu
->halt_cond
);
72 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/WHPX",
74 qemu_thread_create(cpu
->thread
, thread_name
, whpx_cpu_thread_fn
,
75 cpu
, QEMU_THREAD_JOINABLE
);
77 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
81 static void whpx_kick_vcpu_thread(CPUState
*cpu
)
83 if (!qemu_cpu_is_self(cpu
)) {
88 const CpusAccel whpx_cpus
= {
89 .create_vcpu_thread
= whpx_start_vcpu_thread
,
90 .kick_vcpu_thread
= whpx_kick_vcpu_thread
,
92 .synchronize_post_reset
= whpx_cpu_synchronize_post_reset
,
93 .synchronize_post_init
= whpx_cpu_synchronize_post_init
,
94 .synchronize_state
= whpx_cpu_synchronize_state
,
95 .synchronize_pre_loadvm
= whpx_cpu_synchronize_pre_loadvm
,