2020-10-15 17:32:15 +03:00
|
|
|
/*
|
|
|
|
* QEMU TCG Single Threaded vCPUs implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
* Copyright (c) 2014 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2023-04-27 05:09:25 +03:00
|
|
|
#include "qemu/lockable.h"
|
2020-10-15 17:32:15 +03:00
|
|
|
#include "sysemu/tcg.h"
|
|
|
|
#include "sysemu/replay.h"
|
2022-01-22 16:23:41 +03:00
|
|
|
#include "sysemu/cpu-timers.h"
|
2020-10-15 17:32:15 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2021-11-09 21:35:23 +03:00
|
|
|
#include "qemu/notify.h"
|
2020-10-15 17:32:15 +03:00
|
|
|
#include "qemu/guest-random.h"
|
|
|
|
#include "exec/exec-all.h"
|
2023-09-30 05:54:54 +03:00
|
|
|
#include "tcg/startup.h"
|
2021-02-04 19:39:25 +03:00
|
|
|
#include "tcg-accel-ops.h"
|
|
|
|
#include "tcg-accel-ops-rr.h"
|
|
|
|
#include "tcg-accel-ops-icount.h"
|
2020-10-15 17:32:15 +03:00
|
|
|
|
|
|
|
/* Kick all RR vCPUs */
|
2020-10-15 17:32:17 +03:00
|
|
|
void rr_kick_vcpu_thread(CPUState *unused)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
|
|
|
CPUState *cpu;
|
|
|
|
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
cpu_exit(cpu);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TCG vCPU kick timer
|
|
|
|
*
|
|
|
|
* The kick timer is responsible for moving single threaded vCPU
|
|
|
|
* emulation on to the next vCPU. If more than one vCPU is running a
|
2022-10-21 20:36:06 +03:00
|
|
|
* timer event we force a cpu->exit so the next vCPU can get
|
2020-10-15 17:32:15 +03:00
|
|
|
* scheduled.
|
|
|
|
*
|
|
|
|
* The timer is removed if all vCPUs are idle and restarted again once
|
|
|
|
* idleness is complete.
|
|
|
|
*/
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static QEMUTimer *rr_kick_vcpu_timer;
|
|
|
|
static CPUState *rr_current_cpu;
|
2020-10-15 17:32:15 +03:00
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static inline int64_t rr_next_kick_time(void)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
|
|
|
return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Kick the currently round-robin scheduled vCPU to next */
|
2020-10-15 17:32:17 +03:00
|
|
|
static void rr_kick_next_cpu(void)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
|
|
|
CPUState *cpu;
|
|
|
|
do {
|
2023-03-03 15:18:23 +03:00
|
|
|
cpu = qatomic_read(&rr_current_cpu);
|
2020-10-15 17:32:15 +03:00
|
|
|
if (cpu) {
|
|
|
|
cpu_exit(cpu);
|
|
|
|
}
|
2023-03-03 15:18:23 +03:00
|
|
|
/* Finish kicking this cpu before reading again. */
|
|
|
|
smp_mb();
|
|
|
|
} while (cpu != qatomic_read(&rr_current_cpu));
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static void rr_kick_thread(void *opaque)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
2020-10-15 17:32:17 +03:00
|
|
|
timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
|
|
|
|
rr_kick_next_cpu();
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static void rr_start_kick_timer(void)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
2020-10-15 17:32:17 +03:00
|
|
|
if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
|
|
|
|
rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
|
|
|
|
rr_kick_thread, NULL);
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
2020-10-15 17:32:17 +03:00
|
|
|
if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) {
|
|
|
|
timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static void rr_stop_kick_timer(void)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
2020-10-15 17:32:17 +03:00
|
|
|
if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) {
|
|
|
|
timer_del(rr_kick_vcpu_timer);
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static void rr_wait_io_event(void)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
|
|
|
CPUState *cpu;
|
|
|
|
|
|
|
|
while (all_cpu_threads_idle()) {
|
2020-10-15 17:32:17 +03:00
|
|
|
rr_stop_kick_timer();
|
2020-10-15 17:32:15 +03:00
|
|
|
qemu_cond_wait_iothread(first_cpu->halt_cond);
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
rr_start_kick_timer();
|
2020-10-15 17:32:15 +03:00
|
|
|
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
qemu_wait_io_event_common(cpu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy any remaining vCPUs which have been unplugged and have
|
|
|
|
* finished running
|
|
|
|
*/
|
2020-10-15 17:32:17 +03:00
|
|
|
static void rr_deal_with_unplugged_cpus(void)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
|
|
|
CPUState *cpu;
|
|
|
|
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
if (cpu->unplug && !cpu_can_run(cpu)) {
|
2020-10-15 17:32:17 +03:00
|
|
|
tcg_cpus_destroy(cpu);
|
2020-10-15 17:32:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 21:35:23 +03:00
|
|
|
static void rr_force_rcu(Notifier *notify, void *data)
|
|
|
|
{
|
|
|
|
rr_kick_next_cpu();
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:09:25 +03:00
|
|
|
/*
|
|
|
|
* Calculate the number of CPUs that we will process in a single iteration of
|
|
|
|
* the main CPU thread loop so that we can fairly distribute the instruction
|
|
|
|
* count across CPUs.
|
|
|
|
*
|
|
|
|
* The CPU count is cached based on the CPU list generation ID to avoid
|
|
|
|
* iterating the list every time.
|
|
|
|
*/
|
|
|
|
static int rr_cpu_count(void)
|
|
|
|
{
|
|
|
|
static unsigned int last_gen_id = ~0;
|
|
|
|
static int cpu_count;
|
|
|
|
CPUState *cpu;
|
|
|
|
|
|
|
|
QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
|
|
|
|
|
|
|
|
if (cpu_list_generation_id_get() != last_gen_id) {
|
|
|
|
cpu_count = 0;
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
++cpu_count;
|
|
|
|
}
|
|
|
|
last_gen_id = cpu_list_generation_id_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cpu_count;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:15 +03:00
|
|
|
/*
|
|
|
|
* In the single-threaded case each vCPU is simulated in turn. If
|
|
|
|
* there is more than a single vCPU we create a simple timer to kick
|
|
|
|
* the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
|
|
|
|
* This is done explicitly rather than relying on side-effects
|
|
|
|
* elsewhere.
|
|
|
|
*/
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static void *rr_cpu_thread_fn(void *arg)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
2021-11-09 21:35:23 +03:00
|
|
|
Notifier force_rcu;
|
2020-10-15 17:32:15 +03:00
|
|
|
CPUState *cpu = arg;
|
|
|
|
|
2022-10-21 19:34:09 +03:00
|
|
|
assert(tcg_enabled());
|
2020-10-15 17:32:15 +03:00
|
|
|
rcu_register_thread();
|
2021-11-09 21:35:23 +03:00
|
|
|
force_rcu.notify = rr_force_rcu;
|
|
|
|
rcu_add_force_rcu_notifier(&force_rcu);
|
2020-10-15 17:32:15 +03:00
|
|
|
tcg_register_thread();
|
|
|
|
|
|
|
|
qemu_mutex_lock_iothread();
|
|
|
|
qemu_thread_get_self(cpu->thread);
|
|
|
|
|
|
|
|
cpu->thread_id = qemu_get_thread_id();
|
2023-09-16 01:41:39 +03:00
|
|
|
cpu->neg.can_do_io = true;
|
2020-10-15 17:32:15 +03:00
|
|
|
cpu_thread_signal_created(cpu);
|
|
|
|
qemu_guest_random_seed_thread_part2(cpu->random_seed);
|
|
|
|
|
|
|
|
/* wait for initial kick-off after machine start */
|
|
|
|
while (first_cpu->stopped) {
|
|
|
|
qemu_cond_wait_iothread(first_cpu->halt_cond);
|
|
|
|
|
|
|
|
/* process any pending work */
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
current_cpu = cpu;
|
|
|
|
qemu_wait_io_event_common(cpu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
rr_start_kick_timer();
|
2020-10-15 17:32:15 +03:00
|
|
|
|
|
|
|
cpu = first_cpu;
|
|
|
|
|
|
|
|
/* process any pending work */
|
|
|
|
cpu->exit_request = 1;
|
|
|
|
|
|
|
|
while (1) {
|
2023-04-27 05:09:25 +03:00
|
|
|
/* Only used for icount_enabled() */
|
|
|
|
int64_t cpu_budget = 0;
|
|
|
|
|
2020-10-15 17:32:15 +03:00
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
replay_mutex_lock();
|
|
|
|
qemu_mutex_lock_iothread();
|
|
|
|
|
|
|
|
if (icount_enabled()) {
|
2023-04-27 05:09:25 +03:00
|
|
|
int cpu_count = rr_cpu_count();
|
|
|
|
|
2020-10-15 17:32:15 +03:00
|
|
|
/* Account partial waits to QEMU_CLOCK_VIRTUAL. */
|
|
|
|
icount_account_warp_timer();
|
|
|
|
/*
|
|
|
|
* Run the timers here. This is much more efficient than
|
|
|
|
* waking up the I/O thread and waiting for completion.
|
|
|
|
*/
|
2020-10-15 17:32:17 +03:00
|
|
|
icount_handle_deadline();
|
2023-04-27 05:09:25 +03:00
|
|
|
|
|
|
|
cpu_budget = icount_percpu_budget(cpu_count);
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
replay_mutex_unlock();
|
|
|
|
|
|
|
|
if (!cpu) {
|
|
|
|
cpu = first_cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
|
2023-03-03 15:18:23 +03:00
|
|
|
/* Store rr_current_cpu before evaluating cpu_can_run(). */
|
2023-03-03 16:37:51 +03:00
|
|
|
qatomic_set_mb(&rr_current_cpu, cpu);
|
2023-03-03 15:18:23 +03:00
|
|
|
|
2020-10-15 17:32:15 +03:00
|
|
|
current_cpu = cpu;
|
|
|
|
|
|
|
|
qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
|
|
|
|
(cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
|
|
|
|
|
|
|
|
if (cpu_can_run(cpu)) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
if (icount_enabled()) {
|
2023-04-27 05:09:25 +03:00
|
|
|
icount_prepare_for_run(cpu, cpu_budget);
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
2020-10-15 17:32:17 +03:00
|
|
|
r = tcg_cpus_exec(cpu);
|
2020-10-15 17:32:15 +03:00
|
|
|
if (icount_enabled()) {
|
2020-10-15 17:32:17 +03:00
|
|
|
icount_process_data(cpu);
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
qemu_mutex_lock_iothread();
|
|
|
|
|
|
|
|
if (r == EXCP_DEBUG) {
|
|
|
|
cpu_handle_guest_debug(cpu);
|
|
|
|
break;
|
|
|
|
} else if (r == EXCP_ATOMIC) {
|
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
cpu_exec_step_atomic(cpu);
|
|
|
|
qemu_mutex_lock_iothread();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (cpu->stop) {
|
|
|
|
if (cpu->unplug) {
|
|
|
|
cpu = CPU_NEXT(cpu);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu = CPU_NEXT(cpu);
|
|
|
|
} /* while (cpu && !cpu->exit_request).. */
|
|
|
|
|
2023-03-03 15:18:23 +03:00
|
|
|
/* Does not need a memory barrier because a spurious wakeup is okay. */
|
2020-10-15 17:32:17 +03:00
|
|
|
qatomic_set(&rr_current_cpu, NULL);
|
2020-10-15 17:32:15 +03:00
|
|
|
|
|
|
|
if (cpu && cpu->exit_request) {
|
2023-03-03 16:37:51 +03:00
|
|
|
qatomic_set_mb(&cpu->exit_request, 0);
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (icount_enabled() && all_cpu_threads_idle()) {
|
|
|
|
/*
|
|
|
|
* When all cpus are sleeping (e.g in WFI), to avoid a deadlock
|
|
|
|
* in the main_loop, wake it up in order to start the warp timer.
|
|
|
|
*/
|
|
|
|
qemu_notify_event();
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
rr_wait_io_event();
|
|
|
|
rr_deal_with_unplugged_cpus();
|
2020-10-15 17:32:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 21:35:23 +03:00
|
|
|
rcu_remove_force_rcu_notifier(&force_rcu);
|
2020-10-15 17:32:15 +03:00
|
|
|
rcu_unregister_thread();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:16 +03:00
|
|
|
void rr_start_vcpu_thread(CPUState *cpu)
|
|
|
|
{
|
|
|
|
char thread_name[VCPU_THREAD_NAME_SIZE];
|
|
|
|
static QemuCond *single_tcg_halt_cond;
|
|
|
|
static QemuThread *single_tcg_cpu_thread;
|
|
|
|
|
2022-10-21 19:34:09 +03:00
|
|
|
g_assert(tcg_enabled());
|
|
|
|
tcg_cpu_init_cflags(cpu, false);
|
|
|
|
|
2020-10-15 17:32:16 +03:00
|
|
|
if (!single_tcg_cpu_thread) {
|
2022-03-15 17:41:56 +03:00
|
|
|
cpu->thread = g_new0(QemuThread, 1);
|
|
|
|
cpu->halt_cond = g_new0(QemuCond, 1);
|
2020-10-15 17:32:16 +03:00
|
|
|
qemu_cond_init(cpu->halt_cond);
|
|
|
|
|
|
|
|
/* share a single thread for all cpus with TCG */
|
|
|
|
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
|
|
|
|
qemu_thread_create(cpu->thread, thread_name,
|
2020-10-15 17:32:17 +03:00
|
|
|
rr_cpu_thread_fn,
|
2020-10-15 17:32:16 +03:00
|
|
|
cpu, QEMU_THREAD_JOINABLE);
|
|
|
|
|
|
|
|
single_tcg_halt_cond = cpu->halt_cond;
|
|
|
|
single_tcg_cpu_thread = cpu->thread;
|
|
|
|
} else {
|
|
|
|
/* we share the thread */
|
|
|
|
cpu->thread = single_tcg_cpu_thread;
|
|
|
|
cpu->halt_cond = single_tcg_halt_cond;
|
|
|
|
cpu->thread_id = first_cpu->thread_id;
|
2023-09-16 01:41:39 +03:00
|
|
|
cpu->neg.can_do_io = 1;
|
2020-10-15 17:32:16 +03:00
|
|
|
cpu->created = true;
|
|
|
|
}
|
|
|
|
}
|