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