From 7d6653268f68dea8bc39288e3a27bc499b7b8154 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Tue, 13 Mar 2018 17:49:59 -0400
Subject: [PATCH] core: Move process creation out of global state.

---
 src/core/arm/dynarmic/arm_dynarmic.cpp        |  3 +-
 src/core/core.cpp                             |  4 +-
 src/core/core.h                               | 11 ++++++
 src/core/file_sys/savedata_factory.cpp        |  3 +-
 src/core/hle/kernel/handle_table.cpp          |  3 +-
 src/core/hle/kernel/kernel.cpp                |  1 -
 src/core/hle/kernel/process.cpp               |  5 +--
 src/core/hle/kernel/process.h                 |  3 +-
 src/core/hle/kernel/scheduler.cpp             |  7 ++--
 src/core/hle/kernel/server_session.cpp        |  3 +-
 src/core/hle/kernel/shared_memory.cpp         |  5 ++-
 src/core/hle/kernel/svc.cpp                   | 38 ++++++++++---------
 src/core/hle/kernel/thread.cpp                |  4 +-
 src/core/hle/service/ns/pl_u.cpp              |  9 +++--
 src/core/hle/service/service.cpp              |  4 +-
 .../loader/deconstructed_rom_directory.cpp    |  3 +-
 src/core/loader/elf.cpp                       |  1 -
 src/core/loader/nro.cpp                       |  5 +--
 src/core/loader/nso.cpp                       |  5 +--
 src/core/memory.cpp                           | 30 +++++++--------
 src/tests/core/arm/arm_test_common.cpp        |  4 +-
 src/tests/core/memory/memory.cpp              |  8 ++--
 22 files changed, 87 insertions(+), 72 deletions(-)

diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp
index e7f6bf8c2..4da07b177 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.cpp
+++ b/src/core/arm/dynarmic/arm_dynarmic.cpp
@@ -8,6 +8,7 @@
 #include <dynarmic/A64/config.h>
 #include "common/logging/log.h"
 #include "core/arm/dynarmic/arm_dynarmic.h"
+#include "core/core.h"
 #include "core/core_timing.h"
 #include "core/hle/kernel/memory.h"
 #include "core/hle/kernel/svc.h"
@@ -106,7 +107,7 @@ public:
 };
 
 std::unique_ptr<Dynarmic::A64::Jit> MakeJit(const std::unique_ptr<ARM_Dynarmic_Callbacks>& cb) {
-    const auto page_table = Kernel::g_current_process->vm_manager.page_table.pointers.data();
+    const auto page_table = Core::CurrentProcess()->vm_manager.page_table.pointers.data();
 
     Dynarmic::A64::UserConfig config;
     config.callbacks = cb.get();
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 4fb035556..ac6cb9e6e 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -100,7 +100,7 @@ System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& file
         return init_result;
     }
 
-    const Loader::ResultStatus load_result{app_loader->Load(Kernel::g_current_process)};
+    const Loader::ResultStatus load_result{app_loader->Load(current_process)};
     if (Loader::ResultStatus::Success != load_result) {
         LOG_CRITICAL(Core, "Failed to load ROM (Error %i)!", load_result);
         System::Shutdown();
@@ -141,6 +141,8 @@ System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
 
     CoreTiming::Init();
 
+    current_process = Kernel::Process::Create("main");
+
     switch (Settings::values.cpu_core) {
     case Settings::CpuCore::Unicorn:
         cpu_core = std::make_shared<ARM_Unicorn>();
diff --git a/src/core/core.h b/src/core/core.h
index ada23b347..635109b21 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -7,6 +7,7 @@
 #include <memory>
 #include <string>
 #include "common/common_types.h"
+#include "core/hle/kernel/kernel.h"
 #include "core/hle/kernel/scheduler.h"
 #include "core/loader/loader.h"
 #include "core/memory.h"
@@ -112,6 +113,10 @@ public:
         return *scheduler;
     }
 
+    Kernel::SharedPtr<Kernel::Process>& CurrentProcess() {
+        return current_process;
+    }
+
     PerfStats perf_stats;
     FrameLimiter frame_limiter;
 
@@ -149,6 +154,8 @@ private:
     std::unique_ptr<Kernel::Scheduler> scheduler;
     std::unique_ptr<Tegra::GPU> gpu_core;
 
+    Kernel::SharedPtr<Kernel::Process> current_process;
+
     /// When true, signals that a reschedule should happen
     bool reschedule_pending{};
 
@@ -169,4 +176,8 @@ inline TelemetrySession& Telemetry() {
     return System::GetInstance().TelemetrySession();
 }
 
+inline Kernel::SharedPtr<Kernel::Process>& CurrentProcess() {
+    return System::GetInstance().CurrentProcess();
+}
+
 } // namespace Core
diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp
index c3329ce52..14868fed2 100644
--- a/src/core/file_sys/savedata_factory.cpp
+++ b/src/core/file_sys/savedata_factory.cpp
@@ -7,6 +7,7 @@
 #include "common/common_types.h"
 #include "common/logging/log.h"
 #include "common/string_util.h"
+#include "core/core.h"
 #include "core/file_sys/disk_filesystem.h"
 #include "core/file_sys/savedata_factory.h"
 #include "core/hle/kernel/process.h"
@@ -46,7 +47,7 @@ ResultVal<ArchiveFormatInfo> SaveData_Factory::GetFormatInfo(const Path& path) c
 }
 
 std::string SaveData_Factory::GetFullPath() const {
-    u64 title_id = Kernel::g_current_process->program_id;
+    u64 title_id = Core::CurrentProcess()->program_id;
     // TODO(Subv): Somehow obtain this value.
     u32 user = 0;
     return Common::StringFromFormat("%ssave/%016" PRIX64 "/%08X/", nand_directory.c_str(), title_id,
diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp
index 3beb55753..822449cd5 100644
--- a/src/core/hle/kernel/handle_table.cpp
+++ b/src/core/hle/kernel/handle_table.cpp
@@ -5,6 +5,7 @@
 #include <utility>
 #include "common/assert.h"
 #include "common/logging/log.h"
+#include "core/core.h"
 #include "core/hle/kernel/errors.h"
 #include "core/hle/kernel/handle_table.h"
 #include "core/hle/kernel/kernel.h"
@@ -77,7 +78,7 @@ SharedPtr<Object> HandleTable::GetGeneric(Handle handle) const {
     if (handle == CurrentThread) {
         return GetCurrentThread();
     } else if (handle == CurrentProcess) {
-        return g_current_process;
+        return Core::CurrentProcess();
     }
 
     if (!IsValid(handle)) {
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index b0c3f4ae1..b325b879b 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -41,7 +41,6 @@ void Shutdown() {
     g_object_address_table.Clear();
 
     Kernel::ThreadingShutdown();
-    g_current_process = nullptr;
 
     Kernel::TimersShutdown();
     Kernel::ResourceLimitsShutdown();
diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp
index bb6dc28d7..9ca2374ea 100644
--- a/src/core/hle/kernel/process.cpp
+++ b/src/core/hle/kernel/process.cpp
@@ -31,14 +31,14 @@ CodeSet::~CodeSet() {}
 
 u32 Process::next_process_id;
 
-SharedPtr<Process> Process::Create(std::string&& name, u64 program_id) {
+SharedPtr<Process> Process::Create(std::string&& name) {
     SharedPtr<Process> process(new Process);
 
     process->name = std::move(name);
     process->flags.raw = 0;
     process->flags.memory_region.Assign(MemoryRegion::APPLICATION);
     process->status = ProcessStatus::Created;
-    process->program_id = program_id;
+    process->program_id = 0;
 
     process_list.push_back(process);
     return process;
@@ -319,5 +319,4 @@ SharedPtr<Process> GetProcessById(u32 process_id) {
     return *itr;
 }
 
-SharedPtr<Process> g_current_process;
 } // namespace Kernel
diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h
index 1de12efd3..68e77a4d1 100644
--- a/src/core/hle/kernel/process.h
+++ b/src/core/hle/kernel/process.h
@@ -95,7 +95,7 @@ private:
 
 class Process final : public Object {
 public:
-    static SharedPtr<Process> Create(std::string&& name, u64 program_id);
+    static SharedPtr<Process> Create(std::string&& name);
 
     std::string GetTypeName() const override {
         return "Process";
@@ -203,5 +203,4 @@ void ClearProcessList();
 /// Retrieves a process from the current list of processes.
 SharedPtr<Process> GetProcessById(u32 process_id);
 
-extern SharedPtr<Process> g_current_process;
 } // namespace Kernel
diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp
index 235068b22..921f27efb 100644
--- a/src/core/hle/kernel/scheduler.cpp
+++ b/src/core/hle/kernel/scheduler.cpp
@@ -2,6 +2,7 @@
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
+#include "core/core.h"
 #include "core/core_timing.h"
 #include "core/hle/kernel/process.h"
 #include "core/hle/kernel/scheduler.h"
@@ -67,7 +68,7 @@ void Scheduler::SwitchContext(Thread* new_thread) {
         // Cancel any outstanding wakeup events for this thread
         new_thread->CancelWakeupTimer();
 
-        auto previous_process = Kernel::g_current_process;
+        auto previous_process = Core::CurrentProcess();
 
         current_thread = new_thread;
 
@@ -75,8 +76,8 @@ void Scheduler::SwitchContext(Thread* new_thread) {
         new_thread->status = THREADSTATUS_RUNNING;
 
         if (previous_process != current_thread->owner_process) {
-            Kernel::g_current_process = current_thread->owner_process;
-            SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
+            Core::CurrentProcess() = current_thread->owner_process;
+            SetCurrentPageTable(&Core::CurrentProcess()->vm_manager.page_table);
         }
 
         cpu_core->LoadContext(new_thread->context);
diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/server_session.cpp
index 5f31cf19b..9b4a0ef0a 100644
--- a/src/core/hle/kernel/server_session.cpp
+++ b/src/core/hle/kernel/server_session.cpp
@@ -4,6 +4,7 @@
 
 #include <tuple>
 
+#include "core/core.h"
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/kernel/client_port.h"
 #include "core/hle/kernel/client_session.h"
@@ -91,7 +92,7 @@ ResultCode ServerSession::HandleSyncRequest(SharedPtr<Thread> thread) {
 
     Kernel::HLERequestContext context(this);
     u32* cmd_buf = (u32*)Memory::GetPointer(thread->GetTLSAddress());
-    context.PopulateFromIncomingCommandBuffer(cmd_buf, *Kernel::g_current_process,
+    context.PopulateFromIncomingCommandBuffer(cmd_buf, *Core::CurrentProcess(),
                                               Kernel::g_handle_table);
 
     ResultCode result = RESULT_SUCCESS;
diff --git a/src/core/hle/kernel/shared_memory.cpp b/src/core/hle/kernel/shared_memory.cpp
index d4505061e..4d6cd7462 100644
--- a/src/core/hle/kernel/shared_memory.cpp
+++ b/src/core/hle/kernel/shared_memory.cpp
@@ -4,6 +4,7 @@
 
 #include <cstring>
 #include "common/logging/log.h"
+#include "core/core.h"
 #include "core/hle/kernel/errors.h"
 #include "core/hle/kernel/memory.h"
 #include "core/hle/kernel/shared_memory.h"
@@ -51,8 +52,8 @@ SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u
         }
 
         // Refresh the address mappings for the current process.
-        if (Kernel::g_current_process != nullptr) {
-            Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
+        if (Core::CurrentProcess() != nullptr) {
+            Core::CurrentProcess()->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
         }
     } else {
         auto& vm_manager = shared_memory->owner_process->vm_manager;
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index 1ab8cbd88..207583320 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -8,6 +8,7 @@
 #include "common/logging/log.h"
 #include "common/microprofile.h"
 #include "common/string_util.h"
+#include "core/core.h"
 #include "core/core_timing.h"
 #include "core/hle/kernel/client_port.h"
 #include "core/hle/kernel/client_session.h"
@@ -31,7 +32,7 @@ namespace Kernel {
 /// Set the process heap to a given Size. It can both extend and shrink the heap.
 static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
     LOG_TRACE(Kernel_SVC, "called, heap_size=0x%llx", heap_size);
-    auto& process = *g_current_process;
+    auto& process = *Core::CurrentProcess();
     CASCADE_RESULT(*heap_addr,
                    process.HeapAllocate(Memory::HEAP_VADDR, heap_size, VMAPermission::ReadWrite));
     return RESULT_SUCCESS;
@@ -46,14 +47,14 @@ static ResultCode SetMemoryAttribute(VAddr addr, u64 size, u32 state0, u32 state
 static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
     LOG_TRACE(Kernel_SVC, "called, dst_addr=0x%llx, src_addr=0x%llx, size=0x%llx", dst_addr,
               src_addr, size);
-    return g_current_process->MirrorMemory(dst_addr, src_addr, size);
+    return Core::CurrentProcess()->MirrorMemory(dst_addr, src_addr, size);
 }
 
 /// Unmaps a region that was previously mapped with svcMapMemory
 static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
     LOG_TRACE(Kernel_SVC, "called, dst_addr=0x%llx, src_addr=0x%llx, size=0x%llx", dst_addr,
               src_addr, size);
-    return g_current_process->UnmapMemory(dst_addr, src_addr, size);
+    return Core::CurrentProcess()->UnmapMemory(dst_addr, src_addr, size);
 }
 
 /// Connect to an OS service given the port name, returns the handle to the port to out
@@ -306,14 +307,14 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id)
     LOG_TRACE(Kernel_SVC, "called info_id=0x%X, info_sub_id=0x%X, handle=0x%08X", info_id,
               info_sub_id, handle);
 
-    auto& vm_manager = g_current_process->vm_manager;
+    auto& vm_manager = Core::CurrentProcess()->vm_manager;
 
     switch (static_cast<GetInfoType>(info_id)) {
     case GetInfoType::AllowedCpuIdBitmask:
-        *result = g_current_process->allowed_processor_mask;
+        *result = Core::CurrentProcess()->allowed_processor_mask;
         break;
     case GetInfoType::AllowedThreadPrioBitmask:
-        *result = g_current_process->allowed_thread_priority_mask;
+        *result = Core::CurrentProcess()->allowed_thread_priority_mask;
         break;
     case GetInfoType::MapRegionBaseAddr:
         *result = vm_manager.GetMapRegionBaseAddr();
@@ -352,7 +353,7 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id)
         *result = vm_manager.GetNewMapRegionSize();
         break;
     case GetInfoType::IsVirtualAddressMemoryEnabled:
-        *result = g_current_process->is_virtual_address_memory_enabled;
+        *result = Core::CurrentProcess()->is_virtual_address_memory_enabled;
         break;
     case GetInfoType::TitleId:
         LOG_WARNING(Kernel_SVC, "(STUBBED) Attempted to query titleid, returned 0");
@@ -392,7 +393,7 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) {
 
     // Note: The kernel uses the current process's resource limit instead of
     // the one from the thread owner's resource limit.
-    SharedPtr<ResourceLimit>& resource_limit = g_current_process->resource_limit;
+    SharedPtr<ResourceLimit>& resource_limit = Core::CurrentProcess()->resource_limit;
     if (resource_limit->GetMaxResourceValue(ResourceTypes::PRIORITY) > priority) {
         return ERR_NOT_AUTHORIZED;
     }
@@ -435,7 +436,7 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s
     case MemoryPermission::WriteExecute:
     case MemoryPermission::ReadWriteExecute:
     case MemoryPermission::DontCare:
-        return shared_memory->Map(g_current_process.get(), addr, permissions_type,
+        return shared_memory->Map(Core::CurrentProcess().get(), addr, permissions_type,
                                   MemoryPermission::DontCare);
     default:
         LOG_ERROR(Kernel_SVC, "unknown permissions=0x%08X", permissions);
@@ -451,7 +452,7 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64
 
     SharedPtr<SharedMemory> shared_memory = g_handle_table.Get<SharedMemory>(shared_memory_handle);
 
-    return shared_memory->Unmap(g_current_process.get(), addr);
+    return shared_memory->Unmap(Core::CurrentProcess().get(), addr);
 }
 
 /// Query process memory
@@ -463,7 +464,7 @@ static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_i
     }
     auto vma = process->vm_manager.FindVMA(addr);
     memory_info->attributes = 0;
-    if (vma == g_current_process->vm_manager.vma_map.end()) {
+    if (vma == Core::CurrentProcess()->vm_manager.vma_map.end()) {
         memory_info->base_address = 0;
         memory_info->permission = static_cast<u32>(VMAPermission::None);
         memory_info->size = 0;
@@ -487,16 +488,17 @@ static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, VAdd
 
 /// Exits the current process
 static void ExitProcess() {
-    LOG_INFO(Kernel_SVC, "Process %u exiting", g_current_process->process_id);
+    LOG_INFO(Kernel_SVC, "Process %u exiting", Core::CurrentProcess()->process_id);
 
-    ASSERT_MSG(g_current_process->status == ProcessStatus::Running, "Process has already exited");
+    ASSERT_MSG(Core::CurrentProcess()->status == ProcessStatus::Running,
+               "Process has already exited");
 
-    g_current_process->status = ProcessStatus::Exited;
+    Core::CurrentProcess()->status = ProcessStatus::Exited;
 
     // Stop all the process threads that are currently waiting for objects.
     auto& thread_list = Core::System::GetInstance().Scheduler().GetThreadList();
     for (auto& thread : thread_list) {
-        if (thread->owner_process != g_current_process)
+        if (thread->owner_process != Core::CurrentProcess())
             continue;
 
         if (thread == GetCurrentThread())
@@ -525,14 +527,14 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V
         return ERR_OUT_OF_RANGE;
     }
 
-    SharedPtr<ResourceLimit>& resource_limit = g_current_process->resource_limit;
+    SharedPtr<ResourceLimit>& resource_limit = Core::CurrentProcess()->resource_limit;
     if (resource_limit->GetMaxResourceValue(ResourceTypes::PRIORITY) > priority) {
         return ERR_NOT_AUTHORIZED;
     }
 
     if (processor_id == THREADPROCESSORID_DEFAULT) {
         // Set the target CPU to the one specified in the process' exheader.
-        processor_id = g_current_process->ideal_processor;
+        processor_id = Core::CurrentProcess()->ideal_processor;
         ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
     }
 
@@ -554,7 +556,7 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V
 
     CASCADE_RESULT(SharedPtr<Thread> thread,
                    Thread::Create(name, entry_point, priority, arg, processor_id, stack_top,
-                                  g_current_process));
+                                  Core::CurrentProcess()));
     CASCADE_RESULT(thread->guest_handle, g_handle_table.Create(thread));
     *out_handle = thread->guest_handle;
 
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 25828777e..2394620eb 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -94,7 +94,7 @@ void Thread::Stop() {
     u64 tls_page = (tls_address - Memory::TLS_AREA_VADDR) / Memory::PAGE_SIZE;
     u64 tls_slot =
         ((tls_address - Memory::TLS_AREA_VADDR) % Memory::PAGE_SIZE) / Memory::TLS_ENTRY_SIZE;
-    Kernel::g_current_process->tls_slots[tls_page].reset(tls_slot);
+    Core::CurrentProcess()->tls_slots[tls_page].reset(tls_slot);
 }
 
 void WaitCurrentThread_Sleep() {
@@ -353,7 +353,7 @@ void Thread::BoostPriority(u32 priority) {
 SharedPtr<Thread> SetupMainThread(VAddr entry_point, u32 priority,
                                   SharedPtr<Process> owner_process) {
     // Setup page table so we can write to memory
-    SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
+    SetCurrentPageTable(&Core::CurrentProcess()->vm_manager.page_table);
 
     // Initialize new "main" thread
     auto thread_res = Thread::Create("main", entry_point, priority, 0, THREADPROCESSORID_0,
diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp
index cc9d03a7c..695e295ca 100644
--- a/src/core/hle/service/ns/pl_u.cpp
+++ b/src/core/hle/service/ns/pl_u.cpp
@@ -4,6 +4,7 @@
 
 #include "common/common_paths.h"
 #include "common/file_util.h"
+#include "core/core.h"
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/service/ns/pl_u.h"
 
@@ -90,13 +91,13 @@ void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
         // dump. In the future, we need to replace this with a more robust solution.
 
         // Map backing memory for the font data
-        Kernel::g_current_process->vm_manager.MapMemoryBlock(SHARED_FONT_MEM_VADDR, shared_font, 0,
-                                                             SHARED_FONT_MEM_SIZE,
-                                                             Kernel::MemoryState::Shared);
+        Core::CurrentProcess()->vm_manager.MapMemoryBlock(SHARED_FONT_MEM_VADDR, shared_font, 0,
+                                                          SHARED_FONT_MEM_SIZE,
+                                                          Kernel::MemoryState::Shared);
 
         // Create shared font memory object
         shared_font_mem = Kernel::SharedMemory::Create(
-            Kernel::g_current_process, SHARED_FONT_MEM_SIZE, Kernel::MemoryPermission::ReadWrite,
+            Core::CurrentProcess(), SHARED_FONT_MEM_SIZE, Kernel::MemoryPermission::ReadWrite,
             Kernel::MemoryPermission::Read, SHARED_FONT_MEM_VADDR, Kernel::MemoryRegion::BASE,
             "PL_U:shared_font_mem");
     }
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index 78380475c..8818b0f0f 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -7,6 +7,7 @@
 #include "common/assert.h"
 #include "common/logging/log.h"
 #include "common/string_util.h"
+#include "core/core.h"
 #include "core/hle/ipc.h"
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/kernel/client_port.h"
@@ -152,8 +153,7 @@ ResultCode ServiceFrameworkBase::HandleSyncRequest(Kernel::HLERequestContext& co
     }
 
     u32* cmd_buf = (u32*)Memory::GetPointer(Kernel::GetCurrentThread()->GetTLSAddress());
-    context.WriteToOutgoingCommandBuffer(cmd_buf, *Kernel::g_current_process,
-                                         Kernel::g_handle_table);
+    context.WriteToOutgoingCommandBuffer(cmd_buf, *Core::CurrentProcess(), Kernel::g_handle_table);
 
     return RESULT_SUCCESS;
 }
diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp
index 459d127c2..aa09ed323 100644
--- a/src/core/loader/deconstructed_rom_directory.cpp
+++ b/src/core/loader/deconstructed_rom_directory.cpp
@@ -119,8 +119,6 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(
     }
     metadata.Print();
 
-    process = Kernel::Process::Create("main", metadata.GetTitleID());
-
     // Load NSO modules
     VAddr next_load_addr{Memory::PROCESS_IMAGE_VADDR};
     for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3",
@@ -135,6 +133,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(
         }
     }
 
+    process->program_id = metadata.GetTitleID();
     process->svc_access_mask.set();
     process->address_mappings = default_address_mappings;
     process->resource_limit =
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index cdd41f237..164d52258 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -406,7 +406,6 @@ ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) {
     SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
     codeset->name = filename;
 
-    process = Kernel::Process::Create("main", 0);
     process->LoadModule(codeset, codeset->entrypoint);
     process->svc_access_mask.set();
     process->address_mappings = default_address_mappings;
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index c557b66dc..0dc06ccea 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -8,6 +8,7 @@
 #include "common/file_util.h"
 #include "common/logging/log.h"
 #include "common/swap.h"
+#include "core/core.h"
 #include "core/hle/kernel/process.h"
 #include "core/hle/kernel/resource_limit.h"
 #include "core/loader/nro.h"
@@ -112,7 +113,7 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
     // Load codeset for current process
     codeset->name = path;
     codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
-    Kernel::g_current_process->LoadModule(codeset, load_base);
+    Core::CurrentProcess()->LoadModule(codeset, load_base);
 
     return true;
 }
@@ -125,8 +126,6 @@ ResultStatus AppLoader_NRO::Load(Kernel::SharedPtr<Kernel::Process>& process) {
         return ResultStatus::Error;
     }
 
-    process = Kernel::Process::Create("main", 0);
-
     // Load NRO
     static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR};
 
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index 00b5d1d49..c0eeb95d3 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -9,6 +9,7 @@
 #include "common/file_util.h"
 #include "common/logging/log.h"
 #include "common/swap.h"
+#include "core/core.h"
 #include "core/hle/kernel/process.h"
 #include "core/hle/kernel/resource_limit.h"
 #include "core/loader/nso.h"
@@ -142,7 +143,7 @@ VAddr AppLoader_NSO::LoadModule(const std::string& path, VAddr load_base) {
     // Load codeset for current process
     codeset->name = path;
     codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
-    Kernel::g_current_process->LoadModule(codeset, load_base);
+    Core::CurrentProcess()->LoadModule(codeset, load_base);
 
     return load_base + image_size;
 }
@@ -155,8 +156,6 @@ ResultStatus AppLoader_NSO::Load(Kernel::SharedPtr<Kernel::Process>& process) {
         return ResultStatus::Error;
     }
 
-    process = Kernel::Process::Create("main", 0);
-
     // Load module
     LoadModule(filepath, Memory::PROCESS_IMAGE_VADDR);
     LOG_DEBUG(Loader, "loaded module %s @ 0x%" PRIx64, filepath.c_str(),
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index ce62666d7..4e34d8334 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -109,7 +109,7 @@ static std::set<MemoryHookPointer> GetSpecialHandlers(const PageTable& page_tabl
 }
 
 static std::set<MemoryHookPointer> GetSpecialHandlers(VAddr vaddr, u64 size) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     return GetSpecialHandlers(page_table, vaddr, size);
 }
 
@@ -202,7 +202,7 @@ bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) {
 }
 
 bool IsValidVirtualAddress(const VAddr vaddr) {
-    return IsValidVirtualAddress(*Kernel::g_current_process, vaddr);
+    return IsValidVirtualAddress(*Core::CurrentProcess(), vaddr);
 }
 
 bool IsValidPhysicalAddress(const PAddr paddr) {
@@ -364,7 +364,7 @@ void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_
 }
 
 void ReadBlock(const VAddr src_addr, void* dest_buffer, const size_t size) {
-    ReadBlock(*Kernel::g_current_process, src_addr, dest_buffer, size);
+    ReadBlock(*Core::CurrentProcess(), src_addr, dest_buffer, size);
 }
 
 void Write8(const VAddr addr, const u8 data) {
@@ -435,11 +435,11 @@ void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const voi
 }
 
 void WriteBlock(const VAddr dest_addr, const void* src_buffer, const size_t size) {
-    WriteBlock(*Kernel::g_current_process, dest_addr, src_buffer, size);
+    WriteBlock(*Core::CurrentProcess(), dest_addr, src_buffer, size);
 }
 
 void ZeroBlock(const VAddr dest_addr, const size_t size) {
-    const auto& process = *Kernel::g_current_process;
+    const auto& process = *Core::CurrentProcess();
 
     size_t remaining_size = size;
     size_t page_index = dest_addr >> PAGE_BITS;
@@ -480,7 +480,7 @@ void ZeroBlock(const VAddr dest_addr, const size_t size) {
 }
 
 void CopyBlock(VAddr dest_addr, VAddr src_addr, const size_t size) {
-    const auto& process = *Kernel::g_current_process;
+    const auto& process = *Core::CurrentProcess();
 
     size_t remaining_size = size;
     size_t page_index = src_addr >> PAGE_BITS;
@@ -526,7 +526,7 @@ void CopyBlock(VAddr dest_addr, VAddr src_addr, const size_t size) {
 
 template <>
 boost::optional<u8> ReadSpecial<u8>(VAddr addr) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u8)))
         if (auto result = handler->Read8(addr))
             return *result;
@@ -535,7 +535,7 @@ boost::optional<u8> ReadSpecial<u8>(VAddr addr) {
 
 template <>
 boost::optional<u16> ReadSpecial<u16>(VAddr addr) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u16)))
         if (auto result = handler->Read16(addr))
             return *result;
@@ -544,7 +544,7 @@ boost::optional<u16> ReadSpecial<u16>(VAddr addr) {
 
 template <>
 boost::optional<u32> ReadSpecial<u32>(VAddr addr) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u32)))
         if (auto result = handler->Read32(addr))
             return *result;
@@ -553,7 +553,7 @@ boost::optional<u32> ReadSpecial<u32>(VAddr addr) {
 
 template <>
 boost::optional<u64> ReadSpecial<u64>(VAddr addr) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u64)))
         if (auto result = handler->Read64(addr))
             return *result;
@@ -562,7 +562,7 @@ boost::optional<u64> ReadSpecial<u64>(VAddr addr) {
 
 template <>
 bool WriteSpecial<u8>(VAddr addr, const u8 data) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u8)))
         if (handler->Write8(addr, data))
             return true;
@@ -571,7 +571,7 @@ bool WriteSpecial<u8>(VAddr addr, const u8 data) {
 
 template <>
 bool WriteSpecial<u16>(VAddr addr, const u16 data) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u16)))
         if (handler->Write16(addr, data))
             return true;
@@ -580,7 +580,7 @@ bool WriteSpecial<u16>(VAddr addr, const u16 data) {
 
 template <>
 bool WriteSpecial<u32>(VAddr addr, const u32 data) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u32)))
         if (handler->Write32(addr, data))
             return true;
@@ -589,7 +589,7 @@ bool WriteSpecial<u32>(VAddr addr, const u32 data) {
 
 template <>
 bool WriteSpecial<u64>(VAddr addr, const u64 data) {
-    const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
+    const PageTable& page_table = Core::CurrentProcess()->vm_manager.page_table;
     for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u64)))
         if (handler->Write64(addr, data))
             return true;
@@ -632,7 +632,7 @@ boost::optional<VAddr> PhysicalToVirtualAddress(const PAddr addr) {
     } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
         return addr - VRAM_PADDR + VRAM_VADDR;
     } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
-        return addr - FCRAM_PADDR + Kernel::g_current_process->GetLinearHeapAreaAddress();
+        return addr - FCRAM_PADDR + Core::CurrentProcess()->GetLinearHeapAreaAddress();
     } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
         return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
     } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp
index 9296e1e94..7f9f27e19 100644
--- a/src/tests/core/arm/arm_test_common.cpp
+++ b/src/tests/core/arm/arm_test_common.cpp
@@ -15,8 +15,8 @@ static Memory::PageTable* page_table = nullptr;
 TestEnvironment::TestEnvironment(bool mutable_memory_)
     : mutable_memory(mutable_memory_), test_memory(std::make_shared<TestMemory>(this)) {
 
-    Kernel::g_current_process = Kernel::Process::Create("", 0);
-    page_table = &Kernel::g_current_process->vm_manager.page_table;
+    Core::CurrentProcess() = Kernel::Process::Create("");
+    page_table = &Core::CurrentProcess()->vm_manager.page_table;
 
     page_table->pointers.fill(nullptr);
     page_table->special_regions.clear();
diff --git a/src/tests/core/memory/memory.cpp b/src/tests/core/memory/memory.cpp
index 0e0a43dcb..165496a54 100644
--- a/src/tests/core/memory/memory.cpp
+++ b/src/tests/core/memory/memory.cpp
@@ -9,7 +9,7 @@
 
 TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory][!hide]") {
     SECTION("these regions should not be mapped on an empty process") {
-        auto process = Kernel::Process::Create("", 0);
+        auto process = Kernel::Process::Create("");
         CHECK(Memory::IsValidVirtualAddress(*process, Memory::PROCESS_IMAGE_VADDR) == false);
         CHECK(Memory::IsValidVirtualAddress(*process, Memory::HEAP_VADDR) == false);
         CHECK(Memory::IsValidVirtualAddress(*process, Memory::LINEAR_HEAP_VADDR) == false);
@@ -20,14 +20,14 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory][!hide]") {
     }
 
     SECTION("CONFIG_MEMORY_VADDR and SHARED_PAGE_VADDR should be valid after mapping them") {
-        auto process = Kernel::Process::Create("", 0);
+        auto process = Kernel::Process::Create("");
         Kernel::MapSharedPages(process->vm_manager);
         CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == true);
         CHECK(Memory::IsValidVirtualAddress(*process, Memory::SHARED_PAGE_VADDR) == true);
     }
 
     SECTION("special regions should be valid after mapping them") {
-        auto process = Kernel::Process::Create("", 0);
+        auto process = Kernel::Process::Create("");
         SECTION("VRAM") {
             Kernel::HandleSpecialMapping(process->vm_manager,
                                          {Memory::VRAM_VADDR, Memory::VRAM_SIZE, false, false});
@@ -48,7 +48,7 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory][!hide]") {
     }
 
     SECTION("Unmapping a VAddr should make it invalid") {
-        auto process = Kernel::Process::Create("", 0);
+        auto process = Kernel::Process::Create("");
         Kernel::MapSharedPages(process->vm_manager);
         process->vm_manager.UnmapRange(Memory::CONFIG_MEMORY_VADDR, Memory::CONFIG_MEMORY_SIZE);
         CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == false);