tcg-accel-ops-rr.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "qemu/lockable.h"
  27. #include "system/tcg.h"
  28. #include "system/replay.h"
  29. #include "system/cpu-timers.h"
  30. #include "qemu/main-loop.h"
  31. #include "qemu/notify.h"
  32. #include "qemu/guest-random.h"
  33. #include "exec/exec-all.h"
  34. #include "tcg/startup.h"
  35. #include "tcg-accel-ops.h"
  36. #include "tcg-accel-ops-rr.h"
  37. #include "tcg-accel-ops-icount.h"
  38. /* Kick all RR vCPUs */
  39. void rr_kick_vcpu_thread(CPUState *unused)
  40. {
  41. CPUState *cpu;
  42. CPU_FOREACH(cpu) {
  43. cpu_exit(cpu);
  44. };
  45. }
  46. /*
  47. * TCG vCPU kick timer
  48. *
  49. * The kick timer is responsible for moving single threaded vCPU
  50. * emulation on to the next vCPU. If more than one vCPU is running a
  51. * timer event we force a cpu->exit so the next vCPU can get
  52. * scheduled.
  53. *
  54. * The timer is removed if all vCPUs are idle and restarted again once
  55. * idleness is complete.
  56. */
  57. static QEMUTimer *rr_kick_vcpu_timer;
  58. static CPUState *rr_current_cpu;
  59. static inline int64_t rr_next_kick_time(void)
  60. {
  61. return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
  62. }
  63. /* Kick the currently round-robin scheduled vCPU to next */
  64. static void rr_kick_next_cpu(void)
  65. {
  66. CPUState *cpu;
  67. do {
  68. cpu = qatomic_read(&rr_current_cpu);
  69. if (cpu) {
  70. cpu_exit(cpu);
  71. }
  72. /* Finish kicking this cpu before reading again. */
  73. smp_mb();
  74. } while (cpu != qatomic_read(&rr_current_cpu));
  75. }
  76. static void rr_kick_thread(void *opaque)
  77. {
  78. timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
  79. rr_kick_next_cpu();
  80. }
  81. static void rr_start_kick_timer(void)
  82. {
  83. if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
  84. rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  85. rr_kick_thread, NULL);
  86. }
  87. if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) {
  88. timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
  89. }
  90. }
  91. static void rr_stop_kick_timer(void)
  92. {
  93. if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) {
  94. timer_del(rr_kick_vcpu_timer);
  95. }
  96. }
  97. static void rr_wait_io_event(void)
  98. {
  99. CPUState *cpu;
  100. while (all_cpu_threads_idle()) {
  101. rr_stop_kick_timer();
  102. qemu_cond_wait_bql(first_cpu->halt_cond);
  103. }
  104. rr_start_kick_timer();
  105. CPU_FOREACH(cpu) {
  106. qemu_wait_io_event_common(cpu);
  107. }
  108. }
  109. /*
  110. * Destroy any remaining vCPUs which have been unplugged and have
  111. * finished running
  112. */
  113. static void rr_deal_with_unplugged_cpus(void)
  114. {
  115. CPUState *cpu;
  116. CPU_FOREACH(cpu) {
  117. if (cpu->unplug && !cpu_can_run(cpu)) {
  118. tcg_cpu_destroy(cpu);
  119. break;
  120. }
  121. }
  122. }
  123. static void rr_force_rcu(Notifier *notify, void *data)
  124. {
  125. rr_kick_next_cpu();
  126. }
  127. /*
  128. * Calculate the number of CPUs that we will process in a single iteration of
  129. * the main CPU thread loop so that we can fairly distribute the instruction
  130. * count across CPUs.
  131. *
  132. * The CPU count is cached based on the CPU list generation ID to avoid
  133. * iterating the list every time.
  134. */
  135. static int rr_cpu_count(void)
  136. {
  137. static unsigned int last_gen_id = ~0;
  138. static int cpu_count;
  139. CPUState *cpu;
  140. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  141. if (cpu_list_generation_id_get() != last_gen_id) {
  142. cpu_count = 0;
  143. CPU_FOREACH(cpu) {
  144. ++cpu_count;
  145. }
  146. last_gen_id = cpu_list_generation_id_get();
  147. }
  148. return cpu_count;
  149. }
  150. /*
  151. * In the single-threaded case each vCPU is simulated in turn. If
  152. * there is more than a single vCPU we create a simple timer to kick
  153. * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
  154. * This is done explicitly rather than relying on side-effects
  155. * elsewhere.
  156. */
  157. static void *rr_cpu_thread_fn(void *arg)
  158. {
  159. Notifier force_rcu;
  160. CPUState *cpu = arg;
  161. assert(tcg_enabled());
  162. rcu_register_thread();
  163. force_rcu.notify = rr_force_rcu;
  164. rcu_add_force_rcu_notifier(&force_rcu);
  165. tcg_register_thread();
  166. bql_lock();
  167. qemu_thread_get_self(cpu->thread);
  168. cpu->thread_id = qemu_get_thread_id();
  169. cpu->neg.can_do_io = true;
  170. cpu_thread_signal_created(cpu);
  171. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  172. /* wait for initial kick-off after machine start */
  173. while (first_cpu->stopped) {
  174. qemu_cond_wait_bql(first_cpu->halt_cond);
  175. /* process any pending work */
  176. CPU_FOREACH(cpu) {
  177. current_cpu = cpu;
  178. qemu_wait_io_event_common(cpu);
  179. }
  180. }
  181. rr_start_kick_timer();
  182. cpu = first_cpu;
  183. /* process any pending work */
  184. cpu->exit_request = 1;
  185. while (1) {
  186. /* Only used for icount_enabled() */
  187. int64_t cpu_budget = 0;
  188. bql_unlock();
  189. replay_mutex_lock();
  190. bql_lock();
  191. if (icount_enabled()) {
  192. int cpu_count = rr_cpu_count();
  193. /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
  194. icount_account_warp_timer();
  195. /*
  196. * Run the timers here. This is much more efficient than
  197. * waking up the I/O thread and waiting for completion.
  198. */
  199. icount_handle_deadline();
  200. cpu_budget = icount_percpu_budget(cpu_count);
  201. }
  202. replay_mutex_unlock();
  203. if (!cpu) {
  204. cpu = first_cpu;
  205. }
  206. while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
  207. /* Store rr_current_cpu before evaluating cpu_can_run(). */
  208. qatomic_set_mb(&rr_current_cpu, cpu);
  209. current_cpu = cpu;
  210. qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
  211. (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
  212. if (cpu_can_run(cpu)) {
  213. int r;
  214. bql_unlock();
  215. if (icount_enabled()) {
  216. icount_prepare_for_run(cpu, cpu_budget);
  217. }
  218. r = tcg_cpu_exec(cpu);
  219. if (icount_enabled()) {
  220. icount_process_data(cpu);
  221. }
  222. bql_lock();
  223. if (r == EXCP_DEBUG) {
  224. cpu_handle_guest_debug(cpu);
  225. break;
  226. } else if (r == EXCP_ATOMIC) {
  227. bql_unlock();
  228. cpu_exec_step_atomic(cpu);
  229. bql_lock();
  230. break;
  231. }
  232. } else if (cpu->stop) {
  233. if (cpu->unplug) {
  234. cpu = CPU_NEXT(cpu);
  235. }
  236. break;
  237. }
  238. cpu = CPU_NEXT(cpu);
  239. } /* while (cpu && !cpu->exit_request).. */
  240. /* Does not need a memory barrier because a spurious wakeup is okay. */
  241. qatomic_set(&rr_current_cpu, NULL);
  242. if (cpu && cpu->exit_request) {
  243. qatomic_set_mb(&cpu->exit_request, 0);
  244. }
  245. if (icount_enabled() && all_cpu_threads_idle()) {
  246. /*
  247. * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
  248. * in the main_loop, wake it up in order to start the warp timer.
  249. */
  250. qemu_notify_event();
  251. }
  252. rr_wait_io_event();
  253. rr_deal_with_unplugged_cpus();
  254. }
  255. g_assert_not_reached();
  256. }
  257. void rr_start_vcpu_thread(CPUState *cpu)
  258. {
  259. char thread_name[VCPU_THREAD_NAME_SIZE];
  260. static QemuCond *single_tcg_halt_cond;
  261. static QemuThread *single_tcg_cpu_thread;
  262. g_assert(tcg_enabled());
  263. tcg_cpu_init_cflags(cpu, false);
  264. if (!single_tcg_cpu_thread) {
  265. single_tcg_halt_cond = cpu->halt_cond;
  266. single_tcg_cpu_thread = cpu->thread;
  267. /* share a single thread for all cpus with TCG */
  268. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
  269. qemu_thread_create(cpu->thread, thread_name,
  270. rr_cpu_thread_fn,
  271. cpu, QEMU_THREAD_JOINABLE);
  272. } else {
  273. /* we share the thread, dump spare data */
  274. g_free(cpu->thread);
  275. qemu_cond_destroy(cpu->halt_cond);
  276. g_free(cpu->halt_cond);
  277. cpu->thread = single_tcg_cpu_thread;
  278. cpu->halt_cond = single_tcg_halt_cond;
  279. /* copy the stuff done at start of rr_cpu_thread_fn */
  280. cpu->thread_id = first_cpu->thread_id;
  281. cpu->neg.can_do_io = 1;
  282. cpu->created = true;
  283. }
  284. }