cpus-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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.1 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. #include "qemu/lockable.h"
  25. #include "trace/trace-root.h"
  26. QemuMutex qemu_cpu_list_lock;
  27. static QemuCond exclusive_cond;
  28. static QemuCond exclusive_resume;
  29. static QemuCond qemu_work_cond;
  30. /* >= 1 if a thread is inside start_exclusive/end_exclusive. Written
  31. * under qemu_cpu_list_lock, read with atomic operations.
  32. */
  33. static int pending_cpus;
  34. void qemu_init_cpu_list(void)
  35. {
  36. /* This is needed because qemu_init_cpu_list is also called by the
  37. * child process in a fork. */
  38. pending_cpus = 0;
  39. qemu_mutex_init(&qemu_cpu_list_lock);
  40. qemu_cond_init(&exclusive_cond);
  41. qemu_cond_init(&exclusive_resume);
  42. qemu_cond_init(&qemu_work_cond);
  43. }
  44. void cpu_list_lock(void)
  45. {
  46. qemu_mutex_lock(&qemu_cpu_list_lock);
  47. }
  48. void cpu_list_unlock(void)
  49. {
  50. qemu_mutex_unlock(&qemu_cpu_list_lock);
  51. }
  52. static bool cpu_index_auto_assigned;
  53. static int cpu_get_free_index(void)
  54. {
  55. CPUState *some_cpu;
  56. int max_cpu_index = 0;
  57. cpu_index_auto_assigned = true;
  58. CPU_FOREACH(some_cpu) {
  59. if (some_cpu->cpu_index >= max_cpu_index) {
  60. max_cpu_index = some_cpu->cpu_index + 1;
  61. }
  62. }
  63. return max_cpu_index;
  64. }
  65. CPUTailQ cpus = QTAILQ_HEAD_INITIALIZER(cpus);
  66. static unsigned int cpu_list_generation_id;
  67. unsigned int cpu_list_generation_id_get(void)
  68. {
  69. return cpu_list_generation_id;
  70. }
  71. void cpu_list_add(CPUState *cpu)
  72. {
  73. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  74. if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
  75. cpu->cpu_index = cpu_get_free_index();
  76. assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
  77. } else {
  78. assert(!cpu_index_auto_assigned);
  79. }
  80. QTAILQ_INSERT_TAIL_RCU(&cpus, cpu, node);
  81. cpu_list_generation_id++;
  82. }
  83. void cpu_list_remove(CPUState *cpu)
  84. {
  85. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  86. if (!QTAILQ_IN_USE(cpu, node)) {
  87. /* there is nothing to undo since cpu_exec_init() hasn't been called */
  88. return;
  89. }
  90. QTAILQ_REMOVE_RCU(&cpus, cpu, node);
  91. cpu->cpu_index = UNASSIGNED_CPU_INDEX;
  92. cpu_list_generation_id++;
  93. }
  94. CPUState *qemu_get_cpu(int index)
  95. {
  96. CPUState *cpu;
  97. CPU_FOREACH(cpu) {
  98. if (cpu->cpu_index == index) {
  99. return cpu;
  100. }
  101. }
  102. return NULL;
  103. }
  104. /* current CPU in the current thread. It is only valid inside cpu_exec() */
  105. __thread CPUState *current_cpu;
  106. struct qemu_work_item {
  107. QSIMPLEQ_ENTRY(qemu_work_item) node;
  108. run_on_cpu_func func;
  109. run_on_cpu_data data;
  110. bool free, exclusive, done;
  111. };
  112. static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
  113. {
  114. qemu_mutex_lock(&cpu->work_mutex);
  115. QSIMPLEQ_INSERT_TAIL(&cpu->work_list, wi, node);
  116. wi->done = false;
  117. qemu_mutex_unlock(&cpu->work_mutex);
  118. qemu_cpu_kick(cpu);
  119. }
  120. void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
  121. QemuMutex *mutex)
  122. {
  123. struct qemu_work_item wi;
  124. if (qemu_cpu_is_self(cpu)) {
  125. func(cpu, data);
  126. return;
  127. }
  128. wi.func = func;
  129. wi.data = data;
  130. wi.done = false;
  131. wi.free = false;
  132. wi.exclusive = false;
  133. queue_work_on_cpu(cpu, &wi);
  134. while (!qatomic_load_acquire(&wi.done)) {
  135. CPUState *self_cpu = current_cpu;
  136. qemu_cond_wait(&qemu_work_cond, mutex);
  137. current_cpu = self_cpu;
  138. }
  139. }
  140. void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
  141. {
  142. struct qemu_work_item *wi;
  143. wi = g_new0(struct qemu_work_item, 1);
  144. wi->func = func;
  145. wi->data = data;
  146. wi->free = true;
  147. queue_work_on_cpu(cpu, wi);
  148. }
  149. /* Wait for pending exclusive operations to complete. The CPU list lock
  150. must be held. */
  151. static inline void exclusive_idle(void)
  152. {
  153. while (pending_cpus) {
  154. qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
  155. }
  156. }
  157. /* Start an exclusive operation.
  158. Must only be called from outside cpu_exec. */
  159. void start_exclusive(void)
  160. {
  161. CPUState *other_cpu;
  162. int running_cpus;
  163. if (current_cpu->exclusive_context_count) {
  164. current_cpu->exclusive_context_count++;
  165. return;
  166. }
  167. qemu_mutex_lock(&qemu_cpu_list_lock);
  168. exclusive_idle();
  169. /* Make all other cpus stop executing. */
  170. qatomic_set(&pending_cpus, 1);
  171. /* Write pending_cpus before reading other_cpu->running. */
  172. smp_mb();
  173. running_cpus = 0;
  174. CPU_FOREACH(other_cpu) {
  175. if (qatomic_read(&other_cpu->running)) {
  176. other_cpu->has_waiter = true;
  177. running_cpus++;
  178. qemu_cpu_kick(other_cpu);
  179. }
  180. }
  181. qatomic_set(&pending_cpus, running_cpus + 1);
  182. while (pending_cpus > 1) {
  183. qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
  184. }
  185. /* Can release mutex, no one will enter another exclusive
  186. * section until end_exclusive resets pending_cpus to 0.
  187. */
  188. qemu_mutex_unlock(&qemu_cpu_list_lock);
  189. current_cpu->exclusive_context_count = 1;
  190. }
  191. /* Finish an exclusive operation. */
  192. void end_exclusive(void)
  193. {
  194. current_cpu->exclusive_context_count--;
  195. if (current_cpu->exclusive_context_count) {
  196. return;
  197. }
  198. qemu_mutex_lock(&qemu_cpu_list_lock);
  199. qatomic_set(&pending_cpus, 0);
  200. qemu_cond_broadcast(&exclusive_resume);
  201. qemu_mutex_unlock(&qemu_cpu_list_lock);
  202. }
  203. /* Wait for exclusive ops to finish, and begin cpu execution. */
  204. void cpu_exec_start(CPUState *cpu)
  205. {
  206. qatomic_set(&cpu->running, true);
  207. /* Write cpu->running before reading pending_cpus. */
  208. smp_mb();
  209. /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1.
  210. * After taking the lock we'll see cpu->has_waiter == true and run---not
  211. * for long because start_exclusive kicked us. cpu_exec_end will
  212. * decrement pending_cpus and signal the waiter.
  213. *
  214. * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1.
  215. * This includes the case when an exclusive item is running now.
  216. * Then we'll see cpu->has_waiter == false and wait for the item to
  217. * complete.
  218. *
  219. * 3. pending_cpus == 0. Then start_exclusive is definitely going to
  220. * see cpu->running == true, and it will kick the CPU.
  221. */
  222. if (unlikely(qatomic_read(&pending_cpus))) {
  223. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  224. if (!cpu->has_waiter) {
  225. /* Not counted in pending_cpus, let the exclusive item
  226. * run. Since we have the lock, just set cpu->running to true
  227. * while holding it; no need to check pending_cpus again.
  228. */
  229. qatomic_set(&cpu->running, false);
  230. exclusive_idle();
  231. /* Now pending_cpus is zero. */
  232. qatomic_set(&cpu->running, true);
  233. } else {
  234. /* Counted in pending_cpus, go ahead and release the
  235. * waiter at cpu_exec_end.
  236. */
  237. }
  238. }
  239. }
  240. /* Mark cpu as not executing, and release pending exclusive ops. */
  241. void cpu_exec_end(CPUState *cpu)
  242. {
  243. qatomic_set(&cpu->running, false);
  244. /* Write cpu->running before reading pending_cpus. */
  245. smp_mb();
  246. /* 1. start_exclusive saw cpu->running == true. Then it will increment
  247. * pending_cpus and wait for exclusive_cond. After taking the lock
  248. * we'll see cpu->has_waiter == true.
  249. *
  250. * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1.
  251. * This includes the case when an exclusive item started after setting
  252. * cpu->running to false and before we read pending_cpus. Then we'll see
  253. * cpu->has_waiter == false and not touch pending_cpus. The next call to
  254. * cpu_exec_start will run exclusive_idle if still necessary, thus waiting
  255. * for the item to complete.
  256. *
  257. * 3. pending_cpus == 0. Then start_exclusive is definitely going to
  258. * see cpu->running == false, and it can ignore this CPU until the
  259. * next cpu_exec_start.
  260. */
  261. if (unlikely(qatomic_read(&pending_cpus))) {
  262. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  263. if (cpu->has_waiter) {
  264. cpu->has_waiter = false;
  265. qatomic_set(&pending_cpus, pending_cpus - 1);
  266. if (pending_cpus == 1) {
  267. qemu_cond_signal(&exclusive_cond);
  268. }
  269. }
  270. }
  271. }
  272. void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func,
  273. run_on_cpu_data data)
  274. {
  275. struct qemu_work_item *wi;
  276. wi = g_new0(struct qemu_work_item, 1);
  277. wi->func = func;
  278. wi->data = data;
  279. wi->free = true;
  280. wi->exclusive = true;
  281. queue_work_on_cpu(cpu, wi);
  282. }
  283. void process_queued_cpu_work(CPUState *cpu)
  284. {
  285. struct qemu_work_item *wi;
  286. qemu_mutex_lock(&cpu->work_mutex);
  287. if (QSIMPLEQ_EMPTY(&cpu->work_list)) {
  288. qemu_mutex_unlock(&cpu->work_mutex);
  289. return;
  290. }
  291. while (!QSIMPLEQ_EMPTY(&cpu->work_list)) {
  292. wi = QSIMPLEQ_FIRST(&cpu->work_list);
  293. QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node);
  294. qemu_mutex_unlock(&cpu->work_mutex);
  295. if (wi->exclusive) {
  296. /* Running work items outside the BQL avoids the following deadlock:
  297. * 1) start_exclusive() is called with the BQL taken while another
  298. * CPU is running; 2) cpu_exec in the other CPU tries to takes the
  299. * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
  300. * neither CPU can proceed.
  301. */
  302. qemu_mutex_unlock_iothread();
  303. start_exclusive();
  304. wi->func(cpu, wi->data);
  305. end_exclusive();
  306. qemu_mutex_lock_iothread();
  307. } else {
  308. wi->func(cpu, wi->data);
  309. }
  310. qemu_mutex_lock(&cpu->work_mutex);
  311. if (wi->free) {
  312. g_free(wi);
  313. } else {
  314. qatomic_store_release(&wi->done, true);
  315. }
  316. }
  317. qemu_mutex_unlock(&cpu->work_mutex);
  318. qemu_cond_broadcast(&qemu_work_cond);
  319. }
  320. /* Add a breakpoint. */
  321. int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
  322. CPUBreakpoint **breakpoint)
  323. {
  324. CPUClass *cc = CPU_GET_CLASS(cpu);
  325. CPUBreakpoint *bp;
  326. if (cc->gdb_adjust_breakpoint) {
  327. pc = cc->gdb_adjust_breakpoint(cpu, pc);
  328. }
  329. bp = g_malloc(sizeof(*bp));
  330. bp->pc = pc;
  331. bp->flags = flags;
  332. /* keep all GDB-injected breakpoints in front */
  333. if (flags & BP_GDB) {
  334. QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
  335. } else {
  336. QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
  337. }
  338. if (breakpoint) {
  339. *breakpoint = bp;
  340. }
  341. trace_breakpoint_insert(cpu->cpu_index, pc, flags);
  342. return 0;
  343. }
  344. /* Remove a specific breakpoint. */
  345. int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
  346. {
  347. CPUClass *cc = CPU_GET_CLASS(cpu);
  348. CPUBreakpoint *bp;
  349. if (cc->gdb_adjust_breakpoint) {
  350. pc = cc->gdb_adjust_breakpoint(cpu, pc);
  351. }
  352. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  353. if (bp->pc == pc && bp->flags == flags) {
  354. cpu_breakpoint_remove_by_ref(cpu, bp);
  355. return 0;
  356. }
  357. }
  358. return -ENOENT;
  359. }
  360. /* Remove a specific breakpoint by reference. */
  361. void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *bp)
  362. {
  363. QTAILQ_REMOVE(&cpu->breakpoints, bp, entry);
  364. trace_breakpoint_remove(cpu->cpu_index, bp->pc, bp->flags);
  365. g_free(bp);
  366. }
  367. /* Remove all matching breakpoints. */
  368. void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
  369. {
  370. CPUBreakpoint *bp, *next;
  371. QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
  372. if (bp->flags & mask) {
  373. cpu_breakpoint_remove_by_ref(cpu, bp);
  374. }
  375. }
  376. }