X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FCore%2FPei%2FDispatcher%2FDispatcher.c;h=c7f0f8afafb78bc9c6ffa43c3eb4f5af0811648b;hb=30f001ca5f08974c8d5a5483f8b8e1a4f46a1025;hp=b9780a2c7b46b75577bc90ea1848998111d78aa6;hpb=b0d803fe3e5c55a9a0c75bc90ccf40ebbbc9ffa3;p=mirror_edk2.git diff --git a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c index b9780a2c7b..c7f0f8afaf 100644 --- a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c +++ b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c @@ -1,7 +1,8 @@ -/*++ - -Copyright (c) 2006, Intel Corporation -All rights reserved. This program and the accompanying materials +/** @file + EFI PEI Core dispatch services + +Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -9,62 +10,52 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: +**/ - Dispatcher.c +#include "PeiMain.h" -Abstract: - - EFI PEI Core dispatch services +/// +/// temporary memory is filled with this initial value during SEC phase +/// +#define INIT_CAR_VALUE 0x5AA55AA5 -Revision History +typedef struct { + EFI_STATUS_CODE_DATA DataHeader; + EFI_HANDLE Handle; +} PEIM_FILE_HANDLE_EXTENDED_DATA; ---*/ +/** -#include + Discover all Peims and optional Apriori file in one FV. There is at most one + Apriori file in one FV. -STATIC -VOID -InvokePeiCore ( - VOID *Context1, - VOID *Context2 - ); + @param Private Pointer to the private data passed in from caller + @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE. +**/ VOID DiscoverPeimsAndOrderWithApriori ( IN PEI_CORE_INSTANCE *Private, - IN EFI_PEI_FV_HANDLE VolumeHandle + IN PEI_CORE_FV_HANDLE *CoreFileHandle ) -/*++ - -Routine Description: - - Discover all Peims and optional Apriori file in one FV. There is at most one - Apriori file in one FV. - -Arguments: - - Private - Pointer to the private data passed in from caller - VolumeHandle - Fv handle. -Returns: - - NONE - ---*/ { EFI_STATUS Status; EFI_PEI_FV_HANDLE FileHandle; - EFI_PEI_FV_HANDLE AprioriFileHandle; + EFI_PEI_FILE_HANDLE AprioriFileHandle; EFI_GUID *Apriori; UINTN Index; UINTN Index2; UINTN PeimIndex; UINTN PeimCount; EFI_GUID *Guid; - EFI_PEI_FV_HANDLE TempFileHandles[PEI_CORE_MAX_PEIM_PER_FV]; - EFI_GUID FileGuid[PEI_CORE_MAX_PEIM_PER_FV]; - + EFI_PEI_FV_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)]; + EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)]; + EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi; + EFI_FV_FILE_INFO FileInfo; + + FvPpi = CoreFileHandle->FvPpi; + // // Walk the FV and find all the PEIMs and the Apriori file. // @@ -84,47 +75,54 @@ Returns: // // Go ahead to scan this Fv, and cache FileHandles within it. // - for (PeimCount = 0; PeimCount < PEI_CORE_MAX_PEIM_PER_FV; PeimCount++) { - Status = PeiFindFileEx ( - VolumeHandle, - NULL, - PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, - &FileHandle, - &AprioriFileHandle - ); + for (PeimCount = 0; PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) { + Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle); if (Status != EFI_SUCCESS) { break; } - + Private->CurrentFvFileHandles[PeimCount] = FileHandle; } + + // + // Check whether the count of Peims exceeds the max support PEIMs in a FV image + // If more Peims are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file. + // + ASSERT (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)); - Private->AprioriCount = 0; - if (AprioriFileHandle != NULL) { + // + // Get Apriori File handle + // + Private->AprioriCount = 0; + Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle); + if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) { // // Read the Apriori file // - Status = PeiServicesFfsFindSectionData (EFI_SECTION_RAW, &AprioriFileHandle, (VOID **) &Apriori); + Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori); if (!EFI_ERROR (Status)) { // // Calculate the number of PEIMs in the A Priori list // - Private->AprioriCount = *(UINT32 *)(((EFI_FFS_FILE_HEADER *)AprioriFileHandle)->Size) & 0x00FFFFFF; - Private->AprioriCount -= sizeof (EFI_FFS_FILE_HEADER) - sizeof (EFI_COMMON_SECTION_HEADER); + Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo); + ASSERT_EFI_ERROR (Status); + Private->AprioriCount = FileInfo.BufferSize & 0x00FFFFFF; + Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER); Private->AprioriCount /= sizeof (EFI_GUID); - - SetMem (FileGuid, sizeof (FileGuid), 0); + + ZeroMem (FileGuid, sizeof (FileGuid)); for (Index = 0; Index < PeimCount; Index++) { // // Make an array of file name guids that matches the FileHandle array so we can convert // quickly from file name to file handle // - CopyMem (&FileGuid[Index], &((EFI_FFS_FILE_HEADER *)Private->CurrentFvFileHandles[Index])->Name,sizeof(EFI_GUID)); + Status = FvPpi->GetFileInfo (FvPpi, Private->CurrentFvFileHandles[Index], &FileInfo); + CopyMem (&FileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID)); } // // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file. - // Add avalible PEIMs in Apriori file into TempFileHandles array at first. + // Add avalible PEIMs in Apriori file into TempFileHandles array at first. // Index2 = 0; for (Index = 0; Index2 < Private->AprioriCount; Index++) { @@ -135,7 +133,7 @@ Returns: } } if (Guid == NULL) { - break; + break; } PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID); TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex]; @@ -150,7 +148,7 @@ Returns: // Update valid Aprioricount // Private->AprioriCount = Index; - + // // Add in any PEIMs not in the Apriori file // @@ -167,11 +165,11 @@ Returns: //Index the end of array contains re-range Pei moudle. // TempFileHandles[Index] = NULL; - + // // Private->CurrentFvFileHandles is currently in PEIM in the FV order. - // We need to update it to start with files in the A Priori list and - // then the remaining files in PEIM order. + // We need to update it to start with files in the A Priori list and + // then the remaining files in PEIM order. // CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (Private->CurrentFvFileHandles)); } @@ -182,61 +180,478 @@ Returns: // Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE; CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles)); + +} + +// +// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled, +// This part of memory still need reserved on the very top of memory so that the DXE Core could +// use these memory for data initialization. This macro should be sync with the same marco +// defined in DXE Core. +// +#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000 +/** + This function is to test if the memory range described in resource HOB is available or not. + + This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the + memory before PeiLoadFixAddressHook in invoked. so this function is to test if the memory range described by the input resource HOB is + available or not. + + @param PrivateData Pointer to the private data passed in from caller + @param ResourceHob Pointer to a resource HOB which described the memory range described by the input resource HOB +**/ +BOOLEAN +PeiLoadFixAddressIsMemoryRangeAvailable ( + IN PEI_CORE_INSTANCE *PrivateData, + IN EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob + ) +{ + EFI_HOB_MEMORY_ALLOCATION *MemoryHob; + BOOLEAN IsAvailable; + EFI_PEI_HOB_POINTERS Hob; + + IsAvailable = TRUE; + if (PrivateData == NULL || ResourceHob == NULL) { + return FALSE; + } + // + // test if the memory range describe in the HOB is already allocated. + // + for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) { + // + // See if this is a memory allocation HOB + // + if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) { + MemoryHob = Hob.MemoryAllocation; + if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart && + MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength == ResourceHob->PhysicalStart + ResourceHob->ResourceLength) { + IsAvailable = FALSE; + break; + } + } + } + return IsAvailable; + } +/** + Hook function for Loading Module at Fixed Address feature + + This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is + configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When + feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general. + And also the function will re-install PEI memory. + @param PrivateData Pointer to the private data passed in from caller + +**/ VOID -PeiDispatcher ( - IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData, - IN PEI_CORE_INSTANCE *Private +PeiLoadFixAddressHook( + IN PEI_CORE_INSTANCE *PrivateData ) +{ + EFI_PHYSICAL_ADDRESS TopLoadingAddress; + UINT64 PeiMemorySize; + UINT64 TotalReservedMemorySize; + UINT64 MemoryRangeEnd; + EFI_PHYSICAL_ADDRESS HighAddress; + EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob; + EFI_HOB_RESOURCE_DESCRIPTOR *NextResourceHob; + EFI_HOB_RESOURCE_DESCRIPTOR *CurrentResourceHob; + EFI_PEI_HOB_POINTERS CurrentHob; + EFI_PEI_HOB_POINTERS Hob; + EFI_PEI_HOB_POINTERS NextHob; + EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress; + UINT64 MaxMemoryLength; + EFI_HOB_MEMORY_ALLOCATION *MemoryHob; + // + // Initialize Local Variables + // + CurrentResourceHob = NULL; + ResourceHob = NULL; + NextResourceHob = NULL; + MaxMemoryBaseAddress = 0; + MaxMemoryLength = 0; + HighAddress = 0; + TopLoadingAddress = 0; + MemoryRangeEnd = 0; + CurrentHob.Raw = PrivateData->HobList.Raw; + PeiMemorySize = PrivateData->PhysicalMemoryLength; + // + // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size MINIMUM_INITIAL_MEMORY_SIZE + // then RuntimeCodePage range and Boot time code range. + // + TotalReservedMemorySize = MINIMUM_INITIAL_MEMORY_SIZE + EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)); + TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ; + // + // PEI memory range lies below the top reserved memory + // + TotalReservedMemorySize += PeiMemorySize; + + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber))); + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber))); + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber))); + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize)); + // + // Loop through the system memory typed hob to merge the adjacent memory range + // + for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) { + // + // See if this is a resource descriptor HOB + // + if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { + + ResourceHob = Hob.ResourceDescriptor; + // + // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored. + // + if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY && + ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) { + continue; + } + + for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) { + if (NextHob.Raw == Hob.Raw){ + continue; + } + // + // See if this is a resource descriptor HOB + // + if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { + + NextResourceHob = NextHob.ResourceDescriptor; + // + // test if range described in this NextResourceHob is system memory and have the same attribute. + // Note: Here is a assumption that system memory should always be healthy even without test. + // + if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && + (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){ + + // + // See if the memory range described in ResourceHob and NextResourceHob is adjacent + // + if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart && + ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)|| + (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&& + ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) { + + MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ? + (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength); + + ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ? + ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart; + + + ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart); + + ResourceHob->ResourceAttribute = ResourceHob->ResourceAttribute & (~EFI_RESOURCE_ATTRIBUTE_TESTED); + // + // Delete the NextResourceHob by marking it as unused. + // + GET_HOB_TYPE (NextHob) = EFI_HOB_TYPE_UNUSED; + + } + } + } + } + } + } + // + // Some platform is already allocated pages before the HOB re-org. Here to build dedicated resource HOB to describe + // the allocated memory range + // + for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) { + // + // See if this is a memory allocation HOB + // + if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) { + MemoryHob = Hob.MemoryAllocation; + for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) { + // + // See if this is a resource descriptor HOB + // + if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { + NextResourceHob = NextHob.ResourceDescriptor; + // + // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored. + // + if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength > MAX_ADDRESS) { + continue; + } + // + // If the range describe in memory allocation HOB belongs to the memroy range described by the resource hob + // + if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart && + MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) { + // + // Build seperate resource hob for this allocated range + // + if (MemoryHob->AllocDescriptor.MemoryBaseAddress > NextResourceHob->PhysicalStart) { + BuildResourceDescriptorHob ( + EFI_RESOURCE_SYSTEM_MEMORY, + NextResourceHob->ResourceAttribute, + NextResourceHob->PhysicalStart, + (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart) + ); + } + if (MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength < NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) { + BuildResourceDescriptorHob ( + EFI_RESOURCE_SYSTEM_MEMORY, + NextResourceHob->ResourceAttribute, + MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength, + (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength)) + ); + } + NextResourceHob->PhysicalStart = MemoryHob->AllocDescriptor.MemoryBaseAddress; + NextResourceHob->ResourceLength = MemoryHob->AllocDescriptor.MemoryLength; + break; + } + } + } + } + } -/*++ + // + // Try to find and validate the TOP address. + // + if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) { + // + // The LMFA feature is enabled as load module at fixed absolute address. + // + TopLoadingAddress = (EFI_PHYSICAL_ADDRESS)PcdGet64(PcdLoadModuleAtFixAddressEnable); + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Loading module at fixed absolute address.\n")); + // + // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range + // + if ((TopLoadingAddress & EFI_PAGE_MASK) != 0) { + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress)); + ASSERT (FALSE); + } + // + // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies + // + for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) { + // + // See if this is a resource descriptor HOB + // + if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { -Routine Description: + ResourceHob = Hob.ResourceDescriptor; + // + // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS + // + if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && + ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) { + // + // See if Top address specified by user is valid. + // + if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress && + (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress && + PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) { + CurrentResourceHob = ResourceHob; + CurrentHob = Hob; + break; + } + } + } + } + if (CurrentResourceHob != NULL) { + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n", TopLoadingAddress)); + TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE; + } else { + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n", TopLoadingAddress)); + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n")); + // + // Print the recomended Top address range. + // + for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) { + // + // See if this is a resource descriptor HOB + // + if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { + + ResourceHob = Hob.ResourceDescriptor; + // + // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS + // + if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && + ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) { + // + // See if Top address specified by user is valid. + // + if (ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) { + DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n", + (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE), + (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE) + )); + } + } + } + } + // + // Assert here + // + ASSERT (FALSE); + return; + } + } else { + // + // The LMFA feature is enabled as load module at fixed offset relative to TOLM + // Parse the Hob list to find the topest available memory. Generally it is (TOLM - TSEG) + // + // + // Search for a tested memory region that is below MAX_ADDRESS + // + for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) { + // + // See if this is a resource descriptor HOB + // + if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { + + ResourceHob = Hob.ResourceDescriptor; + // + // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS + // + if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && + ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS && + ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) { + // + // See if this is the highest largest system memory region below MaxAddress + // + if (ResourceHob->PhysicalStart > HighAddress) { + CurrentResourceHob = ResourceHob; + CurrentHob = Hob; + HighAddress = CurrentResourceHob->PhysicalStart; + } + } + } + } + if (CurrentResourceHob == NULL) { + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n")); + // + // Assert here + // + ASSERT (FALSE); + return; + } else { + TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ; + } + } + + if (CurrentResourceHob != NULL) { + // + // rebuild resource HOB for PEI memmory and reserved memory + // + BuildResourceDescriptorHob ( + EFI_RESOURCE_SYSTEM_MEMORY, + ( + EFI_RESOURCE_ATTRIBUTE_PRESENT | + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | + EFI_RESOURCE_ATTRIBUTE_TESTED | + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE + ), + (TopLoadingAddress - TotalReservedMemorySize), + TotalReservedMemorySize + ); + // + // rebuild resource for the remain memory if necessary + // + if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) { + BuildResourceDescriptorHob ( + EFI_RESOURCE_SYSTEM_MEMORY, + ( + EFI_RESOURCE_ATTRIBUTE_PRESENT | + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE + ), + CurrentResourceHob->PhysicalStart, + (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart) + ); + } + if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength > TopLoadingAddress ) { + BuildResourceDescriptorHob ( + EFI_RESOURCE_SYSTEM_MEMORY, + ( + EFI_RESOURCE_ATTRIBUTE_PRESENT | + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE + ), + TopLoadingAddress, + (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength - TopLoadingAddress) + ); + } + // + // Delete CurrentHob by marking it as unused since the the memory range described by is rebuilt. + // + GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED; + } + // + // Cache the top address for Loading Module at Fixed Address feature + // + PrivateData->LoadModuleAtFixAddressTopAddress = TopLoadingAddress - MINIMUM_INITIAL_MEMORY_SIZE; + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n", PrivateData->LoadModuleAtFixAddressTopAddress)); + // + // reinstall the PEI memory relative to TopLoadingAddress + // + PrivateData->PhysicalMemoryBegin = TopLoadingAddress - TotalReservedMemorySize; + PrivateData->FreePhysicalMemoryTop = PrivateData->PhysicalMemoryBegin + PeiMemorySize; +} +/** Conduct PEIM dispatch. -Arguments: - - SecCoreData - Points to a data structure containing information about the PEI core's operating + @param SecCoreData Points to a data structure containing information about the PEI core's operating environment, such as the size and location of temporary RAM, the stack location and the BFV location. - PrivateData - Pointer to the private data passed in from caller - DispatchData - Pointer to PEI_CORE_DISPATCH_DATA data. - -Returns: - - EFI_SUCCESS - Successfully dispatched PEIM. - EFI_NOT_FOUND - The dispatch failed. + @param Private Pointer to the private data passed in from caller ---*/ +**/ +VOID +PeiDispatcher ( + IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData, + IN PEI_CORE_INSTANCE *Private + ) { EFI_STATUS Status; UINT32 Index1; UINT32 Index2; - EFI_PEI_SERVICES **PeiServices; - VOID *PrivateInMem; - EFI_PEI_FV_HANDLE VolumeHandle; - EFI_PEI_FILE_HANDLE PeiCoreFileHandle; + CONST EFI_PEI_SERVICES **PeiServices; EFI_PEI_FILE_HANDLE PeimFileHandle; UINTN FvCount; UINTN PeimCount; UINT32 AuthenticationState; EFI_PHYSICAL_ADDRESS EntryPoint; - EFI_PEIM_ENTRY_POINT PeimEntryPoint; - BOOLEAN PeimNeedingDispatch; - BOOLEAN PeimDispatchOnThisPass; + EFI_PEIM_ENTRY_POINT2 PeimEntryPoint; UINTN SaveCurrentPeimCount; + UINTN SaveCurrentFvCount; EFI_PEI_FILE_HANDLE SaveCurrentFileHandle; - VOID *TopOfStack; - PEI_CORE_PARAMETERS PeiCoreParameters; - EFI_DEVICE_HANDLE_EXTENDED_DATA ExtendedData; - - - PeiServices = &Private->PS; + PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData; + EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi; + UINT64 NewStackSize; + EFI_PHYSICAL_ADDRESS TopOfNewStack; + EFI_PHYSICAL_ADDRESS TopOfOldStack; + EFI_PHYSICAL_ADDRESS TemporaryRamBase; + UINTN TemporaryRamSize; + EFI_PHYSICAL_ADDRESS TemporaryStackSize; + UINTN StackOffset; + BOOLEAN StackOffsetPositive; + EFI_FV_FILE_INFO FvFileInfo; + PEI_CORE_FV_HANDLE *CoreFvHandle; + VOID *LoadFixPeiCodeBegin; + + PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps; PeimEntryPoint = NULL; PeimFileHandle = NULL; + EntryPoint = 0; if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { // @@ -244,16 +659,18 @@ Returns: // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE. // SaveCurrentPeimCount = Private->CurrentPeimCount; + SaveCurrentFvCount = Private->CurrentPeimFvCount; SaveCurrentFileHandle = Private->CurrentFileHandle; - for (Index1 = 0;Index1 <= Private->CurrentPeimFvCount; Index1++) { - for (Index2 = 0; (Index2 < PEI_CORE_MAX_PEIM_PER_FV) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) { + for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) { + for (Index2 = 0; (Index2 < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) { if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) { - PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2]; + PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2]; Status = PeiLoadImage ( - &Private->PS, - PeimFileHandle, - &EntryPoint, + (CONST EFI_PEI_SERVICES **) &Private->Ps, + PeimFileHandle, + PEIM_STATE_REGISITER_FOR_SHADOW, + &EntryPoint, &AuthenticationState ); if (Status == EFI_SUCCESS) { @@ -261,18 +678,19 @@ Returns: // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE // Private->Fv[Index1].PeimState[Index2]++; - Private->CurrentFileHandle = PeimFileHandle; - Private->CurrentPeimCount = Index2; + Private->CurrentFileHandle = PeimFileHandle; + Private->CurrentPeimFvCount = Index1; + Private->CurrentPeimCount = Index2; // // Call the PEIM entry point // - PeimEntryPoint = (EFI_PEIM_ENTRY_POINT)(UINTN)EntryPoint; - - PERF_START (0, "PEIM", NULL, 0); - PeimEntryPoint(PeimFileHandle, &Private->PS); - PERF_END (0, "PEIM", NULL, 0); - } - + PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint; + + PERF_START (PeimFileHandle, "PEIM", NULL, 0); + PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->Ps); + PERF_END (PeimFileHandle, "PEIM", NULL, 0); + } + // // Process the Notify list and dispatch any notifies for // newly installed PPIs. @@ -281,8 +699,9 @@ Returns: } } } - Private->CurrentFileHandle = SaveCurrentFileHandle; - Private->CurrentPeimCount = SaveCurrentPeimCount; + Private->CurrentFileHandle = SaveCurrentFileHandle; + Private->CurrentPeimFvCount = SaveCurrentFvCount; + Private->CurrentPeimCount = SaveCurrentPeimCount; } // @@ -294,169 +713,275 @@ Returns: // satisfied, this dipatcher should run only once. // do { - PeimNeedingDispatch = FALSE; - PeimDispatchOnThisPass = FALSE; - + // + // In case that reenter PeiCore happens, the last pass record is still available. + // + if (!Private->PeimDispatcherReenter) { + Private->PeimNeedingDispatch = FALSE; + Private->PeimDispatchOnThisPass = FALSE; + } else { + Private->PeimDispatcherReenter = FALSE; + } + for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) { + CoreFvHandle = FindNextCoreFvHandle (Private, FvCount); + ASSERT (CoreFvHandle != NULL); + + // + // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it. + // + if (CoreFvHandle->FvPpi == NULL) { + continue; + } + Private->CurrentPeimFvCount = FvCount; - VolumeHandle = Private->Fv[FvCount].FvHeader; if (Private->CurrentPeimCount == 0) { // // When going through each FV, at first, search Apriori file to - // reorder all PEIMs to ensure the PEIMs in Apriori file to get + // reorder all PEIMs to ensure the PEIMs in Apriori file to get // dispatch at first. // - DiscoverPeimsAndOrderWithApriori (Private, VolumeHandle); + DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle); } // // Start to dispatch all modules within the current Fv. // - for (PeimCount = Private->CurrentPeimCount; - (PeimCount < PEI_CORE_MAX_PEIM_PER_FV) && (Private->CurrentFvFileHandles[PeimCount] != NULL); + for (PeimCount = Private->CurrentPeimCount; + (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL); PeimCount++) { Private->CurrentPeimCount = PeimCount; PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount]; if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) { if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) { - PeimNeedingDispatch = TRUE; + Private->PeimNeedingDispatch = TRUE; } else { - Status = PeiLoadImage ( - PeiServices, - PeimFileHandle, - &EntryPoint, - &AuthenticationState - ); - if ((Status == EFI_SUCCESS)) { + Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo); + ASSERT_EFI_ERROR (Status); + if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) { + // + // For Fv type file, Produce new FV PPI and FV hob + // + Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle); + AuthenticationState = 0; + } else { + // + // For PEIM driver, Load its entry point + // + Status = PeiLoadImage ( + PeiServices, + PeimFileHandle, + PEIM_STATE_NOT_DISPATCHED, + &EntryPoint, + &AuthenticationState + ); + } + + if (Status == EFI_SUCCESS) { // // The PEIM has its dependencies satisfied, and its entry point // has been found, so invoke it. // - PERF_START (0, "PEIM", NULL, 0); + PERF_START (PeimFileHandle, "PEIM", NULL, 0); ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle; REPORT_STATUS_CODE_WITH_EXTENDED_DATA ( EFI_PROGRESS_CODE, - EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN, + (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN), (VOID *)(&ExtendedData), sizeof (ExtendedData) ); - Status = VerifyPeim (Private, VolumeHandle, PeimFileHandle); + Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle); if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) { // // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED // Private->Fv[FvCount].PeimState[PeimCount]++; - // - // Call the PEIM entry point - // - PeimEntryPoint = (EFI_PEIM_ENTRY_POINT)(UINTN)EntryPoint; - PeimEntryPoint (PeimFileHandle, PeiServices); - PeimDispatchOnThisPass = TRUE; + if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) { + // + // Call the PEIM entry point for PEIM driver + // + PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint; + PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices); + } + + Private->PeimDispatchOnThisPass = TRUE; } REPORT_STATUS_CODE_WITH_EXTENDED_DATA ( EFI_PROGRESS_CODE, - EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END, + (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END), (VOID *)(&ExtendedData), sizeof (ExtendedData) ); - PERF_END (0, "PEIM", NULL, 0); + PERF_END (PeimFileHandle, "PEIM", NULL, 0); } - // - // Process the Notify list and dispatch any notifies for - // newly installed PPIs. - // - ProcessNotifyList (Private); - - // - // If permanent memory was discovered and installed by this - // PEIM, shadow PEI Core and switch the stacks to the new memory. - // if (Private->SwitchStackSignal) { + // + // Before switch stack from temporary memory to permenent memory, caculate the heap and stack + // usage in temporary memory for debuging. + // + DEBUG_CODE_BEGIN (); + UINT32 *StackPointer; + + for (StackPointer = (UINT32*)SecCoreData->StackBase; + (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \ + && (*StackPointer == INIT_CAR_VALUE); + StackPointer ++); + + DEBUG ((EFI_D_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize)); + DEBUG ((EFI_D_INFO, "Temp Heap : BaseAddress=0x%p Length=0x%X\n", Private->HobList.Raw, (UINT32)((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN) Private->HobList.Raw))); + DEBUG ((EFI_D_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize)); + DEBUG ((EFI_D_INFO, " temporary memory stack ever used: %d bytes.\n", + (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase)) + )); + DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n", + (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw) + )); + DEBUG_CODE_END (); + + if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { + // + // Loading Module at Fixed Address is enabled + // + PeiLoadFixAddressHook (Private); + // + // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range. + // + LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber)); + DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PeiCodeBegin = 0x%lX, PeiCodeTop= 0x%lX\n", (UINT64)(UINTN)LoadFixPeiCodeBegin, (UINT64)((UINTN)LoadFixPeiCodeBegin + PcdGet32(PcdLoadFixAddressPeiCodePageNumber) * EFI_PAGE_SIZE))); + } + // - // Make sure we don't retry the same PEIM that added memory + // Reserve the size of new stack at bottom of physical memory // - Private->CurrentPeimCount++; + // The size of new stack in permenent memory must be the same size + // or larger than the size of old stack in temporary memory. + // But if new stack is smaller than the size of old stack, we also reserve + // the size of old stack at bottom of permenent memory. + // + NewStackSize = RShiftU64 (Private->PhysicalMemoryLength, 1); + NewStackSize = ALIGN_VALUE (NewStackSize, EFI_PAGE_SIZE); + NewStackSize = MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize), NewStackSize); + DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (UINT32)SecCoreData->StackSize, (UINT32)NewStackSize)); + ASSERT (NewStackSize >= SecCoreData->StackSize); // - // Migrate IDT from CAR into real memory, so after stack switches to - // the new memory, the caller can get memory version PeiServiceTable. + // Caculate stack offset and heap offset between temporary memory and new permement + // memory seperately. // - //MigrateIdtTable (PeiServices); + TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize; + TopOfNewStack = Private->PhysicalMemoryBegin + NewStackSize; + if (TopOfNewStack >= (UINTN)SecCoreData->PeiTemporaryRamBase) { + Private->HeapOffsetPositive = TRUE; + Private->HeapOffset = (UINTN)(TopOfNewStack - (UINTN)SecCoreData->PeiTemporaryRamBase); + } else { + Private->HeapOffsetPositive = FALSE; + Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - TopOfNewStack); + } + if (TopOfNewStack >= TopOfOldStack) { + StackOffsetPositive = TRUE; + StackOffset = (UINTN)(TopOfNewStack - TopOfOldStack); + } else { + StackOffsetPositive = FALSE; + StackOffset = (UINTN)(TopOfOldStack - TopOfNewStack); + } + Private->StackOffsetPositive = StackOffsetPositive; + Private->StackOffset = StackOffset; + + DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64)Private->HeapOffset, (UINT64)(StackOffset))); + // - // Since we are at dispatch level, only the Core's private data - // is preserved, nobody else should have any data on the stack. - // So we need to copy PEI core instance data to memory. + // Build Stack HOB that describes the permanent memory stack // - PrivateInMem = AllocateCopyPool (sizeof (PEI_CORE_INSTANCE), Private); - ASSERT (PrivateInMem != NULL); + DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack - NewStackSize, NewStackSize)); + BuildStackHob (TopOfNewStack - NewStackSize, NewStackSize); // - // Shadow PEI Core. When permanent memory is avaiable, shadow - // PEI Core and PEIMs to get high performance. + // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address // - PeiCoreFileHandle = NULL; + TemporaryRamBase = (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->TemporaryRamBase; + TemporaryRamSize = SecCoreData->TemporaryRamSize; + TemporaryStackSize = SecCoreData->StackSize; + // - // Find the PEI Core in the BFV + // Caculate new HandOffTable and PrivateData address in permanent memory's stack // - Status = PeiFindFileEx ( - (EFI_PEI_FV_HANDLE)Private->Fv[0].FvHeader, - NULL, - EFI_FV_FILETYPE_PEI_CORE, - &PeiCoreFileHandle, - NULL - ); - ASSERT_EFI_ERROR (Status); - + if (StackOffsetPositive) { + SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData + StackOffset); + Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private + StackOffset); + } else { + SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData - StackOffset); + Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private - StackOffset); + } + // - // Shadow PEI Core into memory so it will run faster + // TemporaryRamSupportPpi is produced by platform's SEC // - Status = PeiLoadImage (PeiServices, PeiCoreFileHandle, &EntryPoint, &AuthenticationState); - ASSERT_EFI_ERROR (Status); - + Status = PeiServicesLocatePpi ( + &gEfiTemporaryRamSupportPpiGuid, + 0, + NULL, + (VOID**)&TemporaryRamSupportPpi + ); + if (!EFI_ERROR (Status)) { + // + // Temporary Ram Support PPI is provided by platform, it will copy + // temporary memory to permenent memory and do stack switching. + // After invoking Temporary Ram Support PPI, the following code's + // stack is in permanent memory. + // + TemporaryRamSupportPpi->TemporaryRamMigration ( + PeiServices, + TemporaryRamBase, + (EFI_PHYSICAL_ADDRESS)(UINTN)(TopOfNewStack - TemporaryStackSize), + TemporaryRamSize + ); + + } else { + // + // In IA32/x64/Itanium architecture, we need platform provide + // TEMPORARY_RAM_MIGRATION_PPI. + // + ASSERT (FALSE); + } + // - // Switch to memory based stack and reenter PEI Core that has been - // shadowed to memory. + // Entry PEI Phase 2 // + PeiCore (SecCoreData, NULL, Private); + // - // Adjust the top of stack to be aligned at CPU_STACK_ALIGNMENT + // Code should not come here // - TopOfStack = (VOID *)((UINTN)Private->StackBase + (UINTN)Private->StackSize - CPU_STACK_ALIGNMENT); - TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT); - - PeiCoreParameters.SecCoreData = SecCoreData; - PeiCoreParameters.PpiList = NULL; - PeiCoreParameters.Data = PrivateInMem; - ASSERT (PeiCoreParameters.Data != 0); - - PeiSwitchStacks ( - InvokePeiCore, - (VOID*) (UINTN) PeiCore, - (VOID*) &PeiCoreParameters, - TopOfStack, - (VOID*)(UINTN)Private->StackBase - ); + ASSERT (FALSE); } + // + // Process the Notify list and dispatch any notifies for + // newly installed PPIs. + // + ProcessNotifyList (Private); + if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \ (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { // - // If memory is availble we shadow images by default for performance reasons. - // We call the entry point a 2nd time so the module knows it's shadowed. + // If memory is availble we shadow images by default for performance reasons. + // We call the entry point a 2nd time so the module knows it's shadowed. // //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0); - PeimEntryPoint (PeimFileHandle, PeiServices); + ASSERT (PeimEntryPoint != NULL); + PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices); //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0); - + // // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE // @@ -486,116 +1011,126 @@ Returns: } // - // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go + // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go // through all the FV. // Private->CurrentPeimFvCount = 0; // - // PeimNeedingDispatch being TRUE means we found a PEIM that did not get + // PeimNeedingDispatch being TRUE means we found a PEIM that did not get // dispatched. So we need to make another pass // - // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this + // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this // pass. If we did not dispatch a PEIM there is no point in trying again // as it will fail the next time too (nothing has changed). // - } while (PeimNeedingDispatch && PeimDispatchOnThisPass); + } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass); } -VOID -InitializeDispatcherData ( - IN PEI_CORE_INSTANCE *PrivateData, - IN PEI_CORE_INSTANCE *OldCoreData, - IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData - ) -/*++ - -Routine Description: - +/** Initialize the Dispatcher's data members -Arguments: - - PeiServices - The PEI core services table. - OldCoreData - Pointer to old core data (before switching stack). + @param PrivateData PeiCore's private data structure + @param OldCoreData Old data from SecCore NULL if being run in non-permament memory mode. - SecCoreData - Points to a data structure containing information about the PEI core's operating + @param SecCoreData Points to a data structure containing information about the PEI core's operating environment, such as the size and location of temporary RAM, the stack location and the BFV location. -Returns: - - None. + @return None. ---*/ +**/ +VOID +InitializeDispatcherData ( + IN PEI_CORE_INSTANCE *PrivateData, + IN PEI_CORE_INSTANCE *OldCoreData, + IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData + ) { if (OldCoreData == NULL) { + PrivateData->PeimDispatcherReenter = FALSE; PeiInitializeFv (PrivateData, SecCoreData); + } else { + PeiReinitializeFv (PrivateData); } return; } +/** + This routine parses the Dependency Expression, if available, and + decides if the module can be executed. + + + @param Private PeiCore's private data structure + @param FileHandle PEIM's file handle + @param PeimCount Peim count in all dispatched PEIMs. + @retval TRUE Can be dispatched + @retval FALSE Cannot be dispatched + +**/ BOOLEAN DepexSatisfied ( IN PEI_CORE_INSTANCE *Private, IN EFI_PEI_FILE_HANDLE FileHandle, IN UINTN PeimCount ) -/*++ - -Routine Description: - - This routine parses the Dependency Expression, if available, and - decides if the module can be executed. - -Arguments: - PeiServices - The PEI Service Table - CurrentPeimAddress - Address of the PEIM Firmware File under investigation - -Returns: - TRUE - Can be dispatched - FALSE - Cannot be dispatched - ---*/ { - EFI_STATUS Status; - VOID *DepexData; + EFI_STATUS Status; + VOID *DepexData; + EFI_FV_FILE_INFO FileInfo; + Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(Unknown)\n")); + } else { + DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo.FileName)); + } + if (PeimCount < Private->AprioriCount) { // // If its in the A priori file then we set Depex to TRUE // + DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n")); return TRUE; } - Status = PeiServicesFfsFindSectionData (EFI_SECTION_PEI_DEPEX, FileHandle, (VOID **) &DepexData); + // + // Depex section not in the encapsulated section. + // + Status = PeiServicesFfsFindSectionData ( + EFI_SECTION_PEI_DEPEX, + FileHandle, + (VOID **)&DepexData + ); + if (EFI_ERROR (Status)) { // // If there is no DEPEX, assume the module can be executed // + DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (No DEPEX)\n")); return TRUE; } // // Evaluate a given DEPEX // - return PeimDispatchReadiness (&Private->PS, DepexData); + return PeimDispatchReadiness (&Private->Ps, DepexData); } /** This routine enable a PEIM to register itself to shadow when PEI Foundation discovery permanent memory. - @param FileHandle File handle of a PEIM. - - @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself. - @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself. - @retval EFI_SUCCESS Successfully to register itself. + @param FileHandle File handle of a PEIM. + + @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself. + @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself. + @retval EFI_SUCCESS Successfully to register itself. -**/ +**/ EFI_STATUS EFIAPI PeiRegisterForShadow ( @@ -618,48 +1153,11 @@ PeiRegisterForShadow ( // return EFI_ALREADY_STARTED; } - + Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW; return EFI_SUCCESS; } -/** - This routine invoke the PeiCore's entry in new stack environment. - - @param Context1 The first context parameter is entry of PeiCore - @param Context2 The second context parameter is parameter structure point for PeiCore - -**/ -STATIC -VOID -InvokePeiCore ( - VOID *Context1, - VOID *Context2 - ) -{ - PEI_CORE_ENTRY_POINT PeiCoreEntryPoint; - PEI_CORE_PARAMETERS *PeiCoreParameters; - - // - // Running on new stack in SEC Core - // - PeiCoreEntryPoint = (PEI_CORE_ENTRY_POINT) (UINTN) Context1; - PeiCoreParameters = (PEI_CORE_PARAMETERS *)Context2; - - // - // Call PEI Core using new stack - // - PeiCoreEntryPoint ( - PeiCoreParameters->SecCoreData, - PeiCoreParameters->PpiList, - PeiCoreParameters->Data - ); - - // - // Never returns - // - ASSERT_EFI_ERROR (FALSE); -}