d2f0207b5c83b78e39fde30176787131fb24a2f6
[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
300 //
301 // Install SMM Ready To Boot protocol.
302 //
303 SmmHandle = NULL;
304 Status = SmmInstallProtocolInterface (
305 &SmmHandle,
306 &gEdkiiSmmReadyToBootProtocolGuid,
307 EFI_NATIVE_INTERFACE,
308 NULL
309 );
310
311 SmiHandlerUnRegister (DispatchHandle);
312
313 return Status;
314 }
315
316 /**
317 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added
318 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the
319 Software SMIs that are nor required after SMRAM is locked and installs the
320 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about
321 to be locked. It also verifies the SMM CPU I/O 2 Protocol has been installed
322 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.
323
324 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
325 @param Context Points to an optional handler context which was specified when the handler was registered.
326 @param CommBuffer A pointer to a collection of data in memory that will
327 be conveyed from a non-SMM environment into an SMM environment.
328 @param CommBufferSize The size of the CommBuffer.
329
330 @return Status Code
331
332 **/
333 EFI_STATUS
334 EFIAPI
335 SmmReadyToLockHandler (
336 IN EFI_HANDLE DispatchHandle,
337 IN CONST VOID *Context, OPTIONAL
338 IN OUT VOID *CommBuffer, OPTIONAL
339 IN OUT UINTN *CommBufferSize OPTIONAL
340 )
341 {
342 EFI_STATUS Status;
343 UINTN Index;
344 EFI_HANDLE SmmHandle;
345 VOID *Interface;
346
347 //
348 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
349 //
350 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
351 if (mSmmCoreSmiHandlers[Index].UnRegister) {
352 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);
353 }
354 }
355
356 //
357 // Install SMM Ready to lock protocol
358 //
359 SmmHandle = NULL;
360 Status = SmmInstallProtocolInterface (
361 &SmmHandle,
362 &gEfiSmmReadyToLockProtocolGuid,
363 EFI_NATIVE_INTERFACE,
364 NULL
365 );
366
367 //
368 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database
369 //
370 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);
371
372 //
373 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed
374 //
375 DEBUG_CODE_BEGIN ();
376 if (EFI_ERROR (Status)) {
377 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
378 }
379 DEBUG_CODE_END ();
380
381 //
382 // Assert if the CPU I/O 2 Protocol is not installed
383 //
384 ASSERT_EFI_ERROR (Status);
385
386 //
387 // Display any drivers that were not dispatched because dependency expression
388 // evaluated to false if this is a debug build
389 //
390 DEBUG_CODE_BEGIN ();
391 SmmDisplayDiscoveredNotDispatched ();
392 DEBUG_CODE_END ();
393
394 //
395 // Not allowed to use gST or gBS after lock
396 //
397 gST = NULL;
398 gBS = NULL;
399
400 SmramProfileReadyToLock ();
401
402 return Status;
403 }
404
405 /**
406 Software SMI handler that is called when the EndOfDxe event is signalled.
407 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that
408 platform code will invoke 3rd part code.
409
410 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
411 @param Context Points to an optional handler context which was specified when the handler was registered.
412 @param CommBuffer A pointer to a collection of data in memory that will
413 be conveyed from a non-SMM environment into an SMM environment.
414 @param CommBufferSize The size of the CommBuffer.
415
416 @return Status Code
417
418 **/
419 EFI_STATUS
420 EFIAPI
421 SmmEndOfDxeHandler (
422 IN EFI_HANDLE DispatchHandle,
423 IN CONST VOID *Context, OPTIONAL
424 IN OUT VOID *CommBuffer, OPTIONAL
425 IN OUT UINTN *CommBufferSize OPTIONAL
426 )
427 {
428 EFI_STATUS Status;
429 EFI_HANDLE SmmHandle;
430 EFI_SMM_SX_DISPATCH2_PROTOCOL *SxDispatch;
431 EFI_SMM_SX_REGISTER_CONTEXT EntryRegisterContext;
432 EFI_HANDLE S3EntryHandle;
433
434 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));
435
436 //
437 // Install SMM EndOfDxe protocol
438 //
439 SmmHandle = NULL;
440 Status = SmmInstallProtocolInterface (
441 &SmmHandle,
442 &gEfiSmmEndOfDxeProtocolGuid,
443 EFI_NATIVE_INTERFACE,
444 NULL
445 );
446
447 //
448 // Locate SmmSxDispatch2 protocol.
449 //
450 Status = SmmLocateProtocol (
451 &gEfiSmmSxDispatch2ProtocolGuid,
452 NULL,
453 (VOID **)&SxDispatch
454 );
455 if (!EFI_ERROR (Status) && (SxDispatch != NULL)) {
456 //
457 // Register a S3 entry callback function to
458 // determine if it will be during S3 resume.
459 //
460 EntryRegisterContext.Type = SxS3;
461 EntryRegisterContext.Phase = SxEntry;
462 Status = SxDispatch->Register (
463 SxDispatch,
464 SmmS3EntryCallBack,
465 &EntryRegisterContext,
466 &S3EntryHandle
467 );
468 ASSERT_EFI_ERROR (Status);
469 }
470
471 return EFI_SUCCESS;
472 }
473
474 /**
475 Software SMI handler that is called when the EndOfS3Resume signal is triggered.
476 This function installs the SMM EndOfS3Resume Protocol so SMM Drivers are informed that
477 S3 resume has finished.
478
479 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
480 @param Context Points to an optional handler context which was specified when the handler was registered.
481 @param CommBuffer A pointer to a collection of data in memory that will
482 be conveyed from a non-SMM environment into an SMM environment.
483 @param CommBufferSize The size of the CommBuffer.
484
485 @return Status Code
486
487 **/
488 EFI_STATUS
489 EFIAPI
490 SmmEndOfS3ResumeHandler (
491 IN EFI_HANDLE DispatchHandle,
492 IN CONST VOID *Context, OPTIONAL
493 IN OUT VOID *CommBuffer, OPTIONAL
494 IN OUT UINTN *CommBufferSize OPTIONAL
495 )
496 {
497 EFI_STATUS Status;
498 EFI_HANDLE SmmHandle;
499
500 DEBUG ((DEBUG_INFO, "SmmEndOfS3ResumeHandler\n"));
501
502 if (!mDuringS3Resume) {
503 DEBUG ((DEBUG_ERROR, "It is not during S3 resume\n"));
504 return EFI_SUCCESS;
505 }
506
507 //
508 // Install SMM EndOfS3Resume protocol
509 //
510 SmmHandle = NULL;
511 Status = SmmInstallProtocolInterface (
512 &SmmHandle,
513 &gEdkiiEndOfS3ResumeGuid,
514 EFI_NATIVE_INTERFACE,
515 NULL
516 );
517 ASSERT_EFI_ERROR (Status);
518
519 //
520 // Uninstall the protocol here because the comsumer just hook the
521 // installation event.
522 //
523 Status = SmmUninstallProtocolInterface (
524 SmmHandle,
525 &gEdkiiEndOfS3ResumeGuid,
526 NULL
527 );
528 ASSERT_EFI_ERROR (Status);
529
530 mDuringS3Resume = FALSE;
531 return Status;
532 }
533
534 /**
535 Determine if two buffers overlap in memory.
536
537 @param[in] Buff1 Pointer to first buffer
538 @param[in] Size1 Size of Buff1
539 @param[in] Buff2 Pointer to second buffer
540 @param[in] Size2 Size of Buff2
541
542 @retval TRUE Buffers overlap in memory.
543 @retval FALSE Buffer doesn't overlap.
544
545 **/
546 BOOLEAN
547 InternalIsBufferOverlapped (
548 IN UINT8 *Buff1,
549 IN UINTN Size1,
550 IN UINT8 *Buff2,
551 IN UINTN Size2
552 )
553 {
554 //
555 // If buff1's end is less than the start of buff2, then it's ok.
556 // Also, if buff1's start is beyond buff2's end, then it's ok.
557 //
558 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {
559 return FALSE;
560 }
561
562 return TRUE;
563 }
564
565 /**
566 The main entry point to SMM Foundation.
567
568 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
569
570 @param SmmEntryContext Processor information and functionality
571 needed by SMM Foundation.
572
573 **/
574 VOID
575 EFIAPI
576 SmmEntryPoint (
577 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext
578 )
579 {
580 EFI_STATUS Status;
581 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
582 BOOLEAN InLegacyBoot;
583 BOOLEAN IsOverlapped;
584 VOID *CommunicationBuffer;
585 UINTN BufferSize;
586
587 PERF_START (NULL, "SMM", NULL, 0) ;
588
589 //
590 // Update SMST with contents of the SmmEntryContext structure
591 //
592 gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp;
593 gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;
594 gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus;
595 gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize;
596 gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState;
597
598 //
599 // Call platform hook before Smm Dispatch
600 //
601 PlatformHookBeforeSmmDispatch ();
602
603 //
604 // Call memory management hook function
605 //
606 SmmEntryPointMemoryManagementHook ();
607
608 //
609 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
610 //
611 InLegacyBoot = mInLegacyBoot;
612 if (!InLegacyBoot) {
613 //
614 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
615 //
616 gSmmCorePrivate->InSmm = TRUE;
617
618 //
619 // Check to see if this is a Synchronous SMI sent through the SMM Communication
620 // Protocol or an Asynchronous SMI
621 //
622 CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;
623 BufferSize = gSmmCorePrivate->BufferSize;
624 if (CommunicationBuffer != NULL) {
625 //
626 // Synchronous SMI for SMM Core or request from Communicate protocol
627 //
628 IsOverlapped = InternalIsBufferOverlapped (
629 (UINT8 *) CommunicationBuffer,
630 BufferSize,
631 (UINT8 *) gSmmCorePrivate,
632 sizeof (*gSmmCorePrivate)
633 );
634 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) {
635 //
636 // If CommunicationBuffer is not in valid address scope,
637 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
638 // return EFI_INVALID_PARAMETER
639 //
640 gSmmCorePrivate->CommunicationBuffer = NULL;
641 gSmmCorePrivate->ReturnStatus = EFI_ACCESS_DENIED;
642 } else {
643 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;
644 BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
645 Status = SmiManage (
646 &CommunicateHeader->HeaderGuid,
647 NULL,
648 CommunicateHeader->Data,
649 &BufferSize
650 );
651 //
652 // Update CommunicationBuffer, BufferSize and ReturnStatus
653 // Communicate service finished, reset the pointer to CommBuffer to NULL
654 //
655 gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
656 gSmmCorePrivate->CommunicationBuffer = NULL;
657 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
658 }
659 }
660 }
661
662 //
663 // Process Asynchronous SMI sources
664 //
665 SmiManage (NULL, NULL, NULL, NULL);
666
667 //
668 // Call platform hook after Smm Dispatch
669 //
670 PlatformHookAfterSmmDispatch ();
671
672 //
673 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
674 //
675 if (!InLegacyBoot) {
676 //
677 // Clear the InSmm flag as we are going to leave SMM
678 //
679 gSmmCorePrivate->InSmm = FALSE;
680 }
681
682 PERF_END (NULL, "SMM", NULL, 0) ;
683 }
684
685 /**
686 Install LoadedImage protocol for SMM Core.
687 **/
688 VOID
689 SmmCoreInstallLoadedImage (
690 VOID
691 )
692 {
693 EFI_STATUS Status;
694 EFI_HANDLE Handle;
695
696 //
697 // Allocate a Loaded Image Protocol in EfiBootServicesData
698 //
699 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);
700 ASSERT_EFI_ERROR (Status);
701
702 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
703 //
704 // Fill in the remaining fields of the Loaded Image Protocol instance.
705 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
706 //
707 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
708 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
709 mSmmCoreLoadedImage->SystemTable = gST;
710
711 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;
712 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;
713 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;
714 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;
715
716 //
717 // Create a new image handle in the UEFI handle database for the SMM Driver
718 //
719 Handle = NULL;
720 Status = gBS->InstallMultipleProtocolInterfaces (
721 &Handle,
722 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,
723 NULL
724 );
725 ASSERT_EFI_ERROR (Status);
726
727 //
728 // Allocate a Loaded Image Protocol in SMM
729 //
730 Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);
731 ASSERT_EFI_ERROR(Status);
732
733 ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));
734 //
735 // Fill in the remaining fields of the Loaded Image Protocol instance.
736 //
737 mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;
738 mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
739 mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
740 mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;
741
742 mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;
743 mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;
744 mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;
745 mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;
746
747 mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;
748 mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase;
749 mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);
750
751 //
752 // Create a new image handle in the SMM handle database for the SMM Driver
753 //
754 mSmmCoreDriverEntry->SmmImageHandle = NULL;
755 Status = SmmInstallProtocolInterface (
756 &mSmmCoreDriverEntry->SmmImageHandle,
757 &gEfiLoadedImageProtocolGuid,
758 EFI_NATIVE_INTERFACE,
759 &mSmmCoreDriverEntry->SmmLoadedImage
760 );
761 ASSERT_EFI_ERROR(Status);
762
763 return ;
764 }
765
766 /**
767 The Entry Point for SMM Core
768
769 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
770 EntryPoint on the SMI vector.
771
772 Note: This function is called for both DXE invocation and SMRAM invocation.
773
774 @param ImageHandle The firmware allocated handle for the EFI image.
775 @param SystemTable A pointer to the EFI System Table.
776
777 @retval EFI_SUCCESS The entry point is executed successfully.
778 @retval Other Some error occurred when executing this entry point.
779
780 **/
781 EFI_STATUS
782 EFIAPI
783 SmmMain (
784 IN EFI_HANDLE ImageHandle,
785 IN EFI_SYSTEM_TABLE *SystemTable
786 )
787 {
788 EFI_STATUS Status;
789 UINTN Index;
790
791 //
792 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
793 //
794 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
795
796 //
797 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
798 //
799 gSmmCorePrivate->Smst = &gSmmCoreSmst;
800 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
801
802 //
803 // No need to initialize memory service.
804 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
805 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
806 //
807
808 SmramProfileInit ();
809
810 //
811 // Copy FullSmramRanges to SMRAM
812 //
813 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;
814 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
815 ASSERT (mFullSmramRanges != NULL);
816 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
817
818 //
819 // Register all SMI Handlers required by the SMM Core
820 //
821 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
822 Status = SmiHandlerRegister (
823 mSmmCoreSmiHandlers[Index].Handler,
824 mSmmCoreSmiHandlers[Index].HandlerType,
825 &mSmmCoreSmiHandlers[Index].DispatchHandle
826 );
827 ASSERT_EFI_ERROR (Status);
828 }
829
830 RegisterSmramProfileHandler ();
831 SmramProfileInstallProtocol ();
832
833 SmmCoreInstallLoadedImage ();
834
835 SmmCoreInitializeMemoryAttributesTable ();
836
837 SmmCoreInitializeSmiHandlerProfile ();
838
839 return EFI_SUCCESS;
840 }