2
0

cpu-common.c 12 KB

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