]>
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 | |
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" | |
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 | ||
75 | void cpu_list_add(CPUState *cpu) | |
76 | { | |
6e8a355d | 77 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
267f685b PB |
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 | } | |
068a5ea0 | 84 | QTAILQ_INSERT_TAIL_RCU(&cpus, cpu, node); |
267f685b PB |
85 | } |
86 | ||
87 | void cpu_list_remove(CPUState *cpu) | |
88 | { | |
6e8a355d | 89 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
267f685b PB |
90 | if (!QTAILQ_IN_USE(cpu, node)) { |
91 | /* there is nothing to undo since cpu_exec_init() hasn't been called */ | |
267f685b PB |
92 | return; |
93 | } | |
94 | ||
068a5ea0 | 95 | QTAILQ_REMOVE_RCU(&cpus, cpu, node); |
267f685b | 96 | cpu->cpu_index = UNASSIGNED_CPU_INDEX; |
267f685b | 97 | } |
d148d90e SF |
98 | |
99 | struct qemu_work_item { | |
0c0fcc20 | 100 | QSIMPLEQ_ENTRY(qemu_work_item) node; |
d148d90e | 101 | run_on_cpu_func func; |
14e6fe12 | 102 | run_on_cpu_data data; |
53f5ed95 | 103 | bool free, exclusive, done; |
d148d90e SF |
104 | }; |
105 | ||
106 | static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) | |
107 | { | |
108 | qemu_mutex_lock(&cpu->work_mutex); | |
0c0fcc20 | 109 | QSIMPLEQ_INSERT_TAIL(&cpu->work_list, wi, node); |
d148d90e SF |
110 | wi->done = false; |
111 | qemu_mutex_unlock(&cpu->work_mutex); | |
112 | ||
113 | qemu_cpu_kick(cpu); | |
114 | } | |
115 | ||
14e6fe12 | 116 | void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data, |
d148d90e SF |
117 | QemuMutex *mutex) |
118 | { | |
119 | struct qemu_work_item wi; | |
120 | ||
121 | if (qemu_cpu_is_self(cpu)) { | |
122 | func(cpu, data); | |
123 | return; | |
124 | } | |
125 | ||
126 | wi.func = func; | |
127 | wi.data = data; | |
0e55539c | 128 | wi.done = false; |
d148d90e | 129 | wi.free = false; |
53f5ed95 | 130 | wi.exclusive = false; |
d148d90e SF |
131 | |
132 | queue_work_on_cpu(cpu, &wi); | |
133 | while (!atomic_mb_read(&wi.done)) { | |
134 | CPUState *self_cpu = current_cpu; | |
135 | ||
136 | qemu_cond_wait(&qemu_work_cond, mutex); | |
137 | current_cpu = self_cpu; | |
138 | } | |
139 | } | |
140 | ||
14e6fe12 | 141 | void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) |
d148d90e SF |
142 | { |
143 | struct qemu_work_item *wi; | |
144 | ||
d148d90e SF |
145 | wi = g_malloc0(sizeof(struct qemu_work_item)); |
146 | wi->func = func; | |
147 | wi->data = data; | |
148 | wi->free = true; | |
149 | ||
150 | queue_work_on_cpu(cpu, wi); | |
151 | } | |
152 | ||
ab129972 PB |
153 | /* Wait for pending exclusive operations to complete. The CPU list lock |
154 | must be held. */ | |
155 | static inline void exclusive_idle(void) | |
156 | { | |
157 | while (pending_cpus) { | |
158 | qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock); | |
159 | } | |
160 | } | |
161 | ||
162 | /* Start an exclusive operation. | |
758e1b2b | 163 | Must only be called from outside cpu_exec. */ |
ab129972 PB |
164 | void start_exclusive(void) |
165 | { | |
166 | CPUState *other_cpu; | |
c265e976 | 167 | int running_cpus; |
ab129972 PB |
168 | |
169 | qemu_mutex_lock(&qemu_cpu_list_lock); | |
170 | exclusive_idle(); | |
171 | ||
172 | /* Make all other cpus stop executing. */ | |
c265e976 PB |
173 | atomic_set(&pending_cpus, 1); |
174 | ||
175 | /* Write pending_cpus before reading other_cpu->running. */ | |
176 | smp_mb(); | |
177 | running_cpus = 0; | |
ab129972 | 178 | CPU_FOREACH(other_cpu) { |
c265e976 PB |
179 | if (atomic_read(&other_cpu->running)) { |
180 | other_cpu->has_waiter = true; | |
181 | running_cpus++; | |
ab129972 PB |
182 | qemu_cpu_kick(other_cpu); |
183 | } | |
184 | } | |
c265e976 PB |
185 | |
186 | atomic_set(&pending_cpus, running_cpus + 1); | |
ab129972 PB |
187 | while (pending_cpus > 1) { |
188 | qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock); | |
189 | } | |
758e1b2b PB |
190 | |
191 | /* Can release mutex, no one will enter another exclusive | |
192 | * section until end_exclusive resets pending_cpus to 0. | |
193 | */ | |
194 | qemu_mutex_unlock(&qemu_cpu_list_lock); | |
cfbc3c60 EC |
195 | |
196 | current_cpu->in_exclusive_context = true; | |
ab129972 PB |
197 | } |
198 | ||
758e1b2b | 199 | /* Finish an exclusive operation. */ |
ab129972 PB |
200 | void end_exclusive(void) |
201 | { | |
cfbc3c60 EC |
202 | current_cpu->in_exclusive_context = false; |
203 | ||
758e1b2b | 204 | qemu_mutex_lock(&qemu_cpu_list_lock); |
c265e976 | 205 | atomic_set(&pending_cpus, 0); |
ab129972 PB |
206 | qemu_cond_broadcast(&exclusive_resume); |
207 | qemu_mutex_unlock(&qemu_cpu_list_lock); | |
208 | } | |
209 | ||
210 | /* Wait for exclusive ops to finish, and begin cpu execution. */ | |
211 | void cpu_exec_start(CPUState *cpu) | |
212 | { | |
c265e976 PB |
213 | atomic_set(&cpu->running, true); |
214 | ||
215 | /* Write cpu->running before reading pending_cpus. */ | |
216 | smp_mb(); | |
217 | ||
218 | /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1. | |
219 | * After taking the lock we'll see cpu->has_waiter == true and run---not | |
220 | * for long because start_exclusive kicked us. cpu_exec_end will | |
221 | * decrement pending_cpus and signal the waiter. | |
222 | * | |
223 | * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1. | |
224 | * This includes the case when an exclusive item is running now. | |
225 | * Then we'll see cpu->has_waiter == false and wait for the item to | |
226 | * complete. | |
227 | * | |
228 | * 3. pending_cpus == 0. Then start_exclusive is definitely going to | |
229 | * see cpu->running == true, and it will kick the CPU. | |
230 | */ | |
231 | if (unlikely(atomic_read(&pending_cpus))) { | |
6e8a355d | 232 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
c265e976 PB |
233 | if (!cpu->has_waiter) { |
234 | /* Not counted in pending_cpus, let the exclusive item | |
235 | * run. Since we have the lock, just set cpu->running to true | |
236 | * while holding it; no need to check pending_cpus again. | |
237 | */ | |
238 | atomic_set(&cpu->running, false); | |
239 | exclusive_idle(); | |
240 | /* Now pending_cpus is zero. */ | |
241 | atomic_set(&cpu->running, true); | |
242 | } else { | |
243 | /* Counted in pending_cpus, go ahead and release the | |
244 | * waiter at cpu_exec_end. | |
245 | */ | |
246 | } | |
c265e976 | 247 | } |
ab129972 PB |
248 | } |
249 | ||
250 | /* Mark cpu as not executing, and release pending exclusive ops. */ | |
251 | void cpu_exec_end(CPUState *cpu) | |
252 | { | |
c265e976 PB |
253 | atomic_set(&cpu->running, false); |
254 | ||
255 | /* Write cpu->running before reading pending_cpus. */ | |
256 | smp_mb(); | |
257 | ||
258 | /* 1. start_exclusive saw cpu->running == true. Then it will increment | |
259 | * pending_cpus and wait for exclusive_cond. After taking the lock | |
260 | * we'll see cpu->has_waiter == true. | |
261 | * | |
262 | * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1. | |
263 | * This includes the case when an exclusive item started after setting | |
264 | * cpu->running to false and before we read pending_cpus. Then we'll see | |
265 | * cpu->has_waiter == false and not touch pending_cpus. The next call to | |
266 | * cpu_exec_start will run exclusive_idle if still necessary, thus waiting | |
267 | * for the item to complete. | |
268 | * | |
269 | * 3. pending_cpus == 0. Then start_exclusive is definitely going to | |
270 | * see cpu->running == false, and it can ignore this CPU until the | |
271 | * next cpu_exec_start. | |
272 | */ | |
273 | if (unlikely(atomic_read(&pending_cpus))) { | |
6e8a355d | 274 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
c265e976 PB |
275 | if (cpu->has_waiter) { |
276 | cpu->has_waiter = false; | |
277 | atomic_set(&pending_cpus, pending_cpus - 1); | |
278 | if (pending_cpus == 1) { | |
279 | qemu_cond_signal(&exclusive_cond); | |
280 | } | |
ab129972 PB |
281 | } |
282 | } | |
ab129972 PB |
283 | } |
284 | ||
14e6fe12 PB |
285 | void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, |
286 | run_on_cpu_data data) | |
53f5ed95 PB |
287 | { |
288 | struct qemu_work_item *wi; | |
289 | ||
290 | wi = g_malloc0(sizeof(struct qemu_work_item)); | |
291 | wi->func = func; | |
292 | wi->data = data; | |
293 | wi->free = true; | |
294 | wi->exclusive = true; | |
295 | ||
296 | queue_work_on_cpu(cpu, wi); | |
297 | } | |
298 | ||
d148d90e SF |
299 | void process_queued_cpu_work(CPUState *cpu) |
300 | { | |
301 | struct qemu_work_item *wi; | |
302 | ||
0c0fcc20 EC |
303 | qemu_mutex_lock(&cpu->work_mutex); |
304 | if (QSIMPLEQ_EMPTY(&cpu->work_list)) { | |
305 | qemu_mutex_unlock(&cpu->work_mutex); | |
d148d90e SF |
306 | return; |
307 | } | |
0c0fcc20 EC |
308 | while (!QSIMPLEQ_EMPTY(&cpu->work_list)) { |
309 | wi = QSIMPLEQ_FIRST(&cpu->work_list); | |
310 | QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node); | |
d148d90e | 311 | qemu_mutex_unlock(&cpu->work_mutex); |
53f5ed95 PB |
312 | if (wi->exclusive) { |
313 | /* Running work items outside the BQL avoids the following deadlock: | |
314 | * 1) start_exclusive() is called with the BQL taken while another | |
315 | * CPU is running; 2) cpu_exec in the other CPU tries to takes the | |
316 | * BQL, so it goes to sleep; start_exclusive() is sleeping too, so | |
317 | * neither CPU can proceed. | |
318 | */ | |
319 | qemu_mutex_unlock_iothread(); | |
320 | start_exclusive(); | |
321 | wi->func(cpu, wi->data); | |
322 | end_exclusive(); | |
323 | qemu_mutex_lock_iothread(); | |
324 | } else { | |
325 | wi->func(cpu, wi->data); | |
326 | } | |
d148d90e SF |
327 | qemu_mutex_lock(&cpu->work_mutex); |
328 | if (wi->free) { | |
329 | g_free(wi); | |
330 | } else { | |
331 | atomic_mb_set(&wi->done, true); | |
332 | } | |
333 | } | |
334 | qemu_mutex_unlock(&cpu->work_mutex); | |
335 | qemu_cond_broadcast(&qemu_work_cond); | |
336 | } |