]>
git.proxmox.com Git - mirror_qemu.git/blob - accel/tcg/tcg-accel-ops-rr.c
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
26 #include "qemu/osdep.h"
27 #include "qemu-common.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"
36 #include "tcg-accel-ops.h"
37 #include "tcg-accel-ops-rr.h"
38 #include "tcg-accel-ops-icount.h"
40 /* Kick all RR vCPUs */
41 void rr_kick_vcpu_thread(CPUState
*unused
)
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 with force a cpu->exit so the next vCPU can get
58 * The timer is removed if all vCPUs are idle and restarted again once
59 * idleness is complete.
62 static QEMUTimer
*rr_kick_vcpu_timer
;
63 static CPUState
*rr_current_cpu
;
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)
75 cpu
= qatomic_mb_read(&rr_current_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());
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)
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();
118 qemu_wait_io_event_common(cpu
);
123 * Destroy any remaining vCPUs which have been unplugged and have
126 static void rr_deal_with_unplugged_cpus(void)
131 if (cpu
->unplug
&& !cpu_can_run(cpu
)) {
132 tcg_cpus_destroy(cpu
);
138 static void rr_force_rcu(Notifier
*notify
, void *data
)
144 * In the single-threaded case each vCPU is simulated in turn. If
145 * there is more than a single vCPU we create a simple timer to kick
146 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
147 * This is done explicitly rather than relying on side-effects
151 static void *rr_cpu_thread_fn(void *arg
)
156 assert(tcg_enabled());
157 rcu_register_thread();
158 force_rcu
.notify
= rr_force_rcu
;
159 rcu_add_force_rcu_notifier(&force_rcu
);
160 tcg_register_thread();
162 qemu_mutex_lock_iothread();
163 qemu_thread_get_self(cpu
->thread
);
165 cpu
->thread_id
= qemu_get_thread_id();
167 cpu_thread_signal_created(cpu
);
168 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
170 /* wait for initial kick-off after machine start */
171 while (first_cpu
->stopped
) {
172 qemu_cond_wait_iothread(first_cpu
->halt_cond
);
174 /* process any pending work */
177 qemu_wait_io_event_common(cpu
);
181 rr_start_kick_timer();
185 /* process any pending work */
186 cpu
->exit_request
= 1;
189 qemu_mutex_unlock_iothread();
191 qemu_mutex_lock_iothread();
193 if (icount_enabled()) {
194 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
195 icount_account_warp_timer();
197 * Run the timers here. This is much more efficient than
198 * waking up the I/O thread and waiting for completion.
200 icount_handle_deadline();
203 replay_mutex_unlock();
209 while (cpu
&& cpu_work_list_empty(cpu
) && !cpu
->exit_request
) {
211 qatomic_mb_set(&rr_current_cpu
, cpu
);
214 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
,
215 (cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
217 if (cpu_can_run(cpu
)) {
220 qemu_mutex_unlock_iothread();
221 if (icount_enabled()) {
222 icount_prepare_for_run(cpu
);
224 r
= tcg_cpus_exec(cpu
);
225 if (icount_enabled()) {
226 icount_process_data(cpu
);
228 qemu_mutex_lock_iothread();
230 if (r
== EXCP_DEBUG
) {
231 cpu_handle_guest_debug(cpu
);
233 } else if (r
== EXCP_ATOMIC
) {
234 qemu_mutex_unlock_iothread();
235 cpu_exec_step_atomic(cpu
);
236 qemu_mutex_lock_iothread();
239 } else if (cpu
->stop
) {
247 } /* while (cpu && !cpu->exit_request).. */
249 /* Does not need qatomic_mb_set because a spurious wakeup is okay. */
250 qatomic_set(&rr_current_cpu
, NULL
);
252 if (cpu
&& cpu
->exit_request
) {
253 qatomic_mb_set(&cpu
->exit_request
, 0);
256 if (icount_enabled() && all_cpu_threads_idle()) {
258 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
259 * in the main_loop, wake it up in order to start the warp timer.
265 rr_deal_with_unplugged_cpus();
268 rcu_remove_force_rcu_notifier(&force_rcu
);
269 rcu_unregister_thread();
273 void rr_start_vcpu_thread(CPUState
*cpu
)
275 char thread_name
[VCPU_THREAD_NAME_SIZE
];
276 static QemuCond
*single_tcg_halt_cond
;
277 static QemuThread
*single_tcg_cpu_thread
;
279 g_assert(tcg_enabled());
280 tcg_cpu_init_cflags(cpu
, false);
282 if (!single_tcg_cpu_thread
) {
283 cpu
->thread
= g_new0(QemuThread
, 1);
284 cpu
->halt_cond
= g_new0(QemuCond
, 1);
285 qemu_cond_init(cpu
->halt_cond
);
287 /* share a single thread for all cpus with TCG */
288 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "ALL CPUs/TCG");
289 qemu_thread_create(cpu
->thread
, thread_name
,
291 cpu
, QEMU_THREAD_JOINABLE
);
293 single_tcg_halt_cond
= cpu
->halt_cond
;
294 single_tcg_cpu_thread
= cpu
->thread
;
296 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
299 /* we share the thread */
300 cpu
->thread
= single_tcg_cpu_thread
;
301 cpu
->halt_cond
= single_tcg_halt_cond
;
302 cpu
->thread_id
= first_cpu
->thread_id
;