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