]>
Commit | Line | Data |
---|---|---|
1 | /*++\r | |
2 | \r | |
3 | Copyright (c) 2009, Hewlett-Packard Company. All rights reserved.<BR>\r | |
4 | Portions copyright (c) 2010, Apple Inc. All rights reserved.<BR>\r | |
5 | \r | |
6 | This program and the accompanying materials \r | |
7 | are licensed and made available under the terms and conditions of the BSD License \r | |
8 | which accompanies this distribution. The full text of the license may be found at \r | |
9 | http://opensource.org/licenses/bsd-license.php \r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
13 | \r | |
14 | \r | |
15 | --*/\r | |
16 | \r | |
17 | #include "CpuDxe.h"\r | |
18 | \r | |
19 | // First Level Descriptors\r | |
20 | typedef UINT32 ARM_FIRST_LEVEL_DESCRIPTOR;\r | |
21 | \r | |
22 | // Second Level Descriptors\r | |
23 | typedef UINT32 ARM_PAGE_TABLE_ENTRY;\r | |
24 | \r | |
25 | EFI_STATUS \r | |
26 | SectionToGcdAttributes (\r | |
27 | IN UINT32 SectionAttributes,\r | |
28 | OUT UINT64 *GcdAttributes\r | |
29 | )\r | |
30 | {\r | |
31 | *GcdAttributes = 0;\r | |
32 | \r | |
33 | // determine cacheability attributes\r | |
34 | switch(SectionAttributes & TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK) {\r | |
35 | case TT_DESCRIPTOR_SECTION_CACHE_POLICY_STRONGLY_ORDERED:\r | |
36 | *GcdAttributes |= EFI_MEMORY_UC;\r | |
37 | break;\r | |
38 | case TT_DESCRIPTOR_SECTION_CACHE_POLICY_SHAREABLE_DEVICE:\r | |
39 | *GcdAttributes |= EFI_MEMORY_UC;\r | |
40 | break;\r | |
41 | case TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC:\r | |
42 | *GcdAttributes |= EFI_MEMORY_WT;\r | |
43 | break;\r | |
44 | case TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_NO_ALLOC:\r | |
45 | *GcdAttributes |= EFI_MEMORY_WB;\r | |
46 | break;\r | |
47 | case TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE:\r | |
48 | *GcdAttributes |= EFI_MEMORY_WC;\r | |
49 | break;\r | |
50 | case TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC:\r | |
51 | *GcdAttributes |= EFI_MEMORY_WB;\r | |
52 | break;\r | |
53 | case TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_SHAREABLE_DEVICE:\r | |
54 | *GcdAttributes |= EFI_MEMORY_UC;\r | |
55 | break;\r | |
56 | default:\r | |
57 | return EFI_UNSUPPORTED;\r | |
58 | }\r | |
59 | \r | |
60 | // determine protection attributes\r | |
61 | switch(SectionAttributes & TT_DESCRIPTOR_SECTION_AP_MASK) {\r | |
62 | case TT_DESCRIPTOR_SECTION_AP_NO_NO: // no read, no write\r | |
63 | //*GcdAttributes |= EFI_MEMORY_WP | EFI_MEMORY_RP;\r | |
64 | break;\r | |
65 | \r | |
66 | case TT_DESCRIPTOR_SECTION_AP_RW_NO:\r | |
67 | case TT_DESCRIPTOR_SECTION_AP_RW_RW:\r | |
68 | // normal read/write access, do not add additional attributes\r | |
69 | break;\r | |
70 | \r | |
71 | // read only cases map to write-protect\r | |
72 | case TT_DESCRIPTOR_SECTION_AP_RO_NO:\r | |
73 | case TT_DESCRIPTOR_SECTION_AP_RO_RO:\r | |
74 | *GcdAttributes |= EFI_MEMORY_WP;\r | |
75 | break;\r | |
76 | \r | |
77 | default:\r | |
78 | return EFI_UNSUPPORTED;\r | |
79 | }\r | |
80 | \r | |
81 | // now process eXectue Never attribute\r | |
82 | if ((SectionAttributes & TT_DESCRIPTOR_SECTION_XN_MASK) != 0 ) {\r | |
83 | *GcdAttributes |= EFI_MEMORY_XP;\r | |
84 | }\r | |
85 | \r | |
86 | return EFI_SUCCESS;\r | |
87 | }\r | |
88 | \r | |
89 | EFI_STATUS\r | |
90 | PageToGcdAttributes (\r | |
91 | IN UINT32 PageAttributes,\r | |
92 | OUT UINT64 *GcdAttributes\r | |
93 | )\r | |
94 | {\r | |
95 | *GcdAttributes = 0;\r | |
96 | \r | |
97 | // determine cacheability attributes\r | |
98 | switch(PageAttributes & TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK) {\r | |
99 | case TT_DESCRIPTOR_PAGE_CACHE_POLICY_STRONGLY_ORDERED:\r | |
100 | *GcdAttributes |= EFI_MEMORY_UC;\r | |
101 | break;\r | |
102 | case TT_DESCRIPTOR_PAGE_CACHE_POLICY_SHAREABLE_DEVICE:\r | |
103 | *GcdAttributes |= EFI_MEMORY_UC;\r | |
104 | break;\r | |
105 | case TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC:\r | |
106 | *GcdAttributes |= EFI_MEMORY_WT;\r | |
107 | break;\r | |
108 | case TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_NO_ALLOC:\r | |
109 | *GcdAttributes |= EFI_MEMORY_WB;\r | |
110 | break;\r | |
111 | case TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE:\r | |
112 | *GcdAttributes |= EFI_MEMORY_WC;\r | |
113 | break;\r | |
114 | case TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_ALLOC:\r | |
115 | *GcdAttributes |= EFI_MEMORY_WB;\r | |
116 | break;\r | |
117 | case TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_SHAREABLE_DEVICE:\r | |
118 | *GcdAttributes |= EFI_MEMORY_UC;\r | |
119 | break;\r | |
120 | default:\r | |
121 | return EFI_UNSUPPORTED;\r | |
122 | }\r | |
123 | \r | |
124 | // determine protection attributes\r | |
125 | switch(PageAttributes & TT_DESCRIPTOR_PAGE_AP_MASK) {\r | |
126 | case TT_DESCRIPTOR_PAGE_AP_NO_NO: // no read, no write\r | |
127 | //*GcdAttributes |= EFI_MEMORY_WP | EFI_MEMORY_RP;\r | |
128 | break;\r | |
129 | \r | |
130 | case TT_DESCRIPTOR_PAGE_AP_RW_NO:\r | |
131 | case TT_DESCRIPTOR_PAGE_AP_RW_RW:\r | |
132 | // normal read/write access, do not add additional attributes\r | |
133 | break;\r | |
134 | \r | |
135 | // read only cases map to write-protect\r | |
136 | case TT_DESCRIPTOR_PAGE_AP_RO_NO:\r | |
137 | case TT_DESCRIPTOR_PAGE_AP_RO_RO:\r | |
138 | *GcdAttributes |= EFI_MEMORY_WP;\r | |
139 | break;\r | |
140 | \r | |
141 | default:\r | |
142 | return EFI_UNSUPPORTED;\r | |
143 | }\r | |
144 | \r | |
145 | // now process eXectue Never attribute\r | |
146 | if ((PageAttributes & TT_DESCRIPTOR_PAGE_XN_MASK) != 0 ) {\r | |
147 | *GcdAttributes |= EFI_MEMORY_XP;\r | |
148 | }\r | |
149 | \r | |
150 | return EFI_SUCCESS;\r | |
151 | }\r | |
152 | \r | |
153 | EFI_STATUS\r | |
154 | SyncCacheConfigPage (\r | |
155 | IN UINT32 SectionIndex,\r | |
156 | IN UINT32 FirstLevelDescriptor,\r | |
157 | IN UINTN NumberOfDescriptors,\r | |
158 | IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r | |
159 | IN OUT EFI_PHYSICAL_ADDRESS *NextRegionBase,\r | |
160 | IN OUT UINT64 *NextRegionLength,\r | |
161 | IN OUT UINT32 *NextSectionAttributes\r | |
162 | )\r | |
163 | {\r | |
164 | EFI_STATUS Status;\r | |
165 | UINT32 i;\r | |
166 | volatile ARM_PAGE_TABLE_ENTRY *SecondLevelTable;\r | |
167 | UINT32 NextPageAttributes = 0;\r | |
168 | UINT32 PageAttributes = 0;\r | |
169 | UINT32 BaseAddress;\r | |
170 | UINT64 GcdAttributes;\r | |
171 | \r | |
172 | // Get the Base Address from FirstLevelDescriptor;\r | |
173 | BaseAddress = TT_DESCRIPTOR_PAGE_BASE_ADDRESS(SectionIndex << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r | |
174 | \r | |
175 | // Convert SectionAttributes into PageAttributes\r | |
176 | NextPageAttributes =\r | |
177 | TT_DESCRIPTOR_CONVERT_TO_PAGE_CACHE_POLICY(*NextSectionAttributes,0) |\r | |
178 | TT_DESCRIPTOR_CONVERT_TO_PAGE_AP(*NextSectionAttributes);\r | |
179 | \r | |
180 | // obtain page table base\r | |
181 | SecondLevelTable = (ARM_PAGE_TABLE_ENTRY *)(FirstLevelDescriptor & TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK);\r | |
182 | \r | |
183 | for (i=0; i < TRANSLATION_TABLE_PAGE_COUNT; i++) {\r | |
184 | if ((SecondLevelTable[i] & TT_DESCRIPTOR_PAGE_TYPE_MASK) == TT_DESCRIPTOR_PAGE_TYPE_PAGE) {\r | |
185 | // extract attributes (cacheability and permissions)\r | |
186 | PageAttributes = SecondLevelTable[i] & (TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK | TT_DESCRIPTOR_PAGE_AP_MASK);\r | |
187 | \r | |
188 | if (NextPageAttributes == 0) {\r | |
189 | // start on a new region\r | |
190 | *NextRegionLength = 0;\r | |
191 | *NextRegionBase = BaseAddress | (i << TT_DESCRIPTOR_PAGE_BASE_SHIFT);\r | |
192 | NextPageAttributes = PageAttributes;\r | |
193 | } else if (PageAttributes != NextPageAttributes) {\r | |
194 | // Convert Section Attributes into GCD Attributes\r | |
195 | Status = PageToGcdAttributes (NextPageAttributes, &GcdAttributes);\r | |
196 | ASSERT_EFI_ERROR (Status);\r | |
197 | \r | |
198 | // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)\r | |
199 | SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, *NextRegionBase, *NextRegionLength, GcdAttributes);\r | |
200 | \r | |
201 | // start on a new region\r | |
202 | *NextRegionLength = 0;\r | |
203 | *NextRegionBase = BaseAddress | (i << TT_DESCRIPTOR_PAGE_BASE_SHIFT);\r | |
204 | NextPageAttributes = PageAttributes;\r | |
205 | }\r | |
206 | } else if (NextPageAttributes != 0) {\r | |
207 | // Convert Page Attributes into GCD Attributes\r | |
208 | Status = PageToGcdAttributes (NextPageAttributes, &GcdAttributes);\r | |
209 | ASSERT_EFI_ERROR (Status);\r | |
210 | \r | |
211 | // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)\r | |
212 | SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, *NextRegionBase, *NextRegionLength, GcdAttributes);\r | |
213 | \r | |
214 | *NextRegionLength = 0;\r | |
215 | *NextRegionBase = BaseAddress | (i << TT_DESCRIPTOR_PAGE_BASE_SHIFT);\r | |
216 | NextPageAttributes = 0;\r | |
217 | }\r | |
218 | *NextRegionLength += TT_DESCRIPTOR_PAGE_SIZE;\r | |
219 | }\r | |
220 | \r | |
221 | // Convert back PageAttributes into SectionAttributes\r | |
222 | *NextSectionAttributes =\r | |
223 | TT_DESCRIPTOR_CONVERT_TO_SECTION_CACHE_POLICY(NextPageAttributes,0) |\r | |
224 | TT_DESCRIPTOR_CONVERT_TO_SECTION_AP(NextPageAttributes);\r | |
225 | \r | |
226 | return EFI_SUCCESS;\r | |
227 | }\r | |
228 | \r | |
229 | EFI_STATUS\r | |
230 | SyncCacheConfig (\r | |
231 | IN EFI_CPU_ARCH_PROTOCOL *CpuProtocol\r | |
232 | )\r | |
233 | {\r | |
234 | EFI_STATUS Status;\r | |
235 | UINT32 i;\r | |
236 | EFI_PHYSICAL_ADDRESS NextRegionBase;\r | |
237 | UINT64 NextRegionLength;\r | |
238 | UINT32 NextSectionAttributes = 0;\r | |
239 | UINT32 SectionAttributes = 0;\r | |
240 | UINT64 GcdAttributes;\r | |
241 | volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r | |
242 | UINTN NumberOfDescriptors;\r | |
243 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r | |
244 | \r | |
245 | \r | |
246 | DEBUG ((EFI_D_PAGE, "SyncCacheConfig()\n"));\r | |
247 | \r | |
248 | // This code assumes MMU is enabled and filed with section translations\r | |
249 | ASSERT (ArmMmuEnabled ());\r | |
250 | \r | |
251 | //\r | |
252 | // Get the memory space map from GCD\r | |
253 | //\r | |
254 | MemorySpaceMap = NULL;\r | |
255 | Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r | |
256 | ASSERT_EFI_ERROR (Status);\r | |
257 | \r | |
258 | \r | |
259 | // The GCD implementation maintains its own copy of the state of memory space attributes. GCD needs\r | |
260 | // to know what the initial memory space attributes are. The CPU Arch. Protocol does not provide a\r | |
261 | // GetMemoryAttributes function for GCD to get this so we must resort to calling GCD (as if we were\r | |
262 | // a client) to update its copy of the attributes. This is bad architecture and should be replaced\r | |
263 | // with a way for GCD to query the CPU Arch. driver of the existing memory space attributes instead.\r | |
264 | \r | |
265 | // obtain page table base\r | |
266 | FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)(ArmGetTTBR0BaseAddress ());\r | |
267 | \r | |
268 | // Get the first region\r | |
269 | NextSectionAttributes = FirstLevelTable[0] & (TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK | TT_DESCRIPTOR_SECTION_AP_MASK);\r | |
270 | \r | |
271 | // iterate through each 1MB descriptor\r | |
272 | NextRegionBase = NextRegionLength = 0;\r | |
273 | for (i=0; i < TRANSLATION_TABLE_SECTION_COUNT; i++) {\r | |
274 | if ((FirstLevelTable[i] & TT_DESCRIPTOR_SECTION_TYPE_MASK) == TT_DESCRIPTOR_SECTION_TYPE_SECTION) {\r | |
275 | // extract attributes (cacheability and permissions)\r | |
276 | SectionAttributes = FirstLevelTable[i] & (TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK | TT_DESCRIPTOR_SECTION_AP_MASK);\r | |
277 | \r | |
278 | if (NextSectionAttributes == 0) {\r | |
279 | // start on a new region\r | |
280 | NextRegionLength = 0;\r | |
281 | NextRegionBase = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(i << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r | |
282 | NextSectionAttributes = SectionAttributes;\r | |
283 | } else if (SectionAttributes != NextSectionAttributes) {\r | |
284 | // Convert Section Attributes into GCD Attributes\r | |
285 | Status = SectionToGcdAttributes (NextSectionAttributes, &GcdAttributes);\r | |
286 | ASSERT_EFI_ERROR (Status);\r | |
287 | \r | |
288 | // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)\r | |
289 | SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, NextRegionBase, NextRegionLength, GcdAttributes);\r | |
290 | \r | |
291 | // start on a new region\r | |
292 | NextRegionLength = 0;\r | |
293 | NextRegionBase = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(i << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r | |
294 | NextSectionAttributes = SectionAttributes;\r | |
295 | }\r | |
296 | NextRegionLength += TT_DESCRIPTOR_SECTION_SIZE;\r | |
297 | } else if (TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(FirstLevelTable[i])) {\r | |
298 | Status = SyncCacheConfigPage (\r | |
299 | i,FirstLevelTable[i],\r | |
300 | NumberOfDescriptors, MemorySpaceMap,\r | |
301 | &NextRegionBase,&NextRegionLength,&NextSectionAttributes);\r | |
302 | ASSERT_EFI_ERROR (Status);\r | |
303 | } else {\r | |
304 | // We do not support yet 16MB sections\r | |
305 | ASSERT ((FirstLevelTable[i] & TT_DESCRIPTOR_SECTION_TYPE_MASK) != TT_DESCRIPTOR_SECTION_TYPE_SUPERSECTION);\r | |
306 | \r | |
307 | // start on a new region\r | |
308 | if (NextSectionAttributes != 0) {\r | |
309 | // Convert Section Attributes into GCD Attributes\r | |
310 | Status = SectionToGcdAttributes (NextSectionAttributes, &GcdAttributes);\r | |
311 | ASSERT_EFI_ERROR (Status);\r | |
312 | \r | |
313 | // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)\r | |
314 | SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, NextRegionBase, NextRegionLength, GcdAttributes);\r | |
315 | \r | |
316 | NextRegionLength = 0;\r | |
317 | NextRegionBase = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(i << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r | |
318 | NextSectionAttributes = 0;\r | |
319 | }\r | |
320 | NextRegionLength += TT_DESCRIPTOR_SECTION_SIZE;\r | |
321 | }\r | |
322 | } // section entry loop\r | |
323 | \r | |
324 | if (NextSectionAttributes != 0) {\r | |
325 | // Convert Section Attributes into GCD Attributes\r | |
326 | Status = SectionToGcdAttributes (NextSectionAttributes, &GcdAttributes);\r | |
327 | ASSERT_EFI_ERROR (Status);\r | |
328 | \r | |
329 | // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)\r | |
330 | SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, NextRegionBase, NextRegionLength, GcdAttributes);\r | |
331 | }\r | |
332 | \r | |
333 | return EFI_SUCCESS;\r | |
334 | }\r | |
335 | \r | |
336 | \r | |
337 | \r | |
338 | EFI_STATUS\r | |
339 | UpdatePageEntries (\r | |
340 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
341 | IN UINT64 Length,\r | |
342 | IN UINT64 Attributes,\r | |
343 | IN EFI_PHYSICAL_ADDRESS VirtualMask\r | |
344 | )\r | |
345 | {\r | |
346 | EFI_STATUS Status;\r | |
347 | UINT32 EntryValue;\r | |
348 | UINT32 EntryMask;\r | |
349 | UINT32 FirstLevelIdx;\r | |
350 | UINT32 Offset;\r | |
351 | UINT32 NumPageEntries;\r | |
352 | UINT32 Descriptor;\r | |
353 | UINT32 p;\r | |
354 | UINT32 PageTableIndex;\r | |
355 | UINT32 PageTableEntry;\r | |
356 | UINT32 CurrentPageTableEntry;\r | |
357 | VOID *Mva;\r | |
358 | \r | |
359 | volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r | |
360 | volatile ARM_PAGE_TABLE_ENTRY *PageTable;\r | |
361 | \r | |
362 | Status = EFI_SUCCESS;\r | |
363 | \r | |
364 | // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r | |
365 | // EntryValue: values at bit positions specified by EntryMask\r | |
366 | EntryMask = TT_DESCRIPTOR_PAGE_TYPE_MASK;\r | |
367 | EntryValue = TT_DESCRIPTOR_PAGE_TYPE_PAGE;\r | |
368 | // Although the PI spec is unclear on this the GCD guarantees that only\r | |
369 | // one Attribute bit is set at a time, so we can safely use a switch statement\r | |
370 | switch (Attributes) {\r | |
371 | case EFI_MEMORY_UC:\r | |
372 | // modify cacheability attributes\r | |
373 | EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r | |
374 | // map to strongly ordered\r | |
375 | EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_STRONGLY_ORDERED; // TEX[2:0] = 0, C=0, B=0\r | |
376 | break;\r | |
377 | \r | |
378 | case EFI_MEMORY_WC:\r | |
379 | // modify cacheability attributes\r | |
380 | EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r | |
381 | // map to normal non-cachable\r | |
382 | EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE; // TEX [2:0]= 001 = 0x2, B=0, C=0\r | |
383 | break;\r | |
384 | \r | |
385 | case EFI_MEMORY_WT:\r | |
386 | // modify cacheability attributes\r | |
387 | EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r | |
388 | // write through with no-allocate\r | |
389 | EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC; // TEX [2:0] = 0, C=1, B=0\r | |
390 | break;\r | |
391 | \r | |
392 | case EFI_MEMORY_WB:\r | |
393 | // modify cacheability attributes\r | |
394 | EntryMask |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK;\r | |
395 | // write back (with allocate)\r | |
396 | EntryValue |= TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_ALLOC; // TEX [2:0] = 001, C=1, B=1\r | |
397 | break;\r | |
398 | \r | |
399 | case EFI_MEMORY_WP:\r | |
400 | case EFI_MEMORY_XP:\r | |
401 | case EFI_MEMORY_UCE:\r | |
402 | // cannot be implemented UEFI definition unclear for ARM\r | |
403 | // Cause a page fault if these ranges are accessed.\r | |
404 | EntryValue = TT_DESCRIPTOR_PAGE_TYPE_FAULT;\r | |
405 | DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): setting page %lx with unsupported attribute %x will page fault on access\n", BaseAddress, Attributes));\r | |
406 | break;\r | |
407 | \r | |
408 | default:\r | |
409 | return EFI_UNSUPPORTED;\r | |
410 | }\r | |
411 | \r | |
412 | // Obtain page table base\r | |
413 | FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTTBR0BaseAddress ();\r | |
414 | \r | |
415 | // Calculate number of 4KB page table entries to change\r | |
416 | NumPageEntries = Length / TT_DESCRIPTOR_PAGE_SIZE;\r | |
417 | \r | |
418 | // Iterate for the number of 4KB pages to change\r | |
419 | Offset = 0;\r | |
420 | for(p = 0; p < NumPageEntries; p++) {\r | |
421 | // Calculate index into first level translation table for page table value\r | |
422 | \r | |
423 | FirstLevelIdx = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress + Offset) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT;\r | |
424 | ASSERT (FirstLevelIdx < TRANSLATION_TABLE_SECTION_COUNT);\r | |
425 | \r | |
426 | // Read the descriptor from the first level page table\r | |
427 | Descriptor = FirstLevelTable[FirstLevelIdx];\r | |
428 | \r | |
429 | // Does this descriptor need to be converted from section entry to 4K pages?\r | |
430 | if (!TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(Descriptor)) {\r | |
431 | Status = ConvertSectionToPages (FirstLevelIdx << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r | |
432 | if (EFI_ERROR(Status)) {\r | |
433 | // Exit for loop\r | |
434 | break; \r | |
435 | } \r | |
436 | \r | |
437 | // Re-read descriptor\r | |
438 | Descriptor = FirstLevelTable[FirstLevelIdx];\r | |
439 | }\r | |
440 | \r | |
441 | // Obtain page table base address\r | |
442 | PageTable = (ARM_PAGE_TABLE_ENTRY *)TT_DESCRIPTOR_PAGE_BASE_ADDRESS(Descriptor);\r | |
443 | \r | |
444 | // Calculate index into the page table\r | |
445 | PageTableIndex = ((BaseAddress + Offset) & TT_DESCRIPTOR_PAGE_INDEX_MASK) >> TT_DESCRIPTOR_PAGE_BASE_SHIFT;\r | |
446 | ASSERT (PageTableIndex < TRANSLATION_TABLE_PAGE_COUNT);\r | |
447 | \r | |
448 | // Get the entry\r | |
449 | CurrentPageTableEntry = PageTable[PageTableIndex];\r | |
450 | \r | |
451 | // Mask off appropriate fields\r | |
452 | PageTableEntry = CurrentPageTableEntry & ~EntryMask;\r | |
453 | \r | |
454 | // Mask in new attributes and/or permissions\r | |
455 | PageTableEntry |= EntryValue;\r | |
456 | \r | |
457 | if (VirtualMask != 0) {\r | |
458 | // Make this virtual address point at a physical page\r | |
459 | PageTableEntry &= ~VirtualMask;\r | |
460 | }\r | |
461 | \r | |
462 | if (CurrentPageTableEntry != PageTableEntry) {\r | |
463 | Mva = (VOID *)(UINTN)((((UINTN)FirstLevelIdx) << TT_DESCRIPTOR_SECTION_BASE_SHIFT) + (PageTableIndex << TT_DESCRIPTOR_PAGE_BASE_SHIFT));\r | |
464 | if ((CurrentPageTableEntry & TT_DESCRIPTOR_PAGE_CACHEABLE_MASK) == TT_DESCRIPTOR_PAGE_CACHEABLE_MASK) {\r | |
465 | // The current section mapping is cacheable so Clean/Invalidate the MVA of the page\r | |
466 | // Note assumes switch(Attributes), not ARMv7 possibilities\r | |
467 | WriteBackInvalidateDataCacheRange (Mva, TT_DESCRIPTOR_PAGE_SIZE);\r | |
468 | }\r | |
469 | \r | |
470 | // Only need to update if we are changing the entry \r | |
471 | PageTable[PageTableIndex] = PageTableEntry; \r | |
472 | ArmUpdateTranslationTableEntry ((VOID *)&PageTable[PageTableIndex], Mva);\r | |
473 | }\r | |
474 | \r | |
475 | Status = EFI_SUCCESS;\r | |
476 | Offset += TT_DESCRIPTOR_PAGE_SIZE;\r | |
477 | \r | |
478 | } // End first level translation table loop\r | |
479 | \r | |
480 | return Status;\r | |
481 | }\r | |
482 | \r | |
483 | \r | |
484 | \r | |
485 | EFI_STATUS\r | |
486 | UpdateSectionEntries (\r | |
487 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
488 | IN UINT64 Length,\r | |
489 | IN UINT64 Attributes,\r | |
490 | IN EFI_PHYSICAL_ADDRESS VirtualMask\r | |
491 | )\r | |
492 | {\r | |
493 | EFI_STATUS Status = EFI_SUCCESS;\r | |
494 | UINT32 EntryMask;\r | |
495 | UINT32 EntryValue;\r | |
496 | UINT32 FirstLevelIdx;\r | |
497 | UINT32 NumSections;\r | |
498 | UINT32 i;\r | |
499 | UINT32 CurrentDescriptor;\r | |
500 | UINT32 Descriptor;\r | |
501 | VOID *Mva;\r | |
502 | volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r | |
503 | \r | |
504 | // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)\r | |
505 | // EntryValue: values at bit positions specified by EntryMask\r | |
506 | \r | |
507 | // Make sure we handle a section range that is unmapped \r | |
508 | EntryMask = TT_DESCRIPTOR_SECTION_TYPE_MASK;\r | |
509 | EntryValue = TT_DESCRIPTOR_SECTION_TYPE_SECTION;\r | |
510 | \r | |
511 | // Although the PI spec is unclear on this the GCD guarantees that only\r | |
512 | // one Attribute bit is set at a time, so we can safely use a switch statement\r | |
513 | switch(Attributes) {\r | |
514 | case EFI_MEMORY_UC:\r | |
515 | // modify cacheability attributes\r | |
516 | EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r | |
517 | // map to strongly ordered\r | |
518 | EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_STRONGLY_ORDERED; // TEX[2:0] = 0, C=0, B=0\r | |
519 | break;\r | |
520 | \r | |
521 | case EFI_MEMORY_WC:\r | |
522 | // modify cacheability attributes\r | |
523 | EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r | |
524 | // map to normal non-cachable\r | |
525 | EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE; // TEX [2:0]= 001 = 0x2, B=0, C=0\r | |
526 | break;\r | |
527 | \r | |
528 | case EFI_MEMORY_WT:\r | |
529 | // modify cacheability attributes\r | |
530 | EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r | |
531 | // write through with no-allocate\r | |
532 | EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC; // TEX [2:0] = 0, C=1, B=0\r | |
533 | break;\r | |
534 | \r | |
535 | case EFI_MEMORY_WB:\r | |
536 | // modify cacheability attributes\r | |
537 | EntryMask |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK;\r | |
538 | // write back (with allocate)\r | |
539 | EntryValue |= TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC; // TEX [2:0] = 001, C=1, B=1\r | |
540 | break;\r | |
541 | \r | |
542 | case EFI_MEMORY_WP:\r | |
543 | case EFI_MEMORY_XP:\r | |
544 | case EFI_MEMORY_RP:\r | |
545 | case EFI_MEMORY_UCE:\r | |
546 | // cannot be implemented UEFI definition unclear for ARM\r | |
547 | // Cause a page fault if these ranges are accessed.\r | |
548 | EntryValue = TT_DESCRIPTOR_SECTION_TYPE_FAULT;\r | |
549 | DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): setting section %lx with unsupported attribute %x will page fault on access\n", BaseAddress, Attributes));\r | |
550 | break;\r | |
551 | \r | |
552 | \r | |
553 | default:\r | |
554 | return EFI_UNSUPPORTED;\r | |
555 | }\r | |
556 | \r | |
557 | // obtain page table base\r | |
558 | FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTTBR0BaseAddress ();\r | |
559 | \r | |
560 | // calculate index into first level translation table for start of modification\r | |
561 | FirstLevelIdx = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT;\r | |
562 | ASSERT (FirstLevelIdx < TRANSLATION_TABLE_SECTION_COUNT);\r | |
563 | \r | |
564 | // calculate number of 1MB first level entries this applies to\r | |
565 | NumSections = Length / TT_DESCRIPTOR_SECTION_SIZE;\r | |
566 | \r | |
567 | // iterate through each descriptor\r | |
568 | for(i=0; i<NumSections; i++) {\r | |
569 | CurrentDescriptor = FirstLevelTable[FirstLevelIdx + i];\r | |
570 | \r | |
571 | // has this descriptor already been coverted to pages?\r | |
572 | if (TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(CurrentDescriptor)) {\r | |
573 | // forward this 1MB range to page table function instead\r | |
574 | Status = UpdatePageEntries ((FirstLevelIdx + i) << TT_DESCRIPTOR_SECTION_BASE_SHIFT, TT_DESCRIPTOR_SECTION_SIZE, Attributes, VirtualMask);\r | |
575 | } else {\r | |
576 | // still a section entry\r | |
577 | \r | |
578 | // mask off appropriate fields\r | |
579 | Descriptor = CurrentDescriptor & ~EntryMask;\r | |
580 | \r | |
581 | // mask in new attributes and/or permissions\r | |
582 | Descriptor |= EntryValue;\r | |
583 | if (VirtualMask != 0) {\r | |
584 | Descriptor &= ~VirtualMask;\r | |
585 | }\r | |
586 | \r | |
587 | if (CurrentDescriptor != Descriptor) {\r | |
588 | Mva = (VOID *)(UINTN)(((UINTN)FirstLevelTable) << TT_DESCRIPTOR_SECTION_BASE_SHIFT);\r | |
589 | if ((CurrentDescriptor & TT_DESCRIPTOR_SECTION_CACHEABLE_MASK) == TT_DESCRIPTOR_SECTION_CACHEABLE_MASK) {\r | |
590 | // The current section mapping is cacheable so Clean/Invalidate the MVA of the section\r | |
591 | // Note assumes switch(Attributes), not ARMv7 possabilities\r | |
592 | WriteBackInvalidateDataCacheRange (Mva, SIZE_1MB);\r | |
593 | }\r | |
594 | \r | |
595 | // Only need to update if we are changing the descriptor \r | |
596 | FirstLevelTable[FirstLevelIdx + i] = Descriptor;\r | |
597 | ArmUpdateTranslationTableEntry ((VOID *)&FirstLevelTable[FirstLevelIdx + i], Mva);\r | |
598 | }\r | |
599 | \r | |
600 | Status = EFI_SUCCESS;\r | |
601 | }\r | |
602 | }\r | |
603 | \r | |
604 | return Status;\r | |
605 | }\r | |
606 | \r | |
607 | EFI_STATUS \r | |
608 | ConvertSectionToPages (\r | |
609 | IN EFI_PHYSICAL_ADDRESS BaseAddress\r | |
610 | )\r | |
611 | {\r | |
612 | EFI_STATUS Status;\r | |
613 | EFI_PHYSICAL_ADDRESS PageTableAddr;\r | |
614 | UINT32 FirstLevelIdx;\r | |
615 | UINT32 SectionDescriptor;\r | |
616 | UINT32 PageTableDescriptor;\r | |
617 | UINT32 PageDescriptor;\r | |
618 | UINT32 Index;\r | |
619 | \r | |
620 | volatile ARM_FIRST_LEVEL_DESCRIPTOR *FirstLevelTable;\r | |
621 | volatile ARM_PAGE_TABLE_ENTRY *PageTable;\r | |
622 | \r | |
623 | DEBUG ((EFI_D_PAGE, "Converting section at 0x%x to pages\n", (UINTN)BaseAddress));\r | |
624 | \r | |
625 | // Obtain page table base\r | |
626 | FirstLevelTable = (ARM_FIRST_LEVEL_DESCRIPTOR *)ArmGetTTBR0BaseAddress ();\r | |
627 | \r | |
628 | // Calculate index into first level translation table for start of modification\r | |
629 | FirstLevelIdx = TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT;\r | |
630 | ASSERT (FirstLevelIdx < TRANSLATION_TABLE_SECTION_COUNT);\r | |
631 | \r | |
632 | // Get section attributes and convert to page attributes\r | |
633 | SectionDescriptor = FirstLevelTable[FirstLevelIdx];\r | |
634 | PageDescriptor = TT_DESCRIPTOR_PAGE_TYPE_PAGE;\r | |
635 | PageDescriptor |= TT_DESCRIPTOR_CONVERT_TO_PAGE_CACHE_POLICY(SectionDescriptor,0);\r | |
636 | PageDescriptor |= TT_DESCRIPTOR_CONVERT_TO_PAGE_AP(SectionDescriptor);\r | |
637 | PageDescriptor |= TT_DESCRIPTOR_CONVERT_TO_PAGE_XN(SectionDescriptor,0);\r | |
638 | PageDescriptor |= TT_DESCRIPTOR_CONVERT_TO_PAGE_NG(SectionDescriptor);\r | |
639 | PageDescriptor |= TT_DESCRIPTOR_CONVERT_TO_PAGE_S(SectionDescriptor);\r | |
640 | \r | |
641 | // Allocate a page table for the 4KB entries (we use up a full page even though we only need 1KB)\r | |
642 | Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, 1, &PageTableAddr);\r | |
643 | if (EFI_ERROR(Status)) {\r | |
644 | return Status;\r | |
645 | }\r | |
646 | \r | |
647 | PageTable = (volatile ARM_PAGE_TABLE_ENTRY *)(UINTN)PageTableAddr;\r | |
648 | \r | |
649 | // Write the page table entries out\r | |
650 | for (Index = 0; Index < TRANSLATION_TABLE_PAGE_COUNT; Index++) {\r | |
651 | PageTable[Index] = TT_DESCRIPTOR_PAGE_BASE_ADDRESS(BaseAddress + (Index << 12)) | PageDescriptor;\r | |
652 | }\r | |
653 | \r | |
654 | // Flush d-cache so descriptors make it back to uncached memory for subsequent table walks\r | |
655 | WriteBackInvalidateDataCacheRange ((VOID *)(UINTN)PageTableAddr, TT_DESCRIPTOR_PAGE_SIZE);\r | |
656 | \r | |
657 | // Formulate page table entry, Domain=0, NS=0\r | |
658 | PageTableDescriptor = (((UINTN)PageTableAddr) & TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK) | TT_DESCRIPTOR_SECTION_TYPE_PAGE_TABLE;\r | |
659 | \r | |
660 | // Write the page table entry out, replacing section entry\r | |
661 | FirstLevelTable[FirstLevelIdx] = PageTableDescriptor;\r | |
662 | \r | |
663 | return EFI_SUCCESS;\r | |
664 | }\r | |
665 | \r | |
666 | \r | |
667 | \r | |
668 | EFI_STATUS\r | |
669 | SetMemoryAttributes (\r | |
670 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
671 | IN UINT64 Length,\r | |
672 | IN UINT64 Attributes,\r | |
673 | IN EFI_PHYSICAL_ADDRESS VirtualMask\r | |
674 | )\r | |
675 | {\r | |
676 | EFI_STATUS Status;\r | |
677 | \r | |
678 | if(((BaseAddress & 0xFFFFF) == 0) && ((Length & 0xFFFFF) == 0)) {\r | |
679 | // Is the base and length a multiple of 1 MB?\r | |
680 | DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): MMU section 0x%x length 0x%x to %lx\n", (UINTN)BaseAddress, (UINTN)Length, Attributes));\r | |
681 | Status = UpdateSectionEntries (BaseAddress, Length, Attributes, VirtualMask);\r | |
682 | } else {\r | |
683 | // Base and/or length is not a multiple of 1 MB\r | |
684 | DEBUG ((EFI_D_PAGE, "SetMemoryAttributes(): MMU page 0x%x length 0x%x to %lx\n", (UINTN)BaseAddress, (UINTN)Length, Attributes));\r | |
685 | Status = UpdatePageEntries (BaseAddress, Length, Attributes, VirtualMask);\r | |
686 | }\r | |
687 | \r | |
688 | // Flush d-cache so descriptors make it back to uncached memory for subsequent table walks\r | |
689 | // flush and invalidate pages\r | |
690 | //TODO: Do we really need to invalidate the caches everytime we change the memory attributes ?\r | |
691 | ArmCleanInvalidateDataCache ();\r | |
692 | \r | |
693 | ArmInvalidateInstructionCache ();\r | |
694 | \r | |
695 | // Invalidate all TLB entries so changes are synced\r | |
696 | ArmInvalidateTlb ();\r | |
697 | \r | |
698 | return Status;\r | |
699 | }\r |