kernel.cpp 24 KB

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