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" |
Markus Armbruster | 2e5b09f | 2019-07-09 17:20:52 +0200 | [diff] [blame] | 23 | #include "hw/core/cpu.h" |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 24 | #include "sysemu/cpus.h" |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame^] | 25 | #include "qemu/lockable.h" |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 26 | |
| 27 | static QemuMutex qemu_cpu_list_lock; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 28 | static QemuCond exclusive_cond; |
| 29 | static QemuCond exclusive_resume; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 30 | static QemuCond qemu_work_cond; |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 31 | |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 32 | /* >= 1 if a thread is inside start_exclusive/end_exclusive. Written |
| 33 | * under qemu_cpu_list_lock, read with atomic operations. |
| 34 | */ |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 35 | static int pending_cpus; |
| 36 | |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 37 | void qemu_init_cpu_list(void) |
| 38 | { |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 43 | qemu_mutex_init(&qemu_cpu_list_lock); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 44 | qemu_cond_init(&exclusive_cond); |
| 45 | qemu_cond_init(&exclusive_resume); |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 46 | qemu_cond_init(&qemu_work_cond); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 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 | static bool cpu_index_auto_assigned; |
| 60 | |
| 61 | static int cpu_get_free_index(void) |
| 62 | { |
| 63 | CPUState *some_cpu; |
| 64 | int cpu_index = 0; |
| 65 | |
| 66 | cpu_index_auto_assigned = true; |
| 67 | CPU_FOREACH(some_cpu) { |
| 68 | cpu_index++; |
| 69 | } |
| 70 | return cpu_index; |
| 71 | } |
| 72 | |
| 73 | void cpu_list_add(CPUState *cpu) |
| 74 | { |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame^] | 75 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 76 | if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) { |
| 77 | cpu->cpu_index = cpu_get_free_index(); |
| 78 | assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX); |
| 79 | } else { |
| 80 | assert(!cpu_index_auto_assigned); |
| 81 | } |
Emilio G. Cota | 068a5ea | 2018-08-19 05:13:35 -0400 | [diff] [blame] | 82 | QTAILQ_INSERT_TAIL_RCU(&cpus, cpu, node); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void cpu_list_remove(CPUState *cpu) |
| 86 | { |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame^] | 87 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 88 | if (!QTAILQ_IN_USE(cpu, node)) { |
| 89 | /* there is nothing to undo since cpu_exec_init() hasn't been called */ |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
Paolo Bonzini | eae3eb3 | 2018-12-06 13:10:34 +0100 | [diff] [blame] | 93 | assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus))); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 94 | |
Emilio G. Cota | 068a5ea | 2018-08-19 05:13:35 -0400 | [diff] [blame] | 95 | QTAILQ_REMOVE_RCU(&cpus, cpu, node); |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 96 | cpu->cpu_index = UNASSIGNED_CPU_INDEX; |
Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame] | 97 | } |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 98 | |
| 99 | struct qemu_work_item { |
| 100 | struct qemu_work_item *next; |
| 101 | run_on_cpu_func func; |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 102 | run_on_cpu_data data; |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 103 | bool free, exclusive, done; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) |
| 107 | { |
| 108 | qemu_mutex_lock(&cpu->work_mutex); |
| 109 | if (cpu->queued_work_first == NULL) { |
| 110 | cpu->queued_work_first = wi; |
| 111 | } else { |
| 112 | cpu->queued_work_last->next = wi; |
| 113 | } |
| 114 | cpu->queued_work_last = wi; |
| 115 | wi->next = NULL; |
| 116 | wi->done = false; |
| 117 | qemu_mutex_unlock(&cpu->work_mutex); |
| 118 | |
| 119 | qemu_cpu_kick(cpu); |
| 120 | } |
| 121 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 122 | 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] | 123 | QemuMutex *mutex) |
| 124 | { |
| 125 | struct qemu_work_item wi; |
| 126 | |
| 127 | if (qemu_cpu_is_self(cpu)) { |
| 128 | func(cpu, data); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | wi.func = func; |
| 133 | wi.data = data; |
Paolo Bonzini | 0e55539 | 2016-09-06 17:28:03 +0200 | [diff] [blame] | 134 | wi.done = false; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 135 | wi.free = false; |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 136 | wi.exclusive = false; |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 137 | |
| 138 | queue_work_on_cpu(cpu, &wi); |
| 139 | while (!atomic_mb_read(&wi.done)) { |
| 140 | CPUState *self_cpu = current_cpu; |
| 141 | |
| 142 | qemu_cond_wait(&qemu_work_cond, mutex); |
| 143 | current_cpu = self_cpu; |
| 144 | } |
| 145 | } |
| 146 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 147 | 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] | 148 | { |
| 149 | struct qemu_work_item *wi; |
| 150 | |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 151 | wi = g_malloc0(sizeof(struct qemu_work_item)); |
| 152 | wi->func = func; |
| 153 | wi->data = data; |
| 154 | wi->free = true; |
| 155 | |
| 156 | queue_work_on_cpu(cpu, wi); |
| 157 | } |
| 158 | |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 159 | /* Wait for pending exclusive operations to complete. The CPU list lock |
| 160 | must be held. */ |
| 161 | static inline void exclusive_idle(void) |
| 162 | { |
| 163 | while (pending_cpus) { |
| 164 | qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* Start an exclusive operation. |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 169 | Must only be called from outside cpu_exec. */ |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 170 | void start_exclusive(void) |
| 171 | { |
| 172 | CPUState *other_cpu; |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 173 | int running_cpus; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 174 | |
| 175 | qemu_mutex_lock(&qemu_cpu_list_lock); |
| 176 | exclusive_idle(); |
| 177 | |
| 178 | /* Make all other cpus stop executing. */ |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 179 | atomic_set(&pending_cpus, 1); |
| 180 | |
| 181 | /* Write pending_cpus before reading other_cpu->running. */ |
| 182 | smp_mb(); |
| 183 | running_cpus = 0; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 184 | CPU_FOREACH(other_cpu) { |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 185 | if (atomic_read(&other_cpu->running)) { |
| 186 | other_cpu->has_waiter = true; |
| 187 | running_cpus++; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 188 | qemu_cpu_kick(other_cpu); |
| 189 | } |
| 190 | } |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 191 | |
| 192 | atomic_set(&pending_cpus, running_cpus + 1); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 193 | while (pending_cpus > 1) { |
| 194 | qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock); |
| 195 | } |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 196 | |
| 197 | /* Can release mutex, no one will enter another exclusive |
| 198 | * section until end_exclusive resets pending_cpus to 0. |
| 199 | */ |
| 200 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
Emilio G. Cota | cfbc3c6 | 2018-11-26 17:14:43 -0500 | [diff] [blame] | 201 | |
| 202 | current_cpu->in_exclusive_context = true; |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 205 | /* Finish an exclusive operation. */ |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 206 | void end_exclusive(void) |
| 207 | { |
Emilio G. Cota | cfbc3c6 | 2018-11-26 17:14:43 -0500 | [diff] [blame] | 208 | current_cpu->in_exclusive_context = false; |
| 209 | |
Paolo Bonzini | 758e1b2 | 2016-09-02 23:33:38 +0200 | [diff] [blame] | 210 | qemu_mutex_lock(&qemu_cpu_list_lock); |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 211 | atomic_set(&pending_cpus, 0); |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 212 | qemu_cond_broadcast(&exclusive_resume); |
| 213 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 214 | } |
| 215 | |
| 216 | /* Wait for exclusive ops to finish, and begin cpu execution. */ |
| 217 | void cpu_exec_start(CPUState *cpu) |
| 218 | { |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 219 | atomic_set(&cpu->running, true); |
| 220 | |
| 221 | /* Write cpu->running before reading pending_cpus. */ |
| 222 | smp_mb(); |
| 223 | |
| 224 | /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1. |
| 225 | * After taking the lock we'll see cpu->has_waiter == true and run---not |
| 226 | * for long because start_exclusive kicked us. cpu_exec_end will |
| 227 | * decrement pending_cpus and signal the waiter. |
| 228 | * |
| 229 | * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1. |
| 230 | * This includes the case when an exclusive item is running now. |
| 231 | * Then we'll see cpu->has_waiter == false and wait for the item to |
| 232 | * complete. |
| 233 | * |
| 234 | * 3. pending_cpus == 0. Then start_exclusive is definitely going to |
| 235 | * see cpu->running == true, and it will kick the CPU. |
| 236 | */ |
| 237 | if (unlikely(atomic_read(&pending_cpus))) { |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame^] | 238 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 239 | if (!cpu->has_waiter) { |
| 240 | /* Not counted in pending_cpus, let the exclusive item |
| 241 | * run. Since we have the lock, just set cpu->running to true |
| 242 | * while holding it; no need to check pending_cpus again. |
| 243 | */ |
| 244 | atomic_set(&cpu->running, false); |
| 245 | exclusive_idle(); |
| 246 | /* Now pending_cpus is zero. */ |
| 247 | atomic_set(&cpu->running, true); |
| 248 | } else { |
| 249 | /* Counted in pending_cpus, go ahead and release the |
| 250 | * waiter at cpu_exec_end. |
| 251 | */ |
| 252 | } |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 253 | } |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Mark cpu as not executing, and release pending exclusive ops. */ |
| 257 | void cpu_exec_end(CPUState *cpu) |
| 258 | { |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 259 | atomic_set(&cpu->running, false); |
| 260 | |
| 261 | /* Write cpu->running before reading pending_cpus. */ |
| 262 | smp_mb(); |
| 263 | |
| 264 | /* 1. start_exclusive saw cpu->running == true. Then it will increment |
| 265 | * pending_cpus and wait for exclusive_cond. After taking the lock |
| 266 | * we'll see cpu->has_waiter == true. |
| 267 | * |
| 268 | * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1. |
| 269 | * This includes the case when an exclusive item started after setting |
| 270 | * cpu->running to false and before we read pending_cpus. Then we'll see |
| 271 | * cpu->has_waiter == false and not touch pending_cpus. The next call to |
| 272 | * cpu_exec_start will run exclusive_idle if still necessary, thus waiting |
| 273 | * for the item to complete. |
| 274 | * |
| 275 | * 3. pending_cpus == 0. Then start_exclusive is definitely going to |
| 276 | * see cpu->running == false, and it can ignore this CPU until the |
| 277 | * next cpu_exec_start. |
| 278 | */ |
| 279 | if (unlikely(atomic_read(&pending_cpus))) { |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame^] | 280 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); |
Paolo Bonzini | c265e97 | 2016-08-31 21:33:58 +0200 | [diff] [blame] | 281 | if (cpu->has_waiter) { |
| 282 | cpu->has_waiter = false; |
| 283 | atomic_set(&pending_cpus, pending_cpus - 1); |
| 284 | if (pending_cpus == 1) { |
| 285 | qemu_cond_signal(&exclusive_cond); |
| 286 | } |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 287 | } |
| 288 | } |
Paolo Bonzini | ab12997 | 2016-08-31 16:56:04 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 291 | void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, |
| 292 | run_on_cpu_data data) |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 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 | |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 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); |
Paolo Bonzini | 53f5ed9 | 2016-08-28 05:38:24 +0200 | [diff] [blame] | 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 | } |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 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 | } |