2022-04-23 20:49:07 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
#include "common/fiber.h"
|
|
|
|
#include "common/microprofile.h"
|
|
|
|
#include "common/scope_exit.h"
|
|
|
|
#include "common/thread.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/core_timing.h"
|
|
|
|
#include "core/cpu_manager.h"
|
|
|
|
#include "core/hle/kernel/k_scheduler.h"
|
2021-01-22 01:15:25 +01:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2020-12-28 16:15:37 +01:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/physical_core.h"
|
|
|
|
#include "video_core/gpu.h"
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
2022-06-01 20:56:21 +02:00
|
|
|
CpuManager::CpuManager(System& system_)
|
|
|
|
: pause_barrier{std::make_unique<Common::Barrier>(1)}, system{system_} {}
|
2020-12-28 16:15:37 +01:00
|
|
|
CpuManager::~CpuManager() = default;
|
|
|
|
|
2021-09-04 02:08:38 +02:00
|
|
|
void CpuManager::ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager,
|
|
|
|
std::size_t core) {
|
|
|
|
cpu_manager.RunThread(stop_token, core);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::Initialize() {
|
|
|
|
running_mode = true;
|
|
|
|
if (is_multicore) {
|
|
|
|
for (std::size_t core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
2021-09-04 02:08:38 +02:00
|
|
|
core_data[core].host_thread = std::jthread(ThreadStart, std::ref(*this), core);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
2022-06-01 20:56:21 +02:00
|
|
|
pause_barrier = std::make_unique<Common::Barrier>(Core::Hardware::NUM_CPU_CORES + 1);
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
2021-09-04 02:08:38 +02:00
|
|
|
core_data[0].host_thread = std::jthread(ThreadStart, std::ref(*this), 0);
|
2022-06-01 20:56:21 +02:00
|
|
|
pause_barrier = std::make_unique<Common::Barrier>(2);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::Shutdown() {
|
|
|
|
running_mode = false;
|
|
|
|
Pause(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(void*)> CpuManager::GetGuestThreadStartFunc() {
|
|
|
|
return GuestThreadFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(void*)> CpuManager::GetIdleThreadStartFunc() {
|
|
|
|
return IdleThreadFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(void*)> CpuManager::GetSuspendThreadStartFunc() {
|
|
|
|
return SuspendThreadFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::GuestThreadFunction(void* cpu_manager_) {
|
|
|
|
CpuManager* cpu_manager = static_cast<CpuManager*>(cpu_manager_);
|
|
|
|
if (cpu_manager->is_multicore) {
|
|
|
|
cpu_manager->MultiCoreRunGuestThread();
|
|
|
|
} else {
|
|
|
|
cpu_manager->SingleCoreRunGuestThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::GuestRewindFunction(void* cpu_manager_) {
|
|
|
|
CpuManager* cpu_manager = static_cast<CpuManager*>(cpu_manager_);
|
|
|
|
if (cpu_manager->is_multicore) {
|
|
|
|
cpu_manager->MultiCoreRunGuestLoop();
|
|
|
|
} else {
|
|
|
|
cpu_manager->SingleCoreRunGuestLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::IdleThreadFunction(void* cpu_manager_) {
|
|
|
|
CpuManager* cpu_manager = static_cast<CpuManager*>(cpu_manager_);
|
|
|
|
if (cpu_manager->is_multicore) {
|
|
|
|
cpu_manager->MultiCoreRunIdleThread();
|
|
|
|
} else {
|
|
|
|
cpu_manager->SingleCoreRunIdleThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::SuspendThreadFunction(void* cpu_manager_) {
|
|
|
|
CpuManager* cpu_manager = static_cast<CpuManager*>(cpu_manager_);
|
|
|
|
if (cpu_manager->is_multicore) {
|
|
|
|
cpu_manager->MultiCoreRunSuspendThread();
|
|
|
|
} else {
|
|
|
|
cpu_manager->SingleCoreRunSuspendThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* CpuManager::GetStartFuncParamater() {
|
|
|
|
return static_cast<void*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// MultiCore ///
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void CpuManager::MultiCoreRunGuestThread() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
kernel.CurrentScheduler()->OnThreadStart();
|
|
|
|
auto* thread = kernel.CurrentScheduler()->GetCurrentThread();
|
2021-03-06 02:58:44 +01:00
|
|
|
auto& host_context = thread->GetHostContext();
|
2020-12-28 16:15:37 +01:00
|
|
|
host_context->SetRewindPoint(GuestRewindFunction, this);
|
|
|
|
MultiCoreRunGuestLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::MultiCoreRunGuestLoop() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
auto* physical_core = &kernel.CurrentPhysicalCore();
|
|
|
|
system.EnterDynarmicProfile();
|
|
|
|
while (!physical_core->IsInterrupted()) {
|
|
|
|
physical_core->Run();
|
|
|
|
physical_core = &kernel.CurrentPhysicalCore();
|
|
|
|
}
|
|
|
|
system.ExitDynarmicProfile();
|
2021-12-07 12:04:29 +01:00
|
|
|
{
|
|
|
|
Kernel::KScopedDisableDispatch dd(kernel);
|
|
|
|
physical_core->ArmInterface().ClearExclusiveState();
|
|
|
|
}
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::MultiCoreRunIdleThread() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
while (true) {
|
2021-12-07 12:04:29 +01:00
|
|
|
Kernel::KScopedDisableDispatch dd(kernel);
|
|
|
|
kernel.CurrentPhysicalCore().Idle();
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::MultiCoreRunSuspendThread() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
kernel.CurrentScheduler()->OnThreadStart();
|
|
|
|
while (true) {
|
2021-12-07 12:04:29 +01:00
|
|
|
auto core = kernel.CurrentPhysicalCoreIndex();
|
2020-12-28 16:15:37 +01:00
|
|
|
auto& scheduler = *kernel.CurrentScheduler();
|
2021-01-22 01:15:25 +01:00
|
|
|
Kernel::KThread* current_thread = scheduler.GetCurrentThread();
|
2021-03-08 07:51:31 +01:00
|
|
|
Common::Fiber::YieldTo(current_thread->GetHostContext(), *core_data[core].host_context);
|
2020-12-28 16:15:37 +01:00
|
|
|
ASSERT(scheduler.ContextSwitchPending());
|
2021-12-07 12:04:29 +01:00
|
|
|
ASSERT(core == kernel.CurrentPhysicalCoreIndex());
|
2020-12-28 16:15:37 +01:00
|
|
|
scheduler.RescheduleCurrentCore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// SingleCore ///
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void CpuManager::SingleCoreRunGuestThread() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
kernel.CurrentScheduler()->OnThreadStart();
|
|
|
|
auto* thread = kernel.CurrentScheduler()->GetCurrentThread();
|
2021-03-06 02:58:44 +01:00
|
|
|
auto& host_context = thread->GetHostContext();
|
2020-12-28 16:15:37 +01:00
|
|
|
host_context->SetRewindPoint(GuestRewindFunction, this);
|
|
|
|
SingleCoreRunGuestLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::SingleCoreRunGuestLoop() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
while (true) {
|
|
|
|
auto* physical_core = &kernel.CurrentPhysicalCore();
|
|
|
|
system.EnterDynarmicProfile();
|
|
|
|
if (!physical_core->IsInterrupted()) {
|
|
|
|
physical_core->Run();
|
|
|
|
physical_core = &kernel.CurrentPhysicalCore();
|
|
|
|
}
|
|
|
|
system.ExitDynarmicProfile();
|
2021-01-22 01:15:25 +01:00
|
|
|
kernel.SetIsPhantomModeForSingleCore(true);
|
2020-12-28 16:15:37 +01:00
|
|
|
system.CoreTiming().Advance();
|
2021-01-22 01:15:25 +01:00
|
|
|
kernel.SetIsPhantomModeForSingleCore(false);
|
2020-12-28 16:15:37 +01:00
|
|
|
physical_core->ArmInterface().ClearExclusiveState();
|
|
|
|
PreemptSingleCore();
|
|
|
|
auto& scheduler = kernel.Scheduler(current_core);
|
|
|
|
scheduler.RescheduleCurrentCore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::SingleCoreRunIdleThread() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
while (true) {
|
|
|
|
auto& physical_core = kernel.CurrentPhysicalCore();
|
|
|
|
PreemptSingleCore(false);
|
|
|
|
system.CoreTiming().AddTicks(1000U);
|
|
|
|
idle_count++;
|
|
|
|
auto& scheduler = physical_core.Scheduler();
|
|
|
|
scheduler.RescheduleCurrentCore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::SingleCoreRunSuspendThread() {
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
kernel.CurrentScheduler()->OnThreadStart();
|
|
|
|
while (true) {
|
|
|
|
auto core = kernel.GetCurrentHostThreadID();
|
|
|
|
auto& scheduler = *kernel.CurrentScheduler();
|
2021-01-22 01:15:25 +01:00
|
|
|
Kernel::KThread* current_thread = scheduler.GetCurrentThread();
|
2021-03-08 07:51:31 +01:00
|
|
|
Common::Fiber::YieldTo(current_thread->GetHostContext(), *core_data[0].host_context);
|
2020-12-28 16:15:37 +01:00
|
|
|
ASSERT(scheduler.ContextSwitchPending());
|
|
|
|
ASSERT(core == kernel.GetCurrentHostThreadID());
|
|
|
|
scheduler.RescheduleCurrentCore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::PreemptSingleCore(bool from_running_enviroment) {
|
|
|
|
{
|
2021-01-22 01:15:25 +01:00
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
auto& scheduler = kernel.Scheduler(current_core);
|
|
|
|
Kernel::KThread* current_thread = scheduler.GetCurrentThread();
|
2020-12-28 16:15:37 +01:00
|
|
|
if (idle_count >= 4 || from_running_enviroment) {
|
|
|
|
if (!from_running_enviroment) {
|
|
|
|
system.CoreTiming().Idle();
|
|
|
|
idle_count = 0;
|
|
|
|
}
|
2021-01-22 01:15:25 +01:00
|
|
|
kernel.SetIsPhantomModeForSingleCore(true);
|
2020-12-28 16:15:37 +01:00
|
|
|
system.CoreTiming().Advance();
|
2021-01-22 01:15:25 +01:00
|
|
|
kernel.SetIsPhantomModeForSingleCore(false);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES);
|
|
|
|
system.CoreTiming().ResetTicks();
|
|
|
|
scheduler.Unload(scheduler.GetCurrentThread());
|
|
|
|
|
2021-01-22 01:15:25 +01:00
|
|
|
auto& next_scheduler = kernel.Scheduler(current_core);
|
2021-03-08 07:51:31 +01:00
|
|
|
Common::Fiber::YieldTo(current_thread->GetHostContext(), *next_scheduler.ControlContext());
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// May have changed scheduler
|
|
|
|
{
|
|
|
|
auto& scheduler = system.Kernel().Scheduler(current_core);
|
|
|
|
scheduler.Reload(scheduler.GetCurrentThread());
|
2021-01-22 01:15:25 +01:00
|
|
|
if (!scheduler.IsIdle()) {
|
2020-12-28 16:15:37 +01:00
|
|
|
idle_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuManager::Pause(bool paused) {
|
2022-06-01 20:56:21 +02:00
|
|
|
if (pause_state == paused) {
|
|
|
|
return;
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
2022-06-01 20:56:21 +02:00
|
|
|
|
|
|
|
pause_state.store(paused);
|
|
|
|
pause_state.notify_all();
|
|
|
|
|
|
|
|
// Wait for all threads to successfully pause/unpause before returning
|
|
|
|
pause_barrier->Sync();
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
2021-09-04 02:08:38 +02:00
|
|
|
void CpuManager::RunThread(std::stop_token stop_token, std::size_t core) {
|
2020-12-28 16:15:37 +01:00
|
|
|
/// Initialization
|
|
|
|
system.RegisterCoreThread(core);
|
|
|
|
std::string name;
|
|
|
|
if (is_multicore) {
|
|
|
|
name = "yuzu:CPUCore_" + std::to_string(core);
|
|
|
|
} else {
|
|
|
|
name = "yuzu:CPUThread";
|
|
|
|
}
|
|
|
|
MicroProfileOnThreadCreate(name.c_str());
|
|
|
|
Common::SetCurrentThreadName(name.c_str());
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
|
|
|
auto& data = core_data[core];
|
|
|
|
data.host_context = Common::Fiber::ThreadToFiber();
|
|
|
|
const bool sc_sync = !is_async_gpu && !is_multicore;
|
|
|
|
bool sc_sync_first_use = sc_sync;
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
SCOPE_EXIT({
|
|
|
|
data.host_context->Exit();
|
|
|
|
MicroProfileOnThreadExit();
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Running
|
|
|
|
while (running_mode) {
|
2022-06-01 20:56:21 +02:00
|
|
|
if (pause_state.load(std::memory_order_relaxed)) {
|
|
|
|
// Wait for caller to acknowledge pausing
|
|
|
|
pause_barrier->Sync();
|
|
|
|
|
|
|
|
// Wait until unpaused
|
|
|
|
pause_state.wait(true, std::memory_order_relaxed);
|
|
|
|
|
|
|
|
// Wait for caller to acknowledge unpausing
|
|
|
|
pause_barrier->Sync();
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
if (sc_sync_first_use) {
|
|
|
|
system.GPU().ObtainContext();
|
|
|
|
sc_sync_first_use = false;
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:04:29 +01:00
|
|
|
// Emulation was stopped
|
2021-11-30 21:23:11 +01:00
|
|
|
if (stop_token.stop_requested()) {
|
2021-12-07 12:04:29 +01:00
|
|
|
return;
|
2021-11-30 21:23:11 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
auto current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread();
|
2021-03-08 07:51:31 +01:00
|
|
|
Common::Fiber::YieldTo(data.host_context, *current_thread->GetHostContext());
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core
|