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