Merge remote-tracking branch 'remotes/vivier2/tags/trivial-branch-for-6.1-pull-reques...
[qemu/ar7.git] / accel / tcg / tcg-accel-ops-rr.c
blobc02c061ecb351abb8df3516a0e20ef1cc9512eb3
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 "qemu-common.h"
28 #include "sysemu/tcg.h"
29 #include "sysemu/replay.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/guest-random.h"
32 #include "exec/exec-all.h"
34 #include "tcg-accel-ops.h"
35 #include "tcg-accel-ops-rr.h"
36 #include "tcg-accel-ops-icount.h"
38 /* Kick all RR vCPUs */
39 void rr_kick_vcpu_thread(CPUState *unused)
41 CPUState *cpu;
43 CPU_FOREACH(cpu) {
44 cpu_exit(cpu);
49 * TCG vCPU kick timer
51 * The kick timer is responsible for moving single threaded vCPU
52 * emulation on to the next vCPU. If more than one vCPU is running a
53 * timer event with force a cpu->exit so the next vCPU can get
54 * scheduled.
56 * The timer is removed if all vCPUs are idle and restarted again once
57 * idleness is complete.
60 static QEMUTimer *rr_kick_vcpu_timer;
61 static CPUState *rr_current_cpu;
63 #define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
65 static inline int64_t rr_next_kick_time(void)
67 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
70 /* Kick the currently round-robin scheduled vCPU to next */
71 static void rr_kick_next_cpu(void)
73 CPUState *cpu;
74 do {
75 cpu = qatomic_mb_read(&rr_current_cpu);
76 if (cpu) {
77 cpu_exit(cpu);
79 } while (cpu != qatomic_mb_read(&rr_current_cpu));
82 static void rr_kick_thread(void *opaque)
84 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
85 rr_kick_next_cpu();
88 static void rr_start_kick_timer(void)
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);
94 if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) {
95 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
99 static void rr_stop_kick_timer(void)
101 if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) {
102 timer_del(rr_kick_vcpu_timer);
106 static void rr_wait_io_event(void)
108 CPUState *cpu;
110 while (all_cpu_threads_idle()) {
111 rr_stop_kick_timer();
112 qemu_cond_wait_iothread(first_cpu->halt_cond);
115 rr_start_kick_timer();
117 CPU_FOREACH(cpu) {
118 qemu_wait_io_event_common(cpu);
123 * Destroy any remaining vCPUs which have been unplugged and have
124 * finished running
126 static void rr_deal_with_unplugged_cpus(void)
128 CPUState *cpu;
130 CPU_FOREACH(cpu) {
131 if (cpu->unplug && !cpu_can_run(cpu)) {
132 tcg_cpus_destroy(cpu);
133 break;
139 * In the single-threaded case each vCPU is simulated in turn. If
140 * there is more than a single vCPU we create a simple timer to kick
141 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
142 * This is done explicitly rather than relying on side-effects
143 * elsewhere.
146 static void *rr_cpu_thread_fn(void *arg)
148 CPUState *cpu = arg;
150 assert(tcg_enabled());
151 rcu_register_thread();
152 tcg_register_thread();
154 qemu_mutex_lock_iothread();
155 qemu_thread_get_self(cpu->thread);
157 cpu->thread_id = qemu_get_thread_id();
158 cpu->can_do_io = 1;
159 cpu_thread_signal_created(cpu);
160 qemu_guest_random_seed_thread_part2(cpu->random_seed);
162 /* wait for initial kick-off after machine start */
163 while (first_cpu->stopped) {
164 qemu_cond_wait_iothread(first_cpu->halt_cond);
166 /* process any pending work */
167 CPU_FOREACH(cpu) {
168 current_cpu = cpu;
169 qemu_wait_io_event_common(cpu);
173 rr_start_kick_timer();
175 cpu = first_cpu;
177 /* process any pending work */
178 cpu->exit_request = 1;
180 while (1) {
181 qemu_mutex_unlock_iothread();
182 replay_mutex_lock();
183 qemu_mutex_lock_iothread();
185 if (icount_enabled()) {
186 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
187 icount_account_warp_timer();
189 * Run the timers here. This is much more efficient than
190 * waking up the I/O thread and waiting for completion.
192 icount_handle_deadline();
195 replay_mutex_unlock();
197 if (!cpu) {
198 cpu = first_cpu;
201 while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
203 qatomic_mb_set(&rr_current_cpu, cpu);
204 current_cpu = cpu;
206 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
207 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
209 if (cpu_can_run(cpu)) {
210 int r;
212 qemu_mutex_unlock_iothread();
213 if (icount_enabled()) {
214 icount_prepare_for_run(cpu);
216 r = tcg_cpus_exec(cpu);
217 if (icount_enabled()) {
218 icount_process_data(cpu);
220 qemu_mutex_lock_iothread();
222 if (r == EXCP_DEBUG) {
223 cpu_handle_guest_debug(cpu);
224 break;
225 } else if (r == EXCP_ATOMIC) {
226 qemu_mutex_unlock_iothread();
227 cpu_exec_step_atomic(cpu);
228 qemu_mutex_lock_iothread();
229 break;
231 } else if (cpu->stop) {
232 if (cpu->unplug) {
233 cpu = CPU_NEXT(cpu);
235 break;
238 cpu = CPU_NEXT(cpu);
239 } /* while (cpu && !cpu->exit_request).. */
241 /* Does not need qatomic_mb_set because a spurious wakeup is okay. */
242 qatomic_set(&rr_current_cpu, NULL);
244 if (cpu && cpu->exit_request) {
245 qatomic_mb_set(&cpu->exit_request, 0);
248 if (icount_enabled() && all_cpu_threads_idle()) {
250 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
251 * in the main_loop, wake it up in order to start the warp timer.
253 qemu_notify_event();
256 rr_wait_io_event();
257 rr_deal_with_unplugged_cpus();
260 rcu_unregister_thread();
261 return NULL;
264 void rr_start_vcpu_thread(CPUState *cpu)
266 char thread_name[VCPU_THREAD_NAME_SIZE];
267 static QemuCond *single_tcg_halt_cond;
268 static QemuThread *single_tcg_cpu_thread;
270 g_assert(tcg_enabled());
271 tcg_cpu_init_cflags(cpu, false);
273 if (!single_tcg_cpu_thread) {
274 cpu->thread = g_malloc0(sizeof(QemuThread));
275 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
276 qemu_cond_init(cpu->halt_cond);
278 /* share a single thread for all cpus with TCG */
279 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
280 qemu_thread_create(cpu->thread, thread_name,
281 rr_cpu_thread_fn,
282 cpu, QEMU_THREAD_JOINABLE);
284 single_tcg_halt_cond = cpu->halt_cond;
285 single_tcg_cpu_thread = cpu->thread;
286 #ifdef _WIN32
287 cpu->hThread = qemu_thread_get_handle(cpu->thread);
288 #endif
289 } else {
290 /* we share the thread */
291 cpu->thread = single_tcg_cpu_thread;
292 cpu->halt_cond = single_tcg_halt_cond;
293 cpu->thread_id = first_cpu->thread_id;
294 cpu->can_do_io = 1;
295 cpu->created = true;