diff --git a/src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp index 7091b8fe..50f60f3f 100644 --- a/src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp +++ b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp @@ -5,6 +5,7 @@ namespace HLE::Kernel::Objects { static u64 AlignUp(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) { + Lib::LockMutexGuard lock(m_mutex); u64 find_free_pos = 0; // iterate through allocated blocked and find the next free position @@ -39,6 +40,7 @@ bool PhysicalMemory::Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignmen return false; } bool PhysicalMemory::Map(u64 virtual_addr, u64 phys_addr, u64 len, int prot, VirtualMemory::MemoryMode cpu_mode, GPU::MemoryMode gpu_mode) { + Lib::LockMutexGuard lock(m_mutex); for (auto& b : m_allocatedBlocks) { if (phys_addr >= b.start_addr && phys_addr < b.start_addr + b.size) { if (b.map_virtual_addr != 0 || b.map_size != 0) { diff --git a/src/Core/PS4/HLE/Kernel/Objects/physical_memory.h b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.h index a64cb374..e2e9ce49 100644 --- a/src/Core/PS4/HLE/Kernel/Objects/physical_memory.h +++ b/src/Core/PS4/HLE/Kernel/Objects/physical_memory.h @@ -3,6 +3,7 @@ #include #include #include +#include "Lib/Threads.h" namespace HLE::Kernel::Objects { @@ -27,6 +28,7 @@ class PhysicalMemory { private: std::vector m_allocatedBlocks; + Lib::Mutex m_mutex; }; } // namespace HLE::Kernel::Objects \ No newline at end of file diff --git a/src/Core/PS4/HLE/Kernel/memory_management.cpp b/src/Core/PS4/HLE/Kernel/memory_management.cpp index 91b72ba3..1840badf 100644 --- a/src/Core/PS4/HLE/Kernel/memory_management.cpp +++ b/src/Core/PS4/HLE/Kernel/memory_management.cpp @@ -67,22 +67,20 @@ int PS4_SYSV_ABI sceKernelAllocateDirectMemory(s64 searchStart, s64 searchEnd, u int PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, u64 len, int prot, int flags, s64 directMemoryStart, u64 alignment) { PRINT_FUNCTION_NAME(); - if (len == 0 || !is16KBAligned(len)) - { + if (len == 0 || !is16KBAligned(len)) { LOG_TRACE_IF(log_file_memory, "sceKernelMapDirectMemory returned SCE_KERNEL_ERROR_EINVAL len invalid\n"); return SCE_KERNEL_ERROR_EINVAL; } - if (!is16KBAligned(directMemoryStart)) - { + if (!is16KBAligned(directMemoryStart)) { LOG_TRACE_IF(log_file_memory, "sceKernelMapDirectMemory returned SCE_KERNEL_ERROR_EINVAL directMemoryStart invalid\n"); return SCE_KERNEL_ERROR_EINVAL; } - if (alignment != 0 || (!isPowerOfTwo(alignment) && !is16KBAligned(alignment))) - { - LOG_TRACE_IF(log_file_memory, "sceKernelMapDirectMemory returned SCE_KERNEL_ERROR_EINVAL alignment invalid\n"); - return SCE_KERNEL_ERROR_EINVAL; + if (alignment != 0) { + if ((!isPowerOfTwo(alignment) && !is16KBAligned(alignment))){ + LOG_TRACE_IF(log_file_memory, "sceKernelMapDirectMemory returned SCE_KERNEL_ERROR_EINVAL alignment invalid\n"); + return SCE_KERNEL_ERROR_EINVAL; + } } - auto* physical_memory = Singleton::Instance(); LOG_INFO_IF(log_file_memory, "len = {}\n", log_hex_full(len)); LOG_INFO_IF(log_file_memory, "prot = {}\n", log_hex_full(prot)); @@ -94,7 +92,7 @@ int PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, u64 len, int prot, int fl GPU::MemoryMode gpu_mode = GPU::MemoryMode::NoAccess; switch (prot) { - case 0x33://SCE_KERNEL_PROT_CPU_READ|SCE_KERNEL_PROT_CPU_WRITE|SCE_KERNEL_PROT_GPU_READ|SCE_KERNEL_PROT_GPU_ALL + case 0x33: // SCE_KERNEL_PROT_CPU_READ|SCE_KERNEL_PROT_CPU_WRITE|SCE_KERNEL_PROT_GPU_READ|SCE_KERNEL_PROT_GPU_ALL cpu_mode = VirtualMemory::MemoryMode::ReadWrite; gpu_mode = GPU::MemoryMode::ReadWrite; break; @@ -116,6 +114,7 @@ int PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, u64 len, int prot, int fl return SCE_KERNEL_ERROR_ENOMEM; } + auto* physical_memory = Singleton::Instance(); if (!physical_memory->Map(out_addr, directMemoryStart, len, prot, cpu_mode, gpu_mode)) { BREAKPOINT(); } @@ -123,7 +122,6 @@ int PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, u64 len, int prot, int fl if (gpu_mode != GPU::MemoryMode::NoAccess) { GPU::MemorySetAllocArea(out_addr, len); } - return SCE_OK; } diff --git a/src/Core/PS4/Linker.cpp b/src/Core/PS4/Linker.cpp index cdb1febf..d048054b 100644 --- a/src/Core/PS4/Linker.cpp +++ b/src/Core/PS4/Linker.cpp @@ -70,6 +70,7 @@ static std::string encodeId(u64 nVal) } Module* Linker::LoadModule(const std::string& elf_name) { + Lib::LockMutexGuard lock(m_mutex); auto* m = new Module; m->linker = this; m->elf = new Elf; diff --git a/src/Core/PS4/Linker.h b/src/Core/PS4/Linker.h index 13f45acf..e34cce04 100644 --- a/src/Core/PS4/Linker.h +++ b/src/Core/PS4/Linker.h @@ -3,6 +3,7 @@ #include #include "Loader/Elf.h" #include "Loader/SymbolsResolver.h" +#include "Lib/Threads.h" struct DynamicModuleInfo; class Linker; @@ -129,4 +130,5 @@ public: std::vector m_modules; SymbolsResolver* m_HLEsymbols = nullptr; + Lib::Mutex m_mutex; }; \ No newline at end of file diff --git a/src/Lib/Threads.h b/src/Lib/Threads.h index 4ca5f515..3773eaf5 100644 --- a/src/Lib/Threads.h +++ b/src/Lib/Threads.h @@ -121,5 +121,11 @@ class LockMutexGuard { private: Mutex& m_mutex; + + public: + LockMutexGuard(const LockMutexGuard&) = delete; + LockMutexGuard& operator=(const LockMutexGuard&) = delete; + LockMutexGuard(LockMutexGuard&&) noexcept = delete; + LockMutexGuard& operator=(LockMutexGuard&&) noexcept = delete; }; } // namespace Lib \ No newline at end of file