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