sceKernelAllocateDirectMemory should be ok for now

This commit is contained in:
georgemoralis 2023-08-01 17:59:08 +03:00
parent 770f743a49
commit dcac8c1661
5 changed files with 44 additions and 47 deletions

View File

@ -37,7 +37,7 @@ add_executable(shadps4
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/PhysicalMemory.h
src/Core/PS4/HLE/Kernel/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

@ -9,6 +9,45 @@
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)); }

View File

@ -1,44 +0,0 @@
#include "PhysicalMemory.h"
namespace HLE::Libs::LibKernel::MemoryManagement {
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& b : m_allocatedBlocks) {
u64 n = b.start_addr + b.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 b{};
b.size = len;
b.start_addr = find_free_pos;
b.memoryType = memoryType;
m_allocatedBlocks.push_back(b);
*physAddrOut = find_free_pos;
return true;
}
return false;
}
} // namespace HLE::Libs::LibKernel::MemoryManagement

View File

@ -21,6 +21,6 @@ class PhysicalMemory {
private: private:
std::vector<AllocatedBlock> m_allocatedBlocks; std::vector<AllocatedBlock> m_allocatedBlocks;
}; };
static PhysicalMemory* g_physical_memory = nullptr;
} // namespace HLE::Libs::LibKernel::MemoryManagement } // namespace HLE::Libs::LibKernel::MemoryManagement

View File

@ -30,6 +30,7 @@
#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[])
@ -41,6 +42,7 @@ 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();