+++ /dev/null
-/** @file\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
- Paging.c\r
-\r
-Abstract:\r
-\r
-Revision History:\r
-\r
-**/\r
-\r
-#include "HobGeneration.h"\r
-#include "VirtualMemory.h"\r
-\r
-//\r
-// Create 2M-page table\r
-// PML4 (47:39)\r
-// PDPTE (38:30)\r
-// PDE (29:21)\r
-//\r
-\r
-#define EFI_2M_PAGE_BITS_NUM 21\r
-#define EFI_MAX_ENTRY_BITS_NUM 9\r
-\r
-#define EFI_PAGE_SIZE_4K 0x1000\r
-#define EFI_PAGE_SIZE_2M (1 << EFI_2M_PAGE_BITS_NUM)\r
-\r
-#ifndef MIN\r
- #define MIN(a, b) ((a) < (b) ? (a) : (b))\r
-#endif\r
-#define ENTRY_NUM(x) ((UINTN)1 << (x))\r
-\r
-UINT8 gPML4BitsNum;\r
-UINT8 gPDPTEBitsNum;\r
-UINT8 gPDEBitsNum;\r
-\r
-UINTN gPageNum2M;\r
-UINTN gPageNum4K;\r
-\r
-VOID\r
-EnableNullPointerProtection (\r
- UINT8 *PageTable\r
- )\r
-{\r
- X64_PAGE_TABLE_ENTRY_4K *PageTableEntry4KB;\r
-\r
- PageTableEntry4KB = (X64_PAGE_TABLE_ENTRY_4K *) (PageTable + gPageNum2M * EFI_PAGE_SIZE_4K);\r
- //\r
- // Fill in the Page Table entries\r
- // Mark 0~4K as not present\r
- //\r
- PageTableEntry4KB->Bits.Present = 0;\r
-\r
- return ;\r
-}\r
-\r
-VOID\r
-X64Create4KPageTables (\r
- UINT8 *PageTable\r
- )\r
-/*++\r
-Routine Description:\r
- Create 4K-Page-Table for the low 2M memory.\r
- This will change the previously created 2M-Page-Table-Entry.\r
---*/\r
-{\r
- UINT64 PageAddress;\r
- UINTN PTEIndex;\r
- X64_PAGE_DIRECTORY_ENTRY_4K *PageDirectoryEntry4KB;\r
- X64_PAGE_TABLE_ENTRY_4K *PageTableEntry4KB;\r
-\r
- //\r
- // Page Table structure 4 level 4K.\r
- //\r
- // PageMapLevel4Entry : bits 47-39\r
- // PageDirectoryPointerEntry : bits 38-30\r
- // Page Table 4K : PageDirectoryEntry4K : bits 29-21\r
- // PageTableEntry : bits 20-12\r
- //\r
-\r
- PageTableEntry4KB = (X64_PAGE_TABLE_ENTRY_4K *)(PageTable + gPageNum2M * EFI_PAGE_SIZE_4K);\r
-\r
- PageDirectoryEntry4KB = (X64_PAGE_DIRECTORY_ENTRY_4K *) (PageTable + 2 * EFI_PAGE_SIZE_4K);\r
- PageDirectoryEntry4KB->Uint64 = (UINT64)(UINTN)PageTableEntry4KB;\r
- PageDirectoryEntry4KB->Bits.ReadWrite = 1;\r
- PageDirectoryEntry4KB->Bits.Present = 1;\r
- PageDirectoryEntry4KB->Bits.MustBeZero = 0;\r
-\r
- for (PTEIndex = 0, PageAddress = 0; \r
- PTEIndex < ENTRY_NUM (EFI_MAX_ENTRY_BITS_NUM); \r
- PTEIndex++, PageTableEntry4KB++, PageAddress += EFI_PAGE_SIZE_4K\r
- ) {\r
- //\r
- // Fill in the Page Table entries\r
- //\r
- PageTableEntry4KB->Uint64 = (UINT64)PageAddress;\r
- PageTableEntry4KB->Bits.ReadWrite = 1;\r
- PageTableEntry4KB->Bits.Present = 1;\r
- }\r
-\r
- return ;\r
-}\r
-\r
-VOID\r
-X64Create2MPageTables (\r
- UINT8 *PageTable\r
- )\r
-{\r
- UINT64 PageAddress;\r
- UINT8 *TempPageTable;\r
- UINTN PML4Index;\r
- UINTN PDPTEIndex;\r
- UINTN PDEIndex;\r
- X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *PageMapLevel4Entry;\r
- X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *PageDirectoryPointerEntry;\r
- X64_PAGE_TABLE_ENTRY_2M *PageDirectoryEntry2MB;\r
-\r
- TempPageTable = PageTable;\r
- PageAddress = 0;\r
-\r
- //\r
- // Page Table structure 3 level 2MB.\r
- //\r
- // PageMapLevel4Entry : bits 47-39\r
- // PageDirectoryPointerEntry : bits 38-30\r
- // Page Table 2MB : PageDirectoryEntry2M : bits 29-21\r
- //\r
-\r
- PageMapLevel4Entry = (X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *)TempPageTable;\r
-\r
- for (PML4Index = 0; PML4Index < ENTRY_NUM (gPML4BitsNum); PML4Index++, PageMapLevel4Entry++) {\r
- //\r
- // Each PML4 entry points to a page of Page Directory Pointer entires.\r
- // \r
- TempPageTable += EFI_PAGE_SIZE_4K;\r
- PageDirectoryPointerEntry = (X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *)TempPageTable;\r
-\r
- //\r
- // Make a PML4 Entry\r
- //\r
- PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)(TempPageTable);\r
- PageMapLevel4Entry->Bits.ReadWrite = 1;\r
- PageMapLevel4Entry->Bits.Present = 1;\r
-\r
- for (PDPTEIndex = 0; PDPTEIndex < ENTRY_NUM (gPDPTEBitsNum); PDPTEIndex++, PageDirectoryPointerEntry++) {\r
- //\r
- // Each Directory Pointer entries points to a page of Page Directory entires.\r
- // \r
- TempPageTable += EFI_PAGE_SIZE_4K;\r
- PageDirectoryEntry2MB = (X64_PAGE_TABLE_ENTRY_2M *)TempPageTable;\r
-\r
- //\r
- // Fill in a Page Directory Pointer Entries\r
- //\r
- PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)(TempPageTable);\r
- PageDirectoryPointerEntry->Bits.ReadWrite = 1;\r
- PageDirectoryPointerEntry->Bits.Present = 1;\r
-\r
- for (PDEIndex = 0; PDEIndex < ENTRY_NUM (gPDEBitsNum); PDEIndex++, PageDirectoryEntry2MB++) {\r
- //\r
- // Fill in the Page Directory entries\r
- //\r
- PageDirectoryEntry2MB->Uint64 = (UINT64)PageAddress;\r
- PageDirectoryEntry2MB->Bits.ReadWrite = 1;\r
- PageDirectoryEntry2MB->Bits.Present = 1;\r
- PageDirectoryEntry2MB->Bits.MustBe1 = 1;\r
-\r
- PageAddress += EFI_PAGE_SIZE_2M;\r
- }\r
- }\r
- }\r
-\r
- return ;\r
-}\r
-\r
-VOID *\r
-PreparePageTable (\r
- VOID *PageNumberTop,\r
- UINT8 SizeOfMemorySpace\r
- )\r
-/*++\r
-Description:\r
- Generate pagetable below PageNumberTop, \r
- and return the bottom address of pagetable for putting other things later.\r
---*/\r
-{\r
- VOID *PageNumberBase;\r
-\r
- SizeOfMemorySpace -= EFI_2M_PAGE_BITS_NUM;\r
- gPDEBitsNum = (UINT8) MIN (SizeOfMemorySpace, EFI_MAX_ENTRY_BITS_NUM);\r
- SizeOfMemorySpace = (UINT8) (SizeOfMemorySpace - gPDEBitsNum);\r
- gPDPTEBitsNum = (UINT8) MIN (SizeOfMemorySpace, EFI_MAX_ENTRY_BITS_NUM);\r
- SizeOfMemorySpace = (UINT8) (SizeOfMemorySpace - gPDPTEBitsNum);\r
- gPML4BitsNum = SizeOfMemorySpace;\r
- if (gPML4BitsNum > EFI_MAX_ENTRY_BITS_NUM) {\r
- return NULL;\r
- }\r
-\r
- //\r
- // Suppose we have:\r
- // 2MPage:\r
- // Entry: PML4 -> PDPTE -> PDE -> Page\r
- // EntryNum: a b c\r
- // then\r
- // Occupy4KPage: 1 a a*b\r
- // \r
- // 2M 4KPage:\r
- // Entry: PTE -> Page\r
- // EntryNum: 512\r
- // then\r
- // Occupy4KPage: 1\r
- // \r
-\r
- gPageNum2M = 1 + ENTRY_NUM (gPML4BitsNum) + ENTRY_NUM (gPML4BitsNum + gPDPTEBitsNum);\r
- gPageNum4K = 1;\r
-\r
-\r
- PageNumberBase = (VOID *)((UINTN)PageNumberTop - (gPageNum2M + gPageNum4K) * EFI_PAGE_SIZE_4K);\r
- ZeroMem (PageNumberBase, (gPageNum2M + gPageNum4K) * EFI_PAGE_SIZE_4K);\r
-\r
- X64Create2MPageTables (PageNumberBase);\r
- X64Create4KPageTables (PageNumberBase);\r
- //\r
- // Not enable NULL Pointer Protection if using INTx call\r
- //\r
-// EnableNullPointerProtection (PageNumberBase);\r
-\r
- return PageNumberBase;\r
-}\r