]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg/PiSmmCore: Implement heap guard feature for SMM mode
[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
2930ef98
JW
509 //
510 // Call memory management hook function
511 //
512 SmmEntryPointMemoryManagementHook ();
513
e42e9404 514 //\r
515 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
516 //\r
a25cb9f6 517 InLegacyBoot = mInLegacyBoot;\r
518 if (!InLegacyBoot) {\r
e42e9404 519 //\r
9fa90bb4 520 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol\r
e42e9404 521 //\r
9fa90bb4 522 gSmmCorePrivate->InSmm = TRUE;\r
e42e9404 523\r
524 //\r
9fa90bb4 525 // Check to see if this is a Synchronous SMI sent through the SMM Communication \r
526 // Protocol or an Asynchronous SMI\r
e42e9404 527 //\r
eaae7b33
JF
528 CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;\r
529 BufferSize = gSmmCorePrivate->BufferSize;\r
530 if (CommunicationBuffer != NULL) {\r
9fa90bb4 531 //\r
532 // Synchronous SMI for SMM Core or request from Communicate protocol\r
533 //\r
3b657538 534 IsOverlapped = InternalIsBufferOverlapped (\r
eaae7b33
JF
535 (UINT8 *) CommunicationBuffer,\r
536 BufferSize,\r
3b657538
SZ
537 (UINT8 *) gSmmCorePrivate,\r
538 sizeof (*gSmmCorePrivate)\r
539 );\r
eaae7b33 540 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) {\r
3720ee6d 541 //\r
3b657538
SZ
542 // If CommunicationBuffer is not in valid address scope,\r
543 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,\r
544 // return EFI_INVALID_PARAMETER\r
3720ee6d
JF
545 //\r
546 gSmmCorePrivate->CommunicationBuffer = NULL;\r
547 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;\r
548 } else {\r
eaae7b33
JF
549 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;\r
550 BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d
JF
551 Status = SmiManage (\r
552 &CommunicateHeader->HeaderGuid, \r
553 NULL, \r
554 CommunicateHeader->Data, \r
eaae7b33 555 &BufferSize\r
3720ee6d
JF
556 );\r
557 //\r
558 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
559 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
560 //\r
eaae7b33 561 gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d
JF
562 gSmmCorePrivate->CommunicationBuffer = NULL;\r
563 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
564 }\r
9fa90bb4 565 }\r
e42e9404 566 }\r
9fa90bb4 567\r
568 //\r
569 // Process Asynchronous SMI sources\r
570 //\r
571 SmiManage (NULL, NULL, NULL, NULL);\r
495797c5 572 \r
573 //\r
574 // Call platform hook after Smm Dispatch\r
575 //\r
576 PlatformHookAfterSmmDispatch ();\r
e42e9404 577\r
578 //\r
9fa90bb4 579 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
e42e9404 580 //\r
a25cb9f6 581 if (!InLegacyBoot) {\r
9fa90bb4 582 //\r
583 // Clear the InSmm flag as we are going to leave SMM\r
584 //\r
585 gSmmCorePrivate->InSmm = FALSE;\r
586 }\r
495797c5 587\r
588 PERF_END (NULL, "SMM", NULL, 0) ;\r
e42e9404 589}\r
590\r
0b256fb1
JY
591/**\r
592 Install LoadedImage protocol for SMM Core.\r
593**/\r
594VOID\r
595SmmCoreInstallLoadedImage (\r
596 VOID\r
597 )\r
598{\r
599 EFI_STATUS Status;\r
600 EFI_HANDLE Handle;\r
601\r
602 //\r
603 // Allocate a Loaded Image Protocol in EfiBootServicesData\r
604 //\r
605 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);\r
606 ASSERT_EFI_ERROR (Status);\r
607\r
608 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
609 //\r
610 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
611 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
612 //\r
613 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
614 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
615 mSmmCoreLoadedImage->SystemTable = gST;\r
616\r
617 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
618 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
619 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
620 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;\r
621\r
622 //\r
623 // Create a new image handle in the UEFI handle database for the SMM Driver\r
624 //\r
625 Handle = NULL;\r
626 Status = gBS->InstallMultipleProtocolInterfaces (\r
627 &Handle,\r
628 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,\r
629 NULL\r
630 );\r
631 ASSERT_EFI_ERROR (Status);\r
632\r
285a682c
JY
633 //\r
634 // Allocate a Loaded Image Protocol in SMM\r
635 //\r
636 Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);\r
637 ASSERT_EFI_ERROR(Status);\r
638\r
639 ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));\r
640 //\r
641 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
642 //\r
643 mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
644 mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
645 mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
646 mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;\r
647\r
648 mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
649 mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
650 mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
651 mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
652\r
653 mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;\r
654 mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase;\r
655 mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);\r
656\r
657 //\r
658 // Create a new image handle in the SMM handle database for the SMM Driver\r
659 //\r
660 mSmmCoreDriverEntry->SmmImageHandle = NULL;\r
661 Status = SmmInstallProtocolInterface (\r
662 &mSmmCoreDriverEntry->SmmImageHandle,\r
663 &gEfiLoadedImageProtocolGuid,\r
664 EFI_NATIVE_INTERFACE,\r
665 &mSmmCoreDriverEntry->SmmLoadedImage\r
666 );\r
667 ASSERT_EFI_ERROR(Status);\r
668\r
0b256fb1
JY
669 return ;\r
670}\r
671\r
e42e9404 672/**\r
673 The Entry Point for SMM Core\r
674\r
675 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core \r
676 EntryPoint on the SMI vector.\r
677\r
678 Note: This function is called for both DXE invocation and SMRAM invocation.\r
679\r
680 @param ImageHandle The firmware allocated handle for the EFI image.\r
681 @param SystemTable A pointer to the EFI System Table.\r
682\r
683 @retval EFI_SUCCESS The entry point is executed successfully.\r
684 @retval Other Some error occurred when executing this entry point.\r
685\r
686**/\r
687EFI_STATUS\r
688EFIAPI\r
689SmmMain (\r
690 IN EFI_HANDLE ImageHandle,\r
691 IN EFI_SYSTEM_TABLE *SystemTable\r
692 )\r
693{\r
694 EFI_STATUS Status;\r
695 UINTN Index;\r
696\r
697 //\r
698 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.\r
699 //\r
700 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
701\r
702 //\r
703 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.\r
704 //\r
705 gSmmCorePrivate->Smst = &gSmmCoreSmst;\r
706 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
2930ef98 707
e42e9404 708 //\r
842b1242
JY
709 // No need to initialize memory service.\r
710 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),\r
711 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.\r
e42e9404 712 //\r
e42e9404 713\r
84edd20b
SZ
714 SmramProfileInit ();\r
715\r
716 //\r
717 // Copy FullSmramRanges to SMRAM\r
718 //\r
c03beb76 719 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;\r
84edd20b
SZ
720 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
721 ASSERT (mFullSmramRanges != NULL);\r
c03beb76 722 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
84edd20b 723\r
e42e9404 724 //\r
725 // Register all SMI Handlers required by the SMM Core\r
726 //\r
727 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
728 Status = SmiHandlerRegister (\r
729 mSmmCoreSmiHandlers[Index].Handler,\r
730 mSmmCoreSmiHandlers[Index].HandlerType,\r
731 &mSmmCoreSmiHandlers[Index].DispatchHandle\r
732 );\r
733 ASSERT_EFI_ERROR (Status);\r
734 }\r
84edd20b
SZ
735\r
736 RegisterSmramProfileHandler ();\r
e524f680 737 SmramProfileInstallProtocol ();\r
84edd20b 738\r
0b256fb1
JY
739 SmmCoreInstallLoadedImage ();\r
740\r
285a682c
JY
741 SmmCoreInitializeMemoryAttributesTable ();\r
742\r
ca41f3f4
JY
743 SmmCoreInitializeSmiHandlerProfile ();\r
744\r
e42e9404 745 return EFI_SUCCESS;\r
746}\r