summaryrefslogtreecommitdiffstats
path: root/src/core/cpu_manager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/cpu_manager.cpp')
-rw-r--r--src/core/cpu_manager.cpp161
1 files changed, 61 insertions, 100 deletions
diff --git a/src/core/cpu_manager.cpp b/src/core/cpu_manager.cpp
index 37d3d83b9..428194129 100644
--- a/src/core/cpu_manager.cpp
+++ b/src/core/cpu_manager.cpp
@@ -8,6 +8,7 @@
#include "core/core.h"
#include "core/core_timing.h"
#include "core/cpu_manager.h"
+#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"
@@ -41,44 +42,65 @@ void CpuManager::Shutdown() {
}
}
-void CpuManager::GuestThreadFunction() {
+void CpuManager::GuestActivateFunction() {
if (is_multicore) {
- MultiCoreRunGuestThread();
+ MultiCoreGuestActivate();
} else {
- SingleCoreRunGuestThread();
+ SingleCoreGuestActivate();
}
}
-void CpuManager::GuestRewindFunction() {
+void CpuManager::GuestThreadFunction() {
if (is_multicore) {
- MultiCoreRunGuestLoop();
+ MultiCoreRunGuestThread();
} else {
- SingleCoreRunGuestLoop();
+ SingleCoreRunGuestThread();
}
}
-void CpuManager::IdleThreadFunction() {
- if (is_multicore) {
- MultiCoreRunIdleThread();
- } else {
- SingleCoreRunIdleThread();
+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::ShutdownThreadFunction() {
- ShutdownThread();
+void CpuManager::HandleInterrupt() {
+ auto& kernel = system.Kernel();
+ auto core_index = kernel.CurrentPhysicalCoreIndex();
+
+ Kernel::KInterruptManager::HandleInterrupt(kernel, static_cast<s32>(core_index));
}
///////////////////////////////////////////////////////////////////////////////
/// MultiCore ///
///////////////////////////////////////////////////////////////////////////////
+void CpuManager::MultiCoreGuestActivate() {
+ // Similar to the HorizonKernelMain callback in HOS
+ auto& kernel = system.Kernel();
+ auto* scheduler = kernel.CurrentScheduler();
+
+ scheduler->Activate();
+ UNREACHABLE();
+}
+
void CpuManager::MultiCoreRunGuestThread() {
+ // Similar to UserModeThreadStarter in HOS
auto& kernel = system.Kernel();
- kernel.CurrentScheduler()->OnThreadStart();
- auto* thread = kernel.CurrentScheduler()->GetSchedulerCurrentThread();
- auto& host_context = thread->GetHostContext();
- host_context->SetRewindPoint([this] { GuestRewindFunction(); });
+ auto* thread = kernel.GetCurrentEmuThread();
+ thread->EnableDispatch();
+
MultiCoreRunGuestLoop();
}
@@ -91,18 +113,8 @@ void CpuManager::MultiCoreRunGuestLoop() {
physical_core->Run();
physical_core = &kernel.CurrentPhysicalCore();
}
- {
- Kernel::KScopedDisableDispatch dd(kernel);
- physical_core->ArmInterface().ClearExclusiveState();
- }
- }
-}
-void CpuManager::MultiCoreRunIdleThread() {
- auto& kernel = system.Kernel();
- while (true) {
- Kernel::KScopedDisableDispatch dd(kernel);
- kernel.CurrentPhysicalCore().Idle();
+ HandleInterrupt();
}
}
@@ -110,83 +122,20 @@ void CpuManager::MultiCoreRunIdleThread() {
/// SingleCore ///
///////////////////////////////////////////////////////////////////////////////
-void CpuManager::SingleCoreRunGuestThread() {
- auto& kernel = system.Kernel();
- kernel.CurrentScheduler()->OnThreadStart();
- auto* thread = kernel.CurrentScheduler()->GetSchedulerCurrentThread();
- auto& host_context = thread->GetHostContext();
- host_context->SetRewindPoint([this] { GuestRewindFunction(); });
- SingleCoreRunGuestLoop();
-}
-
-void CpuManager::SingleCoreRunGuestLoop() {
- auto& kernel = system.Kernel();
- while (true) {
- auto* physical_core = &kernel.CurrentPhysicalCore();
- if (!physical_core->IsInterrupted()) {
- physical_core->Run();
- physical_core = &kernel.CurrentPhysicalCore();
- }
- kernel.SetIsPhantomModeForSingleCore(true);
- system.CoreTiming().Advance();
- kernel.SetIsPhantomModeForSingleCore(false);
- 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::SingleCoreGuestActivate() {}
-void CpuManager::PreemptSingleCore(bool from_running_enviroment) {
- {
- auto& kernel = system.Kernel();
- auto& scheduler = kernel.Scheduler(current_core);
- Kernel::KThread* current_thread = scheduler.GetSchedulerCurrentThread();
- if (idle_count >= 4 || from_running_enviroment) {
- if (!from_running_enviroment) {
- 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);
- Common::Fiber::YieldTo(current_thread->GetHostContext(), *next_scheduler.ControlContext());
- }
+void CpuManager::SingleCoreRunGuestLoop() {}
- // May have changed scheduler
- {
- auto& scheduler = system.Kernel().Scheduler(current_core);
- scheduler.Reload(scheduler.GetSchedulerCurrentThread());
- if (!scheduler.IsIdle()) {
- idle_count = 0;
- }
- }
-}
+void CpuManager::PreemptSingleCore(bool from_running_enviroment) {}
void CpuManager::ShutdownThread() {
auto& kernel = system.Kernel();
+ auto* thread = kernel.GetCurrentEmuThread();
auto core = is_multicore ? kernel.CurrentPhysicalCoreIndex() : 0;
- auto* current_thread = kernel.GetCurrentEmuThread();
- Common::Fiber::YieldTo(current_thread->GetHostContext(), *core_data[core].host_context);
+ Common::Fiber::YieldTo(thread->GetHostContext(), *core_data[core].host_context);
UNREACHABLE();
}
@@ -218,9 +167,21 @@ void CpuManager::RunThread(std::size_t core) {
system.GPU().ObtainContext();
}
- auto* current_thread = system.Kernel().CurrentScheduler()->GetIdleThread();
- Kernel::SetCurrentThread(system.Kernel(), current_thread);
- Common::Fiber::YieldTo(data.host_context, *current_thread->GetHostContext());
+ 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