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