From b5826e0e942868225a9cd19dab6952d4da358524 Mon Sep 17 00:00:00 2001
From: wszechpolak <wszechpolak@yahoo.com>
Date: Thu, 14 Aug 2014 21:37:57 +0200
Subject: [PATCH 1/2] Added stub of NtQueryVirtualMemory

---
 src/xenia/kernel/xboxkrnl_memory.cc | 43 +++++++++++++++++++++++++++++
 src/xenia/kernel/xboxkrnl_memory.h  | 14 ++++++++++
 2 files changed, 57 insertions(+)

diff --git a/src/xenia/kernel/xboxkrnl_memory.cc b/src/xenia/kernel/xboxkrnl_memory.cc
index 9f10b4fba..abe96d4a0 100644
--- a/src/xenia/kernel/xboxkrnl_memory.cc
+++ b/src/xenia/kernel/xboxkrnl_memory.cc
@@ -189,6 +189,48 @@ SHIM_CALL NtFreeVirtualMemory_shim(
 }
 
 
+X_STATUS xeNtQueryVirtualMemory(
+    uint32_t* base_addr_ptr, X_MEMORY_BASIC_INFORMATION *memory_basic_information, bool swap) {
+
+  memory_basic_information->base_address        = XEROUNDUP(*base_addr_ptr, 4096);
+  memory_basic_information->allocation_base     = NULL;
+  memory_basic_information->allocation_protect  = 0;
+  memory_basic_information->region_size         = 0;
+  memory_basic_information->state               = X_MEM_FREE;
+  memory_basic_information->protect             = X_PAGE_NOACCESS;
+  memory_basic_information->type                = 0;
+
+  if (swap) {
+    memory_basic_information->base_address        = poly::byte_swap(memory_basic_information->base_address);
+    memory_basic_information->allocation_base     = poly::byte_swap(memory_basic_information->allocation_base);
+    memory_basic_information->allocation_protect  = poly::byte_swap(memory_basic_information->allocation_protect);
+    memory_basic_information->region_size         = poly::byte_swap(memory_basic_information->region_size);
+    memory_basic_information->state               = poly::byte_swap(memory_basic_information->state);
+    memory_basic_information->protect             = poly::byte_swap(memory_basic_information->protect);
+    memory_basic_information->type                = poly::byte_swap(memory_basic_information->type);
+  }
+
+  XELOGE("NtQueryVirtualMemory NOT IMPLEMENTED");
+
+  return X_STATUS_SUCCESS;
+}
+
+
+SHIM_CALL NtQueryVirtualMemory_shim(
+    PPCContext* ppc_state, KernelState* state) {
+  uint32_t base_addr_ptr = SHIM_GET_ARG_32(0);
+  uint32_t memory_basic_information_ptr = SHIM_GET_ARG_32(1);
+  X_MEMORY_BASIC_INFORMATION *memory_basic_information = (X_MEMORY_BASIC_INFORMATION*)SHIM_MEM_ADDR(memory_basic_information_ptr);
+
+  XELOGD(
+  	"NtQueryVirtualMemory(%.8X, %.8X)",
+    base_addr_ptr, memory_basic_information_ptr);
+
+  X_STATUS result = xeNtQueryVirtualMemory(&base_addr_ptr, memory_basic_information, true);
+  SHIM_SET_RETURN_32(result);
+}
+
+
 uint32_t xeMmAllocatePhysicalMemoryEx(
     uint32_t type, uint32_t region_size, uint32_t protect_bits,
     uint32_t min_addr_range, uint32_t max_addr_range, uint32_t alignment) {
@@ -513,6 +555,7 @@ void xe::kernel::xboxkrnl::RegisterMemoryExports(
     ExportResolver* export_resolver, KernelState* state) {
   SHIM_SET_MAPPING("xboxkrnl.exe", NtAllocateVirtualMemory, state);
   SHIM_SET_MAPPING("xboxkrnl.exe", NtFreeVirtualMemory, state);
+  SHIM_SET_MAPPING("xboxkrnl.exe", NtQueryVirtualMemory, state);
   //SHIM_SET_MAPPING("xboxkrnl.exe", MmAllocatePhysicalMemory, state);
   SHIM_SET_MAPPING("xboxkrnl.exe", MmAllocatePhysicalMemoryEx, state);
   SHIM_SET_MAPPING("xboxkrnl.exe", MmFreePhysicalMemory, state);
diff --git a/src/xenia/kernel/xboxkrnl_memory.h b/src/xenia/kernel/xboxkrnl_memory.h
index 0796dd78b..0b59f5420 100644
--- a/src/xenia/kernel/xboxkrnl_memory.h
+++ b/src/xenia/kernel/xboxkrnl_memory.h
@@ -20,6 +20,20 @@ namespace xe {
 namespace kernel {
 
 
+#pragma pack(push, 1)
+typedef struct {
+	uint32_t base_address;
+	uint32_t allocation_base;
+	uint32_t allocation_protect;
+	uint32_t region_size;
+	uint32_t state;
+	uint32_t protect;
+	uint32_t type;
+}
+X_MEMORY_BASIC_INFORMATION;
+#pragma pack(pop)
+
+
 X_STATUS xeNtAllocateVirtualMemory(
     uint32_t* base_addr_ptr, uint32_t* region_size_ptr,
     uint32_t allocation_type, uint32_t protect_bits,

From b618f38e6296eee0e13303500aeb949261a2faca Mon Sep 17 00:00:00 2001
From: wszechpolak <wszechpolak@yahoo.com>
Date: Thu, 14 Aug 2014 22:10:11 +0200
Subject: [PATCH 2/2] Pass base address without reference

---
 src/xenia/kernel/xboxkrnl_memory.cc | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/xenia/kernel/xboxkrnl_memory.cc b/src/xenia/kernel/xboxkrnl_memory.cc
index abe96d4a0..487d4757a 100644
--- a/src/xenia/kernel/xboxkrnl_memory.cc
+++ b/src/xenia/kernel/xboxkrnl_memory.cc
@@ -190,9 +190,10 @@ SHIM_CALL NtFreeVirtualMemory_shim(
 
 
 X_STATUS xeNtQueryVirtualMemory(
-    uint32_t* base_addr_ptr, X_MEMORY_BASIC_INFORMATION *memory_basic_information, bool swap) {
+    uint32_t base_address, X_MEMORY_BASIC_INFORMATION *memory_basic_information, bool swap) {
 
-  memory_basic_information->base_address        = XEROUNDUP(*base_addr_ptr, 4096);
+  // Just pretend that there is no virtual address allocated at given base address
+  memory_basic_information->base_address        = XEROUNDUP(base_address, 4096);
   memory_basic_information->allocation_base     = NULL;
   memory_basic_information->allocation_protect  = 0;
   memory_basic_information->region_size         = 0;
@@ -218,15 +219,15 @@ X_STATUS xeNtQueryVirtualMemory(
 
 SHIM_CALL NtQueryVirtualMemory_shim(
     PPCContext* ppc_state, KernelState* state) {
-  uint32_t base_addr_ptr = SHIM_GET_ARG_32(0);
+  uint32_t base_address = SHIM_GET_ARG_32(0);
   uint32_t memory_basic_information_ptr = SHIM_GET_ARG_32(1);
   X_MEMORY_BASIC_INFORMATION *memory_basic_information = (X_MEMORY_BASIC_INFORMATION*)SHIM_MEM_ADDR(memory_basic_information_ptr);
 
   XELOGD(
   	"NtQueryVirtualMemory(%.8X, %.8X)",
-    base_addr_ptr, memory_basic_information_ptr);
+    base_address, memory_basic_information_ptr);
 
-  X_STATUS result = xeNtQueryVirtualMemory(&base_addr_ptr, memory_basic_information, true);
+  X_STATUS result = xeNtQueryVirtualMemory(base_address, memory_basic_information, true);
   SHIM_SET_RETURN_32(result);
 }