tcg-accel-ops.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * QEMU TCG vCPU common functionality
  3. *
  4. * Functionality common to all TCG vCPU variants: mttcg, rr and icount.
  5. *
  6. * Copyright (c) 2003-2008 Fabrice Bellard
  7. * Copyright (c) 2014 Red Hat Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "qemu/osdep.h"
  28. #include "sysemu/tcg.h"
  29. #include "sysemu/replay.h"
  30. #include "sysemu/cpu-timers.h"
  31. #include "qemu/main-loop.h"
  32. #include "qemu/guest-random.h"
  33. #include "exec/exec-all.h"
  34. #include "tcg-accel-ops.h"
  35. #include "tcg-accel-ops-mttcg.h"
  36. #include "tcg-accel-ops-rr.h"
  37. #include "tcg-accel-ops-icount.h"
  38. /* common functionality among all TCG variants */
  39. void tcg_cpu_init_cflags(CPUState *cpu, bool parallel)
  40. {
  41. uint32_t cflags = cpu->cluster_index << CF_CLUSTER_SHIFT;
  42. cflags |= parallel ? CF_PARALLEL : 0;
  43. cflags |= icount_enabled() ? CF_USE_ICOUNT : 0;
  44. cpu->tcg_cflags = cflags;
  45. }
  46. void tcg_cpus_destroy(CPUState *cpu)
  47. {
  48. cpu_thread_signal_destroyed(cpu);
  49. }
  50. int tcg_cpus_exec(CPUState *cpu)
  51. {
  52. int ret;
  53. #ifdef CONFIG_PROFILER
  54. int64_t ti;
  55. #endif
  56. assert(tcg_enabled());
  57. #ifdef CONFIG_PROFILER
  58. ti = profile_getclock();
  59. #endif
  60. cpu_exec_start(cpu);
  61. ret = cpu_exec(cpu);
  62. cpu_exec_end(cpu);
  63. #ifdef CONFIG_PROFILER
  64. qatomic_set(&tcg_ctx->prof.cpu_exec_time,
  65. tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
  66. #endif
  67. return ret;
  68. }
  69. /* mask must never be zero, except for A20 change call */
  70. void tcg_handle_interrupt(CPUState *cpu, int mask)
  71. {
  72. g_assert(qemu_mutex_iothread_locked());
  73. cpu->interrupt_request |= mask;
  74. /*
  75. * If called from iothread context, wake the target cpu in
  76. * case its halted.
  77. */
  78. if (!qemu_cpu_is_self(cpu)) {
  79. qemu_cpu_kick(cpu);
  80. } else {
  81. qatomic_set(&cpu_neg(cpu)->icount_decr.u16.high, -1);
  82. }
  83. }
  84. static void tcg_accel_ops_init(AccelOpsClass *ops)
  85. {
  86. if (qemu_tcg_mttcg_enabled()) {
  87. ops->create_vcpu_thread = mttcg_start_vcpu_thread;
  88. ops->kick_vcpu_thread = mttcg_kick_vcpu_thread;
  89. ops->handle_interrupt = tcg_handle_interrupt;
  90. } else if (icount_enabled()) {
  91. ops->create_vcpu_thread = rr_start_vcpu_thread;
  92. ops->kick_vcpu_thread = rr_kick_vcpu_thread;
  93. ops->handle_interrupt = icount_handle_interrupt;
  94. ops->get_virtual_clock = icount_get;
  95. ops->get_elapsed_ticks = icount_get;
  96. } else {
  97. ops->create_vcpu_thread = rr_start_vcpu_thread;
  98. ops->kick_vcpu_thread = rr_kick_vcpu_thread;
  99. ops->handle_interrupt = tcg_handle_interrupt;
  100. }
  101. }
  102. static void tcg_accel_ops_class_init(ObjectClass *oc, void *data)
  103. {
  104. AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
  105. ops->ops_init = tcg_accel_ops_init;
  106. }
  107. static const TypeInfo tcg_accel_ops_type = {
  108. .name = ACCEL_OPS_NAME("tcg"),
  109. .parent = TYPE_ACCEL_OPS,
  110. .class_init = tcg_accel_ops_class_init,
  111. .abstract = true,
  112. };
  113. module_obj(ACCEL_OPS_NAME("tcg"));
  114. static void tcg_accel_ops_register_types(void)
  115. {
  116. type_register_static(&tcg_accel_ops_type);
  117. }
  118. type_init(tcg_accel_ops_register_types);