yuzu-emu
/
yuzu-mainline
Archived
1
0
Fork 0
This repository has been archived on 2024-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
yuzu-mainline/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-26 22:52:16 +00: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-13 22:36:30 +00:00
CpuManager::CpuManager(System& system_) : system{system_} {}
CpuManager::~CpuManager() = default;
2021-08-07 05:32:48 +00:00
void CpuManager::ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager,
std::size_t core) {
2022-06-13 22:36:30 +00:00
cpu_manager.RunThread(core);
}
void CpuManager::Initialize() {
2022-06-13 22:36:30 +00:00
num_cores = is_multicore ? Core::Hardware::NUM_CPU_CORES : 1;
gpu_barrier = std::make_unique<Common::Barrier>(num_cores + 1);
2022-06-13 22:36:30 +00: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-13 22:36:30 +00: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-26 22:52:16 +00:00
void CpuManager::GuestActivateFunction() {
if (is_multicore) {
2022-06-26 22:52:16 +00:00
MultiCoreGuestActivate();
} else {
2022-06-26 22:52:16 +00:00
SingleCoreGuestActivate();
}
}
2022-06-26 22:52:16 +00:00
void CpuManager::GuestThreadFunction() {
if (is_multicore) {
2022-06-26 22:52:16 +00:00
MultiCoreRunGuestThread();
} else {
2022-06-26 22:52:16 +00:00
SingleCoreRunGuestThread();
}
}
2022-06-26 22:52:16 +00: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-26 22:52:16 +00:00
HandleInterrupt();
}
2022-06-26 22:52:16 +00: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-26 22:52:16 +00: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-26 22:52:16 +00:00
// Similar to UserModeThreadStarter in HOS
auto& kernel = system.Kernel();
2022-06-26 22:52:16 +00: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 14:43:34 +00:00
}
2022-06-26 22:52:16 +00:00
HandleInterrupt();
}
}
///////////////////////////////////////////////////////////////////////////////
/// SingleCore ///
///////////////////////////////////////////////////////////////////////////////
2022-06-26 22:52:16 +00:00
void CpuManager::SingleCoreGuestActivate() {}
2022-06-26 22:52:16 +00:00
void CpuManager::SingleCoreRunGuestThread() {}
2022-06-26 22:52:16 +00:00
void CpuManager::SingleCoreRunGuestLoop() {}
2022-06-26 22:52:16 +00:00
void CpuManager::PreemptSingleCore(bool from_running_enviroment) {}
2022-06-13 22:36:30 +00:00
void CpuManager::ShutdownThread() {
auto& kernel = system.Kernel();
2022-06-26 22:52:16 +00:00
auto* thread = kernel.GetCurrentEmuThread();
2022-06-13 22:36:30 +00:00
auto core = is_multicore ? kernel.CurrentPhysicalCoreIndex() : 0;
2022-05-28 00:44:45 +00:00
2022-06-26 22:52:16 +00:00
Common::Fiber::YieldTo(thread->GetHostContext(), *core_data[core].host_context);
2022-06-13 22:36:30 +00:00
UNREACHABLE();
}
2022-06-13 22:36:30 +00: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-13 22:36:30 +00:00
// Running
gpu_barrier->Sync();
2022-06-13 22:36:30 +00:00
if (!is_async_gpu && !is_multicore) {
system.GPU().ObtainContext();
}
2022-06-13 22:36:30 +00:00
2022-06-26 22:52:16 +00: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