]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
MdeModulePkg/FPDT: Add error message for unsupported case
[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 (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
110 DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerSmm: Performance data reported through gEfiFirmwarePerformanceGuid will not be collected by FirmwarePerformanceDataTableSmm\n"));
111 return EFI_UNSUPPORTED;
112 }
113
114 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&
115 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {
116 return EFI_UNSUPPORTED;
117 }
118
119 //
120 // Retrieve current time.
121 //
122 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
123
124 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {
125 //
126 // S3 Suspend started, record the performance data and return.
127 //
128 mSuspendStartTime = CurrentTime;
129 return EFI_SUCCESS;
130 }
131
132 //
133 // We are going to S3 sleep, record S3 Suspend End performance data.
134 //
135 S3SuspendRecord.SuspendStart = mSuspendStartTime;
136 S3SuspendRecord.SuspendEnd = CurrentTime;
137
138 //
139 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
140 //
141 if (!mS3SuspendLockBoxSaved) {
142 Status = SaveLockBox (
143 &gEfiFirmwarePerformanceGuid,
144 &S3SuspendRecord,
145 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
146 );
147 ASSERT_EFI_ERROR (Status);
148
149 mS3SuspendLockBoxSaved = TRUE;
150 } else {
151 Status = UpdateLockBox (
152 &gEfiFirmwarePerformanceGuid,
153 0,
154 &S3SuspendRecord,
155 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
156 );
157 ASSERT_EFI_ERROR (Status);
158 }
159
160 return EFI_SUCCESS;
161 }
162
163 /**
164 Communication service SMI Handler entry.
165
166 This SMI handler provides services for report SMM boot records.
167
168 Caution: This function may receive untrusted input.
169 Communicate buffer and buffer size are external input, so this function will do basic validation.
170
171 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
172 @param[in] RegisterContext Points to an optional handler context which was specified when the
173 handler was registered.
174 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
175 be conveyed from a non-SMM environment into an SMM environment.
176 @param[in, out] CommBufferSize The size of the CommBuffer.
177
178 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
179 should still be called.
180 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
181 still be called.
182 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
183 be called.
184 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
185
186 **/
187 EFI_STATUS
188 EFIAPI
189 FpdtSmiHandler (
190 IN EFI_HANDLE DispatchHandle,
191 IN CONST VOID *RegisterContext,
192 IN OUT VOID *CommBuffer,
193 IN OUT UINTN *CommBufferSize
194 )
195 {
196 EFI_STATUS Status;
197 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
198 UINTN BootRecordOffset;
199 UINTN BootRecordSize;
200 VOID *BootRecordData;
201 UINTN TempCommBufferSize;
202
203 //
204 // If input is invalid, stop processing this SMI
205 //
206 if (CommBuffer == NULL || CommBufferSize == NULL) {
207 return EFI_SUCCESS;
208 }
209
210 TempCommBufferSize = *CommBufferSize;
211
212 if(TempCommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {
213 return EFI_SUCCESS;
214 }
215
216 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
217 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data buffer in SMRAM or overflow!\n"));
218 return EFI_SUCCESS;
219 }
220
221 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;
222
223 Status = EFI_SUCCESS;
224
225 switch (SmmCommData->Function) {
226 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :
227 if (mSmmBootPerformanceTable != NULL) {
228 mBootRecordSize = mSmmBootPerformanceTable->Header.Length - sizeof (SMM_BOOT_PERFORMANCE_TABLE);
229 }
230 SmmCommData->BootRecordSize = mBootRecordSize;
231 break;
232
233 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :
234 Status = EFI_UNSUPPORTED;
235 break;
236
237 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET :
238 BootRecordOffset = SmmCommData->BootRecordOffset;
239 BootRecordData = SmmCommData->BootRecordData;
240 BootRecordSize = SmmCommData->BootRecordSize;
241 if (BootRecordData == NULL || BootRecordOffset >= mBootRecordSize) {
242 Status = EFI_INVALID_PARAMETER;
243 break;
244 }
245
246 //
247 // Sanity check
248 //
249 if (BootRecordSize > mBootRecordSize - BootRecordOffset) {
250 BootRecordSize = mBootRecordSize - BootRecordOffset;
251 }
252 SmmCommData->BootRecordSize = BootRecordSize;
253 if (!SmmIsBufferOutsideSmmValid ((UINTN)BootRecordData, BootRecordSize)) {
254 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM or overflow!\n"));
255 Status = EFI_ACCESS_DENIED;
256 break;
257 }
258
259 CopyMem (
260 (UINT8*)BootRecordData,
261 mBootRecordBuffer + BootRecordOffset,
262 BootRecordSize
263 );
264 break;
265
266 default:
267 Status = EFI_UNSUPPORTED;
268 }
269
270 SmmCommData->ReturnStatus = Status;
271
272 return EFI_SUCCESS;
273 }
274
275 /**
276 The module Entry Point of the Firmware Performance Data Table SMM driver.
277
278 @param[in] ImageHandle The firmware allocated handle for the EFI image.
279 @param[in] SystemTable A pointer to the EFI System Table.
280
281 @retval EFI_SUCCESS The entry point is executed successfully.
282 @retval Other Some error occurs when executing this entry point.
283
284 **/
285 EFI_STATUS
286 EFIAPI
287 FirmwarePerformanceSmmEntryPoint (
288 IN EFI_HANDLE ImageHandle,
289 IN EFI_SYSTEM_TABLE *SystemTable
290 )
291 {
292 EFI_STATUS Status;
293 EFI_HANDLE Handle;
294
295 //
296 // Initialize spin lock
297 //
298 InitializeSpinLock (&mSmmFpdtLock);
299
300 //
301 // Get SMM Report Status Code Handler Protocol.
302 //
303 Status = gSmst->SmmLocateProtocol (
304 &gEfiSmmRscHandlerProtocolGuid,
305 NULL,
306 (VOID **) &mRscHandlerProtocol
307 );
308 ASSERT_EFI_ERROR (Status);
309
310 //
311 // Register report status code listener for BootRecords and S3 Suspend Start and End.
312 //
313 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);
314 ASSERT_EFI_ERROR (Status);
315
316 //
317 // Register SMI handler.
318 //
319 Handle = NULL;
320 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);
321 ASSERT_EFI_ERROR (Status);
322
323 return Status;
324 }