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