]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
Add SmmMemLib, which can be used by SMM driver or SMM core to check communication...
[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
9d00d20e 14 Copyright (c) 2011 - 2013, 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
9d00d20e
SZ
207/**\r
208 This function check if the address refered by Buffer and Length is valid.\r
209\r
210 @param Buffer the buffer address to be checked.\r
211 @param Length the buffer length to be checked.\r
212\r
213 @retval TRUE this address is valid.\r
214 @retval FALSE this address is NOT valid.\r
215**/\r
216BOOLEAN\r
217InternalIsAddressValid (\r
218 IN UINTN Buffer,\r
219 IN UINTN Length\r
220 )\r
221{\r
222 if (Buffer > (MAX_ADDRESS - Length)) {\r
223 //\r
224 // Overflow happen\r
225 //\r
226 return FALSE;\r
227 }\r
228 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {\r
229 return FALSE;\r
230 }\r
231 return TRUE;\r
232}\r
233\r
1c0cc375
LG
234/**\r
235 Communication service SMI Handler entry.\r
236\r
237 This SMI handler provides services for report SMM boot records. \r
238\r
ccd2f6b0 239 Caution: This function may receive untrusted input.\r
240 Communicate buffer and buffer size are external input, so this function will do basic validation.\r
241\r
1c0cc375
LG
242 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
243 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
244 handler was registered.\r
245 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
246 be conveyed from a non-SMM environment into an SMM environment.\r
247 @param[in, out] CommBufferSize The size of the CommBuffer.\r
248\r
ccd2f6b0 249 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r
250 should still be called.\r
251 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r
252 still be called.\r
253 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r
254 be called.\r
255 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
256\r
1c0cc375
LG
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260FpdtSmiHandler (\r
261 IN EFI_HANDLE DispatchHandle,\r
262 IN CONST VOID *RegisterContext,\r
263 IN OUT VOID *CommBuffer,\r
264 IN OUT UINTN *CommBufferSize\r
265 )\r
266{\r
267 EFI_STATUS Status;\r
268 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;\r
5e5bb2a9
SZ
269 UINTN BootRecordSize;\r
270 VOID *BootRecordData;\r
164a9b67 271 UINTN TempCommBufferSize;\r
ccd2f6b0 272\r
273 //\r
274 // If input is invalid, stop processing this SMI\r
275 //\r
276 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
277 return EFI_SUCCESS;\r
278 }\r
279\r
164a9b67
SZ
280 TempCommBufferSize = *CommBufferSize;\r
281\r
282 if(TempCommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {\r
ccd2f6b0 283 return EFI_SUCCESS;\r
284 }\r
1c0cc375 285 \r
164a9b67 286 if (!InternalIsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
5e5bb2a9 287 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data buffer in SMRAM or overflow!\n"));\r
ccd2f6b0 288 return EFI_SUCCESS;\r
1c0cc375
LG
289 }\r
290\r
1c0cc375
LG
291 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;\r
292\r
ccd2f6b0 293 Status = EFI_SUCCESS;\r
294\r
1c0cc375
LG
295 switch (SmmCommData->Function) {\r
296 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :\r
297 SmmCommData->BootRecordSize = mBootRecordSize;\r
298 break;\r
299\r
300 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :\r
5e5bb2a9
SZ
301 BootRecordData = SmmCommData->BootRecordData;\r
302 BootRecordSize = SmmCommData->BootRecordSize;\r
303 if (BootRecordData == NULL || BootRecordSize < mBootRecordSize) {\r
1c0cc375
LG
304 Status = EFI_INVALID_PARAMETER;\r
305 break;\r
306 } \r
ccd2f6b0 307\r
1c0cc375
LG
308 //\r
309 // Sanity check\r
310 //\r
311 SmmCommData->BootRecordSize = mBootRecordSize;\r
5e5bb2a9
SZ
312 if (!InternalIsAddressValid ((UINTN)BootRecordData, mBootRecordSize)) {\r
313 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM or overflow!\n"));\r
1c0cc375
LG
314 Status = EFI_ACCESS_DENIED;\r
315 break;\r
316 }\r
317\r
318 CopyMem (\r
5e5bb2a9 319 (UINT8*)BootRecordData, \r
1c0cc375
LG
320 mBootRecordBuffer, \r
321 mBootRecordSize\r
322 );\r
323 break;\r
324\r
325 default:\r
1c0cc375
LG
326 Status = EFI_UNSUPPORTED;\r
327 }\r
328\r
329 SmmCommData->ReturnStatus = Status;\r
ccd2f6b0 330 \r
1c0cc375
LG
331 return EFI_SUCCESS;\r
332}\r
333\r
0284e90c
LG
334/**\r
335 The module Entry Point of the Firmware Performance Data Table SMM driver.\r
336\r
337 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
338 @param[in] SystemTable A pointer to the EFI System Table.\r
339\r
340 @retval EFI_SUCCESS The entry point is executed successfully.\r
341 @retval Other Some error occurs when executing this entry point.\r
342\r
343**/\r
344EFI_STATUS\r
345EFIAPI\r
346FirmwarePerformanceSmmEntryPoint (\r
347 IN EFI_HANDLE ImageHandle,\r
348 IN EFI_SYSTEM_TABLE *SystemTable\r
349 )\r
350{\r
1c0cc375
LG
351 EFI_STATUS Status;\r
352 EFI_HANDLE Handle;\r
353 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
354 UINTN Size;\r
0284e90c 355\r
1c0cc375
LG
356 //\r
357 // Initialize spin lock\r
358 //\r
359 InitializeSpinLock (&mSmmFpdtLock); \r
360 \r
361 //\r
362 // Get SMM Report Status Code Handler Protocol.\r
363 //\r
364 Status = gSmst->SmmLocateProtocol (\r
365 &gEfiSmmRscHandlerProtocolGuid,\r
366 NULL,\r
367 (VOID **) &mRscHandlerProtocol\r
368 );\r
369 ASSERT_EFI_ERROR (Status);\r
0284e90c 370\r
1c0cc375
LG
371 //\r
372 // Register report status code listener for BootRecords and S3 Suspend Start and End.\r
373 //\r
374 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);\r
375 ASSERT_EFI_ERROR (Status);\r
0284e90c 376\r
1c0cc375
LG
377 //\r
378 // Get SMRAM information\r
379 //\r
380 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
381 ASSERT_EFI_ERROR (Status);\r
382\r
383 Size = 0;\r
384 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
385 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
386\r
387 Status = gSmst->SmmAllocatePool (\r
388 EfiRuntimeServicesData,\r
389 Size,\r
390 (VOID **)&mSmramRanges\r
391 );\r
392 ASSERT_EFI_ERROR (Status);\r
393\r
394 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
395 ASSERT_EFI_ERROR (Status);\r
396\r
397 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
398\r
399 //\r
400 // Register SMI handler.\r
401 //\r
402 Handle = NULL;\r
403 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);\r
404 ASSERT_EFI_ERROR (Status);\r
405\r
406 return Status;\r
0284e90c 407}\r