]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg PiSmmIpl: Use AllocateZeroPool() for FullSmramRanges
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
CommitLineData
e42e9404 1/** @file\r
2 SMM Core Main Entry Point\r
3\r
53ec4d7f 4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
e42e9404 5 This program and the accompanying materials are licensed and made available \r
6 under the terms and conditions of the BSD License which accompanies this \r
7 distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include "PiSmmCore.h"\r
16\r
17//\r
18// Physical pointer to private structure shared between SMM IPL and the SMM Core\r
19//\r
20SMM_CORE_PRIVATE_DATA *gSmmCorePrivate;\r
21\r
22//\r
23// SMM Core global variable for SMM System Table. Only accessed as a physical structure in SMRAM.\r
24//\r
25EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst = {\r
26 {\r
27 SMM_SMST_SIGNATURE,\r
28 EFI_SMM_SYSTEM_TABLE2_REVISION,\r
29 sizeof (gSmmCoreSmst.Hdr)\r
30 },\r
31 NULL, // SmmFirmwareVendor\r
32 0, // SmmFirmwareRevision\r
33 SmmInstallConfigurationTable,\r
34 {\r
35 {\r
36 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmMemRead\r
37 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmMemWrite\r
38 },\r
39 {\r
40 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmIoRead\r
41 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmIoWrite\r
42 }\r
43 },\r
44 SmmAllocatePool,\r
45 SmmFreePool,\r
46 SmmAllocatePages,\r
47 SmmFreePages,\r
48 NULL, // SmmStartupThisAp\r
49 0, // CurrentlyExecutingCpu\r
50 0, // NumberOfCpus\r
51 NULL, // CpuSaveStateSize\r
52 NULL, // CpuSaveState\r
53 0, // NumberOfTableEntries\r
54 NULL, // SmmConfigurationTable\r
55 SmmInstallProtocolInterface,\r
56 SmmUninstallProtocolInterface,\r
57 SmmHandleProtocol,\r
58 SmmRegisterProtocolNotify,\r
59 SmmLocateHandle,\r
60 SmmLocateProtocol,\r
61 SmiManage,\r
62 SmiHandlerRegister,\r
63 SmiHandlerUnRegister\r
64};\r
65\r
66//\r
67// Flag to determine if the platform has performed a legacy boot.\r
68// If this flag is TRUE, then the runtime code and runtime data associated with the \r
53ec4d7f 69// SMM IPL are converted to free memory, so the SMM Core must guarantee that is\r
e42e9404 70// does not touch of the code/data associated with the SMM IPL if this flag is TRUE.\r
71//\r
72BOOLEAN mInLegacyBoot = FALSE;\r
73\r
74//\r
75// Table of SMI Handlers that are registered by the SMM Core when it is initialized\r
76//\r
77SMM_CORE_SMI_HANDLERS mSmmCoreSmiHandlers[] = {\r
53ec4d7f
SZ
78 { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE },\r
79 { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, \r
80 { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE },\r
81 { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid, NULL, FALSE },\r
82 { SmmReadyToBootHandler, &gEfiEventReadyToBootGuid, NULL, FALSE },\r
dc157845 83 { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, TRUE },\r
53ec4d7f 84 { NULL, NULL, NULL, FALSE }\r
e42e9404 85};\r
86\r
84edd20b
SZ
87UINTN mFullSmramRangeCount;\r
88EFI_SMRAM_DESCRIPTOR *mFullSmramRanges;\r
89\r
e42e9404 90/**\r
91 Place holder function until all the SMM System Table Service are available.\r
92\r
93 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
94\r
95 @param Arg1 Undefined\r
96 @param Arg2 Undefined\r
97 @param Arg3 Undefined\r
98 @param Arg4 Undefined\r
99 @param Arg5 Undefined\r
100\r
101 @return EFI_NOT_AVAILABLE_YET\r
102\r
103**/\r
104EFI_STATUS\r
105EFIAPI\r
106SmmEfiNotAvailableYetArg5 (\r
107 UINTN Arg1,\r
108 UINTN Arg2,\r
109 UINTN Arg3,\r
110 UINTN Arg4,\r
111 UINTN Arg5\r
112 )\r
113{\r
114 //\r
115 // This function should never be executed. If it does, then the architectural protocols\r
116 // have not been designed correctly.\r
117 //\r
118 return EFI_NOT_AVAILABLE_YET;\r
119}\r
120\r
121/**\r
122 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM\r
123 Core uses this signal to know that a Legacy Boot has been performed and that \r
124 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can\r
125 not be accessed from SMM anymore since that structure is considered free memory by\r
53ec4d7f
SZ
126 a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM\r
127 driver that system enter legacy boot.\r
e42e9404 128\r
129 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
130 @param Context Points to an optional handler context which was specified when the handler was registered.\r
131 @param CommBuffer A pointer to a collection of data in memory that will\r
132 be conveyed from a non-SMM environment into an SMM environment.\r
133 @param CommBufferSize The size of the CommBuffer.\r
134\r
135 @return Status Code\r
136\r
137**/\r
138EFI_STATUS\r
139EFIAPI\r
140SmmLegacyBootHandler (\r
141 IN EFI_HANDLE DispatchHandle,\r
142 IN CONST VOID *Context, OPTIONAL\r
143 IN OUT VOID *CommBuffer, OPTIONAL\r
144 IN OUT UINTN *CommBufferSize OPTIONAL\r
145 )\r
146{\r
53ec4d7f
SZ
147 EFI_STATUS Status;\r
148 EFI_HANDLE SmmHandle;\r
149\r
150 //\r
151 // Install SMM Legacy Boot protocol.\r
152 //\r
153 SmmHandle = NULL;\r
154 Status = SmmInstallProtocolInterface (\r
155 &SmmHandle,\r
156 &gEdkiiSmmLegacyBootProtocolGuid,\r
157 EFI_NATIVE_INTERFACE,\r
158 NULL\r
159 );\r
160\r
e42e9404 161 mInLegacyBoot = TRUE;\r
53ec4d7f
SZ
162\r
163 SmiHandlerUnRegister (DispatchHandle);\r
164\r
165 return Status;\r
166}\r
167\r
168/**\r
169 Software SMI handler that is called when an Exit Boot Services event is signalled.\r
170 Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver\r
171 that system enter exit boot services.\r
172\r
173 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
174 @param Context Points to an optional handler context which was specified when the handler was registered.\r
175 @param CommBuffer A pointer to a collection of data in memory that will\r
176 be conveyed from a non-SMM environment into an SMM environment.\r
177 @param CommBufferSize The size of the CommBuffer.\r
178\r
179 @return Status Code\r
180\r
181**/\r
182EFI_STATUS\r
183EFIAPI\r
184SmmExitBootServicesHandler (\r
185 IN EFI_HANDLE DispatchHandle,\r
186 IN CONST VOID *Context, OPTIONAL\r
187 IN OUT VOID *CommBuffer, OPTIONAL\r
188 IN OUT UINTN *CommBufferSize OPTIONAL\r
189 )\r
190{\r
191 EFI_STATUS Status;\r
192 EFI_HANDLE SmmHandle;\r
193\r
194 //\r
195 // Install SMM Exit Boot Services protocol.\r
196 //\r
197 SmmHandle = NULL;\r
198 Status = SmmInstallProtocolInterface (\r
199 &SmmHandle,\r
200 &gEdkiiSmmExitBootServicesProtocolGuid,\r
201 EFI_NATIVE_INTERFACE,\r
202 NULL\r
203 );\r
204\r
205 SmiHandlerUnRegister (DispatchHandle);\r
206\r
207 return Status;\r
208}\r
209\r
210/**\r
211 Software SMI handler that is called when an Ready To Boot event is signalled.\r
212 Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver\r
213 that system enter ready to boot.\r
214\r
215 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
216 @param Context Points to an optional handler context which was specified when the handler was registered.\r
217 @param CommBuffer A pointer to a collection of data in memory that will\r
218 be conveyed from a non-SMM environment into an SMM environment.\r
219 @param CommBufferSize The size of the CommBuffer.\r
220\r
221 @return Status Code\r
222\r
223**/\r
224EFI_STATUS\r
225EFIAPI\r
226SmmReadyToBootHandler (\r
227 IN EFI_HANDLE DispatchHandle,\r
228 IN CONST VOID *Context, OPTIONAL\r
229 IN OUT VOID *CommBuffer, OPTIONAL\r
230 IN OUT UINTN *CommBufferSize OPTIONAL\r
231 )\r
232{\r
233 EFI_STATUS Status;\r
234 EFI_HANDLE SmmHandle;\r
235\r
236 //\r
237 // Install SMM Ready To Boot protocol.\r
238 //\r
239 SmmHandle = NULL;\r
240 Status = SmmInstallProtocolInterface (\r
241 &SmmHandle,\r
242 &gEdkiiSmmReadyToBootProtocolGuid,\r
243 EFI_NATIVE_INTERFACE,\r
244 NULL\r
245 );\r
246\r
247 SmiHandlerUnRegister (DispatchHandle);\r
248\r
249 return Status;\r
e42e9404 250}\r
251\r
252/**\r
253 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added\r
254 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the \r
255 Software SMIs that are nor required after SMRAM is locked and installs the \r
256 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about \r
257 to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed\r
258 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.\r
259\r
260 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
261 @param Context Points to an optional handler context which was specified when the handler was registered.\r
262 @param CommBuffer A pointer to a collection of data in memory that will\r
263 be conveyed from a non-SMM environment into an SMM environment.\r
264 @param CommBufferSize The size of the CommBuffer.\r
265\r
266 @return Status Code\r
267\r
268**/\r
269EFI_STATUS\r
270EFIAPI\r
271SmmReadyToLockHandler (\r
272 IN EFI_HANDLE DispatchHandle,\r
273 IN CONST VOID *Context, OPTIONAL\r
274 IN OUT VOID *CommBuffer, OPTIONAL\r
275 IN OUT UINTN *CommBufferSize OPTIONAL\r
276 )\r
277{\r
278 EFI_STATUS Status;\r
279 UINTN Index;\r
280 EFI_HANDLE SmmHandle;\r
281 VOID *Interface;\r
282\r
283 //\r
284 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped\r
285 //\r
286 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
287 if (mSmmCoreSmiHandlers[Index].UnRegister) {\r
288 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);\r
289 }\r
290 }\r
291\r
292 //\r
293 // Install SMM Ready to lock protocol\r
294 //\r
295 SmmHandle = NULL;\r
296 Status = SmmInstallProtocolInterface (\r
297 &SmmHandle,\r
298 &gEfiSmmReadyToLockProtocolGuid,\r
299 EFI_NATIVE_INTERFACE,\r
300 NULL\r
301 );\r
302\r
303 //\r
304 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database\r
305 //\r
306 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);\r
307\r
308 //\r
309 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed\r
310 //\r
311 DEBUG_CODE_BEGIN ();\r
312 if (EFI_ERROR (Status)) {\r
313 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));\r
314 }\r
315 DEBUG_CODE_END ();\r
316\r
317 //\r
318 // Assert if the CPU I/O 2 Protocol is not installed\r
319 //\r
320 ASSERT_EFI_ERROR (Status);\r
321\r
322 //\r
323 // Display any drivers that were not dispatched because dependency expression\r
324 // evaluated to false if this is a debug build\r
325 //\r
326 DEBUG_CODE_BEGIN ();\r
327 SmmDisplayDiscoveredNotDispatched ();\r
328 DEBUG_CODE_END ();\r
329\r
330 //\r
331 // Not allowed to use gST or gBS after lock\r
332 //\r
333 gST = NULL;\r
334 gBS = NULL;\r
335\r
84edd20b
SZ
336 SmramProfileReadyToLock ();\r
337\r
e42e9404 338 return Status;\r
339}\r
340\r
46ece1ff
JY
341/**\r
342 Software SMI handler that is called when the EndOfDxe event is signalled.\r
343 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that\r
344 platform code will invoke 3rd part code.\r
345\r
346 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
347 @param Context Points to an optional handler context which was specified when the handler was registered.\r
348 @param CommBuffer A pointer to a collection of data in memory that will\r
349 be conveyed from a non-SMM environment into an SMM environment.\r
350 @param CommBufferSize The size of the CommBuffer.\r
351\r
352 @return Status Code\r
353\r
354**/\r
355EFI_STATUS\r
356EFIAPI\r
357SmmEndOfDxeHandler (\r
358 IN EFI_HANDLE DispatchHandle,\r
359 IN CONST VOID *Context, OPTIONAL\r
360 IN OUT VOID *CommBuffer, OPTIONAL\r
361 IN OUT UINTN *CommBufferSize OPTIONAL\r
362 )\r
363{\r
364 EFI_STATUS Status;\r
365 EFI_HANDLE SmmHandle;\r
366\r
367 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));\r
368 //\r
369 // Install SMM EndOfDxe protocol\r
370 //\r
371 SmmHandle = NULL;\r
372 Status = SmmInstallProtocolInterface (\r
373 &SmmHandle,\r
374 &gEfiSmmEndOfDxeProtocolGuid,\r
375 EFI_NATIVE_INTERFACE,\r
376 NULL\r
377 );\r
42e2ff2e 378 return Status;\r
46ece1ff
JY
379}\r
380\r
e42e9404 381/**\r
382 The main entry point to SMM Foundation.\r
383\r
384 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
385\r
386 @param SmmEntryContext Processor information and functionality\r
387 needed by SMM Foundation.\r
388\r
389**/\r
390VOID\r
391EFIAPI\r
392SmmEntryPoint (\r
393 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext\r
394)\r
395{\r
396 EFI_STATUS Status;\r
397 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
a25cb9f6 398 BOOLEAN InLegacyBoot;\r
e42e9404 399\r
495797c5 400 PERF_START (NULL, "SMM", NULL, 0) ;\r
401\r
e42e9404 402 //\r
403 // Update SMST using the context\r
404 //\r
405 CopyMem (&gSmmCoreSmst.SmmStartupThisAp, SmmEntryContext, sizeof (EFI_SMM_ENTRY_CONTEXT));\r
406\r
495797c5 407 //\r
408 // Call platform hook before Smm Dispatch\r
409 //\r
410 PlatformHookBeforeSmmDispatch ();\r
411\r
e42e9404 412 //\r
413 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
414 //\r
a25cb9f6 415 InLegacyBoot = mInLegacyBoot;\r
416 if (!InLegacyBoot) {\r
e42e9404 417 //\r
9fa90bb4 418 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol\r
e42e9404 419 //\r
9fa90bb4 420 gSmmCorePrivate->InSmm = TRUE;\r
e42e9404 421\r
422 //\r
9fa90bb4 423 // Check to see if this is a Synchronous SMI sent through the SMM Communication \r
424 // Protocol or an Asynchronous SMI\r
e42e9404 425 //\r
9fa90bb4 426 if (gSmmCorePrivate->CommunicationBuffer != NULL) {\r
427 //\r
428 // Synchronous SMI for SMM Core or request from Communicate protocol\r
429 //\r
842b1242 430 if (!SmmIsBufferOutsideSmmValid ((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize)) {\r
3720ee6d
JF
431 //\r
432 // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER\r
433 //\r
434 gSmmCorePrivate->CommunicationBuffer = NULL;\r
435 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;\r
436 } else {\r
437 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;\r
438 gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
439 Status = SmiManage (\r
440 &CommunicateHeader->HeaderGuid, \r
441 NULL, \r
442 CommunicateHeader->Data, \r
443 &gSmmCorePrivate->BufferSize\r
444 );\r
445 //\r
446 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
447 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
448 //\r
449 gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
450 gSmmCorePrivate->CommunicationBuffer = NULL;\r
451 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
452 }\r
9fa90bb4 453 }\r
e42e9404 454 }\r
9fa90bb4 455\r
456 //\r
457 // Process Asynchronous SMI sources\r
458 //\r
459 SmiManage (NULL, NULL, NULL, NULL);\r
495797c5 460 \r
461 //\r
462 // Call platform hook after Smm Dispatch\r
463 //\r
464 PlatformHookAfterSmmDispatch ();\r
e42e9404 465\r
466 //\r
9fa90bb4 467 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
e42e9404 468 //\r
a25cb9f6 469 if (!InLegacyBoot) {\r
9fa90bb4 470 //\r
471 // Clear the InSmm flag as we are going to leave SMM\r
472 //\r
473 gSmmCorePrivate->InSmm = FALSE;\r
474 }\r
495797c5 475\r
476 PERF_END (NULL, "SMM", NULL, 0) ;\r
e42e9404 477}\r
478\r
479/**\r
480 The Entry Point for SMM Core\r
481\r
482 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core \r
483 EntryPoint on the SMI vector.\r
484\r
485 Note: This function is called for both DXE invocation and SMRAM invocation.\r
486\r
487 @param ImageHandle The firmware allocated handle for the EFI image.\r
488 @param SystemTable A pointer to the EFI System Table.\r
489\r
490 @retval EFI_SUCCESS The entry point is executed successfully.\r
491 @retval Other Some error occurred when executing this entry point.\r
492\r
493**/\r
494EFI_STATUS\r
495EFIAPI\r
496SmmMain (\r
497 IN EFI_HANDLE ImageHandle,\r
498 IN EFI_SYSTEM_TABLE *SystemTable\r
499 )\r
500{\r
501 EFI_STATUS Status;\r
502 UINTN Index;\r
503\r
504 //\r
505 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.\r
506 //\r
507 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
508\r
509 //\r
510 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.\r
511 //\r
512 gSmmCorePrivate->Smst = &gSmmCoreSmst;\r
513 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
514 \r
515 //\r
842b1242
JY
516 // No need to initialize memory service.\r
517 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),\r
518 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.\r
e42e9404 519 //\r
e42e9404 520\r
84edd20b
SZ
521 SmramProfileInit ();\r
522\r
523 //\r
524 // Copy FullSmramRanges to SMRAM\r
525 //\r
c03beb76 526 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;\r
84edd20b
SZ
527 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
528 ASSERT (mFullSmramRanges != NULL);\r
c03beb76 529 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
84edd20b 530\r
e42e9404 531 //\r
532 // Register all SMI Handlers required by the SMM Core\r
533 //\r
534 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
535 Status = SmiHandlerRegister (\r
536 mSmmCoreSmiHandlers[Index].Handler,\r
537 mSmmCoreSmiHandlers[Index].HandlerType,\r
538 &mSmmCoreSmiHandlers[Index].DispatchHandle\r
539 );\r
540 ASSERT_EFI_ERROR (Status);\r
541 }\r
84edd20b
SZ
542\r
543 RegisterSmramProfileHandler ();\r
544\r
e42e9404 545 return EFI_SUCCESS;\r
546}\r