]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c
ArmPkg/ArmMmuLib AARCH64: invalidate page tables before populating them
[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
191fa79b 4* Copyright (c) 2011-2020, ARM Limited. All rights reserved.\r
d7f03464 5* Copyright (c) 2016, Linaro Limited. All rights reserved.\r
b7a09b71 6* Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
d7f03464 7*\r
4059386c 8* SPDX-License-Identifier: BSD-2-Clause-Patent\r
d7f03464
AB
9*\r
10**/\r
11\r
12#include <Uefi.h>\r
13#include <Chipset/AArch64.h>\r
14#include <Library/BaseMemoryLib.h>\r
15#include <Library/CacheMaintenanceLib.h>\r
16#include <Library/MemoryAllocationLib.h>\r
17#include <Library/ArmLib.h>\r
18#include <Library/ArmMmuLib.h>\r
19#include <Library/BaseLib.h>\r
20#include <Library/DebugLib.h>\r
21\r
22// We use this index definition to define an invalid block entry\r
23#define TT_ATTR_INDX_INVALID ((UINT32)~0)\r
24\r
25STATIC\r
26UINT64\r
27ArmMemoryAttributeToPageAttribute (\r
28 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes\r
29 )\r
30{\r
31 switch (Attributes) {\r
829633e3
PL
32 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_NONSHAREABLE:\r
33 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK_NONSHAREABLE:\r
34 return TT_ATTR_INDX_MEMORY_WRITE_BACK;\r
35\r
d7f03464
AB
36 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK:\r
37 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK:\r
38 return TT_ATTR_INDX_MEMORY_WRITE_BACK | TT_SH_INNER_SHAREABLE;\r
39\r
40 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:\r
41 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH:\r
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
46 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED:\r
47 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE;\r
48\r
49 default:\r
50 ASSERT(0);\r
51 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE:\r
52 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE:\r
53 if (ArmReadCurrentEL () == AARCH64_EL2)\r
54 return TT_ATTR_INDX_DEVICE_MEMORY | TT_XN_MASK;\r
55 else\r
56 return TT_ATTR_INDX_DEVICE_MEMORY | TT_UXN_MASK | TT_PXN_MASK;\r
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
b7a09b71 91 GcdAttributes |= EFI_MEMORY_RO;\r
d7f03464
AB
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
e93cb72e
AB
102#define MIN_T0SZ 16\r
103#define BITS_PER_LEVEL 9\r
d7f03464
AB
104\r
105VOID\r
106GetRootTranslationTableInfo (\r
107 IN UINTN T0SZ,\r
108 OUT UINTN *TableLevel,\r
109 OUT UINTN *TableEntryCount\r
110 )\r
111{\r
d7f03464
AB
112 // Get the level of the root table\r
113 if (TableLevel) {\r
e93cb72e 114 *TableLevel = (T0SZ - MIN_T0SZ) / BITS_PER_LEVEL;\r
d7f03464
AB
115 }\r
116\r
d7f03464 117 if (TableEntryCount) {\r
e93cb72e 118 *TableEntryCount = 1UL << (BITS_PER_LEVEL - (T0SZ - MIN_T0SZ) % BITS_PER_LEVEL);\r
d7f03464
AB
119 }\r
120}\r
121\r
122STATIC\r
123VOID\r
191fa79b 124ReplaceTableEntry (\r
d7f03464 125 IN UINT64 *Entry,\r
d5788777 126 IN UINT64 Value,\r
191fa79b
AB
127 IN UINT64 RegionStart,\r
128 IN BOOLEAN IsLiveBlockMapping\r
d7f03464
AB
129 )\r
130{\r
191fa79b 131 if (!ArmMmuEnabled () || !IsLiveBlockMapping) {\r
d7f03464 132 *Entry = Value;\r
191fa79b 133 ArmUpdateTranslationTableEntry (Entry, (VOID *)(UINTN)RegionStart);\r
d7f03464 134 } else {\r
d5788777 135 ArmReplaceLiveTranslationEntry (Entry, Value, RegionStart);\r
d7f03464
AB
136 }\r
137}\r
138\r
139STATIC\r
140VOID\r
191fa79b
AB
141FreePageTablesRecursive (\r
142 IN UINT64 *TranslationTable\r
d7f03464
AB
143 )\r
144{\r
191fa79b 145 UINTN Index;\r
d7f03464 146\r
191fa79b
AB
147 for (Index = 0; Index < TT_ENTRY_COUNT; Index++) {\r
148 if ((TranslationTable[Index] & TT_TYPE_MASK) == TT_TYPE_TABLE_ENTRY) {\r
149 FreePageTablesRecursive ((VOID *)(UINTN)(TranslationTable[Index] &\r
150 TT_ADDRESS_MASK_BLOCK_ENTRY));\r
d7f03464
AB
151 }\r
152 }\r
191fa79b 153 FreePages (TranslationTable, 1);\r
d7f03464
AB
154}\r
155\r
156STATIC\r
191fa79b
AB
157EFI_STATUS\r
158UpdateRegionMappingRecursive (\r
159 IN UINT64 RegionStart,\r
160 IN UINT64 RegionEnd,\r
161 IN UINT64 AttributeSetMask,\r
162 IN UINT64 AttributeClearMask,\r
163 IN UINT64 *PageTable,\r
164 IN UINTN Level\r
d7f03464
AB
165 )\r
166{\r
191fa79b
AB
167 UINTN BlockShift;\r
168 UINT64 BlockMask;\r
169 UINT64 BlockEnd;\r
170 UINT64 *Entry;\r
171 UINT64 EntryValue;\r
172 VOID *TranslationTable;\r
173 EFI_STATUS Status;\r
d7f03464 174\r
191fa79b 175 ASSERT (((RegionStart | RegionEnd) & EFI_PAGE_MASK) == 0);\r
d7f03464 176\r
191fa79b
AB
177 BlockShift = (Level + 1) * BITS_PER_LEVEL + MIN_T0SZ;\r
178 BlockMask = MAX_UINT64 >> BlockShift;\r
d7f03464 179\r
191fa79b
AB
180 DEBUG ((DEBUG_VERBOSE, "%a(%d): %llx - %llx set %lx clr %lx\n", __FUNCTION__,\r
181 Level, RegionStart, RegionEnd, AttributeSetMask, AttributeClearMask));\r
d7f03464 182\r
191fa79b
AB
183 for (; RegionStart < RegionEnd; RegionStart = BlockEnd) {\r
184 BlockEnd = MIN (RegionEnd, (RegionStart | BlockMask) + 1);\r
185 Entry = &PageTable[(RegionStart >> (64 - BlockShift)) & (TT_ENTRY_COUNT - 1)];\r
d7f03464 186\r
191fa79b
AB
187 //\r
188 // If RegionStart or BlockEnd is not aligned to the block size at this\r
189 // level, we will have to create a table mapping in order to map less\r
190 // than a block, and recurse to create the block or page entries at\r
191 // the next level. No block mappings are allowed at all at level 0,\r
192 // so in that case, we have to recurse unconditionally.\r
193 //\r
194 if (Level == 0 || ((RegionStart | BlockEnd) & BlockMask) != 0) {\r
195 ASSERT (Level < 3);\r
d7f03464 196\r
191fa79b
AB
197 if ((*Entry & TT_TYPE_MASK) != TT_TYPE_TABLE_ENTRY) {\r
198 //\r
199 // No table entry exists yet, so we need to allocate a page table\r
200 // for the next level.\r
201 //\r
674e127e 202 TranslationTable = AllocatePages (1);\r
d7f03464 203 if (TranslationTable == NULL) {\r
191fa79b 204 return EFI_OUT_OF_RESOURCES;\r
d7f03464
AB
205 }\r
206\r
748fea62
AB
207 if (!ArmMmuEnabled ()) {\r
208 //\r
209 // Make sure we are not inadvertently hitting in the caches\r
210 // when populating the page tables.\r
211 //\r
212 InvalidateDataCacheRange (TranslationTable, EFI_PAGE_SIZE);\r
213 }\r
214\r
191fa79b
AB
215 if ((*Entry & TT_TYPE_MASK) == TT_TYPE_BLOCK_ENTRY) {\r
216 //\r
217 // We are splitting an existing block entry, so we have to populate\r
218 // the new table with the attributes of the block entry it replaces.\r
219 //\r
220 Status = UpdateRegionMappingRecursive (RegionStart & ~BlockMask,\r
221 (RegionStart | BlockMask) + 1, *Entry & TT_ATTRIBUTES_MASK,\r
222 0, TranslationTable, Level + 1);\r
223 if (EFI_ERROR (Status)) {\r
224 //\r
225 // The range we passed to UpdateRegionMappingRecursive () is block\r
226 // aligned, so it is guaranteed that no further pages were allocated\r
227 // by it, and so we only have to free the page we allocated here.\r
228 //\r
229 FreePages (TranslationTable, 1);\r
230 return Status;\r
231 }\r
232 } else {\r
233 ZeroMem (TranslationTable, EFI_PAGE_SIZE);\r
d7f03464 234 }\r
191fa79b
AB
235 } else {\r
236 TranslationTable = (VOID *)(UINTN)(*Entry & TT_ADDRESS_MASK_BLOCK_ENTRY);\r
d7f03464 237 }\r
d7f03464 238\r
191fa79b
AB
239 //\r
240 // Recurse to the next level\r
241 //\r
242 Status = UpdateRegionMappingRecursive (RegionStart, BlockEnd,\r
243 AttributeSetMask, AttributeClearMask, TranslationTable,\r
244 Level + 1);\r
245 if (EFI_ERROR (Status)) {\r
246 if ((*Entry & TT_TYPE_MASK) != TT_TYPE_TABLE_ENTRY) {\r
247 //\r
248 // We are creating a new table entry, so on failure, we can free all\r
249 // allocations we made recursively, given that the whole subhierarchy\r
250 // has not been wired into the live page tables yet. (This is not\r
251 // possible for existing table entries, since we cannot revert the\r
252 // modifications we made to the subhierarchy it represents.)\r
253 //\r
254 FreePageTablesRecursive (TranslationTable);\r
d7f03464 255 }\r
191fa79b
AB
256 return Status;\r
257 }\r
d7f03464 258\r
191fa79b
AB
259 if ((*Entry & TT_TYPE_MASK) != TT_TYPE_TABLE_ENTRY) {\r
260 EntryValue = (UINTN)TranslationTable | TT_TYPE_TABLE_ENTRY;\r
261 ReplaceTableEntry (Entry, EntryValue, RegionStart,\r
262 (*Entry & TT_TYPE_MASK) == TT_TYPE_BLOCK_ENTRY);\r
d7f03464 263 }\r
191fa79b
AB
264 } else {\r
265 EntryValue = (*Entry & AttributeClearMask) | AttributeSetMask;\r
266 EntryValue |= RegionStart;\r
267 EntryValue |= (Level == 3) ? TT_TYPE_BLOCK_ENTRY_LEVEL3\r
268 : TT_TYPE_BLOCK_ENTRY;\r
269\r
270 ReplaceTableEntry (Entry, EntryValue, RegionStart, FALSE);\r
d7f03464
AB
271 }\r
272 }\r
191fa79b
AB
273 return EFI_SUCCESS;\r
274}\r
d7f03464 275\r
191fa79b
AB
276STATIC\r
277VOID\r
278LookupAddresstoRootTable (\r
279 IN UINT64 MaxAddress,\r
280 OUT UINTN *T0SZ,\r
281 OUT UINTN *TableEntryCount\r
282 )\r
283{\r
284 UINTN TopBit;\r
d7f03464 285\r
191fa79b
AB
286 // Check the parameters are not NULL\r
287 ASSERT ((T0SZ != NULL) && (TableEntryCount != NULL));\r
d7f03464 288\r
191fa79b
AB
289 // Look for the highest bit set in MaxAddress\r
290 for (TopBit = 63; TopBit != 0; TopBit--) {\r
291 if ((1ULL << TopBit) & MaxAddress) {\r
292 // MaxAddress top bit is found\r
293 TopBit = TopBit + 1;\r
294 break;\r
295 }\r
296 }\r
297 ASSERT (TopBit != 0);\r
d7f03464 298\r
191fa79b
AB
299 // Calculate T0SZ from the top bit of the MaxAddress\r
300 *T0SZ = 64 - TopBit;\r
301\r
302 // Get the Table info from T0SZ\r
303 GetRootTranslationTableInfo (*T0SZ, NULL, TableEntryCount);\r
d7f03464
AB
304}\r
305\r
306STATIC\r
f49ea03d 307EFI_STATUS\r
d7f03464 308UpdateRegionMapping (\r
d7f03464
AB
309 IN UINT64 RegionStart,\r
310 IN UINT64 RegionLength,\r
191fa79b
AB
311 IN UINT64 AttributeSetMask,\r
312 IN UINT64 AttributeClearMask\r
d7f03464
AB
313 )\r
314{\r
191fa79b
AB
315 UINTN RootTableLevel;\r
316 UINTN T0SZ;\r
317\r
318 if (((RegionStart | RegionLength) & EFI_PAGE_MASK)) {\r
f49ea03d 319 return EFI_INVALID_PARAMETER;\r
d7f03464
AB
320 }\r
321\r
191fa79b
AB
322 T0SZ = ArmGetTCR () & TCR_T0SZ_MASK;\r
323 GetRootTranslationTableInfo (T0SZ, &RootTableLevel, NULL);\r
d7f03464 324\r
191fa79b
AB
325 return UpdateRegionMappingRecursive (RegionStart, RegionStart + RegionLength,\r
326 AttributeSetMask, AttributeClearMask, ArmGetTTBR0BaseAddress (),\r
327 RootTableLevel);\r
d7f03464
AB
328}\r
329\r
330STATIC\r
f49ea03d 331EFI_STATUS\r
d7f03464
AB
332FillTranslationTable (\r
333 IN UINT64 *RootTable,\r
334 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryRegion\r
335 )\r
336{\r
337 return UpdateRegionMapping (\r
d7f03464
AB
338 MemoryRegion->VirtualBase,\r
339 MemoryRegion->Length,\r
340 ArmMemoryAttributeToPageAttribute (MemoryRegion->Attributes) | TT_AF,\r
341 0\r
342 );\r
343}\r
344\r
e0307a7d
AB
345STATIC\r
346UINT64\r
347GcdAttributeToPageAttribute (\r
348 IN UINT64 GcdAttributes\r
349 )\r
350{\r
351 UINT64 PageAttributes;\r
352\r
353 switch (GcdAttributes & EFI_MEMORY_CACHETYPE_MASK) {\r
354 case EFI_MEMORY_UC:\r
355 PageAttributes = TT_ATTR_INDX_DEVICE_MEMORY;\r
356 break;\r
357 case EFI_MEMORY_WC:\r
358 PageAttributes = TT_ATTR_INDX_MEMORY_NON_CACHEABLE;\r
359 break;\r
360 case EFI_MEMORY_WT:\r
361 PageAttributes = TT_ATTR_INDX_MEMORY_WRITE_THROUGH | TT_SH_INNER_SHAREABLE;\r
362 break;\r
363 case EFI_MEMORY_WB:\r
364 PageAttributes = TT_ATTR_INDX_MEMORY_WRITE_BACK | TT_SH_INNER_SHAREABLE;\r
365 break;\r
366 default:\r
367 PageAttributes = TT_ATTR_INDX_MASK;\r
368 break;\r
369 }\r
370\r
371 if ((GcdAttributes & EFI_MEMORY_XP) != 0 ||\r
372 (GcdAttributes & EFI_MEMORY_CACHETYPE_MASK) == EFI_MEMORY_UC) {\r
373 if (ArmReadCurrentEL () == AARCH64_EL2) {\r
374 PageAttributes |= TT_XN_MASK;\r
375 } else {\r
376 PageAttributes |= TT_UXN_MASK | TT_PXN_MASK;\r
377 }\r
378 }\r
379\r
380 if ((GcdAttributes & EFI_MEMORY_RO) != 0) {\r
381 PageAttributes |= TT_AP_RO_RO;\r
382 }\r
383\r
384 return PageAttributes | TT_AF;\r
385}\r
386\r
f49ea03d 387EFI_STATUS\r
521f3ced 388ArmSetMemoryAttributes (\r
d7f03464
AB
389 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
390 IN UINT64 Length,\r
d9c0d991 391 IN UINT64 Attributes\r
d7f03464
AB
392 )\r
393{\r
e0307a7d
AB
394 UINT64 PageAttributes;\r
395 UINT64 PageAttributeMask;\r
396\r
397 PageAttributes = GcdAttributeToPageAttribute (Attributes);\r
398 PageAttributeMask = 0;\r
399\r
400 if ((Attributes & EFI_MEMORY_CACHETYPE_MASK) == 0) {\r
401 //\r
402 // No memory type was set in Attributes, so we are going to update the\r
403 // permissions only.\r
404 //\r
405 PageAttributes &= TT_AP_MASK | TT_UXN_MASK | TT_PXN_MASK;\r
406 PageAttributeMask = ~(TT_ADDRESS_MASK_BLOCK_ENTRY | TT_AP_MASK |\r
407 TT_PXN_MASK | TT_XN_MASK);\r
408 }\r
d7f03464 409\r
191fa79b
AB
410 return UpdateRegionMapping (BaseAddress, Length, PageAttributes,\r
411 PageAttributeMask);\r
d7f03464
AB
412}\r
413\r
414STATIC\r
f49ea03d 415EFI_STATUS\r
d7f03464
AB
416SetMemoryRegionAttribute (\r
417 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
418 IN UINT64 Length,\r
419 IN UINT64 Attributes,\r
420 IN UINT64 BlockEntryMask\r
421 )\r
422{\r
191fa79b 423 return UpdateRegionMapping (BaseAddress, Length, Attributes, BlockEntryMask);\r
d7f03464
AB
424}\r
425\r
f49ea03d 426EFI_STATUS\r
d7f03464
AB
427ArmSetMemoryRegionNoExec (\r
428 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
429 IN UINT64 Length\r
430 )\r
431{\r
432 UINT64 Val;\r
433\r
434 if (ArmReadCurrentEL () == AARCH64_EL1) {\r
435 Val = TT_PXN_MASK | TT_UXN_MASK;\r
436 } else {\r
437 Val = TT_XN_MASK;\r
438 }\r
439\r
440 return SetMemoryRegionAttribute (\r
441 BaseAddress,\r
442 Length,\r
443 Val,\r
444 ~TT_ADDRESS_MASK_BLOCK_ENTRY);\r
445}\r
446\r
f49ea03d 447EFI_STATUS\r
d7f03464
AB
448ArmClearMemoryRegionNoExec (\r
449 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
450 IN UINT64 Length\r
451 )\r
452{\r
453 UINT64 Mask;\r
454\r
455 // XN maps to UXN in the EL1&0 translation regime\r
456 Mask = ~(TT_ADDRESS_MASK_BLOCK_ENTRY | TT_PXN_MASK | TT_XN_MASK);\r
457\r
458 return SetMemoryRegionAttribute (\r
459 BaseAddress,\r
460 Length,\r
461 0,\r
462 Mask);\r
463}\r
464\r
f49ea03d 465EFI_STATUS\r
d7f03464
AB
466ArmSetMemoryRegionReadOnly (\r
467 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
468 IN UINT64 Length\r
469 )\r
470{\r
471 return SetMemoryRegionAttribute (\r
472 BaseAddress,\r
473 Length,\r
474 TT_AP_RO_RO,\r
475 ~TT_ADDRESS_MASK_BLOCK_ENTRY);\r
476}\r
477\r
f49ea03d 478EFI_STATUS\r
d7f03464
AB
479ArmClearMemoryRegionReadOnly (\r
480 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
481 IN UINT64 Length\r
482 )\r
483{\r
484 return SetMemoryRegionAttribute (\r
485 BaseAddress,\r
486 Length,\r
487 TT_AP_RW_RW,\r
488 ~(TT_ADDRESS_MASK_BLOCK_ENTRY | TT_AP_MASK));\r
489}\r
490\r
f49ea03d 491EFI_STATUS\r
d7f03464
AB
492EFIAPI\r
493ArmConfigureMmu (\r
494 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable,\r
495 OUT VOID **TranslationTableBase OPTIONAL,\r
496 OUT UINTN *TranslationTableSize OPTIONAL\r
497 )\r
498{\r
499 VOID* TranslationTable;\r
d7f03464 500 UINT32 TranslationTableAttribute;\r
d7f03464 501 UINT64 MaxAddress;\r
d7f03464
AB
502 UINTN T0SZ;\r
503 UINTN RootTableEntryCount;\r
504 UINT64 TCR;\r
f49ea03d 505 EFI_STATUS Status;\r
d7f03464
AB
506\r
507 if(MemoryTable == NULL) {\r
508 ASSERT (MemoryTable != NULL);\r
f49ea03d 509 return EFI_INVALID_PARAMETER;\r
d7f03464
AB
510 }\r
511\r
e36b243c
AB
512 //\r
513 // Limit the virtual address space to what we can actually use: UEFI\r
514 // mandates a 1:1 mapping, so no point in making the virtual address\r
515 // space larger than the physical address space. We also have to take\r
516 // into account the architectural limitations that result from UEFI's\r
517 // use of 4 KB pages.\r
518 //\r
519 MaxAddress = MIN (LShiftU64 (1ULL, ArmGetPhysicalAddressBits ()) - 1,\r
1c36f028 520 MAX_ALLOC_ADDRESS);\r
d7f03464
AB
521\r
522 // Lookup the Table Level to get the information\r
523 LookupAddresstoRootTable (MaxAddress, &T0SZ, &RootTableEntryCount);\r
524\r
525 //\r
526 // Set TCR that allows us to retrieve T0SZ in the subsequent functions\r
527 //\r
528 // Ideally we will be running at EL2, but should support EL1 as well.\r
529 // UEFI should not run at EL3.\r
530 if (ArmReadCurrentEL () == AARCH64_EL2) {\r
531 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2\r
532 TCR = T0SZ | (1UL << 31) | (1UL << 23) | TCR_TG0_4KB;\r
533\r
534 // Set the Physical Address Size using MaxAddress\r
535 if (MaxAddress < SIZE_4GB) {\r
536 TCR |= TCR_PS_4GB;\r
537 } else if (MaxAddress < SIZE_64GB) {\r
538 TCR |= TCR_PS_64GB;\r
539 } else if (MaxAddress < SIZE_1TB) {\r
540 TCR |= TCR_PS_1TB;\r
541 } else if (MaxAddress < SIZE_4TB) {\r
542 TCR |= TCR_PS_4TB;\r
543 } else if (MaxAddress < SIZE_16TB) {\r
544 TCR |= TCR_PS_16TB;\r
545 } else if (MaxAddress < SIZE_256TB) {\r
546 TCR |= TCR_PS_256TB;\r
547 } else {\r
548 DEBUG ((EFI_D_ERROR, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress));\r
549 ASSERT (0); // Bigger than 48-bit memory space are not supported\r
f49ea03d 550 return EFI_UNSUPPORTED;\r
d7f03464
AB
551 }\r
552 } else if (ArmReadCurrentEL () == AARCH64_EL1) {\r
553 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.\r
554 TCR = T0SZ | TCR_TG0_4KB | TCR_TG1_4KB | TCR_EPD1;\r
555\r
556 // Set the Physical Address Size using MaxAddress\r
557 if (MaxAddress < SIZE_4GB) {\r
558 TCR |= TCR_IPS_4GB;\r
559 } else if (MaxAddress < SIZE_64GB) {\r
560 TCR |= TCR_IPS_64GB;\r
561 } else if (MaxAddress < SIZE_1TB) {\r
562 TCR |= TCR_IPS_1TB;\r
563 } else if (MaxAddress < SIZE_4TB) {\r
564 TCR |= TCR_IPS_4TB;\r
565 } else if (MaxAddress < SIZE_16TB) {\r
566 TCR |= TCR_IPS_16TB;\r
567 } else if (MaxAddress < SIZE_256TB) {\r
568 TCR |= TCR_IPS_256TB;\r
569 } else {\r
570 DEBUG ((EFI_D_ERROR, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress));\r
571 ASSERT (0); // Bigger than 48-bit memory space are not supported\r
f49ea03d 572 return EFI_UNSUPPORTED;\r
d7f03464
AB
573 }\r
574 } else {\r
575 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.\r
f49ea03d 576 return EFI_UNSUPPORTED;\r
d7f03464
AB
577 }\r
578\r
35718840
AB
579 //\r
580 // Translation table walks are always cache coherent on ARMv8-A, so cache\r
581 // maintenance on page tables is never needed. Since there is a risk of\r
582 // loss of coherency when using mismatched attributes, and given that memory\r
583 // is mapped cacheable except for extraordinary cases (such as non-coherent\r
584 // DMA), have the page table walker perform cached accesses as well, and\r
585 // assert below that that matches the attributes we use for CPU accesses to\r
586 // the region.\r
587 //\r
588 TCR |= TCR_SH_INNER_SHAREABLE |\r
589 TCR_RGN_OUTER_WRITE_BACK_ALLOC |\r
590 TCR_RGN_INNER_WRITE_BACK_ALLOC;\r
591\r
d7f03464
AB
592 // Set TCR\r
593 ArmSetTCR (TCR);\r
594\r
aa961dea
AB
595 // Allocate pages for translation table\r
596 TranslationTable = AllocatePages (1);\r
d7f03464 597 if (TranslationTable == NULL) {\r
f49ea03d 598 return EFI_OUT_OF_RESOURCES;\r
d7f03464
AB
599 }\r
600 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent\r
601 // functions without needing to pass this value across the functions. The MMU is only enabled\r
602 // after the translation tables are populated.\r
603 ArmSetTTBR0 (TranslationTable);\r
604\r
605 if (TranslationTableBase != NULL) {\r
606 *TranslationTableBase = TranslationTable;\r
607 }\r
608\r
609 if (TranslationTableSize != NULL) {\r
aa961dea 610 *TranslationTableSize = RootTableEntryCount * sizeof(UINT64);\r
d7f03464
AB
611 }\r
612\r
748fea62
AB
613 //\r
614 // Make sure we are not inadvertently hitting in the caches\r
615 // when populating the page tables.\r
616 //\r
617 InvalidateDataCacheRange (TranslationTable,\r
618 RootTableEntryCount * sizeof(UINT64));\r
aa961dea 619 ZeroMem (TranslationTable, RootTableEntryCount * sizeof(UINT64));\r
d7f03464
AB
620\r
621 TranslationTableAttribute = TT_ATTR_INDX_INVALID;\r
622 while (MemoryTable->Length != 0) {\r
35718840
AB
623\r
624 DEBUG_CODE_BEGIN ();\r
625 // Find the memory attribute for the Translation Table\r
626 if ((UINTN)TranslationTable >= MemoryTable->PhysicalBase &&\r
aa961dea 627 (UINTN)TranslationTable + EFI_PAGE_SIZE <= MemoryTable->PhysicalBase +\r
35718840
AB
628 MemoryTable->Length) {\r
629 TranslationTableAttribute = MemoryTable->Attributes;\r
630 }\r
631 DEBUG_CODE_END ();\r
d7f03464
AB
632\r
633 Status = FillTranslationTable (TranslationTable, MemoryTable);\r
f49ea03d 634 if (EFI_ERROR (Status)) {\r
d7f03464
AB
635 goto FREE_TRANSLATION_TABLE;\r
636 }\r
637 MemoryTable++;\r
638 }\r
639\r
35718840
AB
640 ASSERT (TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK ||\r
641 TranslationTableAttribute == ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK);\r
d7f03464
AB
642\r
643 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY, MAIR_ATTR_DEVICE_MEMORY) | // mapped to EFI_MEMORY_UC\r
644 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE) | // mapped to EFI_MEMORY_WC\r
645 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH) | // mapped to EFI_MEMORY_WT\r
646 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK)); // mapped to EFI_MEMORY_WB\r
647\r
648 ArmDisableAlignmentCheck ();\r
526f160f 649 ArmEnableStackAlignmentCheck ();\r
d7f03464
AB
650 ArmEnableInstructionCache ();\r
651 ArmEnableDataCache ();\r
652\r
653 ArmEnableMmu ();\r
f49ea03d 654 return EFI_SUCCESS;\r
d7f03464
AB
655\r
656FREE_TRANSLATION_TABLE:\r
aa961dea 657 FreePages (TranslationTable, 1);\r
d7f03464
AB
658 return Status;\r
659}\r
660\r
661RETURN_STATUS\r
662EFIAPI\r
663ArmMmuBaseLibConstructor (\r
664 VOID\r
665 )\r
666{\r
667 extern UINT32 ArmReplaceLiveTranslationEntrySize;\r
668\r
669 //\r
670 // The ArmReplaceLiveTranslationEntry () helper function may be invoked\r
671 // with the MMU off so we have to ensure that it gets cleaned to the PoC\r
672 //\r
673 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry,\r
674 ArmReplaceLiveTranslationEntrySize);\r
675\r
676 return RETURN_SUCCESS;\r
677}\r