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