]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
Fix potential memory leak issue.
[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 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include <PiSmm.h>
19
20 #include <IndustryStandard/Acpi50.h>
21
22 #include <Protocol/SmmReportStatusCodeHandler.h>
23 #include <Protocol/SmmAccess2.h>
24
25 #include <Guid/FirmwarePerformance.h>
26
27 #include <Library/SmmServicesTableLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/TimerLib.h>
31 #include <Library/LockBoxLib.h>
32 #include <Library/PcdLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/SynchronizationLib.h>
37
38 #define EXTENSION_RECORD_SIZE 0x1000
39
40 EFI_SMM_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;
41 UINT64 mSuspendStartTime = 0;
42 BOOLEAN mS3SuspendLockBoxSaved = FALSE;
43 UINT32 mBootRecordSize = 0;
44 UINT32 mBootRecordMaxSize = 0;
45 UINT8 *mBootRecordBuffer = NULL;
46
47 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
48 UINTN mSmramRangeCount;
49 SPIN_LOCK mSmmFpdtLock;
50 BOOLEAN mSmramIsOutOfResource = FALSE;
51
52 /**
53 Report status code listener for SMM. This is used to record the performance
54 data for S3 Suspend Start and S3 Suspend End in FPDT.
55
56 @param[in] CodeType Indicates the type of status code being reported.
57 @param[in] Value Describes the current status of a hardware or software entity.
58 This included information about the class and subclass that is used to
59 classify the entity as well as an operation.
60 @param[in] Instance The enumeration of a hardware or software entity within
61 the system. Valid instance numbers start with 1.
62 @param[in] CallerId This optional parameter may be used to identify the caller.
63 This parameter allows the status code driver to apply different rules to
64 different callers.
65 @param[in] Data This optional parameter may be used to pass additional data.
66
67 @retval EFI_SUCCESS Status code is what we expected.
68 @retval EFI_UNSUPPORTED Status code not supported.
69
70 **/
71 EFI_STATUS
72 EFIAPI
73 FpdtStatusCodeListenerSmm (
74 IN EFI_STATUS_CODE_TYPE CodeType,
75 IN EFI_STATUS_CODE_VALUE Value,
76 IN UINT32 Instance,
77 IN EFI_GUID *CallerId,
78 IN EFI_STATUS_CODE_DATA *Data
79 )
80 {
81 EFI_STATUS Status;
82 UINT64 CurrentTime;
83 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;
84 UINT8 *NewRecordBuffer;
85
86 //
87 // Check whether status code is what we are interested in.
88 //
89 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
90 return EFI_UNSUPPORTED;
91 }
92
93 //
94 // Collect one or more Boot records in boot time
95 //
96 if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
97 AcquireSpinLock (&mSmmFpdtLock);
98
99 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {
100 //
101 // Try to allocate big SMRAM data to store Boot record.
102 //
103 if (mSmramIsOutOfResource) {
104 ReleaseSpinLock (&mSmmFpdtLock);
105 return EFI_OUT_OF_RESOURCES;
106 }
107 NewRecordBuffer = ReallocatePool (mBootRecordSize, mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE, mBootRecordBuffer);
108 if (NewRecordBuffer == NULL) {
109 ReleaseSpinLock (&mSmmFpdtLock);
110 mSmramIsOutOfResource = TRUE;
111 return EFI_OUT_OF_RESOURCES;
112 }
113 mBootRecordBuffer = NewRecordBuffer;
114 mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;
115 }
116 //
117 // Save boot record into the temp memory space.
118 //
119 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);
120 mBootRecordSize += Data->Size;
121
122 ReleaseSpinLock (&mSmmFpdtLock);
123 return EFI_SUCCESS;
124 }
125
126 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&
127 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {
128 return EFI_UNSUPPORTED;
129 }
130
131 //
132 // Retrieve current time.
133 //
134 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
135
136 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {
137 //
138 // S3 Suspend started, record the performance data and return.
139 //
140 mSuspendStartTime = CurrentTime;
141 return EFI_SUCCESS;
142 }
143
144 //
145 // We are going to S3 sleep, record S3 Suspend End performance data.
146 //
147 S3SuspendRecord.SuspendStart = mSuspendStartTime;
148 S3SuspendRecord.SuspendEnd = CurrentTime;
149
150 //
151 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
152 //
153 if (!mS3SuspendLockBoxSaved) {
154 Status = SaveLockBox (
155 &gEfiFirmwarePerformanceGuid,
156 &S3SuspendRecord,
157 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
158 );
159 ASSERT_EFI_ERROR (Status);
160
161 mS3SuspendLockBoxSaved = TRUE;
162 } else {
163 Status = UpdateLockBox (
164 &gEfiFirmwarePerformanceGuid,
165 0,
166 &S3SuspendRecord,
167 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
168 );
169 ASSERT_EFI_ERROR (Status);
170 }
171
172 return EFI_SUCCESS;
173 }
174
175 /**
176 This function check if the address is in SMRAM.
177
178 @param Buffer the buffer address to be checked.
179 @param Length the buffer length to be checked.
180
181 @retval TRUE this address is in SMRAM.
182 @retval FALSE this address is NOT in SMRAM.
183 **/
184 BOOLEAN
185 InternalIsAddressInSmram (
186 IN EFI_PHYSICAL_ADDRESS Buffer,
187 IN UINT64 Length
188 )
189 {
190 UINTN Index;
191
192 for (Index = 0; Index < mSmramRangeCount; Index ++) {
193 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
194 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {
195 return TRUE;
196 }
197 }
198
199 return FALSE;
200 }
201
202 /**
203 Communication service SMI Handler entry.
204
205 This SMI handler provides services for report SMM boot records.
206
207 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
208 @param[in] RegisterContext Points to an optional handler context which was specified when the
209 handler was registered.
210 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
211 be conveyed from a non-SMM environment into an SMM environment.
212 @param[in, out] CommBufferSize The size of the CommBuffer.
213
214 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers should still be called.
215 @retval EFI_INVALID_PARAMETER The interrupt parameter is not valid.
216 @retval EFI_ACCESS_DENIED The interrupt buffer can't be written.
217 @retval EFI_UNSUPPORTED The interrupt is not supported.
218 **/
219 EFI_STATUS
220 EFIAPI
221 FpdtSmiHandler (
222 IN EFI_HANDLE DispatchHandle,
223 IN CONST VOID *RegisterContext,
224 IN OUT VOID *CommBuffer,
225 IN OUT UINTN *CommBufferSize
226 )
227 {
228 EFI_STATUS Status;
229 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
230
231 ASSERT (CommBuffer != NULL);
232 if (CommBuffer == NULL || *CommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {
233 return EFI_INVALID_PARAMETER;
234 }
235
236 Status = EFI_SUCCESS;
237 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;
238
239 switch (SmmCommData->Function) {
240 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :
241 SmmCommData->BootRecordSize = mBootRecordSize;
242 break;
243
244 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :
245 if (SmmCommData->BootRecordData == NULL || SmmCommData->BootRecordSize < mBootRecordSize) {
246 Status = EFI_INVALID_PARAMETER;
247 break;
248 }
249
250 //
251 // Sanity check
252 //
253 SmmCommData->BootRecordSize = mBootRecordSize;
254 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmCommData->BootRecordData, mBootRecordSize)) {
255 DEBUG ((EFI_D_ERROR, "Smm Data buffer is in SMRAM!\n"));
256 Status = EFI_ACCESS_DENIED;
257 break;
258 }
259
260 CopyMem (
261 (UINT8*)SmmCommData->BootRecordData,
262 mBootRecordBuffer,
263 mBootRecordSize
264 );
265 break;
266
267 default:
268 ASSERT (FALSE);
269 Status = EFI_UNSUPPORTED;
270 }
271
272 SmmCommData->ReturnStatus = Status;
273 return EFI_SUCCESS;
274 }
275
276 /**
277 The module Entry Point of the Firmware Performance Data Table SMM driver.
278
279 @param[in] ImageHandle The firmware allocated handle for the EFI image.
280 @param[in] SystemTable A pointer to the EFI System Table.
281
282 @retval EFI_SUCCESS The entry point is executed successfully.
283 @retval Other Some error occurs when executing this entry point.
284
285 **/
286 EFI_STATUS
287 EFIAPI
288 FirmwarePerformanceSmmEntryPoint (
289 IN EFI_HANDLE ImageHandle,
290 IN EFI_SYSTEM_TABLE *SystemTable
291 )
292 {
293 EFI_STATUS Status;
294 EFI_HANDLE Handle;
295 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
296 UINTN Size;
297
298 //
299 // Initialize spin lock
300 //
301 InitializeSpinLock (&mSmmFpdtLock);
302
303 //
304 // Get SMM Report Status Code Handler Protocol.
305 //
306 Status = gSmst->SmmLocateProtocol (
307 &gEfiSmmRscHandlerProtocolGuid,
308 NULL,
309 (VOID **) &mRscHandlerProtocol
310 );
311 ASSERT_EFI_ERROR (Status);
312
313 //
314 // Register report status code listener for BootRecords and S3 Suspend Start and End.
315 //
316 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);
317 ASSERT_EFI_ERROR (Status);
318
319 //
320 // Get SMRAM information
321 //
322 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
323 ASSERT_EFI_ERROR (Status);
324
325 Size = 0;
326 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
327 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
328
329 Status = gSmst->SmmAllocatePool (
330 EfiRuntimeServicesData,
331 Size,
332 (VOID **)&mSmramRanges
333 );
334 ASSERT_EFI_ERROR (Status);
335
336 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
337 ASSERT_EFI_ERROR (Status);
338
339 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
340
341 //
342 // Register SMI handler.
343 //
344 Handle = NULL;
345 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);
346 ASSERT_EFI_ERROR (Status);
347
348 return Status;
349 }