Refactoring PhysicalMemory

This commit is contained in:
georgemoralis 2023-08-02 08:41:41 +03:00
parent 7bc4fda33e
commit a6e1cf43d7
6 changed files with 47 additions and 57 deletions

View File

@ -36,8 +36,8 @@ add_executable(shadps4
src/Core/PS4/Linker.h src/Core/PS4/Linker.h
src/Lib/Threads.cpp src/Lib/Threads.cpp
src/Lib/Threads.h 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") "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) find_package(OpenGL REQUIRED)

View File

@ -5,52 +5,11 @@
#include "../ErrorCodes.h" #include "../ErrorCodes.h"
#include "MemMngCodes.h" #include "MemMngCodes.h"
#include <bit> #include <bit>
#include "PhysicalMemory.h"
#include "../../../../Util/Singleton.h" #include "../../../../Util/Singleton.h"
#include "Objects/PhysicalMemory.h"
namespace HLE::Libs::LibKernel::MemoryManagement { 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 isPowerOfTwo(u64 n) { return std::popcount(n) == 1; }
bool is16KBAligned(u64 n) { return ((n % (16ull * 1024) == 0)); } 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); LOG_INFO_IF(true, "memory_type = {}\n", memoryType);
u64 physical_addr = 0; u64 physical_addr = 0;
auto* physical_memory = Singleton<HLE::Libs::LibKernel::MemoryManagement::PhysicalMemory>::Instance(); auto* physical_memory = Singleton<HLE::Kernel::Objects::PhysicalMemory>::Instance();
if (!physical_memory->Alloc(searchStart, searchEnd, len, alignment, &physical_addr, memoryType)) { if (!physical_memory->Alloc(searchStart, searchEnd, len, alignment, &physical_addr, memoryType)) {
//TODO debug logging //TODO debug logging
return SCE_KERNEL_ERROR_EAGAIN; return SCE_KERNEL_ERROR_EAGAIN;

View File

@ -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;
}
}

View File

@ -1,11 +1,8 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include "../../../../types.h" #include "../../../../../types.h"
namespace HLE::Libs::LibKernel::MemoryManagement { namespace HLE::Kernel::Objects {
void PhysicalMemoryInit();
class PhysicalMemory { class PhysicalMemory {
public: public:
@ -23,4 +20,4 @@ class PhysicalMemory {
}; };
} // namespace HLE::Libs::LibKernel::MemoryManagement }

View File

@ -4,15 +4,15 @@
#include "../../../Debug.h" #include "../../../Debug.h"
#include "../../../Util/Log.h" #include "../../../Util/Log.h"
#include "Kernel/MemoryManagement.h" #include "Kernel/MemoryManagement.h"
#include "Kernel/PhysicalMemory.h"
#include "../../../Util/Singleton.h" #include "../../../Util/Singleton.h"
#include "Kernel/Objects/PhysicalMemory.h"
namespace HLE::Libs::LibKernel { namespace HLE::Libs::LibKernel {
static u64 g_stack_chk_guard = 0xDEADBEEF54321ABC; //dummy return 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) { 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<HLE::Libs::LibKernel::MemoryManagement::PhysicalMemory>::Instance(); auto* physical_memory = Singleton<HLE::Kernel::Objects::PhysicalMemory>::Instance();
BREAKPOINT(); BREAKPOINT();
return 0; return 0;
} }

View File

@ -30,7 +30,6 @@
#include <Zydis/Zydis.h> #include <Zydis/Zydis.h>
#include "Core/PS4/HLE/Libs.h" #include "Core/PS4/HLE/Libs.h"
#include "Lib/Threads.h" #include "Lib/Threads.h"
#include "Core/PS4/HLE/Kernel/PhysicalMemory.h"
// Main code // Main code
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@ -42,7 +41,6 @@ int main(int argc, char* argv[])
logging::init(true);//init logging logging::init(true);//init logging
Lib::InitThreads(); 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<Linker>::Instance(); auto* linker = Singleton<Linker>::Instance();