tcg-accel-ops-rr.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * QEMU TCG Single Threaded vCPUs implementation
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2014 Red Hat Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "sysemu/tcg.h"
  27. #include "sysemu/replay.h"
  28. #include "sysemu/cpu-timers.h"
  29. #include "qemu/main-loop.h"
  30. #include "qemu/notify.h"
  31. #include "qemu/guest-random.h"
  32. #include "exec/exec-all.h"
  33. #include "tcg-accel-ops.h"
  34. #include "tcg-accel-ops-rr.h"
  35. #include "tcg-accel-ops-icount.h"
  36. /* Kick all RR vCPUs */
  37. void rr_kick_vcpu_thread(CPUState *unused)
  38. {
  39. CPUState *cpu;
  40. CPU_FOREACH(cpu) {
  41. cpu_exit(cpu);
  42. };
  43. }
  44. /*
  45. * TCG vCPU kick timer
  46. *
  47. * The kick timer is responsible for moving single threaded vCPU
  48. * emulation on to the next vCPU. If more than one vCPU is running a
  49. * timer event with force a cpu->exit so the next vCPU can get
  50. * scheduled.
  51. *
  52. * The timer is removed if all vCPUs are idle and restarted again once
  53. * idleness is complete.
  54. */
  55. static QEMUTimer *rr_kick_vcpu_timer;
  56. static CPUState *rr_current_cpu;
  57. static inline int64_t rr_next_kick_time(void)
  58. {
  59. return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
  60. }
  61. /* Kick the currently round-robin scheduled vCPU to next */
  62. static void rr_kick_next_cpu(void)
  63. {
  64. CPUState *cpu;
  65. do {
  66. cpu = qatomic_mb_read(&rr_current_cpu);
  67. if (cpu) {
  68. cpu_exit(cpu);
  69. }
  70. } while (cpu != qatomic_mb_read(&rr_current_cpu));
  71. }
  72. static void rr_kick_thread(void *opaque)
  73. {
  74. timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
  75. rr_kick_next_cpu();
  76. }
  77. static void rr_start_kick_timer(void)
  78. {
  79. if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
  80. rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  81. rr_kick_thread, NULL);
  82. }
  83. if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) {
  84. timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
  85. }
  86. }
  87. static void rr_stop_kick_timer(void)
  88. {
  89. if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) {
  90. timer_del(rr_kick_vcpu_timer);
  91. }
  92. }
  93. static void rr_wait_io_event(void)
  94. {
  95. CPUState *cpu;
  96. while (all_cpu_threads_idle()) {
  97. rr_stop_kick_timer();
  98. qemu_cond_wait_iothread(first_cpu->halt_cond);
  99. }
  100. rr_start_kick_timer();
  101. CPU_FOREACH(cpu) {
  102. qemu_wait_io_event_common(cpu);
  103. }
  104. }
  105. /*
  106. * Destroy any remaining vCPUs which have been unplugged and have
  107. * finished running
  108. */
  109. static void rr_deal_with_unplugged_cpus(void)
  110. {
  111. CPUState *cpu;
  112. CPU_FOREACH(cpu) {
  113. if (cpu->unplug && !cpu_can_run(cpu)) {
  114. tcg_cpus_destroy(cpu);
  115. break;
  116. }
  117. }
  118. }
  119. static void rr_force_rcu(Notifier *notify, void *data)
  120. {
  121. rr_kick_next_cpu();
  122. }
  123. /*
  124. * In the single-threaded case each vCPU is simulated in turn. If
  125. * there is more than a single vCPU we create a simple timer to kick
  126. * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
  127. * This is done explicitly rather than relying on side-effects
  128. * elsewhere.
  129. */
  130. static void *rr_cpu_thread_fn(void *arg)
  131. {
  132. Notifier force_rcu;
  133. CPUState *cpu = arg;
  134. assert(tcg_enabled());
  135. rcu_register_thread();
  136. force_rcu.notify = rr_force_rcu;
  137. rcu_add_force_rcu_notifier(&force_rcu);
  138. tcg_register_thread();
  139. qemu_mutex_lock_iothread();
  140. qemu_thread_get_self(cpu->thread);
  141. cpu->thread_id = qemu_get_thread_id();
  142. cpu->can_do_io = 1;
  143. cpu_thread_signal_created(cpu);
  144. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  145. /* wait for initial kick-off after machine start */
  146. while (first_cpu->stopped) {
  147. qemu_cond_wait_iothread(first_cpu->halt_cond);
  148. /* process any pending work */
  149. CPU_FOREACH(cpu) {
  150. current_cpu = cpu;
  151. qemu_wait_io_event_common(cpu);
  152. }
  153. }
  154. rr_start_kick_timer();
  155. cpu = first_cpu;
  156. /* process any pending work */
  157. cpu->exit_request = 1;
  158. while (1) {
  159. qemu_mutex_unlock_iothread();
  160. replay_mutex_lock();
  161. qemu_mutex_lock_iothread();
  162. if (icount_enabled()) {
  163. /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
  164. icount_account_warp_timer();
  165. /*
  166. * Run the timers here. This is much more efficient than
  167. * waking up the I/O thread and waiting for completion.
  168. */
  169. icount_handle_deadline();
  170. }
  171. replay_mutex_unlock();
  172. if (!cpu) {
  173. cpu = first_cpu;
  174. }
  175. while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
  176. qatomic_mb_set(&rr_current_cpu, cpu);
  177. current_cpu = cpu;
  178. qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
  179. (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
  180. if (cpu_can_run(cpu)) {
  181. int r;
  182. qemu_mutex_unlock_iothread();
  183. if (icount_enabled()) {
  184. icount_prepare_for_run(cpu);
  185. }
  186. r = tcg_cpus_exec(cpu);
  187. if (icount_enabled()) {
  188. icount_process_data(cpu);
  189. }
  190. qemu_mutex_lock_iothread();
  191. if (r == EXCP_DEBUG) {
  192. cpu_handle_guest_debug(cpu);
  193. break;
  194. } else if (r == EXCP_ATOMIC) {
  195. qemu_mutex_unlock_iothread();
  196. cpu_exec_step_atomic(cpu);
  197. qemu_mutex_lock_iothread();
  198. break;
  199. }
  200. } else if (cpu->stop) {
  201. if (cpu->unplug) {
  202. cpu = CPU_NEXT(cpu);
  203. }
  204. break;
  205. }
  206. cpu = CPU_NEXT(cpu);
  207. } /* while (cpu && !cpu->exit_request).. */
  208. /* Does not need qatomic_mb_set because a spurious wakeup is okay. */
  209. qatomic_set(&rr_current_cpu, NULL);
  210. if (cpu && cpu->exit_request) {
  211. qatomic_mb_set(&cpu->exit_request, 0);
  212. }
  213. if (icount_enabled() && all_cpu_threads_idle()) {
  214. /*
  215. * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
  216. * in the main_loop, wake it up in order to start the warp timer.
  217. */
  218. qemu_notify_event();
  219. }
  220. rr_wait_io_event();
  221. rr_deal_with_unplugged_cpus();
  222. }
  223. rcu_remove_force_rcu_notifier(&force_rcu);
  224. rcu_unregister_thread();
  225. return NULL;
  226. }
  227. void rr_start_vcpu_thread(CPUState *cpu)
  228. {
  229. char thread_name[VCPU_THREAD_NAME_SIZE];
  230. static QemuCond *single_tcg_halt_cond;
  231. static QemuThread *single_tcg_cpu_thread;
  232. g_assert(tcg_enabled());
  233. tcg_cpu_init_cflags(cpu, false);
  234. if (!single_tcg_cpu_thread) {
  235. cpu->thread = g_new0(QemuThread, 1);
  236. cpu->halt_cond = g_new0(QemuCond, 1);
  237. qemu_cond_init(cpu->halt_cond);
  238. /* share a single thread for all cpus with TCG */
  239. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
  240. qemu_thread_create(cpu->thread, thread_name,
  241. rr_cpu_thread_fn,
  242. cpu, QEMU_THREAD_JOINABLE);
  243. single_tcg_halt_cond = cpu->halt_cond;
  244. single_tcg_cpu_thread = cpu->thread;
  245. #ifdef _WIN32
  246. cpu->hThread = qemu_thread_get_handle(cpu->thread);
  247. #endif
  248. } else {
  249. /* we share the thread */
  250. cpu->thread = single_tcg_cpu_thread;
  251. cpu->halt_cond = single_tcg_halt_cond;
  252. cpu->thread_id = first_cpu->thread_id;
  253. cpu->can_do_io = 1;
  254. cpu->created = true;
  255. }
  256. }