reverted the previous function

This commit is contained in:
Antonio 2024-08-06 21:37:31 -04:00
parent fc4c96b8c0
commit 88cbf4128a
2 changed files with 34 additions and 29 deletions

View File

@ -218,33 +218,30 @@ struct AddressSpace::Impl {
void Protect(VAddr virtual_addr, size_t size, bool read, bool write, bool execute) { void Protect(VAddr virtual_addr, size_t size, bool read, bool write, bool execute) {
DWORD new_flags{}; DWORD new_flags{};
if (read && write) { if (read && write && execute) {
new_flags = PAGE_EXECUTE_READWRITE;
} else if (read && write) {
new_flags = PAGE_READWRITE; new_flags = PAGE_READWRITE;
} else if (read && !write) { } else if (read && !write) {
new_flags = PAGE_READONLY; new_flags = PAGE_READONLY;
} else if (!read && !write) { } else if (execute && !read && !write) {
new_flags = PAGE_EXECUTE;
} else if (!read && !write && !execute) {
new_flags = PAGE_NOACCESS; new_flags = PAGE_NOACCESS;
} else { } else {
UNIMPLEMENTED_MSG("Protection flag combination read={} write={}", read, write); LOG_CRITICAL(Common_Memory, "Unsupported protection flag combination");
} }
MEMORY_BASIC_INFORMATION info; const VAddr virtual_end = virtual_addr + size;
VirtualQuery(reinterpret_cast<LPVOID>(virtual_addr), &info, sizeof(info)); auto [it, end] = placeholders.equal_range({virtual_addr, virtual_end});
LOG_INFO(Common_Memory, "Current protection flags: {}", info.Protect); while (it != end) {
const size_t offset = std::max(it->lower(), virtual_addr);
if (info.Protect == new_flags) { const size_t protect_length = std::min(it->upper(), virtual_end) - offset;
LOG_INFO(Common_Memory, "Protection flags already match, skipping VirtualProtect"); DWORD old_flags{};
return; if (!VirtualProtect(virtual_base + offset, protect_length, new_flags, &old_flags)) {
LOG_CRITICAL(Common_Memory, "Failed to change virtual memory protect rules");
} }
++it;
if (!VirtualProtect(reinterpret_cast<LPVOID>(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);
} }
} }

View File

@ -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) { int MemoryManager::MTypeProtect(VAddr addr, size_t size, VMAType mtype, int prot) {
std::scoped_lock lk{mutex}; 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. // Find the virtual memory area that contains the specified address range.
auto it = FindVMA(addr); auto it = FindVMA(addr);
if (it == vma_map.end() || !it->second.Contains(addr, size)) { 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; 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) { if (vma.type == VMAType::Free) {
LOG_ERROR(Core, "Cannot change protection on free memory region"); LOG_ERROR(Core, "Cannot change protection on free memory region");
return ORBIS_KERNEL_ERROR_EINVAL; 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.type = mtype;
vma.prot = static_cast<MemoryProt>(prot); vma.prot = static_cast<MemoryProt>(prot);
LOG_INFO(Core, "Changed VMA type and protection: type = {:#x}, prot = {:#x}", mtype, prot);
// Use the Protect function from the AddressSpace class. // Use the Protect function from the AddressSpace class.
Core::MemoryPermission perms; Core::MemoryPermission perms;
if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::CpuRead)) != 0) if ((static_cast<int>(prot) & static_cast<int>(MemoryProt::CpuRead)) != 0)