From 6a348b689a0ac8f0c4f65d3b046d5bd414e14bb7 Mon Sep 17 00:00:00 2001 From: Oliver Smith-Denny Date: Wed, 8 Jan 2025 12:39:51 -0800 Subject: [PATCH] MpDxe: Register for cache attribute change event and MTRR calls --- .../Include/Guid/CacheAttributesChangeEvent.h | 18 ++++ MdeModulePkg/MdeModulePkg.dec | 4 + UefiCpuPkg/MpDxe/LoongArch64/MpDxe.c | 3 +- UefiCpuPkg/MpDxe/MpDxe.c | 82 +++++++++++++++++-- UefiCpuPkg/MpDxe/MpDxe.h | 7 +- UefiCpuPkg/MpDxe/MpDxe.inf | 26 +----- UefiCpuPkg/MpDxe/MpDxe.uni | 1 + UefiCpuPkg/MpDxe/MpDxeExtra.uni | 1 + 8 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 MdeModulePkg/Include/Guid/CacheAttributesChangeEvent.h diff --git a/MdeModulePkg/Include/Guid/CacheAttributesChangeEvent.h b/MdeModulePkg/Include/Guid/CacheAttributesChangeEvent.h new file mode 100644 index 0000000000..79e72e9573 --- /dev/null +++ b/MdeModulePkg/Include/Guid/CacheAttributesChangeEvent.h @@ -0,0 +1,18 @@ +/** @file + Event group triggered when caching attributes are updated in the system. + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent + + MU_CHANGE: WHOLE FILE +**/ + +#ifndef CACHE_ATTRIBUTES_CHANGE_EVENT_H_ +#define CACHE_ATTRIBUTES_CHANGE_EVENT_H_ + +#define CACHE_ATTRIBUTES_CHANGE_EVENT_GUID \ + { 0XB8E477C7, 0X26A9, 0X4B9A, { 0XA7, 0XC9, 0X5F, 0X8F, 0X1F, 0X3D, 0X9C, 0X7B } } + +extern EFI_GUID gCacheAttributesChangeEventGuid; + +#endif diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 96eecfff84..ca71686d6a 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -578,6 +578,10 @@ gEdk2JedecSfdpSpiDxeDriverGuid = { 0xBE71701E, 0xB63C, 0x4574, { 0x9C, 0x5C, 0x36, 0x29, 0xE8, 0xEA, 0xC4, 0x14 }} gEdk2JedecSfdpSpiSmmDriverGuid = { 0x95A1E915, 0x195C, 0x477C, { 0x92, 0x6F, 0x7E, 0x24, 0x67, 0xC1, 0xB3, 0x1F }} + # MU_CHANGE START + gCacheAttributesChangeEventGuid = { 0XB8E477C7, 0X26A9, 0X4B9A, { 0XA7, 0XC9, 0X5F, 0X8F, 0X1F, 0X3D, 0X9C, 0X7B } } + # MU_CHANGE END + [Ppis] ## Include/Ppi/FirmwareVolumeShadowPpi.h gEdkiiPeiFirmwareVolumeShadowPpiGuid = { 0x7dfe756c, 0xed8d, 0x4d77, {0x9e, 0xc4, 0x39, 0x9a, 0x8a, 0x81, 0x51, 0x16 } } diff --git a/UefiCpuPkg/MpDxe/LoongArch64/MpDxe.c b/UefiCpuPkg/MpDxe/LoongArch64/MpDxe.c index eddf1947c7..be25604475 100644 --- a/UefiCpuPkg/MpDxe/LoongArch64/MpDxe.c +++ b/UefiCpuPkg/MpDxe/LoongArch64/MpDxe.c @@ -4,6 +4,8 @@ Copyright (c) 2024, Loongson Technology Corporation Limited. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent + + MU_CHANGE: WHOLE FILE **/ #include "MpDxe.h" @@ -543,7 +545,6 @@ InitializeMpSupport ( ASSERT_EFI_ERROR (Status); } - EFI_STATUS EFIAPI InitializeMp ( diff --git a/UefiCpuPkg/MpDxe/MpDxe.c b/UefiCpuPkg/MpDxe/MpDxe.c index 11a0abbe62..c74c054162 100644 --- a/UefiCpuPkg/MpDxe/MpDxe.c +++ b/UefiCpuPkg/MpDxe/MpDxe.c @@ -4,12 +4,16 @@ Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent + MU_CHANGE: WHOLE FILE **/ #include "MpDxe.h" +#include +#include -EFI_HANDLE mMpServiceHandle = NULL; -UINTN mNumberOfProcessors = 1; +EFI_HANDLE mMpServiceHandle = NULL; +UINTN mNumberOfProcessors = 1; +EFI_EVENT mCacheAttributesChangeEvent = NULL; EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = { GetNumberOfProcessors, @@ -21,6 +25,49 @@ EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = { WhoAmI }; +/** + A minimal wrapper function that allows MtrrSetAllMtrrs() to be passed to + EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() as Procedure. + + @param[in] Buffer Pointer to an MTRR_SETTINGS object, to be passed to + MtrrSetAllMtrrs(). +**/ +VOID +EFIAPI +SetMtrrsFromBuffer ( + IN VOID *Buffer + ) +{ + MtrrSetAllMtrrs (Buffer); +} + +// MU_CHANGE START: Cache Attribute Change Event +VOID +EFIAPI +CacheAttributesChangeCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MTRR_SETTINGS MtrrSettings; + + DEBUG ((DEBUG_INFO, "MpDxe updating MTRRs with APs\n")); + + MtrrGetAllMtrrs (&MtrrSettings); + + MpInitLibStartupAllAPs ( + SetMtrrsFromBuffer, + FALSE, + NULL, + 0, + &MtrrSettings, + NULL + ); + // we purposefully don't close the event here, because we want to be called for every update +} + +// MU_CHANGE END: Cache Attribute Change Event + /** This service retrieves the number of logical processor in the platform and the number of those logical processors that are enabled on this boot. @@ -752,14 +799,18 @@ InitializeMpExceptionHandlers ( VOID ) { - // // - // // Enable non-stop mode for #PF triggered by Heap Guard or NULL Pointer - // // Detection. - // // + // MU_CHANGE START - These modes are only for testing - Commenting to untangle + // the dependency on DebugExceptionHandler/PageFaultExceptionHandler + // + // Enable non-stop mode for #PF triggered by Heap Guard or NULL Pointer + // Detection. + // // if (HEAP_GUARD_NONSTOP_MODE || NULL_DETECTION_NONSTOP_MODE) { // RegisterCpuInterruptHandler (EXCEPT_IA32_DEBUG, DebugExceptionHandler); - // RegisterCpuInterruptHandler (EXCEPT_IA32_PAGE_FAULT, PageFaultExceptionHandler); + // RegisterCpuInterruptHandler (EXCEPT_IA32_PAGE_FAULT, + // PageFaultExceptionHandler); // } + // MU_CHANGE END // // Setup stack switch for Stack Guard feature. @@ -816,6 +867,23 @@ InitializeMpSupport ( ); ASSERT_EFI_ERROR (Status); } + + // MU_CHANGE START: CacheAttributesChange Event + // Register for the CacheAttributesChangeEvent + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + CacheAttributesChangeCallback, + NULL, + &gCacheAttributesChangeEventGuid, + &mCacheAttributesChangeEvent + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to create CacheAttributesChangeEvent\n")); + ASSERT_EFI_ERROR (Status); + } + + // MU_CHANGE END: CacheAttributesChange Event } // MU_CHANGE End - CodeQL Change - unguardednullreturndereference diff --git a/UefiCpuPkg/MpDxe/MpDxe.h b/UefiCpuPkg/MpDxe/MpDxe.h index e8c85b5a59..7682563140 100644 --- a/UefiCpuPkg/MpDxe/MpDxe.h +++ b/UefiCpuPkg/MpDxe/MpDxe.h @@ -4,10 +4,11 @@ Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent + MU_CHANGE: WHOLE FILE **/ -#ifndef _MP_DXE_H_ -#define _MP_DXE_H_ +#ifndef MP_DXE_H_ +#define MP_DXE_H_ #include @@ -489,4 +490,4 @@ WhoAmI ( OUT UINTN *ProcessorNumber ); -#endif // _MP_DXE_H_ +#endif // MP_DXE_H_ diff --git a/UefiCpuPkg/MpDxe/MpDxe.inf b/UefiCpuPkg/MpDxe/MpDxe.inf index 6e2417730f..da80485b0b 100644 --- a/UefiCpuPkg/MpDxe/MpDxe.inf +++ b/UefiCpuPkg/MpDxe/MpDxe.inf @@ -7,6 +7,7 @@ # # SPDX-License-Identifier: BSD-2-Clause-Patent # +# MU_CHANGE: WHOLE FILE ## [Defines] @@ -61,35 +62,14 @@ [Protocols] gEfiMpServiceProtocolGuid ## PRODUCES - # gEfiCpuArchProtocolGuid ## CONSUMES - # gEfiMemoryAttributeProtocolGuid ## TCBZ3519 MU_CHANGE PRODUCES - # gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES - # gMemoryProtectionNonstopModeProtocolGuid ## MU_CHANGE: PRODUCES - # gEdkiiGcdSyncCompleteProtocolGuid ## MU_CHANGE: PRODUCES -# gEfiCpuArchProtocolGuid ## PRODUCES -# [Guids] -# gIdleLoopEventGuid ## CONSUMES ## Event -# gEfiVectorHandoffTableGuid ## SOMETIMES_CONSUMES ## SystemTable +[Guids] + gCacheAttributesChangeEventGuid ## CONSUMES [Ppis] gEfiSecPlatformInformation2PpiGuid ## UNDEFINED # HOB gEfiSecPlatformInformationPpiGuid ## UNDEFINED # HOB -# [Pcd] -# gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask ## CONSUMES -# # MU_CHANGE START Remove memory protection PCD references -# # gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ## CONSUMES -# # gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ## CONSUMES -# # gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask ## CONSUMES -# # MU_CHANGE END -# gEfiMdeModulePkgTokenSpaceGuid.PcdTdxSharedBitMask ## CONSUMES -# gUefiCpuPkgTokenSpaceGuid.PcdCpuStackSwitchExceptionList ## CONSUMES -# gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize ## CONSUMES - -# [Pcd.LoongArch64] -# gUefiCpuPkgTokenSpaceGuid.PcdLoongArchExceptionVectorBaseAddress ## CONSUMES - [Depex] gEfiCpuArchProtocolGuid diff --git a/UefiCpuPkg/MpDxe/MpDxe.uni b/UefiCpuPkg/MpDxe/MpDxe.uni index 7da1ae4ec3..9fb1d1c75c 100644 --- a/UefiCpuPkg/MpDxe/MpDxe.uni +++ b/UefiCpuPkg/MpDxe/MpDxe.uni @@ -7,6 +7,7 @@ // // SPDX-License-Identifier: BSD-2-Clause-Patent // +// MU_CHANGE: WHOLE FILE // **/ diff --git a/UefiCpuPkg/MpDxe/MpDxeExtra.uni b/UefiCpuPkg/MpDxe/MpDxeExtra.uni index 8ac0e05dbd..3eac158a85 100644 --- a/UefiCpuPkg/MpDxe/MpDxeExtra.uni +++ b/UefiCpuPkg/MpDxe/MpDxeExtra.uni @@ -5,6 +5,7 @@ // // SPDX-License-Identifier: BSD-2-Clause-Patent // +// MU_CHANGE: WHOLE FILE // **/ #string STR_PROPERTIES_MODULE_NAME