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
;
79 return (T0SZ
- MIN_T0SZ
) / BITS_PER_LEVEL
;
87 IN UINT64 RegionStart
,
88 IN BOOLEAN IsLiveBlockMapping
91 if (!ArmMmuEnabled () || !IsLiveBlockMapping
) {
93 ArmUpdateTranslationTableEntry (Entry
, (VOID
*)(UINTN
)RegionStart
);
95 ArmReplaceLiveTranslationEntry (Entry
, Value
, RegionStart
);
101 FreePageTablesRecursive (
102 IN UINT64
*TranslationTable
,
111 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
112 if ((TranslationTable
[Index
] & TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
113 FreePageTablesRecursive ((VOID
*)(UINTN
)(TranslationTable
[Index
] &
114 TT_ADDRESS_MASK_BLOCK_ENTRY
),
119 FreePages (TranslationTable
, 1);
130 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY_LEVEL3
;
132 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
;
144 // TT_TYPE_TABLE_ENTRY aliases TT_TYPE_BLOCK_ENTRY_LEVEL3
145 // so we need to take the level into account as well.
149 return (Entry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
;
154 UpdateRegionMappingRecursive (
155 IN UINT64 RegionStart
,
157 IN UINT64 AttributeSetMask
,
158 IN UINT64 AttributeClearMask
,
159 IN UINT64
*PageTable
,
168 VOID
*TranslationTable
;
171 ASSERT (((RegionStart
| RegionEnd
) & EFI_PAGE_MASK
) == 0);
173 BlockShift
= (Level
+ 1) * BITS_PER_LEVEL
+ MIN_T0SZ
;
174 BlockMask
= MAX_UINT64
>> BlockShift
;
176 DEBUG ((DEBUG_VERBOSE
, "%a(%d): %llx - %llx set %lx clr %lx\n", __FUNCTION__
,
177 Level
, RegionStart
, RegionEnd
, AttributeSetMask
, AttributeClearMask
));
179 for (; RegionStart
< RegionEnd
; RegionStart
= BlockEnd
) {
180 BlockEnd
= MIN (RegionEnd
, (RegionStart
| BlockMask
) + 1);
181 Entry
= &PageTable
[(RegionStart
>> (64 - BlockShift
)) & (TT_ENTRY_COUNT
- 1)];
184 // If RegionStart or BlockEnd is not aligned to the block size at this
185 // level, we will have to create a table mapping in order to map less
186 // than a block, and recurse to create the block or page entries at
187 // the next level. No block mappings are allowed at all at level 0,
188 // so in that case, we have to recurse unconditionally.
189 // If we are changing a table entry and the AttributeClearMask is non-zero,
190 // we cannot replace it with a block entry without potentially losing
191 // attribute information, so keep the table entry in that case.
193 if (Level
== 0 || ((RegionStart
| BlockEnd
) & BlockMask
) != 0 ||
194 (IsTableEntry (*Entry
, Level
) && AttributeClearMask
!= 0)) {
197 if (!IsTableEntry (*Entry
, Level
)) {
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 ZeroMem (TranslationTable
, EFI_PAGE_SIZE
);
217 if (IsBlockEntry (*Entry
, Level
)) {
219 // We are splitting an existing block entry, so we have to populate
220 // the new table with the attributes of the block entry it replaces.
222 Status
= UpdateRegionMappingRecursive (RegionStart
& ~BlockMask
,
223 (RegionStart
| BlockMask
) + 1, *Entry
& TT_ATTRIBUTES_MASK
,
224 0, TranslationTable
, Level
+ 1);
225 if (EFI_ERROR (Status
)) {
227 // The range we passed to UpdateRegionMappingRecursive () is block
228 // aligned, so it is guaranteed that no further pages were allocated
229 // by it, and so we only have to free the page we allocated here.
231 FreePages (TranslationTable
, 1);
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 (!IsTableEntry (*Entry
, Level
)) {
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
, Level
+ 1);
259 if (!IsTableEntry (*Entry
, Level
)) {
260 EntryValue
= (UINTN
)TranslationTable
| TT_TYPE_TABLE_ENTRY
;
261 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
,
262 IsBlockEntry (*Entry
, Level
));
265 EntryValue
= (*Entry
& AttributeClearMask
) | AttributeSetMask
;
266 EntryValue
|= RegionStart
;
267 EntryValue
|= (Level
== 3) ? TT_TYPE_BLOCK_ENTRY_LEVEL3
268 : TT_TYPE_BLOCK_ENTRY
;
270 if (IsTableEntry (*Entry
, Level
)) {
272 // We are replacing a table entry with a block entry. This is only
273 // possible if we are keeping none of the original attributes.
274 // We can free the table entry's page table, and all the ones below
275 // it, since we are dropping the only possible reference to it.
277 ASSERT (AttributeClearMask
== 0);
278 TranslationTable
= (VOID
*)(UINTN
)(*Entry
& TT_ADDRESS_MASK_BLOCK_ENTRY
);
279 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
, TRUE
);
280 FreePageTablesRecursive (TranslationTable
, Level
+ 1);
282 ReplaceTableEntry (Entry
, EntryValue
, RegionStart
, FALSE
);
291 UpdateRegionMapping (
292 IN UINT64 RegionStart
,
293 IN UINT64 RegionLength
,
294 IN UINT64 AttributeSetMask
,
295 IN UINT64 AttributeClearMask
300 if (((RegionStart
| RegionLength
) & EFI_PAGE_MASK
)) {
301 return EFI_INVALID_PARAMETER
;
304 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
306 return UpdateRegionMappingRecursive (RegionStart
, RegionStart
+ RegionLength
,
307 AttributeSetMask
, AttributeClearMask
, ArmGetTTBR0BaseAddress (),
308 GetRootTableLevel (T0SZ
));
313 FillTranslationTable (
314 IN UINT64
*RootTable
,
315 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
318 return UpdateRegionMapping (
319 MemoryRegion
->VirtualBase
,
320 MemoryRegion
->Length
,
321 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
328 GcdAttributeToPageAttribute (
329 IN UINT64 GcdAttributes
332 UINT64 PageAttributes
;
334 switch (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) {
336 PageAttributes
= TT_ATTR_INDX_DEVICE_MEMORY
;
339 PageAttributes
= TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
342 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
345 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
348 PageAttributes
= TT_ATTR_INDX_MASK
;
352 if ((GcdAttributes
& EFI_MEMORY_XP
) != 0 ||
353 (GcdAttributes
& EFI_MEMORY_CACHETYPE_MASK
) == EFI_MEMORY_UC
) {
354 if (ArmReadCurrentEL () == AARCH64_EL2
) {
355 PageAttributes
|= TT_XN_MASK
;
357 PageAttributes
|= TT_UXN_MASK
| TT_PXN_MASK
;
361 if ((GcdAttributes
& EFI_MEMORY_RO
) != 0) {
362 PageAttributes
|= TT_AP_RO_RO
;
365 return PageAttributes
| TT_AF
;
369 ArmSetMemoryAttributes (
370 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
375 UINT64 PageAttributes
;
376 UINT64 PageAttributeMask
;
378 PageAttributes
= GcdAttributeToPageAttribute (Attributes
);
379 PageAttributeMask
= 0;
381 if ((Attributes
& EFI_MEMORY_CACHETYPE_MASK
) == 0) {
383 // No memory type was set in Attributes, so we are going to update the
386 PageAttributes
&= TT_AP_MASK
| TT_UXN_MASK
| TT_PXN_MASK
;
387 PageAttributeMask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
|
388 TT_PXN_MASK
| TT_XN_MASK
);
391 return UpdateRegionMapping (BaseAddress
, Length
, PageAttributes
,
397 SetMemoryRegionAttribute (
398 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
400 IN UINT64 Attributes
,
401 IN UINT64 BlockEntryMask
404 return UpdateRegionMapping (BaseAddress
, Length
, Attributes
, BlockEntryMask
);
408 ArmSetMemoryRegionNoExec (
409 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
415 if (ArmReadCurrentEL () == AARCH64_EL1
) {
416 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
421 return SetMemoryRegionAttribute (
425 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
429 ArmClearMemoryRegionNoExec (
430 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
436 // XN maps to UXN in the EL1&0 translation regime
437 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
439 return SetMemoryRegionAttribute (
447 ArmSetMemoryRegionReadOnly (
448 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
452 return SetMemoryRegionAttribute (
456 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
460 ArmClearMemoryRegionReadOnly (
461 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
465 return SetMemoryRegionAttribute (
469 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
475 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
476 OUT VOID
**TranslationTableBase OPTIONAL
,
477 OUT UINTN
*TranslationTableSize OPTIONAL
480 VOID
* TranslationTable
;
481 UINTN MaxAddressBits
;
484 UINTN RootTableEntryCount
;
488 if (MemoryTable
== NULL
) {
489 ASSERT (MemoryTable
!= NULL
);
490 return EFI_INVALID_PARAMETER
;
494 // Limit the virtual address space to what we can actually use: UEFI
495 // mandates a 1:1 mapping, so no point in making the virtual address
496 // space larger than the physical address space. We also have to take
497 // into account the architectural limitations that result from UEFI's
498 // use of 4 KB pages.
500 MaxAddressBits
= MIN (ArmGetPhysicalAddressBits (), MAX_VA_BITS
);
501 MaxAddress
= LShiftU64 (1ULL, MaxAddressBits
) - 1;
503 T0SZ
= 64 - MaxAddressBits
;
504 RootTableEntryCount
= GetRootTableEntryCount (T0SZ
);
507 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
509 // Ideally we will be running at EL2, but should support EL1 as well.
510 // UEFI should not run at EL3.
511 if (ArmReadCurrentEL () == AARCH64_EL2
) {
512 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
513 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
515 // Set the Physical Address Size using MaxAddress
516 if (MaxAddress
< SIZE_4GB
) {
518 } else if (MaxAddress
< SIZE_64GB
) {
520 } else if (MaxAddress
< SIZE_1TB
) {
522 } else if (MaxAddress
< SIZE_4TB
) {
524 } else if (MaxAddress
< SIZE_16TB
) {
526 } else if (MaxAddress
< SIZE_256TB
) {
530 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
532 ASSERT (0); // Bigger than 48-bit memory space are not supported
533 return EFI_UNSUPPORTED
;
535 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
536 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
537 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
539 // Set the Physical Address Size using MaxAddress
540 if (MaxAddress
< SIZE_4GB
) {
542 } else if (MaxAddress
< SIZE_64GB
) {
544 } else if (MaxAddress
< SIZE_1TB
) {
546 } else if (MaxAddress
< SIZE_4TB
) {
548 } else if (MaxAddress
< SIZE_16TB
) {
550 } else if (MaxAddress
< SIZE_256TB
) {
551 TCR
|= TCR_IPS_256TB
;
554 "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n",
556 ASSERT (0); // Bigger than 48-bit memory space are not supported
557 return EFI_UNSUPPORTED
;
560 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
561 return EFI_UNSUPPORTED
;
565 // Translation table walks are always cache coherent on ARMv8-A, so cache
566 // maintenance on page tables is never needed. Since there is a risk of
567 // loss of coherency when using mismatched attributes, and given that memory
568 // is mapped cacheable except for extraordinary cases (such as non-coherent
569 // DMA), have the page table walker perform cached accesses as well, and
570 // assert below that that matches the attributes we use for CPU accesses to
573 TCR
|= TCR_SH_INNER_SHAREABLE
|
574 TCR_RGN_OUTER_WRITE_BACK_ALLOC
|
575 TCR_RGN_INNER_WRITE_BACK_ALLOC
;
580 // Allocate pages for translation table
581 TranslationTable
= AllocatePages (1);
582 if (TranslationTable
== NULL
) {
583 return EFI_OUT_OF_RESOURCES
;
586 // We set TTBR0 just after allocating the table to retrieve its location from
587 // the subsequent functions without needing to pass this value across the
588 // functions. The MMU is only enabled after the translation tables are
591 ArmSetTTBR0 (TranslationTable
);
593 if (TranslationTableBase
!= NULL
) {
594 *TranslationTableBase
= TranslationTable
;
597 if (TranslationTableSize
!= NULL
) {
598 *TranslationTableSize
= RootTableEntryCount
* sizeof (UINT64
);
602 // Make sure we are not inadvertently hitting in the caches
603 // when populating the page tables.
605 InvalidateDataCacheRange (TranslationTable
,
606 RootTableEntryCount
* sizeof (UINT64
));
607 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof (UINT64
));
609 while (MemoryTable
->Length
!= 0) {
610 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
611 if (EFI_ERROR (Status
)) {
612 goto FreeTranslationTable
;
618 // EFI_MEMORY_UC ==> MAIR_ATTR_DEVICE_MEMORY
619 // EFI_MEMORY_WC ==> MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
620 // EFI_MEMORY_WT ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
621 // EFI_MEMORY_WB ==> MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
624 MAIR_ATTR (TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) |
625 MAIR_ATTR (TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) |
626 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) |
627 MAIR_ATTR (TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)
630 ArmDisableAlignmentCheck ();
631 ArmEnableStackAlignmentCheck ();
632 ArmEnableInstructionCache ();
633 ArmEnableDataCache ();
638 FreeTranslationTable
:
639 FreePages (TranslationTable
, 1);
645 ArmMmuBaseLibConstructor (
649 extern UINT32 ArmReplaceLiveTranslationEntrySize
;
652 // The ArmReplaceLiveTranslationEntry () helper function may be invoked
653 // with the MMU off so we have to ensure that it gets cleaned to the PoC
655 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry
,
656 ArmReplaceLiveTranslationEntrySize
);
658 return RETURN_SUCCESS
;