]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ArmPkg/Drivers/CpuDxe/Mmu.c
Missed a fix in the Cpu Driver. Added some more debug for Execption handling and...
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / Mmu.c
... / ...
CommitLineData
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
19\r
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
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
179 //*GcdAttributes |= EFI_MEMORY_WP | EFI_MEMORY_RP;\r
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
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
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
332\r
333\r
334EFI_STATUS\r
335SyncCacheConfig (\r
336 IN EFI_CPU_ARCH_PROTOCOL *CpuProtocol\r
337 )\r
338{\r
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
347 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
348 UINTN NumberOfDescriptors;\r
349 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
350\r
351\r
352 DEBUG ((EFI_D_PAGE, "SyncCacheConfig()\n"));\r
353\r
354 // This code assumes MMU is enabled and filed with section translations\r
355 ASSERT (ArmMmuEnabled ());\r
356\r
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
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
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
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
393 ASSERT_EFI_ERROR (Status);\r
394\r
395 // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)\r
396 SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, NextRegionBase, NextRegionLength, GcdAttributes);\r
397\r
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
441 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r
442 // EntryValue: values at bit positions specified by EntryMask\r
443 EntryMask = ARM_PAGE_DESC_TYPE_MASK;\r
444 EntryValue = ARM_PAGE_TYPE_SMALL;\r
445 // Although the PI spec is unclear on this the GCD guarantees that only\r
446 // one Attribute bit is set at a time, so we can safely use a switch statement\r
447 switch (Attributes) {\r
448 case EFI_MEMORY_UC:\r
449 // modify cacheability attributes\r
450 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
451 // map to strongly ordered\r
452 EntryValue |= 0; // TEX[2:0] = 0, C=0, B=0\r
453 break;\r
454\r
455 case EFI_MEMORY_WC:\r
456 // modify cacheability attributes\r
457 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
458 // map to normal non-cachable\r
459 EntryValue |= (0x1 << ARM_SMALL_PAGE_TEX_SHIFT); // TEX [2:0]= 001 = 0x2, B=0, C=0\r
460 break;\r
461\r
462 case EFI_MEMORY_WT:\r
463 // modify cacheability attributes\r
464 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
465 // write through with no-allocate\r
466 EntryValue |= ARM_PAGE_C; // TEX [2:0] = 0, C=1, B=0\r
467 break;\r
468\r
469 case EFI_MEMORY_WB:\r
470 // modify cacheability attributes\r
471 EntryMask |= ARM_SMALL_PAGE_TEX_MASK | ARM_PAGE_C | ARM_PAGE_B;\r
472 // write back (with allocate)\r
473 EntryValue |= (0x1 << ARM_SMALL_PAGE_TEX_SHIFT) | ARM_PAGE_C | ARM_PAGE_B; // TEX [2:0] = 001, C=1, B=1\r
474 break;\r
475\r
476 case EFI_MEMORY_WP:\r
477 case EFI_MEMORY_XP:\r
478 case EFI_MEMORY_UCE:\r
479 // cannot be implemented UEFI definition unclear for ARM\r
480 // Cause a page fault if these ranges are accessed.\r
481 EntryValue = ARM_PAGE_TYPE_FAULT;\r
482 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): setting page %lx with unsupported attribute %x will page fault on access\n", BaseAddress, Attributes));\r
483 break;\r
484\r
485 default:\r
486 return EFI_UNSUPPORTED;\r
487 }\r
488\r
489 // obtain page table base\r
490 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTranslationTableBaseAddress ();\r
491\r
492 // calculate number of 4KB page table entries to change\r
493 NumPageEntries = Length/EFI_PAGE_SIZE;\r
494 \r
495 // iterate for the number of 4KB pages to change\r
496 Offset = 0;\r
497 for(p=0; p<NumPageEntries; p++) {\r
498 // calculate index into first level translation table for page table value\r
499 \r
500 FirstLevelIdx = ((BaseAddress + Offset) & ARM_SECTION_BASE_MASK) >> ARM_SECTION_BASE_SHIFT;\r
501 ASSERT (FirstLevelIdx < FIRST_LEVEL_ENTRY_COUNT);\r
502\r
503 // read the descriptor from the first level page table\r
504 Descriptor = FirstLevelTable[FirstLevelIdx];\r
505\r
506 // does this descriptor need to be converted from section entry to 4K pages?\r
507 if ((Descriptor & ARM_DESC_TYPE_MASK) != ARM_DESC_TYPE_PAGE_TABLE ) {\r
508 Status = ConvertSectionToPages (FirstLevelIdx << ARM_SECTION_BASE_SHIFT);\r
509 if (EFI_ERROR(Status)) {\r
510 // exit for loop\r
511 break; \r
512 } \r
513 \r
514 // re-read descriptor\r
515 Descriptor = FirstLevelTable[FirstLevelIdx];\r
516 }\r
517\r
518 // obtain page table base address\r
519 PageTable = (ARM_PAGE_TABLE_ENTRY *)(Descriptor & ARM_SMALL_PAGE_BASE_MASK);\r
520\r
521 // calculate index into the page table\r
522 PageTableIndex = ((BaseAddress + Offset) & ARM_SMALL_PAGE_INDEX_MASK) >> ARM_SMALL_PAGE_BASE_SHIFT;\r
523 ASSERT (PageTableIndex < SMALL_PAGE_TABLE_ENTRY_COUNT);\r
524\r
525 // get the entry\r
526 PageTableEntry = PageTable[PageTableIndex];\r
527\r
528 // mask off appropriate fields\r
529 PageTableEntry &= ~EntryMask;\r
530\r
531 // mask in new attributes and/or permissions\r
532 PageTableEntry |= EntryValue;\r
533\r
534 if (VirtualMask != 0) {\r
535 // Make this virtual address point at a physical page\r
536 PageTableEntry &= ~VirtualMask;\r
537 }\r
538 \r
539 // update the entry\r
540 PageTable[PageTableIndex] = PageTableEntry; \r
541 \r
542\r
543 Status = EFI_SUCCESS;\r
544 Offset += EFI_PAGE_SIZE;\r
545 \r
546 } // end first level translation table loop\r
547\r
548 return Status;\r
549}\r
550\r
551\r
552\r
553EFI_STATUS\r
554UpdateSectionEntries (\r
555 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
556 IN UINT64 Length,\r
557 IN UINT64 Attributes,\r
558 IN EFI_PHYSICAL_ADDRESS VirtualMask\r
559 )\r
560{\r
561 EFI_STATUS Status = EFI_SUCCESS;\r
562 UINT32 EntryMask;\r
563 UINT32 EntryValue;\r
564 UINT32 FirstLevelIdx;\r
565 UINT32 NumSections;\r
566 UINT32 i;\r
567 UINT32 Descriptor;\r
568\r
569 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
570\r
571 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r
572 // EntryValue: values at bit positions specified by EntryMask\r
573\r
574 // Make sure we handle a section range that is unmapped \r
575 EntryMask = ARM_DESC_TYPE_MASK;\r
576 EntryValue = ARM_DESC_TYPE_SECTION;\r
577\r
578 // Although the PI spec is unclear on this the GCD guarantees that only\r
579 // one Attribute bit is set at a time, so we can safely use a switch statement\r
580 switch(Attributes) {\r
581 case EFI_MEMORY_UC:\r
582 // modify cacheability attributes\r
583 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
584 // map to strongly ordered\r
585 EntryValue |= 0; // TEX[2:0] = 0, C=0, B=0\r
586 break;\r
587\r
588 case EFI_MEMORY_WC:\r
589 // modify cacheability attributes\r
590 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
591 // map to normal non-cachable\r
592 EntryValue |= (0x1 << ARM_SECTION_TEX_SHIFT); // TEX [2:0]= 001 = 0x2, B=0, C=0\r
593 break;\r
594\r
595 case EFI_MEMORY_WT:\r
596 // modify cacheability attributes\r
597 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
598 // write through with no-allocate\r
599 EntryValue |= ARM_SECTION_C; // TEX [2:0] = 0, C=1, B=0\r
600 break;\r
601\r
602 case EFI_MEMORY_WB:\r
603 // modify cacheability attributes\r
604 EntryMask |= ARM_SECTION_TEX_MASK | ARM_SECTION_C | ARM_SECTION_B;\r
605 // write back (with allocate)\r
606 EntryValue |= (0x1 << ARM_SECTION_TEX_SHIFT) | ARM_SECTION_C | ARM_SECTION_B; // TEX [2:0] = 001, C=1, B=1\r
607 break;\r
608\r
609 case EFI_MEMORY_WP:\r
610 case EFI_MEMORY_XP:\r
611 case EFI_MEMORY_RP:\r
612 case EFI_MEMORY_UCE:\r
613 // cannot be implemented UEFI definition unclear for ARM\r
614 // Cause a page fault if these ranges are accessed.\r
615 EntryValue = ARM_DESC_TYPE_FAULT;\r
616 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): setting section %lx with unsupported attribute %x will page fault on access\n", BaseAddress, Attributes));\r
617 break;\r
618\r
619\r
620 default:\r
621 return EFI_UNSUPPORTED;\r
622 }\r
623\r
624 // obtain page table base\r
625 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTranslationTableBaseAddress ();\r
626\r
627 // calculate index into first level translation table for start of modification\r
628 FirstLevelIdx = (BaseAddress & ARM_SECTION_BASE_MASK) >> ARM_SECTION_BASE_SHIFT;\r
629 ASSERT (FirstLevelIdx < FIRST_LEVEL_ENTRY_COUNT);\r
630\r
631 // calculate number of 1MB first level entries this applies to\r
632 NumSections = Length / ARM_PAGE_DESC_ENTRY_MVA_SIZE;\r
633 \r
634 // iterate through each descriptor\r
635 for(i=0; i<NumSections; i++) {\r
636 Descriptor = FirstLevelTable[FirstLevelIdx + i];\r
637\r
638 // has this descriptor already been coverted to pages?\r
639 if ((Descriptor & ARM_DESC_TYPE_MASK) != ARM_DESC_TYPE_PAGE_TABLE ) {\r
640 // forward this 1MB range to page table function instead\r
641 Status = UpdatePageEntries ((FirstLevelIdx + i) << ARM_SECTION_BASE_SHIFT, ARM_PAGE_DESC_ENTRY_MVA_SIZE, Attributes, VirtualMask);\r
642 } else {\r
643 // still a section entry\r
644 \r
645 // mask off appropriate fields\r
646 Descriptor &= ~EntryMask;\r
647\r
648 // mask in new attributes and/or permissions\r
649 Descriptor |= EntryValue;\r
650 if (VirtualMask != 0) {\r
651 Descriptor &= ~VirtualMask;\r
652 }\r
653\r
654 FirstLevelTable[FirstLevelIdx + i] = Descriptor;\r
655\r
656 Status = EFI_SUCCESS;\r
657 }\r
658 }\r
659\r
660 return Status;\r
661}\r
662\r
663EFI_STATUS \r
664ConvertSectionToPages (\r
665 IN EFI_PHYSICAL_ADDRESS BaseAddress\r
666 )\r
667{\r
668 EFI_STATUS Status;\r
669 EFI_PHYSICAL_ADDRESS PageTableAddr;\r
670 UINT32 FirstLevelIdx;\r
671 UINT32 SectionDescriptor;\r
672 UINT32 PageTableDescriptor;\r
673 UINT32 PageDescriptor;\r
674 UINT32 i;\r
675\r
676 volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r
677 volatile ARM_PAGE_TABLE_ENTRY *PageTable;\r
678\r
679 DEBUG ((EFI_D_PAGE, "Converting section at 0x%x to pages\n", (UINTN)BaseAddress));\r
680\r
681 // obtain page table base\r
682 FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTranslationTableBaseAddress ();\r
683\r
684 // calculate index into first level translation table for start of modification\r
685 FirstLevelIdx = (BaseAddress & ARM_SECTION_BASE_MASK) >> ARM_SECTION_BASE_SHIFT;\r
686 ASSERT (FirstLevelIdx < FIRST_LEVEL_ENTRY_COUNT);\r
687\r
688 // get section attributes and convert to page attributes\r
689 SectionDescriptor = FirstLevelTable[FirstLevelIdx];\r
690 PageDescriptor = ARM_PAGE_TYPE_SMALL;\r
691 PageDescriptor |= ((SectionDescriptor & ARM_SECTION_TEX_MASK) >> ARM_SECTION_TEX_SHIFT) << ARM_SMALL_PAGE_TEX_SHIFT;\r
692 if ((SectionDescriptor & ARM_SECTION_B) != 0) {\r
693 PageDescriptor |= ARM_PAGE_B;\r
694 }\r
695 if ((SectionDescriptor & ARM_SECTION_C) != 0) {\r
696 PageDescriptor |= ARM_PAGE_C;\r
697 }\r
698 PageDescriptor |= ((SectionDescriptor & ARM_SECTION_AP10_MASK) >> ARM_SECTION_AP10_SHIFT) << ARM_PAGE_AP10_SHIFT;\r
699 if ((SectionDescriptor & ARM_SECTION_AP2) != 0) {\r
700 PageDescriptor |= ARM_PAGE_AP2;\r
701 }\r
702 if ((SectionDescriptor & ARM_SECTION_XN) != 0) {\r
703 PageDescriptor |= ARM_PAGE_TYPE_SMALL_XN;\r
704 }\r
705 if ((SectionDescriptor & ARM_SECTION_nG) != 0) {\r
706 PageDescriptor |= ARM_PAGE_nG;\r
707 }\r
708 if ((SectionDescriptor & ARM_SECTION_S) != 0) {\r
709 PageDescriptor |= ARM_PAGE_S;\r
710 }\r
711\r
712 // allocate a page table for the 4KB entries (we use up a full page even though we only need 1KB)\r
713 Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, 1, &PageTableAddr);\r
714 if (EFI_ERROR(Status)) {\r
715 return Status;\r
716 }\r
717\r
718 PageTable = (volatile ARM_PAGE_TABLE_ENTRY *)(UINTN)PageTableAddr;\r
719\r
720 // write the page table entries out\r
721 for (i=0; i<(ARM_PAGE_DESC_ENTRY_MVA_SIZE/EFI_PAGE_SIZE); i++) {\r
722 PageTable[i] = ((BaseAddress + (i << 12)) & ARM_SMALL_PAGE_BASE_MASK) | PageDescriptor;\r
723 }\r
724\r
725 // flush d-cache so descriptors make it back to uncached memory for subsequent table walks\r
726 InvalidateDataCacheRange ((VOID *)(UINTN)PageTableAddr, EFI_PAGE_SIZE);\r
727\r
728 // formulate page table entry, Domain=0, NS=0\r
729 PageTableDescriptor = (((UINTN)PageTableAddr) & ARM_PAGE_DESC_BASE_MASK) | ARM_DESC_TYPE_PAGE_TABLE;\r
730\r
731 // write the page table entry out, repalcing section entry\r
732 FirstLevelTable[FirstLevelIdx] = PageTableDescriptor;\r
733\r
734 return EFI_SUCCESS;\r
735}\r
736\r
737\r
738\r
739EFI_STATUS\r
740SetMemoryAttributes (\r
741 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
742 IN UINT64 Length,\r
743 IN UINT64 Attributes,\r
744 IN EFI_PHYSICAL_ADDRESS VirtualMask\r
745 )\r
746{\r
747 EFI_STATUS Status;\r
748 \r
749 if(((BaseAddress & 0xFFFFF) == 0) && ((Length & 0xFFFFF) == 0)) {\r
750 // is the base and length a multiple of 1 MB?\r
751 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): MMU section 0x%x length 0x%x to %lx\n", (UINTN)BaseAddress, (UINTN)Length, Attributes));\r
752 Status = UpdateSectionEntries (BaseAddress, Length, Attributes, VirtualMask);\r
753 } else {\r
754 // base and/or length is not a multiple of 1 MB\r
755 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): MMU page 0x%x length 0x%x to %lx\n", (UINTN)BaseAddress, (UINTN)Length, Attributes));\r
756 Status = UpdatePageEntries (BaseAddress, Length, Attributes, VirtualMask);\r
757 }\r
758\r
759 // flush d-cache so descriptors make it back to uncached memory for subsequent table walks\r
760 // flush and invalidate pages\r
761 ArmCleanInvalidateDataCache ();\r
762 \r
763 ArmInvalidateInstructionCache ();\r
764\r
765 // invalidate all TLB entries so changes are synced\r
766 ArmInvalidateTlb (); \r
767\r
768 return Status;\r
769}\r
770\r
771\r
772/**\r
773 This function modifies the attributes for the memory region specified by BaseAddress and\r
774 Length from their current attributes to the attributes specified by Attributes.\r
775\r
776 @param This The EFI_CPU_ARCH_PROTOCOL instance.\r
777 @param BaseAddress The physical address that is the start address of a memory region.\r
778 @param Length The size in bytes of the memory region.\r
779 @param Attributes The bit mask of attributes to set for the memory region.\r
780\r
781 @retval EFI_SUCCESS The attributes were set for the memory region.\r
782 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
783 BaseAddress and Length cannot be modified.\r
784 @retval EFI_INVALID_PARAMETER Length is zero.\r
785 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
786 the memory resource range.\r
787 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
788 resource range specified by BaseAddress and Length.\r
789 The bit mask of attributes is not support for the memory resource\r
790 range specified by BaseAddress and Length.\r
791\r
792**/\r
793EFI_STATUS\r
794EFIAPI\r
795CpuSetMemoryAttributes (\r
796 IN EFI_CPU_ARCH_PROTOCOL *This,\r
797 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
798 IN UINT64 Length,\r
799 IN UINT64 Attributes\r
800 )\r
801{\r
802 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(%lx, %lx, %lx)\n", BaseAddress, Length, Attributes));\r
803 if ( ((BaseAddress & (EFI_PAGE_SIZE-1)) != 0) || ((Length & (EFI_PAGE_SIZE-1)) != 0)){\r
804 // minimum granularity is EFI_PAGE_SIZE (4KB on ARM)\r
805 DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(%lx, %lx, %lx): minimum ganularity is EFI_PAGE_SIZE\n", BaseAddress, Length, Attributes));\r
806 return EFI_UNSUPPORTED;\r
807 }\r
808 \r
809 return SetMemoryAttributes (BaseAddress, Length, Attributes, 0);\r
810}\r
811\r
812\r
813\r
814//\r
815// Add a new protocol to support \r
816//\r
817\r
818EFI_STATUS\r
819EFIAPI\r
820CpuConvertPagesToUncachedVirtualAddress (\r
821 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
822 IN EFI_PHYSICAL_ADDRESS Address,\r
823 IN UINTN Length,\r
824 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
825 OUT UINT64 *Attributes OPTIONAL\r
826 )\r
827{\r
828 EFI_STATUS Status;\r
829 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
830 \r
831 \r
832 if (Attributes != NULL) {\r
833 Status = gDS->GetMemorySpaceDescriptor (Address, &GcdDescriptor);\r
834 if (!EFI_ERROR (Status)) {\r
835 *Attributes = GcdDescriptor.Attributes;\r
836 }\r
837 }\r
838\r
839 //\r
840 // Make this address range page fault if accessed. If it is a DMA buffer than this would \r
841 // be the PCI address. Code should always use the CPU address, and we will or in VirtualMask\r
842 // to that address. \r
843 //\r
844 Status = SetMemoryAttributes (Address, Length, EFI_MEMORY_WP, 0);\r
845 if (!EFI_ERROR (Status)) {\r
846 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_UC, VirtualMask);\r
847 }\r
848\r
849 return Status;\r
850}\r
851\r
852\r
853EFI_STATUS\r
854EFIAPI\r
855CpuReconvertPagesPages (\r
856 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
857 IN EFI_PHYSICAL_ADDRESS Address,\r
858 IN UINTN Length,\r
859 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
860 IN UINT64 Attributes\r
861 )\r
862{\r
863 EFI_STATUS Status;\r
864DEBUG ((EFI_D_ERROR, "CpuReconvertPagesPages(%lx, %x, %lx, %lx)\n", Address, Length, VirtualMask, Attributes));\r
865ASSERT (FALSE);\r
866//\r
867 // Unmap the alaised Address\r
868 //\r
869 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_WP, 0);\r
870 if (!EFI_ERROR (Status)) {\r
871 //\r
872 // Restore atttributes\r
873 //\r
874 Status = SetMemoryAttributes (Address, Length, Attributes, 0);\r
875 }\r
876 \r
877 return Status;\r
878}\r
879\r
880\r
881VIRTUAL_UNCACHED_PAGES_PROTOCOL gVirtualUncachedPages = {\r
882 CpuConvertPagesToUncachedVirtualAddress,\r
883 CpuReconvertPagesPages\r
884};\r
885\r
886\r
887\r
888\r