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