diff --git a/src/core/address_space.cpp b/src/core/address_space.cpp index 05aa6cbb..4dd06e99 100644 --- a/src/core/address_space.cpp +++ b/src/core/address_space.cpp @@ -217,36 +217,34 @@ struct AddressSpace::Impl { } void Protect(VAddr virtual_addr, size_t size, bool read, bool write, bool execute) { - DWORD new_flags{}; - if (read && write && execute) { - new_flags = PAGE_EXECUTE_READWRITE; - } else if (read && write) { - new_flags = PAGE_READWRITE; - } else if (read && !write) { - new_flags = PAGE_READONLY; - } else if (execute && !read && !write) { - new_flags = PAGE_EXECUTE; - } else if (!read && !write && !execute) { - new_flags = PAGE_NOACCESS; - } else { - LOG_CRITICAL(Common_Memory, "Unsupported protection flag combination"); - } + DWORD new_flags{}; + if (read && write && execute) { + new_flags = PAGE_EXECUTE_READWRITE; + } else if (read && write) { + new_flags = PAGE_READWRITE; + } else if (read && !write) { + new_flags = PAGE_READONLY; + } else if (execute && !read && !write) { + new_flags = PAGE_EXECUTE; + } else if (!read && !write && !execute) { + new_flags = PAGE_NOACCESS; + } else { + LOG_CRITICAL(Common_Memory, "Unsupported protection flag combination"); + } const VAddr virtual_end = virtual_addr + size; - auto [it, end] = placeholders.equal_range({virtual_addr, virtual_end}); - while (it != end) { - const size_t offset = std::max(it->lower(), virtual_addr); - const size_t protect_length = std::min(it->upper(), virtual_end) - offset; - DWORD old_flags{}; - if (!VirtualProtect(virtual_base + offset, protect_length, new_flags, &old_flags)) { - LOG_CRITICAL(Common_Memory, "Failed to change virtual memory protect rules"); - } - ++it; + auto [it, end] = placeholders.equal_range({virtual_addr, virtual_end}); + while (it != end) { + const size_t offset = std::max(it->lower(), virtual_addr); + const size_t protect_length = std::min(it->upper(), virtual_end) - offset; + DWORD old_flags{}; + if (!VirtualProtect(virtual_base + offset, protect_length, new_flags, &old_flags)) { + LOG_CRITICAL(Common_Memory, "Failed to change virtual memory protect rules"); } + ++it; + } } - - HANDLE process{}; HANDLE backing_handle{}; u8* backing_base{}; @@ -468,7 +466,8 @@ void AddressSpace::Unmap(VAddr virtual_addr, size_t size, bool has_backing) { void AddressSpace::Protect(VAddr virtual_addr, size_t size, MemoryPermission perms) { bool read = static_cast(perms & MemoryPermission::Read) != 0; bool write = static_cast(perms & MemoryPermission::Write) != 0; - bool execute = static_cast(perms & MemoryPermission::Execute) != 0; // Assuming you have an Execute permission + bool execute = static_cast(perms & MemoryPermission::Execute) != + 0; // Assuming you have an Execute permission return impl->Protect(virtual_addr, size, read, write, execute); } diff --git a/src/core/libraries/kernel/libkernel.cpp b/src/core/libraries/kernel/libkernel.cpp index 3e2c4f86..4b9565b6 100644 --- a/src/core/libraries/kernel/libkernel.cpp +++ b/src/core/libraries/kernel/libkernel.cpp @@ -427,7 +427,6 @@ void LibKernel_Register(Core::Loader::SymbolsResolver* sym) { LIB_FUNCTION("9bfdLIyuwCY", "libkernel", 1, "libkernel", 1, 1, sceKernelMTypeProtect); LIB_FUNCTION("vSMAm3cxYTY", "libkernel", 1, "libkernel", 1, 1, sceKernelMProtect); - // misc LIB_FUNCTION("WslcK1FQcGI", "libkernel", 1, "libkernel", 1, 1, sceKernelIsNeoMode); LIB_FUNCTION("Ou3iL1abvng", "libkernel", 1, "libkernel", 1, 1, stack_chk_fail); diff --git a/src/core/libraries/kernel/memory_management.cpp b/src/core/libraries/kernel/memory_management.cpp index 0433e2fb..db8a4a52 100644 --- a/src/core/libraries/kernel/memory_management.cpp +++ b/src/core/libraries/kernel/memory_management.cpp @@ -6,12 +6,11 @@ #include "common/assert.h" #include "common/logging/log.h" #include "common/singleton.h" +#include "core/address_space.h" #include "core/libraries/error_codes.h" #include "core/libraries/kernel/memory_management.h" #include "core/linker.h" #include "core/memory.h" -#include "core/address_space.h" - namespace Libraries::Kernel { diff --git a/src/core/memory.cpp b/src/core/memory.cpp index f321d898..41bb60e3 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -279,7 +279,7 @@ int MemoryManager::MProtect(VAddr addr, size_t size, int prot) { return ORBIS_KERNEL_ERROR_EINVAL; } -// Check if the new protection flags are valid. + // 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) | @@ -313,7 +313,6 @@ int MemoryManager::MProtect(VAddr addr, size_t size, int prot) { int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot) { std::scoped_lock lk{mutex}; - // Find the virtual memory area that contains the specified address range. auto it = FindVMA(addr); if (it == vma_map.end() || !it->second.Contains(addr, size)) { @@ -323,7 +322,6 @@ int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot VirtualMemoryArea& vma = it->second; - if (vma.type == VMAType::Free) { LOG_ERROR(Core, "Cannot change protection on free memory region"); return ORBIS_KERNEL_ERROR_EINVAL; @@ -361,9 +359,6 @@ int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot return ORBIS_OK; } - - - int MemoryManager::VirtualQuery(VAddr addr, int flags, ::Libraries::Kernel::OrbisVirtualQueryInfo* info) { std::scoped_lock lk{mutex}; diff --git a/src/core/memory.h b/src/core/memory.h index 5163f024..17902897 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -166,7 +166,8 @@ public: int VirtualQuery(VAddr addr, int flags, ::Libraries::Kernel::OrbisVirtualQueryInfo* info); - int DirectMemoryQuery(PAddr addr, bool find_next, ::Libraries::Kernel::OrbisQueryInfo* out_info); + int DirectMemoryQuery(PAddr addr, bool find_next, + ::Libraries::Kernel::OrbisQueryInfo* out_info); int DirectQueryAvailable(PAddr search_start, PAddr search_end, size_t alignment, PAddr* phys_addr_out, size_t* size_out);