1 /* 2 * QEMU TCG Single Threaded vCPUs implementation 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * Copyright (c) 2014 Red Hat Inc. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #include "qemu/osdep.h" 27 #include "qemu/lockable.h" 28 #include "sysemu/tcg.h" 29 #include "sysemu/replay.h" 30 #include "sysemu/cpu-timers.h" 31 #include "qemu/main-loop.h" 32 #include "qemu/notify.h" 33 #include "qemu/guest-random.h" 34 #include "exec/exec-all.h" 35 36 #include "tcg-accel-ops.h" 37 #include "tcg-accel-ops-rr.h" 38 #include "tcg-accel-ops-icount.h" 39 40 /* Kick all RR vCPUs */ 41 void rr_kick_vcpu_thread(CPUState *unused) 42 { 43 CPUState *cpu; 44 45 CPU_FOREACH(cpu) { 46 cpu_exit(cpu); 47 }; 48 } 49 50 /* 51 * TCG vCPU kick timer 52 * 53 * The kick timer is responsible for moving single threaded vCPU 54 * emulation on to the next vCPU. If more than one vCPU is running a 55 * timer event we force a cpu->exit so the next vCPU can get 56 * scheduled. 57 * 58 * The timer is removed if all vCPUs are idle and restarted again once 59 * idleness is complete. 60 */ 61 62 static QEMUTimer *rr_kick_vcpu_timer; 63 static CPUState *rr_current_cpu; 64 65 static inline int64_t rr_next_kick_time(void) 66 { 67 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD; 68 } 69 70 /* Kick the currently round-robin scheduled vCPU to next */ 71 static void rr_kick_next_cpu(void) 72 { 73 CPUState *cpu; 74 do { 75 cpu = qatomic_mb_read(&rr_current_cpu); 76 if (cpu) { 77 cpu_exit(cpu); 78 } 79 } while (cpu != qatomic_mb_read(&rr_current_cpu)); 80 } 81 82 static void rr_kick_thread(void *opaque) 83 { 84 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time()); 85 rr_kick_next_cpu(); 86 } 87 88 static void rr_start_kick_timer(void) 89 { 90 if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) { 91 rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 92 rr_kick_thread, NULL); 93 } 94 if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) { 95 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time()); 96 } 97 } 98 99 static void rr_stop_kick_timer(void) 100 { 101 if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) { 102 timer_del(rr_kick_vcpu_timer); 103 } 104 } 105 106 static void rr_wait_io_event(void) 107 { 108 CPUState *cpu; 109 110 while (all_cpu_threads_idle()) { 111 rr_stop_kick_timer(); 112 qemu_cond_wait_iothread(first_cpu->halt_cond); 113 } 114 115 rr_start_kick_timer(); 116 117 CPU_FOREACH(cpu) { 118 qemu_wait_io_event_common(cpu); 119 } 120 } 121 122 /* 123 * Destroy any remaining vCPUs which have been unplugged and have 124 * finished running 125 */ 126 static void rr_deal_with_unplugged_cpus(void) 127 { 128 CPUState *cpu; 129 130 CPU_FOREACH(cpu) { 131 if (cpu->unplug && !cpu_can_run(cpu)) { 132 tcg_cpus_destroy(cpu); 133 break; 134 } 135 } 136 } 137 138 static void rr_force_rcu(Notifier *notify, void *data) 139 { 140 rr_kick_next_cpu(); 141 } 142 143 /* 144 * Calculate the number of CPUs that we will process in a single iteration of 145 * the main CPU thread loop so that we can fairly distribute the instruction 146 * count across CPUs. 147 * 148 * The CPU count is cached based on the CPU list generation ID to avoid 149 * iterating the list every time. 150 */ 151 static int rr_cpu_count(void) 152 { 153 static unsigned int last_gen_id = ~0; 154 static int cpu_count; 155 CPUState *cpu; 156 157 QEMU_LOCK_GUARD(&qemu_cpu_list_lock); 158 159 if (cpu_list_generation_id_get() != last_gen_id) { 160 cpu_count = 0; 161 CPU_FOREACH(cpu) { 162 ++cpu_count; 163 } 164 last_gen_id = cpu_list_generation_id_get(); 165 } 166 167 return cpu_count; 168 } 169 170 /* 171 * In the single-threaded case each vCPU is simulated in turn. If 172 * there is more than a single vCPU we create a simple timer to kick 173 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU. 174 * This is done explicitly rather than relying on side-effects 175 * elsewhere. 176 */ 177 178 static void *rr_cpu_thread_fn(void *arg) 179 { 180 Notifier force_rcu; 181 CPUState *cpu = arg; 182 183 assert(tcg_enabled()); 184 rcu_register_thread(); 185 force_rcu.notify = rr_force_rcu; 186 rcu_add_force_rcu_notifier(&force_rcu); 187 tcg_register_thread(); 188 189 qemu_mutex_lock_iothread(); 190 qemu_thread_get_self(cpu->thread); 191 192 cpu->thread_id = qemu_get_thread_id(); 193 cpu->can_do_io = 1; 194 cpu_thread_signal_created(cpu); 195 qemu_guest_random_seed_thread_part2(cpu->random_seed); 196 197 /* wait for initial kick-off after machine start */ 198 while (first_cpu->stopped) { 199 qemu_cond_wait_iothread(first_cpu->halt_cond); 200 201 /* process any pending work */ 202 CPU_FOREACH(cpu) { 203 current_cpu = cpu; 204 qemu_wait_io_event_common(cpu); 205 } 206 } 207 208 rr_start_kick_timer(); 209 210 cpu = first_cpu; 211 212 /* process any pending work */ 213 cpu->exit_request = 1; 214 215 while (1) { 216 /* Only used for icount_enabled() */ 217 int64_t cpu_budget = 0; 218 219 qemu_mutex_unlock_iothread(); 220 replay_mutex_lock(); 221 qemu_mutex_lock_iothread(); 222 223 if (icount_enabled()) { 224 int cpu_count = rr_cpu_count(); 225 226 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */ 227 icount_account_warp_timer(); 228 /* 229 * Run the timers here. This is much more efficient than 230 * waking up the I/O thread and waiting for completion. 231 */ 232 icount_handle_deadline(); 233 234 cpu_budget = icount_percpu_budget(cpu_count); 235 } 236 237 replay_mutex_unlock(); 238 239 if (!cpu) { 240 cpu = first_cpu; 241 } 242 243 while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) { 244 245 qatomic_mb_set(&rr_current_cpu, cpu); 246 current_cpu = cpu; 247 248 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, 249 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); 250 251 if (cpu_can_run(cpu)) { 252 int r; 253 254 qemu_mutex_unlock_iothread(); 255 if (icount_enabled()) { 256 icount_prepare_for_run(cpu, cpu_budget); 257 } 258 r = tcg_cpus_exec(cpu); 259 if (icount_enabled()) { 260 icount_process_data(cpu); 261 } 262 qemu_mutex_lock_iothread(); 263 264 if (r == EXCP_DEBUG) { 265 cpu_handle_guest_debug(cpu); 266 break; 267 } else if (r == EXCP_ATOMIC) { 268 qemu_mutex_unlock_iothread(); 269 cpu_exec_step_atomic(cpu); 270 qemu_mutex_lock_iothread(); 271 break; 272 } 273 } else if (cpu->stop) { 274 if (cpu->unplug) { 275 cpu = CPU_NEXT(cpu); 276 } 277 break; 278 } 279 280 cpu = CPU_NEXT(cpu); 281 } /* while (cpu && !cpu->exit_request).. */ 282 283 /* Does not need qatomic_mb_set because a spurious wakeup is okay. */ 284 qatomic_set(&rr_current_cpu, NULL); 285 286 if (cpu && cpu->exit_request) { 287 qatomic_mb_set(&cpu->exit_request, 0); 288 } 289 290 if (icount_enabled() && all_cpu_threads_idle()) { 291 /* 292 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock 293 * in the main_loop, wake it up in order to start the warp timer. 294 */ 295 qemu_notify_event(); 296 } 297 298 rr_wait_io_event(); 299 rr_deal_with_unplugged_cpus(); 300 } 301 302 rcu_remove_force_rcu_notifier(&force_rcu); 303 rcu_unregister_thread(); 304 return NULL; 305 } 306 307 void rr_start_vcpu_thread(CPUState *cpu) 308 { 309 char thread_name[VCPU_THREAD_NAME_SIZE]; 310 static QemuCond *single_tcg_halt_cond; 311 static QemuThread *single_tcg_cpu_thread; 312 313 g_assert(tcg_enabled()); 314 tcg_cpu_init_cflags(cpu, false); 315 316 if (!single_tcg_cpu_thread) { 317 cpu->thread = g_new0(QemuThread, 1); 318 cpu->halt_cond = g_new0(QemuCond, 1); 319 qemu_cond_init(cpu->halt_cond); 320 321 /* share a single thread for all cpus with TCG */ 322 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG"); 323 qemu_thread_create(cpu->thread, thread_name, 324 rr_cpu_thread_fn, 325 cpu, QEMU_THREAD_JOINABLE); 326 327 single_tcg_halt_cond = cpu->halt_cond; 328 single_tcg_cpu_thread = cpu->thread; 329 #ifdef _WIN32 330 cpu->hThread = qemu_thread_get_handle(cpu->thread); 331 #endif 332 } else { 333 /* we share the thread */ 334 cpu->thread = single_tcg_cpu_thread; 335 cpu->halt_cond = single_tcg_halt_cond; 336 cpu->thread_id = first_cpu->thread_id; 337 cpu->can_do_io = 1; 338 cpu->created = true; 339 } 340 } 341