kernel.cpp 21 KB

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