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
;
81 DEBUG ((EFI_D_ERROR
, "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n", PageAttributes
));
83 // The Global Coherency Domain (GCD) value is defined as a bit set.
84 // Returning 0 means no attribute has been set.
88 // Determine protection attributes
89 if (((PageAttributes
& TT_AP_MASK
) == TT_AP_NO_RO
) || ((PageAttributes
& TT_AP_MASK
) == TT_AP_RO_RO
)) {
90 // Read only cases map to write-protect
91 GcdAttributes
|= EFI_MEMORY_RO
;
94 // Process eXecute Never attribute
95 if ((PageAttributes
& (TT_PXN_MASK
| TT_UXN_MASK
)) != 0 ) {
96 GcdAttributes
|= EFI_MEMORY_XP
;
103 #define BITS_PER_LEVEL 9
106 GetRootTranslationTableInfo (
108 OUT UINTN
*TableLevel
,
109 OUT UINTN
*TableEntryCount
112 // Get the level of the root table
114 *TableLevel
= (T0SZ
- MIN_T0SZ
) / BITS_PER_LEVEL
;
117 if (TableEntryCount
) {
118 *TableEntryCount
= 1UL << (BITS_PER_LEVEL
- (T0SZ
- MIN_T0SZ
) % BITS_PER_LEVEL
);
127 IN UINT64 RegionStart
,
128 IN BOOLEAN IsLiveBlockMapping
131 if (!ArmMmuEnabled () || !IsLiveBlockMapping
) {
133 ArmUpdateTranslationTableEntry (Entry
, (VOID
*)(UINTN
)RegionStart
);
135 ArmReplaceLiveTranslationEntry (Entry
, Value
, RegionStart
);
141 FreePageTablesRecursive (
142 IN UINT64
*TranslationTable
147 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
148 if ((TranslationTable
[Index
] & TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
149 FreePageTablesRecursive ((VOID
*)(UINTN
)(TranslationTable
[Index
] &
150 TT_ADDRESS_MASK_BLOCK_ENTRY
));
153 FreePages (TranslationTable
, 1);
158 UpdateRegionMappingRecursive (
159 IN UINT64 RegionStart
,
161 IN UINT64 AttributeSetMask
,
162 IN UINT64 AttributeClearMask
,
163 IN UINT64
*PageTable
,
172 VOID
*TranslationTable
;
175 ASSERT (((RegionStart
| RegionEnd
) & EFI_PAGE_MASK
) == 0);
177 BlockShift
= (Level
+ 1) * BITS_PER_LEVEL
+ MIN_T0SZ
;
178 BlockMask
= MAX_UINT64
>> BlockShift
;
180 DEBUG ((DEBUG_VERBOSE
, "%a(%d): %llx - %llx set %lx clr %lx\n", __FUNCTION__
,
181 Level
, RegionStart
, RegionEnd
, AttributeSetMask
, AttributeClearMask
));
183 for (; RegionStart
< RegionEnd
; RegionStart
= BlockEnd
) {
184 BlockEnd
= MIN (RegionEnd
, (RegionStart
| BlockMask
) + 1);
185 Entry
= &PageTable
[(RegionStart
>> (64 - BlockShift
)) & (TT_ENTRY_COUNT
- 1)];
188 // If RegionStart or BlockEnd is not aligned to the block size at this
189 // level, we will have to create a table mapping in order to map less
190 // than a block, and recurse to create the block or page entries at
191 // the next level. No block mappings are allowed at all at level 0,
192 // so in that case, we have to recurse unconditionally.
194 if (Level
== 0 || ((RegionStart
| BlockEnd
) & BlockMask
) != 0) {
197 if ((*Entry
& TT_TYPE_MASK
) != TT_TYPE_TABLE_ENTRY
) {
199 // No table entry exists yet, so we need to allocate a page table
200 // for the next level.
202 TranslationTable
= AllocatePages (1);
203 if (TranslationTable
== NULL
) {
204 return EFI_OUT_OF_RESOURCES
;
207 if (!ArmMmuEnabled ()) {
209 // Make sure we are not inadvertently hitting in the caches
210 // when populating the page tables.
212 InvalidateDataCacheRange (TranslationTable
, EFI_PAGE_SIZE
);
215 if ((*Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
) {
217 // We are splitting an existing block entry, so we have to populate
218 // the new table with the attributes of the block entry it replaces.
220 Status
= UpdateRegionMappingRecursive (RegionStart
& ~BlockMask
,
221 (RegionStart
| BlockMask
) + 1, *Entry
& TT_ATTRIBUTES_MASK
,
222 0, TranslationTable
, Level
+ 1);
223 if (EFI_ERROR (Status
)) {
225 // The range we passed to UpdateRegionMappingRecursive () is block
226 // aligned, so it is guaranteed that no further pages were allocated
227 // by it, and so we only have to free the page we allocated here.
229 FreePages (TranslationTable
, 1);
233 ZeroMem (TranslationTable
, EFI_PAGE_SIZE
);
236 TranslationTable
= (VOID
*)(UINTN
)(*Entry
& TT_ADDRESS_MASK_BLOCK_ENTRY
);
240 // Recurse to the next level
242 Status
= UpdateRegionMappingRecursive (RegionStart
, BlockEnd
,
243 AttributeSetMask
, AttributeClearMask
, TranslationTable
,
245 if (EFI_ERROR (Status
)) {
246 if ((*Entry
& TT_TYPE_MASK
) != TT_TYPE_TABLE_ENTRY
) {
248 // We are creating a new table entry, so on failure, we can free all
249 // allocations we made recursively, given that the whole subhierarchy
250 // has not been wired into the live page tables yet. (This is not
251 // possible for existing table entries, since we cannot revert the
252 // modifications we made to the subhierarchy it represents.)
254 FreePageTablesRecursive (TranslationTable
);
259 if ((*Entry
& TT_TYPE_MASK
) != TT_TYPE_TABLE_ENTRY
) {
260 EntryValue
= (UINTN
)TranslationTable
| TT_TYPE_TABLE_ENTRY
;
261 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
,
262 (*Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
);
265 EntryValue
= (*Entry
& AttributeClearMask
) | AttributeSetMask
;
266 EntryValue
|= RegionStart
;
267 EntryValue
|= (Level
== 3) ? TT_TYPE_BLOCK_ENTRY_LEVEL3
268 : TT_TYPE_BLOCK_ENTRY
;
270 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
, FALSE
);
278 LookupAddresstoRootTable (
279 IN UINT64 MaxAddress
,
281 OUT UINTN
*TableEntryCount
286 // Check the parameters are not NULL
287 ASSERT ((T0SZ
!= NULL
) && (TableEntryCount
!= NULL
));
289 // Look for the highest bit set in MaxAddress
290 for (TopBit
= 63; TopBit
!= 0; TopBit
--) {
291 if ((1ULL << TopBit
) & MaxAddress
) {
292 // MaxAddress top bit is found
297 ASSERT (TopBit
!= 0);
299 // Calculate T0SZ from the top bit of the MaxAddress
302 // Get the Table info from T0SZ
303 GetRootTranslationTableInfo (*T0SZ
, NULL
, TableEntryCount
);
308 UpdateRegionMapping (
309 IN UINT64 RegionStart
,
310 IN UINT64 RegionLength
,
311 IN UINT64 AttributeSetMask
,
312 IN UINT64 AttributeClearMask
315 UINTN RootTableLevel
;
318 if (((RegionStart
| RegionLength
) & EFI_PAGE_MASK
)) {
319 return EFI_INVALID_PARAMETER
;
322 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
323 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, NULL
);
325 return UpdateRegionMappingRecursive (RegionStart
, RegionStart
+ RegionLength
,
326 AttributeSetMask
, AttributeClearMask
, ArmGetTTBR0BaseAddress (),
332 FillTranslationTable (
333 IN UINT64
*RootTable
,
334 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
337 return UpdateRegionMapping (
338 MemoryRegion
->VirtualBase
,
339 MemoryRegion
->Length
,
340 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
347 GcdAttributeToPageAttribute (
348 IN UINT64 GcdAttributes
351 UINT64 PageAttributes
;
353 switch (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) {
355 PageAttributes
= TT_ATTR_INDX_DEVICE_MEMORY
;
358 PageAttributes
= TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
361 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
364 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
367 PageAttributes
= TT_ATTR_INDX_MASK
;
371 if ((GcdAttributes
& EFI_MEMORY_XP
) != 0 ||
372 (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) == EFI_MEMORY_UC
) {
373 if (ArmReadCurrentEL () == AARCH64_EL2
) {
374 PageAttributes
|= TT_XN_MASK
;
376 PageAttributes
|= TT_UXN_MASK
| TT_PXN_MASK
;
380 if ((GcdAttributes
& EFI_MEMORY_RO
) != 0) {
381 PageAttributes
|= TT_AP_RO_RO
;
384 return PageAttributes
| TT_AF
;
388 ArmSetMemoryAttributes (
389 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
394 UINT64 PageAttributes
;
395 UINT64 PageAttributeMask
;
397 PageAttributes
= GcdAttributeToPageAttribute (Attributes
);
398 PageAttributeMask
= 0;
400 if ((Attributes
& EFI_MEMORY_CACHETYPE_MASK
) == 0) {
402 // No memory type was set in Attributes, so we are going to update the
405 PageAttributes
&= TT_AP_MASK
| TT_UXN_MASK
| TT_PXN_MASK
;
406 PageAttributeMask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
|
407 TT_PXN_MASK
| TT_XN_MASK
);
410 return UpdateRegionMapping (BaseAddress
, Length
, PageAttributes
,
416 SetMemoryRegionAttribute (
417 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
419 IN UINT64 Attributes
,
420 IN UINT64 BlockEntryMask
423 return UpdateRegionMapping (BaseAddress
, Length
, Attributes
, BlockEntryMask
);
427 ArmSetMemoryRegionNoExec (
428 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
434 if (ArmReadCurrentEL () == AARCH64_EL1
) {
435 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
440 return SetMemoryRegionAttribute (
444 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
448 ArmClearMemoryRegionNoExec (
449 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
455 // XN maps to UXN in the EL1&0 translation regime
456 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
458 return SetMemoryRegionAttribute (
466 ArmSetMemoryRegionReadOnly (
467 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
471 return SetMemoryRegionAttribute (
475 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
479 ArmClearMemoryRegionReadOnly (
480 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
484 return SetMemoryRegionAttribute (
488 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
494 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
495 OUT VOID
**TranslationTableBase OPTIONAL
,
496 OUT UINTN
*TranslationTableSize OPTIONAL
499 VOID
* TranslationTable
;
502 UINTN RootTableEntryCount
;
506 if(MemoryTable
== NULL
) {
507 ASSERT (MemoryTable
!= NULL
);
508 return EFI_INVALID_PARAMETER
;
512 // Limit the virtual address space to what we can actually use: UEFI
513 // mandates a 1:1 mapping, so no point in making the virtual address
514 // space larger than the physical address space. We also have to take
515 // into account the architectural limitations that result from UEFI's
516 // use of 4 KB pages.
518 MaxAddress
= MIN (LShiftU64 (1ULL, ArmGetPhysicalAddressBits ()) - 1,
521 // Lookup the Table Level to get the information
522 LookupAddresstoRootTable (MaxAddress
, &T0SZ
, &RootTableEntryCount
);
525 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
527 // Ideally we will be running at EL2, but should support EL1 as well.
528 // UEFI should not run at EL3.
529 if (ArmReadCurrentEL () == AARCH64_EL2
) {
530 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
531 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
533 // Set the Physical Address Size using MaxAddress
534 if (MaxAddress
< SIZE_4GB
) {
536 } else if (MaxAddress
< SIZE_64GB
) {
538 } else if (MaxAddress
< SIZE_1TB
) {
540 } else if (MaxAddress
< SIZE_4TB
) {
542 } else if (MaxAddress
< SIZE_16TB
) {
544 } else if (MaxAddress
< SIZE_256TB
) {
547 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
548 ASSERT (0); // Bigger than 48-bit memory space are not supported
549 return EFI_UNSUPPORTED
;
551 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
552 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
553 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
555 // Set the Physical Address Size using MaxAddress
556 if (MaxAddress
< SIZE_4GB
) {
558 } else if (MaxAddress
< SIZE_64GB
) {
560 } else if (MaxAddress
< SIZE_1TB
) {
562 } else if (MaxAddress
< SIZE_4TB
) {
564 } else if (MaxAddress
< SIZE_16TB
) {
566 } else if (MaxAddress
< SIZE_256TB
) {
567 TCR
|= TCR_IPS_256TB
;
569 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
570 ASSERT (0); // Bigger than 48-bit memory space are not supported
571 return EFI_UNSUPPORTED
;
574 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
575 return EFI_UNSUPPORTED
;
579 // Translation table walks are always cache coherent on ARMv8-A, so cache
580 // maintenance on page tables is never needed. Since there is a risk of
581 // loss of coherency when using mismatched attributes, and given that memory
582 // is mapped cacheable except for extraordinary cases (such as non-coherent
583 // DMA), have the page table walker perform cached accesses as well, and
584 // assert below that that matches the attributes we use for CPU accesses to
587 TCR
|= TCR_SH_INNER_SHAREABLE
|
588 TCR_RGN_OUTER_WRITE_BACK_ALLOC
|
589 TCR_RGN_INNER_WRITE_BACK_ALLOC
;
594 // Allocate pages for translation table
595 TranslationTable
= AllocatePages (1);
596 if (TranslationTable
== NULL
) {
597 return EFI_OUT_OF_RESOURCES
;
599 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent
600 // functions without needing to pass this value across the functions. The MMU is only enabled
601 // after the translation tables are populated.
602 ArmSetTTBR0 (TranslationTable
);
604 if (TranslationTableBase
!= NULL
) {
605 *TranslationTableBase
= TranslationTable
;
608 if (TranslationTableSize
!= NULL
) {
609 *TranslationTableSize
= RootTableEntryCount
* sizeof(UINT64
);
613 // Make sure we are not inadvertently hitting in the caches
614 // when populating the page tables.
616 InvalidateDataCacheRange (TranslationTable
,
617 RootTableEntryCount
* sizeof(UINT64
));
618 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof(UINT64
));
620 while (MemoryTable
->Length
!= 0) {
621 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
622 if (EFI_ERROR (Status
)) {
623 goto FREE_TRANSLATION_TABLE
;
628 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) | // mapped to EFI_MEMORY_UC
629 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) | // mapped to EFI_MEMORY_WC
630 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) | // mapped to EFI_MEMORY_WT
631 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)); // mapped to EFI_MEMORY_WB
633 ArmDisableAlignmentCheck ();
634 ArmEnableStackAlignmentCheck ();
635 ArmEnableInstructionCache ();
636 ArmEnableDataCache ();
641 FREE_TRANSLATION_TABLE
:
642 FreePages (TranslationTable
, 1);
648 ArmMmuBaseLibConstructor (
652 extern UINT32 ArmReplaceLiveTranslationEntrySize
;
655 // The ArmReplaceLiveTranslationEntry () helper function may be invoked
656 // with the MMU off so we have to ensure that it gets cleaned to the PoC
658 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry
,
659 ArmReplaceLiveTranslationEntrySize
);
661 return RETURN_SUCCESS
;