kernel.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <atomic>
  5. #include <bitset>
  6. #include <functional>
  7. #include <memory>
  8. #include <mutex>
  9. #include <thread>
  10. #include <unordered_map>
  11. #include <utility>
  12. #include "common/assert.h"
  13. #include "common/logging/log.h"
  14. #include "common/thread.h"
  15. #include "core/arm/arm_interface.h"
  16. #include "core/arm/exclusive_monitor.h"
  17. #include "core/core.h"
  18. #include "core/core_timing.h"
  19. #include "core/core_timing_util.h"
  20. #include "core/cpu_manager.h"
  21. #include "core/device_memory.h"
  22. #include "core/hardware_properties.h"
  23. #include "core/hle/kernel/client_port.h"
  24. #include "core/hle/kernel/errors.h"
  25. #include "core/hle/kernel/handle_table.h"
  26. #include "core/hle/kernel/kernel.h"
  27. #include "core/hle/kernel/memory/memory_layout.h"
  28. #include "core/hle/kernel/memory/memory_manager.h"
  29. #include "core/hle/kernel/memory/slab_heap.h"
  30. #include "core/hle/kernel/physical_core.h"
  31. #include "core/hle/kernel/process.h"
  32. #include "core/hle/kernel/resource_limit.h"
  33. #include "core/hle/kernel/scheduler.h"
  34. #include "core/hle/kernel/shared_memory.h"
  35. #include "core/hle/kernel/synchronization.h"
  36. #include "core/hle/kernel/thread.h"
  37. #include "core/hle/kernel/time_manager.h"
  38. #include "core/hle/lock.h"
  39. #include "core/hle/result.h"
  40. #include "core/memory.h"
  41. namespace Kernel {
  42. /**
  43. * Callback that will wake up the thread it was scheduled for
  44. * @param thread_handle The handle of the thread that's been awoken
  45. * @param cycles_late The number of CPU cycles that have passed since the desired wakeup time
  46. */
  47. static void ThreadWakeupCallback(u64 thread_handle, [[maybe_unused]] s64 cycles_late) {
  48. UNREACHABLE();
  49. const auto proper_handle = static_cast<Handle>(thread_handle);
  50. const auto& system = Core::System::GetInstance();
  51. // Lock the global kernel mutex when we enter the kernel HLE.
  52. std::lock_guard lock{HLE::g_hle_lock};
  53. std::shared_ptr<Thread> thread =
  54. system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
  55. if (thread == nullptr) {
  56. LOG_CRITICAL(Kernel, "Callback fired for invalid thread {:08X}", proper_handle);
  57. return;
  58. }
  59. bool resume = true;
  60. if (thread->GetStatus() == ThreadStatus::WaitSynch ||
  61. thread->GetStatus() == ThreadStatus::WaitHLEEvent) {
  62. // Remove the thread from each of its waiting objects' waitlists
  63. for (const auto& object : thread->GetSynchronizationObjects()) {
  64. object->RemoveWaitingThread(thread);
  65. }
  66. thread->ClearSynchronizationObjects();
  67. // Invoke the wakeup callback before clearing the wait objects
  68. if (thread->HasWakeupCallback()) {
  69. resume = thread->InvokeWakeupCallback(ThreadWakeupReason::Timeout, thread, nullptr, 0);
  70. }
  71. } else if (thread->GetStatus() == ThreadStatus::WaitMutex ||
  72. thread->GetStatus() == ThreadStatus::WaitCondVar) {
  73. thread->SetMutexWaitAddress(0);
  74. thread->SetWaitHandle(0);
  75. if (thread->GetStatus() == ThreadStatus::WaitCondVar) {
  76. thread->GetOwnerProcess()->RemoveConditionVariableThread(thread);
  77. thread->SetCondVarWaitAddress(0);
  78. }
  79. auto* const lock_owner = thread->GetLockOwner();
  80. // Threads waking up by timeout from WaitProcessWideKey do not perform priority inheritance
  81. // and don't have a lock owner unless SignalProcessWideKey was called first and the thread
  82. // wasn't awakened due to the mutex already being acquired.
  83. if (lock_owner != nullptr) {
  84. lock_owner->RemoveMutexWaiter(thread);
  85. }
  86. }
  87. if (thread->GetStatus() == ThreadStatus::WaitArb) {
  88. auto& address_arbiter = thread->GetOwnerProcess()->GetAddressArbiter();
  89. address_arbiter.HandleWakeupThread(thread);
  90. }
  91. if (resume) {
  92. if (thread->GetStatus() == ThreadStatus::WaitCondVar ||
  93. thread->GetStatus() == ThreadStatus::WaitArb) {
  94. thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
  95. }
  96. thread->ResumeFromWait();
  97. }
  98. }
  99. struct KernelCore::Impl {
  100. explicit Impl(Core::System& system, KernelCore& kernel)
  101. : global_scheduler{kernel}, synchronization{system}, time_manager{system}, system{system} {}
  102. void SetMulticore(bool is_multicore) {
  103. this->is_multicore = is_multicore;
  104. }
  105. void Initialize(KernelCore& kernel) {
  106. Shutdown();
  107. InitializePhysicalCores();
  108. InitializeSystemResourceLimit(kernel);
  109. InitializeMemoryLayout();
  110. InitializeThreads();
  111. InitializePreemption(kernel);
  112. InitializeSchedulers();
  113. InitializeSuspendThreads();
  114. }
  115. void Shutdown() {
  116. next_object_id = 0;
  117. next_kernel_process_id = Process::InitialKIPIDMin;
  118. next_user_process_id = Process::ProcessIDMin;
  119. next_thread_id = 1;
  120. process_list.clear();
  121. current_process = nullptr;
  122. system_resource_limit = nullptr;
  123. global_handle_table.Clear();
  124. thread_wakeup_event_type = nullptr;
  125. preemption_event = nullptr;
  126. global_scheduler.Shutdown();
  127. named_ports.clear();
  128. for (auto& core : cores) {
  129. core.Shutdown();
  130. }
  131. cores.clear();
  132. exclusive_monitor.reset();
  133. }
  134. void InitializePhysicalCores() {
  135. exclusive_monitor =
  136. Core::MakeExclusiveMonitor(system.Memory(), Core::Hardware::NUM_CPU_CORES);
  137. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  138. cores.emplace_back(system, i, *exclusive_monitor);
  139. }
  140. }
  141. void InitializeSchedulers() {
  142. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  143. cores[i].Scheduler().Initialize();
  144. }
  145. }
  146. // Creates the default system resource limit
  147. void InitializeSystemResourceLimit(KernelCore& kernel) {
  148. system_resource_limit = ResourceLimit::Create(kernel);
  149. // If setting the default system values fails, then something seriously wrong has occurred.
  150. ASSERT(system_resource_limit->SetLimitValue(ResourceType::PhysicalMemory, 0x100000000)
  151. .IsSuccess());
  152. ASSERT(system_resource_limit->SetLimitValue(ResourceType::Threads, 800).IsSuccess());
  153. ASSERT(system_resource_limit->SetLimitValue(ResourceType::Events, 700).IsSuccess());
  154. ASSERT(system_resource_limit->SetLimitValue(ResourceType::TransferMemory, 200).IsSuccess());
  155. ASSERT(system_resource_limit->SetLimitValue(ResourceType::Sessions, 900).IsSuccess());
  156. if (!system_resource_limit->Reserve(ResourceType::PhysicalMemory, 0) ||
  157. !system_resource_limit->Reserve(ResourceType::PhysicalMemory, 0x60000)) {
  158. UNREACHABLE();
  159. }
  160. }
  161. void InitializeThreads() {
  162. thread_wakeup_event_type =
  163. Core::Timing::CreateEvent("ThreadWakeupCallback", ThreadWakeupCallback);
  164. }
  165. void InitializePreemption(KernelCore& kernel) {
  166. preemption_event = Core::Timing::CreateEvent(
  167. "PreemptionCallback", [this, &kernel](u64 userdata, s64 cycles_late) {
  168. {
  169. SchedulerLock lock(kernel);
  170. global_scheduler.PreemptThreads();
  171. }
  172. s64 time_interval = Core::Timing::msToCycles(std::chrono::milliseconds(10));
  173. system.CoreTiming().ScheduleEvent(time_interval, preemption_event);
  174. });
  175. s64 time_interval = Core::Timing::msToCycles(std::chrono::milliseconds(10));
  176. system.CoreTiming().ScheduleEvent(time_interval, preemption_event);
  177. }
  178. void InitializeSuspendThreads() {
  179. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  180. std::string name = "Suspend Thread Id:" + std::to_string(i);
  181. std::function<void(void*)> init_func =
  182. system.GetCpuManager().GetSuspendThreadStartFunc();
  183. void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater();
  184. ThreadType type =
  185. static_cast<ThreadType>(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_SUSPEND);
  186. auto thread_res = Thread::Create(system, type, name, 0, 0, 0, static_cast<u32>(i), 0,
  187. nullptr, std::move(init_func), init_func_parameter);
  188. suspend_threads[i] = std::move(thread_res).Unwrap();
  189. }
  190. }
  191. void MakeCurrentProcess(Process* process) {
  192. current_process = process;
  193. if (process == nullptr) {
  194. return;
  195. }
  196. for (auto& core : cores) {
  197. core.SetIs64Bit(process->Is64BitProcess());
  198. }
  199. u32 core_id = GetCurrentHostThreadID();
  200. if (core_id < Core::Hardware::NUM_CPU_CORES) {
  201. system.Memory().SetCurrentPageTable(*process, core_id);
  202. }
  203. }
  204. void RegisterCoreThread(std::size_t core_id) {
  205. std::unique_lock lock{register_thread_mutex};
  206. if (!is_multicore) {
  207. single_core_thread_id = std::this_thread::get_id();
  208. }
  209. const std::thread::id this_id = std::this_thread::get_id();
  210. const auto it = host_thread_ids.find(this_id);
  211. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  212. ASSERT(it == host_thread_ids.end());
  213. ASSERT(!registered_core_threads[core_id]);
  214. host_thread_ids[this_id] = static_cast<u32>(core_id);
  215. registered_core_threads.set(core_id);
  216. }
  217. void RegisterHostThread() {
  218. std::unique_lock lock{register_thread_mutex};
  219. const std::thread::id this_id = std::this_thread::get_id();
  220. const auto it = host_thread_ids.find(this_id);
  221. if (it != host_thread_ids.end()) {
  222. return;
  223. }
  224. host_thread_ids[this_id] = registered_thread_ids++;
  225. }
  226. u32 GetCurrentHostThreadID() const {
  227. const std::thread::id this_id = std::this_thread::get_id();
  228. if (!is_multicore) {
  229. if (single_core_thread_id == this_id) {
  230. return static_cast<u32>(system.GetCpuManager().CurrentCore());
  231. }
  232. }
  233. const auto it = host_thread_ids.find(this_id);
  234. if (it == host_thread_ids.end()) {
  235. return Core::INVALID_HOST_THREAD_ID;
  236. }
  237. return it->second;
  238. }
  239. Core::EmuThreadHandle GetCurrentEmuThreadID() const {
  240. Core::EmuThreadHandle result = Core::EmuThreadHandle::InvalidHandle();
  241. result.host_handle = GetCurrentHostThreadID();
  242. if (result.host_handle >= Core::Hardware::NUM_CPU_CORES) {
  243. return result;
  244. }
  245. const Kernel::Scheduler& sched = cores[result.host_handle].Scheduler();
  246. const Kernel::Thread* current = sched.GetCurrentThread();
  247. if (current != nullptr) {
  248. result.guest_handle = current->GetGlobalHandle();
  249. } else {
  250. result.guest_handle = InvalidHandle;
  251. }
  252. return result;
  253. }
  254. void InitializeMemoryLayout() {
  255. // Initialize memory layout
  256. constexpr Memory::MemoryLayout layout{Memory::MemoryLayout::GetDefaultLayout()};
  257. constexpr std::size_t hid_size{0x40000};
  258. constexpr std::size_t font_size{0x1100000};
  259. constexpr std::size_t irs_size{0x8000};
  260. constexpr std::size_t time_size{0x1000};
  261. constexpr PAddr hid_addr{layout.System().StartAddress()};
  262. constexpr PAddr font_pa{layout.System().StartAddress() + hid_size};
  263. constexpr PAddr irs_addr{layout.System().StartAddress() + hid_size + font_size};
  264. constexpr PAddr time_addr{layout.System().StartAddress() + hid_size + font_size + irs_size};
  265. // Initialize memory manager
  266. memory_manager = std::make_unique<Memory::MemoryManager>();
  267. memory_manager->InitializeManager(Memory::MemoryManager::Pool::Application,
  268. layout.Application().StartAddress(),
  269. layout.Application().EndAddress());
  270. memory_manager->InitializeManager(Memory::MemoryManager::Pool::Applet,
  271. layout.Applet().StartAddress(),
  272. layout.Applet().EndAddress());
  273. memory_manager->InitializeManager(Memory::MemoryManager::Pool::System,
  274. layout.System().StartAddress(),
  275. layout.System().EndAddress());
  276. hid_shared_mem = Kernel::SharedMemory::Create(
  277. system.Kernel(), system.DeviceMemory(), nullptr,
  278. {hid_addr, hid_size / Memory::PageSize}, Memory::MemoryPermission::None,
  279. Memory::MemoryPermission::Read, hid_addr, hid_size, "HID:SharedMemory");
  280. font_shared_mem = Kernel::SharedMemory::Create(
  281. system.Kernel(), system.DeviceMemory(), nullptr,
  282. {font_pa, font_size / Memory::PageSize}, Memory::MemoryPermission::None,
  283. Memory::MemoryPermission::Read, font_pa, font_size, "Font:SharedMemory");
  284. irs_shared_mem = Kernel::SharedMemory::Create(
  285. system.Kernel(), system.DeviceMemory(), nullptr,
  286. {irs_addr, irs_size / Memory::PageSize}, Memory::MemoryPermission::None,
  287. Memory::MemoryPermission::Read, irs_addr, irs_size, "IRS:SharedMemory");
  288. time_shared_mem = Kernel::SharedMemory::Create(
  289. system.Kernel(), system.DeviceMemory(), nullptr,
  290. {time_addr, time_size / Memory::PageSize}, Memory::MemoryPermission::None,
  291. Memory::MemoryPermission::Read, time_addr, time_size, "Time:SharedMemory");
  292. // Allocate slab heaps
  293. user_slab_heap_pages = std::make_unique<Memory::SlabHeap<Memory::Page>>();
  294. // Initialize slab heaps
  295. constexpr u64 user_slab_heap_size{0x3de000};
  296. user_slab_heap_pages->Initialize(
  297. system.DeviceMemory().GetPointer(Core::DramMemoryMap::SlabHeapBase),
  298. user_slab_heap_size);
  299. }
  300. std::atomic<u32> next_object_id{0};
  301. std::atomic<u64> next_kernel_process_id{Process::InitialKIPIDMin};
  302. std::atomic<u64> next_user_process_id{Process::ProcessIDMin};
  303. std::atomic<u64> next_thread_id{1};
  304. // Lists all processes that exist in the current session.
  305. std::vector<std::shared_ptr<Process>> process_list;
  306. Process* current_process = nullptr;
  307. Kernel::GlobalScheduler global_scheduler;
  308. Kernel::Synchronization synchronization;
  309. Kernel::TimeManager time_manager;
  310. std::shared_ptr<ResourceLimit> system_resource_limit;
  311. std::shared_ptr<Core::Timing::EventType> thread_wakeup_event_type;
  312. std::shared_ptr<Core::Timing::EventType> preemption_event;
  313. // This is the kernel's handle table or supervisor handle table which
  314. // stores all the objects in place.
  315. Kernel::HandleTable global_handle_table;
  316. /// Map of named ports managed by the kernel, which can be retrieved using
  317. /// the ConnectToPort SVC.
  318. NamedPortTable named_ports;
  319. std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor;
  320. std::vector<Kernel::PhysicalCore> cores;
  321. // 0-3 IDs represent core threads, >3 represent others
  322. std::unordered_map<std::thread::id, u32> host_thread_ids;
  323. u32 registered_thread_ids{Core::Hardware::NUM_CPU_CORES};
  324. std::bitset<Core::Hardware::NUM_CPU_CORES> registered_core_threads;
  325. std::mutex register_thread_mutex;
  326. // Kernel memory management
  327. std::unique_ptr<Memory::MemoryManager> memory_manager;
  328. std::unique_ptr<Memory::SlabHeap<Memory::Page>> user_slab_heap_pages;
  329. // Shared memory for services
  330. std::shared_ptr<Kernel::SharedMemory> hid_shared_mem;
  331. std::shared_ptr<Kernel::SharedMemory> font_shared_mem;
  332. std::shared_ptr<Kernel::SharedMemory> irs_shared_mem;
  333. std::shared_ptr<Kernel::SharedMemory> time_shared_mem;
  334. std::array<std::shared_ptr<Thread>, Core::Hardware::NUM_CPU_CORES> suspend_threads{};
  335. bool is_multicore{};
  336. std::thread::id single_core_thread_id{};
  337. // System context
  338. Core::System& system;
  339. };
  340. KernelCore::KernelCore(Core::System& system) : impl{std::make_unique<Impl>(system, *this)} {}
  341. KernelCore::~KernelCore() {
  342. Shutdown();
  343. }
  344. void KernelCore::SetMulticore(bool is_multicore) {
  345. impl->SetMulticore(is_multicore);
  346. }
  347. void KernelCore::Initialize() {
  348. impl->Initialize(*this);
  349. }
  350. void KernelCore::Shutdown() {
  351. impl->Shutdown();
  352. }
  353. std::shared_ptr<ResourceLimit> KernelCore::GetSystemResourceLimit() const {
  354. return impl->system_resource_limit;
  355. }
  356. std::shared_ptr<Thread> KernelCore::RetrieveThreadFromGlobalHandleTable(Handle handle) const {
  357. return impl->global_handle_table.Get<Thread>(handle);
  358. }
  359. void KernelCore::AppendNewProcess(std::shared_ptr<Process> process) {
  360. impl->process_list.push_back(std::move(process));
  361. }
  362. void KernelCore::MakeCurrentProcess(Process* process) {
  363. impl->MakeCurrentProcess(process);
  364. }
  365. Process* KernelCore::CurrentProcess() {
  366. return impl->current_process;
  367. }
  368. const Process* KernelCore::CurrentProcess() const {
  369. return impl->current_process;
  370. }
  371. const std::vector<std::shared_ptr<Process>>& KernelCore::GetProcessList() const {
  372. return impl->process_list;
  373. }
  374. Kernel::GlobalScheduler& KernelCore::GlobalScheduler() {
  375. return impl->global_scheduler;
  376. }
  377. const Kernel::GlobalScheduler& KernelCore::GlobalScheduler() const {
  378. return impl->global_scheduler;
  379. }
  380. Kernel::Scheduler& KernelCore::Scheduler(std::size_t id) {
  381. return impl->cores[id].Scheduler();
  382. }
  383. const Kernel::Scheduler& KernelCore::Scheduler(std::size_t id) const {
  384. return impl->cores[id].Scheduler();
  385. }
  386. Kernel::PhysicalCore& KernelCore::PhysicalCore(std::size_t id) {
  387. return impl->cores[id];
  388. }
  389. const Kernel::PhysicalCore& KernelCore::PhysicalCore(std::size_t id) const {
  390. return impl->cores[id];
  391. }
  392. Kernel::PhysicalCore& KernelCore::CurrentPhysicalCore() {
  393. u32 core_id = impl->GetCurrentHostThreadID();
  394. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  395. return impl->cores[core_id];
  396. }
  397. const Kernel::PhysicalCore& KernelCore::CurrentPhysicalCore() const {
  398. u32 core_id = impl->GetCurrentHostThreadID();
  399. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  400. return impl->cores[core_id];
  401. }
  402. Kernel::Scheduler& KernelCore::CurrentScheduler() {
  403. return CurrentPhysicalCore().Scheduler();
  404. }
  405. const Kernel::Scheduler& KernelCore::CurrentScheduler() const {
  406. return CurrentPhysicalCore().Scheduler();
  407. }
  408. Kernel::Synchronization& KernelCore::Synchronization() {
  409. return impl->synchronization;
  410. }
  411. const Kernel::Synchronization& KernelCore::Synchronization() const {
  412. return impl->synchronization;
  413. }
  414. Kernel::TimeManager& KernelCore::TimeManager() {
  415. return impl->time_manager;
  416. }
  417. const Kernel::TimeManager& KernelCore::TimeManager() const {
  418. return impl->time_manager;
  419. }
  420. Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() {
  421. return *impl->exclusive_monitor;
  422. }
  423. const Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() const {
  424. return *impl->exclusive_monitor;
  425. }
  426. void KernelCore::InvalidateAllInstructionCaches() {
  427. for (std::size_t i = 0; i < impl->global_scheduler.CpuCoresCount(); i++) {
  428. PhysicalCore(i).ArmInterface().ClearInstructionCache();
  429. }
  430. }
  431. void KernelCore::PrepareReschedule(std::size_t id) {
  432. if (id < impl->global_scheduler.CpuCoresCount()) {
  433. impl->cores[id].Stop();
  434. }
  435. }
  436. void KernelCore::AddNamedPort(std::string name, std::shared_ptr<ClientPort> port) {
  437. impl->named_ports.emplace(std::move(name), std::move(port));
  438. }
  439. KernelCore::NamedPortTable::iterator KernelCore::FindNamedPort(const std::string& name) {
  440. return impl->named_ports.find(name);
  441. }
  442. KernelCore::NamedPortTable::const_iterator KernelCore::FindNamedPort(
  443. const std::string& name) const {
  444. return impl->named_ports.find(name);
  445. }
  446. bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const {
  447. return port != impl->named_ports.cend();
  448. }
  449. u32 KernelCore::CreateNewObjectID() {
  450. return impl->next_object_id++;
  451. }
  452. u64 KernelCore::CreateNewThreadID() {
  453. return impl->next_thread_id++;
  454. }
  455. u64 KernelCore::CreateNewKernelProcessID() {
  456. return impl->next_kernel_process_id++;
  457. }
  458. u64 KernelCore::CreateNewUserProcessID() {
  459. return impl->next_user_process_id++;
  460. }
  461. const std::shared_ptr<Core::Timing::EventType>& KernelCore::ThreadWakeupCallbackEventType() const {
  462. return impl->thread_wakeup_event_type;
  463. }
  464. Kernel::HandleTable& KernelCore::GlobalHandleTable() {
  465. return impl->global_handle_table;
  466. }
  467. const Kernel::HandleTable& KernelCore::GlobalHandleTable() const {
  468. return impl->global_handle_table;
  469. }
  470. void KernelCore::RegisterCoreThread(std::size_t core_id) {
  471. impl->RegisterCoreThread(core_id);
  472. }
  473. void KernelCore::RegisterHostThread() {
  474. impl->RegisterHostThread();
  475. }
  476. u32 KernelCore::GetCurrentHostThreadID() const {
  477. return impl->GetCurrentHostThreadID();
  478. }
  479. Core::EmuThreadHandle KernelCore::GetCurrentEmuThreadID() const {
  480. return impl->GetCurrentEmuThreadID();
  481. }
  482. Memory::MemoryManager& KernelCore::MemoryManager() {
  483. return *impl->memory_manager;
  484. }
  485. const Memory::MemoryManager& KernelCore::MemoryManager() const {
  486. return *impl->memory_manager;
  487. }
  488. Memory::SlabHeap<Memory::Page>& KernelCore::GetUserSlabHeapPages() {
  489. return *impl->user_slab_heap_pages;
  490. }
  491. const Memory::SlabHeap<Memory::Page>& KernelCore::GetUserSlabHeapPages() const {
  492. return *impl->user_slab_heap_pages;
  493. }
  494. Kernel::SharedMemory& KernelCore::GetHidSharedMem() {
  495. return *impl->hid_shared_mem;
  496. }
  497. const Kernel::SharedMemory& KernelCore::GetHidSharedMem() const {
  498. return *impl->hid_shared_mem;
  499. }
  500. Kernel::SharedMemory& KernelCore::GetFontSharedMem() {
  501. return *impl->font_shared_mem;
  502. }
  503. const Kernel::SharedMemory& KernelCore::GetFontSharedMem() const {
  504. return *impl->font_shared_mem;
  505. }
  506. Kernel::SharedMemory& KernelCore::GetIrsSharedMem() {
  507. return *impl->irs_shared_mem;
  508. }
  509. const Kernel::SharedMemory& KernelCore::GetIrsSharedMem() const {
  510. return *impl->irs_shared_mem;
  511. }
  512. Kernel::SharedMemory& KernelCore::GetTimeSharedMem() {
  513. return *impl->time_shared_mem;
  514. }
  515. const Kernel::SharedMemory& KernelCore::GetTimeSharedMem() const {
  516. return *impl->time_shared_mem;
  517. }
  518. void KernelCore::Suspend(bool in_suspention) {
  519. const bool should_suspend = exception_exited || in_suspention;
  520. {
  521. SchedulerLock lock(*this);
  522. ThreadStatus status = should_suspend ? ThreadStatus::Ready : ThreadStatus::WaitSleep;
  523. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  524. impl->suspend_threads[i]->SetStatus(status);
  525. }
  526. }
  527. }
  528. void KernelCore::ExceptionalExit() {
  529. exception_exited = true;
  530. Suspend(true);
  531. }
  532. } // namespace Kernel