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