summaryrefslogtreecommitdiffstats
path: root/src/core/cpu_manager.cpp
diff options
context:
space:
mode:
authorLiam <byteslice@airmail.cc>2022-07-06 05:27:25 +0200
committerLiam <byteslice@airmail.cc>2022-07-15 04:47:18 +0200
commit21945ae127480c8332c1110ceada2df4a42a5848 (patch)
treea385c64a14b0d8e8dd71410eaa47575462f8f368 /src/core/cpu_manager.cpp
parentkernel: use KScheduler from mesosphere (diff)
downloadyuzu-21945ae127480c8332c1110ceada2df4a42a5848.tar
yuzu-21945ae127480c8332c1110ceada2df4a42a5848.tar.gz
yuzu-21945ae127480c8332c1110ceada2df4a42a5848.tar.bz2
yuzu-21945ae127480c8332c1110ceada2df4a42a5848.tar.lz
yuzu-21945ae127480c8332c1110ceada2df4a42a5848.tar.xz
yuzu-21945ae127480c8332c1110ceada2df4a42a5848.tar.zst
yuzu-21945ae127480c8332c1110ceada2df4a42a5848.zip
Diffstat (limited to '')
-rw-r--r--src/core/cpu_manager.cpp152
1 files changed, 101 insertions, 51 deletions
diff --git a/src/core/cpu_manager.cpp b/src/core/cpu_manager.cpp
index 428194129..838d6be21 100644
--- a/src/core/cpu_manager.cpp
+++ b/src/core/cpu_manager.cpp
@@ -42,19 +42,19 @@ void CpuManager::Shutdown() {
}
}
-void CpuManager::GuestActivateFunction() {
+void CpuManager::GuestThreadFunction() {
if (is_multicore) {
- MultiCoreGuestActivate();
+ MultiCoreRunGuestThread();
} else {
- SingleCoreGuestActivate();
+ SingleCoreRunGuestThread();
}
}
-void CpuManager::GuestThreadFunction() {
+void CpuManager::IdleThreadFunction() {
if (is_multicore) {
- MultiCoreRunGuestThread();
+ MultiCoreRunIdleThread();
} else {
- SingleCoreRunGuestThread();
+ SingleCoreRunIdleThread();
}
}
@@ -62,19 +62,6 @@ 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();
- }
-
- HandleInterrupt();
-}
-
void CpuManager::HandleInterrupt() {
auto& kernel = system.Kernel();
auto core_index = kernel.CurrentPhysicalCoreIndex();
@@ -86,49 +73,121 @@ void CpuManager::HandleInterrupt() {
/// MultiCore ///
///////////////////////////////////////////////////////////////////////////////
-void CpuManager::MultiCoreGuestActivate() {
- // Similar to the HorizonKernelMain callback in HOS
+void CpuManager::MultiCoreRunGuestThread() {
+ // Similar to UserModeThreadStarter in HOS
auto& kernel = system.Kernel();
- auto* scheduler = kernel.CurrentScheduler();
+ kernel.CurrentScheduler()->OnThreadStart();
- scheduler->Activate();
- UNREACHABLE();
+ while (true) {
+ auto* physical_core = &kernel.CurrentPhysicalCore();
+ while (!physical_core->IsInterrupted()) {
+ physical_core->Run();
+ physical_core = &kernel.CurrentPhysicalCore();
+ }
+
+ HandleInterrupt();
+ }
}
-void CpuManager::MultiCoreRunGuestThread() {
- // Similar to UserModeThreadStarter in HOS
+void CpuManager::MultiCoreRunIdleThread() {
+ // Not accurate to HOS. Remove this entire method when singlecore is removed.
+ // See notes in KScheduler::ScheduleImpl for more information about why this
+ // is inaccurate.
+
auto& kernel = system.Kernel();
- auto* thread = kernel.GetCurrentEmuThread();
- thread->EnableDispatch();
+ kernel.CurrentScheduler()->OnThreadStart();
+
+ while (true) {
+ auto& physical_core = kernel.CurrentPhysicalCore();
+ if (!physical_core.IsInterrupted()) {
+ physical_core.Idle();
+ }
- MultiCoreRunGuestLoop();
+ HandleInterrupt();
+ }
}
-void CpuManager::MultiCoreRunGuestLoop() {
+///////////////////////////////////////////////////////////////////////////////
+/// SingleCore ///
+///////////////////////////////////////////////////////////////////////////////
+
+void CpuManager::SingleCoreRunGuestThread() {
auto& kernel = system.Kernel();
+ kernel.CurrentScheduler()->OnThreadStart();
while (true) {
auto* physical_core = &kernel.CurrentPhysicalCore();
- while (!physical_core->IsInterrupted()) {
+ if (!physical_core->IsInterrupted()) {
physical_core->Run();
physical_core = &kernel.CurrentPhysicalCore();
}
+ kernel.SetIsPhantomModeForSingleCore(true);
+ system.CoreTiming().Advance();
+ kernel.SetIsPhantomModeForSingleCore(false);
+
+ PreemptSingleCore();
HandleInterrupt();
}
}
-///////////////////////////////////////////////////////////////////////////////
-/// SingleCore ///
-///////////////////////////////////////////////////////////////////////////////
+void CpuManager::SingleCoreRunIdleThread() {
+ auto& kernel = system.Kernel();
+ kernel.CurrentScheduler()->OnThreadStart();
+
+ while (true) {
+ PreemptSingleCore(false);
+ system.CoreTiming().AddTicks(1000U);
+ idle_count++;
+ HandleInterrupt();
+ }
+}
-void CpuManager::SingleCoreGuestActivate() {}
+void CpuManager::PreemptSingleCore(bool from_running_environment) {
+ {
+ auto& kernel = system.Kernel();
+ auto& scheduler = kernel.Scheduler(current_core);
+
+ Kernel::KThread* current_thread = scheduler.GetSchedulerCurrentThread();
+ if (idle_count >= 4 || from_running_environment) {
+ if (!from_running_environment) {
+ system.CoreTiming().Idle();
+ idle_count = 0;
+ }
+ kernel.SetIsPhantomModeForSingleCore(true);
+ system.CoreTiming().Advance();
+ kernel.SetIsPhantomModeForSingleCore(false);
+ }
+ current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES);
+ system.CoreTiming().ResetTicks();
+ scheduler.Unload(scheduler.GetSchedulerCurrentThread());
-void CpuManager::SingleCoreRunGuestThread() {}
+ auto& next_scheduler = kernel.Scheduler(current_core);
-void CpuManager::SingleCoreRunGuestLoop() {}
+ // Disable dispatch. We're about to preempt this thread.
+ Kernel::KScopedDisableDispatch dd{kernel};
+ Common::Fiber::YieldTo(current_thread->GetHostContext(), *next_scheduler.GetSwitchFiber());
+ }
-void CpuManager::PreemptSingleCore(bool from_running_enviroment) {}
+ // We've now been scheduled again, and we may have exchanged schedulers.
+ // Reload the scheduler in case it's different.
+ {
+ auto& scheduler = system.Kernel().Scheduler(current_core);
+ scheduler.Reload(scheduler.GetSchedulerCurrentThread());
+ if (!scheduler.IsIdle()) {
+ idle_count = 0;
+ }
+ }
+}
+
+void CpuManager::GuestActivate() {
+ // Similar to the HorizonKernelMain callback in HOS
+ auto& kernel = system.Kernel();
+ auto* scheduler = kernel.CurrentScheduler();
+
+ scheduler->Activate();
+ UNREACHABLE();
+}
void CpuManager::ShutdownThread() {
auto& kernel = system.Kernel();
@@ -168,20 +227,11 @@ void CpuManager::RunThread(std::size_t core) {
}
auto& kernel = system.Kernel();
+ auto& scheduler = *kernel.CurrentScheduler();
+ auto* thread = scheduler.GetSchedulerCurrentThread();
+ Kernel::SetCurrentThread(kernel, thread);
- 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());
+ Common::Fiber::YieldTo(data.host_context, *thread->GetHostContext());
}
} // namespace Core