X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FCpuDxe%2FCpuPageTable.c;h=9658ed74c557dbfcd574a1995b899d4a4d556f44;hp=202d1d9b64c8263b9168a7cedac806f0541f5324;hb=768bd967847e91aa236c1eed3507e0863547f544;hpb=22292ed344b8512c838bdd162533198b51a51c0c diff --git a/UefiCpuPkg/CpuDxe/CpuPageTable.c b/UefiCpuPkg/CpuDxe/CpuPageTable.c index 202d1d9b64..9658ed74c5 100644 --- a/UefiCpuPkg/CpuDxe/CpuPageTable.c +++ b/UefiCpuPkg/CpuDxe/CpuPageTable.c @@ -2,6 +2,8 @@ Page table management support. Copyright (c) 2017, Intel Corporation. All rights reserved.
+ Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -21,6 +23,8 @@ #include #include #include + +#include "CpuDxe.h" #include "CpuPageTable.h" /// @@ -191,12 +195,9 @@ GetCurrentPagingContext ( AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx); if ((RegEdx & BIT20) != 0) { // XD supported - if ((AsmReadMsr64 (0x000001A0) & BIT34) == 0) { - // XD enabled - if ((AsmReadMsr64 (0xC0000080) & BIT11) != 0) { - // XD activated - PagingContext->ContextData.Ia32.Attributes |= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED; - } + if ((AsmReadMsr64 (0xC0000080) & BIT11) != 0) { + // XD activated + PagingContext->ContextData.Ia32.Attributes |= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED; } } if ((RegEdx & BIT26) != 0) { @@ -271,6 +272,7 @@ GetPageTableEntry ( UINT64 *L2PageTable; UINT64 *L3PageTable; UINT64 *L4PageTable; + UINT64 AddressEncMask; ASSERT (PagingContext != NULL); @@ -279,6 +281,10 @@ GetPageTableEntry ( Index2 = ((UINTN)Address >> 21) & PAGING_PAE_INDEX_MASK; Index1 = ((UINTN)Address >> 12) & PAGING_PAE_INDEX_MASK; + // Make sure AddressEncMask is contained to smallest supported address field. + // + AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64; + if (PagingContext->MachineType == IMAGE_FILE_MACHINE_X64) { L4PageTable = (UINT64 *)(UINTN)PagingContext->ContextData.X64.PageTableBase; if (L4PageTable[Index4] == 0) { @@ -286,7 +292,7 @@ GetPageTableEntry ( return NULL; } - L3PageTable = (UINT64 *)(UINTN)(L4PageTable[Index4] & PAGING_4K_ADDRESS_MASK_64); + L3PageTable = (UINT64 *)(UINTN)(L4PageTable[Index4] & ~AddressEncMask & PAGING_4K_ADDRESS_MASK_64); } else { ASSERT((PagingContext->ContextData.Ia32.Attributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE) != 0); L3PageTable = (UINT64 *)(UINTN)PagingContext->ContextData.Ia32.PageTableBase; @@ -301,7 +307,7 @@ GetPageTableEntry ( return &L3PageTable[Index3]; } - L2PageTable = (UINT64 *)(UINTN)(L3PageTable[Index3] & PAGING_4K_ADDRESS_MASK_64); + L2PageTable = (UINT64 *)(UINTN)(L3PageTable[Index3] & ~AddressEncMask & PAGING_4K_ADDRESS_MASK_64); if (L2PageTable[Index2] == 0) { *PageAttribute = PageNone; return NULL; @@ -313,7 +319,7 @@ GetPageTableEntry ( } // 4k - L1PageTable = (UINT64 *)(UINTN)(L2PageTable[Index2] & PAGING_4K_ADDRESS_MASK_64); + L1PageTable = (UINT64 *)(UINTN)(L2PageTable[Index2] & ~AddressEncMask & PAGING_4K_ADDRESS_MASK_64); if ((L1PageTable[Index1] == 0) && (Address != 0)) { *PageAttribute = PageNone; return NULL; @@ -436,8 +442,8 @@ ConvertPageEntryAttribute ( *PageEntry = NewPageEntry; if (CurrentPageEntry != NewPageEntry) { *IsModified = TRUE; - DEBUG ((DEBUG_INFO, "ConvertPageEntryAttribute 0x%lx", CurrentPageEntry)); - DEBUG ((DEBUG_INFO, "->0x%lx\n", NewPageEntry)); + DEBUG ((DEBUG_VERBOSE, "ConvertPageEntryAttribute 0x%lx", CurrentPageEntry)); + DEBUG ((DEBUG_VERBOSE, "->0x%lx\n", NewPageEntry)); } else { *IsModified = FALSE; } @@ -499,11 +505,16 @@ SplitPage ( UINT64 BaseAddress; UINT64 *NewPageEntry; UINTN Index; + UINT64 AddressEncMask; ASSERT (PageAttribute == Page2M || PageAttribute == Page1G); ASSERT (AllocatePagesFunc != NULL); + // Make sure AddressEncMask is contained to smallest supported address field. + // + AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64; + if (PageAttribute == Page2M) { // // Split 2M to 4K @@ -515,11 +526,11 @@ SplitPage ( if (NewPageEntry == NULL) { return RETURN_OUT_OF_RESOURCES; } - BaseAddress = *PageEntry & PAGING_2M_ADDRESS_MASK_64; + BaseAddress = *PageEntry & ~AddressEncMask & PAGING_2M_ADDRESS_MASK_64; for (Index = 0; Index < SIZE_4KB / sizeof(UINT64); Index++) { - NewPageEntry[Index] = BaseAddress + SIZE_4KB * Index + ((*PageEntry) & PAGE_PROGATE_BITS); + NewPageEntry[Index] = (BaseAddress + SIZE_4KB * Index) | AddressEncMask | ((*PageEntry) & PAGE_PROGATE_BITS); } - (*PageEntry) = (UINT64)(UINTN)NewPageEntry + ((*PageEntry) & PAGE_PROGATE_BITS); + (*PageEntry) = (UINT64)(UINTN)NewPageEntry | AddressEncMask | ((*PageEntry) & PAGE_PROGATE_BITS); return RETURN_SUCCESS; } else { return RETURN_UNSUPPORTED; @@ -536,11 +547,11 @@ SplitPage ( if (NewPageEntry == NULL) { return RETURN_OUT_OF_RESOURCES; } - BaseAddress = *PageEntry & PAGING_1G_ADDRESS_MASK_64; + BaseAddress = *PageEntry & ~AddressEncMask & PAGING_1G_ADDRESS_MASK_64; for (Index = 0; Index < SIZE_4KB / sizeof(UINT64); Index++) { - NewPageEntry[Index] = BaseAddress + SIZE_2MB * Index + IA32_PG_PS + ((*PageEntry) & PAGE_PROGATE_BITS); + NewPageEntry[Index] = (BaseAddress + SIZE_2MB * Index) | AddressEncMask | IA32_PG_PS | ((*PageEntry) & PAGE_PROGATE_BITS); } - (*PageEntry) = (UINT64)(UINTN)NewPageEntry + ((*PageEntry) & PAGE_PROGATE_BITS); + (*PageEntry) = (UINT64)(UINTN)NewPageEntry | AddressEncMask | ((*PageEntry) & PAGE_PROGATE_BITS); return RETURN_SUCCESS; } else { return RETURN_UNSUPPORTED; @@ -625,10 +636,10 @@ ConvertMemoryPageAttributes ( switch(CurrentPagingContext.MachineType) { case IMAGE_FILE_MACHINE_I386: if (CurrentPagingContext.ContextData.Ia32.PageTableBase == 0) { - DEBUG ((DEBUG_ERROR, "PageTable is 0!\n")); if (Attributes == 0) { return EFI_SUCCESS; } else { + DEBUG ((DEBUG_ERROR, "PageTable is 0!\n")); return EFI_UNSUPPORTED; } } @@ -758,6 +769,159 @@ AssignMemoryPageAttributes ( return Status; } +/** + Check if Execute Disable feature is enabled or not. +**/ +BOOLEAN +IsExecuteDisableEnabled ( + VOID + ) +{ + MSR_CORE_IA32_EFER_REGISTER MsrEfer; + + MsrEfer.Uint64 = AsmReadMsr64 (MSR_IA32_EFER); + return (MsrEfer.Bits.NXE == 1); +} + +/** + Update GCD memory space attributes according to current page table setup. +**/ +VOID +RefreshGcdMemoryAttributesFromPaging ( + VOID + ) +{ + EFI_STATUS Status; + UINTN NumberOfDescriptors; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap; + PAGE_TABLE_LIB_PAGING_CONTEXT PagingContext; + PAGE_ATTRIBUTE PageAttribute; + UINT64 *PageEntry; + UINT64 PageLength; + UINT64 MemorySpaceLength; + UINT64 Length; + UINT64 BaseAddress; + UINT64 PageStartAddress; + UINT64 Attributes; + UINT64 Capabilities; + UINT64 NewAttributes; + UINTN Index; + + // + // Assuming that memory space map returned is sorted already; otherwise sort + // them in the order of lowest address to highest address. + // + Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap); + ASSERT_EFI_ERROR (Status); + + GetCurrentPagingContext (&PagingContext); + + Attributes = 0; + NewAttributes = 0; + BaseAddress = 0; + PageLength = 0; + + if (IsExecuteDisableEnabled ()) { + Capabilities = EFI_MEMORY_RO | EFI_MEMORY_RP | EFI_MEMORY_XP; + } else { + Capabilities = EFI_MEMORY_RO | EFI_MEMORY_RP; + } + + for (Index = 0; Index < NumberOfDescriptors; Index++) { + if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) { + continue; + } + + // + // Sync the actual paging related capabilities back to GCD service first. + // As a side effect (good one), this can also help to avoid unnecessary + // memory map entries due to the different capabilities of the same type + // memory, such as multiple RT_CODE and RT_DATA entries in memory map, + // which could cause boot failure of some old Linux distro (before v4.3). + // + Status = gDS->SetMemorySpaceCapabilities ( + MemorySpaceMap[Index].BaseAddress, + MemorySpaceMap[Index].Length, + MemorySpaceMap[Index].Capabilities | Capabilities + ); + if (EFI_ERROR (Status)) { + // + // If we cannot udpate the capabilities, we cannot update its + // attributes either. So just simply skip current block of memory. + // + DEBUG (( + DEBUG_WARN, + "Failed to update capability: [%lu] %016lx - %016lx (%016lx -> %016lx)\r\n", + (UINT64)Index, MemorySpaceMap[Index].BaseAddress, + MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - 1, + MemorySpaceMap[Index].Capabilities, + MemorySpaceMap[Index].Capabilities | Capabilities + )); + continue; + } + + if (MemorySpaceMap[Index].BaseAddress >= (BaseAddress + PageLength)) { + // + // Current memory space starts at a new page. Resetting PageLength will + // trigger a retrieval of page attributes at new address. + // + PageLength = 0; + } else { + // + // In case current memory space is not adjacent to last one + // + PageLength -= (MemorySpaceMap[Index].BaseAddress - BaseAddress); + } + + // + // Sync actual page attributes to GCD + // + BaseAddress = MemorySpaceMap[Index].BaseAddress; + MemorySpaceLength = MemorySpaceMap[Index].Length; + while (MemorySpaceLength > 0) { + if (PageLength == 0) { + PageEntry = GetPageTableEntry (&PagingContext, BaseAddress, &PageAttribute); + if (PageEntry == NULL) { + break; + } + + // + // Note current memory space might start in the middle of a page + // + PageStartAddress = (*PageEntry) & (UINT64)PageAttributeToMask(PageAttribute); + PageLength = PageAttributeToLength (PageAttribute) - (BaseAddress - PageStartAddress); + Attributes = GetAttributesFromPageEntry (PageEntry); + } + + Length = MIN (PageLength, MemorySpaceLength); + if (Attributes != (MemorySpaceMap[Index].Attributes & + EFI_MEMORY_PAGETYPE_MASK)) { + NewAttributes = (MemorySpaceMap[Index].Attributes & + ~EFI_MEMORY_PAGETYPE_MASK) | Attributes; + Status = gDS->SetMemorySpaceAttributes ( + BaseAddress, + Length, + NewAttributes + ); + ASSERT_EFI_ERROR (Status); + DEBUG (( + DEBUG_INFO, + "Updated memory space attribute: [%lu] %016lx - %016lx (%016lx -> %016lx)\r\n", + (UINT64)Index, BaseAddress, BaseAddress + Length - 1, + MemorySpaceMap[Index].Attributes, + NewAttributes + )); + } + + PageLength -= Length; + MemorySpaceLength -= Length; + BaseAddress += Length; + } + } + + FreePool (MemorySpaceMap); +} + /** Initialize the Page Table lib. **/