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

@ -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<LPVOID>(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<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);
}
}
++it;
}
}

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) {
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<MemoryProt>(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<int>(prot) & static_cast<int>(MemoryProt::CpuRead)) != 0)