]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
20b84eaee2772409d4ea5bc1eb30ea4b525e937b
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmIpl.c
1 /** @file
2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiDxe.h>
10
11 #include <Protocol/SmmBase2.h>
12 #include <Protocol/SmmCommunication.h>
13 #include <Protocol/SmmAccess2.h>
14 #include <Protocol/SmmConfiguration.h>
15 #include <Protocol/SmmControl2.h>
16 #include <Protocol/DxeSmmReadyToLock.h>
17 #include <Protocol/Cpu.h>
18
19 #include <Guid/EventGroup.h>
20 #include <Guid/EventLegacyBios.h>
21 #include <Guid/LoadModuleAtFixedAddress.h>
22
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/PeCoffLib.h>
26 #include <Library/CacheMaintenanceLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/DxeServicesTableLib.h>
31 #include <Library/DxeServicesLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/UefiRuntimeLib.h>
34 #include <Library/PcdLib.h>
35 #include <Library/ReportStatusCodeLib.h>
36
37 #include "PiSmmCorePrivateData.h"
38
39 #define SMRAM_CAPABILITIES (EFI_MEMORY_WB | EFI_MEMORY_UC)
40
41 #define MEMORY_CACHE_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \
42 EFI_MEMORY_WT | EFI_MEMORY_WB | \
43 EFI_MEMORY_WP | EFI_MEMORY_UCE)
44
45 #define MEMORY_PAGE_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | EFI_MEMORY_RO)
46
47 //
48 // Function prototypes from produced protocols
49 //
50
51 /**
52 Indicate whether the driver is currently executing in the SMM Initialization phase.
53
54 @param This The EFI_SMM_BASE2_PROTOCOL instance.
55 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
56 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
57
58 @retval EFI_INVALID_PARAMETER InSmram was NULL.
59 @retval EFI_SUCCESS The call returned successfully.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 SmmBase2InSmram (
65 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
66 OUT BOOLEAN *InSmram
67 );
68
69 /**
70 Retrieves the location of the System Management System Table (SMST).
71
72 @param This The EFI_SMM_BASE2_PROTOCOL instance.
73 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
74
75 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
76 @retval EFI_SUCCESS The memory was returned to the system.
77 @retval EFI_UNSUPPORTED Not in SMM.
78
79 **/
80 EFI_STATUS
81 EFIAPI
82 SmmBase2GetSmstLocation (
83 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
84 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
85 );
86
87 /**
88 Communicates with a registered handler.
89
90 This function provides a service to send and receive messages from a registered
91 UEFI service. This function is part of the SMM Communication Protocol that may
92 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
93 after SetVirtualAddressMap().
94
95 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
96 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
97 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
98 being returned. Zero if the handler does not wish to reply with any data.
99 This parameter is optional and may be NULL.
100
101 @retval EFI_SUCCESS The message was successfully posted.
102 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
103 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
104 If this error is returned, the MessageLength field
105 in the CommBuffer header or the integer pointed by
106 CommSize, are updated to reflect the maximum payload
107 size the implementation can accommodate.
108 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
109 if not omitted, are in address range that cannot be
110 accessed by the MM environment.
111
112 **/
113 EFI_STATUS
114 EFIAPI
115 SmmCommunicationCommunicate (
116 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
117 IN OUT VOID *CommBuffer,
118 IN OUT UINTN *CommSize OPTIONAL
119 );
120
121 /**
122 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
123
124 @param Event The Event that is being processed, not used.
125 @param Context Event Context, not used.
126
127 **/
128 VOID
129 EFIAPI
130 SmmIplSmmConfigurationEventNotify (
131 IN EFI_EVENT Event,
132 IN VOID *Context
133 );
134
135 /**
136 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
137 or if gEfiEventReadyToBootGuid is signalled.
138
139 @param Event The Event that is being processed, not used.
140 @param Context Event Context, not used.
141
142 **/
143 VOID
144 EFIAPI
145 SmmIplReadyToLockEventNotify (
146 IN EFI_EVENT Event,
147 IN VOID *Context
148 );
149
150 /**
151 Event notification that is fired when DxeDispatch Event Group is signaled.
152
153 @param Event The Event that is being processed, not used.
154 @param Context Event Context, not used.
155
156 **/
157 VOID
158 EFIAPI
159 SmmIplDxeDispatchEventNotify (
160 IN EFI_EVENT Event,
161 IN VOID *Context
162 );
163
164 /**
165 Event notification that is fired when a GUIDed Event Group is signaled.
166
167 @param Event The Event that is being processed, not used.
168 @param Context Event Context, not used.
169
170 **/
171 VOID
172 EFIAPI
173 SmmIplGuidedEventNotify (
174 IN EFI_EVENT Event,
175 IN VOID *Context
176 );
177
178 /**
179 Event notification that is fired when EndOfDxe Event Group is signaled.
180
181 @param Event The Event that is being processed, not used.
182 @param Context Event Context, not used.
183
184 **/
185 VOID
186 EFIAPI
187 SmmIplEndOfDxeEventNotify (
188 IN EFI_EVENT Event,
189 IN VOID *Context
190 );
191
192 /**
193 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
194
195 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
196 It convers pointer to new virtual address.
197
198 @param Event Event whose notification function is being invoked.
199 @param Context Pointer to the notification function's context.
200
201 **/
202 VOID
203 EFIAPI
204 SmmIplSetVirtualAddressNotify (
205 IN EFI_EVENT Event,
206 IN VOID *Context
207 );
208
209 //
210 // Data structure used to declare a table of protocol notifications and event
211 // notifications required by the SMM IPL
212 //
213 typedef struct {
214 BOOLEAN Protocol;
215 BOOLEAN CloseOnLock;
216 EFI_GUID *Guid;
217 EFI_EVENT_NOTIFY NotifyFunction;
218 VOID *NotifyContext;
219 EFI_TPL NotifyTpl;
220 EFI_EVENT Event;
221 } SMM_IPL_EVENT_NOTIFICATION;
222
223 //
224 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
225 //
226 EFI_HANDLE mSmmIplHandle = NULL;
227
228 //
229 // SMM Base 2 Protocol instance
230 //
231 EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {
232 SmmBase2InSmram,
233 SmmBase2GetSmstLocation
234 };
235
236 //
237 // SMM Communication Protocol instance
238 //
239 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {
240 SmmCommunicationCommunicate
241 };
242
243 //
244 // SMM Core Private Data structure that contains the data shared between
245 // the SMM IPL and the SMM Core.
246 //
247 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {
248 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature
249 NULL, // SmmIplImageHandle
250 0, // SmramRangeCount
251 NULL, // SmramRanges
252 NULL, // SmmEntryPoint
253 FALSE, // SmmEntryPointRegistered
254 FALSE, // InSmm
255 NULL, // Smst
256 NULL, // CommunicationBuffer
257 0, // BufferSize
258 EFI_SUCCESS // ReturnStatus
259 };
260
261 //
262 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
263 //
264 SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;
265
266 //
267 // SMM IPL global variables
268 //
269 EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;
270 EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;
271 EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;
272 BOOLEAN mSmmLocked = FALSE;
273 BOOLEAN mEndOfDxe = FALSE;
274 EFI_PHYSICAL_ADDRESS mSmramCacheBase;
275 UINT64 mSmramCacheSize;
276
277 EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;
278 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *mLMFAConfigurationTable = NULL;
279
280 //
281 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
282 //
283 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {
284 //
285 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,
286 // the associated event is immediately signalled, so the notification function will be executed and the
287 // SMM Configuration Protocol will be found if it is already in the handle database.
288 //
289 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },
290 //
291 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
292 // the associated event is immediately signalled, so the notification function will be executed and the
293 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
294 //
295 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },
296 //
297 // Declare event notification on EndOfDxe event. When this notification is established,
298 // the associated event is immediately signalled, so the notification function will be executed and the
299 // SMM End Of Dxe Protocol will be found if it is already in the handle database.
300 //
301 { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplGuidedEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },
302 //
303 // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.
304 //
305 { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplEndOfDxeEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },
306 //
307 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
308 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
309 // if notified, so the SMM Core can dispatch SMM drivers.
310 //
311 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },
312 //
313 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
314 // used to make sure SMRAM is locked before any boot options are processed.
315 //
316 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
317 //
318 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
319 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
320 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
321 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
322 //
323 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },
324 //
325 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
326 // to notify SMM driver that system enter exit boot services.
327 //
328 { FALSE, FALSE, &gEfiEventExitBootServicesGuid, SmmIplGuidedEventNotify, &gEfiEventExitBootServicesGuid, TPL_CALLBACK, NULL },
329 //
330 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
331 // to notify SMM driver that system enter ready to boot.
332 //
333 { FALSE, FALSE, &gEfiEventReadyToBootGuid, SmmIplGuidedEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
334 //
335 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
336 // and mSmmControl2 from physical addresses to virtual addresses.
337 //
338 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },
339 //
340 // Terminate the table of event notifications
341 //
342 { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }
343 };
344
345 /**
346 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
347
348 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
349
350 @param SmramRange The SMRAM range to search from.
351 @param SmramCacheBase The returned cache range base.
352 @param SmramCacheSize The returned cache range size.
353
354 **/
355 VOID
356 GetSmramCacheRange (
357 IN EFI_SMRAM_DESCRIPTOR *SmramRange,
358 OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,
359 OUT UINT64 *SmramCacheSize
360 )
361 {
362 UINTN Index;
363 EFI_PHYSICAL_ADDRESS RangeCpuStart;
364 UINT64 RangePhysicalSize;
365 BOOLEAN FoundAjacentRange;
366
367 *SmramCacheBase = SmramRange->CpuStart;
368 *SmramCacheSize = SmramRange->PhysicalSize;
369
370 do {
371 FoundAjacentRange = FALSE;
372 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
373 RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;
374 RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
375 if (RangeCpuStart < *SmramCacheBase && *SmramCacheBase == (RangeCpuStart + RangePhysicalSize)) {
376 *SmramCacheBase = RangeCpuStart;
377 *SmramCacheSize += RangePhysicalSize;
378 FoundAjacentRange = TRUE;
379 } else if ((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart && RangePhysicalSize > 0) {
380 *SmramCacheSize += RangePhysicalSize;
381 FoundAjacentRange = TRUE;
382 }
383 }
384 } while (FoundAjacentRange);
385
386 }
387
388 /**
389 Indicate whether the driver is currently executing in the SMM Initialization phase.
390
391 @param This The EFI_SMM_BASE2_PROTOCOL instance.
392 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
393 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
394
395 @retval EFI_INVALID_PARAMETER InSmram was NULL.
396 @retval EFI_SUCCESS The call returned successfully.
397
398 **/
399 EFI_STATUS
400 EFIAPI
401 SmmBase2InSmram (
402 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
403 OUT BOOLEAN *InSmram
404 )
405 {
406 if (InSmram == NULL) {
407 return EFI_INVALID_PARAMETER;
408 }
409
410 *InSmram = gSmmCorePrivate->InSmm;
411
412 return EFI_SUCCESS;
413 }
414
415 /**
416 Retrieves the location of the System Management System Table (SMST).
417
418 @param This The EFI_SMM_BASE2_PROTOCOL instance.
419 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
420
421 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
422 @retval EFI_SUCCESS The memory was returned to the system.
423 @retval EFI_UNSUPPORTED Not in SMM.
424
425 **/
426 EFI_STATUS
427 EFIAPI
428 SmmBase2GetSmstLocation (
429 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
430 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
431 )
432 {
433 if ((This == NULL) ||(Smst == NULL)) {
434 return EFI_INVALID_PARAMETER;
435 }
436
437 if (!gSmmCorePrivate->InSmm) {
438 return EFI_UNSUPPORTED;
439 }
440
441 *Smst = gSmmCorePrivate->Smst;
442
443 return EFI_SUCCESS;
444 }
445
446 /**
447 Communicates with a registered handler.
448
449 This function provides a service to send and receive messages from a registered
450 UEFI service. This function is part of the SMM Communication Protocol that may
451 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
452 after SetVirtualAddressMap().
453
454 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
455 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
456 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
457 being returned. Zero if the handler does not wish to reply with any data.
458 This parameter is optional and may be NULL.
459
460 @retval EFI_SUCCESS The message was successfully posted.
461 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
462 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
463 If this error is returned, the MessageLength field
464 in the CommBuffer header or the integer pointed by
465 CommSize, are updated to reflect the maximum payload
466 size the implementation can accommodate.
467 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
468 if not omitted, are in address range that cannot be
469 accessed by the MM environment.
470
471 **/
472 EFI_STATUS
473 EFIAPI
474 SmmCommunicationCommunicate (
475 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
476 IN OUT VOID *CommBuffer,
477 IN OUT UINTN *CommSize OPTIONAL
478 )
479 {
480 EFI_STATUS Status;
481 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
482 BOOLEAN OldInSmm;
483 UINTN TempCommSize;
484
485 //
486 // Check parameters
487 //
488 if (CommBuffer == NULL) {
489 return EFI_INVALID_PARAMETER;
490 }
491
492 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;
493
494 if (CommSize == NULL) {
495 TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;
496 } else {
497 TempCommSize = *CommSize;
498 //
499 // CommSize must hold HeaderGuid and MessageLength
500 //
501 if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {
502 return EFI_INVALID_PARAMETER;
503 }
504 }
505
506 //
507 // If not already in SMM, then generate a Software SMI
508 //
509 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {
510 //
511 // Put arguments for Software SMI in gSmmCorePrivate
512 //
513 gSmmCorePrivate->CommunicationBuffer = CommBuffer;
514 gSmmCorePrivate->BufferSize = TempCommSize;
515
516 //
517 // Generate Software SMI
518 //
519 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);
520 if (EFI_ERROR (Status)) {
521 return EFI_UNSUPPORTED;
522 }
523
524 //
525 // Return status from software SMI
526 //
527 if (CommSize != NULL) {
528 *CommSize = gSmmCorePrivate->BufferSize;
529 }
530 return gSmmCorePrivate->ReturnStatus;
531 }
532
533 //
534 // If we are in SMM, then the execution mode must be physical, which means that
535 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
536 // has been called, then a direct invocation of the Software SMI is not allowed,
537 // so return EFI_INVALID_PARAMETER.
538 //
539 if (EfiGoneVirtual()) {
540 return EFI_INVALID_PARAMETER;
541 }
542
543 //
544 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
545 //
546 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {
547 return EFI_INVALID_PARAMETER;
548 }
549
550 //
551 // Save current InSmm state and set InSmm state to TRUE
552 //
553 OldInSmm = gSmmCorePrivate->InSmm;
554 gSmmCorePrivate->InSmm = TRUE;
555
556 //
557 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.
558 //
559 TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
560 Status = gSmmCorePrivate->Smst->SmiManage (
561 &CommunicateHeader->HeaderGuid,
562 NULL,
563 CommunicateHeader->Data,
564 &TempCommSize
565 );
566 TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
567 if (CommSize != NULL) {
568 *CommSize = TempCommSize;
569 }
570
571 //
572 // Restore original InSmm state
573 //
574 gSmmCorePrivate->InSmm = OldInSmm;
575
576 return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
577 }
578
579 /**
580 Event notification that is fired when GUIDed Event Group is signaled.
581
582 @param Event The Event that is being processed, not used.
583 @param Context Event Context, not used.
584
585 **/
586 VOID
587 EFIAPI
588 SmmIplGuidedEventNotify (
589 IN EFI_EVENT Event,
590 IN VOID *Context
591 )
592 {
593 UINTN Size;
594
595 //
596 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
597 //
598 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
599 mCommunicateHeader.MessageLength = 1;
600 mCommunicateHeader.Data[0] = 0;
601
602 //
603 // Generate the Software SMI and return the result
604 //
605 Size = sizeof (mCommunicateHeader);
606 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
607 }
608
609 /**
610 Event notification that is fired when EndOfDxe Event Group is signaled.
611
612 @param Event The Event that is being processed, not used.
613 @param Context Event Context, not used.
614
615 **/
616 VOID
617 EFIAPI
618 SmmIplEndOfDxeEventNotify (
619 IN EFI_EVENT Event,
620 IN VOID *Context
621 )
622 {
623 mEndOfDxe = TRUE;
624 }
625
626 /**
627 Event notification that is fired when DxeDispatch Event Group is signaled.
628
629 @param Event The Event that is being processed, not used.
630 @param Context Event Context, not used.
631
632 **/
633 VOID
634 EFIAPI
635 SmmIplDxeDispatchEventNotify (
636 IN EFI_EVENT Event,
637 IN VOID *Context
638 )
639 {
640 UINTN Size;
641 EFI_STATUS Status;
642
643 //
644 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
645 //
646 while (TRUE) {
647 //
648 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
649 // Clear the buffer passed into the Software SMI. This buffer will return
650 // the status of the SMM Core Dispatcher.
651 //
652 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
653 mCommunicateHeader.MessageLength = 1;
654 mCommunicateHeader.Data[0] = 0;
655
656 //
657 // Generate the Software SMI and return the result
658 //
659 Size = sizeof (mCommunicateHeader);
660 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
661
662 //
663 // Return if there is no request to restart the SMM Core Dispatcher
664 //
665 if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {
666 return;
667 }
668
669 //
670 // Close all SMRAM ranges to protect SMRAM
671 // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib
672 // so no need to reset the SMRAM to UC in MTRR.
673 //
674 Status = mSmmAccess->Close (mSmmAccess);
675 ASSERT_EFI_ERROR (Status);
676
677 //
678 // Print debug message that the SMRAM window is now closed.
679 //
680 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
681 }
682 }
683
684 /**
685 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
686
687 @param Event The Event that is being processed, not used.
688 @param Context Event Context, not used.
689
690 **/
691 VOID
692 EFIAPI
693 SmmIplSmmConfigurationEventNotify (
694 IN EFI_EVENT Event,
695 IN VOID *Context
696 )
697 {
698 EFI_STATUS Status;
699 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
700
701 //
702 // Make sure this notification is for this handler
703 //
704 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
705 if (EFI_ERROR (Status)) {
706 return;
707 }
708
709 //
710 // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol
711 //
712 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
713 ASSERT_EFI_ERROR (Status);
714
715 //
716 // Set flag to indicate that the SMM Entry Point has been registered which
717 // means that SMIs are now fully operational.
718 //
719 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
720
721 //
722 // Print debug message showing SMM Core entry point address.
723 //
724 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
725 }
726
727 /**
728 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
729 or if gEfiEventReadyToBootGuid is signaled.
730
731 @param Event The Event that is being processed, not used.
732 @param Context Event Context, not used.
733
734 **/
735 VOID
736 EFIAPI
737 SmmIplReadyToLockEventNotify (
738 IN EFI_EVENT Event,
739 IN VOID *Context
740 )
741 {
742 EFI_STATUS Status;
743 VOID *Interface;
744 UINTN Index;
745
746 //
747 // See if we are already locked
748 //
749 if (mSmmLocked) {
750 return;
751 }
752
753 //
754 // Make sure this notification is for this handler
755 //
756 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
757 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
758 if (EFI_ERROR (Status)) {
759 return;
760 }
761 } else {
762 //
763 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
764 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
765 // Print a warning on debug builds.
766 //
767 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
768 }
769
770 if (!mEndOfDxe) {
771 DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
772 REPORT_STATUS_CODE (
773 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
774 (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
775 );
776 ASSERT (FALSE);
777 }
778
779 //
780 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
781 //
782 mSmmAccess->Lock (mSmmAccess);
783
784 //
785 // Close protocol and event notification events that do not apply after the
786 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
787 // event has been signalled.
788 //
789 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
790 if (mSmmIplEvents[Index].CloseOnLock) {
791 gBS->CloseEvent (mSmmIplEvents[Index].Event);
792 }
793 }
794
795 //
796 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
797 //
798 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
799
800 //
801 // Print debug message that the SMRAM window is now locked.
802 //
803 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
804
805 //
806 // Set flag so this operation will not be performed again
807 //
808 mSmmLocked = TRUE;
809 }
810
811 /**
812 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
813
814 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
815 It convers pointer to new virtual address.
816
817 @param Event Event whose notification function is being invoked.
818 @param Context Pointer to the notification function's context.
819
820 **/
821 VOID
822 EFIAPI
823 SmmIplSetVirtualAddressNotify (
824 IN EFI_EVENT Event,
825 IN VOID *Context
826 )
827 {
828 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
829 }
830
831 /**
832 Get the fixed loading address from image header assigned by build tool. This function only be called
833 when Loading module at Fixed address feature enabled.
834
835 @param ImageContext Pointer to the image context structure that describes the PE/COFF
836 image that needs to be examined by this function.
837 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
838 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
839 **/
840 EFI_STATUS
841 GetPeCoffImageFixLoadingAssignedAddress(
842 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
843 )
844 {
845 UINTN SectionHeaderOffset;
846 EFI_STATUS Status;
847 EFI_IMAGE_SECTION_HEADER SectionHeader;
848 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
849 EFI_PHYSICAL_ADDRESS FixLoadingAddress;
850 UINT16 Index;
851 UINTN Size;
852 UINT16 NumberOfSections;
853 EFI_PHYSICAL_ADDRESS SmramBase;
854 UINT64 SmmCodeSize;
855 UINT64 ValueInSectionHeader;
856 //
857 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
858 //
859 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));
860
861 FixLoadingAddress = 0;
862 Status = EFI_NOT_FOUND;
863 SmramBase = mLMFAConfigurationTable->SmramBase;
864 //
865 // Get PeHeader pointer
866 //
867 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
868 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
869 sizeof (UINT32) +
870 sizeof (EFI_IMAGE_FILE_HEADER) +
871 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
872 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
873
874 //
875 // Get base address from the first section header that doesn't point to code section.
876 //
877 for (Index = 0; Index < NumberOfSections; Index++) {
878 //
879 // Read section header from file
880 //
881 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
882 Status = ImageContext->ImageRead (
883 ImageContext->Handle,
884 SectionHeaderOffset,
885 &Size,
886 &SectionHeader
887 );
888 if (EFI_ERROR (Status)) {
889 return Status;
890 }
891
892 Status = EFI_NOT_FOUND;
893
894 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
895 //
896 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
897 // first section header that doesn't point to code section in image header. And there is an assumption that when the
898 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
899 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
900 //
901 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
902 if (ValueInSectionHeader != 0) {
903 //
904 // Found first section header that doesn't point to code section in which build tool saves the
905 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
906 //
907 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);
908
909 if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <= FixLoadingAddress) {
910 //
911 // The assigned address is valid. Return the specified loading address
912 //
913 ImageContext->ImageAddress = FixLoadingAddress;
914 Status = EFI_SUCCESS;
915 }
916 }
917 break;
918 }
919 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
920 }
921 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));
922 return Status;
923 }
924 /**
925 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
926
927 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
928 currently executing image, the rang of SMRAM to
929 hold SMM Core will be excluded.
930 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
931
932 @param[in] Context Context to pass into SMM Core
933
934 @return EFI_STATUS
935
936 **/
937 EFI_STATUS
938 ExecuteSmmCoreFromSmram (
939 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,
940 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,
941 IN VOID *Context
942 )
943 {
944 EFI_STATUS Status;
945 VOID *SourceBuffer;
946 UINTN SourceSize;
947 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
948 UINTN PageCount;
949 EFI_IMAGE_ENTRY_POINT EntryPoint;
950
951 //
952 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
953 //
954 Status = GetSectionFromAnyFvByFileType (
955 EFI_FV_FILETYPE_SMM_CORE,
956 0,
957 EFI_SECTION_PE32,
958 0,
959 &SourceBuffer,
960 &SourceSize
961 );
962 if (EFI_ERROR (Status)) {
963 return Status;
964 }
965
966 //
967 // Initialize ImageContext
968 //
969 ImageContext.Handle = SourceBuffer;
970 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
971
972 //
973 // Get information about the image being loaded
974 //
975 Status = PeCoffLoaderGetImageInfo (&ImageContext);
976 if (EFI_ERROR (Status)) {
977 return Status;
978 }
979 //
980 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
981 // the address assigned by build tool.
982 //
983 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
984 //
985 // Get the fixed loading address assigned by Build tool
986 //
987 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
988 if (!EFI_ERROR (Status)) {
989 //
990 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
991 //
992 PageCount = 0;
993 //
994 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
995 //
996 gSmmCorePrivate->SmramRangeCount --;
997 } else {
998 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
999 //
1000 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1001 // specified by SmramRange
1002 //
1003 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
1004
1005 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
1006 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
1007
1008 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
1009 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
1010 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
1011 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
1012 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
1013
1014 //
1015 // Align buffer on section boundary
1016 //
1017 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1018 }
1019 } else {
1020 //
1021 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1022 // specified by SmramRange
1023 //
1024 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
1025
1026 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
1027 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
1028
1029 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
1030 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
1031 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
1032 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
1033 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
1034
1035 //
1036 // Align buffer on section boundary
1037 //
1038 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1039 }
1040
1041 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
1042 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);
1043
1044 //
1045 // Print debug message showing SMM Core load address.
1046 //
1047 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
1048
1049 //
1050 // Load the image to our new buffer
1051 //
1052 Status = PeCoffLoaderLoadImage (&ImageContext);
1053 if (!EFI_ERROR (Status)) {
1054 //
1055 // Relocate the image in our new buffer
1056 //
1057 Status = PeCoffLoaderRelocateImage (&ImageContext);
1058 if (!EFI_ERROR (Status)) {
1059 //
1060 // Flush the instruction cache so the image data are written before we execute it
1061 //
1062 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
1063
1064 //
1065 // Print debug message showing SMM Core entry point address.
1066 //
1067 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
1068
1069 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;
1070 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;
1071 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));
1072 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));
1073
1074 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;
1075
1076 //
1077 // Execute image
1078 //
1079 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
1080 Status = EntryPoint ((EFI_HANDLE)Context, gST);
1081 }
1082 }
1083
1084 //
1085 // Always free memory allocated by GetFileBufferByFilePath ()
1086 //
1087 FreePool (SourceBuffer);
1088
1089 return Status;
1090 }
1091
1092 /**
1093 SMM split SMRAM entry.
1094
1095 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1096 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1097 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1098 @param[in, out] RangeCount Pointer to range count.
1099 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1100 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1101 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1102 that no need to be split anymore.
1103 @param[in, out] FinalRangeCount Pointer to final range count.
1104
1105 **/
1106 VOID
1107 SmmSplitSmramEntry (
1108 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1109 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,
1110 OUT EFI_SMRAM_DESCRIPTOR *Ranges,
1111 IN OUT UINTN *RangeCount,
1112 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,
1113 IN OUT UINTN *ReservedRangeCount,
1114 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,
1115 IN OUT UINTN *FinalRangeCount
1116 )
1117 {
1118 UINT64 RangeToCompareEnd;
1119 UINT64 ReservedRangeToCompareEnd;
1120
1121 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
1122 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
1123
1124 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1125 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {
1126 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {
1127 //
1128 // RangeToCompare ReservedRangeToCompare
1129 // ---- ---- --------------------------------------
1130 // | | | | -> 1. ReservedRangeToCompare
1131 // ---- | | |--| --------------------------------------
1132 // | | | | | |
1133 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1134 // | | | | | | RangeToCompare->PhysicalSize = 0
1135 // ---- | | |--| --------------------------------------
1136 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1137 // ---- ---- --------------------------------------
1138 //
1139
1140 //
1141 // 1. Update ReservedRangeToCompare.
1142 //
1143 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1144 //
1145 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1146 // Zero RangeToCompare->PhysicalSize.
1147 //
1148 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1149 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1150 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1151 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;
1152 *FinalRangeCount += 1;
1153 RangeToCompare->PhysicalSize = 0;
1154 //
1155 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1156 //
1157 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1158 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;
1159 *ReservedRangeCount += 1;
1160 } else {
1161 //
1162 // RangeToCompare ReservedRangeToCompare
1163 // ---- ---- --------------------------------------
1164 // | | | | -> 1. ReservedRangeToCompare
1165 // ---- | | |--| --------------------------------------
1166 // | | | | | |
1167 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1168 // | | | | | |
1169 // | | ---- |--| --------------------------------------
1170 // | | | | -> 3. RangeToCompare
1171 // ---- ---- --------------------------------------
1172 //
1173
1174 //
1175 // 1. Update ReservedRangeToCompare.
1176 //
1177 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1178 //
1179 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1180 //
1181 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1182 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1183 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1184 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;
1185 *FinalRangeCount += 1;
1186 //
1187 // 3. Update RangeToCompare.
1188 //
1189 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1190 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1191 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1192 }
1193 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1194 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {
1195 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {
1196 //
1197 // RangeToCompare ReservedRangeToCompare
1198 // ---- ---- --------------------------------------
1199 // | | | | -> 1. RangeToCompare
1200 // | | ---- |--| --------------------------------------
1201 // | | | | | |
1202 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1203 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1204 // | | ---- |--| --------------------------------------
1205 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1206 // ---- ---- --------------------------------------
1207 //
1208
1209 //
1210 // 1. Update RangeToCompare.
1211 //
1212 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1213 //
1214 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1215 // ReservedRangeToCompare->SmramReservedSize = 0
1216 //
1217 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1218 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1219 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1220 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;
1221 *FinalRangeCount += 1;
1222 ReservedRangeToCompare->SmramReservedSize = 0;
1223 //
1224 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1225 //
1226 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1227 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1228 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;
1229 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;
1230 *RangeCount += 1;
1231 } else {
1232 //
1233 // RangeToCompare ReservedRangeToCompare
1234 // ---- ---- --------------------------------------
1235 // | | | | -> 1. RangeToCompare
1236 // | | ---- |--| --------------------------------------
1237 // | | | | | |
1238 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1239 // | | | | | |
1240 // ---- | | |--| --------------------------------------
1241 // | | | | -> 3. ReservedRangeToCompare
1242 // ---- ---- --------------------------------------
1243 //
1244
1245 //
1246 // 1. Update RangeToCompare.
1247 //
1248 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1249 //
1250 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1251 // ReservedRangeToCompare->SmramReservedSize = 0
1252 //
1253 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1254 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1255 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1256 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;
1257 *FinalRangeCount += 1;
1258 //
1259 // 3. Update ReservedRangeToCompare.
1260 //
1261 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1262 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1263 }
1264 }
1265 }
1266
1267 /**
1268 Returns if SMRAM range and SMRAM reserved range are overlapped.
1269
1270 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1271 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1272
1273 @retval TRUE There is overlap.
1274 @retval FALSE There is no overlap.
1275
1276 **/
1277 BOOLEAN
1278 SmmIsSmramOverlap (
1279 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1280 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare
1281 )
1282 {
1283 UINT64 RangeToCompareEnd;
1284 UINT64 ReservedRangeToCompareEnd;
1285
1286 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
1287 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
1288
1289 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1290 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {
1291 return TRUE;
1292 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1293 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {
1294 return TRUE;
1295 }
1296 return FALSE;
1297 }
1298
1299 /**
1300 Get full SMRAM ranges.
1301
1302 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1303 SmmConfiguration protocol, split the entries if there is overlap between them.
1304 It will also reserve one entry for SMM core.
1305
1306 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1307
1308 @return Pointer to full SMRAM ranges.
1309
1310 **/
1311 EFI_SMRAM_DESCRIPTOR *
1312 GetFullSmramRanges (
1313 OUT UINTN *FullSmramRangeCount
1314 )
1315 {
1316 EFI_STATUS Status;
1317 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
1318 UINTN Size;
1319 UINTN Index;
1320 UINTN Index2;
1321 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;
1322 UINTN TempSmramRangeCount;
1323 UINTN AdditionSmramRangeCount;
1324 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;
1325 UINTN SmramRangeCount;
1326 EFI_SMRAM_DESCRIPTOR *SmramRanges;
1327 UINTN SmramReservedCount;
1328 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;
1329 UINTN MaxCount;
1330 BOOLEAN Rescan;
1331
1332 //
1333 // Get SMM Configuration Protocol if it is present.
1334 //
1335 SmmConfiguration = NULL;
1336 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);
1337
1338 //
1339 // Get SMRAM information.
1340 //
1341 Size = 0;
1342 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
1343 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
1344
1345 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
1346
1347 //
1348 // Get SMRAM reserved region count.
1349 //
1350 SmramReservedCount = 0;
1351 if (SmmConfiguration != NULL) {
1352 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {
1353 SmramReservedCount++;
1354 }
1355 }
1356
1357 //
1358 // Reserve one entry for SMM Core in the full SMRAM ranges.
1359 //
1360 AdditionSmramRangeCount = 1;
1361 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
1362 //
1363 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1364 //
1365 AdditionSmramRangeCount = 2;
1366 }
1367
1368 if (SmramReservedCount == 0) {
1369 //
1370 // No reserved SMRAM entry from SMM Configuration Protocol.
1371 //
1372 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;
1373 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);
1374 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);
1375 ASSERT (FullSmramRanges != NULL);
1376
1377 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);
1378 ASSERT_EFI_ERROR (Status);
1379
1380 return FullSmramRanges;
1381 }
1382
1383 //
1384 // Why MaxCount = X + 2 * Y?
1385 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1386 //
1387 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1388 // ---- ----
1389 // | | ---- |--|
1390 // | | | | -> | |
1391 // | | ---- |--|
1392 // ---- ----
1393 //
1394 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1395 // ---- ----
1396 // | | | |
1397 // | | ---- |--|
1398 // | | | | | |
1399 // |--| | | -> |--|
1400 // | | | | | |
1401 // | | ---- |--|
1402 // | | | |
1403 // ---- ----
1404 //
1405 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1406 // ---- ----
1407 // | | | |
1408 // | | ---- |--|
1409 // |--| | | |--|
1410 // | | | | -> | |
1411 // |--| | | |--|
1412 // | | ---- |--|
1413 // | | | |
1414 // ---- ----
1415 //
1416 // ......
1417 //
1418 MaxCount = SmramRangeCount + 2 * SmramReservedCount;
1419
1420 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);
1421 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *) AllocatePool (Size);
1422 ASSERT (SmramReservedRanges != NULL);
1423 for (Index = 0; Index < SmramReservedCount; Index++) {
1424 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));
1425 }
1426
1427 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);
1428 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);
1429 ASSERT (TempSmramRanges != NULL);
1430 TempSmramRangeCount = 0;
1431
1432 SmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);
1433 ASSERT (SmramRanges != NULL);
1434 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);
1435 ASSERT_EFI_ERROR (Status);
1436
1437 do {
1438 Rescan = FALSE;
1439 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {
1440 //
1441 // Skip zero size entry.
1442 //
1443 if (SmramRanges[Index].PhysicalSize != 0) {
1444 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {
1445 //
1446 // Skip zero size entry.
1447 //
1448 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {
1449 if (SmmIsSmramOverlap (
1450 &SmramRanges[Index],
1451 &SmramReservedRanges[Index2]
1452 )) {
1453 //
1454 // There is overlap, need to split entry and then rescan.
1455 //
1456 SmmSplitSmramEntry (
1457 &SmramRanges[Index],
1458 &SmramReservedRanges[Index2],
1459 SmramRanges,
1460 &SmramRangeCount,
1461 SmramReservedRanges,
1462 &SmramReservedCount,
1463 TempSmramRanges,
1464 &TempSmramRangeCount
1465 );
1466 Rescan = TRUE;
1467 }
1468 }
1469 }
1470 if (!Rescan) {
1471 //
1472 // No any overlap, copy the entry to the temp SMRAM ranges.
1473 // Zero SmramRanges[Index].PhysicalSize = 0;
1474 //
1475 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1476 SmramRanges[Index].PhysicalSize = 0;
1477 }
1478 }
1479 }
1480 } while (Rescan);
1481 ASSERT (TempSmramRangeCount <= MaxCount);
1482
1483 //
1484 // Sort the entries
1485 //
1486 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));
1487 ASSERT (FullSmramRanges != NULL);
1488 *FullSmramRangeCount = 0;
1489 do {
1490 for (Index = 0; Index < TempSmramRangeCount; Index++) {
1491 if (TempSmramRanges[Index].PhysicalSize != 0) {
1492 break;
1493 }
1494 }
1495 ASSERT (Index < TempSmramRangeCount);
1496 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {
1497 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {
1498 Index = Index2;
1499 }
1500 }
1501 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1502 *FullSmramRangeCount += 1;
1503 TempSmramRanges[Index].PhysicalSize = 0;
1504 } while (*FullSmramRangeCount < TempSmramRangeCount);
1505 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);
1506 *FullSmramRangeCount += AdditionSmramRangeCount;
1507
1508 FreePool (SmramRanges);
1509 FreePool (SmramReservedRanges);
1510 FreePool (TempSmramRanges);
1511
1512 return FullSmramRanges;
1513 }
1514
1515 /**
1516 The Entry Point for SMM IPL
1517
1518 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1519 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1520 critical events required to coordinate between DXE and SMM environments.
1521
1522 @param ImageHandle The firmware allocated handle for the EFI image.
1523 @param SystemTable A pointer to the EFI System Table.
1524
1525 @retval EFI_SUCCESS The entry point is executed successfully.
1526 @retval Other Some error occurred when executing this entry point.
1527
1528 **/
1529 EFI_STATUS
1530 EFIAPI
1531 SmmIplEntry (
1532 IN EFI_HANDLE ImageHandle,
1533 IN EFI_SYSTEM_TABLE *SystemTable
1534 )
1535 {
1536 EFI_STATUS Status;
1537 UINTN Index;
1538 UINT64 MaxSize;
1539 VOID *Registration;
1540 UINT64 SmmCodeSize;
1541 EFI_CPU_ARCH_PROTOCOL *CpuArch;
1542 EFI_STATUS SetAttrStatus;
1543 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;
1544 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;
1545
1546 //
1547 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1548 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1549 // by the SMM Core
1550 //
1551 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
1552
1553 //
1554 // Get SMM Access Protocol
1555 //
1556 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
1557 ASSERT_EFI_ERROR (Status);
1558
1559 //
1560 // Get SMM Control2 Protocol
1561 //
1562 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
1563 ASSERT_EFI_ERROR (Status);
1564
1565 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);
1566
1567 //
1568 // Open all SMRAM ranges
1569 //
1570 Status = mSmmAccess->Open (mSmmAccess);
1571 ASSERT_EFI_ERROR (Status);
1572
1573 //
1574 // Print debug message that the SMRAM window is now open.
1575 //
1576 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
1577
1578 //
1579 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1580 //
1581 mCurrentSmramRange = NULL;
1582 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
1583 //
1584 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1585 //
1586 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
1587 continue;
1588 }
1589
1590 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
1591 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {
1592 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
1593 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
1594 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
1595 }
1596 }
1597 }
1598 }
1599
1600 if (mCurrentSmramRange != NULL) {
1601 //
1602 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1603 //
1604 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",
1605 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
1606 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
1607 ));
1608
1609 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);
1610 //
1611 // Make sure we can change the desired memory attributes.
1612 //
1613 Status = gDS->GetMemorySpaceDescriptor (
1614 mSmramCacheBase,
1615 &MemDesc
1616 );
1617 ASSERT_EFI_ERROR (Status);
1618 if ((MemDesc.Capabilities & SMRAM_CAPABILITIES) != SMRAM_CAPABILITIES) {
1619 gDS->SetMemorySpaceCapabilities (
1620 mSmramCacheBase,
1621 mSmramCacheSize,
1622 MemDesc.Capabilities | SMRAM_CAPABILITIES
1623 );
1624 }
1625 //
1626 // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear
1627 // all paging attributes.
1628 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1629 // is not available here.
1630 //
1631 CpuArch = NULL;
1632 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);
1633 if (!EFI_ERROR (Status)) {
1634 MemDesc.Attributes &= ~(MEMORY_CACHE_ATTRIBUTES | MEMORY_PAGE_ATTRIBUTES);
1635 MemDesc.Attributes |= EFI_MEMORY_WB;
1636 Status = gDS->SetMemorySpaceAttributes (
1637 mSmramCacheBase,
1638 mSmramCacheSize,
1639 MemDesc.Attributes
1640 );
1641 if (EFI_ERROR (Status)) {
1642 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1643 }
1644
1645 DEBUG_CODE (
1646 gDS->GetMemorySpaceDescriptor (
1647 mSmramCacheBase,
1648 &MemDesc
1649 );
1650 DEBUG ((DEBUG_INFO, "SMRAM attributes: %016lx\n", MemDesc.Attributes));
1651 ASSERT ((MemDesc.Attributes & MEMORY_PAGE_ATTRIBUTES) == 0);
1652 );
1653 }
1654 //
1655 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1656 // Modules At Fixed Address Configuration Table.
1657 //
1658 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
1659 //
1660 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1661 //
1662 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);
1663 //
1664 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1665 //
1666 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);
1667 //
1668 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1669 //
1670 Status = EfiGetSystemConfigurationTable (
1671 &gLoadFixedAddressConfigurationTableGuid,
1672 (VOID **) &mLMFAConfigurationTable
1673 );
1674 if (!EFI_ERROR (Status) && mLMFAConfigurationTable != NULL) {
1675 mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;
1676 //
1677 // Print the SMRAM base
1678 //
1679 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));
1680 }
1681
1682 //
1683 // Fill the Smram range for all SMM code
1684 //
1685 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];
1686 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;
1687 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;
1688 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;
1689 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;
1690
1691 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;
1692 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;
1693 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;
1694 }
1695 //
1696 // Load SMM Core into SMRAM and execute it from SMRAM
1697 //
1698 Status = ExecuteSmmCoreFromSmram (
1699 mCurrentSmramRange,
1700 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],
1701 gSmmCorePrivate
1702 );
1703 if (EFI_ERROR (Status)) {
1704 //
1705 // Print error message that the SMM Core failed to be loaded and executed.
1706 //
1707 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1708
1709 //
1710 // Attempt to reset SMRAM cacheability to UC
1711 //
1712 if (CpuArch != NULL) {
1713 SetAttrStatus = gDS->SetMemorySpaceAttributes(
1714 mSmramCacheBase,
1715 mSmramCacheSize,
1716 EFI_MEMORY_UC
1717 );
1718 if (EFI_ERROR (SetAttrStatus)) {
1719 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1720 }
1721 }
1722 }
1723 } else {
1724 //
1725 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1726 //
1727 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1728 }
1729
1730 //
1731 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1732 // resources, and return an error so SMM IPL will be unloaded.
1733 //
1734 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {
1735 //
1736 // Close all SMRAM ranges
1737 //
1738 Status = mSmmAccess->Close (mSmmAccess);
1739 ASSERT_EFI_ERROR (Status);
1740
1741 //
1742 // Print debug message that the SMRAM window is now closed.
1743 //
1744 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
1745
1746 //
1747 // Free all allocated resources
1748 //
1749 FreePool (gSmmCorePrivate->SmramRanges);
1750
1751 return EFI_UNSUPPORTED;
1752 }
1753
1754 //
1755 // Install SMM Base2 Protocol and SMM Communication Protocol
1756 //
1757 Status = gBS->InstallMultipleProtocolInterfaces (
1758 &mSmmIplHandle,
1759 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,
1760 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,
1761 NULL
1762 );
1763 ASSERT_EFI_ERROR (Status);
1764
1765 //
1766 // Create the set of protocol and event notifications that the SMM IPL requires
1767 //
1768 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
1769 if (mSmmIplEvents[Index].Protocol) {
1770 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
1771 mSmmIplEvents[Index].Guid,
1772 mSmmIplEvents[Index].NotifyTpl,
1773 mSmmIplEvents[Index].NotifyFunction,
1774 mSmmIplEvents[Index].NotifyContext,
1775 &Registration
1776 );
1777 } else {
1778 Status = gBS->CreateEventEx (
1779 EVT_NOTIFY_SIGNAL,
1780 mSmmIplEvents[Index].NotifyTpl,
1781 mSmmIplEvents[Index].NotifyFunction,
1782 mSmmIplEvents[Index].NotifyContext,
1783 mSmmIplEvents[Index].Guid,
1784 &mSmmIplEvents[Index].Event
1785 );
1786 ASSERT_EFI_ERROR (Status);
1787 }
1788 }
1789
1790 return EFI_SUCCESS;
1791 }