On 2022-09-26 01:25, Ard Biesheuvel wrote:
Permit the use of this library with the MMU and caches already enabled.
This removes the need for any cache maintenance for coherency, and is
generally better for robustness and performance, especially when running
under virtualization.

Note that this means we have to defer assignment of TTBR0 until the
page tables are ready to be used, and so UpdateRegionMapping() can no
longer read back TTBR0 directly to discover the root table address.

Signed-off-by: Ard Biesheuvel <a...@kernel.org>

Reviewed-by: Leif Lindholm <quic_llind...@quicinc.com>

/
    Leif

---
  ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c | 48 +++++++++++---------
  1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c 
b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c
index 4d75788ed2b2..ae59e9a7d04e 100644
--- a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c
+++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c
@@ -357,6 +357,7 @@ UpdateRegionMapping (
    IN  UINT64   RegionLength,

    IN  UINT64   AttributeSetMask,

    IN  UINT64   AttributeClearMask,

+  IN  UINT64   *RootTable,

    IN  BOOLEAN  TableIsLive

    )

  {

@@ -373,7 +374,7 @@ UpdateRegionMapping (
             RegionStart + RegionLength,

             AttributeSetMask,

             AttributeClearMask,

-           ArmGetTTBR0BaseAddress (),

+           RootTable,

             GetRootTableLevel (T0SZ),

             TableIsLive

             );

@@ -391,6 +392,7 @@ FillTranslationTable (
             MemoryRegion->Length,

             ArmMemoryAttributeToPageAttribute (MemoryRegion->Attributes) | 
TT_AF,

             0,

+           RootTable,

             FALSE

             );

  }

@@ -466,6 +468,7 @@ ArmSetMemoryAttributes (
             Length,

             PageAttributes,

             PageAttributeMask,

+           ArmGetTTBR0BaseAddress (),

             TRUE

             );

  }

@@ -484,6 +487,7 @@ SetMemoryRegionAttribute (
             Length,

             Attributes,

             BlockEntryMask,

+           ArmGetTTBR0BaseAddress (),

             TRUE

             );

  }

@@ -675,14 +679,6 @@ ArmConfigureMmu (
      return EFI_OUT_OF_RESOURCES;

    }

-  //

-  // We set TTBR0 just after allocating the table to retrieve its location from

-  // the subsequent functions without needing to pass this value across the

-  // functions. The MMU is only enabled after the translation tables are

-  // populated.

-  //

-  ArmSetTTBR0 (TranslationTable);

-

    if (TranslationTableBase != NULL) {

      *TranslationTableBase = TranslationTable;

    }

@@ -691,14 +687,17 @@ ArmConfigureMmu (
      *TranslationTableSize = RootTableEntryCount * sizeof (UINT64);

    }

-  //

-  // Make sure we are not inadvertently hitting in the caches

-  // when populating the page tables.

-  //

-  InvalidateDataCacheRange (

-    TranslationTable,

-    RootTableEntryCount * sizeof (UINT64)

-    );

+  if (!ArmMmuEnabled ()) {

+    //

+    // 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));

    while (MemoryTable->Length != 0) {

@@ -723,12 +722,17 @@ ArmConfigureMmu (
      MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_BACK, 
MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK)

      );

-  ArmDisableAlignmentCheck ();

-  ArmEnableStackAlignmentCheck ();

-  ArmEnableInstructionCache ();

-  ArmEnableDataCache ();

+  ArmSetTTBR0 (TranslationTable);

+

+  if (!ArmMmuEnabled ()) {

+    ArmDisableAlignmentCheck ();

+    ArmEnableStackAlignmentCheck ();

+    ArmEnableInstructionCache ();

+    ArmEnableDataCache ();

+

+    ArmEnableMmu ();

+  }

-  ArmEnableMmu ();

    return EFI_SUCCESS;

  FreeTranslationTable:




-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#94367): https://edk2.groups.io/g/devel/message/94367
Mute This Topic: https://groups.io/mt/93922695/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to