]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / Ia32 / DxeLoadFunc.c
CommitLineData
96226baa 1/** @file\r
48557c65 2 Ia32-specific functionality for DxeLoad.\r
95276127 3\r
d1102dba 4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
5997daf7
LD
5Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
9d510e61 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
95276127 8\r
96226baa 9**/\r
95276127 10\r
95276127 11#include "DxeIpl.h"\r
12#include "VirtualMemory.h"\r
13\r
df7aaeb9 14#define IDT_ENTRY_COUNT 32\r
e7af83ae 15\r
bdfbe63e 16typedef struct _X64_IDT_TABLE {\r
17 //\r
18 // Reserved 4 bytes preceding PeiService and IdtTable,\r
19 // since IDT base address should be 8-byte alignment.\r
20 //\r
21 UINT32 Reserved;\r
22 CONST EFI_PEI_SERVICES **PeiService;\r
23 X64_IDT_GATE_DESCRIPTOR IdtTable[IDT_ENTRY_COUNT];\r
24} X64_IDT_TABLE;\r
25\r
95276127 26//\r
27// Global Descriptor Table (GDT)\r
28//\r
b98da1b1 29GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT gGdtEntries[] = {\r
0cf27ce0 30/* selector { Global Segment Descriptor } */\r
31/* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //null descriptor\r
95276127 32/* 0x08 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear data segment descriptor\r
33/* 0x10 */ {{0xffff, 0, 0, 0xf, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear code segment descriptor\r
34/* 0x18 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor\r
35/* 0x20 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system code segment descriptor\r
36/* 0x28 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor\r
37/* 0x30 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor\r
38/* 0x38 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 1, 0, 1, 0}}, //system code segment descriptor\r
39/* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor\r
40};\r
41\r
42//\r
43// IA32 Gdt register\r
44//\r
45GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR gGdt = {\r
46 sizeof (gGdtEntries) - 1,\r
47 (UINTN) gGdtEntries\r
48 };\r
49\r
5d582956 50GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR gLidtDescriptor = {\r
e7af83ae 51 sizeof (X64_IDT_GATE_DESCRIPTOR) * IDT_ENTRY_COUNT - 1,\r
5d582956 52 0\r
53};\r
54\r
5630cdfe
SZ
55/**\r
56 Allocates and fills in the Page Directory and Page Table Entries to\r
57 establish a 4G page table.\r
58\r
59 @param[in] StackBase Stack base address.\r
60 @param[in] StackSize Stack size.\r
61\r
62 @return The address of page table.\r
63\r
64**/\r
65UINTN\r
66Create4GPageTablesIa32Pae (\r
67 IN EFI_PHYSICAL_ADDRESS StackBase,\r
68 IN UINTN StackSize\r
69 )\r
d1102dba 70{\r
5630cdfe
SZ
71 UINT8 PhysicalAddressBits;\r
72 EFI_PHYSICAL_ADDRESS PhysicalAddress;\r
73 UINTN IndexOfPdpEntries;\r
74 UINTN IndexOfPageDirectoryEntries;\r
75 UINT32 NumberOfPdpEntriesNeeded;\r
76 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;\r
77 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;\r
78 PAGE_TABLE_ENTRY *PageDirectoryEntry;\r
79 UINTN TotalPagesNum;\r
80 UINTN PageAddress;\r
5997daf7
LD
81 UINT64 AddressEncMask;\r
82\r
83 //\r
84 // Make sure AddressEncMask is contained to smallest supported address field\r
85 //\r
86 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;\r
5630cdfe
SZ
87\r
88 PhysicalAddressBits = 32;\r
89\r
90 //\r
91 // Calculate the table entries needed.\r
92 //\r
93 NumberOfPdpEntriesNeeded = (UINT32) LShiftU64 (1, (PhysicalAddressBits - 30));\r
94\r
95 TotalPagesNum = NumberOfPdpEntriesNeeded + 1;\r
2ac1730b 96 PageAddress = (UINTN) AllocatePageTableMemory (TotalPagesNum);\r
5630cdfe
SZ
97 ASSERT (PageAddress != 0);\r
98\r
99 PageMap = (VOID *) PageAddress;\r
100 PageAddress += SIZE_4KB;\r
101\r
102 PageDirectoryPointerEntry = PageMap;\r
103 PhysicalAddress = 0;\r
104\r
105 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
106 //\r
107 // Each Directory Pointer entries points to a page of Page Directory entires.\r
108 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.\r
d1102dba 109 //\r
5630cdfe
SZ
110 PageDirectoryEntry = (VOID *) PageAddress;\r
111 PageAddress += SIZE_4KB;\r
112\r
113 //\r
114 // Fill in a Page Directory Pointer Entries\r
115 //\r
5997daf7 116 PageDirectoryPointerEntry->Uint64 = (UINT64) (UINTN) PageDirectoryEntry | AddressEncMask;\r
5630cdfe
SZ
117 PageDirectoryPointerEntry->Bits.Present = 1;\r
118\r
119 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress += SIZE_2MB) {\r
9189ec20
JW
120 if ((IsNullDetectionEnabled () && PhysicalAddress == 0)\r
121 || ((PhysicalAddress < StackBase + StackSize)\r
122 && ((PhysicalAddress + SIZE_2MB) > StackBase))) {\r
5630cdfe
SZ
123 //\r
124 // Need to split this 2M page that covers stack range.\r
125 //\r
126 Split2MPageTo4K (PhysicalAddress, (UINT64 *) PageDirectoryEntry, StackBase, StackSize);\r
127 } else {\r
128 //\r
129 // Fill in the Page Directory entries\r
130 //\r
5997daf7 131 PageDirectoryEntry->Uint64 = (UINT64) PhysicalAddress | AddressEncMask;\r
5630cdfe
SZ
132 PageDirectoryEntry->Bits.ReadWrite = 1;\r
133 PageDirectoryEntry->Bits.Present = 1;\r
134 PageDirectoryEntry->Bits.MustBe1 = 1;\r
135 }\r
136 }\r
137 }\r
138\r
139 for (; IndexOfPdpEntries < 512; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
140 ZeroMem (\r
141 PageDirectoryPointerEntry,\r
142 sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)\r
143 );\r
144 }\r
145\r
2ac1730b
JW
146 //\r
147 // Protect the page table by marking the memory used for page table to be\r
148 // read-only.\r
149 //\r
150 EnablePageTableProtection ((UINTN)PageMap, FALSE);\r
151\r
5630cdfe
SZ
152 return (UINTN) PageMap;\r
153}\r
154\r
155/**\r
156 The function will check if IA32 PAE is supported.\r
157\r
158 @retval TRUE IA32 PAE is supported.\r
159 @retval FALSE IA32 PAE is not supported.\r
160\r
161**/\r
162BOOLEAN\r
163IsIa32PaeSupport (\r
164 VOID\r
165 )\r
166{\r
167 UINT32 RegEax;\r
168 UINT32 RegEdx;\r
169 BOOLEAN Ia32PaeSupport;\r
170\r
171 Ia32PaeSupport = FALSE;\r
172 AsmCpuid (0x0, &RegEax, NULL, NULL, NULL);\r
173 if (RegEax >= 0x1) {\r
174 AsmCpuid (0x1, NULL, NULL, NULL, &RegEdx);\r
175 if ((RegEdx & BIT6) != 0) {\r
176 Ia32PaeSupport = TRUE;\r
177 }\r
178 }\r
179\r
180 return Ia32PaeSupport;\r
181}\r
182\r
e63da9f0
JW
183/**\r
184 The function will check if page table should be setup or not.\r
185\r
186 @retval TRUE Page table should be created.\r
187 @retval FALSE Page table should not be created.\r
188\r
189**/\r
190BOOLEAN\r
191ToBuildPageTable (\r
192 VOID\r
193 )\r
194{\r
195 if (!IsIa32PaeSupport ()) {\r
196 return FALSE;\r
197 }\r
198\r
199 if (IsNullDetectionEnabled ()) {\r
200 return TRUE;\r
201 }\r
202\r
203 if (PcdGet8 (PcdHeapGuardPropertyMask) != 0) {\r
204 return TRUE;\r
205 }\r
206\r
50255363
JW
207 if (PcdGetBool (PcdCpuStackGuard)) {\r
208 return TRUE;\r
209 }\r
210\r
52679261 211 if (IsEnableNonExecNeeded ()) {\r
e63da9f0
JW
212 return TRUE;\r
213 }\r
214\r
215 return FALSE;\r
216}\r
217\r
91d92e25 218/**\r
219 Transfers control to DxeCore.\r
220\r
221 This function performs a CPU architecture specific operations to execute\r
222 the entry point of DxeCore with the parameters of HobList.\r
48557c65 223 It also installs EFI_END_OF_PEI_PPI to signal the end of PEI phase.\r
91d92e25 224\r
48557c65 225 @param DxeCoreEntryPoint The entry point of DxeCore.\r
91d92e25 226 @param HobList The start of HobList passed to DxeCore.\r
91d92e25 227\r
228**/\r
95276127 229VOID\r
230HandOffToDxeCore (\r
231 IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint,\r
9b937a73 232 IN EFI_PEI_HOB_POINTERS HobList\r
95276127 233 )\r
234{\r
235 EFI_STATUS Status;\r
236 EFI_PHYSICAL_ADDRESS BaseOfStack;\r
237 EFI_PHYSICAL_ADDRESS TopOfStack;\r
238 UINTN PageTables;\r
5d582956 239 X64_IDT_GATE_DESCRIPTOR *IdtTable;\r
240 UINTN SizeOfTemplate;\r
241 VOID *TemplateBase;\r
242 EFI_PHYSICAL_ADDRESS VectorAddress;\r
243 UINT32 Index;\r
bdfbe63e 244 X64_IDT_TABLE *IdtTableForX64;\r
57f360f2
JF
245 EFI_VECTOR_HANDOFF_INFO *VectorInfo;\r
246 EFI_PEI_VECTOR_HANDOFF_INFO_PPI *VectorHandoffInfoPpi;\r
5630cdfe 247 BOOLEAN BuildPageTablesIa32Pae;\r
95276127 248\r
9189ec20
JW
249 if (IsNullDetectionEnabled ()) {\r
250 ClearFirst4KPage (HobList.Raw);\r
251 }\r
252\r
95276127 253 Status = PeiServicesAllocatePages (EfiBootServicesData, EFI_SIZE_TO_PAGES (STACK_SIZE), &BaseOfStack);\r
254 ASSERT_EFI_ERROR (Status);\r
0cf27ce0 255\r
95276127 256 if (FeaturePcdGet(PcdDxeIplSwitchToLongMode)) {\r
257 //\r
0cf27ce0 258 // Compute the top of the stack we were allocated, which is used to load X64 dxe core.\r
95276127 259 // Pre-allocate a 32 bytes which confroms to x64 calling convention.\r
260 //\r
0cf27ce0 261 // The first four parameters to a function are passed in rcx, rdx, r8 and r9.\r
262 // Any further parameters are pushed on the stack. Furthermore, space (4 * 8bytes) for the\r
263 // register parameters is reserved on the stack, in case the called function\r
264 // wants to spill them; this is important if the function is variadic.\r
95276127 265 //\r
266 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - 32;\r
267\r
268 //\r
b98da1b1 269 // x64 Calling Conventions requires that the stack must be aligned to 16 bytes\r
95276127 270 //\r
271 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, 16);\r
272\r
273 //\r
274 // Load the GDT of Go64. Since the GDT of 32-bit Tiano locates in the BS_DATA\r
0cf27ce0 275 // memory, it may be corrupted when copying FV to high-end memory\r
95276127 276 //\r
277 AsmWriteGdtr (&gGdt);\r
278 //\r
279 // Create page table and save PageMapLevel4 to CR3\r
280 //\r
5630cdfe 281 PageTables = CreateIdentityMappingPageTables (BaseOfStack, STACK_SIZE);\r
95276127 282\r
283 //\r
48557c65 284 // End of PEI phase signal\r
95276127 285 //\r
98d20e44 286 PERF_EVENT_SIGNAL_BEGIN (gEndOfPeiSignalPpi.Guid);\r
9b937a73 287 Status = PeiServicesInstallPpi (&gEndOfPeiSignalPpi);\r
98d20e44 288 PERF_EVENT_SIGNAL_END (gEndOfPeiSignalPpi.Guid);\r
95276127 289 ASSERT_EFI_ERROR (Status);\r
0cf27ce0 290\r
2af2988f
JW
291 //\r
292 // Paging might be already enabled. To avoid conflict configuration,\r
293 // disable paging first anyway.\r
294 //\r
295 AsmWriteCr0 (AsmReadCr0 () & (~BIT31));\r
95276127 296 AsmWriteCr3 (PageTables);\r
5d582956 297\r
30c8f861 298 //\r
299 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.\r
0cf27ce0 300 //\r
30c8f861 301 UpdateStackHob (BaseOfStack, STACK_SIZE);\r
5d582956 302\r
4bfa7dc4 303 SizeOfTemplate = AsmGetVectorTemplatInfo (&TemplateBase);\r
304\r
305 Status = PeiServicesAllocatePages (\r
0cf27ce0 306 EfiBootServicesData,\r
bdfbe63e 307 EFI_SIZE_TO_PAGES(sizeof (X64_IDT_TABLE) + SizeOfTemplate * IDT_ENTRY_COUNT),\r
b028c102 308 &VectorAddress\r
4bfa7dc4 309 );\r
310 ASSERT_EFI_ERROR (Status);\r
311\r
bdfbe63e 312 //\r
313 // Store EFI_PEI_SERVICES** in the 4 bytes immediately preceding IDT to avoid that\r
314 // it may not be gotten correctly after IDT register is re-written.\r
315 //\r
b028c102 316 IdtTableForX64 = (X64_IDT_TABLE *) (UINTN) VectorAddress;\r
bdfbe63e 317 IdtTableForX64->PeiService = GetPeiServicesTablePointer ();\r
318\r
319 VectorAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) (IdtTableForX64 + 1);\r
320 IdtTable = IdtTableForX64->IdtTable;\r
e7af83ae 321 for (Index = 0; Index < IDT_ENTRY_COUNT; Index++) {\r
4bfa7dc4 322 IdtTable[Index].Ia32IdtEntry.Bits.GateType = 0x8e;\r
323 IdtTable[Index].Ia32IdtEntry.Bits.Reserved_0 = 0;\r
324 IdtTable[Index].Ia32IdtEntry.Bits.Selector = SYS_CODE64_SEL;\r
325\r
326 IdtTable[Index].Ia32IdtEntry.Bits.OffsetLow = (UINT16) VectorAddress;\r
327 IdtTable[Index].Ia32IdtEntry.Bits.OffsetHigh = (UINT16) (RShiftU64 (VectorAddress, 16));\r
328 IdtTable[Index].Offset32To63 = (UINT32) (RShiftU64 (VectorAddress, 32));\r
329 IdtTable[Index].Reserved = 0;\r
330\r
331 CopyMem ((VOID *) (UINTN) VectorAddress, TemplateBase, SizeOfTemplate);\r
332 AsmVectorFixup ((VOID *) (UINTN) VectorAddress, (UINT8) Index);\r
333\r
334 VectorAddress += SizeOfTemplate;\r
5d582956 335 }\r
4bfa7dc4 336\r
337 gLidtDescriptor.Base = (UINTN) IdtTable;\r
0cf27ce0 338\r
e7af83ae 339 //\r
340 // Disable interrupt of Debug timer, since new IDT table cannot handle it.\r
341 //\r
342 SaveAndSetDebugTimerInterrupt (FALSE);\r
343\r
4bfa7dc4 344 AsmWriteIdtr (&gLidtDescriptor);\r
345\r
6fb389d0
JF
346 DEBUG ((\r
347 DEBUG_INFO,\r
348 "%a() Stack Base: 0x%lx, Stack Size: 0x%x\n",\r
349 __FUNCTION__,\r
350 BaseOfStack,\r
351 STACK_SIZE\r
352 ));\r
353\r
5d582956 354 //\r
b98da1b1 355 // Go to Long Mode and transfer control to DxeCore.\r
356 // Interrupts will not get turned on until the CPU AP is loaded.\r
95276127 357 // Call x64 drivers passing in single argument, a pointer to the HOBs.\r
0cf27ce0 358 //\r
95276127 359 AsmEnablePaging64 (\r
360 SYS_CODE64_SEL,\r
361 DxeCoreEntryPoint,\r
362 (EFI_PHYSICAL_ADDRESS)(UINTN)(HobList.Raw),\r
363 0,\r
364 TopOfStack\r
365 );\r
366 } else {\r
57f360f2
JF
367 //\r
368 // Get Vector Hand-off Info PPI and build Guided HOB\r
369 //\r
370 Status = PeiServicesLocatePpi (\r
371 &gEfiVectorHandoffInfoPpiGuid,\r
372 0,\r
373 NULL,\r
374 (VOID **)&VectorHandoffInfoPpi\r
375 );\r
376 if (Status == EFI_SUCCESS) {\r
377 DEBUG ((EFI_D_INFO, "Vector Hand-off Info PPI is gotten, GUIDed HOB is created!\n"));\r
378 VectorInfo = VectorHandoffInfoPpi->Info;\r
379 Index = 1;\r
380 while (VectorInfo->Attribute != EFI_VECTOR_HANDOFF_LAST_ENTRY) {\r
381 VectorInfo ++;\r
382 Index ++;\r
383 }\r
384 BuildGuidDataHob (\r
385 &gEfiVectorHandoffInfoPpiGuid,\r
386 VectorHandoffInfoPpi->Info,\r
387 sizeof (EFI_VECTOR_HANDOFF_INFO) * Index\r
388 );\r
389 }\r
390\r
95276127 391 //\r
392 // Compute the top of the stack we were allocated. Pre-allocate a UINTN\r
393 // for safety.\r
394 //\r
395 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - CPU_STACK_ALIGNMENT;\r
396 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
397\r
09e4a8fd 398 PageTables = 0;\r
e63da9f0 399 BuildPageTablesIa32Pae = ToBuildPageTable ();\r
5630cdfe
SZ
400 if (BuildPageTablesIa32Pae) {\r
401 PageTables = Create4GPageTablesIa32Pae (BaseOfStack, STACK_SIZE);\r
52679261 402 if (IsEnableNonExecNeeded ()) {\r
9189ec20
JW
403 EnableExecuteDisableBit();\r
404 }\r
5630cdfe
SZ
405 }\r
406\r
95276127 407 //\r
48557c65 408 // End of PEI phase signal\r
95276127 409 //\r
98d20e44 410 PERF_EVENT_SIGNAL_BEGIN (gEndOfPeiSignalPpi.Guid);\r
9b937a73 411 Status = PeiServicesInstallPpi (&gEndOfPeiSignalPpi);\r
98d20e44 412 PERF_EVENT_SIGNAL_END (gEndOfPeiSignalPpi.Guid);\r
95276127 413 ASSERT_EFI_ERROR (Status);\r
414\r
5630cdfe 415 if (BuildPageTablesIa32Pae) {\r
2af2988f
JW
416 //\r
417 // Paging might be already enabled. To avoid conflict configuration,\r
418 // disable paging first anyway.\r
419 //\r
420 AsmWriteCr0 (AsmReadCr0 () & (~BIT31));\r
5630cdfe
SZ
421 AsmWriteCr3 (PageTables);\r
422 //\r
423 // Set Physical Address Extension (bit 5 of CR4).\r
424 //\r
425 AsmWriteCr4 (AsmReadCr4 () | BIT5);\r
426 }\r
427\r
30c8f861 428 //\r
429 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.\r
0cf27ce0 430 //\r
30c8f861 431 UpdateStackHob (BaseOfStack, STACK_SIZE);\r
0cf27ce0 432\r
6fb389d0
JF
433 DEBUG ((\r
434 DEBUG_INFO,\r
435 "%a() Stack Base: 0x%lx, Stack Size: 0x%x\n",\r
436 __FUNCTION__,\r
437 BaseOfStack,\r
438 STACK_SIZE\r
439 ));\r
440\r
b98da1b1 441 //\r
442 // Transfer the control to the entry point of DxeCore.\r
443 //\r
5630cdfe
SZ
444 if (BuildPageTablesIa32Pae) {\r
445 AsmEnablePaging32 (\r
446 (SWITCH_STACK_ENTRY_POINT)(UINTN)DxeCoreEntryPoint,\r
447 HobList.Raw,\r
448 NULL,\r
449 (VOID *) (UINTN) TopOfStack\r
450 );\r
451 } else {\r
452 SwitchStack (\r
453 (SWITCH_STACK_ENTRY_POINT)(UINTN)DxeCoreEntryPoint,\r
454 HobList.Raw,\r
455 NULL,\r
456 (VOID *) (UINTN) TopOfStack\r
457 );\r
458 }\r
0cf27ce0 459 }\r
95276127 460}\r
461\r