]> git.proxmox.com Git - mirror_qemu.git/blob - cpus-common.c
7d935fd3dd9f93f9491421c6b50ede8e495bbb8b
[mirror_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 if (qemu_cpu_is_self(cpu)) {
157 func(cpu, data);
158 return;
159 }
160
161 wi = g_malloc0(sizeof(struct qemu_work_item));
162 wi->func = func;
163 wi->data = data;
164 wi->free = true;
165
166 queue_work_on_cpu(cpu, wi);
167 }
168
169 /* Wait for pending exclusive operations to complete. The CPU list lock
170 must be held. */
171 static inline void exclusive_idle(void)
172 {
173 while (pending_cpus) {
174 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
175 }
176 }
177
178 /* Start an exclusive operation.
179 Must only be called from outside cpu_exec, takes
180 qemu_cpu_list_lock. */
181 void start_exclusive(void)
182 {
183 CPUState *other_cpu;
184
185 qemu_mutex_lock(&qemu_cpu_list_lock);
186 exclusive_idle();
187
188 /* Make all other cpus stop executing. */
189 pending_cpus = 1;
190 CPU_FOREACH(other_cpu) {
191 if (other_cpu->running) {
192 pending_cpus++;
193 qemu_cpu_kick(other_cpu);
194 }
195 }
196 while (pending_cpus > 1) {
197 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
198 }
199 }
200
201 /* Finish an exclusive operation. Releases qemu_cpu_list_lock. */
202 void end_exclusive(void)
203 {
204 pending_cpus = 0;
205 qemu_cond_broadcast(&exclusive_resume);
206 qemu_mutex_unlock(&qemu_cpu_list_lock);
207 }
208
209 /* Wait for exclusive ops to finish, and begin cpu execution. */
210 void cpu_exec_start(CPUState *cpu)
211 {
212 qemu_mutex_lock(&qemu_cpu_list_lock);
213 exclusive_idle();
214 cpu->running = true;
215 qemu_mutex_unlock(&qemu_cpu_list_lock);
216 }
217
218 /* Mark cpu as not executing, and release pending exclusive ops. */
219 void cpu_exec_end(CPUState *cpu)
220 {
221 qemu_mutex_lock(&qemu_cpu_list_lock);
222 cpu->running = false;
223 if (pending_cpus > 1) {
224 pending_cpus--;
225 if (pending_cpus == 1) {
226 qemu_cond_signal(&exclusive_cond);
227 }
228 }
229 exclusive_idle();
230 qemu_mutex_unlock(&qemu_cpu_list_lock);
231 }
232
233 void process_queued_cpu_work(CPUState *cpu)
234 {
235 struct qemu_work_item *wi;
236
237 if (cpu->queued_work_first == NULL) {
238 return;
239 }
240
241 qemu_mutex_lock(&cpu->work_mutex);
242 while (cpu->queued_work_first != NULL) {
243 wi = cpu->queued_work_first;
244 cpu->queued_work_first = wi->next;
245 if (!cpu->queued_work_first) {
246 cpu->queued_work_last = NULL;
247 }
248 qemu_mutex_unlock(&cpu->work_mutex);
249 wi->func(cpu, wi->data);
250 qemu_mutex_lock(&cpu->work_mutex);
251 if (wi->free) {
252 g_free(wi);
253 } else {
254 atomic_mb_set(&wi->done, true);
255 }
256 }
257 qemu_mutex_unlock(&cpu->work_mutex);
258 qemu_cond_broadcast(&qemu_work_cond);
259 }