]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg/PiSmmCore: Add SmiHandlerProfile support.
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
CommitLineData
e42e9404 1/** @file\r
2 SMM Core Main Entry Point\r
3\r
ca41f3f4 4 Copyright (c) 2009 - 2017, 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 "PiSmmCore.h"\r
16\r
17//\r
18// Physical pointer to private structure shared between SMM IPL and the SMM Core\r
19//\r
20SMM_CORE_PRIVATE_DATA *gSmmCorePrivate;\r
21\r
22//\r
23// SMM Core global variable for SMM System Table. Only accessed as a physical structure in SMRAM.\r
24//\r
25EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst = {\r
26 {\r
27 SMM_SMST_SIGNATURE,\r
28 EFI_SMM_SYSTEM_TABLE2_REVISION,\r
29 sizeof (gSmmCoreSmst.Hdr)\r
30 },\r
31 NULL, // SmmFirmwareVendor\r
32 0, // SmmFirmwareRevision\r
33 SmmInstallConfigurationTable,\r
34 {\r
35 {\r
36 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmMemRead\r
37 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmMemWrite\r
38 },\r
39 {\r
40 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmIoRead\r
41 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmIoWrite\r
42 }\r
43 },\r
44 SmmAllocatePool,\r
45 SmmFreePool,\r
46 SmmAllocatePages,\r
47 SmmFreePages,\r
48 NULL, // SmmStartupThisAp\r
49 0, // CurrentlyExecutingCpu\r
50 0, // NumberOfCpus\r
51 NULL, // CpuSaveStateSize\r
52 NULL, // CpuSaveState\r
53 0, // NumberOfTableEntries\r
54 NULL, // SmmConfigurationTable\r
55 SmmInstallProtocolInterface,\r
56 SmmUninstallProtocolInterface,\r
57 SmmHandleProtocol,\r
58 SmmRegisterProtocolNotify,\r
59 SmmLocateHandle,\r
60 SmmLocateProtocol,\r
61 SmiManage,\r
62 SmiHandlerRegister,\r
63 SmiHandlerUnRegister\r
64};\r
65\r
66//\r
67// Flag to determine if the platform has performed a legacy boot.\r
68// If this flag is TRUE, then the runtime code and runtime data associated with the \r
53ec4d7f 69// SMM IPL are converted to free memory, so the SMM Core must guarantee that is\r
e42e9404 70// does not touch of the code/data associated with the SMM IPL if this flag is TRUE.\r
71//\r
72BOOLEAN mInLegacyBoot = FALSE;\r
73\r
74//\r
75// Table of SMI Handlers that are registered by the SMM Core when it is initialized\r
76//\r
77SMM_CORE_SMI_HANDLERS mSmmCoreSmiHandlers[] = {\r
53ec4d7f
SZ
78 { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE },\r
79 { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, \r
80 { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE },\r
81 { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid, NULL, FALSE },\r
82 { SmmReadyToBootHandler, &gEfiEventReadyToBootGuid, NULL, FALSE },\r
dc157845 83 { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, TRUE },\r
53ec4d7f 84 { NULL, NULL, NULL, FALSE }\r
e42e9404 85};\r
86\r
84edd20b
SZ
87UINTN mFullSmramRangeCount;\r
88EFI_SMRAM_DESCRIPTOR *mFullSmramRanges;\r
89\r
285a682c
JY
90EFI_SMM_DRIVER_ENTRY *mSmmCoreDriverEntry;\r
91\r
0b256fb1
JY
92EFI_LOADED_IMAGE_PROTOCOL *mSmmCoreLoadedImage;\r
93\r
e42e9404 94/**\r
95 Place holder function until all the SMM System Table Service are available.\r
96\r
97 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
98\r
99 @param Arg1 Undefined\r
100 @param Arg2 Undefined\r
101 @param Arg3 Undefined\r
102 @param Arg4 Undefined\r
103 @param Arg5 Undefined\r
104\r
105 @return EFI_NOT_AVAILABLE_YET\r
106\r
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110SmmEfiNotAvailableYetArg5 (\r
111 UINTN Arg1,\r
112 UINTN Arg2,\r
113 UINTN Arg3,\r
114 UINTN Arg4,\r
115 UINTN Arg5\r
116 )\r
117{\r
118 //\r
119 // This function should never be executed. If it does, then the architectural protocols\r
120 // have not been designed correctly.\r
121 //\r
122 return EFI_NOT_AVAILABLE_YET;\r
123}\r
124\r
125/**\r
126 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM\r
127 Core uses this signal to know that a Legacy Boot has been performed and that \r
128 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can\r
129 not be accessed from SMM anymore since that structure is considered free memory by\r
53ec4d7f
SZ
130 a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM\r
131 driver that system enter legacy boot.\r
e42e9404 132\r
133 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
134 @param Context Points to an optional handler context which was specified when the handler was registered.\r
135 @param CommBuffer A pointer to a collection of data in memory that will\r
136 be conveyed from a non-SMM environment into an SMM environment.\r
137 @param CommBufferSize The size of the CommBuffer.\r
138\r
139 @return Status Code\r
140\r
141**/\r
142EFI_STATUS\r
143EFIAPI\r
144SmmLegacyBootHandler (\r
145 IN EFI_HANDLE DispatchHandle,\r
146 IN CONST VOID *Context, OPTIONAL\r
147 IN OUT VOID *CommBuffer, OPTIONAL\r
148 IN OUT UINTN *CommBufferSize OPTIONAL\r
149 )\r
150{\r
53ec4d7f
SZ
151 EFI_STATUS Status;\r
152 EFI_HANDLE SmmHandle;\r
153\r
154 //\r
155 // Install SMM Legacy Boot protocol.\r
156 //\r
157 SmmHandle = NULL;\r
158 Status = SmmInstallProtocolInterface (\r
159 &SmmHandle,\r
160 &gEdkiiSmmLegacyBootProtocolGuid,\r
161 EFI_NATIVE_INTERFACE,\r
162 NULL\r
163 );\r
164\r
e42e9404 165 mInLegacyBoot = TRUE;\r
53ec4d7f
SZ
166\r
167 SmiHandlerUnRegister (DispatchHandle);\r
168\r
169 return Status;\r
170}\r
171\r
172/**\r
173 Software SMI handler that is called when an Exit Boot Services event is signalled.\r
174 Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver\r
175 that system enter exit boot services.\r
176\r
177 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
178 @param Context Points to an optional handler context which was specified when the handler was registered.\r
179 @param CommBuffer A pointer to a collection of data in memory that will\r
180 be conveyed from a non-SMM environment into an SMM environment.\r
181 @param CommBufferSize The size of the CommBuffer.\r
182\r
183 @return Status Code\r
184\r
185**/\r
186EFI_STATUS\r
187EFIAPI\r
188SmmExitBootServicesHandler (\r
189 IN EFI_HANDLE DispatchHandle,\r
190 IN CONST VOID *Context, OPTIONAL\r
191 IN OUT VOID *CommBuffer, OPTIONAL\r
192 IN OUT UINTN *CommBufferSize OPTIONAL\r
193 )\r
194{\r
195 EFI_STATUS Status;\r
196 EFI_HANDLE SmmHandle;\r
197\r
198 //\r
199 // Install SMM Exit Boot Services protocol.\r
200 //\r
201 SmmHandle = NULL;\r
202 Status = SmmInstallProtocolInterface (\r
203 &SmmHandle,\r
204 &gEdkiiSmmExitBootServicesProtocolGuid,\r
205 EFI_NATIVE_INTERFACE,\r
206 NULL\r
207 );\r
208\r
209 SmiHandlerUnRegister (DispatchHandle);\r
210\r
211 return Status;\r
212}\r
213\r
214/**\r
215 Software SMI handler that is called when an Ready To Boot event is signalled.\r
216 Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver\r
217 that system enter ready to boot.\r
218\r
219 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
220 @param Context Points to an optional handler context which was specified when the handler was registered.\r
221 @param CommBuffer A pointer to a collection of data in memory that will\r
222 be conveyed from a non-SMM environment into an SMM environment.\r
223 @param CommBufferSize The size of the CommBuffer.\r
224\r
225 @return Status Code\r
226\r
227**/\r
228EFI_STATUS\r
229EFIAPI\r
230SmmReadyToBootHandler (\r
231 IN EFI_HANDLE DispatchHandle,\r
232 IN CONST VOID *Context, OPTIONAL\r
233 IN OUT VOID *CommBuffer, OPTIONAL\r
234 IN OUT UINTN *CommBufferSize OPTIONAL\r
235 )\r
236{\r
237 EFI_STATUS Status;\r
238 EFI_HANDLE SmmHandle;\r
239\r
240 //\r
241 // Install SMM Ready To Boot protocol.\r
242 //\r
243 SmmHandle = NULL;\r
244 Status = SmmInstallProtocolInterface (\r
245 &SmmHandle,\r
246 &gEdkiiSmmReadyToBootProtocolGuid,\r
247 EFI_NATIVE_INTERFACE,\r
248 NULL\r
249 );\r
250\r
251 SmiHandlerUnRegister (DispatchHandle);\r
252\r
253 return Status;\r
e42e9404 254}\r
255\r
256/**\r
257 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added\r
258 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the \r
259 Software SMIs that are nor required after SMRAM is locked and installs the \r
260 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about \r
2048c585 261 to be locked. It also verifies the SMM CPU I/O 2 Protocol has been installed\r
e42e9404 262 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.\r
263\r
264 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
265 @param Context Points to an optional handler context which was specified when the handler was registered.\r
266 @param CommBuffer A pointer to a collection of data in memory that will\r
267 be conveyed from a non-SMM environment into an SMM environment.\r
268 @param CommBufferSize The size of the CommBuffer.\r
269\r
270 @return Status Code\r
271\r
272**/\r
273EFI_STATUS\r
274EFIAPI\r
275SmmReadyToLockHandler (\r
276 IN EFI_HANDLE DispatchHandle,\r
277 IN CONST VOID *Context, OPTIONAL\r
278 IN OUT VOID *CommBuffer, OPTIONAL\r
279 IN OUT UINTN *CommBufferSize OPTIONAL\r
280 )\r
281{\r
282 EFI_STATUS Status;\r
283 UINTN Index;\r
284 EFI_HANDLE SmmHandle;\r
285 VOID *Interface;\r
286\r
287 //\r
288 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped\r
289 //\r
290 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
291 if (mSmmCoreSmiHandlers[Index].UnRegister) {\r
292 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);\r
293 }\r
294 }\r
295\r
296 //\r
297 // Install SMM Ready to lock protocol\r
298 //\r
299 SmmHandle = NULL;\r
300 Status = SmmInstallProtocolInterface (\r
301 &SmmHandle,\r
302 &gEfiSmmReadyToLockProtocolGuid,\r
303 EFI_NATIVE_INTERFACE,\r
304 NULL\r
305 );\r
306\r
307 //\r
308 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database\r
309 //\r
310 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);\r
311\r
312 //\r
313 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed\r
314 //\r
315 DEBUG_CODE_BEGIN ();\r
316 if (EFI_ERROR (Status)) {\r
317 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));\r
318 }\r
319 DEBUG_CODE_END ();\r
320\r
321 //\r
322 // Assert if the CPU I/O 2 Protocol is not installed\r
323 //\r
324 ASSERT_EFI_ERROR (Status);\r
325\r
326 //\r
327 // Display any drivers that were not dispatched because dependency expression\r
328 // evaluated to false if this is a debug build\r
329 //\r
330 DEBUG_CODE_BEGIN ();\r
331 SmmDisplayDiscoveredNotDispatched ();\r
332 DEBUG_CODE_END ();\r
333\r
334 //\r
335 // Not allowed to use gST or gBS after lock\r
336 //\r
337 gST = NULL;\r
338 gBS = NULL;\r
339\r
84edd20b
SZ
340 SmramProfileReadyToLock ();\r
341\r
e42e9404 342 return Status;\r
343}\r
344\r
46ece1ff
JY
345/**\r
346 Software SMI handler that is called when the EndOfDxe event is signalled.\r
347 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that\r
348 platform code will invoke 3rd part code.\r
349\r
350 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
351 @param Context Points to an optional handler context which was specified when the handler was registered.\r
352 @param CommBuffer A pointer to a collection of data in memory that will\r
353 be conveyed from a non-SMM environment into an SMM environment.\r
354 @param CommBufferSize The size of the CommBuffer.\r
355\r
356 @return Status Code\r
357\r
358**/\r
359EFI_STATUS\r
360EFIAPI\r
361SmmEndOfDxeHandler (\r
362 IN EFI_HANDLE DispatchHandle,\r
363 IN CONST VOID *Context, OPTIONAL\r
364 IN OUT VOID *CommBuffer, OPTIONAL\r
365 IN OUT UINTN *CommBufferSize OPTIONAL\r
366 )\r
367{\r
368 EFI_STATUS Status;\r
369 EFI_HANDLE SmmHandle;\r
370\r
371 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));\r
372 //\r
373 // Install SMM EndOfDxe protocol\r
374 //\r
375 SmmHandle = NULL;\r
376 Status = SmmInstallProtocolInterface (\r
377 &SmmHandle,\r
378 &gEfiSmmEndOfDxeProtocolGuid,\r
379 EFI_NATIVE_INTERFACE,\r
380 NULL\r
381 );\r
42e2ff2e 382 return Status;\r
46ece1ff
JY
383}\r
384\r
3b657538
SZ
385/**\r
386 Determine if two buffers overlap in memory.\r
387\r
388 @param[in] Buff1 Pointer to first buffer\r
389 @param[in] Size1 Size of Buff1\r
390 @param[in] Buff2 Pointer to second buffer\r
391 @param[in] Size2 Size of Buff2\r
392\r
393 @retval TRUE Buffers overlap in memory.\r
394 @retval FALSE Buffer doesn't overlap.\r
395\r
396**/\r
397BOOLEAN\r
398InternalIsBufferOverlapped (\r
399 IN UINT8 *Buff1,\r
400 IN UINTN Size1,\r
401 IN UINT8 *Buff2,\r
402 IN UINTN Size2\r
403 )\r
404{\r
405 //\r
406 // If buff1's end is less than the start of buff2, then it's ok.\r
407 // Also, if buff1's start is beyond buff2's end, then it's ok.\r
408 //\r
409 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {\r
410 return FALSE;\r
411 }\r
412\r
413 return TRUE;\r
414}\r
415\r
e42e9404 416/**\r
417 The main entry point to SMM Foundation.\r
418\r
419 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
420\r
421 @param SmmEntryContext Processor information and functionality\r
422 needed by SMM Foundation.\r
423\r
424**/\r
425VOID\r
426EFIAPI\r
427SmmEntryPoint (\r
428 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext\r
429)\r
430{\r
431 EFI_STATUS Status;\r
432 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
a25cb9f6 433 BOOLEAN InLegacyBoot;\r
3b657538 434 BOOLEAN IsOverlapped;\r
eaae7b33
JF
435 VOID *CommunicationBuffer;\r
436 UINTN BufferSize;\r
e42e9404 437\r
495797c5 438 PERF_START (NULL, "SMM", NULL, 0) ;\r
439\r
e42e9404 440 //\r
229fd9e7 441 // Update SMST with contents of the SmmEntryContext structure\r
e42e9404 442 //\r
229fd9e7
MK
443 gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp;\r
444 gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;\r
445 gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus;\r
446 gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize;\r
447 gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState;\r
e42e9404 448\r
495797c5 449 //\r
450 // Call platform hook before Smm Dispatch\r
451 //\r
452 PlatformHookBeforeSmmDispatch ();\r
453\r
e42e9404 454 //\r
455 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
456 //\r
a25cb9f6 457 InLegacyBoot = mInLegacyBoot;\r
458 if (!InLegacyBoot) {\r
e42e9404 459 //\r
9fa90bb4 460 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol\r
e42e9404 461 //\r
9fa90bb4 462 gSmmCorePrivate->InSmm = TRUE;\r
e42e9404 463\r
464 //\r
9fa90bb4 465 // Check to see if this is a Synchronous SMI sent through the SMM Communication \r
466 // Protocol or an Asynchronous SMI\r
e42e9404 467 //\r
eaae7b33
JF
468 CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;\r
469 BufferSize = gSmmCorePrivate->BufferSize;\r
470 if (CommunicationBuffer != NULL) {\r
9fa90bb4 471 //\r
472 // Synchronous SMI for SMM Core or request from Communicate protocol\r
473 //\r
3b657538 474 IsOverlapped = InternalIsBufferOverlapped (\r
eaae7b33
JF
475 (UINT8 *) CommunicationBuffer,\r
476 BufferSize,\r
3b657538
SZ
477 (UINT8 *) gSmmCorePrivate,\r
478 sizeof (*gSmmCorePrivate)\r
479 );\r
eaae7b33 480 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) {\r
3720ee6d 481 //\r
3b657538
SZ
482 // If CommunicationBuffer is not in valid address scope,\r
483 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,\r
484 // return EFI_INVALID_PARAMETER\r
3720ee6d
JF
485 //\r
486 gSmmCorePrivate->CommunicationBuffer = NULL;\r
487 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;\r
488 } else {\r
eaae7b33
JF
489 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;\r
490 BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d
JF
491 Status = SmiManage (\r
492 &CommunicateHeader->HeaderGuid, \r
493 NULL, \r
494 CommunicateHeader->Data, \r
eaae7b33 495 &BufferSize\r
3720ee6d
JF
496 );\r
497 //\r
498 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
499 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
500 //\r
eaae7b33 501 gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d
JF
502 gSmmCorePrivate->CommunicationBuffer = NULL;\r
503 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
504 }\r
9fa90bb4 505 }\r
e42e9404 506 }\r
9fa90bb4 507\r
508 //\r
509 // Process Asynchronous SMI sources\r
510 //\r
511 SmiManage (NULL, NULL, NULL, NULL);\r
495797c5 512 \r
513 //\r
514 // Call platform hook after Smm Dispatch\r
515 //\r
516 PlatformHookAfterSmmDispatch ();\r
e42e9404 517\r
518 //\r
9fa90bb4 519 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
e42e9404 520 //\r
a25cb9f6 521 if (!InLegacyBoot) {\r
9fa90bb4 522 //\r
523 // Clear the InSmm flag as we are going to leave SMM\r
524 //\r
525 gSmmCorePrivate->InSmm = FALSE;\r
526 }\r
495797c5 527\r
528 PERF_END (NULL, "SMM", NULL, 0) ;\r
e42e9404 529}\r
530\r
0b256fb1
JY
531/**\r
532 Install LoadedImage protocol for SMM Core.\r
533**/\r
534VOID\r
535SmmCoreInstallLoadedImage (\r
536 VOID\r
537 )\r
538{\r
539 EFI_STATUS Status;\r
540 EFI_HANDLE Handle;\r
541\r
542 //\r
543 // Allocate a Loaded Image Protocol in EfiBootServicesData\r
544 //\r
545 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);\r
546 ASSERT_EFI_ERROR (Status);\r
547\r
548 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
549 //\r
550 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
551 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
552 //\r
553 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
554 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
555 mSmmCoreLoadedImage->SystemTable = gST;\r
556\r
557 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
558 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
559 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
560 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;\r
561\r
562 //\r
563 // Create a new image handle in the UEFI handle database for the SMM Driver\r
564 //\r
565 Handle = NULL;\r
566 Status = gBS->InstallMultipleProtocolInterfaces (\r
567 &Handle,\r
568 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,\r
569 NULL\r
570 );\r
571 ASSERT_EFI_ERROR (Status);\r
572\r
285a682c
JY
573 //\r
574 // Allocate a Loaded Image Protocol in SMM\r
575 //\r
576 Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);\r
577 ASSERT_EFI_ERROR(Status);\r
578\r
579 ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));\r
580 //\r
581 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
582 //\r
583 mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
584 mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
585 mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
586 mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;\r
587\r
588 mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
589 mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
590 mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
591 mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
592\r
593 mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;\r
594 mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase;\r
595 mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);\r
596\r
597 //\r
598 // Create a new image handle in the SMM handle database for the SMM Driver\r
599 //\r
600 mSmmCoreDriverEntry->SmmImageHandle = NULL;\r
601 Status = SmmInstallProtocolInterface (\r
602 &mSmmCoreDriverEntry->SmmImageHandle,\r
603 &gEfiLoadedImageProtocolGuid,\r
604 EFI_NATIVE_INTERFACE,\r
605 &mSmmCoreDriverEntry->SmmLoadedImage\r
606 );\r
607 ASSERT_EFI_ERROR(Status);\r
608\r
0b256fb1
JY
609 return ;\r
610}\r
611\r
e42e9404 612/**\r
613 The Entry Point for SMM Core\r
614\r
615 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core \r
616 EntryPoint on the SMI vector.\r
617\r
618 Note: This function is called for both DXE invocation and SMRAM invocation.\r
619\r
620 @param ImageHandle The firmware allocated handle for the EFI image.\r
621 @param SystemTable A pointer to the EFI System Table.\r
622\r
623 @retval EFI_SUCCESS The entry point is executed successfully.\r
624 @retval Other Some error occurred when executing this entry point.\r
625\r
626**/\r
627EFI_STATUS\r
628EFIAPI\r
629SmmMain (\r
630 IN EFI_HANDLE ImageHandle,\r
631 IN EFI_SYSTEM_TABLE *SystemTable\r
632 )\r
633{\r
634 EFI_STATUS Status;\r
635 UINTN Index;\r
636\r
637 //\r
638 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.\r
639 //\r
640 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
641\r
642 //\r
643 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.\r
644 //\r
645 gSmmCorePrivate->Smst = &gSmmCoreSmst;\r
646 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
647 \r
648 //\r
842b1242
JY
649 // No need to initialize memory service.\r
650 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),\r
651 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.\r
e42e9404 652 //\r
e42e9404 653\r
84edd20b
SZ
654 SmramProfileInit ();\r
655\r
656 //\r
657 // Copy FullSmramRanges to SMRAM\r
658 //\r
c03beb76 659 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;\r
84edd20b
SZ
660 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
661 ASSERT (mFullSmramRanges != NULL);\r
c03beb76 662 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
84edd20b 663\r
e42e9404 664 //\r
665 // Register all SMI Handlers required by the SMM Core\r
666 //\r
667 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
668 Status = SmiHandlerRegister (\r
669 mSmmCoreSmiHandlers[Index].Handler,\r
670 mSmmCoreSmiHandlers[Index].HandlerType,\r
671 &mSmmCoreSmiHandlers[Index].DispatchHandle\r
672 );\r
673 ASSERT_EFI_ERROR (Status);\r
674 }\r
84edd20b
SZ
675\r
676 RegisterSmramProfileHandler ();\r
e524f680 677 SmramProfileInstallProtocol ();\r
84edd20b 678\r
0b256fb1
JY
679 SmmCoreInstallLoadedImage ();\r
680\r
285a682c
JY
681 SmmCoreInitializeMemoryAttributesTable ();\r
682\r
ca41f3f4
JY
683 SmmCoreInitializeSmiHandlerProfile ();\r
684\r
e42e9404 685 return EFI_SUCCESS;\r
686}\r