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