2 * CPU thread main loop - common bits for user and system mode emulation
4 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu/main-loop.h"
22 #include "exec/cpu-common.h"
23 #include "hw/core/cpu.h"
24 #include "sysemu/cpus.h"
25 #include "qemu/lockable.h"
27 static QemuMutex qemu_cpu_list_lock
;
28 static QemuCond exclusive_cond
;
29 static QemuCond exclusive_resume
;
30 static QemuCond qemu_work_cond
;
32 /* >= 1 if a thread is inside start_exclusive/end_exclusive. Written
33 * under qemu_cpu_list_lock, read with atomic operations.
35 static int pending_cpus
;
37 void qemu_init_cpu_list(void)
39 /* This is needed because qemu_init_cpu_list is also called by the
40 * child process in a fork. */
43 qemu_mutex_init(&qemu_cpu_list_lock
);
44 qemu_cond_init(&exclusive_cond
);
45 qemu_cond_init(&exclusive_resume
);
46 qemu_cond_init(&qemu_work_cond
);
49 void cpu_list_lock(void)
51 qemu_mutex_lock(&qemu_cpu_list_lock
);
54 void cpu_list_unlock(void)
56 qemu_mutex_unlock(&qemu_cpu_list_lock
);
59 static bool cpu_index_auto_assigned
;
61 static int cpu_get_free_index(void)
64 int max_cpu_index
= 0;
66 cpu_index_auto_assigned
= true;
67 CPU_FOREACH(some_cpu
) {
68 if (some_cpu
->cpu_index
>= max_cpu_index
) {
69 max_cpu_index
= some_cpu
->cpu_index
+ 1;
75 CPUTailQ cpus
= QTAILQ_HEAD_INITIALIZER(cpus
);
76 static unsigned int cpu_list_generation_id
;
78 unsigned int cpu_list_generation_id_get(void)
80 return cpu_list_generation_id
;
83 void cpu_list_add(CPUState
*cpu
)
85 QEMU_LOCK_GUARD(&qemu_cpu_list_lock
);
86 if (cpu
->cpu_index
== UNASSIGNED_CPU_INDEX
) {
87 cpu
->cpu_index
= cpu_get_free_index();
88 assert(cpu
->cpu_index
!= UNASSIGNED_CPU_INDEX
);
90 assert(!cpu_index_auto_assigned
);
92 QTAILQ_INSERT_TAIL_RCU(&cpus
, cpu
, node
);
93 cpu_list_generation_id
++;
96 void cpu_list_remove(CPUState
*cpu
)
98 QEMU_LOCK_GUARD(&qemu_cpu_list_lock
);
99 if (!QTAILQ_IN_USE(cpu
, node
)) {
100 /* there is nothing to undo since cpu_exec_init() hasn't been called */
104 QTAILQ_REMOVE_RCU(&cpus
, cpu
, node
);
105 cpu
->cpu_index
= UNASSIGNED_CPU_INDEX
;
106 cpu_list_generation_id
++;
109 CPUState
*qemu_get_cpu(int index
)
114 if (cpu
->cpu_index
== index
) {
122 /* current CPU in the current thread. It is only valid inside cpu_exec() */
123 __thread CPUState
*current_cpu
;
125 struct qemu_work_item
{
126 QSIMPLEQ_ENTRY(qemu_work_item
) node
;
127 run_on_cpu_func func
;
128 run_on_cpu_data data
;
129 bool free
, exclusive
, done
;
132 static void queue_work_on_cpu(CPUState
*cpu
, struct qemu_work_item
*wi
)
134 qemu_mutex_lock(&cpu
->work_mutex
);
135 QSIMPLEQ_INSERT_TAIL(&cpu
->work_list
, wi
, node
);
137 qemu_mutex_unlock(&cpu
->work_mutex
);
142 void do_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
,
145 struct qemu_work_item wi
;
147 if (qemu_cpu_is_self(cpu
)) {
156 wi
.exclusive
= false;
158 queue_work_on_cpu(cpu
, &wi
);
159 while (!qatomic_mb_read(&wi
.done
)) {
160 CPUState
*self_cpu
= current_cpu
;
162 qemu_cond_wait(&qemu_work_cond
, mutex
);
163 current_cpu
= self_cpu
;
167 void async_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
)
169 struct qemu_work_item
*wi
;
171 wi
= g_new0(struct qemu_work_item
, 1);
176 queue_work_on_cpu(cpu
, wi
);
179 /* Wait for pending exclusive operations to complete. The CPU list lock
181 static inline void exclusive_idle(void)
183 while (pending_cpus
) {
184 qemu_cond_wait(&exclusive_resume
, &qemu_cpu_list_lock
);
188 /* Start an exclusive operation.
189 Must only be called from outside cpu_exec. */
190 void start_exclusive(void)
195 qemu_mutex_lock(&qemu_cpu_list_lock
);
198 /* Make all other cpus stop executing. */
199 qatomic_set(&pending_cpus
, 1);
201 /* Write pending_cpus before reading other_cpu->running. */
204 CPU_FOREACH(other_cpu
) {
205 if (qatomic_read(&other_cpu
->running
)) {
206 other_cpu
->has_waiter
= true;
208 qemu_cpu_kick(other_cpu
);
212 qatomic_set(&pending_cpus
, running_cpus
+ 1);
213 while (pending_cpus
> 1) {
214 qemu_cond_wait(&exclusive_cond
, &qemu_cpu_list_lock
);
217 /* Can release mutex, no one will enter another exclusive
218 * section until end_exclusive resets pending_cpus to 0.
220 qemu_mutex_unlock(&qemu_cpu_list_lock
);
222 current_cpu
->in_exclusive_context
= true;
225 /* Finish an exclusive operation. */
226 void end_exclusive(void)
228 current_cpu
->in_exclusive_context
= false;
230 qemu_mutex_lock(&qemu_cpu_list_lock
);
231 qatomic_set(&pending_cpus
, 0);
232 qemu_cond_broadcast(&exclusive_resume
);
233 qemu_mutex_unlock(&qemu_cpu_list_lock
);
236 /* Wait for exclusive ops to finish, and begin cpu execution. */
237 void cpu_exec_start(CPUState
*cpu
)
239 qatomic_set(&cpu
->running
, true);
241 /* Write cpu->running before reading pending_cpus. */
244 /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1.
245 * After taking the lock we'll see cpu->has_waiter == true and run---not
246 * for long because start_exclusive kicked us. cpu_exec_end will
247 * decrement pending_cpus and signal the waiter.
249 * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1.
250 * This includes the case when an exclusive item is running now.
251 * Then we'll see cpu->has_waiter == false and wait for the item to
254 * 3. pending_cpus == 0. Then start_exclusive is definitely going to
255 * see cpu->running == true, and it will kick the CPU.
257 if (unlikely(qatomic_read(&pending_cpus
))) {
258 QEMU_LOCK_GUARD(&qemu_cpu_list_lock
);
259 if (!cpu
->has_waiter
) {
260 /* Not counted in pending_cpus, let the exclusive item
261 * run. Since we have the lock, just set cpu->running to true
262 * while holding it; no need to check pending_cpus again.
264 qatomic_set(&cpu
->running
, false);
266 /* Now pending_cpus is zero. */
267 qatomic_set(&cpu
->running
, true);
269 /* Counted in pending_cpus, go ahead and release the
270 * waiter at cpu_exec_end.
276 /* Mark cpu as not executing, and release pending exclusive ops. */
277 void cpu_exec_end(CPUState
*cpu
)
279 qatomic_set(&cpu
->running
, false);
281 /* Write cpu->running before reading pending_cpus. */
284 /* 1. start_exclusive saw cpu->running == true. Then it will increment
285 * pending_cpus and wait for exclusive_cond. After taking the lock
286 * we'll see cpu->has_waiter == true.
288 * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1.
289 * This includes the case when an exclusive item started after setting
290 * cpu->running to false and before we read pending_cpus. Then we'll see
291 * cpu->has_waiter == false and not touch pending_cpus. The next call to
292 * cpu_exec_start will run exclusive_idle if still necessary, thus waiting
293 * for the item to complete.
295 * 3. pending_cpus == 0. Then start_exclusive is definitely going to
296 * see cpu->running == false, and it can ignore this CPU until the
297 * next cpu_exec_start.
299 if (unlikely(qatomic_read(&pending_cpus
))) {
300 QEMU_LOCK_GUARD(&qemu_cpu_list_lock
);
301 if (cpu
->has_waiter
) {
302 cpu
->has_waiter
= false;
303 qatomic_set(&pending_cpus
, pending_cpus
- 1);
304 if (pending_cpus
== 1) {
305 qemu_cond_signal(&exclusive_cond
);
311 void async_safe_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
,
312 run_on_cpu_data data
)
314 struct qemu_work_item
*wi
;
316 wi
= g_new0(struct qemu_work_item
, 1);
320 wi
->exclusive
= true;
322 queue_work_on_cpu(cpu
, wi
);
325 void process_queued_cpu_work(CPUState
*cpu
)
327 struct qemu_work_item
*wi
;
329 qemu_mutex_lock(&cpu
->work_mutex
);
330 if (QSIMPLEQ_EMPTY(&cpu
->work_list
)) {
331 qemu_mutex_unlock(&cpu
->work_mutex
);
334 while (!QSIMPLEQ_EMPTY(&cpu
->work_list
)) {
335 wi
= QSIMPLEQ_FIRST(&cpu
->work_list
);
336 QSIMPLEQ_REMOVE_HEAD(&cpu
->work_list
, node
);
337 qemu_mutex_unlock(&cpu
->work_mutex
);
339 /* Running work items outside the BQL avoids the following deadlock:
340 * 1) start_exclusive() is called with the BQL taken while another
341 * CPU is running; 2) cpu_exec in the other CPU tries to takes the
342 * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
343 * neither CPU can proceed.
345 qemu_mutex_unlock_iothread();
347 wi
->func(cpu
, wi
->data
);
349 qemu_mutex_lock_iothread();
351 wi
->func(cpu
, wi
->data
);
353 qemu_mutex_lock(&cpu
->work_mutex
);
357 qatomic_mb_set(&wi
->done
, true);
360 qemu_mutex_unlock(&cpu
->work_mutex
);
361 qemu_cond_broadcast(&qemu_work_cond
);