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