From 771ee5017be405df3a05ae8dce67552cab3b18b1 Mon Sep 17 00:00:00 2001 From: Eugene Cohen Date: Thu, 20 Nov 2014 01:23:49 +0000 Subject: [PATCH] MdePkg/MdeModulePkg: Implement the missing SetMemorySpaceCapabilities function. It is defined in the PI Specification version 1.3. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Eugene Cohen Reviewed-by: Liming Gao git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16409 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Core/Dxe/DxeMain.h | 50 +++++++- MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c | 1 + MdeModulePkg/Core/Dxe/Gcd/Gcd.c | 68 ++++++++++- MdeModulePkg/Core/Dxe/Gcd/Gcd.h | 23 ++-- MdeModulePkg/Core/Dxe/Mem/Page.c | 147 ++++++++++++++++++------ MdePkg/Include/Pi/PiDxeCis.h | 33 +++++- 6 files changed, 272 insertions(+), 50 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMain.h index bb2bfab370..059a38336a 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -1863,7 +1863,7 @@ CoreGetMemorySpaceDescriptor ( resource range specified by BaseAddress and Length. @retval EFI_UNSUPPORTED The bit mask of attributes is not support for the memory resource range specified by BaseAddress and Length. - @retval EFI_ACCESS_DEFINED The attributes for the memory resource range specified by + @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by BaseAddress and Length cannot be modified. @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of the memory resource range. @@ -1880,6 +1880,32 @@ CoreSetMemorySpaceAttributes ( ); +/** + Modifies the capabilities for a memory region in the global coherency domain of the + processor. + + @param BaseAddress The physical address that is the start address of a memory region. + @param Length The size in bytes of the memory region. + @param Capabilities The bit mask of capabilities that the memory region supports. + + @retval EFI_SUCCESS The capabilities were set for the memory region. + @retval EFI_INVALID_PARAMETER Length is zero. + @retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the + memory region attributes currently in use. + @retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by + BaseAddress and Length cannot be modified. + @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities + of the memory resource range. +**/ +EFI_STATUS +EFIAPI +CoreSetMemorySpaceCapabilities ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Capabilities + ); + + /** Returns a map of the memory resources in the global coherency domain of the processor. @@ -2816,4 +2842,26 @@ CoreUpdateProfile ( IN VOID *Buffer ); +/** + Internal function. Converts a memory range to use new attributes. + + @param Start The first address of the range Must be page + aligned + @param NumberOfPages The number of pages to convert + @param NewAttributes The new attributes value for the range. + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_NOT_FOUND Could not find a descriptor cover the specified + range or convertion not allowed. + @retval EFI_SUCCESS Successfully converts the memory range to the + specified attributes. + +**/ +VOID +CoreUpdateMemoryAttributes ( + IN EFI_PHYSICAL_ADDRESS Start, + IN UINT64 NumberOfPages, + IN UINT64 NewAttributes + ); + #endif diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c index 1ac5cc1721..9b03509e6b 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c +++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c @@ -123,6 +123,7 @@ EFI_DXE_SERVICES mDxeServices = { (EFI_SCHEDULE) CoreSchedule, // Schedule (EFI_TRUST) CoreTrust, // Trust (EFI_PROCESS_FIRMWARE_VOLUME) CoreProcessFirmwareVolume, // ProcessFirmwareVolume + (EFI_SET_MEMORY_SPACE_CAPABILITIES)CoreSetMemorySpaceCapabilities, // SetMemorySpaceCapabilities }; EFI_SYSTEM_TABLE mEfiSystemTableTemplate = { diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c index fe2ac1011d..7268465f36 100644 --- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c +++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c @@ -3,7 +3,7 @@ The GCD services are used to manage the memory and I/O regions that are accessible to the CPU that is executing the DXE core. -Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2014, 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 @@ -798,7 +798,7 @@ CoreConvertSpace ( } break; // - // Set attribute operations + // Set attributes operation // case GCD_SET_ATTRIBUTES_MEMORY_OPERATION: if ((Attributes & EFI_MEMORY_RUNTIME) != 0) { @@ -812,6 +812,23 @@ CoreConvertSpace ( goto Done; } break; + // + // Set capabilities operation + // + case GCD_SET_CAPABILITIES_MEMORY_OPERATION: + if ((BaseAddress & EFI_PAGE_MASK) != 0 || (Length & EFI_PAGE_MASK) != 0) { + Status = EFI_INVALID_PARAMETER; + + goto Done; + } + // + // Current attributes must still be supported with new capabilities + // + if ((Capabilities & Entry->Attributes) != Entry->Attributes) { + Status = EFI_UNSUPPORTED; + goto Done; + } + break; } Link = Link->ForwardLink; } @@ -891,11 +908,17 @@ CoreConvertSpace ( Entry->GcdIoType = EfiGcdIoTypeNonExistent; break; // - // Set attribute operations + // Set attributes operation // case GCD_SET_ATTRIBUTES_MEMORY_OPERATION: Entry->Attributes = Attributes; break; + // + // Set capabilities operation + // + case GCD_SET_CAPABILITIES_MEMORY_OPERATION: + Entry->Capabilities = Capabilities; + break; } Link = Link->ForwardLink; } @@ -1558,6 +1581,45 @@ CoreSetMemorySpaceAttributes ( } +/** + Modifies the capabilities for a memory region in the global coherency domain of the + processor. + + @param BaseAddress The physical address that is the start address of a memory region. + @param Length The size in bytes of the memory region. + @param Capabilities The bit mask of capabilities that the memory region supports. + + @retval EFI_SUCCESS The capabilities were set for the memory region. + @retval EFI_INVALID_PARAMETER Length is zero. + @retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the + memory region attributes currently in use. + @retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by + BaseAddress and Length cannot be modified. + @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities + of the memory resource range. +**/ +EFI_STATUS +EFIAPI +CoreSetMemorySpaceCapabilities ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Capabilities + ) +{ + EFI_STATUS Status; + + DEBUG ((DEBUG_GCD, "GCD:CoreSetMemorySpaceCapabilities(Base=%016lx,Length=%016lx)\n", BaseAddress, Length)); + DEBUG ((DEBUG_GCD, " Capabilities = %016lx\n", Capabilities)); + + Status = CoreConvertSpace (GCD_SET_CAPABILITIES_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, Capabilities, 0); + if (!EFI_ERROR(Status)) { + CoreUpdateMemoryAttributes(BaseAddress, RShiftU64(Length, EFI_PAGE_SHIFT), Capabilities); + } + + return Status; +} + + /** Returns a map of the memory resources in the global coherency domain of the processor. diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.h b/MdeModulePkg/Core/Dxe/Gcd/Gcd.h index b63291529f..1d5fb61092 100644 --- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.h +++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.h @@ -2,7 +2,7 @@ GCD Operations and data structure used to convert from GCD attributes to EFI Memory Map attributes. -Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2014, 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 @@ -22,16 +22,17 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define GCD_MEMORY_SPACE_OPERATION 0x20 #define GCD_IO_SPACE_OPERATION 0x40 -#define GCD_ADD_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 0) -#define GCD_ALLOCATE_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 1) -#define GCD_FREE_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 2) -#define GCD_REMOVE_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 3) -#define GCD_SET_ATTRIBUTES_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 4) - -#define GCD_ADD_IO_OPERATION (GCD_IO_SPACE_OPERATION | 0) -#define GCD_ALLOCATE_IO_OPERATION (GCD_IO_SPACE_OPERATION | 1) -#define GCD_FREE_IO_OPERATION (GCD_IO_SPACE_OPERATION | 2) -#define GCD_REMOVE_IO_OPERATION (GCD_IO_SPACE_OPERATION | 3) +#define GCD_ADD_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 0) +#define GCD_ALLOCATE_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 1) +#define GCD_FREE_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 2) +#define GCD_REMOVE_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 3) +#define GCD_SET_ATTRIBUTES_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 4) +#define GCD_SET_CAPABILITIES_MEMORY_OPERATION (GCD_MEMORY_SPACE_OPERATION | 5) + +#define GCD_ADD_IO_OPERATION (GCD_IO_SPACE_OPERATION | 0) +#define GCD_ALLOCATE_IO_OPERATION (GCD_IO_SPACE_OPERATION | 1) +#define GCD_FREE_IO_OPERATION (GCD_IO_SPACE_OPERATION | 2) +#define GCD_REMOVE_IO_OPERATION (GCD_IO_SPACE_OPERATION | 3) // // The data structure used to convert from GCD attributes to EFI Memory Map attributes diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index 18780070c5..64967259a2 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -671,13 +671,17 @@ CoreAddMemoryDescriptor ( /** - Internal function. Converts a memory range to the specified type. - The range must exist in the memory map. + Internal function. Converts a memory range to the specified type or attributes. + The range must exist in the memory map. Either ChangingType or + ChangingAttributes must be set, but not both. @param Start The first address of the range Must be page aligned @param NumberOfPages The number of pages to convert + @param ChangingType Boolean indicating that type value should be changed @param NewType The new type for the memory range + @param ChangingAttributes Boolean indicating that attributes value should be changed + @param NewAttributes The new attributes for the memory range @retval EFI_INVALID_PARAMETER Invalid parameter @retval EFI_NOT_FOUND Could not find a descriptor cover the specified @@ -687,10 +691,13 @@ CoreAddMemoryDescriptor ( **/ EFI_STATUS -CoreConvertPages ( +CoreConvertPagesEx ( IN UINT64 Start, IN UINT64 NumberOfPages, - IN EFI_MEMORY_TYPE NewType + IN BOOLEAN ChangingType, + IN EFI_MEMORY_TYPE NewType, + IN BOOLEAN ChangingAttributes, + IN UINT64 NewAttributes ) { @@ -698,6 +705,7 @@ CoreConvertPages ( UINT64 End; UINT64 RangeEnd; UINT64 Attribute; + EFI_MEMORY_TYPE MemType; LIST_ENTRY *Link; MEMORY_MAP *Entry; @@ -709,6 +717,7 @@ CoreConvertPages ( ASSERT ((Start & EFI_PAGE_MASK) == 0); ASSERT (End > Start) ; ASSERT_LOCKED (&gMemoryLock); + ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) ); if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start > (Start + NumberOfBytes))) { return EFI_INVALID_PARAMETER; @@ -747,36 +756,43 @@ CoreConvertPages ( RangeEnd = Entry->End; } - DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType)); - - // - // Debug code - verify conversion is allowed - // - if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) { - DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n")); - return EFI_NOT_FOUND; + if (ChangingType) { + DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType)); + } + if (ChangingAttributes) { + DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes)); } - // - // Update counters for the number of pages allocated to each memory type - // - if ((UINT32)Entry->Type < EfiMaxMemoryType) { - if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) || - (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) { - if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) { - mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0; - } else { - mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages; + if (ChangingType) { + // + // Debug code - verify conversion is allowed + // + if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) { + DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n")); + return EFI_NOT_FOUND; + } + + // + // Update counters for the number of pages allocated to each memory type + // + if ((UINT32)Entry->Type < EfiMaxMemoryType) { + if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) || + (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) { + if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) { + mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0; + } else { + mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages; + } } } - } - if ((UINT32)NewType < EfiMaxMemoryType) { - if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) || - (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) { - mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages; - if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) { - gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages; + if ((UINT32)NewType < EfiMaxMemoryType) { + if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) || + (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) { + mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages; + if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) { + gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages; + } } } } @@ -830,9 +846,15 @@ CoreConvertPages ( // // The new range inherits the same Attribute as the Entry - //it is being cut out of + // it is being cut out of unless attributes are being changed // - Attribute = Entry->Attribute; + if (ChangingType) { + Attribute = Entry->Attribute; + MemType = NewType; + } else { + Attribute = NewAttributes; + MemType = Entry->Type; + } // // If the descriptor is empty, then remove it from the map @@ -845,8 +867,8 @@ CoreConvertPages ( // // Add our new range in // - CoreAddRange (NewType, Start, RangeEnd, Attribute); - if (NewType == EfiConventionalMemory) { + CoreAddRange (MemType, Start, RangeEnd, Attribute); + if (ChangingType && (MemType == EfiConventionalMemory)) { // // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees @@ -880,6 +902,65 @@ CoreConvertPages ( } +/** + Internal function. Converts a memory range to the specified type. + The range must exist in the memory map. + + @param Start The first address of the range Must be page + aligned + @param NumberOfPages The number of pages to convert + @param NewType The new type for the memory range + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_NOT_FOUND Could not find a descriptor cover the specified + range or convertion not allowed. + @retval EFI_SUCCESS Successfully converts the memory range to the + specified type. + +**/ +EFI_STATUS +CoreConvertPages ( + IN UINT64 Start, + IN UINT64 NumberOfPages, + IN EFI_MEMORY_TYPE NewType + ) +{ + return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0); +} + + +/** + Internal function. Converts a memory range to use new attributes. + + @param Start The first address of the range Must be page + aligned + @param NumberOfPages The number of pages to convert + @param NewAttributes The new attributes value for the range. + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_NOT_FOUND Could not find a descriptor cover the specified + range or convertion not allowed. + @retval EFI_SUCCESS Successfully converts the memory range to the + specified attributes. + +**/ +VOID +CoreUpdateMemoryAttributes ( + IN EFI_PHYSICAL_ADDRESS Start, + IN UINT64 NumberOfPages, + IN UINT64 NewAttributes + ) +{ + CoreAcquireMemoryLock (); + + // + // Update the attributes to the new value + // + CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes); + + CoreReleaseMemoryLock (); +} + /** Internal function. Finds a consecutive free page range below diff --git a/MdePkg/Include/Pi/PiDxeCis.h b/MdePkg/Include/Pi/PiDxeCis.h index 9bcfecc046..a3d57b16eb 100644 --- a/MdePkg/Include/Pi/PiDxeCis.h +++ b/MdePkg/Include/Pi/PiDxeCis.h @@ -1,7 +1,7 @@ /** @file Include file matches things in PI. -Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2014, 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 that accompanies this distribution. The full text of the license may be found at @@ -365,7 +365,7 @@ EFI_STATUS resource range specified by BaseAddress and Length. @retval EFI_UNSUPPORTED The bit mask of attributes is not support for the memory resource range specified by BaseAddress and Length. - @retval EFI_ACCESS_DEFINED The attributes for the memory resource range specified by + @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by BaseAddress and Length cannot be modified. @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of the memory resource range. @@ -380,6 +380,31 @@ EFI_STATUS IN UINT64 Attributes ); +/** + Modifies the capabilities for a memory region in the global coherency domain of the + processor. + + @param BaseAddress The physical address that is the start address of a memory region. + @param Length The size in bytes of the memory region. + @param Capabilities The bit mask of capabilities that the memory region supports. + + @retval EFI_SUCCESS The capabilities were set for the memory region. + @retval EFI_INVALID_PARAMETER Length is zero. + @retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the + memory region attributes currently in use. + @retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by + BaseAddress and Length cannot be modified. + @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities + of the memory resource range. +**/ +typedef +EFI_STATUS +(EFIAPI *EFI_SET_MEMORY_SPACE_CAPABILITIES) ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Capabilities + ); + /** Returns a map of the memory resources in the global coherency domain of the processor. @@ -694,6 +719,10 @@ typedef struct { // Service to process a single firmware volume found in a capsule // EFI_PROCESS_FIRMWARE_VOLUME ProcessFirmwareVolume; + // + // Extensions to Global Coherency Domain Services + // + EFI_SET_MEMORY_SPACE_CAPABILITIES SetMemorySpaceCapabilities; } DXE_SERVICES; typedef DXE_SERVICES EFI_DXE_SERVICES; -- 2.39.2