]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/DxeIplPeim/Ia32/VirtualMemory.c
Update all files in DxeIpl Peim to follow doxygen style header.
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / Ia32 / VirtualMemory.c
CommitLineData
96226baa 1/** @file\r
95276127 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
96226baa 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
95276127 28\r
859b72fa 29#include "DxeIpl.h"\r
95276127 30#include "VirtualMemory.h"\r
31\r
859b72fa 32\r
95276127 33UINTN\r
34CreateIdentityMappingPageTables (\r
35 VOID\r
36 )\r
37/*++\r
38\r
39Routine Description:\r
40\r
41 Allocates and fills in the Page Directory and Page Table Entries to\r
42 establish a 1:1 Virtual to Physical mapping.\r
43\r
44Arguments:\r
45\r
46 NumberOfProcessorPhysicalAddressBits - Number of processor address bits to use.\r
47 Limits the number of page table entries \r
48 to the physical address space.\r
49\r
50Returns:\r
51\r
52 EFI_SUCCESS The 1:1 Virtual to Physical identity mapping was created\r
53\r
54--*/\r
55{ \r
56 UINT8 PhysicalAddressBits;\r
57 EFI_PHYSICAL_ADDRESS PageAddress;\r
58 UINTN IndexOfPml4Entries;\r
59 UINTN IndexOfPdpEntries;\r
60 UINTN IndexOfPageDirectoryEntries;\r
61 UINTN NumberOfPml4EntriesNeeded;\r
62 UINTN NumberOfPdpEntriesNeeded;\r
63 PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;\r
64 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;\r
65 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;\r
66 PAGE_TABLE_ENTRY *PageDirectoryEntry;\r
67 UINTN TotalPagesNum;\r
68 UINTN BigPageAddress;\r
69 VOID *Hob;\r
70\r
71 //\r
72 // Get physical address bits supported from CPU HOB.\r
73 //\r
74 PhysicalAddressBits = 36;\r
75 \r
76 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);\r
77 if (Hob != NULL) {\r
78 PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace; \r
79 }\r
80\r
81 //\r
82 // Calculate the table entries needed.\r
83 //\r
84 if (PhysicalAddressBits <= 39 ) {\r
85 NumberOfPml4EntriesNeeded = 1;\r
86 NumberOfPdpEntriesNeeded = 1 << (PhysicalAddressBits - 30);\r
87 } else {\r
88 NumberOfPml4EntriesNeeded = 1 << (PhysicalAddressBits - 39);\r
89 NumberOfPdpEntriesNeeded = 512;\r
90 }\r
91\r
92 //\r
93 // Pre-allocate big pages to avoid later allocations. \r
94 //\r
95 TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;\r
96 BigPageAddress = (UINTN) AllocatePages (TotalPagesNum);\r
97 ASSERT (BigPageAddress != 0);\r
98\r
99 //\r
100 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.\r
101 //\r
102 PageMap = (VOID *) BigPageAddress;\r
103 BigPageAddress += EFI_PAGE_SIZE;\r
104\r
105 PageMapLevel4Entry = PageMap;\r
106 PageAddress = 0;\r
107 for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
108 //\r
109 // Each PML4 entry points to a page of Page Directory Pointer entires.\r
110 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.\r
111 //\r
112 PageDirectoryPointerEntry = (VOID *) BigPageAddress;\r
113 BigPageAddress += EFI_PAGE_SIZE;\r
114\r
115 //\r
116 // Make a PML4 Entry\r
117 //\r
118 PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry;\r
119 PageMapLevel4Entry->Bits.ReadWrite = 1;\r
120 PageMapLevel4Entry->Bits.Present = 1;\r
121\r
122 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
123 //\r
124 // Each Directory Pointer entries points to a page of Page Directory entires.\r
125 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.\r
126 // \r
127 PageDirectoryEntry = (VOID *) BigPageAddress;\r
128 BigPageAddress += EFI_PAGE_SIZE;\r
129\r
130 //\r
131 // Fill in a Page Directory Pointer Entries\r
132 //\r
133 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry;\r
134 PageDirectoryPointerEntry->Bits.ReadWrite = 1;\r
135 PageDirectoryPointerEntry->Bits.Present = 1;\r
136\r
137 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += 0x200000) {\r
138 //\r
139 // Fill in the Page Directory entries\r
140 //\r
141 PageDirectoryEntry->Uint64 = (UINT64)PageAddress;\r
142 PageDirectoryEntry->Bits.ReadWrite = 1;\r
143 PageDirectoryEntry->Bits.Present = 1;\r
144 PageDirectoryEntry->Bits.MustBe1 = 1;\r
145\r
146 }\r
147 }\r
148 }\r
149\r
150 //\r
151 // For the PML4 entries we are not using fill in a null entry.\r
152 // For now we just copy the first entry.\r
153 //\r
154 for (; IndexOfPml4Entries < 512; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
155 CopyMem (\r
156 PageMapLevel4Entry,\r
157 PageMap,\r
158 sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)\r
159 );\r
160 }\r
161\r
162 return (UINTN)PageMap; // FIXME\r
163}\r
164\r