From 447ce90b7b48a1110def385fb8f937a80a844f46 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Tue, 22 Oct 2013 23:34:24 -0700 Subject: [PATCH] MmQueryAddressProtect. --- src/xenia/core/memory.cc | 27 +++++++++++++------ src/xenia/core/memory.h | 2 ++ .../modules/xboxkrnl/xboxkrnl_memory.cc | 11 +++++--- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/xenia/core/memory.cc b/src/xenia/core/memory.cc index ed6782ba6..37f476e38 100644 --- a/src/xenia/core/memory.cc +++ b/src/xenia/core/memory.cc @@ -12,6 +12,9 @@ #include #include +// TODO(benvanik): move xbox.h out +#include + #if !XE_PLATFORM(WIN32) #include #endif // WIN32 @@ -377,14 +380,12 @@ int xe_memory_protect( size_t heap_guard_size = FLAGS_heap_guard_pages * 4096; p += heap_guard_size; - DWORD new_protect = 0; - if (access & XE_MEMORY_ACCESS_WRITE) { - new_protect = PAGE_READWRITE; - } else if (access & XE_MEMORY_ACCESS_READ) { - new_protect = PAGE_READONLY; - } else { - new_protect = PAGE_NOACCESS; - } + DWORD new_protect = access; + new_protect = new_protect & ( + X_PAGE_NOACCESS | X_PAGE_READONLY | X_PAGE_READWRITE | + X_PAGE_WRITECOPY | X_PAGE_GUARD | X_PAGE_NOCACHE | + X_PAGE_WRITECOMBINE); + DWORD old_protect; return VirtualProtect(p, size, new_protect, &old_protect) == TRUE ? 0 : 1; } @@ -572,3 +573,13 @@ uint32_t xe_memory_heap_t::Free(uint32_t address, uint32_t size) { return (uint32_t)real_size; } + +uint32_t xe_memory_query_protect(xe_memory_ref memory, uint32_t address) { + uint8_t* p = memory->mapping_base + address; + MEMORY_BASIC_INFORMATION info; + size_t info_size = VirtualQuery((void*)p, &info, sizeof(info)); + if (!info_size) { + return 0; + } + return info.Protect; +} diff --git a/src/xenia/core/memory.h b/src/xenia/core/memory.h index b6b13e2eb..71ed7cade 100644 --- a/src/xenia/core/memory.h +++ b/src/xenia/core/memory.h @@ -61,5 +61,7 @@ int xe_memory_heap_free(xe_memory_ref memory, uint32_t addr, uint32_t size); bool xe_memory_is_valid(xe_memory_ref memory, uint32_t address); int xe_memory_protect(xe_memory_ref memory, uint32_t address, uint32_t size, uint32_t access); +uint32_t xe_memory_query_protect(xe_memory_ref memory, uint32_t address); + #endif // XENIA_CORE_MEMORY_H_ diff --git a/src/xenia/kernel/modules/xboxkrnl/xboxkrnl_memory.cc b/src/xenia/kernel/modules/xboxkrnl/xboxkrnl_memory.cc index 1175391df..e0d96b046 100644 --- a/src/xenia/kernel/modules/xboxkrnl/xboxkrnl_memory.cc +++ b/src/xenia/kernel/modules/xboxkrnl/xboxkrnl_memory.cc @@ -307,8 +307,13 @@ SHIM_CALL MmFreePhysicalMemory_shim( uint32_t xeMmQueryAddressProtect(uint32_t base_address) { - XELOGW("MmQueryAddressProtect faked!"); - return 0; + KernelState* state = shared_kernel_state_; + XEASSERTNOTNULL(state); + + uint32_t access = xe_memory_query_protect( + state->memory(), base_address); + + return access; } @@ -375,6 +380,6 @@ void xe::kernel::xboxkrnl::RegisterMemoryExports( //SHIM_SET_MAPPING("xboxkrnl.exe", MmAllocatePhysicalMemory, state); SHIM_SET_MAPPING("xboxkrnl.exe", MmAllocatePhysicalMemoryEx, state); SHIM_SET_MAPPING("xboxkrnl.exe", MmFreePhysicalMemory, state); - //SHIM_SET_MAPPING("xboxkrnl.exe", MmQueryAddressProtect, state); + SHIM_SET_MAPPING("xboxkrnl.exe", MmQueryAddressProtect, state); SHIM_SET_MAPPING("xboxkrnl.exe", MmGetPhysicalAddress, state); }