]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
Add more SMRAM range check to 3 SMI handler.
[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 - 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
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 <IndustryStandard/Acpi50.h>
28
29 #include <Protocol/SmmReportStatusCodeHandler.h>
30 #include <Protocol/SmmAccess2.h>
31
32 #include <Guid/FirmwarePerformance.h>
33
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>
44
45 #define EXTENSION_RECORD_SIZE 0x1000
46
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;
53
54 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
55 UINTN mSmramRangeCount;
56 SPIN_LOCK mSmmFpdtLock;
57 BOOLEAN mSmramIsOutOfResource = FALSE;
58
59 /**
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.
62
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
71 different callers.
72 @param[in] Data This optional parameter may be used to pass additional data.
73
74 @retval EFI_SUCCESS Status code is what we expected.
75 @retval EFI_UNSUPPORTED Status code not supported.
76
77 **/
78 EFI_STATUS
79 EFIAPI
80 FpdtStatusCodeListenerSmm (
81 IN EFI_STATUS_CODE_TYPE CodeType,
82 IN EFI_STATUS_CODE_VALUE Value,
83 IN UINT32 Instance,
84 IN EFI_GUID *CallerId,
85 IN EFI_STATUS_CODE_DATA *Data
86 )
87 {
88 EFI_STATUS Status;
89 UINT64 CurrentTime;
90 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;
91 UINT8 *NewRecordBuffer;
92
93 //
94 // Check whether status code is what we are interested in.
95 //
96 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
97 return EFI_UNSUPPORTED;
98 }
99
100 //
101 // Collect one or more Boot records in boot time
102 //
103 if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
104 AcquireSpinLock (&mSmmFpdtLock);
105
106 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {
107 //
108 // Try to allocate big SMRAM data to store Boot record.
109 //
110 if (mSmramIsOutOfResource) {
111 ReleaseSpinLock (&mSmmFpdtLock);
112 return EFI_OUT_OF_RESOURCES;
113 }
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;
119 }
120 mBootRecordBuffer = NewRecordBuffer;
121 mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;
122 }
123 //
124 // Save boot record into the temp memory space.
125 //
126 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);
127 mBootRecordSize += Data->Size;
128
129 ReleaseSpinLock (&mSmmFpdtLock);
130 return EFI_SUCCESS;
131 }
132
133 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&
134 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {
135 return EFI_UNSUPPORTED;
136 }
137
138 //
139 // Retrieve current time.
140 //
141 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
142
143 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {
144 //
145 // S3 Suspend started, record the performance data and return.
146 //
147 mSuspendStartTime = CurrentTime;
148 return EFI_SUCCESS;
149 }
150
151 //
152 // We are going to S3 sleep, record S3 Suspend End performance data.
153 //
154 S3SuspendRecord.SuspendStart = mSuspendStartTime;
155 S3SuspendRecord.SuspendEnd = CurrentTime;
156
157 //
158 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
159 //
160 if (!mS3SuspendLockBoxSaved) {
161 Status = SaveLockBox (
162 &gEfiFirmwarePerformanceGuid,
163 &S3SuspendRecord,
164 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
165 );
166 ASSERT_EFI_ERROR (Status);
167
168 mS3SuspendLockBoxSaved = TRUE;
169 } else {
170 Status = UpdateLockBox (
171 &gEfiFirmwarePerformanceGuid,
172 0,
173 &S3SuspendRecord,
174 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
175 );
176 ASSERT_EFI_ERROR (Status);
177 }
178
179 return EFI_SUCCESS;
180 }
181
182 /**
183 This function check if the address is in SMRAM.
184
185 @param Buffer the buffer address to be checked.
186 @param Length the buffer length to be checked.
187
188 @retval TRUE this address is in SMRAM.
189 @retval FALSE this address is NOT in SMRAM.
190 **/
191 BOOLEAN
192 InternalIsAddressInSmram (
193 IN EFI_PHYSICAL_ADDRESS Buffer,
194 IN UINT64 Length
195 )
196 {
197 UINTN Index;
198
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))) {
202 return TRUE;
203 }
204 }
205
206 return FALSE;
207 }
208
209 /**
210 Communication service SMI Handler entry.
211
212 This SMI handler provides services for report SMM boot records.
213
214 Caution: This function may receive untrusted input.
215 Communicate buffer and buffer size are external input, so this function will do basic validation.
216
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.
223
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
227 still be called.
228 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
229 be called.
230 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
231
232 **/
233 EFI_STATUS
234 EFIAPI
235 FpdtSmiHandler (
236 IN EFI_HANDLE DispatchHandle,
237 IN CONST VOID *RegisterContext,
238 IN OUT VOID *CommBuffer,
239 IN OUT UINTN *CommBufferSize
240 )
241 {
242 EFI_STATUS Status;
243 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
244
245 //
246 // If input is invalid, stop processing this SMI
247 //
248 if (CommBuffer == NULL || CommBufferSize == NULL) {
249 return EFI_SUCCESS;
250 }
251
252 if(*CommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {
253 return EFI_SUCCESS;
254 }
255
256 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBuffer, *CommBufferSize)) {
257 DEBUG ((EFI_D_ERROR, "SMM communication data buffer is in SMRAM!\n"));
258 return EFI_SUCCESS;
259 }
260
261 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;
262
263 Status = EFI_SUCCESS;
264
265 switch (SmmCommData->Function) {
266 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :
267 SmmCommData->BootRecordSize = mBootRecordSize;
268 break;
269
270 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :
271 if (SmmCommData->BootRecordData == NULL || SmmCommData->BootRecordSize < mBootRecordSize) {
272 Status = EFI_INVALID_PARAMETER;
273 break;
274 }
275
276 //
277 // Sanity check
278 //
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;
283 break;
284 }
285
286 CopyMem (
287 (UINT8*)SmmCommData->BootRecordData,
288 mBootRecordBuffer,
289 mBootRecordSize
290 );
291 break;
292
293 default:
294 Status = EFI_UNSUPPORTED;
295 }
296
297 SmmCommData->ReturnStatus = Status;
298
299 return EFI_SUCCESS;
300 }
301
302 /**
303 The module Entry Point of the Firmware Performance Data Table SMM driver.
304
305 @param[in] ImageHandle The firmware allocated handle for the EFI image.
306 @param[in] SystemTable A pointer to the EFI System Table.
307
308 @retval EFI_SUCCESS The entry point is executed successfully.
309 @retval Other Some error occurs when executing this entry point.
310
311 **/
312 EFI_STATUS
313 EFIAPI
314 FirmwarePerformanceSmmEntryPoint (
315 IN EFI_HANDLE ImageHandle,
316 IN EFI_SYSTEM_TABLE *SystemTable
317 )
318 {
319 EFI_STATUS Status;
320 EFI_HANDLE Handle;
321 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
322 UINTN Size;
323
324 //
325 // Initialize spin lock
326 //
327 InitializeSpinLock (&mSmmFpdtLock);
328
329 //
330 // Get SMM Report Status Code Handler Protocol.
331 //
332 Status = gSmst->SmmLocateProtocol (
333 &gEfiSmmRscHandlerProtocolGuid,
334 NULL,
335 (VOID **) &mRscHandlerProtocol
336 );
337 ASSERT_EFI_ERROR (Status);
338
339 //
340 // Register report status code listener for BootRecords and S3 Suspend Start and End.
341 //
342 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);
343 ASSERT_EFI_ERROR (Status);
344
345 //
346 // Get SMRAM information
347 //
348 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
349 ASSERT_EFI_ERROR (Status);
350
351 Size = 0;
352 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
353 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
354
355 Status = gSmst->SmmAllocatePool (
356 EfiRuntimeServicesData,
357 Size,
358 (VOID **)&mSmramRanges
359 );
360 ASSERT_EFI_ERROR (Status);
361
362 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
363 ASSERT_EFI_ERROR (Status);
364
365 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
366
367 //
368 // Register SMI handler.
369 //
370 Handle = NULL;
371 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);
372 ASSERT_EFI_ERROR (Status);
373
374 return Status;
375 }