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
150 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
151 if ((TranslationTable
[Index
] & TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
152 FreePageTablesRecursive ((VOID
*)(UINTN
)(TranslationTable
[Index
] &
153 TT_ADDRESS_MASK_BLOCK_ENTRY
));
156 FreePages (TranslationTable
, 1);
161 UpdateRegionMappingRecursive (
162 IN UINT64 RegionStart
,
164 IN UINT64 AttributeSetMask
,
165 IN UINT64 AttributeClearMask
,
166 IN UINT64
*PageTable
,
175 VOID
*TranslationTable
;
178 ASSERT (((RegionStart
| RegionEnd
) & EFI_PAGE_MASK
) == 0);
180 BlockShift
= (Level
+ 1) * BITS_PER_LEVEL
+ MIN_T0SZ
;
181 BlockMask
= MAX_UINT64
>> BlockShift
;
183 DEBUG ((DEBUG_VERBOSE
, "%a(%d): %llx - %llx set %lx clr %lx\n", __FUNCTION__
,
184 Level
, RegionStart
, RegionEnd
, AttributeSetMask
, AttributeClearMask
));
186 for (; RegionStart
< RegionEnd
; RegionStart
= BlockEnd
) {
187 BlockEnd
= MIN (RegionEnd
, (RegionStart
| BlockMask
) + 1);
188 Entry
= &PageTable
[(RegionStart
>> (64 - BlockShift
)) & (TT_ENTRY_COUNT
- 1)];
191 // If RegionStart or BlockEnd is not aligned to the block size at this
192 // level, we will have to create a table mapping in order to map less
193 // than a block, and recurse to create the block or page entries at
194 // the next level. No block mappings are allowed at all at level 0,
195 // so in that case, we have to recurse unconditionally.
197 if (Level
== 0 || ((RegionStart
| BlockEnd
) & BlockMask
) != 0) {
200 if ((*Entry
& TT_TYPE_MASK
) != TT_TYPE_TABLE_ENTRY
) {
202 // No table entry exists yet, so we need to allocate a page table
203 // for the next level.
205 TranslationTable
= AllocatePages (1);
206 if (TranslationTable
== NULL
) {
207 return EFI_OUT_OF_RESOURCES
;
210 if (!ArmMmuEnabled ()) {
212 // Make sure we are not inadvertently hitting in the caches
213 // when populating the page tables.
215 InvalidateDataCacheRange (TranslationTable
, EFI_PAGE_SIZE
);
218 if ((*Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
) {
220 // We are splitting an existing block entry, so we have to populate
221 // the new table with the attributes of the block entry it replaces.
223 Status
= UpdateRegionMappingRecursive (RegionStart
& ~BlockMask
,
224 (RegionStart
| BlockMask
) + 1, *Entry
& TT_ATTRIBUTES_MASK
,
225 0, TranslationTable
, Level
+ 1);
226 if (EFI_ERROR (Status
)) {
228 // The range we passed to UpdateRegionMappingRecursive () is block
229 // aligned, so it is guaranteed that no further pages were allocated
230 // by it, and so we only have to free the page we allocated here.
232 FreePages (TranslationTable
, 1);
236 ZeroMem (TranslationTable
, EFI_PAGE_SIZE
);
239 TranslationTable
= (VOID
*)(UINTN
)(*Entry
& TT_ADDRESS_MASK_BLOCK_ENTRY
);
243 // Recurse to the next level
245 Status
= UpdateRegionMappingRecursive (RegionStart
, BlockEnd
,
246 AttributeSetMask
, AttributeClearMask
, TranslationTable
,
248 if (EFI_ERROR (Status
)) {
249 if ((*Entry
& TT_TYPE_MASK
) != TT_TYPE_TABLE_ENTRY
) {
251 // We are creating a new table entry, so on failure, we can free all
252 // allocations we made recursively, given that the whole subhierarchy
253 // has not been wired into the live page tables yet. (This is not
254 // possible for existing table entries, since we cannot revert the
255 // modifications we made to the subhierarchy it represents.)
257 FreePageTablesRecursive (TranslationTable
);
262 if ((*Entry
& TT_TYPE_MASK
) != TT_TYPE_TABLE_ENTRY
) {
263 EntryValue
= (UINTN
)TranslationTable
| TT_TYPE_TABLE_ENTRY
;
264 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
,
265 (*Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
);
268 EntryValue
= (*Entry
& AttributeClearMask
) | AttributeSetMask
;
269 EntryValue
|= RegionStart
;
270 EntryValue
|= (Level
== 3) ? TT_TYPE_BLOCK_ENTRY_LEVEL3
271 : TT_TYPE_BLOCK_ENTRY
;
273 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
, FALSE
);
281 LookupAddresstoRootTable (
282 IN UINT64 MaxAddress
,
284 OUT UINTN
*TableEntryCount
289 // Check the parameters are not NULL
290 ASSERT ((T0SZ
!= NULL
) && (TableEntryCount
!= NULL
));
292 // Look for the highest bit set in MaxAddress
293 for (TopBit
= 63; TopBit
!= 0; TopBit
--) {
294 if ((1ULL << TopBit
) & MaxAddress
) {
295 // MaxAddress top bit is found
300 ASSERT (TopBit
!= 0);
302 // Calculate T0SZ from the top bit of the MaxAddress
305 // Get the Table info from T0SZ
306 GetRootTranslationTableInfo (*T0SZ
, NULL
, TableEntryCount
);
311 UpdateRegionMapping (
312 IN UINT64 RegionStart
,
313 IN UINT64 RegionLength
,
314 IN UINT64 AttributeSetMask
,
315 IN UINT64 AttributeClearMask
318 UINTN RootTableLevel
;
321 if (((RegionStart
| RegionLength
) & EFI_PAGE_MASK
)) {
322 return EFI_INVALID_PARAMETER
;
325 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
326 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, NULL
);
328 return UpdateRegionMappingRecursive (RegionStart
, RegionStart
+ RegionLength
,
329 AttributeSetMask
, AttributeClearMask
, ArmGetTTBR0BaseAddress (),
335 FillTranslationTable (
336 IN UINT64
*RootTable
,
337 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
340 return UpdateRegionMapping (
341 MemoryRegion
->VirtualBase
,
342 MemoryRegion
->Length
,
343 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
350 GcdAttributeToPageAttribute (
351 IN UINT64 GcdAttributes
354 UINT64 PageAttributes
;
356 switch (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) {
358 PageAttributes
= TT_ATTR_INDX_DEVICE_MEMORY
;
361 PageAttributes
= TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
364 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
367 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
370 PageAttributes
= TT_ATTR_INDX_MASK
;
374 if ((GcdAttributes
& EFI_MEMORY_XP
) != 0 ||
375 (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) == EFI_MEMORY_UC
) {
376 if (ArmReadCurrentEL () == AARCH64_EL2
) {
377 PageAttributes
|= TT_XN_MASK
;
379 PageAttributes
|= TT_UXN_MASK
| TT_PXN_MASK
;
383 if ((GcdAttributes
& EFI_MEMORY_RO
) != 0) {
384 PageAttributes
|= TT_AP_RO_RO
;
387 return PageAttributes
| TT_AF
;
391 ArmSetMemoryAttributes (
392 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
397 UINT64 PageAttributes
;
398 UINT64 PageAttributeMask
;
400 PageAttributes
= GcdAttributeToPageAttribute (Attributes
);
401 PageAttributeMask
= 0;
403 if ((Attributes
& EFI_MEMORY_CACHETYPE_MASK
) == 0) {
405 // No memory type was set in Attributes, so we are going to update the
408 PageAttributes
&= TT_AP_MASK
| TT_UXN_MASK
| TT_PXN_MASK
;
409 PageAttributeMask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
|
410 TT_PXN_MASK
| TT_XN_MASK
);
413 return UpdateRegionMapping (BaseAddress
, Length
, PageAttributes
,
419 SetMemoryRegionAttribute (
420 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
422 IN UINT64 Attributes
,
423 IN UINT64 BlockEntryMask
426 return UpdateRegionMapping (BaseAddress
, Length
, Attributes
, BlockEntryMask
);
430 ArmSetMemoryRegionNoExec (
431 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
437 if (ArmReadCurrentEL () == AARCH64_EL1
) {
438 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
443 return SetMemoryRegionAttribute (
447 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
451 ArmClearMemoryRegionNoExec (
452 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
458 // XN maps to UXN in the EL1&0 translation regime
459 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
461 return SetMemoryRegionAttribute (
469 ArmSetMemoryRegionReadOnly (
470 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
474 return SetMemoryRegionAttribute (
478 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
482 ArmClearMemoryRegionReadOnly (
483 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
487 return SetMemoryRegionAttribute (
491 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
497 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
498 OUT VOID
**TranslationTableBase OPTIONAL
,
499 OUT UINTN
*TranslationTableSize OPTIONAL
502 VOID
* TranslationTable
;
505 UINTN RootTableEntryCount
;
509 if (MemoryTable
== NULL
) {
510 ASSERT (MemoryTable
!= NULL
);
511 return EFI_INVALID_PARAMETER
;
515 // Limit the virtual address space to what we can actually use: UEFI
516 // mandates a 1:1 mapping, so no point in making the virtual address
517 // space larger than the physical address space. We also have to take
518 // into account the architectural limitations that result from UEFI's
519 // use of 4 KB pages.
521 MaxAddress
= MIN (LShiftU64 (1ULL, ArmGetPhysicalAddressBits ()) - 1,
524 // Lookup the Table Level to get the information
525 LookupAddresstoRootTable (MaxAddress
, &T0SZ
, &RootTableEntryCount
);
528 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
530 // Ideally we will be running at EL2, but should support EL1 as well.
531 // UEFI should not run at EL3.
532 if (ArmReadCurrentEL () == AARCH64_EL2
) {
533 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
534 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
536 // Set the Physical Address Size using MaxAddress
537 if (MaxAddress
< SIZE_4GB
) {
539 } else if (MaxAddress
< SIZE_64GB
) {
541 } else if (MaxAddress
< SIZE_1TB
) {
543 } else if (MaxAddress
< SIZE_4TB
) {
545 } else if (MaxAddress
< SIZE_16TB
) {
547 } else if (MaxAddress
< SIZE_256TB
) {
551 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
553 ASSERT (0); // Bigger than 48-bit memory space are not supported
554 return EFI_UNSUPPORTED
;
556 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
557 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
558 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
560 // Set the Physical Address Size using MaxAddress
561 if (MaxAddress
< SIZE_4GB
) {
563 } else if (MaxAddress
< SIZE_64GB
) {
565 } else if (MaxAddress
< SIZE_1TB
) {
567 } else if (MaxAddress
< SIZE_4TB
) {
569 } else if (MaxAddress
< SIZE_16TB
) {
571 } else if (MaxAddress
< SIZE_256TB
) {
572 TCR
|= TCR_IPS_256TB
;
575 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
577 ASSERT (0); // Bigger than 48-bit memory space are not supported
578 return EFI_UNSUPPORTED
;
581 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
582 return EFI_UNSUPPORTED
;
586 // Translation table walks are always cache coherent on ARMv8-A, so cache
587 // maintenance on page tables is never needed. Since there is a risk of
588 // loss of coherency when using mismatched attributes, and given that memory
589 // is mapped cacheable except for extraordinary cases (such as non-coherent
590 // DMA), have the page table walker perform cached accesses as well, and
591 // assert below that that matches the attributes we use for CPU accesses to
594 TCR
|= TCR_SH_INNER_SHAREABLE
|
595 TCR_RGN_OUTER_WRITE_BACK_ALLOC
|
596 TCR_RGN_INNER_WRITE_BACK_ALLOC
;
601 // Allocate pages for translation table
602 TranslationTable
= AllocatePages (1);
603 if (TranslationTable
== NULL
) {
604 return EFI_OUT_OF_RESOURCES
;
607 // We set TTBR0 just after allocating the table to retrieve its location from
608 // the subsequent functions without needing to pass this value across the
609 // functions. The MMU is only enabled after the translation tables are
612 ArmSetTTBR0 (TranslationTable
);
614 if (TranslationTableBase
!= NULL
) {
615 *TranslationTableBase
= TranslationTable
;
618 if (TranslationTableSize
!= NULL
) {
619 *TranslationTableSize
= RootTableEntryCount
* sizeof (UINT64
);
623 // Make sure we are not inadvertently hitting in the caches
624 // when populating the page tables.
626 InvalidateDataCacheRange (TranslationTable
,
627 RootTableEntryCount
* sizeof (UINT64
));
628 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof (UINT64
));
630 while (MemoryTable
->Length
!= 0) {
631 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
632 if (EFI_ERROR (Status
)) {
633 goto FreeTranslationTable
;
639 // EFI_MEMORY_UC ==> MAIR_ATTR_DEVICE_MEMORY
640 // EFI_MEMORY_WC ==> MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
641 // EFI_MEMORY_WT ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
642 // EFI_MEMORY_WB ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
645 MAIR_ATTR (TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) |
646 MAIR_ATTR (TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) |
647 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) |
648 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)
651 ArmDisableAlignmentCheck ();
652 ArmEnableStackAlignmentCheck ();
653 ArmEnableInstructionCache ();
654 ArmEnableDataCache ();
659 FreeTranslationTable
:
660 FreePages (TranslationTable
, 1);
666 ArmMmuBaseLibConstructor (
670 extern UINT32 ArmReplaceLiveTranslationEntrySize
;
673 // The ArmReplaceLiveTranslationEntry () helper function may be invoked
674 // with the MMU off so we have to ensure that it gets cleaned to the PoC
676 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry
,
677 ArmReplaceLiveTranslationEntrySize
);
679 return RETURN_SUCCESS
;