2 * File managing the MMU for ARMv8 architecture
4 * Copyright (c) 2011-2020, ARM Limited. All rights reserved.
5 * Copyright (c) 2016, Linaro Limited. All rights reserved.
6 * Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
8 * SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Chipset/AArch64.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/CacheMaintenanceLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/ArmLib.h>
18 #include <Library/ArmMmuLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
22 // We use this index definition to define an invalid block entry
23 #define TT_ATTR_INDX_INVALID ((UINT32)~0)
27 ArmMemoryAttributeToPageAttribute (
28 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes
32 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_NONSHAREABLE
:
33 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK_NONSHAREABLE
:
34 return TT_ATTR_INDX_MEMORY_WRITE_BACK
;
36 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
:
37 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
:
38 return TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
40 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
:
41 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
:
42 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
44 // Uncached and device mappings are treated as outer shareable by default,
45 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
:
46 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
:
47 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
51 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
:
52 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE
:
53 if (ArmReadCurrentEL () == AARCH64_EL2
)
54 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_XN_MASK
;
56 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_UXN_MASK
| TT_PXN_MASK
;
61 PageAttributeToGcdAttribute (
62 IN UINT64 PageAttributes
67 switch (PageAttributes
& TT_ATTR_INDX_MASK
) {
68 case TT_ATTR_INDX_DEVICE_MEMORY
:
69 GcdAttributes
= EFI_MEMORY_UC
;
71 case TT_ATTR_INDX_MEMORY_NON_CACHEABLE
:
72 GcdAttributes
= EFI_MEMORY_WC
;
74 case TT_ATTR_INDX_MEMORY_WRITE_THROUGH
:
75 GcdAttributes
= EFI_MEMORY_WT
;
77 case TT_ATTR_INDX_MEMORY_WRITE_BACK
:
78 GcdAttributes
= EFI_MEMORY_WB
;
82 "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n",
85 // The Global Coherency Domain (GCD) value is defined as a bit set.
86 // Returning 0 means no attribute has been set.
90 // Determine protection attributes
91 if (((PageAttributes
& TT_AP_MASK
) == TT_AP_NO_RO
) ||
92 ((PageAttributes
& TT_AP_MASK
) == TT_AP_RO_RO
)) {
93 // Read only cases map to write-protect
94 GcdAttributes
|= EFI_MEMORY_RO
;
97 // Process eXecute Never attribute
98 if ((PageAttributes
& (TT_PXN_MASK
| TT_UXN_MASK
)) != 0) {
99 GcdAttributes
|= EFI_MEMORY_XP
;
102 return GcdAttributes
;
106 #define BITS_PER_LEVEL 9
109 GetRootTranslationTableInfo (
111 OUT UINTN
*TableLevel
,
112 OUT UINTN
*TableEntryCount
115 // Get the level of the root table
117 *TableLevel
= (T0SZ
- MIN_T0SZ
) / BITS_PER_LEVEL
;
120 if (TableEntryCount
) {
121 *TableEntryCount
= 1UL << (BITS_PER_LEVEL
- (T0SZ
- MIN_T0SZ
) % BITS_PER_LEVEL
);
130 IN UINT64 RegionStart
,
131 IN BOOLEAN IsLiveBlockMapping
134 if (!ArmMmuEnabled () || !IsLiveBlockMapping
) {
136 ArmUpdateTranslationTableEntry (Entry
, (VOID
*)(UINTN
)RegionStart
);
138 ArmReplaceLiveTranslationEntry (Entry
, Value
, RegionStart
);
144 FreePageTablesRecursive (
145 IN UINT64
*TranslationTable
,
154 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
155 if ((TranslationTable
[Index
] & TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
156 FreePageTablesRecursive ((VOID
*)(UINTN
)(TranslationTable
[Index
] &
157 TT_ADDRESS_MASK_BLOCK_ENTRY
),
162 FreePages (TranslationTable
, 1);
173 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY_LEVEL3
;
175 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
;
187 // TT_TYPE_TABLE_ENTRY aliases TT_TYPE_BLOCK_ENTRY_LEVEL3
188 // so we need to take the level into account as well.
192 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
;
197 UpdateRegionMappingRecursive (
198 IN UINT64 RegionStart
,
200 IN UINT64 AttributeSetMask
,
201 IN UINT64 AttributeClearMask
,
202 IN UINT64
*PageTable
,
211 VOID
*TranslationTable
;
214 ASSERT (((RegionStart
| RegionEnd
) & EFI_PAGE_MASK
) == 0);
216 BlockShift
= (Level
+ 1) * BITS_PER_LEVEL
+ MIN_T0SZ
;
217 BlockMask
= MAX_UINT64
>> BlockShift
;
219 DEBUG ((DEBUG_VERBOSE
, "%a(%d): %llx - %llx set %lx clr %lx\n", __FUNCTION__
,
220 Level
, RegionStart
, RegionEnd
, AttributeSetMask
, AttributeClearMask
));
222 for (; RegionStart
< RegionEnd
; RegionStart
= BlockEnd
) {
223 BlockEnd
= MIN (RegionEnd
, (RegionStart
| BlockMask
) + 1);
224 Entry
= &PageTable
[(RegionStart
>> (64 - BlockShift
)) & (TT_ENTRY_COUNT
- 1)];
227 // If RegionStart or BlockEnd is not aligned to the block size at this
228 // level, we will have to create a table mapping in order to map less
229 // than a block, and recurse to create the block or page entries at
230 // the next level. No block mappings are allowed at all at level 0,
231 // so in that case, we have to recurse unconditionally.
233 if (Level
== 0 || ((RegionStart
| BlockEnd
) & BlockMask
) != 0) {
236 if (!IsTableEntry (*Entry
, Level
)) {
238 // No table entry exists yet, so we need to allocate a page table
239 // for the next level.
241 TranslationTable
= AllocatePages (1);
242 if (TranslationTable
== NULL
) {
243 return EFI_OUT_OF_RESOURCES
;
246 if (!ArmMmuEnabled ()) {
248 // Make sure we are not inadvertently hitting in the caches
249 // when populating the page tables.
251 InvalidateDataCacheRange (TranslationTable
, EFI_PAGE_SIZE
);
254 if (IsBlockEntry (*Entry
, Level
)) {
256 // We are splitting an existing block entry, so we have to populate
257 // the new table with the attributes of the block entry it replaces.
259 Status
= UpdateRegionMappingRecursive (RegionStart
& ~BlockMask
,
260 (RegionStart
| BlockMask
) + 1, *Entry
& TT_ATTRIBUTES_MASK
,
261 0, TranslationTable
, Level
+ 1);
262 if (EFI_ERROR (Status
)) {
264 // The range we passed to UpdateRegionMappingRecursive () is block
265 // aligned, so it is guaranteed that no further pages were allocated
266 // by it, and so we only have to free the page we allocated here.
268 FreePages (TranslationTable
, 1);
272 ZeroMem (TranslationTable
, EFI_PAGE_SIZE
);
275 TranslationTable
= (VOID
*)(UINTN
)(*Entry
& TT_ADDRESS_MASK_BLOCK_ENTRY
);
279 // Recurse to the next level
281 Status
= UpdateRegionMappingRecursive (RegionStart
, BlockEnd
,
282 AttributeSetMask
, AttributeClearMask
, TranslationTable
,
284 if (EFI_ERROR (Status
)) {
285 if (!IsTableEntry (*Entry
, Level
)) {
287 // We are creating a new table entry, so on failure, we can free all
288 // allocations we made recursively, given that the whole subhierarchy
289 // has not been wired into the live page tables yet. (This is not
290 // possible for existing table entries, since we cannot revert the
291 // modifications we made to the subhierarchy it represents.)
293 FreePageTablesRecursive (TranslationTable
, Level
+ 1);
298 if (!IsTableEntry (*Entry
, Level
)) {
299 EntryValue
= (UINTN
)TranslationTable
| TT_TYPE_TABLE_ENTRY
;
300 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
,
301 IsBlockEntry (*Entry
, Level
));
304 EntryValue
= (*Entry
& AttributeClearMask
) | AttributeSetMask
;
305 EntryValue
|= RegionStart
;
306 EntryValue
|= (Level
== 3) ? TT_TYPE_BLOCK_ENTRY_LEVEL3
307 : TT_TYPE_BLOCK_ENTRY
;
309 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
, FALSE
);
317 LookupAddresstoRootTable (
318 IN UINT64 MaxAddress
,
320 OUT UINTN
*TableEntryCount
325 // Check the parameters are not NULL
326 ASSERT ((T0SZ
!= NULL
) && (TableEntryCount
!= NULL
));
328 // Look for the highest bit set in MaxAddress
329 for (TopBit
= 63; TopBit
!= 0; TopBit
--) {
330 if ((1ULL << TopBit
) & MaxAddress
) {
331 // MaxAddress top bit is found
336 ASSERT (TopBit
!= 0);
338 // Calculate T0SZ from the top bit of the MaxAddress
341 // Get the Table info from T0SZ
342 GetRootTranslationTableInfo (*T0SZ
, NULL
, TableEntryCount
);
347 UpdateRegionMapping (
348 IN UINT64 RegionStart
,
349 IN UINT64 RegionLength
,
350 IN UINT64 AttributeSetMask
,
351 IN UINT64 AttributeClearMask
354 UINTN RootTableLevel
;
357 if (((RegionStart
| RegionLength
) & EFI_PAGE_MASK
)) {
358 return EFI_INVALID_PARAMETER
;
361 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
362 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, NULL
);
364 return UpdateRegionMappingRecursive (RegionStart
, RegionStart
+ RegionLength
,
365 AttributeSetMask
, AttributeClearMask
, ArmGetTTBR0BaseAddress (),
371 FillTranslationTable (
372 IN UINT64
*RootTable
,
373 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
376 return UpdateRegionMapping (
377 MemoryRegion
->VirtualBase
,
378 MemoryRegion
->Length
,
379 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
386 GcdAttributeToPageAttribute (
387 IN UINT64 GcdAttributes
390 UINT64 PageAttributes
;
392 switch (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) {
394 PageAttributes
= TT_ATTR_INDX_DEVICE_MEMORY
;
397 PageAttributes
= TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
400 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
403 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
406 PageAttributes
= TT_ATTR_INDX_MASK
;
410 if ((GcdAttributes
& EFI_MEMORY_XP
) != 0 ||
411 (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) == EFI_MEMORY_UC
) {
412 if (ArmReadCurrentEL () == AARCH64_EL2
) {
413 PageAttributes
|= TT_XN_MASK
;
415 PageAttributes
|= TT_UXN_MASK
| TT_PXN_MASK
;
419 if ((GcdAttributes
& EFI_MEMORY_RO
) != 0) {
420 PageAttributes
|= TT_AP_RO_RO
;
423 return PageAttributes
| TT_AF
;
427 ArmSetMemoryAttributes (
428 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
433 UINT64 PageAttributes
;
434 UINT64 PageAttributeMask
;
436 PageAttributes
= GcdAttributeToPageAttribute (Attributes
);
437 PageAttributeMask
= 0;
439 if ((Attributes
& EFI_MEMORY_CACHETYPE_MASK
) == 0) {
441 // No memory type was set in Attributes, so we are going to update the
444 PageAttributes
&= TT_AP_MASK
| TT_UXN_MASK
| TT_PXN_MASK
;
445 PageAttributeMask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
|
446 TT_PXN_MASK
| TT_XN_MASK
);
449 return UpdateRegionMapping (BaseAddress
, Length
, PageAttributes
,
455 SetMemoryRegionAttribute (
456 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
458 IN UINT64 Attributes
,
459 IN UINT64 BlockEntryMask
462 return UpdateRegionMapping (BaseAddress
, Length
, Attributes
, BlockEntryMask
);
466 ArmSetMemoryRegionNoExec (
467 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
473 if (ArmReadCurrentEL () == AARCH64_EL1
) {
474 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
479 return SetMemoryRegionAttribute (
483 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
487 ArmClearMemoryRegionNoExec (
488 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
494 // XN maps to UXN in the EL1&0 translation regime
495 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
497 return SetMemoryRegionAttribute (
505 ArmSetMemoryRegionReadOnly (
506 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
510 return SetMemoryRegionAttribute (
514 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
518 ArmClearMemoryRegionReadOnly (
519 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
523 return SetMemoryRegionAttribute (
527 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
533 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
534 OUT VOID
**TranslationTableBase OPTIONAL
,
535 OUT UINTN
*TranslationTableSize OPTIONAL
538 VOID
* TranslationTable
;
541 UINTN RootTableEntryCount
;
545 if (MemoryTable
== NULL
) {
546 ASSERT (MemoryTable
!= NULL
);
547 return EFI_INVALID_PARAMETER
;
551 // Limit the virtual address space to what we can actually use: UEFI
552 // mandates a 1:1 mapping, so no point in making the virtual address
553 // space larger than the physical address space. We also have to take
554 // into account the architectural limitations that result from UEFI's
555 // use of 4 KB pages.
557 MaxAddress
= MIN (LShiftU64 (1ULL, ArmGetPhysicalAddressBits ()) - 1,
560 // Lookup the Table Level to get the information
561 LookupAddresstoRootTable (MaxAddress
, &T0SZ
, &RootTableEntryCount
);
564 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
566 // Ideally we will be running at EL2, but should support EL1 as well.
567 // UEFI should not run at EL3.
568 if (ArmReadCurrentEL () == AARCH64_EL2
) {
569 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
570 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
572 // Set the Physical Address Size using MaxAddress
573 if (MaxAddress
< SIZE_4GB
) {
575 } else if (MaxAddress
< SIZE_64GB
) {
577 } else if (MaxAddress
< SIZE_1TB
) {
579 } else if (MaxAddress
< SIZE_4TB
) {
581 } else if (MaxAddress
< SIZE_16TB
) {
583 } else if (MaxAddress
< SIZE_256TB
) {
587 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
589 ASSERT (0); // Bigger than 48-bit memory space are not supported
590 return EFI_UNSUPPORTED
;
592 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
593 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
594 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
596 // Set the Physical Address Size using MaxAddress
597 if (MaxAddress
< SIZE_4GB
) {
599 } else if (MaxAddress
< SIZE_64GB
) {
601 } else if (MaxAddress
< SIZE_1TB
) {
603 } else if (MaxAddress
< SIZE_4TB
) {
605 } else if (MaxAddress
< SIZE_16TB
) {
607 } else if (MaxAddress
< SIZE_256TB
) {
608 TCR
|= TCR_IPS_256TB
;
611 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
613 ASSERT (0); // Bigger than 48-bit memory space are not supported
614 return EFI_UNSUPPORTED
;
617 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
618 return EFI_UNSUPPORTED
;
622 // Translation table walks are always cache coherent on ARMv8-A, so cache
623 // maintenance on page tables is never needed. Since there is a risk of
624 // loss of coherency when using mismatched attributes, and given that memory
625 // is mapped cacheable except for extraordinary cases (such as non-coherent
626 // DMA), have the page table walker perform cached accesses as well, and
627 // assert below that that matches the attributes we use for CPU accesses to
630 TCR
|= TCR_SH_INNER_SHAREABLE
|
631 TCR_RGN_OUTER_WRITE_BACK_ALLOC
|
632 TCR_RGN_INNER_WRITE_BACK_ALLOC
;
637 // Allocate pages for translation table
638 TranslationTable
= AllocatePages (1);
639 if (TranslationTable
== NULL
) {
640 return EFI_OUT_OF_RESOURCES
;
643 // We set TTBR0 just after allocating the table to retrieve its location from
644 // the subsequent functions without needing to pass this value across the
645 // functions. The MMU is only enabled after the translation tables are
648 ArmSetTTBR0 (TranslationTable
);
650 if (TranslationTableBase
!= NULL
) {
651 *TranslationTableBase
= TranslationTable
;
654 if (TranslationTableSize
!= NULL
) {
655 *TranslationTableSize
= RootTableEntryCount
* sizeof (UINT64
);
659 // Make sure we are not inadvertently hitting in the caches
660 // when populating the page tables.
662 InvalidateDataCacheRange (TranslationTable
,
663 RootTableEntryCount
* sizeof (UINT64
));
664 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof (UINT64
));
666 while (MemoryTable
->Length
!= 0) {
667 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
668 if (EFI_ERROR (Status
)) {
669 goto FreeTranslationTable
;
675 // EFI_MEMORY_UC ==> MAIR_ATTR_DEVICE_MEMORY
676 // EFI_MEMORY_WC ==> MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
677 // EFI_MEMORY_WT ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
678 // EFI_MEMORY_WB ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
681 MAIR_ATTR (TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) |
682 MAIR_ATTR (TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) |
683 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) |
684 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)
687 ArmDisableAlignmentCheck ();
688 ArmEnableStackAlignmentCheck ();
689 ArmEnableInstructionCache ();
690 ArmEnableDataCache ();
695 FreeTranslationTable
:
696 FreePages (TranslationTable
, 1);
702 ArmMmuBaseLibConstructor (
706 extern UINT32 ArmReplaceLiveTranslationEntrySize
;
709 // The ArmReplaceLiveTranslationEntry () helper function may be invoked
710 // with the MMU off so we have to ensure that it gets cleaned to the PoC
712 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry
,
713 ArmReplaceLiveTranslationEntrySize
);
715 return RETURN_SUCCESS
;