Sync to trunk head (r42241)
[reactos.git] / reactos / ntoskrnl / mm / section.c
index 31b57e1..3bc81c7 100644 (file)
@@ -1,5 +1,4 @@
-/* $Id$
- *
+/*
  * Copyright (C) 1998-2005 ReactOS Team (and the authors from the programmers section)
  *
  * This program is free software; you can redistribute it and/or
@@ -32,7 +31,7 @@
  *                  Ge van Geldorp
  *                  Royce Mitchell III
  *                  Filip Navara
- *                  Aleksey Bragin 
+ *                  Aleksey Bragin
  *                  Jason Filby
  *                  Thomas Weidenmueller
  *                  Gunnar Andre' Dalsnes
@@ -47,7 +46,7 @@
 
 #include <ntoskrnl.h>
 #define NDEBUG
-#include <internal/debug.h>
+#include <debug.h>
 #include <reactos/exeformat.h>
 
 #if defined (ALLOC_PRAGMA)
@@ -137,7 +136,7 @@ MmGetFileNameForSection(IN PROS_SECTION_OBJECT Section,
     if (!NT_SUCCESS(Status))
     {
         /* Failed, free memory */
-        ExFreePool(ObjectNameInfo);
+        ExFreePoolWithTag(ObjectNameInfo, TAG('M', 'm', ' ', ' '));
         return Status;
     }
 
@@ -151,18 +150,59 @@ NTAPI
 MmGetFileNameForAddress(IN PVOID Address,
                         OUT PUNICODE_STRING ModuleName)
 {
-    /*
-     * FIXME: TODO.
-     * Filip says to get the MADDRESS_SPACE from EPROCESS,
-     * then use the MmMarea routines to locate the Marea that 
-     * corresponds to the address. Then make sure it's a section
-     * view type (MEMORY_AREA_SECTION_VIEW) and use the marea's
-     * per-type union to get the .u.SectionView.Section pointer to
-     * the SECTION_OBJECT. Then we can use MmGetFileNameForSection
-     * to get the full filename.
-     */
-    RtlCreateUnicodeString(ModuleName, L"C:\\ReactOS\\system32\\ntdll.dll");
-    return STATUS_SUCCESS;
+   PROS_SECTION_OBJECT Section;
+   PMEMORY_AREA MemoryArea;
+   PMMSUPPORT AddressSpace;
+   POBJECT_NAME_INFORMATION ModuleNameInformation;
+   NTSTATUS Status = STATUS_ADDRESS_NOT_ASSOCIATED;
+
+   /* Get the MM_AVL_TABLE from EPROCESS */
+   if (Address >= MmSystemRangeStart)
+   {
+      AddressSpace = MmGetKernelAddressSpace();
+   }
+   else
+   {
+      AddressSpace = &PsGetCurrentProcess()->Vm;
+   }
+
+   /* Lock address space */
+   MmLockAddressSpace(AddressSpace);
+
+   /* Locate the memory area for the process by address */
+   MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, Address);
+
+   /* Make sure it's a section view type */
+   if ((MemoryArea != NULL) && (MemoryArea->Type == MEMORY_AREA_SECTION_VIEW))
+   {
+      /* Get the section pointer to the SECTION_OBJECT */
+      Section = MemoryArea->Data.SectionData.Section;
+
+      /* Unlock address space */
+      MmUnlockAddressSpace(AddressSpace);
+
+      /* Get the filename of the section */
+      Status = MmGetFileNameForSection(Section,&ModuleNameInformation);
+
+      if (NT_SUCCESS(Status))
+      {
+         /* Init modulename */
+         RtlCreateUnicodeString(ModuleName,
+                                ModuleNameInformation->Name.Buffer);
+
+         /* Free temp taged buffer from MmGetFileNameForSection() */
+         ExFreePoolWithTag(ModuleNameInformation, TAG('M', 'm', ' ', ' '));
+         DPRINT("Found ModuleName %S by address %p\n",
+                ModuleName->Buffer,Address);
+      }
+   }
+   else
+   {
+      /* Unlock address space */
+      MmUnlockAddressSpace(AddressSpace);
+   }
+
+   return Status;
 }
 
 /* Note: Mmsp prefix denotes "Memory Manager Section Private". */
@@ -251,7 +291,7 @@ MmFreeSectionSegments(PFILE_OBJECT FileObject)
          {
             DPRINT1("Image segment %d still referenced (was %d)\n", i,
                     SectionSegments[i].ReferenceCount);
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
          }
          MmFreePageTablesSectionSegment(&SectionSegments[i]);
       }
@@ -269,7 +309,7 @@ MmFreeSectionSegments(PFILE_OBJECT FileObject)
       if (Segment->ReferenceCount != 0)
       {
          DPRINT1("Data segment still referenced\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       MmFreePageTablesSectionSegment(Segment);
       ExFreePool(Segment);
@@ -317,7 +357,7 @@ MmSetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
                                      TAG_SECTION_PAGE_TABLE);
          if (Table == NULL)
          {
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
          }
          memset(Table, 0, sizeof(SECTION_PAGE_TABLE));
          DPRINT("Table %x\n", Table);
@@ -370,16 +410,16 @@ MmSharePageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
    if (Entry == 0)
    {
       DPRINT1("Entry == 0 for MmSharePageEntrySectionSegment\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
    if (SHARE_COUNT_FROM_SSE(Entry) == MAX_SHARE_COUNT)
    {
       DPRINT1("Maximum share count reached\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
    if (IS_SWAP_FROM_SSE(Entry))
    {
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
    Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) + 1);
    MmSetPageEntrySectionSegment(Segment, Offset, Entry);
@@ -400,16 +440,16 @@ MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
    if (Entry == 0)
    {
       DPRINT1("Entry == 0 for MmUnsharePageEntrySectionSegment\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
    if (SHARE_COUNT_FROM_SSE(Entry) == 0)
    {
       DPRINT1("Zero share count for unshare\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
    if (IS_SWAP_FROM_SSE(Entry))
    {
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
    Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) - 1);
    /*
@@ -445,7 +485,7 @@ MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
             if (!NT_SUCCESS(Status))
             {
                DPRINT1("CcRosUnmapCacheSegment failed, status = %x\n", Status);
-               KEBUGCHECK(0);
+                KeBugCheck(MEMORY_MANAGEMENT);
             }
          }
       }
@@ -494,7 +534,7 @@ MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
                   if (!NT_SUCCESS(Status))
                   {
                      DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", Status);
-                     KEBUGCHECK(0);
+                      KeBugCheck(MEMORY_MANAGEMENT);
                   }
                }
                MmSetPageEntrySectionSegment(Segment, Offset, MAKE_SWAP_SSE(SavedSwapEntry));
@@ -505,7 +545,7 @@ MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
          else
          {
             DPRINT1("Found a swapentry for a non private page in an image or data file sgment\n");
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
          }
       }
    }
@@ -617,8 +657,11 @@ MiReadPage(PMEMORY_AREA MemoryArea,
    }
    else
    {
+      PEPROCESS Process;
+      KIRQL Irql;
       PVOID PageAddr;
       ULONG CacheSegOffset;
+
       /*
        * Allocate a page, this is rather complicated by the possibility
        * we might have to move other things out of memory
@@ -651,7 +694,9 @@ MiReadPage(PMEMORY_AREA MemoryArea,
             return Status;
          }
       }
-      PageAddr = MmCreateHyperspaceMapping(*Page);
+
+      Process = PsGetCurrentProcess();
+      PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
       CacheSegOffset = BaseOffset + CacheSeg->Bcb->CacheSegmentSize - FileOffset;
       Length = RawLength - SegOffset;
       if (Length <= CacheSegOffset && Length <= PAGE_SIZE)
@@ -665,6 +710,7 @@ MiReadPage(PMEMORY_AREA MemoryArea,
       else
       {
          memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, CacheSegOffset);
+         MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
          CcRosReleaseCacheSegment(Bcb, CacheSeg, TRUE, FALSE, FALSE);
          Status = CcRosGetCacheSegment(Bcb,
                                        FileOffset + CacheSegOffset,
@@ -674,7 +720,6 @@ MiReadPage(PMEMORY_AREA MemoryArea,
                                        &CacheSeg);
          if (!NT_SUCCESS(Status))
          {
-            MmDeleteHyperspaceMapping(PageAddr);
             return(Status);
          }
          if (!UptoDate)
@@ -687,10 +732,10 @@ MiReadPage(PMEMORY_AREA MemoryArea,
             if (!NT_SUCCESS(Status))
             {
                CcRosReleaseCacheSegment(Bcb, CacheSeg, FALSE, FALSE, FALSE);
-               MmDeleteHyperspaceMapping(PageAddr);
                return Status;
             }
          }
+         PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
          if (Length < PAGE_SIZE)
          {
             memcpy((char*)PageAddr + CacheSegOffset, BaseAddress, Length - CacheSegOffset);
@@ -700,15 +745,15 @@ MiReadPage(PMEMORY_AREA MemoryArea,
             memcpy((char*)PageAddr + CacheSegOffset, BaseAddress, PAGE_SIZE - CacheSegOffset);
          }
       }
+      MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
       CcRosReleaseCacheSegment(Bcb, CacheSeg, TRUE, FALSE, FALSE);
-      MmDeleteHyperspaceMapping(PageAddr);
    }
    return(STATUS_SUCCESS);
 }
 
 NTSTATUS
 NTAPI
-MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
+MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
                              MEMORY_AREA* MemoryArea,
                              PVOID Address,
                              BOOLEAN Locked)
@@ -725,23 +770,24 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
    PMM_PAGEOP PageOp;
    PMM_REGION Region;
    BOOLEAN HasSwapEntry;
-
+   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+    
    /*
     * There is a window between taking the page fault and locking the
     * address space when another thread could load the page so we check
     * that.
     */
-   if (MmIsPagePresent(AddressSpace->Process, Address))
+   if (MmIsPagePresent(Process, Address))
    {
       if (Locked)
       {
-         MmLockPage(MmGetPfnForProcess(AddressSpace->Process, Address));
+         MmLockPage(MmGetPfnForProcess(Process, Address));
       }
       return(STATUS_SUCCESS);
    }
 
    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;
@@ -775,7 +821,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
    if (PageOp == NULL)
    {
       DPRINT1("MmGetPageOp failed\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
 
    /*
@@ -793,12 +839,12 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (Status != STATUS_SUCCESS)
       {
          DPRINT1("Failed to wait for page op, status = %x\n", Status);
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       if (PageOp->Status == STATUS_PENDING)
       {
          DPRINT1("Woke for page op before completion\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       MmLockAddressSpace(AddressSpace);
       /*
@@ -826,10 +872,10 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        * If the completed fault was for another address space then set the
        * page in this one.
        */
-      if (!MmIsPagePresent(AddressSpace->Process, Address))
+      if (!MmIsPagePresent(Process, Address))
       {
          Entry = MmGetPageEntrySectionSegment(Segment, Offset);
-         HasSwapEntry = MmIsPageSwapEntry(AddressSpace->Process, (PVOID)PAddress);
+         HasSwapEntry = MmIsPageSwapEntry(Process, (PVOID)PAddress);
 
          if (PAGE_FROM_SSE(Entry) == 0 || HasSwapEntry)
          {
@@ -848,7 +894,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
          /* FIXME: Should we call MmCreateVirtualMappingUnsafe if
           * (Section->AllocationAttributes & SEC_PHYSICALMEMORY) is true?
           */
-         Status = MmCreateVirtualMapping(AddressSpace->Process,
+         Status = MmCreateVirtualMapping(Process,
                                          Address,
                                          Attributes,
                                          &Page,
@@ -856,9 +902,9 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
          if (!NT_SUCCESS(Status))
          {
             DPRINT1("Unable to create virtual mapping\n");
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
          }
-         MmInsertRmap(Page, AddressSpace->Process, (PVOID)PAddress);
+         MmInsertRmap(Page, Process, (PVOID)PAddress);
       }
       if (Locked)
       {
@@ -871,7 +917,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       return(STATUS_SUCCESS);
    }
 
-   HasSwapEntry = MmIsPageSwapEntry(AddressSpace->Process, (PVOID)PAddress);
+   HasSwapEntry = MmIsPageSwapEntry(Process, (PVOID)PAddress);
    if (HasSwapEntry)
    {
       /*
@@ -885,27 +931,27 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (Segment->Flags & MM_PAGEFILE_SEGMENT)
       {
          DPRINT1("Found a swaped out private page in a pagefile section.\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       MmUnlockSectionSegment(Segment);
-      MmDeletePageFileMapping(AddressSpace->Process, (PVOID)PAddress, &SwapEntry);
+      MmDeletePageFileMapping(Process, (PVOID)PAddress, &SwapEntry);
 
       MmUnlockAddressSpace(AddressSpace);
       Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
       if (!NT_SUCCESS(Status))
       {
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       Status = MmReadFromSwapPage(SwapEntry, Page);
       if (!NT_SUCCESS(Status))
       {
          DPRINT1("MmReadFromSwapPage failed, status = %x\n", Status);
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       MmLockAddressSpace(AddressSpace);
-      Status = MmCreateVirtualMapping(AddressSpace->Process,
+      Status = MmCreateVirtualMapping(Process,
                                       Address,
                                       Region->Protect,
                                       &Page,
@@ -913,7 +959,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (!NT_SUCCESS(Status))
       {
          DPRINT("MmCreateVirtualMapping failed, not out of memory\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
          return(Status);
       }
 
@@ -925,7 +971,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       /*
        * Add the page to the process's working set
        */
-      MmInsertRmap(Page, AddressSpace->Process, (PVOID)PAddress);
+      MmInsertRmap(Page, Process, (PVOID)PAddress);
 
       /*
        * Finish the operation
@@ -950,7 +996,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        * Just map the desired physical page
        */
       Page = Offset >> PAGE_SHIFT;
-      Status = MmCreateVirtualMappingUnsafe(AddressSpace->Process,
+      Status = MmCreateVirtualMappingUnsafe(Process,
                                             Address,
                                             Region->Protect,
                                             &Page,
@@ -958,7 +1004,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (!NT_SUCCESS(Status))
       {
          DPRINT("MmCreateVirtualMappingUnsafe failed, not out of memory\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
          return(Status);
       }
       /*
@@ -994,9 +1040,9 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       }
       if (!NT_SUCCESS(Status))
       {
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
-      Status = MmCreateVirtualMapping(AddressSpace->Process,
+      Status = MmCreateVirtualMapping(Process,
                                       Address,
                                       Region->Protect,
                                       &Page,
@@ -1004,10 +1050,10 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (!NT_SUCCESS(Status))
       {
          DPRINT("MmCreateVirtualMapping failed, not out of memory\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
          return(Status);
       }
-      MmInsertRmap(Page, AddressSpace->Process, (PVOID)PAddress);
+      MmInsertRmap(Page, Process, (PVOID)PAddress);
       if (Locked)
       {
          MmLockPage(Page);
@@ -1085,7 +1131,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (Entry != Entry1)
       {
          DPRINT1("Someone changed ppte entry while we slept\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       /*
@@ -1096,7 +1142,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       MmSetPageEntrySectionSegment(Segment, Offset, Entry);
       MmUnlockSectionSegment(Segment);
 
-      Status = MmCreateVirtualMapping(AddressSpace->Process,
+      Status = MmCreateVirtualMapping(Process,
                                       Address,
                                       Attributes,
                                       &Page,
@@ -1104,9 +1150,9 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (!NT_SUCCESS(Status))
       {
          DPRINT1("Unable to create virtual mapping\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
-      MmInsertRmap(Page, AddressSpace->Process, (PVOID)PAddress);
+      MmInsertRmap(Page, Process, (PVOID)PAddress);
 
       if (Locked)
       {
@@ -1133,13 +1179,13 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
       if (!NT_SUCCESS(Status))
       {
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       Status = MmReadFromSwapPage(SwapEntry, Page);
       if (!NT_SUCCESS(Status))
       {
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       /*
@@ -1156,7 +1202,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (Entry != Entry1)
       {
          DPRINT1("Someone changed ppte entry while we slept\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       /*
@@ -1171,7 +1217,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
        * Save the swap entry.
        */
       MmSetSavedSwapEntryPage(Page, SwapEntry);
-      Status = MmCreateVirtualMapping(AddressSpace->Process,
+      Status = MmCreateVirtualMapping(Process,
                                       Address,
                                       Region->Protect,
                                       &Page,
@@ -1179,9 +1225,9 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (!NT_SUCCESS(Status))
       {
          DPRINT1("Unable to create virtual mapping\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
-      MmInsertRmap(Page, AddressSpace->Process, (PVOID)PAddress);
+      MmInsertRmap(Page, Process, (PVOID)PAddress);
       if (Locked)
       {
          MmLockPage(Page);
@@ -1203,7 +1249,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       MmSharePageEntrySectionSegment(Segment, Offset);
       MmUnlockSectionSegment(Segment);
 
-      Status = MmCreateVirtualMapping(AddressSpace->Process,
+      Status = MmCreateVirtualMapping(Process,
                                       Address,
                                       Attributes,
                                       &Page,
@@ -1211,9 +1257,9 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (!NT_SUCCESS(Status))
       {
          DPRINT1("Unable to create virtual mapping\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
-      MmInsertRmap(Page, AddressSpace->Process, (PVOID)PAddress);
+      MmInsertRmap(Page, Process, (PVOID)PAddress);
       if (Locked)
       {
          MmLockPage(Page);
@@ -1227,7 +1273,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace,
 
 NTSTATUS
 NTAPI
-MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
+MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
                          MEMORY_AREA* MemoryArea,
                          PVOID Address,
                          BOOLEAN Locked)
@@ -1242,14 +1288,15 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    PMM_PAGEOP PageOp;
    PMM_REGION Region;
    ULONG Entry;
-
+   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+    
    DPRINT("MmAccessFaultSectionView(%x, %x, %x, %x)\n", AddressSpace, MemoryArea, Address, Locked);
 
    /*
     * Check if the page has been paged out or has already been set readwrite
     */
-   if (!MmIsPagePresent(AddressSpace->Process, Address) ||
-         MmGetPageProtect(AddressSpace->Process, Address) & PAGE_READWRITE)
+   if (!MmIsPagePresent(Process, Address) ||
+         MmGetPageProtect(Process, Address) & PAGE_READWRITE)
    {
       DPRINT("Address 0x%.8X\n", Address);
       return(STATUS_SUCCESS);
@@ -1259,7 +1306,7 @@ 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;
@@ -1285,14 +1332,14 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
           Region->Protect == PAGE_EXECUTE_READWRITE)))
    {
       DPRINT("Address 0x%.8X\n", Address);
-      return(STATUS_UNSUCCESSFUL);
+      return(STATUS_ACCESS_VIOLATION);
    }
 
    if (IS_SWAP_FROM_SSE(Entry) ||
        PFN_FROM_SSE(Entry) != OldPage)
    {
       /* This is a private page. We must only change the page protection. */
-      MmSetPageProtect(AddressSpace->Process, PAddress, Region->Protect);
+      MmSetPageProtect(Process, PAddress, Region->Protect);
       return(STATUS_SUCCESS);
    }
 
@@ -1304,7 +1351,7 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    if (PageOp == NULL)
    {
       DPRINT1("MmGetPageOp failed\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
 
    /*
@@ -1320,12 +1367,12 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
       if (Status == STATUS_TIMEOUT)
       {
          DPRINT1("Failed to wait for page op, status = %x\n", Status);
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       if (PageOp->Status == STATUS_PENDING)
       {
          DPRINT1("Woke for page op before completion\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       /*
       * Restart the operation
@@ -1347,7 +1394,7 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &NewPage);
    if (!NT_SUCCESS(Status))
    {
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
 
    /*
@@ -1359,12 +1406,12 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    /*
     * Delete the old entry.
     */
-   MmDeleteVirtualMapping(AddressSpace->Process, Address, FALSE, NULL, NULL);
+   MmDeleteVirtualMapping(Process, Address, FALSE, NULL, NULL);
 
    /*
     * Set the PTE to point to the new page
     */
-   Status = MmCreateVirtualMapping(AddressSpace->Process,
+   Status = MmCreateVirtualMapping(Process,
                                    Address,
                                    Region->Protect,
                                    &NewPage,
@@ -1372,13 +1419,13 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    if (!NT_SUCCESS(Status))
    {
       DPRINT("MmCreateVirtualMapping failed, not out of memory\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
       return(Status);
    }
    if (!NT_SUCCESS(Status))
    {
       DPRINT1("Unable to create virtual mapping\n");
-      KEBUGCHECK(0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
    if (Locked)
    {
@@ -1389,8 +1436,8 @@ MmAccessFaultSectionView(PMADDRESS_SPACE AddressSpace,
    /*
     * Unshare the old page.
     */
-   MmDeleteRmap(OldPage, AddressSpace->Process, PAddress);
-   MmInsertRmap(NewPage, AddressSpace->Process, PAddress);
+   MmDeleteRmap(OldPage, Process, PAddress);
+   MmInsertRmap(NewPage, Process, PAddress);
    MmLockSectionSegment(Segment);
    MmUnsharePageEntrySectionSegment(Section, Segment, Offset, FALSE, FALSE);
    MmUnlockSectionSegment(Segment);
@@ -1411,7 +1458,7 @@ MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
    PageOutContext = (MM_SECTION_PAGEOUT_CONTEXT*)Context;
    if (Process)
    {
-      MmLockAddressSpace((PMADDRESS_SPACE)&Process->VadRoot);
+      MmLockAddressSpace(&Process->Vm);
    }
 
    MmDeleteVirtualMapping(Process,
@@ -1435,9 +1482,9 @@ MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
    }
    if (Process)
    {
-      MmUnlockAddressSpace((PMADDRESS_SPACE)&Process->VadRoot);
+      MmUnlockAddressSpace(&Process->Vm);
    }
-   
+
    if (PageOutContext->Private)
    {
       MmReleasePageMemoryConsumer(MC_USER, Page);
@@ -1448,7 +1495,7 @@ MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
 
 NTSTATUS
 NTAPI
-MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
+MmPageOutSectionView(PMMSUPPORT AddressSpace,
                      MEMORY_AREA* MemoryArea,
                      PVOID Address,
                      PMM_PAGEOP PageOp)
@@ -1463,7 +1510,8 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
    PBCB Bcb = NULL;
    BOOLEAN DirectMapped;
    BOOLEAN IsImageSection;
-
+   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+    
    Address = (PVOID)PAGE_ROUND_DOWN(Address);
 
    /*
@@ -1472,7 +1520,7 @@ 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;
 
@@ -1506,21 +1554,21 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
    {
       DPRINT1("Trying to page out from physical memory section address 0x%X "
               "process %d\n", Address,
-              AddressSpace->Process ? AddressSpace->Process->UniqueProcessId : 0);
-      KEBUGCHECK(0);
+              Process ? Process->UniqueProcessId : 0);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
 
    /*
     * Get the section segment entry and the physical address.
     */
    Entry = MmGetPageEntrySectionSegment(Context.Segment, Context.Offset);
-   if (!MmIsPagePresent(AddressSpace->Process, Address))
+   if (!MmIsPagePresent(Process, Address))
    {
       DPRINT1("Trying to page out not-present page at (%d,0x%.8X).\n",
-              AddressSpace->Process ? AddressSpace->Process->UniqueProcessId : 0, Address);
-      KEBUGCHECK(0);
+              Process ? Process->UniqueProcessId : 0, Address);
+       KeBugCheck(MEMORY_MANAGEMENT);
    }
-   Page = MmGetPfnForProcess(AddressSpace->Process, Address);
+   Page = MmGetPfnForProcess(Process, Address);
    SwapEntry = MmGetSavedSwapEntryPage(Page);
 
    /*
@@ -1546,7 +1594,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       if(!MiIsPageFromCache(MemoryArea, Context.Offset))
       {
          DPRINT1("Direct mapped non private page is not associated with the cache.\n");
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
    }
    else
@@ -1565,7 +1613,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       if (!(Context.Segment->Flags & MM_PAGEFILE_SEGMENT) &&
             !(Context.Segment->Characteristics & IMAGE_SCN_MEM_SHARED))
       {
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
    }
 
@@ -1583,7 +1631,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       {
          DPRINT1("Found a %s private page (address %x) in a pagefile segment.\n",
                  Context.WasDirty ? "dirty" : "clean", Address);
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       if (!Context.WasDirty && SwapEntry != 0)
       {
@@ -1601,7 +1649,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       {
          DPRINT1("Found a %s private page (address %x) in a shared section segment.\n",
                  Context.WasDirty ? "dirty" : "clean", Address);
-         KEBUGCHECK(0);
+          KeBugCheck(MEMORY_MANAGEMENT);
       }
       if (!Context.WasDirty || SwapEntry != 0)
       {
@@ -1622,13 +1670,13 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       {
          DPRINT1("Found a swapentry for a non private and direct mapped page (address %x)\n",
                  Address);
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
       Status = CcRosUnmapCacheSegment(Bcb, FileOffset, FALSE);
       if (!NT_SUCCESS(Status))
       {
          DPRINT1("CCRosUnmapCacheSegment failed, status = %x\n", Status);
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
       PageOp->Status = STATUS_SUCCESS;
       MmspCompleteAndReleasePageOp(PageOp);
@@ -1640,7 +1688,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       {
          DPRINT1("Found a swap entry for a non dirty, non private and not direct mapped page (address %x)\n",
                  Address);
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
       MmReleasePageMemoryConsumer(MC_USER, Page);
       PageOp->Status = STATUS_SUCCESS;
@@ -1651,13 +1699,13 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
    {
       MmSetSavedSwapEntryPage(Page, 0);
       MmLockAddressSpace(AddressSpace);
-      Status = MmCreatePageFileMapping(AddressSpace->Process,
+      Status = MmCreatePageFileMapping(Process,
                                        Address,
                                        SwapEntry);
       MmUnlockAddressSpace(AddressSpace);
       if (!NT_SUCCESS(Status))
       {
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
       MmReleasePageMemoryConsumer(MC_USER, Page);
       PageOp->Status = STATUS_SUCCESS;
@@ -1680,14 +1728,14 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
           */
          if (Context.Private)
          {
-            Status = MmCreateVirtualMapping(AddressSpace->Process,
+            Status = MmCreateVirtualMapping(Process,
                                             Address,
                                             MemoryArea->Protect,
                                             &Page,
                                             1);
-            MmSetDirtyPage(AddressSpace->Process, Address);
+            MmSetDirtyPage(Process, Address);
             MmInsertRmap(Page,
-                         AddressSpace->Process,
+                         Process,
                          Address);
          }
          else
@@ -1697,14 +1745,14 @@ 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(AddressSpace->Process,
+            Status = MmCreateVirtualMapping(Process,
                                             Address,
                                             MemoryArea->Protect,
                                             &Page,
                                             1);
-            MmSetDirtyPage(AddressSpace->Process, Address);
+            MmSetDirtyPage(Process, Address);
             MmInsertRmap(Page,
-                         AddressSpace->Process,
+                         Process,
                          Address);
             Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
             MmSetPageEntrySectionSegment(Context.Segment, Context.Offset, Entry);
@@ -1731,26 +1779,26 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
       MmLockAddressSpace(AddressSpace);
       if (Context.Private)
       {
-         Status = MmCreateVirtualMapping(AddressSpace->Process,
+         Status = MmCreateVirtualMapping(Process,
                                          Address,
                                          MemoryArea->Protect,
                                          &Page,
                                          1);
-         MmSetDirtyPage(AddressSpace->Process, Address);
+         MmSetDirtyPage(Process, Address);
          MmInsertRmap(Page,
-                      AddressSpace->Process,
+                      Process,
                       Address);
       }
       else
       {
-         Status = MmCreateVirtualMapping(AddressSpace->Process,
+         Status = MmCreateVirtualMapping(Process,
                                          Address,
                                          MemoryArea->Protect,
                                          &Page,
                                          1);
-         MmSetDirtyPage(AddressSpace->Process, Address);
+         MmSetDirtyPage(Process, Address);
          MmInsertRmap(Page,
-                      AddressSpace->Process,
+                      Process,
                       Address);
          Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
          MmSetPageEntrySectionSegment(Context.Segment, Context.Offset, Entry);
@@ -1779,13 +1827,13 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
    if (Context.Private)
    {
       MmLockAddressSpace(AddressSpace);
-      Status = MmCreatePageFileMapping(AddressSpace->Process,
+      Status = MmCreatePageFileMapping(Process,
                                        Address,
                                        SwapEntry);
       MmUnlockAddressSpace(AddressSpace);
       if (!NT_SUCCESS(Status))
       {
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
    }
    else
@@ -1801,7 +1849,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace,
 
 NTSTATUS
 NTAPI
-MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
+MmWritePageSectionView(PMMSUPPORT AddressSpace,
                        PMEMORY_AREA MemoryArea,
                        PVOID Address,
                        PMM_PAGEOP PageOp)
@@ -1818,10 +1866,11 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
    PBCB Bcb = NULL;
    BOOLEAN DirectMapped;
    BOOLEAN IsImageSection;
+   PEPROCESS Process = MmGetAddressSpaceOwner(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;
 
    /*
@@ -1858,21 +1907,21 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
    {
       DPRINT1("Trying to write back page from physical memory mapped at %X "
               "process %d\n", Address,
-              AddressSpace->Process ? AddressSpace->Process->UniqueProcessId : 0);
-      KEBUGCHECK(0);
+              Process ? Process->UniqueProcessId : 0);
+      KeBugCheck(MEMORY_MANAGEMENT);
    }
 
    /*
     * Get the section segment entry and the physical address.
     */
    Entry = MmGetPageEntrySectionSegment(Segment, Offset);
-   if (!MmIsPagePresent(AddressSpace->Process, Address))
+   if (!MmIsPagePresent(Process, Address))
    {
       DPRINT1("Trying to page out not-present page at (%d,0x%.8X).\n",
-              AddressSpace->Process ? AddressSpace->Process->UniqueProcessId : 0, Address);
-      KEBUGCHECK(0);
+              Process ? Process->UniqueProcessId : 0, Address);
+      KeBugCheck(MEMORY_MANAGEMENT);
    }
-   Page = MmGetPfnForProcess(AddressSpace->Process, Address);
+   Page = MmGetPfnForProcess(Process, Address);
    SwapEntry = MmGetSavedSwapEntryPage(Page);
 
    /*
@@ -1946,8 +1995,8 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace,
    return(STATUS_SUCCESS);
 }
 
-VOID static
-MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
+static VOID
+MmAlterViewAttributes(PMMSUPPORT AddressSpace,
                       PVOID BaseAddress,
                       ULONG RegionSize,
                       ULONG OldType,
@@ -1959,6 +2008,7 @@ MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
    PMM_SECTION_SEGMENT Segment;
    BOOLEAN DoCOW = FALSE;
    ULONG i;
+   PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
 
    MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, BaseAddress);
    Segment = MemoryArea->Data.SectionData.Segment;
@@ -1980,16 +2030,16 @@ MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
           * If we doing COW for this segment then check if the page is
           * already private.
           */
-         if (DoCOW && MmIsPagePresent(AddressSpace->Process, Address))
+         if (DoCOW && MmIsPagePresent(Process, Address))
          {
             ULONG Offset;
             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);
+            Page = MmGetPfnForProcess(Process, Address);
 
             Protect = PAGE_READONLY;
             if (Segment->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
@@ -2000,9 +2050,9 @@ MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
             }
          }
 
-         if (MmIsPagePresent(AddressSpace->Process, Address))
+         if (MmIsPagePresent(Process, Address))
          {
-            MmSetPageProtect(AddressSpace->Process, Address,
+            MmSetPageProtect(Process, Address,
                              Protect);
          }
       }
@@ -2011,7 +2061,7 @@ MmAlterViewAttributes(PMADDRESS_SPACE AddressSpace,
 
 NTSTATUS
 NTAPI
-MmProtectSectionView(PMADDRESS_SPACE AddressSpace,
+MmProtectSectionView(PMMSUPPORT AddressSpace,
                      PMEMORY_AREA MemoryArea,
                      PVOID BaseAddress,
                      ULONG Length,
@@ -2032,7 +2082,6 @@ MmProtectSectionView(PMADDRESS_SPACE AddressSpace,
    if ((MemoryArea->Flags & SEC_NO_CHANGE) &&
        Region->Protect != Protect)
    {
-      CHECKPOINT1;
       return STATUS_INVALID_PAGE_PROTECTION;
    }
 
@@ -2045,11 +2094,11 @@ MmProtectSectionView(PMADDRESS_SPACE AddressSpace,
    return(Status);
 }
 
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 MmQuerySectionView(PMEMORY_AREA MemoryArea,
                    PVOID Address,
                    PMEMORY_BASIC_INFORMATION Info,
-                   PULONG ResultLength)
+                   PSIZE_T ResultLength)
 {
    PMM_REGION Region;
    PVOID RegionBaseAddress;
@@ -2124,7 +2173,7 @@ MmpFreePageFileSegment(PMM_SECTION_SEGMENT Segment)
    }
 }
 
-VOID STDCALL
+VOID NTAPI
 MmpDeleteSection(PVOID ObjectBody)
 {
    PROS_SECTION_OBJECT Section = (PROS_SECTION_OBJECT)ObjectBody;
@@ -2195,15 +2244,15 @@ MmpDeleteSection(PVOID ObjectBody)
    }
 }
 
-VOID STDCALL
+VOID NTAPI
 MmpCloseSection(IN PEPROCESS Process OPTIONAL,
                 IN PVOID Object,
                 IN ACCESS_MASK GrantedAccess,
                 IN ULONG ProcessHandleCount,
                 IN ULONG SystemHandleCount)
 {
-   DPRINT("MmpCloseSection(OB %x, HC %d) RC %d\n",
-          Object, ProcessHandleCount, ObGetObjectPointerCount(Object));
+   DPRINT("MmpCloseSection(OB %x, HC %d)\n",
+          Object, ProcessHandleCount);
 }
 
 NTSTATUS
@@ -2238,7 +2287,7 @@ MmCreatePhysicalMemorySection(VOID)
    if (!NT_SUCCESS(Status))
    {
       DPRINT1("Failed to create PhysicalMemory section\n");
-      KEBUGCHECK(0);
+      KeBugCheck(MEMORY_MANAGEMENT);
    }
    Status = ObInsertObject(PhysSection,
                            NULL,
@@ -2266,7 +2315,7 @@ MmInitSectionImplementation(VOID)
    UNICODE_STRING Name;
 
    DPRINT("Creating Section Object Type\n");
-  
+
    /* Initialize the Section object type  */
    RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
    RtlInitUnicodeString(&Name, L"Section");
@@ -2326,10 +2375,9 @@ MmCreatePageFileSection(PROS_SECTION_OBJECT *SectionObject,
    /*
     * Initialize it
     */
+   RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
    Section->SectionPageProtection = SectionPageProtection;
    Section->AllocationAttributes = AllocationAttributes;
-   Section->Segment = NULL;
-   Section->FileObject = NULL;
    Section->MaximumSize = MaximumSize;
    Segment = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_SECTION_SEGMENT),
                                    TAG_MM_SECTION_SEGMENT);
@@ -2378,6 +2426,7 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
    LARGE_INTEGER Offset;
    CHAR Buffer;
    FILE_STANDARD_INFORMATION FileInfo;
+   ULONG Length;
 
    /*
     * Create the section
@@ -2398,9 +2447,9 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
    /*
     * Initialize it
     */
+   RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
    Section->SectionPageProtection = SectionPageProtection;
    Section->AllocationAttributes = AllocationAttributes;
-   Section->Segment = NULL;
 
    /*
     * Check file access required
@@ -2421,7 +2470,7 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
    Status = ObReferenceObjectByHandle(FileHandle,
                                       FileAccess,
                                       IoFileObjectType,
-                                      UserMode,
+                                      ExGetPreviousMode(),
                                       (PVOID*)(PVOID)&FileObject,
                                       NULL);
    if (!NT_SUCCESS(Status))
@@ -2440,7 +2489,8 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
                                    FileStandardInformation,
                                    sizeof(FILE_STANDARD_INFORMATION),
                                    &FileInfo,
-                                   &Iosb.Information);
+                                   &Length);
+   Iosb.Information = Length;
    if (!NT_SUCCESS(Status))
    {
       ObDereferenceObject(Section);
@@ -2452,9 +2502,9 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
     * FIXME: Revise this once a locking order for file size changes is
     * decided
     */
-   if (UMaximumSize != NULL)
+   if ((UMaximumSize != NULL) && (UMaximumSize->QuadPart != 0))
    {
-      MaximumSize = *UMaximumSize;
+         MaximumSize = *UMaximumSize;
    }
    else
    {
@@ -2631,7 +2681,9 @@ extern NTSTATUS NTAPI ElfFmtCreateSection
 static PEXEFMT_LOADER ExeFmtpLoaders[] =
 {
  PeFmtCreateSection,
+#ifdef __ELF
  ElfFmtCreateSection
+#endif
 };
 
 static
@@ -2677,7 +2729,7 @@ ExeFmtpReadFile(IN PVOID File,
 
    if(Length == 0)
    {
-      KEBUGCHECK(STATUS_INVALID_PARAMETER_4);
+      KeBugCheck(MEMORY_MANAGEMENT);
    }
 
    FileOffset = *Offset;
@@ -2685,7 +2737,7 @@ ExeFmtpReadFile(IN PVOID File,
    /* Negative/special offset: it cannot be used in this context */
    if(FileOffset.u.HighPart < 0)
    {
-      KEBUGCHECK(STATUS_INVALID_PARAMETER_5);
+      KeBugCheck(MEMORY_MANAGEMENT);
    }
 
    AdjustOffset = PAGE_ROUND_DOWN(FileOffset.u.LowPart);
@@ -2754,7 +2806,7 @@ ExeFmtpReadFile(IN PVOID File,
    }
    else
    {
-      ExFreePool(Buffer);
+      ExFreePoolWithTag(Buffer, TAG('M', 'm', 'X', 'r'));
    }
 
    return Status;
@@ -3088,7 +3140,7 @@ MmspPageAlignSegments
           */
          else
          {
-            ASSERT(FALSE);
+            KeBugCheck(MEMORY_MANAGEMENT);
          }
       }
    }
@@ -3163,7 +3215,7 @@ ExeFmtpCreateImageSection(HANDLE FileHandle,
          break;
    }
 
-   ExFreePool(FileHeaderBuffer);
+   ExFreePoolWithTag(FileHeaderBuffer, TAG('M', 'm', 'X', 'r'));
 
    /*
     * No loader handled the format
@@ -3322,6 +3374,7 @@ MmCreateImageSection(PROS_SECTION_OBJECT *SectionObject,
    /*
     * Initialize it
     */
+   RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
    Section->SectionPageProtection = SectionPageProtection;
    Section->AllocationAttributes = AllocationAttributes;
 
@@ -3342,7 +3395,7 @@ MmCreateImageSection(PROS_SECTION_OBJECT *SectionObject,
          ObDereferenceObject(Section);
          return(STATUS_NO_MEMORY);
       }
-      
+
       RtlZeroMemory(ImageSectionObject, sizeof(MM_IMAGE_SECTION_OBJECT));
 
       StatusExeFmt = ExeFmtpCreateImageSection(FileHandle, ImageSectionObject);
@@ -3378,7 +3431,7 @@ MmCreateImageSection(PROS_SECTION_OBJECT *SectionObject,
                                                     ImageSectionObject, NULL))
       {
          /*
-          * An other thread has initialized the some image in the background
+          * An other thread has initialized the same image in the background
           */
          ExFreePool(ImageSectionObject->Segments);
          ExFreePool(ImageSectionObject);
@@ -3431,7 +3484,7 @@ MmCreateImageSection(PROS_SECTION_OBJECT *SectionObject,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 NtCreateSection (OUT PHANDLE SectionHandle,
                  IN ACCESS_MASK DesiredAccess,
                  IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
@@ -3447,19 +3500,23 @@ NtCreateSection (OUT PHANDLE SectionHandle,
 
    PreviousMode = ExGetPreviousMode();
 
-   if(MaximumSize != NULL && PreviousMode != KernelMode)
+   if(PreviousMode != KernelMode)
    {
-     _SEH_TRY
+     _SEH2_TRY
      {
-       /* make a copy on the stack */
-       SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
-       MaximumSize = &SafeMaximumSize;
+       if (MaximumSize != NULL)
+       {
+          /* make a copy on the stack */
+          SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
+          MaximumSize = &SafeMaximumSize;
+       }
+       ProbeForWriteHandle(SectionHandle);
      }
-     _SEH_HANDLE
+     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
      {
-       Status = _SEH_GetExceptionCode();
+       Status = _SEH2_GetExceptionCode();
      }
-     _SEH_END;
+     _SEH2_END;
 
      if(!NT_SUCCESS(Status))
      {
@@ -3506,7 +3563,7 @@ NtCreateSection (OUT PHANDLE SectionHandle,
  *
  * REVISIONS
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 NtOpenSection(PHANDLE   SectionHandle,
               ACCESS_MASK  DesiredAccess,
               POBJECT_ATTRIBUTES ObjectAttributes)
@@ -3519,15 +3576,15 @@ NtOpenSection(PHANDLE   SectionHandle,
 
    if(PreviousMode != KernelMode)
    {
-     _SEH_TRY
+     _SEH2_TRY
      {
        ProbeForWriteHandle(SectionHandle);
      }
-     _SEH_HANDLE
+     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
      {
-       Status = _SEH_GetExceptionCode();
+       Status = _SEH2_GetExceptionCode();
      }
-     _SEH_END;
+     _SEH2_END;
 
      if(!NT_SUCCESS(Status))
      {
@@ -3545,22 +3602,22 @@ NtOpenSection(PHANDLE   SectionHandle,
 
    if(NT_SUCCESS(Status))
    {
-     _SEH_TRY
+     _SEH2_TRY
      {
        *SectionHandle = hSection;
      }
-     _SEH_HANDLE
+     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
      {
-       Status = _SEH_GetExceptionCode();
+       Status = _SEH2_GetExceptionCode();
      }
-     _SEH_END;
+     _SEH2_END;
    }
 
    return(Status);
 }
 
-NTSTATUS static
-MmMapViewOfSegment(PMADDRESS_SPACE AddressSpace,
+static NTSTATUS
+MmMapViewOfSegment(PMMSUPPORT AddressSpace,
                    PROS_SECTION_OBJECT Section,
                    PMM_SECTION_SEGMENT Segment,
                    PVOID* BaseAddress,
@@ -3653,12 +3710,12 @@ MmMapViewOfSegment(PMADDRESS_SPACE AddressSpace,
  *
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 NtMapViewOfSection(IN HANDLE SectionHandle,
                    IN HANDLE ProcessHandle,
                    IN OUT PVOID* BaseAddress  OPTIONAL,
-                   IN ULONG ZeroBits  OPTIONAL,
-                   IN ULONG CommitSize,
+                   IN ULONG_PTR ZeroBits  OPTIONAL,
+                   IN SIZE_T CommitSize,
                    IN OUT PLARGE_INTEGER SectionOffset  OPTIONAL,
                    IN OUT PSIZE_T ViewSize,
                    IN SECTION_INHERIT InheritDisposition,
@@ -3671,16 +3728,16 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
    PROS_SECTION_OBJECT Section;
    PEPROCESS Process;
    KPROCESSOR_MODE PreviousMode;
-   PMADDRESS_SPACE AddressSpace;
+   PMMSUPPORT AddressSpace;
    NTSTATUS Status = STATUS_SUCCESS;
    ULONG tmpProtect;
+   ACCESS_MASK DesiredAccess;
 
    /*
     * Check the protection
     */
    if (Protect & ~PAGE_FLAGS_VALID_FROM_USER_MODE)
    {
-     CHECKPOINT1;
      return STATUS_INVALID_PARAMETER_10;
    }
 
@@ -3694,7 +3751,6 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
        tmpProtect != PAGE_EXECUTE_READWRITE &&
        tmpProtect != PAGE_EXECUTE_WRITECOPY)
    {
-     CHECKPOINT1;
      return STATUS_INVALID_PAGE_PROTECTION;
    }
 
@@ -3706,7 +3762,7 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
      SafeSectionOffset.QuadPart = 0;
      SafeViewSize = 0;
 
-     _SEH_TRY
+     _SEH2_TRY
      {
        if(BaseAddress != NULL)
        {
@@ -3721,11 +3777,11 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
        ProbeForWriteSize_t(ViewSize);
        SafeViewSize = *ViewSize;
      }
-     _SEH_HANDLE
+     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
      {
-       Status = _SEH_GetExceptionCode();
+       Status = _SEH2_GetExceptionCode();
      }
-     _SEH_END;
+     _SEH2_END;
 
      if(!NT_SUCCESS(Status))
      {
@@ -3752,10 +3808,29 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
       return(Status);
    }
 
-   AddressSpace = (PMADDRESS_SPACE)&Process->VadRoot;
+   AddressSpace = &Process->Vm;
+
+   /* Convert NT Protection Attr to Access Mask */
+   if (Protect == PAGE_READONLY)
+   {
+      DesiredAccess = SECTION_MAP_READ;
+   }
+   else if (Protect == PAGE_READWRITE)
+   {
+      DesiredAccess = SECTION_MAP_WRITE;
+   }
+   else if (Protect == PAGE_WRITECOPY)
+   {
+      DesiredAccess = SECTION_QUERY;
+   }
+   /* FIXME: Handle other Protection Attributes. For now keep previous behavior */
+   else
+   {
+      DesiredAccess = SECTION_MAP_READ;
+   }
 
    Status = ObReferenceObjectByHandle(SectionHandle,
-                                      SECTION_MAP_READ,
+                                      DesiredAccess,
                                       MmSectionObjectType,
                                       PreviousMode,
                                       (PVOID*)(PVOID)&Section,
@@ -3796,7 +3871,7 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
    if(NT_SUCCESS(Status))
    {
      /* copy parameters back to the caller */
-     _SEH_TRY
+     _SEH2_TRY
      {
        if(BaseAddress != NULL)
        {
@@ -3811,17 +3886,17 @@ NtMapViewOfSection(IN HANDLE SectionHandle,
          *ViewSize = SafeViewSize;
        }
      }
-     _SEH_HANDLE
+     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
      {
-       Status = _SEH_GetExceptionCode();
+       Status = _SEH2_GetExceptionCode();
      }
-     _SEH_END;
+     _SEH2_END;
    }
 
    return(Status);
 }
 
-VOID static
+static VOID
 MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
                   PFN_TYPE Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
 {
@@ -3834,9 +3909,11 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
    NTSTATUS Status;
    PROS_SECTION_OBJECT Section;
    PMM_SECTION_SEGMENT Segment;
-   PMADDRESS_SPACE AddressSpace;
+   PMMSUPPORT AddressSpace;
+   PEPROCESS Process;
 
-   AddressSpace = (PMADDRESS_SPACE)Context;
+   AddressSpace = (PMMSUPPORT)Context;
+   Process = MmGetAddressSpaceOwner(AddressSpace);
 
    Address = (PVOID)PAGE_ROUND_DOWN(Address);
 
@@ -3857,7 +3934,7 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
       if (Status != STATUS_SUCCESS)
       {
          DPRINT1("Failed to wait for page op, status = %x\n", Status);
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       MmLockAddressSpace(AddressSpace);
@@ -3891,7 +3968,7 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
       if (Segment->Flags & MM_PAGEFILE_SEGMENT)
       {
          DPRINT1("Found a swap entry for a page in a pagefile section.\n");
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
       MmFreeSwapPage(SwapEntry);
    }
@@ -3906,7 +3983,7 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
          if (Segment->Flags & MM_PAGEFILE_SEGMENT)
          {
             DPRINT1("Found a private page in a pagefile section.\n");
-            KEBUGCHECK(0);
+            KeBugCheck(MEMORY_MANAGEMENT);
          }
          /*
           * Just dereference private pages
@@ -3917,19 +3994,19 @@ MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
             MmFreeSwapPage(SavedSwapEntry);
             MmSetSavedSwapEntryPage(Page, 0);
          }
-         MmDeleteRmap(Page, AddressSpace->Process, Address);
+         MmDeleteRmap(Page, Process, Address);
          MmReleasePageMemoryConsumer(MC_USER, Page);
       }
       else
       {
-         MmDeleteRmap(Page, AddressSpace->Process, Address);
+         MmDeleteRmap(Page, Process, Address);
          MmUnsharePageEntrySectionSegment(Section, Segment, Offset, Dirty, FALSE);
       }
    }
 }
 
 static NTSTATUS
-MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
+MmUnmapViewOfSegment(PMMSUPPORT AddressSpace,
                      PVOID BaseAddress)
 {
    NTSTATUS Status;
@@ -3958,7 +4035,7 @@ MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
    {
       CurrentEntry = RemoveHeadList(RegionListHead);
       CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION, RegionListEntry);
-      ExFreePool(CurrentRegion);
+      ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
    }
 
    if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
@@ -3983,13 +4060,13 @@ MmUnmapViewOfSegment(PMADDRESS_SPACE AddressSpace,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 MmUnmapViewOfSection(PEPROCESS Process,
                      PVOID BaseAddress)
 {
    NTSTATUS Status;
    PMEMORY_AREA MemoryArea;
-   PMADDRESS_SPACE AddressSpace;
+   PMMSUPPORT AddressSpace;
    PROS_SECTION_OBJECT Section;
    PMM_PAGEOP PageOp;
    ULONG_PTR Offset;
@@ -4000,8 +4077,8 @@ MmUnmapViewOfSection(PEPROCESS Process,
 
    ASSERT(Process);
 
-   AddressSpace = (PMADDRESS_SPACE)&(Process)->VadRoot;
-   
+   AddressSpace = &Process->Vm;
+
    MmLockAddressSpace(AddressSpace);
    MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace,
                                             BaseAddress);
@@ -4032,7 +4109,7 @@ MmUnmapViewOfSection(PEPROCESS Process,
             if (Status != STATUS_SUCCESS)
             {
                DPRINT1("Failed to wait for page op, status = %x\n", Status);
-               KEBUGCHECK(0);
+               KeBugCheck(MEMORY_MANAGEMENT);
             }
             MmLockAddressSpace(AddressSpace);
             MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace,
@@ -4078,7 +4155,7 @@ MmUnmapViewOfSection(PEPROCESS Process,
       }
       if (i >= NrSegments)
       {
-         KEBUGCHECK(0);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       for (i = 0; i < NrSegments; i++)
@@ -4097,10 +4174,11 @@ MmUnmapViewOfSection(PEPROCESS Process,
       Status = MmUnmapViewOfSegment(AddressSpace, BaseAddress);
    }
 
+   MmUnlockAddressSpace(AddressSpace);
+
    /* Notify debugger */
    if (ImageBaseAddress) DbgkUnMapViewOfSection(ImageBaseAddress);
 
-   MmUnlockAddressSpace(AddressSpace);
    return(STATUS_SUCCESS);
 }
 
@@ -4120,7 +4198,7 @@ MmUnmapViewOfSection(PEPROCESS Process,
  *
  * REVISIONS
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 NtUnmapViewOfSection (HANDLE ProcessHandle,
                       PVOID BaseAddress)
 {
@@ -4176,16 +4254,17 @@ NtUnmapViewOfSection (HANDLE ProcessHandle,
  *
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 NtQuerySection(IN HANDLE SectionHandle,
                IN SECTION_INFORMATION_CLASS SectionInformationClass,
                OUT PVOID SectionInformation,
-               IN ULONG SectionInformationLength,
-               OUT PULONG ResultLength  OPTIONAL)
+               IN SIZE_T SectionInformationLength,
+               OUT PSIZE_T ResultLength  OPTIONAL)
 {
    PROS_SECTION_OBJECT Section;
    KPROCESSOR_MODE PreviousMode;
    NTSTATUS Status = STATUS_SUCCESS;
+   PAGED_CODE();
 
    PreviousMode = ExGetPreviousMode();
 
@@ -4194,6 +4273,7 @@ NtQuerySection(IN HANDLE SectionHandle,
                                         sizeof(ExSectionInfoClass) / sizeof(ExSectionInfoClass[0]),
                                         SectionInformation,
                                         SectionInformationLength,
+                                        NULL,
                                         ResultLength,
                                         PreviousMode);
 
@@ -4217,7 +4297,7 @@ NtQuerySection(IN HANDLE SectionHandle,
          {
             PSECTION_BASIC_INFORMATION Sbi = (PSECTION_BASIC_INFORMATION)SectionInformation;
 
-            _SEH_TRY
+            _SEH2_TRY
             {
                Sbi->Attributes = Section->AllocationAttributes;
                if (Section->AllocationAttributes & SEC_IMAGE)
@@ -4237,11 +4317,11 @@ NtQuerySection(IN HANDLE SectionHandle,
                }
                Status = STATUS_SUCCESS;
             }
-            _SEH_HANDLE
+            _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
             {
-               Status = _SEH_GetExceptionCode();
+               Status = _SEH2_GetExceptionCode();
             }
-            _SEH_END;
+            _SEH2_END;
 
             break;
          }
@@ -4250,7 +4330,7 @@ NtQuerySection(IN HANDLE SectionHandle,
          {
             PSECTION_IMAGE_INFORMATION Sii = (PSECTION_IMAGE_INFORMATION)SectionInformation;
 
-            _SEH_TRY
+            _SEH2_TRY
             {
                memset(Sii, 0, sizeof(SECTION_IMAGE_INFORMATION));
                if (Section->AllocationAttributes & SEC_IMAGE)
@@ -4261,7 +4341,7 @@ NtQuerySection(IN HANDLE SectionHandle,
                   Sii->TransferAddress = (PVOID)ImageSectionObject->EntryPoint;
                   Sii->MaximumStackSize = ImageSectionObject->StackReserve;
                   Sii->CommittedStackSize = ImageSectionObject->StackCommit;
-                  Sii->SubsystemType = ImageSectionObject->Subsystem;
+                  Sii->SubSystemType = ImageSectionObject->Subsystem;
                   Sii->SubSystemMinorVersion = ImageSectionObject->MinorSubsystemVersion;
                   Sii->SubSystemMajorVersion = ImageSectionObject->MajorSubsystemVersion;
                   Sii->ImageCharacteristics = ImageSectionObject->ImageCharacteristics;
@@ -4275,11 +4355,11 @@ NtQuerySection(IN HANDLE SectionHandle,
                }
                Status = STATUS_SUCCESS;
             }
-            _SEH_HANDLE
+            _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
             {
-               Status = _SEH_GetExceptionCode();
+               Status = _SEH2_GetExceptionCode();
             }
-            _SEH_END;
+            _SEH2_END;
 
             break;
          }
@@ -4306,7 +4386,7 @@ NtQuerySection(IN HANDLE SectionHandle,
  * @todo Move the actual code to internal function MmExtendSection.
  * @unimplemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 NtExtendSection(IN HANDLE SectionHandle,
                 IN PLARGE_INTEGER NewMaximumSize)
 {
@@ -4319,17 +4399,17 @@ NtExtendSection(IN HANDLE SectionHandle,
 
    if(PreviousMode != KernelMode)
    {
-     _SEH_TRY
+     _SEH2_TRY
      {
        /* make a copy on the stack */
        SafeNewMaximumSize = ProbeForReadLargeInteger(NewMaximumSize);
        NewMaximumSize = &SafeNewMaximumSize;
      }
-     _SEH_HANDLE
+     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
      {
-       Status = _SEH_GetExceptionCode();
+       Status = _SEH2_GetExceptionCode();
      }
-     _SEH_END;
+     _SEH2_END;
 
      if(!NT_SUCCESS(Status))
      {
@@ -4350,7 +4430,7 @@ NtExtendSection(IN HANDLE SectionHandle,
 
    if (!(Section->AllocationAttributes & SEC_FILE))
    {
-      ObfDereferenceObject(Section);
+      ObDereferenceObject(Section);
       return STATUS_INVALID_PARAMETER;
    }
 
@@ -4384,14 +4464,13 @@ NtExtendSection(IN HANDLE SectionHandle,
  *
  * REVISIONS
  */
-PVOID STDCALL
+PVOID NTAPI
 MmAllocateSection (IN ULONG Length, PVOID BaseAddress)
 {
    PVOID Result;
    MEMORY_AREA* marea;
    NTSTATUS Status;
-   ULONG i;
-   PMADDRESS_SPACE AddressSpace;
+   PMMSUPPORT AddressSpace;
    PHYSICAL_ADDRESS BoundaryAddressMultiple;
 
    DPRINT("MmAllocateSection(Length %x)\n",Length);
@@ -4417,27 +4496,10 @@ MmAllocateSection (IN ULONG Length, PVOID BaseAddress)
       return (NULL);
    }
    DPRINT("Result %p\n",Result);
-   for (i = 0; i < PAGE_ROUND_UP(Length) / PAGE_SIZE; i++)
-   {
-      PFN_TYPE Page;
 
-      Status = MmRequestPageMemoryConsumer(MC_NPPOOL, TRUE, &Page);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Unable to allocate page\n");
-         KEBUGCHECK(0);
-      }
-      Status = MmCreateVirtualMapping (NULL,
-                                       (PVOID)((ULONG_PTR)Result + (i * PAGE_SIZE)),
-                                       PAGE_READWRITE,
-                                       &Page,
-                                       1);
-      if (!NT_SUCCESS(Status))
-      {
-         DPRINT1("Unable to create virtual mapping\n");
-         KEBUGCHECK(0);
-      }
-   }
+   /* Create a virtual mapping for this memory area */
+   MmMapMemoryArea(Result, Length, MC_NPPOOL, PAGE_READWRITE);
+
    return ((PVOID)Result);
 }
 
@@ -4491,12 +4553,12 @@ MmAllocateSection (IN ULONG Length, PVOID BaseAddress)
  *
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 MmMapViewOfSection(IN PVOID SectionObject,
                    IN PEPROCESS Process,
                    IN OUT PVOID *BaseAddress,
-                   IN ULONG ZeroBits,
-                   IN ULONG CommitSize,
+                   IN ULONG_PTR ZeroBits,
+                   IN SIZE_T CommitSize,
                    IN OUT PLARGE_INTEGER SectionOffset OPTIONAL,
                    IN OUT PSIZE_T ViewSize,
                    IN SECTION_INHERIT InheritDisposition,
@@ -4504,27 +4566,20 @@ MmMapViewOfSection(IN PVOID SectionObject,
                    IN ULONG Protect)
 {
    PROS_SECTION_OBJECT Section;
-   PMADDRESS_SPACE AddressSpace;
+   PMMSUPPORT AddressSpace;
    ULONG ViewOffset;
    NTSTATUS Status = STATUS_SUCCESS;
 
    ASSERT(Process);
 
-   if (Protect != PAGE_READONLY &&
-       Protect != PAGE_READWRITE &&
-       Protect != PAGE_WRITECOPY &&
-       Protect != PAGE_EXECUTE &&
-       Protect != PAGE_EXECUTE_READ &&
-       Protect != PAGE_EXECUTE_READWRITE &&
-       Protect != PAGE_EXECUTE_WRITECOPY)
+   if (!Protect || Protect & ~PAGE_FLAGS_VALID_FOR_SECTION)
    {
-      CHECKPOINT1;
       return STATUS_INVALID_PAGE_PROTECTION;
    }
 
 
    Section = (PROS_SECTION_OBJECT)SectionObject;
-   AddressSpace = (PMADDRESS_SPACE)&(Process)->VadRoot;
+   AddressSpace = &Process->Vm;
 
    AllocationType |= (Section->AllocationAttributes & SEC_NO_CHANGE);
 
@@ -4615,21 +4670,21 @@ MmMapViewOfSection(IN PVOID SectionObject,
       if ((Protect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE)) &&
           !(Section->SectionPageProtection & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE)))
       {
-         CHECKPOINT1;
+         MmUnlockAddressSpace(AddressSpace);
          return STATUS_SECTION_PROTECTION;
       }
       /* check for read access */
       if ((Protect & (PAGE_READONLY|PAGE_WRITECOPY|PAGE_EXECUTE_READ|PAGE_EXECUTE_WRITECOPY)) &&
           !(Section->SectionPageProtection & (PAGE_READONLY|PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)))
       {
-         CHECKPOINT1;
+         MmUnlockAddressSpace(AddressSpace);
          return STATUS_SECTION_PROTECTION;
       }
       /* check for execute access */
       if ((Protect & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)) &&
           !(Section->SectionPageProtection & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)))
       {
-         CHECKPOINT1;
+         MmUnlockAddressSpace(AddressSpace);
          return STATUS_SECTION_PROTECTION;
       }
 
@@ -4637,7 +4692,7 @@ MmMapViewOfSection(IN PVOID SectionObject,
       {
          /* Following this pointer would lead to us to the dark side */
          /* What to do? Bugcheck? Return status? Do the mambo? */
-         KEBUGCHECK(MEMORY_MANAGEMENT);
+         KeBugCheck(MEMORY_MANAGEMENT);
       }
 
       if (SectionOffset == NULL)
@@ -4664,6 +4719,8 @@ MmMapViewOfSection(IN PVOID SectionObject,
          (*ViewSize) = Section->MaximumSize.u.LowPart - ViewOffset;
       }
 
+      *ViewSize = PAGE_ROUND_UP(*ViewSize);
+
       MmLockSectionSegment(Section->Segment);
       Status = MmMapViewOfSegment(AddressSpace,
                                   Section,
@@ -4689,19 +4746,55 @@ MmMapViewOfSection(IN PVOID SectionObject,
 /*
  * @unimplemented
  */
-BOOLEAN STDCALL
+BOOLEAN NTAPI
 MmCanFileBeTruncated (IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
                       IN PLARGE_INTEGER   NewFileSize)
 {
-   UNIMPLEMENTED;
-   return (FALSE);
+   /* Check whether an ImageSectionObject exists */
+   if (SectionObjectPointer->ImageSectionObject != NULL)
+   {
+      DPRINT1("ERROR: File can't be truncated because it has an image section\n");
+      return FALSE;
+   }
+
+   if (SectionObjectPointer->DataSectionObject != NULL)
+   {
+      PMM_SECTION_SEGMENT Segment;
+
+      Segment = (PMM_SECTION_SEGMENT)SectionObjectPointer->
+                DataSectionObject;
+
+      if (Segment->ReferenceCount != 0)
+      {
+          /* Check size of file */
+          if (SectionObjectPointer->SharedCacheMap)
+          {
+             PBCB Bcb = SectionObjectPointer->SharedCacheMap;
+             if (NewFileSize->QuadPart <= Bcb->FileSize.QuadPart)
+             {
+                return FALSE;
+             }
+          }
+      }
+      else
+      {
+         /* Something must gone wrong
+          * how can we have a Section but no 
+          * reference? */
+         DPRINT1("ERROR: DataSectionObject without reference!\n");
+      }
+   }
+
+   DPRINT("FIXME: didn't check for outstanding write probes\n");
+
+   return TRUE;
 }
 
 
 /*
  * @unimplemented
  */
-BOOLEAN STDCALL
+BOOLEAN NTAPI
 MmDisableModifiedWriteOfSection (ULONG Unknown0)
 {
    UNIMPLEMENTED;
@@ -4711,7 +4804,7 @@ MmDisableModifiedWriteOfSection (ULONG Unknown0)
 /*
  * @implemented
  */
-BOOLEAN STDCALL
+BOOLEAN NTAPI
 MmFlushImageSection (IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
                      IN MMFLUSH_TYPE   FlushType)
 {
@@ -4734,7 +4827,7 @@ MmFlushImageSection (IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
 /*
  * @unimplemented
  */
-BOOLEAN STDCALL
+BOOLEAN NTAPI
 MmForceSectionClosed (
     IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
     IN BOOLEAN                  DelayClose)
@@ -4747,13 +4840,13 @@ MmForceSectionClosed (
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 MmMapViewInSystemSpace (IN PVOID SectionObject,
                         OUT PVOID * MappedBase,
-                        IN OUT PULONG ViewSize)
+                        IN OUT PSIZE_T ViewSize)
 {
    PROS_SECTION_OBJECT Section;
-   PMADDRESS_SPACE AddressSpace;
+   PMMSUPPORT AddressSpace;
    NTSTATUS Status;
 
    DPRINT("MmMapViewInSystemSpace() called\n");
@@ -4795,7 +4888,7 @@ MmMapViewInSystemSpace (IN PVOID SectionObject,
  * @unimplemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 MmMapViewInSessionSpace (
     IN PVOID Section,
     OUT PVOID *MappedBase,
@@ -4810,10 +4903,10 @@ MmMapViewInSessionSpace (
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 MmUnmapViewInSystemSpace (IN PVOID MappedBase)
 {
-   PMADDRESS_SPACE AddressSpace;
+   PMMSUPPORT AddressSpace;
    NTSTATUS Status;
 
    DPRINT("MmUnmapViewInSystemSpace() called\n");
@@ -4829,7 +4922,7 @@ MmUnmapViewInSystemSpace (IN PVOID MappedBase)
  * @unimplemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 MmUnmapViewInSessionSpace (
     IN PVOID MappedBase
     )
@@ -4841,7 +4934,7 @@ MmUnmapViewInSessionSpace (
 /*
  * @unimplemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 MmSetBankedSection (ULONG Unknown0,
                     ULONG Unknown1,
                     ULONG Unknown2,
@@ -4909,7 +5002,7 @@ MmSetBankedSection (ULONG Unknown0,
  *
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 MmCreateSection (OUT PVOID  * Section,
                  IN ACCESS_MASK  DesiredAccess,
                  IN POBJECT_ATTRIBUTES ObjectAttributes     OPTIONAL,
@@ -4926,8 +5019,7 @@ MmCreateSection (OUT PVOID  * Section,
     * Check the protection
     */
    Protection = SectionPageProtection & ~(PAGE_GUARD|PAGE_NOCACHE);
-   if (Protection != PAGE_NOACCESS &&
-       Protection != PAGE_READONLY &&
+   if (Protection != PAGE_READONLY &&
        Protection != PAGE_READWRITE &&
        Protection != PAGE_WRITECOPY &&
        Protection != PAGE_EXECUTE &&
@@ -4935,7 +5027,6 @@ MmCreateSection (OUT PVOID  * Section,
        Protection != PAGE_EXECUTE_READWRITE &&
        Protection != PAGE_EXECUTE_WRITECOPY)
    {
-     CHECKPOINT1;
      return STATUS_INVALID_PAGE_PROTECTION;
    }
 
@@ -4972,8 +5063,8 @@ MmCreateSection (OUT PVOID  * Section,
 NTSTATUS
 NTAPI
 NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle,
-                            IN OUT PULONG NumberOfPages,
-                            IN OUT PULONG UserPfnArray)
+                            IN OUT PULONG_PTR NumberOfPages,
+                            IN OUT PULONG_PTR UserPfnArray)
 {
     UNIMPLEMENTED;
     return STATUS_NOT_IMPLEMENTED;
@@ -4981,9 +5072,9 @@ NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle,
 
 NTSTATUS
 NTAPI
-NtMapUserPhysicalPages(IN PVOID *VirtualAddresses,
-                       IN ULONG NumberOfPages,
-                       IN OUT PULONG UserPfnArray)
+NtMapUserPhysicalPages(IN PVOID VirtualAddresses,
+                       IN ULONG_PTR NumberOfPages,
+                       IN OUT PULONG_PTR UserPfnArray)
 {
     UNIMPLEMENTED;
     return STATUS_NOT_IMPLEMENTED;
@@ -4992,8 +5083,8 @@ NtMapUserPhysicalPages(IN PVOID *VirtualAddresses,
 NTSTATUS
 NTAPI
 NtMapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses,
-                              IN ULONG NumberOfPages,
-                              IN OUT PULONG UserPfnArray)
+                              IN ULONG_PTR NumberOfPages,
+                              IN OUT PULONG_PTR UserPfnArray)
 {
     UNIMPLEMENTED;
     return STATUS_NOT_IMPLEMENTED;
@@ -5002,8 +5093,8 @@ NtMapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses,
 NTSTATUS
 NTAPI
 NtFreeUserPhysicalPages(IN HANDLE ProcessHandle,
-                        IN OUT PULONG NumberOfPages,
-                        IN OUT PULONG UserPfnArray)
+                        IN OUT PULONG_PTR NumberOfPages,
+                        IN OUT PULONG_PTR UserPfnArray)
 {
     UNIMPLEMENTED;
     return STATUS_NOT_IMPLEMENTED;