]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg PiSmmCore: Unregister each other for LegacyBoot and EBS
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
1 /** @file
2 SMM Core Main Entry Point
3
4 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PiSmmCore.h"
16
17 //
18 // Physical pointer to private structure shared between SMM IPL and the SMM Core
19 //
20 SMM_CORE_PRIVATE_DATA *gSmmCorePrivate;
21
22 //
23 // SMM Core global variable for SMM System Table. Only accessed as a physical structure in SMRAM.
24 //
25 EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst = {
26 {
27 SMM_SMST_SIGNATURE,
28 EFI_SMM_SYSTEM_TABLE2_REVISION,
29 sizeof (gSmmCoreSmst.Hdr)
30 },
31 NULL, // SmmFirmwareVendor
32 0, // SmmFirmwareRevision
33 SmmInstallConfigurationTable,
34 {
35 {
36 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmMemRead
37 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmMemWrite
38 },
39 {
40 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmIoRead
41 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmIoWrite
42 }
43 },
44 SmmAllocatePool,
45 SmmFreePool,
46 SmmAllocatePages,
47 SmmFreePages,
48 NULL, // SmmStartupThisAp
49 0, // CurrentlyExecutingCpu
50 0, // NumberOfCpus
51 NULL, // CpuSaveStateSize
52 NULL, // CpuSaveState
53 0, // NumberOfTableEntries
54 NULL, // SmmConfigurationTable
55 SmmInstallProtocolInterface,
56 SmmUninstallProtocolInterface,
57 SmmHandleProtocol,
58 SmmRegisterProtocolNotify,
59 SmmLocateHandle,
60 SmmLocateProtocol,
61 SmiManage,
62 SmiHandlerRegister,
63 SmiHandlerUnRegister
64 };
65
66 //
67 // Flag to determine if the platform has performed a legacy boot.
68 // If this flag is TRUE, then the runtime code and runtime data associated with the
69 // SMM IPL are converted to free memory, so the SMM Core must guarantee that is
70 // does not touch of the code/data associated with the SMM IPL if this flag is TRUE.
71 //
72 BOOLEAN mInLegacyBoot = FALSE;
73
74 //
75 // Flag to determine if it is during S3 resume.
76 // It will be set in S3 entry callback and cleared at EndOfS3Resume.
77 //
78 BOOLEAN mDuringS3Resume = FALSE;
79
80 //
81 // Table of SMI Handlers that are registered by the SMM Core when it is initialized
82 //
83 SMM_CORE_SMI_HANDLERS mSmmCoreSmiHandlers[] = {
84 { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE },
85 { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE },
86 { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE },
87 { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid, NULL, FALSE },
88 { SmmReadyToBootHandler, &gEfiEventReadyToBootGuid, NULL, FALSE },
89 { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, TRUE },
90 { SmmEndOfS3ResumeHandler, &gEdkiiEndOfS3ResumeGuid, NULL, FALSE },
91 { NULL, NULL, NULL, FALSE }
92 };
93
94 UINTN mFullSmramRangeCount;
95 EFI_SMRAM_DESCRIPTOR *mFullSmramRanges;
96
97 EFI_SMM_DRIVER_ENTRY *mSmmCoreDriverEntry;
98
99 EFI_LOADED_IMAGE_PROTOCOL *mSmmCoreLoadedImage;
100
101 /**
102 Place holder function until all the SMM System Table Service are available.
103
104 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
105
106 @param Arg1 Undefined
107 @param Arg2 Undefined
108 @param Arg3 Undefined
109 @param Arg4 Undefined
110 @param Arg5 Undefined
111
112 @return EFI_NOT_AVAILABLE_YET
113
114 **/
115 EFI_STATUS
116 EFIAPI
117 SmmEfiNotAvailableYetArg5 (
118 UINTN Arg1,
119 UINTN Arg2,
120 UINTN Arg3,
121 UINTN Arg4,
122 UINTN Arg5
123 )
124 {
125 //
126 // This function should never be executed. If it does, then the architectural protocols
127 // have not been designed correctly.
128 //
129 return EFI_NOT_AVAILABLE_YET;
130 }
131
132 /**
133 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM
134 Core uses this signal to know that a Legacy Boot has been performed and that
135 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can
136 not be accessed from SMM anymore since that structure is considered free memory by
137 a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM
138 driver that system enter legacy boot.
139
140 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
141 @param Context Points to an optional handler context which was specified when the handler was registered.
142 @param CommBuffer A pointer to a collection of data in memory that will
143 be conveyed from a non-SMM environment into an SMM environment.
144 @param CommBufferSize The size of the CommBuffer.
145
146 @return Status Code
147
148 **/
149 EFI_STATUS
150 EFIAPI
151 SmmLegacyBootHandler (
152 IN EFI_HANDLE DispatchHandle,
153 IN CONST VOID *Context, OPTIONAL
154 IN OUT VOID *CommBuffer, OPTIONAL
155 IN OUT UINTN *CommBufferSize OPTIONAL
156 )
157 {
158 EFI_STATUS Status;
159 EFI_HANDLE SmmHandle;
160 UINTN Index;
161
162 //
163 // Install SMM Legacy Boot protocol.
164 //
165 SmmHandle = NULL;
166 Status = SmmInstallProtocolInterface (
167 &SmmHandle,
168 &gEdkiiSmmLegacyBootProtocolGuid,
169 EFI_NATIVE_INTERFACE,
170 NULL
171 );
172
173 mInLegacyBoot = TRUE;
174
175 SmiHandlerUnRegister (DispatchHandle);
176
177 //
178 // It is legacy boot, unregister ExitBootService SMI handler.
179 //
180 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
181 if (CompareGuid (mSmmCoreSmiHandlers[Index].HandlerType, &gEfiEventExitBootServicesGuid)) {
182 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);
183 break;
184 }
185 }
186
187 return Status;
188 }
189
190 /**
191 Software SMI handler that is called when an Exit Boot Services event is signalled.
192 Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver
193 that system enter exit boot services.
194
195 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
196 @param Context Points to an optional handler context which was specified when the handler was registered.
197 @param CommBuffer A pointer to a collection of data in memory that will
198 be conveyed from a non-SMM environment into an SMM environment.
199 @param CommBufferSize The size of the CommBuffer.
200
201 @return Status Code
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 SmmExitBootServicesHandler (
207 IN EFI_HANDLE DispatchHandle,
208 IN CONST VOID *Context, OPTIONAL
209 IN OUT VOID *CommBuffer, OPTIONAL
210 IN OUT UINTN *CommBufferSize OPTIONAL
211 )
212 {
213 EFI_STATUS Status;
214 EFI_HANDLE SmmHandle;
215 UINTN Index;
216
217 //
218 // Install SMM Exit Boot Services protocol.
219 //
220 SmmHandle = NULL;
221 Status = SmmInstallProtocolInterface (
222 &SmmHandle,
223 &gEdkiiSmmExitBootServicesProtocolGuid,
224 EFI_NATIVE_INTERFACE,
225 NULL
226 );
227
228 SmiHandlerUnRegister (DispatchHandle);
229
230 //
231 // It is UEFI boot, unregister LegacyBoot SMI handler.
232 //
233 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
234 if (CompareGuid (mSmmCoreSmiHandlers[Index].HandlerType, &gEfiEventLegacyBootGuid)) {
235 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);
236 break;
237 }
238 }
239
240 return Status;
241 }
242
243 /**
244 Main entry point for an SMM handler dispatch or communicate-based callback.
245
246 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
247 @param[in] Context Points to an optional handler context which was specified when the
248 handler was registered.
249 @param[in,out] CommBuffer A pointer to a collection of data in memory that will
250 be conveyed from a non-SMM environment into an SMM environment.
251 @param[in,out] CommBufferSize The size of the CommBuffer.
252
253 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
254 should still be called.
255 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
256 still be called.
257 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
258 be called.
259 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
260 **/
261 EFI_STATUS
262 EFIAPI
263 SmmS3EntryCallBack (
264 IN EFI_HANDLE DispatchHandle,
265 IN CONST VOID *Context OPTIONAL,
266 IN OUT VOID *CommBuffer OPTIONAL,
267 IN OUT UINTN *CommBufferSize OPTIONAL
268 )
269 {
270 mDuringS3Resume = TRUE;
271 return EFI_SUCCESS;
272 }
273
274 /**
275 Software SMI handler that is called when an Ready To Boot event is signalled.
276 Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver
277 that system enter ready to boot.
278
279 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
280 @param Context Points to an optional handler context which was specified when the handler was registered.
281 @param CommBuffer A pointer to a collection of data in memory that will
282 be conveyed from a non-SMM environment into an SMM environment.
283 @param CommBufferSize The size of the CommBuffer.
284
285 @return Status Code
286
287 **/
288 EFI_STATUS
289 EFIAPI
290 SmmReadyToBootHandler (
291 IN EFI_HANDLE DispatchHandle,
292 IN CONST VOID *Context, OPTIONAL
293 IN OUT VOID *CommBuffer, OPTIONAL
294 IN OUT UINTN *CommBufferSize OPTIONAL
295 )
296 {
297 EFI_STATUS Status;
298 EFI_HANDLE SmmHandle;
299 EFI_SMM_SX_DISPATCH2_PROTOCOL *SxDispatch;
300 EFI_SMM_SX_REGISTER_CONTEXT EntryRegisterContext;
301 EFI_HANDLE S3EntryHandle;
302
303 //
304 // Install SMM Ready To Boot protocol.
305 //
306 SmmHandle = NULL;
307 Status = SmmInstallProtocolInterface (
308 &SmmHandle,
309 &gEdkiiSmmReadyToBootProtocolGuid,
310 EFI_NATIVE_INTERFACE,
311 NULL
312 );
313
314 SmiHandlerUnRegister (DispatchHandle);
315
316 //
317 // Locate SmmSxDispatch2 protocol.
318 //
319 Status = SmmLocateProtocol (
320 &gEfiSmmSxDispatch2ProtocolGuid,
321 NULL,
322 &SxDispatch
323 );
324 if (!EFI_ERROR (Status)) {
325 //
326 // Register a S3 entry callback function to
327 // determine if it will be during S3 resume.
328 //
329 EntryRegisterContext.Type = SxS3;
330 EntryRegisterContext.Phase = SxEntry;
331 Status = SxDispatch->Register (
332 SxDispatch,
333 SmmS3EntryCallBack,
334 &EntryRegisterContext,
335 &S3EntryHandle
336 );
337 ASSERT_EFI_ERROR (Status);
338 }
339
340 return EFI_SUCCESS;
341 }
342
343 /**
344 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added
345 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the
346 Software SMIs that are nor required after SMRAM is locked and installs the
347 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about
348 to be locked. It also verifies the SMM CPU I/O 2 Protocol has been installed
349 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.
350
351 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
352 @param Context Points to an optional handler context which was specified when the handler was registered.
353 @param CommBuffer A pointer to a collection of data in memory that will
354 be conveyed from a non-SMM environment into an SMM environment.
355 @param CommBufferSize The size of the CommBuffer.
356
357 @return Status Code
358
359 **/
360 EFI_STATUS
361 EFIAPI
362 SmmReadyToLockHandler (
363 IN EFI_HANDLE DispatchHandle,
364 IN CONST VOID *Context, OPTIONAL
365 IN OUT VOID *CommBuffer, OPTIONAL
366 IN OUT UINTN *CommBufferSize OPTIONAL
367 )
368 {
369 EFI_STATUS Status;
370 UINTN Index;
371 EFI_HANDLE SmmHandle;
372 VOID *Interface;
373
374 //
375 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
376 //
377 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
378 if (mSmmCoreSmiHandlers[Index].UnRegister) {
379 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);
380 }
381 }
382
383 //
384 // Install SMM Ready to lock protocol
385 //
386 SmmHandle = NULL;
387 Status = SmmInstallProtocolInterface (
388 &SmmHandle,
389 &gEfiSmmReadyToLockProtocolGuid,
390 EFI_NATIVE_INTERFACE,
391 NULL
392 );
393
394 //
395 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database
396 //
397 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);
398
399 //
400 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed
401 //
402 DEBUG_CODE_BEGIN ();
403 if (EFI_ERROR (Status)) {
404 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
405 }
406 DEBUG_CODE_END ();
407
408 //
409 // Assert if the CPU I/O 2 Protocol is not installed
410 //
411 ASSERT_EFI_ERROR (Status);
412
413 //
414 // Display any drivers that were not dispatched because dependency expression
415 // evaluated to false if this is a debug build
416 //
417 DEBUG_CODE_BEGIN ();
418 SmmDisplayDiscoveredNotDispatched ();
419 DEBUG_CODE_END ();
420
421 //
422 // Not allowed to use gST or gBS after lock
423 //
424 gST = NULL;
425 gBS = NULL;
426
427 SmramProfileReadyToLock ();
428
429 return Status;
430 }
431
432 /**
433 Software SMI handler that is called when the EndOfDxe event is signalled.
434 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that
435 platform code will invoke 3rd part code.
436
437 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
438 @param Context Points to an optional handler context which was specified when the handler was registered.
439 @param CommBuffer A pointer to a collection of data in memory that will
440 be conveyed from a non-SMM environment into an SMM environment.
441 @param CommBufferSize The size of the CommBuffer.
442
443 @return Status Code
444
445 **/
446 EFI_STATUS
447 EFIAPI
448 SmmEndOfDxeHandler (
449 IN EFI_HANDLE DispatchHandle,
450 IN CONST VOID *Context, OPTIONAL
451 IN OUT VOID *CommBuffer, OPTIONAL
452 IN OUT UINTN *CommBufferSize OPTIONAL
453 )
454 {
455 EFI_STATUS Status;
456 EFI_HANDLE SmmHandle;
457
458 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));
459 //
460 // Install SMM EndOfDxe protocol
461 //
462 SmmHandle = NULL;
463 Status = SmmInstallProtocolInterface (
464 &SmmHandle,
465 &gEfiSmmEndOfDxeProtocolGuid,
466 EFI_NATIVE_INTERFACE,
467 NULL
468 );
469 return Status;
470 }
471
472 /**
473 Software SMI handler that is called when the EndOfS3Resume signal is triggered.
474 This function installs the SMM EndOfS3Resume Protocol so SMM Drivers are informed that
475 S3 resume has finished.
476
477 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
478 @param Context Points to an optional handler context which was specified when the handler was registered.
479 @param CommBuffer A pointer to a collection of data in memory that will
480 be conveyed from a non-SMM environment into an SMM environment.
481 @param CommBufferSize The size of the CommBuffer.
482
483 @return Status Code
484
485 **/
486 EFI_STATUS
487 EFIAPI
488 SmmEndOfS3ResumeHandler (
489 IN EFI_HANDLE DispatchHandle,
490 IN CONST VOID *Context, OPTIONAL
491 IN OUT VOID *CommBuffer, OPTIONAL
492 IN OUT UINTN *CommBufferSize OPTIONAL
493 )
494 {
495 EFI_STATUS Status;
496 EFI_HANDLE SmmHandle;
497
498 DEBUG ((DEBUG_INFO, "SmmEndOfS3ResumeHandler\n"));
499
500 if (!mDuringS3Resume) {
501 DEBUG ((DEBUG_ERROR, "It is not during S3 resume\n"));
502 return EFI_SUCCESS;
503 }
504
505 //
506 // Install SMM EndOfS3Resume protocol
507 //
508 SmmHandle = NULL;
509 Status = SmmInstallProtocolInterface (
510 &SmmHandle,
511 &gEdkiiEndOfS3ResumeGuid,
512 EFI_NATIVE_INTERFACE,
513 NULL
514 );
515 ASSERT_EFI_ERROR (Status);
516
517 //
518 // Uninstall the protocol here because the comsumer just hook the
519 // installation event.
520 //
521 Status = SmmUninstallProtocolInterface (
522 SmmHandle,
523 &gEdkiiEndOfS3ResumeGuid,
524 NULL
525 );
526 ASSERT_EFI_ERROR (Status);
527
528 mDuringS3Resume = FALSE;
529
530 return Status;
531 }
532
533 /**
534 Determine if two buffers overlap in memory.
535
536 @param[in] Buff1 Pointer to first buffer
537 @param[in] Size1 Size of Buff1
538 @param[in] Buff2 Pointer to second buffer
539 @param[in] Size2 Size of Buff2
540
541 @retval TRUE Buffers overlap in memory.
542 @retval FALSE Buffer doesn't overlap.
543
544 **/
545 BOOLEAN
546 InternalIsBufferOverlapped (
547 IN UINT8 *Buff1,
548 IN UINTN Size1,
549 IN UINT8 *Buff2,
550 IN UINTN Size2
551 )
552 {
553 //
554 // If buff1's end is less than the start of buff2, then it's ok.
555 // Also, if buff1's start is beyond buff2's end, then it's ok.
556 //
557 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {
558 return FALSE;
559 }
560
561 return TRUE;
562 }
563
564 /**
565 The main entry point to SMM Foundation.
566
567 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
568
569 @param SmmEntryContext Processor information and functionality
570 needed by SMM Foundation.
571
572 **/
573 VOID
574 EFIAPI
575 SmmEntryPoint (
576 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext
577 )
578 {
579 EFI_STATUS Status;
580 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
581 BOOLEAN InLegacyBoot;
582 BOOLEAN IsOverlapped;
583 VOID *CommunicationBuffer;
584 UINTN BufferSize;
585
586 PERF_START (NULL, "SMM", NULL, 0) ;
587
588 //
589 // Update SMST with contents of the SmmEntryContext structure
590 //
591 gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp;
592 gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;
593 gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus;
594 gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize;
595 gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState;
596
597 //
598 // Call platform hook before Smm Dispatch
599 //
600 PlatformHookBeforeSmmDispatch ();
601
602 //
603 // Call memory management hook function
604 //
605 SmmEntryPointMemoryManagementHook ();
606
607 //
608 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
609 //
610 InLegacyBoot = mInLegacyBoot;
611 if (!InLegacyBoot) {
612 //
613 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
614 //
615 gSmmCorePrivate->InSmm = TRUE;
616
617 //
618 // Check to see if this is a Synchronous SMI sent through the SMM Communication
619 // Protocol or an Asynchronous SMI
620 //
621 CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;
622 BufferSize = gSmmCorePrivate->BufferSize;
623 if (CommunicationBuffer != NULL) {
624 //
625 // Synchronous SMI for SMM Core or request from Communicate protocol
626 //
627 IsOverlapped = InternalIsBufferOverlapped (
628 (UINT8 *) CommunicationBuffer,
629 BufferSize,
630 (UINT8 *) gSmmCorePrivate,
631 sizeof (*gSmmCorePrivate)
632 );
633 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) {
634 //
635 // If CommunicationBuffer is not in valid address scope,
636 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
637 // return EFI_INVALID_PARAMETER
638 //
639 gSmmCorePrivate->CommunicationBuffer = NULL;
640 gSmmCorePrivate->ReturnStatus = EFI_ACCESS_DENIED;
641 } else {
642 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;
643 BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
644 Status = SmiManage (
645 &CommunicateHeader->HeaderGuid,
646 NULL,
647 CommunicateHeader->Data,
648 &BufferSize
649 );
650 //
651 // Update CommunicationBuffer, BufferSize and ReturnStatus
652 // Communicate service finished, reset the pointer to CommBuffer to NULL
653 //
654 gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
655 gSmmCorePrivate->CommunicationBuffer = NULL;
656 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
657 }
658 }
659 }
660
661 //
662 // Process Asynchronous SMI sources
663 //
664 SmiManage (NULL, NULL, NULL, NULL);
665
666 //
667 // Call platform hook after Smm Dispatch
668 //
669 PlatformHookAfterSmmDispatch ();
670
671 //
672 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
673 //
674 if (!InLegacyBoot) {
675 //
676 // Clear the InSmm flag as we are going to leave SMM
677 //
678 gSmmCorePrivate->InSmm = FALSE;
679 }
680
681 PERF_END (NULL, "SMM", NULL, 0) ;
682 }
683
684 /**
685 Install LoadedImage protocol for SMM Core.
686 **/
687 VOID
688 SmmCoreInstallLoadedImage (
689 VOID
690 )
691 {
692 EFI_STATUS Status;
693 EFI_HANDLE Handle;
694
695 //
696 // Allocate a Loaded Image Protocol in EfiBootServicesData
697 //
698 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);
699 ASSERT_EFI_ERROR (Status);
700
701 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
702 //
703 // Fill in the remaining fields of the Loaded Image Protocol instance.
704 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
705 //
706 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
707 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
708 mSmmCoreLoadedImage->SystemTable = gST;
709
710 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;
711 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;
712 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;
713 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;
714
715 //
716 // Create a new image handle in the UEFI handle database for the SMM Driver
717 //
718 Handle = NULL;
719 Status = gBS->InstallMultipleProtocolInterfaces (
720 &Handle,
721 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,
722 NULL
723 );
724 ASSERT_EFI_ERROR (Status);
725
726 //
727 // Allocate a Loaded Image Protocol in SMM
728 //
729 Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);
730 ASSERT_EFI_ERROR(Status);
731
732 ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));
733 //
734 // Fill in the remaining fields of the Loaded Image Protocol instance.
735 //
736 mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;
737 mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
738 mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
739 mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;
740
741 mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;
742 mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;
743 mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;
744 mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;
745
746 mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;
747 mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase;
748 mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);
749
750 //
751 // Create a new image handle in the SMM handle database for the SMM Driver
752 //
753 mSmmCoreDriverEntry->SmmImageHandle = NULL;
754 Status = SmmInstallProtocolInterface (
755 &mSmmCoreDriverEntry->SmmImageHandle,
756 &gEfiLoadedImageProtocolGuid,
757 EFI_NATIVE_INTERFACE,
758 &mSmmCoreDriverEntry->SmmLoadedImage
759 );
760 ASSERT_EFI_ERROR(Status);
761
762 return ;
763 }
764
765 /**
766 The Entry Point for SMM Core
767
768 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
769 EntryPoint on the SMI vector.
770
771 Note: This function is called for both DXE invocation and SMRAM invocation.
772
773 @param ImageHandle The firmware allocated handle for the EFI image.
774 @param SystemTable A pointer to the EFI System Table.
775
776 @retval EFI_SUCCESS The entry point is executed successfully.
777 @retval Other Some error occurred when executing this entry point.
778
779 **/
780 EFI_STATUS
781 EFIAPI
782 SmmMain (
783 IN EFI_HANDLE ImageHandle,
784 IN EFI_SYSTEM_TABLE *SystemTable
785 )
786 {
787 EFI_STATUS Status;
788 UINTN Index;
789
790 //
791 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
792 //
793 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
794
795 //
796 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
797 //
798 gSmmCorePrivate->Smst = &gSmmCoreSmst;
799 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
800
801 //
802 // No need to initialize memory service.
803 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
804 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
805 //
806
807 SmramProfileInit ();
808
809 //
810 // Copy FullSmramRanges to SMRAM
811 //
812 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;
813 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
814 ASSERT (mFullSmramRanges != NULL);
815 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
816
817 //
818 // Register all SMI Handlers required by the SMM Core
819 //
820 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
821 Status = SmiHandlerRegister (
822 mSmmCoreSmiHandlers[Index].Handler,
823 mSmmCoreSmiHandlers[Index].HandlerType,
824 &mSmmCoreSmiHandlers[Index].DispatchHandle
825 );
826 ASSERT_EFI_ERROR (Status);
827 }
828
829 RegisterSmramProfileHandler ();
830 SmramProfileInstallProtocol ();
831
832 SmmCoreInstallLoadedImage ();
833
834 SmmCoreInitializeMemoryAttributesTable ();
835
836 SmmCoreInitializeSmiHandlerProfile ();
837
838 return EFI_SUCCESS;
839 }