[ros-kernel] Re: MmMapLockedPages patch review

Anich Gregor blight at blight.eu.org
Sun Mar 7 17:27:53 CET 2004


Hi!

I do not know much about memory management but maybe something like the
following patch could work?
By the way, I have not really tested this.

-- blight

Index: ntoskrnl/mm/mdl.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/mm/mdl.c,v
retrieving revision 1.59
diff -u -a -b -r1.59 mdl.c
--- ntoskrnl/mm/mdl.c   6 Mar 2004 22:21:20 -0000       1.59
+++ ntoskrnl/mm/mdl.c   7 Mar 2004 02:29:47 -0000
@@ -152,6 +152,7 @@
    KIRQL oldIrql;
    ULONG RegionSize;
    ULONG StartingOffset;
+   PEPROCESS CurrentProcess, OldProcess;

    DPRINT("MmMapLockedPages(Mdl %x, AccessMode %x)\n", Mdl, AccessMode);

@@ -160,14 +161,47 @@
        return(Mdl->MappedSystemVa);
      }

+   /* Calculate the number of pages required. */
+   RegionSize = PAGE_ROUND_UP(Mdl->ByteCount + Mdl->ByteOffset) / PAGE_SIZE;
+
    if (AccessMode == UserMode)
      {
-       DPRINT1("MDL mapping to user-mode not yet handled.\n");
-       KEBUGCHECK(0);
+       MEMORY_AREA *Result;
+       LARGE_INTEGER BoundaryAddressMultiple;
+       NTSTATUS Status;
+
+       BoundaryAddressMultiple.QuadPart = 0;
+       Base = NULL;
+
+       CurrentProcess = OldProcess = PsGetCurrentProcess();
+       if (Mdl->Process != CurrentProcess)
+            {
+           KeAttachProcess(Mdl->Process);
+           CurrentProcess = Mdl->Process;
      }

-   /* Calculate the number of pages required. */
-   RegionSize = PAGE_ROUND_UP(Mdl->ByteCount + Mdl->ByteOffset) / PAGE_SIZE;
+       MmLockAddressSpace(&CurrentProcess->AddressSpace);
+       Status = MmCreateMemoryArea(CurrentProcess,
+                             &CurrentProcess->AddressSpace,
+                             MEMORY_AREA_MDL_MAPPING,
+                             &Base,
+                             RegionSize * PAGE_SIZE,
+                             0,
+                             &Result,
+                             FALSE,
+                             FALSE,
+                             BoundaryAddressMultiple);
+       if (!NT_SUCCESS(Status))
+         {
+           MmUnlockAddressSpace(&CurrentProcess->AddressSpace);
+           KEBUGCHECK(0);
+           /* FIXME: handle this? */
+         }
+       MmUnlockAddressSpace(&CurrentProcess->AddressSpace);
+     }
+   else
+     {
+       CurrentProcess = OldProcess = NULL;

    /* Allocate that number of pages from the mdl mapping region. */
    KeAcquireSpinLock(&MiMdlMappingRegionLock, &oldIrql);
@@ -188,6 +222,9 @@
    }

    KeReleaseSpinLock(&MiMdlMappingRegionLock, oldIrql);
+     }
+
+

    /* Set the virtual mappings for the MDL pages. */
    MdlPages = (PULONG)(Mdl + 1);
@@ -198,7 +235,7 @@
        PHYSICAL_ADDRESS dummyJunkNeeded;
        dummyJunkNeeded.QuadPart = MdlPages[i];
 #endif
-       Status = MmCreateVirtualMapping(NULL,
+       Status = MmCreateVirtualMapping(CurrentProcess,
                                       (PVOID)((ULONG)Base+(i*PAGE_SIZE)),
                                       PAGE_READWRITE,
 #if defined(__GNUC__)
@@ -214,6 +251,11 @@
         }
      }

+   if (AccessMode == UserMode && CurrentProcess != OldProcess)
+     {
+       KeDetachProcess();
+     }
+
    /* Mark the MDL has having being mapped. */
    Mdl->MdlFlags = Mdl->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
    Mdl->MappedSystemVa = (char*)Base + Mdl->ByteOffset;


More information about the Ros-kernel mailing list