2 This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.
4 This module registers report status code listener to collect performance data
5 for SMM driver boot records and S3 Suspend Performance Record.
7 Caution: This module requires additional review when modified.
8 This driver will have external input - communicate buffer in SMM mode.
9 This external input must be validated carefully to avoid security issue like
10 buffer overflow, integer overflow.
12 FpdtSmiHandler() will receive untrusted input and do basic validation.
14 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
15 This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 #include <Protocol/SmmReportStatusCodeHandler.h>
28 #include <Protocol/SmmAccess2.h>
30 #include <Guid/FirmwarePerformance.h>
32 #include <Library/SmmServicesTableLib.h>
33 #include <Library/BaseLib.h>
34 #include <Library/DebugLib.h>
35 #include <Library/TimerLib.h>
36 #include <Library/LockBoxLib.h>
37 #include <Library/PcdLib.h>
38 #include <Library/BaseMemoryLib.h>
39 #include <Library/MemoryAllocationLib.h>
40 #include <Library/UefiBootServicesTableLib.h>
41 #include <Library/SynchronizationLib.h>
43 #define EXTENSION_RECORD_SIZE 0x1000
45 EFI_SMM_RSC_HANDLER_PROTOCOL
*mRscHandlerProtocol
= NULL
;
46 UINT64 mSuspendStartTime
= 0;
47 BOOLEAN mS3SuspendLockBoxSaved
= FALSE
;
48 UINT32 mBootRecordSize
= 0;
49 UINT32 mBootRecordMaxSize
= 0;
50 UINT8
*mBootRecordBuffer
= NULL
;
52 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
53 UINTN mSmramRangeCount
;
54 SPIN_LOCK mSmmFpdtLock
;
55 BOOLEAN mSmramIsOutOfResource
= FALSE
;
58 Report status code listener for SMM. This is used to record the performance
59 data for S3 Suspend Start and S3 Suspend End in FPDT.
61 @param[in] CodeType Indicates the type of status code being reported.
62 @param[in] Value Describes the current status of a hardware or software entity.
63 This included information about the class and subclass that is used to
64 classify the entity as well as an operation.
65 @param[in] Instance The enumeration of a hardware or software entity within
66 the system. Valid instance numbers start with 1.
67 @param[in] CallerId This optional parameter may be used to identify the caller.
68 This parameter allows the status code driver to apply different rules to
70 @param[in] Data This optional parameter may be used to pass additional data.
72 @retval EFI_SUCCESS Status code is what we expected.
73 @retval EFI_UNSUPPORTED Status code not supported.
78 FpdtStatusCodeListenerSmm (
79 IN EFI_STATUS_CODE_TYPE CodeType
,
80 IN EFI_STATUS_CODE_VALUE Value
,
82 IN EFI_GUID
*CallerId
,
83 IN EFI_STATUS_CODE_DATA
*Data
88 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord
;
89 UINT8
*NewRecordBuffer
;
92 // Check whether status code is what we are interested in.
94 if ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) != EFI_PROGRESS_CODE
) {
95 return EFI_UNSUPPORTED
;
99 // Collect one or more Boot records in boot time
101 if (Data
!= NULL
&& CompareGuid (&Data
->Type
, &gEfiFirmwarePerformanceGuid
)) {
102 AcquireSpinLock (&mSmmFpdtLock
);
104 if (mBootRecordSize
+ Data
->Size
> mBootRecordMaxSize
) {
106 // Try to allocate big SMRAM data to store Boot record.
108 if (mSmramIsOutOfResource
) {
109 ReleaseSpinLock (&mSmmFpdtLock
);
110 return EFI_OUT_OF_RESOURCES
;
112 NewRecordBuffer
= ReallocatePool (mBootRecordSize
, mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
, mBootRecordBuffer
);
113 if (NewRecordBuffer
== NULL
) {
114 ReleaseSpinLock (&mSmmFpdtLock
);
115 mSmramIsOutOfResource
= TRUE
;
116 return EFI_OUT_OF_RESOURCES
;
118 mBootRecordBuffer
= NewRecordBuffer
;
119 mBootRecordMaxSize
= mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
;
122 // Save boot record into the temp memory space.
124 CopyMem (mBootRecordBuffer
+ mBootRecordSize
, Data
+ 1, Data
->Size
);
125 mBootRecordSize
+= Data
->Size
;
127 ReleaseSpinLock (&mSmmFpdtLock
);
131 if ((Value
!= PcdGet32 (PcdProgressCodeS3SuspendStart
)) &&
132 (Value
!= PcdGet32 (PcdProgressCodeS3SuspendEnd
))) {
133 return EFI_UNSUPPORTED
;
137 // Retrieve current time.
139 CurrentTime
= GetTimeInNanoSecond (GetPerformanceCounter ());
141 if (Value
== PcdGet32 (PcdProgressCodeS3SuspendStart
)) {
143 // S3 Suspend started, record the performance data and return.
145 mSuspendStartTime
= CurrentTime
;
150 // We are going to S3 sleep, record S3 Suspend End performance data.
152 S3SuspendRecord
.SuspendStart
= mSuspendStartTime
;
153 S3SuspendRecord
.SuspendEnd
= CurrentTime
;
156 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
158 if (!mS3SuspendLockBoxSaved
) {
159 Status
= SaveLockBox (
160 &gEfiFirmwarePerformanceGuid
,
162 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
164 ASSERT_EFI_ERROR (Status
);
166 mS3SuspendLockBoxSaved
= TRUE
;
168 Status
= UpdateLockBox (
169 &gEfiFirmwarePerformanceGuid
,
172 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
174 ASSERT_EFI_ERROR (Status
);
181 This function check if the address is in SMRAM.
183 @param Buffer the buffer address to be checked.
184 @param Length the buffer length to be checked.
186 @retval TRUE this address is in SMRAM.
187 @retval FALSE this address is NOT in SMRAM.
190 InternalIsAddressInSmram (
191 IN EFI_PHYSICAL_ADDRESS Buffer
,
197 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
198 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
199 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
208 This function check if the address refered by Buffer and Length is valid.
210 @param Buffer the buffer address to be checked.
211 @param Length the buffer length to be checked.
213 @retval TRUE this address is valid.
214 @retval FALSE this address is NOT valid.
217 InternalIsAddressValid (
222 if (Buffer
> (MAX_ADDRESS
- Length
)) {
228 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)Buffer
, (UINT64
)Length
)) {
235 Communication service SMI Handler entry.
237 This SMI handler provides services for report SMM boot records.
239 Caution: This function may receive untrusted input.
240 Communicate buffer and buffer size are external input, so this function will do basic validation.
242 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
243 @param[in] RegisterContext Points to an optional handler context which was specified when the
244 handler was registered.
245 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
246 be conveyed from a non-SMM environment into an SMM environment.
247 @param[in, out] CommBufferSize The size of the CommBuffer.
249 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
250 should still be called.
251 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
253 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
255 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
261 IN EFI_HANDLE DispatchHandle
,
262 IN CONST VOID
*RegisterContext
,
263 IN OUT VOID
*CommBuffer
,
264 IN OUT UINTN
*CommBufferSize
268 SMM_BOOT_RECORD_COMMUNICATE
*SmmCommData
;
269 UINTN BootRecordSize
;
270 VOID
*BootRecordData
;
271 UINTN TempCommBufferSize
;
274 // If input is invalid, stop processing this SMI
276 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
280 TempCommBufferSize
= *CommBufferSize
;
282 if(TempCommBufferSize
< sizeof (SMM_BOOT_RECORD_COMMUNICATE
)) {
286 if (!InternalIsAddressValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
287 DEBUG ((EFI_D_ERROR
, "FpdtSmiHandler: SMM communication data buffer in SMRAM or overflow!\n"));
291 SmmCommData
= (SMM_BOOT_RECORD_COMMUNICATE
*)CommBuffer
;
293 Status
= EFI_SUCCESS
;
295 switch (SmmCommData
->Function
) {
296 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE
:
297 SmmCommData
->BootRecordSize
= mBootRecordSize
;
300 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA
:
301 BootRecordData
= SmmCommData
->BootRecordData
;
302 BootRecordSize
= SmmCommData
->BootRecordSize
;
303 if (BootRecordData
== NULL
|| BootRecordSize
< mBootRecordSize
) {
304 Status
= EFI_INVALID_PARAMETER
;
311 SmmCommData
->BootRecordSize
= mBootRecordSize
;
312 if (!InternalIsAddressValid ((UINTN
)BootRecordData
, mBootRecordSize
)) {
313 DEBUG ((EFI_D_ERROR
, "FpdtSmiHandler: SMM Data buffer in SMRAM or overflow!\n"));
314 Status
= EFI_ACCESS_DENIED
;
319 (UINT8
*)BootRecordData
,
326 Status
= EFI_UNSUPPORTED
;
329 SmmCommData
->ReturnStatus
= Status
;
335 The module Entry Point of the Firmware Performance Data Table SMM driver.
337 @param[in] ImageHandle The firmware allocated handle for the EFI image.
338 @param[in] SystemTable A pointer to the EFI System Table.
340 @retval EFI_SUCCESS The entry point is executed successfully.
341 @retval Other Some error occurs when executing this entry point.
346 FirmwarePerformanceSmmEntryPoint (
347 IN EFI_HANDLE ImageHandle
,
348 IN EFI_SYSTEM_TABLE
*SystemTable
353 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
357 // Initialize spin lock
359 InitializeSpinLock (&mSmmFpdtLock
);
362 // Get SMM Report Status Code Handler Protocol.
364 Status
= gSmst
->SmmLocateProtocol (
365 &gEfiSmmRscHandlerProtocolGuid
,
367 (VOID
**) &mRscHandlerProtocol
369 ASSERT_EFI_ERROR (Status
);
372 // Register report status code listener for BootRecords and S3 Suspend Start and End.
374 Status
= mRscHandlerProtocol
->Register (FpdtStatusCodeListenerSmm
);
375 ASSERT_EFI_ERROR (Status
);
378 // Get SMRAM information
380 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
381 ASSERT_EFI_ERROR (Status
);
384 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
385 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
387 Status
= gSmst
->SmmAllocatePool (
388 EfiRuntimeServicesData
,
390 (VOID
**)&mSmramRanges
392 ASSERT_EFI_ERROR (Status
);
394 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
395 ASSERT_EFI_ERROR (Status
);
397 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
400 // Register SMI handler.
403 Status
= gSmst
->SmiHandlerRegister (FpdtSmiHandler
, &gEfiFirmwarePerformanceGuid
, &Handle
);
404 ASSERT_EFI_ERROR (Status
);