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