From a6e1cf43d7ccebac3364691d45d46dec33f479b0 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Wed, 2 Aug 2023 08:41:41 +0300 Subject: [PATCH] Refactoring PhysicalMemory --- CMakeLists.txt | 4 +- src/Core/PS4/HLE/Kernel/MemoryManagement.cpp | 45 +------------------ .../PS4/HLE/Kernel/Objects/PhysicalMemory.cpp | 36 +++++++++++++++ .../HLE/Kernel/{ => Objects}/PhysicalMemory.h | 9 ++-- src/Core/PS4/HLE/LibKernel.cpp | 4 +- src/main.cpp | 6 +-- 6 files changed, 47 insertions(+), 57 deletions(-) create mode 100644 src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp rename src/Core/PS4/HLE/Kernel/{ => Objects}/PhysicalMemory.h (71%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4200bfe5..a254e801 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,8 +36,8 @@ add_executable(shadps4 src/Core/PS4/Linker.h src/Lib/Threads.cpp src/Lib/Threads.h - src/Core/PS4/HLE/Kernel/PhysicalMemory.h - + src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h + src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Util/StringUtil.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Lib/Timer.cpp" "src/Lib/Timer.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceVideoOut.cpp" "src/Core/PS4/HLE/LibSceVideoOut.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h" "src/Core/PS4/HLE/ErrorCodes.h" "src/Debug.h" "src/Core/PS4/HLE/Kernel/MemoryManagement.cpp" "src/Core/PS4/HLE/Kernel/MemoryManagement.h" "src/Core/PS4/HLE/Kernel/MemMngCodes.h" "src/Util/StringUtil.cpp") find_package(OpenGL REQUIRED) diff --git a/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp b/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp index 0975a986..40cbd847 100644 --- a/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp +++ b/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp @@ -5,52 +5,11 @@ #include "../ErrorCodes.h" #include "MemMngCodes.h" #include -#include "PhysicalMemory.h" #include "../../../../Util/Singleton.h" +#include "Objects/PhysicalMemory.h" namespace HLE::Libs::LibKernel::MemoryManagement { - -//static PhysicalMemory* g_physical_memory = nullptr; - -void PhysicalMemoryInit() { /* - g_physical_memory = new PhysicalMemory;*/ -} - -static u64 align_pos(u64 pos, u64 align) { return (align != 0 ? (pos + (align - 1)) & ~(align - 1) : pos); } - -bool PhysicalMemory::Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignment, u64* physAddrOut, int memoryType) { - - u64 find_free_pos = 0; - - //iterate through allocated blocked and find the next free position - if (!m_allocatedBlocks.empty()) { - for (const auto& block : m_allocatedBlocks) { - u64 n = block.start_addr + block.size; - if (n > find_free_pos) { - find_free_pos = n; - } - } - } - - //align free position - find_free_pos = align_pos(find_free_pos, alignment); - - //if the new position is between searchStart - searchEnd , allocate a new block - if (find_free_pos >= searchStart && find_free_pos + len <= searchEnd) { - AllocatedBlock block{}; - block.size = len; - block.start_addr = find_free_pos; - block.memoryType = memoryType; - - m_allocatedBlocks.push_back(block); - - *physAddrOut = find_free_pos; - return true; - } - - return false; -} bool isPowerOfTwo(u64 n) { return std::popcount(n) == 1; } bool is16KBAligned(u64 n) { return ((n % (16ull * 1024) == 0)); } @@ -88,7 +47,7 @@ int PS4_SYSV_ABI sceKernelAllocateDirectMemory(s64 searchStart, s64 searchEnd, u LOG_INFO_IF(true, "memory_type = {}\n", memoryType); u64 physical_addr = 0; - auto* physical_memory = Singleton::Instance(); + auto* physical_memory = Singleton::Instance(); if (!physical_memory->Alloc(searchStart, searchEnd, len, alignment, &physical_addr, memoryType)) { //TODO debug logging return SCE_KERNEL_ERROR_EAGAIN; diff --git a/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp b/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp new file mode 100644 index 00000000..05ab602c --- /dev/null +++ b/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.cpp @@ -0,0 +1,36 @@ +#include "PhysicalMemory.h" + +namespace HLE::Kernel::Objects { + +static u64 align_up(u64 pos, u64 align) { return (align != 0 ? (pos + (align - 1)) & ~(align - 1) : pos); } + +bool PhysicalMemory::Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignment, u64* physAddrOut, int memoryType) { + u64 find_free_pos = 0; + + // iterate through allocated blocked and find the next free position + for (const auto& block : m_allocatedBlocks) { + u64 n = block.start_addr + block.size; + if (n > find_free_pos) { + find_free_pos = n; + } + } + + // align free position + find_free_pos = align_up(find_free_pos, alignment); + + // if the new position is between searchStart - searchEnd , allocate a new block + if (find_free_pos >= searchStart && find_free_pos + len <= searchEnd) { + AllocatedBlock block{}; + block.size = len; + block.start_addr = find_free_pos; + block.memoryType = memoryType; + + m_allocatedBlocks.push_back(block); + + *physAddrOut = find_free_pos; + return true; + } + + return false; +} +} diff --git a/src/Core/PS4/HLE/Kernel/PhysicalMemory.h b/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h similarity index 71% rename from src/Core/PS4/HLE/Kernel/PhysicalMemory.h rename to src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h index a71e197d..480dec5b 100644 --- a/src/Core/PS4/HLE/Kernel/PhysicalMemory.h +++ b/src/Core/PS4/HLE/Kernel/Objects/PhysicalMemory.h @@ -1,11 +1,8 @@ #pragma once #include -#include "../../../../types.h" +#include "../../../../../types.h" -namespace HLE::Libs::LibKernel::MemoryManagement { - - -void PhysicalMemoryInit(); +namespace HLE::Kernel::Objects { class PhysicalMemory { public: @@ -23,4 +20,4 @@ class PhysicalMemory { }; -} // namespace HLE::Libs::LibKernel::MemoryManagement \ No newline at end of file +} \ No newline at end of file diff --git a/src/Core/PS4/HLE/LibKernel.cpp b/src/Core/PS4/HLE/LibKernel.cpp index 4395747e..9776d2e5 100644 --- a/src/Core/PS4/HLE/LibKernel.cpp +++ b/src/Core/PS4/HLE/LibKernel.cpp @@ -4,15 +4,15 @@ #include "../../../Debug.h" #include "../../../Util/Log.h" #include "Kernel/MemoryManagement.h" -#include "Kernel/PhysicalMemory.h" #include "../../../Util/Singleton.h" +#include "Kernel/Objects/PhysicalMemory.h" namespace HLE::Libs::LibKernel { static u64 g_stack_chk_guard = 0xDEADBEEF54321ABC; //dummy return int32_t PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, size_t len, int prot, int flags, off_t directMemoryStart, size_t alignment) { - auto* physical_memory = Singleton::Instance(); + auto* physical_memory = Singleton::Instance(); BREAKPOINT(); return 0; } diff --git a/src/main.cpp b/src/main.cpp index e8a167d9..64e5d845 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,20 +30,18 @@ #include #include "Core/PS4/HLE/Libs.h" #include "Lib/Threads.h" -#include "Core/PS4/HLE/Kernel/PhysicalMemory.h" // Main code int main(int argc, char* argv[]) { - if (argc == 1) { + if (argc == 1) { printf("Usage: %s \n", argv[0]); return -1; } logging::init(true);//init logging Lib::InitThreads(); - HLE::Libs::LibKernel::MemoryManagement::PhysicalMemoryInit(); - const char* const path = argv[1]; //argument 1 is the path of self file to boot + const char* const path = argv[1]; // argument 1 is the path of self file to boot auto* linker = Singleton::Instance(); HLE::Libs::Init_HLE_Libs(linker->getHLESymbols());