kernel.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. struct KernelCore::Impl {
  47. explicit Impl(Core::System& system, KernelCore& kernel)
  48. : global_scheduler{kernel}, synchronization{system}, time_manager{system},
  49. global_handle_table{kernel}, system{system} {}
  50. void SetMulticore(bool is_multicore) {
  51. this->is_multicore = is_multicore;
  52. }
  53. void Initialize(KernelCore& kernel) {
  54. Shutdown();
  55. RegisterHostThread();
  56. InitializePhysicalCores();
  57. InitializeSystemResourceLimit(kernel);
  58. InitializeMemoryLayout();
  59. InitializePreemption(kernel);
  60. InitializeSchedulers();
  61. InitializeSuspendThreads();
  62. }
  63. void Shutdown() {
  64. next_object_id = 0;
  65. next_kernel_process_id = Process::InitialKIPIDMin;
  66. next_user_process_id = Process::ProcessIDMin;
  67. next_thread_id = 1;
  68. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  69. if (suspend_threads[i]) {
  70. suspend_threads[i].reset();
  71. }
  72. }
  73. for (std::size_t i = 0; i < cores.size(); i++) {
  74. cores[i].Shutdown();
  75. schedulers[i].reset();
  76. }
  77. cores.clear();
  78. registered_core_threads.reset();
  79. process_list.clear();
  80. current_process = nullptr;
  81. system_resource_limit = nullptr;
  82. global_handle_table.Clear();
  83. preemption_event = nullptr;
  84. global_scheduler.Shutdown();
  85. named_ports.clear();
  86. for (auto& core : cores) {
  87. core.Shutdown();
  88. }
  89. cores.clear();
  90. exclusive_monitor.reset();
  91. host_thread_ids.clear();
  92. }
  93. void InitializePhysicalCores() {
  94. exclusive_monitor =
  95. Core::MakeExclusiveMonitor(system.Memory(), Core::Hardware::NUM_CPU_CORES);
  96. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  97. schedulers[i] = std::make_unique<Kernel::Scheduler>(system, i);
  98. cores.emplace_back(system, i, *schedulers[i], interrupts[i]);
  99. }
  100. }
  101. void InitializeSchedulers() {
  102. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  103. cores[i].Scheduler().Initialize();
  104. }
  105. }
  106. // Creates the default system resource limit
  107. void InitializeSystemResourceLimit(KernelCore& kernel) {
  108. system_resource_limit = ResourceLimit::Create(kernel);
  109. // If setting the default system values fails, then something seriously wrong has occurred.
  110. ASSERT(system_resource_limit->SetLimitValue(ResourceType::PhysicalMemory, 0x100000000)
  111. .IsSuccess());
  112. ASSERT(system_resource_limit->SetLimitValue(ResourceType::Threads, 800).IsSuccess());
  113. ASSERT(system_resource_limit->SetLimitValue(ResourceType::Events, 700).IsSuccess());
  114. ASSERT(system_resource_limit->SetLimitValue(ResourceType::TransferMemory, 200).IsSuccess());
  115. ASSERT(system_resource_limit->SetLimitValue(ResourceType::Sessions, 900).IsSuccess());
  116. if (!system_resource_limit->Reserve(ResourceType::PhysicalMemory, 0) ||
  117. !system_resource_limit->Reserve(ResourceType::PhysicalMemory, 0x60000)) {
  118. UNREACHABLE();
  119. }
  120. }
  121. void InitializePreemption(KernelCore& kernel) {
  122. preemption_event = Core::Timing::CreateEvent(
  123. "PreemptionCallback", [this, &kernel](std::uintptr_t, std::chrono::nanoseconds) {
  124. {
  125. SchedulerLock lock(kernel);
  126. global_scheduler.PreemptThreads();
  127. }
  128. const auto time_interval = std::chrono::nanoseconds{
  129. Core::Timing::msToCycles(std::chrono::milliseconds(10))};
  130. system.CoreTiming().ScheduleEvent(time_interval, preemption_event);
  131. });
  132. const auto time_interval =
  133. std::chrono::nanoseconds{Core::Timing::msToCycles(std::chrono::milliseconds(10))};
  134. system.CoreTiming().ScheduleEvent(time_interval, preemption_event);
  135. }
  136. void InitializeSuspendThreads() {
  137. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  138. std::string name = "Suspend Thread Id:" + std::to_string(i);
  139. std::function<void(void*)> init_func = Core::CpuManager::GetSuspendThreadStartFunc();
  140. void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater();
  141. const auto type =
  142. static_cast<ThreadType>(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_SUSPEND);
  143. auto thread_res =
  144. Thread::Create(system, type, std::move(name), 0, 0, 0, static_cast<u32>(i), 0,
  145. nullptr, std::move(init_func), init_func_parameter);
  146. suspend_threads[i] = std::move(thread_res).Unwrap();
  147. }
  148. }
  149. void MakeCurrentProcess(Process* process) {
  150. current_process = process;
  151. if (process == nullptr) {
  152. return;
  153. }
  154. u32 core_id = GetCurrentHostThreadID();
  155. if (core_id < Core::Hardware::NUM_CPU_CORES) {
  156. system.Memory().SetCurrentPageTable(*process, core_id);
  157. }
  158. }
  159. void RegisterCoreThread(std::size_t core_id) {
  160. std::unique_lock lock{register_thread_mutex};
  161. if (!is_multicore) {
  162. single_core_thread_id = std::this_thread::get_id();
  163. }
  164. const std::thread::id this_id = std::this_thread::get_id();
  165. const auto it = host_thread_ids.find(this_id);
  166. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  167. ASSERT(it == host_thread_ids.end());
  168. ASSERT(!registered_core_threads[core_id]);
  169. host_thread_ids[this_id] = static_cast<u32>(core_id);
  170. registered_core_threads.set(core_id);
  171. }
  172. void RegisterHostThread() {
  173. std::unique_lock lock{register_thread_mutex};
  174. const std::thread::id this_id = std::this_thread::get_id();
  175. const auto it = host_thread_ids.find(this_id);
  176. if (it != host_thread_ids.end()) {
  177. return;
  178. }
  179. host_thread_ids[this_id] = registered_thread_ids++;
  180. }
  181. u32 GetCurrentHostThreadID() const {
  182. const std::thread::id this_id = std::this_thread::get_id();
  183. if (!is_multicore) {
  184. if (single_core_thread_id == this_id) {
  185. return static_cast<u32>(system.GetCpuManager().CurrentCore());
  186. }
  187. }
  188. const auto it = host_thread_ids.find(this_id);
  189. if (it == host_thread_ids.end()) {
  190. return Core::INVALID_HOST_THREAD_ID;
  191. }
  192. return it->second;
  193. }
  194. Core::EmuThreadHandle GetCurrentEmuThreadID() const {
  195. Core::EmuThreadHandle result = Core::EmuThreadHandle::InvalidHandle();
  196. result.host_handle = GetCurrentHostThreadID();
  197. if (result.host_handle >= Core::Hardware::NUM_CPU_CORES) {
  198. return result;
  199. }
  200. const Kernel::Scheduler& sched = cores[result.host_handle].Scheduler();
  201. const Kernel::Thread* current = sched.GetCurrentThread();
  202. if (current != nullptr && !current->IsPhantomMode()) {
  203. result.guest_handle = current->GetGlobalHandle();
  204. } else {
  205. result.guest_handle = InvalidHandle;
  206. }
  207. return result;
  208. }
  209. void InitializeMemoryLayout() {
  210. // Initialize memory layout
  211. constexpr Memory::MemoryLayout layout{Memory::MemoryLayout::GetDefaultLayout()};
  212. constexpr std::size_t hid_size{0x40000};
  213. constexpr std::size_t font_size{0x1100000};
  214. constexpr std::size_t irs_size{0x8000};
  215. constexpr std::size_t time_size{0x1000};
  216. constexpr PAddr hid_addr{layout.System().StartAddress()};
  217. constexpr PAddr font_pa{layout.System().StartAddress() + hid_size};
  218. constexpr PAddr irs_addr{layout.System().StartAddress() + hid_size + font_size};
  219. constexpr PAddr time_addr{layout.System().StartAddress() + hid_size + font_size + irs_size};
  220. // Initialize memory manager
  221. memory_manager = std::make_unique<Memory::MemoryManager>();
  222. memory_manager->InitializeManager(Memory::MemoryManager::Pool::Application,
  223. layout.Application().StartAddress(),
  224. layout.Application().EndAddress());
  225. memory_manager->InitializeManager(Memory::MemoryManager::Pool::Applet,
  226. layout.Applet().StartAddress(),
  227. layout.Applet().EndAddress());
  228. memory_manager->InitializeManager(Memory::MemoryManager::Pool::System,
  229. layout.System().StartAddress(),
  230. layout.System().EndAddress());
  231. hid_shared_mem = Kernel::SharedMemory::Create(
  232. system.Kernel(), system.DeviceMemory(), nullptr,
  233. {hid_addr, hid_size / Memory::PageSize}, Memory::MemoryPermission::None,
  234. Memory::MemoryPermission::Read, hid_addr, hid_size, "HID:SharedMemory");
  235. font_shared_mem = Kernel::SharedMemory::Create(
  236. system.Kernel(), system.DeviceMemory(), nullptr,
  237. {font_pa, font_size / Memory::PageSize}, Memory::MemoryPermission::None,
  238. Memory::MemoryPermission::Read, font_pa, font_size, "Font:SharedMemory");
  239. irs_shared_mem = Kernel::SharedMemory::Create(
  240. system.Kernel(), system.DeviceMemory(), nullptr,
  241. {irs_addr, irs_size / Memory::PageSize}, Memory::MemoryPermission::None,
  242. Memory::MemoryPermission::Read, irs_addr, irs_size, "IRS:SharedMemory");
  243. time_shared_mem = Kernel::SharedMemory::Create(
  244. system.Kernel(), system.DeviceMemory(), nullptr,
  245. {time_addr, time_size / Memory::PageSize}, Memory::MemoryPermission::None,
  246. Memory::MemoryPermission::Read, time_addr, time_size, "Time:SharedMemory");
  247. // Allocate slab heaps
  248. user_slab_heap_pages = std::make_unique<Memory::SlabHeap<Memory::Page>>();
  249. // Initialize slab heaps
  250. constexpr u64 user_slab_heap_size{0x3de000};
  251. user_slab_heap_pages->Initialize(
  252. system.DeviceMemory().GetPointer(Core::DramMemoryMap::SlabHeapBase),
  253. user_slab_heap_size);
  254. }
  255. std::atomic<u32> next_object_id{0};
  256. std::atomic<u64> next_kernel_process_id{Process::InitialKIPIDMin};
  257. std::atomic<u64> next_user_process_id{Process::ProcessIDMin};
  258. std::atomic<u64> next_thread_id{1};
  259. // Lists all processes that exist in the current session.
  260. std::vector<std::shared_ptr<Process>> process_list;
  261. Process* current_process = nullptr;
  262. Kernel::GlobalScheduler global_scheduler;
  263. Kernel::Synchronization synchronization;
  264. Kernel::TimeManager time_manager;
  265. std::shared_ptr<ResourceLimit> system_resource_limit;
  266. std::shared_ptr<Core::Timing::EventType> preemption_event;
  267. // This is the kernel's handle table or supervisor handle table which
  268. // stores all the objects in place.
  269. HandleTable global_handle_table;
  270. /// Map of named ports managed by the kernel, which can be retrieved using
  271. /// the ConnectToPort SVC.
  272. NamedPortTable named_ports;
  273. std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor;
  274. std::vector<Kernel::PhysicalCore> cores;
  275. // 0-3 IDs represent core threads, >3 represent others
  276. std::unordered_map<std::thread::id, u32> host_thread_ids;
  277. u32 registered_thread_ids{Core::Hardware::NUM_CPU_CORES};
  278. std::bitset<Core::Hardware::NUM_CPU_CORES> registered_core_threads;
  279. std::mutex register_thread_mutex;
  280. // Kernel memory management
  281. std::unique_ptr<Memory::MemoryManager> memory_manager;
  282. std::unique_ptr<Memory::SlabHeap<Memory::Page>> user_slab_heap_pages;
  283. // Shared memory for services
  284. std::shared_ptr<Kernel::SharedMemory> hid_shared_mem;
  285. std::shared_ptr<Kernel::SharedMemory> font_shared_mem;
  286. std::shared_ptr<Kernel::SharedMemory> irs_shared_mem;
  287. std::shared_ptr<Kernel::SharedMemory> time_shared_mem;
  288. std::array<std::shared_ptr<Thread>, Core::Hardware::NUM_CPU_CORES> suspend_threads{};
  289. std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES> interrupts{};
  290. std::array<std::unique_ptr<Kernel::Scheduler>, Core::Hardware::NUM_CPU_CORES> schedulers{};
  291. bool is_multicore{};
  292. std::thread::id single_core_thread_id{};
  293. std::array<u64, Core::Hardware::NUM_CPU_CORES> svc_ticks{};
  294. // System context
  295. Core::System& system;
  296. };
  297. KernelCore::KernelCore(Core::System& system) : impl{std::make_unique<Impl>(system, *this)} {}
  298. KernelCore::~KernelCore() {
  299. Shutdown();
  300. }
  301. void KernelCore::SetMulticore(bool is_multicore) {
  302. impl->SetMulticore(is_multicore);
  303. }
  304. void KernelCore::Initialize() {
  305. impl->Initialize(*this);
  306. }
  307. void KernelCore::Shutdown() {
  308. impl->Shutdown();
  309. }
  310. std::shared_ptr<ResourceLimit> KernelCore::GetSystemResourceLimit() const {
  311. return impl->system_resource_limit;
  312. }
  313. std::shared_ptr<Thread> KernelCore::RetrieveThreadFromGlobalHandleTable(Handle handle) const {
  314. return impl->global_handle_table.Get<Thread>(handle);
  315. }
  316. void KernelCore::AppendNewProcess(std::shared_ptr<Process> process) {
  317. impl->process_list.push_back(std::move(process));
  318. }
  319. void KernelCore::MakeCurrentProcess(Process* process) {
  320. impl->MakeCurrentProcess(process);
  321. }
  322. Process* KernelCore::CurrentProcess() {
  323. return impl->current_process;
  324. }
  325. const Process* KernelCore::CurrentProcess() const {
  326. return impl->current_process;
  327. }
  328. const std::vector<std::shared_ptr<Process>>& KernelCore::GetProcessList() const {
  329. return impl->process_list;
  330. }
  331. Kernel::GlobalScheduler& KernelCore::GlobalScheduler() {
  332. return impl->global_scheduler;
  333. }
  334. const Kernel::GlobalScheduler& KernelCore::GlobalScheduler() const {
  335. return impl->global_scheduler;
  336. }
  337. Kernel::Scheduler& KernelCore::Scheduler(std::size_t id) {
  338. return *impl->schedulers[id];
  339. }
  340. const Kernel::Scheduler& KernelCore::Scheduler(std::size_t id) const {
  341. return *impl->schedulers[id];
  342. }
  343. Kernel::PhysicalCore& KernelCore::PhysicalCore(std::size_t id) {
  344. return impl->cores[id];
  345. }
  346. const Kernel::PhysicalCore& KernelCore::PhysicalCore(std::size_t id) const {
  347. return impl->cores[id];
  348. }
  349. Kernel::PhysicalCore& KernelCore::CurrentPhysicalCore() {
  350. u32 core_id = impl->GetCurrentHostThreadID();
  351. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  352. return impl->cores[core_id];
  353. }
  354. const Kernel::PhysicalCore& KernelCore::CurrentPhysicalCore() const {
  355. u32 core_id = impl->GetCurrentHostThreadID();
  356. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  357. return impl->cores[core_id];
  358. }
  359. Kernel::Scheduler& KernelCore::CurrentScheduler() {
  360. u32 core_id = impl->GetCurrentHostThreadID();
  361. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  362. return *impl->schedulers[core_id];
  363. }
  364. const Kernel::Scheduler& KernelCore::CurrentScheduler() const {
  365. u32 core_id = impl->GetCurrentHostThreadID();
  366. ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
  367. return *impl->schedulers[core_id];
  368. }
  369. std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& KernelCore::Interrupts() {
  370. return impl->interrupts;
  371. }
  372. const std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& KernelCore::Interrupts()
  373. const {
  374. return impl->interrupts;
  375. }
  376. Kernel::Synchronization& KernelCore::Synchronization() {
  377. return impl->synchronization;
  378. }
  379. const Kernel::Synchronization& KernelCore::Synchronization() const {
  380. return impl->synchronization;
  381. }
  382. Kernel::TimeManager& KernelCore::TimeManager() {
  383. return impl->time_manager;
  384. }
  385. const Kernel::TimeManager& KernelCore::TimeManager() const {
  386. return impl->time_manager;
  387. }
  388. Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() {
  389. return *impl->exclusive_monitor;
  390. }
  391. const Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() const {
  392. return *impl->exclusive_monitor;
  393. }
  394. void KernelCore::InvalidateAllInstructionCaches() {
  395. auto& threads = GlobalScheduler().GetThreadList();
  396. for (auto& thread : threads) {
  397. if (!thread->IsHLEThread()) {
  398. auto& arm_interface = thread->ArmInterface();
  399. arm_interface.ClearInstructionCache();
  400. }
  401. }
  402. }
  403. void KernelCore::PrepareReschedule(std::size_t id) {
  404. // TODO: Reimplement, this
  405. }
  406. void KernelCore::AddNamedPort(std::string name, std::shared_ptr<ClientPort> port) {
  407. impl->named_ports.emplace(std::move(name), std::move(port));
  408. }
  409. KernelCore::NamedPortTable::iterator KernelCore::FindNamedPort(const std::string& name) {
  410. return impl->named_ports.find(name);
  411. }
  412. KernelCore::NamedPortTable::const_iterator KernelCore::FindNamedPort(
  413. const std::string& name) const {
  414. return impl->named_ports.find(name);
  415. }
  416. bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const {
  417. return port != impl->named_ports.cend();
  418. }
  419. u32 KernelCore::CreateNewObjectID() {
  420. return impl->next_object_id++;
  421. }
  422. u64 KernelCore::CreateNewThreadID() {
  423. return impl->next_thread_id++;
  424. }
  425. u64 KernelCore::CreateNewKernelProcessID() {
  426. return impl->next_kernel_process_id++;
  427. }
  428. u64 KernelCore::CreateNewUserProcessID() {
  429. return impl->next_user_process_id++;
  430. }
  431. Kernel::HandleTable& KernelCore::GlobalHandleTable() {
  432. return impl->global_handle_table;
  433. }
  434. const Kernel::HandleTable& KernelCore::GlobalHandleTable() const {
  435. return impl->global_handle_table;
  436. }
  437. void KernelCore::RegisterCoreThread(std::size_t core_id) {
  438. impl->RegisterCoreThread(core_id);
  439. }
  440. void KernelCore::RegisterHostThread() {
  441. impl->RegisterHostThread();
  442. }
  443. u32 KernelCore::GetCurrentHostThreadID() const {
  444. return impl->GetCurrentHostThreadID();
  445. }
  446. Core::EmuThreadHandle KernelCore::GetCurrentEmuThreadID() const {
  447. return impl->GetCurrentEmuThreadID();
  448. }
  449. Memory::MemoryManager& KernelCore::MemoryManager() {
  450. return *impl->memory_manager;
  451. }
  452. const Memory::MemoryManager& KernelCore::MemoryManager() const {
  453. return *impl->memory_manager;
  454. }
  455. Memory::SlabHeap<Memory::Page>& KernelCore::GetUserSlabHeapPages() {
  456. return *impl->user_slab_heap_pages;
  457. }
  458. const Memory::SlabHeap<Memory::Page>& KernelCore::GetUserSlabHeapPages() const {
  459. return *impl->user_slab_heap_pages;
  460. }
  461. Kernel::SharedMemory& KernelCore::GetHidSharedMem() {
  462. return *impl->hid_shared_mem;
  463. }
  464. const Kernel::SharedMemory& KernelCore::GetHidSharedMem() const {
  465. return *impl->hid_shared_mem;
  466. }
  467. Kernel::SharedMemory& KernelCore::GetFontSharedMem() {
  468. return *impl->font_shared_mem;
  469. }
  470. const Kernel::SharedMemory& KernelCore::GetFontSharedMem() const {
  471. return *impl->font_shared_mem;
  472. }
  473. Kernel::SharedMemory& KernelCore::GetIrsSharedMem() {
  474. return *impl->irs_shared_mem;
  475. }
  476. const Kernel::SharedMemory& KernelCore::GetIrsSharedMem() const {
  477. return *impl->irs_shared_mem;
  478. }
  479. Kernel::SharedMemory& KernelCore::GetTimeSharedMem() {
  480. return *impl->time_shared_mem;
  481. }
  482. const Kernel::SharedMemory& KernelCore::GetTimeSharedMem() const {
  483. return *impl->time_shared_mem;
  484. }
  485. void KernelCore::Suspend(bool in_suspention) {
  486. const bool should_suspend = exception_exited || in_suspention;
  487. {
  488. SchedulerLock lock(*this);
  489. ThreadStatus status = should_suspend ? ThreadStatus::Ready : ThreadStatus::WaitSleep;
  490. for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
  491. impl->suspend_threads[i]->SetStatus(status);
  492. }
  493. }
  494. }
  495. bool KernelCore::IsMulticore() const {
  496. return impl->is_multicore;
  497. }
  498. void KernelCore::ExceptionalExit() {
  499. exception_exited = true;
  500. Suspend(true);
  501. }
  502. void KernelCore::EnterSVCProfile() {
  503. std::size_t core = impl->GetCurrentHostThreadID();
  504. impl->svc_ticks[core] = MicroProfileEnter(MICROPROFILE_TOKEN(Kernel_SVC));
  505. }
  506. void KernelCore::ExitSVCProfile() {
  507. std::size_t core = impl->GetCurrentHostThreadID();
  508. MicroProfileLeave(MICROPROFILE_TOKEN(Kernel_SVC), impl->svc_ticks[core]);
  509. }
  510. } // namespace Kernel