Implements SmmCorePlatformHookLib library for AMD platform. Cc: Abner Chang <abner.ch...@amd.com> Cc: Paul Grimes <paul.gri...@amd.com> Signed-off-by: Abdul Lateef Attar <abdullateef.at...@amd.com> --- .../AMD/AmdPlatformPkg/AmdPlatformPkg.dec | 5 + .../AMD/AmdPlatformPkg/AmdPlatformPkg.dsc | 6 + .../Library/AmdSmmCorePlatformHookLib.h | 68 +++++++ .../SmmCorePlatformHookLib.c | 169 ++++++++++++++++++ .../SmmCorePlatformHookLib.inf | 39 ++++ .../SmmCorePlatformHookLib.uni | 11 ++ 6 files changed, 298 insertions(+) create mode 100644 Platform/AMD/AmdPlatformPkg/Include/Library/AmdSmmCorePlatformHookLib.h create mode 100644 Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.c create mode 100644 Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.inf create mode 100644 Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.uni
diff --git a/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dec b/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dec index 106820dc85..3020e628a3 100644 --- a/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dec +++ b/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dec @@ -21,6 +21,11 @@ ## @libraryclass Defines a get/set interface for platform specific data. PlatformSocLib|Include/Library/AmdPlatformSocLib.h + ## @libraryclass AMD SMM core platform hook library + # Provide the functions to register hook before and + # after SMM dispatcher. + SmmCorePlatformHookLib|Include/Library/AmdSmmCorePlatformHookLib.h + [Guids] gAmdPlatformPkgTokenSpaceGuid = { 0x663DE733, 0x70E0, 0x4D37, { 0xBB, 0x30, 0x7D, 0x9E, 0xAF, 0x9B, 0xDA, 0xE9 }} diff --git a/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dsc b/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dsc index d11d3594e3..482e6f2f30 100644 --- a/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dsc +++ b/Platform/AMD/AmdPlatformPkg/AmdPlatformPkg.dsc @@ -62,6 +62,9 @@ MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf PlatformSocLib|AmdPlatformPkg/Library/DxePlatformSocLib/DxePlatformSocLibNull.inf +[LibraryClasses.common.SMM_CORE] + SmmCorePlatformHookLib|AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.inf + [Components] AmdPlatformPkg/Library/BaseAlwaysFalseDepexLib/BaseAlwaysFalseDepexLib.inf AmdPlatformPkg/Library/DxePlatformSocLib/DxePlatformSocLibNull.inf @@ -73,3 +76,6 @@ AmdPlatformPkg/Universal/LogoDxe/LogoDxe.inf # Server platfrom Bitmap logo driver AmdPlatformPkg/Universal/LogoDxe/S3LogoDxe.inf AmdPlatformPkg/Universal/SmbiosCommonDxe/SmbiosCommonDxe.inf + +[Components.common.SMM_CORE] + AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.inf diff --git a/Platform/AMD/AmdPlatformPkg/Include/Library/AmdSmmCorePlatformHookLib.h b/Platform/AMD/AmdPlatformPkg/Include/Library/AmdSmmCorePlatformHookLib.h new file mode 100644 index 0000000000..6ae76920b4 --- /dev/null +++ b/Platform/AMD/AmdPlatformPkg/Include/Library/AmdSmmCorePlatformHookLib.h @@ -0,0 +1,68 @@ +/** @file + AMD Smm Core Platform Hook Library + + Copyright (C) 2023 - 2024 Advanced Micro Devices, Inc. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef AMD_SMM_CORE_PLATFORM_HOOK_LIB_ +#define AMD_SMM_CORE_PLATFORM_HOOK_LIB_ + +/** + This is the prototype of SMM Dispatcher hook before. + + @retval EFI_STATUS + +**/ +typedef +EFI_STATUS +(EFIAPI *AMD_SMM_DISPATCH_HOOK_BEFORE)( + VOID + ); + +/** + This is the prototype of SMM Dispatcher after before. + + @retval EFI_STATUS + +**/ +typedef +EFI_STATUS +(EFIAPI *AMD_SMM_DISPATCH_HOOK_AFTER)( + VOID + ); + +typedef UINT32 AMD_SMM_DISPATCH_HOOK_PRIORITY; + +/// +/// This is the structure of the SMM Dispatcher hook record +/// +typedef struct { + LIST_ENTRY NextList; ///< Point to next AMD_SMM_DISPATCH_HOOK. + AMD_SMM_DISPATCH_HOOK_BEFORE AmdSmmDispatchHookBefore; ///< The hook before function. + AMD_SMM_DISPATCH_HOOK_AFTER AmdSmmDispatchHookAfter; ///< The hook after function. + AMD_SMM_DISPATCH_HOOK_PRIORITY Priority; ///< The priority of this hook instance. + ///< Priority could be implemented for + ///< future usage. +} AMD_SMM_DISPATCH_HOOK; + +/** + Register a SMM dispatcher hook. + + @param[in] SmmDispatchHookBefore Function hook to SMM Dispatch before. + @param[in] SmmDispatchHookAfter Function hook to SMM Dispatch after. + @param[in] Priority The priority to execute the hook. + + @retval EFI_SUCCESS The hook is registered successfully. + +**/ +EFI_STATUS +RegisterSmmDispatcherHook ( + IN AMD_SMM_DISPATCH_HOOK_BEFORE SmmDispatchHookBefore OPTIONAL, + IN AMD_SMM_DISPATCH_HOOK_AFTER SmmDispatchHookAfter OPTIONAL, + IN AMD_SMM_DISPATCH_HOOK_PRIORITY Priority + ); + +#endif // AMD_SMM_CORE_PLATFORM_HOOK_LIB_ diff --git a/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.c b/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.c new file mode 100644 index 0000000000..ad6a40749f --- /dev/null +++ b/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.c @@ -0,0 +1,169 @@ +/** @file + AMD SMM core hook library + + Copyright (C) 2023 - 2024 Advanced Micro Devices, Inc. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include <PiSmm.h> +#include <Library/SmmServicesTableLib.h> +#include <Library/BaseLib.h> +#include <Library/DebugLib.h> +#include <Library/BaseMemoryLib.h> +#include <Library/MemoryAllocationLib.h> +#include <Library/SmmCorePlatformHookLib.h> +#include <Library/AmdSmmCorePlatformHookLib.h> + +LIST_ENTRY *RegisteredHookPtr = NULL; +LIST_ENTRY RegisteredHook; + +/** + Register a SMM dispatcher hook. + + @param[in] SmmDispatchHookBefore Function hook to SMM Dispatch before. + @param[in] SmmDispatchHookAfter Function hook to SMM Dispatch after. + @param[in] Priority The priority to execute the hook. + + @retval EFI_SUCCESS The hook is registered successfully. + +**/ +EFI_STATUS +RegisterSmmDispatcherHook ( + IN AMD_SMM_DISPATCH_HOOK_BEFORE SmmDispatchHookBefore OPTIONAL, + IN AMD_SMM_DISPATCH_HOOK_AFTER SmmDispatchHookAfter OPTIONAL, + IN AMD_SMM_DISPATCH_HOOK_PRIORITY Priority + ) +{ + AMD_SMM_DISPATCH_HOOK *ThisHook; + + if ((SmmDispatchHookBefore == NULL) && (SmmDispatchHookAfter == NULL)) { + DEBUG ((DEBUG_ERROR, "%a: Both SmmDispatchHookBefore and SmmDispatchHookAfter are NULL.\n", __func__)); + return EFI_INVALID_PARAMETER; + } + + if (RegisteredHookPtr == NULL) { + InitializeListHead (&RegisteredHook); + RegisteredHookPtr = &RegisteredHook; + } + + ThisHook = (AMD_SMM_DISPATCH_HOOK *)AllocateZeroPool (sizeof (AMD_SMM_DISPATCH_HOOK)); + if (ThisHook == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Insufficient memory for AMD_SMM_DISPATCH_HOOK.\n", __func__)); + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (&ThisHook->NextList); + ThisHook->AmdSmmDispatchHookBefore = SmmDispatchHookBefore; + ThisHook->AmdSmmDispatchHookAfter = SmmDispatchHookAfter; + ThisHook->Priority = Priority; + InsertHeadList (&RegisteredHook, &ThisHook->NextList); + DEBUG ((DEBUG_VERBOSE, "%a: New AMD_SMM_DISPATCH_HOOK is inserted.\n", __func__)); + return EFI_SUCCESS; +} + +/** + Performs platform specific tasks before invoking registered SMI handlers. + + This function performs platform specific tasks before invoking registered SMI handlers. + + @retval EFI_SUCCESS The platform hook completes successfully. + @retval Other values The platform hook cannot complete due to some error. + +**/ +EFI_STATUS +EFIAPI +PlatformHookBeforeSmmDispatch ( + VOID + ) +{ + AMD_SMM_DISPATCH_HOOK *ThisHook; + + if (IsListEmpty (&RegisteredHook)) { + return EFI_NOT_FOUND; + } + + ThisHook = (AMD_SMM_DISPATCH_HOOK *)GetFirstNode (&RegisteredHook); + while (TRUE) { + // + // We can handle priority in the future when needed. + // + if (ThisHook->AmdSmmDispatchHookBefore != NULL) { + ThisHook->AmdSmmDispatchHookBefore (); + } + + if (IsNodeAtEnd (&RegisteredHook, &ThisHook->NextList)) { + break; + } + + ThisHook = (AMD_SMM_DISPATCH_HOOK *)GetNextNode (&RegisteredHook, &ThisHook->NextList); + } + + return EFI_SUCCESS; +} + +/** + Performs platform specific tasks after invoking registered SMI handlers. + + This function performs platform specific tasks after invoking registered SMI handlers. + + @retval EFI_SUCCESS The platform hook completes successfully. + @retval Other values The platform hook cannot complete due to some error. + +**/ +EFI_STATUS +EFIAPI +PlatformHookAfterSmmDispatch ( + VOID + ) +{ + AMD_SMM_DISPATCH_HOOK *ThisHook; + + if (IsListEmpty (&RegisteredHook)) { + return EFI_NOT_FOUND; + } + + ThisHook = (AMD_SMM_DISPATCH_HOOK *)GetFirstNode (&RegisteredHook); + while (TRUE) { + // + // We can handle priority in the future when needed. + // + if (ThisHook->AmdSmmDispatchHookAfter != NULL) { + ThisHook->AmdSmmDispatchHookAfter (); + } + + if (IsNodeAtEnd (&RegisteredHook, &ThisHook->NextList)) { + break; + } + + ThisHook = (AMD_SMM_DISPATCH_HOOK *)GetNextNode (&RegisteredHook, &ThisHook->NextList); + } + + return EFI_SUCCESS; +} + +/** + Constructor for SmmLockBox library. + This is used to set SmmLockBox context, which will be used in PEI phase in S3 boot path later. + + @param[in] ImageHandle Image handle of this driver. + @param[in] SystemTable A Pointer to the EFI System Table. + + @retval EFI_SUCCESS + @return Others Some error occurs. +**/ +EFI_STATUS +EFIAPI +SmmCorePlatformHookConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + if (RegisteredHookPtr == NULL) { + InitializeListHead (&RegisteredHook); + RegisteredHookPtr = &RegisteredHook; + } + + return EFI_SUCCESS; +} diff --git a/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.inf b/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.inf new file mode 100644 index 0000000000..a79fc56077 --- /dev/null +++ b/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.inf @@ -0,0 +1,39 @@ +## @file +# INF of SMM Core hook library INF file. +# +# Copyright (C) 2023 - 2024 Advanced Micro Devices, Inc. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +## +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SmmCorePlatformHookLib + MODULE_UNI_FILE = SmmCorePlatformHookLib.uni + FILE_GUID = 70ACB6CA-80D2-46a9-A0EB-0C83DFC476C9 + MODULE_TYPE = SMM_CORE + VERSION_STRING = 1.0 + PI_SPECIFICATION_VERSION = 0x0001000A + LIBRARY_CLASS = SmmCorePlatformHookLib|SMM_CORE + CONSTRUCTOR = SmmCorePlatformHookConstructor + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SmmCorePlatformHookLib.c + +[Packages] + AmdPlatformPkg/AmdPlatformPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + SmmServicesTableLib + diff --git a/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.uni b/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.uni new file mode 100644 index 0000000000..0b326c159b --- /dev/null +++ b/Platform/AMD/AmdPlatformPkg/Library/SmmCorePlatformHookLib/SmmCorePlatformHookLib.uni @@ -0,0 +1,11 @@ +## @file +# UNI file of SMM Core hook library module +# +# Copyright (C) 2023 - 2024 Advanced Micro Devices, Inc. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## +#string STR_MODULE_ABSTRACT #language en-US "SMM Core Platform Hook Library instance" + +#string STR_MODULE_DESCRIPTION #language en-US "SMM Core Platform Hook Library instance" -- 2.34.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#119307): https://edk2.groups.io/g/devel/message/119307 Mute This Topic: https://groups.io/mt/106349081/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-