/** Make sure the memory map is following all the construction rules, it is the last time to check memory map error before exit boot services. @param MapKey Memory map key @retval EFI_INVALID_PARAMETER Memory map not consistent with construction rules. @retval EFI_SUCCESS Valid memory map. **/ EFI_STATUS CoreTerminateMemoryMap ( IN UINTN MapKey ) { EFI_STATUS Status; LIST_ENTRY *Link; MEMORY_MAP *Entry; Status = EFI_SUCCESS; CoreAcquireMemoryLock (); if (MapKey == mMemoryMapKey) { // // Make sure the memory map is following all the construction rules // This is the last chance we will be able to display any messages on // the console devices. // for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) { Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) { if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) { DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n")); Status = EFI_INVALID_PARAMETER; goto Done; } if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) { DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n")); Status = EFI_INVALID_PARAMETER; goto Done; } if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) { DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n")); Status = EFI_INVALID_PARAMETER; goto Done; } } } // // The map key they gave us matches what we expect. Fall through and // return success. In an ideal world we would clear out all of // EfiBootServicesCode and EfiBootServicesData. However this function // is not the last one called by ExitBootServices(), so we have to // preserve the memory contents. // } else { Status = EFI_INVALID_PARAMETER; } Done: CoreReleaseMemoryLock (); return Status; }
/** Frees pool. @param Buffer The allocated pool entry to free @retval EFI_INVALID_PARAMETER Buffer is not a valid value. @retval EFI_SUCCESS Pool successfully freed. **/ EFI_STATUS EFIAPI CoreInternalFreePool ( IN VOID *Buffer ) { EFI_STATUS Status; if (Buffer == NULL) { return EFI_INVALID_PARAMETER; } CoreAcquireMemoryLock (); Status = CoreFreePoolI (Buffer); CoreReleaseMemoryLock (); return Status; }
/** Called to initialize the memory map and add descriptors to the current descriptor list. The first descriptor that is added must be general usable memory as the addition allocates heap. @param Type The type of memory to add @param Start The starting address in the memory range Must be page aligned @param NumberOfPages The number of pages in the range @param Attribute Attributes of the memory to add @return None. The range is added to the memory map **/ VOID CoreAddMemoryDescriptor ( IN EFI_MEMORY_TYPE Type, IN EFI_PHYSICAL_ADDRESS Start, IN UINT64 NumberOfPages, IN UINT64 Attribute ) { EFI_PHYSICAL_ADDRESS End; EFI_STATUS Status; UINTN Index; UINTN FreeIndex; if ((Start & EFI_PAGE_MASK) != 0) { return; } if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) { return; } CoreAcquireMemoryLock (); End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1; CoreAddRange (Type, Start, End, Attribute); CoreFreeMemoryMapStack (); CoreReleaseMemoryLock (); // // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type // if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) { CoreLoadingFixedAddressHook(); } // // Check to see if the statistics for the different memory types have already been established // if (mMemoryTypeInformationInitialized) { return; } // // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array // for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { // // Make sure the memory type in the gMemoryTypeInformation[] array is valid // Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); if (Type < 0 || Type > EfiMaxMemoryType) { continue; } if (gMemoryTypeInformation[Index].NumberOfPages != 0) { // // Allocate pages for the current memory type from the top of available memory // Status = CoreAllocatePages ( AllocateAnyPages, Type, gMemoryTypeInformation[Index].NumberOfPages, &mMemoryTypeStatistics[Type].BaseAddress ); if (EFI_ERROR (Status)) { // // If an error occurs allocating the pages for the current memory type, then // free all the pages allocates for the previous memory types and return. This // operation with be retied when/if more memory is added to the system // for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) { // // Make sure the memory type in the gMemoryTypeInformation[] array is valid // Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type); if (Type < 0 || Type > EfiMaxMemoryType) { continue; } if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) { CoreFreePages ( mMemoryTypeStatistics[Type].BaseAddress, gMemoryTypeInformation[FreeIndex].NumberOfPages ); mMemoryTypeStatistics[Type].BaseAddress = 0; mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS; } } return; } // // Compute the address at the top of the current statistics // mMemoryTypeStatistics[Type].MaximumAddress = mMemoryTypeStatistics[Type].BaseAddress + LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1; // // If the current base address is the lowest address so far, then update the default // maximum address // if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) { mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1; } } } // // There was enough system memory for all the the memory types were allocated. So, // those memory areas can be freed for future allocations, and all future memory // allocations can occur within their respective bins // for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { // // Make sure the memory type in the gMemoryTypeInformation[] array is valid // Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type); if (Type < 0 || Type > EfiMaxMemoryType) { continue; } if (gMemoryTypeInformation[Index].NumberOfPages != 0) { CoreFreePages ( mMemoryTypeStatistics[Type].BaseAddress, gMemoryTypeInformation[Index].NumberOfPages ); mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages; gMemoryTypeInformation[Index].NumberOfPages = 0; } } // // If the number of pages reserved for a memory type is 0, then all allocations for that type // should be in the default range. // for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) { for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) { mMemoryTypeStatistics[Type].InformationIndex = Index; } } mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0; if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) { mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress; } } mMemoryTypeInformationInitialized = TRUE; }
/** This function returns a copy of the current memory map. The map is an array of memory descriptors, each of which describes a contiguous block of memory. @param MemoryMapSize A pointer to the size, in bytes, of the MemoryMap buffer. On input, this is the size of the buffer allocated by the caller. On output, it is the size of the buffer returned by the firmware if the buffer was large enough, or the size of the buffer needed to contain the map if the buffer was too small. @param MemoryMap A pointer to the buffer in which firmware places the current memory map. @param MapKey A pointer to the location in which firmware returns the key for the current memory map. @param DescriptorSize A pointer to the location in which firmware returns the size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR. @param DescriptorVersion A pointer to the location in which firmware returns the version number associated with the EFI_MEMORY_DESCRIPTOR. @retval EFI_SUCCESS The memory map was returned in the MemoryMap buffer. @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current buffer size needed to hold the memory map is returned in MemoryMapSize. @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. **/ EFI_STATUS EFIAPI CoreGetMemoryMap ( IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, OUT UINTN *MapKey, OUT UINTN *DescriptorSize, OUT UINT32 *DescriptorVersion ) { EFI_STATUS Status; UINTN Size; UINTN BufferSize; UINTN NumberOfRuntimeEntries; LIST_ENTRY *Link; MEMORY_MAP *Entry; EFI_GCD_MAP_ENTRY *GcdMapEntry; EFI_MEMORY_TYPE Type; // // Make sure the parameters are valid // if (MemoryMapSize == NULL) { return EFI_INVALID_PARAMETER; } CoreAcquireGcdMemoryLock (); // // Count the number of Reserved and MMIO entries that are marked for runtime use // NumberOfRuntimeEntries = 0; for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) { GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE); if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) || (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) { if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) { NumberOfRuntimeEntries++; } } } Size = sizeof (EFI_MEMORY_DESCRIPTOR); // // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will // prevent people from having pointer math bugs in their code. // now you have to use *DescriptorSize to make things work. // Size += sizeof(UINT64) - (Size % sizeof (UINT64)); if (DescriptorSize != NULL) { *DescriptorSize = Size; } if (DescriptorVersion != NULL) { *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION; } CoreAcquireMemoryLock (); // // Compute the buffer size needed to fit the entire map // BufferSize = Size * NumberOfRuntimeEntries; for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) { BufferSize += Size; } if (*MemoryMapSize < BufferSize) { Status = EFI_BUFFER_TOO_SMALL; goto Done; } if (MemoryMap == NULL) { Status = EFI_INVALID_PARAMETER; goto Done; } // // Build the map // ZeroMem (MemoryMap, BufferSize); for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) { Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); ASSERT (Entry->VirtualStart == 0); // // Convert internal map into an EFI_MEMORY_DESCRIPTOR // MemoryMap->Type = Entry->Type; MemoryMap->PhysicalStart = Entry->Start; MemoryMap->VirtualStart = Entry->VirtualStart; MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT); // // If the memory type is EfiConventionalMemory, then determine if the range is part of a // memory type bin and needs to be converted to the same memory type as the rest of the // memory type bin in order to minimize EFI Memory Map changes across reboots. This // improves the chances for a successful S4 resume in the presence of minor page allocation // differences across reboots. // if (MemoryMap->Type == EfiConventionalMemory) { for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) { if (mMemoryTypeStatistics[Type].Special && mMemoryTypeStatistics[Type].NumberOfPages > 0 && Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress && Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) { MemoryMap->Type = Type; } } } MemoryMap->Attribute = Entry->Attribute; if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) { MemoryMap->Attribute |= EFI_MEMORY_RUNTIME; } MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size); } for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) { GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE); if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) || (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) { if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) { // // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries // that are marked for runtime use // MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress; MemoryMap->VirtualStart = 0; MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT); MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO; if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) { MemoryMap->Type = EfiReservedMemoryType; } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) { if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) { MemoryMap->Type = EfiMemoryMappedIOPortSpace; } else { MemoryMap->Type = EfiMemoryMappedIO; } } MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size); } } } Status = EFI_SUCCESS; Done: CoreReleaseMemoryLock (); CoreReleaseGcdMemoryLock (); // // Update the map key finally // if (MapKey != NULL) { *MapKey = mMemoryMapKey; } *MemoryMapSize = BufferSize; return Status; }
/** Frees previous allocated pages. @param Memory Base address of memory being freed @param NumberOfPages The number of pages to free @retval EFI_NOT_FOUND Could not find the entry that covers the range @retval EFI_INVALID_PARAMETER Address not aligned @return EFI_SUCCESS -Pages successfully freed. **/ EFI_STATUS EFIAPI CoreFreePages ( IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages ) { EFI_STATUS Status; LIST_ENTRY *Link; MEMORY_MAP *Entry; UINTN Alignment; // // Free the range // CoreAcquireMemoryLock (); // // Find the entry that the covers the range // Entry = NULL; for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) { Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); if (Entry->Start <= Memory && Entry->End > Memory) { break; } } if (Link == &gMemoryMap) { CoreReleaseMemoryLock (); return EFI_NOT_FOUND; } Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT; ASSERT (Entry != NULL); if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS || Entry->Type == EfiRuntimeServicesCode || Entry->Type == EfiRuntimeServicesData) { Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT; } if ((Memory & (Alignment - 1)) != 0) { CoreReleaseMemoryLock (); return EFI_INVALID_PARAMETER; } NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1; NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1); Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory); CoreReleaseMemoryLock (); if (EFI_ERROR (Status)) { return Status; } // // Destroy the contents // if (Memory < MAX_ADDRESS) { DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT); } return Status; }
/** Allocates pages from the memory map. @param Type The type of allocation to perform @param MemoryType The type of memory to turn the allocated pages into @param NumberOfPages The number of pages to allocate @param Memory A pointer to receive the base allocated memory address @return Status. On success, Memory is filled in with the base address allocated @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec. @retval EFI_NOT_FOUND Could not allocate pages match the requirement. @retval EFI_OUT_OF_RESOURCES No enough pages to allocate. @retval EFI_SUCCESS Pages successfully allocated. **/ EFI_STATUS EFIAPI CoreAllocatePages ( IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN UINTN NumberOfPages, IN OUT EFI_PHYSICAL_ADDRESS *Memory ) { EFI_STATUS Status; UINT64 Start; UINT64 MaxAddress; UINTN Alignment; if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) { return EFI_INVALID_PARAMETER; } if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) || MemoryType == EfiConventionalMemory) { return EFI_INVALID_PARAMETER; } Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT; if (MemoryType == EfiACPIReclaimMemory || MemoryType == EfiACPIMemoryNVS || MemoryType == EfiRuntimeServicesCode || MemoryType == EfiRuntimeServicesData) { Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT; } if (Type == AllocateAddress) { if ((*Memory & (Alignment - 1)) != 0) { return EFI_NOT_FOUND; } } NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1; NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1); // // If this is for below a particular address, then // Start = *Memory; // // The max address is the max natively addressable address for the processor // MaxAddress = MAX_ADDRESS; if (Type == AllocateMaxAddress) { MaxAddress = Start; } CoreAcquireMemoryLock (); // // If not a specific address, then find an address to allocate // if (Type != AllocateAddress) { Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment); if (Start == 0) { Status = EFI_OUT_OF_RESOURCES; goto Done; } } // // Convert pages from FreeMemory to the requested type // Status = CoreConvertPages (Start, NumberOfPages, MemoryType); Done: CoreReleaseMemoryLock (); if (!EFI_ERROR (Status)) { *Memory = Start; } return Status; }