kvm-accel-ops.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * QEMU KVM support
  3. *
  4. * Copyright IBM, Corp. 2008
  5. * Red Hat, Inc. 2008
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Glauber Costa <gcosta@redhat.com>
  10. *
  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.
  13. *
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qemu/error-report.h"
  17. #include "qemu/main-loop.h"
  18. #include "system/kvm.h"
  19. #include "system/kvm_int.h"
  20. #include "system/runstate.h"
  21. #include "system/cpus.h"
  22. #include "qemu/guest-random.h"
  23. #include "qapi/error.h"
  24. #include <linux/kvm.h>
  25. #include "kvm-cpus.h"
  26. static void *kvm_vcpu_thread_fn(void *arg)
  27. {
  28. CPUState *cpu = arg;
  29. int r;
  30. rcu_register_thread();
  31. bql_lock();
  32. qemu_thread_get_self(cpu->thread);
  33. cpu->thread_id = qemu_get_thread_id();
  34. current_cpu = cpu;
  35. r = kvm_init_vcpu(cpu, &error_fatal);
  36. kvm_init_cpu_signals(cpu);
  37. /* signal CPU creation */
  38. cpu_thread_signal_created(cpu);
  39. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  40. do {
  41. if (cpu_can_run(cpu)) {
  42. r = kvm_cpu_exec(cpu);
  43. if (r == EXCP_DEBUG) {
  44. cpu_handle_guest_debug(cpu);
  45. }
  46. }
  47. qemu_wait_io_event(cpu);
  48. } while (!cpu->unplug || cpu_can_run(cpu));
  49. kvm_destroy_vcpu(cpu);
  50. cpu_thread_signal_destroyed(cpu);
  51. bql_unlock();
  52. rcu_unregister_thread();
  53. return NULL;
  54. }
  55. static void kvm_start_vcpu_thread(CPUState *cpu)
  56. {
  57. char thread_name[VCPU_THREAD_NAME_SIZE];
  58. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
  59. cpu->cpu_index);
  60. qemu_thread_create(cpu->thread, thread_name, kvm_vcpu_thread_fn,
  61. cpu, QEMU_THREAD_JOINABLE);
  62. }
  63. static bool kvm_vcpu_thread_is_idle(CPUState *cpu)
  64. {
  65. return !kvm_halt_in_kernel();
  66. }
  67. static bool kvm_cpus_are_resettable(void)
  68. {
  69. return !kvm_enabled() || !kvm_state->guest_state_protected;
  70. }
  71. #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
  72. static int kvm_update_guest_debug_ops(CPUState *cpu)
  73. {
  74. return kvm_update_guest_debug(cpu, 0);
  75. }
  76. #endif
  77. static void kvm_accel_ops_class_init(ObjectClass *oc, void *data)
  78. {
  79. AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
  80. ops->create_vcpu_thread = kvm_start_vcpu_thread;
  81. ops->cpu_thread_is_idle = kvm_vcpu_thread_is_idle;
  82. ops->cpus_are_resettable = kvm_cpus_are_resettable;
  83. ops->synchronize_post_reset = kvm_cpu_synchronize_post_reset;
  84. ops->synchronize_post_init = kvm_cpu_synchronize_post_init;
  85. ops->synchronize_state = kvm_cpu_synchronize_state;
  86. ops->synchronize_pre_loadvm = kvm_cpu_synchronize_pre_loadvm;
  87. #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
  88. ops->update_guest_debug = kvm_update_guest_debug_ops;
  89. ops->supports_guest_debug = kvm_supports_guest_debug;
  90. ops->insert_breakpoint = kvm_insert_breakpoint;
  91. ops->remove_breakpoint = kvm_remove_breakpoint;
  92. ops->remove_all_breakpoints = kvm_remove_all_breakpoints;
  93. #endif
  94. }
  95. static const TypeInfo kvm_accel_ops_type = {
  96. .name = ACCEL_OPS_NAME("kvm"),
  97. .parent = TYPE_ACCEL_OPS,
  98. .class_init = kvm_accel_ops_class_init,
  99. .abstract = true,
  100. };
  101. static void kvm_accel_ops_register_types(void)
  102. {
  103. type_register_static(&kvm_accel_ops_type);
  104. }
  105. type_init(kvm_accel_ops_register_types);