From 88cbf4128ae36175d626be887cc871c6e93cce7d Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 6 Aug 2024 21:37:31 -0400 Subject: [PATCH] reverted the previous function --- src/core/address_space.cpp | 55 ++++++++++++++++++-------------------- src/core/memory.cpp | 8 ++++++ 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/core/address_space.cpp b/src/core/address_space.cpp index d5d35157..4ea9ca43 100644 --- a/src/core/address_space.cpp +++ b/src/core/address_space.cpp @@ -217,36 +217,33 @@ struct AddressSpace::Impl { } void Protect(VAddr virtual_addr, size_t size, bool read, bool write, bool execute) { - DWORD new_flags{}; - if (read && write) { - new_flags = PAGE_READWRITE; - } else if (read && !write) { - new_flags = PAGE_READONLY; - } else if (!read && !write) { - new_flags = PAGE_NOACCESS; - } else { - UNIMPLEMENTED_MSG("Protection flag combination read={} write={}", read, write); + 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"); } - - MEMORY_BASIC_INFORMATION info; - VirtualQuery(reinterpret_cast(virtual_addr), &info, sizeof(info)); - LOG_INFO(Common_Memory, "Current protection flags: {}", info.Protect); - - if (info.Protect == new_flags) { - LOG_INFO(Common_Memory, "Protection flags already match, skipping VirtualProtect"); - return; - } - - - - if (!VirtualProtect(reinterpret_cast(virtual_addr), size, new_flags, nullptr)) { - DWORD error = GetLastError(); - LOG_CRITICAL( - Common_Memory, - "Failed to change virtual memory protect rules: error {}, new_flags = {:#x}", error, - new_flags); - } - } + ++it; + } + } diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 0d49ce98..11043b19 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -318,6 +318,9 @@ 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}; + LOG_INFO(Core, "MTypeProtect called: addr = {:#x}, size = {:#x}, mtype = {:#x}, prot = {:#x}", + addr, size, mtype, prot); + // 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)) { @@ -326,6 +329,9 @@ int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot } VirtualMemoryArea& vma = it->second; + LOG_INFO(Core, "VMA found: base = {:#x}, size = {:#x}, prot = {:#x}, type = {}", vma.base, + vma.size, vma.prot, vma.type); + if (vma.type == VMAType::Free) { LOG_ERROR(Core, "Cannot change protection on free memory region"); return ORBIS_KERNEL_ERROR_EINVAL; @@ -346,6 +352,8 @@ int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot vma.type = mtype; vma.prot = static_cast(prot); + LOG_INFO(Core, "Changed VMA type and protection: type = {:#x}, prot = {:#x}", mtype, prot); + // Use the Protect function from the AddressSpace class. Core::MemoryPermission perms; if ((static_cast(prot) & static_cast(MemoryProt::CpuRead)) != 0)