Skip to content

Commit

Permalink
Ring3: Added EFI_MEMORY_USER attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Dec 25, 2023
1 parent 2b27afe commit 928716f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
14 changes: 14 additions & 0 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -2791,4 +2791,18 @@ MergeMemoryMap (
IN UINTN DescriptorSize
);

/**
Set UEFI image memory attributes.
@param[in] BaseAddress Specified start address
@param[in] Length Specified length
@param[in] Attributes Specified attributes
**/
VOID
SetUefiImageMemoryAttributes (
IN UINT64 BaseAddress,
IN UINT64 Length,
IN UINT64 Attributes
);

#endif
4 changes: 4 additions & 0 deletions MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,10 @@ CoreLoadPeImage (
//
if (!EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName));

if (AsciiStrStr (EfiFileName, "Ext4") != NULL) {
SetUefiImageMemoryAttributes (BufferAddress, EFI_PAGES_TO_SIZE (DstBufPages), EFI_MEMORY_USER);
}
}
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));

Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ SetUefiImageMemoryAttributes (
Status = CoreGetMemorySpaceDescriptor (BaseAddress, &Descriptor);
ASSERT_EFI_ERROR (Status);

FinalAttributes = (Descriptor.Attributes & EFI_CACHE_ATTRIBUTE_MASK) | (Attributes & EFI_MEMORY_ATTRIBUTE_MASK);
FinalAttributes = (Descriptor.Attributes & (EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_USER)) | (Attributes & EFI_MEMORY_ATTRIBUTE_MASK);

DEBUG ((DEBUG_INFO, "SetUefiImageMemoryAttributes - 0x%016lx - 0x%016lx (0x%016lx)\n", BaseAddress, Length, FinalAttributes));

Expand Down
8 changes: 7 additions & 1 deletion MdePkg/Include/Uefi/UefiSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ typedef enum {
//
#define EFI_MEMORY_CPU_CRYPTO 0x0000000000080000ULL

//
// If this flag is set, the memory region contains user code or data.
// If this flag is clear, the memory region contains supervisor code or data.
//
#define EFI_MEMORY_USER 0x0000000000100000ULL

//
// Runtime memory attribute
//
Expand All @@ -115,7 +121,7 @@ typedef enum {
//
#define EFI_CACHE_ATTRIBUTE_MASK (EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_WP)
#define EFI_MEMORY_ACCESS_MASK (EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_RO)
#define EFI_MEMORY_ATTRIBUTE_MASK (EFI_MEMORY_ACCESS_MASK | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO)
#define EFI_MEMORY_ATTRIBUTE_MASK (EFI_MEMORY_ACCESS_MASK | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO | EFI_MEMORY_USER)

///
/// Memory descriptor version number.
Expand Down
21 changes: 21 additions & 0 deletions UefiCpuPkg/Library/CpuArchLib/CpuPageTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,27 @@ ConvertPageEntryAttribute (
}
}

if ((Attributes & EFI_MEMORY_USER) != 0) {
switch (PageAction) {
case PageActionAssign:
case PageActionSet:
NewPageEntry |= IA32_PG_U;
break;
case PageActionClear:
NewPageEntry &= ~(UINT64)IA32_PG_U;
break;
}
} else {
switch (PageAction) {
case PageActionAssign:
NewPageEntry &= ~(UINT64)IA32_PG_U;
break;
case PageActionSet:
case PageActionClear:
break;
}
}

GetPagingDetails (&PagingContext->ContextData, NULL, &PageAttributes);

if ((*PageAttributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED) != 0) {
Expand Down

0 comments on commit 928716f

Please sign in to comment.