2
0

cpu-common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "system/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. int cpu_get_free_index(void)
  53. {
  54. CPUState *some_cpu;
  55. int max_cpu_index = 0;
  56. CPU_FOREACH(some_cpu) {
  57. if (some_cpu->cpu_index >= max_cpu_index) {
  58. max_cpu_index = some_cpu->cpu_index + 1;
  59. }
  60. }
  61. return max_cpu_index;
  62. }
  63. CPUTailQ cpus_queue = QTAILQ_HEAD_INITIALIZER(cpus_queue);
  64. static unsigned int cpu_list_generation_id;
  65. unsigned int cpu_list_generation_id_get(void)
  66. {
  67. return cpu_list_generation_id;
  68. }
  69. void cpu_list_add(CPUState *cpu)
  70. {
  71. static bool cpu_index_auto_assigned;
  72. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  73. if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
  74. cpu_index_auto_assigned = true;
  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_queue, 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_queue, 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. /* Ensure we are not running, or start_exclusive will be blocked. */
  164. g_assert(!current_cpu->running);
  165. if (current_cpu->exclusive_context_count) {
  166. current_cpu->exclusive_context_count++;
  167. return;
  168. }
  169. qemu_mutex_lock(&qemu_cpu_list_lock);
  170. exclusive_idle();
  171. /* Make all other cpus stop executing. */
  172. qatomic_set(&pending_cpus, 1);
  173. /* Write pending_cpus before reading other_cpu->running. */
  174. smp_mb();
  175. running_cpus = 0;
  176. CPU_FOREACH(other_cpu) {
  177. if (qatomic_read(&other_cpu->running)) {
  178. other_cpu->has_waiter = true;
  179. running_cpus++;
  180. qemu_cpu_kick(other_cpu);
  181. }
  182. }
  183. qatomic_set(&pending_cpus, running_cpus + 1);
  184. while (pending_cpus > 1) {
  185. qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
  186. }
  187. /* Can release mutex, no one will enter another exclusive
  188. * section until end_exclusive resets pending_cpus to 0.
  189. */
  190. qemu_mutex_unlock(&qemu_cpu_list_lock);
  191. current_cpu->exclusive_context_count = 1;
  192. }
  193. /* Finish an exclusive operation. */
  194. void end_exclusive(void)
  195. {
  196. current_cpu->exclusive_context_count--;
  197. if (current_cpu->exclusive_context_count) {
  198. return;
  199. }
  200. qemu_mutex_lock(&qemu_cpu_list_lock);
  201. qatomic_set(&pending_cpus, 0);
  202. qemu_cond_broadcast(&exclusive_resume);
  203. qemu_mutex_unlock(&qemu_cpu_list_lock);
  204. }
  205. /* Wait for exclusive ops to finish, and begin cpu execution. */
  206. void cpu_exec_start(CPUState *cpu)
  207. {
  208. qatomic_set(&cpu->running, true);
  209. /* Write cpu->running before reading pending_cpus. */
  210. smp_mb();
  211. /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1.
  212. * After taking the lock we'll see cpu->has_waiter == true and run---not
  213. * for long because start_exclusive kicked us. cpu_exec_end will
  214. * decrement pending_cpus and signal the waiter.
  215. *
  216. * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1.
  217. * This includes the case when an exclusive item is running now.
  218. * Then we'll see cpu->has_waiter == false and wait for the item to
  219. * complete.
  220. *
  221. * 3. pending_cpus == 0. Then start_exclusive is definitely going to
  222. * see cpu->running == true, and it will kick the CPU.
  223. */
  224. if (unlikely(qatomic_read(&pending_cpus))) {
  225. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  226. if (!cpu->has_waiter) {
  227. /* Not counted in pending_cpus, let the exclusive item
  228. * run. Since we have the lock, just set cpu->running to true
  229. * while holding it; no need to check pending_cpus again.
  230. */
  231. qatomic_set(&cpu->running, false);
  232. exclusive_idle();
  233. /* Now pending_cpus is zero. */
  234. qatomic_set(&cpu->running, true);
  235. } else {
  236. /* Counted in pending_cpus, go ahead and release the
  237. * waiter at cpu_exec_end.
  238. */
  239. }
  240. }
  241. }
  242. /* Mark cpu as not executing, and release pending exclusive ops. */
  243. void cpu_exec_end(CPUState *cpu)
  244. {
  245. qatomic_set(&cpu->running, false);
  246. /* Write cpu->running before reading pending_cpus. */
  247. smp_mb();
  248. /* 1. start_exclusive saw cpu->running == true. Then it will increment
  249. * pending_cpus and wait for exclusive_cond. After taking the lock
  250. * we'll see cpu->has_waiter == true.
  251. *
  252. * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1.
  253. * This includes the case when an exclusive item started after setting
  254. * cpu->running to false and before we read pending_cpus. Then we'll see
  255. * cpu->has_waiter == false and not touch pending_cpus. The next call to
  256. * cpu_exec_start will run exclusive_idle if still necessary, thus waiting
  257. * for the item to complete.
  258. *
  259. * 3. pending_cpus == 0. Then start_exclusive is definitely going to
  260. * see cpu->running == false, and it can ignore this CPU until the
  261. * next cpu_exec_start.
  262. */
  263. if (unlikely(qatomic_read(&pending_cpus))) {
  264. QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
  265. if (cpu->has_waiter) {
  266. cpu->has_waiter = false;
  267. qatomic_set(&pending_cpus, pending_cpus - 1);
  268. if (pending_cpus == 1) {
  269. qemu_cond_signal(&exclusive_cond);
  270. }
  271. }
  272. }
  273. }
  274. void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func,
  275. run_on_cpu_data data)
  276. {
  277. struct qemu_work_item *wi;
  278. wi = g_new0(struct qemu_work_item, 1);
  279. wi->func = func;
  280. wi->data = data;
  281. wi->free = true;
  282. wi->exclusive = true;
  283. queue_work_on_cpu(cpu, wi);
  284. }
  285. void free_queued_cpu_work(CPUState *cpu)
  286. {
  287. while (!QSIMPLEQ_EMPTY(&cpu->work_list)) {
  288. struct qemu_work_item *wi = QSIMPLEQ_FIRST(&cpu->work_list);
  289. QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node);
  290. if (wi->free) {
  291. g_free(wi);
  292. }
  293. }
  294. }
  295. void process_queued_cpu_work(CPUState *cpu)
  296. {
  297. struct qemu_work_item *wi;
  298. qemu_mutex_lock(&cpu->work_mutex);
  299. if (QSIMPLEQ_EMPTY(&cpu->work_list)) {
  300. qemu_mutex_unlock(&cpu->work_mutex);
  301. return;
  302. }
  303. while (!QSIMPLEQ_EMPTY(&cpu->work_list)) {
  304. wi = QSIMPLEQ_FIRST(&cpu->work_list);
  305. QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node);
  306. qemu_mutex_unlock(&cpu->work_mutex);
  307. if (wi->exclusive) {
  308. /* Running work items outside the BQL avoids the following deadlock:
  309. * 1) start_exclusive() is called with the BQL taken while another
  310. * CPU is running; 2) cpu_exec in the other CPU tries to takes the
  311. * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
  312. * neither CPU can proceed.
  313. */
  314. bql_unlock();
  315. start_exclusive();
  316. wi->func(cpu, wi->data);
  317. end_exclusive();
  318. bql_lock();
  319. } else {
  320. wi->func(cpu, wi->data);
  321. }
  322. qemu_mutex_lock(&cpu->work_mutex);
  323. if (wi->free) {
  324. g_free(wi);
  325. } else {
  326. qatomic_store_release(&wi->done, true);
  327. }
  328. }
  329. qemu_mutex_unlock(&cpu->work_mutex);
  330. qemu_cond_broadcast(&qemu_work_cond);
  331. }
  332. /* Add a breakpoint. */
  333. int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
  334. CPUBreakpoint **breakpoint)
  335. {
  336. CPUClass *cc = CPU_GET_CLASS(cpu);
  337. CPUBreakpoint *bp;
  338. if (cc->gdb_adjust_breakpoint) {
  339. pc = cc->gdb_adjust_breakpoint(cpu, pc);
  340. }
  341. bp = g_malloc(sizeof(*bp));
  342. bp->pc = pc;
  343. bp->flags = flags;
  344. /* keep all GDB-injected breakpoints in front */
  345. if (flags & BP_GDB) {
  346. QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
  347. } else {
  348. QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
  349. }
  350. if (breakpoint) {
  351. *breakpoint = bp;
  352. }
  353. trace_breakpoint_insert(cpu->cpu_index, pc, flags);
  354. return 0;
  355. }
  356. /* Remove a specific breakpoint. */
  357. int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
  358. {
  359. CPUClass *cc = CPU_GET_CLASS(cpu);
  360. CPUBreakpoint *bp;
  361. if (cc->gdb_adjust_breakpoint) {
  362. pc = cc->gdb_adjust_breakpoint(cpu, pc);
  363. }
  364. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  365. if (bp->pc == pc && bp->flags == flags) {
  366. cpu_breakpoint_remove_by_ref(cpu, bp);
  367. return 0;
  368. }
  369. }
  370. return -ENOENT;
  371. }
  372. /* Remove a specific breakpoint by reference. */
  373. void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *bp)
  374. {
  375. QTAILQ_REMOVE(&cpu->breakpoints, bp, entry);
  376. trace_breakpoint_remove(cpu->cpu_index, bp->pc, bp->flags);
  377. g_free(bp);
  378. }
  379. /* Remove all matching breakpoints. */
  380. void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
  381. {
  382. CPUBreakpoint *bp, *next;
  383. QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
  384. if (bp->flags & mask) {
  385. cpu_breakpoint_remove_by_ref(cpu, bp);
  386. }
  387. }
  388. }