]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuDxe/CpuPageTable.c
UefiCpuPkg/CpuDxe: fix bad boot performance
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuPageTable.c
CommitLineData
22292ed3
JY
1/** @file\r
2 Page table management support.\r
3\r
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
627dcba3
LD
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
22292ed3
JY
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <Base.h>\r
18#include <Uefi.h>\r
19#include <Library/BaseLib.h>\r
20#include <Library/CpuLib.h>\r
21#include <Library/BaseMemoryLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/DebugLib.h>\r
24#include <Library/UefiBootServicesTableLib.h>\r
25#include <Protocol/MpService.h>\r
c1cab54c
JW
26\r
27#include "CpuDxe.h"\r
22292ed3
JY
28#include "CpuPageTable.h"\r
29\r
30///\r
31/// Page Table Entry\r
32///\r
33#define IA32_PG_P BIT0\r
34#define IA32_PG_RW BIT1\r
35#define IA32_PG_U BIT2\r
36#define IA32_PG_WT BIT3\r
37#define IA32_PG_CD BIT4\r
38#define IA32_PG_A BIT5\r
39#define IA32_PG_D BIT6\r
40#define IA32_PG_PS BIT7\r
41#define IA32_PG_PAT_2M BIT12\r
42#define IA32_PG_PAT_4K IA32_PG_PS\r
43#define IA32_PG_PMNT BIT62\r
44#define IA32_PG_NX BIT63\r
45\r
46#define PAGE_ATTRIBUTE_BITS (IA32_PG_D | IA32_PG_A | IA32_PG_U | IA32_PG_RW | IA32_PG_P)\r
47//\r
48// Bits 1, 2, 5, 6 are reserved in the IA32 PAE PDPTE\r
49// X64 PAE PDPTE does not have such restriction\r
50//\r
51#define IA32_PAE_PDPTE_ATTRIBUTE_BITS (IA32_PG_P)\r
52\r
53#define PAGE_PROGATE_BITS (IA32_PG_NX | PAGE_ATTRIBUTE_BITS)\r
54\r
55#define PAGING_4K_MASK 0xFFF\r
56#define PAGING_2M_MASK 0x1FFFFF\r
57#define PAGING_1G_MASK 0x3FFFFFFF\r
58\r
59#define PAGING_PAE_INDEX_MASK 0x1FF\r
60\r
61#define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull\r
62#define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull\r
63#define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull\r
64\r
65typedef enum {\r
66 PageNone,\r
67 Page4K,\r
68 Page2M,\r
69 Page1G,\r
70} PAGE_ATTRIBUTE;\r
71\r
72typedef struct {\r
73 PAGE_ATTRIBUTE Attribute;\r
74 UINT64 Length;\r
75 UINT64 AddressMask;\r
76} PAGE_ATTRIBUTE_TABLE;\r
77\r
78typedef enum {\r
79 PageActionAssign,\r
80 PageActionSet,\r
81 PageActionClear,\r
82} PAGE_ACTION;\r
83\r
84PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] = {\r
85 {Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64},\r
86 {Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64},\r
87 {Page1G, SIZE_1GB, PAGING_1G_ADDRESS_MASK_64},\r
88};\r
89\r
147fd35c
JW
90PAGE_TABLE_POOL *mPageTablePool = NULL;\r
91\r
22292ed3
JY
92/**\r
93 Enable write protection function for AP.\r
94\r
95 @param[in,out] Buffer The pointer to private data buffer.\r
96**/\r
97VOID\r
98EFIAPI\r
99SyncCpuEnableWriteProtection (\r
100 IN OUT VOID *Buffer\r
101 )\r
102{\r
103 AsmWriteCr0 (AsmReadCr0 () | BIT16);\r
104}\r
105\r
106/**\r
107 CpuFlushTlb function for AP.\r
108\r
109 @param[in,out] Buffer The pointer to private data buffer.\r
110**/\r
111VOID\r
112EFIAPI\r
113SyncCpuFlushTlb (\r
114 IN OUT VOID *Buffer\r
115 )\r
116{\r
117 CpuFlushTlb();\r
118}\r
119\r
120/**\r
121 Sync memory page attributes for AP.\r
122\r
123 @param[in] Procedure A pointer to the function to be run on enabled APs of\r
124 the system.\r
125**/\r
126VOID\r
127SyncMemoryPageAttributesAp (\r
128 IN EFI_AP_PROCEDURE Procedure\r
129 )\r
130{\r
131 EFI_STATUS Status;\r
132 EFI_MP_SERVICES_PROTOCOL *MpService;\r
133\r
134 Status = gBS->LocateProtocol (\r
135 &gEfiMpServiceProtocolGuid,\r
136 NULL,\r
137 (VOID **)&MpService\r
138 );\r
139 //\r
140 // Synchronize the update with all APs\r
141 //\r
142 if (!EFI_ERROR (Status)) {\r
143 Status = MpService->StartupAllAPs (\r
144 MpService, // This\r
145 Procedure, // Procedure\r
146 FALSE, // SingleThread\r
147 NULL, // WaitEvent\r
148 0, // TimeoutInMicrosecsond\r
149 NULL, // ProcedureArgument\r
150 NULL // FailedCpuList\r
151 );\r
152 ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_STARTED || Status == EFI_NOT_READY);\r
153 }\r
154}\r
155\r
156/**\r
157 Return current paging context.\r
158\r
159 @param[in,out] PagingContext The paging context.\r
160**/\r
161VOID\r
162GetCurrentPagingContext (\r
163 IN OUT PAGE_TABLE_LIB_PAGING_CONTEXT *PagingContext\r
164 )\r
165{\r
166 UINT32 RegEax;\r
167 UINT32 RegEdx;\r
168\r
169 ZeroMem(PagingContext, sizeof(*PagingContext));\r
170 if (sizeof(UINTN) == sizeof(UINT64)) {\r
171 PagingContext->MachineType = IMAGE_FILE_MACHINE_X64;\r
172 } else {\r
173 PagingContext->MachineType = IMAGE_FILE_MACHINE_I386;\r
174 }\r
175 if ((AsmReadCr0 () & BIT31) != 0) {\r
176 PagingContext->ContextData.X64.PageTableBase = (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64);\r
22292ed3
JY
177 } else {\r
178 PagingContext->ContextData.X64.PageTableBase = 0;\r
179 }\r
180\r
181 if ((AsmReadCr4 () & BIT4) != 0) {\r
182 PagingContext->ContextData.Ia32.Attributes |= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PSE;\r
183 }\r
184 if ((AsmReadCr4 () & BIT5) != 0) {\r
185 PagingContext->ContextData.Ia32.Attributes |= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE;\r
186 }\r
187 if ((AsmReadCr0 () & BIT16) != 0) {\r
188 PagingContext->ContextData.Ia32.Attributes |= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_WP_ENABLE;\r
189 }\r
190\r
191 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
192 if (RegEax > 0x80000000) {\r
193 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
194 if ((RegEdx & BIT20) != 0) {\r
195 // XD supported\r
01eb3f39
JF
196 if ((AsmReadMsr64 (0xC0000080) & BIT11) != 0) {\r
197 // XD activated\r
198 PagingContext->ContextData.Ia32.Attributes |= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED;\r
22292ed3
JY
199 }\r
200 }\r
201 if ((RegEdx & BIT26) != 0) {\r
202 PagingContext->ContextData.Ia32.Attributes |= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAGE_1G_SUPPORT;\r
203 }\r
204 }\r
205}\r
206\r
207/**\r
208 Return length according to page attributes.\r
209\r
210 @param[in] PageAttributes The page attribute of the page entry.\r
211\r
212 @return The length of page entry.\r
213**/\r
214UINTN\r
215PageAttributeToLength (\r
216 IN PAGE_ATTRIBUTE PageAttribute\r
217 )\r
218{\r
219 UINTN Index;\r
220 for (Index = 0; Index < sizeof(mPageAttributeTable)/sizeof(mPageAttributeTable[0]); Index++) {\r
221 if (PageAttribute == mPageAttributeTable[Index].Attribute) {\r
222 return (UINTN)mPageAttributeTable[Index].Length;\r
223 }\r
224 }\r
225 return 0;\r
226}\r
227\r
228/**\r
229 Return address mask according to page attributes.\r
230\r
231 @param[in] PageAttributes The page attribute of the page entry.\r
232\r
233 @return The address mask of page entry.\r
234**/\r
235UINTN\r
236PageAttributeToMask (\r
237 IN PAGE_ATTRIBUTE PageAttribute\r
238 )\r
239{\r
240 UINTN Index;\r
241 for (Index = 0; Index < sizeof(mPageAttributeTable)/sizeof(mPageAttributeTable[0]); Index++) {\r
242 if (PageAttribute == mPageAttributeTable[Index].Attribute) {\r
243 return (UINTN)mPageAttributeTable[Index].AddressMask;\r
244 }\r
245 }\r
246 return 0;\r
247}\r
248\r
249/**\r
250 Return page table entry to match the address.\r
251\r
252 @param[in] PagingContext The paging context.\r
253 @param[in] Address The address to be checked.\r
254 @param[out] PageAttributes The page attribute of the page entry.\r
255\r
256 @return The page entry.\r
257**/\r
258VOID *\r
259GetPageTableEntry (\r
260 IN PAGE_TABLE_LIB_PAGING_CONTEXT *PagingContext,\r
261 IN PHYSICAL_ADDRESS Address,\r
262 OUT PAGE_ATTRIBUTE *PageAttribute\r
263 )\r
264{\r
265 UINTN Index1;\r
266 UINTN Index2;\r
267 UINTN Index3;\r
268 UINTN Index4;\r
269 UINT64 *L1PageTable;\r
270 UINT64 *L2PageTable;\r
271 UINT64 *L3PageTable;\r
272 UINT64 *L4PageTable;\r
627dcba3 273 UINT64 AddressEncMask;\r
22292ed3
JY
274\r
275 ASSERT (PagingContext != NULL);\r
276\r
277 Index4 = ((UINTN)RShiftU64 (Address, 39)) & PAGING_PAE_INDEX_MASK;\r
278 Index3 = ((UINTN)Address >> 30) & PAGING_PAE_INDEX_MASK;\r
279 Index2 = ((UINTN)Address >> 21) & PAGING_PAE_INDEX_MASK;\r
280 Index1 = ((UINTN)Address >> 12) & PAGING_PAE_INDEX_MASK;\r
281\r
627dcba3
LD
282 // Make sure AddressEncMask is contained to smallest supported address field.\r
283 //\r
284 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;\r
285\r
22292ed3
JY
286 if (PagingContext->MachineType == IMAGE_FILE_MACHINE_X64) {\r
287 L4PageTable = (UINT64 *)(UINTN)PagingContext->ContextData.X64.PageTableBase;\r
288 if (L4PageTable[Index4] == 0) {\r
289 *PageAttribute = PageNone;\r
290 return NULL;\r
291 }\r
292\r
627dcba3 293 L3PageTable = (UINT64 *)(UINTN)(L4PageTable[Index4] & ~AddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r
22292ed3
JY
294 } else {\r
295 ASSERT((PagingContext->ContextData.Ia32.Attributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE) != 0);\r
296 L3PageTable = (UINT64 *)(UINTN)PagingContext->ContextData.Ia32.PageTableBase;\r
297 }\r
298 if (L3PageTable[Index3] == 0) {\r
299 *PageAttribute = PageNone;\r
300 return NULL;\r
301 }\r
302 if ((L3PageTable[Index3] & IA32_PG_PS) != 0) {\r
303 // 1G\r
304 *PageAttribute = Page1G;\r
305 return &L3PageTable[Index3];\r
306 }\r
307\r
627dcba3 308 L2PageTable = (UINT64 *)(UINTN)(L3PageTable[Index3] & ~AddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r
22292ed3
JY
309 if (L2PageTable[Index2] == 0) {\r
310 *PageAttribute = PageNone;\r
311 return NULL;\r
312 }\r
313 if ((L2PageTable[Index2] & IA32_PG_PS) != 0) {\r
314 // 2M\r
315 *PageAttribute = Page2M;\r
316 return &L2PageTable[Index2];\r
317 }\r
318\r
319 // 4k\r
627dcba3 320 L1PageTable = (UINT64 *)(UINTN)(L2PageTable[Index2] & ~AddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r
22292ed3
JY
321 if ((L1PageTable[Index1] == 0) && (Address != 0)) {\r
322 *PageAttribute = PageNone;\r
323 return NULL;\r
324 }\r
325 *PageAttribute = Page4K;\r
326 return &L1PageTable[Index1];\r
327}\r
328\r
329/**\r
330 Return memory attributes of page entry.\r
331\r
332 @param[in] PageEntry The page entry.\r
333\r
334 @return Memory attributes of page entry.\r
335**/\r
336UINT64\r
337GetAttributesFromPageEntry (\r
338 IN UINT64 *PageEntry\r
339 )\r
340{\r
341 UINT64 Attributes;\r
342 Attributes = 0;\r
343 if ((*PageEntry & IA32_PG_P) == 0) {\r
344 Attributes |= EFI_MEMORY_RP;\r
345 }\r
346 if ((*PageEntry & IA32_PG_RW) == 0) {\r
347 Attributes |= EFI_MEMORY_RO;\r
348 }\r
349 if ((*PageEntry & IA32_PG_NX) != 0) {\r
350 Attributes |= EFI_MEMORY_XP;\r
351 }\r
352 return Attributes;\r
353}\r
354\r
355/**\r
356 Modify memory attributes of page entry.\r
357\r
358 @param[in] PagingContext The paging context.\r
359 @param[in] PageEntry The page entry.\r
360 @param[in] Attributes The bit mask of attributes to modify for the memory region.\r
361 @param[in] PageAction The page action.\r
362 @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.\r
363**/\r
364VOID\r
365ConvertPageEntryAttribute (\r
366 IN PAGE_TABLE_LIB_PAGING_CONTEXT *PagingContext,\r
367 IN UINT64 *PageEntry,\r
368 IN UINT64 Attributes,\r
369 IN PAGE_ACTION PageAction,\r
370 OUT BOOLEAN *IsModified\r
371 )\r
372{\r
373 UINT64 CurrentPageEntry;\r
374 UINT64 NewPageEntry;\r
375\r
376 CurrentPageEntry = *PageEntry;\r
377 NewPageEntry = CurrentPageEntry;\r
378 if ((Attributes & EFI_MEMORY_RP) != 0) {\r
379 switch (PageAction) {\r
380 case PageActionAssign:\r
381 case PageActionSet:\r
382 NewPageEntry &= ~(UINT64)IA32_PG_P;\r
383 break;\r
384 case PageActionClear:\r
385 NewPageEntry |= IA32_PG_P;\r
386 break;\r
387 }\r
388 } else {\r
389 switch (PageAction) {\r
390 case PageActionAssign:\r
391 NewPageEntry |= IA32_PG_P;\r
392 break;\r
393 case PageActionSet:\r
394 case PageActionClear:\r
395 break;\r
396 }\r
397 }\r
398 if ((Attributes & EFI_MEMORY_RO) != 0) {\r
399 switch (PageAction) {\r
400 case PageActionAssign:\r
401 case PageActionSet:\r
402 NewPageEntry &= ~(UINT64)IA32_PG_RW;\r
403 break;\r
404 case PageActionClear:\r
405 NewPageEntry |= IA32_PG_RW;\r
406 break;\r
407 }\r
408 } else {\r
409 switch (PageAction) {\r
410 case PageActionAssign:\r
411 NewPageEntry |= IA32_PG_RW;\r
412 break;\r
413 case PageActionSet:\r
414 case PageActionClear:\r
415 break;\r
416 }\r
417 }\r
418 if ((PagingContext->ContextData.Ia32.Attributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED) != 0) {\r
419 if ((Attributes & EFI_MEMORY_XP) != 0) {\r
420 switch (PageAction) {\r
421 case PageActionAssign:\r
422 case PageActionSet:\r
423 NewPageEntry |= IA32_PG_NX;\r
424 break;\r
425 case PageActionClear:\r
426 NewPageEntry &= ~IA32_PG_NX;\r
427 break;\r
428 }\r
429 } else {\r
430 switch (PageAction) {\r
431 case PageActionAssign:\r
432 NewPageEntry &= ~IA32_PG_NX;\r
433 break;\r
434 case PageActionSet:\r
435 case PageActionClear:\r
436 break;\r
437 }\r
438 }\r
439 }\r
440 *PageEntry = NewPageEntry;\r
441 if (CurrentPageEntry != NewPageEntry) {\r
442 *IsModified = TRUE;\r
827330cc
JW
443 DEBUG ((DEBUG_VERBOSE, "ConvertPageEntryAttribute 0x%lx", CurrentPageEntry));\r
444 DEBUG ((DEBUG_VERBOSE, "->0x%lx\n", NewPageEntry));\r
22292ed3
JY
445 } else {\r
446 *IsModified = FALSE;\r
447 }\r
448}\r
449\r
450/**\r
451 This function returns if there is need to split page entry.\r
452\r
453 @param[in] BaseAddress The base address to be checked.\r
454 @param[in] Length The length to be checked.\r
455 @param[in] PageEntry The page entry to be checked.\r
456 @param[in] PageAttribute The page attribute of the page entry.\r
457\r
458 @retval SplitAttributes on if there is need to split page entry.\r
459**/\r
460PAGE_ATTRIBUTE\r
461NeedSplitPage (\r
462 IN PHYSICAL_ADDRESS BaseAddress,\r
463 IN UINT64 Length,\r
464 IN UINT64 *PageEntry,\r
465 IN PAGE_ATTRIBUTE PageAttribute\r
466 )\r
467{\r
468 UINT64 PageEntryLength;\r
469\r
470 PageEntryLength = PageAttributeToLength (PageAttribute);\r
471\r
472 if (((BaseAddress & (PageEntryLength - 1)) == 0) && (Length >= PageEntryLength)) {\r
473 return PageNone;\r
474 }\r
475\r
476 if (((BaseAddress & PAGING_2M_MASK) != 0) || (Length < SIZE_2MB)) {\r
477 return Page4K;\r
478 }\r
479\r
480 return Page2M;\r
481}\r
482\r
483/**\r
484 This function splits one page entry to small page entries.\r
485\r
486 @param[in] PageEntry The page entry to be splitted.\r
487 @param[in] PageAttribute The page attribute of the page entry.\r
488 @param[in] SplitAttribute How to split the page entry.\r
489 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.\r
490\r
491 @retval RETURN_SUCCESS The page entry is splitted.\r
492 @retval RETURN_UNSUPPORTED The page entry does not support to be splitted.\r
493 @retval RETURN_OUT_OF_RESOURCES No resource to split page entry.\r
494**/\r
495RETURN_STATUS\r
496SplitPage (\r
497 IN UINT64 *PageEntry,\r
498 IN PAGE_ATTRIBUTE PageAttribute,\r
499 IN PAGE_ATTRIBUTE SplitAttribute,\r
500 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc\r
501 )\r
502{\r
503 UINT64 BaseAddress;\r
504 UINT64 *NewPageEntry;\r
505 UINTN Index;\r
627dcba3 506 UINT64 AddressEncMask;\r
22292ed3
JY
507\r
508 ASSERT (PageAttribute == Page2M || PageAttribute == Page1G);\r
509\r
510 ASSERT (AllocatePagesFunc != NULL);\r
511\r
627dcba3
LD
512 // Make sure AddressEncMask is contained to smallest supported address field.\r
513 //\r
514 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;\r
515\r
22292ed3
JY
516 if (PageAttribute == Page2M) {\r
517 //\r
518 // Split 2M to 4K\r
519 //\r
520 ASSERT (SplitAttribute == Page4K);\r
521 if (SplitAttribute == Page4K) {\r
522 NewPageEntry = AllocatePagesFunc (1);\r
523 DEBUG ((DEBUG_INFO, "Split - 0x%x\n", NewPageEntry));\r
524 if (NewPageEntry == NULL) {\r
525 return RETURN_OUT_OF_RESOURCES;\r
526 }\r
627dcba3 527 BaseAddress = *PageEntry & ~AddressEncMask & PAGING_2M_ADDRESS_MASK_64;\r
22292ed3 528 for (Index = 0; Index < SIZE_4KB / sizeof(UINT64); Index++) {\r
627dcba3 529 NewPageEntry[Index] = (BaseAddress + SIZE_4KB * Index) | AddressEncMask | ((*PageEntry) & PAGE_PROGATE_BITS);\r
22292ed3 530 }\r
fbe2c4b9 531 (*PageEntry) = (UINT64)(UINTN)NewPageEntry | AddressEncMask | ((*PageEntry) & PAGE_ATTRIBUTE_BITS);\r
22292ed3
JY
532 return RETURN_SUCCESS;\r
533 } else {\r
534 return RETURN_UNSUPPORTED;\r
535 }\r
536 } else if (PageAttribute == Page1G) {\r
537 //\r
538 // Split 1G to 2M\r
539 // No need support 1G->4K directly, we should use 1G->2M, then 2M->4K to get more compact page table.\r
540 //\r
541 ASSERT (SplitAttribute == Page2M || SplitAttribute == Page4K);\r
542 if ((SplitAttribute == Page2M || SplitAttribute == Page4K)) {\r
543 NewPageEntry = AllocatePagesFunc (1);\r
544 DEBUG ((DEBUG_INFO, "Split - 0x%x\n", NewPageEntry));\r
545 if (NewPageEntry == NULL) {\r
546 return RETURN_OUT_OF_RESOURCES;\r
547 }\r
627dcba3 548 BaseAddress = *PageEntry & ~AddressEncMask & PAGING_1G_ADDRESS_MASK_64;\r
22292ed3 549 for (Index = 0; Index < SIZE_4KB / sizeof(UINT64); Index++) {\r
627dcba3 550 NewPageEntry[Index] = (BaseAddress + SIZE_2MB * Index) | AddressEncMask | IA32_PG_PS | ((*PageEntry) & PAGE_PROGATE_BITS);\r
22292ed3 551 }\r
fbe2c4b9 552 (*PageEntry) = (UINT64)(UINTN)NewPageEntry | AddressEncMask | ((*PageEntry) & PAGE_ATTRIBUTE_BITS);\r
22292ed3
JY
553 return RETURN_SUCCESS;\r
554 } else {\r
555 return RETURN_UNSUPPORTED;\r
556 }\r
557 } else {\r
558 return RETURN_UNSUPPORTED;\r
559 }\r
560}\r
561\r
147fd35c
JW
562/**\r
563 Check the WP status in CR0 register. This bit is used to lock or unlock write\r
564 access to pages marked as read-only.\r
565\r
566 @retval TRUE Write protection is enabled.\r
567 @retval FALSE Write protection is disabled.\r
568**/\r
569BOOLEAN\r
570IsReadOnlyPageWriteProtected (\r
571 VOID\r
572 )\r
573{\r
574 return ((AsmReadCr0 () & BIT16) != 0);\r
575}\r
576\r
577/**\r
578 Disable write protection function for AP.\r
579\r
580 @param[in,out] Buffer The pointer to private data buffer.\r
581**/\r
582VOID\r
583EFIAPI\r
584SyncCpuDisableWriteProtection (\r
585 IN OUT VOID *Buffer\r
586 )\r
587{\r
588 AsmWriteCr0 (AsmReadCr0() & ~BIT16);\r
589}\r
590\r
591/**\r
592 Disable Write Protect on pages marked as read-only.\r
593**/\r
594VOID\r
595DisableReadOnlyPageWriteProtect (\r
596 VOID\r
597 )\r
598{\r
599 AsmWriteCr0 (AsmReadCr0() & ~BIT16);\r
147fd35c
JW
600}\r
601\r
602/**\r
603 Enable Write Protect on pages marked as read-only.\r
604**/\r
605VOID\r
606EnableReadOnlyPageWriteProtect (\r
607 VOID\r
608 )\r
609{\r
610 AsmWriteCr0 (AsmReadCr0() | BIT16);\r
147fd35c
JW
611}\r
612\r
22292ed3
JY
613/**\r
614 This function modifies the page attributes for the memory region specified by BaseAddress and\r
615 Length from their current attributes to the attributes specified by Attributes.\r
616\r
617 Caller should make sure BaseAddress and Length is at page boundary.\r
618\r
619 @param[in] PagingContext The paging context. NULL means get page table from current CPU context.\r
620 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
621 @param[in] Length The size in bytes of the memory region.\r
622 @param[in] Attributes The bit mask of attributes to modify for the memory region.\r
623 @param[in] PageAction The page action.\r
624 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.\r
625 NULL mean page split is unsupported.\r
626 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r
627 @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.\r
628\r
629 @retval RETURN_SUCCESS The attributes were modified for the memory region.\r
630 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r
631 BaseAddress and Length cannot be modified.\r
632 @retval RETURN_INVALID_PARAMETER Length is zero.\r
633 Attributes specified an illegal combination of attributes that\r
634 cannot be set together.\r
635 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
636 the memory resource range.\r
637 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory\r
638 resource range specified by BaseAddress and Length.\r
639 The bit mask of attributes is not support for the memory resource\r
640 range specified by BaseAddress and Length.\r
641**/\r
642RETURN_STATUS\r
643ConvertMemoryPageAttributes (\r
644 IN PAGE_TABLE_LIB_PAGING_CONTEXT *PagingContext OPTIONAL,\r
645 IN PHYSICAL_ADDRESS BaseAddress,\r
646 IN UINT64 Length,\r
647 IN UINT64 Attributes,\r
648 IN PAGE_ACTION PageAction,\r
649 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL,\r
650 OUT BOOLEAN *IsSplitted, OPTIONAL\r
651 OUT BOOLEAN *IsModified OPTIONAL\r
652 )\r
653{\r
654 PAGE_TABLE_LIB_PAGING_CONTEXT CurrentPagingContext;\r
655 UINT64 *PageEntry;\r
656 PAGE_ATTRIBUTE PageAttribute;\r
657 UINTN PageEntryLength;\r
658 PAGE_ATTRIBUTE SplitAttribute;\r
659 RETURN_STATUS Status;\r
660 BOOLEAN IsEntryModified;\r
147fd35c 661 BOOLEAN IsWpEnabled;\r
22292ed3
JY
662\r
663 if ((BaseAddress & (SIZE_4KB - 1)) != 0) {\r
664 DEBUG ((DEBUG_ERROR, "BaseAddress(0x%lx) is not aligned!\n", BaseAddress));\r
665 return EFI_UNSUPPORTED;\r
666 }\r
667 if ((Length & (SIZE_4KB - 1)) != 0) {\r
668 DEBUG ((DEBUG_ERROR, "Length(0x%lx) is not aligned!\n", Length));\r
669 return EFI_UNSUPPORTED;\r
670 }\r
671 if (Length == 0) {\r
672 DEBUG ((DEBUG_ERROR, "Length is 0!\n"));\r
673 return RETURN_INVALID_PARAMETER;\r
674 }\r
675\r
676 if ((Attributes & ~(EFI_MEMORY_RP | EFI_MEMORY_RO | EFI_MEMORY_XP)) != 0) {\r
677 DEBUG ((DEBUG_ERROR, "Attributes(0x%lx) has unsupported bit\n", Attributes));\r
678 return EFI_UNSUPPORTED;\r
679 }\r
680\r
681 if (PagingContext == NULL) {\r
682 GetCurrentPagingContext (&CurrentPagingContext);\r
683 } else {\r
684 CopyMem (&CurrentPagingContext, PagingContext, sizeof(CurrentPagingContext));\r
685 }\r
686 switch(CurrentPagingContext.MachineType) {\r
687 case IMAGE_FILE_MACHINE_I386:\r
688 if (CurrentPagingContext.ContextData.Ia32.PageTableBase == 0) {\r
22292ed3
JY
689 if (Attributes == 0) {\r
690 return EFI_SUCCESS;\r
691 } else {\r
c5719579 692 DEBUG ((DEBUG_ERROR, "PageTable is 0!\n"));\r
22292ed3
JY
693 return EFI_UNSUPPORTED;\r
694 }\r
695 }\r
696 if ((CurrentPagingContext.ContextData.Ia32.Attributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE) == 0) {\r
697 DEBUG ((DEBUG_ERROR, "Non-PAE Paging!\n"));\r
698 return EFI_UNSUPPORTED;\r
699 }\r
4f10654e
JW
700 if ((BaseAddress + Length) > BASE_4GB) {\r
701 DEBUG ((DEBUG_ERROR, "Beyond 4GB memory in 32-bit mode!\n"));\r
702 return EFI_UNSUPPORTED;\r
703 }\r
22292ed3
JY
704 break;\r
705 case IMAGE_FILE_MACHINE_X64:\r
706 ASSERT (CurrentPagingContext.ContextData.X64.PageTableBase != 0);\r
707 break;\r
708 default:\r
709 ASSERT(FALSE);\r
710 return EFI_UNSUPPORTED;\r
711 break;\r
712 }\r
713\r
714// DEBUG ((DEBUG_ERROR, "ConvertMemoryPageAttributes(%x) - %016lx, %016lx, %02lx\n", IsSet, BaseAddress, Length, Attributes));\r
715\r
716 if (IsSplitted != NULL) {\r
717 *IsSplitted = FALSE;\r
718 }\r
719 if (IsModified != NULL) {\r
720 *IsModified = FALSE;\r
721 }\r
147fd35c
JW
722 if (AllocatePagesFunc == NULL) {\r
723 AllocatePagesFunc = AllocatePageTableMemory;\r
724 }\r
725\r
726 //\r
727 // Make sure that the page table is changeable.\r
728 //\r
729 IsWpEnabled = IsReadOnlyPageWriteProtected ();\r
730 if (IsWpEnabled) {\r
731 DisableReadOnlyPageWriteProtect ();\r
732 }\r
22292ed3
JY
733\r
734 //\r
735 // Below logic is to check 2M/4K page to make sure we donot waist memory.\r
736 //\r
147fd35c 737 Status = EFI_SUCCESS;\r
22292ed3
JY
738 while (Length != 0) {\r
739 PageEntry = GetPageTableEntry (&CurrentPagingContext, BaseAddress, &PageAttribute);\r
740 if (PageEntry == NULL) {\r
147fd35c
JW
741 Status = RETURN_UNSUPPORTED;\r
742 goto Done;\r
22292ed3
JY
743 }\r
744 PageEntryLength = PageAttributeToLength (PageAttribute);\r
745 SplitAttribute = NeedSplitPage (BaseAddress, Length, PageEntry, PageAttribute);\r
746 if (SplitAttribute == PageNone) {\r
747 ConvertPageEntryAttribute (&CurrentPagingContext, PageEntry, Attributes, PageAction, &IsEntryModified);\r
748 if (IsEntryModified) {\r
749 if (IsModified != NULL) {\r
750 *IsModified = TRUE;\r
751 }\r
752 }\r
753 //\r
754 // Convert success, move to next\r
755 //\r
756 BaseAddress += PageEntryLength;\r
757 Length -= PageEntryLength;\r
758 } else {\r
759 if (AllocatePagesFunc == NULL) {\r
147fd35c
JW
760 Status = RETURN_UNSUPPORTED;\r
761 goto Done;\r
22292ed3
JY
762 }\r
763 Status = SplitPage (PageEntry, PageAttribute, SplitAttribute, AllocatePagesFunc);\r
764 if (RETURN_ERROR (Status)) {\r
147fd35c
JW
765 Status = RETURN_UNSUPPORTED;\r
766 goto Done;\r
22292ed3
JY
767 }\r
768 if (IsSplitted != NULL) {\r
769 *IsSplitted = TRUE;\r
770 }\r
771 if (IsModified != NULL) {\r
772 *IsModified = TRUE;\r
773 }\r
774 //\r
775 // Just split current page\r
776 // Convert success in next around\r
777 //\r
778 }\r
779 }\r
780\r
147fd35c
JW
781Done:\r
782 //\r
783 // Restore page table write protection, if any.\r
784 //\r
785 if (IsWpEnabled) {\r
786 EnableReadOnlyPageWriteProtect ();\r
787 }\r
788 return Status;\r
22292ed3
JY
789}\r
790\r
791/**\r
792 This function assigns the page attributes for the memory region specified by BaseAddress and\r
793 Length from their current attributes to the attributes specified by Attributes.\r
794\r
795 Caller should make sure BaseAddress and Length is at page boundary.\r
796\r
797 Caller need guarentee the TPL <= TPL_NOTIFY, if there is split page request.\r
798\r
799 @param[in] PagingContext The paging context. NULL means get page table from current CPU context.\r
800 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
801 @param[in] Length The size in bytes of the memory region.\r
802 @param[in] Attributes The bit mask of attributes to set for the memory region.\r
803 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.\r
804 NULL mean page split is unsupported.\r
805\r
806 @retval RETURN_SUCCESS The attributes were cleared for the memory region.\r
807 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r
808 BaseAddress and Length cannot be modified.\r
809 @retval RETURN_INVALID_PARAMETER Length is zero.\r
810 Attributes specified an illegal combination of attributes that\r
811 cannot be set together.\r
812 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
813 the memory resource range.\r
814 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory\r
815 resource range specified by BaseAddress and Length.\r
816 The bit mask of attributes is not support for the memory resource\r
817 range specified by BaseAddress and Length.\r
818**/\r
819RETURN_STATUS\r
820EFIAPI\r
821AssignMemoryPageAttributes (\r
822 IN PAGE_TABLE_LIB_PAGING_CONTEXT *PagingContext OPTIONAL,\r
823 IN PHYSICAL_ADDRESS BaseAddress,\r
824 IN UINT64 Length,\r
825 IN UINT64 Attributes,\r
826 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL\r
827 )\r
828{\r
829 RETURN_STATUS Status;\r
830 BOOLEAN IsModified;\r
831 BOOLEAN IsSplitted;\r
832\r
833// DEBUG((DEBUG_INFO, "AssignMemoryPageAttributes: 0x%lx - 0x%lx (0x%lx)\n", BaseAddress, Length, Attributes));\r
834 Status = ConvertMemoryPageAttributes (PagingContext, BaseAddress, Length, Attributes, PageActionAssign, AllocatePagesFunc, &IsSplitted, &IsModified);\r
835 if (!EFI_ERROR(Status)) {\r
836 if ((PagingContext == NULL) && IsModified) {\r
837 //\r
838 // Flush TLB as last step\r
839 //\r
840 CpuFlushTlb();\r
841 SyncMemoryPageAttributesAp (SyncCpuFlushTlb);\r
842 }\r
843 }\r
844\r
845 return Status;\r
846}\r
847\r
768bd967
JW
848/**\r
849 Check if Execute Disable feature is enabled or not.\r
850**/\r
851BOOLEAN\r
852IsExecuteDisableEnabled (\r
853 VOID\r
854 )\r
855{\r
856 MSR_CORE_IA32_EFER_REGISTER MsrEfer;\r
857\r
858 MsrEfer.Uint64 = AsmReadMsr64 (MSR_IA32_EFER);\r
859 return (MsrEfer.Bits.NXE == 1);\r
860}\r
861\r
c1cab54c
JW
862/**\r
863 Update GCD memory space attributes according to current page table setup.\r
864**/\r
865VOID\r
866RefreshGcdMemoryAttributesFromPaging (\r
867 VOID\r
868 )\r
869{\r
870 EFI_STATUS Status;\r
871 UINTN NumberOfDescriptors;\r
872 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
873 PAGE_TABLE_LIB_PAGING_CONTEXT PagingContext;\r
874 PAGE_ATTRIBUTE PageAttribute;\r
875 UINT64 *PageEntry;\r
876 UINT64 PageLength;\r
877 UINT64 MemorySpaceLength;\r
878 UINT64 Length;\r
879 UINT64 BaseAddress;\r
880 UINT64 PageStartAddress;\r
881 UINT64 Attributes;\r
882 UINT64 Capabilities;\r
768bd967 883 UINT64 NewAttributes;\r
c1cab54c
JW
884 UINTN Index;\r
885\r
886 //\r
887 // Assuming that memory space map returned is sorted already; otherwise sort\r
888 // them in the order of lowest address to highest address.\r
889 //\r
890 Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
891 ASSERT_EFI_ERROR (Status);\r
892\r
893 GetCurrentPagingContext (&PagingContext);\r
894\r
768bd967
JW
895 Attributes = 0;\r
896 NewAttributes = 0;\r
897 BaseAddress = 0;\r
898 PageLength = 0;\r
899\r
900 if (IsExecuteDisableEnabled ()) {\r
901 Capabilities = EFI_MEMORY_RO | EFI_MEMORY_RP | EFI_MEMORY_XP;\r
902 } else {\r
903 Capabilities = EFI_MEMORY_RO | EFI_MEMORY_RP;\r
904 }\r
96207191 905\r
c1cab54c
JW
906 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
907 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
908 continue;\r
909 }\r
910\r
768bd967
JW
911 //\r
912 // Sync the actual paging related capabilities back to GCD service first.\r
913 // As a side effect (good one), this can also help to avoid unnecessary\r
914 // memory map entries due to the different capabilities of the same type\r
915 // memory, such as multiple RT_CODE and RT_DATA entries in memory map,\r
916 // which could cause boot failure of some old Linux distro (before v4.3).\r
917 //\r
918 Status = gDS->SetMemorySpaceCapabilities (\r
919 MemorySpaceMap[Index].BaseAddress,\r
920 MemorySpaceMap[Index].Length,\r
921 MemorySpaceMap[Index].Capabilities | Capabilities\r
922 );\r
923 if (EFI_ERROR (Status)) {\r
924 //\r
925 // If we cannot udpate the capabilities, we cannot update its\r
926 // attributes either. So just simply skip current block of memory.\r
927 //\r
928 DEBUG ((\r
929 DEBUG_WARN,\r
930 "Failed to update capability: [%lu] %016lx - %016lx (%016lx -> %016lx)\r\n",\r
931 (UINT64)Index, MemorySpaceMap[Index].BaseAddress,\r
932 MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - 1,\r
933 MemorySpaceMap[Index].Capabilities,\r
934 MemorySpaceMap[Index].Capabilities | Capabilities\r
935 ));\r
936 continue;\r
937 }\r
938\r
c1cab54c
JW
939 if (MemorySpaceMap[Index].BaseAddress >= (BaseAddress + PageLength)) {\r
940 //\r
941 // Current memory space starts at a new page. Resetting PageLength will\r
942 // trigger a retrieval of page attributes at new address.\r
943 //\r
944 PageLength = 0;\r
945 } else {\r
946 //\r
947 // In case current memory space is not adjacent to last one\r
948 //\r
949 PageLength -= (MemorySpaceMap[Index].BaseAddress - BaseAddress);\r
950 }\r
951\r
768bd967
JW
952 //\r
953 // Sync actual page attributes to GCD\r
954 //\r
c1cab54c
JW
955 BaseAddress = MemorySpaceMap[Index].BaseAddress;\r
956 MemorySpaceLength = MemorySpaceMap[Index].Length;\r
957 while (MemorySpaceLength > 0) {\r
958 if (PageLength == 0) {\r
959 PageEntry = GetPageTableEntry (&PagingContext, BaseAddress, &PageAttribute);\r
960 if (PageEntry == NULL) {\r
961 break;\r
962 }\r
963\r
964 //\r
965 // Note current memory space might start in the middle of a page\r
966 //\r
967 PageStartAddress = (*PageEntry) & (UINT64)PageAttributeToMask(PageAttribute);\r
968 PageLength = PageAttributeToLength (PageAttribute) - (BaseAddress - PageStartAddress);\r
969 Attributes = GetAttributesFromPageEntry (PageEntry);\r
c1cab54c
JW
970 }\r
971\r
972 Length = MIN (PageLength, MemorySpaceLength);\r
768bd967
JW
973 if (Attributes != (MemorySpaceMap[Index].Attributes &\r
974 EFI_MEMORY_PAGETYPE_MASK)) {\r
975 NewAttributes = (MemorySpaceMap[Index].Attributes &\r
976 ~EFI_MEMORY_PAGETYPE_MASK) | Attributes;\r
977 Status = gDS->SetMemorySpaceAttributes (\r
978 BaseAddress,\r
979 Length,\r
980 NewAttributes\r
981 );\r
982 ASSERT_EFI_ERROR (Status);\r
983 DEBUG ((\r
fbe2c4b9 984 DEBUG_VERBOSE,\r
768bd967
JW
985 "Updated memory space attribute: [%lu] %016lx - %016lx (%016lx -> %016lx)\r\n",\r
986 (UINT64)Index, BaseAddress, BaseAddress + Length - 1,\r
987 MemorySpaceMap[Index].Attributes,\r
988 NewAttributes\r
989 ));\r
c1cab54c
JW
990 }\r
991\r
992 PageLength -= Length;\r
993 MemorySpaceLength -= Length;\r
994 BaseAddress += Length;\r
995 }\r
996 }\r
997\r
998 FreePool (MemorySpaceMap);\r
999}\r
1000\r
147fd35c
JW
1001/**\r
1002 Initialize a buffer pool for page table use only.\r
1003\r
1004 To reduce the potential split operation on page table, the pages reserved for\r
1005 page table should be allocated in the times of PAGE_TABLE_POOL_UNIT_PAGES and\r
1006 at the boundary of PAGE_TABLE_POOL_ALIGNMENT. So the page pool is always\r
1007 initialized with number of pages greater than or equal to the given PoolPages.\r
1008\r
1009 Once the pages in the pool are used up, this method should be called again to\r
1010 reserve at least another PAGE_TABLE_POOL_UNIT_PAGES. Usually this won't happen\r
1011 often in practice.\r
1012\r
1013 @param[in] PoolPages The least page number of the pool to be created.\r
1014\r
1015 @retval TRUE The pool is initialized successfully.\r
1016 @retval FALSE The memory is out of resource.\r
1017**/\r
1018BOOLEAN\r
1019InitializePageTablePool (\r
1020 IN UINTN PoolPages\r
1021 )\r
1022{\r
1023 VOID *Buffer;\r
1024 BOOLEAN IsModified;\r
1025\r
1026 //\r
1027 // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one page for\r
1028 // header.\r
1029 //\r
1030 PoolPages += 1; // Add one page for header.\r
1031 PoolPages = ((PoolPages - 1) / PAGE_TABLE_POOL_UNIT_PAGES + 1) *\r
1032 PAGE_TABLE_POOL_UNIT_PAGES;\r
1033 Buffer = AllocateAlignedPages (PoolPages, PAGE_TABLE_POOL_ALIGNMENT);\r
1034 if (Buffer == NULL) {\r
1035 DEBUG ((DEBUG_ERROR, "ERROR: Out of aligned pages\r\n"));\r
1036 return FALSE;\r
1037 }\r
1038\r
1039 //\r
1040 // Link all pools into a list for easier track later.\r
1041 //\r
1042 if (mPageTablePool == NULL) {\r
1043 mPageTablePool = Buffer;\r
1044 mPageTablePool->NextPool = mPageTablePool;\r
1045 } else {\r
1046 ((PAGE_TABLE_POOL *)Buffer)->NextPool = mPageTablePool->NextPool;\r
1047 mPageTablePool->NextPool = Buffer;\r
1048 mPageTablePool = Buffer;\r
1049 }\r
1050\r
1051 //\r
1052 // Reserve one page for pool header.\r
1053 //\r
1054 mPageTablePool->FreePages = PoolPages - 1;\r
1055 mPageTablePool->Offset = EFI_PAGES_TO_SIZE (1);\r
1056\r
1057 //\r
1058 // Mark the whole pool pages as read-only.\r
1059 //\r
1060 ConvertMemoryPageAttributes (\r
1061 NULL,\r
1062 (PHYSICAL_ADDRESS)(UINTN)Buffer,\r
1063 EFI_PAGES_TO_SIZE (PoolPages),\r
1064 EFI_MEMORY_RO,\r
1065 PageActionSet,\r
1066 AllocatePageTableMemory,\r
1067 NULL,\r
1068 &IsModified\r
1069 );\r
1070 ASSERT (IsModified == TRUE);\r
1071\r
1072 return TRUE;\r
1073}\r
1074\r
1075/**\r
1076 This API provides a way to allocate memory for page table.\r
1077\r
1078 This API can be called more than once to allocate memory for page tables.\r
1079\r
1080 Allocates the number of 4KB pages and returns a pointer to the allocated\r
1081 buffer. The buffer returned is aligned on a 4KB boundary.\r
1082\r
1083 If Pages is 0, then NULL is returned.\r
1084 If there is not enough memory remaining to satisfy the request, then NULL is\r
1085 returned.\r
1086\r
1087 @param Pages The number of 4 KB pages to allocate.\r
1088\r
1089 @return A pointer to the allocated buffer or NULL if allocation fails.\r
1090\r
1091**/\r
1092VOID *\r
1093EFIAPI\r
1094AllocatePageTableMemory (\r
1095 IN UINTN Pages\r
1096 )\r
1097{\r
1098 VOID *Buffer;\r
1099\r
1100 if (Pages == 0) {\r
1101 return NULL;\r
1102 }\r
1103\r
1104 //\r
1105 // Renew the pool if necessary.\r
1106 //\r
1107 if (mPageTablePool == NULL ||\r
1108 Pages > mPageTablePool->FreePages) {\r
1109 if (!InitializePageTablePool (Pages)) {\r
1110 return NULL;\r
1111 }\r
1112 }\r
1113\r
1114 Buffer = (UINT8 *)mPageTablePool + mPageTablePool->Offset;\r
1115\r
1116 mPageTablePool->Offset += EFI_PAGES_TO_SIZE (Pages);\r
1117 mPageTablePool->FreePages -= Pages;\r
1118\r
1119 return Buffer;\r
1120}\r
1121\r
22292ed3
JY
1122/**\r
1123 Initialize the Page Table lib.\r
1124**/\r
1125VOID\r
1126InitializePageTableLib (\r
1127 VOID\r
1128 )\r
1129{\r
1130 PAGE_TABLE_LIB_PAGING_CONTEXT CurrentPagingContext;\r
1131\r
1132 GetCurrentPagingContext (&CurrentPagingContext);\r
147fd35c
JW
1133\r
1134 //\r
1135 // Reserve memory of page tables for future uses, if paging is enabled.\r
1136 //\r
1137 if (CurrentPagingContext.ContextData.X64.PageTableBase != 0 &&\r
1138 (CurrentPagingContext.ContextData.Ia32.Attributes &\r
1139 PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE) != 0) {\r
1140 DisableReadOnlyPageWriteProtect ();\r
1141 InitializePageTablePool (1);\r
1142 EnableReadOnlyPageWriteProtect ();\r
1143 }\r
1144\r
22292ed3
JY
1145 DEBUG ((DEBUG_INFO, "CurrentPagingContext:\n", CurrentPagingContext.MachineType));\r
1146 DEBUG ((DEBUG_INFO, " MachineType - 0x%x\n", CurrentPagingContext.MachineType));\r
1147 DEBUG ((DEBUG_INFO, " PageTableBase - 0x%x\n", CurrentPagingContext.ContextData.X64.PageTableBase));\r
1148 DEBUG ((DEBUG_INFO, " Attributes - 0x%x\n", CurrentPagingContext.ContextData.X64.Attributes));\r
1149\r
1150 return ;\r
1151}\r
1152\r