diff --git a/CMakeLists.txt b/CMakeLists.txt index 44ce20d0..acbff76b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ add_executable(shadps4 src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp src/Util/string_util.cpp src/Util/string_util.cpp - "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.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/memory_management.cpp" "src/Core/PS4/HLE/Kernel/memory_management.h" "src/Core/PS4/GPU/gpu_memory.cpp" "src/Core/PS4/GPU/gpu_memory.h") + "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.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/memory_management.cpp" "src/Core/PS4/HLE/Kernel/memory_management.h" "src/Core/PS4/GPU/gpu_memory.cpp" "src/Core/PS4/GPU/gpu_memory.h" "src/emulator.cpp" "src/emulator.h") find_package(OpenGL REQUIRED) target_link_libraries(shadps4 PUBLIC fmt mincore spdlog IMGUI SDL3-shared ${OPENGL_LIBRARY}) 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 diff --git a/src/emulator.cpp b/src/emulator.cpp new file mode 100644 index 00000000..f079b36f --- /dev/null +++ b/src/emulator.cpp @@ -0,0 +1,9 @@ +#include "emulator.h" + +namespace Emulator { +void emuInit() {} +void emuRun() { + for (;;) { + } +} +} // namespace emulator \ No newline at end of file diff --git a/src/emulator.h b/src/emulator.h new file mode 100644 index 00000000..99090aa9 --- /dev/null +++ b/src/emulator.h @@ -0,0 +1,6 @@ +#pragma once + +namespace Emulator { +void emuInit(); +void emuRun(); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6794b22f..7ce1f129 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,6 +30,7 @@ #include #include "Core/PS4/HLE/Libs.h" #include "Lib/Threads.h" +#include // Main code int main(int argc, char* argv[]) @@ -40,15 +41,24 @@ int main(int argc, char* argv[]) } logging::init(true); // init logging + Emulator::emuInit(); Lib::InitThreads(); - const char* const path = argv[1]; //argument 1 is the path of self file to boot - // const char* const path = "F:\\ps4games\\CUSA03840 - Resident Evil 6\\eboot.bin"; - + 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()); auto *module =linker->LoadModule(path);//load main executable - linker->Execute(); + Lib::Thread mainthread( + [](void*) { + auto* linker = Singleton::Instance(); + linker->Execute(); + }, + nullptr); + mainthread.DetachThread(); + Emulator::emuRun(); + mainthread.JoinThread(); + #if 0 // Setup SDL if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMEPAD) != 0)