+ Pages = EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER));\r
+ AlignmentMask = SIZE_4MB - 1;\r
+ RealPages = Pages + EFI_SIZE_TO_PAGES (SIZE_4MB);\r
+\r
+ //\r
+ // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress\r
+ // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below\r
+ // MAX_ADDRESS\r
+ //\r
+ Memory = PcdGet64 (PcdMaxEfiSystemTablePointerAddress);\r
+ if (Memory == 0) {\r
+ Memory = MAX_ADDRESS;\r
+ }\r
+ Status = CoreAllocatePages (\r
+ AllocateMaxAddress, \r
+ EfiBootServicesData,\r
+ RealPages, \r
+ &Memory\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ if (PcdGet64 (PcdMaxEfiSystemTablePointerAddress) != 0) {\r
+ DEBUG ((EFI_D_INFO, "Allocate memory for EFI_SYSTEM_TABLE_POINTER below PcdMaxEfiSystemTablePointerAddress failed. \\r
+ Retry to allocate memroy as close to the top of memory as feasible.\n"));\r
+ }\r
+ //\r
+ // If the initial memory allocation fails, then reattempt allocation\r
+ // as close to the top of memory as feasible.\r
+ //\r
+ Status = CoreAllocatePages (\r
+ AllocateAnyPages, \r
+ EfiBootServicesData,\r
+ RealPages, \r
+ &Memory\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ if (EFI_ERROR (Status)) {\r
+ return;\r
+ }\r
+ } \r
+\r
+ //\r
+ // Free overallocated pages\r
+ //\r
+ AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
+ UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
+ if (UnalignedPages > 0) {\r
+ //\r
+ // Free first unaligned page(s).\r
+ //\r
+ Status = CoreFreePages (Memory, UnalignedPages);\r
+ ASSERT_EFI_ERROR (Status);\r
+ }\r
+ Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
+ UnalignedPages = RealPages - Pages - UnalignedPages;\r
+ if (UnalignedPages > 0) {\r
+ //\r
+ // Free last unaligned page(s).\r
+ //\r
+ Status = CoreFreePages (Memory, UnalignedPages);\r
+ ASSERT_EFI_ERROR (Status);\r
+ }\r
+\r
+ //\r
+ // Set mDebugTable to the 4MB aligned allocated pages\r
+ //\r
+ mDebugTable = (EFI_SYSTEM_TABLE_POINTER *)(AlignedMemory);\r