b833763f9a00ea3dc650f02f2210652afd1ccbd0
[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, &gEdkiiSmmEndOfS3ResumeProtocolGuid, 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 event is trigged.
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 &gEdkiiSmmEndOfS3ResumeProtocolGuid,
421 EFI_NATIVE_INTERFACE,
422 NULL
423 );
424 ASSERT_EFI_ERROR (Status);
425
426 //
427 // Uninstall the protocol here because the comsume just hook the
428 // installation event.
429 //
430 Status = SmmUninstallProtocolInterface (
431 SmmHandle,
432 &gEdkiiSmmEndOfS3ResumeProtocolGuid,
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 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
511 //
512 InLegacyBoot = mInLegacyBoot;
513 if (!InLegacyBoot) {
514 //
515 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
516 //
517 gSmmCorePrivate->InSmm = TRUE;
518
519 //
520 // Check to see if this is a Synchronous SMI sent through the SMM Communication
521 // Protocol or an Asynchronous SMI
522 //
523 CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;
524 BufferSize = gSmmCorePrivate->BufferSize;
525 if (CommunicationBuffer != NULL) {
526 //
527 // Synchronous SMI for SMM Core or request from Communicate protocol
528 //
529 IsOverlapped = InternalIsBufferOverlapped (
530 (UINT8 *) CommunicationBuffer,
531 BufferSize,
532 (UINT8 *) gSmmCorePrivate,
533 sizeof (*gSmmCorePrivate)
534 );
535 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) {
536 //
537 // If CommunicationBuffer is not in valid address scope,
538 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
539 // return EFI_INVALID_PARAMETER
540 //
541 gSmmCorePrivate->CommunicationBuffer = NULL;
542 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
543 } else {
544 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;
545 BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
546 Status = SmiManage (
547 &CommunicateHeader->HeaderGuid,
548 NULL,
549 CommunicateHeader->Data,
550 &BufferSize
551 );
552 //
553 // Update CommunicationBuffer, BufferSize and ReturnStatus
554 // Communicate service finished, reset the pointer to CommBuffer to NULL
555 //
556 gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
557 gSmmCorePrivate->CommunicationBuffer = NULL;
558 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
559 }
560 }
561 }
562
563 //
564 // Process Asynchronous SMI sources
565 //
566 SmiManage (NULL, NULL, NULL, NULL);
567
568 //
569 // Call platform hook after Smm Dispatch
570 //
571 PlatformHookAfterSmmDispatch ();
572
573 //
574 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
575 //
576 if (!InLegacyBoot) {
577 //
578 // Clear the InSmm flag as we are going to leave SMM
579 //
580 gSmmCorePrivate->InSmm = FALSE;
581 }
582
583 PERF_END (NULL, "SMM", NULL, 0) ;
584 }
585
586 /**
587 Install LoadedImage protocol for SMM Core.
588 **/
589 VOID
590 SmmCoreInstallLoadedImage (
591 VOID
592 )
593 {
594 EFI_STATUS Status;
595 EFI_HANDLE Handle;
596
597 //
598 // Allocate a Loaded Image Protocol in EfiBootServicesData
599 //
600 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);
601 ASSERT_EFI_ERROR (Status);
602
603 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
604 //
605 // Fill in the remaining fields of the Loaded Image Protocol instance.
606 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
607 //
608 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
609 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
610 mSmmCoreLoadedImage->SystemTable = gST;
611
612 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;
613 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;
614 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;
615 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;
616
617 //
618 // Create a new image handle in the UEFI handle database for the SMM Driver
619 //
620 Handle = NULL;
621 Status = gBS->InstallMultipleProtocolInterfaces (
622 &Handle,
623 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,
624 NULL
625 );
626 ASSERT_EFI_ERROR (Status);
627
628 //
629 // Allocate a Loaded Image Protocol in SMM
630 //
631 Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);
632 ASSERT_EFI_ERROR(Status);
633
634 ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));
635 //
636 // Fill in the remaining fields of the Loaded Image Protocol instance.
637 //
638 mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;
639 mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
640 mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
641 mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;
642
643 mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;
644 mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;
645 mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;
646 mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;
647
648 mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;
649 mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase;
650 mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);
651
652 //
653 // Create a new image handle in the SMM handle database for the SMM Driver
654 //
655 mSmmCoreDriverEntry->SmmImageHandle = NULL;
656 Status = SmmInstallProtocolInterface (
657 &mSmmCoreDriverEntry->SmmImageHandle,
658 &gEfiLoadedImageProtocolGuid,
659 EFI_NATIVE_INTERFACE,
660 &mSmmCoreDriverEntry->SmmLoadedImage
661 );
662 ASSERT_EFI_ERROR(Status);
663
664 return ;
665 }
666
667 /**
668 The Entry Point for SMM Core
669
670 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
671 EntryPoint on the SMI vector.
672
673 Note: This function is called for both DXE invocation and SMRAM invocation.
674
675 @param ImageHandle The firmware allocated handle for the EFI image.
676 @param SystemTable A pointer to the EFI System Table.
677
678 @retval EFI_SUCCESS The entry point is executed successfully.
679 @retval Other Some error occurred when executing this entry point.
680
681 **/
682 EFI_STATUS
683 EFIAPI
684 SmmMain (
685 IN EFI_HANDLE ImageHandle,
686 IN EFI_SYSTEM_TABLE *SystemTable
687 )
688 {
689 EFI_STATUS Status;
690 UINTN Index;
691
692 //
693 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
694 //
695 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
696
697 //
698 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
699 //
700 gSmmCorePrivate->Smst = &gSmmCoreSmst;
701 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
702
703 //
704 // No need to initialize memory service.
705 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
706 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
707 //
708
709 SmramProfileInit ();
710
711 //
712 // Copy FullSmramRanges to SMRAM
713 //
714 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;
715 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
716 ASSERT (mFullSmramRanges != NULL);
717 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
718
719 //
720 // Register all SMI Handlers required by the SMM Core
721 //
722 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
723 Status = SmiHandlerRegister (
724 mSmmCoreSmiHandlers[Index].Handler,
725 mSmmCoreSmiHandlers[Index].HandlerType,
726 &mSmmCoreSmiHandlers[Index].DispatchHandle
727 );
728 ASSERT_EFI_ERROR (Status);
729 }
730
731 RegisterSmramProfileHandler ();
732 SmramProfileInstallProtocol ();
733
734 SmmCoreInstallLoadedImage ();
735
736 SmmCoreInitializeMemoryAttributesTable ();
737
738 SmmCoreInitializeSmiHandlerProfile ();
739
740 return EFI_SUCCESS;
741 }