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