2020-10-15 17:32:15 +03:00
|
|
|
/*
|
|
|
|
* QEMU TCG Multi 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"
|
|
|
|
#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"
|
|
|
|
#include "hw/boards.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-mttcg.h"
|
2020-10-15 17:32:15 +03:00
|
|
|
|
2021-11-09 21:35:23 +03:00
|
|
|
typedef struct MttcgForceRcuNotifier {
|
|
|
|
Notifier notifier;
|
|
|
|
CPUState *cpu;
|
|
|
|
} MttcgForceRcuNotifier;
|
|
|
|
|
|
|
|
static void do_nothing(CPUState *cpu, run_on_cpu_data d)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mttcg_force_rcu(Notifier *notify, void *data)
|
|
|
|
{
|
|
|
|
CPUState *cpu = container_of(notify, MttcgForceRcuNotifier, notifier)->cpu;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called with rcu_registry_lock held, using async_run_on_cpu() ensures
|
|
|
|
* that there are no deadlocks.
|
|
|
|
*/
|
|
|
|
async_run_on_cpu(cpu, do_nothing, RUN_ON_CPU_NULL);
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:32:15 +03:00
|
|
|
/*
|
|
|
|
* In the multi-threaded case each vCPU has its own thread. The TLS
|
|
|
|
* variable current_cpu can be used deep in the code to find the
|
|
|
|
* current CPUState for a given thread.
|
|
|
|
*/
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
static void *mttcg_cpu_thread_fn(void *arg)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
2021-11-09 21:35:23 +03:00
|
|
|
MttcgForceRcuNotifier force_rcu;
|
2020-10-15 17:32:15 +03:00
|
|
|
CPUState *cpu = arg;
|
|
|
|
|
|
|
|
assert(tcg_enabled());
|
|
|
|
g_assert(!icount_enabled());
|
|
|
|
|
|
|
|
rcu_register_thread();
|
2021-11-09 21:35:23 +03:00
|
|
|
force_rcu.notifier.notify = mttcg_force_rcu;
|
|
|
|
force_rcu.cpu = cpu;
|
|
|
|
rcu_add_force_rcu_notifier(&force_rcu.notifier);
|
2020-10-15 17:32:15 +03:00
|
|
|
tcg_register_thread();
|
|
|
|
|
2024-01-02 18:35:25 +03:00
|
|
|
bql_lock();
|
2020-10-15 17:32:15 +03:00
|
|
|
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
|
|
|
current_cpu = cpu;
|
|
|
|
cpu_thread_signal_created(cpu);
|
|
|
|
qemu_guest_random_seed_thread_part2(cpu->random_seed);
|
|
|
|
|
|
|
|
/* process any pending work */
|
|
|
|
cpu->exit_request = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (cpu_can_run(cpu)) {
|
|
|
|
int r;
|
2024-01-02 18:35:25 +03:00
|
|
|
bql_unlock();
|
2024-01-24 13:16:33 +03:00
|
|
|
r = tcg_cpu_exec(cpu);
|
2024-01-02 18:35:25 +03:00
|
|
|
bql_lock();
|
2020-10-15 17:32:15 +03:00
|
|
|
switch (r) {
|
|
|
|
case EXCP_DEBUG:
|
|
|
|
cpu_handle_guest_debug(cpu);
|
|
|
|
break;
|
|
|
|
case EXCP_HALTED:
|
|
|
|
/*
|
2023-08-29 04:06:58 +03:00
|
|
|
* Usually cpu->halted is set, but may have already been
|
|
|
|
* reset by another thread by the time we arrive here.
|
2020-10-15 17:32:15 +03:00
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case EXCP_ATOMIC:
|
2024-01-02 18:35:25 +03:00
|
|
|
bql_unlock();
|
2020-10-15 17:32:15 +03:00
|
|
|
cpu_exec_step_atomic(cpu);
|
2024-01-02 18:35:25 +03:00
|
|
|
bql_lock();
|
2020-10-15 17:32:15 +03:00
|
|
|
default:
|
|
|
|
/* Ignore everything else? */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 16:37:51 +03:00
|
|
|
qatomic_set_mb(&cpu->exit_request, 0);
|
2020-10-15 17:32:15 +03:00
|
|
|
qemu_wait_io_event(cpu);
|
|
|
|
} while (!cpu->unplug || cpu_can_run(cpu));
|
|
|
|
|
2024-01-24 13:16:32 +03:00
|
|
|
tcg_cpu_destroy(cpu);
|
2024-01-02 18:35:25 +03:00
|
|
|
bql_unlock();
|
2021-11-09 21:35:23 +03:00
|
|
|
rcu_remove_force_rcu_notifier(&force_rcu.notifier);
|
2020-10-15 17:32:15 +03:00
|
|
|
rcu_unregister_thread();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:39:25 +03:00
|
|
|
void mttcg_kick_vcpu_thread(CPUState *cpu)
|
2020-10-15 17:32:15 +03:00
|
|
|
{
|
|
|
|
cpu_exit(cpu);
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:39:25 +03:00
|
|
|
void mttcg_start_vcpu_thread(CPUState *cpu)
|
2020-10-15 17:32:16 +03:00
|
|
|
{
|
|
|
|
char thread_name[VCPU_THREAD_NAME_SIZE];
|
|
|
|
|
2022-10-21 19:34:09 +03:00
|
|
|
g_assert(tcg_enabled());
|
|
|
|
tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1);
|
|
|
|
|
2022-03-15 17:41:56 +03:00
|
|
|
cpu->thread = g_new0(QemuThread, 1);
|
2020-10-15 17:32:16 +03:00
|
|
|
cpu->halt_cond = g_malloc0(sizeof(QemuCond));
|
|
|
|
qemu_cond_init(cpu->halt_cond);
|
|
|
|
|
|
|
|
/* create a thread per vCPU with TCG (MTTCG) */
|
|
|
|
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
|
|
|
|
cpu->cpu_index);
|
|
|
|
|
2020-10-15 17:32:17 +03:00
|
|
|
qemu_thread_create(cpu->thread, thread_name, mttcg_cpu_thread_fn,
|
2020-10-15 17:32:16 +03:00
|
|
|
cpu, QEMU_THREAD_JOINABLE);
|
|
|
|
}
|