Fixed the check for the MEM_TOP_DOWN flag in MmMapViewOfSection.
[reactos.git] / reactos / ntoskrnl / mm / section.c
index 84e6c6f..584c9da 100644 (file)
@@ -157,6 +157,7 @@ MmFreePageTablesSectionSegment(PMM_SECTION_SEGMENT Segment)
 }
 
 VOID
+NTAPI
 MmFreeSectionSegments(PFILE_OBJECT FileObject)
 {
    if (FileObject->SectionObjectPointer->ImageSectionObject != NULL)
@@ -202,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)
@@ -250,6 +254,7 @@ MmSetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
 
 
 ULONG
+NTAPI
 MmGetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
                              ULONG Offset)
 {
@@ -280,6 +285,7 @@ MmGetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
 }
 
 VOID
+NTAPI
 MmSharePageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
                                ULONG Offset)
 {
@@ -305,6 +311,7 @@ MmSharePageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
 }
 
 BOOLEAN
+NTAPI
 MmUnsharePageEntrySectionSegment(PSECTION_OBJECT Section,
                                  PMM_SECTION_SEGMENT Segment,
                                  ULONG Offset,
@@ -453,6 +460,7 @@ BOOL MiIsPageFromCache(PMEMORY_AREA MemoryArea,
 }
 
 NTSTATUS
+NTAPI
 MiReadPage(PMEMORY_AREA MemoryArea,
            ULONG SegOffset,
            PPFN_TYPE Page)
@@ -624,6 +632,7 @@ MiReadPage(PMEMORY_AREA MemoryArea,
 }
 
 NTSTATUS
+NTAPI
 MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
                              MEMORY_AREA* MemoryArea,
                              PVOID Address,
@@ -657,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;
@@ -864,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,
@@ -940,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)
@@ -1142,6 +1151,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
 }
 
 NTSTATUS
+NTAPI
 MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
                          MEMORY_AREA* MemoryArea,
                          PVOID Address,
@@ -1172,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;
@@ -1267,6 +1278,7 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
     */
    MiCopyFromUserPage(NewPage, PAddress);
 
+   MmLockAddressSpace(AddressSpace);
    /*
     * Delete the old entry.
     */
@@ -1275,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,
@@ -1287,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");
@@ -1303,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);
@@ -1321,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,
@@ -1332,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,
@@ -1371,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;
@@ -1425,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)
    {
@@ -1548,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);
@@ -1570,7 +1597,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       if (SwapEntry == 0)
       {
          MmShowOutOfSpaceMessagePagingFile();
-
+         MmLockAddressSpace(AddressSpace);
          /*
           * For private pages restore the old mappings.
           */
@@ -1605,6 +1632,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
             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);
@@ -1623,6 +1651,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
        * As above: undo our actions.
        * FIXME: Also free the swap page.
        */
+      MmLockAddressSpace(AddressSpace);
       if (Context.Private)
       {
          Status = MmCreateVirtualMapping(AddressSpace->Process,
@@ -1649,6 +1678,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
          Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
          MmSetPageEntrySectionSegment(Context.Segment, Context.Offset, Entry);
       }
+      MmUnlockAddressSpace(AddressSpace);
       PageOp->Status = STATUS_UNSUCCESSFUL;
       MmspCompleteAndReleasePageOp(PageOp);
       return(STATUS_UNSUCCESSFUL);
@@ -1671,9 +1701,11 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
 
    if (Context.Private)
    {
+      MmLockAddressSpace(AddressSpace);
       Status = MmCreatePageFileMapping(AddressSpace->Process,
                                        Address,
                                        SwapEntry);
+      MmUnlockAddressSpace(AddressSpace);
       if (!NT_SUCCESS(Status))
       {
          KEBUGCHECK(0);
@@ -1691,6 +1723,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
 }
 
 NTSTATUS
+NTAPI
 MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
                        PMEMORY_AREA MemoryArea,
                        PVOID Address,
@@ -1711,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.
@@ -1732,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;
@@ -1767,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)
    {
@@ -1790,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);
@@ -1875,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)
             {
@@ -1898,6 +1933,7 @@ MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
 }
 
 NTSTATUS
+NTAPI
 MmProtectSectionView(PMADDRESS_SPACE AddressSpace,
                      PMEMORY_AREA MemoryArea,
                      PVOID BaseAddress,
@@ -1948,7 +1984,7 @@ MmQuerySectionView(PMEMORY_AREA MemoryArea,
    if (Section->AllocationAttributes & SEC_IMAGE)
    {
       Segment = MemoryArea->Data.SectionData.Segment;
-      Info->AllocationBase = MemoryArea->StartingAddress - Segment->VirtualAddress;
+      Info->AllocationBase = (PBYTE)MemoryArea->StartingAddress - Segment->VirtualAddress;
       Info->Type = MEM_IMAGE;
    }
    else
@@ -1958,8 +1994,7 @@ MmQuerySectionView(PMEMORY_AREA MemoryArea,
    }
    Info->BaseAddress = RegionBaseAddress;
    Info->AllocationProtect = MemoryArea->Attributes;
-   Info->RegionSize = PAGE_ROUND_UP((ULONG_PTR)MemoryArea->EndingAddress -
-                                    (ULONG_PTR)MemoryArea->StartingAddress);
+   Info->RegionSize = Region->Length;
    Info->State = MEM_COMMIT;
    Info->Protect = Region->Protect;
 
@@ -1968,6 +2003,7 @@ MmQuerySectionView(PMEMORY_AREA MemoryArea,
 }
 
 VOID
+NTAPI
 MmpFreePageFileSegment(PMM_SECTION_SEGMENT Segment)
 {
    ULONG Length;
@@ -2083,13 +2119,15 @@ MmpCloseSection(PVOID ObjectBody,
           ObjectBody, HandleCount, ObGetObjectPointerCount(ObjectBody));
 }
 
-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;
 
    /*
@@ -2125,11 +2163,14 @@ MmCreatePhysicalMemorySection(VOID)
       ObDereferenceObject(PhysSection);
    }
    PhysSection->AllocationAttributes |= SEC_PHYSICALMEMORY;
+   PhysSection->Segment->Flags &= ~MM_PAGEFILE_SEGMENT;
 
    return(STATUS_SUCCESS);
 }
 
-NTSTATUS INIT_FUNCTION
+NTSTATUS
+INIT_FUNCTION
+NTAPI
 MmInitSectionImplementation(VOID)
 {
    OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
@@ -2153,6 +2194,7 @@ MmInitSectionImplementation(VOID)
 }
 
 NTSTATUS
+NTAPI
 MmCreatePageFileSection(PSECTION_OBJECT *SectionObject,
                         ACCESS_MASK DesiredAccess,
                         POBJECT_ATTRIBUTES ObjectAttributes,
@@ -2224,6 +2266,7 @@ MmCreatePageFileSection(PSECTION_OBJECT *SectionObject,
 
 
 NTSTATUS
+NTAPI
 MmCreateDataFileSection(PSECTION_OBJECT *SectionObject,
                         ACCESS_MASK DesiredAccess,
                         POBJECT_ATTRIBUTES ObjectAttributes,
@@ -2303,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);
@@ -2337,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);
@@ -2556,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;
@@ -2790,6 +2831,7 @@ MmspPageAlignSegments
    ULONG i;
    ULONG LastSegment;
    BOOLEAN Initialized;
+   PMM_SECTION_SEGMENT EffectiveSegment;
 
    if (Flags & EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED)
    {
@@ -2799,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
        */
@@ -2857,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
@@ -2914,7 +2958,7 @@ MmspPageAlignSegments
             /*
              * 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;
@@ -2958,6 +3002,7 @@ MmspPageAlignSegments
          }
       }
    }
+   ImageSectionObject->NrSegments = LastSegment + 1;
 
    return TRUE;
 }
@@ -3035,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));
    }
 
@@ -3207,6 +3252,8 @@ MmCreateImageSection(PSECTION_OBJECT *SectionObject,
          ObDereferenceObject(Section);
          return(STATUS_NO_MEMORY);
       }
+      
+      RtlZeroMemory(ImageSectionObject, sizeof(MM_IMAGE_SECTION_OBJECT));
 
       StatusExeFmt = ExeFmtpCreateImageSection(FileHandle, ImageSectionObject);
 
@@ -3314,11 +3361,8 @@ NtCreateSection (OUT PHANDLE SectionHandle,
    {
      _SEH_TRY
      {
-       ProbeForRead(MaximumSize,
-                    sizeof(LARGE_INTEGER),
-                    sizeof(ULONG));
        /* make a copy on the stack */
-       SafeMaximumSize = *MaximumSize;
+       SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
        MaximumSize = &SafeMaximumSize;
      }
      _SEH_HANDLE
@@ -3398,9 +3442,7 @@ NtOpenSection(PHANDLE   SectionHandle,
    {
      _SEH_TRY
      {
-       ProbeForWrite(SectionHandle,
-                     sizeof(HANDLE),
-                     sizeof(ULONG));
+       ProbeForWriteHandle(SectionHandle);
      }
      _SEH_HANDLE
      {
@@ -3570,21 +3612,15 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
      {
        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
@@ -3730,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);
       }
    }
@@ -3780,7 +3816,7 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
    }
 }
 
-NTSTATUS
+STATIC NTSTATUS
 MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
                      PVOID BaseAddress)
 {
@@ -3843,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);
@@ -3850,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)
@@ -3908,6 +3984,7 @@ MmUnmapViewOfSection(PEPROCESS Process,
    {
       Status = MmUnmapViewOfSegment(AddressSpace, BaseAddress);
    }
+   MmUnlockAddressSpace(AddressSpace);
    return(STATUS_SUCCESS);
 }
 
@@ -3953,9 +4030,7 @@ NtUnmapViewOfSection (HANDLE ProcessHandle,
       return(Status);
    }
 
-   MmLockAddressSpace(&Process->AddressSpace);
    Status = MmUnmapViewOfSection(Process, BaseAddress);
-   MmUnlockAddressSpace(&Process->AddressSpace);
 
    ObDereferenceObject(Process);
 
@@ -3998,13 +4073,13 @@ NtQuerySection(IN HANDLE SectionHandle,
 
    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))
    {
@@ -4067,15 +4142,15 @@ 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)
@@ -4130,11 +4205,8 @@ NtExtendSection(IN HANDLE SectionHandle,
    {
      _SEH_TRY
      {
-       ProbeForRead(NewMaximumSize,
-                    sizeof(LARGE_INTEGER),
-                    sizeof(ULONG));
        /* make a copy on the stack */
-       SafeNewMaximumSize = *NewMaximumSize;
+       SafeNewMaximumSize = ProbeForReadLargeInteger(NewMaximumSize);
        NewMaximumSize = &SafeNewMaximumSize;
      }
      _SEH_HANDLE
@@ -4448,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))
       {