yuzu/src/core/cpu_manager.cpp

188 lines
5.4 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#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"
2022-06-27 00:52:16 +02:00
#include "core/hle/kernel/k_interrupt_manager.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/physical_core.h"
#include "video_core/gpu.h"
namespace Core {
2022-06-14 00:36:30 +02:00
CpuManager::CpuManager(System& system_) : system{system_} {}
CpuManager::~CpuManager() = default;
2021-08-07 07:32:48 +02:00
void CpuManager::ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager,
std::size_t core) {
2022-06-14 00:36:30 +02:00
cpu_manager.RunThread(core);
}
void CpuManager::Initialize() {
2022-06-14 00:36:30 +02:00
num_cores = is_multicore ? Core::Hardware::NUM_CPU_CORES : 1;
gpu_barrier = std::make_unique<Common::Barrier>(num_cores + 1);
2022-06-14 00:36:30 +02:00
for (std::size_t core = 0; core < num_cores; core++) {
core_data[core].host_thread = std::jthread(ThreadStart, std::ref(*this), core);
}
}
void CpuManager::Shutdown() {
2022-06-14 00:36:30 +02:00
for (std::size_t core = 0; core < num_cores; core++) {
if (core_data[core].host_thread.joinable()) {
core_data[core].host_thread.join();
}
}
}
2022-06-27 00:52:16 +02:00
void CpuManager::GuestActivateFunction() {
if (is_multicore) {
2022-06-27 00:52:16 +02:00
MultiCoreGuestActivate();
} else {
2022-06-27 00:52:16 +02:00
SingleCoreGuestActivate();
}
}
2022-06-27 00:52:16 +02:00
void CpuManager::GuestThreadFunction() {
if (is_multicore) {
2022-06-27 00:52:16 +02:00
MultiCoreRunGuestThread();
} else {
2022-06-27 00:52:16 +02:00
SingleCoreRunGuestThread();
}
}
2022-06-27 00:52:16 +02:00
void CpuManager::ShutdownThreadFunction() {
ShutdownThread();
}
void CpuManager::WaitForAndHandleInterrupt() {
auto& kernel = system.Kernel();
auto& physical_core = kernel.CurrentPhysicalCore();
ASSERT(Kernel::GetCurrentThread(kernel).GetDisableDispatchCount() == 1);
if (!physical_core.IsInterrupted()) {
physical_core.Idle();
}
2022-06-27 00:52:16 +02:00
HandleInterrupt();
}
2022-06-27 00:52:16 +02:00
void CpuManager::HandleInterrupt() {
auto& kernel = system.Kernel();
auto core_index = kernel.CurrentPhysicalCoreIndex();
Kernel::KInterruptManager::HandleInterrupt(kernel, static_cast<s32>(core_index));
}
///////////////////////////////////////////////////////////////////////////////
/// MultiCore ///
///////////////////////////////////////////////////////////////////////////////
2022-06-27 00:52:16 +02:00
void CpuManager::MultiCoreGuestActivate() {
// Similar to the HorizonKernelMain callback in HOS
auto& kernel = system.Kernel();
auto* scheduler = kernel.CurrentScheduler();
scheduler->Activate();
UNREACHABLE();
}
void CpuManager::MultiCoreRunGuestThread() {
2022-06-27 00:52:16 +02:00
// Similar to UserModeThreadStarter in HOS
auto& kernel = system.Kernel();
2022-06-27 00:52:16 +02:00
auto* thread = kernel.GetCurrentEmuThread();
thread->EnableDispatch();
MultiCoreRunGuestLoop();
}
void CpuManager::MultiCoreRunGuestLoop() {
auto& kernel = system.Kernel();
while (true) {
auto* physical_core = &kernel.CurrentPhysicalCore();
while (!physical_core->IsInterrupted()) {
physical_core->Run();
physical_core = &kernel.CurrentPhysicalCore();
2020-02-25 15:43:34 +01:00
}
2022-06-27 00:52:16 +02:00
HandleInterrupt();
}
}
///////////////////////////////////////////////////////////////////////////////
/// SingleCore ///
///////////////////////////////////////////////////////////////////////////////
2022-06-27 00:52:16 +02:00
void CpuManager::SingleCoreGuestActivate() {}
2022-06-27 00:52:16 +02:00
void CpuManager::SingleCoreRunGuestThread() {}
2022-06-27 00:52:16 +02:00
void CpuManager::SingleCoreRunGuestLoop() {}
2022-06-27 00:52:16 +02:00
void CpuManager::PreemptSingleCore(bool from_running_enviroment) {}
2022-06-14 00:36:30 +02:00
void CpuManager::ShutdownThread() {
auto& kernel = system.Kernel();
2022-06-27 00:52:16 +02:00
auto* thread = kernel.GetCurrentEmuThread();
2022-06-14 00:36:30 +02:00
auto core = is_multicore ? kernel.CurrentPhysicalCoreIndex() : 0;
2022-05-28 02:44:45 +02:00
2022-06-27 00:52:16 +02:00
Common::Fiber::YieldTo(thread->GetHostContext(), *core_data[core].host_context);
2022-06-14 00:36:30 +02:00
UNREACHABLE();
}
2022-06-14 00:36:30 +02:00
void CpuManager::RunThread(std::size_t core) {
/// 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();
// Cleanup
SCOPE_EXIT({
data.host_context->Exit();
MicroProfileOnThreadExit();
});
2022-06-14 00:36:30 +02:00
// Running
gpu_barrier->Sync();
2022-06-14 00:36:30 +02:00
if (!is_async_gpu && !is_multicore) {
system.GPU().ObtainContext();
}
2022-06-14 00:36:30 +02:00
2022-06-27 00:52:16 +02:00
auto& kernel = system.Kernel();
auto* main_thread = Kernel::KThread::Create(kernel);
main_thread->SetName(fmt::format("MainThread:{}", core));
ASSERT(Kernel::KThread::InitializeMainThread(system, main_thread, static_cast<s32>(core))
.IsSuccess());
auto* idle_thread = Kernel::KThread::Create(kernel);
ASSERT(Kernel::KThread::InitializeIdleThread(system, idle_thread, static_cast<s32>(core))
.IsSuccess());
kernel.SetCurrentEmuThread(main_thread);
kernel.CurrentScheduler()->Initialize(idle_thread);
Common::Fiber::YieldTo(data.host_context, *main_thread->GetHostContext());
}
} // namespace Core