Skip to contentSkip to conversation

Windows host support bugfixes

PATCH In review Filip Navara 1 participant 2 messages Apr 10, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Windows host support bugfixes

Hi,

the attached patch solves the memory corruption problems which caused QVM86 to crash on Windows NT host. The underlying problem was that the guest physical memory was allocated using malloc and wasn't aligned to page boundary which later caused problems when mapping the pages in the kernel module...

Regards,
Filip Navara

2004-04-10 Filip Navara <[email protected]>

* README: Fix typos.
* patch.qvm86: Add patch to force page aligned allocations on WinNT hosts.
* qvm86-win32.h: Ditto.
* qvm86-win32.c (host_alloc_pages, host_free_pages): Allocate pages from non-cached memory instead of standard memory pool. (host_map_user): Guard for unaligned mappings, better error handling. (host_unmap_user): Fix a memory leak.

Index: README
===================================================================
RCS file: /cvsroot/qvm86/qvm86/README,v
retrieving revision 1.4

README +2 -2
diff -u -r1.4 README
@@ -27,8 +27,8 @@
 /dev/qvm86 should be created automatically when you load the module.
 
 On Windows hosts you need to copy qvm86.sys to windows/system32/drivers, apply
-avm86.reg, and do "net start qvm86".  Windows host support is extremely
-experimental.  The windows code has ben built with the mingw32 toolchains.
+qvm86.reg and do "net start qvm86".  Windows host support is extremely
+experimental.  The windows code has been built with the mingw32 toolchains.
 It is possible to compile on cygwin, but some hacking of makefiles is
 required to ensure -mno-cygwin is used.
 

Index: patch.qvm86
===================================================================
RCS file: /cvsroot/qvm86/qvm86/patch.qvm86,v
retrieving revision 1.3

patch.qvm86 +47 -2

61 lines not on this page · raw message

Index: qvm86-win32.c
===================================================================
RCS file: /cvsroot/qvm86/qvm86/qvm86-win32.c,v
retrieving revision 1.1

qvm86-win32.c +17 -3
diff -u -r1.1 qvm86-win32.c
@@ -27,7 +27,7 @@
 {
     void * ptr;
 
-    ptr = (void *)ExAllocatePoolWithTag(NonPagedPool, size, TAG('Q','V','M','Z'));
+    ptr = ExAllocatePoolWithTag(NonPagedPool, size, TAG('Q','V','M','Z'));
     if (!ptr)
         return NULL;
     RtlZeroMemory(ptr, size);
@@ -39,7 +39,7 @@
 {
     char * ptr;
 
-    ptr = (void *)ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE * count, TAG('Q','V','M','P'));
+    ptr = MmAllocateNonCachedMemory(PAGE_SIZE * count);
     if (!ptr)
         return NULL;
     RtlZeroMemory(ptr, PAGE_SIZE * count);
@@ -59,7 +59,7 @@
 {
     if (!ptr)
         return;
-    ExFreePool(ptr);
+    MmFreeNonCachedMemory(ptr, PAGE_SIZE * count);
 }
 
 unsigned long
@@ -76,9 +76,21 @@
     PMDL mdl;
     PPFN_NUMBER mdl_pages;
 
+    /* fail if the user address isn't aligned */
+    if (useraddr & 0xfff)
+    {
+        DbgPrint("qvm86: Tried to map unaligned user memory (%p)\n", useraddr);
+        map->host = NULL;
+        return;
+    }
+
     mdl = ExAllocatePool(NonPagedPool, sizeof(MDL) + sizeof(PFN_NUMBER));
     if (mdl == NULL)
+    {
+        DbgPrint("qvm86: Not enough memory for MDL structure\n");
+        map->host = NULL;
         return;
+    }
     mdl_pages = (PPFN_NUMBER)(mdl + 1);
 
     MmInitializeMdl(mdl, useraddr, PAGE_SIZE);
@@ -87,6 +99,7 @@
     map->host = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
     if (map->host == NULL)
     {
+        DbgPrint("qvm86: Can't map MDL to kernel address space\n");
         ExFreePool(mdl);
         return;
     }
@@ -104,6 +117,7 @@
 
     MmUnmapLockedPages(map->host, mdl);
     MmUnlockPages(mdl);
+    ExFreePool(mdl);
 
     /* TODO: This should probably be done elsewhere.  */
     map->host = NULL;
#1 lore raw
Quoted 6 lines · Filip Navara

Hi,

the attached patch solves the memory corruption problems which caused QVM86 to crash on Windows NT host. The underlying problem was that the guest physical memory was allocated using malloc and wasn't aligned to page boundary which later caused problems when mapping the pages in the kernel module...

Regards,
Filip Navara

Sorry, this was supposed to go to qvm86-devel mailing list...

- Filip