]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 #include "PiSmmCorePrivateData.h"
38 #include <Library/SafeIntLib.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 Indicate whether the driver is currently executing in the SMM Initialization phase.
424
425 @param This The EFI_SMM_BASE2_PROTOCOL instance.
426 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
427 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
428
429 @retval EFI_INVALID_PARAMETER InSmram was NULL.
430 @retval EFI_SUCCESS The call returned successfully.
431
432 **/
433 EFI_STATUS
434 EFIAPI
435 SmmBase2InSmram (
436 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
437 OUT BOOLEAN *InSmram
438 )
439 {
440 if (InSmram == NULL) {
441 return EFI_INVALID_PARAMETER;
442 }
443
444 *InSmram = gSmmCorePrivate->InSmm;
445
446 return EFI_SUCCESS;
447 }
448
449 /**
450 Retrieves the location of the System Management System Table (SMST).
451
452 @param This The EFI_SMM_BASE2_PROTOCOL instance.
453 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
454
455 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
456 @retval EFI_SUCCESS The memory was returned to the system.
457 @retval EFI_UNSUPPORTED Not in SMM.
458
459 **/
460 EFI_STATUS
461 EFIAPI
462 SmmBase2GetSmstLocation (
463 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
464 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
465 )
466 {
467 if ((This == NULL) || (Smst == NULL)) {
468 return EFI_INVALID_PARAMETER;
469 }
470
471 if (!gSmmCorePrivate->InSmm) {
472 return EFI_UNSUPPORTED;
473 }
474
475 *Smst = gSmmCorePrivate->Smst;
476
477 return EFI_SUCCESS;
478 }
479
480 /**
481 Communicates with a registered handler.
482
483 This function provides a service to send and receive messages from a registered
484 UEFI service. This function is part of the SMM Communication Protocol that may
485 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
486 after SetVirtualAddressMap().
487
488 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
489 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
490 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
491 being returned. Zero if the handler does not wish to reply with any data.
492 This parameter is optional and may be NULL.
493
494 @retval EFI_SUCCESS The message was successfully posted.
495 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
496 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
497 If this error is returned, the MessageLength field
498 in the CommBuffer header or the integer pointed by
499 CommSize, are updated to reflect the maximum payload
500 size the implementation can accommodate.
501 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
502 if not omitted, are in address range that cannot be
503 accessed by the MM environment.
504
505 **/
506 EFI_STATUS
507 EFIAPI
508 SmmCommunicationCommunicate (
509 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
510 IN OUT VOID *CommBuffer,
511 IN OUT UINTN *CommSize OPTIONAL
512 )
513 {
514 EFI_STATUS Status;
515 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
516 BOOLEAN OldInSmm;
517 UINTN TempCommSize;
518
519 //
520 // Check parameters
521 //
522 if (CommBuffer == NULL) {
523 return EFI_INVALID_PARAMETER;
524 }
525
526 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;
527
528 if (CommSize == NULL) {
529 TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;
530 } else {
531 TempCommSize = *CommSize;
532 //
533 // CommSize must hold HeaderGuid and MessageLength
534 //
535 if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {
536 return EFI_INVALID_PARAMETER;
537 }
538 }
539
540 //
541 // If not already in SMM, then generate a Software SMI
542 //
543 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {
544 //
545 // Put arguments for Software SMI in gSmmCorePrivate
546 //
547 gSmmCorePrivate->CommunicationBuffer = CommBuffer;
548 gSmmCorePrivate->BufferSize = TempCommSize;
549
550 //
551 // Generate Software SMI
552 //
553 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);
554 if (EFI_ERROR (Status)) {
555 return EFI_UNSUPPORTED;
556 }
557
558 //
559 // Return status from software SMI
560 //
561 if (CommSize != NULL) {
562 *CommSize = gSmmCorePrivate->BufferSize;
563 }
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 (
648 &mSmmCommunication,
649 CommBufferPhysical,
650 CommSize
651 );
652 }
653
654 /**
655 Event notification that is fired when GUIDed Event Group is signaled.
656
657 @param Event The Event that is being processed, not used.
658 @param Context Event Context, not used.
659
660 **/
661 VOID
662 EFIAPI
663 SmmIplGuidedEventNotify (
664 IN EFI_EVENT Event,
665 IN VOID *Context
666 )
667 {
668 UINTN Size;
669
670 //
671 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
672 //
673 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
674 mCommunicateHeader.MessageLength = 1;
675 mCommunicateHeader.Data[0] = 0;
676
677 //
678 // Generate the Software SMI and return the result
679 //
680 Size = sizeof (mCommunicateHeader);
681 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
682 }
683
684 /**
685 Event notification that is fired when EndOfDxe Event Group is signaled.
686
687 @param Event The Event that is being processed, not used.
688 @param Context Event Context, not used.
689
690 **/
691 VOID
692 EFIAPI
693 SmmIplEndOfDxeEventNotify (
694 IN EFI_EVENT Event,
695 IN VOID *Context
696 )
697 {
698 mEndOfDxe = TRUE;
699 }
700
701 /**
702 Event notification that is fired when DxeDispatch Event Group is signaled.
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 SmmIplDxeDispatchEventNotify (
711 IN EFI_EVENT Event,
712 IN VOID *Context
713 )
714 {
715 UINTN Size;
716 EFI_STATUS Status;
717
718 //
719 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
720 //
721 while (TRUE) {
722 //
723 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
724 // Clear the buffer passed into the Software SMI. This buffer will return
725 // the status of the SMM Core Dispatcher.
726 //
727 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
728 mCommunicateHeader.MessageLength = 1;
729 mCommunicateHeader.Data[0] = 0;
730
731 //
732 // Generate the Software SMI and return the result
733 //
734 Size = sizeof (mCommunicateHeader);
735 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
736
737 //
738 // Return if there is no request to restart the SMM Core Dispatcher
739 //
740 if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {
741 return;
742 }
743
744 //
745 // Close all SMRAM ranges to protect SMRAM
746 // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib
747 // so no need to reset the SMRAM to UC in MTRR.
748 //
749 Status = mSmmAccess->Close (mSmmAccess);
750 ASSERT_EFI_ERROR (Status);
751
752 //
753 // Print debug message that the SMRAM window is now closed.
754 //
755 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
756 }
757 }
758
759 /**
760 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
761
762 @param Event The Event that is being processed, not used.
763 @param Context Event Context, not used.
764
765 **/
766 VOID
767 EFIAPI
768 SmmIplSmmConfigurationEventNotify (
769 IN EFI_EVENT Event,
770 IN VOID *Context
771 )
772 {
773 EFI_STATUS Status;
774 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
775
776 //
777 // Make sure this notification is for this handler
778 //
779 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
780 if (EFI_ERROR (Status)) {
781 return;
782 }
783
784 //
785 // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol
786 //
787 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
788 ASSERT_EFI_ERROR (Status);
789
790 //
791 // Set flag to indicate that the SMM Entry Point has been registered which
792 // means that SMIs are now fully operational.
793 //
794 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
795
796 //
797 // Print debug message showing SMM Core entry point address.
798 //
799 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
800 }
801
802 /**
803 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
804 or if gEfiEventReadyToBootGuid is signaled.
805
806 @param Event The Event that is being processed, not used.
807 @param Context Event Context, not used.
808
809 **/
810 VOID
811 EFIAPI
812 SmmIplReadyToLockEventNotify (
813 IN EFI_EVENT Event,
814 IN VOID *Context
815 )
816 {
817 EFI_STATUS Status;
818 VOID *Interface;
819 UINTN Index;
820
821 //
822 // See if we are already locked
823 //
824 if (mSmmLocked) {
825 return;
826 }
827
828 //
829 // Make sure this notification is for this handler
830 //
831 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
832 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
833 if (EFI_ERROR (Status)) {
834 return;
835 }
836 } else {
837 //
838 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
839 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
840 // Print a warning on debug builds.
841 //
842 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
843 }
844
845 if (!mEndOfDxe) {
846 DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
847 REPORT_STATUS_CODE (
848 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
849 (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
850 );
851 ASSERT (FALSE);
852 }
853
854 //
855 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
856 //
857 mSmmAccess->Lock (mSmmAccess);
858
859 //
860 // Close protocol and event notification events that do not apply after the
861 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
862 // event has been signalled.
863 //
864 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
865 if (mSmmIplEvents[Index].CloseOnLock) {
866 gBS->CloseEvent (mSmmIplEvents[Index].Event);
867 }
868 }
869
870 //
871 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
872 //
873 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
874
875 //
876 // Print debug message that the SMRAM window is now locked.
877 //
878 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
879
880 //
881 // Set flag so this operation will not be performed again
882 //
883 mSmmLocked = TRUE;
884 }
885
886 /**
887 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
888
889 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
890 It convers pointer to new virtual address.
891
892 @param Event Event whose notification function is being invoked.
893 @param Context Pointer to the notification function's context.
894
895 **/
896 VOID
897 EFIAPI
898 SmmIplSetVirtualAddressNotify (
899 IN EFI_EVENT Event,
900 IN VOID *Context
901 )
902 {
903 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
904 }
905
906 /**
907 Get the fixed loading address from image header assigned by build tool. This function only be called
908 when Loading module at Fixed address feature enabled.
909
910 @param ImageContext Pointer to the image context structure that describes the PE/COFF
911 image that needs to be examined by this function.
912 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
913 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
914 **/
915 EFI_STATUS
916 GetPeCoffImageFixLoadingAssignedAddress (
917 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
918 )
919 {
920 UINTN SectionHeaderOffset;
921 EFI_STATUS Status;
922 EFI_IMAGE_SECTION_HEADER SectionHeader;
923 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
924 EFI_PHYSICAL_ADDRESS FixLoadingAddress;
925 UINT16 Index;
926 UINTN Size;
927 UINT16 NumberOfSections;
928 EFI_PHYSICAL_ADDRESS SmramBase;
929 UINT64 SmmCodeSize;
930 UINT64 ValueInSectionHeader;
931
932 //
933 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
934 //
935 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber));
936
937 FixLoadingAddress = 0;
938 Status = EFI_NOT_FOUND;
939 SmramBase = mLMFAConfigurationTable->SmramBase;
940 //
941 // Get PeHeader pointer
942 //
943 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8 *)ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
944 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
945 sizeof (UINT32) +
946 sizeof (EFI_IMAGE_FILE_HEADER) +
947 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
948 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
949
950 //
951 // Get base address from the first section header that doesn't point to code section.
952 //
953 for (Index = 0; Index < NumberOfSections; Index++) {
954 //
955 // Read section header from file
956 //
957 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
958 Status = ImageContext->ImageRead (
959 ImageContext->Handle,
960 SectionHeaderOffset,
961 &Size,
962 &SectionHeader
963 );
964 if (EFI_ERROR (Status)) {
965 return Status;
966 }
967
968 Status = EFI_NOT_FOUND;
969
970 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
971 //
972 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
973 // first section header that doesn't point to code section in image header. And there is an assumption that when the
974 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
975 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
976 //
977 ValueInSectionHeader = ReadUnaligned64 ((UINT64 *)&SectionHeader.PointerToRelocations);
978 if (ValueInSectionHeader != 0) {
979 //
980 // Found first section header that doesn't point to code section in which build tool saves the
981 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
982 //
983 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);
984
985 if ((SmramBase + SmmCodeSize > FixLoadingAddress) && (SmramBase <= FixLoadingAddress)) {
986 //
987 // The assigned address is valid. Return the specified loading address
988 //
989 ImageContext->ImageAddress = FixLoadingAddress;
990 Status = EFI_SUCCESS;
991 }
992 }
993
994 break;
995 }
996
997 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
998 }
999
1000 DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));
1001 return Status;
1002 }
1003
1004 /**
1005 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
1006
1007 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
1008 currently executing image, the rang of SMRAM to
1009 hold SMM Core will be excluded.
1010 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
1011
1012 @param[in] Context Context to pass into SMM Core
1013
1014 @return EFI_STATUS
1015
1016 **/
1017 EFI_STATUS
1018 ExecuteSmmCoreFromSmram (
1019 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,
1020 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,
1021 IN VOID *Context
1022 )
1023 {
1024 EFI_STATUS Status;
1025 VOID *SourceBuffer;
1026 UINTN SourceSize;
1027 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
1028 UINTN PageCount;
1029 EFI_IMAGE_ENTRY_POINT EntryPoint;
1030
1031 //
1032 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
1033 //
1034 Status = GetSectionFromAnyFvByFileType (
1035 EFI_FV_FILETYPE_SMM_CORE,
1036 0,
1037 EFI_SECTION_PE32,
1038 0,
1039 &SourceBuffer,
1040 &SourceSize
1041 );
1042 if (EFI_ERROR (Status)) {
1043 return Status;
1044 }
1045
1046 //
1047 // Initialize ImageContext
1048 //
1049 ImageContext.Handle = SourceBuffer;
1050 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
1051
1052 //
1053 // Get information about the image being loaded
1054 //
1055 Status = PeCoffLoaderGetImageInfo (&ImageContext);
1056 if (EFI_ERROR (Status)) {
1057 return Status;
1058 }
1059
1060 //
1061 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
1062 // the address assigned by build tool.
1063 //
1064 if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {
1065 //
1066 // Get the fixed loading address assigned by Build tool
1067 //
1068 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
1069 if (!EFI_ERROR (Status)) {
1070 //
1071 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
1072 //
1073 PageCount = 0;
1074 //
1075 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
1076 //
1077 gSmmCorePrivate->SmramRangeCount--;
1078 } else {
1079 DEBUG ((DEBUG_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
1080 //
1081 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1082 // specified by SmramRange
1083 //
1084 PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
1085
1086 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
1087 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
1088
1089 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
1090 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
1091 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
1092 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
1093 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
1094
1095 //
1096 // Align buffer on section boundary
1097 //
1098 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1099 }
1100 } else {
1101 //
1102 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1103 // specified by SmramRange
1104 //
1105 PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
1106
1107 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
1108 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
1109
1110 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
1111 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
1112 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
1113 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
1114 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
1115
1116 //
1117 // Align buffer on section boundary
1118 //
1119 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1120 }
1121
1122 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
1123 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);
1124
1125 //
1126 // Print debug message showing SMM Core load address.
1127 //
1128 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
1129
1130 //
1131 // Load the image to our new buffer
1132 //
1133 Status = PeCoffLoaderLoadImage (&ImageContext);
1134 if (!EFI_ERROR (Status)) {
1135 //
1136 // Relocate the image in our new buffer
1137 //
1138 Status = PeCoffLoaderRelocateImage (&ImageContext);
1139 if (!EFI_ERROR (Status)) {
1140 //
1141 // Flush the instruction cache so the image data are written before we execute it
1142 //
1143 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
1144
1145 //
1146 // Print debug message showing SMM Core entry point address.
1147 //
1148 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
1149
1150 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;
1151 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;
1152 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));
1153 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));
1154
1155 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;
1156
1157 //
1158 // Execute image
1159 //
1160 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
1161 Status = EntryPoint ((EFI_HANDLE)Context, gST);
1162 }
1163 }
1164
1165 //
1166 // Always free memory allocated by GetFileBufferByFilePath ()
1167 //
1168 FreePool (SourceBuffer);
1169
1170 return Status;
1171 }
1172
1173 /**
1174 SMM split SMRAM entry.
1175
1176 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1177 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1178 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1179 @param[in, out] RangeCount Pointer to range count.
1180 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1181 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1182 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1183 that no need to be split anymore.
1184 @param[in, out] FinalRangeCount Pointer to final range count.
1185
1186 **/
1187 VOID
1188 SmmSplitSmramEntry (
1189 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1190 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,
1191 OUT EFI_SMRAM_DESCRIPTOR *Ranges,
1192 IN OUT UINTN *RangeCount,
1193 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,
1194 IN OUT UINTN *ReservedRangeCount,
1195 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,
1196 IN OUT UINTN *FinalRangeCount
1197 )
1198 {
1199 UINT64 RangeToCompareEnd;
1200 UINT64 ReservedRangeToCompareEnd;
1201
1202 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
1203 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
1204
1205 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1206 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd))
1207 {
1208 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {
1209 //
1210 // RangeToCompare ReservedRangeToCompare
1211 // ---- ---- --------------------------------------
1212 // | | | | -> 1. ReservedRangeToCompare
1213 // ---- | | |--| --------------------------------------
1214 // | | | | | |
1215 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1216 // | | | | | | RangeToCompare->PhysicalSize = 0
1217 // ---- | | |--| --------------------------------------
1218 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1219 // ---- ---- --------------------------------------
1220 //
1221
1222 //
1223 // 1. Update ReservedRangeToCompare.
1224 //
1225 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1226 //
1227 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1228 // Zero RangeToCompare->PhysicalSize.
1229 //
1230 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1231 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1232 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1233 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;
1234 *FinalRangeCount += 1;
1235 RangeToCompare->PhysicalSize = 0;
1236 //
1237 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1238 //
1239 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1240 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;
1241 *ReservedRangeCount += 1;
1242 } else {
1243 //
1244 // RangeToCompare ReservedRangeToCompare
1245 // ---- ---- --------------------------------------
1246 // | | | | -> 1. ReservedRangeToCompare
1247 // ---- | | |--| --------------------------------------
1248 // | | | | | |
1249 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1250 // | | | | | |
1251 // | | ---- |--| --------------------------------------
1252 // | | | | -> 3. RangeToCompare
1253 // ---- ---- --------------------------------------
1254 //
1255
1256 //
1257 // 1. Update ReservedRangeToCompare.
1258 //
1259 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1260 //
1261 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1262 //
1263 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1264 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1265 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1266 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;
1267 *FinalRangeCount += 1;
1268 //
1269 // 3. Update RangeToCompare.
1270 //
1271 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1272 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1273 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1274 }
1275 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1276 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd))
1277 {
1278 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {
1279 //
1280 // RangeToCompare ReservedRangeToCompare
1281 // ---- ---- --------------------------------------
1282 // | | | | -> 1. RangeToCompare
1283 // | | ---- |--| --------------------------------------
1284 // | | | | | |
1285 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1286 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1287 // | | ---- |--| --------------------------------------
1288 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1289 // ---- ---- --------------------------------------
1290 //
1291
1292 //
1293 // 1. Update RangeToCompare.
1294 //
1295 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1296 //
1297 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1298 // ReservedRangeToCompare->SmramReservedSize = 0
1299 //
1300 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1301 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1302 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1303 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;
1304 *FinalRangeCount += 1;
1305 ReservedRangeToCompare->SmramReservedSize = 0;
1306 //
1307 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1308 //
1309 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1310 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1311 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;
1312 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;
1313 *RangeCount += 1;
1314 } else {
1315 //
1316 // RangeToCompare ReservedRangeToCompare
1317 // ---- ---- --------------------------------------
1318 // | | | | -> 1. RangeToCompare
1319 // | | ---- |--| --------------------------------------
1320 // | | | | | |
1321 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1322 // | | | | | |
1323 // ---- | | |--| --------------------------------------
1324 // | | | | -> 3. ReservedRangeToCompare
1325 // ---- ---- --------------------------------------
1326 //
1327
1328 //
1329 // 1. Update RangeToCompare.
1330 //
1331 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1332 //
1333 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1334 // ReservedRangeToCompare->SmramReservedSize = 0
1335 //
1336 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1337 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1338 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1339 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;
1340 *FinalRangeCount += 1;
1341 //
1342 // 3. Update ReservedRangeToCompare.
1343 //
1344 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1345 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1346 }
1347 }
1348 }
1349
1350 /**
1351 Returns if SMRAM range and SMRAM reserved range are overlapped.
1352
1353 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1354 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1355
1356 @retval TRUE There is overlap.
1357 @retval TRUE Math error.
1358 @retval FALSE There is no overlap.
1359
1360 **/
1361 BOOLEAN
1362 SmmIsSmramOverlap (
1363 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1364 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare
1365 )
1366 {
1367 UINT64 RangeToCompareEnd;
1368 UINT64 ReservedRangeToCompareEnd;
1369 BOOLEAN IsOverUnderflow1;
1370 BOOLEAN IsOverUnderflow2;
1371
1372 // Check for over or underflow.
1373 IsOverUnderflow1 = EFI_ERROR (
1374 SafeUint64Add (
1375 (UINT64)RangeToCompare->CpuStart,
1376 RangeToCompare->PhysicalSize,
1377 &RangeToCompareEnd
1378 )
1379 );
1380 IsOverUnderflow2 = EFI_ERROR (
1381 SafeUint64Add (
1382 (UINT64)ReservedRangeToCompare->SmramReservedStart,
1383 ReservedRangeToCompare->SmramReservedSize,
1384 &ReservedRangeToCompareEnd
1385 )
1386 );
1387 if (IsOverUnderflow1 || IsOverUnderflow2) {
1388 return TRUE;
1389 }
1390
1391 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1392 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd))
1393 {
1394 return TRUE;
1395 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1396 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd))
1397 {
1398 return TRUE;
1399 }
1400
1401 return FALSE;
1402 }
1403
1404 /**
1405 Get full SMRAM ranges.
1406
1407 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1408 SmmConfiguration protocol, split the entries if there is overlap between them.
1409 It will also reserve one entry for SMM core.
1410
1411 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1412
1413 @return Pointer to full SMRAM ranges.
1414
1415 **/
1416 EFI_SMRAM_DESCRIPTOR *
1417 GetFullSmramRanges (
1418 OUT UINTN *FullSmramRangeCount
1419 )
1420 {
1421 EFI_STATUS Status;
1422 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
1423 UINTN Size;
1424 UINTN Index;
1425 UINTN Index2;
1426 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;
1427 UINTN TempSmramRangeCount;
1428 UINTN AdditionSmramRangeCount;
1429 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;
1430 UINTN SmramRangeCount;
1431 EFI_SMRAM_DESCRIPTOR *SmramRanges;
1432 UINTN SmramReservedCount;
1433 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;
1434 UINTN MaxCount;
1435 BOOLEAN Rescan;
1436
1437 //
1438 // Get SMM Configuration Protocol if it is present.
1439 //
1440 SmmConfiguration = NULL;
1441 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **)&SmmConfiguration);
1442
1443 //
1444 // Get SMRAM information.
1445 //
1446 Size = 0;
1447 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
1448 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
1449
1450 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
1451
1452 //
1453 // Get SMRAM reserved region count.
1454 //
1455 SmramReservedCount = 0;
1456 if (SmmConfiguration != NULL) {
1457 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {
1458 SmramReservedCount++;
1459 }
1460 }
1461
1462 //
1463 // Reserve one entry for SMM Core in the full SMRAM ranges.
1464 //
1465 AdditionSmramRangeCount = 1;
1466 if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {
1467 //
1468 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1469 //
1470 AdditionSmramRangeCount = 2;
1471 }
1472
1473 if (SmramReservedCount == 0) {
1474 //
1475 // No reserved SMRAM entry from SMM Configuration Protocol.
1476 //
1477 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;
1478 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);
1479 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocateZeroPool (Size);
1480 ASSERT (FullSmramRanges != NULL);
1481
1482 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);
1483 ASSERT_EFI_ERROR (Status);
1484
1485 return FullSmramRanges;
1486 }
1487
1488 //
1489 // Why MaxCount = X + 2 * Y?
1490 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1491 //
1492 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1493 // ---- ----
1494 // | | ---- |--|
1495 // | | | | -> | |
1496 // | | ---- |--|
1497 // ---- ----
1498 //
1499 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1500 // ---- ----
1501 // | | | |
1502 // | | ---- |--|
1503 // | | | | | |
1504 // |--| | | -> |--|
1505 // | | | | | |
1506 // | | ---- |--|
1507 // | | | |
1508 // ---- ----
1509 //
1510 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1511 // ---- ----
1512 // | | | |
1513 // | | ---- |--|
1514 // |--| | | |--|
1515 // | | | | -> | |
1516 // |--| | | |--|
1517 // | | ---- |--|
1518 // | | | |
1519 // ---- ----
1520 //
1521 // ......
1522 //
1523 MaxCount = SmramRangeCount + 2 * SmramReservedCount;
1524
1525 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);
1526 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *)AllocatePool (Size);
1527 ASSERT (SmramReservedRanges != NULL);
1528 for (Index = 0; Index < SmramReservedCount; Index++) {
1529 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));
1530 }
1531
1532 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);
1533 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);
1534 ASSERT (TempSmramRanges != NULL);
1535 TempSmramRangeCount = 0;
1536
1537 SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);
1538 ASSERT (SmramRanges != NULL);
1539 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);
1540 ASSERT_EFI_ERROR (Status);
1541
1542 do {
1543 Rescan = FALSE;
1544 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {
1545 //
1546 // Skip zero size entry.
1547 //
1548 if (SmramRanges[Index].PhysicalSize != 0) {
1549 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {
1550 //
1551 // Skip zero size entry.
1552 //
1553 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {
1554 if (SmmIsSmramOverlap (
1555 &SmramRanges[Index],
1556 &SmramReservedRanges[Index2]
1557 ))
1558 {
1559 //
1560 // There is overlap, need to split entry and then rescan.
1561 //
1562 SmmSplitSmramEntry (
1563 &SmramRanges[Index],
1564 &SmramReservedRanges[Index2],
1565 SmramRanges,
1566 &SmramRangeCount,
1567 SmramReservedRanges,
1568 &SmramReservedCount,
1569 TempSmramRanges,
1570 &TempSmramRangeCount
1571 );
1572 Rescan = TRUE;
1573 }
1574 }
1575 }
1576
1577 if (!Rescan) {
1578 //
1579 // No any overlap, copy the entry to the temp SMRAM ranges.
1580 // Zero SmramRanges[Index].PhysicalSize = 0;
1581 //
1582 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1583 SmramRanges[Index].PhysicalSize = 0;
1584 }
1585 }
1586 }
1587 } while (Rescan);
1588
1589 ASSERT (TempSmramRangeCount <= MaxCount);
1590
1591 //
1592 // Sort the entries
1593 //
1594 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));
1595 ASSERT (FullSmramRanges != NULL);
1596 *FullSmramRangeCount = 0;
1597 do {
1598 for (Index = 0; Index < TempSmramRangeCount; Index++) {
1599 if (TempSmramRanges[Index].PhysicalSize != 0) {
1600 break;
1601 }
1602 }
1603
1604 ASSERT (Index < TempSmramRangeCount);
1605 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {
1606 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {
1607 Index = Index2;
1608 }
1609 }
1610
1611 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1612 *FullSmramRangeCount += 1;
1613 TempSmramRanges[Index].PhysicalSize = 0;
1614 } while (*FullSmramRangeCount < TempSmramRangeCount);
1615
1616 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);
1617 *FullSmramRangeCount += AdditionSmramRangeCount;
1618
1619 FreePool (SmramRanges);
1620 FreePool (SmramReservedRanges);
1621 FreePool (TempSmramRanges);
1622
1623 return FullSmramRanges;
1624 }
1625
1626 /**
1627 The Entry Point for SMM IPL
1628
1629 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1630 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1631 critical events required to coordinate between DXE and SMM environments.
1632
1633 @param ImageHandle The firmware allocated handle for the EFI image.
1634 @param SystemTable A pointer to the EFI System Table.
1635
1636 @retval EFI_SUCCESS The entry point is executed successfully.
1637 @retval Other Some error occurred when executing this entry point.
1638
1639 **/
1640 EFI_STATUS
1641 EFIAPI
1642 SmmIplEntry (
1643 IN EFI_HANDLE ImageHandle,
1644 IN EFI_SYSTEM_TABLE *SystemTable
1645 )
1646 {
1647 EFI_STATUS Status;
1648 UINTN Index;
1649 UINT64 MaxSize;
1650 VOID *Registration;
1651 UINT64 SmmCodeSize;
1652 EFI_CPU_ARCH_PROTOCOL *CpuArch;
1653 EFI_STATUS SetAttrStatus;
1654 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;
1655 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;
1656
1657 //
1658 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1659 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1660 // by the SMM Core
1661 //
1662 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
1663
1664 //
1665 // Get SMM Access Protocol
1666 //
1667 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
1668 ASSERT_EFI_ERROR (Status);
1669
1670 //
1671 // Get SMM Control2 Protocol
1672 //
1673 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
1674 ASSERT_EFI_ERROR (Status);
1675
1676 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);
1677
1678 //
1679 // Open all SMRAM ranges
1680 //
1681 Status = mSmmAccess->Open (mSmmAccess);
1682 ASSERT_EFI_ERROR (Status);
1683
1684 //
1685 // Print debug message that the SMRAM window is now open.
1686 //
1687 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
1688
1689 //
1690 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1691 //
1692 mCurrentSmramRange = NULL;
1693 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
1694 //
1695 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1696 //
1697 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
1698 continue;
1699 }
1700
1701 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
1702 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {
1703 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
1704 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
1705 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
1706 }
1707 }
1708 }
1709 }
1710
1711 if (mCurrentSmramRange != NULL) {
1712 //
1713 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1714 //
1715 DEBUG ((
1716 DEBUG_INFO,
1717 "SMM IPL found SMRAM window %p - %p\n",
1718 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
1719 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
1720 ));
1721
1722 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);
1723 //
1724 // Make sure we can change the desired memory attributes.
1725 //
1726 Status = gDS->GetMemorySpaceDescriptor (
1727 mSmramCacheBase,
1728 &MemDesc
1729 );
1730 ASSERT_EFI_ERROR (Status);
1731 if ((MemDesc.Capabilities & SMRAM_CAPABILITIES) != SMRAM_CAPABILITIES) {
1732 gDS->SetMemorySpaceCapabilities (
1733 mSmramCacheBase,
1734 mSmramCacheSize,
1735 MemDesc.Capabilities | SMRAM_CAPABILITIES
1736 );
1737 }
1738
1739 //
1740 // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear
1741 // all paging attributes.
1742 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1743 // is not available here.
1744 //
1745 CpuArch = NULL;
1746 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);
1747 if (!EFI_ERROR (Status)) {
1748 MemDesc.Attributes &= ~(EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK);
1749 MemDesc.Attributes |= EFI_MEMORY_WB;
1750 Status = gDS->SetMemorySpaceAttributes (
1751 mSmramCacheBase,
1752 mSmramCacheSize,
1753 MemDesc.Attributes
1754 );
1755 if (EFI_ERROR (Status)) {
1756 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1757 }
1758
1759 DEBUG_CODE (
1760 gDS->GetMemorySpaceDescriptor (
1761 mSmramCacheBase,
1762 &MemDesc
1763 );
1764 DEBUG ((DEBUG_INFO, "SMRAM attributes: %016lx\n", MemDesc.Attributes));
1765 ASSERT ((MemDesc.Attributes & EFI_MEMORY_ATTRIBUTE_MASK) == 0);
1766 );
1767 }
1768
1769 //
1770 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1771 // Modules At Fixed Address Configuration Table.
1772 //
1773 if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {
1774 //
1775 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1776 //
1777 SmmCodeSize = LShiftU64 (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);
1778 //
1779 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1780 //
1781 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);
1782 //
1783 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1784 //
1785 Status = EfiGetSystemConfigurationTable (
1786 &gLoadFixedAddressConfigurationTableGuid,
1787 (VOID **)&mLMFAConfigurationTable
1788 );
1789 if (!EFI_ERROR (Status) && (mLMFAConfigurationTable != NULL)) {
1790 mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;
1791 //
1792 // Print the SMRAM base
1793 //
1794 DEBUG ((DEBUG_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));
1795 }
1796
1797 //
1798 // Fill the Smram range for all SMM code
1799 //
1800 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];
1801 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;
1802 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;
1803 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;
1804 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;
1805
1806 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;
1807 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;
1808 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;
1809 }
1810
1811 //
1812 // Load SMM Core into SMRAM and execute it from SMRAM
1813 //
1814 Status = ExecuteSmmCoreFromSmram (
1815 mCurrentSmramRange,
1816 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],
1817 gSmmCorePrivate
1818 );
1819 if (EFI_ERROR (Status)) {
1820 //
1821 // Print error message that the SMM Core failed to be loaded and executed.
1822 //
1823 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1824
1825 //
1826 // Attempt to reset SMRAM cacheability to UC
1827 //
1828 if (CpuArch != NULL) {
1829 SetAttrStatus = gDS->SetMemorySpaceAttributes (
1830 mSmramCacheBase,
1831 mSmramCacheSize,
1832 EFI_MEMORY_UC
1833 );
1834 if (EFI_ERROR (SetAttrStatus)) {
1835 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1836 }
1837 }
1838 }
1839 } else {
1840 //
1841 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1842 //
1843 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1844 }
1845
1846 //
1847 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1848 // resources, and return an error so SMM IPL will be unloaded.
1849 //
1850 if ((mCurrentSmramRange == NULL) || EFI_ERROR (Status)) {
1851 //
1852 // Close all SMRAM ranges
1853 //
1854 Status = mSmmAccess->Close (mSmmAccess);
1855 ASSERT_EFI_ERROR (Status);
1856
1857 //
1858 // Print debug message that the SMRAM window is now closed.
1859 //
1860 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
1861
1862 //
1863 // Free all allocated resources
1864 //
1865 FreePool (gSmmCorePrivate->SmramRanges);
1866
1867 return EFI_UNSUPPORTED;
1868 }
1869
1870 //
1871 // Install SMM Base2 Protocol and SMM Communication Protocol
1872 //
1873 Status = gBS->InstallMultipleProtocolInterfaces (
1874 &mSmmIplHandle,
1875 &gEfiSmmBase2ProtocolGuid,
1876 &mSmmBase2,
1877 &gEfiSmmCommunicationProtocolGuid,
1878 &mSmmCommunication,
1879 &gEfiMmCommunication2ProtocolGuid,
1880 &mMmCommunication2,
1881 NULL
1882 );
1883 ASSERT_EFI_ERROR (Status);
1884
1885 //
1886 // Create the set of protocol and event notifications that the SMM IPL requires
1887 //
1888 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
1889 if (mSmmIplEvents[Index].Protocol) {
1890 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
1891 mSmmIplEvents[Index].Guid,
1892 mSmmIplEvents[Index].NotifyTpl,
1893 mSmmIplEvents[Index].NotifyFunction,
1894 mSmmIplEvents[Index].NotifyContext,
1895 &Registration
1896 );
1897 } else {
1898 Status = gBS->CreateEventEx (
1899 EVT_NOTIFY_SIGNAL,
1900 mSmmIplEvents[Index].NotifyTpl,
1901 mSmmIplEvents[Index].NotifyFunction,
1902 mSmmIplEvents[Index].NotifyContext,
1903 mSmmIplEvents[Index].Guid,
1904 &mSmmIplEvents[Index].Event
1905 );
1906 ASSERT_EFI_ERROR (Status);
1907 }
1908 }
1909
1910 return EFI_SUCCESS;
1911 }