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