]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg PiSmmCore: Prevent overlap for gSmmCorePrivate and CommBuffer
[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 using the context
436 //
437 CopyMem (&gSmmCoreSmst.SmmStartupThisAp, SmmEntryContext, sizeof (EFI_SMM_ENTRY_CONTEXT));
438
439 //
440 // Call platform hook before Smm Dispatch
441 //
442 PlatformHookBeforeSmmDispatch ();
443
444 //
445 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
446 //
447 InLegacyBoot = mInLegacyBoot;
448 if (!InLegacyBoot) {
449 //
450 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
451 //
452 gSmmCorePrivate->InSmm = TRUE;
453
454 //
455 // Check to see if this is a Synchronous SMI sent through the SMM Communication
456 // Protocol or an Asynchronous SMI
457 //
458 if (gSmmCorePrivate->CommunicationBuffer != NULL) {
459 //
460 // Synchronous SMI for SMM Core or request from Communicate protocol
461 //
462 IsOverlapped = InternalIsBufferOverlapped (
463 (UINT8 *) gSmmCorePrivate->CommunicationBuffer,
464 gSmmCorePrivate->BufferSize,
465 (UINT8 *) gSmmCorePrivate,
466 sizeof (*gSmmCorePrivate)
467 );
468 if (!SmmIsBufferOutsideSmmValid ((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize) || IsOverlapped) {
469 //
470 // If CommunicationBuffer is not in valid address scope,
471 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
472 // return EFI_INVALID_PARAMETER
473 //
474 gSmmCorePrivate->CommunicationBuffer = NULL;
475 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
476 } else {
477 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;
478 gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
479 Status = SmiManage (
480 &CommunicateHeader->HeaderGuid,
481 NULL,
482 CommunicateHeader->Data,
483 &gSmmCorePrivate->BufferSize
484 );
485 //
486 // Update CommunicationBuffer, BufferSize and ReturnStatus
487 // Communicate service finished, reset the pointer to CommBuffer to NULL
488 //
489 gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
490 gSmmCorePrivate->CommunicationBuffer = NULL;
491 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
492 }
493 }
494 }
495
496 //
497 // Process Asynchronous SMI sources
498 //
499 SmiManage (NULL, NULL, NULL, NULL);
500
501 //
502 // Call platform hook after Smm Dispatch
503 //
504 PlatformHookAfterSmmDispatch ();
505
506 //
507 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
508 //
509 if (!InLegacyBoot) {
510 //
511 // Clear the InSmm flag as we are going to leave SMM
512 //
513 gSmmCorePrivate->InSmm = FALSE;
514 }
515
516 PERF_END (NULL, "SMM", NULL, 0) ;
517 }
518
519 /**
520 The Entry Point for SMM Core
521
522 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
523 EntryPoint on the SMI vector.
524
525 Note: This function is called for both DXE invocation and SMRAM invocation.
526
527 @param ImageHandle The firmware allocated handle for the EFI image.
528 @param SystemTable A pointer to the EFI System Table.
529
530 @retval EFI_SUCCESS The entry point is executed successfully.
531 @retval Other Some error occurred when executing this entry point.
532
533 **/
534 EFI_STATUS
535 EFIAPI
536 SmmMain (
537 IN EFI_HANDLE ImageHandle,
538 IN EFI_SYSTEM_TABLE *SystemTable
539 )
540 {
541 EFI_STATUS Status;
542 UINTN Index;
543
544 //
545 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
546 //
547 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
548
549 //
550 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
551 //
552 gSmmCorePrivate->Smst = &gSmmCoreSmst;
553 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
554
555 //
556 // No need to initialize memory service.
557 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
558 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
559 //
560
561 SmramProfileInit ();
562
563 //
564 // Copy FullSmramRanges to SMRAM
565 //
566 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;
567 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
568 ASSERT (mFullSmramRanges != NULL);
569 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));
570
571 //
572 // Register all SMI Handlers required by the SMM Core
573 //
574 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
575 Status = SmiHandlerRegister (
576 mSmmCoreSmiHandlers[Index].Handler,
577 mSmmCoreSmiHandlers[Index].HandlerType,
578 &mSmmCoreSmiHandlers[Index].DispatchHandle
579 );
580 ASSERT_EFI_ERROR (Status);
581 }
582
583 RegisterSmramProfileHandler ();
584
585 return EFI_SUCCESS;
586 }