Skip to content

Commit

Permalink
Add Memory Attribute Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorBeebe authored and kenlautner committed May 4, 2023
1 parent 66c6c8a commit a521e4f
Show file tree
Hide file tree
Showing 20 changed files with 1,390 additions and 12 deletions.
4 changes: 3 additions & 1 deletion MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include <Protocol/Security2.h>
#include <Protocol/Reset.h>
#include <Protocol/Cpu.h>
#include <Protocol/Cpu2.h> // MS_CHANGE
#include <Protocol/Cpu2.h> // MS_CHANGE
#include <Protocol/MemoryAttribute.h> // MU_CHANGE
#include <Protocol/Metronome.h>
#include <Protocol/FirmwareVolumeBlock.h>
#include <Protocol/Capsule.h>
Expand Down Expand Up @@ -273,6 +274,7 @@ extern EFI_SECURITY_ARCH_PROTOCOL *gSecurity;
extern EFI_SECURITY2_ARCH_PROTOCOL *gSecurity2;
extern EFI_BDS_ARCH_PROTOCOL *gBds;
extern EFI_SMM_BASE2_PROTOCOL *gSmmBase2;
extern EFI_MEMORY_ATTRIBUTE_PROTOCOL *MemoryAttributeProtocol; // MU_CHANGE

extern volatile EFI_TPL gEfiCurrentTpl; // MS_CHANGE

Expand Down
1 change: 1 addition & 0 deletions MdeModulePkg/Core/Dxe/DxeMain.inf
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
gEfiCapsuleArchProtocolGuid ## CONSUMES
gEfiWatchdogTimerArchProtocolGuid ## CONSUMES
gEfiCpu2ProtocolGuid ## SOMETIMES_CONSUMES ## MS_CHANGE
gEfiMemoryAttributeProtocolGuid ## CONSUMES ## MS_CHANGE
gMemoryProtectionSpecialRegionProtocolGuid ## PRODUCES ## MU_CHANGE

[Pcd]
Expand Down
55 changes: 47 additions & 8 deletions MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent

#include <Protocol/FirmwareVolume2.h>
#include <Protocol/SimpleFileSystem.h>
#include <Protocol/HeapGuardDebug.h> // MS_CHANGE
#include <Protocol/MemoryAttribute.h> // MU_CHANGE

#include "DxeMain.h"
#include "Mem/HeapGuard.h"
Expand Down Expand Up @@ -68,6 +68,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent

extern LIST_ENTRY mGcdMemorySpaceMap;

EFI_MEMORY_ATTRIBUTE_PROTOCOL *mMemoryAttribute = NULL; // MU_CHANGE

STATIC LIST_ENTRY mProtectedImageRecordList;
// MS_CHANGE - START
STATIC HEAP_GUARD_DEBUG_PROTOCOL mHeapGuardDebug = {
Expand Down Expand Up @@ -1280,6 +1282,7 @@ CoreInitializeMemoryProtection (
EFI_EVENT Event;
EFI_EVENT DisableNullDetectionEvent;
EFI_EVENT EnableNullDetectionEvent; // MU_CHANGE
EFI_EVENT MemoryAttributeProtocolEvent; // MU_CHANGE
VOID *Registration;

// mImageProtectionPolicy = gDxeMps.ImageProtectionPolicy; // MU_CHANGE
Expand Down Expand Up @@ -1324,6 +1327,26 @@ CoreInitializeMemoryProtection (
);
ASSERT_EFI_ERROR (Status);

// MU_CHANGE START: Register an event to populate the memory attribute protocol
Status = CoreCreateEvent (
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
MemoryAttributeProtocolNotify,
NULL,
&MemoryAttributeProtocolEvent
);
ASSERT_EFI_ERROR (Status);

//
// Register for protocol notification
//
Status = CoreRegisterProtocolNotify (
&gEfiMemoryAttributeProtocolGuid,
MemoryAttributeProtocolEvent,
&Registration
);
ASSERT_EFI_ERROR (Status);
// MU_CHANGE END
//
// Register a callback to disable NULL pointer detection at EndOfDxe
//
Expand Down Expand Up @@ -1508,16 +1531,32 @@ ApplyMemoryProtectionPolicy (
//
NewAttributes = GetPermissionAttributeForMemoryType (NewType);

if (OldType != EfiMaxMemoryType) {
OldAttributes = GetPermissionAttributeForMemoryType (OldType);
if (OldAttributes == NewAttributes) {
// policy is the same between OldType and NewType
// MU_CHANGE START: There is a potential bug where attributes are not properly set
// for all pages during a call to AllocatePages(). This may be due to a bug somewhere
// during the free page process.
// if (OldType != EfiMaxMemoryType) {
// OldAttributes = GetPermissionAttributeForMemoryType (OldType);
// if (OldAttributes == NewAttributes) {
// // policy is the same between OldType and NewType
// return EFI_SUCCESS;
// }
// } else if (NewAttributes == 0) {
// // newly added region of a type that does not require protection
// return EFI_SUCCESS;
// }

// To catch the edge case where the attributes are not consistent across the range, get the
// attributes from the page table to see if they are consistent. If they are not consistent,
// GetMemoryAttributes() will return an error.
if (MemoryAttributeProtocol != NULL) {
if (!EFI_ERROR (MemoryAttributeProtocol->GetMemoryAttributes (MemoryAttributeProtocol, Memory, Length, &OldAttributes)) &&
(OldAttributes == NewAttributes))
{
return EFI_SUCCESS;
}
} else if (NewAttributes == 0) {
// newly added region of a type that does not require protection
return EFI_SUCCESS;
}

// MU_CHANGE END

return gCpu->SetMemoryAttributes (gCpu, Memory, Length, NewAttributes);
}
33 changes: 33 additions & 0 deletions MdeModulePkg/Core/Dxe/Misc/MemoryProtectionSupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ MEMORY_PROTECTION_SPECIAL_REGION_PRIVATE_LIST_HEAD mSpecialMemoryRegionsPrivate
};

BOOLEAN mIsSystemNxCompatible = TRUE;
EFI_MEMORY_ATTRIBUTE_PROTOCOL *MemoryAttributeProtocol = NULL;

#define IS_BITMAP_INDEX_SET(Bitmap, Index) ((((UINT8*)Bitmap)[Index / 8] & (1 << (Index % 8))) != 0 ? TRUE : FALSE)
#define SET_BITMAP_INDEX(Bitmap, Index) (((UINT8*)Bitmap)[Index / 8] |= (1 << (Index % 8)))

Expand Down Expand Up @@ -3061,6 +3063,37 @@ MemoryProtectionCpuArchProtocolNotifyMu (
CoreCloseEvent (Event);
}

/**
A notification for the Memory Attribute Protocol.
@param[in] Event Event whose notification function is being invoked.
@param[in] Context Pointer to the notification function's context,
which is implementation-dependent.
**/
VOID
EFIAPI
MemoryAttributeProtocolNotify (
IN EFI_EVENT Event,
IN VOID *Context
)
{
EFI_STATUS Status;

Status = gBS->LocateProtocol (&gEfiMemoryAttributeProtocolGuid, NULL, (VOID **)&MemoryAttributeProtocol);

if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_INFO,
"%a - Unable to locate the memory attribute protocol! Status = %r\n",
__FUNCTION__,
Status
));
}

CoreCloseEvent (Event);
}

/**
Clears the attributes from a memory range.
Expand Down
15 changes: 15 additions & 0 deletions MdeModulePkg/Core/Dxe/Misc/MemoryProtectionSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ MemoryProtectionCpuArchProtocolNotifyMu (
IN VOID *Context
);

/**
A notification for the Memory Attribute Protocol.
@param[in] Event Event whose notification function is being invoked.
@param[in] Context Pointer to the notification function's context,
which is implementation-dependent.
**/
VOID
EFIAPI
MemoryAttributeProtocolNotify (
IN EFI_EVENT Event,
IN VOID *Context
);

/**
Sets the NX compatibility global to FALSE so future checks to
IsSystemNxCompatible() will return FALSE.
Expand Down
131 changes: 131 additions & 0 deletions MdePkg/Include/Protocol/MemoryAttribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/** @file
TCBZ3519
EFI Memory Attribute Protocol provides retrieval and update service
for memory attributes in EFI environment.
Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef __EFI_MEMORY_ATTRIBUTE_H__
#define __EFI_MEMORY_ATTRIBUTE_H__

#define EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID \
{ \
0xf4560cf6, 0x40ec, 0x4b4a, { 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 } \
}

typedef struct _EFI_MEMORY_ATTRIBUTE_PROTOCOL EFI_MEMORY_ATTRIBUTE_PROTOCOL;

/**
This function set given attributes of the memory region specified by
BaseAddress and Length.
The valid Attributes is EFI_MEMORY_RP, EFI_MEMORY_XP, and EFI_MEMORY_RO.
@param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
@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 Attributes The bit mask of attributes to set for the memory
region.
@retval EFI_SUCCESS The attributes were set for the memory region.
@retval EFI_INVALID_PARAMETER Length is zero.
Attributes specified an illegal combination of
attributes that cannot be set together.
@retval EFI_UNSUPPORTED The processor does not support one or more
bytes of the memory resource range specified
by BaseAddress and Length.
The bit mask of attributes is not supported for
the memory resource range specified by
BaseAddress and Length.
**/
typedef
EFI_STATUS
(EFIAPI *EFI_SET_MEMORY_ATTRIBUTES)(
IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Attributes
);

/**
This function clears given attributes of the memory region specified by
BaseAddress and Length.
The valid Attributes is EFI_MEMORY_RP, EFI_MEMORY_XP, and EFI_MEMORY_RO.
@param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
@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 Attributes The bit mask of attributes to clear for the memory
region.
@retval EFI_SUCCESS The attributes were cleared for the memory region.
@retval EFI_INVALID_PARAMETER Length is zero.
Attributes specified an illegal combination of
attributes that cannot be cleared together.
@retval EFI_UNSUPPORTED The processor does not support one or more
bytes of the memory resource range specified
by BaseAddress and Length.
The bit mask of attributes is not supported for
the memory resource range specified by
BaseAddress and Length.
**/
typedef
EFI_STATUS
(EFIAPI *EFI_CLEAR_MEMORY_ATTRIBUTES)(
IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Attributes
);

/**
This function retrieves the attributes of the memory region specified by
BaseAddress and Length. If different attributes are got from different part
of the memory region, EFI_NO_MAPPING will be returned.
@param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
@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 Attributes Pointer to attributes returned.
@retval EFI_SUCCESS The attributes got for the memory region.
@retval EFI_INVALID_PARAMETER Length is zero.
Attributes is NULL.
@retval EFI_NO_MAPPING Attributes are not consistent cross the memory
region.
@retval EFI_UNSUPPORTED The processor does not support one or more
bytes of the memory resource range specified
by BaseAddress and Length.
**/
typedef
EFI_STATUS
(EFIAPI *EFI_GET_MEMORY_ATTRIBUTES)(
IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
OUT UINT64 *Attributes
);

///
/// EFI Memory Attribute Protocol provides services to retrieve or update
/// attribute of memory in the EFI environment.
///
struct _EFI_MEMORY_ATTRIBUTE_PROTOCOL {
EFI_GET_MEMORY_ATTRIBUTES GetMemoryAttributes;
EFI_SET_MEMORY_ATTRIBUTES SetMemoryAttributes;
EFI_CLEAR_MEMORY_ATTRIBUTES ClearMemoryAttributes;
};

extern EFI_GUID gEfiMemoryAttributeProtocolGuid;

#endif
3 changes: 3 additions & 0 deletions MdePkg/MdePkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,9 @@
## Include/Protocol/RedfishDiscover.h
gEfiRedfishDiscoverProtocolGuid = { 0x5db12509, 0x4550, 0x4347, { 0x96, 0xb3, 0x73, 0xc0, 0xff, 0x6e, 0x86, 0x9f }}

## TCBZ3519 Include/Protocol/MemoryAttribute.h
gEfiMemoryAttributeProtocolGuid = { 0xf4560cf6, 0x40ec, 0x4b4a, { 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 }}

#
# Protocols defined in Shell2.0
#
Expand Down
2 changes: 2 additions & 0 deletions MdePkg/MdePkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@

[Components.IA32, Components.X64, Components.AARCH64]
MdePkg/Library/BaseRngLib/BaseRngLib.inf
# TCBZ3519 MU_CHANGE: UEFI shell test for MemoryAttributeProtocol
MdePkg/Test/ShellTest/MemoryAttributeProtocolFuncTestApp/MemoryAttributeProtocolFuncTestApp.inf

[Components.IA32, Components.X64]
MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
Expand Down
Loading

0 comments on commit a521e4f

Please sign in to comment.