[vbox-dev] Help requested to implement external host memory. (shared host-guest memory)

nev vbox at zavalon.com
Tue Jan 29 05:07:58 GMT 2013


Hi,

I have a need to implement a block of shared host (Linux) memory with
Guest (Windows).
Ideally I would like to do this without modifying the source and
implement everything in an Extension Pack.

I have an Extension pack that does work, and a Windows device driver,
BUT it requires the base source to be modified.

I believe these changes will not cause any incompatibility with existing
software.

Is it possible for these changes to be include into the source?
(You are free to use it with what ever license you require)

Or is there another way of achieving the same result without modifying
the source?
Maybe the experimental pass thru Pci device does something similar, if
so can you share that information with me.

The changes add a bit flag to a flag marked as "reserved for future use".
The patch uses the flag to control if new memory is allocated (and
zeroed) to the device (the existing case),
or uses the memory pointer passed into the function (and does not clear
the memory).
The patch also modifies a number of "Asserts".

A patch for Version 4.2.4 is attached

Thank you for your time.
Regards
Nev
-------------- next part --------------
diff -Naur -x '*.pyc' -x '.*' ../VirtualBox-4.2.4-unmodified/src/VBox/HostDrivers/Support/linux/SUPLib-linux.cpp src/VBox/HostDrivers/Support/linux/SUPLib-linux.cpp
--- ../VirtualBox-4.2.4-unmodified/src/VBox/HostDrivers/Support/linux/SUPLib-linux.cpp	2012-10-27 03:24:44.000000000 +1100
+++ src/VBox/HostDrivers/Support/linux/SUPLib-linux.cpp	2013-01-29 14:40:04.650527940 +1100
@@ -211,7 +211,11 @@
 int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages)
 {
     size_t cbMmap = (pThis->fSysMadviseWorks ? cPages : cPages + 2) << PAGE_SHIFT;
-    char *pvPages = (char *)mmap(NULL, cbMmap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    
+    /* for ExternalHostMemory use input value instead of allocating new region with mmap */
+    AssertMsg(*ppvPages && pThis->fSysMadviseWorks, ("ExternalHostMemory requires fSysMadviseWorks to be true\n"));
+    bool const externalHostMemory = *ppvPages != NULL;
+    char *pvPages = externalHostMemory ? (char*)*ppvPages : (char *)mmap(NULL, cbMmap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
     if (pvPages == MAP_FAILED)
         return VERR_NO_MEMORY;
 
@@ -238,7 +242,8 @@
         mprotect(pvPages + cbMmap - PAGE_SIZE, PAGE_SIZE, PROT_NONE);
         *ppvPages = pvPages + PAGE_SIZE;
     }
-    memset(*ppvPages, 0, cPages << PAGE_SHIFT);
+    if (!externalHostMemory) /* for ExternalHostMemory do NOT zero the shared memory */
+        memset(*ppvPages, 0, cPages << PAGE_SHIFT);
     return VINF_SUCCESS;
 }
 
diff -Naur -x '*.pyc' -x '.*' ../VirtualBox-4.2.4-unmodified/src/VBox/HostDrivers/Support/SUPLib.cpp src/VBox/HostDrivers/Support/SUPLib.cpp
--- ../VirtualBox-4.2.4-unmodified/src/VBox/HostDrivers/Support/SUPLib.cpp	2012-10-27 03:24:44.000000000 +1100
+++ src/VBox/HostDrivers/Support/SUPLib.cpp	2013-01-29 14:35:47.506714566 +1100
@@ -1035,13 +1035,14 @@
      * Validate.
      */
     AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
-    *ppvPages = NULL;
+    if (!(fFlags & VBOX_EXT_HOST_MEM)) /* for ExternalHostMemory do NOT over write the input */
+        *ppvPages = NULL;
     AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
     if (pR0Ptr)
         *pR0Ptr = NIL_RTR0PTR;
     AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
     AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
-    AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
+    AssertReturn(!(fFlags & ~VBOX_EXT_HOST_MEM), VERR_INVALID_PARAMETER);
 
     /* fake */
     if (RT_UNLIKELY(g_u32FakeMode))
@@ -1066,7 +1067,7 @@
      * Use fallback for non-R0 mapping?
      */
     if (    !pR0Ptr
-        &&  !g_fSupportsPageAllocNoKernel)
+        &&  (!g_fSupportsPageAllocNoKernel || (fFlags & VBOX_EXT_HOST_MEM))) /* for ExternalHostMemory force Fallback */
         return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
 
     /*
diff -Naur -x '*.pyc' -x '.*' ../VirtualBox-4.2.4-unmodified/src/VBox/VMM/VMMR3/PGMPhys.cpp src/VBox/VMM/VMMR3/PGMPhys.cpp
--- ../VirtualBox-4.2.4-unmodified/src/VBox/VMM/VMMR3/PGMPhys.cpp	2012-10-27 03:25:11.000000000 +1100
+++ src/VBox/VMM/VMMR3/PGMPhys.cpp	2013-01-29 14:46:45.607539643 +1100
@@ -2461,9 +2461,11 @@
  *                          region. Otherwise it can be any number safe
  *                          UINT8_MAX.
  * @param   cb              The size of the region.  Must be page aligned.
- * @param   fFlags          Reserved for future use, must be zero.
- * @param   ppv             Where to store the pointer to the ring-3 mapping of
- *                          the memory.
+ * @param   fFlags          Reserved for future use, must be zero,
+ *                          except for device using ExternalHostMemory, 
+ *                          when it can be set to VBOX_EXT_HOST_MEM
+ * @param   ppv             Where to store the pointer to the ring-3 mapping of the memory. 
+ *                          Except for ExternalHostMemory when it is input/output
  * @param   pszDesc         The description.
  */
 VMMR3DECL(int) PGMR3PhysMMIO2Register(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS cb, uint32_t fFlags, void **ppv, const char *pszDesc)
@@ -2480,7 +2482,8 @@
     AssertReturn(pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion) == NULL, VERR_ALREADY_EXISTS);
     AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
     AssertReturn(cb, VERR_INVALID_PARAMETER);
-    AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
+    /* for ExternalHostMemory: fFlags can have VBOX_EXT_HOST_MEM set */
+    AssertReturn(!(fFlags & ~VBOX_EXT_HOST_MEM), VERR_INVALID_PARAMETER);
 
     const uint32_t cPages = cb >> PAGE_SHIFT;
     AssertLogRelReturn(((RTGCPHYS)cPages << PAGE_SHIFT) == cb, VERR_INVALID_PARAMETER);
@@ -2503,13 +2506,16 @@
     int rc = MMR3AdjustFixedReservation(pVM, cPages, pszDesc);
     if (RT_SUCCESS(rc))
     {
-        void *pvPages;
+        void *pvPages = *ppv; /* for ExternalHostMemory pass on input value */
         PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(SUPPAGE));
         if (RT_SUCCESS(rc))
-            rc = SUPR3PageAllocEx(cPages, 0 /*fFlags*/, &pvPages, NULL /*pR0Ptr*/, paPages);
+            /* for ExternalHostMemory pass input fFlags */
+            rc = SUPR3PageAllocEx(cPages, fFlags, &pvPages, NULL /*pR0Ptr*/, paPages);
         if (RT_SUCCESS(rc))
         {
-            memset(pvPages, 0, cPages * PAGE_SIZE);
+            /* for ExternalHostMemory do NOT zero the shared memory */
+            if (!(fFlags & VBOX_EXT_HOST_MEM)) 
+                memset(pvPages, 0, cPages * PAGE_SIZE);
 
             /*
              * Create the MMIO2 range record for it.
diff -Naur -x '*.pyc' -x '.*' ../VirtualBox-4.2.4-unmodified/include/VBox/sup.h include/VBox/sup.h
--- ../VirtualBox-4.2.4-unmodified/include/VBox/sup.h	2012-10-27 03:23:19.000000000 +1100
+++ include/VBox/sup.h	2013-01-29 14:33:17.136480543 +1100
@@ -37,6 +37,7 @@
 struct VTGOBJHDR;
 struct VTGPROBELOC;
 
+#define VBOX_EXT_HOST_MEM (1 << 0) /* used by the device in previously reserved fFlag, to enable ExternalHostMemory*/
 
 /** @defgroup   grp_sup     The Support Library API
  * @{


More information about the vbox-dev mailing list