]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg: PiSmmCore: Remove confusing CopyMem() of SMM_ENTRY_CONTEXT
[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 Determine if two buffers overlap in memory.
383
384 @param[in] Buff1 Pointer to first buffer
385 @param[in] Size1 Size of Buff1
386 @param[in] Buff2 Pointer to second buffer
387 @param[in] Size2 Size of Buff2
388
389 @retval TRUE Buffers overlap in memory.
390 @retval FALSE Buffer doesn't overlap.
391
392 **/
393 BOOLEAN
394 InternalIsBufferOverlapped (
395 IN UINT8 *Buff1,
396 IN UINTN Size1,
397 IN UINT8 *Buff2,
398 IN UINTN Size2
399 )
400 {
401 //
402 // If buff1's end is less than the start of buff2, then it's ok.
403 // Also, if buff1's start is beyond buff2's end, then it's ok.
404 //
405 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {
406 return FALSE;
407 }
408
409 return TRUE;
410 }
411
412 /**
413 The main entry point to SMM Foundation.
414
415 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
416
417 @param SmmEntryContext Processor information and functionality
418 needed by SMM Foundation.
419
420 **/
421 VOID
422 EFIAPI
423 SmmEntryPoint (
424 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext
425 )
426 {
427 EFI_STATUS Status;
428 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
429 BOOLEAN InLegacyBoot;
430 BOOLEAN IsOverlapped;
431
432 PERF_START (NULL, "SMM", NULL, 0) ;
433
434 //
435 // Update SMST with contents of the SmmEntryContext structure
436 //
437 gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp;
438 gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;
439 gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus;
440 gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize;
441 gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState;
442
443 //
444 // Call platform hook before Smm Dispatch
445 //
446 PlatformHookBeforeSmmDispatch ();
447
448 //
449 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
450 //
451 InLegacyBoot = mInLegacyBoot;
452 if (!InLegacyBoot) {
453 //
454 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
455 //
456 gSmmCorePrivate->InSmm = TRUE;
457
458 //
459 // Check to see if this is a Synchronous SMI sent through the SMM Communication
460 // Protocol or an Asynchronous SMI
461 //
462 if (gSmmCorePrivate->CommunicationBuffer != NULL) {
463 //
464 // Synchronous SMI for SMM Core or request from Communicate protocol
465 //
466 IsOverlapped = InternalIsBufferOverlapped (
467 (UINT8 *) gSmmCorePrivate->CommunicationBuffer,
468 gSmmCorePrivate->BufferSize,
469 (UINT8 *) gSmmCorePrivate,
470 sizeof (*gSmmCorePrivate)
471 );
472 if (!SmmIsBufferOutsideSmmValid ((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize) || IsOverlapped) {
473 //
474 // If CommunicationBuffer is not in valid address scope,
475 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
476 // return EFI_INVALID_PARAMETER
477 //
478 gSmmCorePrivate->CommunicationBuffer = NULL;
479 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
480 } else {
481 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;
482 gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
483 Status = SmiManage (
484 &CommunicateHeader->HeaderGuid,
485 NULL,
486 CommunicateHeader->Data,
487 &gSmmCorePrivate->BufferSize
488 );
489 //
490 // Update CommunicationBuffer, BufferSize and ReturnStatus
491 // Communicate service finished, reset the pointer to CommBuffer to NULL
492 //
493 gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
494 gSmmCorePrivate->CommunicationBuffer = NULL;
495 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
496 }
497 }
498 }
499
500 //
501 // Process Asynchronous SMI sources
502 //
503 SmiManage (NULL, NULL, NULL, NULL);
504
505 //
506 // Call platform hook after Smm Dispatch
507 //
508 PlatformHookAfterSmmDispatch ();
509
510 //
511 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
512 //
513 if (!InLegacyBoot) {
514 //
515 // Clear the InSmm flag as we are going to leave SMM
516 //
517 gSmmCorePrivate->InSmm = FALSE;
518 }
519
520 PERF_END (NULL, "SMM", NULL, 0) ;
521 }
522
523 /**
524 The Entry Point for SMM Core
525
526 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
527 EntryPoint on the SMI vector.
528
529 Note: This function is called for both DXE invocation and SMRAM invocation.
530
531 @param ImageHandle The firmware allocated handle for the EFI image.
532 @param SystemTable A pointer to the EFI System Table.
533
534 @retval EFI_SUCCESS The entry point is executed successfully.
535 @retval Other Some error occurred when executing this entry point.
536
537 **/
538 EFI_STATUS
539 EFIAPI
540 SmmMain (
541 IN EFI_HANDLE ImageHandle,
542 IN EFI_SYSTEM_TABLE *SystemTable
543 )
544 {
545 EFI_STATUS Status;
546 UINTN Index;
547
548 //
549 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
550 //
551 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
552
553 //
554 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
555 //
556 gSmmCorePrivate->Smst = &gSmmCoreSmst;
557 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
558
559 //
560 // No need to initialize memory service.
561 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
562 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
563 //
564
565 SmramProfileInit ();
566
567 //
568 // Copy FullSmramRanges to SMRAM
569 //
570 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;
571 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
572 ASSERT (mFullSmramRanges != NULL);
573 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
574
575 //
576 // Register all SMI Handlers required by the SMM Core
577 //
578 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
579 Status = SmiHandlerRegister (
580 mSmmCoreSmiHandlers[Index].Handler,
581 mSmmCoreSmiHandlers[Index].HandlerType,
582 &mSmmCoreSmiHandlers[Index].DispatchHandle
583 );
584 ASSERT_EFI_ERROR (Status);
585 }
586
587 RegisterSmramProfileHandler ();
588
589 return EFI_SUCCESS;
590 }