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