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