]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c
Always set WP in CR0.
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / MpService.c
CommitLineData
529a5a86
MK
1/** @file\r
2SMM MP service implementation\r
3\r
4Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PiSmmCpuDxeSmm.h"\r
16\r
17//\r
18// Slots for all MTRR( FIXED MTRR + VARIABLE MTRR + MTRR_LIB_IA32_MTRR_DEF_TYPE)\r
19//\r
20UINT64 gSmiMtrrs[MTRR_NUMBER_OF_FIXED_MTRR + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];\r
21UINT64 gPhyMask;\r
22SMM_DISPATCHER_MP_SYNC_DATA *mSmmMpSyncData = NULL;\r
23UINTN mSmmMpSyncDataSize;\r
24\r
25/**\r
26 Performs an atomic compare exchange operation to get semaphore.\r
27 The compare exchange operation must be performed using\r
28 MP safe mechanisms.\r
29\r
30 @param Sem IN: 32-bit unsigned integer\r
31 OUT: original integer - 1\r
32 @return Original integer - 1\r
33\r
34**/\r
35UINT32\r
36WaitForSemaphore (\r
37 IN OUT volatile UINT32 *Sem\r
38 )\r
39{\r
40 UINT32 Value;\r
41\r
42 do {\r
43 Value = *Sem;\r
44 } while (Value == 0 ||\r
45 InterlockedCompareExchange32 (\r
46 (UINT32*)Sem,\r
47 Value,\r
48 Value - 1\r
49 ) != Value);\r
50 return Value - 1;\r
51}\r
52\r
53\r
54/**\r
55 Performs an atomic compare exchange operation to release semaphore.\r
56 The compare exchange operation must be performed using\r
57 MP safe mechanisms.\r
58\r
59 @param Sem IN: 32-bit unsigned integer\r
60 OUT: original integer + 1\r
61 @return Original integer + 1\r
62\r
63**/\r
64UINT32\r
65ReleaseSemaphore (\r
66 IN OUT volatile UINT32 *Sem\r
67 )\r
68{\r
69 UINT32 Value;\r
70\r
71 do {\r
72 Value = *Sem;\r
73 } while (Value + 1 != 0 &&\r
74 InterlockedCompareExchange32 (\r
75 (UINT32*)Sem,\r
76 Value,\r
77 Value + 1\r
78 ) != Value);\r
79 return Value + 1;\r
80}\r
81\r
82/**\r
83 Performs an atomic compare exchange operation to lock semaphore.\r
84 The compare exchange operation must be performed using\r
85 MP safe mechanisms.\r
86\r
87 @param Sem IN: 32-bit unsigned integer\r
88 OUT: -1\r
89 @return Original integer\r
90\r
91**/\r
92UINT32\r
93LockdownSemaphore (\r
94 IN OUT volatile UINT32 *Sem\r
95 )\r
96{\r
97 UINT32 Value;\r
98\r
99 do {\r
100 Value = *Sem;\r
101 } while (InterlockedCompareExchange32 (\r
102 (UINT32*)Sem,\r
103 Value, (UINT32)-1\r
104 ) != Value);\r
105 return Value;\r
106}\r
107\r
108/**\r
109 Wait all APs to performs an atomic compare exchange operation to release semaphore.\r
110\r
111 @param NumberOfAPs AP number\r
112\r
113**/\r
114VOID\r
115WaitForAllAPs (\r
116 IN UINTN NumberOfAPs\r
117 )\r
118{\r
119 UINTN BspIndex;\r
120\r
121 BspIndex = mSmmMpSyncData->BspIndex;\r
122 while (NumberOfAPs-- > 0) {\r
123 WaitForSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);\r
124 }\r
125}\r
126\r
127/**\r
128 Performs an atomic compare exchange operation to release semaphore\r
129 for each AP.\r
130\r
131**/\r
132VOID\r
133ReleaseAllAPs (\r
134 VOID\r
135 )\r
136{\r
137 UINTN Index;\r
138 UINTN BspIndex;\r
139\r
140 BspIndex = mSmmMpSyncData->BspIndex;\r
141 for (Index = mMaxNumberOfCpus; Index-- > 0;) {\r
142 if (Index != BspIndex && mSmmMpSyncData->CpuData[Index].Present) {\r
143 ReleaseSemaphore (&mSmmMpSyncData->CpuData[Index].Run);\r
144 }\r
145 }\r
146}\r
147\r
148/**\r
149 Checks if all CPUs (with certain exceptions) have checked in for this SMI run\r
150\r
151 @param Exceptions CPU Arrival exception flags.\r
152\r
153 @retval TRUE if all CPUs the have checked in.\r
154 @retval FALSE if at least one Normal AP hasn't checked in.\r
155\r
156**/\r
157BOOLEAN\r
158AllCpusInSmmWithExceptions (\r
159 SMM_CPU_ARRIVAL_EXCEPTIONS Exceptions\r
160 )\r
161{\r
162 UINTN Index;\r
163 SMM_CPU_DATA_BLOCK *CpuData;\r
164 EFI_PROCESSOR_INFORMATION *ProcessorInfo;\r
165\r
166 ASSERT (mSmmMpSyncData->Counter <= mNumberOfCpus);\r
167\r
168 if (mSmmMpSyncData->Counter == mNumberOfCpus) {\r
169 return TRUE;\r
170 }\r
171\r
172 CpuData = mSmmMpSyncData->CpuData;\r
173 ProcessorInfo = gSmmCpuPrivate->ProcessorInfo;\r
174 for (Index = mMaxNumberOfCpus; Index-- > 0;) {\r
175 if (!CpuData[Index].Present && ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) {\r
176 if (((Exceptions & ARRIVAL_EXCEPTION_DELAYED) != 0) && SmmCpuFeaturesGetSmmRegister (Index, SmmRegSmmDelayed) != 0) {\r
177 continue;\r
178 }\r
179 if (((Exceptions & ARRIVAL_EXCEPTION_BLOCKED) != 0) && SmmCpuFeaturesGetSmmRegister (Index, SmmRegSmmBlocked) != 0) {\r
180 continue;\r
181 }\r
182 if (((Exceptions & ARRIVAL_EXCEPTION_SMI_DISABLED) != 0) && SmmCpuFeaturesGetSmmRegister (Index, SmmRegSmmEnable) != 0) {\r
183 continue;\r
184 }\r
185 return FALSE;\r
186 }\r
187 }\r
188\r
189\r
190 return TRUE;\r
191}\r
192\r
193\r
194/**\r
195 Given timeout constraint, wait for all APs to arrive, and insure when this function returns, no AP will execute normal mode code before\r
196 entering SMM, except SMI disabled APs.\r
197\r
198**/\r
199VOID\r
200SmmWaitForApArrival (\r
201 VOID\r
202 )\r
203{\r
204 UINT64 Timer;\r
205 UINTN Index;\r
206\r
207 ASSERT (mSmmMpSyncData->Counter <= mNumberOfCpus);\r
208\r
209 //\r
210 // Platform implementor should choose a timeout value appropriately:\r
211 // - The timeout value should balance the SMM time constrains and the likelihood that delayed CPUs are excluded in the SMM run. Note\r
212 // the SMI Handlers must ALWAYS take into account the cases that not all APs are available in an SMI run.\r
213 // - 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
214 // 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
215 // 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
216 // SMI IPI), because with a buffered SMI, and CPU will enter SMM immediately after it is brought out of the blocked state.\r
217 // - The timeout value must be longer than longest possible IO operation in the system\r
218 //\r
219\r
220 //\r
221 // Sync with APs 1st timeout\r
222 //\r
223 for (Timer = StartSyncTimer ();\r
224 !IsSyncTimerTimeout (Timer) &&\r
225 !AllCpusInSmmWithExceptions (ARRIVAL_EXCEPTION_BLOCKED | ARRIVAL_EXCEPTION_SMI_DISABLED );\r
226 ) {\r
227 CpuPause ();\r
228 }\r
229\r
230 //\r
231 // Not all APs have arrived, so we need 2nd round of timeout. IPIs should be sent to ALL none present APs,\r
232 // because:\r
233 // 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
234 // 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
235 // enter SMI immediately without executing instructions in normal mode. Note traditional flow requires there are no APs doing normal mode\r
236 // work while SMI handling is on-going.\r
237 // b) As a consequence of SMI IPI sending, (spurious) SMI may occur after this SMM run.\r
238 // c) ** NOTE **: Use SMI disabling feature VERY CAREFULLY (if at all) for traditional flow, because a processor in SMI-disabled state\r
239 // will execute normal mode code, which breaks the traditional SMI handlers' assumption that no APs are doing normal\r
240 // mode work while SMI handling is on-going.\r
241 // d) We don't add code to check SMI disabling status to skip sending IPI to SMI disabled APs, because:\r
242 // - In traditional flow, SMI disabling is discouraged.\r
243 // - In relaxed flow, CheckApArrival() will check SMI disabling status before calling this function.\r
244 // In both cases, adding SMI-disabling checking code increases overhead.\r
245 //\r
246 if (mSmmMpSyncData->Counter < mNumberOfCpus) {\r
247 //\r
248 // Send SMI IPIs to bring outside processors in\r
249 //\r
250 for (Index = mMaxNumberOfCpus; Index-- > 0;) {\r
251 if (!mSmmMpSyncData->CpuData[Index].Present && gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) {\r
252 SendSmiIpi ((UINT32)gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId);\r
253 }\r
254 }\r
255\r
256 //\r
257 // Sync with APs 2nd timeout.\r
258 //\r
259 for (Timer = StartSyncTimer ();\r
260 !IsSyncTimerTimeout (Timer) &&\r
261 !AllCpusInSmmWithExceptions (ARRIVAL_EXCEPTION_BLOCKED | ARRIVAL_EXCEPTION_SMI_DISABLED );\r
262 ) {\r
263 CpuPause ();\r
264 }\r
265 }\r
266\r
267 return;\r
268}\r
269\r
270\r
271/**\r
272 Replace OS MTRR's with SMI MTRR's.\r
273\r
274 @param CpuIndex Processor Index\r
275\r
276**/\r
277VOID\r
278ReplaceOSMtrrs (\r
279 IN UINTN CpuIndex\r
280 )\r
281{\r
282 PROCESSOR_SMM_DESCRIPTOR *Psd;\r
283 UINT64 *SmiMtrrs;\r
284 MTRR_SETTINGS *BiosMtrr;\r
285\r
286 Psd = (PROCESSOR_SMM_DESCRIPTOR*)(mCpuHotPlugData.SmBase[CpuIndex] + SMM_PSD_OFFSET);\r
287 SmiMtrrs = (UINT64*)(UINTN)Psd->MtrrBaseMaskPtr;\r
288\r
289 SmmCpuFeaturesDisableSmrr ();\r
290\r
291 //\r
292 // Replace all MTRRs registers\r
293 //\r
294 BiosMtrr = (MTRR_SETTINGS*)SmiMtrrs;\r
295 MtrrSetAllMtrrs(BiosMtrr);\r
296}\r
297\r
298/**\r
299 SMI handler for BSP.\r
300\r
301 @param CpuIndex BSP processor Index\r
302 @param SyncMode SMM MP sync mode\r
303\r
304**/\r
305VOID\r
306BSPHandler (\r
307 IN UINTN CpuIndex,\r
308 IN SMM_CPU_SYNC_MODE SyncMode\r
309 )\r
310{\r
311 UINTN Index;\r
312 MTRR_SETTINGS Mtrrs;\r
313 UINTN ApCount;\r
314 BOOLEAN ClearTopLevelSmiResult;\r
315 UINTN PresentCount;\r
316\r
317 ASSERT (CpuIndex == mSmmMpSyncData->BspIndex);\r
318 ApCount = 0;\r
319\r
320 //\r
321 // Flag BSP's presence\r
322 //\r
323 mSmmMpSyncData->InsideSmm = TRUE;\r
324\r
325 //\r
326 // Initialize Debug Agent to start source level debug in BSP handler\r
327 //\r
328 InitializeDebugAgent (DEBUG_AGENT_INIT_ENTER_SMI, NULL, NULL);\r
329\r
330 //\r
331 // Mark this processor's presence\r
332 //\r
333 mSmmMpSyncData->CpuData[CpuIndex].Present = TRUE;\r
334\r
335 //\r
336 // Clear platform top level SMI status bit before calling SMI handlers. If\r
337 // we cleared it after SMI handlers are run, we would miss the SMI that\r
338 // occurs after SMI handlers are done and before SMI status bit is cleared.\r
339 //\r
340 ClearTopLevelSmiResult = ClearTopLevelSmiStatus();\r
341 ASSERT (ClearTopLevelSmiResult == TRUE);\r
342\r
343 //\r
344 // Set running processor index\r
345 //\r
346 gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu = CpuIndex;\r
347\r
348 //\r
349 // If Traditional Sync Mode or need to configure MTRRs: gather all available APs.\r
350 //\r
351 if (SyncMode == SmmCpuSyncModeTradition || SmmCpuFeaturesNeedConfigureMtrrs()) {\r
352\r
353 //\r
354 // Wait for APs to arrive\r
355 //\r
356 SmmWaitForApArrival();\r
357\r
358 //\r
359 // Lock the counter down and retrieve the number of APs\r
360 //\r
361 mSmmMpSyncData->AllCpusInSync = TRUE;\r
362 ApCount = LockdownSemaphore (&mSmmMpSyncData->Counter) - 1;\r
363\r
364 //\r
365 // Wait for all APs to get ready for programming MTRRs\r
366 //\r
367 WaitForAllAPs (ApCount);\r
368\r
369 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
370 //\r
371 // Signal all APs it's time for backup MTRRs\r
372 //\r
373 ReleaseAllAPs ();\r
374\r
375 //\r
376 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at\r
377 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set\r
378 // to a large enough value to avoid this situation.\r
379 // Note: For HT capable CPUs, threads within a core share the same set of MTRRs.\r
380 // We do the backup first and then set MTRR to avoid race condition for threads\r
381 // in the same core.\r
382 //\r
383 MtrrGetAllMtrrs(&Mtrrs);\r
384\r
385 //\r
386 // Wait for all APs to complete their MTRR saving\r
387 //\r
388 WaitForAllAPs (ApCount);\r
389\r
390 //\r
391 // Let all processors program SMM MTRRs together\r
392 //\r
393 ReleaseAllAPs ();\r
394\r
395 //\r
396 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at\r
397 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set\r
398 // to a large enough value to avoid this situation.\r
399 //\r
400 ReplaceOSMtrrs (CpuIndex);\r
401\r
402 //\r
403 // Wait for all APs to complete their MTRR programming\r
404 //\r
405 WaitForAllAPs (ApCount);\r
406 }\r
407 }\r
408\r
409 //\r
410 // The BUSY lock is initialized to Acquired state\r
411 //\r
412 AcquireSpinLockOrFail (&mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
413\r
414 //\r
9f419739 415 // Perform the pre tasks\r
529a5a86 416 //\r
9f419739 417 PerformPreTasks ();\r
529a5a86
MK
418\r
419 //\r
420 // Invoke SMM Foundation EntryPoint with the processor information context.\r
421 //\r
422 gSmmCpuPrivate->SmmCoreEntry (&gSmmCpuPrivate->SmmCoreEntryContext);\r
423\r
424 //\r
425 // Make sure all APs have completed their pending none-block tasks\r
426 //\r
427 for (Index = mMaxNumberOfCpus; Index-- > 0;) {\r
428 if (Index != CpuIndex && mSmmMpSyncData->CpuData[Index].Present) {\r
429 AcquireSpinLock (&mSmmMpSyncData->CpuData[Index].Busy);\r
430 ReleaseSpinLock (&mSmmMpSyncData->CpuData[Index].Busy);;\r
431 }\r
432 }\r
433\r
434 //\r
435 // Perform the remaining tasks\r
436 //\r
437 PerformRemainingTasks ();\r
438\r
439 //\r
440 // If Relaxed-AP Sync Mode: gather all available APs after BSP SMM handlers are done, and\r
441 // make those APs to exit SMI synchronously. APs which arrive later will be excluded and\r
442 // will run through freely.\r
443 //\r
444 if (SyncMode != SmmCpuSyncModeTradition && !SmmCpuFeaturesNeedConfigureMtrrs()) {\r
445\r
446 //\r
447 // Lock the counter down and retrieve the number of APs\r
448 //\r
449 mSmmMpSyncData->AllCpusInSync = TRUE;\r
450 ApCount = LockdownSemaphore (&mSmmMpSyncData->Counter) - 1;\r
451 //\r
452 // Make sure all APs have their Present flag set\r
453 //\r
454 while (TRUE) {\r
455 PresentCount = 0;\r
456 for (Index = mMaxNumberOfCpus; Index-- > 0;) {\r
457 if (mSmmMpSyncData->CpuData[Index].Present) {\r
458 PresentCount ++;\r
459 }\r
460 }\r
461 if (PresentCount > ApCount) {\r
462 break;\r
463 }\r
464 }\r
465 }\r
466\r
467 //\r
468 // Notify all APs to exit\r
469 //\r
470 mSmmMpSyncData->InsideSmm = FALSE;\r
471 ReleaseAllAPs ();\r
472\r
473 //\r
474 // Wait for all APs to complete their pending tasks\r
475 //\r
476 WaitForAllAPs (ApCount);\r
477\r
478 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
479 //\r
480 // Signal APs to restore MTRRs\r
481 //\r
482 ReleaseAllAPs ();\r
483\r
484 //\r
485 // Restore OS MTRRs\r
486 //\r
487 SmmCpuFeaturesReenableSmrr ();\r
488 MtrrSetAllMtrrs(&Mtrrs);\r
489\r
490 //\r
491 // Wait for all APs to complete MTRR programming\r
492 //\r
493 WaitForAllAPs (ApCount);\r
494 }\r
495\r
496 //\r
497 // Stop source level debug in BSP handler, the code below will not be\r
498 // debugged.\r
499 //\r
500 InitializeDebugAgent (DEBUG_AGENT_INIT_EXIT_SMI, NULL, NULL);\r
501\r
502 //\r
503 // Signal APs to Reset states/semaphore for this processor\r
504 //\r
505 ReleaseAllAPs ();\r
506\r
507 //\r
508 // Perform pending operations for hot-plug\r
509 //\r
510 SmmCpuUpdate ();\r
511\r
512 //\r
513 // Clear the Present flag of BSP\r
514 //\r
515 mSmmMpSyncData->CpuData[CpuIndex].Present = FALSE;\r
516\r
517 //\r
518 // Gather APs to exit SMM synchronously. Note the Present flag is cleared by now but\r
519 // WaitForAllAps does not depend on the Present flag.\r
520 //\r
521 WaitForAllAPs (ApCount);\r
522\r
523 //\r
524 // Reset BspIndex to -1, meaning BSP has not been elected.\r
525 //\r
526 if (FeaturePcdGet (PcdCpuSmmEnableBspElection)) {\r
527 mSmmMpSyncData->BspIndex = (UINT32)-1;\r
528 }\r
529\r
530 //\r
531 // Allow APs to check in from this point on\r
532 //\r
533 mSmmMpSyncData->Counter = 0;\r
534 mSmmMpSyncData->AllCpusInSync = FALSE;\r
535}\r
536\r
537/**\r
538 SMI handler for AP.\r
539\r
540 @param CpuIndex AP processor Index.\r
541 @param ValidSmi Indicates that current SMI is a valid SMI or not.\r
542 @param SyncMode SMM MP sync mode.\r
543\r
544**/\r
545VOID\r
546APHandler (\r
547 IN UINTN CpuIndex,\r
548 IN BOOLEAN ValidSmi,\r
549 IN SMM_CPU_SYNC_MODE SyncMode\r
550 )\r
551{\r
552 UINT64 Timer;\r
553 UINTN BspIndex;\r
554 MTRR_SETTINGS Mtrrs;\r
555\r
556 //\r
557 // Timeout BSP\r
558 //\r
559 for (Timer = StartSyncTimer ();\r
560 !IsSyncTimerTimeout (Timer) &&\r
561 !mSmmMpSyncData->InsideSmm;\r
562 ) {\r
563 CpuPause ();\r
564 }\r
565\r
566 if (!mSmmMpSyncData->InsideSmm) {\r
567 //\r
568 // BSP timeout in the first round\r
569 //\r
570 if (mSmmMpSyncData->BspIndex != -1) {\r
571 //\r
572 // BSP Index is known\r
573 //\r
574 BspIndex = mSmmMpSyncData->BspIndex;\r
575 ASSERT (CpuIndex != BspIndex);\r
576\r
577 //\r
578 // Send SMI IPI to bring BSP in\r
579 //\r
580 SendSmiIpi ((UINT32)gSmmCpuPrivate->ProcessorInfo[BspIndex].ProcessorId);\r
581\r
582 //\r
583 // Now clock BSP for the 2nd time\r
584 //\r
585 for (Timer = StartSyncTimer ();\r
586 !IsSyncTimerTimeout (Timer) &&\r
587 !mSmmMpSyncData->InsideSmm;\r
588 ) {\r
589 CpuPause ();\r
590 }\r
591\r
592 if (!mSmmMpSyncData->InsideSmm) {\r
593 //\r
594 // Give up since BSP is unable to enter SMM\r
595 // and signal the completion of this AP\r
596 WaitForSemaphore (&mSmmMpSyncData->Counter);\r
597 return;\r
598 }\r
599 } else {\r
600 //\r
601 // Don't know BSP index. Give up without sending IPI to BSP.\r
602 //\r
603 WaitForSemaphore (&mSmmMpSyncData->Counter);\r
604 return;\r
605 }\r
606 }\r
607\r
608 //\r
609 // BSP is available\r
610 //\r
611 BspIndex = mSmmMpSyncData->BspIndex;\r
612 ASSERT (CpuIndex != BspIndex);\r
613\r
614 //\r
615 // Mark this processor's presence\r
616 //\r
617 mSmmMpSyncData->CpuData[CpuIndex].Present = TRUE;\r
618\r
619 if (SyncMode == SmmCpuSyncModeTradition || SmmCpuFeaturesNeedConfigureMtrrs()) {\r
620 //\r
621 // Notify BSP of arrival at this point\r
622 //\r
623 ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);\r
624 }\r
625\r
626 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
627 //\r
628 // Wait for the signal from BSP to backup MTRRs\r
629 //\r
630 WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);\r
631\r
632 //\r
633 // Backup OS MTRRs\r
634 //\r
635 MtrrGetAllMtrrs(&Mtrrs);\r
636\r
637 //\r
638 // Signal BSP the completion of this AP\r
639 //\r
640 ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);\r
641\r
642 //\r
643 // Wait for BSP's signal to program MTRRs\r
644 //\r
645 WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);\r
646\r
647 //\r
648 // Replace OS MTRRs with SMI MTRRs\r
649 //\r
650 ReplaceOSMtrrs (CpuIndex);\r
651\r
652 //\r
653 // Signal BSP the completion of this AP\r
654 //\r
655 ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);\r
656 }\r
657\r
658 while (TRUE) {\r
659 //\r
660 // Wait for something to happen\r
661 //\r
662 WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);\r
663\r
664 //\r
665 // Check if BSP wants to exit SMM\r
666 //\r
667 if (!mSmmMpSyncData->InsideSmm) {\r
668 break;\r
669 }\r
670\r
671 //\r
672 // BUSY should be acquired by SmmStartupThisAp()\r
673 //\r
674 ASSERT (\r
675 !AcquireSpinLockOrFail (&mSmmMpSyncData->CpuData[CpuIndex].Busy)\r
676 );\r
677\r
678 //\r
679 // Invoke the scheduled procedure\r
680 //\r
681 (*mSmmMpSyncData->CpuData[CpuIndex].Procedure) (\r
682 (VOID*)mSmmMpSyncData->CpuData[CpuIndex].Parameter\r
683 );\r
684\r
685 //\r
686 // Release BUSY\r
687 //\r
688 ReleaseSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
689 }\r
690\r
691 if (SmmCpuFeaturesNeedConfigureMtrrs()) {\r
692 //\r
693 // Notify BSP the readiness of this AP to program MTRRs\r
694 //\r
695 ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);\r
696\r
697 //\r
698 // Wait for the signal from BSP to program MTRRs\r
699 //\r
700 WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);\r
701\r
702 //\r
703 // Restore OS MTRRs\r
704 //\r
705 SmmCpuFeaturesReenableSmrr ();\r
706 MtrrSetAllMtrrs(&Mtrrs);\r
707 }\r
708\r
709 //\r
710 // Notify BSP the readiness of this AP to Reset states/semaphore for this processor\r
711 //\r
712 ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);\r
713\r
714 //\r
715 // Wait for the signal from BSP to Reset states/semaphore for this processor\r
716 //\r
717 WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);\r
718\r
719 //\r
720 // Reset states/semaphore for this processor\r
721 //\r
722 mSmmMpSyncData->CpuData[CpuIndex].Present = FALSE;\r
723\r
724 //\r
725 // Notify BSP the readiness of this AP to exit SMM\r
726 //\r
727 ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);\r
728\r
729}\r
730\r
731/**\r
732 Create 4G PageTable in SMRAM.\r
733\r
734 @param ExtraPages Additional page numbers besides for 4G memory\r
735 @return PageTable Address\r
736\r
737**/\r
738UINT32\r
739Gen4GPageTable (\r
740 IN UINTN ExtraPages\r
741 )\r
742{\r
743 VOID *PageTable;\r
744 UINTN Index;\r
745 UINT64 *Pte;\r
746 UINTN PagesNeeded;\r
747 UINTN Low2MBoundary;\r
748 UINTN High2MBoundary;\r
749 UINTN Pages;\r
750 UINTN GuardPage;\r
751 UINT64 *Pdpte;\r
752 UINTN PageIndex;\r
753 UINTN PageAddress;\r
754\r
755 Low2MBoundary = 0;\r
756 High2MBoundary = 0;\r
757 PagesNeeded = 0;\r
758 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
759 //\r
760 // Add one more page for known good stack, then find the lower 2MB aligned address.\r
761 //\r
762 Low2MBoundary = (mSmmStackArrayBase + EFI_PAGE_SIZE) & ~(SIZE_2MB-1);\r
763 //\r
764 // Add two more pages for known good stack and stack guard page,\r
765 // then find the lower 2MB aligned address.\r
766 //\r
767 High2MBoundary = (mSmmStackArrayEnd - mSmmStackSize + EFI_PAGE_SIZE * 2) & ~(SIZE_2MB-1);\r
768 PagesNeeded = ((High2MBoundary - Low2MBoundary) / SIZE_2MB) + 1;\r
769 }\r
770 //\r
771 // Allocate the page table\r
772 //\r
9daa916d 773 PageTable = AllocatePageTableMemory (ExtraPages + 5 + PagesNeeded);\r
529a5a86
MK
774 ASSERT (PageTable != NULL);\r
775\r
776 PageTable = (VOID *)((UINTN)PageTable + EFI_PAGES_TO_SIZE (ExtraPages));\r
777 Pte = (UINT64*)PageTable;\r
778\r
779 //\r
780 // Zero out all page table entries first\r
781 //\r
782 ZeroMem (Pte, EFI_PAGES_TO_SIZE (1));\r
783\r
784 //\r
785 // Set Page Directory Pointers\r
786 //\r
787 for (Index = 0; Index < 4; Index++) {\r
8e496a7a 788 Pte[Index] = (UINTN)PageTable + EFI_PAGE_SIZE * (Index + 1) + PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
789 }\r
790 Pte += EFI_PAGE_SIZE / sizeof (*Pte);\r
791\r
792 //\r
793 // Fill in Page Directory Entries\r
794 //\r
795 for (Index = 0; Index < EFI_PAGE_SIZE * 4 / sizeof (*Pte); Index++) {\r
8e496a7a 796 Pte[Index] = (Index << 21) | IA32_PG_PS | PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
797 }\r
798\r
799 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
800 Pages = (UINTN)PageTable + EFI_PAGES_TO_SIZE (5);\r
801 GuardPage = mSmmStackArrayBase + EFI_PAGE_SIZE;\r
802 Pdpte = (UINT64*)PageTable;\r
803 for (PageIndex = Low2MBoundary; PageIndex <= High2MBoundary; PageIndex += SIZE_2MB) {\r
804 Pte = (UINT64*)(UINTN)(Pdpte[BitFieldRead32 ((UINT32)PageIndex, 30, 31)] & ~(EFI_PAGE_SIZE - 1));\r
8e496a7a 805 Pte[BitFieldRead32 ((UINT32)PageIndex, 21, 29)] = (UINT64)Pages | PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
806 //\r
807 // Fill in Page Table Entries\r
808 //\r
809 Pte = (UINT64*)Pages;\r
810 PageAddress = PageIndex;\r
811 for (Index = 0; Index < EFI_PAGE_SIZE / sizeof (*Pte); Index++) {\r
812 if (PageAddress == GuardPage) {\r
813 //\r
814 // Mark the guard page as non-present\r
815 //\r
816 Pte[Index] = PageAddress;\r
817 GuardPage += mSmmStackSize;\r
818 if (GuardPage > mSmmStackArrayEnd) {\r
819 GuardPage = 0;\r
820 }\r
821 } else {\r
8e496a7a 822 Pte[Index] = PageAddress | PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
823 }\r
824 PageAddress+= EFI_PAGE_SIZE;\r
825 }\r
826 Pages += EFI_PAGE_SIZE;\r
827 }\r
828 }\r
829\r
830 return (UINT32)(UINTN)PageTable;\r
831}\r
832\r
833/**\r
834 Set memory cache ability.\r
835\r
836 @param PageTable PageTable Address\r
837 @param Address Memory Address to change cache ability\r
838 @param Cacheability Cache ability to set\r
839\r
840**/\r
841VOID\r
842SetCacheability (\r
843 IN UINT64 *PageTable,\r
844 IN UINTN Address,\r
845 IN UINT8 Cacheability\r
846 )\r
847{\r
848 UINTN PTIndex;\r
849 VOID *NewPageTableAddress;\r
850 UINT64 *NewPageTable;\r
851 UINTN Index;\r
852\r
853 ASSERT ((Address & EFI_PAGE_MASK) == 0);\r
854\r
855 if (sizeof (UINTN) == sizeof (UINT64)) {\r
856 PTIndex = (UINTN)RShiftU64 (Address, 39) & 0x1ff;\r
857 ASSERT (PageTable[PTIndex] & IA32_PG_P);\r
858 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & gPhyMask);\r
859 }\r
860\r
861 PTIndex = (UINTN)RShiftU64 (Address, 30) & 0x1ff;\r
862 ASSERT (PageTable[PTIndex] & IA32_PG_P);\r
863 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & gPhyMask);\r
864\r
865 //\r
866 // A perfect implementation should check the original cacheability with the\r
867 // one being set, and break a 2M page entry into pieces only when they\r
868 // disagreed.\r
869 //\r
870 PTIndex = (UINTN)RShiftU64 (Address, 21) & 0x1ff;\r
871 if ((PageTable[PTIndex] & IA32_PG_PS) != 0) {\r
872 //\r
873 // Allocate a page from SMRAM\r
874 //\r
9daa916d 875 NewPageTableAddress = AllocatePageTableMemory (1);\r
529a5a86
MK
876 ASSERT (NewPageTableAddress != NULL);\r
877\r
878 NewPageTable = (UINT64 *)NewPageTableAddress;\r
879\r
880 for (Index = 0; Index < 0x200; Index++) {\r
881 NewPageTable[Index] = PageTable[PTIndex];\r
882 if ((NewPageTable[Index] & IA32_PG_PAT_2M) != 0) {\r
883 NewPageTable[Index] &= ~((UINT64)IA32_PG_PAT_2M);\r
884 NewPageTable[Index] |= (UINT64)IA32_PG_PAT_4K;\r
885 }\r
886 NewPageTable[Index] |= (UINT64)(Index << EFI_PAGE_SHIFT);\r
887 }\r
888\r
8e496a7a 889 PageTable[PTIndex] = ((UINTN)NewPageTableAddress & gPhyMask) | PAGE_ATTRIBUTE_BITS;\r
529a5a86
MK
890 }\r
891\r
892 ASSERT (PageTable[PTIndex] & IA32_PG_P);\r
893 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & gPhyMask);\r
894\r
895 PTIndex = (UINTN)RShiftU64 (Address, 12) & 0x1ff;\r
896 ASSERT (PageTable[PTIndex] & IA32_PG_P);\r
897 PageTable[PTIndex] &= ~((UINT64)((IA32_PG_PAT_4K | IA32_PG_CD | IA32_PG_WT)));\r
898 PageTable[PTIndex] |= (UINT64)Cacheability;\r
899}\r
900\r
901\r
902/**\r
903 Schedule a procedure to run on the specified CPU.\r
904\r
905 @param Procedure The address of the procedure to run\r
906 @param CpuIndex Target CPU Index\r
907 @param ProcArguments The parameter to pass to the procedure\r
908\r
909 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
910 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
911 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
912 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
913 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
914\r
915**/\r
916EFI_STATUS\r
917EFIAPI\r
918SmmStartupThisAp (\r
919 IN EFI_AP_PROCEDURE Procedure,\r
920 IN UINTN CpuIndex,\r
921 IN OUT VOID *ProcArguments OPTIONAL\r
922 )\r
923{\r
924 if (CpuIndex >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus ||\r
925 CpuIndex == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu ||\r
926 !mSmmMpSyncData->CpuData[CpuIndex].Present ||\r
927 gSmmCpuPrivate->Operation[CpuIndex] == SmmCpuRemove ||\r
928 !AcquireSpinLockOrFail (&mSmmMpSyncData->CpuData[CpuIndex].Busy)) {\r
929 return EFI_INVALID_PARAMETER;\r
930 }\r
931\r
932 mSmmMpSyncData->CpuData[CpuIndex].Procedure = Procedure;\r
933 mSmmMpSyncData->CpuData[CpuIndex].Parameter = ProcArguments;\r
934 ReleaseSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);\r
935\r
936 if (FeaturePcdGet (PcdCpuSmmBlockStartupThisAp)) {\r
937 AcquireSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
938 ReleaseSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
939 }\r
940 return EFI_SUCCESS;\r
941}\r
942\r
f45f2d4a
JY
943/**\r
944 This funciton sets DR6 & DR7 according to SMM save state, before running SMM C code.\r
945 They are useful when you want to enable hardware breakpoints in SMM without entry SMM mode.\r
946\r
947 NOTE: It might not be appreciated in runtime since it might\r
948 conflict with OS debugging facilities. Turn them off in RELEASE.\r
949\r
950 @param CpuIndex CPU Index\r
951\r
952**/\r
953VOID\r
954EFIAPI\r
955CpuSmmDebugEntry (\r
956 IN UINTN CpuIndex\r
957 )\r
958{\r
959 SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
960 \r
961 if (FeaturePcdGet (PcdCpuSmmDebug)) {\r
962 CpuSaveState = (SMRAM_SAVE_STATE_MAP *)gSmst->CpuSaveState[CpuIndex];\r
963 if (mSmmSaveStateRegisterLma == EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT) {\r
964 AsmWriteDr6 (CpuSaveState->x86._DR6);\r
965 AsmWriteDr7 (CpuSaveState->x86._DR7);\r
966 } else {\r
967 AsmWriteDr6 ((UINTN)CpuSaveState->x64._DR6);\r
968 AsmWriteDr7 ((UINTN)CpuSaveState->x64._DR7);\r
969 }\r
970 }\r
971}\r
972\r
973/**\r
974 This funciton restores DR6 & DR7 to SMM save state.\r
975\r
976 NOTE: It might not be appreciated in runtime since it might\r
977 conflict with OS debugging facilities. Turn them off in RELEASE.\r
978\r
979 @param CpuIndex CPU Index\r
980\r
981**/\r
982VOID\r
983EFIAPI\r
984CpuSmmDebugExit (\r
985 IN UINTN CpuIndex\r
986 )\r
987{\r
988 SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
989\r
990 if (FeaturePcdGet (PcdCpuSmmDebug)) {\r
991 CpuSaveState = (SMRAM_SAVE_STATE_MAP *)gSmst->CpuSaveState[CpuIndex];\r
992 if (mSmmSaveStateRegisterLma == EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT) {\r
993 CpuSaveState->x86._DR7 = (UINT32)AsmReadDr7 ();\r
994 CpuSaveState->x86._DR6 = (UINT32)AsmReadDr6 ();\r
995 } else {\r
996 CpuSaveState->x64._DR7 = AsmReadDr7 ();\r
997 CpuSaveState->x64._DR6 = AsmReadDr6 ();\r
998 }\r
999 }\r
1000}\r
1001\r
529a5a86
MK
1002/**\r
1003 C function for SMI entry, each processor comes here upon SMI trigger.\r
1004\r
1005 @param CpuIndex CPU Index\r
1006\r
1007**/\r
1008VOID\r
1009EFIAPI\r
1010SmiRendezvous (\r
1011 IN UINTN CpuIndex\r
1012 )\r
1013{\r
1014 EFI_STATUS Status;\r
1015 BOOLEAN ValidSmi;\r
1016 BOOLEAN IsBsp;\r
1017 BOOLEAN BspInProgress;\r
1018 UINTN Index;\r
1019 UINTN Cr2;\r
1020\r
1021 //\r
1022 // Save Cr2 because Page Fault exception in SMM may override its value\r
1023 //\r
1024 Cr2 = AsmReadCr2 ();\r
1025\r
1026 //\r
1027 // Perform CPU specific entry hooks\r
1028 //\r
1029 SmmCpuFeaturesRendezvousEntry (CpuIndex);\r
1030\r
1031 //\r
1032 // Determine if this is a valid SMI\r
1033 //\r
1034 ValidSmi = PlatformValidSmi();\r
1035\r
1036 //\r
1037 // Determine if BSP has been already in progress. Note this must be checked after\r
1038 // ValidSmi because BSP may clear a valid SMI source after checking in.\r
1039 //\r
1040 BspInProgress = mSmmMpSyncData->InsideSmm;\r
1041\r
1042 if (!BspInProgress && !ValidSmi) {\r
1043 //\r
1044 // If we reach here, it means when we sampled the ValidSmi flag, SMI status had not\r
1045 // been cleared by BSP in a new SMI run (so we have a truly invalid SMI), or SMI\r
1046 // status had been cleared by BSP and an existing SMI run has almost ended. (Note\r
1047 // we sampled ValidSmi flag BEFORE judging BSP-in-progress status.) In both cases, there\r
1048 // is nothing we need to do.\r
1049 //\r
1050 goto Exit;\r
1051 } else {\r
1052 //\r
1053 // Signal presence of this processor\r
1054 //\r
1055 if (ReleaseSemaphore (&mSmmMpSyncData->Counter) == 0) {\r
1056 //\r
1057 // BSP has already ended the synchronization, so QUIT!!!\r
1058 //\r
1059\r
1060 //\r
1061 // Wait for BSP's signal to finish SMI\r
1062 //\r
1063 while (mSmmMpSyncData->AllCpusInSync) {\r
1064 CpuPause ();\r
1065 }\r
1066 goto Exit;\r
1067 } else {\r
1068\r
1069 //\r
1070 // The BUSY lock is initialized to Released state.\r
1071 // This needs to be done early enough to be ready for BSP's SmmStartupThisAp() call.\r
1072 // E.g., with Relaxed AP flow, SmmStartupThisAp() may be called immediately\r
1073 // after AP's present flag is detected.\r
1074 //\r
1075 InitializeSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy);\r
1076 }\r
1077\r
1078 //\r
1079 // Try to enable NX\r
1080 //\r
1081 if (mXdSupported) {\r
1082 ActivateXd ();\r
1083 }\r
1084\r
1085 if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
1086 ActivateSmmProfile (CpuIndex);\r
1087 }\r
1088\r
1089 if (BspInProgress) {\r
1090 //\r
1091 // BSP has been elected. Follow AP path, regardless of ValidSmi flag\r
1092 // as BSP may have cleared the SMI status\r
1093 //\r
1094 APHandler (CpuIndex, ValidSmi, mSmmMpSyncData->EffectiveSyncMode);\r
1095 } else {\r
1096 //\r
1097 // We have a valid SMI\r
1098 //\r
1099\r
1100 //\r
1101 // Elect BSP\r
1102 //\r
1103 IsBsp = FALSE;\r
1104 if (FeaturePcdGet (PcdCpuSmmEnableBspElection)) {\r
1105 if (!mSmmMpSyncData->SwitchBsp || mSmmMpSyncData->CandidateBsp[CpuIndex]) {\r
1106 //\r
1107 // Call platform hook to do BSP election\r
1108 //\r
1109 Status = PlatformSmmBspElection (&IsBsp);\r
1110 if (EFI_SUCCESS == Status) {\r
1111 //\r
1112 // Platform hook determines successfully\r
1113 //\r
1114 if (IsBsp) {\r
1115 mSmmMpSyncData->BspIndex = (UINT32)CpuIndex;\r
1116 }\r
1117 } else {\r
1118 //\r
1119 // Platform hook fails to determine, use default BSP election method\r
1120 //\r
1121 InterlockedCompareExchange32 (\r
1122 (UINT32*)&mSmmMpSyncData->BspIndex,\r
1123 (UINT32)-1,\r
1124 (UINT32)CpuIndex\r
1125 );\r
1126 }\r
1127 }\r
1128 }\r
1129\r
1130 //\r
1131 // "mSmmMpSyncData->BspIndex == CpuIndex" means this is the BSP\r
1132 //\r
1133 if (mSmmMpSyncData->BspIndex == CpuIndex) {\r
1134\r
1135 //\r
1136 // Clear last request for SwitchBsp.\r
1137 //\r
1138 if (mSmmMpSyncData->SwitchBsp) {\r
1139 mSmmMpSyncData->SwitchBsp = FALSE;\r
1140 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
1141 mSmmMpSyncData->CandidateBsp[Index] = FALSE;\r
1142 }\r
1143 }\r
1144\r
1145 if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
1146 SmmProfileRecordSmiNum ();\r
1147 }\r
1148\r
1149 //\r
1150 // BSP Handler is always called with a ValidSmi == TRUE\r
1151 //\r
1152 BSPHandler (CpuIndex, mSmmMpSyncData->EffectiveSyncMode);\r
1153\r
1154 } else {\r
1155 APHandler (CpuIndex, ValidSmi, mSmmMpSyncData->EffectiveSyncMode);\r
1156 }\r
1157 }\r
1158\r
1159 ASSERT (mSmmMpSyncData->CpuData[CpuIndex].Run == 0);\r
1160\r
1161 //\r
1162 // Wait for BSP's signal to exit SMI\r
1163 //\r
1164 while (mSmmMpSyncData->AllCpusInSync) {\r
1165 CpuPause ();\r
1166 }\r
1167 }\r
1168\r
1169Exit:\r
1170 SmmCpuFeaturesRendezvousExit (CpuIndex);\r
1171 //\r
1172 // Restore Cr2\r
1173 //\r
1174 AsmWriteCr2 (Cr2);\r
1175}\r
1176\r
1177\r
1178/**\r
1179 Initialize un-cacheable data.\r
1180\r
1181**/\r
1182VOID\r
1183EFIAPI\r
1184InitializeMpSyncData (\r
1185 VOID\r
1186 )\r
1187{\r
1188 if (mSmmMpSyncData != NULL) {\r
1189 ZeroMem (mSmmMpSyncData, mSmmMpSyncDataSize);\r
1190 mSmmMpSyncData->CpuData = (SMM_CPU_DATA_BLOCK *)((UINT8 *)mSmmMpSyncData + sizeof (SMM_DISPATCHER_MP_SYNC_DATA));\r
1191 mSmmMpSyncData->CandidateBsp = (BOOLEAN *)(mSmmMpSyncData->CpuData + gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus);\r
1192 if (FeaturePcdGet (PcdCpuSmmEnableBspElection)) {\r
1193 //\r
1194 // Enable BSP election by setting BspIndex to -1\r
1195 //\r
1196 mSmmMpSyncData->BspIndex = (UINT32)-1;\r
1197 }\r
1198 mSmmMpSyncData->EffectiveSyncMode = (SMM_CPU_SYNC_MODE) PcdGet8 (PcdCpuSmmSyncMode);\r
1199 }\r
1200}\r
1201\r
1202/**\r
1203 Initialize global data for MP synchronization.\r
1204\r
1205 @param Stacks Base address of SMI stack buffer for all processors.\r
1206 @param StackSize Stack size for each processor in SMM.\r
1207\r
1208**/\r
1209UINT32\r
1210InitializeMpServiceData (\r
1211 IN VOID *Stacks,\r
1212 IN UINTN StackSize\r
1213 )\r
1214{\r
1215 UINT32 Cr3;\r
1216 UINTN Index;\r
1217 MTRR_SETTINGS *Mtrr;\r
1218 PROCESSOR_SMM_DESCRIPTOR *Psd;\r
529a5a86 1219 UINT8 *GdtTssTables;\r
529a5a86
MK
1220 UINTN GdtTableStepSize;\r
1221\r
1222 //\r
1223 // Initialize physical address mask\r
1224 // NOTE: Physical memory above virtual address limit is not supported !!!\r
1225 //\r
1226 AsmCpuid (0x80000008, (UINT32*)&Index, NULL, NULL, NULL);\r
1227 gPhyMask = LShiftU64 (1, (UINT8)Index) - 1;\r
1228 gPhyMask &= (1ull << 48) - EFI_PAGE_SIZE;\r
1229\r
1230 //\r
1231 // Create page tables\r
1232 //\r
1233 Cr3 = SmmInitPageTable ();\r
1234\r
fe5f1949 1235 GdtTssTables = InitGdt (Cr3, &GdtTableStepSize);\r
529a5a86
MK
1236\r
1237 //\r
1238 // Initialize PROCESSOR_SMM_DESCRIPTOR for each CPU\r
1239 //\r
1240 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
1241 Psd = (PROCESSOR_SMM_DESCRIPTOR *)(VOID *)(UINTN)(mCpuHotPlugData.SmBase[Index] + SMM_PSD_OFFSET);\r
1242 CopyMem (Psd, &gcPsd, sizeof (gcPsd));\r
fe5f1949
JY
1243 Psd->SmmGdtPtr = (UINT64)(UINTN)(GdtTssTables + GdtTableStepSize * Index);\r
1244 Psd->SmmGdtSize = gcSmiGdtr.Limit + 1;\r
529a5a86
MK
1245\r
1246 //\r
1247 // Install SMI handler\r
1248 //\r
1249 InstallSmiHandler (\r
1250 Index,\r
1251 (UINT32)mCpuHotPlugData.SmBase[Index],\r
1252 (VOID*)((UINTN)Stacks + (StackSize * Index)),\r
1253 StackSize,\r
1254 (UINTN)Psd->SmmGdtPtr,\r
1255 Psd->SmmGdtSize,\r
1256 gcSmiIdtr.Base,\r
1257 gcSmiIdtr.Limit + 1,\r
1258 Cr3\r
1259 );\r
1260 }\r
1261\r
1262 //\r
1263 // Initialize mSmmMpSyncData\r
1264 //\r
1265 mSmmMpSyncDataSize = sizeof (SMM_DISPATCHER_MP_SYNC_DATA) +\r
1266 (sizeof (SMM_CPU_DATA_BLOCK) + sizeof (BOOLEAN)) * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;\r
1267 mSmmMpSyncData = (SMM_DISPATCHER_MP_SYNC_DATA*) AllocatePages (EFI_SIZE_TO_PAGES (mSmmMpSyncDataSize));\r
1268 ASSERT (mSmmMpSyncData != NULL);\r
1269 InitializeMpSyncData ();\r
1270\r
1271 //\r
1272 // Record current MTRR settings\r
1273 //\r
1274 ZeroMem(gSmiMtrrs, sizeof (gSmiMtrrs));\r
1275 Mtrr = (MTRR_SETTINGS*)gSmiMtrrs;\r
1276 MtrrGetAllMtrrs (Mtrr);\r
1277\r
1278 return Cr3;\r
1279}\r
1280\r
1281/**\r
1282\r
1283 Register the SMM Foundation entry point.\r
1284\r
1285 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance\r
1286 @param SmmEntryPoint SMM Foundation EntryPoint\r
1287\r
1288 @retval EFI_SUCCESS Successfully to register SMM foundation entry point\r
1289\r
1290**/\r
1291EFI_STATUS\r
1292EFIAPI\r
1293RegisterSmmEntry (\r
1294 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL *This,\r
1295 IN EFI_SMM_ENTRY_POINT SmmEntryPoint\r
1296 )\r
1297{\r
1298 //\r
1299 // Record SMM Foundation EntryPoint, later invoke it on SMI entry vector.\r
1300 //\r
1301 gSmmCpuPrivate->SmmCoreEntry = SmmEntryPoint;\r
1302 return EFI_SUCCESS;\r
1303}\r