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