From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id DAF4120945B77 for ; Sun, 17 Sep 2017 20:06:09 -0700 (PDT) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Sep 2017 20:09:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,410,1500966000"; d="scan'208,223";a="312755909" Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.41]) by fmsmga004.fm.intel.com with ESMTP; 17 Sep 2017 20:09:10 -0700 From: Jian J Wang To: edk2-devel@lists.01.org Cc: Jiewen Yao , Star Zeng , Laszlo Ersek , Michael Kinney Date: Mon, 18 Sep 2017 11:08:55 +0800 Message-Id: <20170918030855.11876-3-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20170918030855.11876-1-jian.j.wang@intel.com> References: <20170918030855.11876-1-jian.j.wang@intel.com> Subject: [PATCH 2/2] UefiCpuPkg/CpuDxe: Fix out-of-sync issue in page X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Sep 2017 03:06:10 -0000 >>From CpuDxe driver perspective, it doesn't update GCD memory attributes from current page table setup during its initialization. So the memory attributes in GCD might not reflect all memory attributes in real world. Cc: Jiewen Yao Cc: Star Zeng Cc: Laszlo Ersek Cc: Michael Kinney Suggested-by: Jiewen Yao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- UefiCpuPkg/CpuDxe/CpuDxe.c | 5 +++ UefiCpuPkg/CpuDxe/CpuDxe.h | 9 ++++ UefiCpuPkg/CpuDxe/CpuPageTable.c | 92 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c index b386f3b677..4e8fa100e0 100644 --- a/UefiCpuPkg/CpuDxe/CpuDxe.c +++ b/UefiCpuPkg/CpuDxe/CpuDxe.c @@ -863,6 +863,11 @@ RefreshGcdMemoryAttributes ( FreePool (MemorySpaceMap); } + // + // Update page attributes + // + RefreshGcdMemoryAttributesFromPaging(); + mIsFlushingGCD = FALSE; } diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.h b/UefiCpuPkg/CpuDxe/CpuDxe.h index 4861abee76..a25b35c6eb 100644 --- a/UefiCpuPkg/CpuDxe/CpuDxe.h +++ b/UefiCpuPkg/CpuDxe/CpuDxe.h @@ -52,6 +52,10 @@ EFI_MEMORY_UCE \ ) +#define EFI_MEMORY_PAGETYPE_MASK (EFI_MEMORY_RP | \ + EFI_MEMORY_XP | \ + EFI_MEMORY_RO \ + ) /** Flush CPU data cache. If the instruction cache is fully coherent @@ -261,5 +265,10 @@ SetDataSelectors ( UINT16 Selector ); +VOID +RefreshGcdMemoryAttributesFromPaging ( + VOID + ); + #endif diff --git a/UefiCpuPkg/CpuDxe/CpuPageTable.c b/UefiCpuPkg/CpuDxe/CpuPageTable.c index 2c61e7503e..bca4a497ac 100644 --- a/UefiCpuPkg/CpuDxe/CpuPageTable.c +++ b/UefiCpuPkg/CpuDxe/CpuPageTable.c @@ -23,6 +23,8 @@ #include #include #include + +#include "CpuDxe.h" #include "CpuPageTable.h" /// @@ -767,6 +769,96 @@ AssignMemoryPageAttributes ( return Status; } +/** + 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; + BOOLEAN DoUpdate; + 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); + + BaseAddress = 0; + PageLength = 0; + for (Index = 0; Index < NumberOfDescriptors; Index++) { + if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) { + 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; + } + + // Sync real 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); + + if (Attributes != (MemorySpaceMap[Index].Attributes & EFI_MEMORY_PAGETYPE_MASK)) { + DoUpdate = TRUE; + Attributes |= (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_PAGETYPE_MASK); + } else { + DoUpdate = FALSE; + } + } + + Length = MIN (PageLength, MemorySpaceLength); + if (DoUpdate) { + gDS->SetMemorySpaceCapabilities (BaseAddress, Length, Attributes); + gDS->SetMemorySpaceAttributes (BaseAddress, Length, Attributes); + DEBUG ((DEBUG_INFO, "Update memory space attribute: [%02d] %016lx - %016lx (%08lx -> %08lx)\r\n", + Index, BaseAddress, BaseAddress + Length - 1, + MemorySpaceMap[Index].Attributes, Attributes)); + } + + PageLength -= Length; + MemorySpaceLength -= Length; + BaseAddress += Length; + } + } + + FreePool (MemorySpaceMap); +} + /** Initialize the Page Table lib. **/ -- 2.14.1.windows.1