]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/Mmu.c
Added a library for the default exception handler and started to add a dissasembler...
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / Mmu.c
CommitLineData
aeb61534
A
1/*++\r
2\r
3Copyright (c) 2009, Hewlett-Packard Company \r
4Portions copyright (c) 2010, Apple Inc. All rights reserved.\r
5\r
6All rights reserved. This program and the accompanying materials \r
7are licensed and made available under the terms and conditions of the BSD License \r
8which accompanies this distribution. The full text of the license may be found at \r
9http://opensource.org/licenses/bsd-license.php \r
10 \r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
13\r
14\r
15--*/\r
16\r
17#include "CpuDxe.h"\r
18\r
f659880b 19\r
aeb61534
A
20//\r
21// Translation/page table definitions\r
22//\r
23\r
24// First Level Descriptors\r
25typedef UINT32 ARM_FIRST_LEVEL_DESCRIPTOR;\r
26\r
27// memory space covered by a first level descriptor\r
28#define ARM_PAGE_DESC_ENTRY_MVA_SIZE 0x00100000 // 1MB\r
29\r
30// number of first level descriptors to cover entire 32-bit memory space\r
31#define FIRST_LEVEL_ENTRY_COUNT (0xFFFFFFFF / ARM_PAGE_DESC_ENTRY_MVA_SIZE + 1)\r
32\r
33\r
34// page table 1st level descriptor entries\r
35#define ARM_PAGE_DESC_BASE_MASK 0xFFFFFC00\r
36#define ARM_PAGE_DESC_BASE_SHFIT 10\r
37#define ARM_PAGE_DESC_DOMAIN_MASK 0x000001E0\r
38#define ARM_PAGE_DESC_DOMAIN_SHIFT 5\r
39#define ARM_PAGE_DESC_NS 0x00000008\r
40\r
41#define ARM_FIRST_LEVEL_DESC_ALIGN 0x00004000 // 16KB\r
42\r
43// section 1st level desriptor entries\r
44#define ARM_SECTION_BASE_MASK 0xFFF00000\r
45#define ARM_SECTION_BASE_SHIFT 20\r
46#define ARM_SECTION_NS 0x00080000\r
47#define ARM_SECTION_nG 0x00020000\r
48#define ARM_SECTION_S 0x00010000\r
49#define ARM_SECTION_AP2 0x00008000\r
50#define ARM_SECTION_TEX_MASK 0x00007000\r
51#define ARM_SECTION_TEX_SHIFT 12\r
52#define ARM_SECTION_AP10_MASK 0x00000C00\r
53#define ARM_SECTION_AP10_SHIFT 10\r
54#define ARM_SECTION_DOMAIN_MASK 0x000001E0\r
55#define ARM_SECTION_DOMAIN_SHIFT 5\r
56#define ARM_SECTION_XN 0x00000010\r
57#define ARM_SECTION_C 0x00000008\r
58#define ARM_SECTION_B 0x00000004\r
59\r
60// section level AP[2:0] definitions\r
61#define ARM_SECTION_AP_NO_ACCESS 0 // AP[2:0] = 0\r
62#define ARM_SECTION_AP_READ_WRITE ARM_SECTION_AP10_MASK // AP[2:0] = 011\r
63#define ARM_SECTION_AP_READ_ONLY (ARM_SECTION_AP2 | ARM_SECTION_AP10_MASK) // AP[2:0] = 111\r
64\r
65// common 1st level descriptor fields\r
66#define ARM_DESC_TYPE_MASK 0x00000003\r
67\r
68// descriptor type values\r
69#define ARM_DESC_TYPE_FAULT 0x0\r
70#define ARM_DESC_TYPE_PAGE_TABLE 0x1\r
71#define ARM_DESC_TYPE_SECTION 0x2\r
72\r
73\r
74// Second Level Descriptors\r
75typedef UINT32 ARM_PAGE_TABLE_ENTRY;\r
76\r
77// small page 2nd level descriptor entries\r
78#define ARM_SMALL_PAGE_BASE_MASK 0xFFFFF000\r
79#define ARM_SMALL_PAGE_INDEX_MASK 0x000FF000\r
80#define ARM_SMALL_PAGE_BASE_SHIFT 12\r
81#define ARM_SMALL_PAGE_TEX_MASK 0x000001C0\r
82#define ARM_SMALL_PAGE_TEX_SHIFT 6\r
83#define ARM_SMALL_PAGE_XN 0x00000001\r
84\r
85// large page 2nd level descriptor entries\r
86#define ARM_LARGE_PAGE_BASE_MASK 0xFFFF0000\r
87#define ARM_LARGE_PAGE_BASE_SHIFT 16\r
88#define ARM_LARGE_PAGE_TEX_MASK 0x00007000\r
89#define ARM_LARGE_PAGE_TEX_SHIFT 12\r
90#define ARM_LARGE_PAGE_XN 0x00008000\r
91\r
92// common 2nd level desriptor fields\r
93#define ARM_PAGE_nG 0x00000800\r
94#define ARM_PAGE_S 0x00000400\r
95#define ARM_PAGE_AP2 0x00000200\r
96#define ARM_PAGE_AP10_MASK 0x00000030\r
97#define ARM_PAGE_AP10_SHIFT 4\r
98#define ARM_PAGE_C 0x00000008\r
99#define ARM_PAGE_B 0x00000004\r
100#define ARM_PAGE_DESC_TYPE_MASK 0x00000003\r
101\r
102// descriptor type values\r
103#define ARM_PAGE_TYPE_FAULT 0x0\r
104#define ARM_PAGE_TYPE_LARGE 0x1\r
105#define ARM_PAGE_TYPE_SMALL 0x2\r
106#define ARM_PAGE_TYPE_SMALL_XN 0x3\r
107\r
108#define SMALL_PAGE_TABLE_ENTRY_COUNT (ARM_PAGE_DESC_ENTRY_MVA_SIZE / EFI_PAGE_SIZE)\r
109\r
110\r
111// Translation Table Base 0 fields\r
112#define ARM_TTBR0_BASE_MASK 0xFFFFC000\r
113#define ARM_TTBR0_BASE_SHIFT 14\r
114#define ARM_TTRB0_NOS 0x00000020\r
115\r
116// define the combination of interesting attributes: cacheability and access permissions\r
117#define ARM_SECTION_CACHEABILITY_MASK ( ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B )\r
118#define ARM_SECTION_RW_PERMISSIONS_MASK ( ARM_SECTION_AP2 | ARM_SECTION_AP10_MASK )\r
119#define ARM_DESCRIPTOR_ATTRIBUTES ( ARM_SECTION_CACHEABILITY_MASK | ARM_SECTION_RW_PERMISSIONS_MASK | ARM_SECTION_XN )\r
120\r
121// cacheability values for section entries\r
122#define ARM_SECTION_STRONGLY_ORDERED 0\r
123#define ARM_SECTION_SHAREABLE_DEVICE ARM_SECTION_B\r
124#define ARM_SECTION_WRITE_THROUGH ARM_SECTION_C\r
125#define ARM_SECTION_WRITE_BACK_NWA ( ARM_SECTION_C| ARM_SECTION_B )\r
126#define ARM_SECTION_NORMAL_UNCACHEABLE ( 0x1 << ARM_SECTION_TEX_SHIFT )\r
127#define ARM_SECTION_WRITE_BACK ( ( 0x1 << ARM_SECTION_TEX_SHIFT ) | ARM_SECTION_C | ARM_SECTION_B )\r
128#define ARM_SECTION_NONSHAREABLE_DEVICE ( 0x2 << ARM_SECTION_TEX_SHIFT )\r
129\r
130// permissions values for section entries\r
131#define ARM_SECTION_NO_ACCESS 0\r
132#define ARM_SECTION_PRIV_ACCESS_ONLY ( 0x1 << ARM_SECTION_AP10_SHIFT)\r
133#define ARM_SECTION_USER_READ_ONLY ( 0x2 << ARM_SECTION_AP10_SHIFT)\r
134#define ARM_SECTION_FULL_ACCESS ( 0x3 << ARM_SECTION_AP10_SHIFT)\r
135#define ARM_SECTION_PRIV_READ_ONLY ( ARM_SECTION_AP2 | (0x1 << ARM_SECTION_AP10_SHIFT) )\r
136#define ARM_SECTION_READ_ONLY_DEP ( ARM_SECTION_AP2 | (0x2 << ARM_SECTION_AP10_SHIFT) )\r
137#define ARM_SECTION_READ_ONLY ( ARM_SECTION_AP2 | (0x3 << ARM_SECTION_AP10_SHIFT) )\r
138\r
139\r
140\r
141EFI_STATUS \r
142SectionToGcdAttributes (\r
143 IN UINT32 SectionAttributes,\r
144 OUT UINT64 *GcdAttributes\r
145 )\r
146{\r
147 *GcdAttributes = 0;\r
148\r
149 // determine cacheability attributes\r
150 switch(SectionAttributes & ARM_SECTION_CACHEABILITY_MASK) {\r
151 case ARM_SECTION_STRONGLY_ORDERED:\r
152 *GcdAttributes |= EFI_MEMORY_UC;\r
153 break;\r
154 case ARM_SECTION_SHAREABLE_DEVICE:\r
155 *GcdAttributes |= EFI_MEMORY_UC;\r
156 break;\r
157 case ARM_SECTION_WRITE_THROUGH:\r
158 *GcdAttributes |= EFI_MEMORY_WT;\r
159 break;\r
160 case ARM_SECTION_WRITE_BACK_NWA:\r
161 *GcdAttributes |= EFI_MEMORY_WB;\r
162 break;\r
163 case ARM_SECTION_NORMAL_UNCACHEABLE:\r
164 *GcdAttributes |= EFI_MEMORY_WC;\r
165 break;\r
166 case ARM_SECTION_WRITE_BACK:\r
167 *GcdAttributes |= EFI_MEMORY_WB;\r
168 break;\r
169 case ARM_SECTION_NONSHAREABLE_DEVICE:\r
170 *GcdAttributes |= EFI_MEMORY_UC;\r
171 break;\r
172 default:\r
173 return EFI_UNSUPPORTED;\r
aeb61534
A
174 }\r
175 \r
176 // determine protection attributes\r
177 switch(SectionAttributes & ARM_SECTION_RW_PERMISSIONS_MASK) {\r
178 case ARM_SECTION_NO_ACCESS: // no read, no write\r
f659880b 179 //*GcdAttributes |= EFI_MEMORY_WP | EFI_MEMORY_RP;\r
aeb61534
A
180 break;\r
181\r
182 case ARM_SECTION_PRIV_ACCESS_ONLY:\r
183 case ARM_SECTION_FULL_ACCESS:\r
184 // normal read/write access, do not add additional attributes\r
185 break;\r
186\r
187 // read only cases map to write-protect\r
188 case ARM_SECTION_PRIV_READ_ONLY:\r
189 case ARM_SECTION_READ_ONLY_DEP:\r
190 case ARM_SECTION_READ_ONLY:\r
191 *GcdAttributes |= EFI_MEMORY_WP;\r
192 break;\r
193\r
194 default:\r
195 return EFI_UNSUPPORTED;\r
aeb61534
A
196 }\r
197\r
198 // now process eXectue Never attribute\r
199 if ((SectionAttributes & ARM_SECTION_XN) != 0 ) {\r
200 *GcdAttributes |= EFI_MEMORY_XP;\r
201 }\r
202\r
203 return EFI_SUCCESS;\r
204}\r
205\r
f659880b
A
206/**\r
207 Searches memory descriptors covered by given memory range.\r
208\r
209 This function searches into the Gcd Memory Space for descriptors\r
210 (from StartIndex to EndIndex) that contains the memory range\r
211 specified by BaseAddress and Length.\r
212\r
213 @param MemorySpaceMap Gcd Memory Space Map as array.\r
214 @param NumberOfDescriptors Number of descriptors in map.\r
215 @param BaseAddress BaseAddress for the requested range.\r
216 @param Length Length for the requested range.\r
217 @param StartIndex Start index into the Gcd Memory Space Map.\r
218 @param EndIndex End index into the Gcd Memory Space Map.\r
219\r
220 @retval EFI_SUCCESS Search successfully.\r
221 @retval EFI_NOT_FOUND The requested descriptors does not exist.\r
222\r
223**/\r
224EFI_STATUS\r
225SearchGcdMemorySpaces (\r
226 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
227 IN UINTN NumberOfDescriptors,\r
228 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
229 IN UINT64 Length,\r
230 OUT UINTN *StartIndex,\r
231 OUT UINTN *EndIndex\r
232 )\r
233{\r
234 UINTN Index;\r
235\r
236 *StartIndex = 0;\r
237 *EndIndex = 0;\r
238 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
239 if (BaseAddress >= MemorySpaceMap[Index].BaseAddress &&\r
240 BaseAddress < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r
241 *StartIndex = Index;\r
242 }\r
243 if (BaseAddress + Length - 1 >= MemorySpaceMap[Index].BaseAddress &&\r
244 BaseAddress + Length - 1 < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r
245 *EndIndex = Index;\r
246 return EFI_SUCCESS;\r
247 }\r
248 }\r
249 return EFI_NOT_FOUND;\r
250}\r
251\r
252\r
253/**\r
254 Sets the attributes for a specified range in Gcd Memory Space Map.\r
255\r
256 This function sets the attributes for a specified range in\r
257 Gcd Memory Space Map.\r
258\r
259 @param MemorySpaceMap Gcd Memory Space Map as array\r
260 @param NumberOfDescriptors Number of descriptors in map\r
261 @param BaseAddress BaseAddress for the range\r
262 @param Length Length for the range\r
263 @param Attributes Attributes to set\r
264\r
265 @retval EFI_SUCCESS Memory attributes set successfully\r
266 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space\r
267\r
268**/\r
269EFI_STATUS\r
270SetGcdMemorySpaceAttributes (\r
271 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
272 IN UINTN NumberOfDescriptors,\r
273 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
274 IN UINT64 Length,\r
275 IN UINT64 Attributes\r
276 )\r
277{\r
278 EFI_STATUS Status;\r
279 UINTN Index;\r
280 UINTN StartIndex;\r
281 UINTN EndIndex;\r
282 EFI_PHYSICAL_ADDRESS RegionStart;\r
283 UINT64 RegionLength;\r
284\r
285 //\r
286 // Get all memory descriptors covered by the memory range\r
287 //\r
288 Status = SearchGcdMemorySpaces (\r
289 MemorySpaceMap,\r
290 NumberOfDescriptors,\r
291 BaseAddress,\r
292 Length,\r
293 &StartIndex,\r
294 &EndIndex\r
295 );\r
296 if (EFI_ERROR (Status)) {\r
297 return Status;\r
298 }\r
299\r
300 //\r
301 // Go through all related descriptors and set attributes accordingly\r
302 //\r
303 for (Index = StartIndex; Index <= EndIndex; Index++) {\r
304 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
305 continue;\r
306 }\r
307 //\r
308 // Calculate the start and end address of the overlapping range\r
309 //\r
310 if (BaseAddress >= MemorySpaceMap[Index].BaseAddress) {\r
311 RegionStart = BaseAddress;\r
312 } else {\r
313 RegionStart = MemorySpaceMap[Index].BaseAddress;\r
314 }\r
315 if (BaseAddress + Length - 1 < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r
316 RegionLength = BaseAddress + Length - RegionStart;\r
317 } else {\r
318 RegionLength = MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - RegionStart;\r
319 }\r
320 //\r
321 // Set memory attributes according to MTRR attribute and the original attribute of descriptor\r
322 //\r
323 gDS->SetMemorySpaceAttributes (\r
324 RegionStart,\r
325 RegionLength,\r
326 (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) | (MemorySpaceMap[Index].Capabilities & Attributes)\r
327 );\r
328 }\r
329\r
330 return EFI_SUCCESS;\r
331}\r
aeb61534
A
332\r
333\r
334EFI_STATUS\r
335SyncCacheConfig (\r
336 IN EFI_CPU_ARCH_PROTOCOL *CpuProtocol\r
337 )\r
338{\r
f659880b
A
339 EFI_STATUS Status;\r
340 UINT32 i;\r
341 UINT32 Descriptor;\r
342 UINT32 SectionAttributes;\r
343 EFI_PHYSICAL_ADDRESS NextRegionBase;\r
344 UINT64 NextRegionLength;\r
345 UINT64 GcdAttributes;\r
346 UINT32 NextRegionAttributes = 0;\r
aeb61534 347 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
f659880b
A
348 UINTN NumberOfDescriptors;\r
349 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
aeb61534
A
350\r
351\r
225290eb 352 DEBUG ((EFI_D_PAGE, "SyncCacheConfig()\n"));\r
f659880b 353\r
aeb61534
A
354 // This code assumes MMU is enabled and filed with section translations\r
355 ASSERT (ArmMmuEnabled ());\r
356\r
f659880b
A
357 //\r
358 // Get the memory space map from GCD\r
359 //\r
360 MemorySpaceMap = NULL;\r
361 Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
362 ASSERT_EFI_ERROR (Status);\r
363\r
aeb61534
A
364\r
365 // The GCD implementation maintains its own copy of the state of memory space attributes. GCD needs\r
366 // to know what the initial memory space attributes are. The CPU Arch. Protocol does not provide a\r
367 // GetMemoryAttributes function for GCD to get this so we must resort to calling GCD (as if we were\r
368 // a client) to update its copy of the attributes. This is bad architecture and should be replaced\r
369 // with a way for GCD to query the CPU Arch. driver of the existing memory space attributes instead.\r
370\r
371 // obtain page table base\r
372 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)(ArmGetTranslationTableBaseAddress ());\r
373\r
374\r
375 // iterate through each 1MB descriptor\r
376 NextRegionBase = NextRegionLength = 0;\r
377 for (i=0; i< FIRST_LEVEL_ENTRY_COUNT; i++) {\r
378\r
f659880b
A
379 // obtain existing descriptor and make sure it contains a valid Base Address even if it is a fault section\r
380 Descriptor = FirstLevelTable[i] | (ARM_SECTION_BASE_MASK & (i << ARM_SECTION_BASE_SHIFT));\r
aeb61534
A
381\r
382 // extract attributes (cacheability and permissions)\r
383 SectionAttributes = Descriptor & 0xDEC;\r
384\r
385 // do we already have an existing region (or are we about to finish)?\r
386 // Skip the first entry, and make sure we close on the last entry\r
387 if ( (NextRegionLength > 0) || (i == (FIRST_LEVEL_ENTRY_COUNT-1)) ) {\r
388 // attributes are changing, update attributes in GCD\r
389 if (SectionAttributes != NextRegionAttributes) {\r
390 \r
391 // convert section entry attributes to GCD bitmask\r
392 Status = SectionToGcdAttributes (NextRegionAttributes, &GcdAttributes);\r
f659880b 393 ASSERT_EFI_ERROR (Status);\r
aeb61534
A
394\r
395 // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)\r
f659880b
A
396 SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, NextRegionBase, NextRegionLength, GcdAttributes);\r
397\r
aeb61534
A
398\r
399 // start on a new region\r
400 NextRegionLength = 0;\r
401 NextRegionBase = Descriptor & ARM_SECTION_BASE_MASK;\r
402 }\r
403 }\r
404\r
405 // starting a new region?\r
406 if (NextRegionLength == 0) {\r
407 NextRegionAttributes = SectionAttributes;\r
408 }\r
409\r
410 NextRegionLength += ARM_PAGE_DESC_ENTRY_MVA_SIZE;\r
411\r
412 } // section entry loop\r
413\r
414 return EFI_SUCCESS;\r
415}\r
416\r
417\r
418\r
419EFI_STATUS\r
420UpdatePageEntries (\r
421 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
422 IN UINT64 Length,\r
423 IN UINT64 Attributes,\r
424 IN EFI_PHYSICAL_ADDRESS VirtualMask\r
425 )\r
426{\r
427 EFI_STATUS Status;\r
428 UINT32 EntryValue;\r
429 UINT32 EntryMask;\r
430 UINT32 FirstLevelIdx;\r
431 UINT32 Offset;\r
432 UINT32 NumPageEntries;\r
433 UINT32 Descriptor;\r
434 UINT32 p;\r
435 UINT32 PageTableIndex;\r
436 UINT32 PageTableEntry;\r
437\r
438 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
439 volatile ARM_PAGE_TABLE_ENTRY *PageTable;\r
440\r
920cb926
A
441 Status = EFI_SUCCESS;\r
442\r
aeb61534
A
443 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r
444 // EntryValue: values at bit positions specified by EntryMask\r
d4f167a9
A
445 EntryMask = ARM_PAGE_DESC_TYPE_MASK;\r
446 EntryValue = ARM_PAGE_TYPE_SMALL;\r
aeb61534
A
447 // Although the PI spec is unclear on this the GCD guarantees that only\r
448 // one Attribute bit is set at a time, so we can safely use a switch statement\r
449 switch (Attributes) {\r
450 case EFI_MEMORY_UC:\r
451 // modify cacheability attributes\r
d4f167a9 452 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
aeb61534 453 // map to strongly ordered\r
d4f167a9 454 EntryValue |= 0; // TEX[2:0] = 0, C=0, B=0\r
aeb61534
A
455 break;\r
456\r
457 case EFI_MEMORY_WC:\r
458 // modify cacheability attributes\r
d4f167a9 459 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
aeb61534 460 // map to normal non-cachable\r
d4f167a9 461 EntryValue |= (0x1 << ARM_SMALL_PAGE_TEX_SHIFT); // TEX [2:0]= 001 = 0x2, B=0, C=0\r
aeb61534
A
462 break;\r
463\r
464 case EFI_MEMORY_WT:\r
465 // modify cacheability attributes\r
d4f167a9 466 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
aeb61534 467 // write through with no-allocate\r
d4f167a9 468 EntryValue |= ARM_PAGE_C; // TEX [2:0] = 0, C=1, B=0\r
aeb61534
A
469 break;\r
470\r
471 case EFI_MEMORY_WB:\r
472 // modify cacheability attributes\r
d4f167a9 473 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
aeb61534 474 // write back (with allocate)\r
d4f167a9 475 EntryValue |= (0x1 << ARM_SMALL_PAGE_TEX_SHIFT) | ARM_PAGE_C | ARM_PAGE_B; // TEX [2:0] = 001, C=1, B=1\r
aeb61534
A
476 break;\r
477\r
478 case EFI_MEMORY_WP:\r
479 case EFI_MEMORY_XP:\r
480 case EFI_MEMORY_UCE:\r
481 // cannot be implemented UEFI definition unclear for ARM\r
482 // Cause a page fault if these ranges are accessed.\r
d4f167a9 483 EntryValue = ARM_PAGE_TYPE_FAULT;\r
225290eb 484 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): setting page %lx with unsupported attribute %x will page fault on access\n", BaseAddress, Attributes));\r
aeb61534
A
485 break;\r
486\r
487 default:\r
488 return EFI_UNSUPPORTED;\r
aeb61534
A
489 }\r
490\r
491 // obtain page table base\r
492 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTranslationTableBaseAddress ();\r
493\r
494 // calculate number of 4KB page table entries to change\r
495 NumPageEntries = Length/EFI_PAGE_SIZE;\r
496 \r
497 // iterate for the number of 4KB pages to change\r
498 Offset = 0;\r
499 for(p=0; p<NumPageEntries; p++) {\r
500 // calculate index into first level translation table for page table value\r
501 \r
502 FirstLevelIdx = ((BaseAddress + Offset) & ARM_SECTION_BASE_MASK) >> ARM_SECTION_BASE_SHIFT;\r
503 ASSERT (FirstLevelIdx < FIRST_LEVEL_ENTRY_COUNT);\r
504\r
505 // read the descriptor from the first level page table\r
506 Descriptor = FirstLevelTable[FirstLevelIdx];\r
507\r
508 // does this descriptor need to be converted from section entry to 4K pages?\r
f659880b 509 if ((Descriptor & ARM_DESC_TYPE_MASK) != ARM_DESC_TYPE_PAGE_TABLE ) {\r
aeb61534
A
510 Status = ConvertSectionToPages (FirstLevelIdx << ARM_SECTION_BASE_SHIFT);\r
511 if (EFI_ERROR(Status)) {\r
512 // exit for loop\r
513 break; \r
514 } \r
515 \r
516 // re-read descriptor\r
517 Descriptor = FirstLevelTable[FirstLevelIdx];\r
518 }\r
519\r
520 // obtain page table base address\r
521 PageTable = (ARM_PAGE_TABLE_ENTRY *)(Descriptor & ARM_SMALL_PAGE_BASE_MASK);\r
522\r
523 // calculate index into the page table\r
524 PageTableIndex = ((BaseAddress + Offset) & ARM_SMALL_PAGE_INDEX_MASK) >> ARM_SMALL_PAGE_BASE_SHIFT;\r
f659880b 525 ASSERT (PageTableIndex < SMALL_PAGE_TABLE_ENTRY_COUNT);\r
aeb61534
A
526\r
527 // get the entry\r
528 PageTableEntry = PageTable[PageTableIndex];\r
529\r
530 // mask off appropriate fields\r
531 PageTableEntry &= ~EntryMask;\r
532\r
533 // mask in new attributes and/or permissions\r
534 PageTableEntry |= EntryValue;\r
535\r
536 if (VirtualMask != 0) {\r
537 // Make this virtual address point at a physical page\r
538 PageTableEntry &= ~VirtualMask;\r
539 }\r
540 \r
541 // update the entry\r
542 PageTable[PageTableIndex] = PageTableEntry; \r
543 \r
544\r
545 Status = EFI_SUCCESS;\r
546 Offset += EFI_PAGE_SIZE;\r
547 \r
548 } // end first level translation table loop\r
549\r
550 return Status;\r
551}\r
552\r
553\r
554\r
555EFI_STATUS\r
556UpdateSectionEntries (\r
557 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
558 IN UINT64 Length,\r
559 IN UINT64 Attributes,\r
560 IN EFI_PHYSICAL_ADDRESS VirtualMask\r
561 )\r
562{\r
563 EFI_STATUS Status = EFI_SUCCESS;\r
564 UINT32 EntryMask;\r
565 UINT32 EntryValue;\r
566 UINT32 FirstLevelIdx;\r
567 UINT32 NumSections;\r
568 UINT32 i;\r
569 UINT32 Descriptor;\r
570\r
571 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
572\r
573 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r
574 // EntryValue: values at bit positions specified by EntryMask\r
575\r
f659880b
A
576 // Make sure we handle a section range that is unmapped \r
577 EntryMask = ARM_DESC_TYPE_MASK;\r
578 EntryValue = ARM_DESC_TYPE_SECTION;\r
579\r
aeb61534
A
580 // Although the PI spec is unclear on this the GCD guarantees that only\r
581 // one Attribute bit is set at a time, so we can safely use a switch statement\r
582 switch(Attributes) {\r
583 case EFI_MEMORY_UC:\r
584 // modify cacheability attributes\r
f659880b 585 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
aeb61534 586 // map to strongly ordered\r
f659880b 587 EntryValue |= 0; // TEX[2:0] = 0, C=0, B=0\r
aeb61534
A
588 break;\r
589\r
590 case EFI_MEMORY_WC:\r
591 // modify cacheability attributes\r
f659880b 592 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
aeb61534 593 // map to normal non-cachable\r
f659880b 594 EntryValue |= (0x1 << ARM_SECTION_TEX_SHIFT); // TEX [2:0]= 001 = 0x2, B=0, C=0\r
aeb61534
A
595 break;\r
596\r
597 case EFI_MEMORY_WT:\r
598 // modify cacheability attributes\r
f659880b 599 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
aeb61534 600 // write through with no-allocate\r
f659880b 601 EntryValue |= ARM_SECTION_C; // TEX [2:0] = 0, C=1, B=0\r
aeb61534
A
602 break;\r
603\r
604 case EFI_MEMORY_WB:\r
605 // modify cacheability attributes\r
f659880b 606 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
aeb61534 607 // write back (with allocate)\r
f659880b 608 EntryValue |= (0x1 << ARM_SECTION_TEX_SHIFT) | ARM_SECTION_C | ARM_SECTION_B; // TEX [2:0] = 001, C=1, B=1\r
aeb61534
A
609 break;\r
610\r
611 case EFI_MEMORY_WP:\r
612 case EFI_MEMORY_XP:\r
613 case EFI_MEMORY_RP:\r
614 case EFI_MEMORY_UCE:\r
615 // cannot be implemented UEFI definition unclear for ARM\r
616 // Cause a page fault if these ranges are accessed.\r
f659880b 617 EntryValue = ARM_DESC_TYPE_FAULT;\r
225290eb 618 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): setting section %lx with unsupported attribute %x will page fault on access\n", BaseAddress, Attributes));\r
aeb61534
A
619 break;\r
620\r
621\r
622 default:\r
623 return EFI_UNSUPPORTED;\r
aeb61534
A
624 }\r
625\r
626 // obtain page table base\r
627 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTranslationTableBaseAddress ();\r
628\r
629 // calculate index into first level translation table for start of modification\r
630 FirstLevelIdx = (BaseAddress & ARM_SECTION_BASE_MASK) >> ARM_SECTION_BASE_SHIFT;\r
631 ASSERT (FirstLevelIdx < FIRST_LEVEL_ENTRY_COUNT);\r
632\r
633 // calculate number of 1MB first level entries this applies to\r
634 NumSections = Length / ARM_PAGE_DESC_ENTRY_MVA_SIZE;\r
635 \r
636 // iterate through each descriptor\r
637 for(i=0; i<NumSections; i++) {\r
638 Descriptor = FirstLevelTable[FirstLevelIdx + i];\r
639\r
640 // has this descriptor already been coverted to pages?\r
f659880b 641 if ((Descriptor & ARM_DESC_TYPE_MASK) != ARM_DESC_TYPE_PAGE_TABLE ) {\r
aeb61534
A
642 // forward this 1MB range to page table function instead\r
643 Status = UpdatePageEntries ((FirstLevelIdx + i) << ARM_SECTION_BASE_SHIFT, ARM_PAGE_DESC_ENTRY_MVA_SIZE, Attributes, VirtualMask);\r
644 } else {\r
645 // still a section entry\r
646 \r
647 // mask off appropriate fields\r
648 Descriptor &= ~EntryMask;\r
649\r
650 // mask in new attributes and/or permissions\r
651 Descriptor |= EntryValue;\r
652 if (VirtualMask != 0) {\r
653 Descriptor &= ~VirtualMask;\r
654 }\r
655\r
656 FirstLevelTable[FirstLevelIdx + i] = Descriptor;\r
657\r
658 Status = EFI_SUCCESS;\r
659 }\r
660 }\r
661\r
662 return Status;\r
663}\r
664\r
665EFI_STATUS \r
666ConvertSectionToPages (\r
667 IN EFI_PHYSICAL_ADDRESS BaseAddress\r
668 )\r
669{\r
670 EFI_STATUS Status;\r
671 EFI_PHYSICAL_ADDRESS PageTableAddr;\r
672 UINT32 FirstLevelIdx;\r
673 UINT32 SectionDescriptor;\r
674 UINT32 PageTableDescriptor;\r
675 UINT32 PageDescriptor;\r
676 UINT32 i;\r
677\r
678 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
679 volatile ARM_PAGE_TABLE_ENTRY *PageTable;\r
680\r
225290eb 681 DEBUG ((EFI_D_PAGE, "Converting section at 0x%x to pages\n", (UINTN)BaseAddress));\r
aeb61534
A
682\r
683 // obtain page table base\r
684 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTranslationTableBaseAddress ();\r
685\r
686 // calculate index into first level translation table for start of modification\r
687 FirstLevelIdx = (BaseAddress & ARM_SECTION_BASE_MASK) >> ARM_SECTION_BASE_SHIFT;\r
f659880b 688 ASSERT (FirstLevelIdx < FIRST_LEVEL_ENTRY_COUNT);\r
aeb61534
A
689\r
690 // get section attributes and convert to page attributes\r
691 SectionDescriptor = FirstLevelTable[FirstLevelIdx];\r
692 PageDescriptor = ARM_PAGE_TYPE_SMALL;\r
693 PageDescriptor |= ((SectionDescriptor & ARM_SECTION_TEX_MASK) >> ARM_SECTION_TEX_SHIFT) << ARM_SMALL_PAGE_TEX_SHIFT;\r
694 if ((SectionDescriptor & ARM_SECTION_B) != 0) {\r
695 PageDescriptor |= ARM_PAGE_B;\r
696 }\r
697 if ((SectionDescriptor & ARM_SECTION_C) != 0) {\r
698 PageDescriptor |= ARM_PAGE_C;\r
699 }\r
700 PageDescriptor |= ((SectionDescriptor & ARM_SECTION_AP10_MASK) >> ARM_SECTION_AP10_SHIFT) << ARM_PAGE_AP10_SHIFT;\r
701 if ((SectionDescriptor & ARM_SECTION_AP2) != 0) {\r
702 PageDescriptor |= ARM_PAGE_AP2;\r
703 }\r
704 if ((SectionDescriptor & ARM_SECTION_XN) != 0) {\r
705 PageDescriptor |= ARM_PAGE_TYPE_SMALL_XN;\r
706 }\r
707 if ((SectionDescriptor & ARM_SECTION_nG) != 0) {\r
708 PageDescriptor |= ARM_PAGE_nG;\r
709 }\r
710 if ((SectionDescriptor & ARM_SECTION_S) != 0) {\r
711 PageDescriptor |= ARM_PAGE_S;\r
712 }\r
713\r
714 // allocate a page table for the 4KB entries (we use up a full page even though we only need 1KB)\r
715 Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, 1, &PageTableAddr);\r
716 if (EFI_ERROR(Status)) {\r
717 return Status;\r
718 }\r
719\r
720 PageTable = (volatile ARM_PAGE_TABLE_ENTRY *)(UINTN)PageTableAddr;\r
721\r
722 // write the page table entries out\r
723 for (i=0; i<(ARM_PAGE_DESC_ENTRY_MVA_SIZE/EFI_PAGE_SIZE); i++) {\r
724 PageTable[i] = ((BaseAddress + (i << 12)) & ARM_SMALL_PAGE_BASE_MASK) | PageDescriptor;\r
725 }\r
726\r
727 // flush d-cache so descriptors make it back to uncached memory for subsequent table walks\r
f659880b 728 InvalidateDataCacheRange ((VOID *)(UINTN)PageTableAddr, EFI_PAGE_SIZE);\r
aeb61534
A
729\r
730 // formulate page table entry, Domain=0, NS=0\r
731 PageTableDescriptor = (((UINTN)PageTableAddr) & ARM_PAGE_DESC_BASE_MASK) | ARM_DESC_TYPE_PAGE_TABLE;\r
732\r
733 // write the page table entry out, repalcing section entry\r
734 FirstLevelTable[FirstLevelIdx] = PageTableDescriptor;\r
735\r
736 return EFI_SUCCESS;\r
737}\r
738\r
739\r
740\r
741EFI_STATUS\r
742SetMemoryAttributes (\r
743 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
744 IN UINT64 Length,\r
745 IN UINT64 Attributes,\r
746 IN EFI_PHYSICAL_ADDRESS VirtualMask\r
747 )\r
748{\r
749 EFI_STATUS Status;\r
750 \r
751 if(((BaseAddress & 0xFFFFF) == 0) && ((Length & 0xFFFFF) == 0)) {\r
752 // is the base and length a multiple of 1 MB?\r
225290eb 753 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): MMU section 0x%x length 0x%x to %lx\n", (UINTN)BaseAddress, (UINTN)Length, Attributes));\r
aeb61534
A
754 Status = UpdateSectionEntries (BaseAddress, Length, Attributes, VirtualMask);\r
755 } else {\r
756 // base and/or length is not a multiple of 1 MB\r
225290eb 757 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): MMU page 0x%x length 0x%x to %lx\n", (UINTN)BaseAddress, (UINTN)Length, Attributes));\r
aeb61534
A
758 Status = UpdatePageEntries (BaseAddress, Length, Attributes, VirtualMask);\r
759 }\r
760\r
761 // flush d-cache so descriptors make it back to uncached memory for subsequent table walks\r
762 // flush and invalidate pages\r
763 ArmCleanInvalidateDataCache ();\r
764 \r
765 ArmInvalidateInstructionCache ();\r
766\r
767 // invalidate all TLB entries so changes are synced\r
768 ArmInvalidateTlb (); \r
769\r
770 return Status;\r
771}\r
772\r
773\r
774/**\r
775 This function modifies the attributes for the memory region specified by BaseAddress and\r
776 Length from their current attributes to the attributes specified by Attributes.\r
777\r
778 @param This The EFI_CPU_ARCH_PROTOCOL instance.\r
779 @param BaseAddress The physical address that is the start address of a memory region.\r
780 @param Length The size in bytes of the memory region.\r
781 @param Attributes The bit mask of attributes to set for the memory region.\r
782\r
783 @retval EFI_SUCCESS The attributes were set for the memory region.\r
784 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
785 BaseAddress and Length cannot be modified.\r
786 @retval EFI_INVALID_PARAMETER Length is zero.\r
787 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
788 the memory resource range.\r
789 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
790 resource range specified by BaseAddress and Length.\r
791 The bit mask of attributes is not support for the memory resource\r
792 range specified by BaseAddress and Length.\r
793\r
794**/\r
795EFI_STATUS\r
796EFIAPI\r
797CpuSetMemoryAttributes (\r
798 IN EFI_CPU_ARCH_PROTOCOL *This,\r
799 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
800 IN UINT64 Length,\r
801 IN UINT64 Attributes\r
802 )\r
803{\r
225290eb 804 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(%lx, %lx, %lx)\n", BaseAddress, Length, Attributes));\r
aeb61534
A
805 if ( ((BaseAddress & (EFI_PAGE_SIZE-1)) != 0) || ((Length & (EFI_PAGE_SIZE-1)) != 0)){\r
806 // minimum granularity is EFI_PAGE_SIZE (4KB on ARM)\r
225290eb 807 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(%lx, %lx, %lx): minimum ganularity is EFI_PAGE_SIZE\n", BaseAddress, Length, Attributes));\r
aeb61534
A
808 return EFI_UNSUPPORTED;\r
809 }\r
810 \r
811 return SetMemoryAttributes (BaseAddress, Length, Attributes, 0);\r
812}\r
813\r
814\r
815\r
816//\r
817// Add a new protocol to support \r
818//\r
819\r
820EFI_STATUS\r
821EFIAPI\r
822CpuConvertPagesToUncachedVirtualAddress (\r
823 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
824 IN EFI_PHYSICAL_ADDRESS Address,\r
825 IN UINTN Length,\r
826 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
827 OUT UINT64 *Attributes OPTIONAL\r
828 )\r
829{\r
830 EFI_STATUS Status;\r
831 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
832 \r
833 \r
834 if (Attributes != NULL) {\r
835 Status = gDS->GetMemorySpaceDescriptor (Address, &GcdDescriptor);\r
836 if (!EFI_ERROR (Status)) {\r
837 *Attributes = GcdDescriptor.Attributes;\r
838 }\r
839 }\r
225290eb 840\r
aeb61534
A
841 //\r
842 // Make this address range page fault if accessed. If it is a DMA buffer than this would \r
843 // be the PCI address. Code should always use the CPU address, and we will or in VirtualMask\r
844 // to that address. \r
845 //\r
f659880b 846 Status = SetMemoryAttributes (Address, Length, EFI_MEMORY_WP, 0);\r
aeb61534
A
847 if (!EFI_ERROR (Status)) {\r
848 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_UC, VirtualMask);\r
849 }\r
850\r
6f72e28d 851 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "ConvertPagesToUncachedVirtualAddress()\n Unmapped 0x%08lx Mapped 0x%08lx 0x%x bytes\n", Address, Address | VirtualMask, Length));\r
852\r
aeb61534
A
853 return Status;\r
854}\r
855\r
856\r
857EFI_STATUS\r
858EFIAPI\r
6f72e28d 859CpuReconvertPages (\r
aeb61534
A
860 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
861 IN EFI_PHYSICAL_ADDRESS Address,\r
862 IN UINTN Length,\r
863 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
864 IN UINT64 Attributes\r
865 )\r
866{\r
867 EFI_STATUS Status;\r
6f72e28d 868\r
869 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "CpuReconvertPages(%lx, %x, %lx, %lx)\n", Address, Length, VirtualMask, Attributes));\r
870 \r
871 //\r
aeb61534
A
872 // Unmap the alaised Address\r
873 //\r
f659880b 874 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_WP, 0);\r
aeb61534
A
875 if (!EFI_ERROR (Status)) {\r
876 //\r
877 // Restore atttributes\r
878 //\r
879 Status = SetMemoryAttributes (Address, Length, Attributes, 0);\r
880 }\r
881 \r
882 return Status;\r
883}\r
884\r
885\r
886VIRTUAL_UNCACHED_PAGES_PROTOCOL gVirtualUncachedPages = {\r
887 CpuConvertPagesToUncachedVirtualAddress,\r
6f72e28d 888 CpuReconvertPages\r
aeb61534
A
889};\r
890\r
891\r
892\r
893\r