ReactOS 0.4.15-dev-7842-g558ab78
mm.c File Reference
#include <freeldr.h>
#include <debug.h>
Include dependency graph for mm.c:

Go to the source code of this file.

Functions

 DBG_DEFAULT_CHANNEL (MEMORY)
 
PVOID MmAllocateMemoryWithType (SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
 
PVOID MmAllocateMemoryAtAddress (SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
 
VOID MmSetMemoryType (PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType)
 
PVOID MmAllocateHighestMemoryBelowAddress (SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
 
VOID MmFreeMemory (PVOID MemoryPointer)
 
PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap (PFN_NUMBER *NoEntries)
 

Variables

PFN_NUMBER LoaderPagesSpanned = 0
 

Function Documentation

◆ DBG_DEFAULT_CHANNEL()

DBG_DEFAULT_CHANNEL ( MEMORY  )

◆ MmAllocateHighestMemoryBelowAddress()

PVOID MmAllocateHighestMemoryBelowAddress ( SIZE_T  MemorySize,
PVOID  DesiredAddress,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 160 of file mm.c.

161{
162 PFN_NUMBER PagesNeeded;
163 PFN_NUMBER FirstFreePageFromEnd;
164 PFN_NUMBER DesiredAddressPageNumber;
165 PVOID MemPointer;
166
167 if (MemorySize == 0)
168 {
169 WARN("MmAllocateHighestMemoryBelowAddress() called for 0 bytes. Returning NULL.\n");
170 UiMessageBoxCritical("Memory allocation failed: MmAllocateHighestMemoryBelowAddress() called for 0 bytes.");
171 return NULL;
172 }
173
174 // Find out how many blocks it will take to
175 // satisfy this allocation
176 PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
177
178 // Get the page number for their desired address
179 DesiredAddressPageNumber = (ULONG_PTR)DesiredAddress / MM_PAGE_SIZE;
180
181 // If we don't have enough available mem
182 // then return NULL
183 if (FreePagesInLookupTable < PagesNeeded)
184 {
185 ERR("Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize);
186 UiMessageBoxCritical("Memory allocation failed: out of memory.");
187 return NULL;
188 }
189
190 FirstFreePageFromEnd = MmFindAvailablePagesBeforePage(PageLookupTableAddress, TotalPagesInLookupTable, PagesNeeded, DesiredAddressPageNumber);
191
192 if (FirstFreePageFromEnd == 0)
193 {
194 ERR("Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize);
195 UiMessageBoxCritical("Memory allocation failed: out of memory.");
196 return NULL;
197 }
198
199 MmAllocatePagesInLookupTable(PageLookupTableAddress, FirstFreePageFromEnd, PagesNeeded, MemoryType);
200
201 FreePagesInLookupTable -= PagesNeeded;
202 MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
203
204 TRACE("Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd);
205 TRACE("Memory allocation pointer: 0x%x\n", MemPointer);
206
207 // Update LoaderPagesSpanned count
208 if ((((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT) > LoaderPagesSpanned)
209 LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT);
210
211 // Now return the pointer
212 return MemPointer;
213}
#define WARN(fmt,...)
Definition: debug.h:112
#define ERR(fmt,...)
Definition: debug.h:110
PVOID PageLookupTableAddress
Definition: meminit.c:26
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
PFN_NUMBER MmFindAvailablePagesBeforePage(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage)
Definition: meminit.c:626
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
Definition: meminit.c:536
VOID UiMessageBoxCritical(_In_ PCSTR MessageText)
Definition: ui.c:372
#define NULL
Definition: types.h:112
#define ULONG_PTR
Definition: config.h:101
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define ROUND_UP(n, align)
Definition: eventvwr.h:34
PFN_NUMBER LoaderPagesSpanned
Definition: mm.c:29
ULONG PFN_NUMBER
Definition: ke.h:9
#define TRACE(s)
Definition: solgame.cpp:4
static CONST DWORD MemorySize[]
Definition: svga.c:32
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65

◆ MmAllocateMemoryAtAddress()

PVOID MmAllocateMemoryAtAddress ( SIZE_T  MemorySize,
PVOID  DesiredAddress,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 85 of file mm.c.

86{
87 PFN_NUMBER PagesNeeded;
88 PFN_NUMBER StartPageNumber;
89 PVOID MemPointer;
90
91 if (MemorySize == 0)
92 {
93 WARN("MmAllocateMemoryAtAddress() called for 0 bytes. Returning NULL.\n");
94 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemoryAtAddress() called for 0 bytes.");
95 return NULL;
96 }
97
98 // Find out how many blocks it will take to
99 // satisfy this allocation
100 PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
101
102 // Get the starting page number
103 StartPageNumber = MmGetPageNumberFromAddress(DesiredAddress);
104
105 // If we don't have enough available mem
106 // then return NULL
107 if (FreePagesInLookupTable < PagesNeeded)
108 {
109 ERR("Memory allocation failed in MmAllocateMemoryAtAddress(). "
110 "Not enough free memory to allocate %d bytes (requesting %d pages but have only %d). "
111 "\n", MemorySize, PagesNeeded, FreePagesInLookupTable);
112 UiMessageBoxCritical("Memory allocation failed: out of memory.");
113 return NULL;
114 }
115
117 {
118 WARN("Memory allocation failed in MmAllocateMemoryAtAddress(). "
119 "Not enough free memory to allocate %d bytes at address %p.\n",
120 MemorySize, DesiredAddress);
121
122 // Don't tell this to user since caller should try to alloc this memory
123 // at a different address
124 //UiMessageBoxCritical("Memory allocation failed: out of memory.");
125 return NULL;
126 }
127
128 MmAllocatePagesInLookupTable(PageLookupTableAddress, StartPageNumber, PagesNeeded, MemoryType);
129
130 FreePagesInLookupTable -= PagesNeeded;
131 MemPointer = (PVOID)((ULONG_PTR)StartPageNumber * MM_PAGE_SIZE);
132
133 TRACE("Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, StartPageNumber);
134 TRACE("Memory allocation pointer: 0x%x\n", MemPointer);
135
136 // Update LoaderPagesSpanned count
137 if ((((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT) > LoaderPagesSpanned)
138 LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT);
139
140 // Now return the pointer
141 return MemPointer;
142}
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:371
BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount)
Definition: meminit.c:674
#define FALSE
Definition: types.h:117
#define PAGE_SIZE
Definition: env_spec_w32.h:49

Referenced by MempAllocatePageTables(), and PeLdrLoadImage().

◆ MmAllocateMemoryWithType()

PVOID MmAllocateMemoryWithType ( SIZE_T  MemorySize,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 31 of file mm.c.

32{
33 PFN_NUMBER PagesNeeded;
34 PFN_NUMBER FirstFreePageFromEnd;
35 PVOID MemPointer;
36
37 if (MemorySize == 0)
38 {
39 WARN("MmAllocateMemory() called for 0 bytes. Returning NULL.\n");
40 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemory() called for 0 bytes.");
41 return NULL;
42 }
43
45
46 // Find out how many blocks it will take to
47 // satisfy this allocation
48 PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
49
50 // If we don't have enough available mem
51 // then return NULL
52 if (FreePagesInLookupTable < PagesNeeded)
53 {
54 ERR("Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize);
55 UiMessageBoxCritical("Memory allocation failed: out of memory.");
56 return NULL;
57 }
58
59 FirstFreePageFromEnd = MmFindAvailablePages(PageLookupTableAddress, TotalPagesInLookupTable, PagesNeeded, FALSE);
60
61 if (FirstFreePageFromEnd == 0)
62 {
63 ERR("Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize);
64 UiMessageBoxCritical("Memory allocation failed: out of memory.");
65 return NULL;
66 }
67
68 MmAllocatePagesInLookupTable(PageLookupTableAddress, FirstFreePageFromEnd, PagesNeeded, MemoryType);
69
70 FreePagesInLookupTable -= PagesNeeded;
71 MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
72
73 TRACE("Allocated %d bytes (%d pages) of memory (type %ld) starting at page 0x%lx.\n",
74 MemorySize, PagesNeeded, MemoryType, FirstFreePageFromEnd);
75 TRACE("Memory allocation pointer: 0x%x\n", MemPointer);
76
77 // Update LoaderPagesSpanned count
78 if ((((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT) > LoaderPagesSpanned)
79 LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT);
80
81 // Now return the pointer
82 return MemPointer;
83}
PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
Definition: meminit.c:567

Referenced by AllocateAndInitLPB(), EfiEntry(), FrLdrHeapCreate(), MempAllocatePageTables(), MempGetOrCreatePageDir(), PeLdrLoadImage(), RamDiskLoadVirtualFile(), RegLoadHiveLog(), UefiInitializeBootDevices(), UefiSetupBlockDevices(), VideoAllocateOffScreenBuffer(), WinLdrLoadModule(), WinLdrLoadNLSData(), WinLdrLoadSystemHive(), WinLdrSetupMachineDependent(), and WinLdrSetupMemoryLayout().

◆ MmFreeMemory()

◆ MmGetMemoryMap()

PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap ( PFN_NUMBER NoEntries)

Definition at line 297 of file mm.c.

298{
300
301 *NoEntries = TotalPagesInLookupTable;
302
303 return RealPageLookupTable;
304}
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by WinLdrSetupMemoryLayout().

◆ MmSetMemoryType()

VOID MmSetMemoryType ( PVOID  MemoryAddress,
SIZE_T  MemorySize,
TYPE_OF_MEMORY  NewType 
)

Definition at line 144 of file mm.c.

145{
146 PFN_NUMBER PagesNeeded;
147 PFN_NUMBER StartPageNumber;
148
149 // Find out how many blocks it will take to
150 // satisfy this allocation
151 PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
152
153 // Get the starting page number
154 StartPageNumber = MmGetPageNumberFromAddress(MemoryAddress);
155
156 // Set new type for these pages
157 MmAllocatePagesInLookupTable(PageLookupTableAddress, StartPageNumber, PagesNeeded, NewType);
158}

Referenced by MempAllocatePageTables().

Variable Documentation

◆ LoaderPagesSpanned