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.1 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 "qemu/lockable.h" 25 #include "trace/trace-root.h" 26 27 QemuMutex qemu_cpu_list_lock; 28 static QemuCond exclusive_cond; 29 static QemuCond exclusive_resume; 30 static QemuCond qemu_work_cond; 31 32 /* >= 1 if a thread is inside start_exclusive/end_exclusive. Written 33 * under qemu_cpu_list_lock, read with atomic operations. 34 */ 35 static int pending_cpus; 36 37 void qemu_init_cpu_list(void) 38 { 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 43 qemu_mutex_init(&qemu_cpu_list_lock); 44 qemu_cond_init(&exclusive_cond); 45 qemu_cond_init(&exclusive_resume); 46 qemu_cond_init(&qemu_work_cond); 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 60 int cpu_get_free_index(void) 61 { 62 CPUState *some_cpu; 63 int max_cpu_index = 0; 64 65 CPU_FOREACH(some_cpu) { 66 if (some_cpu->cpu_index >= max_cpu_index) { 67 max_cpu_index = some_cpu->cpu_index + 1; 68 } 69 } 70 return max_cpu_index; 71 } 72 73 CPUTailQ cpus_queue = QTAILQ_HEAD_INITIALIZER(cpus_queue); 74 static unsigned int cpu_list_generation_id; 75 76 unsigned int cpu_list_generation_id_get(void) 77 { 78 return cpu_list_generation_id; 79 } 80 81 void cpu_list_add(CPUState *cpu) 82 { 83 static bool cpu_index_auto_assigned; 84 85 QEMU_LOCK_GUARD(&qemu_cpu_list_lock); 86 if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) { 87 cpu_index_auto_assigned = true; 88 cpu->cpu_index = cpu_get_free_index(); 89 assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX); 90 } else { 91 assert(!cpu_index_auto_assigned); 92 } 93 QTAILQ_INSERT_TAIL_RCU(&cpus_queue, cpu, node); 94 cpu_list_generation_id++; 95 } 96 97 void cpu_list_remove(CPUState *cpu) 98 { 99 QEMU_LOCK_GUARD(&qemu_cpu_list_lock); 100 if (!QTAILQ_IN_USE(cpu, node)) { 101 /* there is nothing to undo since cpu_exec_init() hasn't been called */ 102 return; 103 } 104 105 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node); 106 cpu->cpu_index = UNASSIGNED_CPU_INDEX; 107 cpu_list_generation_id++; 108 } 109 110 CPUState *qemu_get_cpu(int index) 111 { 112 CPUState *cpu; 113 114 CPU_FOREACH(cpu) { 115 if (cpu->cpu_index == index) { 116 return cpu; 117 } 118 } 119 120 return NULL; 121 } 122 123 /* current CPU in the current thread. It is only valid inside cpu_exec() */ 124 __thread CPUState *current_cpu; 125 126 struct qemu_work_item { 127 QSIMPLEQ_ENTRY(qemu_work_item) node; 128 run_on_cpu_func func; 129 run_on_cpu_data data; 130 bool free, exclusive, done; 131 }; 132 133 static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) 134 { 135 qemu_mutex_lock(&cpu->work_mutex); 136 QSIMPLEQ_INSERT_TAIL(&cpu->work_list, wi, node); 137 wi->done = false; 138 qemu_mutex_unlock(&cpu->work_mutex); 139 140 /* exit the inner loop and reach qemu_process_cpu_events_common(). */ 141 cpu_exit(cpu); 142 } 143 144 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data, 145 QemuMutex *mutex) 146 { 147 struct qemu_work_item wi; 148 149 if (qemu_cpu_is_self(cpu)) { 150 func(cpu, data); 151 return; 152 } 153 154 wi.func = func; 155 wi.data = data; 156 wi.done = false; 157 wi.free = false; 158 wi.exclusive = false; 159 160 queue_work_on_cpu(cpu, &wi); 161 while (!qatomic_load_acquire(&wi.done)) { 162 CPUState *self_cpu = current_cpu; 163 164 qemu_cond_wait(&qemu_work_cond, mutex); 165 current_cpu = self_cpu; 166 } 167 } 168 169 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) 170 { 171 struct qemu_work_item *wi; 172 173 wi = g_new0(struct qemu_work_item, 1); 174 wi->func = func; 175 wi->data = data; 176 wi->free = true; 177 178 queue_work_on_cpu(cpu, wi); 179 } 180 181 /* Wait for pending exclusive operations to complete. The CPU list lock 182 must be held. */ 183 static inline void exclusive_idle(void) 184 { 185 while (pending_cpus) { 186 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock); 187 } 188 } 189 190 /* Start an exclusive operation. 191 Must only be called from outside cpu_exec. */ 192 void start_exclusive(void) 193 { 194 CPUState *other_cpu; 195 int running_cpus; 196 197 /* Ensure we are not running, or start_exclusive will be blocked. */ 198 g_assert(!current_cpu->running); 199 200 if (current_cpu->exclusive_context_count) { 201 current_cpu->exclusive_context_count++; 202 return; 203 } 204 205 qemu_mutex_lock(&qemu_cpu_list_lock); 206 exclusive_idle(); 207 208 /* Make all other cpus stop executing. */ 209 qatomic_set(&pending_cpus, 1); 210 211 /* Write pending_cpus before reading other_cpu->running. */ 212 smp_mb(); 213 running_cpus = 0; 214 CPU_FOREACH(other_cpu) { 215 if (qatomic_read(&other_cpu->running)) { 216 other_cpu->has_waiter = true; 217 running_cpus++; 218 qemu_cpu_kick(other_cpu); 219 } 220 } 221 222 qatomic_set(&pending_cpus, running_cpus + 1); 223 while (pending_cpus > 1) { 224 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock); 225 } 226 227 /* Can release mutex, no one will enter another exclusive 228 * section until end_exclusive resets pending_cpus to 0. 229 */ 230 qemu_mutex_unlock(&qemu_cpu_list_lock); 231 232 current_cpu->exclusive_context_count = 1; 233 } 234 235 /* Finish an exclusive operation. */ 236 void end_exclusive(void) 237 { 238 current_cpu->exclusive_context_count--; 239 if (current_cpu->exclusive_context_count) { 240 return; 241 } 242 243 qemu_mutex_lock(&qemu_cpu_list_lock); 244 qatomic_set(&pending_cpus, 0); 245 qemu_cond_broadcast(&exclusive_resume); 246 qemu_mutex_unlock(&qemu_cpu_list_lock); 247 } 248 249 /* Wait for exclusive ops to finish, and begin cpu execution. */ 250 void cpu_exec_start(CPUState *cpu) 251 { 252 trace_cpu_exec_start(cpu->cpu_index); 253 254 qatomic_set(&cpu->running, true); 255 256 /* Write cpu->running before reading pending_cpus. */ 257 smp_mb(); 258 259 /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1. 260 * After taking the lock we'll see cpu->has_waiter == true and run---not 261 * for long because start_exclusive kicked us. cpu_exec_end will 262 * decrement pending_cpus and signal the waiter. 263 * 264 * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1. 265 * This includes the case when an exclusive item is running now. 266 * Then we'll see cpu->has_waiter == false and wait for the item to 267 * complete. 268 * 269 * 3. pending_cpus == 0. Then start_exclusive is definitely going to 270 * see cpu->running == true, and it will kick the CPU. 271 */ 272 if (unlikely(qatomic_read(&pending_cpus))) { 273 QEMU_LOCK_GUARD(&qemu_cpu_list_lock); 274 if (!cpu->has_waiter) { 275 /* Not counted in pending_cpus, let the exclusive item 276 * run. Since we have the lock, just set cpu->running to true 277 * while holding it; no need to check pending_cpus again. 278 */ 279 qatomic_set(&cpu->running, false); 280 exclusive_idle(); 281 /* Now pending_cpus is zero. */ 282 qatomic_set(&cpu->running, true); 283 } else { 284 /* Counted in pending_cpus, go ahead and release the 285 * waiter at cpu_exec_end. 286 */ 287 } 288 } 289 } 290 291 /* Mark cpu as not executing, and release pending exclusive ops. */ 292 void cpu_exec_end(CPUState *cpu) 293 { 294 qatomic_set(&cpu->running, false); 295 296 /* Write cpu->running before reading pending_cpus. */ 297 smp_mb(); 298 299 /* 1. start_exclusive saw cpu->running == true. Then it will increment 300 * pending_cpus and wait for exclusive_cond. After taking the lock 301 * we'll see cpu->has_waiter == true. 302 * 303 * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1. 304 * This includes the case when an exclusive item started after setting 305 * cpu->running to false and before we read pending_cpus. Then we'll see 306 * cpu->has_waiter == false and not touch pending_cpus. The next call to 307 * cpu_exec_start will run exclusive_idle if still necessary, thus waiting 308 * for the item to complete. 309 * 310 * 3. pending_cpus == 0. Then start_exclusive is definitely going to 311 * see cpu->running == false, and it can ignore this CPU until the 312 * next cpu_exec_start. 313 */ 314 if (unlikely(qatomic_read(&pending_cpus))) { 315 QEMU_LOCK_GUARD(&qemu_cpu_list_lock); 316 if (cpu->has_waiter) { 317 cpu->has_waiter = false; 318 qatomic_set(&pending_cpus, pending_cpus - 1); 319 if (pending_cpus == 1) { 320 qemu_cond_signal(&exclusive_cond); 321 } 322 } 323 } 324 trace_cpu_exec_end(cpu->cpu_index); 325 } 326 327 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, 328 run_on_cpu_data data) 329 { 330 struct qemu_work_item *wi; 331 332 wi = g_new0(struct qemu_work_item, 1); 333 wi->func = func; 334 wi->data = data; 335 wi->free = true; 336 wi->exclusive = true; 337 338 queue_work_on_cpu(cpu, wi); 339 } 340 341 void free_queued_cpu_work(CPUState *cpu) 342 { 343 while (!QSIMPLEQ_EMPTY(&cpu->work_list)) { 344 struct qemu_work_item *wi = QSIMPLEQ_FIRST(&cpu->work_list); 345 QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node); 346 if (wi->free) { 347 g_free(wi); 348 } 349 } 350 } 351 352 void process_queued_cpu_work(CPUState *cpu) 353 { 354 struct qemu_work_item *wi; 355 356 qemu_mutex_lock(&cpu->work_mutex); 357 if (QSIMPLEQ_EMPTY(&cpu->work_list)) { 358 qemu_mutex_unlock(&cpu->work_mutex); 359 return; 360 } 361 while (!QSIMPLEQ_EMPTY(&cpu->work_list)) { 362 wi = QSIMPLEQ_FIRST(&cpu->work_list); 363 QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node); 364 qemu_mutex_unlock(&cpu->work_mutex); 365 if (wi->exclusive) { 366 /* Running work items outside the BQL avoids the following deadlock: 367 * 1) start_exclusive() is called with the BQL taken while another 368 * CPU is running; 2) cpu_exec in the other CPU tries to takes the 369 * BQL, so it goes to sleep; start_exclusive() is sleeping too, so 370 * neither CPU can proceed. 371 */ 372 bql_unlock(); 373 start_exclusive(); 374 wi->func(cpu, wi->data); 375 end_exclusive(); 376 bql_lock(); 377 } else { 378 wi->func(cpu, wi->data); 379 } 380 qemu_mutex_lock(&cpu->work_mutex); 381 if (wi->free) { 382 g_free(wi); 383 } else { 384 qatomic_store_release(&wi->done, true); 385 } 386 } 387 qemu_mutex_unlock(&cpu->work_mutex); 388 qemu_cond_broadcast(&qemu_work_cond); 389 } 390 391 /* Add a breakpoint. */ 392 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags, 393 CPUBreakpoint **breakpoint) 394 { 395 CPUBreakpoint *bp; 396 397 if (cpu->cc->gdb_adjust_breakpoint) { 398 pc = cpu->cc->gdb_adjust_breakpoint(cpu, pc); 399 } 400 401 bp = g_malloc(sizeof(*bp)); 402 403 bp->pc = pc; 404 bp->flags = flags; 405 406 /* keep all GDB-injected breakpoints in front */ 407 if (flags & BP_GDB) { 408 QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry); 409 } else { 410 QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry); 411 } 412 413 if (breakpoint) { 414 *breakpoint = bp; 415 } 416 417 trace_breakpoint_insert(cpu->cpu_index, pc, flags); 418 return 0; 419 } 420 421 /* Remove a specific breakpoint. */ 422 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags) 423 { 424 CPUBreakpoint *bp; 425 426 if (cpu->cc->gdb_adjust_breakpoint) { 427 pc = cpu->cc->gdb_adjust_breakpoint(cpu, pc); 428 } 429 430 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { 431 if (bp->pc == pc && bp->flags == flags) { 432 cpu_breakpoint_remove_by_ref(cpu, bp); 433 return 0; 434 } 435 } 436 return -ENOENT; 437 } 438 439 /* Remove a specific breakpoint by reference. */ 440 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *bp) 441 { 442 QTAILQ_REMOVE(&cpu->breakpoints, bp, entry); 443 444 trace_breakpoint_remove(cpu->cpu_index, bp->pc, bp->flags); 445 g_free(bp); 446 } 447 448 /* Remove all matching breakpoints. */ 449 void cpu_breakpoint_remove_all(CPUState *cpu, int mask) 450 { 451 CPUBreakpoint *bp, *next; 452 453 QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) { 454 if (bp->flags & mask) { 455 cpu_breakpoint_remove_by_ref(cpu, bp); 456 } 457 } 458 } 459