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 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"
24 #include "sysemu/cpus.h"
26 static QemuMutex qemu_cpu_list_lock
;
27 static QemuCond exclusive_cond
;
28 static QemuCond exclusive_resume
;
29 static QemuCond qemu_work_cond
;
31 static int pending_cpus
;
33 void qemu_init_cpu_list(void)
35 /* This is needed because qemu_init_cpu_list is also called by the
36 * child process in a fork. */
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
);
45 void cpu_list_lock(void)
47 qemu_mutex_lock(&qemu_cpu_list_lock
);
50 void cpu_list_unlock(void)
52 qemu_mutex_unlock(&qemu_cpu_list_lock
);
55 static bool cpu_index_auto_assigned
;
57 static int cpu_get_free_index(void)
62 cpu_index_auto_assigned
= true;
63 CPU_FOREACH(some_cpu
) {
69 static void finish_safe_work(CPUState
*cpu
)
75 void cpu_list_add(CPUState
*cpu
)
77 qemu_mutex_lock(&qemu_cpu_list_lock
);
78 if (cpu
->cpu_index
== UNASSIGNED_CPU_INDEX
) {
79 cpu
->cpu_index
= cpu_get_free_index();
80 assert(cpu
->cpu_index
!= UNASSIGNED_CPU_INDEX
);
82 assert(!cpu_index_auto_assigned
);
84 QTAILQ_INSERT_TAIL(&cpus
, cpu
, node
);
85 qemu_mutex_unlock(&qemu_cpu_list_lock
);
87 finish_safe_work(cpu
);
90 void cpu_list_remove(CPUState
*cpu
)
92 qemu_mutex_lock(&qemu_cpu_list_lock
);
93 if (!QTAILQ_IN_USE(cpu
, node
)) {
94 /* there is nothing to undo since cpu_exec_init() hasn't been called */
95 qemu_mutex_unlock(&qemu_cpu_list_lock
);
99 assert(!(cpu_index_auto_assigned
&& cpu
!= QTAILQ_LAST(&cpus
, CPUTailQ
)));
101 QTAILQ_REMOVE(&cpus
, cpu
, node
);
102 cpu
->cpu_index
= UNASSIGNED_CPU_INDEX
;
103 qemu_mutex_unlock(&qemu_cpu_list_lock
);
106 struct qemu_work_item
{
107 struct qemu_work_item
*next
;
108 run_on_cpu_func func
;
110 bool free
, exclusive
, done
;
113 static void queue_work_on_cpu(CPUState
*cpu
, struct qemu_work_item
*wi
)
115 qemu_mutex_lock(&cpu
->work_mutex
);
116 if (cpu
->queued_work_first
== NULL
) {
117 cpu
->queued_work_first
= wi
;
119 cpu
->queued_work_last
->next
= wi
;
121 cpu
->queued_work_last
= wi
;
124 qemu_mutex_unlock(&cpu
->work_mutex
);
129 void do_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, void *data
,
132 struct qemu_work_item wi
;
134 if (qemu_cpu_is_self(cpu
)) {
143 wi
.exclusive
= false;
145 queue_work_on_cpu(cpu
, &wi
);
146 while (!atomic_mb_read(&wi
.done
)) {
147 CPUState
*self_cpu
= current_cpu
;
149 qemu_cond_wait(&qemu_work_cond
, mutex
);
150 current_cpu
= self_cpu
;
154 void async_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, void *data
)
156 struct qemu_work_item
*wi
;
158 wi
= g_malloc0(sizeof(struct qemu_work_item
));
163 queue_work_on_cpu(cpu
, wi
);
166 /* Wait for pending exclusive operations to complete. The CPU list lock
168 static inline void exclusive_idle(void)
170 while (pending_cpus
) {
171 qemu_cond_wait(&exclusive_resume
, &qemu_cpu_list_lock
);
175 /* Start an exclusive operation.
176 Must only be called from outside cpu_exec. */
177 void start_exclusive(void)
181 qemu_mutex_lock(&qemu_cpu_list_lock
);
184 /* Make all other cpus stop executing. */
186 CPU_FOREACH(other_cpu
) {
187 if (other_cpu
->running
) {
189 qemu_cpu_kick(other_cpu
);
192 while (pending_cpus
> 1) {
193 qemu_cond_wait(&exclusive_cond
, &qemu_cpu_list_lock
);
196 /* Can release mutex, no one will enter another exclusive
197 * section until end_exclusive resets pending_cpus to 0.
199 qemu_mutex_unlock(&qemu_cpu_list_lock
);
202 /* Finish an exclusive operation. */
203 void end_exclusive(void)
205 qemu_mutex_lock(&qemu_cpu_list_lock
);
207 qemu_cond_broadcast(&exclusive_resume
);
208 qemu_mutex_unlock(&qemu_cpu_list_lock
);
211 /* Wait for exclusive ops to finish, and begin cpu execution. */
212 void cpu_exec_start(CPUState
*cpu
)
214 qemu_mutex_lock(&qemu_cpu_list_lock
);
217 qemu_mutex_unlock(&qemu_cpu_list_lock
);
220 /* Mark cpu as not executing, and release pending exclusive ops. */
221 void cpu_exec_end(CPUState
*cpu
)
223 qemu_mutex_lock(&qemu_cpu_list_lock
);
224 cpu
->running
= false;
225 if (pending_cpus
> 1) {
227 if (pending_cpus
== 1) {
228 qemu_cond_signal(&exclusive_cond
);
231 qemu_mutex_unlock(&qemu_cpu_list_lock
);
234 void async_safe_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, void *data
)
236 struct qemu_work_item
*wi
;
238 wi
= g_malloc0(sizeof(struct qemu_work_item
));
242 wi
->exclusive
= true;
244 queue_work_on_cpu(cpu
, wi
);
247 void process_queued_cpu_work(CPUState
*cpu
)
249 struct qemu_work_item
*wi
;
251 if (cpu
->queued_work_first
== NULL
) {
255 qemu_mutex_lock(&cpu
->work_mutex
);
256 while (cpu
->queued_work_first
!= NULL
) {
257 wi
= cpu
->queued_work_first
;
258 cpu
->queued_work_first
= wi
->next
;
259 if (!cpu
->queued_work_first
) {
260 cpu
->queued_work_last
= NULL
;
262 qemu_mutex_unlock(&cpu
->work_mutex
);
264 /* Running work items outside the BQL avoids the following deadlock:
265 * 1) start_exclusive() is called with the BQL taken while another
266 * CPU is running; 2) cpu_exec in the other CPU tries to takes the
267 * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
268 * neither CPU can proceed.
270 qemu_mutex_unlock_iothread();
272 wi
->func(cpu
, wi
->data
);
274 qemu_mutex_lock_iothread();
276 wi
->func(cpu
, wi
->data
);
278 qemu_mutex_lock(&cpu
->work_mutex
);
282 atomic_mb_set(&wi
->done
, true);
285 qemu_mutex_unlock(&cpu
->work_mutex
);
286 qemu_cond_broadcast(&qemu_work_cond
);