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