Author: ros-arm-bringup
Date: Thu Feb 14 23:30:31 2008
New Revision: 32364
URL: http://svn.reactos.org/svn/reactos?rev=32364&view=rev
Log:
One would expect that a function called "MmIsUsablePage" would return whether a not a page is usable. In other words, we are making sure that the page is free/available, so that we may make use of it. Apparently not so -- MmIsUsable page returned if a page was NOT usable, but was instead "already used". The caller's wish was to ensure he was correctly using a used page, not to check if he could start using a usable page. This would just be an annoying gramatical/logic error (but makes "sense" in the way it's used), if it weren't for the fact that MmIsUsablePage also returned TRUE for BIOS pages (which meant, "yes, you are correctly using/overwriting memory we spent time ensuring to mark as reserved/BIOS").
Renamed the function to MmIsPageInUse, and only return TRUE if the page is in use. Like the name says.
Modified:
trunk/reactos/ntoskrnl/include/internal/mm.h
trunk/reactos/ntoskrnl/mm/freelist.c
trunk/reactos/ntoskrnl/mm/i386/page.c
Modified: trunk/reactos/ntoskrnl/include/internal/mm.h
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/include/internal/…
==============================================================================
--- trunk/reactos/ntoskrnl/include/internal/mm.h (original)
+++ trunk/reactos/ntoskrnl/include/internal/mm.h Thu Feb 14 23:30:31 2008
@@ -1145,7 +1145,7 @@
BOOLEAN
NTAPI
-MmIsUsablePage(PFN_TYPE Page);
+MmIsPageInUse(PFN_TYPE Page);
VOID
NTAPI
Modified: trunk/reactos/ntoskrnl/mm/freelist.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/freelist.c?rev…
==============================================================================
--- trunk/reactos/ntoskrnl/mm/freelist.c (original)
+++ trunk/reactos/ntoskrnl/mm/freelist.c Thu Feb 14 23:30:31 2008
@@ -640,23 +640,17 @@
BOOLEAN
NTAPI
-MmIsUsablePage(PFN_TYPE Pfn)
-{
-
- DPRINT("MmIsUsablePage(PhysicalAddress %x)\n", Pfn << PAGE_SHIFT);
+MmIsPageInUse(PFN_TYPE Pfn)
+{
+
+ DPRINT("MmIsPageInUse(PhysicalAddress %x)\n", Pfn << PAGE_SHIFT);
if (Pfn == 0 || Pfn >= MmPageArraySize)
{
KEBUGCHECK(0);
}
-
- if (MmPageArray[Pfn].Flags.Type != MM_PHYSICAL_PAGE_USED &&
- MmPageArray[Pfn].Flags.Type != MM_PHYSICAL_PAGE_BIOS)
- {
- return(FALSE);
- }
-
- return(TRUE);
+
+ return (MmPageArray[Pfn].Flags.Type == MM_PHYSICAL_PAGE_USED);
}
VOID
Modified: trunk/reactos/ntoskrnl/mm/i386/page.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/i386/page.c?re…
==============================================================================
--- trunk/reactos/ntoskrnl/mm/i386/page.c (original)
+++ trunk/reactos/ntoskrnl/mm/i386/page.c Thu Feb 14 23:30:31 2008
@@ -1941,18 +1941,10 @@
for (i = 0; i < PageCount; i++)
{
- if (!MmIsUsablePage(Pages[i]))
- {
- /* Is this an attempt to map KUSER_SHARED_DATA? */
- if ((Address == (PVOID)0x7FFE0000) && (PageCount == 1) && (Pages[0] == 2))
- {
- // allow
- }
- else
- {
- DPRINT1("Page at address %x not usable\n", PFN_TO_PTE(Pages[i]));
- KEBUGCHECK(0);
- }
+ if (!MmIsPageInUse(Pages[i]))
+ {
+ DPRINT1("Page at address %x not in use\n", PFN_TO_PTE(Pages[i]));
+ KEBUGCHECK(0);
}
}
Author: ros-arm-bringup
Date: Thu Feb 14 23:20:44 2008
New Revision: 32362
URL: http://svn.reactos.org/svn/reactos?rev=32362&view=rev
Log:
Fix a couple of off-by-one bugs we recently introduced -- PFNs are one of the only indexes which are actually 0-based, so you really want to loop from 0 to the last page, inclusive (unlike most loops where you would stop *before* the last element index).
Modified:
trunk/reactos/ntoskrnl/mm/freelist.c
Modified: trunk/reactos/ntoskrnl/mm/freelist.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/freelist.c?rev…
==============================================================================
--- trunk/reactos/ntoskrnl/mm/freelist.c (original)
+++ trunk/reactos/ntoskrnl/mm/freelist.c Thu Feb 14 23:20:44 2008
@@ -354,7 +354,7 @@
KernelPageEnd = LastPhysKernelAddress / PAGE_SIZE;
/* Loop every page on the system */
- for (i = 0; i < MmPageArraySize; i++)
+ for (i = 0; i <= MmPageArraySize; i++)
{
/* Check if it's part of RAM */
if (MiIsPfnRam(BIOSMemoryMap, AddressRangeCount, i))
@@ -417,7 +417,7 @@
MmPageArray[i].MapCount = 1;
MmStats.NrSystemPages++;
}
- else if (i > (MiFreeDescriptor->BasePage + MiFreeDescriptor->PageCount - 1))
+ else if (i >= (MiFreeDescriptor->BasePage + MiFreeDescriptor->PageCount))
{
/* These are pages we allocated above to hold the PFN DB */
MmPageArray[i].Flags.Type = MM_PHYSICAL_PAGE_USED;
Author: ros-arm-bringup
Date: Thu Feb 14 21:33:38 2008
New Revision: 32359
URL: http://svn.reactos.org/svn/reactos?rev=32359&view=rev
Log:
We were looping the memory descriptors in order to find the number of pages that are available to the system, that is to say, your RAM, minus pages that the BIOS said belong to it. This part is good. Next up, we were creating the page array for these pages, up to the highest entry, which we called, the number of pages on the system. This is the problem. Suppose we had 1000 pages somewhere in low memory that were used by the BIOS, we'd now call the total pages RAM - 1000 (correct). However, we'd also set the highest page array entry to RAM - 1000, which is wrong, because esssentially this eats up 10MB of memory, since the top 10MB (which are FREE, usable memory) are never entered into the database. So really, what we want to do is differentiate the TOTAL amount of usable RAM, versus the HIGHEST page that is usable (which is actually what should be the highest entry in the page array). This will reclaim the lost RAM ReactOS has been eating up all these days. But it gets better: eventually, someone noticed ReactOS was eating memory, and added 1MB more to the "total", making the highest entry "1mb higher". This ...kind of... fixes the problem above by giving you one more MB, but what if ReactOS was only eating up 150KB, as was more the case? Then ReactOS would believe that the other 850KB of memory are "Free physical memory", when actually, they're pages that don't even exist. Wow!
Fixed these bugs.
Modified:
trunk/reactos/ntoskrnl/mm/freelist.c
trunk/reactos/ntoskrnl/mm/mminit.c
Modified: trunk/reactos/ntoskrnl/mm/freelist.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/freelist.c?rev…
==============================================================================
--- trunk/reactos/ntoskrnl/mm/freelist.c (original)
+++ trunk/reactos/ntoskrnl/mm/freelist.c Thu Feb 14 21:33:38 2008
@@ -290,7 +290,7 @@
ULONG i;
ULONG Reserved;
NTSTATUS Status;
- PFN_TYPE LastPage, Pfn;
+ PFN_TYPE LastPage, Pfn = 0;
ULONG PdeStart = PsGetCurrentProcess()->Pcb.DirectoryTableBase.LowPart;
ULONG PdePageStart, PdePageEnd;
ULONG VideoPageStart, VideoPageEnd;
Modified: trunk/reactos/ntoskrnl/mm/mminit.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/mminit.c?rev=3…
==============================================================================
--- trunk/reactos/ntoskrnl/mm/mminit.c (original)
+++ trunk/reactos/ntoskrnl/mm/mminit.c Thu Feb 14 21:33:38 2008
@@ -49,7 +49,7 @@
PHYSICAL_ADDRESS MmSharedDataPagePhysicalAddress;
PVOID MiNonPagedPoolStart;
ULONG MiNonPagedPoolLength;
-ULONG MmNumberOfPhysicalPages;
+ULONG MmNumberOfPhysicalPages, MmHighestPhysicalPage, MmLowestPhysicalPage;
extern KMUTANT MmSystemLoadLock;
BOOLEAN MiDbgEnableMdDump =
#ifdef _ARM_
@@ -201,13 +201,12 @@
MmInitializeMemoryConsumer(MC_USER, MmTrimUserMemory);
}
-ULONG
+VOID
NTAPI
MiCountFreePagesInLoaderBlock(PLOADER_PARAMETER_BLOCK LoaderBlock)
{
PLIST_ENTRY NextEntry;
PMEMORY_ALLOCATION_DESCRIPTOR Md;
- ULONG TotalPages = 0;
for (NextEntry = KeLoaderBlock->MemoryDescriptorListHead.Flink;
NextEntry != &KeLoaderBlock->MemoryDescriptorListHead;
@@ -215,19 +214,51 @@
{
Md = CONTAINING_RECORD(NextEntry, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
- if (Md->MemoryType == LoaderBad ||
- Md->MemoryType == LoaderFirmwarePermanent ||
- Md->MemoryType == LoaderSpecialMemory ||
- Md->MemoryType == LoaderBBTMemory)
+ /* Skip invisible memory */
+ if ((Md->MemoryType != LoaderFirmwarePermanent) &&
+ (Md->MemoryType != LoaderSpecialMemory) &&
+ (Md->MemoryType != LoaderHALCachedMemory) &&
+ (Md->MemoryType != LoaderBBTMemory))
{
- /* Don't count these blocks */
- continue;
+ /* Check if BURNMEM was used */
+ if (Md->MemoryType != LoaderBad)
+ {
+ /* Count this in the total of pages */
+ MmNumberOfPhysicalPages += Md->PageCount;
+ }
+
+ /* Check if this is the new lowest page */
+ if (Md->BasePage < MmLowestPhysicalPage)
+ {
+ /* Update the lowest page */
+ MmLowestPhysicalPage = Md->BasePage;
+ }
+
+ /* Check if this is the new highest page */
+ if ((Md->BasePage + Md->PageCount) > MmHighestPhysicalPage)
+ {
+ /* Update the highest page */
+ MmHighestPhysicalPage = Md->BasePage + Md->PageCount - 1;
+ }
}
-
- TotalPages += Md->PageCount;
- }
-
- return TotalPages;
+ }
+}
+
+VOID
+NTAPI
+MiDbgDumpBiosMap(IN PADDRESS_RANGE BIOSMemoryMap,
+ IN ULONG AddressRangeCount)
+{
+ ULONG i;
+
+ DPRINT1("Base\t\tLength\t\tType\n");
+ for (i = 0; i < AddressRangeCount; i++)
+ {
+ DPRINT1("%08lX\t%08lX\t%d\n",
+ BIOSMemoryMap[i].BaseAddrLow,
+ BIOSMemoryMap[i].LengthLow,
+ BIOSMemoryMap[i].Type);
+ }
}
VOID
@@ -269,8 +300,7 @@
Md->MemoryType == LoaderHalCode)
{
if (Md->BasePage+Md->PageCount > LastKrnlPhysAddr)
- LastKrnlPhysAddr = Md->BasePage+Md->PageCount;
-
+ LastKrnlPhysAddr = Md->BasePage+Md->PageCount;
}
}
@@ -293,6 +323,7 @@
/* Dump memory descriptors */
if (MiDbgEnableMdDump) MiDbgDumpMemoryDescriptors();
+ if (MiDbgEnableMdDump) MiDbgDumpBiosMap(BIOSMemoryMap, AddressRangeCount);
/* Set the page directory */
PsGetCurrentProcess()->Pcb.DirectoryTableBase.LowPart = (ULONG)MmGetPageDirectory();
@@ -322,19 +353,9 @@
RtlZeroMemory(&MmStats, sizeof(MmStats));
/* Count RAM */
- MmStats.NrTotalPages = MiCountFreePagesInLoaderBlock(KeLoaderBlock);
- MmNumberOfPhysicalPages = MmStats.NrTotalPages;
- if (!MmStats.NrTotalPages)
- {
- DbgPrint("Memory not detected, default to 8 MB\n");
- MmStats.NrTotalPages = 2048;
- }
- else
- {
- /* HACK: add 1MB for standard memory (not extended). Why? */
- DbgPrint("Used memory %dKb\n", (MmStats.NrTotalPages * PAGE_SIZE) / 1024);
- MmStats.NrTotalPages += 256;
- }
+ MiCountFreePagesInLoaderBlock(KeLoaderBlock);
+ MmStats.NrTotalPages = MmNumberOfPhysicalPages;
+ DbgPrint("Used memory %dKb\n", (MmStats.NrTotalPages * PAGE_SIZE) / 1024);
/* Initialize the kernel address space */
MmInitializeKernelAddressSpace();
@@ -343,7 +364,7 @@
/* Initialize the page list */
LastKernelAddress = (ULONG_PTR)MmInitializePageList(FirstKrnlPhysAddr,
LastKrnlPhysAddr,
- MmStats.NrTotalPages,
+ MmHighestPhysicalPage,
PAGE_ROUND_UP(LastKernelAddress),
BIOSMemoryMap,
AddressRangeCount);