ReactOS 0.4.16-dev-736-g28b802b
meminit.c File Reference
#include <freeldr.h>
#include <debug.h>
Include dependency graph for meminit.c:

Go to the source code of this file.

Functions

 DBG_DEFAULT_CHANNEL (MEMORY)
 
ULONG MmGetBiosMemoryMap (_Out_ PFREELDR_MEMORY_DESCRIPTOR *MemoryMap)
 
PFN_NUMBER MmGetTotalPagesInLookupTable (VOID)
 
PCSTR MmGetSystemMemoryMapTypeString (TYPE_OF_MEMORY Type)
 
ULONG AddMemoryDescriptor (IN OUT PFREELDR_MEMORY_DESCRIPTOR List, IN ULONG MaxCount, IN PFN_NUMBER BasePage, IN PFN_NUMBER PageCount, IN TYPE_OF_MEMORY MemoryType)
 
const FREELDR_MEMORY_DESCRIPTORArcGetMemoryDescriptor (const FREELDR_MEMORY_DESCRIPTOR *Current)
 
static VOID MmCheckFreeldrImageFile (VOID)
 
BOOLEAN MmInitializeMemoryManager (VOID)
 
PFN_NUMBER MmGetPageNumberFromAddress (PVOID Address)
 
PFN_NUMBER MmGetAddressablePageCountIncludingHoles (VOID)
 
PVOID MmFindLocationForPageLookupTable (PFN_NUMBER TotalPageCount)
 
VOID MmInitPageLookupTable (PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
 
VOID MmMarkPagesInLookupTable (PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
 
VOID MmAllocatePagesInLookupTable (PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
 
PFN_NUMBER MmCountFreePagesInLookupTable (PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
 
PFN_NUMBER MmFindAvailablePages (PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
 
PFN_NUMBER MmFindAvailablePagesBeforePage (PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage)
 
VOID MmUpdateLastFreePageHint (PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
 
BOOLEAN MmAreMemoryPagesAvailable (PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount)
 
PFN_NUMBER MmGetHighestPhysicalPage (VOID)
 

Variables

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
 

Function Documentation

◆ AddMemoryDescriptor()

ULONG AddMemoryDescriptor ( IN OUT PFREELDR_MEMORY_DESCRIPTOR  List,
IN ULONG  MaxCount,
IN PFN_NUMBER  BasePage,
IN PFN_NUMBER  PageCount,
IN TYPE_OF_MEMORY  MemoryType 
)

Definition at line 123 of file meminit.c.

129{
130 ULONG Index, DescriptCount;
132 TRACE("AddMemoryDescriptor(0x%Ix, 0x%Ix, %u)\n",
133 BasePage, PageCount, MemoryType);
134
135 EndPage = BasePage + PageCount;
136
137 /* Skip over all descriptor below the new range */
138 Index = 0;
139 while ((List[Index].PageCount != 0) &&
140 ((List[Index].BasePage + List[Index].PageCount) <= BasePage))
141 {
142 Index++;
143 }
144
145 /* Count the descriptors */
146 DescriptCount = Index;
147 while (List[DescriptCount].PageCount != 0)
148 {
149 DescriptCount++;
150 }
151
152 /* Check if the existing range conflicts with the new range */
153 while ((List[Index].PageCount != 0) &&
154 (List[Index].BasePage < EndPage))
155 {
156 TRACE("AddMemoryDescriptor conflict @%lu: new=[%lx:%lx], existing=[%lx,%lx]\n",
157 Index, BasePage, PageCount, List[Index].BasePage, List[Index].PageCount);
158
159 /*
160 * We have 4 overlapping cases:
161 *
162 * Case (a) (b) (c) (d)
163 * Existing range |---| |-----| |---| |---|
164 * New range |---| |---| |-----| |---|
165 *
166 */
167
168 /* Check if the existing range starts before the new range (a)/(b) */
169 if (List[Index].BasePage < BasePage)
170 {
171 /* Check if the existing range extends beyond the new range (b) */
172 if (List[Index].BasePage + List[Index].PageCount > EndPage)
173 {
174 /* Split the descriptor */
176 &List[Index],
177 (DescriptCount - Index) * sizeof(List[0]));
178 List[Index + 1].BasePage = EndPage;
179 List[Index + 1].PageCount = List[Index].BasePage +
180 List[Index].PageCount -
181 List[Index + 1].BasePage;
182 List[Index].PageCount = BasePage - List[Index].BasePage;
183 Index++;
184 DescriptCount++;
185 break;
186 }
187 else
188 {
189 /* Crop the existing range and continue with the next range */
190 List[Index].PageCount = BasePage - List[Index].BasePage;
191 Index++;
192 }
193 }
194 /* Check if the existing range is fully covered by the new range (c) */
195 else if ((List[Index].BasePage + List[Index].PageCount) <=
196 EndPage)
197 {
198 /* Delete this descriptor */
200 &List[Index + 1],
201 (DescriptCount - Index) * sizeof(List[0]));
202 DescriptCount--;
203 }
204 /* Otherwise the existing range ends after the new range (d) */
205 else
206 {
207 /* Crop the existing range at the start and bail out */
208 List[Index].PageCount -= EndPage - List[Index].BasePage;
209 List[Index].BasePage = EndPage;
210 break;
211 }
212 }
213
214 /* Make sure we can still add a new descriptor */
215 if (DescriptCount >= MaxCount)
216 {
219 __FILE__,
220 __LINE__,
221 "Ran out of static memory descriptors!");
222 }
223
224 /* Insert the new descriptor */
225 if (Index < DescriptCount)
226 {
228 &List[Index],
229 (DescriptCount - Index) * sizeof(List[0]));
230 }
231
232 List[Index].BasePage = BasePage;
233 List[Index].PageCount = PageCount;
234 List[Index].MemoryType = MemoryType;
235 DescriptCount++;
236
237#if 0 // only enable on demand!
238 DbgDumpMemoryMap(List);
239#endif
240 return DescriptCount;
241}
VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: debug.c:28
@ MEMORY_INIT_FAILURE
Definition: debug.h:146
ULONG PFN_NUMBER
Definition: ke.h:9
#define TRACE(s)
Definition: solgame.cpp:4
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFCMRESLIST List
Definition: wdfresource.h:550
int WINAPI EndPage(_In_ HDC)

Referenced by PcMemGetBiosMemoryMap(), PcMemGetMemoryMap(), ReserveMemory(), and SetMemory().

◆ ArcGetMemoryDescriptor()

const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor ( const FREELDR_MEMORY_DESCRIPTOR Current)

Definition at line 244 of file meminit.c.

245{
246 if (Current == NULL)
247 {
248 return BiosMemoryMap;
249 }
250 else
251 {
252 Current++;
253 if (Current->PageCount == 0) return NULL;
254 return Current;
255 }
256}
#define NULL
Definition: types.h:112
PFREELDR_MEMORY_DESCRIPTOR BiosMemoryMap
Definition: meminit.c:33
PFN_NUMBER PageCount
Definition: mm.h:45

Referenced by MmFindLocationForPageLookupTable(), MmGetAddressablePageCountIncludingHoles(), MmInitializeMemoryManager(), and MmInitPageLookupTable().

◆ DBG_DEFAULT_CHANNEL()

DBG_DEFAULT_CHANNEL ( MEMORY  )

◆ MmAllocatePagesInLookupTable()

VOID MmAllocatePagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  StartPage,
PFN_NUMBER  PageCount,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 557 of file meminit.c.

558{
559 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
561
563 for (Index=StartPage; Index<(StartPage+PageCount); Index++)
564 {
565 RealPageLookupTable[Index].PageAllocated = MemoryType;
566 RealPageLookupTable[Index].PageAllocationLength = (Index == StartPage) ? PageCount : 0;
567 }
568}
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
PFN_NUMBER PageAllocationLength
Definition: mm.h:87
TYPE_OF_MEMORY PageAllocated
Definition: mm.h:86
int WINAPI StartPage(_In_ HDC)

Referenced by MmAllocateHighestMemoryBelowAddress(), MmAllocateMemoryAtAddress(), MmAllocateMemoryWithType(), and MmSetMemoryType().

◆ MmAreMemoryPagesAvailable()

BOOLEAN MmAreMemoryPagesAvailable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PVOID  PageAddress,
PFN_NUMBER  PageCount 
)

Definition at line 695 of file meminit.c.

696{
697 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
700
702
704
706
707 // Make sure they aren't trying to go past the
708 // end of available memory
709 if ((StartPage + PageCount) > TotalPageCount)
710 {
711 return FALSE;
712 }
713
714 for (Index = StartPage; Index < (StartPage + PageCount); Index++)
715 {
716 // If this page is allocated then there obviously isn't
717 // memory available so return FALSE
718 if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
719 {
720 return FALSE;
721 }
722 }
723
724 return TRUE;
725}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:392
@ LoaderFree
Definition: arc.h:176
_Must_inspect_result_ _In_ SIZE_T _In_ PVOID PageAddress
Definition: mmfuncs.h:472

Referenced by MmAllocateMemoryAtAddress().

◆ MmCheckFreeldrImageFile()

static VOID MmCheckFreeldrImageFile ( VOID  )
static

Definition at line 260 of file meminit.c.

261{
262#ifndef UEFIBOOT
263 PIMAGE_NT_HEADERS NtHeaders;
264 PIMAGE_FILE_HEADER FileHeader;
265 PIMAGE_OPTIONAL_HEADER OptionalHeader;
266
267 /* Get the NT headers */
268 NtHeaders = RtlImageNtHeader(&__ImageBase);
269 if (!NtHeaders)
270 {
271 ERR("Could not get NtHeaders!\n");
274 __FILE__,
275 __LINE__,
276 "Could not get NtHeaders!\n");
277 }
278
279 /* Check the file header */
280 FileHeader = &NtHeaders->FileHeader;
281 if ((FileHeader->Machine != IMAGE_FILE_MACHINE_NATIVE) ||
282 (FileHeader->NumberOfSections != FREELDR_SECTION_COUNT) ||
283 (FileHeader->PointerToSymbolTable != 0) || // Symbols stripped
284 (FileHeader->NumberOfSymbols != 0) || // "" ""
285 (FileHeader->SizeOfOptionalHeader != sizeof(IMAGE_OPTIONAL_HEADER)))
286 {
287 ERR("FreeLdr FileHeader is invalid.\n");
290 __FILE__,
291 __LINE__,
292 "FreeLdr FileHeader is invalid.\n"
293 "Machine == 0x%lx, expected 0x%lx\n"
294 "NumberOfSections == 0x%lx, expected 0x%lx\n"
295 "PointerToSymbolTable == 0x%lx, expected 0\n"
296 "NumberOfSymbols == 0x%lx, expected 0\n"
297 "SizeOfOptionalHeader == 0x%lx, expected 0x%lx\n",
298 FileHeader->Machine, IMAGE_FILE_MACHINE_NATIVE,
300 FileHeader->PointerToSymbolTable,
301 FileHeader->NumberOfSymbols,
302 FileHeader->SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER));
303 }
304
305 /* Check the optional header */
306 OptionalHeader = &NtHeaders->OptionalHeader;
307 if ((OptionalHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC) ||
308 (OptionalHeader->Subsystem != IMAGE_SUBSYSTEM_NATIVE) ||
309 (OptionalHeader->ImageBase != FREELDR_PE_BASE) ||
310 (OptionalHeader->SizeOfImage > MAX_FREELDR_PE_SIZE) ||
311 (OptionalHeader->SectionAlignment != OptionalHeader->FileAlignment))
312 {
313 ERR("FreeLdr OptionalHeader is invalid.\n");
316 __FILE__,
317 __LINE__,
318 "FreeLdr OptionalHeader is invalid.\n"
319 "Magic == 0x%lx, expected 0x%lx\n"
320 "Subsystem == 0x%lx, expected 1 (native)\n"
321 "ImageBase == 0x%lx, expected 0x%lx\n"
322 "SizeOfImage == 0x%lx, maximum 0x%lx\n"
323 "SectionAlignment 0x%lx doesn't match FileAlignment 0x%lx\n",
324 OptionalHeader->Magic, IMAGE_NT_OPTIONAL_HDR_MAGIC,
325 OptionalHeader->Subsystem,
326 OptionalHeader->ImageBase, FREELDR_PE_BASE,
327 OptionalHeader->SizeOfImage, MAX_FREELDR_PE_SIZE,
328 OptionalHeader->SectionAlignment, OptionalHeader->FileAlignment);
329 }
330
331 /* Calculate the full image size */
333#endif
334}
#define ERR(fmt,...)
Definition: precomp.h:57
#define MAX_FREELDR_PE_SIZE
Definition: hardware.h:20
#define FREELDR_BASE
Definition: hardware.h:18
#define FREELDR_PE_BASE
Definition: hardware.h:19
@ FREELDR_IMAGE_CORRUPTION
Definition: debug.h:145
#define FREELDR_SECTION_COUNT
Definition: mm.h:37
#define __ImageBase
Definition: crt_handler.c:22
#define RtlImageNtHeader
Definition: compat.h:806
#define ULONG_PTR
Definition: config.h:101
SIZE_T FrLdrImageSize
Definition: meminit.c:35
#define IMAGE_SUBSYSTEM_NATIVE
Definition: ntimage.h:436
#define IMAGE_NT_OPTIONAL_HDR_MAGIC
Definition: ntimage.h:387
DWORD NumberOfSymbols
Definition: ntddk_ex.h:126
DWORD PointerToSymbolTable
Definition: ntddk_ex.h:125
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183

Referenced by MmInitializeMemoryManager().

◆ MmCountFreePagesInLookupTable()

PFN_NUMBER MmCountFreePagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 570 of file meminit.c.

571{
572 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
574 PFN_NUMBER FreePageCount;
575
576 FreePageCount = 0;
577 for (Index=0; Index<TotalPageCount; Index++)
578 {
579 if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
580 {
581 FreePageCount++;
582 }
583 }
584
585 return FreePageCount;
586}

Referenced by MmInitializeMemoryManager().

◆ MmFindAvailablePages()

PFN_NUMBER MmFindAvailablePages ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PFN_NUMBER  PagesNeeded,
BOOLEAN  FromEnd 
)

Definition at line 588 of file meminit.c.

589{
590 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
591 PFN_NUMBER AvailablePagesSoFar;
593
594 if (LastFreePageHint > TotalPageCount)
595 {
596 LastFreePageHint = TotalPageCount;
597 }
598
599 AvailablePagesSoFar = 0;
600 if (FromEnd)
601 {
602 /* Allocate "high" (from end) pages */
603 for (Index=LastFreePageHint-1; Index>0; Index--)
604 {
605 if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
606 {
607 AvailablePagesSoFar = 0;
608 continue;
609 }
610 else
611 {
612 AvailablePagesSoFar++;
613 }
614
615 if (AvailablePagesSoFar >= PagesNeeded)
616 {
618 }
619 }
620 }
621 else
622 {
623 TRACE("Alloc low memory, LastFreePageHint 0x%x, TPC 0x%x\n", LastFreePageHint, TotalPageCount);
624 /* Allocate "low" pages */
625 for (Index=1; Index < LastFreePageHint; Index++)
626 {
627 if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
628 {
629 AvailablePagesSoFar = 0;
630 continue;
631 }
632 else
633 {
634 AvailablePagesSoFar++;
635 }
636
637 if (AvailablePagesSoFar >= PagesNeeded)
638 {
639 return Index - AvailablePagesSoFar + 1 + MmLowestPhysicalPage;
640 }
641 }
642 }
643
644 return 0;
645}
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29

Referenced by MmAllocateMemoryWithType(), and MmFindAvailablePagesBeforePage().

◆ MmFindAvailablePagesBeforePage()

PFN_NUMBER MmFindAvailablePagesBeforePage ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PFN_NUMBER  PagesNeeded,
PFN_NUMBER  LastPage 
)

Definition at line 647 of file meminit.c.

648{
649 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
650 PFN_NUMBER AvailablePagesSoFar;
652
653 if (LastPage > TotalPageCount)
654 {
655 return MmFindAvailablePages(PageLookupTable, TotalPageCount, PagesNeeded, TRUE);
656 }
657
658 AvailablePagesSoFar = 0;
659 for (Index=LastPage-1; Index>0; Index--)
660 {
661 if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
662 {
663 AvailablePagesSoFar = 0;
664 continue;
665 }
666 else
667 {
668 AvailablePagesSoFar++;
669 }
670
671 if (AvailablePagesSoFar >= PagesNeeded)
672 {
674 }
675 }
676
677 return 0;
678}
PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
Definition: meminit.c:588

Referenced by MmAllocateHighestMemoryBelowAddress().

◆ MmFindLocationForPageLookupTable()

PVOID MmFindLocationForPageLookupTable ( PFN_NUMBER  TotalPageCount)

Definition at line 437 of file meminit.c.

438{
440 SIZE_T PageLookupTableSize;
441 PFN_NUMBER RequiredPages;
442 PFN_NUMBER CandidateBasePage = 0;
443 PFN_NUMBER CandidatePageCount = 0;
444 PFN_NUMBER PageLookupTableEndPage;
445 PVOID PageLookupTableMemAddress;
446
447 // Calculate how much pages we need to keep the page lookup table
448 PageLookupTableSize = TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM);
449 RequiredPages = PageLookupTableSize / MM_PAGE_SIZE;
450
451 // Search the highest memory block big enough to contain lookup table
453 {
454 // Continue, if memory is not free
455 if (MemoryDescriptor->MemoryType != LoaderFree) continue;
456
457 // Continue, if the block is not big enough
458 if (MemoryDescriptor->PageCount < RequiredPages) continue;
459
460 // Continue, if it is not at a higher address than previous address
461 if (MemoryDescriptor->BasePage < CandidateBasePage) continue;
462
463 // Continue, if the address is too high
464 if (MemoryDescriptor->BasePage + RequiredPages >= MM_MAX_PAGE_LOADER) continue;
465
466 // Memory block is more suitable than the previous one
467 CandidateBasePage = MemoryDescriptor->BasePage;
468 CandidatePageCount = MemoryDescriptor->PageCount;
469 }
470
471 // Calculate the end address for the lookup table
472 PageLookupTableEndPage = min(CandidateBasePage + CandidatePageCount,
473 MM_MAX_PAGE_LOADER);
474
475 // Calculate the virtual address
476 PageLookupTableMemAddress = (PVOID)((PageLookupTableEndPage * PAGE_SIZE)
477 - PageLookupTableSize);
478
479 TRACE("MmFindLocationForPageLookupTable() returning 0x%x\n", PageLookupTableMemAddress);
480
481 return PageLookupTableMemAddress;
482}
#define PAGE_SIZE
Definition: env_spec_w32.h:49
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:244
#define min(a, b)
Definition: monoChain.cc:55
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
_Must_inspect_result_ _In_ WDFUSBDEVICE _In_opt_ WDFREQUEST _In_opt_ PWDF_REQUEST_SEND_OPTIONS _In_ PWDF_USB_CONTROL_SETUP_PACKET _In_opt_ PWDF_MEMORY_DESCRIPTOR MemoryDescriptor
Definition: wdfusb.h:1339

Referenced by MmInitializeMemoryManager().

◆ MmGetAddressablePageCountIncludingHoles()

PFN_NUMBER MmGetAddressablePageCountIncludingHoles ( VOID  )

Definition at line 397 of file meminit.c.

398{
400 PFN_NUMBER PageCount;
401
402 //
403 // Go through the whole memory map to get max address
404 //
406 {
407 //
408 // Check if we got a higher end page address
409 //
410 if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > MmHighestPhysicalPage)
411 {
412 //
413 // Yes, remember it if this is real memory
414 //
415 if (MemoryDescriptor->MemoryType == LoaderFree)
417 }
418
419 //
420 // Check if we got a higher (usable) start page address
421 //
423 {
424 //
425 // Yes, remember it if this is real memory
426 //
428 }
429 }
430
433 TRACE("MmGetAddressablePageCountIncludingHoles() returning 0x%x\n", PageCount);
434 return PageCount;
435}
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31

Referenced by MmInitializeMemoryManager().

◆ MmGetBiosMemoryMap()

ULONG MmGetBiosMemoryMap ( _Out_ PFREELDR_MEMORY_DESCRIPTOR MemoryMap)

Definition at line 38 of file meminit.c.

39{
42}
BIOS_MEMORY_MAP MemoryMap[32]
Definition: loader.c:11
ULONG BiosMemoryMapEntryCount
Definition: meminit.c:34

Referenced by WinLdrSetupMemoryLayout().

◆ MmGetHighestPhysicalPage()

PFN_NUMBER MmGetHighestPhysicalPage ( VOID  )

Definition at line 728 of file meminit.c.

729{
731}

Referenced by WinLdrSetupMemoryLayout().

◆ MmGetPageNumberFromAddress()

PFN_NUMBER MmGetPageNumberFromAddress ( PVOID  Address)

Definition at line 392 of file meminit.c.

393{
394 return ((ULONG_PTR)Address) / MM_PAGE_SIZE;
395}
static WCHAR Address[46]
Definition: ping.c:68
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by MmAllocateMemoryAtAddress(), MmAreMemoryPagesAvailable(), MmInitPageLookupTable(), and MmSetMemoryType().

◆ MmGetSystemMemoryMapTypeString()

PCSTR MmGetSystemMemoryMapTypeString ( TYPE_OF_MEMORY  Type)

Definition at line 115 of file meminit.c.

117{
118 return "-";
119}

Referenced by MmInitializeMemoryManager(), MmInitPageLookupTable(), and PcMemFinalizeMemoryMap().

◆ MmGetTotalPagesInLookupTable()

PFN_NUMBER MmGetTotalPagesInLookupTable ( VOID  )

Definition at line 45 of file meminit.c.

46{
48}
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27

Referenced by MempAllocatePageTables().

◆ MmInitializeMemoryManager()

BOOLEAN MmInitializeMemoryManager ( VOID  )

Definition at line 336 of file meminit.c.

337{
338#if DBG
340#endif
341
342 TRACE("Initializing Memory Manager.\n");
343
344 /* Check the freeldr binary */
346
348
349#if DBG
350 // Dump the system memory map
351 TRACE("System Memory Map (Base Address, Length, Type):\n");
353 {
354 TRACE("%x\t %x\t %s\n",
355 MemoryDescriptor->BasePage * MM_PAGE_SIZE,
356 MemoryDescriptor->PageCount * MM_PAGE_SIZE,
358 }
359#endif
360
361 // Find address for the page lookup table
365
366 if (PageLookupTableAddress == 0)
367 {
368 // If we get here then we probably couldn't
369 // find a contiguous chunk of memory big
370 // enough to hold the page lookup table
371 printf("Error initializing memory manager!\n");
372 return FALSE;
373 }
374
375 // Initialize the page lookup table
377
379
382
384
385 TRACE("Memory Manager initialized. 0x%x pages available.\n", FreePagesInLookupTable);
386
387
388 return TRUE;
389}
MACHVTBL MachVtbl
Definition: arcemul.c:21
VOID MmInitializeHeap(PVOID PageLookupTable)
Definition: heap.c:562
#define printf
Definition: freeldr.h:97
PVOID PageLookupTableAddress
Definition: meminit.c:26
PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID)
Definition: meminit.c:397
static VOID MmCheckFreeldrImageFile(VOID)
Definition: meminit.c:260
VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:484
PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:570
PCSTR MmGetSystemMemoryMapTypeString(TYPE_OF_MEMORY Type)
Definition: meminit.c:115
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:680
PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount)
Definition: meminit.c:437
PFREELDR_MEMORY_DESCRIPTOR(* GetMemoryMap)(PULONG MaxMemoryMapSize)
Definition: machine.h:63

Referenced by BootMain(), and EfiEntry().

◆ MmInitPageLookupTable()

VOID MmInitPageLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 484 of file meminit.c.

485{
487 PFN_NUMBER PageLookupTableStartPage;
488 PFN_NUMBER PageLookupTablePageCount;
489
490 TRACE("MmInitPageLookupTable()\n");
491
492 // Mark every page as allocated initially
493 // We will go through and mark pages again according to the memory map
494 // But this will mark any holes not described in the map as allocated
496
497 // Parse the whole memory map
499 {
500 // Mark used pages in the lookup table
501
502 if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount <= TotalPageCount)
503 {
504 TRACE("Marking pages 0x%lx-0x%lx as type %s\n",
505 MemoryDescriptor->BasePage,
506 MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
508 MmMarkPagesInLookupTable(PageLookupTable,
509 MemoryDescriptor->BasePage,
510 MemoryDescriptor->PageCount,
511 MemoryDescriptor->MemoryType);
512 }
513 else
514 TRACE("Ignoring pages 0x%lx-0x%lx (%s)\n",
515 MemoryDescriptor->BasePage,
516 MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
518 }
519
520 // Mark the pages that the lookup table occupies as reserved
521 PageLookupTableStartPage = MmGetPageNumberFromAddress(PageLookupTable);
522 PageLookupTablePageCount = MmGetPageNumberFromAddress((PVOID)((ULONG_PTR)PageLookupTable + ROUND_UP(TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM), MM_PAGE_SIZE))) - PageLookupTableStartPage;
523 TRACE("Marking the page lookup table pages as reserved StartPage: 0x%x PageCount: 0x%x\n", PageLookupTableStartPage, PageLookupTablePageCount);
524 MmMarkPagesInLookupTable(PageLookupTable, PageLookupTableStartPage, PageLookupTablePageCount, LoaderFirmwareTemporary);
525}
#define ROUND_UP(n, align)
Definition: eventvwr.h:34
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
Definition: meminit.c:527
@ LoaderFirmwareTemporary
Definition: arc.h:179
@ LoaderFirmwarePermanent
Definition: arc.h:180

Referenced by MmInitializeMemoryManager().

◆ MmMarkPagesInLookupTable()

VOID MmMarkPagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  StartPage,
PFN_NUMBER  PageCount,
TYPE_OF_MEMORY  PageAllocated 
)

Definition at line 527 of file meminit.c.

528{
529 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
531 TRACE("MmMarkPagesInLookupTable()\n");
532
533 /* Validate the range */
535 ((StartPage + PageCount - 1) > MmHighestPhysicalPage))
536 {
537 ERR("Memory (0x%lx:0x%lx) outside of lookup table! Valid range: 0x%lx-0x%lx.\n",
539 return;
540 }
541
543 for (Index=StartPage; Index<(StartPage+PageCount); Index++)
544 {
545#if 0
546 if ((Index <= (StartPage + 16)) || (Index >= (StartPage+PageCount-16)))
547 {
548 TRACE("Index = 0x%x StartPage = 0x%x PageCount = 0x%x\n", Index, StartPage, PageCount);
549 }
550#endif
551 RealPageLookupTable[Index].PageAllocated = PageAllocated;
552 RealPageLookupTable[Index].PageAllocationLength = (PageAllocated != LoaderFree) ? 1 : 0;
553 }
554 TRACE("MmMarkPagesInLookupTable() Done\n");
555}

Referenced by FrLdrHeapDestroy(), FrLdrHeapRelease(), and MmInitPageLookupTable().

◆ MmUpdateLastFreePageHint()

VOID MmUpdateLastFreePageHint ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 680 of file meminit.c.

681{
682 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
684
685 for (Index=TotalPageCount-1; Index>0; Index--)
686 {
687 if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
688 {
690 break;
691 }
692 }
693}

Referenced by MmInitializeMemoryManager().

Variable Documentation

◆ BiosMemoryMap

◆ BiosMemoryMapEntryCount

ULONG BiosMemoryMapEntryCount

◆ FreePagesInLookupTable

◆ FrLdrImageSize

SIZE_T FrLdrImageSize

◆ LastFreePageHint

PFN_NUMBER LastFreePageHint = 0

◆ MmHighestPhysicalPage

◆ MmLowestPhysicalPage

◆ PageLookupTableAddress

◆ TotalPagesInLookupTable