Fixed nits

This commit is contained in:
Antonio 2024-08-17 14:49:23 -04:00
parent fefd4b3bfd
commit dbb9a45af3
4 changed files with 60 additions and 49 deletions

View File

@ -208,31 +208,29 @@ int PS4_SYSV_ABI sceKernelQueryMemoryProtection(void* addr, void** start, void**
return memory->QueryProtection(std::bit_cast<VAddr>(addr), start, end, prot); return memory->QueryProtection(std::bit_cast<VAddr>(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(); 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<VAddr>(addr), size, prot); // Cast the 'prot' integer to 'MemoryProt' enum type
Core::MemoryProt protection_flags = static_cast<Core::MemoryProt>(prot);
int result = memory_manager->Protect(std::bit_cast<VAddr>(addr), size, protection_flags);
if (result != ORBIS_OK) { if (result != ORBIS_OK) {
LOG_ERROR(Kernel_Vmm, "MProtect failed with result {}", result); LOG_ERROR(Kernel_Vmm, "MProtect failed with result {}", result);
} }
return 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(); Core::MemoryManager* memory_manager = Core::Memory::Instance();
if (!memory_manager) {
LOG_ERROR(Kernel_Vmm, "Failed to get MemoryManager instance"); // Cast the 'prot' integer to 'MemoryProt' enum type
return ORBIS_KERNEL_ERROR_EINVAL; Core::MemoryProt protection_flags = static_cast<Core::MemoryProt>(prot);
}
int result = memory_manager->MTypeProtect(std::bit_cast<VAddr>(addr), size, int result = memory_manager->MTypeProtect(std::bit_cast<VAddr>(addr), size,
static_cast<Core::VMAType>(mtype), prot); static_cast<Core::VMAType>(mtype), protection_flags);
if (result != ORBIS_OK) { if (result != ORBIS_OK) {
LOG_ERROR(Kernel_Vmm, "MTypeProtect failed with result {}", result); LOG_ERROR(Kernel_Vmm, "MTypeProtect failed with result {}", result);
} }

View File

@ -95,9 +95,9 @@ s32 PS4_SYSV_ABI sceKernelMapFlexibleMemory(void** addr_in_out, std::size_t len,
int flags); int flags);
int PS4_SYSV_ABI sceKernelQueryMemoryProtection(void* addr, void** start, void** end, u32* prot); 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, int PS4_SYSV_ABI sceKernelDirectMemoryQuery(u64 offset, int flags, OrbisQueryInfo* query_info,
size_t infoSize); size_t infoSize);

View File

@ -269,7 +269,7 @@ int MemoryManager::QueryProtection(VAddr addr, void** start, void** end, u32* pr
return ORBIS_OK; 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}; std::scoped_lock lk{mutex};
// Find the virtual memory area that contains the specified address range. // 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; return ORBIS_KERNEL_ERROR_EINVAL;
} }
// Check if the new protection flags are valid. // Validate protection flags
if ((static_cast<int>(prot) & MemoryProt valid_flags = MemoryProt::NoAccess | MemoryProt::CpuRead | MemoryProt::CpuReadWrite |
~(static_cast<int>(MemoryProt::NoAccess) | static_cast<int>(MemoryProt::CpuRead) | MemoryProt::GpuRead | MemoryProt::GpuWrite | MemoryProt::GpuReadWrite;
static_cast<int>(MemoryProt::CpuReadWrite) | static_cast<int>(MemoryProt::GpuRead) |
static_cast<int>(MemoryProt::GpuWrite) | static_cast<int>(MemoryProt::GpuReadWrite))) != if ((prot & ~valid_flags) != MemoryProt::NoAccess) {
0) {
LOG_ERROR(Core, "Invalid protection flags, prot: {:#x}, GpuWrite: {:#x}", LOG_ERROR(Core, "Invalid protection flags, prot: {:#x}, GpuWrite: {:#x}",
static_cast<uint32_t>(prot), static_cast<uint32_t>(MemoryProt::GpuWrite)); static_cast<uint32_t>(prot), static_cast<uint32_t>(MemoryProt::GpuWrite));
return ORBIS_KERNEL_ERROR_EINVAL; return ORBIS_KERNEL_ERROR_EINVAL;
} }
// Change the protection on the specified address range. // Change protection
vma.prot = static_cast<MemoryProt>(prot); vma.prot = prot;
// Use the Protect function from the AddressSpace class. // Set permissions
Core::MemoryPermission perms; Core::MemoryPermission perms{};
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::CpuRead)) != 0)
if ((prot & MemoryProt::CpuRead) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::Read; perms |= Core::MemoryPermission::Read;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::CpuReadWrite)) != 0) }
if ((prot & MemoryProt::CpuReadWrite) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::ReadWrite; perms |= Core::MemoryPermission::ReadWrite;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::GpuRead)) != 0) }
if ((prot & MemoryProt::GpuRead) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::Read; perms |= Core::MemoryPermission::Read;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::GpuWrite)) != 0) }
if ((prot & MemoryProt::GpuWrite) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::Write; perms |= Core::MemoryPermission::Write;
if ((static_cast<int>(prot) & static_cast<int>(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); impl.Protect(addr, size, perms);
return ORBIS_OK; 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}; std::scoped_lock lk{mutex};
// Find the virtual memory area that contains the specified address range. // 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; return ORBIS_KERNEL_ERROR_EINVAL;
} }
// Check if the new protection flags are valid. // Validate protection flags
if ((static_cast<int>(prot) & MemoryProt valid_flags = MemoryProt::NoAccess | MemoryProt::CpuRead | MemoryProt::CpuReadWrite |
~(static_cast<int>(MemoryProt::NoAccess) | static_cast<int>(MemoryProt::CpuRead) | MemoryProt::GpuRead | MemoryProt::GpuWrite | MemoryProt::GpuReadWrite;
static_cast<int>(MemoryProt::CpuReadWrite) | static_cast<int>(MemoryProt::GpuRead) |
static_cast<int>(MemoryProt::GpuWrite) | static_cast<int>(MemoryProt::GpuReadWrite))) != if ((prot & ~valid_flags) != MemoryProt::NoAccess) {
0) {
LOG_ERROR(Core, "Invalid protection flags, prot: {:#x}, GpuWrite: {:#x}", LOG_ERROR(Core, "Invalid protection flags, prot: {:#x}, GpuWrite: {:#x}",
static_cast<uint32_t>(prot), static_cast<uint32_t>(MemoryProt::GpuWrite)); static_cast<uint32_t>(prot), static_cast<uint32_t>(MemoryProt::GpuWrite));
return ORBIS_KERNEL_ERROR_EINVAL; return ORBIS_KERNEL_ERROR_EINVAL;
} }
// Change the type and protection on the specified address range. // Change type and protection
vma.type = mtype; vma.type = mtype;
vma.prot = static_cast<MemoryProt>(prot); vma.prot = prot;
// Use the Protect function from the AddressSpace class. // Set permissions
Core::MemoryPermission perms; Core::MemoryPermission perms{};
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::CpuRead)) != 0)
if ((prot & MemoryProt::CpuRead) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::Read; perms |= Core::MemoryPermission::Read;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::CpuReadWrite)) != 0) }
if ((prot & MemoryProt::CpuReadWrite) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::ReadWrite; perms |= Core::MemoryPermission::ReadWrite;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::GpuRead)) != 0) }
if ((prot & MemoryProt::GpuRead) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::Read; perms |= Core::MemoryPermission::Read;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::GpuWrite)) != 0) }
if ((prot & MemoryProt::GpuWrite) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::Write; perms |= Core::MemoryPermission::Write;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::GpuReadWrite)) != 0) }
if ((prot & MemoryProt::GpuReadWrite) != MemoryProt::NoAccess) {
perms |= Core::MemoryPermission::ReadWrite; perms |= Core::MemoryPermission::ReadWrite;
}
impl.Protect(addr, size, perms); impl.Protect(addr, size, perms);
return ORBIS_OK; return ORBIS_OK;

View File

@ -30,6 +30,7 @@ enum class MemoryProt : u32 {
GpuWrite = 32, GpuWrite = 32,
GpuReadWrite = 38, GpuReadWrite = 38,
}; };
DECLARE_ENUM_FLAG_OPERATORS(MemoryProt)
enum class MemoryMapFlags : u32 { enum class MemoryMapFlags : u32 {
NoFlags = 0, NoFlags = 0,
@ -161,9 +162,9 @@ public:
int QueryProtection(VAddr addr, void** start, void** end, u32* prot); 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); int VirtualQuery(VAddr addr, int flags, ::Libraries::Kernel::OrbisVirtualQueryInfo* info);