]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Core/DxeIplPeim/Ia32/VirtualMemory.c
To fix
[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 UINT8 PhysicalAddressBits;\r
878ddf1f 61 EFI_PHYSICAL_ADDRESS PageAddress;\r
e4e7fed9 62 UINTN IndexOfPml4Entries;\r
63 UINTN IndexOfPdpEntries;\r
64 UINTN IndexOfPageDirectoryEntries;\r
65 UINTN NumberOfPml4EntriesNeeded;\r
66 UINTN NumberOfPdpEntriesNeeded;\r
67 PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;\r
68 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;\r
69 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;\r
70 PAGE_TABLE_ENTRY *PageDirectoryEntry;\r
e2d56e0a 71 UINTN TotalPagesNum;\r
72 UINTN BigPageAddress;\r
5d10269c 73 VOID *Hob;\r
878ddf1f 74\r
75 //\r
5d10269c 76 // Get physical address bits supported from CPU HOB.\r
878ddf1f 77 //\r
5d10269c 78 PhysicalAddressBits = 36;\r
79 \r
80 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);\r
81 if (Hob != NULL) {\r
82 PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace; \r
e4e7fed9 83 }\r
878ddf1f 84\r
85 //\r
e4e7fed9 86 // Calculate the table entries needed.\r
878ddf1f 87 //\r
e4e7fed9 88 if (PhysicalAddressBits <= 39 ) {\r
89 NumberOfPml4EntriesNeeded = 1;\r
90 NumberOfPdpEntriesNeeded = 1 << (PhysicalAddressBits - 30);\r
878ddf1f 91 } else {\r
e4e7fed9 92 NumberOfPml4EntriesNeeded = 1 << (PhysicalAddressBits - 39);\r
93 NumberOfPdpEntriesNeeded = 512;\r
878ddf1f 94 }\r
95\r
e2d56e0a 96 //\r
97 // Pre-allocate big pages to avoid later allocations. \r
98 //\r
99 TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;\r
100 BigPageAddress = (UINTN) AllocatePages (TotalPagesNum);\r
101 ASSERT (BigPageAddress != 0);\r
102\r
103 //\r
104 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.\r
105 //\r
106 PageMap = (VOID *) BigPageAddress;\r
107 BigPageAddress += EFI_PAGE_SIZE;\r
108\r
e4e7fed9 109 PageMapLevel4Entry = PageMap;\r
110 PageAddress = 0;\r
111 for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
878ddf1f 112 //\r
113 // Each PML4 entry points to a page of Page Directory Pointer entires.\r
e4e7fed9 114 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.\r
115 //\r
e2d56e0a 116 PageDirectoryPointerEntry = (VOID *) BigPageAddress;\r
117 BigPageAddress += EFI_PAGE_SIZE;\r
878ddf1f 118\r
119 //\r
120 // Make a PML4 Entry\r
121 //\r
122 PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry;\r
123 PageMapLevel4Entry->Bits.ReadWrite = 1;\r
124 PageMapLevel4Entry->Bits.Present = 1;\r
125\r
e4e7fed9 126 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
878ddf1f 127 //\r
128 // Each Directory Pointer entries points to a page of Page Directory entires.\r
e4e7fed9 129 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.\r
878ddf1f 130 // \r
e2d56e0a 131 PageDirectoryEntry = (VOID *) BigPageAddress;\r
132 BigPageAddress += EFI_PAGE_SIZE;\r
878ddf1f 133\r
134 //\r
135 // Fill in a Page Directory Pointer Entries\r
136 //\r
e4e7fed9 137 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry;\r
878ddf1f 138 PageDirectoryPointerEntry->Bits.ReadWrite = 1;\r
139 PageDirectoryPointerEntry->Bits.Present = 1;\r
140\r
e4e7fed9 141 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += 0x200000) {\r
878ddf1f 142 //\r
143 // Fill in the Page Directory entries\r
144 //\r
e4e7fed9 145 PageDirectoryEntry->Uint64 = (UINT64)PageAddress;\r
146 PageDirectoryEntry->Bits.ReadWrite = 1;\r
147 PageDirectoryEntry->Bits.Present = 1;\r
148 PageDirectoryEntry->Bits.MustBe1 = 1;\r
878ddf1f 149\r
878ddf1f 150 }\r
151 }\r
152 }\r
153\r
154 //\r
155 // For the PML4 entries we are not using fill in a null entry.\r
e4e7fed9 156 // For now we just copy the first entry.\r
157 //\r
158 for (; IndexOfPml4Entries < 512; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
159 CopyMem (\r
160 PageMapLevel4Entry,\r
161 PageMap,\r
162 sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)\r
163 );\r
878ddf1f 164 }\r
165\r
f40793be 166 return (UINTN)PageMap; // FIXME\r
878ddf1f 167}\r
168\r