]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg: Allocate contiguous memory for stacks and APs loop.
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
CommitLineData
3e8ad6bd
JF
1/** @file\r
2 MP initialize support functions for DXE phase.\r
3\r
a6f799e7 4 Copyright (c) 2016 - 2023, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
3e8ad6bd
JF
6\r
7**/\r
8\r
9#include "MpLib.h"\r
96378861
JF
10\r
11#include <Library/UefiLib.h>\r
12#include <Library/UefiBootServicesTableLib.h>\r
43c9fdcc 13#include <Library/DebugAgentLib.h>\r
15720a6c 14#include <Library/DxeServicesTableLib.h>\r
a89f558d 15#include <Library/CcExitLib.h>\r
7b7508ad
TL
16#include <Register/Amd/Fam17Msr.h>\r
17#include <Register/Amd/Ghcb.h>\r
96378861 18\r
b6e45716
JF
19#include <Protocol/Timer.h>\r
20\r
053e878b 21#define AP_SAFE_STACK_SIZE 128\r
96378861 22\r
a6f799e7
XY
23CPU_MP_DATA *mCpuMpData = NULL;\r
24EFI_EVENT mCheckAllApsEvent = NULL;\r
25EFI_EVENT mMpInitExitBootServicesEvent = NULL;\r
26EFI_EVENT mLegacyBootEvent = NULL;\r
27volatile BOOLEAN mStopCheckAllApsStatus = TRUE;\r
28RELOCATE_AP_LOOP_ENTRY mReservedApLoop;\r
29UINTN mReservedTopOfApStack;\r
30volatile UINT32 mNumberToFinish = 0;\r
93ca4c0f 31\r
dbc22a17
TL
32//\r
33// Begin wakeup buffer allocation below 0x88000\r
34//\r
053e878b 35STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;\r
dbc22a17 36\r
43c9fdcc
JF
37/**\r
38 Enable Debug Agent to support source debugging on AP function.\r
39\r
40**/\r
41VOID\r
42EnableDebugAgent (\r
43 VOID\r
44 )\r
45{\r
46 //\r
47 // Initialize Debug Agent to support source level debug in DXE phase\r
48 //\r
49 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);\r
50}\r
51\r
93ca4c0f
JF
52/**\r
53 Get the pointer to CPU MP Data structure.\r
54\r
55 @return The pointer to CPU MP Data structure.\r
56**/\r
57CPU_MP_DATA *\r
58GetCpuMpData (\r
59 VOID\r
60 )\r
61{\r
62 ASSERT (mCpuMpData != NULL);\r
63 return mCpuMpData;\r
64}\r
65\r
66/**\r
67 Save the pointer to CPU MP Data structure.\r
68\r
69 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.\r
70**/\r
71VOID\r
72SaveCpuMpData (\r
053e878b 73 IN CPU_MP_DATA *CpuMpData\r
93ca4c0f
JF
74 )\r
75{\r
76 mCpuMpData = CpuMpData;\r
77}\r
78\r
96378861 79/**\r
e4ff6349 80 Get available system memory below 0x88000 by specified size.\r
ed66e0e3 81\r
a6b3d753 82 @param[in] WakeupBufferSize Wakeup buffer size required\r
3ed4e502 83\r
a6b3d753
SZ
84 @retval other Return wakeup buffer address below 1MB.\r
85 @retval -1 Cannot find free memory below 1MB.\r
ed66e0e3 86**/\r
a6b3d753
SZ
87UINTN\r
88GetWakeupBuffer (\r
053e878b 89 IN UINTN WakeupBufferSize\r
ed66e0e3
JF
90 )\r
91{\r
053e878b
MK
92 EFI_STATUS Status;\r
93 EFI_PHYSICAL_ADDRESS StartAddress;\r
94 EFI_MEMORY_TYPE MemoryType;\r
20da7ca4 95\r
06544455
TL
96 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs) &&\r
97 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp))\r
98 {\r
99 //\r
100 // An SEV-ES-only guest requires the memory to be reserved. SEV-SNP, which\r
101 // is also considered SEV-ES, uses a different AP startup method, though,\r
102 // which does not have the same requirement.\r
103 //\r
20da7ca4
TL
104 MemoryType = EfiReservedMemoryType;\r
105 } else {\r
106 MemoryType = EfiBootServicesData;\r
107 }\r
a6b3d753 108\r
e4ff6349
ED
109 //\r
110 // Try to allocate buffer below 1M for waking vector.\r
111 // LegacyBios driver only reports warning when page allocation in range\r
112 // [0x60000, 0x88000) fails.\r
113 // This library is consumed by CpuDxe driver to produce CPU Arch protocol.\r
114 // LagacyBios driver depends on CPU Arch protocol which guarantees below\r
115 // allocation runs earlier than LegacyBios driver.\r
116 //\r
b95908e0 117 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {\r
dbc22a17
TL
118 //\r
119 // SEV-ES Wakeup buffer should be under 0x88000 and under any previous one\r
120 //\r
121 StartAddress = mSevEsDxeWakeupBuffer;\r
122 } else {\r
123 StartAddress = 0x88000;\r
124 }\r
053e878b 125\r
a6b3d753
SZ
126 Status = gBS->AllocatePages (\r
127 AllocateMaxAddress,\r
20da7ca4 128 MemoryType,\r
a6b3d753
SZ
129 EFI_SIZE_TO_PAGES (WakeupBufferSize),\r
130 &StartAddress\r
131 );\r
132 ASSERT_EFI_ERROR (Status);\r
e4ff6349 133 if (EFI_ERROR (Status)) {\r
053e878b 134 StartAddress = (EFI_PHYSICAL_ADDRESS)-1;\r
b95908e0 135 } else if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {\r
dbc22a17
TL
136 //\r
137 // Next SEV-ES wakeup buffer allocation must be below this allocation\r
138 //\r
139 mSevEsDxeWakeupBuffer = StartAddress;\r
3ed4e502 140 }\r
e4ff6349 141\r
053e878b
MK
142 DEBUG ((\r
143 DEBUG_INFO,\r
144 "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
145 (UINTN)StartAddress,\r
146 WakeupBufferSize\r
147 ));\r
e4ff6349 148\r
053e878b 149 return (UINTN)StartAddress;\r
ed66e0e3
JF
150}\r
151\r
f32bfe6d
JW
152/**\r
153 Get available EfiBootServicesCode memory below 4GB by specified size.\r
154\r
155 This buffer is required to safely transfer AP from real address mode to\r
156 protected mode or long mode, due to the fact that the buffer returned by\r
157 GetWakeupBuffer() may be marked as non-executable.\r
158\r
159 @param[in] BufferSize Wakeup transition buffer size.\r
160\r
161 @retval other Return wakeup transition buffer address below 4GB.\r
162 @retval 0 Cannot find free memory below 4GB.\r
163**/\r
164UINTN\r
54aeed7e 165AllocateCodeBuffer (\r
053e878b 166 IN UINTN BufferSize\r
f32bfe6d
JW
167 )\r
168{\r
053e878b
MK
169 EFI_STATUS Status;\r
170 EFI_PHYSICAL_ADDRESS StartAddress;\r
f32bfe6d
JW
171\r
172 StartAddress = BASE_4GB - 1;\r
053e878b
MK
173 Status = gBS->AllocatePages (\r
174 AllocateMaxAddress,\r
175 EfiBootServicesCode,\r
176 EFI_SIZE_TO_PAGES (BufferSize),\r
177 &StartAddress\r
178 );\r
f32bfe6d
JW
179 if (EFI_ERROR (Status)) {\r
180 StartAddress = 0;\r
181 }\r
182\r
183 return (UINTN)StartAddress;\r
184}\r
185\r
7b7508ad
TL
186/**\r
187 Return the address of the SEV-ES AP jump table.\r
188\r
189 This buffer is required in order for an SEV-ES guest to transition from\r
190 UEFI into an OS.\r
191\r
192 @return Return SEV-ES AP jump table buffer\r
193**/\r
194UINTN\r
195GetSevEsAPMemory (\r
196 VOID\r
197 )\r
198{\r
20da7ca4
TL
199 EFI_STATUS Status;\r
200 EFI_PHYSICAL_ADDRESS StartAddress;\r
201 MSR_SEV_ES_GHCB_REGISTER Msr;\r
202 GHCB *Ghcb;\r
1b0db1ec 203 BOOLEAN InterruptState;\r
7b7508ad
TL
204\r
205 //\r
206 // Allocate 1 page for AP jump table page\r
207 //\r
208 StartAddress = BASE_4GB - 1;\r
053e878b
MK
209 Status = gBS->AllocatePages (\r
210 AllocateMaxAddress,\r
211 EfiReservedMemoryType,\r
212 1,\r
213 &StartAddress\r
214 );\r
7b7508ad
TL
215 ASSERT_EFI_ERROR (Status);\r
216\r
053e878b 217 DEBUG ((DEBUG_INFO, "Dxe: SevEsAPMemory = %lx\n", (UINTN)StartAddress));\r
7b7508ad 218\r
20da7ca4
TL
219 //\r
220 // Save the SevEsAPMemory as the AP jump table.\r
221 //\r
222 Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);\r
053e878b 223 Ghcb = Msr.Ghcb;\r
20da7ca4 224\r
765ba5bf
MX
225 CcExitVmgInit (Ghcb, &InterruptState);\r
226 CcExitVmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64)(UINTN)StartAddress);\r
227 CcExitVmgDone (Ghcb, InterruptState);\r
20da7ca4 228\r
053e878b 229 return (UINTN)StartAddress;\r
7b7508ad
TL
230}\r
231\r
96378861
JF
232/**\r
233 Checks APs status and updates APs status if needed.\r
234\r
235**/\r
236VOID\r
237CheckAndUpdateApsStatus (\r
238 VOID\r
239 )\r
240{\r
053e878b
MK
241 UINTN ProcessorNumber;\r
242 EFI_STATUS Status;\r
243 CPU_MP_DATA *CpuMpData;\r
08085f08
JF
244\r
245 CpuMpData = GetCpuMpData ();\r
246\r
247 //\r
248 // First, check whether pending StartupAllAPs() exists.\r
249 //\r
250 if (CpuMpData->WaitEvent != NULL) {\r
08085f08
JF
251 Status = CheckAllAPs ();\r
252 //\r
253 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.\r
254 //\r
255 if (Status != EFI_NOT_READY) {\r
053e878b 256 Status = gBS->SignalEvent (CpuMpData->WaitEvent);\r
08085f08
JF
257 CpuMpData->WaitEvent = NULL;\r
258 }\r
259 }\r
260\r
261 //\r
262 // Second, check whether pending StartupThisAPs() callings exist.\r
263 //\r
264 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
08085f08
JF
265 if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {\r
266 continue;\r
267 }\r
268\r
269 Status = CheckThisAP (ProcessorNumber);\r
270\r
271 if (Status != EFI_NOT_READY) {\r
272 gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);\r
053e878b 273 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;\r
08085f08
JF
274 }\r
275 }\r
96378861
JF
276}\r
277\r
278/**\r
279 Checks APs' status periodically.\r
280\r
438f1766 281 This function is triggered by timer periodically to check the\r
96378861
JF
282 state of APs for StartupAllAPs() and StartupThisAP() executed\r
283 in non-blocking mode.\r
284\r
285 @param[in] Event Event triggered.\r
286 @param[in] Context Parameter passed with the event.\r
287\r
288**/\r
289VOID\r
290EFIAPI\r
291CheckApsStatus (\r
053e878b
MK
292 IN EFI_EVENT Event,\r
293 IN VOID *Context\r
96378861
JF
294 )\r
295{\r
296 //\r
297 // If CheckApsStatus() is not stopped, otherwise return immediately.\r
298 //\r
299 if (!mStopCheckAllApsStatus) {\r
300 CheckAndUpdateApsStatus ();\r
301 }\r
302}\r
ed66e0e3 303\r
7b7508ad
TL
304/**\r
305 Get Protected mode code segment with 16-bit default addressing\r
306 from current GDT table.\r
307\r
308 @return Protected mode 16-bit code segment value.\r
309**/\r
310UINT16\r
311GetProtectedMode16CS (\r
312 VOID\r
313 )\r
314{\r
315 IA32_DESCRIPTOR GdtrDesc;\r
316 IA32_SEGMENT_DESCRIPTOR *GdtEntry;\r
317 UINTN GdtEntryCount;\r
318 UINT16 Index;\r
319\r
053e878b 320 Index = (UINT16)-1;\r
7b7508ad
TL
321 AsmReadGdtr (&GdtrDesc);\r
322 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
053e878b 323 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;\r
7b7508ad
TL
324 for (Index = 0; Index < GdtEntryCount; Index++) {\r
325 if (GdtEntry->Bits.L == 0) {\r
053e878b 326 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 0)) {\r
7b7508ad
TL
327 break;\r
328 }\r
329 }\r
053e878b 330\r
7b7508ad
TL
331 GdtEntry++;\r
332 }\r
053e878b 333\r
7b7508ad
TL
334 ASSERT (Index != GdtEntryCount);\r
335 return Index * 8;\r
336}\r
337\r
4d3314f6
JF
338/**\r
339 Get Protected mode code segment from current GDT table.\r
340\r
b31c1ad1 341 @return Protected mode code segment value.\r
4d3314f6
JF
342**/\r
343UINT16\r
344GetProtectedModeCS (\r
345 VOID\r
346 )\r
347{\r
348 IA32_DESCRIPTOR GdtrDesc;\r
349 IA32_SEGMENT_DESCRIPTOR *GdtEntry;\r
350 UINTN GdtEntryCount;\r
351 UINT16 Index;\r
352\r
4d3314f6
JF
353 AsmReadGdtr (&GdtrDesc);\r
354 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
053e878b 355 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;\r
4d3314f6
JF
356 for (Index = 0; Index < GdtEntryCount; Index++) {\r
357 if (GdtEntry->Bits.L == 0) {\r
053e878b 358 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 1)) {\r
4d3314f6
JF
359 break;\r
360 }\r
361 }\r
053e878b 362\r
4d3314f6
JF
363 GdtEntry++;\r
364 }\r
053e878b 365\r
37fba7c2 366 ASSERT (Index != GdtEntryCount);\r
4d3314f6
JF
367 return Index * 8;\r
368}\r
369\r
370/**\r
371 Do sync on APs.\r
372\r
373 @param[in, out] Buffer Pointer to private data buffer.\r
374**/\r
375VOID\r
376EFIAPI\r
377RelocateApLoop (\r
378 IN OUT VOID *Buffer\r
379 )\r
380{\r
a6f799e7
XY
381 CPU_MP_DATA *CpuMpData;\r
382 BOOLEAN MwaitSupport;\r
383 UINTN ProcessorNumber;\r
384 UINTN StackStart;\r
4d3314f6 385\r
7367cc6c 386 MpInitLibWhoAmI (&ProcessorNumber);\r
4d3314f6
JF
387 CpuMpData = GetCpuMpData ();\r
388 MwaitSupport = IsMwaitSupport ();\r
33a3408f 389 if (CpuMpData->UseSevEsAPMethod) {\r
a6f799e7
XY
390 //\r
391 // 64-bit AMD processors with SEV-ES\r
392 //\r
33a3408f 393 StackStart = CpuMpData->SevEsAPResetStackStart;\r
a6f799e7
XY
394 mReservedApLoop.AmdSevEntry (\r
395 MwaitSupport,\r
396 CpuMpData->ApTargetCState,\r
397 CpuMpData->PmCodeSegment,\r
398 StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
399 (UINTN)&mNumberToFinish,\r
400 CpuMpData->Pm16CodeSegment,\r
401 CpuMpData->SevEsAPBuffer,\r
402 CpuMpData->WakeupBuffer\r
403 );\r
20da7ca4 404 } else {\r
a6f799e7
XY
405 //\r
406 // Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit AMD processors without SEV-ES\r
407 //\r
33a3408f 408 StackStart = mReservedTopOfApStack;\r
a6f799e7
XY
409 mReservedApLoop.GenericEntry (\r
410 MwaitSupport,\r
411 CpuMpData->ApTargetCState,\r
412 CpuMpData->PmCodeSegment,\r
413 StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
414 (UINTN)&mNumberToFinish,\r
415 CpuMpData->Pm16CodeSegment,\r
416 CpuMpData->SevEsAPBuffer,\r
417 CpuMpData->WakeupBuffer\r
418 );\r
20da7ca4 419 }\r
053e878b 420\r
4d3314f6
JF
421 //\r
422 // It should never reach here\r
423 //\r
424 ASSERT (FALSE);\r
425}\r
426\r
427/**\r
428 Callback function for ExitBootServices.\r
429\r
430 @param[in] Event Event whose notification function is being invoked.\r
431 @param[in] Context The pointer to the notification function's context,\r
432 which is implementation-dependent.\r
433\r
434**/\r
435VOID\r
436EFIAPI\r
86af2eb8 437MpInitChangeApLoopCallback (\r
053e878b
MK
438 IN EFI_EVENT Event,\r
439 IN VOID *Context\r
4d3314f6
JF
440 )\r
441{\r
053e878b 442 CPU_MP_DATA *CpuMpData;\r
5183fb37 443\r
053e878b
MK
444 CpuMpData = GetCpuMpData ();\r
445 CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
7b7508ad 446 CpuMpData->Pm16CodeSegment = GetProtectedMode16CS ();\r
053e878b
MK
447 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
448 mNumberToFinish = CpuMpData->CpuCount - 1;\r
cf4e79e4 449 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
9f91cb01
JF
450 while (mNumberToFinish > 0) {\r
451 CpuPause ();\r
452 }\r
20da7ca4 453\r
06544455 454 if (CpuMpData->UseSevEsAPMethod && (CpuMpData->WakeupBuffer != (UINTN)-1)) {\r
20da7ca4
TL
455 //\r
456 // There are APs present. Re-use reserved memory area below 1MB from\r
457 // WakeupBuffer as the area to be used for transitioning to 16-bit mode\r
458 // in support of booting of the AP by an OS.\r
459 //\r
460 CopyMem (\r
053e878b
MK
461 (VOID *)CpuMpData->WakeupBuffer,\r
462 (VOID *)(CpuMpData->AddressMap.RendezvousFunnelAddress +\r
463 CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),\r
20da7ca4
TL
464 CpuMpData->AddressMap.SwitchToRealPM16ModeSize\r
465 );\r
466 }\r
467\r
86af2eb8 468 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
4d3314f6
JF
469}\r
470\r
93ca4c0f
JF
471/**\r
472 Initialize global data for MP support.\r
473\r
474 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
475**/\r
476VOID\r
477InitMpGlobalData (\r
053e878b 478 IN CPU_MP_DATA *CpuMpData\r
93ca4c0f
JF
479 )\r
480{\r
053e878b 481 EFI_STATUS Status;\r
cbcf0cd6 482 EFI_PHYSICAL_ADDRESS Address;\r
053e878b
MK
483 UINTN Index;\r
484 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
485 UINTN StackBase;\r
486 CPU_INFO_IN_HOB *CpuInfoInHob;\r
e9782e69
XY
487 UINTN StackPages;\r
488 UINTN FuncPages;\r
96378861 489\r
93ca4c0f
JF
490 SaveCpuMpData (CpuMpData);\r
491\r
14e8137c
JF
492 if (CpuMpData->CpuCount == 1) {\r
493 //\r
494 // If only BSP exists, return\r
495 //\r
496 return;\r
497 }\r
498\r
15720a6c
JW
499 if (PcdGetBool (PcdCpuStackGuard)) {\r
500 //\r
501 // One extra page at the bottom of the stack is needed for Guard page.\r
502 //\r
503 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {\r
504 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));\r
505 ASSERT (FALSE);\r
506 }\r
507\r
52315261
JW
508 //\r
509 // DXE will reuse stack allocated for APs at PEI phase if it's available.\r
510 // Let's check it here.\r
511 //\r
512 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of\r
513 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be\r
514 // set here.\r
515 //\r
516 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
15720a6c 517 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {\r
053e878b 518 if ((CpuInfoInHob != NULL) && (CpuInfoInHob[Index].ApTopOfStack != 0)) {\r
20737c2f 519 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;\r
52315261
JW
520 } else {\r
521 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;\r
522 }\r
15720a6c
JW
523\r
524 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);\r
525 ASSERT_EFI_ERROR (Status);\r
526\r
527 Status = gDS->SetMemorySpaceAttributes (\r
528 StackBase,\r
529 EFI_PAGES_TO_SIZE (1),\r
530 MemDesc.Attributes | EFI_MEMORY_RP\r
531 );\r
532 ASSERT_EFI_ERROR (Status);\r
52315261 533\r
053e878b
MK
534 DEBUG ((\r
535 DEBUG_INFO,\r
536 "Stack Guard set at %lx [cpu%lu]!\n",\r
537 (UINT64)StackBase,\r
538 (UINT64)Index\r
539 ));\r
15720a6c
JW
540 }\r
541 }\r
542\r
5183fb37 543 //\r
ffd6b0b1
JF
544 // Avoid APs access invalid buffer data which allocated by BootServices,\r
545 // so we will allocate reserved data for AP loop code. We also need to\r
546 // allocate this buffer below 4GB due to APs may be transferred to 32bit\r
547 // protected mode on long mode DXE.\r
5183fb37
JF
548 // Allocating it in advance since memory services are not available in\r
549 // Exit Boot Services callback function.\r
550 //\r
e9782e69
XY
551 // +------------+ (TopOfApStack)\r
552 // | Stack * N |\r
553 // +------------+ (stack base, 4k aligned)\r
554 // | Padding |\r
555 // +------------+\r
556 // | Ap Loop |\r
557 // +------------+ ((low address, 4k-aligned)\r
558 //\r
559\r
560 StackPages = EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SIZE);\r
561 FuncPages = EFI_SIZE_TO_PAGES (CpuMpData->AddressMap.RelocateApLoopFuncSize);\r
562\r
cbcf0cd6
YX
563 Address = BASE_4GB - 1;\r
564 Status = gBS->AllocatePages (\r
565 AllocateMaxAddress,\r
566 EfiReservedMemoryType,\r
e9782e69 567 StackPages + FuncPages,\r
cbcf0cd6
YX
568 &Address\r
569 );\r
570 ASSERT_EFI_ERROR (Status);\r
bc2288f5 571\r
a6f799e7
XY
572 mReservedApLoop.Data = (VOID *)(UINTN)Address;\r
573 ASSERT (mReservedApLoop.Data != NULL);\r
73ccde8f 574\r
cbcf0cd6
YX
575 //\r
576 // Make sure that the buffer memory is executable if NX protection is enabled\r
577 // for EfiReservedMemoryType.\r
578 //\r
579 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD\r
580 // service.\r
581 //\r
582 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);\r
583 if (!EFI_ERROR (Status)) {\r
584 gDS->SetMemorySpaceAttributes (\r
585 Address,\r
e9782e69 586 EFI_PAGES_TO_SIZE (FuncPages),\r
cbcf0cd6
YX
587 MemDesc.Attributes & (~EFI_MEMORY_XP)\r
588 );\r
73ccde8f
XY
589 }\r
590\r
e9782e69 591 mReservedTopOfApStack = (UINTN)Address + EFI_PAGES_TO_SIZE (StackPages+FuncPages);\r
cbcf0cd6
YX
592 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
593 CopyMem (\r
a6f799e7 594 mReservedApLoop.Data,\r
cbcf0cd6
YX
595 CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
596 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
597 );\r
5183fb37 598\r
96378861
JF
599 Status = gBS->CreateEvent (\r
600 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
601 TPL_NOTIFY,\r
602 CheckApsStatus,\r
603 NULL,\r
604 &mCheckAllApsEvent\r
605 );\r
606 ASSERT_EFI_ERROR (Status);\r
607\r
608 //\r
609 // Set timer to check all APs status.\r
610 //\r
611 Status = gBS->SetTimer (\r
612 mCheckAllApsEvent,\r
613 TimerPeriodic,\r
a1c35ff3
HW
614 EFI_TIMER_PERIOD_MICROSECONDS (\r
615 PcdGet32 (PcdCpuApStatusCheckIntervalInMicroSeconds)\r
616 )\r
96378861
JF
617 );\r
618 ASSERT_EFI_ERROR (Status);\r
8677a56a 619\r
4d3314f6
JF
620 Status = gBS->CreateEvent (\r
621 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
622 TPL_CALLBACK,\r
86af2eb8 623 MpInitChangeApLoopCallback,\r
4d3314f6
JF
624 NULL,\r
625 &mMpInitExitBootServicesEvent\r
626 );\r
627 ASSERT_EFI_ERROR (Status);\r
8677a56a
JF
628\r
629 Status = gBS->CreateEventEx (\r
630 EVT_NOTIFY_SIGNAL,\r
631 TPL_CALLBACK,\r
632 MpInitChangeApLoopCallback,\r
633 NULL,\r
634 &gEfiEventLegacyBootGuid,\r
635 &mLegacyBootEvent\r
636 );\r
637 ASSERT_EFI_ERROR (Status);\r
93ca4c0f 638}\r
3e8ad6bd
JF
639\r
640/**\r
641 This service executes a caller provided function on all enabled APs.\r
642\r
643 @param[in] Procedure A pointer to the function to be run on\r
644 enabled APs of the system. See type\r
645 EFI_AP_PROCEDURE.\r
646 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
647 the function specified by Procedure one by\r
648 one, in ascending order of processor handle\r
649 number. If FALSE, then all the enabled APs\r
650 execute the function specified by Procedure\r
651 simultaneously.\r
652 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
653 service. If it is NULL, then execute in\r
654 blocking mode. BSP waits until all APs finish\r
655 or TimeoutInMicroSeconds expires. If it's\r
656 not NULL, then execute in non-blocking mode.\r
657 BSP requests the function specified by\r
658 Procedure to be started on all the enabled\r
659 APs, and go on executing immediately. If\r
660 all return from Procedure, or TimeoutInMicroSeconds\r
661 expires, this event is signaled. The BSP\r
662 can use the CheckEvent() or WaitForEvent()\r
663 services to check the state of event. Type\r
664 EFI_EVENT is defined in CreateEvent() in\r
665 the Unified Extensible Firmware Interface\r
666 Specification.\r
367284e7 667 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
668 APs to return from Procedure, either for\r
669 blocking or non-blocking mode. Zero means\r
670 infinity. If the timeout expires before\r
671 all APs return from Procedure, then Procedure\r
672 on the failed APs is terminated. All enabled\r
673 APs are available for next function assigned\r
674 by MpInitLibStartupAllAPs() or\r
675 MPInitLibStartupThisAP().\r
676 If the timeout expires in blocking mode,\r
677 BSP returns EFI_TIMEOUT. If the timeout\r
678 expires in non-blocking mode, WaitEvent\r
679 is signaled with SignalEvent().\r
680 @param[in] ProcedureArgument The parameter passed into Procedure for\r
681 all APs.\r
682 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
683 if all APs finish successfully, then its\r
684 content is set to NULL. If not all APs\r
685 finish before timeout expires, then its\r
686 content is set to address of the buffer\r
687 holding handle numbers of the failed APs.\r
688 The buffer is allocated by MP Initialization\r
689 library, and it's the caller's responsibility to\r
690 free the buffer with FreePool() service.\r
691 In blocking mode, it is ready for consumption\r
692 when the call returns. In non-blocking mode,\r
693 it is ready when WaitEvent is signaled. The\r
694 list of failed CPU is terminated by\r
695 END_OF_CPU_LIST.\r
696\r
697 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
698 the timeout expired.\r
699 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
700 to all enabled APs.\r
701 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
702 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
703 signaled.\r
704 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
705 supported.\r
706 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
707 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
708 @retval EFI_NOT_READY Any enabled APs are busy.\r
709 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
710 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
711 all enabled APs have finished.\r
712 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
713\r
714**/\r
715EFI_STATUS\r
716EFIAPI\r
717MpInitLibStartupAllAPs (\r
053e878b
MK
718 IN EFI_AP_PROCEDURE Procedure,\r
719 IN BOOLEAN SingleThread,\r
720 IN EFI_EVENT WaitEvent OPTIONAL,\r
721 IN UINTN TimeoutInMicroseconds,\r
722 IN VOID *ProcedureArgument OPTIONAL,\r
723 OUT UINTN **FailedCpuList OPTIONAL\r
3e8ad6bd
JF
724 )\r
725{\r
053e878b 726 EFI_STATUS Status;\r
86efe976
JF
727\r
728 //\r
729 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
730 //\r
731 mStopCheckAllApsStatus = TRUE;\r
732\r
ee0c39fa 733 Status = StartupAllCPUsWorker (\r
86efe976
JF
734 Procedure,\r
735 SingleThread,\r
ee0c39fa 736 TRUE,\r
86efe976
JF
737 WaitEvent,\r
738 TimeoutInMicroseconds,\r
739 ProcedureArgument,\r
740 FailedCpuList\r
741 );\r
742\r
743 //\r
744 // Start checkAllApsStatus\r
745 //\r
746 mStopCheckAllApsStatus = FALSE;\r
747\r
748 return Status;\r
3e8ad6bd
JF
749}\r
750\r
751/**\r
752 This service lets the caller get one enabled AP to execute a caller-provided\r
753 function.\r
754\r
755 @param[in] Procedure A pointer to the function to be run on the\r
756 designated AP of the system. See type\r
757 EFI_AP_PROCEDURE.\r
758 @param[in] ProcessorNumber The handle number of the AP. The range is\r
759 from 0 to the total number of logical\r
760 processors minus 1. The total number of\r
761 logical processors can be retrieved by\r
762 MpInitLibGetNumberOfProcessors().\r
763 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
764 service. If it is NULL, then execute in\r
765 blocking mode. BSP waits until this AP finish\r
766 or TimeoutInMicroSeconds expires. If it's\r
767 not NULL, then execute in non-blocking mode.\r
768 BSP requests the function specified by\r
769 Procedure to be started on this AP,\r
770 and go on executing immediately. If this AP\r
771 return from Procedure or TimeoutInMicroSeconds\r
772 expires, this event is signaled. The BSP\r
773 can use the CheckEvent() or WaitForEvent()\r
774 services to check the state of event. Type\r
775 EFI_EVENT is defined in CreateEvent() in\r
776 the Unified Extensible Firmware Interface\r
777 Specification.\r
367284e7 778 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
779 this AP to finish this Procedure, either for\r
780 blocking or non-blocking mode. Zero means\r
781 infinity. If the timeout expires before\r
782 this AP returns from Procedure, then Procedure\r
783 on the AP is terminated. The\r
784 AP is available for next function assigned\r
785 by MpInitLibStartupAllAPs() or\r
786 MpInitLibStartupThisAP().\r
787 If the timeout expires in blocking mode,\r
788 BSP returns EFI_TIMEOUT. If the timeout\r
789 expires in non-blocking mode, WaitEvent\r
790 is signaled with SignalEvent().\r
791 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
792 specified AP.\r
793 @param[out] Finished If NULL, this parameter is ignored. In\r
794 blocking mode, this parameter is ignored.\r
795 In non-blocking mode, if AP returns from\r
796 Procedure before the timeout expires, its\r
797 content is set to TRUE. Otherwise, the\r
798 value is set to FALSE. The caller can\r
799 determine if the AP returned from Procedure\r
800 by evaluating this value.\r
801\r
802 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
803 the timeout expires.\r
804 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
805 dispatched to specified AP.\r
806 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
807 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
808 signaled.\r
809 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
810 supported.\r
811 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
812 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
813 the specified AP has finished.\r
814 @retval EFI_NOT_READY The specified AP is busy.\r
815 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
816 @retval EFI_NOT_FOUND The processor with the handle specified by\r
817 ProcessorNumber does not exist.\r
818 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
819 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
820\r
821**/\r
822EFI_STATUS\r
823EFIAPI\r
824MpInitLibStartupThisAP (\r
053e878b
MK
825 IN EFI_AP_PROCEDURE Procedure,\r
826 IN UINTN ProcessorNumber,\r
827 IN EFI_EVENT WaitEvent OPTIONAL,\r
828 IN UINTN TimeoutInMicroseconds,\r
829 IN VOID *ProcedureArgument OPTIONAL,\r
830 OUT BOOLEAN *Finished OPTIONAL\r
3e8ad6bd
JF
831 )\r
832{\r
053e878b 833 EFI_STATUS Status;\r
20ae5774
JF
834\r
835 //\r
836 // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
837 //\r
838 mStopCheckAllApsStatus = TRUE;\r
839\r
840 Status = StartupThisAPWorker (\r
841 Procedure,\r
842 ProcessorNumber,\r
843 WaitEvent,\r
844 TimeoutInMicroseconds,\r
845 ProcedureArgument,\r
846 Finished\r
847 );\r
848\r
849 mStopCheckAllApsStatus = FALSE;\r
850\r
851 return Status;\r
3e8ad6bd
JF
852}\r
853\r
854/**\r
855 This service switches the requested AP to be the BSP from that point onward.\r
856 This service changes the BSP for all purposes. This call can only be performed\r
857 by the current BSP.\r
858\r
859 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
860 BSP. The range is from 0 to the total number of\r
861 logical processors minus 1. The total number of\r
862 logical processors can be retrieved by\r
863 MpInitLibGetNumberOfProcessors().\r
864 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
865 enabled AP. Otherwise, it will be disabled.\r
866\r
867 @retval EFI_SUCCESS BSP successfully switched.\r
868 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
869 this service returning.\r
870 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
871 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
872 @retval EFI_NOT_FOUND The processor with the handle specified by\r
873 ProcessorNumber does not exist.\r
874 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
875 a disabled AP.\r
876 @retval EFI_NOT_READY The specified AP is busy.\r
877 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
878\r
879**/\r
880EFI_STATUS\r
881EFIAPI\r
882MpInitLibSwitchBSP (\r
053e878b
MK
883 IN UINTN ProcessorNumber,\r
884 IN BOOLEAN EnableOldBSP\r
3e8ad6bd
JF
885 )\r
886{\r
053e878b
MK
887 EFI_STATUS Status;\r
888 EFI_TIMER_ARCH_PROTOCOL *Timer;\r
889 UINT64 TimerPeriod;\r
41be0da5 890\r
8ad05bd2 891 TimerPeriod = 0;\r
b6e45716
JF
892 //\r
893 // Locate Timer Arch Protocol\r
894 //\r
053e878b 895 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&Timer);\r
b6e45716
JF
896 if (EFI_ERROR (Status)) {\r
897 Timer = NULL;\r
898 }\r
899\r
900 if (Timer != NULL) {\r
901 //\r
902 // Save current rate of DXE Timer\r
903 //\r
904 Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
905 //\r
906 // Disable DXE Timer and drain pending interrupts\r
907 //\r
908 Timer->SetTimerPeriod (Timer, 0);\r
909 }\r
41be0da5
JF
910\r
911 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
912\r
b6e45716
JF
913 if (Timer != NULL) {\r
914 //\r
915 // Enable and restore rate of DXE Timer\r
916 //\r
917 Timer->SetTimerPeriod (Timer, TimerPeriod);\r
918 }\r
919\r
41be0da5 920 return Status;\r
3e8ad6bd
JF
921}\r
922\r
923/**\r
924 This service lets the caller enable or disable an AP from this point onward.\r
925 This service may only be called from the BSP.\r
926\r
927 @param[in] ProcessorNumber The handle number of AP.\r
928 The range is from 0 to the total number of\r
929 logical processors minus 1. The total number of\r
930 logical processors can be retrieved by\r
931 MpInitLibGetNumberOfProcessors().\r
932 @param[in] EnableAP Specifies the new state for the processor for\r
933 enabled, FALSE for disabled.\r
934 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
935 the new health status of the AP. This flag\r
936 corresponds to StatusFlag defined in\r
937 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
938 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
939 bits are ignored. If it is NULL, this parameter\r
940 is ignored.\r
941\r
942 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
943 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
944 prior to this service returning.\r
945 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
946 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
947 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
948 does not exist.\r
949 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
950 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
951\r
952**/\r
953EFI_STATUS\r
954EFIAPI\r
955MpInitLibEnableDisableAP (\r
053e878b
MK
956 IN UINTN ProcessorNumber,\r
957 IN BOOLEAN EnableAP,\r
958 IN UINT32 *HealthFlag OPTIONAL\r
3e8ad6bd
JF
959 )\r
960{\r
053e878b
MK
961 EFI_STATUS Status;\r
962 BOOLEAN TempStopCheckState;\r
e37109bc
JF
963\r
964 TempStopCheckState = FALSE;\r
965 //\r
966 // temporarily stop checkAllAPsStatus for initialize parameters.\r
967 //\r
968 if (!mStopCheckAllApsStatus) {\r
969 mStopCheckAllApsStatus = TRUE;\r
970 TempStopCheckState = TRUE;\r
971 }\r
972\r
973 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
974\r
975 if (TempStopCheckState) {\r
976 mStopCheckAllApsStatus = FALSE;\r
977 }\r
978\r
979 return Status;\r
3e8ad6bd 980}\r
c788c2b1
SF
981\r
982/**\r
983 This funtion will try to invoke platform specific microcode shadow logic to\r
984 relocate microcode update patches into memory.\r
985\r
4ac82ea1 986 @param[in, out] CpuMpData The pointer to CPU MP Data structure.\r
c788c2b1
SF
987\r
988 @retval EFI_SUCCESS Shadow microcode success.\r
989 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.\r
990 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow\r
991 PPI/Protocol.\r
992**/\r
993EFI_STATUS\r
994PlatformShadowMicrocode (\r
053e878b 995 IN OUT CPU_MP_DATA *CpuMpData\r
c788c2b1
SF
996 )\r
997{\r
998 //\r
999 // There is no DXE version of platform shadow microcode protocol so far.\r
1000 // A platform which only uses DxeMpInitLib instance could only supports\r
1001 // the PCD based microcode shadowing.\r
1002 //\r
1003 return EFI_UNSUPPORTED;\r
1004}\r