]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c
UefiCpuPkg/PiSmmCpuDxeSmm: Fix buffer overflow issue.
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / MpService.c
CommitLineData
529a5a86
MK
1/** @file\r
2SMM MP service implementation\r
3\r
3eb69b08 4Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>\r
241f9149
LD
5Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
0acd8697 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
529a5a86
MK
8\r
9**/\r
10\r
11#include "PiSmmCpuDxeSmm.h"\r
12\r
13//\r
14// Slots for all MTRR( FIXED MTRR + VARIABLE MTRR + MTRR_LIB_IA32_MTRR_DEF_TYPE)\r
15//\r
26ab5ac3 16MTRR_SETTINGS gSmiMtrrs;\r
529a5a86
MK
17UINT64 gPhyMask;\r
18SMM_DISPATCHER_MP_SYNC_DATA *mSmmMpSyncData = NULL;\r
19UINTN mSmmMpSyncDataSize;\r
1d648531
JF
20SMM_CPU_SEMAPHORES mSmmCpuSemaphores;\r
21UINTN mSemaphoreSize;\r
fe3a75bc 22SPIN_LOCK *mPFLock = NULL;\r
b43dd229 23SMM_CPU_SYNC_MODE mCpuSmmSyncMode;\r
ba40cb31 24BOOLEAN mMachineCheckSupported = FALSE;\r
529a5a86
MK
25\r
26/**\r
27 Performs an atomic compare exchange operation to get semaphore.\r
28 The compare exchange operation must be performed using\r
29 MP safe mechanisms.\r
30\r
31 @param Sem IN: 32-bit unsigned integer\r
32 OUT: original integer - 1\r
33 @return Original integer - 1\r
34\r
35**/\r
36UINT32\r
37WaitForSemaphore (\r
38 IN OUT volatile UINT32 *Sem\r
39 )\r
40{\r
41 UINT32 Value;\r
42\r
43 do {\r
44 Value = *Sem;\r
45 } while (Value == 0 ||\r
46 InterlockedCompareExchange32 (\r
47 (UINT32*)Sem,\r
48 Value,\r
49 Value - 1\r
50 ) != Value);\r
51 return Value - 1;\r
52}\r
53\r
54\r
55/**\r
56 Performs an atomic compare exchange operation to release semaphore.\r
57 The compare exchange operation must be performed using\r
58 MP safe mechanisms.\r
59\r
60 @param Sem IN: 32-bit unsigned integer\r
61 OUT: original integer + 1\r
62 @return Original integer + 1\r
63\r
64**/\r
65UINT32\r
66ReleaseSemaphore (\r
67 IN OUT volatile UINT32 *Sem\r
68 )\r
69{\r
70 UINT32 Value;\r
71\r
72 do {\r
73 Value = *Sem;\r
74 } while (Value + 1 != 0 &&\r
75 InterlockedCompareExchange32 (\r
76 (UINT32*)Sem,\r
77 Value,\r
78 Value + 1\r
79 ) != Value);\r
80 return Value + 1;\r
81}\r
82\r
83/**\r
84 Performs an atomic compare exchange operation to lock semaphore.\r
85 The compare exchange operation must be performed using\r
86 MP safe mechanisms.\r
87\r
88 @param Sem IN: 32-bit unsigned integer\r
89 OUT: -1\r
90 @return Original integer\r
91\r
92**/\r
93UINT32\r
94LockdownSemaphore (\r
95 IN OUT volatile UINT32 *Sem\r
96 )\r
97{\r
98 UINT32 Value;\r
99\r
100 do {\r
101 Value = *Sem;\r
102 } while (InterlockedCompareExchange32 (\r
103 (UINT32*)Sem,\r
104 Value, (UINT32)-1\r
105 ) != Value);\r
106 return Value;\r
107}\r
108\r
109/**\r
110 Wait all APs to performs an atomic compare exchange operation to release semaphore.\r
111\r
112 @param NumberOfAPs AP number\r
113\r
114**/\r
115VOID\r
116WaitForAllAPs (\r
117 IN UINTN NumberOfAPs\r
118 )\r
119{\r
120 UINTN BspIndex;\r
121\r
122 BspIndex = mSmmMpSyncData->BspIndex;\r
123 while (NumberOfAPs-- > 0) {\r
ed3d5ecb 124 WaitForSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run);\r
529a5a86
MK
125 }\r
126}\r
127\r
128/**\r
129 Performs an atomic compare exchange operation to release semaphore\r
130 for each AP.\r
131\r
132**/\r
133VOID\r
134ReleaseAllAPs (\r
135 VOID\r
136 )\r
137{\r
138 UINTN Index;\r
529a5a86 139\r
123b720e 140 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
51dd408a 141 if (IsPresentAp (Index)) {\r
ed3d5ecb 142 ReleaseSemaphore (mSmmMpSyncData->CpuData[Index].Run);\r
529a5a86
MK
143 }\r
144 }\r
145}\r
146\r
147/**\r
148 Checks if all CPUs (with certain exceptions) have checked in for this SMI run\r
149\r
150 @param Exceptions CPU Arrival exception flags.\r
151\r
152 @retval TRUE if all CPUs the have checked in.\r
153 @retval FALSE if at least one Normal AP hasn't checked in.\r
154\r
155**/\r
156BOOLEAN\r
157AllCpusInSmmWithExceptions (\r
158 SMM_CPU_ARRIVAL_EXCEPTIONS Exceptions\r
159 )\r
160{\r
161 UINTN Index;\r
162 SMM_CPU_DATA_BLOCK *CpuData;\r
163 EFI_PROCESSOR_INFORMATION *ProcessorInfo;\r
164\r
fe3a75bc 165 ASSERT (*mSmmMpSyncData->Counter <= mNumberOfCpus);\r
529a5a86 166\r
fe3a75bc 167 if (*mSmmMpSyncData->Counter == mNumberOfCpus) {\r
529a5a86
MK
168 return TRUE;\r
169 }\r
170\r
171 CpuData = mSmmMpSyncData->CpuData;\r
172 ProcessorInfo = gSmmCpuPrivate->ProcessorInfo;\r
123b720e 173 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
ed3d5ecb 174 if (!(*(CpuData[Index].Present)) && ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) {\r
529a5a86
MK
175 if (((Exceptions & ARRIVAL_EXCEPTION_DELAYED) != 0) && SmmCpuFeaturesGetSmmRegister (Index, SmmRegSmmDelayed) != 0) {\r
176 continue;\r
177 }\r
178 if (((Exceptions & ARRIVAL_EXCEPTION_BLOCKED) != 0) && SmmCpuFeaturesGetSmmRegister (Index, SmmRegSmmBlocked) != 0) {\r
179 continue;\r
180 }\r
181 if (((Exceptions & ARRIVAL_EXCEPTION_SMI_DISABLED) != 0) && SmmCpuFeaturesGetSmmRegister (Index, SmmRegSmmEnable) != 0) {\r
182 continue;\r
183 }\r
184 return FALSE;\r
185 }\r
186 }\r
187\r
188\r
189 return TRUE;\r
190}\r
191\r
12c66382
ED
192/**\r
193 Has OS enabled Lmce in the MSR_IA32_MCG_EXT_CTL\r
7367cc6c 194\r
12c66382
ED
195 @retval TRUE Os enable lmce.\r
196 @retval FALSE Os not enable lmce.\r
197\r
198**/\r
199BOOLEAN\r
200IsLmceOsEnabled (\r
201 VOID\r
202 )\r
203{\r
204 MSR_IA32_MCG_CAP_REGISTER McgCap;\r
205 MSR_IA32_FEATURE_CONTROL_REGISTER FeatureCtrl;\r
206 MSR_IA32_MCG_EXT_CTL_REGISTER McgExtCtrl;\r
207\r
208 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);\r
209 if (McgCap.Bits.MCG_LMCE_P == 0) {\r
210 return FALSE;\r
211 }\r
212\r
213 FeatureCtrl.Uint64 = AsmReadMsr64 (MSR_IA32_FEATURE_CONTROL);\r
214 if (FeatureCtrl.Bits.LmceOn == 0) {\r
215 return FALSE;\r
216 }\r
217\r
218 McgExtCtrl.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_EXT_CTL);\r
219 return (BOOLEAN) (McgExtCtrl.Bits.LMCE_EN == 1);\r
220}\r
221\r
222/**\r
7367cc6c 223 Return if Local machine check exception signaled.\r
12c66382 224\r
7367cc6c 225 Indicates (when set) that a local machine check exception was generated. This indicates that the current machine-check event was\r
12c66382
ED
226 delivered to only the logical processor.\r
227\r
228 @retval TRUE LMCE was signaled.\r
229 @retval FALSE LMCE was not signaled.\r
230\r
231**/\r
232BOOLEAN\r
233IsLmceSignaled (\r
234 VOID\r
235 )\r
236{\r
237 MSR_IA32_MCG_STATUS_REGISTER McgStatus;\r
238\r
239 McgStatus.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_STATUS);\r
240 return (BOOLEAN) (McgStatus.Bits.LMCE_S == 1);\r
241}\r
529a5a86
MK
242\r
243/**\r
244 Given timeout constraint, wait for all APs to arrive, and insure when this function returns, no AP will execute normal mode code before\r
245 entering SMM, except SMI disabled APs.\r
246\r
247**/\r
248VOID\r
249SmmWaitForApArrival (\r
250 VOID\r
251 )\r
252{\r
253 UINT64 Timer;\r
254 UINTN Index;\r
12c66382
ED
255 BOOLEAN LmceEn;\r
256 BOOLEAN LmceSignal;\r
529a5a86 257\r
fe3a75bc 258 ASSERT (*mSmmMpSyncData->Counter <= mNumberOfCpus);\r
529a5a86 259\r
ba40cb31
MK
260 LmceEn = FALSE;\r
261 LmceSignal = FALSE;\r
262 if (mMachineCheckSupported) {\r
263 LmceEn = IsLmceOsEnabled ();\r
264 LmceSignal = IsLmceSignaled();\r
265 }\r
12c66382 266\r
529a5a86
MK
267 //\r
268 // Platform implementor should choose a timeout value appropriately:\r
269 // - The timeout value should balance the SMM time constrains and the likelihood that delayed CPUs are excluded in the SMM run. Note\r
270 // the SMI Handlers must ALWAYS take into account the cases that not all APs are available in an SMI run.\r
271 // - The timeout value must, in the case of 2nd timeout, be at least long enough to give time for all APs to receive the SMI IPI\r
272 // and either enter SMM or buffer the SMI, to insure there is no CPU running normal mode code when SMI handling starts. This will\r
273 // be TRUE even if a blocked CPU is brought out of the blocked state by a normal mode CPU (before the normal mode CPU received the\r
274 // SMI IPI), because with a buffered SMI, and CPU will enter SMM immediately after it is brought out of the blocked state.\r
275 // - The timeout value must be longer than longest possible IO operation in the system\r
276 //\r
277\r
278 //\r
279 // Sync with APs 1st timeout\r
280 //\r
281 for (Timer = StartSyncTimer ();\r
12c66382 282 !IsSyncTimerTimeout (Timer) && !(LmceEn && LmceSignal) &&\r
529a5a86
MK
283 !AllCpusInSmmWithExceptions (ARRIVAL_EXCEPTION_BLOCKED | ARRIVAL_EXCEPTION_SMI_DISABLED );\r
284 ) {\r
285 CpuPause ();\r
286 }\r
287\r
288 //\r
289 // Not all APs have arrived, so we need 2nd round of timeout. IPIs should be sent to ALL none present APs,\r
290 // because:\r
291 // a) Delayed AP may have just come out of the delayed state. Blocked AP may have just been brought out of blocked state by some AP running\r
292 // normal mode code. These APs need to be guaranteed to have an SMI pending to insure that once they are out of delayed / blocked state, they\r
293 // enter SMI immediately without executing instructions in normal mode. Note traditional flow requires there are no APs doing normal mode\r
294 // work while SMI handling is on-going.\r
295 // b) As a consequence of SMI IPI sending, (spurious) SMI may occur after this SMM run.\r
296 // c) ** NOTE **: Use SMI disabling feature VERY CAREFULLY (if at all) for traditional flow, because a processor in SMI-disabled state\r
297 // will execute normal mode code, which breaks the traditional SMI handlers' assumption that no APs are doing normal\r
298 // mode work while SMI handling is on-going.\r
299 // d) We don't add code to check SMI disabling status to skip sending IPI to SMI disabled APs, because:\r
300 // - In traditional flow, SMI disabling is discouraged.\r
301 // - In relaxed flow, CheckApArrival() will check SMI disabling status before calling this function.\r
302 // In both cases, adding SMI-disabling checking code increases overhead.\r
303 //\r
fe3a75bc 304 if (*mSmmMpSyncData->Counter < mNumberOfCpus) {\r
529a5a86
MK
305 //\r
306 // Send SMI IPIs to bring outside processors in\r
307 //\r
123b720e 308 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
ed3d5ecb 309 if (!(*(mSmmMpSyncData->CpuData[Index].Present)) && gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) {\r
529a5a86
MK
310 SendSmiIpi ((UINT32)gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId);\r
311 }\r
312 }\r
313\r
314 //\r
315 // Sync with APs 2nd timeout.\r
316 //\r
317 for (Timer = StartSyncTimer ();\r
318 !IsSyncTimerTimeout (Timer) &&\r
319 !AllCpusInSmmWithExceptions (ARRIVAL_EXCEPTION_BLOCKED | ARRIVAL_EXCEPTION_SMI_DISABLED );\r
320 ) {\r
321 CpuPause ();\r
322 }\r
323 }\r
324\r
325 return;\r
326}\r
327\r
328\r
329/**\r
330 Replace OS MTRR's with SMI MTRR's.\r
331\r
332 @param CpuIndex Processor Index\r
333\r
334**/\r
335VOID\r
336ReplaceOSMtrrs (\r
337 IN UINTN CpuIndex\r
338 )\r
339{\r
529a5a86
MK
340 SmmCpuFeaturesDisableSmrr ();\r
341\r
342 //\r
343 // Replace all MTRRs registers\r
344 //\r
26ab5ac3 345 MtrrSetAllMtrrs (&gSmiMtrrs);\r
529a5a86
MK
346}\r
347\r
51dd408a
ED
348/**\r
349 Wheck whether task has been finished by all APs.\r
350\r
351 @param BlockMode Whether did it in block mode or non-block mode.\r
352\r
353 @retval TRUE Task has been finished by all APs.\r
354 @retval FALSE Task not has been finished by all APs.\r
355\r
356**/\r
357BOOLEAN\r
358WaitForAllAPsNotBusy (\r
359 IN BOOLEAN BlockMode\r
360 )\r
361{\r
362 UINTN Index;\r
363\r
123b720e 364 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
51dd408a
ED
365 //\r
366 // Ignore BSP and APs which not call in SMM.\r
367 //\r
368 if (!IsPresentAp(Index)) {\r
369 continue;\r
370 }\r
371\r
372 if (BlockMode) {\r
373 AcquireSpinLock(mSmmMpSyncData->CpuData[Index].Busy);\r
374 ReleaseSpinLock(mSmmMpSyncData->CpuData[Index].Busy);\r
375 } else {\r
376 if (AcquireSpinLockOrFail (mSmmMpSyncData->CpuData[Index].Busy)) {\r
377 ReleaseSpinLock(mSmmMpSyncData->CpuData[Index].Busy);\r
378 } else {\r
379 return FALSE;\r
380 }\r
381 }\r
382 }\r
383\r
384 return TRUE;\r
385}\r
386\r
387/**\r
388 Check whether it is an present AP.\r
389\r
390 @param CpuIndex The AP index which calls this function.\r
391\r
392 @retval TRUE It's a present AP.\r
393 @retval TRUE This is not an AP or it is not present.\r
394\r
395**/\r
396BOOLEAN\r
397IsPresentAp (\r
398 IN UINTN CpuIndex\r
399 )\r
400{\r
401 return ((CpuIndex != gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) &&\r
402 *(mSmmMpSyncData->CpuData[CpuIndex].Present));\r
403}\r
404\r
51dd408a
ED
405/**\r
406 Clean up the status flags used during executing the procedure.\r
407\r
408 @param CpuIndex The AP index which calls this function.\r
409\r
410**/\r
411VOID\r
412ReleaseToken (\r
413 IN UINTN CpuIndex\r
414 )\r
415{\r
a457823f 416 PROCEDURE_TOKEN *Token;\r
51dd408a 417\r
a457823f
ED
418 Token = mSmmMpSyncData->CpuData[CpuIndex].Token;\r
419\r
420 if (InterlockedDecrement (&Token->RunningApCount) == 0) {\r
421 ReleaseSpinLock (Token->SpinLock);\r
51dd408a 422 }\r
a457823f
ED
423\r
424 mSmmMpSyncData->CpuData[CpuIndex].Token = NULL;\r
51dd408a
ED
425}\r
426\r
427/**\r
428 Free the tokens in the maintained list.\r
429\r
430**/\r
431VOID\r
432FreeTokens (\r
433 VOID\r
434 )\r
435{\r
436 LIST_ENTRY *Link;\r
437 PROCEDURE_TOKEN *ProcToken;\r
9caaa79d
ED
438 TOKEN_BUFFER *TokenBuf;\r
439\r
440 //\r
441 // Only free the token buffer recorded in the OldTOkenBufList\r
442 // upon exiting SMI. Current token buffer stays allocated so\r
443 // next SMI doesn't need to re-allocate.\r
444 //\r
445 gSmmCpuPrivate->UsedTokenNum = 0;\r
446\r
447 Link = GetFirstNode (&gSmmCpuPrivate->OldTokenBufList);\r
448 while (!IsNull (&gSmmCpuPrivate->OldTokenBufList, Link)) {\r
449 TokenBuf = TOKEN_BUFFER_FROM_LINK (Link);\r
450\r
451 Link = RemoveEntryList (&TokenBuf->Link);\r
452\r
453 FreePool (TokenBuf->Buffer);\r
454 FreePool (TokenBuf);\r
455 }\r
51dd408a
ED
456\r
457 while (!IsListEmpty (&gSmmCpuPrivate->TokenList)) {\r
458 Link = GetFirstNode (&gSmmCpuPrivate->TokenList);\r
459 ProcToken = PROCEDURE_TOKEN_FROM_LINK (Link);\r
460\r
461 RemoveEntryList (&ProcToken->Link);\r
462\r
51dd408a
ED
463 FreePool (ProcToken);\r
464 }\r
465}\r
466\r
529a5a86
MK
467/**\r
468 SMI handler for BSP.\r
469\r
470 @param CpuIndex BSP processor Index\r
471 @param SyncMode SMM MP sync mode\r
472\r
473**/\r
474VOID\r
475BSPHandler (\r
476 IN UINTN CpuIndex,\r
477 IN SMM_CPU_SYNC_MODE SyncMode\r
478 )\r
479{\r
480 UINTN Index;\r
481 MTRR_SETTINGS Mtrrs;\r
482 UINTN ApCount;\r
483 BOOLEAN ClearTopLevelSmiResult;\r
484 UINTN PresentCount;\r
485\r
486 ASSERT (CpuIndex == mSmmMpSyncData->BspIndex);\r
487 ApCount = 0;\r
488\r
489 //\r
490 // Flag BSP's presence\r
491 //\r
fe3a75bc 492 *mSmmMpSyncData->InsideSmm = TRUE;\r
529a5a86
MK
493\r
494 //\r
495 // Initialize Debug Agent to start source level debug in BSP handler\r
496 //\r
497 InitializeDebugAgent (DEBUG_AGENT_INIT_ENTER_SMI, NULL, NULL);\r
498\r
499 //\r
500 // Mark this processor's presence\r
501 //\r
ed3d5ecb 502 *(mSmmMpSyncData->CpuData[CpuIndex].Present) = TRUE;\r
529a5a86
MK
503\r
504 //\r
505 // Clear platform top level SMI status bit before calling SMI handlers. If\r
506 // we cleared it after SMI handlers are run, we would miss the SMI that\r
507 // occurs after SMI handlers are done and before SMI status bit is cleared.\r
508 //\r
509 ClearTopLevelSmiResult = ClearTopLevelSmiStatus();\r
510 ASSERT (ClearTopLevelSmiResult == TRUE);\r
511\r
512 //\r
513 // Set running processor index\r
514 //\r
515 gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu = CpuIndex;\r
516\r
517 //\r
518 // If Traditional Sync Mode or need to configure MTRRs: gather all available APs.\r
519 //\r
520 if (SyncMode == SmmCpuSyncModeTradition || SmmCpuFeaturesNeedConfigureMtrrs()) {\r
521\r
522 //\r
523 // Wait for APs to arrive\r
524 //\r
525 SmmWaitForApArrival();\r
526\r
527 //\r
528 // Lock the counter down and retrieve the number of APs\r
529 //\r
fe3a75bc
JF
530 *mSmmMpSyncData->AllCpusInSync = TRUE;\r
531 ApCount = LockdownSemaphore (mSmmMpSyncData->Counter) - 1;\r
529a5a86
MK
532\r
533 //\r
534 // Wait for all APs to get ready for programming MTRRs\r
535 //\r
536 WaitForAllAPs (ApCount);\r
537\r
538 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
539 //\r
540 // Signal all APs it's time for backup MTRRs\r
541 //\r
542 ReleaseAllAPs ();\r
543\r
544 //\r
545 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at\r
546 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set\r
547 // to a large enough value to avoid this situation.\r
548 // Note: For HT capable CPUs, threads within a core share the same set of MTRRs.\r
549 // We do the backup first and then set MTRR to avoid race condition for threads\r
550 // in the same core.\r
551 //\r
552 MtrrGetAllMtrrs(&Mtrrs);\r
553\r
554 //\r
555 // Wait for all APs to complete their MTRR saving\r
556 //\r
557 WaitForAllAPs (ApCount);\r
558\r
559 //\r
560 // Let all processors program SMM MTRRs together\r
561 //\r
562 ReleaseAllAPs ();\r
563\r
564 //\r
565 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at\r
566 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set\r
567 // to a large enough value to avoid this situation.\r
568 //\r
569 ReplaceOSMtrrs (CpuIndex);\r
570\r
571 //\r
572 // Wait for all APs to complete their MTRR programming\r
573 //\r
574 WaitForAllAPs (ApCount);\r
575 }\r
576 }\r
577\r
578 //\r
579 // The BUSY lock is initialized to Acquired state\r
580 //\r
170a3c1e 581 AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
529a5a86
MK
582\r
583 //\r
9f419739 584 // Perform the pre tasks\r
529a5a86 585 //\r
9f419739 586 PerformPreTasks ();\r
529a5a86
MK
587\r
588 //\r
589 // Invoke SMM Foundation EntryPoint with the processor information context.\r
590 //\r
591 gSmmCpuPrivate->SmmCoreEntry (&gSmmCpuPrivate->SmmCoreEntryContext);\r
592\r
593 //\r
594 // Make sure all APs have completed their pending none-block tasks\r
595 //\r
51dd408a 596 WaitForAllAPsNotBusy (TRUE);\r
529a5a86
MK
597\r
598 //\r
599 // Perform the remaining tasks\r
600 //\r
601 PerformRemainingTasks ();\r
602\r
603 //\r
604 // If Relaxed-AP Sync Mode: gather all available APs after BSP SMM handlers are done, and\r
605 // make those APs to exit SMI synchronously. APs which arrive later will be excluded and\r
606 // will run through freely.\r
607 //\r
608 if (SyncMode != SmmCpuSyncModeTradition && !SmmCpuFeaturesNeedConfigureMtrrs()) {\r
609\r
610 //\r
611 // Lock the counter down and retrieve the number of APs\r
612 //\r
fe3a75bc
JF
613 *mSmmMpSyncData->AllCpusInSync = TRUE;\r
614 ApCount = LockdownSemaphore (mSmmMpSyncData->Counter) - 1;\r
529a5a86
MK
615 //\r
616 // Make sure all APs have their Present flag set\r
617 //\r
618 while (TRUE) {\r
619 PresentCount = 0;\r
123b720e 620 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
ed3d5ecb 621 if (*(mSmmMpSyncData->CpuData[Index].Present)) {\r
529a5a86
MK
622 PresentCount ++;\r
623 }\r
624 }\r
625 if (PresentCount > ApCount) {\r
626 break;\r
627 }\r
628 }\r
629 }\r
630\r
631 //\r
632 // Notify all APs to exit\r
633 //\r
fe3a75bc 634 *mSmmMpSyncData->InsideSmm = FALSE;\r
529a5a86
MK
635 ReleaseAllAPs ();\r
636\r
637 //\r
638 // Wait for all APs to complete their pending tasks\r
639 //\r
640 WaitForAllAPs (ApCount);\r
641\r
642 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
643 //\r
644 // Signal APs to restore MTRRs\r
645 //\r
646 ReleaseAllAPs ();\r
647\r
648 //\r
649 // Restore OS MTRRs\r
650 //\r
651 SmmCpuFeaturesReenableSmrr ();\r
652 MtrrSetAllMtrrs(&Mtrrs);\r
653\r
654 //\r
655 // Wait for all APs to complete MTRR programming\r
656 //\r
657 WaitForAllAPs (ApCount);\r
658 }\r
659\r
660 //\r
661 // Stop source level debug in BSP handler, the code below will not be\r
662 // debugged.\r
663 //\r
664 InitializeDebugAgent (DEBUG_AGENT_INIT_EXIT_SMI, NULL, NULL);\r
665\r
666 //\r
667 // Signal APs to Reset states/semaphore for this processor\r
668 //\r
669 ReleaseAllAPs ();\r
670\r
671 //\r
672 // Perform pending operations for hot-plug\r
673 //\r
674 SmmCpuUpdate ();\r
675\r
676 //\r
677 // Clear the Present flag of BSP\r
678 //\r
ed3d5ecb 679 *(mSmmMpSyncData->CpuData[CpuIndex].Present) = FALSE;\r
529a5a86
MK
680\r
681 //\r
682 // Gather APs to exit SMM synchronously. Note the Present flag is cleared by now but\r
683 // WaitForAllAps does not depend on the Present flag.\r
684 //\r
685 WaitForAllAPs (ApCount);\r
686\r
51dd408a
ED
687 //\r
688 // Clean the tokens buffer.\r
689 //\r
690 FreeTokens ();\r
691\r
529a5a86
MK
692 //\r
693 // Reset BspIndex to -1, meaning BSP has not been elected.\r
694 //\r
695 if (FeaturePcdGet (PcdCpuSmmEnableBspElection)) {\r
696 mSmmMpSyncData->BspIndex = (UINT32)-1;\r
697 }\r
698\r
699 //\r
700 // Allow APs to check in from this point on\r
701 //\r
fe3a75bc
JF
702 *mSmmMpSyncData->Counter = 0;\r
703 *mSmmMpSyncData->AllCpusInSync = FALSE;\r
529a5a86
MK
704}\r
705\r
706/**\r
707 SMI handler for AP.\r
708\r
709 @param CpuIndex AP processor Index.\r
710 @param ValidSmi Indicates that current SMI is a valid SMI or not.\r
711 @param SyncMode SMM MP sync mode.\r
712\r
713**/\r
714VOID\r
715APHandler (\r
716 IN UINTN CpuIndex,\r
717 IN BOOLEAN ValidSmi,\r
718 IN SMM_CPU_SYNC_MODE SyncMode\r
719 )\r
720{\r
721 UINT64 Timer;\r
722 UINTN BspIndex;\r
723 MTRR_SETTINGS Mtrrs;\r
51dd408a 724 EFI_STATUS ProcedureStatus;\r
529a5a86
MK
725\r
726 //\r
727 // Timeout BSP\r
728 //\r
729 for (Timer = StartSyncTimer ();\r
730 !IsSyncTimerTimeout (Timer) &&\r
fe3a75bc 731 !(*mSmmMpSyncData->InsideSmm);\r
529a5a86
MK
732 ) {\r
733 CpuPause ();\r
734 }\r
735\r
fe3a75bc 736 if (!(*mSmmMpSyncData->InsideSmm)) {\r
529a5a86
MK
737 //\r
738 // BSP timeout in the first round\r
739 //\r
740 if (mSmmMpSyncData->BspIndex != -1) {\r
741 //\r
742 // BSP Index is known\r
743 //\r
744 BspIndex = mSmmMpSyncData->BspIndex;\r
745 ASSERT (CpuIndex != BspIndex);\r
746\r
747 //\r
748 // Send SMI IPI to bring BSP in\r
749 //\r
750 SendSmiIpi ((UINT32)gSmmCpuPrivate->ProcessorInfo[BspIndex].ProcessorId);\r
751\r
752 //\r
753 // Now clock BSP for the 2nd time\r
754 //\r
755 for (Timer = StartSyncTimer ();\r
756 !IsSyncTimerTimeout (Timer) &&\r
fe3a75bc 757 !(*mSmmMpSyncData->InsideSmm);\r
529a5a86
MK
758 ) {\r
759 CpuPause ();\r
760 }\r
761\r
fe3a75bc 762 if (!(*mSmmMpSyncData->InsideSmm)) {\r
529a5a86
MK
763 //\r
764 // Give up since BSP is unable to enter SMM\r
765 // and signal the completion of this AP\r
fe3a75bc 766 WaitForSemaphore (mSmmMpSyncData->Counter);\r
529a5a86
MK
767 return;\r
768 }\r
769 } else {\r
770 //\r
771 // Don't know BSP index. Give up without sending IPI to BSP.\r
772 //\r
fe3a75bc 773 WaitForSemaphore (mSmmMpSyncData->Counter);\r
529a5a86
MK
774 return;\r
775 }\r
776 }\r
777\r
778 //\r
779 // BSP is available\r
780 //\r
781 BspIndex = mSmmMpSyncData->BspIndex;\r
782 ASSERT (CpuIndex != BspIndex);\r
783\r
784 //\r
785 // Mark this processor's presence\r
786 //\r
ed3d5ecb 787 *(mSmmMpSyncData->CpuData[CpuIndex].Present) = TRUE;\r
529a5a86
MK
788\r
789 if (SyncMode == SmmCpuSyncModeTradition || SmmCpuFeaturesNeedConfigureMtrrs()) {\r
790 //\r
791 // Notify BSP of arrival at this point\r
792 //\r
ed3d5ecb 793 ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run);\r
529a5a86
MK
794 }\r
795\r
796 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
797 //\r
798 // Wait for the signal from BSP to backup MTRRs\r
799 //\r
ed3d5ecb 800 WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run);\r
529a5a86
MK
801\r
802 //\r
803 // Backup OS MTRRs\r
804 //\r
805 MtrrGetAllMtrrs(&Mtrrs);\r
806\r
807 //\r
808 // Signal BSP the completion of this AP\r
809 //\r
ed3d5ecb 810 ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run);\r
529a5a86
MK
811\r
812 //\r
813 // Wait for BSP's signal to program MTRRs\r
814 //\r
ed3d5ecb 815 WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run);\r
529a5a86
MK
816\r
817 //\r
818 // Replace OS MTRRs with SMI MTRRs\r
819 //\r
820 ReplaceOSMtrrs (CpuIndex);\r
821\r
822 //\r
823 // Signal BSP the completion of this AP\r
824 //\r
ed3d5ecb 825 ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run);\r
529a5a86
MK
826 }\r
827\r
828 while (TRUE) {\r
829 //\r
830 // Wait for something to happen\r
831 //\r
ed3d5ecb 832 WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run);\r
529a5a86
MK
833\r
834 //\r
835 // Check if BSP wants to exit SMM\r
836 //\r
fe3a75bc 837 if (!(*mSmmMpSyncData->InsideSmm)) {\r
529a5a86
MK
838 break;\r
839 }\r
840\r
841 //\r
842 // BUSY should be acquired by SmmStartupThisAp()\r
843 //\r
844 ASSERT (\r
ed3d5ecb 845 !AcquireSpinLockOrFail (mSmmMpSyncData->CpuData[CpuIndex].Busy)\r
529a5a86
MK
846 );\r
847\r
848 //\r
849 // Invoke the scheduled procedure\r
850 //\r
51dd408a
ED
851 ProcedureStatus = (*mSmmMpSyncData->CpuData[CpuIndex].Procedure) (\r
852 (VOID*)mSmmMpSyncData->CpuData[CpuIndex].Parameter\r
853 );\r
854 if (mSmmMpSyncData->CpuData[CpuIndex].Status != NULL) {\r
855 *mSmmMpSyncData->CpuData[CpuIndex].Status = ProcedureStatus;\r
856 }\r
529a5a86 857\r
a457823f
ED
858 if (mSmmMpSyncData->CpuData[CpuIndex].Token != NULL) {\r
859 ReleaseToken (CpuIndex);\r
860 }\r
861\r
529a5a86
MK
862 //\r
863 // Release BUSY\r
864 //\r
ed3d5ecb 865 ReleaseSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
529a5a86
MK
866 }\r
867\r
868 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
869 //\r
870 // Notify BSP the readiness of this AP to program MTRRs\r
871 //\r
ed3d5ecb 872 ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run);\r
529a5a86
MK
873\r
874 //\r
875 // Wait for the signal from BSP to program MTRRs\r
876 //\r
ed3d5ecb 877 WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run);\r
529a5a86
MK
878\r
879 //\r
880 // Restore OS MTRRs\r
881 //\r
882 SmmCpuFeaturesReenableSmrr ();\r
883 MtrrSetAllMtrrs(&Mtrrs);\r
884 }\r
885\r
886 //\r
887 // Notify BSP the readiness of this AP to Reset states/semaphore for this processor\r
888 //\r
ed3d5ecb 889 ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run);\r
529a5a86
MK
890\r
891 //\r
892 // Wait for the signal from BSP to Reset states/semaphore for this processor\r
893 //\r
ed3d5ecb 894 WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run);\r
529a5a86
MK
895\r
896 //\r
897 // Reset states/semaphore for this processor\r
898 //\r
ed3d5ecb 899 *(mSmmMpSyncData->CpuData[CpuIndex].Present) = FALSE;\r
529a5a86
MK
900\r
901 //\r
902 // Notify BSP the readiness of this AP to exit SMM\r
903 //\r
ed3d5ecb 904 ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run);\r
529a5a86
MK
905\r
906}\r
907\r
908/**\r
909 Create 4G PageTable in SMRAM.\r
910\r
717fb604 911 @param[in] Is32BitPageTable Whether the page table is 32-bit PAE\r
529a5a86
MK
912 @return PageTable Address\r
913\r
914**/\r
915UINT32\r
916Gen4GPageTable (\r
881520ea 917 IN BOOLEAN Is32BitPageTable\r
529a5a86
MK
918 )\r
919{\r
920 VOID *PageTable;\r
921 UINTN Index;\r
922 UINT64 *Pte;\r
923 UINTN PagesNeeded;\r
924 UINTN Low2MBoundary;\r
925 UINTN High2MBoundary;\r
926 UINTN Pages;\r
927 UINTN GuardPage;\r
928 UINT64 *Pdpte;\r
929 UINTN PageIndex;\r
930 UINTN PageAddress;\r
931\r
932 Low2MBoundary = 0;\r
933 High2MBoundary = 0;\r
934 PagesNeeded = 0;\r
935 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
936 //\r
937 // Add one more page for known good stack, then find the lower 2MB aligned address.\r
938 //\r
939 Low2MBoundary = (mSmmStackArrayBase + EFI_PAGE_SIZE) & ~(SIZE_2MB-1);\r
940 //\r
941 // Add two more pages for known good stack and stack guard page,\r
942 // then find the lower 2MB aligned address.\r
943 //\r
944 High2MBoundary = (mSmmStackArrayEnd - mSmmStackSize + EFI_PAGE_SIZE * 2) & ~(SIZE_2MB-1);\r
945 PagesNeeded = ((High2MBoundary - Low2MBoundary) / SIZE_2MB) + 1;\r
946 }\r
947 //\r
948 // Allocate the page table\r
949 //\r
717fb604 950 PageTable = AllocatePageTableMemory (5 + PagesNeeded);\r
529a5a86
MK
951 ASSERT (PageTable != NULL);\r
952\r
717fb604 953 PageTable = (VOID *)((UINTN)PageTable);\r
529a5a86
MK
954 Pte = (UINT64*)PageTable;\r
955\r
956 //\r
957 // Zero out all page table entries first\r
958 //\r
959 ZeroMem (Pte, EFI_PAGES_TO_SIZE (1));\r
960\r
961 //\r
962 // Set Page Directory Pointers\r
963 //\r
964 for (Index = 0; Index < 4; Index++) {\r
e62a0eb6 965 Pte[Index] = ((UINTN)PageTable + EFI_PAGE_SIZE * (Index + 1)) | mAddressEncMask |\r
241f9149 966 (Is32BitPageTable ? IA32_PAE_PDPTE_ATTRIBUTE_BITS : PAGE_ATTRIBUTE_BITS);\r
529a5a86
MK
967 }\r
968 Pte += EFI_PAGE_SIZE / sizeof (*Pte);\r
969\r
970 //\r
971 // Fill in Page Directory Entries\r
972 //\r
973 for (Index = 0; Index < EFI_PAGE_SIZE * 4 / sizeof (*Pte); Index++) {\r
241f9149 974 Pte[Index] = (Index << 21) | mAddressEncMask | IA32_PG_PS | PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
975 }\r
976\r
f8c1133b 977 Pdpte = (UINT64*)PageTable;\r
529a5a86
MK
978 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
979 Pages = (UINTN)PageTable + EFI_PAGES_TO_SIZE (5);\r
980 GuardPage = mSmmStackArrayBase + EFI_PAGE_SIZE;\r
529a5a86 981 for (PageIndex = Low2MBoundary; PageIndex <= High2MBoundary; PageIndex += SIZE_2MB) {\r
241f9149
LD
982 Pte = (UINT64*)(UINTN)(Pdpte[BitFieldRead32 ((UINT32)PageIndex, 30, 31)] & ~mAddressEncMask & ~(EFI_PAGE_SIZE - 1));\r
983 Pte[BitFieldRead32 ((UINT32)PageIndex, 21, 29)] = (UINT64)Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
984 //\r
985 // Fill in Page Table Entries\r
986 //\r
987 Pte = (UINT64*)Pages;\r
988 PageAddress = PageIndex;\r
989 for (Index = 0; Index < EFI_PAGE_SIZE / sizeof (*Pte); Index++) {\r
990 if (PageAddress == GuardPage) {\r
991 //\r
992 // Mark the guard page as non-present\r
993 //\r
241f9149 994 Pte[Index] = PageAddress | mAddressEncMask;\r
529a5a86
MK
995 GuardPage += mSmmStackSize;\r
996 if (GuardPage > mSmmStackArrayEnd) {\r
997 GuardPage = 0;\r
998 }\r
999 } else {\r
241f9149 1000 Pte[Index] = PageAddress | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
1001 }\r
1002 PageAddress+= EFI_PAGE_SIZE;\r
1003 }\r
1004 Pages += EFI_PAGE_SIZE;\r
1005 }\r
1006 }\r
1007\r
f8c1133b
JW
1008 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT1) != 0) {\r
1009 Pte = (UINT64*)(UINTN)(Pdpte[0] & ~mAddressEncMask & ~(EFI_PAGE_SIZE - 1));\r
1010 if ((Pte[0] & IA32_PG_PS) == 0) {\r
1011 // 4K-page entries are already mapped. Just hide the first one anyway.\r
1012 Pte = (UINT64*)(UINTN)(Pte[0] & ~mAddressEncMask & ~(EFI_PAGE_SIZE - 1));\r
79da2d28 1013 Pte[0] &= ~(UINT64)IA32_PG_P; // Hide page 0\r
f8c1133b
JW
1014 } else {\r
1015 // Create 4K-page entries\r
1016 Pages = (UINTN)AllocatePageTableMemory (1);\r
1017 ASSERT (Pages != 0);\r
1018\r
1019 Pte[0] = (UINT64)(Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS);\r
1020\r
1021 Pte = (UINT64*)Pages;\r
1022 PageAddress = 0;\r
1023 Pte[0] = PageAddress | mAddressEncMask; // Hide page 0 but present left\r
1024 for (Index = 1; Index < EFI_PAGE_SIZE / sizeof (*Pte); Index++) {\r
1025 PageAddress += EFI_PAGE_SIZE;\r
1026 Pte[Index] = PageAddress | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
1027 }\r
1028 }\r
1029 }\r
1030\r
529a5a86
MK
1031 return (UINT32)(UINTN)PageTable;\r
1032}\r
1033\r
51dd408a
ED
1034/**\r
1035 Checks whether the input token is the current used token.\r
1036\r
1037 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or\r
1038 BroadcastProcedure.\r
1039\r
1040 @retval TRUE The input token is the current used token.\r
1041 @retval FALSE The input token is not the current used token.\r
1042**/\r
1043BOOLEAN\r
1044IsTokenInUse (\r
1045 IN SPIN_LOCK *Token\r
1046 )\r
1047{\r
1048 LIST_ENTRY *Link;\r
1049 PROCEDURE_TOKEN *ProcToken;\r
1050\r
1051 if (Token == NULL) {\r
1052 return FALSE;\r
1053 }\r
1054\r
1055 Link = GetFirstNode (&gSmmCpuPrivate->TokenList);\r
1056 while (!IsNull (&gSmmCpuPrivate->TokenList, Link)) {\r
1057 ProcToken = PROCEDURE_TOKEN_FROM_LINK (Link);\r
1058\r
a457823f 1059 if (ProcToken->SpinLock == Token) {\r
51dd408a
ED
1060 return TRUE;\r
1061 }\r
1062\r
1063 Link = GetNextNode (&gSmmCpuPrivate->TokenList, Link);\r
1064 }\r
1065\r
1066 return FALSE;\r
1067}\r
1068\r
1069/**\r
1070 create token and save it to the maintain list.\r
1071\r
a457823f
ED
1072 @param RunningApCount Input running AP count.\r
1073\r
51dd408a
ED
1074 @retval return the spin lock used as token.\r
1075\r
1076**/\r
a457823f 1077PROCEDURE_TOKEN *\r
51dd408a 1078CreateToken (\r
a457823f 1079 IN UINT32 RunningApCount\r
51dd408a
ED
1080 )\r
1081{\r
9caaa79d 1082 PROCEDURE_TOKEN *ProcToken;\r
a457823f 1083 SPIN_LOCK *SpinLock;\r
51dd408a 1084 UINTN SpinLockSize;\r
9caaa79d
ED
1085 TOKEN_BUFFER *TokenBuf;\r
1086 UINT32 TokenCountPerChunk;\r
51dd408a
ED
1087\r
1088 SpinLockSize = GetSpinLockProperties ();\r
9caaa79d
ED
1089 TokenCountPerChunk = FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk);\r
1090\r
1091 if (gSmmCpuPrivate->UsedTokenNum == TokenCountPerChunk) {\r
1092 DEBUG ((DEBUG_VERBOSE, "CpuSmm: No free token buffer, allocate new buffer!\n"));\r
1093\r
1094 //\r
1095 // Record current token buffer for later free action usage.\r
1096 // Current used token buffer not in this list.\r
1097 //\r
1098 TokenBuf = AllocatePool (sizeof (TOKEN_BUFFER));\r
1099 ASSERT (TokenBuf != NULL);\r
1100 TokenBuf->Signature = TOKEN_BUFFER_SIGNATURE;\r
1101 TokenBuf->Buffer = gSmmCpuPrivate->CurrentTokenBuf;\r
1102\r
1103 InsertTailList (&gSmmCpuPrivate->OldTokenBufList, &TokenBuf->Link);\r
1104\r
1105 gSmmCpuPrivate->CurrentTokenBuf = AllocatePool (SpinLockSize * TokenCountPerChunk);\r
1106 ASSERT (gSmmCpuPrivate->CurrentTokenBuf != NULL);\r
1107 gSmmCpuPrivate->UsedTokenNum = 0;\r
1108 }\r
1109\r
a457823f 1110 SpinLock = (SPIN_LOCK *)(gSmmCpuPrivate->CurrentTokenBuf + SpinLockSize * gSmmCpuPrivate->UsedTokenNum);\r
9caaa79d
ED
1111 gSmmCpuPrivate->UsedTokenNum++;\r
1112\r
a457823f
ED
1113 InitializeSpinLock (SpinLock);\r
1114 AcquireSpinLock (SpinLock);\r
51dd408a
ED
1115\r
1116 ProcToken = AllocatePool (sizeof (PROCEDURE_TOKEN));\r
1117 ASSERT (ProcToken != NULL);\r
1118 ProcToken->Signature = PROCEDURE_TOKEN_SIGNATURE;\r
a457823f
ED
1119 ProcToken->SpinLock = SpinLock;\r
1120 ProcToken->RunningApCount = RunningApCount;\r
51dd408a
ED
1121\r
1122 InsertTailList (&gSmmCpuPrivate->TokenList, &ProcToken->Link);\r
1123\r
a457823f 1124 return ProcToken;\r
51dd408a
ED
1125}\r
1126\r
1127/**\r
1128 Checks status of specified AP.\r
1129\r
1130 This function checks whether the specified AP has finished the task assigned\r
1131 by StartupThisAP(), and whether timeout expires.\r
1132\r
1133 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or\r
1134 BroadcastProcedure.\r
1135\r
1136 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
1137 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
1138**/\r
1139EFI_STATUS\r
1140IsApReady (\r
1141 IN SPIN_LOCK *Token\r
1142 )\r
1143{\r
1144 if (AcquireSpinLockOrFail (Token)) {\r
1145 ReleaseSpinLock (Token);\r
1146 return EFI_SUCCESS;\r
1147 }\r
1148\r
1149 return EFI_NOT_READY;\r
1150}\r
1151\r
529a5a86
MK
1152/**\r
1153 Schedule a procedure to run on the specified CPU.\r
1154\r
717fb604
JY
1155 @param[in] Procedure The address of the procedure to run\r
1156 @param[in] CpuIndex Target CPU Index\r
51dd408a
ED
1157 @param[in,out] ProcArguments The parameter to pass to the procedure\r
1158 @param[in] Token This is an optional parameter that allows the caller to execute the\r
1159 procedure in a blocking or non-blocking fashion. If it is NULL the\r
1160 call is blocking, and the call will not return until the AP has\r
1161 completed the procedure. If the token is not NULL, the call will\r
1162 return immediately. The caller can check whether the procedure has\r
1163 completed with CheckOnProcedure or WaitForProcedure.\r
1164 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for the APs to finish\r
1165 execution of Procedure, either for blocking or non-blocking mode.\r
1166 Zero means infinity. If the timeout expires before all APs return\r
1167 from Procedure, then Procedure on the failed APs is terminated. If\r
1168 the timeout expires in blocking mode, the call returns EFI_TIMEOUT.\r
1169 If the timeout expires in non-blocking mode, the timeout determined\r
1170 can be through CheckOnProcedure or WaitForProcedure.\r
1171 Note that timeout support is optional. Whether an implementation\r
1172 supports this feature can be determined via the Attributes data\r
1173 member.\r
1174 @param[in,out] CpuStatus This optional pointer may be used to get the status code returned\r
1175 by Procedure when it completes execution on the target AP, or with\r
1176 EFI_TIMEOUT if the Procedure fails to complete within the optional\r
1177 timeout. The implementation will update this variable with\r
1178 EFI_NOT_READY prior to starting Procedure on the target AP.\r
529a5a86
MK
1179\r
1180 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
1181 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
1182 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
1183 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
1184 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
1185\r
1186**/\r
1187EFI_STATUS\r
717fb604 1188InternalSmmStartupThisAp (\r
51dd408a
ED
1189 IN EFI_AP_PROCEDURE2 Procedure,\r
1190 IN UINTN CpuIndex,\r
1191 IN OUT VOID *ProcArguments OPTIONAL,\r
1192 IN MM_COMPLETION *Token,\r
1193 IN UINTN TimeoutInMicroseconds,\r
1194 IN OUT EFI_STATUS *CpuStatus\r
529a5a86
MK
1195 )\r
1196{\r
a457823f
ED
1197 PROCEDURE_TOKEN *ProcToken;\r
1198\r
717fb604
JY
1199 if (CpuIndex >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus) {\r
1200 DEBUG((DEBUG_ERROR, "CpuIndex(%d) >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus(%d)\n", CpuIndex, gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus));\r
1201 return EFI_INVALID_PARAMETER;\r
1202 }\r
1203 if (CpuIndex == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) {\r
1204 DEBUG((DEBUG_ERROR, "CpuIndex(%d) == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu\n", CpuIndex));\r
529a5a86
MK
1205 return EFI_INVALID_PARAMETER;\r
1206 }\r
b7025df8
JF
1207 if (gSmmCpuPrivate->ProcessorInfo[CpuIndex].ProcessorId == INVALID_APIC_ID) {\r
1208 return EFI_INVALID_PARAMETER;\r
1209 }\r
717fb604
JY
1210 if (!(*(mSmmMpSyncData->CpuData[CpuIndex].Present))) {\r
1211 if (mSmmMpSyncData->EffectiveSyncMode == SmmCpuSyncModeTradition) {\r
1212 DEBUG((DEBUG_ERROR, "!mSmmMpSyncData->CpuData[%d].Present\n", CpuIndex));\r
1213 }\r
1214 return EFI_INVALID_PARAMETER;\r
1215 }\r
1216 if (gSmmCpuPrivate->Operation[CpuIndex] == SmmCpuRemove) {\r
1217 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
1218 DEBUG((DEBUG_ERROR, "gSmmCpuPrivate->Operation[%d] == SmmCpuRemove\n", CpuIndex));\r
1219 }\r
1220 return EFI_INVALID_PARAMETER;\r
1221 }\r
51dd408a
ED
1222 if ((TimeoutInMicroseconds != 0) && ((mSmmMp.Attributes & EFI_MM_MP_TIMEOUT_SUPPORTED) == 0)) {\r
1223 return EFI_INVALID_PARAMETER;\r
1224 }\r
1225 if (Procedure == NULL) {\r
1226 return EFI_INVALID_PARAMETER;\r
1227 }\r
717fb604 1228\r
832c4c7a 1229 AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
51dd408a 1230\r
529a5a86
MK
1231 mSmmMpSyncData->CpuData[CpuIndex].Procedure = Procedure;\r
1232 mSmmMpSyncData->CpuData[CpuIndex].Parameter = ProcArguments;\r
51dd408a 1233 if (Token != NULL) {\r
a457823f
ED
1234 ProcToken= CreateToken (1);\r
1235 mSmmMpSyncData->CpuData[CpuIndex].Token = ProcToken;\r
1236 *Token = (MM_COMPLETION)ProcToken->SpinLock;\r
51dd408a
ED
1237 }\r
1238 mSmmMpSyncData->CpuData[CpuIndex].Status = CpuStatus;\r
1239 if (mSmmMpSyncData->CpuData[CpuIndex].Status != NULL) {\r
1240 *mSmmMpSyncData->CpuData[CpuIndex].Status = EFI_NOT_READY;\r
1241 }\r
1242\r
ed3d5ecb 1243 ReleaseSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run);\r
529a5a86 1244\r
51dd408a 1245 if (Token == NULL) {\r
ed3d5ecb
JF
1246 AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
1247 ReleaseSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
529a5a86 1248 }\r
51dd408a
ED
1249\r
1250 return EFI_SUCCESS;\r
1251}\r
1252\r
1253/**\r
1254 Worker function to execute a caller provided function on all enabled APs.\r
1255\r
1256 @param[in] Procedure A pointer to the function to be run on\r
1257 enabled APs of the system.\r
1258 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
1259 APs to return from Procedure, either for\r
1260 blocking or non-blocking mode.\r
1261 @param[in,out] ProcedureArguments The parameter passed into Procedure for\r
1262 all APs.\r
1263 @param[in,out] Token This is an optional parameter that allows the caller to execute the\r
1264 procedure in a blocking or non-blocking fashion. If it is NULL the\r
1265 call is blocking, and the call will not return until the AP has\r
1266 completed the procedure. If the token is not NULL, the call will\r
1267 return immediately. The caller can check whether the procedure has\r
1268 completed with CheckOnProcedure or WaitForProcedure.\r
1269 @param[in,out] CPUStatus This optional pointer may be used to get the status code returned\r
1270 by Procedure when it completes execution on the target AP, or with\r
1271 EFI_TIMEOUT if the Procedure fails to complete within the optional\r
1272 timeout. The implementation will update this variable with\r
1273 EFI_NOT_READY prior to starting Procedure on the target AP.\r
1274\r
1275\r
1276 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
1277 the timeout expired.\r
1278 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
1279 to all enabled APs.\r
1280 @retval others Failed to Startup all APs.\r
1281\r
1282**/\r
1283EFI_STATUS\r
1284InternalSmmStartupAllAPs (\r
1285 IN EFI_AP_PROCEDURE2 Procedure,\r
1286 IN UINTN TimeoutInMicroseconds,\r
1287 IN OUT VOID *ProcedureArguments OPTIONAL,\r
1288 IN OUT MM_COMPLETION *Token,\r
1289 IN OUT EFI_STATUS *CPUStatus\r
1290 )\r
1291{\r
1292 UINTN Index;\r
1293 UINTN CpuCount;\r
a457823f 1294 PROCEDURE_TOKEN *ProcToken;\r
51dd408a
ED
1295\r
1296 if ((TimeoutInMicroseconds != 0) && ((mSmmMp.Attributes & EFI_MM_MP_TIMEOUT_SUPPORTED) == 0)) {\r
1297 return EFI_INVALID_PARAMETER;\r
1298 }\r
1299 if (Procedure == NULL) {\r
1300 return EFI_INVALID_PARAMETER;\r
1301 }\r
1302\r
1303 CpuCount = 0;\r
123b720e 1304 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
51dd408a
ED
1305 if (IsPresentAp (Index)) {\r
1306 CpuCount ++;\r
1307\r
1308 if (gSmmCpuPrivate->Operation[Index] == SmmCpuRemove) {\r
1309 return EFI_INVALID_PARAMETER;\r
1310 }\r
1311\r
1312 if (!AcquireSpinLockOrFail(mSmmMpSyncData->CpuData[Index].Busy)) {\r
1313 return EFI_NOT_READY;\r
1314 }\r
1315 ReleaseSpinLock (mSmmMpSyncData->CpuData[Index].Busy);\r
1316 }\r
1317 }\r
1318 if (CpuCount == 0) {\r
1319 return EFI_NOT_STARTED;\r
1320 }\r
1321\r
1322 if (Token != NULL) {\r
a457823f
ED
1323 ProcToken = CreateToken ((UINT32)mMaxNumberOfCpus);\r
1324 *Token = (MM_COMPLETION)ProcToken->SpinLock;\r
1325 } else {\r
1326 ProcToken = NULL;\r
51dd408a
ED
1327 }\r
1328\r
1329 //\r
1330 // Make sure all BUSY should be acquired.\r
1331 //\r
1332 // Because former code already check mSmmMpSyncData->CpuData[***].Busy for each AP.\r
1333 // Here code always use AcquireSpinLock instead of AcquireSpinLockOrFail for not\r
1334 // block mode.\r
1335 //\r
123b720e 1336 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
51dd408a
ED
1337 if (IsPresentAp (Index)) {\r
1338 AcquireSpinLock (mSmmMpSyncData->CpuData[Index].Busy);\r
1339 }\r
1340 }\r
1341\r
123b720e 1342 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
51dd408a
ED
1343 if (IsPresentAp (Index)) {\r
1344 mSmmMpSyncData->CpuData[Index].Procedure = (EFI_AP_PROCEDURE2) Procedure;\r
1345 mSmmMpSyncData->CpuData[Index].Parameter = ProcedureArguments;\r
a457823f
ED
1346 if (ProcToken != NULL) {\r
1347 mSmmMpSyncData->CpuData[Index].Token = ProcToken;\r
51dd408a
ED
1348 }\r
1349 if (CPUStatus != NULL) {\r
1350 mSmmMpSyncData->CpuData[Index].Status = &CPUStatus[Index];\r
1351 if (mSmmMpSyncData->CpuData[Index].Status != NULL) {\r
1352 *mSmmMpSyncData->CpuData[Index].Status = EFI_NOT_READY;\r
1353 }\r
1354 }\r
1355 } else {\r
1356 //\r
1357 // PI spec requirement:\r
1358 // For every excluded processor, the array entry must contain a value of EFI_NOT_STARTED.\r
1359 //\r
1360 if (CPUStatus != NULL) {\r
1361 CPUStatus[Index] = EFI_NOT_STARTED;\r
1362 }\r
a457823f
ED
1363\r
1364 //\r
1365 // Decrease the count to mark this processor(AP or BSP) as finished.\r
1366 //\r
1367 if (ProcToken != NULL) {\r
1368 WaitForSemaphore (&ProcToken->RunningApCount);\r
1369 }\r
51dd408a
ED
1370 }\r
1371 }\r
1372\r
1373 ReleaseAllAPs ();\r
1374\r
1375 if (Token == NULL) {\r
1376 //\r
1377 // Make sure all APs have completed their tasks.\r
1378 //\r
1379 WaitForAllAPsNotBusy (TRUE);\r
1380 }\r
1381\r
1382 return EFI_SUCCESS;\r
1383}\r
1384\r
1385/**\r
1386 ISO C99 6.5.2.2 "Function calls", paragraph 9:\r
1387 If the function is defined with a type that is not compatible with\r
1388 the type (of the expression) pointed to by the expression that\r
1389 denotes the called function, the behavior is undefined.\r
1390\r
1391 So add below wrapper function to convert between EFI_AP_PROCEDURE\r
1392 and EFI_AP_PROCEDURE2.\r
1393\r
1394 Wrapper for Procedures.\r
1395\r
1396 @param[in] Buffer Pointer to PROCEDURE_WRAPPER buffer.\r
1397\r
1398**/\r
1399EFI_STATUS\r
1400EFIAPI\r
1401ProcedureWrapper (\r
5ed4c46f 1402 IN VOID *Buffer\r
51dd408a
ED
1403 )\r
1404{\r
1405 PROCEDURE_WRAPPER *Wrapper;\r
1406\r
1407 Wrapper = Buffer;\r
1408 Wrapper->Procedure (Wrapper->ProcedureArgument);\r
1409\r
529a5a86
MK
1410 return EFI_SUCCESS;\r
1411}\r
1412\r
717fb604
JY
1413/**\r
1414 Schedule a procedure to run on the specified CPU in blocking mode.\r
1415\r
1416 @param[in] Procedure The address of the procedure to run\r
1417 @param[in] CpuIndex Target CPU Index\r
1418 @param[in, out] ProcArguments The parameter to pass to the procedure\r
1419\r
1420 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
1421 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
1422 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
1423 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
1424 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
1425\r
1426**/\r
1427EFI_STATUS\r
1428EFIAPI\r
1429SmmBlockingStartupThisAp (\r
1430 IN EFI_AP_PROCEDURE Procedure,\r
1431 IN UINTN CpuIndex,\r
1432 IN OUT VOID *ProcArguments OPTIONAL\r
1433 )\r
1434{\r
51dd408a
ED
1435 PROCEDURE_WRAPPER Wrapper;\r
1436\r
1437 Wrapper.Procedure = Procedure;\r
1438 Wrapper.ProcedureArgument = ProcArguments;\r
1439\r
1440 //\r
1441 // Use wrapper function to convert EFI_AP_PROCEDURE to EFI_AP_PROCEDURE2.\r
1442 //\r
1443 return InternalSmmStartupThisAp (ProcedureWrapper, CpuIndex, &Wrapper, NULL, 0, NULL);\r
717fb604
JY
1444}\r
1445\r
1446/**\r
1447 Schedule a procedure to run on the specified CPU.\r
1448\r
1449 @param Procedure The address of the procedure to run\r
1450 @param CpuIndex Target CPU Index\r
1451 @param ProcArguments The parameter to pass to the procedure\r
1452\r
1453 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
1454 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
1455 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
1456 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
1457 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
1458\r
1459**/\r
1460EFI_STATUS\r
1461EFIAPI\r
1462SmmStartupThisAp (\r
1463 IN EFI_AP_PROCEDURE Procedure,\r
1464 IN UINTN CpuIndex,\r
1465 IN OUT VOID *ProcArguments OPTIONAL\r
1466 )\r
1467{\r
51dd408a
ED
1468 MM_COMPLETION Token;\r
1469\r
1470 gSmmCpuPrivate->ApWrapperFunc[CpuIndex].Procedure = Procedure;\r
1471 gSmmCpuPrivate->ApWrapperFunc[CpuIndex].ProcedureArgument = ProcArguments;\r
1472\r
1473 //\r
1474 // Use wrapper function to convert EFI_AP_PROCEDURE to EFI_AP_PROCEDURE2.\r
1475 //\r
1476 return InternalSmmStartupThisAp (\r
1477 ProcedureWrapper,\r
1478 CpuIndex,\r
1479 &gSmmCpuPrivate->ApWrapperFunc[CpuIndex],\r
1480 FeaturePcdGet (PcdCpuSmmBlockStartupThisAp) ? NULL : &Token,\r
1481 0,\r
1482 NULL\r
1483 );\r
717fb604
JY
1484}\r
1485\r
f45f2d4a 1486/**\r
3eed6dda 1487 This function sets DR6 & DR7 according to SMM save state, before running SMM C code.\r
f45f2d4a
JY
1488 They are useful when you want to enable hardware breakpoints in SMM without entry SMM mode.\r
1489\r
1490 NOTE: It might not be appreciated in runtime since it might\r
1491 conflict with OS debugging facilities. Turn them off in RELEASE.\r
1492\r
1493 @param CpuIndex CPU Index\r
1494\r
1495**/\r
1496VOID\r
1497EFIAPI\r
1498CpuSmmDebugEntry (\r
1499 IN UINTN CpuIndex\r
1500 )\r
1501{\r
1502 SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
7367cc6c 1503\r
f45f2d4a 1504 if (FeaturePcdGet (PcdCpuSmmDebug)) {\r
717fb604 1505 ASSERT(CpuIndex < mMaxNumberOfCpus);\r
3eed6dda 1506 CpuSaveState = (SMRAM_SAVE_STATE_MAP *)gSmmCpuPrivate->CpuSaveState[CpuIndex];\r
f45f2d4a
JY
1507 if (mSmmSaveStateRegisterLma == EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT) {\r
1508 AsmWriteDr6 (CpuSaveState->x86._DR6);\r
1509 AsmWriteDr7 (CpuSaveState->x86._DR7);\r
1510 } else {\r
1511 AsmWriteDr6 ((UINTN)CpuSaveState->x64._DR6);\r
1512 AsmWriteDr7 ((UINTN)CpuSaveState->x64._DR7);\r
1513 }\r
1514 }\r
1515}\r
1516\r
1517/**\r
3eed6dda 1518 This function restores DR6 & DR7 to SMM save state.\r
f45f2d4a
JY
1519\r
1520 NOTE: It might not be appreciated in runtime since it might\r
1521 conflict with OS debugging facilities. Turn them off in RELEASE.\r
1522\r
1523 @param CpuIndex CPU Index\r
1524\r
1525**/\r
1526VOID\r
1527EFIAPI\r
1528CpuSmmDebugExit (\r
1529 IN UINTN CpuIndex\r
1530 )\r
1531{\r
1532 SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
1533\r
1534 if (FeaturePcdGet (PcdCpuSmmDebug)) {\r
717fb604 1535 ASSERT(CpuIndex < mMaxNumberOfCpus);\r
3eed6dda 1536 CpuSaveState = (SMRAM_SAVE_STATE_MAP *)gSmmCpuPrivate->CpuSaveState[CpuIndex];\r
f45f2d4a
JY
1537 if (mSmmSaveStateRegisterLma == EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT) {\r
1538 CpuSaveState->x86._DR7 = (UINT32)AsmReadDr7 ();\r
1539 CpuSaveState->x86._DR6 = (UINT32)AsmReadDr6 ();\r
1540 } else {\r
1541 CpuSaveState->x64._DR7 = AsmReadDr7 ();\r
1542 CpuSaveState->x64._DR6 = AsmReadDr6 ();\r
1543 }\r
1544 }\r
1545}\r
1546\r
529a5a86
MK
1547/**\r
1548 C function for SMI entry, each processor comes here upon SMI trigger.\r
1549\r
1550 @param CpuIndex CPU Index\r
1551\r
1552**/\r
1553VOID\r
1554EFIAPI\r
1555SmiRendezvous (\r
1556 IN UINTN CpuIndex\r
1557 )\r
1558{\r
f85d3ce2
JF
1559 EFI_STATUS Status;\r
1560 BOOLEAN ValidSmi;\r
1561 BOOLEAN IsBsp;\r
1562 BOOLEAN BspInProgress;\r
1563 UINTN Index;\r
1564 UINTN Cr2;\r
717fb604
JY
1565\r
1566 ASSERT(CpuIndex < mMaxNumberOfCpus);\r
529a5a86
MK
1567\r
1568 //\r
37f9fea5
VN
1569 // Save Cr2 because Page Fault exception in SMM may override its value,\r
1570 // when using on-demand paging for above 4G memory.\r
529a5a86 1571 //\r
37f9fea5
VN
1572 Cr2 = 0;\r
1573 SaveCr2 (&Cr2);\r
529a5a86 1574\r
51dd408a
ED
1575 //\r
1576 // Call the user register Startup function first.\r
1577 //\r
1578 if (mSmmMpSyncData->StartupProcedure != NULL) {\r
1579 mSmmMpSyncData->StartupProcedure (mSmmMpSyncData->StartupProcArgs);\r
1580 }\r
1581\r
529a5a86
MK
1582 //\r
1583 // Perform CPU specific entry hooks\r
1584 //\r
1585 SmmCpuFeaturesRendezvousEntry (CpuIndex);\r
1586\r
1587 //\r
1588 // Determine if this is a valid SMI\r
1589 //\r
1590 ValidSmi = PlatformValidSmi();\r
1591\r
1592 //\r
1593 // Determine if BSP has been already in progress. Note this must be checked after\r
1594 // ValidSmi because BSP may clear a valid SMI source after checking in.\r
1595 //\r
fe3a75bc 1596 BspInProgress = *mSmmMpSyncData->InsideSmm;\r
529a5a86
MK
1597\r
1598 if (!BspInProgress && !ValidSmi) {\r
1599 //\r
1600 // If we reach here, it means when we sampled the ValidSmi flag, SMI status had not\r
1601 // been cleared by BSP in a new SMI run (so we have a truly invalid SMI), or SMI\r
1602 // status had been cleared by BSP and an existing SMI run has almost ended. (Note\r
1603 // we sampled ValidSmi flag BEFORE judging BSP-in-progress status.) In both cases, there\r
1604 // is nothing we need to do.\r
1605 //\r
1606 goto Exit;\r
1607 } else {\r
1608 //\r
1609 // Signal presence of this processor\r
1610 //\r
fe3a75bc 1611 if (ReleaseSemaphore (mSmmMpSyncData->Counter) == 0) {\r
529a5a86
MK
1612 //\r
1613 // BSP has already ended the synchronization, so QUIT!!!\r
1614 //\r
1615\r
1616 //\r
1617 // Wait for BSP's signal to finish SMI\r
1618 //\r
fe3a75bc 1619 while (*mSmmMpSyncData->AllCpusInSync) {\r
529a5a86
MK
1620 CpuPause ();\r
1621 }\r
1622 goto Exit;\r
1623 } else {\r
1624\r
1625 //\r
1626 // The BUSY lock is initialized to Released state.\r
1627 // This needs to be done early enough to be ready for BSP's SmmStartupThisAp() call.\r
1628 // E.g., with Relaxed AP flow, SmmStartupThisAp() may be called immediately\r
1629 // after AP's present flag is detected.\r
1630 //\r
ed3d5ecb 1631 InitializeSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
529a5a86
MK
1632 }\r
1633\r
529a5a86
MK
1634 if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
1635 ActivateSmmProfile (CpuIndex);\r
1636 }\r
1637\r
1638 if (BspInProgress) {\r
1639 //\r
1640 // BSP has been elected. Follow AP path, regardless of ValidSmi flag\r
1641 // as BSP may have cleared the SMI status\r
1642 //\r
1643 APHandler (CpuIndex, ValidSmi, mSmmMpSyncData->EffectiveSyncMode);\r
1644 } else {\r
1645 //\r
1646 // We have a valid SMI\r
1647 //\r
1648\r
1649 //\r
1650 // Elect BSP\r
1651 //\r
1652 IsBsp = FALSE;\r
1653 if (FeaturePcdGet (PcdCpuSmmEnableBspElection)) {\r
1654 if (!mSmmMpSyncData->SwitchBsp || mSmmMpSyncData->CandidateBsp[CpuIndex]) {\r
1655 //\r
1656 // Call platform hook to do BSP election\r
1657 //\r
1658 Status = PlatformSmmBspElection (&IsBsp);\r
1659 if (EFI_SUCCESS == Status) {\r
1660 //\r
1661 // Platform hook determines successfully\r
1662 //\r
1663 if (IsBsp) {\r
1664 mSmmMpSyncData->BspIndex = (UINT32)CpuIndex;\r
1665 }\r
1666 } else {\r
1667 //\r
1668 // Platform hook fails to determine, use default BSP election method\r
1669 //\r
1670 InterlockedCompareExchange32 (\r
1671 (UINT32*)&mSmmMpSyncData->BspIndex,\r
1672 (UINT32)-1,\r
1673 (UINT32)CpuIndex\r
1674 );\r
1675 }\r
1676 }\r
1677 }\r
1678\r
1679 //\r
1680 // "mSmmMpSyncData->BspIndex == CpuIndex" means this is the BSP\r
1681 //\r
1682 if (mSmmMpSyncData->BspIndex == CpuIndex) {\r
1683\r
1684 //\r
1685 // Clear last request for SwitchBsp.\r
1686 //\r
1687 if (mSmmMpSyncData->SwitchBsp) {\r
1688 mSmmMpSyncData->SwitchBsp = FALSE;\r
1689 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
1690 mSmmMpSyncData->CandidateBsp[Index] = FALSE;\r
1691 }\r
1692 }\r
1693\r
1694 if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
1695 SmmProfileRecordSmiNum ();\r
1696 }\r
1697\r
1698 //\r
1699 // BSP Handler is always called with a ValidSmi == TRUE\r
1700 //\r
1701 BSPHandler (CpuIndex, mSmmMpSyncData->EffectiveSyncMode);\r
529a5a86
MK
1702 } else {\r
1703 APHandler (CpuIndex, ValidSmi, mSmmMpSyncData->EffectiveSyncMode);\r
1704 }\r
1705 }\r
1706\r
ed3d5ecb 1707 ASSERT (*mSmmMpSyncData->CpuData[CpuIndex].Run == 0);\r
529a5a86
MK
1708\r
1709 //\r
1710 // Wait for BSP's signal to exit SMI\r
1711 //\r
fe3a75bc 1712 while (*mSmmMpSyncData->AllCpusInSync) {\r
529a5a86
MK
1713 CpuPause ();\r
1714 }\r
1715 }\r
1716\r
1717Exit:\r
1718 SmmCpuFeaturesRendezvousExit (CpuIndex);\r
37f9fea5 1719\r
529a5a86
MK
1720 //\r
1721 // Restore Cr2\r
1722 //\r
37f9fea5 1723 RestoreCr2 (Cr2);\r
529a5a86
MK
1724}\r
1725\r
51dd408a
ED
1726/**\r
1727 Allocate buffer for SpinLock and Wrapper function buffer.\r
1728\r
1729**/\r
1730VOID\r
1731InitializeDataForMmMp (\r
1732 VOID\r
1733 )\r
1734{\r
9caaa79d
ED
1735 UINTN SpinLockSize;\r
1736 UINT32 TokenCountPerChunk;\r
1737\r
1738 SpinLockSize = GetSpinLockProperties ();\r
1739 TokenCountPerChunk = FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk);\r
1740 ASSERT (TokenCountPerChunk != 0);\r
1741 if (TokenCountPerChunk == 0) {\r
1742 DEBUG ((DEBUG_ERROR, "PcdCpuSmmMpTokenCountPerChunk should not be Zero!\n"));\r
1743 CpuDeadLoop ();\r
1744 }\r
1745 DEBUG ((DEBUG_INFO, "CpuSmm: SpinLock Size = 0x%x, PcdCpuSmmMpTokenCountPerChunk = 0x%x\n", SpinLockSize, TokenCountPerChunk));\r
1746\r
1747 gSmmCpuPrivate->CurrentTokenBuf = AllocatePool (SpinLockSize * TokenCountPerChunk);\r
1748 ASSERT (gSmmCpuPrivate->CurrentTokenBuf != NULL);\r
1749\r
1750 gSmmCpuPrivate->UsedTokenNum = 0;\r
1751\r
51dd408a
ED
1752 gSmmCpuPrivate->ApWrapperFunc = AllocatePool (sizeof (PROCEDURE_WRAPPER) * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus);\r
1753 ASSERT (gSmmCpuPrivate->ApWrapperFunc != NULL);\r
1754\r
1755 InitializeListHead (&gSmmCpuPrivate->TokenList);\r
9caaa79d 1756 InitializeListHead (&gSmmCpuPrivate->OldTokenBufList);\r
51dd408a
ED
1757}\r
1758\r
1d648531
JF
1759/**\r
1760 Allocate buffer for all semaphores and spin locks.\r
1761\r
1762**/\r
1763VOID\r
1764InitializeSmmCpuSemaphores (\r
1765 VOID\r
1766 )\r
1767{\r
1768 UINTN ProcessorCount;\r
1769 UINTN TotalSize;\r
1770 UINTN GlobalSemaphoresSize;\r
4e920581 1771 UINTN CpuSemaphoresSize;\r
1d648531
JF
1772 UINTN SemaphoreSize;\r
1773 UINTN Pages;\r
1774 UINTN *SemaphoreBlock;\r
1775 UINTN SemaphoreAddr;\r
1776\r
1777 SemaphoreSize = GetSpinLockProperties ();\r
1778 ProcessorCount = gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;\r
1779 GlobalSemaphoresSize = (sizeof (SMM_CPU_SEMAPHORE_GLOBAL) / sizeof (VOID *)) * SemaphoreSize;\r
4e920581 1780 CpuSemaphoresSize = (sizeof (SMM_CPU_SEMAPHORE_CPU) / sizeof (VOID *)) * ProcessorCount * SemaphoreSize;\r
31fb3334 1781 TotalSize = GlobalSemaphoresSize + CpuSemaphoresSize;\r
1d648531
JF
1782 DEBUG((EFI_D_INFO, "One Semaphore Size = 0x%x\n", SemaphoreSize));\r
1783 DEBUG((EFI_D_INFO, "Total Semaphores Size = 0x%x\n", TotalSize));\r
1784 Pages = EFI_SIZE_TO_PAGES (TotalSize);\r
1785 SemaphoreBlock = AllocatePages (Pages);\r
1786 ASSERT (SemaphoreBlock != NULL);\r
1787 ZeroMem (SemaphoreBlock, TotalSize);\r
1788\r
1789 SemaphoreAddr = (UINTN)SemaphoreBlock;\r
1790 mSmmCpuSemaphores.SemaphoreGlobal.Counter = (UINT32 *)SemaphoreAddr;\r
1791 SemaphoreAddr += SemaphoreSize;\r
1792 mSmmCpuSemaphores.SemaphoreGlobal.InsideSmm = (BOOLEAN *)SemaphoreAddr;\r
1793 SemaphoreAddr += SemaphoreSize;\r
1794 mSmmCpuSemaphores.SemaphoreGlobal.AllCpusInSync = (BOOLEAN *)SemaphoreAddr;\r
1795 SemaphoreAddr += SemaphoreSize;\r
1796 mSmmCpuSemaphores.SemaphoreGlobal.PFLock = (SPIN_LOCK *)SemaphoreAddr;\r
1797 SemaphoreAddr += SemaphoreSize;\r
1798 mSmmCpuSemaphores.SemaphoreGlobal.CodeAccessCheckLock\r
1799 = (SPIN_LOCK *)SemaphoreAddr;\r
6c4c15fa 1800 SemaphoreAddr += SemaphoreSize;\r
6c4c15fa 1801\r
4e920581
JF
1802 SemaphoreAddr = (UINTN)SemaphoreBlock + GlobalSemaphoresSize;\r
1803 mSmmCpuSemaphores.SemaphoreCpu.Busy = (SPIN_LOCK *)SemaphoreAddr;\r
1804 SemaphoreAddr += ProcessorCount * SemaphoreSize;\r
1805 mSmmCpuSemaphores.SemaphoreCpu.Run = (UINT32 *)SemaphoreAddr;\r
1806 SemaphoreAddr += ProcessorCount * SemaphoreSize;\r
1807 mSmmCpuSemaphores.SemaphoreCpu.Present = (BOOLEAN *)SemaphoreAddr;\r
1808\r
fe3a75bc
JF
1809 mPFLock = mSmmCpuSemaphores.SemaphoreGlobal.PFLock;\r
1810 mConfigSmmCodeAccessCheckLock = mSmmCpuSemaphores.SemaphoreGlobal.CodeAccessCheckLock;\r
1811\r
1d648531
JF
1812 mSemaphoreSize = SemaphoreSize;\r
1813}\r
529a5a86
MK
1814\r
1815/**\r
1816 Initialize un-cacheable data.\r
1817\r
1818**/\r
1819VOID\r
1820EFIAPI\r
1821InitializeMpSyncData (\r
1822 VOID\r
1823 )\r
1824{\r
8b9311b7
JF
1825 UINTN CpuIndex;\r
1826\r
529a5a86 1827 if (mSmmMpSyncData != NULL) {\r
e78a2a49
JF
1828 //\r
1829 // mSmmMpSyncDataSize includes one structure of SMM_DISPATCHER_MP_SYNC_DATA, one\r
1830 // CpuData array of SMM_CPU_DATA_BLOCK and one CandidateBsp array of BOOLEAN.\r
1831 //\r
1832 ZeroMem (mSmmMpSyncData, mSmmMpSyncDataSize);\r
529a5a86
MK
1833 mSmmMpSyncData->CpuData = (SMM_CPU_DATA_BLOCK *)((UINT8 *)mSmmMpSyncData + sizeof (SMM_DISPATCHER_MP_SYNC_DATA));\r
1834 mSmmMpSyncData->CandidateBsp = (BOOLEAN *)(mSmmMpSyncData->CpuData + gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus);\r
1835 if (FeaturePcdGet (PcdCpuSmmEnableBspElection)) {\r
1836 //\r
1837 // Enable BSP election by setting BspIndex to -1\r
1838 //\r
1839 mSmmMpSyncData->BspIndex = (UINT32)-1;\r
1840 }\r
b43dd229 1841 mSmmMpSyncData->EffectiveSyncMode = mCpuSmmSyncMode;\r
1d648531 1842\r
8b9311b7
JF
1843 mSmmMpSyncData->Counter = mSmmCpuSemaphores.SemaphoreGlobal.Counter;\r
1844 mSmmMpSyncData->InsideSmm = mSmmCpuSemaphores.SemaphoreGlobal.InsideSmm;\r
1845 mSmmMpSyncData->AllCpusInSync = mSmmCpuSemaphores.SemaphoreGlobal.AllCpusInSync;\r
1846 ASSERT (mSmmMpSyncData->Counter != NULL && mSmmMpSyncData->InsideSmm != NULL &&\r
1847 mSmmMpSyncData->AllCpusInSync != NULL);\r
1848 *mSmmMpSyncData->Counter = 0;\r
1849 *mSmmMpSyncData->InsideSmm = FALSE;\r
1850 *mSmmMpSyncData->AllCpusInSync = FALSE;\r
1851\r
1852 for (CpuIndex = 0; CpuIndex < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; CpuIndex ++) {\r
1853 mSmmMpSyncData->CpuData[CpuIndex].Busy =\r
1854 (SPIN_LOCK *)((UINTN)mSmmCpuSemaphores.SemaphoreCpu.Busy + mSemaphoreSize * CpuIndex);\r
1855 mSmmMpSyncData->CpuData[CpuIndex].Run =\r
1856 (UINT32 *)((UINTN)mSmmCpuSemaphores.SemaphoreCpu.Run + mSemaphoreSize * CpuIndex);\r
1857 mSmmMpSyncData->CpuData[CpuIndex].Present =\r
1858 (BOOLEAN *)((UINTN)mSmmCpuSemaphores.SemaphoreCpu.Present + mSemaphoreSize * CpuIndex);\r
56e4a7d7
JF
1859 *(mSmmMpSyncData->CpuData[CpuIndex].Busy) = 0;\r
1860 *(mSmmMpSyncData->CpuData[CpuIndex].Run) = 0;\r
1861 *(mSmmMpSyncData->CpuData[CpuIndex].Present) = FALSE;\r
8b9311b7 1862 }\r
529a5a86
MK
1863 }\r
1864}\r
1865\r
1866/**\r
1867 Initialize global data for MP synchronization.\r
1868\r
3eb69b08
JY
1869 @param Stacks Base address of SMI stack buffer for all processors.\r
1870 @param StackSize Stack size for each processor in SMM.\r
1871 @param ShadowStackSize Shadow Stack size for each processor in SMM.\r
529a5a86
MK
1872\r
1873**/\r
1874UINT32\r
1875InitializeMpServiceData (\r
1876 IN VOID *Stacks,\r
3eb69b08
JY
1877 IN UINTN StackSize,\r
1878 IN UINTN ShadowStackSize\r
529a5a86
MK
1879 )\r
1880{\r
1881 UINT32 Cr3;\r
1882 UINTN Index;\r
529a5a86 1883 UINT8 *GdtTssTables;\r
529a5a86 1884 UINTN GdtTableStepSize;\r
ba40cb31
MK
1885 CPUID_VERSION_INFO_EDX RegEdx;\r
1886\r
1887 //\r
1888 // Determine if this CPU supports machine check\r
1889 //\r
1890 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &RegEdx.Uint32);\r
1891 mMachineCheckSupported = (BOOLEAN)(RegEdx.Bits.MCA == 1);\r
529a5a86 1892\r
8b9311b7
JF
1893 //\r
1894 // Allocate memory for all locks and semaphores\r
1895 //\r
1896 InitializeSmmCpuSemaphores ();\r
1897\r
d67b73cc
JF
1898 //\r
1899 // Initialize mSmmMpSyncData\r
1900 //\r
1901 mSmmMpSyncDataSize = sizeof (SMM_DISPATCHER_MP_SYNC_DATA) +\r
1902 (sizeof (SMM_CPU_DATA_BLOCK) + sizeof (BOOLEAN)) * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;\r
1903 mSmmMpSyncData = (SMM_DISPATCHER_MP_SYNC_DATA*) AllocatePages (EFI_SIZE_TO_PAGES (mSmmMpSyncDataSize));\r
1904 ASSERT (mSmmMpSyncData != NULL);\r
b43dd229 1905 mCpuSmmSyncMode = (SMM_CPU_SYNC_MODE)PcdGet8 (PcdCpuSmmSyncMode);\r
d67b73cc
JF
1906 InitializeMpSyncData ();\r
1907\r
529a5a86
MK
1908 //\r
1909 // Initialize physical address mask\r
1910 // NOTE: Physical memory above virtual address limit is not supported !!!\r
1911 //\r
1912 AsmCpuid (0x80000008, (UINT32*)&Index, NULL, NULL, NULL);\r
1913 gPhyMask = LShiftU64 (1, (UINT8)Index) - 1;\r
1914 gPhyMask &= (1ull << 48) - EFI_PAGE_SIZE;\r
1915\r
1916 //\r
1917 // Create page tables\r
1918 //\r
1919 Cr3 = SmmInitPageTable ();\r
1920\r
fe5f1949 1921 GdtTssTables = InitGdt (Cr3, &GdtTableStepSize);\r
529a5a86
MK
1922\r
1923 //\r
f12367a0 1924 // Install SMI handler for each CPU\r
529a5a86
MK
1925 //\r
1926 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
529a5a86
MK
1927 InstallSmiHandler (\r
1928 Index,\r
1929 (UINT32)mCpuHotPlugData.SmBase[Index],\r
3eb69b08 1930 (VOID*)((UINTN)Stacks + (StackSize + ShadowStackSize) * Index),\r
529a5a86 1931 StackSize,\r
f12367a0
MK
1932 (UINTN)(GdtTssTables + GdtTableStepSize * Index),\r
1933 gcSmiGdtr.Limit + 1,\r
529a5a86
MK
1934 gcSmiIdtr.Base,\r
1935 gcSmiIdtr.Limit + 1,\r
1936 Cr3\r
1937 );\r
1938 }\r
1939\r
529a5a86
MK
1940 //\r
1941 // Record current MTRR settings\r
1942 //\r
26ab5ac3
MK
1943 ZeroMem (&gSmiMtrrs, sizeof (gSmiMtrrs));\r
1944 MtrrGetAllMtrrs (&gSmiMtrrs);\r
529a5a86
MK
1945\r
1946 return Cr3;\r
1947}\r
1948\r
1949/**\r
1950\r
1951 Register the SMM Foundation entry point.\r
1952\r
1953 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance\r
1954 @param SmmEntryPoint SMM Foundation EntryPoint\r
1955\r
1956 @retval EFI_SUCCESS Successfully to register SMM foundation entry point\r
1957\r
1958**/\r
1959EFI_STATUS\r
1960EFIAPI\r
1961RegisterSmmEntry (\r
1962 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL *This,\r
1963 IN EFI_SMM_ENTRY_POINT SmmEntryPoint\r
1964 )\r
1965{\r
1966 //\r
1967 // Record SMM Foundation EntryPoint, later invoke it on SMI entry vector.\r
1968 //\r
1969 gSmmCpuPrivate->SmmCoreEntry = SmmEntryPoint;\r
1970 return EFI_SUCCESS;\r
1971}\r
51dd408a
ED
1972\r
1973/**\r
1974\r
1975 Register the SMM Foundation entry point.\r
1976\r
1977 @param[in] Procedure A pointer to the code stream to be run on the designated target AP\r
1978 of the system. Type EFI_AP_PROCEDURE is defined below in Volume 2\r
1979 with the related definitions of\r
1980 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs.\r
1981 If caller may pass a value of NULL to deregister any existing\r
1982 startup procedure.\r
073f2ced 1983 @param[in,out] ProcedureArguments Allows the caller to pass a list of parameters to the code that is\r
51dd408a
ED
1984 run by the AP. It is an optional common mailbox between APs and\r
1985 the caller to share information\r
1986\r
1987 @retval EFI_SUCCESS The Procedure has been set successfully.\r
1988 @retval EFI_INVALID_PARAMETER The Procedure is NULL but ProcedureArguments not NULL.\r
1989\r
1990**/\r
1991EFI_STATUS\r
1992RegisterStartupProcedure (\r
073f2ced
SZ
1993 IN EFI_AP_PROCEDURE Procedure,\r
1994 IN OUT VOID *ProcedureArguments OPTIONAL\r
51dd408a
ED
1995 )\r
1996{\r
1997 if (Procedure == NULL && ProcedureArguments != NULL) {\r
1998 return EFI_INVALID_PARAMETER;\r
1999 }\r
2000 if (mSmmMpSyncData == NULL) {\r
2001 return EFI_NOT_READY;\r
2002 }\r
2003\r
2004 mSmmMpSyncData->StartupProcedure = Procedure;\r
2005 mSmmMpSyncData->StartupProcArgs = ProcedureArguments;\r
2006\r
2007 return EFI_SUCCESS;\r
2008}\r