]> git.ipfire.org Git - thirdparty/qemu.git/blob - cpus-common.c
38b1d553fbc886c9356120b88748066762028fdb
[thirdparty/qemu.git] / cpus-common.c
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 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
20 #include "qemu/osdep.h"
21 #include "qemu/main-loop.h"
22 #include "exec/cpu-common.h"
23 #include "qom/cpu.h"
24 #include "sysemu/cpus.h"
25
26 static QemuMutex qemu_cpu_list_lock;
27 static QemuCond exclusive_cond;
28 static QemuCond exclusive_resume;
29 static QemuCond qemu_work_cond;
30
31 static int pending_cpus;
32
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
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
45 void cpu_list_lock(void)
46 {
47 qemu_mutex_lock(&qemu_cpu_list_lock);
48 }
49
50 void cpu_list_unlock(void)
51 {
52 qemu_mutex_unlock(&qemu_cpu_list_lock);
53 }
54
55 static bool cpu_index_auto_assigned;
56
57 static int cpu_get_free_index(void)
58 {
59 CPUState *some_cpu;
60 int cpu_index = 0;
61
62 cpu_index_auto_assigned = true;
63 CPU_FOREACH(some_cpu) {
64 cpu_index++;
65 }
66 return cpu_index;
67 }
68
69 static void finish_safe_work(CPUState *cpu)
70 {
71 cpu_exec_start(cpu);
72 cpu_exec_end(cpu);
73 }
74
75 void cpu_list_add(CPUState *cpu)
76 {
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);
81 } else {
82 assert(!cpu_index_auto_assigned);
83 }
84 QTAILQ_INSERT_TAIL(&cpus, cpu, node);
85 qemu_mutex_unlock(&qemu_cpu_list_lock);
86
87 finish_safe_work(cpu);
88 }
89
90 void cpu_list_remove(CPUState *cpu)
91 {
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);
96 return;
97 }
98
99 assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus, CPUTailQ)));
100
101 QTAILQ_REMOVE(&cpus, cpu, node);
102 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
103 qemu_mutex_unlock(&qemu_cpu_list_lock);
104 }
105
106 struct qemu_work_item {
107 struct qemu_work_item *next;
108 run_on_cpu_func func;
109 void *data;
110 bool free, exclusive, done;
111 };
112
113 static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
114 {
115 qemu_mutex_lock(&cpu->work_mutex);
116 if (cpu->queued_work_first == NULL) {
117 cpu->queued_work_first = wi;
118 } else {
119 cpu->queued_work_last->next = wi;
120 }
121 cpu->queued_work_last = wi;
122 wi->next = NULL;
123 wi->done = false;
124 qemu_mutex_unlock(&cpu->work_mutex);
125
126 qemu_cpu_kick(cpu);
127 }
128
129 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data,
130 QemuMutex *mutex)
131 {
132 struct qemu_work_item wi;
133
134 if (qemu_cpu_is_self(cpu)) {
135 func(cpu, data);
136 return;
137 }
138
139 wi.func = func;
140 wi.data = data;
141 wi.done = false;
142 wi.free = false;
143 wi.exclusive = false;
144
145 queue_work_on_cpu(cpu, &wi);
146 while (!atomic_mb_read(&wi.done)) {
147 CPUState *self_cpu = current_cpu;
148
149 qemu_cond_wait(&qemu_work_cond, mutex);
150 current_cpu = self_cpu;
151 }
152 }
153
154 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data)
155 {
156 struct qemu_work_item *wi;
157
158 wi = g_malloc0(sizeof(struct qemu_work_item));
159 wi->func = func;
160 wi->data = data;
161 wi->free = true;
162
163 queue_work_on_cpu(cpu, wi);
164 }
165
166 /* Wait for pending exclusive operations to complete. The CPU list lock
167 must be held. */
168 static inline void exclusive_idle(void)
169 {
170 while (pending_cpus) {
171 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
172 }
173 }
174
175 /* Start an exclusive operation.
176 Must only be called from outside cpu_exec. */
177 void start_exclusive(void)
178 {
179 CPUState *other_cpu;
180
181 qemu_mutex_lock(&qemu_cpu_list_lock);
182 exclusive_idle();
183
184 /* Make all other cpus stop executing. */
185 pending_cpus = 1;
186 CPU_FOREACH(other_cpu) {
187 if (other_cpu->running) {
188 pending_cpus++;
189 qemu_cpu_kick(other_cpu);
190 }
191 }
192 while (pending_cpus > 1) {
193 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
194 }
195
196 /* Can release mutex, no one will enter another exclusive
197 * section until end_exclusive resets pending_cpus to 0.
198 */
199 qemu_mutex_unlock(&qemu_cpu_list_lock);
200 }
201
202 /* Finish an exclusive operation. */
203 void end_exclusive(void)
204 {
205 qemu_mutex_lock(&qemu_cpu_list_lock);
206 pending_cpus = 0;
207 qemu_cond_broadcast(&exclusive_resume);
208 qemu_mutex_unlock(&qemu_cpu_list_lock);
209 }
210
211 /* Wait for exclusive ops to finish, and begin cpu execution. */
212 void cpu_exec_start(CPUState *cpu)
213 {
214 qemu_mutex_lock(&qemu_cpu_list_lock);
215 exclusive_idle();
216 cpu->running = true;
217 qemu_mutex_unlock(&qemu_cpu_list_lock);
218 }
219
220 /* Mark cpu as not executing, and release pending exclusive ops. */
221 void cpu_exec_end(CPUState *cpu)
222 {
223 qemu_mutex_lock(&qemu_cpu_list_lock);
224 cpu->running = false;
225 if (pending_cpus > 1) {
226 pending_cpus--;
227 if (pending_cpus == 1) {
228 qemu_cond_signal(&exclusive_cond);
229 }
230 }
231 qemu_mutex_unlock(&qemu_cpu_list_lock);
232 }
233
234 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data)
235 {
236 struct qemu_work_item *wi;
237
238 wi = g_malloc0(sizeof(struct qemu_work_item));
239 wi->func = func;
240 wi->data = data;
241 wi->free = true;
242 wi->exclusive = true;
243
244 queue_work_on_cpu(cpu, wi);
245 }
246
247 void process_queued_cpu_work(CPUState *cpu)
248 {
249 struct qemu_work_item *wi;
250
251 if (cpu->queued_work_first == NULL) {
252 return;
253 }
254
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;
261 }
262 qemu_mutex_unlock(&cpu->work_mutex);
263 if (wi->exclusive) {
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.
269 */
270 qemu_mutex_unlock_iothread();
271 start_exclusive();
272 wi->func(cpu, wi->data);
273 end_exclusive();
274 qemu_mutex_lock_iothread();
275 } else {
276 wi->func(cpu, wi->data);
277 }
278 qemu_mutex_lock(&cpu->work_mutex);
279 if (wi->free) {
280 g_free(wi);
281 } else {
282 atomic_mb_set(&wi->done, true);
283 }
284 }
285 qemu_mutex_unlock(&cpu->work_mutex);
286 qemu_cond_broadcast(&qemu_work_cond);
287 }