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