summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/kernel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/kernel/kernel.cpp')
-rw-r--r--src/core/hle/kernel/kernel.cpp68
1 files changed, 49 insertions, 19 deletions
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 9251f29ad..eed2dc9f3 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -24,6 +24,7 @@
24#include "core/hardware_properties.h" 24#include "core/hardware_properties.h"
25#include "core/hle/kernel/init/init_slab_setup.h" 25#include "core/hle/kernel/init/init_slab_setup.h"
26#include "core/hle/kernel/k_client_port.h" 26#include "core/hle/kernel/k_client_port.h"
27#include "core/hle/kernel/k_dynamic_resource_manager.h"
27#include "core/hle/kernel/k_handle_table.h" 28#include "core/hle/kernel/k_handle_table.h"
28#include "core/hle/kernel/k_memory_layout.h" 29#include "core/hle/kernel/k_memory_layout.h"
29#include "core/hle/kernel/k_memory_manager.h" 30#include "core/hle/kernel/k_memory_manager.h"
@@ -73,8 +74,16 @@ struct KernelCore::Impl {
73 InitializeMemoryLayout(); 74 InitializeMemoryLayout();
74 Init::InitializeKPageBufferSlabHeap(system); 75 Init::InitializeKPageBufferSlabHeap(system);
75 InitializeShutdownThreads(); 76 InitializeShutdownThreads();
76 InitializePreemption(kernel);
77 InitializePhysicalCores(); 77 InitializePhysicalCores();
78 InitializePreemption(kernel);
79
80 // Initialize the Dynamic Slab Heaps.
81 {
82 const auto& pt_heap_region = memory_layout->GetPageTableHeapRegion();
83 ASSERT(pt_heap_region.GetEndAddress() != 0);
84
85 InitializeResourceManagers(pt_heap_region.GetAddress(), pt_heap_region.GetSize());
86 }
78 87
79 RegisterHostThread(); 88 RegisterHostThread();
80 } 89 }
@@ -86,6 +95,15 @@ struct KernelCore::Impl {
86 } 95 }
87 } 96 }
88 97
98 void CloseCurrentProcess() {
99 (*current_process).Finalize();
100 // current_process->Close();
101 // TODO: The current process should be destroyed based on accurate ref counting after
102 // calling Close(). Adding a manual Destroy() call instead to avoid a memory leak.
103 (*current_process).Destroy();
104 current_process = nullptr;
105 }
106
89 void Shutdown() { 107 void Shutdown() {
90 is_shutting_down.store(true, std::memory_order_relaxed); 108 is_shutting_down.store(true, std::memory_order_relaxed);
91 SCOPE_EXIT({ is_shutting_down.store(false, std::memory_order_relaxed); }); 109 SCOPE_EXIT({ is_shutting_down.store(false, std::memory_order_relaxed); });
@@ -99,10 +117,6 @@ struct KernelCore::Impl {
99 next_user_process_id = KProcess::ProcessIDMin; 117 next_user_process_id = KProcess::ProcessIDMin;
100 next_thread_id = 1; 118 next_thread_id = 1;
101 119
102 for (auto& core : cores) {
103 core = nullptr;
104 }
105
106 global_handle_table->Finalize(); 120 global_handle_table->Finalize();
107 global_handle_table.reset(); 121 global_handle_table.reset();
108 122
@@ -152,15 +166,7 @@ struct KernelCore::Impl {
152 } 166 }
153 } 167 }
154 168
155 // Shutdown all processes. 169 CloseCurrentProcess();
156 if (current_process) {
157 (*current_process).Finalize();
158 // current_process->Close();
159 // TODO: The current process should be destroyed based on accurate ref counting after
160 // calling Close(). Adding a manual Destroy() call instead to avoid a memory leak.
161 (*current_process).Destroy();
162 current_process = nullptr;
163 }
164 170
165 // Track kernel objects that were not freed on shutdown 171 // Track kernel objects that were not freed on shutdown
166 { 172 {
@@ -257,6 +263,18 @@ struct KernelCore::Impl {
257 system.CoreTiming().ScheduleLoopingEvent(time_interval, time_interval, preemption_event); 263 system.CoreTiming().ScheduleLoopingEvent(time_interval, time_interval, preemption_event);
258 } 264 }
259 265
266 void InitializeResourceManagers(VAddr address, size_t size) {
267 dynamic_page_manager = std::make_unique<KDynamicPageManager>();
268 memory_block_heap = std::make_unique<KMemoryBlockSlabHeap>();
269 app_memory_block_manager = std::make_unique<KMemoryBlockSlabManager>();
270
271 dynamic_page_manager->Initialize(address, size);
272 static constexpr size_t ApplicationMemoryBlockSlabHeapSize = 20000;
273 memory_block_heap->Initialize(dynamic_page_manager.get(),
274 ApplicationMemoryBlockSlabHeapSize);
275 app_memory_block_manager->Initialize(nullptr, memory_block_heap.get());
276 }
277
260 void InitializeShutdownThreads() { 278 void InitializeShutdownThreads() {
261 for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { 279 for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) {
262 shutdown_threads[core_id] = KThread::Create(system.Kernel()); 280 shutdown_threads[core_id] = KThread::Create(system.Kernel());
@@ -344,11 +362,6 @@ struct KernelCore::Impl {
344 static inline thread_local KThread* current_thread{nullptr}; 362 static inline thread_local KThread* current_thread{nullptr};
345 363
346 KThread* GetCurrentEmuThread() { 364 KThread* GetCurrentEmuThread() {
347 // If we are shutting down the kernel, none of this is relevant anymore.
348 if (IsShuttingDown()) {
349 return {};
350 }
351
352 const auto thread_id = GetCurrentHostThreadID(); 365 const auto thread_id = GetCurrentHostThreadID();
353 if (thread_id >= Core::Hardware::NUM_CPU_CORES) { 366 if (thread_id >= Core::Hardware::NUM_CPU_CORES) {
354 return GetHostDummyThread(); 367 return GetHostDummyThread();
@@ -770,6 +783,11 @@ struct KernelCore::Impl {
770 // Kernel memory management 783 // Kernel memory management
771 std::unique_ptr<KMemoryManager> memory_manager; 784 std::unique_ptr<KMemoryManager> memory_manager;
772 785
786 // Dynamic slab managers
787 std::unique_ptr<KDynamicPageManager> dynamic_page_manager;
788 std::unique_ptr<KMemoryBlockSlabHeap> memory_block_heap;
789 std::unique_ptr<KMemoryBlockSlabManager> app_memory_block_manager;
790
773 // Shared memory for services 791 // Shared memory for services
774 Kernel::KSharedMemory* hid_shared_mem{}; 792 Kernel::KSharedMemory* hid_shared_mem{};
775 Kernel::KSharedMemory* font_shared_mem{}; 793 Kernel::KSharedMemory* font_shared_mem{};
@@ -853,6 +871,10 @@ const KProcess* KernelCore::CurrentProcess() const {
853 return impl->current_process; 871 return impl->current_process;
854} 872}
855 873
874void KernelCore::CloseCurrentProcess() {
875 impl->CloseCurrentProcess();
876}
877
856const std::vector<KProcess*>& KernelCore::GetProcessList() const { 878const std::vector<KProcess*>& KernelCore::GetProcessList() const {
857 return impl->process_list; 879 return impl->process_list;
858} 880}
@@ -1041,6 +1063,14 @@ const KMemoryManager& KernelCore::MemoryManager() const {
1041 return *impl->memory_manager; 1063 return *impl->memory_manager;
1042} 1064}
1043 1065
1066KMemoryBlockSlabManager& KernelCore::GetApplicationMemoryBlockManager() {
1067 return *impl->app_memory_block_manager;
1068}
1069
1070const KMemoryBlockSlabManager& KernelCore::GetApplicationMemoryBlockManager() const {
1071 return *impl->app_memory_block_manager;
1072}
1073
1044Kernel::KSharedMemory& KernelCore::GetHidSharedMem() { 1074Kernel::KSharedMemory& KernelCore::GetHidSharedMem() {
1045 return *impl->hid_shared_mem; 1075 return *impl->hid_shared_mem;
1046} 1076}