kernel.cpp 21 KB

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