X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FCore%2FPei%2FDispatcher%2FDispatcher.c;h=b5e2a824553f22253938542817db1c10cabd7e94;hb=9b8e61be26dd303ffb88785bee63bdd1410b6234;hp=eb6313ac2377bb6c7785c37dac8d0adedd8aa161;hpb=b7250b714a24c650d3ed3abbf1a8c7d125edd85d;p=mirror_edk2.git diff --git a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c index eb6313ac23..b5e2a82455 100644 --- a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c +++ b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c @@ -1,8 +1,8 @@ /** @file EFI PEI Core dispatch services -Copyright (c) 2006 - 2010, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2006 - 2013, 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 @@ -41,7 +41,7 @@ DiscoverPeimsAndOrderWithApriori ( ) { EFI_STATUS Status; - EFI_PEI_FV_HANDLE FileHandle; + EFI_PEI_FILE_HANDLE FileHandle; EFI_PEI_FILE_HANDLE AprioriFileHandle; EFI_GUID *Apriori; UINTN Index; @@ -49,7 +49,7 @@ DiscoverPeimsAndOrderWithApriori ( UINTN PeimIndex; UINTN PeimCount; EFI_GUID *Guid; - EFI_PEI_FV_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)]; + EFI_PEI_FILE_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv) + 1]; EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)]; EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi; EFI_FV_FILE_INFO FileInfo; @@ -75,20 +75,21 @@ DiscoverPeimsAndOrderWithApriori ( // // Go ahead to scan this Fv, and cache FileHandles within it. // - for (PeimCount = 0; PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) { + Status = EFI_NOT_FOUND; + 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) { + if (Status != EFI_SUCCESS || PeimCount == FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) { 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)); + ASSERT ((Status != EFI_SUCCESS) || (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv))); // // Get Apriori File handle @@ -106,8 +107,12 @@ DiscoverPeimsAndOrderWithApriori ( // Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo); ASSERT_EFI_ERROR (Status); - Private->AprioriCount = FileInfo.BufferSize & 0x00FFFFFF; - Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER); + Private->AprioriCount = FileInfo.BufferSize; + if (IS_SECTION2 (FileInfo.Buffer)) { + Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER2); + } else { + Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER); + } Private->AprioriCount /= sizeof (EFI_GUID); ZeroMem (FileGuid, sizeof (FileGuid)); @@ -122,7 +127,7 @@ DiscoverPeimsAndOrderWithApriori ( // // 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 available PEIMs in Apriori file into TempFileHandles array at first. // Index2 = 0; for (Index = 0; Index2 < Private->AprioriCount; Index++) { @@ -183,56 +188,6 @@ DiscoverPeimsAndOrderWithApriori ( } -/** - Shadow PeiCore module from flash to installed memory. - - @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation. - @param PrivateInMem PeiCore's private data structure - - @return PeiCore function address after shadowing. -**/ -VOID* -ShadowPeiCore( - IN CONST EFI_PEI_SERVICES **PeiServices, - IN PEI_CORE_INSTANCE *PrivateInMem - ) -{ - EFI_PEI_FILE_HANDLE PeiCoreFileHandle; - EFI_PHYSICAL_ADDRESS EntryPoint; - EFI_STATUS Status; - UINT32 AuthenticationState; - - PeiCoreFileHandle = NULL; - - // - // Find the PEI Core in the BFV - // - Status = PrivateInMem->Fv[0].FvPpi->FindFileByType ( - PrivateInMem->Fv[0].FvPpi, - EFI_FV_FILETYPE_PEI_CORE, - PrivateInMem->Fv[0].FvHandle, - &PeiCoreFileHandle - ); - ASSERT_EFI_ERROR (Status); - - // - // Shadow PEI Core into memory so it will run faster - // - Status = PeiLoadImage ( - PeiServices, - *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle), - PEIM_STATE_REGISITER_FOR_SHADOW, - &EntryPoint, - &AuthenticationState - ); - ASSERT_EFI_ERROR (Status); - - // - // Compute the PeiCore's function address after shaowed PeiCore. - // _ModuleEntryPoint is PeiCore main function entry - // - return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint); -} // // 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 @@ -311,8 +266,6 @@ PeiLoadFixAddressHook( 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 @@ -320,8 +273,6 @@ PeiLoadFixAddressHook( CurrentResourceHob = NULL; ResourceHob = NULL; NextResourceHob = NULL; - MaxMemoryBaseAddress = 0; - MaxMemoryLength = 0; HighAddress = 0; TopLoadingAddress = 0; MemoryRangeEnd = 0; @@ -355,7 +306,7 @@ PeiLoadFixAddressHook( // // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored. // - if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY && + if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY || ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) { continue; } @@ -425,7 +376,7 @@ PeiLoadFixAddressHook( // // 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) { + if (NextResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY || NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength > MAX_ADDRESS) { continue; } // @@ -656,6 +607,29 @@ PeiLoadFixAddressHook( PrivateData->PhysicalMemoryBegin = TopLoadingAddress - TotalReservedMemorySize; PrivateData->FreePhysicalMemoryTop = PrivateData->PhysicalMemoryBegin + PeiMemorySize; } + +/** + This routine is invoked in switch stack as PeiCore Entry. + + @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. + @param Private Pointer to old core data that is used to initialize the + core's data areas. +**/ +VOID +EFIAPI +PeiCoreEntry ( + IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData, + IN PEI_CORE_INSTANCE *Private + ) +{ + // + // Entry PEI Phase 2 + // + PeiCore (SecCoreData, NULL, Private); +} + /** Conduct PEIM dispatch. @@ -685,28 +659,36 @@ PeiDispatcher ( UINTN SaveCurrentFvCount; EFI_PEI_FILE_HANDLE SaveCurrentFileHandle; PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData; - EFI_PHYSICAL_ADDRESS NewPermenentMemoryBase; - TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi; - EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffTable; - EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffTable; - INTN StackOffset; - INTN HeapOffset; - PEI_CORE_INSTANCE *PrivateInMem; - UINT64 NewPeiStackSize; - UINT64 OldPeiStackSize; - UINT64 StackGap; + EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi; + UINT64 NewStackSize; + EFI_PHYSICAL_ADDRESS BaseOfNewHeap; + EFI_PHYSICAL_ADDRESS TopOfNewStack; + EFI_PHYSICAL_ADDRESS TopOfOldStack; + EFI_PHYSICAL_ADDRESS TemporaryRamBase; + UINTN TemporaryRamSize; + UINTN TemporaryStackSize; + VOID *TemporaryStackBase; + UINTN PeiTemporaryRamSize; + VOID *PeiTemporaryRamBase; + UINTN StackOffset; + BOOLEAN StackOffsetPositive; + EFI_PHYSICAL_ADDRESS HoleMemBase; + UINTN HoleMemSize; EFI_FV_FILE_INFO FvFileInfo; - UINTN OldCheckingTop; - UINTN OldCheckingBottom; PEI_CORE_FV_HANDLE *CoreFvHandle; VOID *LoadFixPeiCodeBegin; - + EFI_PHYSICAL_ADDRESS TempBase1; + UINTN TempSize1; + EFI_PHYSICAL_ADDRESS TempBase2; + UINTN TempSize2; + UINTN Index; + PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps; PeimEntryPoint = NULL; PeimFileHandle = NULL; EntryPoint = 0; - if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { + if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) { // // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE. @@ -817,8 +799,14 @@ PeiDispatcher ( // // For Fv type file, Produce new FV PPI and FV hob // - Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle); - AuthenticationState = 0; + Status = ProcessFvFile (Private, &Private->Fv[FvCount], PeimFileHandle); + if (Status == EFI_SUCCESS) { + // + // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED + // + Private->Fv[FvCount].PeimState[PeimCount]++; + Private->PeimDispatchOnThisPass = TRUE; + } } else { // // For PEIM driver, Load its entry point @@ -830,50 +818,45 @@ PeiDispatcher ( &EntryPoint, &AuthenticationState ); - } - - if ((Status == EFI_SUCCESS)) { - // - // The PEIM has its dependencies satisfied, and its entry point - // has been found, so invoke it. - // - 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), - (VOID *)(&ExtendedData), - sizeof (ExtendedData) - ); - - Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle); - if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) { + if (Status == EFI_SUCCESS) { // - // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED + // The PEIM has its dependencies satisfied, and its entry point + // has been found, so invoke it. // - Private->Fv[FvCount].PeimState[PeimCount]++; + 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), + (VOID *)(&ExtendedData), + sizeof (ExtendedData) + ); - if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) { + Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle, AuthenticationState); + if (Status != EFI_SECURITY_VIOLATION) { + // + // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED + // + Private->Fv[FvCount].PeimState[PeimCount]++; // // 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; } - Private->PeimDispatchOnThisPass = TRUE; - } - - REPORT_STATUS_CODE_WITH_EXTENDED_DATA ( - EFI_PROGRESS_CODE, - (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN), - (VOID *)(&ExtendedData), - sizeof (ExtendedData) - ); - PERF_END (PeimFileHandle, "PEIM", NULL, 0); + REPORT_STATUS_CODE_WITH_EXTENDED_DATA ( + EFI_PROGRESS_CODE, + (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END), + (VOID *)(&ExtendedData), + sizeof (ExtendedData) + ); + PERF_END (PeimFileHandle, "PEIM", NULL, 0); + } } if (Private->SwitchStackSignal) { @@ -889,13 +872,14 @@ PeiDispatcher ( && (*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", - (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase)) + (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase)) )); DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n", - ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - - (UINTN) Private->HobList.Raw) + (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw) )); DEBUG_CODE_END (); @@ -903,9 +887,10 @@ PeiDispatcher ( // // Loading Module at Fixed Address is enabled // - PeiLoadFixAddressHook(Private); + PeiLoadFixAddressHook (Private); + // - // if Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range. + // 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))); @@ -914,170 +899,197 @@ PeiDispatcher ( // // Reserve the size of new stack at bottom of physical memory // - OldPeiStackSize = (UINT64) SecCoreData->StackSize; - NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; - if (PcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) { - Private->StackSize = NewPeiStackSize; - } else { - Private->StackSize = PcdGet32(PcdPeiCoreMaxPeiStackSize); - } - - // - // In theory, the size of new stack in permenent memory should large than - // size of old stack in temporary memory. + // 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. // - DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (INT32) OldPeiStackSize, (INT32) Private->StackSize)); - ASSERT (Private->StackSize >= OldPeiStackSize); - StackGap = Private->StackSize - OldPeiStackSize; + 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); // - // Update HandOffHob for new installed permenent memory + // Caculate stack offset and heap offset between temporary memory and new permement + // memory seperately. // - OldHandOffTable = Private->HobList.HandoffInformationTable; - OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase); - OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize); + TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize; + TopOfNewStack = Private->PhysicalMemoryBegin + NewStackSize; + 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))); // - // The whole temporary memory will be migrated to physical memory. - // CAUTION: The new base is computed accounding to gap of new stack. + // Build Stack HOB that describes the permanent memory stack // - NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap; - + DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack - NewStackSize, NewStackSize)); + BuildStackHob (TopOfNewStack - NewStackSize, NewStackSize); + // - // Caculate stack offset and heap offset between temporary memory and new permement - // memory seperately. + // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address // - StackOffset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase; - HeapOffset = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \ - (UINTN) SecCoreData->PeiTemporaryRamBase); - DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (INT64)HeapOffset, (INT64)StackOffset)); + TemporaryRamBase = (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->TemporaryRamBase; + TemporaryRamSize = SecCoreData->TemporaryRamSize; + TemporaryStackSize = SecCoreData->StackSize; + TemporaryStackBase = SecCoreData->StackBase; + PeiTemporaryRamSize = SecCoreData->PeiTemporaryRamSize; + PeiTemporaryRamBase = SecCoreData->PeiTemporaryRamBase; - // - // Caculate new HandOffTable and PrivateData address in permenet memory's stack - // - NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset); - PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset); - // // TemporaryRamSupportPpi is produced by platform's SEC // - Status = PeiLocatePpi ( - (CONST EFI_PEI_SERVICES **) PeiServices, + Status = PeiServicesLocatePpi ( &gEfiTemporaryRamSupportPpiGuid, 0, NULL, (VOID**)&TemporaryRamSupportPpi ); - - if (!EFI_ERROR (Status)) { // - // Temporary Ram support Ppi is provided by platform, it will copy + // Heap Offset + // + BaseOfNewHeap = TopOfNewStack; + if (BaseOfNewHeap >= (UINTN)SecCoreData->PeiTemporaryRamBase) { + Private->HeapOffsetPositive = TRUE; + Private->HeapOffset = (UINTN)(BaseOfNewHeap - (UINTN)SecCoreData->PeiTemporaryRamBase); + } else { + Private->HeapOffsetPositive = FALSE; + Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - BaseOfNewHeap); + } + + // + // Caculate new HandOffTable and PrivateData address in permanent memory's stack + // + 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); + } + + // + // Temporary Ram Support PPI is provided by platform, it will copy // temporary memory to permenent memory and do stack switching. - // After invoken temporary Ram support, following code's stack is in - // memory but not in temporary memory. + // After invoking Temporary Ram Support PPI, the following code's + // stack is in permanent memory. // TemporaryRamSupportPpi->TemporaryRamMigration ( - (CONST EFI_PEI_SERVICES **) PeiServices, - (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase, - (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase, - SecCoreData->TemporaryRamSize + PeiServices, + TemporaryRamBase, + (EFI_PHYSICAL_ADDRESS)(UINTN)(TopOfNewStack - TemporaryStackSize), + TemporaryRamSize ); + // + // Entry PEI Phase 2 + // + PeiCore (SecCoreData, NULL, Private); } else { // - // In IA32/x64/Itanium architecture, we need platform provide - // TEMPORAY_RAM_MIGRATION_PPI. + // Heap Offset // - ASSERT (FALSE); - } - - - // - // - // Fixup the PeiCore's private data - // - PrivateInMem->Ps = &PrivateInMem->ServiceTableShadow; - PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo; - PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset); - PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK); - - PeiServices = (CONST EFI_PEI_SERVICES **) &PrivateInMem->Ps; - - // - // Fixup for PeiService's address - // - SetPeiServicesTablePointer(PeiServices); - - // - // Update HandOffHob for new installed permenent memory - // - NewHandOffTable->EfiEndOfHobList = - (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset); - NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin + - PrivateInMem->PhysicalMemoryLength; - NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin; - NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop; - NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList + - sizeof (EFI_HOB_GENERIC_HEADER); - - // - // We need convert the PPI desciptor's pointer - // - ConvertPpiPointers (PrivateInMem, - OldCheckingBottom, - OldCheckingTop, - HeapOffset - ); - - DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", - PrivateInMem->StackBase, - PrivateInMem->StackSize)); - BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize); + BaseOfNewHeap = TopOfNewStack; + HoleMemBase = TopOfNewStack; + HoleMemSize = TemporaryRamSize - PeiTemporaryRamSize - TemporaryStackSize; + if (HoleMemSize != 0) { + // + // Make sure HOB List start address is 8 byte alignment. + // + BaseOfNewHeap = ALIGN_VALUE (BaseOfNewHeap + HoleMemSize, 8); + } + if (BaseOfNewHeap >= (UINTN)SecCoreData->PeiTemporaryRamBase) { + Private->HeapOffsetPositive = TRUE; + Private->HeapOffset = (UINTN)(BaseOfNewHeap - (UINTN)SecCoreData->PeiTemporaryRamBase); + } else { + Private->HeapOffsetPositive = FALSE; + Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - BaseOfNewHeap); + } - // - // After the whole temporary memory is migrated, then we can allocate page in - // permenent memory. - // - PrivateInMem->PeiMemoryInstalled = TRUE; + // + // Migrate Heap + // + CopyMem ((UINT8 *) (UINTN) BaseOfNewHeap, (UINT8 *) PeiTemporaryRamBase, PeiTemporaryRamSize); + + // + // Migrate Stack + // + CopyMem ((UINT8 *) (UINTN) (TopOfNewStack - TemporaryStackSize), TemporaryStackBase, TemporaryStackSize); + + // + // Copy Hole Range Data + // Convert PPI from Hole. + // + if (HoleMemSize != 0) { + // + // Prepare Hole + // + if (PeiTemporaryRamBase < TemporaryStackBase) { + TempBase1 = (EFI_PHYSICAL_ADDRESS) (UINTN) PeiTemporaryRamBase; + TempSize1 = PeiTemporaryRamSize; + TempBase2 = (EFI_PHYSICAL_ADDRESS) (UINTN) TemporaryStackBase; + TempSize2 = TemporaryStackSize; + } else { + TempBase1 = (EFI_PHYSICAL_ADDRESS) (UINTN) TemporaryStackBase; + TempSize1 = TemporaryStackSize; + TempBase2 =(EFI_PHYSICAL_ADDRESS) (UINTN) PeiTemporaryRamBase; + TempSize2 = PeiTemporaryRamSize; + } + if (TemporaryRamBase < TempBase1) { + Private->HoleData[0].Base = TemporaryRamBase; + Private->HoleData[0].Size = (UINTN) (TempBase1 - TemporaryRamBase); + } + if (TempBase1 + TempSize1 < TempBase2) { + Private->HoleData[1].Base = TempBase1 + TempSize1; + Private->HoleData[1].Size = (UINTN) (TempBase2 - TempBase1 - TempSize1); + } + if (TempBase2 + TempSize2 < TemporaryRamBase + TemporaryRamSize) { + Private->HoleData[2].Base = TempBase2 + TempSize2; + Private->HoleData[2].Size = (UINTN) (TemporaryRamBase + TemporaryRamSize - TempBase2 - TempSize2); + } + + // + // Copy Hole Range data. + // + for (Index = 0; Index < HOLE_MAX_NUMBER; Index ++) { + if (Private->HoleData[Index].Size > 0) { + if (HoleMemBase > Private->HoleData[Index].Base) { + Private->HoleData[Index].OffsetPositive = TRUE; + Private->HoleData[Index].Offset = (UINTN) (HoleMemBase - Private->HoleData[Index].Base); + } else { + Private->HoleData[Index].OffsetPositive = FALSE; + Private->HoleData[Index].Offset = (UINTN) (Private->HoleData[Index].Base - HoleMemBase); + } + CopyMem ((VOID *) (UINTN) HoleMemBase, (VOID *) (UINTN) Private->HoleData[Index].Base, Private->HoleData[Index].Size); + HoleMemBase = HoleMemBase + Private->HoleData[Index].Size; + } + } + } - // - // Indicate that PeiCore reenter - // - PrivateInMem->PeimDispatcherReenter = TRUE; - - if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (PrivateInMem->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { // - // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array. - // Every bit in the array indicate the status of the corresponding memory page available or not + // Switch new stack // - PrivateInMem->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64)); + SwitchStack ( + (SWITCH_STACK_ENTRY_POINT)(UINTN)PeiCoreEntry, + (VOID *) SecCoreData, + (VOID *) Private, + (VOID *) (UINTN) TopOfNewStack + ); } - // - // Shadow PEI Core. When permanent memory is avaiable, shadow - // PEI Core and PEIMs to get high performance. - // - PrivateInMem->ShadowedPeiCore = ShadowPeiCore ( - PeiServices, - PrivateInMem - ); - // - // Process the Notify list and dispatch any notifies for - // newly installed PPIs. - // - ProcessNotifyList (PrivateInMem); - - // - // Entry PEI Phase 2 - // - PeiCore (SecCoreData, NULL, PrivateInMem); // // Code should not come here // - ASSERT_EFI_ERROR(FALSE); + ASSERT (FALSE); } // @@ -1087,7 +1099,7 @@ PeiDispatcher ( ProcessNotifyList (Private); if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \ - (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { + (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) { // // 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. @@ -1195,11 +1207,20 @@ DepexSatisfied ( { 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; } @@ -1216,6 +1237,7 @@ DepexSatisfied ( // // If there is no DEPEX, assume the module can be executed // + DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (No DEPEX)\n")); return TRUE; }