sync with trunk head (34904)
[reactos.git] / reactos / boot / freeldr / freeldr / mm / mm.c
1 /*
2 * FreeLoader
3 * Copyright (C) 2006-2008 Aleksey Bragin <aleksey@reactos.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include <debug.h>
22
23 #ifdef DBG
24 VOID DumpMemoryAllocMap(VOID);
25 VOID MemAllocTest(VOID);
26 #endif // DBG
27
28 ULONG LoaderPagesSpanned = 0;
29
30 PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType)
31 {
32 ULONG PagesNeeded;
33 ULONG FirstFreePageFromEnd;
34 PVOID MemPointer;
35
36 if (MemorySize == 0)
37 {
38 DbgPrint((DPRINT_MEMORY, "MmAllocateMemory() called for 0 bytes. Returning NULL.\n"));
39 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemory() called for 0 bytes.");
40 return NULL;
41 }
42
43 MemorySize = ROUND_UP(MemorySize, 4);
44
45 // Find out how many blocks it will take to
46 // satisfy this allocation
47 PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
48
49 // If we don't have enough available mem
50 // then return NULL
51 if (FreePagesInLookupTable < PagesNeeded)
52 {
53 DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize));
54 UiMessageBoxCritical("Memory allocation failed: out of memory.");
55 return NULL;
56 }
57
58 FirstFreePageFromEnd = MmFindAvailablePages(PageLookupTableAddress, TotalPagesInLookupTable, PagesNeeded, FALSE);
59
60 if (FirstFreePageFromEnd == 0)
61 {
62 DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize));
63 UiMessageBoxCritical("Memory allocation failed: out of memory.");
64 return NULL;
65 }
66
67 MmAllocatePagesInLookupTable(PageLookupTableAddress, FirstFreePageFromEnd, PagesNeeded, MemoryType);
68
69 FreePagesInLookupTable -= PagesNeeded;
70 MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
71
72 #ifdef DBG
73 DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd));
74 DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
75 #endif // DBG
76
77 // Update LoaderPagesSpanned count
78 if ((((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT) > LoaderPagesSpanned)
79 LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT);
80
81 // Now return the pointer
82 return MemPointer;
83 }
84
85 PVOID MmHeapAlloc(ULONG MemorySize)
86 {
87 PVOID Result;
88 LONG CurAlloc, TotalFree, MaxFree, NumberOfGets, NumberOfRels;
89
90 if (MemorySize > MM_PAGE_SIZE)
91 {
92 DbgPrint((DPRINT_MEMORY, "Consider using other functions to allocate %d bytes of memory!\n", MemorySize));
93 }
94
95 // Get the buffer from BGET pool
96 Result = bget(MemorySize);
97
98 if (Result == NULL)
99 {
100 DbgPrint((DPRINT_MEMORY, "Heap allocation for %d bytes failed\n", MemorySize));
101 }
102
103 // Gather some stats
104 bstats(&CurAlloc, &TotalFree, &MaxFree, &NumberOfGets, &NumberOfRels);
105
106 DbgPrint((DPRINT_MEMORY, "Current alloced %d bytes, free %d bytes, allocs %d, frees %d\n",
107 CurAlloc, TotalFree, NumberOfGets, NumberOfRels));
108
109 return Result;
110 }
111
112 VOID MmHeapFree(PVOID MemoryPointer)
113 {
114 // Release the buffer to the pool
115 brel(MemoryPointer);
116 }
117
118 PVOID MmAllocateMemory(ULONG MemorySize)
119 {
120 // Temporary forwarder...
121 return MmAllocateMemoryWithType(MemorySize, LoaderOsloaderHeap);
122 }
123
124 PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
125 {
126 ULONG PagesNeeded;
127 ULONG StartPageNumber;
128 PVOID MemPointer;
129
130 if (MemorySize == 0)
131 {
132 DbgPrint((DPRINT_MEMORY, "MmAllocateMemoryAtAddress() called for 0 bytes. Returning NULL.\n"));
133 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemoryAtAddress() called for 0 bytes.");
134 return NULL;
135 }
136
137 // Find out how many blocks it will take to
138 // satisfy this allocation
139 PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
140
141 // Get the starting page number
142 StartPageNumber = MmGetPageNumberFromAddress(DesiredAddress);
143
144 // If we don't have enough available mem
145 // then return NULL
146 if (FreePagesInLookupTable < PagesNeeded)
147 {
148 DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
149 "Not enough free memory to allocate %d bytes (requesting %d pages but have only %d). "
150 "\n", MemorySize, PagesNeeded, FreePagesInLookupTable));
151 UiMessageBoxCritical("Memory allocation failed: out of memory.");
152 return NULL;
153 }
154
155 if (MmAreMemoryPagesAvailable(PageLookupTableAddress, TotalPagesInLookupTable, DesiredAddress, PagesNeeded) == FALSE)
156 {
157 DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
158 "Not enough free memory to allocate %d bytes at address %p.\n",
159 MemorySize, DesiredAddress));
160
161 // Don't tell this to user since caller should try to alloc this memory
162 // at a different address
163 //UiMessageBoxCritical("Memory allocation failed: out of memory.");
164 return NULL;
165 }
166
167 MmAllocatePagesInLookupTable(PageLookupTableAddress, StartPageNumber, PagesNeeded, MemoryType);
168
169 FreePagesInLookupTable -= PagesNeeded;
170 MemPointer = (PVOID)((ULONG_PTR)StartPageNumber * MM_PAGE_SIZE);
171
172 #ifdef DBG
173 DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, StartPageNumber));
174 DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
175 #endif // DBG
176
177 // Update LoaderPagesSpanned count
178 if ((((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT) > LoaderPagesSpanned)
179 LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT);
180
181 // Now return the pointer
182 return MemPointer;
183 }
184
185 PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
186 {
187 ULONG PagesNeeded;
188 ULONG FirstFreePageFromEnd;
189 ULONG DesiredAddressPageNumber;
190 PVOID MemPointer;
191
192 if (MemorySize == 0)
193 {
194 DbgPrint((DPRINT_MEMORY, "MmAllocateHighestMemoryBelowAddress() called for 0 bytes. Returning NULL.\n"));
195 UiMessageBoxCritical("Memory allocation failed: MmAllocateHighestMemoryBelowAddress() called for 0 bytes.");
196 return NULL;
197 }
198
199 // Find out how many blocks it will take to
200 // satisfy this allocation
201 PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
202
203 // Get the page number for their desired address
204 DesiredAddressPageNumber = (ULONG_PTR)DesiredAddress / MM_PAGE_SIZE;
205
206 // If we don't have enough available mem
207 // then return NULL
208 if (FreePagesInLookupTable < PagesNeeded)
209 {
210 DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize));
211 UiMessageBoxCritical("Memory allocation failed: out of memory.");
212 return NULL;
213 }
214
215 FirstFreePageFromEnd = MmFindAvailablePagesBeforePage(PageLookupTableAddress, TotalPagesInLookupTable, PagesNeeded, DesiredAddressPageNumber);
216
217 if (FirstFreePageFromEnd == 0)
218 {
219 DbgPrint((DPRINT_MEMORY, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize));
220 UiMessageBoxCritical("Memory allocation failed: out of memory.");
221 return NULL;
222 }
223
224 MmAllocatePagesInLookupTable(PageLookupTableAddress, FirstFreePageFromEnd, PagesNeeded, MemoryType);
225
226 FreePagesInLookupTable -= PagesNeeded;
227 MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
228
229 #ifdef DBG
230 DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd));
231 DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
232 #endif // DBG
233
234 // Update LoaderPagesSpanned count
235 if ((((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT) > LoaderPagesSpanned)
236 LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT);
237
238 // Now return the pointer
239 return MemPointer;
240 }
241
242 VOID MmFreeMemory(PVOID MemoryPointer)
243 {
244 }
245
246 #ifdef DBG
247
248 VOID DumpMemoryAllocMap(VOID)
249 {
250 ULONG Idx;
251 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTableAddress;
252
253 DbgPrint((DPRINT_MEMORY, "----------- Memory Allocation Bitmap -----------\n"));
254
255 for (Idx=0; Idx<TotalPagesInLookupTable; Idx++)
256 {
257 if ((Idx % 32) == 0)
258 {
259 DbgPrint((DPRINT_MEMORY, "\n"));
260 DbgPrint((DPRINT_MEMORY, "%08x:\t", (Idx * MM_PAGE_SIZE)));
261 }
262 else if ((Idx % 4) == 0)
263 {
264 DbgPrint((DPRINT_MEMORY, " "));
265 }
266
267 switch (RealPageLookupTable[Idx].PageAllocated)
268 {
269 case LoaderFree:
270 DbgPrint((DPRINT_MEMORY, "*"));
271 break;
272 case LoaderBad:
273 DbgPrint((DPRINT_MEMORY, "-"));
274 break;
275 case LoaderLoadedProgram:
276 DbgPrint((DPRINT_MEMORY, "O"));
277 break;
278 case LoaderFirmwareTemporary:
279 DbgPrint((DPRINT_MEMORY, "T"));
280 break;
281 case LoaderFirmwarePermanent:
282 DbgPrint((DPRINT_MEMORY, "P"));
283 break;
284 case LoaderOsloaderHeap:
285 DbgPrint((DPRINT_MEMORY, "H"));
286 break;
287 case LoaderOsloaderStack:
288 DbgPrint((DPRINT_MEMORY, "S"));
289 break;
290 case LoaderSystemCode:
291 DbgPrint((DPRINT_MEMORY, "K"));
292 break;
293 case LoaderHalCode:
294 DbgPrint((DPRINT_MEMORY, "L"));
295 break;
296 case LoaderBootDriver:
297 DbgPrint((DPRINT_MEMORY, "B"));
298 break;
299 case LoaderStartupPcrPage:
300 DbgPrint((DPRINT_MEMORY, "G"));
301 break;
302 case LoaderRegistryData:
303 DbgPrint((DPRINT_MEMORY, "R"));
304 break;
305 case LoaderMemoryData:
306 DbgPrint((DPRINT_MEMORY, "M"));
307 break;
308 case LoaderNlsData:
309 DbgPrint((DPRINT_MEMORY, "N"));
310 break;
311 case LoaderSpecialMemory:
312 DbgPrint((DPRINT_MEMORY, "C"));
313 break;
314 default:
315 DbgPrint((DPRINT_MEMORY, "?"));
316 break;
317 }
318 }
319
320 DbgPrint((DPRINT_MEMORY, "\n"));
321 }
322 #endif // DBG
323
324 ULONG GetSystemMemorySize(VOID)
325 {
326 return (TotalPagesInLookupTable * MM_PAGE_SIZE);
327 }
328
329 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(ULONG *NoEntries)
330 {
331 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTableAddress;
332
333 *NoEntries = TotalPagesInLookupTable;
334
335 return RealPageLookupTable;
336 }