]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Core/DxeIplPeim/Ia32/VirtualMemory.c
Add blank line at end of file to pass GCC build.
[mirror_edk2.git] / EdkModulePkg / Core / DxeIplPeim / Ia32 / VirtualMemory.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13 VirtualMemory.c\r
14 \r
15Abstract:\r
16\r
17 x64 Virtual Memory Management Services in the form of an IA-32 driver. \r
18 Used to establish a 1:1 Virtual to Physical Mapping that is required to\r
19 enter Long Mode (x64 64-bit mode).\r
20\r
21 While we make a 1:1 mapping (identity mapping) for all physical pages \r
22 we still need to use the MTRR's to ensure that the cachability attirbutes\r
23 for all memory regions is correct.\r
24\r
25 The basic idea is to use 2MB page table entries where ever possible. If\r
26 more granularity of cachability is required then 4K page tables are used.\r
27\r
28 References:\r
29 1) IA-32 Intel(R) Atchitecture Software Developer's Manual Volume 1:Basic Architecture, Intel\r
30 2) IA-32 Intel(R) Atchitecture Software Developer's Manual Volume 2:Instruction Set Reference, Intel\r
31 3) IA-32 Intel(R) Atchitecture Software Developer's Manual Volume 3:System Programmer's Guide, Intel\r
32 \r
33--*/ \r
34\r
35#include "VirtualMemory.h"\r
36\r
f40793be 37UINTN\r
878ddf1f 38CreateIdentityMappingPageTables (\r
e4e7fed9 39 VOID\r
878ddf1f 40 )\r
41/*++\r
42\r
43Routine Description:\r
44\r
45 Allocates and fills in the Page Directory and Page Table Entries to\r
e4e7fed9 46 establish a 1:1 Virtual to Physical mapping.\r
878ddf1f 47\r
48Arguments:\r
49\r
50 NumberOfProcessorPhysicalAddressBits - Number of processor address bits to use.\r
51 Limits the number of page table entries \r
52 to the physical address space.\r
53\r
54Returns:\r
878ddf1f 55\r
56 EFI_SUCCESS The 1:1 Virtual to Physical identity mapping was created\r
57\r
58--*/\r
59{ \r
e4e7fed9 60 UINT32 RegEax;\r
61 UINT8 PhysicalAddressBits;\r
878ddf1f 62 EFI_PHYSICAL_ADDRESS PageAddress;\r
e4e7fed9 63 UINTN IndexOfPml4Entries;\r
64 UINTN IndexOfPdpEntries;\r
65 UINTN IndexOfPageDirectoryEntries;\r
66 UINTN NumberOfPml4EntriesNeeded;\r
67 UINTN NumberOfPdpEntriesNeeded;\r
68 PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;\r
69 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;\r
70 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;\r
71 PAGE_TABLE_ENTRY *PageDirectoryEntry;\r
e2d56e0a 72 UINTN TotalPagesNum;\r
73 UINTN BigPageAddress;\r
878ddf1f 74\r
75 //\r
e4e7fed9 76 // Get physical address bits supported.\r
878ddf1f 77 //\r
e4e7fed9 78 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
79 if (RegEax >= 0x80000008) {\r
80 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
81 PhysicalAddressBits = (UINT8) RegEax;\r
82 } else {\r
83 PhysicalAddressBits = 36;\r
84 }\r
878ddf1f 85\r
86 //\r
e4e7fed9 87 // Calculate the table entries needed.\r
878ddf1f 88 //\r
e4e7fed9 89 if (PhysicalAddressBits <= 39 ) {\r
90 NumberOfPml4EntriesNeeded = 1;\r
91 NumberOfPdpEntriesNeeded = 1 << (PhysicalAddressBits - 30);\r
878ddf1f 92 } else {\r
e4e7fed9 93 NumberOfPml4EntriesNeeded = 1 << (PhysicalAddressBits - 39);\r
94 NumberOfPdpEntriesNeeded = 512;\r
878ddf1f 95 }\r
96\r
e2d56e0a 97 //\r
98 // Pre-allocate big pages to avoid later allocations. \r
99 //\r
100 TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;\r
101 BigPageAddress = (UINTN) AllocatePages (TotalPagesNum);\r
102 ASSERT (BigPageAddress != 0);\r
103\r
104 //\r
105 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.\r
106 //\r
107 PageMap = (VOID *) BigPageAddress;\r
108 BigPageAddress += EFI_PAGE_SIZE;\r
109\r
e4e7fed9 110 PageMapLevel4Entry = PageMap;\r
111 PageAddress = 0;\r
112 for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
878ddf1f 113 //\r
114 // Each PML4 entry points to a page of Page Directory Pointer entires.\r
e4e7fed9 115 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.\r
116 //\r
e2d56e0a 117 PageDirectoryPointerEntry = (VOID *) BigPageAddress;\r
118 BigPageAddress += EFI_PAGE_SIZE;\r
878ddf1f 119\r
120 //\r
121 // Make a PML4 Entry\r
122 //\r
123 PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry;\r
124 PageMapLevel4Entry->Bits.ReadWrite = 1;\r
125 PageMapLevel4Entry->Bits.Present = 1;\r
126\r
e4e7fed9 127 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
878ddf1f 128 //\r
129 // Each Directory Pointer entries points to a page of Page Directory entires.\r
e4e7fed9 130 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.\r
878ddf1f 131 // \r
e2d56e0a 132 PageDirectoryEntry = (VOID *) BigPageAddress;\r
133 BigPageAddress += EFI_PAGE_SIZE;\r
878ddf1f 134\r
135 //\r
136 // Fill in a Page Directory Pointer Entries\r
137 //\r
e4e7fed9 138 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry;\r
878ddf1f 139 PageDirectoryPointerEntry->Bits.ReadWrite = 1;\r
140 PageDirectoryPointerEntry->Bits.Present = 1;\r
141\r
e4e7fed9 142 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += 0x200000) {\r
878ddf1f 143 //\r
144 // Fill in the Page Directory entries\r
145 //\r
e4e7fed9 146 PageDirectoryEntry->Uint64 = (UINT64)PageAddress;\r
147 PageDirectoryEntry->Bits.ReadWrite = 1;\r
148 PageDirectoryEntry->Bits.Present = 1;\r
149 PageDirectoryEntry->Bits.MustBe1 = 1;\r
878ddf1f 150\r
878ddf1f 151 }\r
152 }\r
153 }\r
154\r
155 //\r
156 // For the PML4 entries we are not using fill in a null entry.\r
e4e7fed9 157 // For now we just copy the first entry.\r
158 //\r
159 for (; IndexOfPml4Entries < 512; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
160 CopyMem (\r
161 PageMapLevel4Entry,\r
162 PageMap,\r
163 sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)\r
164 );\r
878ddf1f 165 }\r
166\r
f40793be 167 return (UINTN)PageMap; // FIXME\r
878ddf1f 168}\r
169\r