[NTOSKRNL]
[reactos.git] / ntoskrnl / mm / section.c
index 2dd55ee..af87192 100644 (file)
@@ -60,7 +60,7 @@
 
 #undef MmSetPageEntrySectionSegment
 #define MmSetPageEntrySectionSegment(S,O,E) do { \
-        DPRINT("SetPageEntrySectionSegment(old,%x,%x,%x)\n",(S),(O)->LowPart,E); \
+        DPRINT("SetPageEntrySectionSegment(old,%p,%x,%x)\n",(S),(O)->LowPart,E); \
         _MmSetPageEntrySectionSegment((S),(O),(E),__FILE__,__LINE__);   \
        } while (0)
 
@@ -161,6 +161,7 @@ static ULONG SectionCharacteristicsToProtect[16] =
     PAGE_EXECUTE_READWRITE, /* 15 = WRITABLE, READABLE, EXECUTABLE, SHARED */
 };
 
+extern ULONG MmMakeFileAccess [];
 ACCESS_MASK NTAPI MiArm3GetCorrectFileAccessMask(IN ACCESS_MASK SectionPageProtection);
 static GENERIC_MAPPING MmpSectionMapping = {
          STANDARD_RIGHTS_READ | SECTION_MAP_READ | SECTION_QUERY,
@@ -199,6 +200,7 @@ NTSTATUS NTAPI PeFmtCreateSection(IN CONST VOID * FileHeader,
     ULONG cbHeadersSize = 0;
     ULONG nSectionAlignment;
     ULONG nFileAlignment;
+    ULONG_PTR ImageBase;
     const IMAGE_DOS_HEADER * pidhDosHeader;
     const IMAGE_NT_HEADERS32 * pinhNtHeader;
     const IMAGE_OPTIONAL_HEADER32 * piohOptHeader;
@@ -278,7 +280,14 @@ l_ReadHeaderFromFile:
         nStatus = ReadFileCb(File, &lnOffset, sizeof(IMAGE_NT_HEADERS64), &pData, &pBuffer, &cbReadSize);
 
         if(!NT_SUCCESS(nStatus))
-            DIE(("ReadFile failed, status %08X\n", nStatus));
+        {
+            NTSTATUS ReturnedStatus = nStatus;
+
+            /* If it attempted to read past the end of the file, it means e_lfanew is invalid */
+            if (ReturnedStatus == STATUS_END_OF_FILE) nStatus = STATUS_ROS_EXEFMT_UNKNOWN_FORMAT;
+
+            DIE(("ReadFile failed, status %08X\n", ReturnedStatus));
+        }
 
         ASSERT(pData);
         ASSERT(pBuffer);
@@ -389,21 +398,73 @@ l_ReadHeaderFromFile:
         /* PE32 */
         case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
         {
-            if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, ImageBase))
-                ImageSectionObject->ImageBase = piohOptHeader->ImageBase;
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, ImageBase))
+                ImageBase = piohOptHeader->ImageBase;
 
             if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfImage))
-                ImageSectionObject->ImageSize = piohOptHeader->SizeOfImage;
+                ImageSectionObject->ImageInformation.ImageFileSize = piohOptHeader->SizeOfImage;
 
             if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackReserve))
-                ImageSectionObject->StackReserve = piohOptHeader->SizeOfStackReserve;
+                ImageSectionObject->ImageInformation.MaximumStackSize = piohOptHeader->SizeOfStackReserve;
 
             if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackCommit))
-                ImageSectionObject->StackCommit = piohOptHeader->SizeOfStackCommit;
+                ImageSectionObject->ImageInformation.CommittedStackSize = piohOptHeader->SizeOfStackCommit;
+
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Subsystem))
+            {
+                ImageSectionObject->ImageInformation.SubSystemType = piohOptHeader->Subsystem;
+
+                if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
+                    RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MajorSubsystemVersion))
+                {
+                    ImageSectionObject->ImageInformation.SubSystemMinorVersion = piohOptHeader->MinorSubsystemVersion;
+                    ImageSectionObject->ImageInformation.SubSystemMajorVersion = piohOptHeader->MajorSubsystemVersion;
+                }
+            }
+
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+            {
+                ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
+                    piohOptHeader->AddressOfEntryPoint);
+            }
+
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfCode))
+                ImageSectionObject->ImageInformation.ImageContainsCode = piohOptHeader->SizeOfCode != 0;
+            else
+                ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
+
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+            {
+                if (piohOptHeader->AddressOfEntryPoint == 0)
+                {
+                    ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
+                }
+            }
+
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, LoaderFlags))
+                ImageSectionObject->ImageInformation.LoaderFlags = piohOptHeader->LoaderFlags;
+
+            if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, DllCharacteristics))
+            {
+                ImageSectionObject->ImageInformation.DllCharacteristics = piohOptHeader->DllCharacteristics;
+
+                /*
+                 * Since we don't really implement SxS yet and LD doesn't supoprt /ALLOWISOLATION:NO, hard-code
+                 * this flag here, which will prevent the loader and other code from doing any .manifest or SxS
+                 * magic to any binary.
+                 *
+                 * This will break applications that depend on SxS when running with real Windows Kernel32/SxS/etc
+                 * but honestly that's not tested. It will also break them when running no ReactOS once we implement
+                 * the SxS support -- at which point, duh, this should be removed.
+                 *
+                 * But right now, any app depending on SxS is already broken anyway, so this flag only helps.
+                 */
+                ImageSectionObject->ImageInformation.DllCharacteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
+            }
 
             break;
         }
-
+#ifdef _WIN64
         /* PE64 */
         case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
         {
@@ -413,10 +474,9 @@ l_ReadHeaderFromFile:
 
             if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, ImageBase))
             {
+                ImageBase = pioh64OptHeader->ImageBase;
                 if(pioh64OptHeader->ImageBase > MAXULONG_PTR)
                     DIE(("ImageBase exceeds the address space\n"));
-
-                ImageSectionObject->ImageBase = (ULONG_PTR)pioh64OptHeader->ImageBase;
             }
 
             if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfImage))
@@ -424,7 +484,7 @@ l_ReadHeaderFromFile:
                 if(pioh64OptHeader->SizeOfImage > MAXULONG_PTR)
                     DIE(("SizeOfImage exceeds the address space\n"));
 
-                ImageSectionObject->ImageSize = pioh64OptHeader->SizeOfImage;
+                ImageSectionObject->ImageInformation.ImageFileSize = pioh64OptHeader->SizeOfImage;
             }
 
             if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackReserve))
@@ -432,7 +492,7 @@ l_ReadHeaderFromFile:
                 if(pioh64OptHeader->SizeOfStackReserve > MAXULONG_PTR)
                     DIE(("SizeOfStackReserve exceeds the address space\n"));
 
-                ImageSectionObject->StackReserve = (ULONG_PTR)pioh64OptHeader->SizeOfStackReserve;
+                ImageSectionObject->ImageInformation.MaximumStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackReserve;
             }
 
             if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackCommit))
@@ -440,42 +500,60 @@ l_ReadHeaderFromFile:
                 if(pioh64OptHeader->SizeOfStackCommit > MAXULONG_PTR)
                     DIE(("SizeOfStackCommit exceeds the address space\n"));
 
-                ImageSectionObject->StackCommit = (ULONG_PTR)pioh64OptHeader->SizeOfStackCommit;
+                ImageSectionObject->ImageInformation.CommittedStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackCommit;
             }
 
-            break;
-        }
-    }
+            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, Subsystem))
+            {
+                ImageSectionObject->ImageInformation.SubSystemType = pioh64OptHeader->Subsystem;
+
+                if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
+                    RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, MajorSubsystemVersion))
+                {
+                    ImageSectionObject->ImageInformation.SubSystemMinorVersion = pioh64OptHeader->MinorSubsystemVersion;
+                    ImageSectionObject->ImageInformation.SubSystemMajorVersion = pioh64OptHeader->MajorSubsystemVersion;
+                }
+            }
 
-    /* [1], section 3.4.2 */
-    if((ULONG_PTR)ImageSectionObject->ImageBase % 0x10000)
-        DIE(("ImageBase is not aligned on a 64KB boundary"));
+            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+            {
+                ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
+                    pioh64OptHeader->AddressOfEntryPoint);
+            }
 
-    if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Subsystem))
-    {
-        ImageSectionObject->Subsystem = piohOptHeader->Subsystem;
+            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfCode))
+                ImageSectionObject->ImageInformation.ImageContainsCode = pioh64OptHeader->SizeOfCode != 0;
+            else
+                ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
 
-        if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
-           RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MajorSubsystemVersion))
-        {
-            ImageSectionObject->MinorSubsystemVersion = piohOptHeader->MinorSubsystemVersion;
-            ImageSectionObject->MajorSubsystemVersion = piohOptHeader->MajorSubsystemVersion;
-        }
-    }
+            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
+            {
+                if (pioh64OptHeader->AddressOfEntryPoint == 0)
+                {
+                    ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
+                }
+            }
 
-    if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
-    {
-        ImageSectionObject->EntryPoint = ImageSectionObject->ImageBase +
-                                         piohOptHeader->AddressOfEntryPoint;
+            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, LoaderFlags))
+                ImageSectionObject->ImageInformation.LoaderFlags = pioh64OptHeader->LoaderFlags;
+
+            if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, DllCharacteristics))
+                ImageSectionObject->ImageInformation.DllCharacteristics = pioh64OptHeader->DllCharacteristics;
+
+            break;
+        }
+#endif // _WIN64
     }
 
-    if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfCode))
-        ImageSectionObject->Executable = piohOptHeader->SizeOfCode != 0;
-    else
-        ImageSectionObject->Executable = TRUE;
+    /* [1], section 3.4.2 */
+    if((ULONG_PTR)ImageBase % 0x10000)
+        DIE(("ImageBase is not aligned on a 64KB boundary"));
 
-    ImageSectionObject->ImageCharacteristics = pinhNtHeader->FileHeader.Characteristics;
-    ImageSectionObject->Machine = pinhNtHeader->FileHeader.Machine;
+    ImageSectionObject->ImageInformation.ImageCharacteristics = pinhNtHeader->FileHeader.Characteristics;
+    ImageSectionObject->ImageInformation.Machine = pinhNtHeader->FileHeader.Machine;
+    ImageSectionObject->ImageInformation.GpValue = 0;
+    ImageSectionObject->ImageInformation.ZeroBits = 0;
+    ImageSectionObject->BasedAddress = (PVOID)ImageBase;
 
     /* SECTION HEADERS */
     nStatus = STATUS_INVALID_IMAGE_FORMAT;
@@ -677,6 +755,7 @@ l_ReadHeaderFromFile:
             pssSegments[i].Length.QuadPart = pishSectionHeaders[i].Misc.VirtualSize;
 
         pssSegments[i].Length.LowPart = ALIGN_UP_BY(pssSegments[i].Length.LowPart, nSectionAlignment);
+        /* FIXME: always false */
         if (pssSegments[i].Length.QuadPart < pssSegments[i].Length.QuadPart)
             DIE(("Cannot align the virtual size of section %u\n", i));
 
@@ -696,7 +775,7 @@ l_ReadHeaderFromFile:
         *Flags |= EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED;
 
     /* Success */
-    nStatus = STATUS_ROS_EXEFMT_LOADED_FORMAT | EXEFMT_LOADED_PE32;
+    nStatus = STATUS_SUCCESS;// STATUS_ROS_EXEFMT_LOADED_FORMAT | EXEFMT_LOADED_PE32;
 
 l_Return:
     if(pBuffer)
@@ -735,7 +814,7 @@ MmFreeSectionSegments(PFILE_OBJECT FileObject)
       {
          if (SectionSegments[i].ReferenceCount != 0)
          {
-            DPRINT1("Image segment %d still referenced (was %d)\n", i,
+            DPRINT1("Image segment %lu still referenced (was %lu)\n", i,
                     SectionSegments[i].ReferenceCount);
             KeBugCheck(MEMORY_MANAGEMENT);
          }
@@ -824,7 +903,7 @@ MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
    {
       PFILE_OBJECT FileObject;
 #ifndef NEWCC
-      PBCB Bcb;
+      PROS_SHARED_CACHE_MAP SharedCacheMap;
 #endif
       SWAPENTRY SavedSwapEntry;
       PFN_NUMBER Page;
@@ -846,16 +925,16 @@ MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
                (Offset->QuadPart + PAGE_SIZE <= Segment->RawLength.QuadPart || !IsImageSection))
          {
             NTSTATUS Status;
-            Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
+            SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
             IsDirectMapped = TRUE;
 #ifndef NEWCC
-            Status = CcRosUnmapCacheSegment(Bcb, FileOffset.LowPart, Dirty);
+            Status = CcRosUnmapVacb(SharedCacheMap, FileOffset.LowPart, Dirty);
 #else
             Status = STATUS_SUCCESS;
 #endif
             if (!NT_SUCCESS(Status))
             {
-               DPRINT1("CcRosUnmapCacheSegment failed, status = %x\n", Status);
+               DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
                KeBugCheck(MEMORY_MANAGEMENT);
             }
          }
@@ -943,13 +1022,13 @@ BOOLEAN MiIsPageFromCache(PMEMORY_AREA MemoryArea,
 #ifndef NEWCC
    if (!(MemoryArea->Data.SectionData.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
    {
-      PBCB Bcb;
-      PCACHE_SEGMENT CacheSeg;
-      Bcb = MemoryArea->Data.SectionData.Section->FileObject->SectionObjectPointer->SharedCacheMap;
-      CacheSeg = CcRosLookupCacheSegment(Bcb, (ULONG)(SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset));
-      if (CacheSeg)
+      PROS_SHARED_CACHE_MAP SharedCacheMap;
+      PROS_VACB Vacb;
+      SharedCacheMap = MemoryArea->Data.SectionData.Section->FileObject->SectionObjectPointer->SharedCacheMap;
+      Vacb = CcRosLookupVacb(SharedCacheMap, (ULONG)(SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset));
+      if (Vacb)
       {
-         CcRosReleaseCacheSegment(Bcb, CacheSeg, CacheSeg->Valid, FALSE, TRUE);
+         CcRosReleaseVacb(SharedCacheMap, Vacb, Vacb->Valid, FALSE, TRUE);
          return TRUE;
       }
    }
@@ -994,32 +1073,32 @@ MiReadPage(PMEMORY_AREA MemoryArea,
  *       Page - Variable that receives a page contains the read data.
  */
 {
-   ULONG BaseOffset;
+   ULONGLONG BaseOffset;
    ULONGLONG FileOffset;
    PVOID BaseAddress;
    BOOLEAN UptoDate;
-   PCACHE_SEGMENT CacheSeg;
+   PROS_VACB Vacb;
    PFILE_OBJECT FileObject;
    NTSTATUS Status;
    ULONG_PTR RawLength;
-   PBCB Bcb;
+   PROS_SHARED_CACHE_MAP SharedCacheMap;
    BOOLEAN IsImageSection;
    ULONG_PTR Length;
 
    FileObject = MemoryArea->Data.SectionData.Section->FileObject;
-   Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
+   SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
    RawLength = (ULONG_PTR)(MemoryArea->Data.SectionData.Segment->RawLength.QuadPart);
    FileOffset = SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset;
    IsImageSection = MemoryArea->Data.SectionData.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
 
-   ASSERT(Bcb);
+   ASSERT(SharedCacheMap);
 
-   DPRINT("%S %x\n", FileObject->FileName.Buffer, FileOffset);
+   DPRINT("%S %I64x\n", FileObject->FileName.Buffer, FileOffset);
 
    /*
     * If the file system is letting us go directly to the cache and the
     * memory area was mapped at an offset in the file which is page aligned
-    * then get the related cache segment.
+    * then get the related VACB.
     */
    if (((FileOffset % PAGE_SIZE) == 0) &&
        ((SegOffset + PAGE_SIZE <= RawLength) || !IsImageSection) &&
@@ -1027,16 +1106,16 @@ MiReadPage(PMEMORY_AREA MemoryArea,
    {
 
       /*
-       * Get the related cache segment; we use a lower level interface than
-       * filesystems do because it is safe for us to use an offset with a
+       * Get the related VACB; we use a lower level interface than
+       * filesystems do because it is safe for us to use an offset with an
        * alignment less than the file system block size.
        */
-      Status = CcRosGetCacheSegment(Bcb,
-                                    (ULONG)FileOffset,
-                                    &BaseOffset,
-                                    &BaseAddress,
-                                    &UptoDate,
-                                    &CacheSeg);
+      Status = CcRosGetVacb(SharedCacheMap,
+                            (ULONG)FileOffset,
+                            &BaseOffset,
+                            &BaseAddress,
+                            &UptoDate,
+                            &Vacb);
       if (!NT_SUCCESS(Status))
       {
          return(Status);
@@ -1044,30 +1123,34 @@ MiReadPage(PMEMORY_AREA MemoryArea,
       if (!UptoDate)
       {
          /*
-          * If the cache segment isn't up to date then call the file
+          * If the VACB isn't up to date then call the file
           * system to read in the data.
           */
-         Status = ReadCacheSegment(CacheSeg);
+         Status = CcReadVirtualAddress(Vacb);
          if (!NT_SUCCESS(Status))
          {
-            CcRosReleaseCacheSegment(Bcb, CacheSeg, FALSE, FALSE, FALSE);
+            CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
             return Status;
          }
       }
+
+      /* Probe the page, since it's PDE might not be synced */
+      (void)*((volatile char*)BaseAddress + FileOffset - BaseOffset);
+
       /*
-       * Retrieve the page from the cache segment that we actually want.
+       * Retrieve the page from the view that we actually want.
        */
       (*Page) = MmGetPhysicalAddress((char*)BaseAddress +
                                      FileOffset - BaseOffset).LowPart >> PAGE_SHIFT;
 
-      CcRosReleaseCacheSegment(Bcb, CacheSeg, TRUE, FALSE, TRUE);
+      CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, TRUE);
    }
    else
    {
       PEPROCESS Process;
       KIRQL Irql;
       PVOID PageAddr;
-      ULONG_PTR CacheSegOffset;
+      ULONG_PTR VacbOffset;
 
       /*
        * Allocate a page, this is rather complicated by the possibility
@@ -1080,12 +1163,12 @@ MiReadPage(PMEMORY_AREA MemoryArea,
       {
          return(Status);
       }
-      Status = CcRosGetCacheSegment(Bcb,
-                                    (ULONG)FileOffset,
-                                    &BaseOffset,
-                                    &BaseAddress,
-                                    &UptoDate,
-                                    &CacheSeg);
+      Status = CcRosGetVacb(SharedCacheMap,
+                            (ULONG)FileOffset,
+                            &BaseOffset,
+                            &BaseAddress,
+                            &UptoDate,
+                            &Vacb);
       if (!NT_SUCCESS(Status))
       {
          return(Status);
@@ -1093,40 +1176,40 @@ MiReadPage(PMEMORY_AREA MemoryArea,
       if (!UptoDate)
       {
          /*
-          * If the cache segment isn't up to date then call the file
+          * If the VACB isn't up to date then call the file
           * system to read in the data.
           */
-         Status = ReadCacheSegment(CacheSeg);
+         Status = CcReadVirtualAddress(Vacb);
          if (!NT_SUCCESS(Status))
          {
-            CcRosReleaseCacheSegment(Bcb, CacheSeg, FALSE, FALSE, FALSE);
+            CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
             return Status;
          }
       }
 
       Process = PsGetCurrentProcess();
       PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
-      CacheSegOffset = (ULONG_PTR)(BaseOffset + CacheSeg->Bcb->CacheSegmentSize - FileOffset);
+      VacbOffset = (ULONG_PTR)(BaseOffset + VACB_MAPPING_GRANULARITY - FileOffset);
       Length = RawLength - SegOffset;
-      if (Length <= CacheSegOffset && Length <= PAGE_SIZE)
+      if (Length <= VacbOffset && Length <= PAGE_SIZE)
       {
          memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, Length);
       }
-      else if (CacheSegOffset >= PAGE_SIZE)
+      else if (VacbOffset >= PAGE_SIZE)
       {
          memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, PAGE_SIZE);
       }
       else
       {
-         memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, CacheSegOffset);
+         memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, VacbOffset);
          MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
-         CcRosReleaseCacheSegment(Bcb, CacheSeg, TRUE, FALSE, FALSE);
-         Status = CcRosGetCacheSegment(Bcb,
-                                       (ULONG)(FileOffset + CacheSegOffset),
-                                       &BaseOffset,
-                                       &BaseAddress,
-                                       &UptoDate,
-                                       &CacheSeg);
+         CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
+         Status = CcRosGetVacb(SharedCacheMap,
+                               (ULONG)(FileOffset + VacbOffset),
+                               &BaseOffset,
+                               &BaseAddress,
+                               &UptoDate,
+                               &Vacb);
          if (!NT_SUCCESS(Status))
          {
             return(Status);
@@ -1134,28 +1217,28 @@ MiReadPage(PMEMORY_AREA MemoryArea,
          if (!UptoDate)
          {
             /*
-             * If the cache segment isn't up to date then call the file
+             * If the VACB isn't up to date then call the file
              * system to read in the data.
              */
-            Status = ReadCacheSegment(CacheSeg);
+            Status = CcReadVirtualAddress(Vacb);
             if (!NT_SUCCESS(Status))
             {
-               CcRosReleaseCacheSegment(Bcb, CacheSeg, FALSE, FALSE, FALSE);
+               CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
                return Status;
             }
          }
          PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
          if (Length < PAGE_SIZE)
          {
-            memcpy((char*)PageAddr + CacheSegOffset, BaseAddress, Length - CacheSegOffset);
+            memcpy((char*)PageAddr + VacbOffset, BaseAddress, Length - VacbOffset);
          }
          else
          {
-            memcpy((char*)PageAddr + CacheSegOffset, BaseAddress, PAGE_SIZE - CacheSegOffset);
+            memcpy((char*)PageAddr + VacbOffset, BaseAddress, PAGE_SIZE - VacbOffset);
          }
       }
       MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
-      CcRosReleaseCacheSegment(Bcb, CacheSeg, TRUE, FALSE, FALSE);
+      CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
    }
    return(STATUS_SUCCESS);
 }
@@ -1275,7 +1358,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
       MmUnlockAddressSpace(AddressSpace);
       MiWaitForPageEvent(NULL, NULL);
       MmLockAddressSpace(AddressSpace);
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_MM_RESTART_OPERATION);
    }
 
@@ -1359,7 +1442,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
        * Finish the operation
        */
       MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_SUCCESS);
    }
 
@@ -1389,53 +1472,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
        * Cleanup and release locks
        */
       MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%.8X\n", Address);
-      return(STATUS_SUCCESS);
-   }
-
-   /*
-    * Map anonymous memory for BSS sections
-    */
-   if (Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
-   {
-      /* We'll be unlocking the address space below.  Prevent us from being preempted
-       * in faulting in the page. */
-      MmCreatePageFileMapping(Process, Address, MM_WAIT_ENTRY);
-      MmUnlockSectionSegment(Segment);
-      MI_SET_USAGE(MI_USAGE_SECTION);
-      if (Process) MI_SET_PROCESS2(Process->ImageFileName);
-      if (!Process) MI_SET_PROCESS2("Kernel Section");
-      Status = MmRequestPageMemoryConsumer(MC_USER, FALSE, &Page);
-      if (!NT_SUCCESS(Status))
-      {
-          MmUnlockAddressSpace(AddressSpace);
-          Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
-          MmLockAddressSpace(AddressSpace);
-      }
-      if (!NT_SUCCESS(Status))
-      {
-          KeBugCheck(MEMORY_MANAGEMENT);
-      }
-      /* Remove the wait entry we placed, so that we can map the page */
-      MmDeletePageFileMapping(Process, PAddress, &SwapEntry);
-      Status = MmCreateVirtualMapping(Process,
-                                      PAddress,
-                                      Region->Protect,
-                                      &Page,
-                                      1);
-      if (!NT_SUCCESS(Status))
-      {
-          DPRINT("MmCreateVirtualMapping failed, not out of memory\n");
-          KeBugCheck(MEMORY_MANAGEMENT);
-          return(Status);
-      }
-      MmInsertRmap(Page, Process, Address);
-
-      /*
-       * Cleanup and release locks
-       */
-      MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_SUCCESS);
    }
 
@@ -1493,7 +1530,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
           */
          MmLockAddressSpace(AddressSpace);
          MiSetPageEvent(Process, Address);
-         DPRINT("Address 0x%.8X\n", Address);
+         DPRINT("Address 0x%p\n", Address);
          return(Status);
       }
 
@@ -1508,7 +1545,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
       MmUnlockSectionSegment(Segment);
 
       MmDeletePageFileMapping(Process, PAddress, &FakeSwapEntry);
-      DPRINT("CreateVirtualMapping Page %x Process %p PAddress %p Attributes %x\n", 
+      DPRINT("CreateVirtualMapping Page %x Process %p PAddress %p Attributes %x\n",
               Page, Process, PAddress, Attributes);
       Status = MmCreateVirtualMapping(Process,
                                       PAddress,
@@ -1524,7 +1561,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
       MmInsertRmap(Page, Process, Address);
 
       MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_SUCCESS);
    }
    else if (IS_SWAP_FROM_SSE(Entry))
@@ -1595,7 +1632,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
       }
       MmInsertRmap(Page, Process, Address);
       MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_SUCCESS);
    }
    else
@@ -1622,7 +1659,7 @@ MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
       }
       MmInsertRmap(Page, Process, Address);
       MiSetPageEvent(Process, Address);
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_SUCCESS);
    }
 }
@@ -1645,14 +1682,14 @@ MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
    SWAPENTRY SwapEntry;
 
-   DPRINT("MmAccessFaultSectionView(%x, %x, %x, %x)\n", AddressSpace, MemoryArea, Address);
+   DPRINT("MmAccessFaultSectionView(%p, %p, %p)\n", AddressSpace, MemoryArea, Address);
 
    /*
     * Check if the page has already been set readwrite
     */
    if (MmGetPageProtect(Process, Address) & PAGE_READWRITE)
    {
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_SUCCESS);
    }
 
@@ -1686,7 +1723,7 @@ MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
          (Region->Protect == PAGE_READWRITE ||
           Region->Protect == PAGE_EXECUTE_READWRITE)))
    {
-      DPRINT("Address 0x%.8X\n", Address);
+      DPRINT("Address 0x%p\n", Address);
       return(STATUS_ACCESS_VIOLATION);
    }
 
@@ -1719,7 +1756,7 @@ MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
         * Restart the operation
         */
        MmLockAddressSpace(AddressSpace);
-       DPRINT("Address 0x%.8X\n", Address);
+       DPRINT("Address 0x%p\n", Address);
        return(STATUS_MM_RESTART_OPERATION);
    }
 
@@ -1778,7 +1815,7 @@ MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
    MmUnlockSectionSegment(Segment);
 
    MiSetPageEvent(Process, Address);
-   DPRINT("Address 0x%.8X\n", Address);
+   DPRINT("Address 0x%p\n", Address);
    return(STATUS_SUCCESS);
 }
 
@@ -1839,7 +1876,7 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
    NTSTATUS Status;
    PFILE_OBJECT FileObject;
 #ifndef NEWCC
-   PBCB Bcb = NULL;
+   PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
 #endif
    BOOLEAN DirectMapped;
    BOOLEAN IsImageSection;
@@ -1871,7 +1908,7 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
    if (FileObject != NULL &&
        !(Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
    {
-      Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
+      SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
 
       /*
        * If the file system is letting us go directly to the cache and the
@@ -1893,8 +1930,8 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
     */
    if (Context.Section->AllocationAttributes & SEC_PHYSICALMEMORY)
    {
-      DPRINT1("Trying to page out from physical memory section address 0x%X "
-              "process %d\n", Address,
+      DPRINT1("Trying to page out from physical memory section address 0x%p "
+              "process %p\n", Address,
               Process ? Process->UniqueProcessId : 0);
        KeBugCheck(MEMORY_MANAGEMENT);
    }
@@ -1904,7 +1941,7 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
     */
    if (!MmIsPagePresent(Process, Address))
    {
-      DPRINT1("Trying to page out not-present page at (%d,0x%.8X).\n",
+      DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
               Process ? Process->UniqueProcessId : 0, Address);
        KeBugCheck(MEMORY_MANAGEMENT);
    }
@@ -1916,7 +1953,7 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
     */
    if (MmGetReferenceCountPage(Page) != 1)
    {
-       DPRINT("Cannot page out locked section page: 0x%p (RefCount: %d)\n",
+       DPRINT("Cannot page out locked section page: 0x%lu (RefCount: %lu)\n",
                Page, MmGetReferenceCountPage(Page));
        MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
        MmUnlockSectionSegment(Context.Segment);
@@ -1940,7 +1977,7 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
    }
 
    /*
-    * Take an additional reference to the page or the cache segment.
+    * Take an additional reference to the page or the VACB.
     */
    if (DirectMapped && !Context.Private)
    {
@@ -1990,7 +2027,7 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
    {
       if (Context.Private)
       {
-         DPRINT1("Found a %s private page (address %x) in a pagefile segment.\n",
+         DPRINT1("Found a %s private page (address %p) in a pagefile segment.\n",
                  Context.WasDirty ? "dirty" : "clean", Address);
          KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
       }
@@ -2009,7 +2046,7 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
    {
       if (Context.Private)
       {
-         DPRINT1("Found a %s private page (address %x) in a shared section segment.\n",
+         DPRINT1("Found a %s private page (address %p) in a shared section segment.\n",
                  Context.WasDirty ? "dirty" : "clean", Address);
          KeBugCheckEx(MEMORY_MANAGEMENT, Page, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
       }
@@ -2031,20 +2068,20 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
    {
       if (SwapEntry != 0)
       {
-         DPRINT1("Found a swapentry for a non private and direct mapped page (address %x)\n",
+         DPRINT1("Found a swapentry for a non private and direct mapped page (address %p)\n",
                  Address);
          KeBugCheckEx(MEMORY_MANAGEMENT, STATUS_UNSUCCESSFUL, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address);
       }
 #ifndef NEWCC
-      Status = CcRosUnmapCacheSegment(Bcb, (ULONG)FileOffset, FALSE);
+      Status = CcRosUnmapVacb(SharedCacheMap, (ULONG)FileOffset, FALSE);
 #else
       Status = STATUS_SUCCESS;
 #endif
 #ifndef NEWCC
       if (!NT_SUCCESS(Status))
       {
-         DPRINT1("CCRosUnmapCacheSegment failed, status = %x\n", Status);
-         KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)Bcb, (ULONG_PTR)FileOffset, (ULONG_PTR)Address);
+         DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
+         KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)SharedCacheMap, (ULONG_PTR)FileOffset, (ULONG_PTR)Address);
       }
 #endif
       MiSetPageEvent(NULL, NULL);
@@ -2054,11 +2091,11 @@ MmPageOutSectionView(PMMSUPPORT AddressSpace,
    {
       if (SwapEntry != 0)
       {
-         DPRINT1("Found a swap entry for a non dirty, non private and not direct mapped page (address %x)\n",
+         DPRINT1("Found a swap entry for a non dirty, non private and not direct mapped page (address %p)\n",
                  Address);
          KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, Page, (ULONG_PTR)Process, (ULONG_PTR)Address);
       }
-      MmReleasePageMemoryConsumer(MC_USER, Page); 
+      MmReleasePageMemoryConsumer(MC_USER, Page);
       MiSetPageEvent(NULL, NULL);
       return(STATUS_SUCCESS);
    }
@@ -2246,7 +2283,7 @@ MmWritePageSectionView(PMMSUPPORT AddressSpace,
    BOOLEAN Private;
    NTSTATUS Status;
    PFILE_OBJECT FileObject;
-   PBCB Bcb = NULL;
+   PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
    BOOLEAN DirectMapped;
    BOOLEAN IsImageSection;
    PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
@@ -2268,7 +2305,7 @@ MmWritePageSectionView(PMMSUPPORT AddressSpace,
    if (FileObject != NULL &&
          !(Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
    {
-      Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
+      SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
 
       /*
        * If the file system is letting us go directly to the cache and the
@@ -2288,8 +2325,8 @@ MmWritePageSectionView(PMMSUPPORT AddressSpace,
     */
    if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
    {
-      DPRINT1("Trying to write back page from physical memory mapped at %X "
-              "process %d\n", Address,
+      DPRINT1("Trying to write back page from physical memory mapped at %p "
+              "process %p\n", Address,
               Process ? Process->UniqueProcessId : 0);
       KeBugCheck(MEMORY_MANAGEMENT);
    }
@@ -2300,7 +2337,7 @@ MmWritePageSectionView(PMMSUPPORT AddressSpace,
    Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
    if (!MmIsPagePresent(Process, Address))
    {
-      DPRINT1("Trying to page out not-present page at (%d,0x%.8X).\n",
+      DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
               Process ? Process->UniqueProcessId : 0, Address);
       KeBugCheck(MEMORY_MANAGEMENT);
    }
@@ -2332,11 +2369,11 @@ MmWritePageSectionView(PMMSUPPORT AddressSpace,
     */
    if (DirectMapped && !Private)
    {
-      LARGE_INTEGER SOffset;
+      //LARGE_INTEGER SOffset;
       ASSERT(SwapEntry == 0);
-      SOffset.QuadPart = Offset.QuadPart + Segment->Image.FileOffset;
+      //SOffset.QuadPart = Offset.QuadPart + Segment->Image.FileOffset;
 #ifndef NEWCC
-      CcRosMarkDirtyCacheSegment(Bcb, Offset.LowPart);
+      CcRosMarkDirtyVacb(SharedCacheMap, Offset.LowPart);
 #endif
       MmLockSectionSegment(Segment);
       MmSetPageEntrySectionSegment(Segment, &Offset, PageEntry);
@@ -2681,8 +2718,7 @@ MmpCloseSection(IN PEPROCESS Process OPTIONAL,
                 IN ULONG ProcessHandleCount,
                 IN ULONG SystemHandleCount)
 {
-   DPRINT("MmpCloseSection(OB %x, HC %d)\n",
-          Object, ProcessHandleCount);
+    DPRINT("MmpCloseSection(OB %p, HC %lu)\n", Object, ProcessHandleCount);
 }
 
 NTSTATUS
@@ -2788,7 +2824,8 @@ MmCreatePageFileSection(PROS_SECTION_OBJECT *SectionObject,
 
    if (UMaximumSize == NULL)
    {
-      return(STATUS_UNSUCCESSFUL);
+      DPRINT1("MmCreatePageFileSection: (UMaximumSize == NULL)\n");
+      return(STATUS_INVALID_PARAMETER);
    }
    MaximumSize = *UMaximumSize;
 
@@ -2806,6 +2843,7 @@ MmCreatePageFileSection(PROS_SECTION_OBJECT *SectionObject,
                            (PVOID*)(PVOID)&Section);
    if (!NT_SUCCESS(Status))
    {
+      DPRINT1("MmCreatePageFileSection: failed to create object (0x%lx)\n", Status);
       return(Status);
    }
 
@@ -3642,18 +3680,18 @@ ExeFmtpCreateImageSection(HANDLE FileHandle,
     * Some defaults
     */
    /* FIXME? are these values platform-dependent? */
-   if(ImageSectionObject->StackReserve == 0)
-      ImageSectionObject->StackReserve = 0x40000;
+   if (ImageSectionObject->ImageInformation.MaximumStackSize == 0)
+       ImageSectionObject->ImageInformation.MaximumStackSize = 0x40000;
 
-   if(ImageSectionObject->StackCommit == 0)
-      ImageSectionObject->StackCommit = 0x1000;
+   if(ImageSectionObject->ImageInformation.CommittedStackSize == 0)
+       ImageSectionObject->ImageInformation.CommittedStackSize = 0x1000;
 
-   if(ImageSectionObject->ImageBase == 0)
+   if(ImageSectionObject->BasedAddress == NULL)
    {
-      if(ImageSectionObject->ImageCharacteristics & IMAGE_FILE_DLL)
-         ImageSectionObject->ImageBase = 0x10000000;
+      if(ImageSectionObject->ImageInformation.ImageCharacteristics & IMAGE_FILE_DLL)
+         ImageSectionObject->BasedAddress = (PVOID)0x10000000;
       else
-         ImageSectionObject->ImageBase = 0x00400000;
+         ImageSectionObject->BasedAddress = (PVOID)0x00400000;
    }
 
    /*
@@ -3873,7 +3911,7 @@ MmMapViewOfSegment(PMMSUPPORT AddressSpace,
 {
    PMEMORY_AREA MArea;
    NTSTATUS Status;
-   PHYSICAL_ADDRESS BoundaryAddressMultiple;
+   ULONG Granularity;
 
    if (Segment->WriteCopy)
    {
@@ -3892,7 +3930,10 @@ MmMapViewOfSegment(PMMSUPPORT AddressSpace,
            Protect = PAGE_EXECUTE_READWRITE;
    }
 
-   BoundaryAddressMultiple.QuadPart = 0;
+   if (*BaseAddress == NULL)
+      Granularity = MM_ALLOCATION_GRANULARITY;
+   else
+      Granularity = PAGE_SIZE;
 
 #ifdef NEWCC
    if (Segment->Flags & MM_DATAFILE_SEGMENT) {
@@ -3910,10 +3951,10 @@ MmMapViewOfSegment(PMMSUPPORT AddressSpace,
                                &MArea,
                                FALSE,
                                AllocationType,
-                               BoundaryAddressMultiple);
+                               Granularity);
    if (!NT_SUCCESS(Status))
    {
-      DPRINT1("Mapping between 0x%.8X and 0x%.8X failed (%X).\n",
+      DPRINT1("Mapping between 0x%p and 0x%p failed (%X).\n",
               (*BaseAddress), (char*)(*BaseAddress) + ViewSize, Status);
       return(Status);
    }
@@ -3936,7 +3977,7 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
 {
    ULONG_PTR Entry;
    PFILE_OBJECT FileObject;
-   PBCB Bcb;
+   PROS_SHARED_CACHE_MAP SharedCacheMap;
    LARGE_INTEGER Offset;
    SWAPENTRY SavedSwapEntry;
    PROS_SECTION_OBJECT Section;
@@ -3977,9 +4018,9 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
       if (Page == PFN_FROM_SSE(Entry) && Dirty)
       {
          FileObject = MemoryArea->Data.SectionData.Section->FileObject;
-         Bcb = FileObject->SectionObjectPointer->SharedCacheMap;
+         SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
 #ifndef NEWCC
-         CcRosMarkDirtyCacheSegment(Bcb, (ULONG)(Offset.QuadPart + Segment->Image.FileOffset));
+         CcRosMarkDirtyVacb(SharedCacheMap, (ULONG)(Offset.QuadPart + Segment->Image.FileOffset));
 #endif
          ASSERT(SwapEntry == 0);
       }
@@ -4099,7 +4140,7 @@ MiRosUnmapViewOfSection(IN PEPROCESS Process,
    PROS_SECTION_OBJECT Section;
    PVOID ImageBaseAddress = 0;
 
-   DPRINT("Opening memory area Process %x BaseAddress %x\n",
+   DPRINT("Opening memory area Process %p BaseAddress %p\n",
           Process, BaseAddress);
 
    ASSERT(Process);
@@ -4113,7 +4154,7 @@ MiRosUnmapViewOfSection(IN PEPROCESS Process,
        MemoryArea->Type != MEMORY_AREA_SECTION_VIEW ||
        MemoryArea->DeleteInProgress)
    {
-      ASSERT(MemoryArea->Type != MEMORY_AREA_OWNED_BY_ARM3);
+      if (MemoryArea) NT_ASSERT(MemoryArea->Type != MEMORY_AREA_OWNED_BY_ARM3);
       MmUnlockAddressSpace(AddressSpace);
       return STATUS_NOT_MAPPED_VIEW;
    }
@@ -4161,12 +4202,14 @@ MiRosUnmapViewOfSection(IN PEPROCESS Process,
                                  ((char*)ImageBaseAddress + (ULONG_PTR)SectionSegments[i].Image.VirtualAddress);
 
             Status = MmUnmapViewOfSegment(AddressSpace, SBaseAddress);
+            NT_ASSERT(NT_SUCCESS(Status));
          }
       }
    }
    else
    {
       Status = MmUnmapViewOfSegment(AddressSpace, BaseAddress);
+      NT_ASSERT(NT_SUCCESS(Status));
    }
 
    MmUnlockAddressSpace(AddressSpace);
@@ -4280,21 +4323,12 @@ NtQuerySection(IN HANDLE SectionHandle,
 
             _SEH2_TRY
             {
-               memset(Sii, 0, sizeof(SECTION_IMAGE_INFORMATION));
                if (Section->AllocationAttributes & SEC_IMAGE)
                {
                   PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
                   ImageSectionObject = Section->ImageSection;
 
-                  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;
+                  *Sii = ImageSectionObject->ImageInformation;
                }
 
                if (ResultLength != NULL)
@@ -4429,11 +4463,10 @@ MmMapViewOfSection(IN PVOID SectionObject,
       SectionSegments = ImageSectionObject->Segments;
       NrSegments = ImageSectionObject->NrSegments;
 
-
       ImageBase = (ULONG_PTR)*BaseAddress;
       if (ImageBase == 0)
       {
-         ImageBase = ImageSectionObject->ImageBase;
+          ImageBase = (ULONG_PTR)ImageSectionObject->BasedAddress;
       }
 
       ImageSize = 0;
@@ -4448,12 +4481,22 @@ MmMapViewOfSection(IN PVOID SectionObject,
          }
       }
 
-      ImageSectionObject->ImageSize = (ULONG)ImageSize;
+      ImageSectionObject->ImageInformation.ImageFileSize = (ULONG)ImageSize;
 
       /* Check for an illegal base address */
-      if ((ImageBase + ImageSize) > (ULONG_PTR)MmHighestUserAddress)
+      if (((ImageBase + ImageSize) > (ULONG_PTR)MmHighestUserAddress) ||
+          ((ImageBase + ImageSize) < ImageSize))
       {
-          ImageBase = PAGE_ROUND_DOWN((ULONG_PTR)MmHighestUserAddress - ImageSize);
+         NT_ASSERT(*BaseAddress == NULL);
+         ImageBase = ALIGN_DOWN_BY((ULONG_PTR)MmHighestUserAddress - ImageSize,
+                                   MM_VIRTMEM_GRANULARITY);
+         NotAtBase = TRUE;
+      }
+      else if (ImageBase != ALIGN_DOWN_BY(ImageBase, MM_VIRTMEM_GRANULARITY))
+      {
+         NT_ASSERT(*BaseAddress == NULL);
+         ImageBase = ALIGN_DOWN_BY(ImageBase, MM_VIRTMEM_GRANULARITY);
+         NotAtBase = TRUE;
       }
 
       /* Check there is enough space to map the section at that point. */
@@ -4464,14 +4507,14 @@ MmMapViewOfSection(IN PVOID SectionObject,
          if ((*BaseAddress) != NULL)
          {
             MmUnlockAddressSpace(AddressSpace);
-            return(STATUS_UNSUCCESSFUL);
+            return(STATUS_CONFLICTING_ADDRESSES);
          }
          /* Otherwise find a gap to map the image. */
-         ImageBase = (ULONG_PTR)MmFindGap(AddressSpace, PAGE_ROUND_UP(ImageSize), PAGE_SIZE, FALSE);
+         ImageBase = (ULONG_PTR)MmFindGap(AddressSpace, PAGE_ROUND_UP(ImageSize), MM_VIRTMEM_GRANULARITY, FALSE);
          if (ImageBase == 0)
          {
             MmUnlockAddressSpace(AddressSpace);
-            return(STATUS_UNSUCCESSFUL);
+            return(STATUS_CONFLICTING_ADDRESSES);
          }
          /* Remember that we loaded image at a different base address */
          NotAtBase = TRUE;
@@ -4579,6 +4622,7 @@ MmMapViewOfSection(IN PVOID SectionObject,
    }
 
    MmUnlockAddressSpace(AddressSpace);
+   NT_ASSERT(*BaseAddress == ALIGN_DOWN_POINTER_BY(*BaseAddress, MM_VIRTMEM_GRANULARITY));
 
    if (NotAtBase)
        Status = STATUS_IMAGE_NOT_AT_BASE;
@@ -4637,8 +4681,8 @@ MmCanFileBeTruncated (IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
           /* Check size of file */
           if (SectionObjectPointer->SharedCacheMap)
           {
-             PBCB Bcb = SectionObjectPointer->SharedCacheMap;
-             if (NewFileSize->QuadPart <= Bcb->FileSize.QuadPart)
+             PROS_SHARED_CACHE_MAP SharedCacheMap = SectionObjectPointer->SharedCacheMap;
+             if (NewFileSize->QuadPart <= SharedCacheMap->FileSize.QuadPart)
              {
                 return FALSE;
              }
@@ -4683,7 +4727,7 @@ MmFlushImageSection (IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
             return FALSE;
          }
 #ifndef NEWCC
-         CcRosSetRemoveOnClose(SectionObjectPointer);
+         CcRosRemoveIfClosed(SectionObjectPointer);
 #endif
          return TRUE;
       case MmFlushForWrite:
@@ -4852,7 +4896,7 @@ MmCreateSection (OUT PVOID  * Section,
                  IN PFILE_OBJECT  FileObject  OPTIONAL)
 {
     NTSTATUS Status;
-    ULONG Protection, FileAccess;
+    ULONG Protection;
     PROS_SECTION_OBJECT *SectionObject = (PROS_SECTION_OBJECT *)Section;
 
     /* Check if an ARM3 section is being created instead */
@@ -4871,55 +4915,54 @@ MmCreateSection (OUT PVOID  * Section,
         }
     }
 
-    /*
-     * Check the protection
-     */
-    Protection = SectionPageProtection & ~(PAGE_GUARD | PAGE_NOCACHE);
-    if (Protection != PAGE_READONLY &&
-        Protection != PAGE_READWRITE &&
-        Protection != PAGE_WRITECOPY &&
-        Protection != PAGE_EXECUTE &&
-        Protection != PAGE_EXECUTE_READ &&
-        Protection != PAGE_EXECUTE_READWRITE &&
-        Protection != PAGE_EXECUTE_WRITECOPY)
+    /* Convert section flag to page flag */
+    if (AllocationAttributes & SEC_NOCACHE) SectionPageProtection |= PAGE_NOCACHE;
+
+    /* Check to make sure the protection is correct. Nt* does this already */
+    Protection = MiMakeProtectionMask(SectionPageProtection);
+    if (Protection == MM_INVALID_PROTECTION)
     {
+        DPRINT1("Page protection is invalid\n");
         return STATUS_INVALID_PAGE_PROTECTION;
     }
 
-    if ((DesiredAccess & SECTION_MAP_WRITE) &&
-        (Protection == PAGE_READWRITE ||
-         Protection == PAGE_EXECUTE_READWRITE) &&
-       !(AllocationAttributes & SEC_IMAGE))
-    {
-        DPRINT("Creating a section with WRITE access\n");
-        FileAccess = FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE;
-    }
-    else
+    /* Check if this is going to be a data or image backed file section */
+    if ((FileHandle) || (FileObject))
     {
-        DPRINT("Creating a section with READ access\n");
-        FileAccess = FILE_READ_DATA | SYNCHRONIZE;
-    }
-
-    /* FIXME: somehow combine this with the above checks */
-    if (AllocationAttributes & SEC_IMAGE)
-        FileAccess = MiArm3GetCorrectFileAccessMask(SectionPageProtection);
+        /* These cannot be mapped with large pages */
+        if (AllocationAttributes & SEC_LARGE_PAGES)
+        {
+            DPRINT1("Large pages cannot be used with an image mapping\n");
+            return STATUS_INVALID_PARAMETER_6;
+        }
 
-    if (!FileObject && FileHandle)
-    {
-        Status = ObReferenceObjectByHandle(FileHandle,
-                                           FileAccess,
-                                           IoFileObjectType,
-                                           ExGetPreviousMode(),
-                                           (PVOID *)&FileObject,
-                                           NULL);
-        if (!NT_SUCCESS(Status))
+        /* Did the caller pass an object? */
+        if (FileObject)
         {
-            DPRINT("Failed: 0x%08lx\n", Status);
-            return Status;
+            /* Reference the object directly */
+            ObReferenceObject(FileObject);
         }
+        else
+        {
+            /* Reference the file handle to get the object */
+            Status = ObReferenceObjectByHandle(FileHandle,
+                                               MmMakeFileAccess[Protection],
+                                               IoFileObjectType,
+                                               ExGetPreviousMode(),
+                                               (PVOID*)&FileObject,
+                                               NULL);
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("Failed to get a handle to the FO: %lx\n", Status);
+                return Status;
+            }
+        }
+    }
+    else
+    {
+        /* A handle must be supplied with SEC_IMAGE, as this is the no-handle path */
+        if (AllocationAttributes & SEC_IMAGE) return STATUS_INVALID_FILE_FOR_SECTION;
     }
-    else if (FileObject)
-        ObReferenceObject(FileObject);
 
 #ifndef NEWCC // A hack for initializing caching.
     // This is needed only in the old case.
@@ -4940,7 +4983,10 @@ MmCreateSection (OUT PVOID  * Section,
                             &ByteOffset,
                             NULL);
         if (!NT_SUCCESS(Status) && Status != STATUS_END_OF_FILE)
+        {
+            DPRINT1("CC failure: %lx\n", Status);
             return Status;
+        }
         // Caching is initialized...
     }
 #endif