From nobody Sun May 5 11:35:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+55636+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+55636+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1583570340; cv=none; d=zohomail.com; s=zohoarc; b=V9ySRw9BM6jYRZl2CPTbiCOEGAQrLhR2k2VXm7ypQvrcC1ZuDIrWrtC8FHb933KYsYLSHfj1pCsuVKXO8SCy6YqoQ/iXYp7fxu90lj6i1DnkmdWxV539pr2+VRfPf9yGfgxZsSrqTv9VRFXPPzvAH9G4SmtBBrFG4VcQOER1HPI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583570340; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=xu3vejPTsoHYPagrDApvEyxG0kGXUjEJP2OuzJ2aRLk=; b=SKJhb/rErovcTq53sxkzFBePRIco9BjcHyPdtc2j8zD3BhHzpyyKfN7lCCFncKSgEe6hu1P+Nds1Sft7Yz9+gBOgnAuyqrz7pm5Kz4QuOhUTCUSDJBVIhV3nDaNR9UYfBNPBaCVKgKWKXOC5j7kB17OFDc5KVfvGPvlfcEElRgs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+55636+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1583570340201422.2315219926203; Sat, 7 Mar 2020 00:39:00 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 8ZnYYY1788612xXeVvVdCpeF; Sat, 07 Mar 2020 00:38:59 -0800 X-Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by mx.groups.io with SMTP id smtpd.web12.10670.1583570339204019910 for ; Sat, 07 Mar 2020 00:38:59 -0800 X-Received: by mail-wr1-f43.google.com with SMTP id p2so4220991wrw.7 for ; Sat, 07 Mar 2020 00:38:58 -0800 (PST) X-Gm-Message-State: faHccyJkme0oXtsn4Nv7ntIbx1787277AA= X-Google-Smtp-Source: ADFU+vvu4+BzIIK+WtckbeubNS+FsUp/Bo2iSepF5ZzE+7agwNq+0TUUs63p5Vb2svrp40De8Fl96g== X-Received: by 2002:adf:e94d:: with SMTP id m13mr7569940wrn.415.1583570337119; Sat, 07 Mar 2020 00:38:57 -0800 (PST) X-Received: from e123331-lin.home ([2a01:cb1d:112:6f00:816e:ff0d:fb69:f613]) by smtp.gmail.com with ESMTPSA id t14sm20430953wrp.63.2020.03.07.00.38.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2020 00:38:55 -0800 (PST) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: leif@nuviainc.com, Ard Biesheuvel Subject: [edk2-devel] [PATCH v4 1/2] ArmPkg/ArmMmuLib AARCH64: rewrite page table code Date: Sat, 7 Mar 2020 09:38:48 +0100 Message-Id: <20200307083849.8940-2-ard.biesheuvel@linaro.org> In-Reply-To: <20200307083849.8940-1-ard.biesheuvel@linaro.org> References: <20200307083849.8940-1-ard.biesheuvel@linaro.org> Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ard.biesheuvel@linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1583570339; bh=NhOTZiU0er7yax9P39bkTPxsnicZW02uaLT5sNqRI7k=; h=Cc:Date:From:Reply-To:Subject:To; b=EPj7dI9ioMih4xQfGXv/EcQoeia6lNoN7RD2gPZ480eDZVx/W164htg4TYfTJVGeJ5n et9D3s15dyw2H9kEDalKGV86Zp8VsXZlrDnuSBBeD9Fh82mOh5cWYzirWgb/JeHlkwakC fWdNegPWgwLJGkJSxw4OhbYf4aAOBksqen0= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Replace the slightly overcomplicated page table management code with a simplified, recursive implementation that should be far easier to reason about. Note that, as a side effect, this extends the per-entry cache invalidation that we do on page table entries to block and page entries, whereas the previous change inadvertently only affected the creation of table entries. Signed-off-by: Ard Biesheuvel Reviewed-by: Leif Lindholm --- ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c | 391 ++++++++------------ 1 file changed, 148 insertions(+), 243 deletions(-) diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c b/ArmPkg/Libr= ary/ArmMmuLib/AArch64/ArmMmuLibCore.c index 204e33c75f95..00a38bc31d0a 100644 --- a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c +++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c @@ -1,7 +1,7 @@ /** @file * File managing the MMU for ARMv8 architecture * -* Copyright (c) 2011-2014, ARM Limited. All rights reserved. +* Copyright (c) 2011-2020, ARM Limited. All rights reserved. * Copyright (c) 2016, Linaro Limited. All rights reserved. * Copyright (c) 2017, Intel Corporation. All rights reserved.
* @@ -121,19 +121,150 @@ GetRootTranslationTableInfo ( =20 STATIC VOID -ReplaceLiveEntry ( +ReplaceTableEntry ( IN UINT64 *Entry, IN UINT64 Value, - IN UINT64 RegionStart + IN UINT64 RegionStart, + IN BOOLEAN IsLiveBlockMapping ) { - if (!ArmMmuEnabled ()) { + if (!ArmMmuEnabled () || !IsLiveBlockMapping) { *Entry =3D Value; + ArmUpdateTranslationTableEntry (Entry, (VOID *)(UINTN)RegionStart); } else { ArmReplaceLiveTranslationEntry (Entry, Value, RegionStart); } } =20 +STATIC +VOID +FreePageTablesRecursive ( + IN UINT64 *TranslationTable + ) +{ + UINTN Index; + + for (Index =3D 0; Index < TT_ENTRY_COUNT; Index++) { + if ((TranslationTable[Index] & TT_TYPE_MASK) =3D=3D TT_TYPE_TABLE_ENTR= Y) { + FreePageTablesRecursive ((VOID *)(UINTN)(TranslationTable[Index] & + TT_ADDRESS_MASK_BLOCK_ENTRY= )); + } + } + FreePages (TranslationTable, 1); +} + +STATIC +EFI_STATUS +UpdateRegionMappingRecursive ( + IN UINT64 RegionStart, + IN UINT64 RegionEnd, + IN UINT64 AttributeSetMask, + IN UINT64 AttributeClearMask, + IN UINT64 *PageTable, + IN UINTN Level + ) +{ + UINTN BlockShift; + UINT64 BlockMask; + UINT64 BlockEnd; + UINT64 *Entry; + UINT64 EntryValue; + VOID *TranslationTable; + EFI_STATUS Status; + + ASSERT (((RegionStart | RegionEnd) & EFI_PAGE_MASK) =3D=3D 0); + + BlockShift =3D (Level + 1) * BITS_PER_LEVEL + MIN_T0SZ; + BlockMask =3D MAX_UINT64 >> BlockShift; + + DEBUG ((DEBUG_VERBOSE, "%a(%d): %llx - %llx set %lx clr %lx\n", __FUNCTI= ON__, + Level, RegionStart, RegionEnd, AttributeSetMask, AttributeClearMask)); + + for (; RegionStart < RegionEnd; RegionStart =3D BlockEnd) { + BlockEnd =3D MIN (RegionEnd, (RegionStart | BlockMask) + 1); + Entry =3D &PageTable[(RegionStart >> (64 - BlockShift)) & (TT_ENTRY_CO= UNT - 1)]; + + // + // If RegionStart or BlockEnd is not aligned to the block size at this + // level, we will have to create a table mapping in order to map less + // than a block, and recurse to create the block or page entries at + // the next level. No block mappings are allowed at all at level 0, + // so in that case, we have to recurse unconditionally. + // + if (Level =3D=3D 0 || ((RegionStart | BlockEnd) & BlockMask) !=3D 0) { + ASSERT (Level < 3); + + if ((*Entry & TT_TYPE_MASK) !=3D TT_TYPE_TABLE_ENTRY) { + // + // No table entry exists yet, so we need to allocate a page table + // for the next level. + // + TranslationTable =3D AllocatePages (1); + if (TranslationTable =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if ((*Entry & TT_TYPE_MASK) =3D=3D TT_TYPE_BLOCK_ENTRY) { + // + // We are splitting an existing block entry, so we have to popul= ate + // the new table with the attributes of the block entry it repla= ces. + // + Status =3D UpdateRegionMappingRecursive (RegionStart & ~BlockMas= k, + (RegionStart | BlockMask) + 1, *Entry & TT_ATTRIBUTES= _MASK, + 0, TranslationTable, Level + 1); + if (EFI_ERROR (Status)) { + // + // The range we passed to UpdateRegionMappingRecursive () is b= lock + // aligned, so it is guaranteed that no further pages were all= ocated + // by it, and so we only have to free the page we allocated he= re. + // + FreePages (TranslationTable, 1); + return Status; + } + } else { + ZeroMem (TranslationTable, EFI_PAGE_SIZE); + } + } else { + TranslationTable =3D (VOID *)(UINTN)(*Entry & TT_ADDRESS_MASK_BLOC= K_ENTRY); + } + + // + // Recurse to the next level + // + Status =3D UpdateRegionMappingRecursive (RegionStart, BlockEnd, + AttributeSetMask, AttributeClearMask, TranslationTable, + Level + 1); + if (EFI_ERROR (Status)) { + if ((*Entry & TT_TYPE_MASK) !=3D TT_TYPE_TABLE_ENTRY) { + // + // We are creating a new table entry, so on failure, we can free= all + // allocations we made recursively, given that the whole subhier= archy + // has not been wired into the live page tables yet. (This is not + // possible for existing table entries, since we cannot revert t= he + // modifications we made to the subhierarchy it represents.) + // + FreePageTablesRecursive (TranslationTable); + } + return Status; + } + + if ((*Entry & TT_TYPE_MASK) !=3D TT_TYPE_TABLE_ENTRY) { + EntryValue =3D (UINTN)TranslationTable | TT_TYPE_TABLE_ENTRY; + ReplaceTableEntry (Entry, EntryValue, RegionStart, + (*Entry & TT_TYPE_MASK) =3D=3D TT_TYPE_BLOCK_EN= TRY); + } + } else { + EntryValue =3D (*Entry & AttributeClearMask) | AttributeSetMask; + EntryValue |=3D RegionStart; + EntryValue |=3D (Level =3D=3D 3) ? TT_TYPE_BLOCK_ENTRY_LEVEL3 + : TT_TYPE_BLOCK_ENTRY; + + ReplaceTableEntry (Entry, EntryValue, RegionStart, FALSE); + } + } + return EFI_SUCCESS; +} + STATIC VOID LookupAddresstoRootTable ( @@ -164,230 +295,28 @@ LookupAddresstoRootTable ( GetRootTranslationTableInfo (*T0SZ, NULL, TableEntryCount); } =20 -STATIC -UINT64* -GetBlockEntryListFromAddress ( - IN UINT64 *RootTable, - IN UINT64 RegionStart, - OUT UINTN *TableLevel, - IN OUT UINT64 *BlockEntrySize, - OUT UINT64 **LastBlockEntry - ) -{ - UINTN RootTableLevel; - UINTN RootTableEntryCount; - UINT64 *TranslationTable; - UINT64 *BlockEntry; - UINT64 *SubTableBlockEntry; - UINT64 BlockEntryAddress; - UINTN BaseAddressAlignment; - UINTN PageLevel; - UINTN Index; - UINTN IndexLevel; - UINTN T0SZ; - UINT64 Attributes; - UINT64 TableAttributes; - - // Initialize variable - BlockEntry =3D NULL; - - // Ensure the parameters are valid - if (!(TableLevel && BlockEntrySize && LastBlockEntry)) { - ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER); - return NULL; - } - - // Ensure the Region is aligned on 4KB boundary - if ((RegionStart & (SIZE_4KB - 1)) !=3D 0) { - ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER); - return NULL; - } - - // Ensure the required size is aligned on 4KB boundary and not 0 - if ((*BlockEntrySize & (SIZE_4KB - 1)) !=3D 0 || *BlockEntrySize =3D=3D = 0) { - ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER); - return NULL; - } - - T0SZ =3D ArmGetTCR () & TCR_T0SZ_MASK; - // Get the Table info from T0SZ - GetRootTranslationTableInfo (T0SZ, &RootTableLevel, &RootTableEntryCount= ); - - // If the start address is 0x0 then we use the size of the region to ide= ntify the alignment - if (RegionStart =3D=3D 0) { - // Identify the highest possible alignment for the Region Size - BaseAddressAlignment =3D LowBitSet64 (*BlockEntrySize); - } else { - // Identify the highest possible alignment for the Base Address - BaseAddressAlignment =3D LowBitSet64 (RegionStart); - } - - // Identify the Page Level the RegionStart must belong to. Note that Pag= eLevel - // should be at least 1 since block translations are not supported at le= vel 0 - PageLevel =3D MAX (3 - ((BaseAddressAlignment - 12) / 9), 1); - - // If the required size is smaller than the current block size then we n= eed to go to the page below. - // The PageLevel was calculated on the Base Address alignment but did no= t take in account the alignment - // of the allocation size - while (*BlockEntrySize < TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel)) { - // It does not fit so we need to go a page level above - PageLevel++; - } - - // - // Get the Table Descriptor for the corresponding PageLevel. We need to = decompose RegionStart to get appropriate entries - // - - TranslationTable =3D RootTable; - for (IndexLevel =3D RootTableLevel; IndexLevel <=3D PageLevel; IndexLeve= l++) { - BlockEntry =3D (UINT64*)TT_GET_ENTRY_FOR_ADDRESS (TranslationTable, In= dexLevel, RegionStart); - - if ((IndexLevel !=3D 3) && ((*BlockEntry & TT_TYPE_MASK) =3D=3D TT_TYP= E_TABLE_ENTRY)) { - // Go to the next table - TranslationTable =3D (UINT64*)(*BlockEntry & TT_ADDRESS_MASK_DESCRIP= TION_TABLE); - - // If we are at the last level then update the last level to next le= vel - if (IndexLevel =3D=3D PageLevel) { - // Enter the next level - PageLevel++; - } - } else if ((*BlockEntry & TT_TYPE_MASK) =3D=3D TT_TYPE_BLOCK_ENTRY) { - // If we are not at the last level then we need to split this BlockE= ntry - if (IndexLevel !=3D PageLevel) { - // Retrieve the attributes from the block entry - Attributes =3D *BlockEntry & TT_ATTRIBUTES_MASK; - - // Convert the block entry attributes into Table descriptor attrib= utes - TableAttributes =3D TT_TABLE_AP_NO_PERMISSION; - if (Attributes & TT_NS) { - TableAttributes =3D TT_TABLE_NS; - } - - // Get the address corresponding at this entry - BlockEntryAddress =3D RegionStart; - BlockEntryAddress =3D BlockEntryAddress >> TT_ADDRESS_OFFSET_AT_LE= VEL(IndexLevel); - // Shift back to right to set zero before the effective address - BlockEntryAddress =3D BlockEntryAddress << TT_ADDRESS_OFFSET_AT_LE= VEL(IndexLevel); - - // Set the correct entry type for the next page level - if ((IndexLevel + 1) =3D=3D 3) { - Attributes |=3D TT_TYPE_BLOCK_ENTRY_LEVEL3; - } else { - Attributes |=3D TT_TYPE_BLOCK_ENTRY; - } - - // Create a new translation table - TranslationTable =3D AllocatePages (1); - if (TranslationTable =3D=3D NULL) { - return NULL; - } - - // Populate the newly created lower level table - SubTableBlockEntry =3D TranslationTable; - for (Index =3D 0; Index < TT_ENTRY_COUNT; Index++) { - *SubTableBlockEntry =3D Attributes | (BlockEntryAddress + (Index= << TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel + 1))); - SubTableBlockEntry++; - } - - // Fill the BlockEntry with the new TranslationTable - ReplaceLiveEntry (BlockEntry, - (UINTN)TranslationTable | TableAttributes | TT_TYPE_TABLE_ENTRY, - RegionStart); - } - } else { - if (IndexLevel !=3D PageLevel) { - // - // Case when we have an Invalid Entry and we are at a page level a= bove of the one targetted. - // - - // Create a new translation table - TranslationTable =3D AllocatePages (1); - if (TranslationTable =3D=3D NULL) { - return NULL; - } - - ZeroMem (TranslationTable, TT_ENTRY_COUNT * sizeof(UINT64)); - - // Fill the new BlockEntry with the TranslationTable - *BlockEntry =3D ((UINTN)TranslationTable & TT_ADDRESS_MASK_DESCRIP= TION_TABLE) | TT_TYPE_TABLE_ENTRY; - } - } - } - - // Expose the found PageLevel to the caller - *TableLevel =3D PageLevel; - - // Now, we have the Table Level we can get the Block Size associated to = this table - *BlockEntrySize =3D TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel); - - // The last block of the root table depends on the number of entry in th= is table, - // otherwise it is always the (TT_ENTRY_COUNT - 1)th entry in the table. - *LastBlockEntry =3D TT_LAST_BLOCK_ADDRESS(TranslationTable, - (PageLevel =3D=3D RootTableLevel) ? RootTableEntryCount : TT_ENTRY_C= OUNT); - - return BlockEntry; -} - STATIC EFI_STATUS UpdateRegionMapping ( - IN UINT64 *RootTable, IN UINT64 RegionStart, IN UINT64 RegionLength, - IN UINT64 Attributes, - IN UINT64 BlockEntryMask + IN UINT64 AttributeSetMask, + IN UINT64 AttributeClearMask ) { - UINT32 Type; - UINT64 *BlockEntry; - UINT64 *LastBlockEntry; - UINT64 BlockEntrySize; - UINTN TableLevel; + UINTN RootTableLevel; + UINTN T0SZ; =20 - // Ensure the Length is aligned on 4KB boundary - if ((RegionLength =3D=3D 0) || ((RegionLength & (SIZE_4KB - 1)) !=3D 0))= { - ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER); + if (((RegionStart | RegionLength) & EFI_PAGE_MASK)) { return EFI_INVALID_PARAMETER; } =20 - do { - // Get the first Block Entry that matches the Virtual Address and also= the information on the Table Descriptor - // such as the size of the Block Entry and the address of the last Blo= ckEntry of the Table Descriptor - BlockEntrySize =3D RegionLength; - BlockEntry =3D GetBlockEntryListFromAddress (RootTable, RegionStart, &= TableLevel, &BlockEntrySize, &LastBlockEntry); - if (BlockEntry =3D=3D NULL) { - // GetBlockEntryListFromAddress() return NULL when it fails to alloc= ate new pages from the Translation Tables - return EFI_OUT_OF_RESOURCES; - } + T0SZ =3D ArmGetTCR () & TCR_T0SZ_MASK; + GetRootTranslationTableInfo (T0SZ, &RootTableLevel, NULL); =20 - if (TableLevel !=3D 3) { - Type =3D TT_TYPE_BLOCK_ENTRY; - } else { - Type =3D TT_TYPE_BLOCK_ENTRY_LEVEL3; - } - - do { - // Fill the Block Entry with attribute and output block address - *BlockEntry &=3D BlockEntryMask; - *BlockEntry |=3D (RegionStart & TT_ADDRESS_MASK_BLOCK_ENTRY) | Attri= butes | Type; - - ArmUpdateTranslationTableEntry (BlockEntry, (VOID *)RegionStart); - - // Go to the next BlockEntry - RegionStart +=3D BlockEntrySize; - RegionLength -=3D BlockEntrySize; - BlockEntry++; - - // Break the inner loop when next block is a table - // Rerun GetBlockEntryListFromAddress to avoid page table memory leak - if (TableLevel !=3D 3 && BlockEntry <=3D LastBlockEntry && - (*BlockEntry & TT_TYPE_MASK) =3D=3D TT_TYPE_TABLE_ENTRY) { - break; - } - } while ((RegionLength >=3D BlockEntrySize) && (BlockEntry <=3D LastBl= ockEntry)); - } while (RegionLength !=3D 0); - - return EFI_SUCCESS; + return UpdateRegionMappingRecursive (RegionStart, RegionStart + RegionLe= ngth, + AttributeSetMask, AttributeClearMask, ArmGetTTBR0BaseAddress (), + RootTableLevel); } =20 STATIC @@ -398,7 +327,6 @@ FillTranslationTable ( ) { return UpdateRegionMapping ( - RootTable, MemoryRegion->VirtualBase, MemoryRegion->Length, ArmMemoryAttributeToPageAttribute (MemoryRegion->Attributes) | = TT_AF, @@ -455,8 +383,6 @@ ArmSetMemoryAttributes ( IN UINT64 Attributes ) { - EFI_STATUS Status; - UINT64 *TranslationTable; UINT64 PageAttributes; UINT64 PageAttributeMask; =20 @@ -473,19 +399,8 @@ ArmSetMemoryAttributes ( TT_PXN_MASK | TT_XN_MASK); } =20 - TranslationTable =3D ArmGetTTBR0BaseAddress (); - - Status =3D UpdateRegionMapping ( - TranslationTable, - BaseAddress, - Length, - PageAttributes, - PageAttributeMask); - if (EFI_ERROR (Status)) { - return Status; - } - - return EFI_SUCCESS; + return UpdateRegionMapping (BaseAddress, Length, PageAttributes, + PageAttributeMask); } =20 STATIC @@ -497,17 +412,7 @@ SetMemoryRegionAttribute ( IN UINT64 BlockEntryMask ) { - EFI_STATUS Status; - UINT64 *RootTable; - - RootTable =3D ArmGetTTBR0BaseAddress (); - - Status =3D UpdateRegionMapping (RootTable, BaseAddress, Length, Attribut= es, BlockEntryMask); - if (EFI_ERROR (Status)) { - return Status; - } - - return EFI_SUCCESS; + return UpdateRegionMapping (BaseAddress, Length, Attributes, BlockEntryM= ask); } =20 EFI_STATUS --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#55636): https://edk2.groups.io/g/devel/message/55636 Mute This Topic: https://groups.io/mt/71792241/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Sun May 5 11:35:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+55637+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+55637+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1583570340; cv=none; d=zohomail.com; s=zohoarc; b=MKikhlZzZ6aB8KpyiythzVDab6M8nMTJCaeXGnqztVMVICLA2vHdX+tBzpdnLk+jyoXZnTNEwbA8e+eyCL2KsFss9SM0LyHq0OpEDfD7Kc6VMKZg4f0gVQgzKZL9XZF9zNtsAUrU5RVHjvob2/balxYArE+Nrbe8Qa7FGkI5b1w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583570340; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=f/O4TGv/DcT6/0kC3EKiGGbjZZq29r32Nh1QjNeFp9Y=; b=cydrG3vIFzsW1FHC3EELubtXbSJJD+bO7qJjCH5N5B0GqR8+/BeFSZJaSB7N2pSCVOp7hL7fJilRnapRUOnOzhVcA62CrPf7hXm2YYgF52kRgq9H1AgDsaG41L6HDYC7k9gs37EdaX6ZMQ4jqRGNSjAk0st4/ja7jE8SX29KypA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+55637+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1583570340979838.2254092612139; Sat, 7 Mar 2020 00:39:00 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 4CBMYY1788612xYScwH1rcfw; Sat, 07 Mar 2020 00:39:00 -0800 X-Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by mx.groups.io with SMTP id smtpd.web12.10671.1583570340035458881 for ; Sat, 07 Mar 2020 00:39:00 -0800 X-Received: by mail-wr1-f67.google.com with SMTP id y17so5029706wrn.6 for ; Sat, 07 Mar 2020 00:38:59 -0800 (PST) X-Gm-Message-State: 1DAAJjHldPCLvl9ADpARhDFhx1787277AA= X-Google-Smtp-Source: ADFU+vt58F3rW4AGctGx3vnA0vW4I4jStSTvDgXbiOf+yO5xxgZyCPFvnCyAv0yNYXD+a9+LVoJWoA== X-Received: by 2002:adf:f4c9:: with SMTP id h9mr8823746wrp.168.1583570338228; Sat, 07 Mar 2020 00:38:58 -0800 (PST) X-Received: from e123331-lin.home ([2a01:cb1d:112:6f00:816e:ff0d:fb69:f613]) by smtp.gmail.com with ESMTPSA id t14sm20430953wrp.63.2020.03.07.00.38.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2020 00:38:57 -0800 (PST) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: leif@nuviainc.com, Ard Biesheuvel Subject: [edk2-devel] [PATCH v4 2/2] ArmPkg/ArmMmuLib AARCH64: invalidate page tables before populating them Date: Sat, 7 Mar 2020 09:38:49 +0100 Message-Id: <20200307083849.8940-3-ard.biesheuvel@linaro.org> In-Reply-To: <20200307083849.8940-1-ard.biesheuvel@linaro.org> References: <20200307083849.8940-1-ard.biesheuvel@linaro.org> Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ard.biesheuvel@linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1583570340; bh=suBoAMjaR2LiRdmEAM0cwLTEUJfk8/WCdq8wMNpMoko=; h=Cc:Date:From:Reply-To:Subject:To; b=cV1P/hpFw2C70ntmqtec1x9kGMS6R9dOwgHVCerYv1gGFNwshaEDiZ9VoCTIH5oSSDv PTJHR/zerZv3Fas0R9kf3fze0oK2io3aej4+tzn+pz9iejVR/HBinfNpJqIPEdnWkIrVL SBv1lJ5Cl1PVqYWhSPrIXq0N66ijX6TOyYk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" As it turns out, ARMv8 also permits accesses made with the MMU and caches off to hit in the caches, so to ensure that any modifications we make before enabling the MMU are visible afterwards as well, we should invalidate page tables right after allocation like we do now on ARM, if the MMU is still disabled at that point. Signed-off-by: Ard Biesheuvel Reviewed-by: Leif Lindholm --- ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c b/ArmPkg/Libr= ary/ArmMmuLib/AArch64/ArmMmuLibCore.c index 00a38bc31d0a..221175ca6535 100644 --- a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c +++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c @@ -204,6 +204,14 @@ UpdateRegionMappingRecursive ( return EFI_OUT_OF_RESOURCES; } =20 + if (!ArmMmuEnabled ()) { + // + // Make sure we are not inadvertently hitting in the caches + // when populating the page tables. + // + InvalidateDataCacheRange (TranslationTable, EFI_PAGE_SIZE); + } + if ((*Entry & TT_TYPE_MASK) =3D=3D TT_TYPE_BLOCK_ENTRY) { // // We are splitting an existing block entry, so we have to popul= ate @@ -602,6 +610,12 @@ ArmConfigureMmu ( *TranslationTableSize =3D RootTableEntryCount * sizeof(UINT64); } =20 + // + // Make sure we are not inadvertently hitting in the caches + // when populating the page tables. + // + InvalidateDataCacheRange (TranslationTable, + RootTableEntryCount * sizeof(UINT64)); ZeroMem (TranslationTable, RootTableEntryCount * sizeof(UINT64)); =20 TranslationTableAttribute =3D TT_ATTR_INDX_INVALID; --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#55637): https://edk2.groups.io/g/devel/message/55637 Mute This Topic: https://groups.io/mt/71792242/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-