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