From acfc801d14b1883f54b8fe66ac428982f9898258 Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Fri, 21 Sep 2018 01:26:29 -0400
Subject: [PATCH] thread/process: Move TLS slot marking/freeing to the process
 class

Allows making several members of the process class private, it also
avoids going through Core::CurrentProcess() just to retrieve the owning
process.
---
 src/core/hle/kernel/process.cpp | 58 +++++++++++++++++++++++++++++++++
 src/core/hle/kernel/process.h   | 28 +++++++++-------
 src/core/hle/kernel/thread.cpp  | 58 ++-------------------------------
 src/core/hle/kernel/thread.h    | 13 +++++++-
 4 files changed, 89 insertions(+), 68 deletions(-)

diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp
index 914bbe0a17..0c8ea94fcc 100644
--- a/src/core/hle/kernel/process.cpp
+++ b/src/core/hle/kernel/process.cpp
@@ -128,6 +128,64 @@ void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) {
     Kernel::SetupMainThread(kernel, entry_point, main_thread_priority, *this);
 }
 
+/**
+ * Finds a free location for the TLS section of a thread.
+ * @param tls_slots The TLS page array of the thread's owner process.
+ * Returns a tuple of (page, slot, alloc_needed) where:
+ * page: The index of the first allocated TLS page that has free slots.
+ * slot: The index of the first free slot in the indicated page.
+ * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full).
+ */
+static std::tuple<std::size_t, std::size_t, bool> FindFreeThreadLocalSlot(
+    const std::vector<std::bitset<8>>& tls_slots) {
+    // Iterate over all the allocated pages, and try to find one where not all slots are used.
+    for (std::size_t page = 0; page < tls_slots.size(); ++page) {
+        const auto& page_tls_slots = tls_slots[page];
+        if (!page_tls_slots.all()) {
+            // We found a page with at least one free slot, find which slot it is
+            for (std::size_t slot = 0; slot < page_tls_slots.size(); ++slot) {
+                if (!page_tls_slots.test(slot)) {
+                    return std::make_tuple(page, slot, false);
+                }
+            }
+        }
+    }
+
+    return std::make_tuple(0, 0, true);
+}
+
+VAddr Process::MarkNextAvailableTLSSlotAsUsed(Thread& thread) {
+    auto [available_page, available_slot, needs_allocation] = FindFreeThreadLocalSlot(tls_slots);
+
+    if (needs_allocation) {
+        tls_slots.emplace_back(0); // The page is completely available at the start
+        available_page = tls_slots.size() - 1;
+        available_slot = 0; // Use the first slot in the new page
+
+        // Allocate some memory from the end of the linear heap for this region.
+        auto& tls_memory = thread.GetTLSMemory();
+        tls_memory->insert(tls_memory->end(), Memory::PAGE_SIZE, 0);
+
+        vm_manager.RefreshMemoryBlockMappings(tls_memory.get());
+
+        vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE,
+                                  tls_memory, 0, Memory::PAGE_SIZE, MemoryState::ThreadLocal);
+    }
+
+    tls_slots[available_page].set(available_slot);
+
+    return Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE +
+           available_slot * Memory::TLS_ENTRY_SIZE;
+}
+
+void Process::FreeTLSSlot(VAddr tls_address) {
+    const VAddr tls_base = tls_address - Memory::TLS_AREA_VADDR;
+    const VAddr tls_page = tls_base / Memory::PAGE_SIZE;
+    const VAddr tls_slot = (tls_base % Memory::PAGE_SIZE) / Memory::TLS_ENTRY_SIZE;
+
+    tls_slots[tls_page].reset(tls_slot);
+}
+
 void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) {
     const auto MapSegment = [&](CodeSet::Segment& segment, VMAPermission permissions,
                                 MemoryState memory_state) {
diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h
index 81538f70c6..84027a31a9 100644
--- a/src/core/hle/kernel/process.h
+++ b/src/core/hle/kernel/process.h
@@ -176,8 +176,25 @@ public:
     ///////////////////////////////////////////////////////////////////////////////////////////////
     // Memory Management
 
+    // Marks the next available region as used and returns the address of the slot.
+    VAddr MarkNextAvailableTLSSlotAsUsed(Thread& thread);
+
+    // Frees a used TLS slot identified by the given address
+    void FreeTLSSlot(VAddr tls_address);
+
+    ResultVal<VAddr> HeapAllocate(VAddr target, u64 size, VMAPermission perms);
+    ResultCode HeapFree(VAddr target, u32 size);
+
+    ResultCode MirrorMemory(VAddr dst_addr, VAddr src_addr, u64 size);
+
+    ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size);
+
     VMManager vm_manager;
 
+private:
+    explicit Process(KernelCore& kernel);
+    ~Process() override;
+
     // Memory used to back the allocations in the regular heap. A single vector is used to cover
     // the entire virtual address space extents that bound the allocations, including any holes.
     // This makes deallocation and reallocation of holes fast and keeps process memory contiguous
@@ -197,17 +214,6 @@ public:
     std::vector<std::bitset<8>> tls_slots;
 
     std::string name;
-
-    ResultVal<VAddr> HeapAllocate(VAddr target, u64 size, VMAPermission perms);
-    ResultCode HeapFree(VAddr target, u32 size);
-
-    ResultCode MirrorMemory(VAddr dst_addr, VAddr src_addr, u64 size);
-
-    ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size);
-
-private:
-    explicit Process(KernelCore& kernel);
-    ~Process() override;
 };
 
 } // namespace Kernel
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index c2d7535c92..315f653385 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -65,10 +65,7 @@ void Thread::Stop() {
     wait_objects.clear();
 
     // Mark the TLS slot in the thread's page as free.
-    const u64 tls_page = (tls_address - Memory::TLS_AREA_VADDR) / Memory::PAGE_SIZE;
-    const u64 tls_slot =
-        ((tls_address - Memory::TLS_AREA_VADDR) % Memory::PAGE_SIZE) / Memory::TLS_ENTRY_SIZE;
-    Core::CurrentProcess()->tls_slots[tls_page].reset(tls_slot);
+    owner_process->FreeTLSSlot(tls_address);
 }
 
 void WaitCurrentThread_Sleep() {
@@ -177,32 +174,6 @@ void Thread::ResumeFromWait() {
     Core::System::GetInstance().CpuCore(processor_id).PrepareReschedule();
 }
 
-/**
- * Finds a free location for the TLS section of a thread.
- * @param tls_slots The TLS page array of the thread's owner process.
- * Returns a tuple of (page, slot, alloc_needed) where:
- * page: The index of the first allocated TLS page that has free slots.
- * slot: The index of the first free slot in the indicated page.
- * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full).
- */
-static std::tuple<std::size_t, std::size_t, bool> GetFreeThreadLocalSlot(
-    const std::vector<std::bitset<8>>& tls_slots) {
-    // Iterate over all the allocated pages, and try to find one where not all slots are used.
-    for (std::size_t page = 0; page < tls_slots.size(); ++page) {
-        const auto& page_tls_slots = tls_slots[page];
-        if (!page_tls_slots.all()) {
-            // We found a page with at least one free slot, find which slot it is
-            for (std::size_t slot = 0; slot < page_tls_slots.size(); ++slot) {
-                if (!page_tls_slots.test(slot)) {
-                    return std::make_tuple(page, slot, false);
-                }
-            }
-        }
-    }
-
-    return std::make_tuple(0, 0, true);
-}
-
 /**
  * Resets a thread context, making it ready to be scheduled and run by the CPU
  * @param context Thread context to reset
@@ -264,32 +235,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(KernelCore& kernel, std::string name
     thread->owner_process = owner_process;
     thread->scheduler = Core::System::GetInstance().Scheduler(processor_id);
     thread->scheduler->AddThread(thread, priority);
-
-    // Find the next available TLS index, and mark it as used
-    auto& tls_slots = owner_process->tls_slots;
-
-    auto [available_page, available_slot, needs_allocation] = GetFreeThreadLocalSlot(tls_slots);
-    if (needs_allocation) {
-        tls_slots.emplace_back(0); // The page is completely available at the start
-        available_page = tls_slots.size() - 1;
-        available_slot = 0; // Use the first slot in the new page
-
-        // Allocate some memory from the end of the linear heap for this region.
-        const std::size_t offset = thread->tls_memory->size();
-        thread->tls_memory->insert(thread->tls_memory->end(), Memory::PAGE_SIZE, 0);
-
-        auto& vm_manager = owner_process->vm_manager;
-        vm_manager.RefreshMemoryBlockMappings(thread->tls_memory.get());
-
-        vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE,
-                                  thread->tls_memory, 0, Memory::PAGE_SIZE,
-                                  MemoryState::ThreadLocal);
-    }
-
-    // Mark the slot as used
-    tls_slots[available_page].set(available_slot);
-    thread->tls_address = Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE +
-                          available_slot * Memory::TLS_ENTRY_SIZE;
+    thread->tls_address = thread->owner_process->MarkNextAvailableTLSSlotAsUsed(*thread);
 
     // TODO(peachum): move to ScheduleThread() when scheduler is added so selected core is used
     // to initialize the context
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index 91e9b79ecb..4250144c31 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -62,6 +62,9 @@ enum class ThreadWakeupReason {
 
 class Thread final : public WaitObject {
 public:
+    using TLSMemory = std::vector<u8>;
+    using TLSMemoryPtr = std::shared_ptr<TLSMemory>;
+
     /**
      * Creates and returns a new thread. The new thread is immediately scheduled
      * @param kernel The kernel instance this thread will be created under.
@@ -134,6 +137,14 @@ public:
         return thread_id;
     }
 
+    TLSMemoryPtr& GetTLSMemory() {
+        return tls_memory;
+    }
+
+    const TLSMemoryPtr& GetTLSMemory() const {
+        return tls_memory;
+    }
+
     /**
      * Resumes a thread from waiting
      */
@@ -269,7 +280,7 @@ private:
     explicit Thread(KernelCore& kernel);
     ~Thread() override;
 
-    std::shared_ptr<std::vector<u8>> tls_memory = std::make_shared<std::vector<u8>>();
+    TLSMemoryPtr tls_memory = std::make_shared<TLSMemory>();
 };
 
 /**