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