2
0

cpus-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * CPU thread main loop - common bits for user and system mode emulation
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/main-loop.h"
  21. #include "exec/cpu-common.h"
  22. #include "hw/core/cpu.h"
  23. #include "sysemu/cpus.h"
  24. static QemuMutex qemu_cpu_list_lock;
  25. static QemuCond exclusive_cond;
  26. static QemuCond exclusive_resume;
  27. static QemuCond qemu_work_cond;
  28. /* >= 1 if a thread is inside start_exclusive/end_exclusive. Written
  29. * under qemu_cpu_list_lock, read with atomic operations.
  30. */
  31. static int pending_cpus;
  32. void qemu_init_cpu_list(void)
  33. {
  34. /* This is needed because qemu_init_cpu_list is also called by the
  35. * child process in a fork. */
  36. pending_cpus = 0;
  37. qemu_mutex_init(&qemu_cpu_list_lock);
  38. qemu_cond_init(&exclusive_cond);
  39. qemu_cond_init(&exclusive_resume);
  40. qemu_cond_init(&qemu_work_cond);
  41. }
  42. void cpu_list_lock(void)
  43. {
  44. qemu_mutex_lock(&qemu_cpu_list_lock);
  45. }
  46. void cpu_list_unlock(void)
  47. {
  48. qemu_mutex_unlock(&qemu_cpu_list_lock);
  49. }
  50. static bool cpu_index_auto_assigned;
  51. static int cpu_get_free_index(void)
  52. {
  53. CPUState *some_cpu;
  54. int cpu_index = 0;
  55. cpu_index_auto_assigned = true;
  56. CPU_FOREACH(some_cpu) {
  57. cpu_index++;
  58. }
  59. return cpu_index;
  60. }
  61. void cpu_list_add(CPUState *cpu)
  62. {
  63. qemu_mutex_lock(&qemu_cpu_list_lock);
  64. if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
  65. cpu->cpu_index = cpu_get_free_index();
  66. assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
  67. } else {
  68. assert(!cpu_index_auto_assigned);
  69. }
  70. QTAILQ_INSERT_TAIL_RCU(&cpus, cpu, node);
  71. qemu_mutex_unlock(&qemu_cpu_list_lock);
  72. }
  73. void cpu_list_remove(CPUState *cpu)
  74. {
  75. qemu_mutex_lock(&qemu_cpu_list_lock);
  76. if (!QTAILQ_IN_USE(cpu, node)) {
  77. /* there is nothing to undo since cpu_exec_init() hasn't been called */
  78. qemu_mutex_unlock(&qemu_cpu_list_lock);
  79. return;
  80. }
  81. assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus)));
  82. QTAILQ_REMOVE_RCU(&cpus, cpu, node);
  83. cpu->cpu_index = UNASSIGNED_CPU_INDEX;
  84. qemu_mutex_unlock(&qemu_cpu_list_lock);
  85. }
  86. struct qemu_work_item {
  87. struct qemu_work_item *next;
  88. run_on_cpu_func func;
  89. run_on_cpu_data data;
  90. bool free, exclusive, done;
  91. };
  92. static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
  93. {
  94. qemu_mutex_lock(&cpu->work_mutex);
  95. if (cpu->queued_work_first == NULL) {
  96. cpu->queued_work_first = wi;
  97. } else {
  98. cpu->queued_work_last->next = wi;
  99. }
  100. cpu->queued_work_last = wi;
  101. wi->next = NULL;
  102. wi->done = false;
  103. qemu_mutex_unlock(&cpu->work_mutex);
  104. qemu_cpu_kick(cpu);
  105. }
  106. void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
  107. QemuMutex *mutex)
  108. {
  109. struct qemu_work_item wi;
  110. if (qemu_cpu_is_self(cpu)) {
  111. func(cpu, data);
  112. return;
  113. }
  114. wi.func = func;
  115. wi.data = data;
  116. wi.done = false;
  117. wi.free = false;
  118. wi.exclusive = false;
  119. queue_work_on_cpu(cpu, &wi);
  120. while (!atomic_mb_read(&wi.done)) {
  121. CPUState *self_cpu = current_cpu;
  122. qemu_cond_wait(&qemu_work_cond, mutex);
  123. current_cpu = self_cpu;
  124. }
  125. }
  126. void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
  127. {
  128. struct qemu_work_item *wi;
  129. wi = g_malloc0(sizeof(struct qemu_work_item));
  130. wi->func = func;
  131. wi->data = data;
  132. wi->free = true;
  133. queue_work_on_cpu(cpu, wi);
  134. }
  135. /* Wait for pending exclusive operations to complete. The CPU list lock
  136. must be held. */
  137. static inline void exclusive_idle(void)
  138. {
  139. while (pending_cpus) {
  140. qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
  141. }
  142. }
  143. /* Start an exclusive operation.
  144. Must only be called from outside cpu_exec. */
  145. void start_exclusive(void)
  146. {
  147. CPUState *other_cpu;
  148. int running_cpus;
  149. qemu_mutex_lock(&qemu_cpu_list_lock);
  150. exclusive_idle();
  151. /* Make all other cpus stop executing. */
  152. atomic_set(&pending_cpus, 1);
  153. /* Write pending_cpus before reading other_cpu->running. */
  154. smp_mb();
  155. running_cpus = 0;
  156. CPU_FOREACH(other_cpu) {
  157. if (atomic_read(&other_cpu->running)) {
  158. other_cpu->has_waiter = true;
  159. running_cpus++;
  160. qemu_cpu_kick(other_cpu);
  161. }
  162. }
  163. atomic_set(&pending_cpus, running_cpus + 1);
  164. while (pending_cpus > 1) {
  165. qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
  166. }
  167. /* Can release mutex, no one will enter another exclusive
  168. * section until end_exclusive resets pending_cpus to 0.
  169. */
  170. qemu_mutex_unlock(&qemu_cpu_list_lock);
  171. current_cpu->in_exclusive_context = true;
  172. }
  173. /* Finish an exclusive operation. */
  174. void end_exclusive(void)
  175. {
  176. current_cpu->in_exclusive_context = false;
  177. qemu_mutex_lock(&qemu_cpu_list_lock);
  178. atomic_set(&pending_cpus, 0);
  179. qemu_cond_broadcast(&exclusive_resume);
  180. qemu_mutex_unlock(&qemu_cpu_list_lock);
  181. }
  182. /* Wait for exclusive ops to finish, and begin cpu execution. */
  183. void cpu_exec_start(CPUState *cpu)
  184. {
  185. atomic_set(&cpu->running, true);
  186. /* Write cpu->running before reading pending_cpus. */
  187. smp_mb();
  188. /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1.
  189. * After taking the lock we'll see cpu->has_waiter == true and run---not
  190. * for long because start_exclusive kicked us. cpu_exec_end will
  191. * decrement pending_cpus and signal the waiter.
  192. *
  193. * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1.
  194. * This includes the case when an exclusive item is running now.
  195. * Then we'll see cpu->has_waiter == false and wait for the item to
  196. * complete.
  197. *
  198. * 3. pending_cpus == 0. Then start_exclusive is definitely going to
  199. * see cpu->running == true, and it will kick the CPU.
  200. */
  201. if (unlikely(atomic_read(&pending_cpus))) {
  202. qemu_mutex_lock(&qemu_cpu_list_lock);
  203. if (!cpu->has_waiter) {
  204. /* Not counted in pending_cpus, let the exclusive item
  205. * run. Since we have the lock, just set cpu->running to true
  206. * while holding it; no need to check pending_cpus again.
  207. */
  208. atomic_set(&cpu->running, false);
  209. exclusive_idle();
  210. /* Now pending_cpus is zero. */
  211. atomic_set(&cpu->running, true);
  212. } else {
  213. /* Counted in pending_cpus, go ahead and release the
  214. * waiter at cpu_exec_end.
  215. */
  216. }
  217. qemu_mutex_unlock(&qemu_cpu_list_lock);
  218. }
  219. }
  220. /* Mark cpu as not executing, and release pending exclusive ops. */
  221. void cpu_exec_end(CPUState *cpu)
  222. {
  223. atomic_set(&cpu->running, false);
  224. /* Write cpu->running before reading pending_cpus. */
  225. smp_mb();
  226. /* 1. start_exclusive saw cpu->running == true. Then it will increment
  227. * pending_cpus and wait for exclusive_cond. After taking the lock
  228. * we'll see cpu->has_waiter == true.
  229. *
  230. * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1.
  231. * This includes the case when an exclusive item started after setting
  232. * cpu->running to false and before we read pending_cpus. Then we'll see
  233. * cpu->has_waiter == false and not touch pending_cpus. The next call to
  234. * cpu_exec_start will run exclusive_idle if still necessary, thus waiting
  235. * for the item to complete.
  236. *
  237. * 3. pending_cpus == 0. Then start_exclusive is definitely going to
  238. * see cpu->running == false, and it can ignore this CPU until the
  239. * next cpu_exec_start.
  240. */
  241. if (unlikely(atomic_read(&pending_cpus))) {
  242. qemu_mutex_lock(&qemu_cpu_list_lock);
  243. if (cpu->has_waiter) {
  244. cpu->has_waiter = false;
  245. atomic_set(&pending_cpus, pending_cpus - 1);
  246. if (pending_cpus == 1) {
  247. qemu_cond_signal(&exclusive_cond);
  248. }
  249. }
  250. qemu_mutex_unlock(&qemu_cpu_list_lock);
  251. }
  252. }
  253. void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func,
  254. run_on_cpu_data data)
  255. {
  256. struct qemu_work_item *wi;
  257. wi = g_malloc0(sizeof(struct qemu_work_item));
  258. wi->func = func;
  259. wi->data = data;
  260. wi->free = true;
  261. wi->exclusive = true;
  262. queue_work_on_cpu(cpu, wi);
  263. }
  264. void process_queued_cpu_work(CPUState *cpu)
  265. {
  266. struct qemu_work_item *wi;
  267. if (cpu->queued_work_first == NULL) {
  268. return;
  269. }
  270. qemu_mutex_lock(&cpu->work_mutex);
  271. while (cpu->queued_work_first != NULL) {
  272. wi = cpu->queued_work_first;
  273. cpu->queued_work_first = wi->next;
  274. if (!cpu->queued_work_first) {
  275. cpu->queued_work_last = NULL;
  276. }
  277. qemu_mutex_unlock(&cpu->work_mutex);
  278. if (wi->exclusive) {
  279. /* Running work items outside the BQL avoids the following deadlock:
  280. * 1) start_exclusive() is called with the BQL taken while another
  281. * CPU is running; 2) cpu_exec in the other CPU tries to takes the
  282. * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
  283. * neither CPU can proceed.
  284. */
  285. qemu_mutex_unlock_iothread();
  286. start_exclusive();
  287. wi->func(cpu, wi->data);
  288. end_exclusive();
  289. qemu_mutex_lock_iothread();
  290. } else {
  291. wi->func(cpu, wi->data);
  292. }
  293. qemu_mutex_lock(&cpu->work_mutex);
  294. if (wi->free) {
  295. g_free(wi);
  296. } else {
  297. atomic_mb_set(&wi->done, true);
  298. }
  299. }
  300. qemu_mutex_unlock(&cpu->work_mutex);
  301. qemu_cond_broadcast(&qemu_work_cond);
  302. }