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 - 2012, 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 <IndustryStandard/Acpi50.h>
29 #include <Protocol/SmmReportStatusCodeHandler.h>
30 #include <Protocol/SmmAccess2.h>
32 #include <Guid/FirmwarePerformance.h>
34 #include <Library/SmmServicesTableLib.h>
35 #include <Library/BaseLib.h>
36 #include <Library/DebugLib.h>
37 #include <Library/TimerLib.h>
38 #include <Library/LockBoxLib.h>
39 #include <Library/PcdLib.h>
40 #include <Library/BaseMemoryLib.h>
41 #include <Library/MemoryAllocationLib.h>
42 #include <Library/UefiBootServicesTableLib.h>
43 #include <Library/SynchronizationLib.h>
45 #define EXTENSION_RECORD_SIZE 0x1000
47 EFI_SMM_RSC_HANDLER_PROTOCOL
*mRscHandlerProtocol
= NULL
;
48 UINT64 mSuspendStartTime
= 0;
49 BOOLEAN mS3SuspendLockBoxSaved
= FALSE
;
50 UINT32 mBootRecordSize
= 0;
51 UINT32 mBootRecordMaxSize
= 0;
52 UINT8
*mBootRecordBuffer
= NULL
;
54 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
55 UINTN mSmramRangeCount
;
56 SPIN_LOCK mSmmFpdtLock
;
57 BOOLEAN mSmramIsOutOfResource
= FALSE
;
60 Report status code listener for SMM. This is used to record the performance
61 data for S3 Suspend Start and S3 Suspend End in FPDT.
63 @param[in] CodeType Indicates the type of status code being reported.
64 @param[in] Value Describes the current status of a hardware or software entity.
65 This included information about the class and subclass that is used to
66 classify the entity as well as an operation.
67 @param[in] Instance The enumeration of a hardware or software entity within
68 the system. Valid instance numbers start with 1.
69 @param[in] CallerId This optional parameter may be used to identify the caller.
70 This parameter allows the status code driver to apply different rules to
72 @param[in] Data This optional parameter may be used to pass additional data.
74 @retval EFI_SUCCESS Status code is what we expected.
75 @retval EFI_UNSUPPORTED Status code not supported.
80 FpdtStatusCodeListenerSmm (
81 IN EFI_STATUS_CODE_TYPE CodeType
,
82 IN EFI_STATUS_CODE_VALUE Value
,
84 IN EFI_GUID
*CallerId
,
85 IN EFI_STATUS_CODE_DATA
*Data
90 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord
;
91 UINT8
*NewRecordBuffer
;
94 // Check whether status code is what we are interested in.
96 if ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) != EFI_PROGRESS_CODE
) {
97 return EFI_UNSUPPORTED
;
101 // Collect one or more Boot records in boot time
103 if (Data
!= NULL
&& CompareGuid (&Data
->Type
, &gEfiFirmwarePerformanceGuid
)) {
104 AcquireSpinLock (&mSmmFpdtLock
);
106 if (mBootRecordSize
+ Data
->Size
> mBootRecordMaxSize
) {
108 // Try to allocate big SMRAM data to store Boot record.
110 if (mSmramIsOutOfResource
) {
111 ReleaseSpinLock (&mSmmFpdtLock
);
112 return EFI_OUT_OF_RESOURCES
;
114 NewRecordBuffer
= ReallocatePool (mBootRecordSize
, mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
, mBootRecordBuffer
);
115 if (NewRecordBuffer
== NULL
) {
116 ReleaseSpinLock (&mSmmFpdtLock
);
117 mSmramIsOutOfResource
= TRUE
;
118 return EFI_OUT_OF_RESOURCES
;
120 mBootRecordBuffer
= NewRecordBuffer
;
121 mBootRecordMaxSize
= mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
;
124 // Save boot record into the temp memory space.
126 CopyMem (mBootRecordBuffer
+ mBootRecordSize
, Data
+ 1, Data
->Size
);
127 mBootRecordSize
+= Data
->Size
;
129 ReleaseSpinLock (&mSmmFpdtLock
);
133 if ((Value
!= PcdGet32 (PcdProgressCodeS3SuspendStart
)) &&
134 (Value
!= PcdGet32 (PcdProgressCodeS3SuspendEnd
))) {
135 return EFI_UNSUPPORTED
;
139 // Retrieve current time.
141 CurrentTime
= GetTimeInNanoSecond (GetPerformanceCounter ());
143 if (Value
== PcdGet32 (PcdProgressCodeS3SuspendStart
)) {
145 // S3 Suspend started, record the performance data and return.
147 mSuspendStartTime
= CurrentTime
;
152 // We are going to S3 sleep, record S3 Suspend End performance data.
154 S3SuspendRecord
.SuspendStart
= mSuspendStartTime
;
155 S3SuspendRecord
.SuspendEnd
= CurrentTime
;
158 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
160 if (!mS3SuspendLockBoxSaved
) {
161 Status
= SaveLockBox (
162 &gEfiFirmwarePerformanceGuid
,
164 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
166 ASSERT_EFI_ERROR (Status
);
168 mS3SuspendLockBoxSaved
= TRUE
;
170 Status
= UpdateLockBox (
171 &gEfiFirmwarePerformanceGuid
,
174 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
176 ASSERT_EFI_ERROR (Status
);
183 This function check if the address is in SMRAM.
185 @param Buffer the buffer address to be checked.
186 @param Length the buffer length to be checked.
188 @retval TRUE this address is in SMRAM.
189 @retval FALSE this address is NOT in SMRAM.
192 InternalIsAddressInSmram (
193 IN EFI_PHYSICAL_ADDRESS Buffer
,
199 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
200 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
201 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
210 Communication service SMI Handler entry.
212 This SMI handler provides services for report SMM boot records.
214 Caution: This function may receive untrusted input.
215 Communicate buffer and buffer size are external input, so this function will do basic validation.
217 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
218 @param[in] RegisterContext Points to an optional handler context which was specified when the
219 handler was registered.
220 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
221 be conveyed from a non-SMM environment into an SMM environment.
222 @param[in, out] CommBufferSize The size of the CommBuffer.
224 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
225 should still be called.
226 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
228 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
230 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
236 IN EFI_HANDLE DispatchHandle
,
237 IN CONST VOID
*RegisterContext
,
238 IN OUT VOID
*CommBuffer
,
239 IN OUT UINTN
*CommBufferSize
243 SMM_BOOT_RECORD_COMMUNICATE
*SmmCommData
;
246 // If input is invalid, stop processing this SMI
248 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
252 if(*CommBufferSize
< sizeof (SMM_BOOT_RECORD_COMMUNICATE
)) {
256 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CommBuffer
, *CommBufferSize
)) {
257 DEBUG ((EFI_D_ERROR
, "SMM communication data buffer is in SMRAM!\n"));
261 SmmCommData
= (SMM_BOOT_RECORD_COMMUNICATE
*)CommBuffer
;
263 Status
= EFI_SUCCESS
;
265 switch (SmmCommData
->Function
) {
266 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE
:
267 SmmCommData
->BootRecordSize
= mBootRecordSize
;
270 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA
:
271 if (SmmCommData
->BootRecordData
== NULL
|| SmmCommData
->BootRecordSize
< mBootRecordSize
) {
272 Status
= EFI_INVALID_PARAMETER
;
279 SmmCommData
->BootRecordSize
= mBootRecordSize
;
280 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)SmmCommData
->BootRecordData
, mBootRecordSize
)) {
281 DEBUG ((EFI_D_ERROR
, "SMM Data buffer is in SMRAM!\n"));
282 Status
= EFI_ACCESS_DENIED
;
287 (UINT8
*)SmmCommData
->BootRecordData
,
294 Status
= EFI_UNSUPPORTED
;
297 SmmCommData
->ReturnStatus
= Status
;
303 The module Entry Point of the Firmware Performance Data Table SMM driver.
305 @param[in] ImageHandle The firmware allocated handle for the EFI image.
306 @param[in] SystemTable A pointer to the EFI System Table.
308 @retval EFI_SUCCESS The entry point is executed successfully.
309 @retval Other Some error occurs when executing this entry point.
314 FirmwarePerformanceSmmEntryPoint (
315 IN EFI_HANDLE ImageHandle
,
316 IN EFI_SYSTEM_TABLE
*SystemTable
321 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
325 // Initialize spin lock
327 InitializeSpinLock (&mSmmFpdtLock
);
330 // Get SMM Report Status Code Handler Protocol.
332 Status
= gSmst
->SmmLocateProtocol (
333 &gEfiSmmRscHandlerProtocolGuid
,
335 (VOID
**) &mRscHandlerProtocol
337 ASSERT_EFI_ERROR (Status
);
340 // Register report status code listener for BootRecords and S3 Suspend Start and End.
342 Status
= mRscHandlerProtocol
->Register (FpdtStatusCodeListenerSmm
);
343 ASSERT_EFI_ERROR (Status
);
346 // Get SMRAM information
348 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
349 ASSERT_EFI_ERROR (Status
);
352 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
353 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
355 Status
= gSmst
->SmmAllocatePool (
356 EfiRuntimeServicesData
,
358 (VOID
**)&mSmramRanges
360 ASSERT_EFI_ERROR (Status
);
362 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
363 ASSERT_EFI_ERROR (Status
);
365 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
368 // Register SMI handler.
371 Status
= gSmst
->SmiHandlerRegister (FpdtSmiHandler
, &gEfiFirmwarePerformanceGuid
, &Handle
);
372 ASSERT_EFI_ERROR (Status
);