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