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 "exec/cpu-common.h"
23 #include "sysemu/cpus.h"
25 static QemuMutex qemu_cpu_list_lock
;
26 static QemuCond exclusive_cond
;
27 static QemuCond exclusive_resume
;
28 static QemuCond qemu_work_cond
;
30 static int pending_cpus
;
32 void qemu_init_cpu_list(void)
34 /* This is needed because qemu_init_cpu_list is also called by the
35 * child process in a fork. */
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
);
44 void cpu_list_lock(void)
46 qemu_mutex_lock(&qemu_cpu_list_lock
);
49 void cpu_list_unlock(void)
51 qemu_mutex_unlock(&qemu_cpu_list_lock
);
54 static bool cpu_index_auto_assigned
;
56 static int cpu_get_free_index(void)
61 cpu_index_auto_assigned
= true;
62 CPU_FOREACH(some_cpu
) {
68 static void finish_safe_work(CPUState
*cpu
)
74 void cpu_list_add(CPUState
*cpu
)
76 qemu_mutex_lock(&qemu_cpu_list_lock
);
77 if (cpu
->cpu_index
== UNASSIGNED_CPU_INDEX
) {
78 cpu
->cpu_index
= cpu_get_free_index();
79 assert(cpu
->cpu_index
!= UNASSIGNED_CPU_INDEX
);
81 assert(!cpu_index_auto_assigned
);
83 QTAILQ_INSERT_TAIL(&cpus
, cpu
, node
);
84 qemu_mutex_unlock(&qemu_cpu_list_lock
);
86 finish_safe_work(cpu
);
89 void cpu_list_remove(CPUState
*cpu
)
91 qemu_mutex_lock(&qemu_cpu_list_lock
);
92 if (!QTAILQ_IN_USE(cpu
, node
)) {
93 /* there is nothing to undo since cpu_exec_init() hasn't been called */
94 qemu_mutex_unlock(&qemu_cpu_list_lock
);
98 assert(!(cpu_index_auto_assigned
&& cpu
!= QTAILQ_LAST(&cpus
, CPUTailQ
)));
100 QTAILQ_REMOVE(&cpus
, cpu
, node
);
101 cpu
->cpu_index
= UNASSIGNED_CPU_INDEX
;
102 qemu_mutex_unlock(&qemu_cpu_list_lock
);
105 struct qemu_work_item
{
106 struct qemu_work_item
*next
;
107 run_on_cpu_func func
;
112 static void queue_work_on_cpu(CPUState
*cpu
, struct qemu_work_item
*wi
)
114 qemu_mutex_lock(&cpu
->work_mutex
);
115 if (cpu
->queued_work_first
== NULL
) {
116 cpu
->queued_work_first
= wi
;
118 cpu
->queued_work_last
->next
= wi
;
120 cpu
->queued_work_last
= wi
;
123 qemu_mutex_unlock(&cpu
->work_mutex
);
128 void do_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, void *data
,
131 struct qemu_work_item wi
;
133 if (qemu_cpu_is_self(cpu
)) {
143 queue_work_on_cpu(cpu
, &wi
);
144 while (!atomic_mb_read(&wi
.done
)) {
145 CPUState
*self_cpu
= current_cpu
;
147 qemu_cond_wait(&qemu_work_cond
, mutex
);
148 current_cpu
= self_cpu
;
152 void async_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, void *data
)
154 struct qemu_work_item
*wi
;
156 wi
= g_malloc0(sizeof(struct qemu_work_item
));
161 queue_work_on_cpu(cpu
, wi
);
164 /* Wait for pending exclusive operations to complete. The CPU list lock
166 static inline void exclusive_idle(void)
168 while (pending_cpus
) {
169 qemu_cond_wait(&exclusive_resume
, &qemu_cpu_list_lock
);
173 /* Start an exclusive operation.
174 Must only be called from outside cpu_exec, takes
175 qemu_cpu_list_lock. */
176 void start_exclusive(void)
180 qemu_mutex_lock(&qemu_cpu_list_lock
);
183 /* Make all other cpus stop executing. */
185 CPU_FOREACH(other_cpu
) {
186 if (other_cpu
->running
) {
188 qemu_cpu_kick(other_cpu
);
191 while (pending_cpus
> 1) {
192 qemu_cond_wait(&exclusive_cond
, &qemu_cpu_list_lock
);
196 /* Finish an exclusive operation. Releases qemu_cpu_list_lock. */
197 void end_exclusive(void)
200 qemu_cond_broadcast(&exclusive_resume
);
201 qemu_mutex_unlock(&qemu_cpu_list_lock
);
204 /* Wait for exclusive ops to finish, and begin cpu execution. */
205 void cpu_exec_start(CPUState
*cpu
)
207 qemu_mutex_lock(&qemu_cpu_list_lock
);
210 qemu_mutex_unlock(&qemu_cpu_list_lock
);
213 /* Mark cpu as not executing, and release pending exclusive ops. */
214 void cpu_exec_end(CPUState
*cpu
)
216 qemu_mutex_lock(&qemu_cpu_list_lock
);
217 cpu
->running
= false;
218 if (pending_cpus
> 1) {
220 if (pending_cpus
== 1) {
221 qemu_cond_signal(&exclusive_cond
);
224 qemu_mutex_unlock(&qemu_cpu_list_lock
);
227 void process_queued_cpu_work(CPUState
*cpu
)
229 struct qemu_work_item
*wi
;
231 if (cpu
->queued_work_first
== NULL
) {
235 qemu_mutex_lock(&cpu
->work_mutex
);
236 while (cpu
->queued_work_first
!= NULL
) {
237 wi
= cpu
->queued_work_first
;
238 cpu
->queued_work_first
= wi
->next
;
239 if (!cpu
->queued_work_first
) {
240 cpu
->queued_work_last
= NULL
;
242 qemu_mutex_unlock(&cpu
->work_mutex
);
243 wi
->func(cpu
, wi
->data
);
244 qemu_mutex_lock(&cpu
->work_mutex
);
248 atomic_mb_set(&wi
->done
, true);
251 qemu_mutex_unlock(&cpu
->work_mutex
);
252 qemu_cond_broadcast(&qemu_work_cond
);