ReactOS  r75214
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 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)
 

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

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 102 of file meminit.c.

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

108 {
109  ULONG Index, DescriptCount;
111  TRACE("AddMemoryDescriptor(0x%Ix, 0x%Ix, %u)\n",
112  BasePage, PageCount, MemoryType);
113 
114  EndPage = BasePage + PageCount;
115 
116  /* Skip over all descriptor below the new range */
117  Index = 0;
118  while ((List[Index].PageCount != 0) &&
119  ((List[Index].BasePage + List[Index].PageCount) <= BasePage))
120  {
121  Index++;
122  }
123 
124  /* Count the descriptors */
125  DescriptCount = Index;
126  while (List[DescriptCount].PageCount != 0)
127  {
128  DescriptCount++;
129  }
130 
131  /* Check if the existing range conflicts with the new range */
132  while ((List[Index].PageCount != 0) &&
133  (List[Index].BasePage < EndPage))
134  {
135  TRACE("AddMemoryDescriptor conflict @%lu: new=[%lx:%lx], existing=[%lx,%lx]\n",
136  Index, BasePage, PageCount, List[Index].BasePage, List[Index].PageCount);
137 
138  /*
139  * We have 4 overlapping cases:
140  *
141  * Case (a) (b) (c) (d)
142  * Existing range |---| |-----| |---| |---|
143  * New range |---| |---| |-----| |---|
144  *
145  */
146 
147  /* Check if the existing range starts before the new range (a)/(b) */
148  if (List[Index].BasePage < BasePage)
149  {
150  /* Check if the existing range extends beyond the new range (b) */
151  if (List[Index].BasePage + List[Index].PageCount > EndPage)
152  {
153  /* Split the descriptor */
154  RtlMoveMemory(&List[Index + 1],
155  &List[Index],
156  (DescriptCount - Index) * sizeof(List[0]));
157  List[Index + 1].BasePage = EndPage;
158  List[Index + 1].PageCount = List[Index].BasePage +
159  List[Index].PageCount -
160  List[Index + 1].BasePage;
161  List[Index].PageCount = BasePage - List[Index].BasePage;
162  Index++;
163  DescriptCount++;
164  break;
165  }
166  else
167  {
168  /* Crop the existing range and continue with the next range */
169  List[Index].PageCount = BasePage - List[Index].BasePage;
170  Index++;
171  }
172  }
173  /* Check if the existing range is fully covered by the new range (c) */
174  else if ((List[Index].BasePage + List[Index].PageCount) <=
175  EndPage)
176  {
177  /* Delete this descriptor */
178  RtlMoveMemory(&List[Index],
179  &List[Index + 1],
180  (DescriptCount - Index) * sizeof(List[0]));
181  DescriptCount--;
182  }
183  /* Otherwise the existing range ends after the new range (d) */
184  else
185  {
186  /* Crop the existing range at the start and bail out */
187  List[Index].PageCount -= EndPage - List[Index].BasePage;
188  List[Index].BasePage = EndPage;
189  break;
190  }
191  }
192 
193  /* Make sure we can still add a new descriptor */
194  if (DescriptCount >= MaxCount)
195  {
198  __FILE__,
199  __LINE__,
200  "Ran out of static memory descriptors!");
201  }
202 
203  /* Insert the new descriptor */
204  if (Index < DescriptCount)
205  {
206  RtlMoveMemory(&List[Index + 1],
207  &List[Index],
208  (DescriptCount - Index) * sizeof(List[0]));
209  }
210 
211  List[Index].BasePage = BasePage;
212  List[Index].PageCount = PageCount;
213  List[Index].MemoryType = MemoryType;
214  DescriptCount++;
215 
216 #if 0 // only enable on demand!
217  DbgDumpMemoryMap(List);
218 #endif
219  return DescriptCount;
220 }
ACPI_EFI_MEMORY_TYPE
Definition: acefiex.h:128
ACPI_EFI_MEMORY_TYPE MemoryType
Definition: acefiex.h:525
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
int WINAPI EndPage(_In_ HDC)
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
ULONG PFN_NUMBER
Definition: ke.h:8
#define TRACE(s)
Definition: solgame.cpp:4
LIST_ENTRY List
Definition: psmgr.c:57
VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: entry.c:23
unsigned int ULONG
Definition: retypes.h:1
const FREELDR_MEMORY_DESCRIPTOR* ArcGetMemoryDescriptor ( const FREELDR_MEMORY_DESCRIPTOR Current)

Definition at line 223 of file meminit.c.

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

224 {
225  if (Current == NULL)
226  {
227  return BiosMemoryMap;
228  }
229  else
230  {
231  Current++;
232  if (Current->PageCount == 0) return NULL;
233  return Current;
234  }
235 }
PFREELDR_MEMORY_DESCRIPTOR BiosMemoryMap
Definition: meminit.c:33
smooth NULL
Definition: ftsmooth.c:513
PFN_NUMBER PageCount
Definition: mm.h:40
DBG_DEFAULT_CHANNEL ( MEMORY  )
VOID MmAllocatePagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  StartPage,
PFN_NUMBER  PageCount,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 526 of file meminit.c.

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

527 {
528  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
530 
532  for (Index=StartPage; Index<(StartPage+PageCount); Index++)
533  {
534  RealPageLookupTable[Index].PageAllocated = MemoryType;
535  RealPageLookupTable[Index].PageAllocationLength = (Index == StartPage) ? PageCount : 0;
536  }
537 }
ACPI_EFI_MEMORY_TYPE MemoryType
Definition: acefiex.h:525
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
int WINAPI StartPage(_In_ HDC)
ULONG PFN_NUMBER
Definition: ke.h:8
PFN_NUMBER PageAllocationLength
Definition: mm.h:77
TYPE_OF_MEMORY PageAllocated
Definition: mm.h:76
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
BOOLEAN MmAreMemoryPagesAvailable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PVOID  PageAddress,
PFN_NUMBER  PageCount 
)

Definition at line 664 of file meminit.c.

Referenced by MmAllocateMemoryAtAddress().

665 {
666  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
669 
671 
672  if (StartPage < MmLowestPhysicalPage) return FALSE;
673 
674  StartPage -= MmLowestPhysicalPage;
675 
676  // Make sure they aren't trying to go past the
677  // end of available memory
678  if ((StartPage + PageCount) > TotalPageCount)
679  {
680  return FALSE;
681  }
682 
683  for (Index = StartPage; Index < (StartPage + PageCount); Index++)
684  {
685  // If this page is allocated then there obviously isn't
686  // memory available so return FALSE
687  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
688  {
689  return FALSE;
690  }
691  }
692 
693  return TRUE;
694 }
#define TRUE
Definition: types.h:120
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
_Must_inspect_result_ _In_ SIZE_T _In_ PVOID PageAddress
Definition: mmfuncs.h:472
int WINAPI StartPage(_In_ HDC)
ULONG PFN_NUMBER
Definition: ke.h:8
#define FALSE
Definition: types.h:117
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:369
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
static VOID MmCheckFreeldrImageFile ( VOID  )
static

Definition at line 239 of file meminit.c.

Referenced by MmInitializeMemoryManager().

240 {
241  PIMAGE_NT_HEADERS NtHeaders;
242  PIMAGE_FILE_HEADER FileHeader;
243  PIMAGE_OPTIONAL_HEADER OptionalHeader;
244 
245  /* Get the NT headers */
246  NtHeaders = RtlImageNtHeader(&__ImageBase);
247  if (!NtHeaders)
248  {
249  ERR("Could not get NtHeaders!\n");
252  __FILE__,
253  __LINE__,
254  "Could not get NtHeaders!\n");
255  }
256 
257  /* Check the file header */
258  FileHeader = &NtHeaders->FileHeader;
259  if ((FileHeader->Machine != IMAGE_FILE_MACHINE_NATIVE) ||
260  (FileHeader->NumberOfSections != FREELDR_SECTION_COUNT) ||
261  (FileHeader->PointerToSymbolTable != 0) || // Symbols stripped
262  (FileHeader->NumberOfSymbols != 0) || // "" ""
263  (FileHeader->SizeOfOptionalHeader != sizeof(IMAGE_OPTIONAL_HEADER)))
264  {
265  ERR("FreeLdr FileHeader is invalid.\n");
268  __FILE__,
269  __LINE__,
270  "FreeLdr FileHeader is invalid.\n"
271  "Machine == 0x%lx, expected 0x%lx\n"
272  "NumberOfSections == 0x%lx, expected 0x%lx\n"
273  "PointerToSymbolTable == 0x%lx, expected 0\n"
274  "NumberOfSymbols == 0x%lx, expected 0\n"
275  "SizeOfOptionalHeader == 0x%lx, expected 0x%lx\n",
276  FileHeader->Machine, IMAGE_FILE_MACHINE_NATIVE,
278  FileHeader->PointerToSymbolTable,
279  FileHeader->NumberOfSymbols,
280  FileHeader->SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER));
281  }
282 
283  /* Check the optional header */
284  OptionalHeader = &NtHeaders->OptionalHeader;
285  if ((OptionalHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC) ||
286  (OptionalHeader->Subsystem != IMAGE_SUBSYSTEM_NATIVE) ||
287  (OptionalHeader->ImageBase != FREELDR_PE_BASE) ||
288  (OptionalHeader->SizeOfImage > MAX_FREELDR_PE_SIZE) ||
289  (OptionalHeader->SectionAlignment != OptionalHeader->FileAlignment))
290  {
291  ERR("FreeLdr OptionalHeader is invalid.\n");
294  __FILE__,
295  __LINE__,
296  "FreeLdr OptionalHeader is invalid.\n"
297  "Magic == 0x%lx, expected 0x%lx\n"
298  "Subsystem == 0x%lx, expected 1 (native)\n"
299  "ImageBase == 0x%lx, expected 0x%lx\n"
300  "SizeOfImage == 0x%lx, maximum 0x%lx\n"
301  "SectionAlignment 0x%lx doesn't match FileAlignment 0x%lx\n",
302  OptionalHeader->Magic, IMAGE_NT_OPTIONAL_HDR_MAGIC,
303  OptionalHeader->Subsystem,
304  OptionalHeader->ImageBase, FREELDR_PE_BASE,
305  OptionalHeader->SizeOfImage, MAX_FREELDR_PE_SIZE,
306  OptionalHeader->SectionAlignment, OptionalHeader->FileAlignment);
307  }
308 
309  /* Calculate the full image size */
311 }
#define FREELDR_PE_BASE
Definition: hardware.h:19
#define FREELDR_BASE
Definition: hardware.h:18
#define MAX_FREELDR_PE_SIZE
Definition: hardware.h:20
#define FREELDR_SECTION_COUNT
Definition: mm.h:32
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
#define IMAGE_SUBSYSTEM_NATIVE
Definition: ntimage.h:436
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
DWORD NumberOfSymbols
Definition: ntddk_ex.h:126
SIZE_T FrLdrImageSize
Definition: meminit.c:35
#define ERR(fmt,...)
Definition: debug.h:109
VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: entry.c:23
DWORD PointerToSymbolTable
Definition: ntddk_ex.h:125
#define IMAGE_NT_OPTIONAL_HDR_MAGIC
Definition: ntimage.h:387
#define RtlImageNtHeader
Definition: compat.h:457
#define ULONG_PTR
Definition: config.h:101
#define __ImageBase
Definition: crt_handler.c:22
PFN_NUMBER MmCountFreePagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 539 of file meminit.c.

Referenced by MmInitializeMemoryManager().

540 {
541  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
543  PFN_NUMBER FreePageCount;
544 
545  FreePageCount = 0;
546  for (Index=0; Index<TotalPageCount; Index++)
547  {
548  if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
549  {
550  FreePageCount++;
551  }
552  }
553 
554  return FreePageCount;
555 }
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
ULONG PFN_NUMBER
Definition: ke.h:8
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
PFN_NUMBER MmFindAvailablePages ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PFN_NUMBER  PagesNeeded,
BOOLEAN  FromEnd 
)

Definition at line 557 of file meminit.c.

Referenced by MmAllocateMemoryWithType(), and MmFindAvailablePagesBeforePage().

558 {
559  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
560  PFN_NUMBER AvailablePagesSoFar;
562 
563  if (LastFreePageHint > TotalPageCount)
564  {
565  LastFreePageHint = TotalPageCount;
566  }
567 
568  AvailablePagesSoFar = 0;
569  if (FromEnd)
570  {
571  /* Allocate "high" (from end) pages */
572  for (Index=LastFreePageHint-1; Index>0; Index--)
573  {
574  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
575  {
576  AvailablePagesSoFar = 0;
577  continue;
578  }
579  else
580  {
581  AvailablePagesSoFar++;
582  }
583 
584  if (AvailablePagesSoFar >= PagesNeeded)
585  {
586  return Index + MmLowestPhysicalPage;
587  }
588  }
589  }
590  else
591  {
592  TRACE("Alloc low memory, LastFreePageHint 0x%x, TPC 0x%x\n", LastFreePageHint, TotalPageCount);
593  /* Allocate "low" pages */
594  for (Index=1; Index < LastFreePageHint; Index++)
595  {
596  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
597  {
598  AvailablePagesSoFar = 0;
599  continue;
600  }
601  else
602  {
603  AvailablePagesSoFar++;
604  }
605 
606  if (AvailablePagesSoFar >= PagesNeeded)
607  {
608  return Index - AvailablePagesSoFar + 1 + MmLowestPhysicalPage;
609  }
610  }
611  }
612 
613  return 0;
614 }
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
ULONG PFN_NUMBER
Definition: ke.h:8
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
PFN_NUMBER MmFindAvailablePagesBeforePage ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PFN_NUMBER  PagesNeeded,
PFN_NUMBER  LastPage 
)

Definition at line 616 of file meminit.c.

Referenced by MmAllocateHighestMemoryBelowAddress().

617 {
618  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
619  PFN_NUMBER AvailablePagesSoFar;
621 
622  if (LastPage > TotalPageCount)
623  {
624  return MmFindAvailablePages(PageLookupTable, TotalPageCount, PagesNeeded, TRUE);
625  }
626 
627  AvailablePagesSoFar = 0;
628  for (Index=LastPage-1; Index>0; Index--)
629  {
630  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
631  {
632  AvailablePagesSoFar = 0;
633  continue;
634  }
635  else
636  {
637  AvailablePagesSoFar++;
638  }
639 
640  if (AvailablePagesSoFar >= PagesNeeded)
641  {
642  return Index + MmLowestPhysicalPage;
643  }
644  }
645 
646  return 0;
647 }
#define TRUE
Definition: types.h:120
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
ULONG PFN_NUMBER
Definition: ke.h:8
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
Definition: meminit.c:557
PVOID MmFindLocationForPageLookupTable ( PFN_NUMBER  TotalPageCount)

Definition at line 414 of file meminit.c.

Referenced by MmInitializeMemoryManager().

415 {
417  SIZE_T PageLookupTableSize;
418  PFN_NUMBER PageLookupTablePages;
419  PFN_NUMBER PageLookupTableStartPage = 0;
420  PVOID PageLookupTableMemAddress = NULL;
421 
422  // Calculate how much pages we need to keep the page lookup table
423  PageLookupTableSize = TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM);
424  PageLookupTablePages = PageLookupTableSize / MM_PAGE_SIZE;
425 
426  // Search the highest memory block big enough to contain lookup table
427  while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
428  {
429  // Continue, if memory is not free
430  if (MemoryDescriptor->MemoryType != LoaderFree) continue;
431 
432  // Continue, if the block is not big enough?
433  if (MemoryDescriptor->PageCount < PageLookupTablePages) continue;
434 
435  // Continue, if it is not at a higher address than previous address
436  if (MemoryDescriptor->BasePage < PageLookupTableStartPage) continue;
437 
438  // Continue, if the address is too high
439  if (MemoryDescriptor->BasePage >= MM_MAX_PAGE) continue;
440 
441  // Memory block is more suitable than the previous one
442  PageLookupTableStartPage = MemoryDescriptor->BasePage;
443  PageLookupTableMemAddress = (PVOID)((ULONG_PTR)
444  (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount) * MM_PAGE_SIZE
445  - PageLookupTableSize);
446  }
447 
448  TRACE("MmFindLocationForPageLookupTable() returning 0x%x\n", PageLookupTableMemAddress);
449 
450  return PageLookupTableMemAddress;
451 }
DWORD *typedef PVOID
Definition: winlogon.h:52
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3251
TYPE_OF_MEMORY MemoryType
Definition: mm.h:38
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
uint32_t ULONG_PTR
Definition: typedefs.h:64
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:513
PFN_NUMBER BasePage
Definition: mm.h:39
#define TRACE(s)
Definition: solgame.cpp:4
ULONG_PTR SIZE_T
Definition: typedefs.h:79
PFN_NUMBER PageCount
Definition: mm.h:40
PFN_NUMBER MmGetAddressablePageCountIncludingHoles ( VOID  )

Definition at line 374 of file meminit.c.

Referenced by MmInitializeMemoryManager().

375 {
377  PFN_NUMBER PageCount;
378 
379  //
380  // Go through the whole memory map to get max address
381  //
382  while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
383  {
384  //
385  // Check if we got a higher end page address
386  //
387  if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > MmHighestPhysicalPage)
388  {
389  //
390  // Yes, remember it if this is real memory
391  //
392  if (MemoryDescriptor->MemoryType == LoaderFree)
393  MmHighestPhysicalPage = MemoryDescriptor->BasePage + MemoryDescriptor->PageCount;
394  }
395 
396  //
397  // Check if we got a higher (usable) start page address
398  //
399  if (MemoryDescriptor->BasePage < MmLowestPhysicalPage)
400  {
401  //
402  // Yes, remember it if this is real memory
403  //
404  MmLowestPhysicalPage = MemoryDescriptor->BasePage;
405  }
406  }
407 
408  TRACE("lo/hi %lx %lx\n", MmLowestPhysicalPage, MmHighestPhysicalPage);
410  TRACE("MmGetAddressablePageCountIncludingHoles() returning 0x%x\n", PageCount);
411  return PageCount;
412 }
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3251
TYPE_OF_MEMORY MemoryType
Definition: mm.h:38
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:513
PFN_NUMBER BasePage
Definition: mm.h:39
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31
PFN_NUMBER PageCount
Definition: mm.h:40
PFN_NUMBER MmGetPageNumberFromAddress ( PVOID  Address)

Definition at line 369 of file meminit.c.

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

370 {
371  return ((ULONG_PTR)Address) / MM_PAGE_SIZE;
372 }
PVOID ULONG Address
Definition: oprghdlr.h:14
uint32_t ULONG_PTR
Definition: typedefs.h:64
BOOLEAN MmInitializeMemoryManager ( VOID  )

Definition at line 313 of file meminit.c.

Referenced by BootMain().

314 {
315 #if DBG
317 #endif
318 
319  TRACE("Initializing Memory Manager.\n");
320 
321  /* Check the freeldr binary */
323 
325 
326 #if DBG
327  // Dump the system memory map
328  TRACE("System Memory Map (Base Address, Length, Type):\n");
329  while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
330  {
331  TRACE("%x\t %x\t %s\n",
332  MemoryDescriptor->BasePage * MM_PAGE_SIZE,
333  MemoryDescriptor->PageCount * MM_PAGE_SIZE,
334  MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
335  }
336 #endif
337 
338  // Find address for the page lookup table
342 
343  if (PageLookupTableAddress == 0)
344  {
345  // If we get here then we probably couldn't
346  // find a contiguous chunk of memory big
347  // enough to hold the page lookup table
348  printf("Error initializing memory manager!\n");
349  return FALSE;
350  }
351 
352  // Initialize the page lookup table
354 
356 
359 
361 
362  TRACE("Memory Manager initialized. 0x%x pages available.\n", FreePagesInLookupTable);
363 
364 
365  return TRUE;
366 }
PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount)
Definition: meminit.c:414
#define TRUE
Definition: types.h:120
PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:539
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3251
PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID)
Definition: meminit.c:374
TYPE_OF_MEMORY MemoryType
Definition: mm.h:38
PFREELDR_MEMORY_DESCRIPTOR BiosMemoryMap
Definition: meminit.c:33
ULONG BiosMemoryMapEntryCount
Definition: meminit.c:34
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
VOID MmInitializeHeap(PVOID PageLookupTable)
Definition: heap.c:531
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:513
VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:649
static VOID MmCheckFreeldrImageFile(VOID)
Definition: meminit.c:239
PFN_NUMBER BasePage
Definition: mm.h:39
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29
PFREELDR_MEMORY_DESCRIPTOR(* GetMemoryMap)(PULONG MaxMemoryMapSize)
Definition: machine.h:62
VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:453
PVOID PageLookupTableAddress
Definition: meminit.c:26
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31
MACHVTBL MachVtbl
Definition: machine.c:21
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
#define printf
Definition: config.h:203
PFN_NUMBER PageCount
Definition: mm.h:40
VOID MmInitPageLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 453 of file meminit.c.

Referenced by MmInitializeMemoryManager().

454 {
456  PFN_NUMBER PageLookupTableStartPage;
457  PFN_NUMBER PageLookupTablePageCount;
458 
459  TRACE("MmInitPageLookupTable()\n");
460 
461  // Mark every page as allocated initially
462  // We will go through and mark pages again according to the memory map
463  // But this will mark any holes not described in the map as allocated
464  MmMarkPagesInLookupTable(PageLookupTable, MmLowestPhysicalPage, TotalPageCount, LoaderFirmwarePermanent);
465 
466  // Parse the whole memory map
467  while ((MemoryDescriptor = ArcGetMemoryDescriptor(MemoryDescriptor)) != NULL)
468  {
469  // Mark used pages in the lookup table
470 
471  if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount <= TotalPageCount)
472  {
473  TRACE("Marking pages 0x%lx-0x%lx as type %s\n",
474  MemoryDescriptor->BasePage,
475  MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
476  MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
477  MmMarkPagesInLookupTable(PageLookupTable,
478  MemoryDescriptor->BasePage,
479  MemoryDescriptor->PageCount,
480  MemoryDescriptor->MemoryType);
481  }
482  else
483  TRACE("Ignoring pages 0x%lx-0x%lx (%s)\n",
484  MemoryDescriptor->BasePage,
485  MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
486  MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
487  }
488 
489  // Mark the pages that the lookup table occupies as reserved
490  PageLookupTableStartPage = MmGetPageNumberFromAddress(PageLookupTable);
491  PageLookupTablePageCount = MmGetPageNumberFromAddress((PVOID)((ULONG_PTR)PageLookupTable + ROUND_UP(TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM), MM_PAGE_SIZE))) - PageLookupTableStartPage;
492  TRACE("Marking the page lookup table pages as reserved StartPage: 0x%x PageCount: 0x%x\n", PageLookupTableStartPage, PageLookupTablePageCount);
493  MmMarkPagesInLookupTable(PageLookupTable, PageLookupTableStartPage, PageLookupTablePageCount, LoaderFirmwareTemporary);
494 }
DWORD *typedef PVOID
Definition: winlogon.h:52
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3251
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
TYPE_OF_MEMORY MemoryType
Definition: mm.h:38
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
uint32_t ULONG_PTR
Definition: typedefs.h:64
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:513
PFN_NUMBER BasePage
Definition: mm.h:39
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:369
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
Definition: meminit.c:496
PFN_NUMBER PageCount
Definition: mm.h:40
VOID MmMarkPagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  StartPage,
PFN_NUMBER  PageCount,
TYPE_OF_MEMORY  PageAllocated 
)

Definition at line 496 of file meminit.c.

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

497 {
498  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
500  TRACE("MmMarkPagesInLookupTable()\n");
501 
502  /* Validate the range */
504  ((StartPage + PageCount - 1) > MmHighestPhysicalPage))
505  {
506  ERR("Memory (0x%lx:0x%lx) outside of lookup table! Valid range: 0x%lx-0x%lx.\n",
508  return;
509  }
510 
512  for (Index=StartPage; Index<(StartPage+PageCount); Index++)
513  {
514 #if 0
515  if ((Index <= (StartPage + 16)) || (Index >= (StartPage+PageCount-16)))
516  {
517  TRACE("Index = 0x%x StartPage = 0x%x PageCount = 0x%x\n", Index, StartPage, PageCount);
518  }
519 #endif
520  RealPageLookupTable[Index].PageAllocated = PageAllocated;
521  RealPageLookupTable[Index].PageAllocationLength = (PageAllocated != LoaderFree) ? 1 : 0;
522  }
523  TRACE("MmMarkPagesInLookupTable() Done\n");
524 }
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
int WINAPI StartPage(_In_ HDC)
ULONG PFN_NUMBER
Definition: ke.h:8
PFN_NUMBER PageAllocationLength
Definition: mm.h:77
TYPE_OF_MEMORY PageAllocated
Definition: mm.h:76
#define TRACE(s)
Definition: solgame.cpp:4
#define ERR(fmt,...)
Definition: debug.h:109
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
VOID MmUpdateLastFreePageHint ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 649 of file meminit.c.

Referenced by MmInitializeMemoryManager().

650 {
651  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
653 
654  for (Index=TotalPageCount-1; Index>0; Index--)
655  {
656  if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
657  {
659  break;
660  }
661  }
662 }
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
ULONG PFN_NUMBER
Definition: ke.h:8
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Variable Documentation

Definition at line 33 of file meminit.c.

Referenced by ArcGetMemoryDescriptor().

ULONG BiosMemoryMapEntryCount

Definition at line 34 of file meminit.c.

Referenced by MmInitializeMemoryManager(), and WinLdrSetupMemoryLayout().

SIZE_T FrLdrImageSize

Definition at line 35 of file meminit.c.

Referenced by MmCheckFreeldrImageFile(), PcMemGetBiosMemoryMap(), and PcMemGetMemoryMap().

PFN_NUMBER LastFreePageHint = 0