]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg/PiSmmCore: fix GCC build error
[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
299 EFI_SMM_SX_DISPATCH2_PROTOCOL *SxDispatch;\r
300 EFI_SMM_SX_REGISTER_CONTEXT EntryRegisterContext;\r
301 EFI_HANDLE S3EntryHandle;\r
53ec4d7f
SZ
302\r
303 //\r
304 // Install SMM Ready To Boot protocol.\r
305 //\r
306 SmmHandle = NULL;\r
307 Status = SmmInstallProtocolInterface (\r
308 &SmmHandle,\r
309 &gEdkiiSmmReadyToBootProtocolGuid,\r
310 EFI_NATIVE_INTERFACE,\r
311 NULL\r
312 );\r
313\r
314 SmiHandlerUnRegister (DispatchHandle);\r
315\r
7b9b55b2
SZ
316 //\r
317 // Locate SmmSxDispatch2 protocol.\r
318 //\r
319 Status = SmmLocateProtocol (\r
320 &gEfiSmmSxDispatch2ProtocolGuid,\r
321 NULL,\r
dfc5a4de 322 (VOID **)&SxDispatch\r
7b9b55b2
SZ
323 );\r
324 if (!EFI_ERROR (Status)) {\r
325 //\r
326 // Register a S3 entry callback function to\r
327 // determine if it will be during S3 resume.\r
328 //\r
329 EntryRegisterContext.Type = SxS3;\r
330 EntryRegisterContext.Phase = SxEntry;\r
331 Status = SxDispatch->Register (\r
332 SxDispatch,\r
333 SmmS3EntryCallBack,\r
334 &EntryRegisterContext,\r
335 &S3EntryHandle\r
336 );\r
337 ASSERT_EFI_ERROR (Status);\r
338 }\r
339\r
340 return EFI_SUCCESS;\r
e42e9404 341}\r
342\r
343/**\r
344 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added\r
345 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the \r
346 Software SMIs that are nor required after SMRAM is locked and installs the \r
347 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about \r
2048c585 348 to be locked. It also verifies the SMM CPU I/O 2 Protocol has been installed\r
e42e9404 349 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.\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
362SmmReadyToLockHandler (\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 UINTN Index;\r
371 EFI_HANDLE SmmHandle;\r
372 VOID *Interface;\r
373\r
374 //\r
375 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped\r
376 //\r
377 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
378 if (mSmmCoreSmiHandlers[Index].UnRegister) {\r
379 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);\r
380 }\r
381 }\r
382\r
383 //\r
384 // Install SMM Ready to lock protocol\r
385 //\r
386 SmmHandle = NULL;\r
387 Status = SmmInstallProtocolInterface (\r
388 &SmmHandle,\r
389 &gEfiSmmReadyToLockProtocolGuid,\r
390 EFI_NATIVE_INTERFACE,\r
391 NULL\r
392 );\r
393\r
394 //\r
395 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database\r
396 //\r
397 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);\r
398\r
399 //\r
400 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed\r
401 //\r
402 DEBUG_CODE_BEGIN ();\r
403 if (EFI_ERROR (Status)) {\r
404 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));\r
405 }\r
406 DEBUG_CODE_END ();\r
407\r
408 //\r
409 // Assert if the CPU I/O 2 Protocol is not installed\r
410 //\r
411 ASSERT_EFI_ERROR (Status);\r
412\r
413 //\r
414 // Display any drivers that were not dispatched because dependency expression\r
415 // evaluated to false if this is a debug build\r
416 //\r
417 DEBUG_CODE_BEGIN ();\r
418 SmmDisplayDiscoveredNotDispatched ();\r
419 DEBUG_CODE_END ();\r
420\r
421 //\r
422 // Not allowed to use gST or gBS after lock\r
423 //\r
424 gST = NULL;\r
425 gBS = NULL;\r
426\r
84edd20b
SZ
427 SmramProfileReadyToLock ();\r
428\r
e42e9404 429 return Status;\r
430}\r
431\r
46ece1ff
JY
432/**\r
433 Software SMI handler that is called when the EndOfDxe event is signalled.\r
434 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that\r
435 platform code will invoke 3rd part code.\r
436\r
437 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
438 @param Context Points to an optional handler context which was specified when the handler was registered.\r
439 @param CommBuffer A pointer to a collection of data in memory that will\r
440 be conveyed from a non-SMM environment into an SMM environment.\r
441 @param CommBufferSize The size of the CommBuffer.\r
442\r
443 @return Status Code\r
444\r
445**/\r
446EFI_STATUS\r
447EFIAPI\r
448SmmEndOfDxeHandler (\r
449 IN EFI_HANDLE DispatchHandle,\r
450 IN CONST VOID *Context, OPTIONAL\r
451 IN OUT VOID *CommBuffer, OPTIONAL\r
452 IN OUT UINTN *CommBufferSize OPTIONAL\r
453 )\r
454{\r
455 EFI_STATUS Status;\r
456 EFI_HANDLE SmmHandle;\r
457\r
458 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));\r
459 //\r
460 // Install SMM EndOfDxe protocol\r
461 //\r
462 SmmHandle = NULL;\r
463 Status = SmmInstallProtocolInterface (\r
464 &SmmHandle,\r
465 &gEfiSmmEndOfDxeProtocolGuid,\r
466 EFI_NATIVE_INTERFACE,\r
467 NULL\r
468 );\r
42e2ff2e 469 return Status;\r
46ece1ff
JY
470}\r
471\r
d76c2da8 472/**\r
a85e7127 473 Software SMI handler that is called when the EndOfS3Resume signal is triggered.\r
d76c2da8
ED
474 This function installs the SMM EndOfS3Resume Protocol so SMM Drivers are informed that\r
475 S3 resume has finished.\r
476\r
477 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
478 @param Context Points to an optional handler context which was specified when the handler was registered.\r
479 @param CommBuffer A pointer to a collection of data in memory that will\r
480 be conveyed from a non-SMM environment into an SMM environment.\r
481 @param CommBufferSize The size of the CommBuffer.\r
482\r
483 @return Status Code\r
484\r
485**/\r
486EFI_STATUS\r
487EFIAPI\r
488SmmEndOfS3ResumeHandler (\r
489 IN EFI_HANDLE DispatchHandle,\r
490 IN CONST VOID *Context, OPTIONAL\r
491 IN OUT VOID *CommBuffer, OPTIONAL\r
492 IN OUT UINTN *CommBufferSize OPTIONAL\r
493 )\r
494{\r
495 EFI_STATUS Status;\r
496 EFI_HANDLE SmmHandle;\r
497\r
7b9b55b2
SZ
498 DEBUG ((DEBUG_INFO, "SmmEndOfS3ResumeHandler\n"));\r
499\r
500 if (!mDuringS3Resume) {\r
501 DEBUG ((DEBUG_ERROR, "It is not during S3 resume\n"));\r
502 return EFI_SUCCESS;\r
503 }\r
d76c2da8
ED
504\r
505 //\r
506 // Install SMM EndOfS3Resume protocol\r
507 //\r
508 SmmHandle = NULL;\r
509 Status = SmmInstallProtocolInterface (\r
510 &SmmHandle,\r
a85e7127 511 &gEdkiiEndOfS3ResumeGuid,\r
d76c2da8
ED
512 EFI_NATIVE_INTERFACE,\r
513 NULL\r
514 );\r
515 ASSERT_EFI_ERROR (Status);\r
516\r
517 //\r
a85e7127 518 // Uninstall the protocol here because the comsumer just hook the\r
d76c2da8
ED
519 // installation event.\r
520 //\r
521 Status = SmmUninstallProtocolInterface (\r
522 SmmHandle,\r
a85e7127 523 &gEdkiiEndOfS3ResumeGuid,\r
d76c2da8
ED
524 NULL\r
525 );\r
526 ASSERT_EFI_ERROR (Status);\r
527\r
7b9b55b2
SZ
528 mDuringS3Resume = FALSE;\r
529\r
d76c2da8
ED
530 return Status;\r
531}\r
532\r
3b657538
SZ
533/**\r
534 Determine if two buffers overlap in memory.\r
535\r
536 @param[in] Buff1 Pointer to first buffer\r
537 @param[in] Size1 Size of Buff1\r
538 @param[in] Buff2 Pointer to second buffer\r
539 @param[in] Size2 Size of Buff2\r
540\r
541 @retval TRUE Buffers overlap in memory.\r
542 @retval FALSE Buffer doesn't overlap.\r
543\r
544**/\r
545BOOLEAN\r
546InternalIsBufferOverlapped (\r
547 IN UINT8 *Buff1,\r
548 IN UINTN Size1,\r
549 IN UINT8 *Buff2,\r
550 IN UINTN Size2\r
551 )\r
552{\r
553 //\r
554 // If buff1's end is less than the start of buff2, then it's ok.\r
555 // Also, if buff1's start is beyond buff2's end, then it's ok.\r
556 //\r
557 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {\r
558 return FALSE;\r
559 }\r
560\r
561 return TRUE;\r
562}\r
563\r
e42e9404 564/**\r
565 The main entry point to SMM Foundation.\r
566\r
567 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
568\r
569 @param SmmEntryContext Processor information and functionality\r
570 needed by SMM Foundation.\r
571\r
572**/\r
573VOID\r
574EFIAPI\r
575SmmEntryPoint (\r
576 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext\r
577)\r
578{\r
579 EFI_STATUS Status;\r
580 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
a25cb9f6 581 BOOLEAN InLegacyBoot;\r
3b657538 582 BOOLEAN IsOverlapped;\r
eaae7b33
JF
583 VOID *CommunicationBuffer;\r
584 UINTN BufferSize;\r
e42e9404 585\r
495797c5 586 PERF_START (NULL, "SMM", NULL, 0) ;\r
587\r
e42e9404 588 //\r
229fd9e7 589 // Update SMST with contents of the SmmEntryContext structure\r
e42e9404 590 //\r
229fd9e7
MK
591 gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp;\r
592 gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;\r
593 gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus;\r
594 gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize;\r
595 gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState;\r
e42e9404 596\r
495797c5 597 //\r
598 // Call platform hook before Smm Dispatch\r
599 //\r
600 PlatformHookBeforeSmmDispatch ();\r
601\r
e63da9f0
JW
602 //\r
603 // Call memory management hook function\r
604 //\r
605 SmmEntryPointMemoryManagementHook ();\r
606\r
e42e9404 607 //\r
608 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
609 //\r
a25cb9f6 610 InLegacyBoot = mInLegacyBoot;\r
611 if (!InLegacyBoot) {\r
e42e9404 612 //\r
9fa90bb4 613 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol\r
e42e9404 614 //\r
9fa90bb4 615 gSmmCorePrivate->InSmm = TRUE;\r
e42e9404 616\r
617 //\r
9fa90bb4 618 // Check to see if this is a Synchronous SMI sent through the SMM Communication \r
619 // Protocol or an Asynchronous SMI\r
e42e9404 620 //\r
eaae7b33
JF
621 CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;\r
622 BufferSize = gSmmCorePrivate->BufferSize;\r
623 if (CommunicationBuffer != NULL) {\r
9fa90bb4 624 //\r
625 // Synchronous SMI for SMM Core or request from Communicate protocol\r
626 //\r
3b657538 627 IsOverlapped = InternalIsBufferOverlapped (\r
eaae7b33
JF
628 (UINT8 *) CommunicationBuffer,\r
629 BufferSize,\r
3b657538
SZ
630 (UINT8 *) gSmmCorePrivate,\r
631 sizeof (*gSmmCorePrivate)\r
632 );\r
eaae7b33 633 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) {\r
3720ee6d 634 //\r
3b657538
SZ
635 // If CommunicationBuffer is not in valid address scope,\r
636 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,\r
637 // return EFI_INVALID_PARAMETER\r
3720ee6d
JF
638 //\r
639 gSmmCorePrivate->CommunicationBuffer = NULL;\r
d1632f69 640 gSmmCorePrivate->ReturnStatus = EFI_ACCESS_DENIED;\r
3720ee6d 641 } else {\r
eaae7b33
JF
642 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;\r
643 BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d
JF
644 Status = SmiManage (\r
645 &CommunicateHeader->HeaderGuid, \r
646 NULL, \r
647 CommunicateHeader->Data, \r
eaae7b33 648 &BufferSize\r
3720ee6d
JF
649 );\r
650 //\r
651 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
652 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
653 //\r
eaae7b33 654 gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
3720ee6d
JF
655 gSmmCorePrivate->CommunicationBuffer = NULL;\r
656 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
657 }\r
9fa90bb4 658 }\r
e42e9404 659 }\r
9fa90bb4 660\r
661 //\r
662 // Process Asynchronous SMI sources\r
663 //\r
664 SmiManage (NULL, NULL, NULL, NULL);\r
495797c5 665 \r
666 //\r
667 // Call platform hook after Smm Dispatch\r
668 //\r
669 PlatformHookAfterSmmDispatch ();\r
e42e9404 670\r
671 //\r
9fa90bb4 672 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
e42e9404 673 //\r
a25cb9f6 674 if (!InLegacyBoot) {\r
9fa90bb4 675 //\r
676 // Clear the InSmm flag as we are going to leave SMM\r
677 //\r
678 gSmmCorePrivate->InSmm = FALSE;\r
679 }\r
495797c5 680\r
681 PERF_END (NULL, "SMM", NULL, 0) ;\r
e42e9404 682}\r
683\r
0b256fb1
JY
684/**\r
685 Install LoadedImage protocol for SMM Core.\r
686**/\r
687VOID\r
688SmmCoreInstallLoadedImage (\r
689 VOID\r
690 )\r
691{\r
692 EFI_STATUS Status;\r
693 EFI_HANDLE Handle;\r
694\r
695 //\r
696 // Allocate a Loaded Image Protocol in EfiBootServicesData\r
697 //\r
698 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);\r
699 ASSERT_EFI_ERROR (Status);\r
700\r
701 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
702 //\r
703 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
704 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
705 //\r
706 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
707 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
708 mSmmCoreLoadedImage->SystemTable = gST;\r
709\r
710 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
711 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
712 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
713 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;\r
714\r
715 //\r
716 // Create a new image handle in the UEFI handle database for the SMM Driver\r
717 //\r
718 Handle = NULL;\r
719 Status = gBS->InstallMultipleProtocolInterfaces (\r
720 &Handle,\r
721 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,\r
722 NULL\r
723 );\r
724 ASSERT_EFI_ERROR (Status);\r
725\r
285a682c
JY
726 //\r
727 // Allocate a Loaded Image Protocol in SMM\r
728 //\r
729 Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);\r
730 ASSERT_EFI_ERROR(Status);\r
731\r
732 ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));\r
733 //\r
734 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
735 //\r
736 mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
737 mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
738 mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
739 mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;\r
740\r
741 mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
742 mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
743 mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
744 mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
745\r
746 mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;\r
747 mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase;\r
748 mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);\r
749\r
750 //\r
751 // Create a new image handle in the SMM handle database for the SMM Driver\r
752 //\r
753 mSmmCoreDriverEntry->SmmImageHandle = NULL;\r
754 Status = SmmInstallProtocolInterface (\r
755 &mSmmCoreDriverEntry->SmmImageHandle,\r
756 &gEfiLoadedImageProtocolGuid,\r
757 EFI_NATIVE_INTERFACE,\r
758 &mSmmCoreDriverEntry->SmmLoadedImage\r
759 );\r
760 ASSERT_EFI_ERROR(Status);\r
761\r
0b256fb1
JY
762 return ;\r
763}\r
764\r
e42e9404 765/**\r
766 The Entry Point for SMM Core\r
767\r
768 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core \r
769 EntryPoint on the SMI vector.\r
770\r
771 Note: This function is called for both DXE invocation and SMRAM invocation.\r
772\r
773 @param ImageHandle The firmware allocated handle for the EFI image.\r
774 @param SystemTable A pointer to the EFI System Table.\r
775\r
776 @retval EFI_SUCCESS The entry point is executed successfully.\r
777 @retval Other Some error occurred when executing this entry point.\r
778\r
779**/\r
780EFI_STATUS\r
781EFIAPI\r
782SmmMain (\r
783 IN EFI_HANDLE ImageHandle,\r
784 IN EFI_SYSTEM_TABLE *SystemTable\r
785 )\r
786{\r
787 EFI_STATUS Status;\r
788 UINTN Index;\r
789\r
790 //\r
791 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.\r
792 //\r
793 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
794\r
795 //\r
796 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.\r
797 //\r
798 gSmmCorePrivate->Smst = &gSmmCoreSmst;\r
799 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
e63da9f0 800\r
e42e9404 801 //\r
842b1242
JY
802 // No need to initialize memory service.\r
803 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),\r
804 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.\r
e42e9404 805 //\r
e42e9404 806\r
84edd20b
SZ
807 SmramProfileInit ();\r
808\r
809 //\r
810 // Copy FullSmramRanges to SMRAM\r
811 //\r
c03beb76 812 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;\r
84edd20b
SZ
813 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
814 ASSERT (mFullSmramRanges != NULL);\r
c03beb76 815 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
84edd20b 816\r
e42e9404 817 //\r
818 // Register all SMI Handlers required by the SMM Core\r
819 //\r
820 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
821 Status = SmiHandlerRegister (\r
822 mSmmCoreSmiHandlers[Index].Handler,\r
823 mSmmCoreSmiHandlers[Index].HandlerType,\r
824 &mSmmCoreSmiHandlers[Index].DispatchHandle\r
825 );\r
826 ASSERT_EFI_ERROR (Status);\r
827 }\r
84edd20b
SZ
828\r
829 RegisterSmramProfileHandler ();\r
e524f680 830 SmramProfileInstallProtocol ();\r
84edd20b 831\r
0b256fb1
JY
832 SmmCoreInstallLoadedImage ();\r
833\r
285a682c
JY
834 SmmCoreInitializeMemoryAttributesTable ();\r
835\r
ca41f3f4
JY
836 SmmCoreInitializeSmiHandlerProfile ();\r
837\r
e42e9404 838 return EFI_SUCCESS;\r
839}\r