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