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