nbd: remove incorrect coroutine_fn annotations
[qemu.git] / accel / tcg / tcg-accel-ops-rr.c
blobcc8adc23802c7b62f13a1c6d2573e56950a9e537
1 /*
2 * QEMU TCG Single Threaded vCPUs implementation
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2014 Red Hat Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "qemu/osdep.h"
27 #include "sysemu/tcg.h"
28 #include "sysemu/replay.h"
29 #include "sysemu/cpu-timers.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/notify.h"
32 #include "qemu/guest-random.h"
33 #include "exec/exec-all.h"
35 #include "tcg-accel-ops.h"
36 #include "tcg-accel-ops-rr.h"
37 #include "tcg-accel-ops-icount.h"
39 /* Kick all RR vCPUs */
40 void rr_kick_vcpu_thread(CPUState *unused)
42 CPUState *cpu;
44 CPU_FOREACH(cpu) {
45 cpu_exit(cpu);
50 * TCG vCPU kick timer
52 * The kick timer is responsible for moving single threaded vCPU
53 * emulation on to the next vCPU. If more than one vCPU is running a
54 * timer event with force a cpu->exit so the next vCPU can get
55 * scheduled.
57 * The timer is removed if all vCPUs are idle and restarted again once
58 * idleness is complete.
61 static QEMUTimer *rr_kick_vcpu_timer;
62 static CPUState *rr_current_cpu;
64 static inline int64_t rr_next_kick_time(void)
66 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
69 /* Kick the currently round-robin scheduled vCPU to next */
70 static void rr_kick_next_cpu(void)
72 CPUState *cpu;
73 do {
74 cpu = qatomic_mb_read(&rr_current_cpu);
75 if (cpu) {
76 cpu_exit(cpu);
78 } while (cpu != qatomic_mb_read(&rr_current_cpu));
81 static void rr_kick_thread(void *opaque)
83 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
84 rr_kick_next_cpu();
87 static void rr_start_kick_timer(void)
89 if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
90 rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
91 rr_kick_thread, NULL);
93 if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) {
94 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
98 static void rr_stop_kick_timer(void)
100 if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) {
101 timer_del(rr_kick_vcpu_timer);
105 static void rr_wait_io_event(void)
107 CPUState *cpu;
109 while (all_cpu_threads_idle()) {
110 rr_stop_kick_timer();
111 qemu_cond_wait_iothread(first_cpu->halt_cond);
114 rr_start_kick_timer();
116 CPU_FOREACH(cpu) {
117 qemu_wait_io_event_common(cpu);
122 * Destroy any remaining vCPUs which have been unplugged and have
123 * finished running
125 static void rr_deal_with_unplugged_cpus(void)
127 CPUState *cpu;
129 CPU_FOREACH(cpu) {
130 if (cpu->unplug && !cpu_can_run(cpu)) {
131 tcg_cpus_destroy(cpu);
132 break;
137 static void rr_force_rcu(Notifier *notify, void *data)
139 rr_kick_next_cpu();
143 * In the single-threaded case each vCPU is simulated in turn. If
144 * there is more than a single vCPU we create a simple timer to kick
145 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
146 * This is done explicitly rather than relying on side-effects
147 * elsewhere.
150 static void *rr_cpu_thread_fn(void *arg)
152 Notifier force_rcu;
153 CPUState *cpu = arg;
155 g_assert(tcg_enabled());
156 tcg_cpu_init_cflags(cpu, false);
158 rcu_register_thread();
159 force_rcu.notify = rr_force_rcu;
160 rcu_add_force_rcu_notifier(&force_rcu);
161 tcg_register_thread();
163 qemu_mutex_lock_iothread();
164 qemu_thread_get_self(cpu->thread);
166 cpu->thread_id = qemu_get_thread_id();
167 cpu->can_do_io = 1;
168 cpu_thread_signal_created(cpu);
169 qemu_guest_random_seed_thread_part2(cpu->random_seed);
171 /* wait for initial kick-off after machine start */
172 while (first_cpu->stopped) {
173 qemu_cond_wait_iothread(first_cpu->halt_cond);
175 /* process any pending work */
176 CPU_FOREACH(cpu) {
177 current_cpu = cpu;
178 qemu_wait_io_event_common(cpu);
182 rr_start_kick_timer();
184 cpu = first_cpu;
186 /* process any pending work */
187 cpu->exit_request = 1;
189 while (1) {
190 qemu_mutex_unlock_iothread();
191 replay_mutex_lock();
192 qemu_mutex_lock_iothread();
194 if (icount_enabled()) {
195 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
196 icount_account_warp_timer();
198 * Run the timers here. This is much more efficient than
199 * waking up the I/O thread and waiting for completion.
201 icount_handle_deadline();
204 replay_mutex_unlock();
206 if (!cpu) {
207 cpu = first_cpu;
210 while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
212 qatomic_mb_set(&rr_current_cpu, cpu);
213 current_cpu = cpu;
215 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
216 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
218 if (cpu_can_run(cpu)) {
219 int r;
221 qemu_mutex_unlock_iothread();
222 if (icount_enabled()) {
223 icount_prepare_for_run(cpu);
225 r = tcg_cpus_exec(cpu);
226 if (icount_enabled()) {
227 icount_process_data(cpu);
229 qemu_mutex_lock_iothread();
231 if (r == EXCP_DEBUG) {
232 cpu_handle_guest_debug(cpu);
233 break;
234 } else if (r == EXCP_ATOMIC) {
235 qemu_mutex_unlock_iothread();
236 cpu_exec_step_atomic(cpu);
237 qemu_mutex_lock_iothread();
238 break;
240 } else if (cpu->stop) {
241 if (cpu->unplug) {
242 cpu = CPU_NEXT(cpu);
244 break;
247 cpu = CPU_NEXT(cpu);
248 } /* while (cpu && !cpu->exit_request).. */
250 /* Does not need qatomic_mb_set because a spurious wakeup is okay. */
251 qatomic_set(&rr_current_cpu, NULL);
253 if (cpu && cpu->exit_request) {
254 qatomic_mb_set(&cpu->exit_request, 0);
257 if (icount_enabled() && all_cpu_threads_idle()) {
259 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
260 * in the main_loop, wake it up in order to start the warp timer.
262 qemu_notify_event();
265 rr_wait_io_event();
266 rr_deal_with_unplugged_cpus();
269 rcu_remove_force_rcu_notifier(&force_rcu);
270 rcu_unregister_thread();
271 return NULL;
274 void rr_start_vcpu_thread(CPUState *cpu)
276 char thread_name[VCPU_THREAD_NAME_SIZE];
277 static QemuCond *single_tcg_halt_cond;
278 static QemuThread *single_tcg_cpu_thread;
280 if (!single_tcg_cpu_thread) {
281 cpu->thread = g_new0(QemuThread, 1);
282 cpu->halt_cond = g_new0(QemuCond, 1);
283 qemu_cond_init(cpu->halt_cond);
285 /* share a single thread for all cpus with TCG */
286 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
287 qemu_thread_create(cpu->thread, thread_name,
288 rr_cpu_thread_fn,
289 cpu, QEMU_THREAD_JOINABLE);
291 single_tcg_halt_cond = cpu->halt_cond;
292 single_tcg_cpu_thread = cpu->thread;
293 #ifdef _WIN32
294 cpu->hThread = qemu_thread_get_handle(cpu->thread);
295 #endif
296 } else {
297 /* we share the thread */
298 cpu->thread = single_tcg_cpu_thread;
299 cpu->halt_cond = single_tcg_halt_cond;
300 cpu->thread_id = first_cpu->thread_id;
301 cpu->can_do_io = 1;
302 cpu->created = true;