]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
MdeModulePkg: StatusCodeHandler: StatusCodeHandler driver in StandaloneMm
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / FirmwarePerformanceDataTableSmm / FirmwarePerformanceSmm.c
1 /** @file
2 This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.
3
4 This module registers report status code listener to collect performance data
5 for SMM driver boot records and S3 Suspend Performance Record.
6
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.
11
12 FpdtSmiHandler() will receive untrusted input and do basic validation.
13
14 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
15 SPDX-License-Identifier: BSD-2-Clause-Patent
16
17 **/
18
19 #include <PiSmm.h>
20
21 #include <Protocol/SmmReportStatusCodeHandler.h>
22
23 #include <Guid/FirmwarePerformance.h>
24
25 #include <Library/SmmServicesTableLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/TimerLib.h>
29 #include <Library/LockBoxLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/SynchronizationLib.h>
35 #include <Library/SmmMemLib.h>
36
37 SMM_BOOT_PERFORMANCE_TABLE *mSmmBootPerformanceTable = NULL;
38
39 EFI_SMM_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;
40 UINT64 mSuspendStartTime = 0;
41 BOOLEAN mS3SuspendLockBoxSaved = FALSE;
42 UINT32 mBootRecordSize = 0;
43 UINT8 *mBootRecordBuffer = NULL;
44
45 SPIN_LOCK mSmmFpdtLock;
46 BOOLEAN mSmramIsOutOfResource = FALSE;
47
48 /**
49 Report status code listener for SMM. This is used to record the performance
50 data for S3 Suspend Start and S3 Suspend End in FPDT.
51
52 @param[in] CodeType Indicates the type of status code being reported.
53 @param[in] Value Describes the current status of a hardware or software entity.
54 This included information about the class and subclass that is used to
55 classify the entity as well as an operation.
56 @param[in] Instance The enumeration of a hardware or software entity within
57 the system. Valid instance numbers start with 1.
58 @param[in] CallerId This optional parameter may be used to identify the caller.
59 This parameter allows the status code driver to apply different rules to
60 different callers.
61 @param[in] Data This optional parameter may be used to pass additional data.
62
63 @retval EFI_SUCCESS Status code is what we expected.
64 @retval EFI_UNSUPPORTED Status code not supported.
65
66 **/
67 EFI_STATUS
68 EFIAPI
69 FpdtStatusCodeListenerSmm (
70 IN EFI_STATUS_CODE_TYPE CodeType,
71 IN EFI_STATUS_CODE_VALUE Value,
72 IN UINT32 Instance,
73 IN EFI_GUID *CallerId,
74 IN EFI_STATUS_CODE_DATA *Data
75 )
76 {
77 EFI_STATUS Status;
78 UINT64 CurrentTime;
79 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;
80
81 //
82 // Check whether status code is what we are interested in.
83 //
84 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
85 return EFI_UNSUPPORTED;
86 }
87
88 //
89 // Collect one or more Boot records in boot time
90 //
91 if (Data != NULL && CompareGuid (&Data->Type, &gEdkiiFpdtExtendedFirmwarePerformanceGuid)) {
92 AcquireSpinLock (&mSmmFpdtLock);
93 //
94 // Get the boot performance data.
95 //
96 CopyMem (&mSmmBootPerformanceTable, Data + 1, Data->Size);
97 mBootRecordBuffer = ((UINT8 *) (mSmmBootPerformanceTable)) + sizeof (SMM_BOOT_PERFORMANCE_TABLE);
98
99 ReleaseSpinLock (&mSmmFpdtLock);
100 return EFI_SUCCESS;
101 }
102
103 if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
104 DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerSmm: Performance data reported through gEfiFirmwarePerformanceGuid will not be collected by FirmwarePerformanceDataTableSmm\n"));
105 return EFI_UNSUPPORTED;
106 }
107
108 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&
109 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {
110 return EFI_UNSUPPORTED;
111 }
112
113 //
114 // Retrieve current time.
115 //
116 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
117
118 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {
119 //
120 // S3 Suspend started, record the performance data and return.
121 //
122 mSuspendStartTime = CurrentTime;
123 return EFI_SUCCESS;
124 }
125
126 //
127 // We are going to S3 sleep, record S3 Suspend End performance data.
128 //
129 S3SuspendRecord.SuspendStart = mSuspendStartTime;
130 S3SuspendRecord.SuspendEnd = CurrentTime;
131
132 //
133 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
134 //
135 if (!mS3SuspendLockBoxSaved) {
136 Status = SaveLockBox (
137 &gEfiFirmwarePerformanceGuid,
138 &S3SuspendRecord,
139 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
140 );
141 ASSERT_EFI_ERROR (Status);
142
143 mS3SuspendLockBoxSaved = TRUE;
144 } else {
145 Status = UpdateLockBox (
146 &gEfiFirmwarePerformanceGuid,
147 0,
148 &S3SuspendRecord,
149 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
150 );
151 ASSERT_EFI_ERROR (Status);
152 }
153
154 return EFI_SUCCESS;
155 }
156
157 /**
158 Communication service SMI Handler entry.
159
160 This SMI handler provides services for report SMM boot records.
161
162 Caution: This function may receive untrusted input.
163 Communicate buffer and buffer size are external input, so this function will do basic validation.
164
165 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
166 @param[in] RegisterContext Points to an optional handler context which was specified when the
167 handler was registered.
168 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
169 be conveyed from a non-SMM environment into an SMM environment.
170 @param[in, out] CommBufferSize The size of the CommBuffer.
171
172 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
173 should still be called.
174 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
175 still be called.
176 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
177 be called.
178 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
179
180 **/
181 EFI_STATUS
182 EFIAPI
183 FpdtSmiHandler (
184 IN EFI_HANDLE DispatchHandle,
185 IN CONST VOID *RegisterContext,
186 IN OUT VOID *CommBuffer,
187 IN OUT UINTN *CommBufferSize
188 )
189 {
190 EFI_STATUS Status;
191 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
192 UINTN BootRecordOffset;
193 UINTN BootRecordSize;
194 VOID *BootRecordData;
195 UINTN TempCommBufferSize;
196
197 //
198 // If input is invalid, stop processing this SMI
199 //
200 if (CommBuffer == NULL || CommBufferSize == NULL) {
201 return EFI_SUCCESS;
202 }
203
204 TempCommBufferSize = *CommBufferSize;
205
206 if(TempCommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {
207 return EFI_SUCCESS;
208 }
209
210 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
211 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data buffer in SMRAM or overflow!\n"));
212 return EFI_SUCCESS;
213 }
214
215 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;
216
217 Status = EFI_SUCCESS;
218
219 switch (SmmCommData->Function) {
220 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :
221 if (mSmmBootPerformanceTable != NULL) {
222 mBootRecordSize = mSmmBootPerformanceTable->Header.Length - sizeof (SMM_BOOT_PERFORMANCE_TABLE);
223 }
224 SmmCommData->BootRecordSize = mBootRecordSize;
225 break;
226
227 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :
228 Status = EFI_UNSUPPORTED;
229 break;
230
231 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET :
232 BootRecordOffset = SmmCommData->BootRecordOffset;
233 BootRecordData = SmmCommData->BootRecordData;
234 BootRecordSize = SmmCommData->BootRecordSize;
235 if (BootRecordData == NULL || BootRecordOffset >= mBootRecordSize) {
236 Status = EFI_INVALID_PARAMETER;
237 break;
238 }
239
240 //
241 // Sanity check
242 //
243 if (BootRecordSize > mBootRecordSize - BootRecordOffset) {
244 BootRecordSize = mBootRecordSize - BootRecordOffset;
245 }
246 SmmCommData->BootRecordSize = BootRecordSize;
247 if (!SmmIsBufferOutsideSmmValid ((UINTN)BootRecordData, BootRecordSize)) {
248 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM or overflow!\n"));
249 Status = EFI_ACCESS_DENIED;
250 break;
251 }
252
253 CopyMem (
254 (UINT8*)BootRecordData,
255 mBootRecordBuffer + BootRecordOffset,
256 BootRecordSize
257 );
258 break;
259
260 default:
261 Status = EFI_UNSUPPORTED;
262 }
263
264 SmmCommData->ReturnStatus = Status;
265
266 return EFI_SUCCESS;
267 }
268
269 /**
270 The module Entry Point of the Firmware Performance Data Table SMM driver.
271
272 @param[in] ImageHandle The firmware allocated handle for the EFI image.
273 @param[in] SystemTable A pointer to the EFI System Table.
274
275 @retval EFI_SUCCESS The entry point is executed successfully.
276 @retval Other Some error occurs when executing this entry point.
277
278 **/
279 EFI_STATUS
280 EFIAPI
281 FirmwarePerformanceSmmEntryPoint (
282 IN EFI_HANDLE ImageHandle,
283 IN EFI_SYSTEM_TABLE *SystemTable
284 )
285 {
286 EFI_STATUS Status;
287 EFI_HANDLE Handle;
288
289 //
290 // Initialize spin lock
291 //
292 InitializeSpinLock (&mSmmFpdtLock);
293
294 //
295 // Get SMM Report Status Code Handler Protocol.
296 //
297 Status = gSmst->SmmLocateProtocol (
298 &gEfiSmmRscHandlerProtocolGuid,
299 NULL,
300 (VOID **) &mRscHandlerProtocol
301 );
302 ASSERT_EFI_ERROR (Status);
303
304 //
305 // Register report status code listener for BootRecords and S3 Suspend Start and End.
306 //
307 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);
308 ASSERT_EFI_ERROR (Status);
309
310 //
311 // Register SMI handler.
312 //
313 Handle = NULL;
314 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);
315 ASSERT_EFI_ERROR (Status);
316
317 return Status;
318 }