Fixed the check for the MEM_TOP_DOWN flag in MmMapViewOfSection.
[reactos.git] / reactos / ntoskrnl / mm / section.c
index 931a659..584c9da 100644 (file)
@@ -1,11 +1,47 @@
 /* $Id$
  *
- * COPYRIGHT:       See COPYING in the top level directory
+ * Copyright (C) 1998-2005 ReactOS Team (and the authors from the programmers section)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/mm/section.c
  * PURPOSE:         Implements section objects
- * 
- * PROGRAMMERS:     David Welch (welch@mcmail.com)
+ *
+ * PROGRAMMERS:     Rex Jolliff
+ *                  David Welch
+ *                  Eric Kohl
+ *                  Emanuele Aliberti
+ *                  Eugene Ingerman
+ *                  Hartmut Birr
+ *                  Casper Hornstrup
+ *                  KJK::Hyperion
+ *                  Guido de Jong
+ *                  Ge van Geldorp
+ *                  Royce Mitchell III
+ *                  Filip Navara
+ *                  Aleksey Bragin 
+ *                  Jason Filby
+ *                  Thomas Weidenmueller
+ *                  Gunnar Andre' Dalsnes
+ *                  Mike Nordell
+ *                  Alex Ionescu
+ *                  Gregor Anich
+ *                  Steven Edwards
+ *                  Herve Poussineau
  */
 
 /* INCLUDES *****************************************************************/
@@ -38,9 +74,6 @@ static GENERIC_MAPPING MmpSectionMapping = {
          STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
          SECTION_ALL_ACCESS};
 
-#define TAG_MM_SECTION_SEGMENT   TAG('M', 'M', 'S', 'S')
-#define TAG_SECTION_PAGE_TABLE   TAG('M', 'S', 'P', 'T')
-
 #define PAGE_FROM_SSE(E)         ((E) & 0xFFFFF000)
 #define PFN_FROM_SSE(E)          ((E) >> PAGE_SHIFT)
 #define SHARE_COUNT_FROM_SSE(E)  (((E) & 0x00000FFE) >> 1)
@@ -124,6 +157,7 @@ MmFreePageTablesSectionSegment(PMM_SECTION_SEGMENT Segment)
 }
 
 VOID
+NTAPI
 MmFreeSectionSegments(PFILE_OBJECT FileObject)
 {
    if (FileObject->SectionObjectPointer->ImageSectionObject != NULL)
@@ -169,18 +203,21 @@ MmFreeSectionSegments(PFILE_OBJECT FileObject)
 }
 
 VOID
+NTAPI
 MmLockSectionSegment(PMM_SECTION_SEGMENT Segment)
 {
    ExAcquireFastMutex(&Segment->Lock);
 }
 
 VOID
+NTAPI
 MmUnlockSectionSegment(PMM_SECTION_SEGMENT Segment)
 {
    ExReleaseFastMutex(&Segment->Lock);
 }
 
 VOID
+NTAPI
 MmSetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
                              ULONG Offset,
                              ULONG Entry)
@@ -201,7 +238,7 @@ MmSetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
       {
          Table =
             Segment->PageDirectory.PageTables[DirectoryOffset] =
-               ExAllocatePoolWithTag(NonPagedPool, sizeof(SECTION_PAGE_TABLE),
+               ExAllocatePoolWithTag(PagedPool, sizeof(SECTION_PAGE_TABLE),
                                      TAG_SECTION_PAGE_TABLE);
          if (Table == NULL)
          {
@@ -217,6 +254,7 @@ MmSetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
 
 
 ULONG
+NTAPI
 MmGetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
                              ULONG Offset)
 {
@@ -247,6 +285,7 @@ MmGetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
 }
 
 VOID
+NTAPI
 MmSharePageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
                                ULONG Offset)
 {
@@ -272,6 +311,7 @@ MmSharePageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
 }
 
 BOOLEAN
+NTAPI
 MmUnsharePageEntrySectionSegment(PSECTION_OBJECT Section,
                                  PMM_SECTION_SEGMENT Segment,
                                  ULONG Offset,
@@ -371,7 +411,7 @@ MmUnsharePageEntrySectionSegment(PSECTION_OBJECT Section,
                {
                   /*
                    * FIXME:
-                   *   We hold all locks. Nobody can do something with the current 
+                   *   We hold all locks. Nobody can do something with the current
                    *   process and the current segment (also not within an other process).
                    */
                   NTSTATUS Status;
@@ -420,6 +460,7 @@ BOOL MiIsPageFromCache(PMEMORY_AREA MemoryArea,
 }
 
 NTSTATUS
+NTAPI
 MiReadPage(PMEMORY_AREA MemoryArea,
            ULONG SegOffset,
            PPFN_TYPE Page)
@@ -591,6 +632,7 @@ MiReadPage(PMEMORY_AREA MemoryArea,
 }
 
 NTSTATUS
+NTAPI
 MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
                              MEMORY_AREA* MemoryArea,
                              PVOID Address,
@@ -624,7 +666,8 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
    }
 
    PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
-   Offset = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress;
+   Offset = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress 
+            + MemoryArea->Data.SectionData.ViewOffset;
 
    Segment = MemoryArea->Data.SectionData.Segment;
    Section = MemoryArea->Data.SectionData.Section;
@@ -716,7 +759,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
          if (PAGE_FROM_SSE(Entry) == 0 || HasSwapEntry)
          {
             /*
-             * The page was a private page in another or in our address space 
+             * The page was a private page in another or in our address space
             */
             MmUnlockSectionSegment(Segment);
             MmspCompleteAndReleasePageOp(PageOp);
@@ -730,7 +773,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
          /* FIXME: Should we call MmCreateVirtualMappingUnsafe if
           * (Section->AllocationAttributes & SEC_PHYSICALMEMORY) is true?
           */
-         Status = MmCreateVirtualMapping(MemoryArea->Process,
+         Status = MmCreateVirtualMapping(AddressSpace->Process,
                                          Address,
                                          Attributes,
                                          &Page,
@@ -740,7 +783,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
             DbgPrint("Unable to create virtual mapping\n");
             KEBUGCHECK(0);
          }
-         MmInsertRmap(Page, MemoryArea->Process, (PVOID)PAddress);
+         MmInsertRmap(Page, AddressSpace->Process, (PVOID)PAddress);
       }
       if (Locked)
       {
@@ -831,7 +874,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       /*
       * Just map the desired physical page
       */
-      Page = (Offset + MemoryArea->Data.SectionData.ViewOffset) >> PAGE_SHIFT;
+      Page = Offset >> PAGE_SHIFT;
       Status = MmCreateVirtualMappingUnsafe(AddressSpace->Process,
                                             Address,
                                             Region->Protect,
@@ -864,7 +907,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
    /*
     * Map anonymous memory for BSS sections
     */
-   if (Segment->Characteristics & IMAGE_SCN_LNK_OTHER)
+   if (Segment->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
    {
       MmUnlockSectionSegment(Segment);
       Status = MmRequestPageMemoryConsumer(MC_USER, FALSE, &Page);
@@ -907,7 +950,6 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
    /*
     * Get the entry corresponding to the offset within the section
     */
-   Offset += MemoryArea->Data.SectionData.ViewOffset;
    Entry = MmGetPageEntrySectionSegment(Segment, Offset);
 
    if (Entry == 0)
@@ -1109,6 +1151,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
 }
 
 NTSTATUS
+NTAPI
 MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
                          MEMORY_AREA* MemoryArea,
                          PVOID Address,
@@ -1139,7 +1182,8 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
     * Find the offset of the page
     */
    PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
-   Offset = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress;
+   Offset = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress 
+            + MemoryArea->Data.SectionData.ViewOffset;
 
    Segment = MemoryArea->Data.SectionData.Segment;
    Section = MemoryArea->Data.SectionData.Section;
@@ -1234,6 +1278,7 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
     */
    MiCopyFromUserPage(NewPage, PAddress);
 
+   MmLockAddressSpace(AddressSpace);
    /*
     * Delete the old entry.
     */
@@ -1242,7 +1287,6 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    /*
     * Set the PTE to point to the new page
     */
-   MmLockAddressSpace(AddressSpace);
    Status = MmCreateVirtualMapping(AddressSpace->Process,
                                    Address,
                                    Region->Protect,
@@ -1254,7 +1298,6 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
       KEBUGCHECK(0);
       return(Status);
    }
-   MmInsertRmap(NewPage, AddressSpace->Process, PAddress);
    if (!NT_SUCCESS(Status))
    {
       DbgPrint("Unable to create virtual mapping\n");
@@ -1270,6 +1313,7 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
     * Unshare the old page.
     */
    MmDeleteRmap(OldPage, AddressSpace->Process, PAddress);
+   MmInsertRmap(NewPage, AddressSpace->Process, PAddress);
    MmLockSectionSegment(Segment);
    MmUnsharePageEntrySectionSegment(Section, Segment, Offset, FALSE, FALSE);
    MmUnlockSectionSegment(Segment);
@@ -1288,6 +1332,11 @@ MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
    PFN_TYPE Page;
 
    PageOutContext = (MM_SECTION_PAGEOUT_CONTEXT*)Context;
+   if (Process)
+   {
+      MmLockAddressSpace(&Process->AddressSpace);
+   }
+
    MmDeleteVirtualMapping(Process,
                           Address,
                           FALSE,
@@ -1299,21 +1348,29 @@ MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
    }
    if (!PageOutContext->Private)
    {
+      MmLockSectionSegment(PageOutContext->Segment);
       MmUnsharePageEntrySectionSegment(PageOutContext->Section,
                                        PageOutContext->Segment,
                                        PageOutContext->Offset,
                                        PageOutContext->WasDirty,
                                        TRUE);
+      MmUnlockSectionSegment(PageOutContext->Segment);
    }
-   else
+   if (Process)
+   {
+      MmUnlockAddressSpace(&Process->AddressSpace);
+   }
+   
+   if (PageOutContext->Private)
    {
       MmReleasePageMemoryConsumer(MC_USER, Page);
    }
 
-   DPRINT("PhysicalAddress %I64x, Address %x\n", Page, Address);
+   DPRINT("PhysicalAddress %x, Address %x\n", Page << PAGE_SHIFT, Address);
 }
 
 NTSTATUS
+NTAPI
 MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
                      MEMORY_AREA* MemoryArea,
                      PVOID Address,
@@ -1338,7 +1395,8 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
    Context.Segment = MemoryArea->Data.SectionData.Segment;
    Context.Section = MemoryArea->Data.SectionData.Section;
 
-   Context.Offset = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress;
+   Context.Offset = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress 
+                    + MemoryArea->Data.SectionData.ViewOffset;
    FileOffset = Context.Offset + Context.Segment->FileOffset;
 
    IsImageSection = Context.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
@@ -1392,7 +1450,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
     * Prepare the context structure for the rmap delete call.
     */
    Context.WasDirty = FALSE;
-   if (Context.Segment->Characteristics & IMAGE_SCN_LNK_OTHER ||
+   if (Context.Segment->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
          IS_SWAP_FROM_SSE(Entry) ||
          PFN_FROM_SSE(Entry) != Page)
    {
@@ -1515,9 +1573,11 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
    else if (!Context.WasDirty && Context.Private && SwapEntry != 0)
    {
       MmSetSavedSwapEntryPage(Page, 0);
+      MmLockAddressSpace(AddressSpace);
       Status = MmCreatePageFileMapping(AddressSpace->Process,
                                        Address,
                                        SwapEntry);
+      MmUnlockAddressSpace(AddressSpace);
       if (!NT_SUCCESS(Status))
       {
          KEBUGCHECK(0);
@@ -1537,20 +1597,20 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       if (SwapEntry == 0)
       {
          MmShowOutOfSpaceMessagePagingFile();
-
+         MmLockAddressSpace(AddressSpace);
          /*
           * For private pages restore the old mappings.
           */
          if (Context.Private)
          {
-            Status = MmCreateVirtualMapping(MemoryArea->Process,
+            Status = MmCreateVirtualMapping(AddressSpace->Process,
                                             Address,
                                             MemoryArea->Attributes,
                                             &Page,
                                             1);
-            MmSetDirtyPage(MemoryArea->Process, Address);
+            MmSetDirtyPage(AddressSpace->Process, Address);
             MmInsertRmap(Page,
-                         MemoryArea->Process,
+                         AddressSpace->Process,
                          Address);
          }
          else
@@ -1560,18 +1620,19 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
              * set it back into the section segment entry so we don't loose
              * our copy. Otherwise it will be handled by the cache manager.
              */
-            Status = MmCreateVirtualMapping(MemoryArea->Process,
+            Status = MmCreateVirtualMapping(AddressSpace->Process,
                                             Address,
                                             MemoryArea->Attributes,
                                             &Page,
                                             1);
-            MmSetDirtyPage(MemoryArea->Process, Address);
+            MmSetDirtyPage(AddressSpace->Process, Address);
             MmInsertRmap(Page,
-                         MemoryArea->Process,
+                         AddressSpace->Process,
                          Address);
             Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
             MmSetPageEntrySectionSegment(Context.Segment, Context.Offset, Entry);
          }
+         MmUnlockAddressSpace(AddressSpace);
          PageOp->Status = STATUS_UNSUCCESSFUL;
          MmspCompleteAndReleasePageOp(PageOp);
          return(STATUS_PAGEFILE_QUOTA);
@@ -1590,32 +1651,34 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
        * As above: undo our actions.
        * FIXME: Also free the swap page.
        */
+      MmLockAddressSpace(AddressSpace);
       if (Context.Private)
       {
-         Status = MmCreateVirtualMapping(MemoryArea->Process,
+         Status = MmCreateVirtualMapping(AddressSpace->Process,
                                          Address,
                                          MemoryArea->Attributes,
                                          &Page,
                                          1);
-         MmSetDirtyPage(MemoryArea->Process, Address);
+         MmSetDirtyPage(AddressSpace->Process, Address);
          MmInsertRmap(Page,
-                      MemoryArea->Process,
+                      AddressSpace->Process,
                       Address);
       }
       else
       {
-         Status = MmCreateVirtualMapping(MemoryArea->Process,
+         Status = MmCreateVirtualMapping(AddressSpace->Process,
                                          Address,
                                          MemoryArea->Attributes,
                                          &Page,
                                          1);
-         MmSetDirtyPage(MemoryArea->Process, Address);
+         MmSetDirtyPage(AddressSpace->Process, Address);
          MmInsertRmap(Page,
-                      MemoryArea->Process,
+                      AddressSpace->Process,
                       Address);
          Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
          MmSetPageEntrySectionSegment(Context.Segment, Context.Offset, Entry);
       }
+      MmUnlockAddressSpace(AddressSpace);
       PageOp->Status = STATUS_UNSUCCESSFUL;
       MmspCompleteAndReleasePageOp(PageOp);
       return(STATUS_UNSUCCESSFUL);
@@ -1638,9 +1701,11 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
 
    if (Context.Private)
    {
-      Status = MmCreatePageFileMapping(MemoryArea->Process,
+      MmLockAddressSpace(AddressSpace);
+      Status = MmCreatePageFileMapping(AddressSpace->Process,
                                        Address,
                                        SwapEntry);
+      MmUnlockAddressSpace(AddressSpace);
       if (!NT_SUCCESS(Status))
       {
          KEBUGCHECK(0);
@@ -1658,6 +1723,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
 }
 
 NTSTATUS
+NTAPI
 MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
                        PMEMORY_AREA MemoryArea,
                        PVOID Address,
@@ -1678,7 +1744,8 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
 
    Address = (PVOID)PAGE_ROUND_DOWN(Address);
 
-   Offset = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress;
+   Offset = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress 
+            + MemoryArea->Data.SectionData.ViewOffset;
 
    /*
     * Get the segment and section.
@@ -1699,7 +1766,7 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
        * memory area was mapped at an offset in the file which is page aligned
        * then note this is a direct mapped page.
        */
-      if ((Offset + MemoryArea->Data.SectionData.ViewOffset % PAGE_SIZE) == 0 &&
+      if (((Offset + Segment->FileOffset) % PAGE_SIZE) == 0 &&
             (Offset + PAGE_SIZE <= Segment->RawLength || !IsImageSection))
       {
          DirectMapped = TRUE;
@@ -1734,7 +1801,7 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
    /*
     * Check for a private (COWed) page.
     */
-   if (Segment->Characteristics & IMAGE_SCN_LNK_OTHER ||
+   if (Segment->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
          IS_SWAP_FROM_SSE(Entry) ||
          PFN_FROM_SSE(Entry) != Page)
    {
@@ -1757,7 +1824,7 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
    if (DirectMapped && !Private)
    {
       ASSERT(SwapEntry == 0);
-      CcRosMarkDirtyCacheSegment(Bcb, Offset + MemoryArea->Data.SectionData.ViewOffset);
+      CcRosMarkDirtyCacheSegment(Bcb, Offset + Segment->FileOffset);
       PageOp->Status = STATUS_SUCCESS;
       MmspCompleteAndReleasePageOp(PageOp);
       return(STATUS_SUCCESS);
@@ -1842,12 +1909,13 @@ MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
             ULONG Entry;
             PFN_TYPE Page;
 
-            Offset = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress;
+            Offset = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress 
+                     + MemoryArea->Data.SectionData.ViewOffset;
             Entry = MmGetPageEntrySectionSegment(Segment, Offset);
             Page = MmGetPfnForProcess(AddressSpace->Process, Address);
 
             Protect = PAGE_READONLY;
-            if (Segment->Characteristics & IMAGE_SCN_LNK_OTHER ||
+            if (Segment->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
                   IS_SWAP_FROM_SSE(Entry) ||
                   PFN_FROM_SSE(Entry) != Page)
             {
@@ -1865,6 +1933,7 @@ MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
 }
 
 NTSTATUS
+NTAPI
 MmProtectSectionView(PMADDRESS_SPACE AddressSpace,
                      PMEMORY_AREA MemoryArea,
                      PVOID BaseAddress,
@@ -1901,9 +1970,7 @@ MmQuerySectionView(PMEMORY_AREA MemoryArea,
    PMM_REGION Region;
    PVOID RegionBaseAddress;
    PSECTION_OBJECT Section;
-   PLIST_ENTRY CurrentEntry;
-   PMEMORY_AREA CurrentMArea;
-   KIRQL oldIrql;
+   PMM_SECTION_SEGMENT Segment;
 
    Region = MmFindRegion((PVOID)MemoryArea->StartingAddress,
                          &MemoryArea->Data.SectionData.RegionListHead,
@@ -1912,39 +1979,22 @@ MmQuerySectionView(PMEMORY_AREA MemoryArea,
    {
       return STATUS_UNSUCCESSFUL;
    }
+
    Section = MemoryArea->Data.SectionData.Section;
    if (Section->AllocationAttributes & SEC_IMAGE)
    {
-      KeAcquireSpinLock(&Section->ViewListLock, &oldIrql);
-      CurrentEntry = Section->ViewListHead.Flink;
-      Info->AllocationBase = NULL;
-      while (CurrentEntry != &Section->ViewListHead)
-      {
-         CurrentMArea = CONTAINING_RECORD(CurrentEntry, MEMORY_AREA, Data.SectionData.ViewListEntry);
-         CurrentEntry = CurrentEntry->Flink;
-         if (Info->AllocationBase == NULL)
-         {
-            Info->AllocationBase = CurrentMArea->StartingAddress;
-         }
-         else if (CurrentMArea->StartingAddress < Info->AllocationBase)
-         {
-            Info->AllocationBase = CurrentMArea->StartingAddress;
-         }
-      }
-      KeReleaseSpinLock(&Section->ViewListLock, oldIrql);
-      Info->BaseAddress = RegionBaseAddress;
-      Info->AllocationProtect = MemoryArea->Attributes;
+      Segment = MemoryArea->Data.SectionData.Segment;
+      Info->AllocationBase = (PBYTE)MemoryArea->StartingAddress - Segment->VirtualAddress;
       Info->Type = MEM_IMAGE;
    }
    else
    {
-      Info->BaseAddress = RegionBaseAddress;
       Info->AllocationBase = MemoryArea->StartingAddress;
-      Info->AllocationProtect = MemoryArea->Attributes;
       Info->Type = MEM_MAPPED;
    }
-   Info->RegionSize = PAGE_ROUND_UP((ULONG_PTR)MemoryArea->EndingAddress -
-                                    (ULONG_PTR)MemoryArea->StartingAddress);
+   Info->BaseAddress = RegionBaseAddress;
+   Info->AllocationProtect = MemoryArea->Attributes;
+   Info->RegionSize = Region->Length;
    Info->State = MEM_COMMIT;
    Info->Protect = Region->Protect;
 
@@ -1953,6 +2003,7 @@ MmQuerySectionView(PMEMORY_AREA MemoryArea,
 }
 
 VOID
+NTAPI
 MmpFreePageFileSegment(PMM_SECTION_SEGMENT Segment)
 {
    ULONG Length;
@@ -2068,34 +2119,15 @@ MmpCloseSection(PVOID ObjectBody,
           ObjectBody, HandleCount, ObGetObjectPointerCount(ObjectBody));
 }
 
-NTSTATUS STDCALL
-MmpCreateSection(PVOID ObjectBody,
-                 PVOID Parent,
-                 PWSTR RemainingPath,
-                 POBJECT_ATTRIBUTES ObjectAttributes)
-{
-   DPRINT("MmpCreateSection(ObjectBody %x, Parent %x, RemainingPath %S)\n",
-          ObjectBody, Parent, RemainingPath);
-
-   if (RemainingPath == NULL)
-   {
-      return(STATUS_SUCCESS);
-   }
-
-   if (wcschr(RemainingPath+1, L'\\') != NULL)
-   {
-      return(STATUS_UNSUCCESSFUL);
-   }
-   return(STATUS_SUCCESS);
-}
-
-NTSTATUS INIT_FUNCTION
+NTSTATUS
+INIT_FUNCTION
+NTAPI
 MmCreatePhysicalMemorySection(VOID)
 {
    PSECTION_OBJECT PhysSection;
    NTSTATUS Status;
    OBJECT_ATTRIBUTES Obj;
-   UNICODE_STRING Name = ROS_STRING_INITIALIZER(L"\\Device\\PhysicalMemory");
+   UNICODE_STRING Name = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
    LARGE_INTEGER SectionSize;
 
    /*
@@ -2120,48 +2152,49 @@ MmCreatePhysicalMemorySection(VOID)
       DbgPrint("Failed to create PhysicalMemory section\n");
       KEBUGCHECK(0);
    }
+   Status = ObInsertObject(PhysSection,
+                           NULL,
+                           SECTION_ALL_ACCESS,
+                           0,
+                           NULL,
+                           NULL);
+   if (!NT_SUCCESS(Status))
+   {
+      ObDereferenceObject(PhysSection);
+   }
    PhysSection->AllocationAttributes |= SEC_PHYSICALMEMORY;
+   PhysSection->Segment->Flags &= ~MM_PAGEFILE_SEGMENT;
 
    return(STATUS_SUCCESS);
 }
 
-NTSTATUS INIT_FUNCTION
+NTSTATUS
+INIT_FUNCTION
+NTAPI
 MmInitSectionImplementation(VOID)
 {
-   MmSectionObjectType = ExAllocatePool(NonPagedPool,sizeof(OBJECT_TYPE));
-
-   RtlInitUnicodeString(&MmSectionObjectType->TypeName, L"Section");
-
-   MmSectionObjectType->Tag = TAG('S', 'E', 'C', 'T');
-   MmSectionObjectType->TotalObjects = 0;
-   MmSectionObjectType->TotalHandles = 0;
-   MmSectionObjectType->PeakObjects = 0;
-   MmSectionObjectType->PeakHandles = 0;
-   MmSectionObjectType->PagedPoolCharge = 0;
-   MmSectionObjectType->NonpagedPoolCharge = sizeof(SECTION_OBJECT);
-   MmSectionObjectType->Mapping = &MmpSectionMapping;
-   MmSectionObjectType->Dump = NULL;
-   MmSectionObjectType->Open = NULL;
-   MmSectionObjectType->Close = MmpCloseSection;
-   MmSectionObjectType->Delete = MmpDeleteSection;
-   MmSectionObjectType->Parse = NULL;
-   MmSectionObjectType->Security = NULL;
-   MmSectionObjectType->QueryName = NULL;
-   MmSectionObjectType->OkayToClose = NULL;
-   MmSectionObjectType->Create = MmpCreateSection;
-   MmSectionObjectType->DuplicationNotify = NULL;
+   OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
+   UNICODE_STRING Name;
 
-   /*
-    * NOTE: Do not register the section object type here because
-    * the object manager it not initialized yet!
-    * The section object type will be created in ObInit().
-    */
-   ObpCreateTypeObject(MmSectionObjectType);
+   DPRINT("Creating Section Object Type\n");
+  
+   /* Initialize the Section object type  */
+   RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
+   RtlInitUnicodeString(&Name, L"Section");
+   ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
+   ObjectTypeInitializer.DefaultPagedPoolCharge = sizeof(SECTION_OBJECT);
+   ObjectTypeInitializer.PoolType = PagedPool;
+   ObjectTypeInitializer.UseDefaultObject = TRUE;
+   ObjectTypeInitializer.GenericMapping = MmpSectionMapping;
+   ObjectTypeInitializer.DeleteProcedure = MmpDeleteSection;
+   ObjectTypeInitializer.CloseProcedure = MmpCloseSection;
+   ObpCreateTypeObject(&ObjectTypeInitializer, &Name, &MmSectionObjectType);
 
    return(STATUS_SUCCESS);
 }
 
 NTSTATUS
+NTAPI
 MmCreatePageFileSection(PSECTION_OBJECT *SectionObject,
                         ACCESS_MASK DesiredAccess,
                         POBJECT_ATTRIBUTES ObjectAttributes,
@@ -2206,8 +2239,6 @@ MmCreatePageFileSection(PSECTION_OBJECT *SectionObject,
    Section->SectionPageProtection = SectionPageProtection;
    Section->AllocationAttributes = AllocationAttributes;
    Section->Segment = NULL;
-   InitializeListHead(&Section->ViewListHead);
-   KeInitializeSpinLock(&Section->ViewListLock);
    Section->FileObject = NULL;
    Section->MaximumSize = MaximumSize;
    Segment = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_SECTION_SEGMENT),
@@ -2235,6 +2266,7 @@ MmCreatePageFileSection(PSECTION_OBJECT *SectionObject,
 
 
 NTSTATUS
+NTAPI
 MmCreateDataFileSection(PSECTION_OBJECT *SectionObject,
                         ACCESS_MASK DesiredAccess,
                         POBJECT_ATTRIBUTES ObjectAttributes,
@@ -2273,15 +2305,12 @@ MmCreateDataFileSection(PSECTION_OBJECT *SectionObject,
    {
       return(Status);
    }
-
    /*
     * Initialize it
     */
    Section->SectionPageProtection = SectionPageProtection;
    Section->AllocationAttributes = AllocationAttributes;
    Section->Segment = NULL;
-   InitializeListHead(&Section->ViewListHead);
-   KeInitializeSpinLock(&Section->ViewListLock);
 
    /*
     * Check file access required
@@ -2317,11 +2346,11 @@ MmCreateDataFileSection(PSECTION_OBJECT *SectionObject,
     * (as in case of the EXT2FS driver by Manoj Paul Joseph where the
     * standard file information is filled on first request).
     */
-   Status = NtQueryInformationFile(FileHandle,
-                                   &Iosb,
-                                   &FileInfo,
+   Status = IoQueryFileInformation(FileObject,
+                                   FileStandardInformation,
                                    sizeof(FILE_STANDARD_INFORMATION),
-                                   FileStandardInformation);
+                                   &FileInfo,
+                                   &Iosb.Information);
    if (!NT_SUCCESS(Status))
    {
       ObDereferenceObject(Section);
@@ -2351,11 +2380,10 @@ MmCreateDataFileSection(PSECTION_OBJECT *SectionObject,
 
    if (MaximumSize.QuadPart > FileInfo.EndOfFile.QuadPart)
    {
-      Status = NtSetInformationFile(FileHandle,
-                                    &Iosb,
-                                    &MaximumSize,
-                                    sizeof(LARGE_INTEGER),
-                                    FileAllocationInformation);
+      Status = IoSetInformation(FileObject,
+                                FileAllocationInformation,
+                                sizeof(LARGE_INTEGER),
+                                &MaximumSize);
       if (!NT_SUCCESS(Status))
       {
          ObDereferenceObject(Section);
@@ -2570,7 +2598,6 @@ ExeFmtpReadFile(IN PVOID File,
       KEBUGCHECK(STATUS_INVALID_PARAMETER_5);
    }
 
-   ASSERT(PAGE_SIZE <= MAXULONG);
    AdjustOffset = PAGE_ROUND_DOWN(FileOffset.u.LowPart);
    OffsetAdjustment = FileOffset.u.LowPart - AdjustOffset;
    FileOffset.u.LowPart = AdjustOffset;
@@ -2615,7 +2642,7 @@ ExeFmtpReadFile(IN PVOID File,
                           BufferSize,
                           &FileOffset,
                           NULL);
-   
+
       if(NT_SUCCESS(Status))
       {
          UsedSize = Iosb.Information;
@@ -2773,7 +2800,7 @@ MmspCheckSegmentBounds
           * TODO: relax the limitation on gaps. For example, gaps smaller than a
           * page could be OK (Windows seems to be OK with them), and larger gaps
           * could lead to image sections spanning several discontiguous regions
-          * (NtMapViewOfSection could then refuse to map them, and they could 
+          * (NtMapViewOfSection could then refuse to map them, and they could
           * e.g. only be allowed as parameters to NtCreateProcess, like on UNIX)
           */
          if ((ImageSectionObject->Segments[i - 1].VirtualAddress +
@@ -2804,6 +2831,7 @@ MmspPageAlignSegments
    ULONG i;
    ULONG LastSegment;
    BOOLEAN Initialized;
+   PMM_SECTION_SEGMENT EffectiveSegment;
 
    if (Flags & EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED)
    {
@@ -2813,11 +2841,10 @@ MmspPageAlignSegments
 
    Initialized = FALSE;
    LastSegment = 0;
+   EffectiveSegment = &ImageSectionObject->Segments[LastSegment];
 
    for ( i = 0; i < ImageSectionObject->NrSegments; ++ i )
    {
-      PMM_SECTION_SEGMENT EffectiveSegment = &ImageSectionObject->Segments[LastSegment];
-
       /*
        * The first segment requires special handling
        */
@@ -2827,25 +2854,25 @@ MmspPageAlignSegments
          ULONG_PTR VirtualOffset;
 
          VirtualAddress = EffectiveSegment->VirtualAddress;
-   
+
          /* Round down the virtual address to the nearest page */
          EffectiveSegment->VirtualAddress = PAGE_ROUND_DOWN(VirtualAddress);
-   
+
          /* Round up the virtual size to the nearest page */
          EffectiveSegment->Length = PAGE_ROUND_UP(VirtualAddress + EffectiveSegment->Length) -
                                     EffectiveSegment->VirtualAddress;
-   
+
          /* Adjust the raw address and size */
          VirtualOffset = VirtualAddress - EffectiveSegment->VirtualAddress;
-   
+
          if (EffectiveSegment->FileOffset < VirtualOffset)
          {
             return FALSE;
          }
-   
+
          /*
-          * Garbage in, garbage out: unaligned base addresses make the file 
-          * offset point in curious and odd places, but that's what we were 
+          * Garbage in, garbage out: unaligned base addresses make the file
+          * offset point in curious and odd places, but that's what we were
           * asked for
           */
          EffectiveSegment->FileOffset -= VirtualOffset;
@@ -2858,7 +2885,7 @@ MmspPageAlignSegments
 
          EndOfEffectiveSegment = EffectiveSegment->VirtualAddress + EffectiveSegment->Length;
          ASSERT((EndOfEffectiveSegment % PAGE_SIZE) == 0);
-   
+
          /*
           * The current segment begins exactly where the current effective
           * segment ended, therefore beginning a new effective segment
@@ -2871,11 +2898,14 @@ MmspPageAlignSegments
 
             EffectiveSegment = &ImageSectionObject->Segments[LastSegment];
 
-            /*
-             * Copy the current segment. If necessary, the effective segment
-             * will be expanded later
-             */
-            *EffectiveSegment = *Segment;
+            if (LastSegment != i)
+            {
+               /*
+                * Copy the current segment. If necessary, the effective segment
+                * will be expanded later
+                */
+               *EffectiveSegment = *Segment;
+            }
 
             /*
              * Page-align the virtual size. We know for sure the virtual address
@@ -2915,24 +2945,24 @@ MmspPageAlignSegments
             /*
              * Extend the file size
              */
-   
+
             /* Unaligned segments must be contiguous within the file */
             if (Segment->FileOffset != (EffectiveSegment->FileOffset +
                                         EffectiveSegment->RawLength))
             {
                return FALSE;
             }
-            
+
             EffectiveSegment->RawLength += Segment->RawLength;
-   
+
             /*
              * Extend the virtual size
              */
-            ASSERT(PAGE_ROUND_UP(Segment->VirtualAddress + Segment->Length) > EndOfEffectiveSegment);
-   
+            ASSERT(PAGE_ROUND_UP(Segment->VirtualAddress + Segment->Length) >= EndOfEffectiveSegment);
+
             EffectiveSegment->Length = PAGE_ROUND_UP(Segment->VirtualAddress + Segment->Length) -
                                        EffectiveSegment->VirtualAddress;
-   
+
             /*
              * Merge the protection
              */
@@ -2972,6 +3002,7 @@ MmspPageAlignSegments
          }
       }
    }
+   ImageSectionObject->NrSegments = LastSegment + 1;
 
    return TRUE;
 }
@@ -3049,7 +3080,7 @@ ExeFmtpCreateImageSection(HANDLE FileHandle,
     */
    if (Status == STATUS_ROS_EXEFMT_UNKNOWN_FORMAT)
    {
-      Status = STATUS_INVALID_IMAGE_FORMAT;
+      Status = STATUS_INVALID_IMAGE_NOT_MZ;
       ASSERT(!NT_SUCCESS(Status));
    }
 
@@ -3064,10 +3095,10 @@ ExeFmtpCreateImageSection(HANDLE FileHandle,
    /* FIXME? are these values platform-dependent? */
    if(ImageSectionObject->StackReserve == 0)
       ImageSectionObject->StackReserve = 0x40000;
-  
+
    if(ImageSectionObject->StackCommit == 0)
       ImageSectionObject->StackCommit = 0x1000;
-  
+
    if(ImageSectionObject->ImageBase == 0)
    {
       if(ImageSectionObject->ImageCharacteristics & IMAGE_FILE_DLL)
@@ -3101,7 +3132,7 @@ ExeFmtpCreateImageSection(HANDLE FileHandle,
 
       SizeOfSegments = sizeof(MM_SECTION_SEGMENT) * ImageSectionObject->NrSegments;
 
-      Segments = ExAllocatePoolWithTag(NonPagedPool,
+      Segments = ExAllocatePoolWithTag(PagedPool,
                                        SizeOfSegments,
                                        TAG_MM_SECTION_SEGMENT);
 
@@ -3174,6 +3205,7 @@ MmCreateImageSection(PSECTION_OBJECT *SectionObject,
                                       UserMode,
                                       (PVOID*)(PVOID)&FileObject,
                                       NULL);
+
    if (!NT_SUCCESS(Status))
    {
       return Status;
@@ -3202,8 +3234,6 @@ MmCreateImageSection(PSECTION_OBJECT *SectionObject,
     */
    Section->SectionPageProtection = SectionPageProtection;
    Section->AllocationAttributes = AllocationAttributes;
-   InitializeListHead(&Section->ViewListHead);
-   KeInitializeSpinLock(&Section->ViewListLock);
 
    /*
     * Initialized caching for this file object if previously caching
@@ -3215,13 +3245,15 @@ MmCreateImageSection(PSECTION_OBJECT *SectionObject,
    {
       NTSTATUS StatusExeFmt;
 
-      ImageSectionObject = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_IMAGE_SECTION_OBJECT), TAG_MM_SECTION_SEGMENT);
+      ImageSectionObject = ExAllocatePoolWithTag(PagedPool, sizeof(MM_IMAGE_SECTION_OBJECT), TAG_MM_SECTION_SEGMENT);
       if (ImageSectionObject == NULL)
       {
          ObDereferenceObject(FileObject);
          ObDereferenceObject(Section);
          return(STATUS_NO_MEMORY);
       }
+      
+      RtlZeroMemory(ImageSectionObject, sizeof(MM_IMAGE_SECTION_OBJECT));
 
       StatusExeFmt = ExeFmtpCreateImageSection(FileHandle, ImageSectionObject);
 
@@ -3324,16 +3356,13 @@ NtCreateSection (OUT PHANDLE SectionHandle,
    NTSTATUS Status = STATUS_SUCCESS;
 
    PreviousMode = ExGetPreviousMode();
-   
+
    if(MaximumSize != NULL && PreviousMode != KernelMode)
    {
      _SEH_TRY
      {
-       ProbeForRead(MaximumSize,
-                    sizeof(LARGE_INTEGER),
-                    sizeof(ULONG));
        /* make a copy on the stack */
-       SafeMaximumSize = *MaximumSize;
+       SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
        MaximumSize = &SafeMaximumSize;
      }
      _SEH_HANDLE
@@ -3341,13 +3370,13 @@ NtCreateSection (OUT PHANDLE SectionHandle,
        Status = _SEH_GetExceptionCode();
      }
      _SEH_END;
-     
+
      if(!NT_SUCCESS(Status))
      {
        return Status;
      }
    }
-   
+
    /*
     * Check the protection
     */
@@ -3406,23 +3435,21 @@ NtOpenSection(PHANDLE   SectionHandle,
    HANDLE hSection;
    KPROCESSOR_MODE PreviousMode;
    NTSTATUS Status = STATUS_SUCCESS;
-   
+
    PreviousMode = ExGetPreviousMode();
-   
+
    if(PreviousMode != KernelMode)
    {
      _SEH_TRY
      {
-       ProbeForWrite(SectionHandle,
-                     sizeof(HANDLE),
-                     sizeof(ULONG));
+       ProbeForWriteHandle(SectionHandle);
      }
      _SEH_HANDLE
      {
        Status = _SEH_GetExceptionCode();
      }
      _SEH_END;
-     
+
      if(!NT_SUCCESS(Status))
      {
        return Status;
@@ -3466,7 +3493,6 @@ MmMapViewOfSegment(PEPROCESS Process,
 {
    PMEMORY_AREA MArea;
    NTSTATUS Status;
-   KIRQL oldIrql;
    PHYSICAL_ADDRESS BoundaryAddressMultiple;
 
    BoundaryAddressMultiple.QuadPart = 0;
@@ -3488,10 +3514,6 @@ MmMapViewOfSegment(PEPROCESS Process,
       return(Status);
    }
 
-   KeAcquireSpinLock(&Section->ViewListLock, &oldIrql);
-   InsertTailList(&Section->ViewListHead,
-                  &MArea->Data.SectionData.ViewListEntry);
-   KeReleaseSpinLock(&Section->ViewListLock, oldIrql);
 
    ObReferenceObjectByPointer((PVOID)Section,
                               SECTION_MAP_READ,
@@ -3577,34 +3599,28 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
    KPROCESSOR_MODE PreviousMode;
    PMADDRESS_SPACE AddressSpace;
    NTSTATUS Status = STATUS_SUCCESS;
-   
+
    PreviousMode = ExGetPreviousMode();
-   
+
    if(PreviousMode != KernelMode)
    {
      SafeBaseAddress = NULL;
      SafeSectionOffset.QuadPart = 0;
      SafeViewSize = 0;
-     
+
      _SEH_TRY
      {
        if(BaseAddress != NULL)
        {
-         ProbeForWrite(BaseAddress,
-                       sizeof(PVOID),
-                       sizeof(ULONG));
+         ProbeForWritePointer(BaseAddress);
          SafeBaseAddress = *BaseAddress;
        }
        if(SectionOffset != NULL)
        {
-         ProbeForWrite(SectionOffset,
-                       sizeof(LARGE_INTEGER),
-                       sizeof(ULONG));
+         ProbeForWriteLargeInteger(SectionOffset);
          SafeSectionOffset = *SectionOffset;
        }
-       ProbeForWrite(ViewSize,
-                     sizeof(ULONG),
-                     sizeof(ULONG));
+       ProbeForWriteUlong(ViewSize);
        SafeViewSize = *ViewSize;
      }
      _SEH_HANDLE
@@ -3612,7 +3628,7 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
        Status = _SEH_GetExceptionCode();
      }
      _SEH_END;
-     
+
      if(!NT_SUCCESS(Status))
      {
        return Status;
@@ -3664,7 +3680,7 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
 
    ObDereferenceObject(Section);
    ObDereferenceObject(Process);
-   
+
    if(NT_SUCCESS(Status))
    {
      /* copy parameters back to the caller */
@@ -3697,7 +3713,6 @@ VOID STATIC
 MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
                   PFN_TYPE Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
 {
-   PMEMORY_AREA MArea;
    ULONG Entry;
    PFILE_OBJECT FileObject;
    PBCB Bcb;
@@ -3707,23 +3722,24 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
    NTSTATUS Status;
    PSECTION_OBJECT Section;
    PMM_SECTION_SEGMENT Segment;
+   PMADDRESS_SPACE AddressSpace;
 
-   MArea = (PMEMORY_AREA)Context;
+   AddressSpace = (PMADDRESS_SPACE)Context;
 
    Address = (PVOID)PAGE_ROUND_DOWN(Address);
 
-   Offset = ((ULONG_PTR)Address - (ULONG_PTR)MArea->StartingAddress) +
+   Offset = ((ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress) +
             MemoryArea->Data.SectionData.ViewOffset;
 
-   Section = MArea->Data.SectionData.Section;
-   Segment = MArea->Data.SectionData.Segment;
+   Section = MemoryArea->Data.SectionData.Section;
+   Segment = MemoryArea->Data.SectionData.Segment;
 
-   PageOp = MmCheckForPageOp(MArea, NULL, NULL, Segment, Offset);
+   PageOp = MmCheckForPageOp(MemoryArea, NULL, NULL, Segment, Offset);
 
    while (PageOp)
    {
       MmUnlockSectionSegment(Segment);
-      MmUnlockAddressSpace(&MArea->Process->AddressSpace);
+      MmUnlockAddressSpace(AddressSpace);
 
       Status = MmspWaitForPageOpCompletionEvent(PageOp);
       if (Status != STATUS_SUCCESS)
@@ -3732,10 +3748,10 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
          KEBUGCHECK(0);
       }
 
-      MmLockAddressSpace(&MArea->Process->AddressSpace);
+      MmLockAddressSpace(AddressSpace);
       MmLockSectionSegment(Segment);
       MmspCompleteAndReleasePageOp(PageOp);
-      PageOp = MmCheckForPageOp(MArea, NULL, NULL, Segment, Offset);
+      PageOp = MmCheckForPageOp(MemoryArea, NULL, NULL, Segment, Offset);
    }
 
    Entry = MmGetPageEntrySectionSegment(Segment, Offset);
@@ -3750,7 +3766,7 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
       {
          FileObject = MemoryArea->Data.SectionData.Section->FileObject;
          Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
-         CcRosMarkDirtyCacheSegment(Bcb, Offset);
+         CcRosMarkDirtyCacheSegment(Bcb, Offset + Segment->FileOffset);
          ASSERT(SwapEntry == 0);
       }
    }
@@ -3789,18 +3805,18 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
             MmFreeSwapPage(SavedSwapEntry);
             MmSetSavedSwapEntryPage(Page, 0);
          }
-         MmDeleteRmap(Page, MArea->Process, Address);
+         MmDeleteRmap(Page, AddressSpace->Process, Address);
          MmReleasePageMemoryConsumer(MC_USER, Page);
       }
       else
       {
-         MmDeleteRmap(Page, MArea->Process, Address);
+         MmDeleteRmap(Page, AddressSpace->Process, Address);
          MmUnsharePageEntrySectionSegment(Section, Segment, Offset, Dirty, FALSE);
       }
    }
 }
 
-NTSTATUS
+STATIC NTSTATUS
 MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
                      PVOID BaseAddress)
 {
@@ -3808,7 +3824,6 @@ MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
    PMEMORY_AREA MemoryArea;
    PSECTION_OBJECT Section;
    PMM_SECTION_SEGMENT Segment;
-   KIRQL oldIrql;
    PLIST_ENTRY CurrentEntry;
    PMM_REGION CurrentRegion;
    PLIST_ENTRY RegionListHead;
@@ -3825,9 +3840,6 @@ MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
    Segment = MemoryArea->Data.SectionData.Segment;
 
    MmLockSectionSegment(Segment);
-   KeAcquireSpinLock(&Section->ViewListLock, &oldIrql);
-   RemoveEntryList(&MemoryArea->Data.SectionData.ViewListEntry);
-   KeReleaseSpinLock(&Section->ViewListLock, oldIrql);
 
    RegionListHead = &MemoryArea->Data.SectionData.RegionListHead;
    while (!IsListEmpty(RegionListHead))
@@ -3849,7 +3861,7 @@ MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
       Status = MmFreeMemoryArea(AddressSpace,
                                 MemoryArea,
                                 MmFreeSectionPage,
-                                MemoryArea);
+                                AddressSpace);
    }
    MmUnlockSectionSegment(Segment);
    ObDereferenceObject(Section);
@@ -3867,6 +3879,8 @@ MmUnmapViewOfSection(PEPROCESS Process,
    PMEMORY_AREA MemoryArea;
    PMADDRESS_SPACE AddressSpace;
    PSECTION_OBJECT Section;
+   PMM_PAGEOP PageOp;
+   ULONG_PTR Offset;
 
    DPRINT("Opening memory area Process %x BaseAddress %x\n",
           Process, BaseAddress);
@@ -3874,15 +3888,53 @@ MmUnmapViewOfSection(PEPROCESS Process,
    ASSERT(Process);
 
    AddressSpace = &Process->AddressSpace;
+   
+   MmLockAddressSpace(AddressSpace);
    MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace,
                                             BaseAddress);
    if (MemoryArea == NULL ||
        MemoryArea->Type != MEMORY_AREA_SECTION_VIEW ||
        MemoryArea->DeleteInProgress)
    {
+      MmUnlockAddressSpace(AddressSpace);
       return STATUS_NOT_MAPPED_VIEW;
    }
 
+   MemoryArea->DeleteInProgress = TRUE;
+
+   while (MemoryArea->PageOpCount)
+   {
+      Offset = PAGE_ROUND_UP((ULONG_PTR)MemoryArea->EndingAddress - (ULONG_PTR)MemoryArea->StartingAddress);
+
+      while (Offset)
+      {
+         Offset -= PAGE_SIZE;
+         PageOp = MmCheckForPageOp(MemoryArea, NULL, NULL,
+                                   MemoryArea->Data.SectionData.Segment,
+                                   Offset + MemoryArea->Data.SectionData.ViewOffset);
+         if (PageOp)
+         {
+            MmUnlockAddressSpace(AddressSpace);
+            Status = MmspWaitForPageOpCompletionEvent(PageOp);
+            if (Status != STATUS_SUCCESS)
+            {
+               DPRINT1("Failed to wait for page op, status = %x\n", Status);
+               KEBUGCHECK(0);
+            }
+            MmLockAddressSpace(AddressSpace);
+            MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace,
+                                                     BaseAddress);
+            if (MemoryArea == NULL ||
+                MemoryArea->Type != MEMORY_AREA_SECTION_VIEW)
+            {
+               MmUnlockAddressSpace(AddressSpace);
+               return STATUS_NOT_MAPPED_VIEW;
+            }
+            break;
+         }
+      }
+   }
+
    Section = MemoryArea->Data.SectionData.Section;
 
    if (Section->AllocationAttributes & SEC_IMAGE)
@@ -3932,6 +3984,7 @@ MmUnmapViewOfSection(PEPROCESS Process,
    {
       Status = MmUnmapViewOfSegment(AddressSpace, BaseAddress);
    }
+   MmUnlockAddressSpace(AddressSpace);
    return(STATUS_SUCCESS);
 }
 
@@ -3977,9 +4030,7 @@ NtUnmapViewOfSection (HANDLE ProcessHandle,
       return(Status);
    }
 
-   MmLockAddressSpace(&Process->AddressSpace);
    Status = MmUnmapViewOfSection(Process, BaseAddress);
-   MmUnlockAddressSpace(&Process->AddressSpace);
 
    ObDereferenceObject(Process);
 
@@ -3989,7 +4040,7 @@ NtUnmapViewOfSection (HANDLE ProcessHandle,
 
 /**
  * Queries the information of a section object.
- * 
+ *
  * @param SectionHandle
  *        Handle to the section object. It must be opened with SECTION_QUERY
  *        access.
@@ -4019,16 +4070,16 @@ NtQuerySection(IN HANDLE SectionHandle,
    PSECTION_OBJECT Section;
    KPROCESSOR_MODE PreviousMode;
    NTSTATUS Status = STATUS_SUCCESS;
-   
+
    PreviousMode = ExGetPreviousMode();
-   
-   DefaultQueryInfoBufferCheck(SectionInformationClass,
-                               ExSectionInfoClass,
-                               SectionInformation,
-                               SectionInformationLength,
-                               ResultLength,
-                               PreviousMode,
-                               &Status);
+
+   Status = DefaultQueryInfoBufferCheck(SectionInformationClass,
+                                        ExSectionInfoClass,
+                                        sizeof(ExSectionInfoClass) / sizeof(ExSectionInfoClass[0]),
+                                        SectionInformation,
+                                        SectionInformationLength,
+                                        ResultLength,
+                                        PreviousMode);
 
    if(!NT_SUCCESS(Status))
    {
@@ -4075,7 +4126,7 @@ NtQuerySection(IN HANDLE SectionHandle,
                Status = _SEH_GetExceptionCode();
             }
             _SEH_END;
-            
+
             break;
          }
 
@@ -4091,17 +4142,17 @@ NtQuerySection(IN HANDLE SectionHandle,
                   PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
                   ImageSectionObject = Section->ImageSection;
 
-                  Sii->EntryPoint = ImageSectionObject->EntryPoint;
-                  Sii->StackReserve = ImageSectionObject->StackReserve;
-                  Sii->StackCommit = ImageSectionObject->StackCommit;
-                  Sii->Subsystem = ImageSectionObject->Subsystem;
-                  Sii->MinorSubsystemVersion = ImageSectionObject->MinorSubsystemVersion;
-                  Sii->MajorSubsystemVersion = ImageSectionObject->MajorSubsystemVersion;
-                  Sii->Characteristics = ImageSectionObject->ImageCharacteristics;
-                  Sii->ImageNumber = ImageSectionObject->Machine;
-                  Sii->Executable = ImageSectionObject->Executable;
+                  Sii->TransferAddress = (PVOID)ImageSectionObject->EntryPoint;
+                  Sii->MaximumStackSize = ImageSectionObject->StackReserve;
+                  Sii->CommittedStackSize = ImageSectionObject->StackCommit;
+                  Sii->SubsystemType = ImageSectionObject->Subsystem;
+                  Sii->SubSystemMinorVersion = ImageSectionObject->MinorSubsystemVersion;
+                  Sii->SubSystemMajorVersion = ImageSectionObject->MajorSubsystemVersion;
+                  Sii->ImageCharacteristics = ImageSectionObject->ImageCharacteristics;
+                  Sii->Machine = ImageSectionObject->Machine;
+                  Sii->ImageContainsCode = ImageSectionObject->Executable;
                }
-               
+
                if (ResultLength != NULL)
                {
                   *ResultLength = sizeof(SECTION_IMAGE_INFORMATION);
@@ -4113,21 +4164,21 @@ NtQuerySection(IN HANDLE SectionHandle,
                Status = _SEH_GetExceptionCode();
             }
             _SEH_END;
-            
+
             break;
          }
       }
 
       ObDereferenceObject(Section);
    }
-   
+
    return(Status);
 }
 
 
 /**
  * Extends size of file backed section.
- * 
+ *
  * @param SectionHandle
  *        Handle to the section object. It must be opened with
  *        SECTION_EXTEND_SIZE access.
@@ -4147,18 +4198,15 @@ NtExtendSection(IN HANDLE SectionHandle,
    PSECTION_OBJECT Section;
    KPROCESSOR_MODE PreviousMode;
    NTSTATUS Status = STATUS_SUCCESS;
-   
+
    PreviousMode = ExGetPreviousMode();
-   
+
    if(PreviousMode != KernelMode)
    {
      _SEH_TRY
      {
-       ProbeForRead(NewMaximumSize,
-                    sizeof(LARGE_INTEGER),
-                    sizeof(ULONG));
        /* make a copy on the stack */
-       SafeNewMaximumSize = *NewMaximumSize;
+       SafeNewMaximumSize = ProbeForReadLargeInteger(NewMaximumSize);
        NewMaximumSize = &SafeNewMaximumSize;
      }
      _SEH_HANDLE
@@ -4166,7 +4214,7 @@ NtExtendSection(IN HANDLE SectionHandle,
        Status = _SEH_GetExceptionCode();
      }
      _SEH_END;
-     
+
      if(!NT_SUCCESS(Status))
      {
        return Status;
@@ -4189,7 +4237,7 @@ NtExtendSection(IN HANDLE SectionHandle,
       ObfDereferenceObject(Section);
       return STATUS_INVALID_PARAMETER;
    }
-   
+
    /*
     * - Acquire file extneding resource.
     * - Check if we're not resizing the section below it's actual size!
@@ -4472,7 +4520,7 @@ MmMapViewOfSection(IN PVOID SectionObject,
                                   *ViewSize,
                                   Protect,
                                   ViewOffset,
-                                  (AllocationType & MEM_TOP_DOWN));
+                                  (AllocationType & MEM_TOP_DOWN) == MEM_TOP_DOWN);
       MmUnlockSectionSegment(Section->Segment);
       if (!NT_SUCCESS(Status))
       {