]>
Commit | Line | Data |
---|---|---|
f3b33289 | 1 | /** @file\r |
d1102dba | 2 | x64 Long Mode Virtual Memory Management Definitions\r |
f3b33289 | 3 | \r |
4 | References:\r | |
48557c65 | 5 | 1) IA-32 Intel(R) Architecture Software Developer's Manual Volume 1:Basic Architecture, Intel\r |
6 | 2) IA-32 Intel(R) Architecture Software Developer's Manual Volume 2:Instruction Set Reference, Intel\r | |
7 | 3) IA-32 Intel(R) Architecture Software Developer's Manual Volume 3:System Programmer's Guide, Intel\r | |
f3b33289 | 8 | 4) AMD64 Architecture Programmer's Manual Volume 2: System Programming\r |
9 | \r | |
d1102dba | 10 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r |
5997daf7 LD |
11 | Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r |
12 | \r | |
9d510e61 | 13 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
f3b33289 | 14 | \r |
d1102dba | 15 | **/\r |
1436aea4 | 16 | \r |
f3b33289 | 17 | #ifndef _VIRTUAL_MEMORY_H_\r |
18 | #define _VIRTUAL_MEMORY_H_\r | |
19 | \r | |
1436aea4 | 20 | #define SYS_CODE64_SEL 0x38\r |
5997daf7 | 21 | \r |
f3b33289 | 22 | #pragma pack(1)\r |
23 | \r | |
24 | typedef union {\r | |
25 | struct {\r | |
1436aea4 MK |
26 | UINT32 LimitLow : 16;\r |
27 | UINT32 BaseLow : 16;\r | |
28 | UINT32 BaseMid : 8;\r | |
29 | UINT32 Type : 4;\r | |
30 | UINT32 System : 1;\r | |
31 | UINT32 Dpl : 2;\r | |
32 | UINT32 Present : 1;\r | |
33 | UINT32 LimitHigh : 4;\r | |
34 | UINT32 Software : 1;\r | |
35 | UINT32 Reserved : 1;\r | |
36 | UINT32 DefaultSize : 1;\r | |
37 | UINT32 Granularity : 1;\r | |
38 | UINT32 BaseHigh : 8;\r | |
f3b33289 | 39 | } Bits;\r |
1436aea4 | 40 | UINT64 Uint64;\r |
f3b33289 | 41 | } IA32_GDT;\r |
42 | \r | |
43 | typedef struct {\r | |
1436aea4 MK |
44 | IA32_IDT_GATE_DESCRIPTOR Ia32IdtEntry;\r |
45 | UINT32 Offset32To63;\r | |
46 | UINT32 Reserved;\r | |
f3b33289 | 47 | } X64_IDT_GATE_DESCRIPTOR;\r |
48 | \r | |
49 | //\r | |
50 | // Page-Map Level-4 Offset (PML4) and\r | |
51 | // Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB\r | |
52 | //\r | |
53 | \r | |
54 | typedef union {\r | |
55 | struct {\r | |
1436aea4 MK |
56 | UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory\r |
57 | UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write\r | |
58 | UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User\r | |
59 | UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching\r | |
60 | UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached\r | |
61 | UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)\r | |
62 | UINT64 Reserved : 1; // Reserved\r | |
63 | UINT64 MustBeZero : 2; // Must Be Zero\r | |
64 | UINT64 Available : 3; // Available for use by system software\r | |
65 | UINT64 PageTableBaseAddress : 40; // Page Table Base Address\r | |
66 | UINT64 AvabilableHigh : 11; // Available for use by system software\r | |
67 | UINT64 Nx : 1; // No Execute bit\r | |
f3b33289 | 68 | } Bits;\r |
69 | UINT64 Uint64;\r | |
70 | } PAGE_MAP_AND_DIRECTORY_POINTER;\r | |
71 | \r | |
5630cdfe SZ |
72 | //\r |
73 | // Page Table Entry 4KB\r | |
74 | //\r | |
75 | typedef union {\r | |
76 | struct {\r | |
1436aea4 MK |
77 | UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory\r |
78 | UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write\r | |
79 | UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User\r | |
80 | UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching\r | |
81 | UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached\r | |
82 | UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)\r | |
83 | UINT64 Dirty : 1; // 0 = Not Dirty, 1 = written by processor on access to page\r | |
84 | UINT64 PAT : 1; //\r | |
85 | UINT64 Global : 1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write\r | |
86 | UINT64 Available : 3; // Available for use by system software\r | |
87 | UINT64 PageTableBaseAddress : 40; // Page Table Base Address\r | |
88 | UINT64 AvabilableHigh : 11; // Available for use by system software\r | |
89 | UINT64 Nx : 1; // 0 = Execute Code, 1 = No Code Execution\r | |
5630cdfe SZ |
90 | } Bits;\r |
91 | UINT64 Uint64;\r | |
92 | } PAGE_TABLE_4K_ENTRY;\r | |
93 | \r | |
f3b33289 | 94 | //\r |
95 | // Page Table Entry 2MB\r | |
96 | //\r | |
97 | typedef union {\r | |
98 | struct {\r | |
1436aea4 MK |
99 | UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory\r |
100 | UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write\r | |
101 | UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User\r | |
102 | UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching\r | |
103 | UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached\r | |
104 | UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)\r | |
105 | UINT64 Dirty : 1; // 0 = Not Dirty, 1 = written by processor on access to page\r | |
106 | UINT64 MustBe1 : 1; // Must be 1\r | |
107 | UINT64 Global : 1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write\r | |
108 | UINT64 Available : 3; // Available for use by system software\r | |
109 | UINT64 PAT : 1; //\r | |
110 | UINT64 MustBeZero : 8; // Must be zero;\r | |
111 | UINT64 PageTableBaseAddress : 31; // Page Table Base Address\r | |
112 | UINT64 AvabilableHigh : 11; // Available for use by system software\r | |
113 | UINT64 Nx : 1; // 0 = Execute Code, 1 = No Code Execution\r | |
f3b33289 | 114 | } Bits;\r |
115 | UINT64 Uint64;\r | |
116 | } PAGE_TABLE_ENTRY;\r | |
117 | \r | |
c56b6566 JY |
118 | //\r |
119 | // Page Table Entry 1GB\r | |
120 | //\r | |
121 | typedef union {\r | |
122 | struct {\r | |
1436aea4 MK |
123 | UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory\r |
124 | UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write\r | |
125 | UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User\r | |
126 | UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching\r | |
127 | UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached\r | |
128 | UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)\r | |
129 | UINT64 Dirty : 1; // 0 = Not Dirty, 1 = written by processor on access to page\r | |
130 | UINT64 MustBe1 : 1; // Must be 1\r | |
131 | UINT64 Global : 1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write\r | |
132 | UINT64 Available : 3; // Available for use by system software\r | |
133 | UINT64 PAT : 1; //\r | |
134 | UINT64 MustBeZero : 17; // Must be zero;\r | |
135 | UINT64 PageTableBaseAddress : 22; // Page Table Base Address\r | |
136 | UINT64 AvabilableHigh : 11; // Available for use by system software\r | |
137 | UINT64 Nx : 1; // 0 = Execute Code, 1 = No Code Execution\r | |
c56b6566 JY |
138 | } Bits;\r |
139 | UINT64 Uint64;\r | |
140 | } PAGE_TABLE_1G_ENTRY;\r | |
141 | \r | |
f3b33289 | 142 | #pragma pack()\r |
143 | \r | |
1436aea4 | 144 | #define CR0_WP BIT16\r |
2ac1730b | 145 | \r |
1436aea4 MK |
146 | #define IA32_PG_P BIT0\r |
147 | #define IA32_PG_RW BIT1\r | |
148 | #define IA32_PG_PS BIT7\r | |
2ac1730b | 149 | \r |
1436aea4 | 150 | #define PAGING_PAE_INDEX_MASK 0x1FF\r |
5630cdfe | 151 | \r |
1436aea4 MK |
152 | #define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull\r |
153 | #define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull\r | |
154 | #define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull\r | |
5997daf7 | 155 | \r |
1436aea4 MK |
156 | #define PAGING_L1_ADDRESS_SHIFT 12\r |
157 | #define PAGING_L2_ADDRESS_SHIFT 21\r | |
158 | #define PAGING_L3_ADDRESS_SHIFT 30\r | |
159 | #define PAGING_L4_ADDRESS_SHIFT 39\r | |
2ac1730b | 160 | \r |
1436aea4 | 161 | #define PAGING_PML4E_NUMBER 4\r |
2ac1730b JW |
162 | \r |
163 | #define PAGE_TABLE_POOL_ALIGNMENT BASE_2MB\r | |
164 | #define PAGE_TABLE_POOL_UNIT_SIZE SIZE_2MB\r | |
165 | #define PAGE_TABLE_POOL_UNIT_PAGES EFI_SIZE_TO_PAGES (PAGE_TABLE_POOL_UNIT_SIZE)\r | |
166 | #define PAGE_TABLE_POOL_ALIGN_MASK \\r | |
167 | (~(EFI_PHYSICAL_ADDRESS)(PAGE_TABLE_POOL_ALIGNMENT - 1))\r | |
168 | \r | |
169 | typedef struct {\r | |
1436aea4 MK |
170 | VOID *NextPool;\r |
171 | UINTN Offset;\r | |
172 | UINTN FreePages;\r | |
2ac1730b JW |
173 | } PAGE_TABLE_POOL;\r |
174 | \r | |
52679261 JW |
175 | /**\r |
176 | Check if Execute Disable Bit (IA32_EFER.NXE) should be enabled or not.\r | |
177 | \r | |
178 | @retval TRUE IA32_EFER.NXE should be enabled.\r | |
179 | @retval FALSE IA32_EFER.NXE should not be enabled.\r | |
180 | \r | |
181 | **/\r | |
182 | BOOLEAN\r | |
183 | IsEnableNonExecNeeded (\r | |
184 | VOID\r | |
185 | );\r | |
186 | \r | |
5630cdfe SZ |
187 | /**\r |
188 | Enable Execute Disable Bit.\r | |
189 | \r | |
190 | **/\r | |
191 | VOID\r | |
192 | EnableExecuteDisableBit (\r | |
193 | VOID\r | |
194 | );\r | |
195 | \r | |
196 | /**\r | |
197 | Split 2M page to 4K.\r | |
198 | \r | |
199 | @param[in] PhysicalAddress Start physical address the 2M page covered.\r | |
200 | @param[in, out] PageEntry2M Pointer to 2M page entry.\r | |
201 | @param[in] StackBase Stack base address.\r | |
202 | @param[in] StackSize Stack size.\r | |
b098f5e9 TL |
203 | @param[in] GhcbBase GHCB page area base address.\r |
204 | @param[in] GhcbSize GHCB page area size.\r | |
5630cdfe SZ |
205 | \r |
206 | **/\r | |
207 | VOID\r | |
208 | Split2MPageTo4K (\r | |
1436aea4 MK |
209 | IN EFI_PHYSICAL_ADDRESS PhysicalAddress,\r |
210 | IN OUT UINT64 *PageEntry2M,\r | |
211 | IN EFI_PHYSICAL_ADDRESS StackBase,\r | |
212 | IN UINTN StackSize,\r | |
213 | IN EFI_PHYSICAL_ADDRESS GhcbBase,\r | |
214 | IN UINTN GhcbSize\r | |
5630cdfe | 215 | );\r |
f3b33289 | 216 | \r |
f3b33289 | 217 | /**\r |
218 | Allocates and fills in the Page Directory and Page Table Entries to\r | |
219 | establish a 1:1 Virtual to Physical mapping.\r | |
220 | \r | |
5630cdfe SZ |
221 | @param[in] StackBase Stack base address.\r |
222 | @param[in] StackSize Stack size.\r | |
b098f5e9 TL |
223 | @param[in] GhcbBase GHCB page area base address.\r |
224 | @param[in] GhcbSize GHCB page area size.\r | |
f3b33289 | 225 | \r |
48557c65 | 226 | @return The address of 4 level page map.\r |
f3b33289 | 227 | \r |
228 | **/\r | |
229 | UINTN\r | |
230 | CreateIdentityMappingPageTables (\r | |
1436aea4 MK |
231 | IN EFI_PHYSICAL_ADDRESS StackBase,\r |
232 | IN UINTN StackSize,\r | |
233 | IN EFI_PHYSICAL_ADDRESS GhcbBase,\r | |
234 | IN UINTN GhcbkSize\r | |
f3b33289 | 235 | );\r |
236 | \r | |
f3b33289 | 237 | /**\r |
d1102dba | 238 | \r |
f3b33289 | 239 | Fix up the vector number in the vector code.\r |
d1102dba | 240 | \r |
f3b33289 | 241 | @param VectorBase Base address of the vector handler.\r |
f3b33289 | 242 | @param VectorNum Index of vector.\r |
243 | \r | |
244 | **/\r | |
245 | VOID\r | |
246 | EFIAPI\r | |
247 | AsmVectorFixup (\r | |
1436aea4 MK |
248 | VOID *VectorBase,\r |
249 | UINT8 VectorNum\r | |
f3b33289 | 250 | );\r |
251 | \r | |
f3b33289 | 252 | /**\r |
d1102dba | 253 | \r |
f3b33289 | 254 | Get the information of vector template.\r |
d1102dba | 255 | \r |
f3b33289 | 256 | @param TemplateBase Base address of the template code.\r |
d1102dba | 257 | \r |
f3b33289 | 258 | @return Size of the Template code.\r |
259 | \r | |
260 | **/\r | |
261 | UINTN\r | |
262 | EFIAPI\r | |
263 | AsmGetVectorTemplatInfo (\r | |
264 | OUT VOID **TemplateBase\r | |
265 | );\r | |
266 | \r | |
ac733f31 SZ |
267 | /**\r |
268 | Clear legacy memory located at the first 4K-page.\r | |
269 | \r | |
270 | This function traverses the whole HOB list to check if memory from 0 to 4095\r | |
271 | exists and has not been allocated, and then clear it if so.\r | |
272 | \r | |
273 | @param HobStart The start of HobList passed to DxeCore.\r | |
274 | \r | |
275 | **/\r | |
276 | VOID\r | |
277 | ClearFirst4KPage (\r | |
1436aea4 | 278 | IN VOID *HobStart\r |
ac733f31 SZ |
279 | );\r |
280 | \r | |
281 | /**\r | |
282 | Return configure status of NULL pointer detection feature.\r | |
283 | \r | |
284 | @return TRUE NULL pointer detection feature is enabled\r | |
285 | @return FALSE NULL pointer detection feature is disabled\r | |
286 | **/\r | |
287 | BOOLEAN\r | |
288 | IsNullDetectionEnabled (\r | |
289 | VOID\r | |
290 | );\r | |
f3b33289 | 291 | \r |
2ac1730b JW |
292 | /**\r |
293 | Prevent the memory pages used for page table from been overwritten.\r | |
294 | \r | |
295 | @param[in] PageTableBase Base address of page table (CR3).\r | |
296 | @param[in] Level4Paging Level 4 paging flag.\r | |
297 | \r | |
298 | **/\r | |
299 | VOID\r | |
300 | EnablePageTableProtection (\r | |
1436aea4 MK |
301 | IN UINTN PageTableBase,\r |
302 | IN BOOLEAN Level4Paging\r | |
2ac1730b JW |
303 | );\r |
304 | \r | |
305 | /**\r | |
306 | This API provides a way to allocate memory for page table.\r | |
307 | \r | |
308 | This API can be called more than once to allocate memory for page tables.\r | |
309 | \r | |
310 | Allocates the number of 4KB pages and returns a pointer to the allocated\r | |
311 | buffer. The buffer returned is aligned on a 4KB boundary.\r | |
312 | \r | |
313 | If Pages is 0, then NULL is returned.\r | |
314 | If there is not enough memory remaining to satisfy the request, then NULL is\r | |
315 | returned.\r | |
316 | \r | |
317 | @param Pages The number of 4 KB pages to allocate.\r | |
318 | \r | |
319 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
320 | \r | |
321 | **/\r | |
322 | VOID *\r | |
323 | AllocatePageTableMemory (\r | |
1436aea4 | 324 | IN UINTN Pages\r |
2ac1730b JW |
325 | );\r |
326 | \r | |
d1102dba | 327 | #endif\r |