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