]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
Add SmmMemLib, which can be used by SMM driver or SMM core to check communication...
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
CommitLineData
e42e9404 1/** @file\r
2 SMM Core Main Entry Point\r
3\r
84edd20b 4 Copyright (c) 2009 - 2014, 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
69// SMM IPL are converted to free memory, so the SMM COre must guarantee that is\r
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
78 { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE },\r
d5c3d1be 79 { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, \r
e42e9404 80 { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE },\r
46ece1ff 81 { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, FALSE },\r
e42e9404 82 { NULL, NULL, NULL, FALSE }\r
83};\r
84\r
84edd20b
SZ
85UINTN mFullSmramRangeCount;\r
86EFI_SMRAM_DESCRIPTOR *mFullSmramRanges;\r
87\r
3720ee6d
JF
88//\r
89// Maximum support address used to check input CommunicationBuffer\r
90//\r
91UINTN mMaximumSupportAddress = 0;\r
92\r
e42e9404 93/**\r
94 Place holder function until all the SMM System Table Service are available.\r
95\r
96 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
97\r
98 @param Arg1 Undefined\r
99 @param Arg2 Undefined\r
100 @param Arg3 Undefined\r
101 @param Arg4 Undefined\r
102 @param Arg5 Undefined\r
103\r
104 @return EFI_NOT_AVAILABLE_YET\r
105\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109SmmEfiNotAvailableYetArg5 (\r
110 UINTN Arg1,\r
111 UINTN Arg2,\r
112 UINTN Arg3,\r
113 UINTN Arg4,\r
114 UINTN Arg5\r
115 )\r
116{\r
117 //\r
118 // This function should never be executed. If it does, then the architectural protocols\r
119 // have not been designed correctly.\r
120 //\r
121 return EFI_NOT_AVAILABLE_YET;\r
122}\r
123\r
124/**\r
125 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM\r
126 Core uses this signal to know that a Legacy Boot has been performed and that \r
127 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can\r
128 not be accessed from SMM anymore since that structure is considered free memory by\r
129 a legacy OS.\r
130\r
131 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
132 @param Context Points to an optional handler context which was specified when the handler was registered.\r
133 @param CommBuffer A pointer to a collection of data in memory that will\r
134 be conveyed from a non-SMM environment into an SMM environment.\r
135 @param CommBufferSize The size of the CommBuffer.\r
136\r
137 @return Status Code\r
138\r
139**/\r
140EFI_STATUS\r
141EFIAPI\r
142SmmLegacyBootHandler (\r
143 IN EFI_HANDLE DispatchHandle,\r
144 IN CONST VOID *Context, OPTIONAL\r
145 IN OUT VOID *CommBuffer, OPTIONAL\r
146 IN OUT UINTN *CommBufferSize OPTIONAL\r
147 )\r
148{\r
149 mInLegacyBoot = TRUE;\r
150 return EFI_SUCCESS;\r
151}\r
152\r
153/**\r
154 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added\r
155 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the \r
156 Software SMIs that are nor required after SMRAM is locked and installs the \r
157 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about \r
158 to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed\r
159 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.\r
160\r
161 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
162 @param Context Points to an optional handler context which was specified when the handler was registered.\r
163 @param CommBuffer A pointer to a collection of data in memory that will\r
164 be conveyed from a non-SMM environment into an SMM environment.\r
165 @param CommBufferSize The size of the CommBuffer.\r
166\r
167 @return Status Code\r
168\r
169**/\r
170EFI_STATUS\r
171EFIAPI\r
172SmmReadyToLockHandler (\r
173 IN EFI_HANDLE DispatchHandle,\r
174 IN CONST VOID *Context, OPTIONAL\r
175 IN OUT VOID *CommBuffer, OPTIONAL\r
176 IN OUT UINTN *CommBufferSize OPTIONAL\r
177 )\r
178{\r
179 EFI_STATUS Status;\r
180 UINTN Index;\r
181 EFI_HANDLE SmmHandle;\r
182 VOID *Interface;\r
183\r
184 //\r
185 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped\r
186 //\r
187 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
188 if (mSmmCoreSmiHandlers[Index].UnRegister) {\r
189 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);\r
190 }\r
191 }\r
192\r
193 //\r
194 // Install SMM Ready to lock protocol\r
195 //\r
196 SmmHandle = NULL;\r
197 Status = SmmInstallProtocolInterface (\r
198 &SmmHandle,\r
199 &gEfiSmmReadyToLockProtocolGuid,\r
200 EFI_NATIVE_INTERFACE,\r
201 NULL\r
202 );\r
203\r
204 //\r
205 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database\r
206 //\r
207 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);\r
208\r
209 //\r
210 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed\r
211 //\r
212 DEBUG_CODE_BEGIN ();\r
213 if (EFI_ERROR (Status)) {\r
214 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));\r
215 }\r
216 DEBUG_CODE_END ();\r
217\r
218 //\r
219 // Assert if the CPU I/O 2 Protocol is not installed\r
220 //\r
221 ASSERT_EFI_ERROR (Status);\r
222\r
223 //\r
224 // Display any drivers that were not dispatched because dependency expression\r
225 // evaluated to false if this is a debug build\r
226 //\r
227 DEBUG_CODE_BEGIN ();\r
228 SmmDisplayDiscoveredNotDispatched ();\r
229 DEBUG_CODE_END ();\r
230\r
231 //\r
232 // Not allowed to use gST or gBS after lock\r
233 //\r
234 gST = NULL;\r
235 gBS = NULL;\r
236\r
84edd20b
SZ
237 SmramProfileReadyToLock ();\r
238\r
e42e9404 239 return Status;\r
240}\r
241\r
46ece1ff
JY
242/**\r
243 Software SMI handler that is called when the EndOfDxe event is signalled.\r
244 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that\r
245 platform code will invoke 3rd part code.\r
246\r
247 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
248 @param Context Points to an optional handler context which was specified when the handler was registered.\r
249 @param 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 CommBufferSize The size of the CommBuffer.\r
252\r
253 @return Status Code\r
254\r
255**/\r
256EFI_STATUS\r
257EFIAPI\r
258SmmEndOfDxeHandler (\r
259 IN EFI_HANDLE DispatchHandle,\r
260 IN CONST VOID *Context, OPTIONAL\r
261 IN OUT VOID *CommBuffer, OPTIONAL\r
262 IN OUT UINTN *CommBufferSize OPTIONAL\r
263 )\r
264{\r
265 EFI_STATUS Status;\r
266 EFI_HANDLE SmmHandle;\r
267\r
268 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));\r
269 //\r
270 // Install SMM EndOfDxe protocol\r
271 //\r
272 SmmHandle = NULL;\r
273 Status = SmmInstallProtocolInterface (\r
274 &SmmHandle,\r
275 &gEfiSmmEndOfDxeProtocolGuid,\r
276 EFI_NATIVE_INTERFACE,\r
277 NULL\r
278 );\r
42e2ff2e 279 return Status;\r
46ece1ff
JY
280}\r
281\r
3720ee6d
JF
282/**\r
283 Caculate and save the maximum support address.\r
284\r
285**/\r
286VOID\r
287CaculateMaximumSupportAddress (\r
288 VOID\r
289 )\r
290{\r
291 VOID *Hob;\r
292 UINT32 RegEax;\r
293 UINT8 PhysicalAddressBits;\r
294\r
295 //\r
296 // Get physical address bits supported.\r
297 //\r
298 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);\r
299 if (Hob != NULL) {\r
300 PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;\r
301 } else {\r
302 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
303 if (RegEax >= 0x80000008) {\r
304 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
305 PhysicalAddressBits = (UINT8) RegEax;\r
306 } else {\r
307 PhysicalAddressBits = 36;\r
308 }\r
309 }\r
310 //\r
311 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.\r
312 //\r
313 ASSERT (PhysicalAddressBits <= 52);\r
314 if (PhysicalAddressBits > 48) {\r
315 PhysicalAddressBits = 48;\r
316 }\r
317 \r
318 //\r
319 // Save the maximum support address in one global variable \r
320 //\r
321 mMaximumSupportAddress = (UINTN) (LShiftU64 (1, PhysicalAddressBits) - 1);\r
322 DEBUG ((EFI_D_INFO, "mMaximumSupportAddress = 0x%lx\n", mMaximumSupportAddress));\r
323}\r
324\r
325/**\r
326 Check if input buffer is in valid address scope or not.\r
327\r
328 @param[in] Pointer Pointer to the input buffer.\r
329 @param[in] BufferSize Input buffer size in bytes.\r
330\r
331 @retval TRUE The input buffer is in valid address scope. \r
332 @retval FALSE The input buffer is not in valid address scope. \r
333\r
334**/\r
335BOOLEAN\r
336IsValidPointer (\r
337 IN VOID *Pointer,\r
338 IN UINTN BufferSize\r
339 )\r
340{\r
341 if ((UINTN) Pointer > mMaximumSupportAddress) {\r
342 return FALSE;\r
343 }\r
344\r
345 if (BufferSize > (mMaximumSupportAddress - (UINTN) Pointer)) {\r
346 return FALSE;\r
347 }\r
348\r
349 return TRUE;\r
350}\r
351\r
e42e9404 352/**\r
353 The main entry point to SMM Foundation.\r
354\r
355 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
356\r
357 @param SmmEntryContext Processor information and functionality\r
358 needed by SMM Foundation.\r
359\r
360**/\r
361VOID\r
362EFIAPI\r
363SmmEntryPoint (\r
364 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext\r
365)\r
366{\r
367 EFI_STATUS Status;\r
368 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
a25cb9f6 369 BOOLEAN InLegacyBoot;\r
e42e9404 370\r
495797c5 371 PERF_START (NULL, "SMM", NULL, 0) ;\r
372\r
e42e9404 373 //\r
374 // Update SMST using the context\r
375 //\r
376 CopyMem (&gSmmCoreSmst.SmmStartupThisAp, SmmEntryContext, sizeof (EFI_SMM_ENTRY_CONTEXT));\r
377\r
495797c5 378 //\r
379 // Call platform hook before Smm Dispatch\r
380 //\r
381 PlatformHookBeforeSmmDispatch ();\r
382\r
e42e9404 383 //\r
384 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
385 //\r
a25cb9f6 386 InLegacyBoot = mInLegacyBoot;\r
387 if (!InLegacyBoot) {\r
e42e9404 388 //\r
9fa90bb4 389 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol\r
e42e9404 390 //\r
9fa90bb4 391 gSmmCorePrivate->InSmm = TRUE;\r
e42e9404 392\r
393 //\r
9fa90bb4 394 // Check to see if this is a Synchronous SMI sent through the SMM Communication \r
395 // Protocol or an Asynchronous SMI\r
e42e9404 396 //\r
9fa90bb4 397 if (gSmmCorePrivate->CommunicationBuffer != NULL) {\r
398 //\r
399 // Synchronous SMI for SMM Core or request from Communicate protocol\r
400 //\r
3720ee6d
JF
401 if (!IsValidPointer (gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize)) {\r
402 //\r
403 // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER\r
404 //\r
405 gSmmCorePrivate->CommunicationBuffer = NULL;\r
406 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;\r
407 } else {\r
408 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;\r
409 gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
410 Status = SmiManage (\r
411 &CommunicateHeader->HeaderGuid, \r
412 NULL, \r
413 CommunicateHeader->Data, \r
414 &gSmmCorePrivate->BufferSize\r
415 );\r
416 //\r
417 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
418 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
419 //\r
420 gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
421 gSmmCorePrivate->CommunicationBuffer = NULL;\r
422 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
423 }\r
9fa90bb4 424 }\r
e42e9404 425 }\r
9fa90bb4 426\r
427 //\r
428 // Process Asynchronous SMI sources\r
429 //\r
430 SmiManage (NULL, NULL, NULL, NULL);\r
495797c5 431 \r
432 //\r
433 // Call platform hook after Smm Dispatch\r
434 //\r
435 PlatformHookAfterSmmDispatch ();\r
e42e9404 436\r
437 //\r
9fa90bb4 438 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
e42e9404 439 //\r
a25cb9f6 440 if (!InLegacyBoot) {\r
9fa90bb4 441 //\r
442 // Clear the InSmm flag as we are going to leave SMM\r
443 //\r
444 gSmmCorePrivate->InSmm = FALSE;\r
445 }\r
495797c5 446\r
447 PERF_END (NULL, "SMM", NULL, 0) ;\r
e42e9404 448}\r
449\r
450/**\r
451 The Entry Point for SMM Core\r
452\r
453 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core \r
454 EntryPoint on the SMI vector.\r
455\r
456 Note: This function is called for both DXE invocation and SMRAM invocation.\r
457\r
458 @param ImageHandle The firmware allocated handle for the EFI image.\r
459 @param SystemTable A pointer to the EFI System Table.\r
460\r
461 @retval EFI_SUCCESS The entry point is executed successfully.\r
462 @retval Other Some error occurred when executing this entry point.\r
463\r
464**/\r
465EFI_STATUS\r
466EFIAPI\r
467SmmMain (\r
468 IN EFI_HANDLE ImageHandle,\r
469 IN EFI_SYSTEM_TABLE *SystemTable\r
470 )\r
471{\r
472 EFI_STATUS Status;\r
473 UINTN Index;\r
474\r
475 //\r
476 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.\r
477 //\r
478 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
479\r
480 //\r
481 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.\r
482 //\r
483 gSmmCorePrivate->Smst = &gSmmCoreSmst;\r
484 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
485 \r
486 //\r
487 // Initialize memory service using free SMRAM\r
488 //\r
489 SmmInitializeMemoryServices (gSmmCorePrivate->SmramRangeCount, gSmmCorePrivate->SmramRanges);\r
490\r
84edd20b
SZ
491 SmramProfileInit ();\r
492\r
493 //\r
494 // Copy FullSmramRanges to SMRAM\r
495 //\r
496 mFullSmramRangeCount = gSmmCorePrivate->FullSmramRangeCount;\r
497 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
498 ASSERT (mFullSmramRanges != NULL);\r
499 CopyMem (mFullSmramRanges, gSmmCorePrivate->FullSmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
500\r
e42e9404 501 //\r
502 // Register all SMI Handlers required by the SMM Core\r
503 //\r
504 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
505 Status = SmiHandlerRegister (\r
506 mSmmCoreSmiHandlers[Index].Handler,\r
507 mSmmCoreSmiHandlers[Index].HandlerType,\r
508 &mSmmCoreSmiHandlers[Index].DispatchHandle\r
509 );\r
510 ASSERT_EFI_ERROR (Status);\r
511 }\r
84edd20b
SZ
512\r
513 RegisterSmramProfileHandler ();\r
514\r
3720ee6d
JF
515 //\r
516 // Caculate and save maximum support address used in SmmEntryPoint().\r
517 //\r
518 CaculateMaximumSupportAddress ();\r
519\r
e42e9404 520 return EFI_SUCCESS;\r
521}\r