whpx-accel-ops.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "qemu/osdep.h"
  11. #include "system/kvm_int.h"
  12. #include "qemu/main-loop.h"
  13. #include "system/accel-ops.h"
  14. #include "system/cpus.h"
  15. #include "qemu/guest-random.h"
  16. #include "system/whpx.h"
  17. #include "whpx-internal.h"
  18. #include "whpx-accel-ops.h"
  19. static void *whpx_cpu_thread_fn(void *arg)
  20. {
  21. CPUState *cpu = arg;
  22. int r;
  23. rcu_register_thread();
  24. bql_lock();
  25. qemu_thread_get_self(cpu->thread);
  26. cpu->thread_id = qemu_get_thread_id();
  27. current_cpu = cpu;
  28. r = whpx_init_vcpu(cpu);
  29. if (r < 0) {
  30. fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
  31. exit(1);
  32. }
  33. /* signal CPU creation */
  34. cpu_thread_signal_created(cpu);
  35. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  36. do {
  37. if (cpu_can_run(cpu)) {
  38. r = whpx_vcpu_exec(cpu);
  39. if (r == EXCP_DEBUG) {
  40. cpu_handle_guest_debug(cpu);
  41. }
  42. }
  43. while (cpu_thread_is_idle(cpu)) {
  44. qemu_cond_wait_bql(cpu->halt_cond);
  45. }
  46. qemu_wait_io_event_common(cpu);
  47. } while (!cpu->unplug || cpu_can_run(cpu));
  48. whpx_destroy_vcpu(cpu);
  49. cpu_thread_signal_destroyed(cpu);
  50. bql_unlock();
  51. rcu_unregister_thread();
  52. return NULL;
  53. }
  54. static void whpx_start_vcpu_thread(CPUState *cpu)
  55. {
  56. char thread_name[VCPU_THREAD_NAME_SIZE];
  57. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
  58. cpu->cpu_index);
  59. qemu_thread_create(cpu->thread, thread_name, whpx_cpu_thread_fn,
  60. cpu, QEMU_THREAD_JOINABLE);
  61. }
  62. static void whpx_kick_vcpu_thread(CPUState *cpu)
  63. {
  64. if (!qemu_cpu_is_self(cpu)) {
  65. whpx_vcpu_kick(cpu);
  66. }
  67. }
  68. static bool whpx_vcpu_thread_is_idle(CPUState *cpu)
  69. {
  70. return !whpx_apic_in_platform();
  71. }
  72. static void whpx_accel_ops_class_init(ObjectClass *oc, void *data)
  73. {
  74. AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
  75. ops->create_vcpu_thread = whpx_start_vcpu_thread;
  76. ops->kick_vcpu_thread = whpx_kick_vcpu_thread;
  77. ops->cpu_thread_is_idle = whpx_vcpu_thread_is_idle;
  78. ops->synchronize_post_reset = whpx_cpu_synchronize_post_reset;
  79. ops->synchronize_post_init = whpx_cpu_synchronize_post_init;
  80. ops->synchronize_state = whpx_cpu_synchronize_state;
  81. ops->synchronize_pre_loadvm = whpx_cpu_synchronize_pre_loadvm;
  82. ops->synchronize_pre_resume = whpx_cpu_synchronize_pre_resume;
  83. }
  84. static const TypeInfo whpx_accel_ops_type = {
  85. .name = ACCEL_OPS_NAME("whpx"),
  86. .parent = TYPE_ACCEL_OPS,
  87. .class_init = whpx_accel_ops_class_init,
  88. .abstract = true,
  89. };
  90. static void whpx_accel_ops_register_types(void)
  91. {
  92. type_register_static(&whpx_accel_ops_type);
  93. }
  94. type_init(whpx_accel_ops_register_types);