qemu/target/i386/whpx-cpus.c
<<
>>
Prefs
   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 "sysemu/kvm_int.h"
  13#include "qemu/main-loop.h"
  14#include "sysemu/cpus.h"
  15#include "qemu/guest-random.h"
  16
  17#include "sysemu/whpx.h"
  18#include "whpx-cpus.h"
  19
  20#include <WinHvPlatform.h>
  21#include <WinHvEmulation.h>
  22
  23static void *whpx_cpu_thread_fn(void *arg)
  24{
  25    CPUState *cpu = arg;
  26    int r;
  27
  28    rcu_register_thread();
  29
  30    qemu_mutex_lock_iothread();
  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        if (cpu_can_run(cpu)) {
  47            r = whpx_vcpu_exec(cpu);
  48            if (r == EXCP_DEBUG) {
  49                cpu_handle_guest_debug(cpu);
  50            }
  51        }
  52        while (cpu_thread_is_idle(cpu)) {
  53            qemu_cond_wait_iothread(cpu->halt_cond);
  54        }
  55        qemu_wait_io_event_common(cpu);
  56    } while (!cpu->unplug || cpu_can_run(cpu));
  57
  58    whpx_destroy_vcpu(cpu);
  59    cpu_thread_signal_destroyed(cpu);
  60    qemu_mutex_unlock_iothread();
  61    rcu_unregister_thread();
  62    return NULL;
  63}
  64
  65static void whpx_start_vcpu_thread(CPUState *cpu)
  66{
  67    char thread_name[VCPU_THREAD_NAME_SIZE];
  68
  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",
  73             cpu->cpu_index);
  74    qemu_thread_create(cpu->thread, thread_name, whpx_cpu_thread_fn,
  75                       cpu, QEMU_THREAD_JOINABLE);
  76#ifdef _WIN32
  77    cpu->hThread = qemu_thread_get_handle(cpu->thread);
  78#endif
  79}
  80
  81static void whpx_kick_vcpu_thread(CPUState *cpu)
  82{
  83    if (!qemu_cpu_is_self(cpu)) {
  84        whpx_vcpu_kick(cpu);
  85    }
  86}
  87
  88const CpusAccel whpx_cpus = {
  89    .create_vcpu_thread = whpx_start_vcpu_thread,
  90    .kick_vcpu_thread = whpx_kick_vcpu_thread,
  91
  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,
  96};
  97