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 #define BITS_PER_LEVEL 9
62 #define MAX_VA_BITS 48
66 GetRootTableEntryCount (
70 return TT_ENTRY_COUNT
>> (T0SZ
- MIN_T0SZ
) % BITS_PER_LEVEL
;
74 GetRootTranslationTableInfo (
76 OUT UINTN
*TableLevel
,
77 OUT UINTN
*TableEntryCount
80 // Get the level of the root table
82 *TableLevel
= (T0SZ
- MIN_T0SZ
) / BITS_PER_LEVEL
;
85 if (TableEntryCount
) {
86 *TableEntryCount
= 1UL << (BITS_PER_LEVEL
- (T0SZ
- MIN_T0SZ
) % BITS_PER_LEVEL
);
95 IN UINT64 RegionStart
,
96 IN BOOLEAN IsLiveBlockMapping
99 if (!ArmMmuEnabled () || !IsLiveBlockMapping
) {
101 ArmUpdateTranslationTableEntry (Entry
, (VOID
*)(UINTN
)RegionStart
);
103 ArmReplaceLiveTranslationEntry (Entry
, Value
, RegionStart
);
109 FreePageTablesRecursive (
110 IN UINT64
*TranslationTable
,
119 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
120 if ((TranslationTable
[Index
] & TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
121 FreePageTablesRecursive ((VOID
*)(UINTN
)(TranslationTable
[Index
] &
122 TT_ADDRESS_MASK_BLOCK_ENTRY
),
127 FreePages (TranslationTable
, 1);
138 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY_LEVEL3
;
140 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
;
152 // TT_TYPE_TABLE_ENTRY aliases TT_TYPE_BLOCK_ENTRY_LEVEL3
153 // so we need to take the level into account as well.
157 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
;
162 UpdateRegionMappingRecursive (
163 IN UINT64 RegionStart
,
165 IN UINT64 AttributeSetMask
,
166 IN UINT64 AttributeClearMask
,
167 IN UINT64
*PageTable
,
176 VOID
*TranslationTable
;
179 ASSERT (((RegionStart
| RegionEnd
) & EFI_PAGE_MASK
) == 0);
181 BlockShift
= (Level
+ 1) * BITS_PER_LEVEL
+ MIN_T0SZ
;
182 BlockMask
= MAX_UINT64
>> BlockShift
;
184 DEBUG ((DEBUG_VERBOSE
, "%a(%d): %llx - %llx set %lx clr %lx\n", __FUNCTION__
,
185 Level
, RegionStart
, RegionEnd
, AttributeSetMask
, AttributeClearMask
));
187 for (; RegionStart
< RegionEnd
; RegionStart
= BlockEnd
) {
188 BlockEnd
= MIN (RegionEnd
, (RegionStart
| BlockMask
) + 1);
189 Entry
= &PageTable
[(RegionStart
>> (64 - BlockShift
)) & (TT_ENTRY_COUNT
- 1)];
192 // If RegionStart or BlockEnd is not aligned to the block size at this
193 // level, we will have to create a table mapping in order to map less
194 // than a block, and recurse to create the block or page entries at
195 // the next level. No block mappings are allowed at all at level 0,
196 // so in that case, we have to recurse unconditionally.
197 // If we are changing a table entry and the AttributeClearMask is non-zero,
198 // we cannot replace it with a block entry without potentially losing
199 // attribute information, so keep the table entry in that case.
201 if (Level
== 0 || ((RegionStart
| BlockEnd
) & BlockMask
) != 0 ||
202 (IsTableEntry (*Entry
, Level
) && AttributeClearMask
!= 0)) {
205 if (!IsTableEntry (*Entry
, Level
)) {
207 // No table entry exists yet, so we need to allocate a page table
208 // for the next level.
210 TranslationTable
= AllocatePages (1);
211 if (TranslationTable
== NULL
) {
212 return EFI_OUT_OF_RESOURCES
;
215 if (!ArmMmuEnabled ()) {
217 // Make sure we are not inadvertently hitting in the caches
218 // when populating the page tables.
220 InvalidateDataCacheRange (TranslationTable
, EFI_PAGE_SIZE
);
223 ZeroMem (TranslationTable
, EFI_PAGE_SIZE
);
225 if (IsBlockEntry (*Entry
, Level
)) {
227 // We are splitting an existing block entry, so we have to populate
228 // the new table with the attributes of the block entry it replaces.
230 Status
= UpdateRegionMappingRecursive (RegionStart
& ~BlockMask
,
231 (RegionStart
| BlockMask
) + 1, *Entry
& TT_ATTRIBUTES_MASK
,
232 0, TranslationTable
, Level
+ 1);
233 if (EFI_ERROR (Status
)) {
235 // The range we passed to UpdateRegionMappingRecursive () is block
236 // aligned, so it is guaranteed that no further pages were allocated
237 // by it, and so we only have to free the page we allocated here.
239 FreePages (TranslationTable
, 1);
244 TranslationTable
= (VOID
*)(UINTN
)(*Entry
& TT_ADDRESS_MASK_BLOCK_ENTRY
);
248 // Recurse to the next level
250 Status
= UpdateRegionMappingRecursive (RegionStart
, BlockEnd
,
251 AttributeSetMask
, AttributeClearMask
, TranslationTable
,
253 if (EFI_ERROR (Status
)) {
254 if (!IsTableEntry (*Entry
, Level
)) {
256 // We are creating a new table entry, so on failure, we can free all
257 // allocations we made recursively, given that the whole subhierarchy
258 // has not been wired into the live page tables yet. (This is not
259 // possible for existing table entries, since we cannot revert the
260 // modifications we made to the subhierarchy it represents.)
262 FreePageTablesRecursive (TranslationTable
, Level
+ 1);
267 if (!IsTableEntry (*Entry
, Level
)) {
268 EntryValue
= (UINTN
)TranslationTable
| TT_TYPE_TABLE_ENTRY
;
269 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
,
270 IsBlockEntry (*Entry
, Level
));
273 EntryValue
= (*Entry
& AttributeClearMask
) | AttributeSetMask
;
274 EntryValue
|= RegionStart
;
275 EntryValue
|= (Level
== 3) ? TT_TYPE_BLOCK_ENTRY_LEVEL3
276 : TT_TYPE_BLOCK_ENTRY
;
278 if (IsTableEntry (*Entry
, Level
)) {
280 // We are replacing a table entry with a block entry. This is only
281 // possible if we are keeping none of the original attributes.
282 // We can free the table entry's page table, and all the ones below
283 // it, since we are dropping the only possible reference to it.
285 ASSERT (AttributeClearMask
== 0);
286 TranslationTable
= (VOID
*)(UINTN
)(*Entry
& TT_ADDRESS_MASK_BLOCK_ENTRY
);
287 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
, TRUE
);
288 FreePageTablesRecursive (TranslationTable
, Level
+ 1);
290 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
, FALSE
);
299 UpdateRegionMapping (
300 IN UINT64 RegionStart
,
301 IN UINT64 RegionLength
,
302 IN UINT64 AttributeSetMask
,
303 IN UINT64 AttributeClearMask
306 UINTN RootTableLevel
;
309 if (((RegionStart
| RegionLength
) & EFI_PAGE_MASK
)) {
310 return EFI_INVALID_PARAMETER
;
313 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
314 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, NULL
);
316 return UpdateRegionMappingRecursive (RegionStart
, RegionStart
+ RegionLength
,
317 AttributeSetMask
, AttributeClearMask
, ArmGetTTBR0BaseAddress (),
323 FillTranslationTable (
324 IN UINT64
*RootTable
,
325 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
328 return UpdateRegionMapping (
329 MemoryRegion
->VirtualBase
,
330 MemoryRegion
->Length
,
331 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
338 GcdAttributeToPageAttribute (
339 IN UINT64 GcdAttributes
342 UINT64 PageAttributes
;
344 switch (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) {
346 PageAttributes
= TT_ATTR_INDX_DEVICE_MEMORY
;
349 PageAttributes
= TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
352 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
355 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
358 PageAttributes
= TT_ATTR_INDX_MASK
;
362 if ((GcdAttributes
& EFI_MEMORY_XP
) != 0 ||
363 (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) == EFI_MEMORY_UC
) {
364 if (ArmReadCurrentEL () == AARCH64_EL2
) {
365 PageAttributes
|= TT_XN_MASK
;
367 PageAttributes
|= TT_UXN_MASK
| TT_PXN_MASK
;
371 if ((GcdAttributes
& EFI_MEMORY_RO
) != 0) {
372 PageAttributes
|= TT_AP_RO_RO
;
375 return PageAttributes
| TT_AF
;
379 ArmSetMemoryAttributes (
380 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
385 UINT64 PageAttributes
;
386 UINT64 PageAttributeMask
;
388 PageAttributes
= GcdAttributeToPageAttribute (Attributes
);
389 PageAttributeMask
= 0;
391 if ((Attributes
& EFI_MEMORY_CACHETYPE_MASK
) == 0) {
393 // No memory type was set in Attributes, so we are going to update the
396 PageAttributes
&= TT_AP_MASK
| TT_UXN_MASK
| TT_PXN_MASK
;
397 PageAttributeMask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
|
398 TT_PXN_MASK
| TT_XN_MASK
);
401 return UpdateRegionMapping (BaseAddress
, Length
, PageAttributes
,
407 SetMemoryRegionAttribute (
408 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
410 IN UINT64 Attributes
,
411 IN UINT64 BlockEntryMask
414 return UpdateRegionMapping (BaseAddress
, Length
, Attributes
, BlockEntryMask
);
418 ArmSetMemoryRegionNoExec (
419 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
425 if (ArmReadCurrentEL () == AARCH64_EL1
) {
426 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
431 return SetMemoryRegionAttribute (
435 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
439 ArmClearMemoryRegionNoExec (
440 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
446 // XN maps to UXN in the EL1&0 translation regime
447 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
449 return SetMemoryRegionAttribute (
457 ArmSetMemoryRegionReadOnly (
458 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
462 return SetMemoryRegionAttribute (
466 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
470 ArmClearMemoryRegionReadOnly (
471 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
475 return SetMemoryRegionAttribute (
479 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
485 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
486 OUT VOID
**TranslationTableBase OPTIONAL
,
487 OUT UINTN
*TranslationTableSize OPTIONAL
490 VOID
* TranslationTable
;
491 UINTN MaxAddressBits
;
494 UINTN RootTableEntryCount
;
498 if (MemoryTable
== NULL
) {
499 ASSERT (MemoryTable
!= NULL
);
500 return EFI_INVALID_PARAMETER
;
504 // Limit the virtual address space to what we can actually use: UEFI
505 // mandates a 1:1 mapping, so no point in making the virtual address
506 // space larger than the physical address space. We also have to take
507 // into account the architectural limitations that result from UEFI's
508 // use of 4 KB pages.
510 MaxAddressBits
= MIN (ArmGetPhysicalAddressBits (), MAX_VA_BITS
);
511 MaxAddress
= LShiftU64 (1ULL, MaxAddressBits
) - 1;
513 T0SZ
= 64 - MaxAddressBits
;
514 RootTableEntryCount
= GetRootTableEntryCount (T0SZ
);
517 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
519 // Ideally we will be running at EL2, but should support EL1 as well.
520 // UEFI should not run at EL3.
521 if (ArmReadCurrentEL () == AARCH64_EL2
) {
522 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
523 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
525 // Set the Physical Address Size using MaxAddress
526 if (MaxAddress
< SIZE_4GB
) {
528 } else if (MaxAddress
< SIZE_64GB
) {
530 } else if (MaxAddress
< SIZE_1TB
) {
532 } else if (MaxAddress
< SIZE_4TB
) {
534 } else if (MaxAddress
< SIZE_16TB
) {
536 } else if (MaxAddress
< SIZE_256TB
) {
540 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
542 ASSERT (0); // Bigger than 48-bit memory space are not supported
543 return EFI_UNSUPPORTED
;
545 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
546 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
547 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
549 // Set the Physical Address Size using MaxAddress
550 if (MaxAddress
< SIZE_4GB
) {
552 } else if (MaxAddress
< SIZE_64GB
) {
554 } else if (MaxAddress
< SIZE_1TB
) {
556 } else if (MaxAddress
< SIZE_4TB
) {
558 } else if (MaxAddress
< SIZE_16TB
) {
560 } else if (MaxAddress
< SIZE_256TB
) {
561 TCR
|= TCR_IPS_256TB
;
564 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
566 ASSERT (0); // Bigger than 48-bit memory space are not supported
567 return EFI_UNSUPPORTED
;
570 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
571 return EFI_UNSUPPORTED
;
575 // Translation table walks are always cache coherent on ARMv8-A, so cache
576 // maintenance on page tables is never needed. Since there is a risk of
577 // loss of coherency when using mismatched attributes, and given that memory
578 // is mapped cacheable except for extraordinary cases (such as non-coherent
579 // DMA), have the page table walker perform cached accesses as well, and
580 // assert below that that matches the attributes we use for CPU accesses to
583 TCR
|= TCR_SH_INNER_SHAREABLE
|
584 TCR_RGN_OUTER_WRITE_BACK_ALLOC
|
585 TCR_RGN_INNER_WRITE_BACK_ALLOC
;
590 // Allocate pages for translation table
591 TranslationTable
= AllocatePages (1);
592 if (TranslationTable
== NULL
) {
593 return EFI_OUT_OF_RESOURCES
;
596 // We set TTBR0 just after allocating the table to retrieve its location from
597 // the subsequent functions without needing to pass this value across the
598 // functions. The MMU is only enabled after the translation tables are
601 ArmSetTTBR0 (TranslationTable
);
603 if (TranslationTableBase
!= NULL
) {
604 *TranslationTableBase
= TranslationTable
;
607 if (TranslationTableSize
!= NULL
) {
608 *TranslationTableSize
= RootTableEntryCount
* sizeof (UINT64
);
612 // Make sure we are not inadvertently hitting in the caches
613 // when populating the page tables.
615 InvalidateDataCacheRange (TranslationTable
,
616 RootTableEntryCount
* sizeof (UINT64
));
617 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof (UINT64
));
619 while (MemoryTable
->Length
!= 0) {
620 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
621 if (EFI_ERROR (Status
)) {
622 goto FreeTranslationTable
;
628 // EFI_MEMORY_UC ==> MAIR_ATTR_DEVICE_MEMORY
629 // EFI_MEMORY_WC ==> MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
630 // EFI_MEMORY_WT ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
631 // EFI_MEMORY_WB ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
634 MAIR_ATTR (TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) |
635 MAIR_ATTR (TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) |
636 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) |
637 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)
640 ArmDisableAlignmentCheck ();
641 ArmEnableStackAlignmentCheck ();
642 ArmEnableInstructionCache ();
643 ArmEnableDataCache ();
648 FreeTranslationTable
:
649 FreePages (TranslationTable
, 1);
655 ArmMmuBaseLibConstructor (
659 extern UINT32 ArmReplaceLiveTranslationEntrySize
;
662 // The ArmReplaceLiveTranslationEntry () helper function may be invoked
663 // with the MMU off so we have to ensure that it gets cleaned to the PoC
665 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry
,
666 ArmReplaceLiveTranslationEntrySize
);
668 return RETURN_SUCCESS
;