]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg/S3SmmInitDone.h: Fix copyright coding style error.
[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
d1102dba
LG
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
e42e9404 9\r
d1102dba
LG
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
e42e9404 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
d1102dba 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 84 { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE },\r
d1102dba 85 { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE },\r
d76c2da8
ED
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
d1102dba 135 Core uses this signal to know that a Legacy Boot has been performed and that\r
e42e9404 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
d1102dba
LG
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
647 //\r
229fd9e7 648 // Update SMST with contents of the SmmEntryContext structure\r
e42e9404 649 //\r
229fd9e7
MK
650 gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp;\r
651 gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;\r
652 gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus;\r
653 gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize;\r
654 gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState;\r
e42e9404 655\r
495797c5 656 //\r
657 // Call platform hook before Smm Dispatch\r
658 //\r
659 PlatformHookBeforeSmmDispatch ();\r
660\r
e63da9f0
JW
661 //\r
662 // Call memory management hook function\r
663 //\r
664 SmmEntryPointMemoryManagementHook ();\r
665\r
e42e9404 666 //\r
667 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
668 //\r
a25cb9f6 669 InLegacyBoot = mInLegacyBoot;\r
670 if (!InLegacyBoot) {\r
e42e9404 671 //\r
9fa90bb4 672 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol\r
e42e9404 673 //\r
9fa90bb4 674 gSmmCorePrivate->InSmm = TRUE;\r
e42e9404 675\r
676 //\r
d1102dba 677 // Check to see if this is a Synchronous SMI sent through the SMM Communication\r
9fa90bb4 678 // Protocol or an Asynchronous SMI\r
e42e9404 679 //\r
eaae7b33
JF
680 CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;\r
681 BufferSize = gSmmCorePrivate->BufferSize;\r
682 if (CommunicationBuffer != NULL) {\r
9fa90bb4 683 //\r
684 // Synchronous SMI for SMM Core or request from Communicate protocol\r
685 //\r
3b657538 686 IsOverlapped = InternalIsBufferOverlapped (\r
eaae7b33
JF
687 (UINT8 *) CommunicationBuffer,\r
688 BufferSize,\r
3b657538
SZ
689 (UINT8 *) gSmmCorePrivate,\r
690 sizeof (*gSmmCorePrivate)\r
691 );\r
eaae7b33 692 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) {\r
3720ee6d 693 //\r
3b657538
SZ
694 // If CommunicationBuffer is not in valid address scope,\r
695 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,\r
696 // return EFI_INVALID_PARAMETER\r
3720ee6d
JF
697 //\r
698 gSmmCorePrivate->CommunicationBuffer = NULL;\r
d1632f69 699 gSmmCorePrivate->ReturnStatus = EFI_ACCESS_DENIED;\r
3720ee6d 700 } else {\r
eaae7b33
JF
701 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;\r
702 BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d 703 Status = SmiManage (\r
d1102dba
LG
704 &CommunicateHeader->HeaderGuid,\r
705 NULL,\r
706 CommunicateHeader->Data,\r
eaae7b33 707 &BufferSize\r
3720ee6d
JF
708 );\r
709 //\r
710 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
711 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
712 //\r
eaae7b33 713 gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d
JF
714 gSmmCorePrivate->CommunicationBuffer = NULL;\r
715 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
716 }\r
9fa90bb4 717 }\r
e42e9404 718 }\r
9fa90bb4 719\r
720 //\r
721 // Process Asynchronous SMI sources\r
722 //\r
723 SmiManage (NULL, NULL, NULL, NULL);\r
d1102dba 724\r
495797c5 725 //\r
726 // Call platform hook after Smm Dispatch\r
727 //\r
728 PlatformHookAfterSmmDispatch ();\r
e42e9404 729\r
730 //\r
9fa90bb4 731 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
e42e9404 732 //\r
a25cb9f6 733 if (!InLegacyBoot) {\r
9fa90bb4 734 //\r
735 // Clear the InSmm flag as we are going to leave SMM\r
736 //\r
737 gSmmCorePrivate->InSmm = FALSE;\r
738 }\r
e42e9404 739}\r
740\r
0b256fb1
JY
741/**\r
742 Install LoadedImage protocol for SMM Core.\r
743**/\r
744VOID\r
745SmmCoreInstallLoadedImage (\r
746 VOID\r
747 )\r
748{\r
749 EFI_STATUS Status;\r
750 EFI_HANDLE Handle;\r
751\r
752 //\r
753 // Allocate a Loaded Image Protocol in EfiBootServicesData\r
754 //\r
755 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);\r
756 ASSERT_EFI_ERROR (Status);\r
757\r
758 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
759 //\r
760 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
761 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
762 //\r
763 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
764 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
765 mSmmCoreLoadedImage->SystemTable = gST;\r
766\r
767 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
768 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
769 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
770 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;\r
771\r
772 //\r
773 // Create a new image handle in the UEFI handle database for the SMM Driver\r
774 //\r
775 Handle = NULL;\r
776 Status = gBS->InstallMultipleProtocolInterfaces (\r
777 &Handle,\r
778 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,\r
779 NULL\r
780 );\r
781 ASSERT_EFI_ERROR (Status);\r
782\r
285a682c
JY
783 //\r
784 // Allocate a Loaded Image Protocol in SMM\r
785 //\r
786 Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);\r
787 ASSERT_EFI_ERROR(Status);\r
788\r
789 ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));\r
790 //\r
791 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
792 //\r
793 mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
794 mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
795 mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
796 mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;\r
797\r
798 mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
799 mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
800 mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
801 mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
802\r
803 mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;\r
804 mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase;\r
805 mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);\r
806\r
807 //\r
808 // Create a new image handle in the SMM handle database for the SMM Driver\r
809 //\r
810 mSmmCoreDriverEntry->SmmImageHandle = NULL;\r
811 Status = SmmInstallProtocolInterface (\r
812 &mSmmCoreDriverEntry->SmmImageHandle,\r
813 &gEfiLoadedImageProtocolGuid,\r
814 EFI_NATIVE_INTERFACE,\r
815 &mSmmCoreDriverEntry->SmmLoadedImage\r
816 );\r
817 ASSERT_EFI_ERROR(Status);\r
818\r
0b256fb1
JY
819 return ;\r
820}\r
821\r
e42e9404 822/**\r
823 The Entry Point for SMM Core\r
824\r
d1102dba 825 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core\r
e42e9404 826 EntryPoint on the SMI vector.\r
827\r
828 Note: This function is called for both DXE invocation and SMRAM invocation.\r
829\r
830 @param ImageHandle The firmware allocated handle for the EFI image.\r
831 @param SystemTable A pointer to the EFI System Table.\r
832\r
833 @retval EFI_SUCCESS The entry point is executed successfully.\r
834 @retval Other Some error occurred when executing this entry point.\r
835\r
836**/\r
837EFI_STATUS\r
838EFIAPI\r
839SmmMain (\r
840 IN EFI_HANDLE ImageHandle,\r
841 IN EFI_SYSTEM_TABLE *SystemTable\r
842 )\r
843{\r
844 EFI_STATUS Status;\r
845 UINTN Index;\r
846\r
847 //\r
848 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.\r
849 //\r
850 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
851\r
852 //\r
853 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.\r
854 //\r
855 gSmmCorePrivate->Smst = &gSmmCoreSmst;\r
856 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
e63da9f0 857\r
e42e9404 858 //\r
842b1242
JY
859 // No need to initialize memory service.\r
860 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),\r
861 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.\r
e42e9404 862 //\r
e42e9404 863\r
84edd20b
SZ
864 SmramProfileInit ();\r
865\r
866 //\r
867 // Copy FullSmramRanges to SMRAM\r
868 //\r
c03beb76 869 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;\r
84edd20b
SZ
870 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
871 ASSERT (mFullSmramRanges != NULL);\r
c03beb76 872 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
84edd20b 873\r
e42e9404 874 //\r
875 // Register all SMI Handlers required by the SMM Core\r
876 //\r
877 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
878 Status = SmiHandlerRegister (\r
879 mSmmCoreSmiHandlers[Index].Handler,\r
880 mSmmCoreSmiHandlers[Index].HandlerType,\r
881 &mSmmCoreSmiHandlers[Index].DispatchHandle\r
882 );\r
883 ASSERT_EFI_ERROR (Status);\r
884 }\r
84edd20b
SZ
885\r
886 RegisterSmramProfileHandler ();\r
e524f680 887 SmramProfileInstallProtocol ();\r
84edd20b 888\r
0b256fb1
JY
889 SmmCoreInstallLoadedImage ();\r
890\r
285a682c
JY
891 SmmCoreInitializeMemoryAttributesTable ();\r
892\r
ca41f3f4
JY
893 SmmCoreInitializeSmiHandlerProfile ();\r
894\r
e42e9404 895 return EFI_SUCCESS;\r
896}\r