-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
meminit.c
704 lines (611 loc) · 23.3 KB
/
meminit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
* FreeLoader
* Copyright (C) 2006-2008 Aleksey Bragin <aleksey@reactos.org>
* Copyright (C) 2006-2009 Hervé Poussineau <hpoussin@reactos.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <freeldr.h>
#include <debug.h>
DBG_DEFAULT_CHANNEL(MEMORY);
PVOID PageLookupTableAddress = NULL;
PFN_NUMBER TotalPagesInLookupTable = 0;
PFN_NUMBER FreePagesInLookupTable = 0;
PFN_NUMBER LastFreePageHint = 0;
PFN_NUMBER MmLowestPhysicalPage = 0xFFFFFFFF;
PFN_NUMBER MmHighestPhysicalPage = 0;
PFREELDR_MEMORY_DESCRIPTOR BiosMemoryMap;
ULONG BiosMemoryMapEntryCount;
SIZE_T FrLdrImageSize;
#if DBG
typedef struct
{
TYPE_OF_MEMORY Type;
PCSTR TypeString;
} FREELDR_MEMORY_TYPE, *PFREELDR_MEMORY_TYPE;
FREELDR_MEMORY_TYPE MemoryTypeArray[] =
{
{ LoaderMaximum, "Unknown memory" },
{ LoaderFree, "Free memory" },
{ LoaderBad, "Bad memory" },
{ LoaderLoadedProgram, "LoadedProgram" },
{ LoaderFirmwareTemporary, "FirmwareTemporary" },
{ LoaderFirmwarePermanent, "FirmwarePermanent" },
{ LoaderOsloaderHeap, "OsloaderHeap" },
{ LoaderOsloaderStack, "OsloaderStack" },
{ LoaderSystemCode, "SystemCode" },
{ LoaderHalCode, "HalCode" },
{ LoaderBootDriver, "BootDriver" },
{ LoaderRegistryData, "RegistryData" },
{ LoaderMemoryData, "MemoryData" },
{ LoaderNlsData, "NlsData" },
{ LoaderSpecialMemory, "SpecialMemory" },
{ LoaderReserve, "Reserve" },
};
ULONG MemoryTypeCount = sizeof(MemoryTypeArray) / sizeof(MemoryTypeArray[0]);
PCSTR
MmGetSystemMemoryMapTypeString(
TYPE_OF_MEMORY Type)
{
ULONG Index;
for (Index = 1; Index < MemoryTypeCount; Index++)
{
if (MemoryTypeArray[Index].Type == Type)
{
return MemoryTypeArray[Index].TypeString;
}
}
return MemoryTypeArray[0].TypeString;
}
VOID
DbgDumpMemoryMap(
PFREELDR_MEMORY_DESCRIPTOR List)
{
ULONG i;
DbgPrint("Dumping Memory map:\n");
for (i = 0; List[i].PageCount != 0; i++)
{
DbgPrint("%02d %08x - %08x: %s\n",
i,
List[i].BasePage * PAGE_SIZE,
(List[i].BasePage + List[i].PageCount) * PAGE_SIZE,
MmGetSystemMemoryMapTypeString(List[i].MemoryType));
}
DbgPrint("\n");
}
#endif
ULONG
AddMemoryDescriptor(
IN OUT PFREELDR_MEMORY_DESCRIPTOR List,
IN ULONG MaxCount,
IN PFN_NUMBER BasePage,
IN PFN_NUMBER PageCount,
IN TYPE_OF_MEMORY MemoryType)
{
ULONG Index, DescriptCount;
PFN_NUMBER EndPage;
TRACE("AddMemoryDescriptor(0x%Ix, 0x%Ix, %u)\n",
BasePage, PageCount, MemoryType);
EndPage = BasePage + PageCount;
/* Skip over all descriptor below the new range */
Index = 0;
while ((List[Index].PageCount != 0) &&
((List[Index].BasePage + List[Index].PageCount) <= BasePage))
{
Index++;
}
/* Count the descriptors */
DescriptCount = Index;
while (List[DescriptCount].PageCount != 0)
{
DescriptCount++;
}
/* Check if the existing range conflicts with the new range */
while ((List[Index].PageCount != 0) &&
(List[Index].BasePage < EndPage))
{
TRACE("AddMemoryDescriptor conflict @%lu: new=[%lx:%lx], existing=[%lx,%lx]\n",
Index, BasePage, PageCount, List[Index].BasePage, List[Index].PageCount);
/*
* We have 4 overlapping cases:
*
* Case (a) (b) (c) (d)
* Existing range |---| |-----| |---| |---|
* New range |---| |---| |-----| |---|
*
*/
/* Check if the existing range starts before the new range (a)/(b) */
if (List[Index].BasePage < BasePage)
{
/* Check if the existing range extends beyond the new range (b) */
if (List[Index].BasePage + List[Index].PageCount > EndPage)
{
/* Split the descriptor */
RtlMoveMemory(&List[Index + 1],
&List[Index],
(DescriptCount - Index) * sizeof(List[0]));
List[Index + 1].BasePage = EndPage;
List[Index + 1].PageCount = List[Index].BasePage +
List[Index].PageCount -
List[Index + 1].BasePage;
List[Index].PageCount = BasePage - List[Index].BasePage;
Index++;
DescriptCount++;
break;
}
else
{
/* Crop the existing range and continue with the next range */
List[Index].PageCount = BasePage - List[Index].BasePage;
Index++;
}
}
/* Check if the existing range is fully covered by the new range (c) */
else if ((List[Index].BasePage + List[Index].PageCount) <=
EndPage)
{
/* Delete this descriptor */
RtlMoveMemory(&List[Index],
&List[Index + 1],
(DescriptCount - Index) * sizeof(List[0]));
DescriptCount--;
}
/* Otherwise the existing range ends after the new range (d) */
else
{
/* Crop the existing range at the start and bail out */
List[Index].PageCount -= EndPage - List[Index].BasePage;
List[Index].BasePage = EndPage;
break;
}
}
/* Make sure we can still add a new descriptor */
if (DescriptCount >= MaxCount)
{
FrLdrBugCheckWithMessage(
MEMORY_INIT_FAILURE,
__FILE__,
__LINE__,
"Ran out of static memory descriptors!");
}
/* Insert the new descriptor */
if (Index < DescriptCount)
{
RtlMoveMemory(&List[Index + 1],
&List[Index],
(DescriptCount - Index) * sizeof(List[0]));
}
List[Index].BasePage = BasePage;
List[Index].PageCount = PageCount;
List[Index].MemoryType = MemoryType;
DescriptCount++;
#if 0 // only enable on demand!
DbgDumpMemoryMap(List);
#endif
return DescriptCount;
}
const FREELDR_MEMORY_DESCRIPTOR*
ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR* Current)
{
if (Current == NULL)
{
return BiosMemoryMap;
}
else
{
Current++;
if (Current->PageCount == 0) return NULL;
return Current;
}
}
static
VOID
MmCheckFreeldrImageFile(VOID)
{
#ifndef UEFIBOOT
PIMAGE_NT_HEADERS NtHeaders;
PIMAGE_FILE_HEADER FileHeader;
PIMAGE_OPTIONAL_HEADER OptionalHeader;
/* Get the NT headers */
NtHeaders = RtlImageNtHeader(&__ImageBase);
if (!NtHeaders)
{
ERR("Could not get NtHeaders!\n");
FrLdrBugCheckWithMessage(
FREELDR_IMAGE_CORRUPTION,
__FILE__,
__LINE__,
"Could not get NtHeaders!\n");
}
/* Check the file header */
FileHeader = &NtHeaders->FileHeader;
if ((FileHeader->Machine != IMAGE_FILE_MACHINE_NATIVE) ||
(FileHeader->NumberOfSections != FREELDR_SECTION_COUNT) ||
(FileHeader->PointerToSymbolTable != 0) || // Symbols stripped
(FileHeader->NumberOfSymbols != 0) || // "" ""
(FileHeader->SizeOfOptionalHeader != sizeof(IMAGE_OPTIONAL_HEADER)))
{
ERR("FreeLdr FileHeader is invalid.\n");
FrLdrBugCheckWithMessage(
FREELDR_IMAGE_CORRUPTION,
__FILE__,
__LINE__,
"FreeLdr FileHeader is invalid.\n"
"Machine == 0x%lx, expected 0x%lx\n"
"NumberOfSections == 0x%lx, expected 0x%lx\n"
"PointerToSymbolTable == 0x%lx, expected 0\n"
"NumberOfSymbols == 0x%lx, expected 0\n"
"SizeOfOptionalHeader == 0x%lx, expected 0x%lx\n",
FileHeader->Machine, IMAGE_FILE_MACHINE_NATIVE,
FileHeader->NumberOfSections, FREELDR_SECTION_COUNT,
FileHeader->PointerToSymbolTable,
FileHeader->NumberOfSymbols,
FileHeader->SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER));
}
/* Check the optional header */
OptionalHeader = &NtHeaders->OptionalHeader;
if ((OptionalHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC) ||
(OptionalHeader->Subsystem != IMAGE_SUBSYSTEM_NATIVE) ||
(OptionalHeader->ImageBase != FREELDR_PE_BASE) ||
(OptionalHeader->SizeOfImage > MAX_FREELDR_PE_SIZE) ||
(OptionalHeader->SectionAlignment != OptionalHeader->FileAlignment))
{
ERR("FreeLdr OptionalHeader is invalid.\n");
FrLdrBugCheckWithMessage(
FREELDR_IMAGE_CORRUPTION,
__FILE__,
__LINE__,
"FreeLdr OptionalHeader is invalid.\n"
"Magic == 0x%lx, expected 0x%lx\n"
"Subsystem == 0x%lx, expected 1 (native)\n"
"ImageBase == 0x%lx, expected 0x%lx\n"
"SizeOfImage == 0x%lx, maximum 0x%lx\n"
"SectionAlignment 0x%lx doesn't match FileAlignment 0x%lx\n",
OptionalHeader->Magic, IMAGE_NT_OPTIONAL_HDR_MAGIC,
OptionalHeader->Subsystem,
OptionalHeader->ImageBase, FREELDR_PE_BASE,
OptionalHeader->SizeOfImage, MAX_FREELDR_PE_SIZE,
OptionalHeader->SectionAlignment, OptionalHeader->FileAlignment);
}
/* Calculate the full image size */
FrLdrImageSize = (ULONG_PTR)&__ImageBase + OptionalHeader->SizeOfImage - FREELDR_BASE;
#endif
}
BOOLEAN MmInitializeMemoryManager(VOID)
{
#if DBG
const FREELDR_MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
#endif
TRACE("Initializing Memory Manager.\n");
/* Check the freeldr binary */
MmCheckFreeldrImageFile();
BiosMemoryMap = MachVtbl.GetMemoryMap(&BiosMemoryMapEntryCount);
#if DBG
// Dump the system memory map
TRACE("System Memory Map (Base Address, Length, Type):\n");
while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
{
TRACE("%x\t %x\t %s\n",
MemoryDescriptor->BasePage * MM_PAGE_SIZE,
MemoryDescriptor->PageCount * MM_PAGE_SIZE,
MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
}
#endif
// Find address for the page lookup table
TotalPagesInLookupTable = MmGetAddressablePageCountIncludingHoles();
PageLookupTableAddress = MmFindLocationForPageLookupTable(TotalPagesInLookupTable);
LastFreePageHint = MmHighestPhysicalPage;
if (PageLookupTableAddress == 0)
{
// If we get here then we probably couldn't
// find a contiguous chunk of memory big
// enough to hold the page lookup table
printf("Error initializing memory manager!\n");
return FALSE;
}
// Initialize the page lookup table
MmInitPageLookupTable(PageLookupTableAddress, TotalPagesInLookupTable);
MmUpdateLastFreePageHint(PageLookupTableAddress, TotalPagesInLookupTable);
FreePagesInLookupTable = MmCountFreePagesInLookupTable(PageLookupTableAddress,
TotalPagesInLookupTable);
MmInitializeHeap(PageLookupTableAddress);
TRACE("Memory Manager initialized. 0x%x pages available.\n", FreePagesInLookupTable);
return TRUE;
}
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
{
return ((ULONG_PTR)Address) / MM_PAGE_SIZE;
}
PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID)
{
const FREELDR_MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
PFN_NUMBER PageCount;
//
// Go through the whole memory map to get max address
//
while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
{
//
// Check if we got a higher end page address
//
if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > MmHighestPhysicalPage)
{
//
// Yes, remember it if this is real memory
//
if (MemoryDescriptor->MemoryType == LoaderFree)
MmHighestPhysicalPage = MemoryDescriptor->BasePage + MemoryDescriptor->PageCount;
}
//
// Check if we got a higher (usable) start page address
//
if (MemoryDescriptor->BasePage < MmLowestPhysicalPage)
{
//
// Yes, remember it if this is real memory
//
MmLowestPhysicalPage = MemoryDescriptor->BasePage;
}
}
TRACE("lo/hi %lx %lx\n", MmLowestPhysicalPage, MmHighestPhysicalPage);
PageCount = MmHighestPhysicalPage - MmLowestPhysicalPage;
TRACE("MmGetAddressablePageCountIncludingHoles() returning 0x%x\n", PageCount);
return PageCount;
}
PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount)
{
const FREELDR_MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
SIZE_T PageLookupTableSize;
PFN_NUMBER RequiredPages;
PFN_NUMBER CandidateBasePage = 0;
PFN_NUMBER CandidatePageCount;
PFN_NUMBER PageLookupTableEndPage;
PVOID PageLookupTableMemAddress = NULL;
// Calculate how much pages we need to keep the page lookup table
PageLookupTableSize = TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM);
RequiredPages = PageLookupTableSize / MM_PAGE_SIZE;
// Search the highest memory block big enough to contain lookup table
while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
{
// Continue, if memory is not free
if (MemoryDescriptor->MemoryType != LoaderFree) continue;
// Continue, if the block is not big enough
if (MemoryDescriptor->PageCount < RequiredPages) continue;
// Continue, if it is not at a higher address than previous address
if (MemoryDescriptor->BasePage < CandidateBasePage) continue;
// Continue, if the address is too high
if (MemoryDescriptor->BasePage + RequiredPages >= TotalPageCount) continue;
// Memory block is more suitable than the previous one
CandidateBasePage = MemoryDescriptor->BasePage;
CandidatePageCount = MemoryDescriptor->PageCount;
}
// Calculate the end address for the lookup table
PageLookupTableEndPage = min(CandidateBasePage + CandidatePageCount,
TotalPageCount);
// Calculate the virtual address
PageLookupTableMemAddress = (PVOID)((PageLookupTableEndPage * PAGE_SIZE)
- PageLookupTableSize);
TRACE("MmFindLocationForPageLookupTable() returning 0x%x\n", PageLookupTableMemAddress);
return PageLookupTableMemAddress;
}
VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
{
const FREELDR_MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
PFN_NUMBER PageLookupTableStartPage;
PFN_NUMBER PageLookupTablePageCount;
TRACE("MmInitPageLookupTable()\n");
// Mark every page as allocated initially
// We will go through and mark pages again according to the memory map
// But this will mark any holes not described in the map as allocated
MmMarkPagesInLookupTable(PageLookupTable, MmLowestPhysicalPage, TotalPageCount, LoaderFirmwarePermanent);
// Parse the whole memory map
while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
{
// Mark used pages in the lookup table
if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount <= TotalPageCount)
{
TRACE("Marking pages 0x%lx-0x%lx as type %s\n",
MemoryDescriptor->BasePage,
MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
MmMarkPagesInLookupTable(PageLookupTable,
MemoryDescriptor->BasePage,
MemoryDescriptor->PageCount,
MemoryDescriptor->MemoryType);
}
else
TRACE("Ignoring pages 0x%lx-0x%lx (%s)\n",
MemoryDescriptor->BasePage,
MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
}
// Mark the pages that the lookup table occupies as reserved
PageLookupTableStartPage = MmGetPageNumberFromAddress(PageLookupTable);
PageLookupTablePageCount = MmGetPageNumberFromAddress((PVOID)((ULONG_PTR)PageLookupTable + ROUND_UP(TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM), MM_PAGE_SIZE))) - PageLookupTableStartPage;
TRACE("Marking the page lookup table pages as reserved StartPage: 0x%x PageCount: 0x%x\n", PageLookupTableStartPage, PageLookupTablePageCount);
MmMarkPagesInLookupTable(PageLookupTable, PageLookupTableStartPage, PageLookupTablePageCount, LoaderFirmwareTemporary);
}
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
PFN_NUMBER Index;
TRACE("MmMarkPagesInLookupTable()\n");
/* Validate the range */
if ((StartPage < MmLowestPhysicalPage) ||
((StartPage + PageCount - 1) > MmHighestPhysicalPage))
{
ERR("Memory (0x%lx:0x%lx) outside of lookup table! Valid range: 0x%lx-0x%lx.\n",
StartPage, PageCount, MmLowestPhysicalPage, MmHighestPhysicalPage);
return;
}
StartPage -= MmLowestPhysicalPage;
for (Index=StartPage; Index<(StartPage+PageCount); Index++)
{
#if 0
if ((Index <= (StartPage + 16)) || (Index >= (StartPage+PageCount-16)))
{
TRACE("Index = 0x%x StartPage = 0x%x PageCount = 0x%x\n", Index, StartPage, PageCount);
}
#endif
RealPageLookupTable[Index].PageAllocated = PageAllocated;
RealPageLookupTable[Index].PageAllocationLength = (PageAllocated != LoaderFree) ? 1 : 0;
}
TRACE("MmMarkPagesInLookupTable() Done\n");
}
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
PFN_NUMBER Index;
StartPage -= MmLowestPhysicalPage;
for (Index=StartPage; Index<(StartPage+PageCount); Index++)
{
RealPageLookupTable[Index].PageAllocated = MemoryType;
RealPageLookupTable[Index].PageAllocationLength = (Index == StartPage) ? PageCount : 0;
}
}
PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
PFN_NUMBER Index;
PFN_NUMBER FreePageCount;
FreePageCount = 0;
for (Index=0; Index<TotalPageCount; Index++)
{
if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
{
FreePageCount++;
}
}
return FreePageCount;
}
PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
PFN_NUMBER AvailablePagesSoFar;
PFN_NUMBER Index;
if (LastFreePageHint > TotalPageCount)
{
LastFreePageHint = TotalPageCount;
}
AvailablePagesSoFar = 0;
if (FromEnd)
{
/* Allocate "high" (from end) pages */
for (Index=LastFreePageHint-1; Index>0; Index--)
{
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
AvailablePagesSoFar = 0;
continue;
}
else
{
AvailablePagesSoFar++;
}
if (AvailablePagesSoFar >= PagesNeeded)
{
return Index + MmLowestPhysicalPage;
}
}
}
else
{
TRACE("Alloc low memory, LastFreePageHint 0x%x, TPC 0x%x\n", LastFreePageHint, TotalPageCount);
/* Allocate "low" pages */
for (Index=1; Index < LastFreePageHint; Index++)
{
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
AvailablePagesSoFar = 0;
continue;
}
else
{
AvailablePagesSoFar++;
}
if (AvailablePagesSoFar >= PagesNeeded)
{
return Index - AvailablePagesSoFar + 1 + MmLowestPhysicalPage;
}
}
}
return 0;
}
PFN_NUMBER MmFindAvailablePagesBeforePage(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
PFN_NUMBER AvailablePagesSoFar;
PFN_NUMBER Index;
if (LastPage > TotalPageCount)
{
return MmFindAvailablePages(PageLookupTable, TotalPageCount, PagesNeeded, TRUE);
}
AvailablePagesSoFar = 0;
for (Index=LastPage-1; Index>0; Index--)
{
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
AvailablePagesSoFar = 0;
continue;
}
else
{
AvailablePagesSoFar++;
}
if (AvailablePagesSoFar >= PagesNeeded)
{
return Index + MmLowestPhysicalPage;
}
}
return 0;
}
VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
PFN_NUMBER Index;
for (Index=TotalPageCount-1; Index>0; Index--)
{
if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
{
LastFreePageHint = Index + 1 + MmLowestPhysicalPage;
break;
}
}
}
BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
PFN_NUMBER StartPage;
PFN_NUMBER Index;
StartPage = MmGetPageNumberFromAddress(PageAddress);
if (StartPage < MmLowestPhysicalPage) return FALSE;
StartPage -= MmLowestPhysicalPage;
// Make sure they aren't trying to go past the
// end of available memory
if ((StartPage + PageCount) > TotalPageCount)
{
return FALSE;
}
for (Index = StartPage; Index < (StartPage + PageCount); Index++)
{
// If this page is allocated then there obviously isn't
// memory available so return FALSE
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
return FALSE;
}
}
return TRUE;
}