20 UINT64 CurrentNumberOfPages;
22 UINTN InformationIndex;
30UINTN mMemoryMapKey = 0;
32#define MAX_MAP_DEPTH 6
42UINTN mFreeMapStack = 0;
47BOOLEAN mMemoryTypeInformationInitialized =
FALSE;
89 { EfiMaxMemoryType, 0 }
134 Entry->Link.ForwardLink =
NULL;
136 if (Entry->FromPages) {
167 ASSERT ((Start & EFI_PAGE_MASK) == 0);
168 ASSERT (End > Start);
172 DEBUG ((DEBUG_PAGE,
"AddRange: %lx-%lx to %d\n", Start, End, Type));
184 if ((
PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) == 0) {
213 Link = gMemoryMap.ForwardLink;
214 while (Link != &gMemoryMap) {
215 Entry =
CR (Link,
MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
216 Link = Link->ForwardLink;
218 if (Entry->Type != Type) {
222 if (Entry->Attribute != Attribute) {
226 if (Entry->End + 1 == Start) {
227 Start = Entry->Start;
229 }
else if (Entry->Start == End + 1) {
286 if (FreeDescriptorEntries !=
NULL) {
291 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;
327 if (mFreeMapStack != 0) {
345 ASSERT (Entry !=
NULL);
362 Entry->FromPages =
TRUE;
367 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {
368 Entry2 =
CR (Link2,
MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
369 if (Entry2->FromPages && (Entry2->Start > Entry->Start)) {
403 DEBUG ((DEBUG_PAGE,
"Promote the memory resource\n"));
408 Link = mGcdMemorySpaceMap.ForwardLink;
409 while (Link != &mGcdMemorySpaceMap) {
414 ((Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==
415 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)))
420 if ((Entry->Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) {
426 Entry->Capabilities |= EFI_MEMORY_TESTED;
427 Entry->ImageHandle = gDxeCoreImageHandle;
428 Entry->DeviceHandle =
NULL;
438 Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)
445 Link = Link->ForwardLink;
462 Descriptor.
Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED |
463 EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)
483 UINT32 RuntimeCodePageNumber;
484 UINT32 BootTimeCodePageNumber;
492 if (!gLoadFixedAddressCodeMemoryReady) {
493 RuntimeCodePageNumber =
PcdGet32 (PcdLoadFixAddressRuntimeCodePageNumber);
494 BootTimeCodePageNumber =
PcdGet32 (PcdLoadFixAddressBootTimeCodePageNumber);
503 RuntimeCodePageNumber,
506 if (EFI_ERROR (Status)) {
519 BootTimeCodePageNumber,
522 if (EFI_ERROR (Status)) {
529 RuntimeCodePageNumber
534 gLoadFixedAddressCodeMemoryReady =
TRUE;
567 if (mMemoryTypeInformationInitialized) {
568 DEBUG ((DEBUG_ERROR,
"%a: Ignored. Bins already set.\n", __func__));
575 Top = Start + Length;
577 for (Index = 0; gMemoryTypeInformation[Index].
Type != EfiMaxMemoryType; Index++) {
582 if ((UINT32)Type > EfiMaxMemoryType) {
586 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {
593 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
606 Size += (Top & (Alignment - 1));
611 Top &= ~(Alignment - 1);
623 Top = Start + Length;
624 for (Index = 0; gMemoryTypeInformation[Index].
Type != EfiMaxMemoryType; Index++) {
629 if ((UINT32)Type > EfiMaxMemoryType) {
633 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {
640 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
646 Top = (Top - BinSize) & ~(Alignment - 1);
648 mMemoryTypeStatistics[Type].BaseAddress = Top;
649 mMemoryTypeStatistics[Type].MaximumAddress = Top + BinSize - 1;
655 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {
656 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;
669 for (Index = 0; gMemoryTypeInformation[Index].
Type != EfiMaxMemoryType; Index++) {
671 mMemoryTypeStatistics[Type].InformationIndex = Index;
675 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;
677 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;
681 mMemoryTypeInformationInitialized =
TRUE;
703 IN UINT64 NumberOfPages,
714 if ((Start & EFI_PAGE_MASK) != 0) {
718 if ((Type >= EfiMaxMemoryType) && (Type < MEMORY_TYPE_OEM_RESERVED_MIN)) {
723 End = Start +
LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;
732 LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT)
738 if (
PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {
745 if (mMemoryTypeInformationInitialized) {
752 for (Index = 0; gMemoryTypeInformation[Index].
Type != EfiMaxMemoryType; Index++) {
757 if ((UINT32)Type > EfiMaxMemoryType) {
761 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {
768 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
782 gMemoryTypeInformation[Index].NumberOfPages,
783 &mMemoryTypeStatistics[Type].BaseAddress
785 if (EFI_ERROR (Status)) {
791 for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {
796 if ((UINT32)Type > EfiMaxMemoryType) {
800 if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {
802 mMemoryTypeStatistics[Type].BaseAddress,
803 gMemoryTypeInformation[FreeIndex].NumberOfPages
805 mMemoryTypeStatistics[Type].BaseAddress = 0;
816 mMemoryTypeStatistics[Type].MaximumAddress =
817 mMemoryTypeStatistics[Type].BaseAddress +
818 LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;
824 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {
825 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;
835 for (Index = 0; gMemoryTypeInformation[Index].
Type != EfiMaxMemoryType; Index++) {
840 if ((UINT32)Type > EfiMaxMemoryType) {
844 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {
846 mMemoryTypeStatistics[Type].BaseAddress,
847 gMemoryTypeInformation[Index].NumberOfPages
849 mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].
NumberOfPages;
859 for (Index = 0; gMemoryTypeInformation[Index].
Type != EfiMaxMemoryType; Index++) {
861 mMemoryTypeStatistics[Type].InformationIndex = Index;
865 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;
867 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;
871 mMemoryTypeInformationInitialized =
TRUE;
897 IN UINT64 NumberOfPages,
898 IN BOOLEAN ChangingType,
900 IN BOOLEAN ChangingAttributes,
901 IN UINT64 NewAttributes
904 UINT64 NumberOfBytes;
913 NumberOfBytes =
LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
914 End = Start + NumberOfBytes - 1;
916 ASSERT (NumberOfPages);
917 ASSERT ((Start & EFI_PAGE_MASK) == 0);
918 ASSERT (End > Start);
920 ASSERT ((ChangingType ==
FALSE) || (ChangingAttributes ==
FALSE));
922 if ((NumberOfPages == 0) || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {
923 return EFI_INVALID_PARAMETER;
930 while (Start < End) {
934 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
935 Entry =
CR (Link,
MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
937 if ((Entry->Start <= Start) && (Entry->End > Start)) {
942 if ((Link == &gMemoryMap) || (Entry ==
NULL)) {
943 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"ConvertPages: failed to find range %lx - %lx\n", Start, End));
944 return EFI_NOT_FOUND;
953 if (Entry->End < End) {
954 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"ConvertPages: range %lx - %lx covers multiple entries\n", Start, End));
955 return EFI_NOT_FOUND;
965 ASSERT (Entry !=
NULL);
966 return EFI_NOT_FOUND;
969 if (Entry->End < End) {
970 RangeEnd = Entry->End;
974 DEBUG ((DEBUG_PAGE,
"ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));
977 if (ChangingAttributes) {
978 DEBUG ((DEBUG_PAGE,
"ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));
986 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"ConvertPages: Incompatible memory types, "));
988 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"the pages to free have been freed\n"));
990 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"the pages to allocate have been allocated\n"));
993 return EFI_NOT_FOUND;
999 if ((UINT32)Entry->Type < EfiMaxMemoryType) {
1000 if (((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress) && (Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress)) ||
1001 ((Start >= mDefaultBaseAddress) && (Start <= mDefaultMaximumAddress)))
1003 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {
1004 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;
1006 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;
1011 if ((UINT32)NewType < EfiMaxMemoryType) {
1012 if (((Start >= mMemoryTypeStatistics[NewType].BaseAddress) && (Start <= mMemoryTypeStatistics[NewType].MaximumAddress)) ||
1013 ((Start >= mDefaultBaseAddress) && (Start <= mDefaultMaximumAddress)))
1015 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;
1016 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {
1017 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].
NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;
1026 if (Entry->Start == Start) {
1030 Entry->Start = RangeEnd + 1;
1031 }
else if (Entry->End == RangeEnd) {
1035 Entry->End = Start - 1;
1055 Entry->End = Start - 1;
1056 ASSERT (Entry->Start < Entry->End);
1070 Attribute = Entry->Attribute;
1073 Attribute = NewAttributes;
1074 MemType = Entry->Type;
1080 if (Entry->Start == Entry->End + 1) {
1103 if (RangeEnd > EFI_PAGE_SIZE) {
1119 Start = RangeEnd + 1;
1148 IN UINT64 NumberOfPages,
1167 IN UINT64 NumberOfPages,
1168 IN UINT64 NewAttributes
1198 IN UINT64 MaxAddress,
1199 IN UINT64 MinAddress,
1200 IN UINT64 NumberOfPages,
1203 IN BOOLEAN NeedGuard
1206 UINT64 NumberOfBytes;
1210 UINT64 DescNumberOfBytes;
1214 if ((MaxAddress < EFI_PAGE_MASK) || (NumberOfPages == 0)) {
1218 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {
1226 MaxAddress -= (EFI_PAGE_MASK + 1);
1231 MaxAddress &= ~(UINT64)EFI_PAGE_MASK;
1236 MaxAddress |= EFI_PAGE_MASK;
1239 NumberOfBytes =
LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
1242 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1243 Entry =
CR (Link,
MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1255 if ((Entry->Attribute & EFI_MEMORY_SP) != 0) {
1259 DescStart = Entry->Start;
1260 DescEnd = Entry->End;
1265 if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {
1272 if (DescEnd >= MaxAddress) {
1273 DescEnd = MaxAddress;
1276 DescEnd = ((DescEnd + 1) & (~((UINT64)Alignment - 1))) - 1;
1279 if (DescEnd < DescStart) {
1287 DescNumberOfBytes = DescEnd - DescStart + 1;
1289 if (DescNumberOfBytes >= NumberOfBytes) {
1293 if ((DescEnd - NumberOfBytes + 1) < MinAddress) {
1300 if (DescEnd > Target) {
1303 DescEnd + 1 - DescNumberOfBytes,
1320 Target -= NumberOfBytes - 1;
1325 if ((Target & EFI_PAGE_MASK) != 0) {
1348 IN UINT64 MaxAddress,
1352 IN BOOLEAN NeedGuard
1360 if (((UINT32)NewType < EfiMaxMemoryType) && (MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress)) {
1362 mMemoryTypeStatistics[NewType].MaximumAddress,
1363 mMemoryTypeStatistics[NewType].BaseAddress,
1377 if (MaxAddress >= mDefaultMaximumAddress) {
1379 mDefaultMaximumAddress,
1387 if (Start < mDefaultBaseAddress) {
1388 mDefaultBaseAddress = NeedGuard ? Start - EFI_PAGE_SIZE : Start;
1423 return FindFreePages (MaxAddress, NoPages, NewType, Alignment, NeedGuard);
1452 IN BOOLEAN NeedGuard
1457 UINT64 NumberOfBytes;
1464 return EFI_INVALID_PARAMETER;
1467 if (((MemoryType >= EfiMaxMemoryType) && (MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN)) ||
1470 return EFI_INVALID_PARAMETER;
1473 if (Memory ==
NULL) {
1474 return EFI_INVALID_PARAMETER;
1484 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
1494 if (Alignment != EFI_PAGE_SIZE) {
1499 if ((*Memory & (Alignment - 1)) != 0) {
1500 return EFI_NOT_FOUND;
1530 return EFI_NOT_FOUND;
1533 if ((NumberOfPages == 0) ||
1534 (NumberOfPages >
RShiftU64 (MaxAddress, EFI_PAGE_SHIFT)))
1536 return EFI_NOT_FOUND;
1539 NumberOfBytes =
LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
1540 End = Start + NumberOfBytes - 1;
1542 if ((Start >= End) ||
1543 (Start > MaxAddress) ||
1546 return EFI_NOT_FOUND;
1557 for (CheckType = (
EFI_MEMORY_TYPE)0; CheckType < EfiMaxMemoryType; CheckType++) {
1558 if ((MemoryType != CheckType) &&
1559 mMemoryTypeStatistics[CheckType].Special &&
1560 (mMemoryTypeStatistics[CheckType].NumberOfPages > 0))
1562 if ((Start >= mMemoryTypeStatistics[CheckType].BaseAddress) &&
1563 (Start <= mMemoryTypeStatistics[CheckType].MaximumAddress))
1565 return EFI_NOT_FOUND;
1568 if ((End >= mMemoryTypeStatistics[CheckType].BaseAddress) &&
1569 (End <= mMemoryTypeStatistics[CheckType].MaximumAddress))
1571 return EFI_NOT_FOUND;
1574 if ((Start < mMemoryTypeStatistics[CheckType].BaseAddress) &&
1575 (End > mMemoryTypeStatistics[CheckType].MaximumAddress))
1577 return EFI_NOT_FOUND;
1601 Status = EFI_OUT_OF_RESOURCES;
1615 if (EFI_ERROR (Status)) {
1632 if (!EFI_ERROR (Status)) {
1681 if (!EFI_ERROR (Status)) {
1684 MemoryProfileActionAllocatePages,
1738 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1739 Entry =
CR (Link,
MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1740 if ((Entry->Start <= Memory) && (Entry->End > Memory)) {
1745 if (Link == &gMemoryMap) {
1746 Status = EFI_NOT_FOUND;
1750 if (Entry ==
NULL) {
1751 ASSERT (Entry !=
NULL);
1752 Status = EFI_NOT_FOUND;
1763 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
1766 if ((Memory & (Alignment - 1)) != 0) {
1767 Status = EFI_INVALID_PARAMETER;
1774 if (MemoryType !=
NULL) {
1775 *MemoryType = Entry->Type;
1819 if (gMemoryAttributeProtocol !=
NULL) {
1820 Status = gMemoryAttributeProtocol->GetMemoryAttributes (
1821 gMemoryAttributeProtocol,
1833 if ((Status == EFI_NO_MAPPING) ||
1834 (!EFI_ERROR (Status) && ((Attributes & EFI_MEMORY_RO) || (Attributes & EFI_MEMORY_RP))))
1838 "%a: Memory %llx for %llx Pages failed to get attributes with status %r or it is read-only or read-protected. "
1839 "Attributes: %llx. Leaking memory!\n",
1851 if (!EFI_ERROR (Status)) {
1855 MemoryProfileActionFreePages,
1897 for ( ; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {
1901 if (MemoryMap->Type != MemoryMapDescriptor->Type) {
1908 if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {
1915 if (MemoryMap->PhysicalStart +
EFI_PAGES_TO_SIZE ((
UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {
1919 MemoryMap->
NumberOfPages += MemoryMapDescriptor->NumberOfPages;
1924 return MemoryMapDescriptor;
1930 if (MemoryMap->PhysicalStart -
EFI_PAGES_TO_SIZE ((
UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {
1934 MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;
1935 MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;
1936 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;
1941 return MemoryMapDescriptor;
1951 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);
1966SetEfiMemoryDescriptorType (
1971 if (MemoryMap ==
NULL) {
1972 ASSERT (MemoryMap !=
NULL);
1976 MemoryMap->Type = Type;
1977 if (MemoryMap->Type >= EfiMaxMemoryType) {
1981 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {
1982 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;
1984 MemoryMap->Attribute &= ~EFI_MEMORY_RUNTIME;
2025 OUT UINT32 *DescriptorVersion
2031 UINTN NumberOfEntries;
2048 if (MemoryMapSize ==
NULL) {
2049 return EFI_INVALID_PARAMETER;
2058 NumberOfEntries = 0;
2059 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
2065 ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME)))
2078 Size +=
sizeof (UINT64) - (Size %
sizeof (UINT64));
2080 if (DescriptorSize !=
NULL) {
2081 *DescriptorSize = Size;
2084 if (DescriptorVersion !=
NULL) {
2093 BufferSize = Size * NumberOfEntries;
2094 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
2104 if (mMemoryTypeStatistics[Type].Special &&
2105 (mMemoryTypeStatistics[Type].NumberOfPages > 0))
2107 BufferSize += 2 * Size;
2111 if (*MemoryMapSize < BufferSize) {
2112 Status = EFI_BUFFER_TOO_SMALL;
2116 if (MemoryMap ==
NULL) {
2117 Status = EFI_INVALID_PARAMETER;
2124 ZeroMem (MemoryMap, BufferSize);
2125 MemoryMapStart = MemoryMap;
2126 MemoryMapEnd = MemoryMapStart;
2127 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
2128 Entry =
CR (Link,
MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
2129 ASSERT (Entry->VirtualStart == 0);
2133 MemoryMapEnd->
Attribute = Entry->Attribute;
2134 SetEfiMemoryDescriptorType (MemoryMapEnd, Entry->Type);
2136 if ((
UINTN)((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMapStart) > *MemoryMapSize) {
2137 Status = EFI_BUFFER_TOO_SMALL;
2152 if (mMemoryTypeStatistics[Type].NumberOfPages == 0) {
2156 if (!mMemoryTypeStatistics[Type].Special) {
2160 BinStart = mMemoryTypeStatistics[Type].BaseAddress;
2161 BinEnd = mMemoryTypeStatistics[Type].MaximumAddress;
2165 BufferSize = ((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMapStart);
2170 for (MemoryMap = MemoryMapStart; MemoryMap < MemoryMapEnd; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size)) {
2178 EntryStart = MemoryMap->PhysicalStart;
2185 if ((EntryEnd < BinStart) || (EntryStart > BinEnd)) {
2193 if ((EntryStart >= BinStart) && (EntryEnd <= BinEnd)) {
2194 SetEfiMemoryDescriptorType (MemoryMap, Type);
2206 if (EntryStart < BinStart) {
2218 NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size),
2220 ((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMap)
2222 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);
2223 MemoryMapEnd = NEXT_MEMORY_DESCRIPTOR (MemoryMapEnd, Size);
2224 if ((
UINTN)((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMapStart) > *MemoryMapSize) {
2229 Status = EFI_BUFFER_TOO_SMALL;
2233 MemoryMap->PhysicalStart = BinStart;
2235 SetEfiMemoryDescriptorType (MemoryMap, Type);
2237 if (EntryEnd > BinEnd) {
2248 NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size),
2250 ((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMap)
2252 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);
2253 MemoryMapEnd = NEXT_MEMORY_DESCRIPTOR (MemoryMapEnd, Size);
2254 if ((
UINTN)((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMapStart) > *MemoryMapSize) {
2259 Status = EFI_BUFFER_TOO_SMALL;
2263 MemoryMap->PhysicalStart = BinEnd + 1;
2278 if (EntryEnd > BinEnd) {
2282 MemoryMap->PhysicalStart = EntryStart;
2284 SetEfiMemoryDescriptorType (MemoryMap, Type);
2292 NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size),
2294 ((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMap)
2296 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);
2297 MemoryMapEnd = NEXT_MEMORY_DESCRIPTOR (MemoryMapEnd, Size);
2298 if ((
UINTN)((UINT8 *)MemoryMapEnd - (UINT8 *)MemoryMapStart) > *MemoryMapSize) {
2299 Status = EFI_BUFFER_TOO_SMALL;
2303 MemoryMap->PhysicalStart = BinEnd + 1;
2317 MemoryMap = MemoryMapEnd;
2319 ZeroMem (&MergeGcdMapEntry,
sizeof (MergeGcdMapEntry));
2321 for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {
2322 if (Link != &mGcdMemorySpaceMap) {
2328 if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) &&
2329 (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&
2330 (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&
2331 (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType))
2333 MergeGcdMapEntry.EndAddress = GcdMapEntry->EndAddress;
2340 ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME)))
2346 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);
2347 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);
2352 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;
2353 MemoryMap->VirtualStart = 0;
2354 MemoryMap->NumberOfPages =
RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);
2355 MemoryMap->Attribute = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) |
2356 (MergeGcdMapEntry.Capabilities & (EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK));
2361 if ((MergeGcdMapEntry.Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {
2373 if ((
UINTN)((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart) > *MemoryMapSize) {
2378 Status = EFI_BUFFER_TOO_SMALL;
2388 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);
2389 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);
2394 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;
2395 MemoryMap->VirtualStart = 0;
2396 MemoryMap->NumberOfPages =
RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);
2397 MemoryMap->Attribute = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV |
2398 (MergeGcdMapEntry.Capabilities & (EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK));
2406 if ((
UINTN)((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart) > *MemoryMapSize) {
2411 Status = EFI_BUFFER_TOO_SMALL;
2421 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);
2422 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);
2427 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;
2428 MemoryMap->VirtualStart = 0;
2429 MemoryMap->NumberOfPages =
RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);
2430 MemoryMap->Attribute = MergeGcdMapEntry.Attributes |
2431 (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |
2432 EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));
2440 if ((
UINTN)((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart) > *MemoryMapSize) {
2445 Status = EFI_BUFFER_TOO_SMALL;
2450 if (Link == &mGcdMemorySpaceMap) {
2457 if (GcdMapEntry !=
NULL) {
2461 CopyMem (&MergeGcdMapEntry, GcdMapEntry,
sizeof (MergeGcdMapEntry));
2468 BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);
2481 MemoryMapEnd = MemoryMap;
2482 MemoryMap = MemoryMapStart;
2483 while (MemoryMap < MemoryMapEnd) {
2484 MemoryMap->
Attribute &= ~(UINT64)EFI_MEMORY_ACCESS_MASK;
2485 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);
2497 if (MapKey !=
NULL) {
2498 *MapKey = mMemoryMapKey;
2505 *MemoryMapSize = BufferSize;
2531 IN BOOLEAN NeedGuard
2551 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));
2603 if (MapKey == mMemoryMapKey) {
2610 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
2611 Entry =
CR (Link,
MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
2612 if (Entry->Type < EfiMaxMemoryType) {
2613 if (mMemoryTypeStatistics[Entry->Type].Runtime) {
2616 if ((Entry->Start & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {
2617 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
2618 Status = EFI_INVALID_PARAMETER;
2622 if (((Entry->End + 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {
2623 DEBUG ((DEBUG_ERROR | DEBUG_PAGE,
"ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
2624 Status = EFI_INVALID_PARAMETER;
2639 Status = EFI_INVALID_PARAMETER;
#define DEFAULT_PAGE_ALLOCATION_GRANULARITY
#define MAX_ALLOC_ADDRESS
BOOLEAN EFIAPI IsListEmpty(IN CONST LIST_ENTRY *ListHead)
LIST_ENTRY *EFIAPI RemoveEntryList(IN CONST LIST_ENTRY *Entry)
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
#define INITIALIZE_LIST_HEAD_VARIABLE(ListHead)
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
LIST_ENTRY *EFIAPI InsertTailList(IN OUT LIST_ENTRY *ListHead, IN OUT LIST_ENTRY *Entry)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID SetGuardForMemory(IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages)
VOID EFIAPI GuardFreedPagesChecked(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINTN Pages)
VOID EFIAPI DumpGuardedMemoryBitmap(VOID)
BOOLEAN PromoteGuardedFreePages(OUT EFI_PHYSICAL_ADDRESS *StartAddress, OUT EFI_PHYSICAL_ADDRESS *EndAddress)
BOOLEAN IsHeapGuardEnabled(UINT8 GuardType)
UINT64 AdjustMemoryS(IN UINT64 Start, IN UINT64 Size, IN UINT64 SizeRequested)
BOOLEAN EFIAPI IsMemoryGuarded(IN EFI_PHYSICAL_ADDRESS Address)
BOOLEAN IsPageTypeToGuard(IN EFI_MEMORY_TYPE MemoryType, IN EFI_ALLOCATE_TYPE AllocateType)
EFI_STATUS CoreConvertPagesWithGuard(IN UINT64 Start, IN UINTN NumberOfPages, IN EFI_MEMORY_TYPE NewType)
EFI_STATUS EFIAPI CoreGetMemorySpaceDescriptor(IN EFI_PHYSICAL_ADDRESS BaseAddress, OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor)
VOID CoreAcquireGcdMemoryLock(VOID)
VOID CoreAcquireLock(IN EFI_LOCK *Lock)
VOID CoreReleaseLock(IN EFI_LOCK *Lock)
VOID MergeMemoryMap(IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN OUT UINTN *MemoryMapSize, IN UINTN DescriptorSize)
EFI_STATUS EFIAPI CoreUpdateProfile(IN EFI_PHYSICAL_ADDRESS CallerAddress, IN MEMORY_PROFILE_ACTION Action, IN EFI_MEMORY_TYPE MemoryType, IN UINTN Size, IN VOID *Buffer, IN CHAR8 *ActionString OPTIONAL)
VOID CoreNotifySignalList(IN EFI_GUID *EventGroup)
EFI_STATUS EFIAPI ApplyMemoryProtectionPolicy(IN EFI_MEMORY_TYPE OldType, IN EFI_MEMORY_TYPE NewType, IN EFI_PHYSICAL_ADDRESS Memory, IN UINT64 Length)
VOID InstallMemoryAttributesTableOnMemoryAllocation(IN EFI_MEMORY_TYPE MemoryType)
VOID CoreReleaseGcdMemoryLock(VOID)
VOID CoreFreeMemoryMapStack(VOID)
VOID RemoveMemoryMapEntry(IN OUT MEMORY_MAP *Entry)
BOOLEAN PromoteMemoryResource(VOID)
VOID CoreLoadingFixedAddressHook(VOID)
VOID CoreAddMemoryDescriptor(IN EFI_MEMORY_TYPE Type, IN EFI_PHYSICAL_ADDRESS Start, IN UINT64 NumberOfPages, IN UINT64 Attribute)
EFI_STATUS EFIAPI CoreFreePages(IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages)
VOID CoreUpdateMemoryAttributes(IN EFI_PHYSICAL_ADDRESS Start, IN UINT64 NumberOfPages, IN UINT64 NewAttributes)
EFI_STATUS EFIAPI CoreInternalFreePages(IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages, OUT EFI_MEMORY_TYPE *MemoryType OPTIONAL)
UINT64 FindFreePages(IN UINT64 MaxAddress, IN UINT64 NoPages, IN EFI_MEMORY_TYPE NewType, IN UINTN Alignment, IN BOOLEAN NeedGuard)
EFI_STATUS EFIAPI CoreAllocatePages(IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN UINTN NumberOfPages, OUT EFI_PHYSICAL_ADDRESS *Memory)
EFI_STATUS CoreConvertPages(IN UINT64 Start, IN UINT64 NumberOfPages, IN EFI_MEMORY_TYPE NewType)
EFI_STATUS EFIAPI CoreGetMemoryMap(IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, OUT UINTN *MapKey, OUT UINTN *DescriptorSize, OUT UINT32 *DescriptorVersion)
VOID * CoreAllocatePoolPages(IN EFI_MEMORY_TYPE PoolType, IN UINTN NumberOfPages, IN UINTN Alignment, IN BOOLEAN NeedGuard)
LIST_ENTRY mFreeMemoryMapEntryList
MEMORY_MAP * AllocateMemoryMapEntry(VOID)
EFI_MEMORY_DESCRIPTOR * MergeMemoryMapDescriptor(IN EFI_MEMORY_DESCRIPTOR *MemoryMap, IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor, IN UINTN DescriptorSize)
VOID CoreReleaseMemoryLock(VOID)
VOID CoreAcquireMemoryLock(VOID)
EFI_STATUS CoreConvertPagesEx(IN UINT64 Start, IN UINT64 NumberOfPages, IN BOOLEAN ChangingType, IN EFI_MEMORY_TYPE NewType, IN BOOLEAN ChangingAttributes, IN UINT64 NewAttributes)
VOID CoreSetMemoryTypeInformationRange(IN EFI_PHYSICAL_ADDRESS Start, IN UINT64 Length)
UINT64 CoreFindFreePagesI(IN UINT64 MaxAddress, IN UINT64 MinAddress, IN UINT64 NumberOfPages, IN EFI_MEMORY_TYPE NewType, IN UINTN Alignment, IN BOOLEAN NeedGuard)
MEMORY_MAP mMapStack[MAX_MAP_DEPTH]
EFI_STATUS EFIAPI CoreInternalAllocatePages(IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN UINTN NumberOfPages, IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN BOOLEAN NeedGuard)
VOID CoreAddRange(IN EFI_MEMORY_TYPE Type, IN EFI_PHYSICAL_ADDRESS Start, IN EFI_PHYSICAL_ADDRESS End, IN UINT64 Attribute)
VOID CoreFreePoolPages(IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages)
EFI_STATUS CoreTerminateMemoryMap(IN UINTN MapKey)
#define RETURN_ADDRESS(L)
#define ALIGN_VALUE(Value, Alignment)
#define GLOBAL_REMOVE_IF_UNREFERENCED
#define DEBUG(Expression)
#define CR(Record, TYPE, Field, TestSignature)
#define DEBUG_CLEAR_MEMORY(Address, Length)
#define DEBUG_CODE(Expression)
#define PcdGet64(TokenName)
#define PcdGet8(TokenName)
#define PcdGet32(TokenName)
@ EfiGcdMemoryTypeReserved
@ EfiGcdMemoryTypePersistent
@ EfiGcdMemoryTypeMoreReliable
@ EfiGcdMemoryTypeUnaccepted
@ EfiGcdMemoryTypeSystemMemory
@ EfiGcdMemoryTypeMemoryMappedIo
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_PAGES_TO_SIZE(Pages)
#define EFI_SIZE_TO_PAGES(Size)
#define ASSERT_LOCKED(LockParameter)
@ EfiMemoryMappedIOPortSpace
@ EfiUnacceptedMemoryType
#define EFI_MEMORY_DESCRIPTOR_VERSION
EFI_PHYSICAL_ADDRESS DxeCodeTopAddress
The top address below which the Dxe runtime code and below which the Dxe runtime/boot code and PEI co...
EFI_VIRTUAL_ADDRESS VirtualStart
EFI_PHYSICAL_ADDRESS PhysicalStart
UINT32 NumberOfPages
The pages of this type memory.
UINT32 Type
EFI memory type defined in UEFI specification.