Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Armpkg mmulib eager mappings #6427

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions ArmPkg/Drivers/CpuDxe/CpuDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,60 @@ RemapUnusedMemoryNx (
ArmSetMemoryAttributes (
MemoryMapEntry->PhysicalStart,
EFI_PAGES_TO_SIZE (MemoryMapEntry->NumberOfPages),
EFI_MEMORY_XP,
EFI_MEMORY_XP
0,
0,
TRUE
);
}

MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
}

FreePool (MemoryMap);

MemoryMapSize = 0;
MemoryMap = NULL;

Status = gBS->GetMemoryMap (
&MemoryMapSize,
MemoryMap,
&MapKey,
&DescriptorSize,
&DescriptorVersion
);
ASSERT (Status == EFI_BUFFER_TOO_SMALL);
do {
MemoryMap = (EFI_MEMORY_DESCRIPTOR *)AllocatePool (MemoryMapSize);
ASSERT (MemoryMap != NULL);
Status = gBS->GetMemoryMap (
&MemoryMapSize,
MemoryMap,
&MapKey,
&DescriptorSize,
&DescriptorVersion
);
if (EFI_ERROR (Status)) {
FreePool (MemoryMap);
}
} while (Status == EFI_BUFFER_TOO_SMALL);

MemoryMapEntry = MemoryMap;
MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + MemoryMapSize);
while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {
if (MemoryMapEntry->Type == EfiConventionalMemory) {
ArmSetMemoryAttributes (
MemoryMapEntry->PhysicalStart,
EFI_PAGES_TO_SIZE (MemoryMapEntry->NumberOfPages),
EFI_MEMORY_RO | EFI_MEMORY_XP,
EFI_MEMORY_RO | EFI_MEMORY_XP,
FALSE
);
}

MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
}

FreePool (MemoryMap);
}

EFI_STATUS
Expand Down
2 changes: 1 addition & 1 deletion ArmPkg/Drivers/CpuDxe/CpuMmuCommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ CpuSetMemoryAttributes (
if (EFI_ERROR (Status) || (RegionArmAttributes != ArmAttributes) ||
((BaseAddress + Length) > (RegionBaseAddress + RegionLength)))
{
return ArmSetMemoryAttributes (BaseAddress, Length, EfiAttributes, 0);
return ArmSetMemoryAttributes (BaseAddress, Length, EfiAttributes, 0, FALSE);
} else {
return EFI_SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions ArmPkg/Drivers/CpuDxe/MemoryAttribute.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ SetMemoryAttributes (
return EFI_UNSUPPORTED;
}

return ArmSetMemoryAttributes (BaseAddress, Length, Attributes, Attributes);
return ArmSetMemoryAttributes (BaseAddress, Length, Attributes, Attributes, FALSE);
}

/**
Expand Down Expand Up @@ -284,7 +284,7 @@ ClearMemoryAttributes (
return EFI_UNSUPPORTED;
}

return ArmSetMemoryAttributes (BaseAddress, Length, 0, Attributes);
return ArmSetMemoryAttributes (BaseAddress, Length, 0, Attributes, FALSE);
}

EFI_MEMORY_ATTRIBUTE_PROTOCOL mMemoryAttribute = {
Expand Down
2 changes: 1 addition & 1 deletion ArmPkg/Drivers/CpuPei/CpuPei.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SetMemoryPermissions (
return EFI_INVALID_PARAMETER;
}

return ArmSetMemoryAttributes (BaseAddress, Length, Attributes, AttributeMask);
return ArmSetMemoryAttributes (BaseAddress, Length, Attributes, AttributeMask, FALSE);
}

STATIC CONST EDKII_MEMORY_ATTRIBUTE_PPI mMemoryAttributePpi = {
Expand Down
5 changes: 4 additions & 1 deletion ArmPkg/Include/Library/ArmMmuLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ ArmReplaceLiveTranslationEntry (
@param[in] Length The size in bytes of the memory region.
@param[in] Attributes Mask of memory attributes to set.
@param[in] AttributeMask Mask of memory attributes to take into account.
@param[in] PagesOnly Whether all mappings should be down to pages,
even in cases where block mappings could be used.

@retval EFI_SUCCESS The attributes were set for the memory region.
@retval EFI_INVALID_PARAMETER BaseAddress or Length is not suitably aligned.
Expand All @@ -68,7 +70,8 @@ ArmSetMemoryAttributes (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Attributes,
IN UINT64 AttributeMask
IN UINT64 AttributeMask,
IN BOOLEAN PagesOnly
);

#endif // ARM_MMU_LIB_H_
28 changes: 19 additions & 9 deletions ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ UpdateRegionMappingRecursive (
IN UINT64 AttributeClearMask,
IN UINT64 *PageTable,
IN UINTN Level,
IN BOOLEAN TableIsLive
IN BOOLEAN TableIsLive,
IN BOOLEAN PagesOnly
)
{
UINTN BlockShift;
Expand Down Expand Up @@ -252,7 +253,7 @@ UpdateRegionMappingRecursive (
// mappings in that particular case.
//
if ((Level == 0) || (((RegionStart | BlockEnd) & BlockMask) != 0) ||
((Level < 3) && (((UINT64)PageTable & ~BlockMask) == RegionStart)) ||
((Level < 3) && (PagesOnly || (((UINT64)PageTable & ~BlockMask) == RegionStart))) ||
IsTableEntry (*Entry, Level))
{
ASSERT (Level < 3);
Expand All @@ -262,7 +263,7 @@ UpdateRegionMappingRecursive (
// If the region we are trying to map is already covered by a block
// entry with the right attributes, don't bother splitting it up.
//
if (IsBlockEntry (*Entry, Level) &&
if (!PagesOnly && IsBlockEntry (*Entry, Level) &&
((*Entry & TT_ATTRIBUTES_MASK & ~AttributeClearMask) == AttributeSetMask))
{
continue;
Expand Down Expand Up @@ -299,7 +300,8 @@ UpdateRegionMappingRecursive (
0,
TranslationTable,
Level + 1,
FALSE
FALSE,
PagesOnly
);
if (EFI_ERROR (Status)) {
//
Expand Down Expand Up @@ -328,7 +330,8 @@ UpdateRegionMappingRecursive (
AttributeClearMask,
TranslationTable,
Level + 1,
NextTableIsLive
NextTableIsLive,
PagesOnly
);
if (EFI_ERROR (Status)) {
if (!IsTableEntry (*Entry, Level)) {
Expand Down Expand Up @@ -376,7 +379,8 @@ UpdateRegionMapping (
IN UINT64 AttributeSetMask,
IN UINT64 AttributeClearMask,
IN UINT64 *RootTable,
IN BOOLEAN TableIsLive
IN BOOLEAN TableIsLive,
IN BOOLEAN PagesOnly
)
{
UINTN T0SZ;
Expand All @@ -401,7 +405,8 @@ UpdateRegionMapping (
AttributeClearMask,
RootTable,
GetRootTableLevel (T0SZ),
TableIsLive
TableIsLive,
PagesOnly
);
}

Expand All @@ -418,6 +423,7 @@ FillTranslationTable (
ArmMemoryAttributeToPageAttribute (MemoryRegion->Attributes) | TT_AF,
0,
RootTable,
FALSE,
FALSE
);
}
Expand Down Expand Up @@ -493,6 +499,8 @@ GcdAttributeToPageAttribute (
@param[in] Length The size in bytes of the memory region.
@param[in] Attributes Mask of memory attributes to set.
@param[in] AttributeMask Mask of memory attributes to take into account.
@param[in] PagesOnly Whether all mappings should be down to pages,
even in cases where block mappings could be used.

@retval EFI_SUCCESS The attributes were set for the memory region.
@retval EFI_INVALID_PARAMETER BaseAddress or Length is not suitably aligned.
Expand All @@ -507,7 +515,8 @@ ArmSetMemoryAttributes (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Attributes,
IN UINT64 AttributeMask
IN UINT64 AttributeMask,
IN BOOLEAN PagesOnly
)
{
UINT64 PageAttributes;
Expand Down Expand Up @@ -548,7 +557,8 @@ ArmSetMemoryAttributes (
PageAttributes,
PageAttributeMask,
ArmGetTTBR0BaseAddress (),
TRUE
TRUE,
PagesOnly
);
}

Expand Down
5 changes: 4 additions & 1 deletion ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibUpdate.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ SetMemoryAttributes (
@param[in] Length The size in bytes of the memory region.
@param[in] Attributes Mask of memory attributes to set.
@param[in] AttributeMask Mask of memory attributes to take into account.
@param[in] PagesOnly Whether all mappings should be down to pages,
even in cases where block mappings could be used.

@retval EFI_SUCCESS The attributes were set for the memory region.
@retval EFI_INVALID_PARAMETER BaseAddress or Length is not suitably aligned.
Expand All @@ -504,7 +506,8 @@ ArmSetMemoryAttributes (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 Length,
IN UINT64 Attributes,
IN UINT64 AttributeMask
IN UINT64 AttributeMask,
IN BOOLEAN PagesOnly
)
{
UINT32 TtEntryMask;
Expand Down
3 changes: 2 additions & 1 deletion ArmPkg/Library/OpteeLib/Optee.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ OpteeSharedMemoryRemap (
PhysicalAddress,
Size,
EFI_MEMORY_WB | EFI_MEMORY_XP,
0
0,
FALSE
);
if (EFI_ERROR (Status)) {
return Status;
Expand Down
22 changes: 11 additions & 11 deletions MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ ApplyMemoryProtectionPolicy (
IN UINT64 Length
)
{
UINT64 OldAttributes;
// UINT64 OldAttributes;
UINT64 NewAttributes;

//
Expand Down Expand Up @@ -1095,16 +1095,16 @@ ApplyMemoryProtectionPolicy (
//
NewAttributes = GetPermissionAttributeForMemoryType (NewType);

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;
}
// 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;
// }

return gCpu->SetMemoryAttributes (gCpu, Memory, Length, NewAttributes);
}
Loading