]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c
ArmPkg/CpuDxe: ARM: ignore page table updates that only change permissions
[mirror_edk2.git] / ArmPkg / Library / ArmMmuLib / AArch64 / ArmMmuLibCore.c
CommitLineData
d7f03464
AB
1/** @file\r
2* File managing the MMU for ARMv8 architecture\r
3*\r
4* Copyright (c) 2011-2014, ARM Limited. All rights reserved.\r
5* Copyright (c) 2016, Linaro Limited. All rights reserved.\r
b7a09b71 6* Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
d7f03464
AB
7*\r
8* This program and the accompanying materials\r
9* are licensed and made available under the terms and conditions of the BSD License\r
10* which accompanies this distribution. The full text of the license may be found at\r
11* http://opensource.org/licenses/bsd-license.php\r
12*\r
13* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15*\r
16**/\r
17\r
18#include <Uefi.h>\r
19#include <Chipset/AArch64.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/CacheMaintenanceLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/ArmLib.h>\r
24#include <Library/ArmMmuLib.h>\r
25#include <Library/BaseLib.h>\r
26#include <Library/DebugLib.h>\r
27\r
28// We use this index definition to define an invalid block entry\r
29#define TT_ATTR_INDX_INVALID ((UINT32)~0)\r
30\r
31STATIC\r
32UINT64\r
33ArmMemoryAttributeToPageAttribute (\r
34 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes\r
35 )\r
36{\r
37 switch (Attributes) {\r
38 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK:\r
39 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK:\r
40 return TT_ATTR_INDX_MEMORY_WRITE_BACK | TT_SH_INNER_SHAREABLE;\r
41\r
42 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:\r
43 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH:\r
44 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH | TT_SH_INNER_SHAREABLE;\r
45\r
46 // Uncached and device mappings are treated as outer shareable by default,\r
47 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED:\r
48 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED:\r
49 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE;\r
50\r
51 default:\r
52 ASSERT(0);\r
53 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE:\r
54 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE:\r
55 if (ArmReadCurrentEL () == AARCH64_EL2)\r
56 return TT_ATTR_INDX_DEVICE_MEMORY | TT_XN_MASK;\r
57 else\r
58 return TT_ATTR_INDX_DEVICE_MEMORY | TT_UXN_MASK | TT_PXN_MASK;\r
59 }\r
60}\r
61\r
62UINT64\r
63PageAttributeToGcdAttribute (\r
64 IN UINT64 PageAttributes\r
65 )\r
66{\r
67 UINT64 GcdAttributes;\r
68\r
69 switch (PageAttributes & TT_ATTR_INDX_MASK) {\r
70 case TT_ATTR_INDX_DEVICE_MEMORY:\r
71 GcdAttributes = EFI_MEMORY_UC;\r
72 break;\r
73 case TT_ATTR_INDX_MEMORY_NON_CACHEABLE:\r
74 GcdAttributes = EFI_MEMORY_WC;\r
75 break;\r
76 case TT_ATTR_INDX_MEMORY_WRITE_THROUGH:\r
77 GcdAttributes = EFI_MEMORY_WT;\r
78 break;\r
79 case TT_ATTR_INDX_MEMORY_WRITE_BACK:\r
80 GcdAttributes = EFI_MEMORY_WB;\r
81 break;\r
82 default:\r
83 DEBUG ((EFI_D_ERROR, "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n", PageAttributes));\r
84 ASSERT (0);\r
85 // The Global Coherency Domain (GCD) value is defined as a bit set.\r
86 // Returning 0 means no attribute has been set.\r
87 GcdAttributes = 0;\r
88 }\r
89\r
90 // Determine protection attributes\r
91 if (((PageAttributes & TT_AP_MASK) == TT_AP_NO_RO) || ((PageAttributes & TT_AP_MASK) == TT_AP_RO_RO)) {\r
92 // Read only cases map to write-protect\r
b7a09b71 93 GcdAttributes |= EFI_MEMORY_RO;\r
d7f03464
AB
94 }\r
95\r
96 // Process eXecute Never attribute\r
97 if ((PageAttributes & (TT_PXN_MASK | TT_UXN_MASK)) != 0 ) {\r
98 GcdAttributes |= EFI_MEMORY_XP;\r
99 }\r
100\r
101 return GcdAttributes;\r
102}\r
103\r
104ARM_MEMORY_REGION_ATTRIBUTES\r
105GcdAttributeToArmAttribute (\r
106 IN UINT64 GcdAttributes\r
107 )\r
108{\r
109 switch (GcdAttributes & 0xFF) {\r
110 case EFI_MEMORY_UC:\r
111 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;\r
112 case EFI_MEMORY_WC:\r
113 return ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED;\r
114 case EFI_MEMORY_WT:\r
115 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH;\r
116 case EFI_MEMORY_WB:\r
117 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;\r
118 default:\r
119 DEBUG ((EFI_D_ERROR, "GcdAttributeToArmAttribute: 0x%lX attributes is not supported.\n", GcdAttributes));\r
120 ASSERT (0);\r
121 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;\r
122 }\r
123}\r
124\r
e93cb72e
AB
125#define MIN_T0SZ 16\r
126#define BITS_PER_LEVEL 9\r
d7f03464
AB
127\r
128VOID\r
129GetRootTranslationTableInfo (\r
130 IN UINTN T0SZ,\r
131 OUT UINTN *TableLevel,\r
132 OUT UINTN *TableEntryCount\r
133 )\r
134{\r
d7f03464
AB
135 // Get the level of the root table\r
136 if (TableLevel) {\r
e93cb72e 137 *TableLevel = (T0SZ - MIN_T0SZ) / BITS_PER_LEVEL;\r
d7f03464
AB
138 }\r
139\r
d7f03464 140 if (TableEntryCount) {\r
e93cb72e 141 *TableEntryCount = 1UL << (BITS_PER_LEVEL - (T0SZ - MIN_T0SZ) % BITS_PER_LEVEL);\r
d7f03464
AB
142 }\r
143}\r
144\r
145STATIC\r
146VOID\r
147ReplaceLiveEntry (\r
148 IN UINT64 *Entry,\r
149 IN UINT64 Value\r
150 )\r
151{\r
152 if (!ArmMmuEnabled ()) {\r
153 *Entry = Value;\r
154 } else {\r
155 ArmReplaceLiveTranslationEntry (Entry, Value);\r
156 }\r
157}\r
158\r
159STATIC\r
160VOID\r
161LookupAddresstoRootTable (\r
162 IN UINT64 MaxAddress,\r
163 OUT UINTN *T0SZ,\r
164 OUT UINTN *TableEntryCount\r
165 )\r
166{\r
167 UINTN TopBit;\r
168\r
169 // Check the parameters are not NULL\r
170 ASSERT ((T0SZ != NULL) && (TableEntryCount != NULL));\r
171\r
172 // Look for the highest bit set in MaxAddress\r
173 for (TopBit = 63; TopBit != 0; TopBit--) {\r
174 if ((1ULL << TopBit) & MaxAddress) {\r
175 // MaxAddress top bit is found\r
176 TopBit = TopBit + 1;\r
177 break;\r
178 }\r
179 }\r
180 ASSERT (TopBit != 0);\r
181\r
182 // Calculate T0SZ from the top bit of the MaxAddress\r
183 *T0SZ = 64 - TopBit;\r
184\r
185 // Get the Table info from T0SZ\r
186 GetRootTranslationTableInfo (*T0SZ, NULL, TableEntryCount);\r
187}\r
188\r
189STATIC\r
190UINT64*\r
191GetBlockEntryListFromAddress (\r
192 IN UINT64 *RootTable,\r
193 IN UINT64 RegionStart,\r
194 OUT UINTN *TableLevel,\r
195 IN OUT UINT64 *BlockEntrySize,\r
196 OUT UINT64 **LastBlockEntry\r
197 )\r
198{\r
199 UINTN RootTableLevel;\r
200 UINTN RootTableEntryCount;\r
201 UINT64 *TranslationTable;\r
202 UINT64 *BlockEntry;\r
203 UINT64 *SubTableBlockEntry;\r
204 UINT64 BlockEntryAddress;\r
205 UINTN BaseAddressAlignment;\r
206 UINTN PageLevel;\r
207 UINTN Index;\r
208 UINTN IndexLevel;\r
209 UINTN T0SZ;\r
210 UINT64 Attributes;\r
211 UINT64 TableAttributes;\r
212\r
213 // Initialize variable\r
214 BlockEntry = NULL;\r
215\r
216 // Ensure the parameters are valid\r
217 if (!(TableLevel && BlockEntrySize && LastBlockEntry)) {\r
218 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
219 return NULL;\r
220 }\r
221\r
222 // Ensure the Region is aligned on 4KB boundary\r
223 if ((RegionStart & (SIZE_4KB - 1)) != 0) {\r
224 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
225 return NULL;\r
226 }\r
227\r
228 // Ensure the required size is aligned on 4KB boundary and not 0\r
229 if ((*BlockEntrySize & (SIZE_4KB - 1)) != 0 || *BlockEntrySize == 0) {\r
230 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
231 return NULL;\r
232 }\r
233\r
234 T0SZ = ArmGetTCR () & TCR_T0SZ_MASK;\r
235 // Get the Table info from T0SZ\r
236 GetRootTranslationTableInfo (T0SZ, &RootTableLevel, &RootTableEntryCount);\r
237\r
238 // If the start address is 0x0 then we use the size of the region to identify the alignment\r
239 if (RegionStart == 0) {\r
240 // Identify the highest possible alignment for the Region Size\r
241 BaseAddressAlignment = LowBitSet64 (*BlockEntrySize);\r
242 } else {\r
243 // Identify the highest possible alignment for the Base Address\r
244 BaseAddressAlignment = LowBitSet64 (RegionStart);\r
245 }\r
246\r
247 // Identify the Page Level the RegionStart must belong to. Note that PageLevel\r
248 // should be at least 1 since block translations are not supported at level 0\r
249 PageLevel = MAX (3 - ((BaseAddressAlignment - 12) / 9), 1);\r
250\r
251 // If the required size is smaller than the current block size then we need to go to the page below.\r
252 // The PageLevel was calculated on the Base Address alignment but did not take in account the alignment\r
253 // of the allocation size\r
254 while (*BlockEntrySize < TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel)) {\r
255 // It does not fit so we need to go a page level above\r
256 PageLevel++;\r
257 }\r
258\r
259 //\r
260 // Get the Table Descriptor for the corresponding PageLevel. We need to decompose RegionStart to get appropriate entries\r
261 //\r
262\r
263 TranslationTable = RootTable;\r
264 for (IndexLevel = RootTableLevel; IndexLevel <= PageLevel; IndexLevel++) {\r
265 BlockEntry = (UINT64*)TT_GET_ENTRY_FOR_ADDRESS (TranslationTable, IndexLevel, RegionStart);\r
266\r
267 if ((IndexLevel != 3) && ((*BlockEntry & TT_TYPE_MASK) == TT_TYPE_TABLE_ENTRY)) {\r
268 // Go to the next table\r
269 TranslationTable = (UINT64*)(*BlockEntry & TT_ADDRESS_MASK_DESCRIPTION_TABLE);\r
270\r
271 // If we are at the last level then update the last level to next level\r
272 if (IndexLevel == PageLevel) {\r
273 // Enter the next level\r
274 PageLevel++;\r
275 }\r
276 } else if ((*BlockEntry & TT_TYPE_MASK) == TT_TYPE_BLOCK_ENTRY) {\r
277 // If we are not at the last level then we need to split this BlockEntry\r
278 if (IndexLevel != PageLevel) {\r
279 // Retrieve the attributes from the block entry\r
280 Attributes = *BlockEntry & TT_ATTRIBUTES_MASK;\r
281\r
282 // Convert the block entry attributes into Table descriptor attributes\r
283 TableAttributes = TT_TABLE_AP_NO_PERMISSION;\r
284 if (Attributes & TT_NS) {\r
285 TableAttributes = TT_TABLE_NS;\r
286 }\r
287\r
288 // Get the address corresponding at this entry\r
289 BlockEntryAddress = RegionStart;\r
290 BlockEntryAddress = BlockEntryAddress >> TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel);\r
291 // Shift back to right to set zero before the effective address\r
292 BlockEntryAddress = BlockEntryAddress << TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel);\r
293\r
294 // Set the correct entry type for the next page level\r
295 if ((IndexLevel + 1) == 3) {\r
296 Attributes |= TT_TYPE_BLOCK_ENTRY_LEVEL3;\r
297 } else {\r
298 Attributes |= TT_TYPE_BLOCK_ENTRY;\r
299 }\r
300\r
301 // Create a new translation table\r
674e127e 302 TranslationTable = AllocatePages (1);\r
d7f03464
AB
303 if (TranslationTable == NULL) {\r
304 return NULL;\r
305 }\r
306\r
307 // Populate the newly created lower level table\r
308 SubTableBlockEntry = TranslationTable;\r
309 for (Index = 0; Index < TT_ENTRY_COUNT; Index++) {\r
310 *SubTableBlockEntry = Attributes | (BlockEntryAddress + (Index << TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel + 1)));\r
311 SubTableBlockEntry++;\r
312 }\r
313\r
314 // Fill the BlockEntry with the new TranslationTable\r
315 ReplaceLiveEntry (BlockEntry,\r
316 ((UINTN)TranslationTable & TT_ADDRESS_MASK_DESCRIPTION_TABLE) | TableAttributes | TT_TYPE_TABLE_ENTRY);\r
317 }\r
318 } else {\r
319 if (IndexLevel != PageLevel) {\r
320 //\r
321 // Case when we have an Invalid Entry and we are at a page level above of the one targetted.\r
322 //\r
323\r
324 // Create a new translation table\r
674e127e 325 TranslationTable = AllocatePages (1);\r
d7f03464
AB
326 if (TranslationTable == NULL) {\r
327 return NULL;\r
328 }\r
329\r
330 ZeroMem (TranslationTable, TT_ENTRY_COUNT * sizeof(UINT64));\r
331\r
332 // Fill the new BlockEntry with the TranslationTable\r
333 *BlockEntry = ((UINTN)TranslationTable & TT_ADDRESS_MASK_DESCRIPTION_TABLE) | TT_TYPE_TABLE_ENTRY;\r
334 }\r
335 }\r
336 }\r
337\r
338 // Expose the found PageLevel to the caller\r
339 *TableLevel = PageLevel;\r
340\r
341 // Now, we have the Table Level we can get the Block Size associated to this table\r
342 *BlockEntrySize = TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel);\r
343\r
344 // The last block of the root table depends on the number of entry in this table,\r
345 // otherwise it is always the (TT_ENTRY_COUNT - 1)th entry in the table.\r
346 *LastBlockEntry = TT_LAST_BLOCK_ADDRESS(TranslationTable,\r
347 (PageLevel == RootTableLevel) ? RootTableEntryCount : TT_ENTRY_COUNT);\r
348\r
349 return BlockEntry;\r
350}\r
351\r
352STATIC\r
353RETURN_STATUS\r
354UpdateRegionMapping (\r
355 IN UINT64 *RootTable,\r
356 IN UINT64 RegionStart,\r
357 IN UINT64 RegionLength,\r
358 IN UINT64 Attributes,\r
359 IN UINT64 BlockEntryMask\r
360 )\r
361{\r
362 UINT32 Type;\r
363 UINT64 *BlockEntry;\r
364 UINT64 *LastBlockEntry;\r
365 UINT64 BlockEntrySize;\r
366 UINTN TableLevel;\r
367\r
368 // Ensure the Length is aligned on 4KB boundary\r
369 if ((RegionLength == 0) || ((RegionLength & (SIZE_4KB - 1)) != 0)) {\r
370 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);\r
371 return RETURN_INVALID_PARAMETER;\r
372 }\r
373\r
374 do {\r
375 // Get the first Block Entry that matches the Virtual Address and also the information on the Table Descriptor\r
376 // such as the the size of the Block Entry and the address of the last BlockEntry of the Table Descriptor\r
377 BlockEntrySize = RegionLength;\r
378 BlockEntry = GetBlockEntryListFromAddress (RootTable, RegionStart, &TableLevel, &BlockEntrySize, &LastBlockEntry);\r
379 if (BlockEntry == NULL) {\r
380 // GetBlockEntryListFromAddress() return NULL when it fails to allocate new pages from the Translation Tables\r
381 return RETURN_OUT_OF_RESOURCES;\r
382 }\r
383\r
384 if (TableLevel != 3) {\r
385 Type = TT_TYPE_BLOCK_ENTRY;\r
386 } else {\r
387 Type = TT_TYPE_BLOCK_ENTRY_LEVEL3;\r
388 }\r
389\r
390 do {\r
391 // Fill the Block Entry with attribute and output block address\r
392 *BlockEntry &= BlockEntryMask;\r
393 *BlockEntry |= (RegionStart & TT_ADDRESS_MASK_BLOCK_ENTRY) | Attributes | Type;\r
394\r
395 // Go to the next BlockEntry\r
396 RegionStart += BlockEntrySize;\r
397 RegionLength -= BlockEntrySize;\r
398 BlockEntry++;\r
399\r
400 // Break the inner loop when next block is a table\r
401 // Rerun GetBlockEntryListFromAddress to avoid page table memory leak\r
402 if (TableLevel != 3 &&\r
403 (*BlockEntry & TT_TYPE_MASK) == TT_TYPE_TABLE_ENTRY) {\r
404 break;\r
405 }\r
406 } while ((RegionLength >= BlockEntrySize) && (BlockEntry <= LastBlockEntry));\r
407 } while (RegionLength != 0);\r
408\r
409 return RETURN_SUCCESS;\r
410}\r
411\r
412STATIC\r
413RETURN_STATUS\r
414FillTranslationTable (\r
415 IN UINT64 *RootTable,\r
416 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryRegion\r
417 )\r
418{\r
419 return UpdateRegionMapping (\r
420 RootTable,\r
421 MemoryRegion->VirtualBase,\r
422 MemoryRegion->Length,\r
423 ArmMemoryAttributeToPageAttribute (MemoryRegion->Attributes) | TT_AF,\r
424 0\r
425 );\r
426}\r
427\r
428RETURN_STATUS\r
429SetMemoryAttributes (\r
430 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
431 IN UINT64 Length,\r
432 IN UINT64 Attributes,\r
433 IN EFI_PHYSICAL_ADDRESS VirtualMask\r
434 )\r
435{\r
436 RETURN_STATUS Status;\r
437 ARM_MEMORY_REGION_DESCRIPTOR MemoryRegion;\r
438 UINT64 *TranslationTable;\r
439\r
440 MemoryRegion.PhysicalBase = BaseAddress;\r
441 MemoryRegion.VirtualBase = BaseAddress;\r
442 MemoryRegion.Length = Length;\r
443 MemoryRegion.Attributes = GcdAttributeToArmAttribute (Attributes);\r
444\r
445 TranslationTable = ArmGetTTBR0BaseAddress ();\r
446\r
447 Status = FillTranslationTable (TranslationTable, &MemoryRegion);\r
448 if (RETURN_ERROR (Status)) {\r
449 return Status;\r
450 }\r
451\r
452 // Invalidate all TLB entries so changes are synced\r
453 ArmInvalidateTlb ();\r
454\r
455 return RETURN_SUCCESS;\r
456}\r
457\r
458STATIC\r
459RETURN_STATUS\r
460SetMemoryRegionAttribute (\r
461 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
462 IN UINT64 Length,\r
463 IN UINT64 Attributes,\r
464 IN UINT64 BlockEntryMask\r
465 )\r
466{\r
467 RETURN_STATUS Status;\r
468 UINT64 *RootTable;\r
469\r
470 RootTable = ArmGetTTBR0BaseAddress ();\r
471\r
472 Status = UpdateRegionMapping (RootTable, BaseAddress, Length, Attributes, BlockEntryMask);\r
473 if (RETURN_ERROR (Status)) {\r
474 return Status;\r
475 }\r
476\r
477 // Invalidate all TLB entries so changes are synced\r
478 ArmInvalidateTlb ();\r
479\r
480 return RETURN_SUCCESS;\r
481}\r
482\r
483RETURN_STATUS\r
484ArmSetMemoryRegionNoExec (\r
485 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
486 IN UINT64 Length\r
487 )\r
488{\r
489 UINT64 Val;\r
490\r
491 if (ArmReadCurrentEL () == AARCH64_EL1) {\r
492 Val = TT_PXN_MASK | TT_UXN_MASK;\r
493 } else {\r
494 Val = TT_XN_MASK;\r
495 }\r
496\r
497 return SetMemoryRegionAttribute (\r
498 BaseAddress,\r
499 Length,\r
500 Val,\r
501 ~TT_ADDRESS_MASK_BLOCK_ENTRY);\r
502}\r
503\r
504RETURN_STATUS\r
505ArmClearMemoryRegionNoExec (\r
506 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
507 IN UINT64 Length\r
508 )\r
509{\r
510 UINT64 Mask;\r
511\r
512 // XN maps to UXN in the EL1&0 translation regime\r
513 Mask = ~(TT_ADDRESS_MASK_BLOCK_ENTRY | TT_PXN_MASK | TT_XN_MASK);\r
514\r
515 return SetMemoryRegionAttribute (\r
516 BaseAddress,\r
517 Length,\r
518 0,\r
519 Mask);\r
520}\r
521\r
522RETURN_STATUS\r
523ArmSetMemoryRegionReadOnly (\r
524 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
525 IN UINT64 Length\r
526 )\r
527{\r
528 return SetMemoryRegionAttribute (\r
529 BaseAddress,\r
530 Length,\r
531 TT_AP_RO_RO,\r
532 ~TT_ADDRESS_MASK_BLOCK_ENTRY);\r
533}\r
534\r
535RETURN_STATUS\r
536ArmClearMemoryRegionReadOnly (\r
537 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
538 IN UINT64 Length\r
539 )\r
540{\r
541 return SetMemoryRegionAttribute (\r
542 BaseAddress,\r
543 Length,\r
544 TT_AP_RW_RW,\r
545 ~(TT_ADDRESS_MASK_BLOCK_ENTRY | TT_AP_MASK));\r
546}\r
547\r
548RETURN_STATUS\r
549EFIAPI\r
550ArmConfigureMmu (\r
551 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable,\r
552 OUT VOID **TranslationTableBase OPTIONAL,\r
553 OUT UINTN *TranslationTableSize OPTIONAL\r
554 )\r
555{\r
556 VOID* TranslationTable;\r
d7f03464 557 UINT32 TranslationTableAttribute;\r
d7f03464 558 UINT64 MaxAddress;\r
d7f03464
AB
559 UINTN T0SZ;\r
560 UINTN RootTableEntryCount;\r
561 UINT64 TCR;\r
562 RETURN_STATUS Status;\r
563\r
564 if(MemoryTable == NULL) {\r
565 ASSERT (MemoryTable != NULL);\r
566 return RETURN_INVALID_PARAMETER;\r
567 }\r
568\r
dd82465a
AB
569 // Cover the entire GCD memory space\r
570 MaxAddress = (1UL << PcdGet8 (PcdPrePiCpuMemorySize)) - 1;\r
d7f03464
AB
571\r
572 // Lookup the Table Level to get the information\r
573 LookupAddresstoRootTable (MaxAddress, &T0SZ, &RootTableEntryCount);\r
574\r
575 //\r
576 // Set TCR that allows us to retrieve T0SZ in the subsequent functions\r
577 //\r
578 // Ideally we will be running at EL2, but should support EL1 as well.\r
579 // UEFI should not run at EL3.\r
580 if (ArmReadCurrentEL () == AARCH64_EL2) {\r
581 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2\r
582 TCR = T0SZ | (1UL << 31) | (1UL << 23) | TCR_TG0_4KB;\r
583\r
584 // Set the Physical Address Size using MaxAddress\r
585 if (MaxAddress < SIZE_4GB) {\r
586 TCR |= TCR_PS_4GB;\r
587 } else if (MaxAddress < SIZE_64GB) {\r
588 TCR |= TCR_PS_64GB;\r
589 } else if (MaxAddress < SIZE_1TB) {\r
590 TCR |= TCR_PS_1TB;\r
591 } else if (MaxAddress < SIZE_4TB) {\r
592 TCR |= TCR_PS_4TB;\r
593 } else if (MaxAddress < SIZE_16TB) {\r
594 TCR |= TCR_PS_16TB;\r
595 } else if (MaxAddress < SIZE_256TB) {\r
596 TCR |= TCR_PS_256TB;\r
597 } else {\r
598 DEBUG ((EFI_D_ERROR, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress));\r
599 ASSERT (0); // Bigger than 48-bit memory space are not supported\r
600 return RETURN_UNSUPPORTED;\r
601 }\r
602 } else if (ArmReadCurrentEL () == AARCH64_EL1) {\r
603 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.\r
604 TCR = T0SZ | TCR_TG0_4KB | TCR_TG1_4KB | TCR_EPD1;\r
605\r
606 // Set the Physical Address Size using MaxAddress\r
607 if (MaxAddress < SIZE_4GB) {\r
608 TCR |= TCR_IPS_4GB;\r
609 } else if (MaxAddress < SIZE_64GB) {\r
610 TCR |= TCR_IPS_64GB;\r
611 } else if (MaxAddress < SIZE_1TB) {\r
612 TCR |= TCR_IPS_1TB;\r
613 } else if (MaxAddress < SIZE_4TB) {\r
614 TCR |= TCR_IPS_4TB;\r
615 } else if (MaxAddress < SIZE_16TB) {\r
616 TCR |= TCR_IPS_16TB;\r
617 } else if (MaxAddress < SIZE_256TB) {\r
618 TCR |= TCR_IPS_256TB;\r
619 } else {\r
620 DEBUG ((EFI_D_ERROR, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress));\r
621 ASSERT (0); // Bigger than 48-bit memory space are not supported\r
622 return RETURN_UNSUPPORTED;\r
623 }\r
624 } else {\r
625 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.\r
626 return RETURN_UNSUPPORTED;\r
627 }\r
628\r
35718840
AB
629 //\r
630 // Translation table walks are always cache coherent on ARMv8-A, so cache\r
631 // maintenance on page tables is never needed. Since there is a risk of\r
632 // loss of coherency when using mismatched attributes, and given that memory\r
633 // is mapped cacheable except for extraordinary cases (such as non-coherent\r
634 // DMA), have the page table walker perform cached accesses as well, and\r
635 // assert below that that matches the attributes we use for CPU accesses to\r
636 // the region.\r
637 //\r
638 TCR |= TCR_SH_INNER_SHAREABLE |\r
639 TCR_RGN_OUTER_WRITE_BACK_ALLOC |\r
640 TCR_RGN_INNER_WRITE_BACK_ALLOC;\r
641\r
d7f03464
AB
642 // Set TCR\r
643 ArmSetTCR (TCR);\r
644\r
aa961dea
AB
645 // Allocate pages for translation table\r
646 TranslationTable = AllocatePages (1);\r
d7f03464
AB
647 if (TranslationTable == NULL) {\r
648 return RETURN_OUT_OF_RESOURCES;\r
649 }\r
650 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent\r
651 // functions without needing to pass this value across the functions. The MMU is only enabled\r
652 // after the translation tables are populated.\r
653 ArmSetTTBR0 (TranslationTable);\r
654\r
655 if (TranslationTableBase != NULL) {\r
656 *TranslationTableBase = TranslationTable;\r
657 }\r
658\r
659 if (TranslationTableSize != NULL) {\r
aa961dea 660 *TranslationTableSize = RootTableEntryCount * sizeof(UINT64);\r
d7f03464
AB
661 }\r
662\r
aa961dea 663 ZeroMem (TranslationTable, RootTableEntryCount * sizeof(UINT64));\r
d7f03464
AB
664\r
665 // Disable MMU and caches. ArmDisableMmu() also invalidates the TLBs\r
666 ArmDisableMmu ();\r
667 ArmDisableDataCache ();\r
668 ArmDisableInstructionCache ();\r
669\r
670 // Make sure nothing sneaked into the cache\r
671 ArmCleanInvalidateDataCache ();\r
672 ArmInvalidateInstructionCache ();\r
673\r
674 TranslationTableAttribute = TT_ATTR_INDX_INVALID;\r
675 while (MemoryTable->Length != 0) {\r
35718840
AB
676\r
677 DEBUG_CODE_BEGIN ();\r
678 // Find the memory attribute for the Translation Table\r
679 if ((UINTN)TranslationTable >= MemoryTable->PhysicalBase &&\r
aa961dea 680 (UINTN)TranslationTable + EFI_PAGE_SIZE <= MemoryTable->PhysicalBase +\r
35718840
AB
681 MemoryTable->Length) {\r
682 TranslationTableAttribute = MemoryTable->Attributes;\r
683 }\r
684 DEBUG_CODE_END ();\r
d7f03464
AB
685\r
686 Status = FillTranslationTable (TranslationTable, MemoryTable);\r
687 if (RETURN_ERROR (Status)) {\r
688 goto FREE_TRANSLATION_TABLE;\r
689 }\r
690 MemoryTable++;\r
691 }\r
692\r
35718840
AB
693 ASSERT (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK ||\r
694 TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK);\r
d7f03464
AB
695\r
696 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY, MAIR_ATTR_DEVICE_MEMORY) | // mapped to EFI_MEMORY_UC\r
697 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE) | // mapped to EFI_MEMORY_WC\r
698 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH) | // mapped to EFI_MEMORY_WT\r
699 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK)); // mapped to EFI_MEMORY_WB\r
700\r
701 ArmDisableAlignmentCheck ();\r
702 ArmEnableInstructionCache ();\r
703 ArmEnableDataCache ();\r
704\r
705 ArmEnableMmu ();\r
706 return RETURN_SUCCESS;\r
707\r
708FREE_TRANSLATION_TABLE:\r
aa961dea 709 FreePages (TranslationTable, 1);\r
d7f03464
AB
710 return Status;\r
711}\r
712\r
713RETURN_STATUS\r
714EFIAPI\r
715ArmMmuBaseLibConstructor (\r
716 VOID\r
717 )\r
718{\r
719 extern UINT32 ArmReplaceLiveTranslationEntrySize;\r
720\r
721 //\r
722 // The ArmReplaceLiveTranslationEntry () helper function may be invoked\r
723 // with the MMU off so we have to ensure that it gets cleaned to the PoC\r
724 //\r
725 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry,\r
726 ArmReplaceLiveTranslationEntrySize);\r
727\r
728 return RETURN_SUCCESS;\r
729}\r