From dbb9a45af34e522e0b7eb224ad3aef8517971bef Mon Sep 17 00:00:00 2001 From: Antonio Date: Sat, 17 Aug 2024 14:49:23 -0400 Subject: [PATCH] Fixed nits --- .../libraries/kernel/memory_management.cpp | 22 +++--- src/core/libraries/kernel/memory_management.h | 4 +- src/core/memory.cpp | 78 +++++++++++-------- src/core/memory.h | 5 +- 4 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/core/libraries/kernel/memory_management.cpp b/src/core/libraries/kernel/memory_management.cpp index 189c7387..55f37e34 100644 --- a/src/core/libraries/kernel/memory_management.cpp +++ b/src/core/libraries/kernel/memory_management.cpp @@ -208,31 +208,29 @@ int PS4_SYSV_ABI sceKernelQueryMemoryProtection(void* addr, void** start, void** return memory->QueryProtection(std::bit_cast(addr), start, end, prot); } -int PS4_SYSV_ABI sceKernelMProtect(void* addr, size_t size, int prot) { +int PS4_SYSV_ABI sceKernelMProtect(const void* addr, size_t size, int prot) { Core::MemoryManager* memory_manager = Core::Memory::Instance(); - if (!memory_manager) { - LOG_ERROR(Kernel_Vmm, "Failed to get MemoryManager instance"); - return ORBIS_KERNEL_ERROR_EINVAL; - } - int result = memory_manager->MProtect(std::bit_cast(addr), size, prot); + // Cast the 'prot' integer to 'MemoryProt' enum type + Core::MemoryProt protection_flags = static_cast(prot); + + int result = memory_manager->Protect(std::bit_cast(addr), size, protection_flags); if (result != ORBIS_OK) { LOG_ERROR(Kernel_Vmm, "MProtect failed with result {}", result); } return result; } -int PS4_SYSV_ABI sceKernelMTypeProtect(void* addr, size_t size, int mtype, int prot) { +int PS4_SYSV_ABI sceKernelMTypeProtect(const void* addr, size_t size, int mtype, int prot) { Core::MemoryManager* memory_manager = Core::Memory::Instance(); - if (!memory_manager) { - LOG_ERROR(Kernel_Vmm, "Failed to get MemoryManager instance"); - return ORBIS_KERNEL_ERROR_EINVAL; - } + + // Cast the 'prot' integer to 'MemoryProt' enum type + Core::MemoryProt protection_flags = static_cast(prot); int result = memory_manager->MTypeProtect(std::bit_cast(addr), size, - static_cast(mtype), prot); + static_cast(mtype), protection_flags); if (result != ORBIS_OK) { LOG_ERROR(Kernel_Vmm, "MTypeProtect failed with result {}", result); } diff --git a/src/core/libraries/kernel/memory_management.h b/src/core/libraries/kernel/memory_management.h index 440685c3..97018e43 100644 --- a/src/core/libraries/kernel/memory_management.h +++ b/src/core/libraries/kernel/memory_management.h @@ -95,9 +95,9 @@ s32 PS4_SYSV_ABI sceKernelMapFlexibleMemory(void** addr_in_out, std::size_t len, int flags); int PS4_SYSV_ABI sceKernelQueryMemoryProtection(void* addr, void** start, void** end, u32* prot); -int PS4_SYSV_ABI sceKernelMProtect(void* addr, size_t size, int prot); +int PS4_SYSV_ABI sceKernelMProtect(const void* addr, size_t size, int prot); -int PS4_SYSV_ABI sceKernelMTypeProtect(void* addr, size_t size, int mtype, int prot); +int PS4_SYSV_ABI sceKernelMTypeProtect(const void* addr, size_t size, int mtype, int prot); int PS4_SYSV_ABI sceKernelDirectMemoryQuery(u64 offset, int flags, OrbisQueryInfo* query_info, size_t infoSize); diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 931009e2..c06ad832 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -269,7 +269,7 @@ int MemoryManager::QueryProtection(VAddr addr, void** start, void** end, u32* pr return ORBIS_OK; } -int MemoryManager::MProtect(VAddr addr, size_t size, int prot) { +int MemoryManager::Protect(VAddr addr, size_t size, MemoryProt prot) { std::scoped_lock lk{mutex}; // Find the virtual memory area that contains the specified address range. @@ -285,38 +285,44 @@ int MemoryManager::MProtect(VAddr addr, size_t size, int prot) { return ORBIS_KERNEL_ERROR_EINVAL; } - // Check if the new protection flags are valid. - if ((static_cast(prot) & - ~(static_cast(MemoryProt::NoAccess) | static_cast(MemoryProt::CpuRead) | - static_cast(MemoryProt::CpuReadWrite) | static_cast(MemoryProt::GpuRead) | - static_cast(MemoryProt::GpuWrite) | static_cast(MemoryProt::GpuReadWrite))) != - 0) { + // Validate protection flags + MemoryProt valid_flags = MemoryProt::NoAccess | MemoryProt::CpuRead | MemoryProt::CpuReadWrite | + MemoryProt::GpuRead | MemoryProt::GpuWrite | MemoryProt::GpuReadWrite; + + if ((prot & ~valid_flags) != MemoryProt::NoAccess) { LOG_ERROR(Core, "Invalid protection flags, prot: {:#x}, GpuWrite: {:#x}", static_cast(prot), static_cast(MemoryProt::GpuWrite)); return ORBIS_KERNEL_ERROR_EINVAL; } - // Change the protection on the specified address range. - vma.prot = static_cast(prot); + // Change protection + vma.prot = prot; - // Use the Protect function from the AddressSpace class. - Core::MemoryPermission perms; - if ((static_cast(prot) & static_cast(MemoryProt::CpuRead)) != 0) + // Set permissions + Core::MemoryPermission perms{}; + + if ((prot & MemoryProt::CpuRead) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::Read; - if ((static_cast(prot) & static_cast(MemoryProt::CpuReadWrite)) != 0) + } + if ((prot & MemoryProt::CpuReadWrite) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::ReadWrite; - if ((static_cast(prot) & static_cast(MemoryProt::GpuRead)) != 0) + } + if ((prot & MemoryProt::GpuRead) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::Read; - if ((static_cast(prot) & static_cast(MemoryProt::GpuWrite)) != 0) + } + if ((prot & MemoryProt::GpuWrite) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::Write; - if ((static_cast(prot) & static_cast(MemoryProt::GpuReadWrite)) != 0) - perms |= Core::MemoryPermission::ReadWrite; // Add this line + } + if ((prot & MemoryProt::GpuReadWrite) != MemoryProt::NoAccess) { + perms |= Core::MemoryPermission::ReadWrite; + } + impl.Protect(addr, size, perms); return ORBIS_OK; } -int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot) { +int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, MemoryProt prot) { std::scoped_lock lk{mutex}; // Find the virtual memory area that contains the specified address range. @@ -333,33 +339,39 @@ int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot return ORBIS_KERNEL_ERROR_EINVAL; } - // Check if the new protection flags are valid. - if ((static_cast(prot) & - ~(static_cast(MemoryProt::NoAccess) | static_cast(MemoryProt::CpuRead) | - static_cast(MemoryProt::CpuReadWrite) | static_cast(MemoryProt::GpuRead) | - static_cast(MemoryProt::GpuWrite) | static_cast(MemoryProt::GpuReadWrite))) != - 0) { + // Validate protection flags + MemoryProt valid_flags = MemoryProt::NoAccess | MemoryProt::CpuRead | MemoryProt::CpuReadWrite | + MemoryProt::GpuRead | MemoryProt::GpuWrite | MemoryProt::GpuReadWrite; + + if ((prot & ~valid_flags) != MemoryProt::NoAccess) { LOG_ERROR(Core, "Invalid protection flags, prot: {:#x}, GpuWrite: {:#x}", static_cast(prot), static_cast(MemoryProt::GpuWrite)); return ORBIS_KERNEL_ERROR_EINVAL; } - // Change the type and protection on the specified address range. + // Change type and protection vma.type = mtype; - vma.prot = static_cast(prot); + vma.prot = prot; - // Use the Protect function from the AddressSpace class. - Core::MemoryPermission perms; - if ((static_cast(prot) & static_cast(MemoryProt::CpuRead)) != 0) + // Set permissions + Core::MemoryPermission perms{}; + + if ((prot & MemoryProt::CpuRead) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::Read; - if ((static_cast(prot) & static_cast(MemoryProt::CpuReadWrite)) != 0) + } + if ((prot & MemoryProt::CpuReadWrite) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::ReadWrite; - if ((static_cast(prot) & static_cast(MemoryProt::GpuRead)) != 0) + } + if ((prot & MemoryProt::GpuRead) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::Read; - if ((static_cast(prot) & static_cast(MemoryProt::GpuWrite)) != 0) + } + if ((prot & MemoryProt::GpuWrite) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::Write; - if ((static_cast(prot) & static_cast(MemoryProt::GpuReadWrite)) != 0) + } + if ((prot & MemoryProt::GpuReadWrite) != MemoryProt::NoAccess) { perms |= Core::MemoryPermission::ReadWrite; + } + impl.Protect(addr, size, perms); return ORBIS_OK; diff --git a/src/core/memory.h b/src/core/memory.h index 9c046c25..6502829b 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -30,6 +30,7 @@ enum class MemoryProt : u32 { GpuWrite = 32, GpuReadWrite = 38, }; +DECLARE_ENUM_FLAG_OPERATORS(MemoryProt) enum class MemoryMapFlags : u32 { NoFlags = 0, @@ -161,9 +162,9 @@ public: int QueryProtection(VAddr addr, void** start, void** end, u32* prot); - int MProtect(VAddr addr, size_t size, int prot); + int Protect(VAddr addr, size_t size, MemoryProt prot); - int MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot); + int MTypeProtect(VAddr addr, size_t size, VMAType mtype, MemoryProt prot); int VirtualQuery(VAddr addr, int flags, ::Libraries::Kernel::OrbisVirtualQueryInfo* info);