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