Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 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" |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 21 | #include "qemu/main-loop.h" |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 22 | #include "exec/cpu-common.h" |
| 23 | #include "qom/cpu.h" |
| 24 | #include "sysemu/cpus.h" |
| 25 | |
| 26 | static QemuMutex qemu_cpu_list_lock; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 27 | static QemuCond exclusive_cond; |
| 28 | static QemuCond exclusive_resume; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 29 | static QemuCond qemu_work_cond; |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 30 | |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 31 | /* >= 1 if a thread is inside start_exclusive/end_exclusive. Written |
| 32 | * under qemu_cpu_list_lock, read with atomic operations. |
| 33 | */ |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 34 | static int pending_cpus; |
| 35 | |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 36 | void qemu_init_cpu_list(void) |
| 37 | { |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 42 | qemu_mutex_init(&qemu_cpu_list_lock); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 43 | qemu_cond_init(&exclusive_cond); |
| 44 | qemu_cond_init(&exclusive_resume); |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 45 | qemu_cond_init(&qemu_work_cond); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 72 | static void finish_safe_work(CPUState *cpu) |
| 73 | { |
| 74 | cpu_exec_start(cpu); |
| 75 | cpu_exec_end(cpu); |
| 76 | } |
| 77 | |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 78 | void cpu_list_add(CPUState *cpu) |
| 79 | { |
| 80 | qemu_mutex_lock(&qemu_cpu_list_lock); |
| 81 | if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) { |
| 82 | cpu->cpu_index = cpu_get_free_index(); |
| 83 | assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX); |
| 84 | } else { |
| 85 | assert(!cpu_index_auto_assigned); |
| 86 | } |
| 87 | QTAILQ_INSERT_TAIL(&cpus, cpu, node); |
| 88 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 89 | |
| 90 | finish_safe_work(cpu); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void cpu_list_remove(CPUState *cpu) |
| 94 | { |
| 95 | qemu_mutex_lock(&qemu_cpu_list_lock); |
| 96 | if (!QTAILQ_IN_USE(cpu, node)) { |
| 97 | /* there is nothing to undo since cpu_exec_init() hasn't been called */ |
| 98 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus, CPUTailQ))); |
| 103 | |
| 104 | QTAILQ_REMOVE(&cpus, cpu, node); |
| 105 | cpu->cpu_index = UNASSIGNED_CPU_INDEX; |
| 106 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 107 | } |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 108 | |
| 109 | struct qemu_work_item { |
| 110 | struct qemu_work_item *next; |
| 111 | run_on_cpu_func func; |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 112 | run_on_cpu_data data; |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 113 | bool free, exclusive, done; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) |
| 117 | { |
| 118 | qemu_mutex_lock(&cpu->work_mutex); |
| 119 | if (cpu->queued_work_first == NULL) { |
| 120 | cpu->queued_work_first = wi; |
| 121 | } else { |
| 122 | cpu->queued_work_last->next = wi; |
| 123 | } |
| 124 | cpu->queued_work_last = wi; |
| 125 | wi->next = NULL; |
| 126 | wi->done = false; |
| 127 | qemu_mutex_unlock(&cpu->work_mutex); |
| 128 | |
| 129 | qemu_cpu_kick(cpu); |
| 130 | } |
| 131 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 132 | void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data, |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 133 | QemuMutex *mutex) |
| 134 | { |
| 135 | struct qemu_work_item wi; |
| 136 | |
| 137 | if (qemu_cpu_is_self(cpu)) { |
| 138 | func(cpu, data); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | wi.func = func; |
| 143 | wi.data = data; |
Paolo Bonzini | 0e55539 | 2016-09-06 17:28:03 +0200 | [diff] [blame] | 144 | wi.done = false; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 145 | wi.free = false; |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 146 | wi.exclusive = false; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 147 | |
| 148 | queue_work_on_cpu(cpu, &wi); |
| 149 | while (!atomic_mb_read(&wi.done)) { |
| 150 | CPUState *self_cpu = current_cpu; |
| 151 | |
| 152 | qemu_cond_wait(&qemu_work_cond, mutex); |
| 153 | current_cpu = self_cpu; |
| 154 | } |
| 155 | } |
| 156 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 157 | void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 158 | { |
| 159 | struct qemu_work_item *wi; |
| 160 | |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 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. |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 179 | Must only be called from outside cpu_exec. */ |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 180 | void start_exclusive(void) |
| 181 | { |
| 182 | CPUState *other_cpu; |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 183 | int running_cpus; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 184 | |
| 185 | qemu_mutex_lock(&qemu_cpu_list_lock); |
| 186 | exclusive_idle(); |
| 187 | |
| 188 | /* Make all other cpus stop executing. */ |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 189 | atomic_set(&pending_cpus, 1); |
| 190 | |
| 191 | /* Write pending_cpus before reading other_cpu->running. */ |
| 192 | smp_mb(); |
| 193 | running_cpus = 0; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 194 | CPU_FOREACH(other_cpu) { |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 195 | if (atomic_read(&other_cpu->running)) { |
| 196 | other_cpu->has_waiter = true; |
| 197 | running_cpus++; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 198 | qemu_cpu_kick(other_cpu); |
| 199 | } |
| 200 | } |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 201 | |
| 202 | atomic_set(&pending_cpus, running_cpus + 1); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 203 | while (pending_cpus > 1) { |
| 204 | qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock); |
| 205 | } |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 206 | |
| 207 | /* Can release mutex, no one will enter another exclusive |
| 208 | * section until end_exclusive resets pending_cpus to 0. |
| 209 | */ |
| 210 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 213 | /* Finish an exclusive operation. */ |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 214 | void end_exclusive(void) |
| 215 | { |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 216 | qemu_mutex_lock(&qemu_cpu_list_lock); |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 217 | atomic_set(&pending_cpus, 0); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 218 | qemu_cond_broadcast(&exclusive_resume); |
| 219 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 220 | } |
| 221 | |
| 222 | /* Wait for exclusive ops to finish, and begin cpu execution. */ |
| 223 | void cpu_exec_start(CPUState *cpu) |
| 224 | { |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 225 | atomic_set(&cpu->running, true); |
| 226 | |
| 227 | /* Write cpu->running before reading pending_cpus. */ |
| 228 | smp_mb(); |
| 229 | |
| 230 | /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1. |
| 231 | * After taking the lock we'll see cpu->has_waiter == true and run---not |
| 232 | * for long because start_exclusive kicked us. cpu_exec_end will |
| 233 | * decrement pending_cpus and signal the waiter. |
| 234 | * |
| 235 | * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1. |
| 236 | * This includes the case when an exclusive item is running now. |
| 237 | * Then we'll see cpu->has_waiter == false and wait for the item to |
| 238 | * complete. |
| 239 | * |
| 240 | * 3. pending_cpus == 0. Then start_exclusive is definitely going to |
| 241 | * see cpu->running == true, and it will kick the CPU. |
| 242 | */ |
| 243 | if (unlikely(atomic_read(&pending_cpus))) { |
| 244 | qemu_mutex_lock(&qemu_cpu_list_lock); |
| 245 | if (!cpu->has_waiter) { |
| 246 | /* Not counted in pending_cpus, let the exclusive item |
| 247 | * run. Since we have the lock, just set cpu->running to true |
| 248 | * while holding it; no need to check pending_cpus again. |
| 249 | */ |
| 250 | atomic_set(&cpu->running, false); |
| 251 | exclusive_idle(); |
| 252 | /* Now pending_cpus is zero. */ |
| 253 | atomic_set(&cpu->running, true); |
| 254 | } else { |
| 255 | /* Counted in pending_cpus, go ahead and release the |
| 256 | * waiter at cpu_exec_end. |
| 257 | */ |
| 258 | } |
| 259 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 260 | } |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* Mark cpu as not executing, and release pending exclusive ops. */ |
| 264 | void cpu_exec_end(CPUState *cpu) |
| 265 | { |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 266 | atomic_set(&cpu->running, false); |
| 267 | |
| 268 | /* Write cpu->running before reading pending_cpus. */ |
| 269 | smp_mb(); |
| 270 | |
| 271 | /* 1. start_exclusive saw cpu->running == true. Then it will increment |
| 272 | * pending_cpus and wait for exclusive_cond. After taking the lock |
| 273 | * we'll see cpu->has_waiter == true. |
| 274 | * |
| 275 | * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1. |
| 276 | * This includes the case when an exclusive item started after setting |
| 277 | * cpu->running to false and before we read pending_cpus. Then we'll see |
| 278 | * cpu->has_waiter == false and not touch pending_cpus. The next call to |
| 279 | * cpu_exec_start will run exclusive_idle if still necessary, thus waiting |
| 280 | * for the item to complete. |
| 281 | * |
| 282 | * 3. pending_cpus == 0. Then start_exclusive is definitely going to |
| 283 | * see cpu->running == false, and it can ignore this CPU until the |
| 284 | * next cpu_exec_start. |
| 285 | */ |
| 286 | if (unlikely(atomic_read(&pending_cpus))) { |
| 287 | qemu_mutex_lock(&qemu_cpu_list_lock); |
| 288 | if (cpu->has_waiter) { |
| 289 | cpu->has_waiter = false; |
| 290 | atomic_set(&pending_cpus, pending_cpus - 1); |
| 291 | if (pending_cpus == 1) { |
| 292 | qemu_cond_signal(&exclusive_cond); |
| 293 | } |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 294 | } |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 295 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 296 | } |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 299 | void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, |
| 300 | run_on_cpu_data data) |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 301 | { |
| 302 | struct qemu_work_item *wi; |
| 303 | |
| 304 | wi = g_malloc0(sizeof(struct qemu_work_item)); |
| 305 | wi->func = func; |
| 306 | wi->data = data; |
| 307 | wi->free = true; |
| 308 | wi->exclusive = true; |
| 309 | |
| 310 | queue_work_on_cpu(cpu, wi); |
| 311 | } |
| 312 | |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 313 | void process_queued_cpu_work(CPUState *cpu) |
| 314 | { |
| 315 | struct qemu_work_item *wi; |
| 316 | |
| 317 | if (cpu->queued_work_first == NULL) { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | qemu_mutex_lock(&cpu->work_mutex); |
| 322 | while (cpu->queued_work_first != NULL) { |
| 323 | wi = cpu->queued_work_first; |
| 324 | cpu->queued_work_first = wi->next; |
| 325 | if (!cpu->queued_work_first) { |
| 326 | cpu->queued_work_last = NULL; |
| 327 | } |
| 328 | qemu_mutex_unlock(&cpu->work_mutex); |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 329 | if (wi->exclusive) { |
| 330 | /* Running work items outside the BQL avoids the following deadlock: |
| 331 | * 1) start_exclusive() is called with the BQL taken while another |
| 332 | * CPU is running; 2) cpu_exec in the other CPU tries to takes the |
| 333 | * BQL, so it goes to sleep; start_exclusive() is sleeping too, so |
| 334 | * neither CPU can proceed. |
| 335 | */ |
| 336 | qemu_mutex_unlock_iothread(); |
| 337 | start_exclusive(); |
| 338 | wi->func(cpu, wi->data); |
| 339 | end_exclusive(); |
| 340 | qemu_mutex_lock_iothread(); |
| 341 | } else { |
| 342 | wi->func(cpu, wi->data); |
| 343 | } |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 344 | qemu_mutex_lock(&cpu->work_mutex); |
| 345 | if (wi->free) { |
| 346 | g_free(wi); |
| 347 | } else { |
| 348 | atomic_mb_set(&wi->done, true); |
| 349 | } |
| 350 | } |
| 351 | qemu_mutex_unlock(&cpu->work_mutex); |
| 352 | qemu_cond_broadcast(&qemu_work_cond); |
| 353 | } |