Merge freeldr from amd64 branch:
[reactos.git] / reactos / boot / freeldr / freeldr / mm / mm.c
index 23a9ae1..2f08b9c 100644 (file)
@@ -35,7 +35,7 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
 
        if (MemorySize == 0)
        {
-               DbgPrint((DPRINT_MEMORY, "MmAllocateMemory() called for 0 bytes. Returning NULL.\n"));
+               DPRINTM(DPRINT_MEMORY, "MmAllocateMemory() called for 0 bytes. Returning NULL.\n");
                UiMessageBoxCritical("Memory allocation failed: MmAllocateMemory() called for 0 bytes.");
                return NULL;
        }
@@ -50,7 +50,7 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
        // then return NULL
        if (FreePagesInLookupTable < PagesNeeded)
        {
-               DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize));
+               DPRINTM(DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize);
                UiMessageBoxCritical("Memory allocation failed: out of memory.");
                return NULL;
        }
@@ -59,7 +59,7 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
 
        if (FirstFreePageFromEnd == 0)
        {
-               DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize));
+               DPRINTM(DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize);
                UiMessageBoxCritical("Memory allocation failed: out of memory.");
                return NULL;
        }
@@ -70,8 +70,8 @@ PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
        MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
 
 #ifdef DBG
-       DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd));
-       DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
+       DPRINTM(DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd);
+       DPRINTM(DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer);
 #endif // DBG
 
        // Update LoaderPagesSpanned count
@@ -89,7 +89,7 @@ PVOID MmHeapAlloc(ULONG MemorySize)
 
        if (MemorySize > MM_PAGE_SIZE)
        {
-               DbgPrint((DPRINT_MEMORY, "Consider using other functions to allocate %d bytes of memory!\n", MemorySize));
+               DPRINTM(DPRINT_MEMORY, "Consider using other functions to allocate %d bytes of memory!\n", MemorySize);
        }
 
        // Get the buffer from BGET pool
@@ -97,14 +97,14 @@ PVOID MmHeapAlloc(ULONG MemorySize)
 
        if (Result == NULL)
        {
-               DbgPrint((DPRINT_MEMORY, "Heap allocation for %d bytes failed\n", MemorySize));
+               DPRINTM(DPRINT_MEMORY, "Heap allocation for %d bytes failed\n", MemorySize);
        }
 
        // Gather some stats
        bstats(&CurAlloc, &TotalFree, &MaxFree, &NumberOfGets, &NumberOfRels);
 
-       DbgPrint((DPRINT_MEMORY, "Current alloced %d bytes, free %d bytes, allocs %d, frees %d\n",
-               CurAlloc, TotalFree, NumberOfGets, NumberOfRels));
+       DPRINTM(DPRINT_MEMORY, "Current alloced %d bytes, free %d bytes, allocs %d, frees %d\n",
+               CurAlloc, TotalFree, NumberOfGets, NumberOfRels);
 
        return Result;
 }
@@ -129,7 +129,7 @@ PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_
 
        if (MemorySize == 0)
        {
-               DbgPrint((DPRINT_MEMORY, "MmAllocateMemoryAtAddress() called for 0 bytes. Returning NULL.\n"));
+               DPRINTM(DPRINT_MEMORY, "MmAllocateMemoryAtAddress() called for 0 bytes. Returning NULL.\n");
                UiMessageBoxCritical("Memory allocation failed: MmAllocateMemoryAtAddress() called for 0 bytes.");
                return NULL;
        }
@@ -145,18 +145,18 @@ PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_
        // then return NULL
        if (FreePagesInLookupTable < PagesNeeded)
        {
-               DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
+               DPRINTM(DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
                        "Not enough free memory to allocate %d bytes (requesting %d pages but have only %d). "
-                       "\n", MemorySize, PagesNeeded, FreePagesInLookupTable));
+                       "\n", MemorySize, PagesNeeded, FreePagesInLookupTable);
                UiMessageBoxCritical("Memory allocation failed: out of memory.");
                return NULL;
        }
 
        if (MmAreMemoryPagesAvailable(PageLookupTableAddress, TotalPagesInLookupTable, DesiredAddress, PagesNeeded) == FALSE)
        {
-               DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
+               DPRINTM(DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
                        "Not enough free memory to allocate %d bytes at address %p.\n",
-                       MemorySize, DesiredAddress));
+                       MemorySize, DesiredAddress);
 
                // Don't tell this to user since caller should try to alloc this memory
                // at a different address
@@ -170,8 +170,8 @@ PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_
        MemPointer = (PVOID)((ULONG_PTR)StartPageNumber * MM_PAGE_SIZE);
 
 #ifdef DBG
-       DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, StartPageNumber));
-       DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
+       DPRINTM(DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, StartPageNumber);
+       DPRINTM(DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer);
 #endif // DBG
 
        // Update LoaderPagesSpanned count
@@ -182,6 +182,22 @@ PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_
        return MemPointer;
 }
 
+VOID MmSetMemoryType(PVOID MemoryAddress, ULONG MemorySize, TYPE_OF_MEMORY NewType)
+{
+       ULONG           PagesNeeded;
+       ULONG           StartPageNumber;
+
+       // Find out how many blocks it will take to
+       // satisfy this allocation
+       PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
+
+       // Get the starting page number
+       StartPageNumber = MmGetPageNumberFromAddress(MemoryAddress);
+
+       // Set new type for these pages
+       MmAllocatePagesInLookupTable(PageLookupTableAddress, StartPageNumber, PagesNeeded, NewType);
+}
+
 PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
 {
        ULONG           PagesNeeded;
@@ -191,7 +207,7 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
 
        if (MemorySize == 0)
        {
-               DbgPrint((DPRINT_MEMORY, "MmAllocateHighestMemoryBelowAddress() called for 0 bytes. Returning NULL.\n"));
+               DPRINTM(DPRINT_MEMORY, "MmAllocateHighestMemoryBelowAddress() called for 0 bytes. Returning NULL.\n");
                UiMessageBoxCritical("Memory allocation failed: MmAllocateHighestMemoryBelowAddress() called for 0 bytes.");
                return NULL;
        }
@@ -207,7 +223,7 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
        // then return NULL
        if (FreePagesInLookupTable < PagesNeeded)
        {
-               DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize));
+               DPRINTM(DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize);
                UiMessageBoxCritical("Memory allocation failed: out of memory.");
                return NULL;
        }
@@ -216,7 +232,7 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
 
        if (FirstFreePageFromEnd == 0)
        {
-               DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize));
+               DPRINTM(DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize);
                UiMessageBoxCritical("Memory allocation failed: out of memory.");
                return NULL;
        }
@@ -227,8 +243,8 @@ PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress
        MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
 
 #ifdef DBG
-       DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd));
-       DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
+       DPRINTM(DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd);
+       DPRINTM(DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer);
 #endif // DBG
 
        // Update LoaderPagesSpanned count
@@ -250,74 +266,74 @@ VOID DumpMemoryAllocMap(VOID)
        ULONG                                                   Idx;
        PPAGE_LOOKUP_TABLE_ITEM         RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTableAddress;
 
-       DbgPrint((DPRINT_MEMORY, "----------- Memory Allocation Bitmap -----------\n"));
+       DPRINTM(DPRINT_MEMORY, "----------- Memory Allocation Bitmap -----------\n");
 
        for (Idx=0; Idx<TotalPagesInLookupTable; Idx++)
        {
                if ((Idx % 32) == 0)
                {
-                       DbgPrint((DPRINT_MEMORY, "\n"));
-                       DbgPrint((DPRINT_MEMORY, "%08x:\t", (Idx * MM_PAGE_SIZE)));
+                       DPRINTM(DPRINT_MEMORY, "\n");
+                       DPRINTM(DPRINT_MEMORY, "%08x:\t", (Idx * MM_PAGE_SIZE));
                }
                else if ((Idx % 4) == 0)
                {
-                       DbgPrint((DPRINT_MEMORY, " "));
+                       DPRINTM(DPRINT_MEMORY, " ");
                }
 
                switch (RealPageLookupTable[Idx].PageAllocated)
                {
                case LoaderFree:
-                       DbgPrint((DPRINT_MEMORY, "*"));
+                       DPRINTM(DPRINT_MEMORY, "*");
                        break;
                case LoaderBad:
-                       DbgPrint((DPRINT_MEMORY, "-"));
+                       DPRINTM(DPRINT_MEMORY, "-");
                        break;
                case LoaderLoadedProgram:
-                       DbgPrint((DPRINT_MEMORY, "O"));
+                       DPRINTM(DPRINT_MEMORY, "O");
                        break;
                case LoaderFirmwareTemporary:
-                       DbgPrint((DPRINT_MEMORY, "T"));
+                       DPRINTM(DPRINT_MEMORY, "T");
                        break;
                case LoaderFirmwarePermanent:
-                       DbgPrint((DPRINT_MEMORY, "P"));
+                       DPRINTM(DPRINT_MEMORY, "P");
                        break;
                case LoaderOsloaderHeap:
-                       DbgPrint((DPRINT_MEMORY, "H"));
+                       DPRINTM(DPRINT_MEMORY, "H");
                        break;
                case LoaderOsloaderStack:
-                       DbgPrint((DPRINT_MEMORY, "S"));
+                       DPRINTM(DPRINT_MEMORY, "S");
                        break;
                case LoaderSystemCode:
-                       DbgPrint((DPRINT_MEMORY, "K"));
+                       DPRINTM(DPRINT_MEMORY, "K");
                        break;
                case LoaderHalCode:
-                       DbgPrint((DPRINT_MEMORY, "L"));
+                       DPRINTM(DPRINT_MEMORY, "L");
                        break;
                case LoaderBootDriver:
-                       DbgPrint((DPRINT_MEMORY, "B"));
+                       DPRINTM(DPRINT_MEMORY, "B");
                        break;
                case LoaderStartupPcrPage:
-                       DbgPrint((DPRINT_MEMORY, "G"));
+                       DPRINTM(DPRINT_MEMORY, "G");
                        break;
                case LoaderRegistryData:
-                       DbgPrint((DPRINT_MEMORY, "R"));
+                       DPRINTM(DPRINT_MEMORY, "R");
                        break;
                case LoaderMemoryData:
-                       DbgPrint((DPRINT_MEMORY, "M"));
+                       DPRINTM(DPRINT_MEMORY, "M");
                        break;
                case LoaderNlsData:
-                       DbgPrint((DPRINT_MEMORY, "N"));
+                       DPRINTM(DPRINT_MEMORY, "N");
                        break;
                case LoaderSpecialMemory:
-                       DbgPrint((DPRINT_MEMORY, "C"));
+                       DPRINTM(DPRINT_MEMORY, "C");
                        break;
                default:
-                       DbgPrint((DPRINT_MEMORY, "?"));
+                       DPRINTM(DPRINT_MEMORY, "?");
                        break;
                }
        }
 
-       DbgPrint((DPRINT_MEMORY, "\n"));
+       DPRINTM(DPRINT_MEMORY, "\n");
 }
 #endif // DBG