MdeModulePkg/FPDT: Add error message for unsupported case
[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
d4ee449d
DB
109 if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {\r
110 DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerSmm: Performance data reported through gEfiFirmwarePerformanceGuid will not be collected by FirmwarePerformanceDataTableSmm\n"));\r
111 return EFI_UNSUPPORTED;\r
112 }\r
113\r
0284e90c
LG
114 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&\r
115 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {\r
116 return EFI_UNSUPPORTED;\r
117 }\r
118\r
119 //\r
120 // Retrieve current time.\r
121 //\r
122 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());\r
123\r
124 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {\r
125 //\r
126 // S3 Suspend started, record the performance data and return.\r
127 //\r
128 mSuspendStartTime = CurrentTime;\r
129 return EFI_SUCCESS;\r
130 }\r
131\r
132 //\r
133 // We are going to S3 sleep, record S3 Suspend End performance data.\r
134 //\r
135 S3SuspendRecord.SuspendStart = mSuspendStartTime;\r
136 S3SuspendRecord.SuspendEnd = CurrentTime;\r
137\r
138 //\r
139 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.\r
140 //\r
141 if (!mS3SuspendLockBoxSaved) {\r
142 Status = SaveLockBox (\r
143 &gEfiFirmwarePerformanceGuid,\r
144 &S3SuspendRecord,\r
145 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r
146 );\r
147 ASSERT_EFI_ERROR (Status);\r
148\r
149 mS3SuspendLockBoxSaved = TRUE;\r
150 } else {\r
151 Status = UpdateLockBox (\r
152 &gEfiFirmwarePerformanceGuid,\r
153 0,\r
154 &S3SuspendRecord,\r
155 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r
156 );\r
157 ASSERT_EFI_ERROR (Status);\r
158 }\r
159\r
160 return EFI_SUCCESS;\r
161}\r
162\r
1c0cc375
LG
163/**\r
164 Communication service SMI Handler entry.\r
165\r
166 This SMI handler provides services for report SMM boot records. \r
167\r
ccd2f6b0 168 Caution: This function may receive untrusted input.\r
169 Communicate buffer and buffer size are external input, so this function will do basic validation.\r
170\r
1c0cc375
LG
171 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
172 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
173 handler was registered.\r
174 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
175 be conveyed from a non-SMM environment into an SMM environment.\r
176 @param[in, out] CommBufferSize The size of the CommBuffer.\r
177\r
ccd2f6b0 178 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r
179 should still be called.\r
180 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r
181 still be called.\r
182 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r
183 be called.\r
184 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
185\r
1c0cc375
LG
186**/\r
187EFI_STATUS\r
188EFIAPI\r
189FpdtSmiHandler (\r
190 IN EFI_HANDLE DispatchHandle,\r
191 IN CONST VOID *RegisterContext,\r
192 IN OUT VOID *CommBuffer,\r
193 IN OUT UINTN *CommBufferSize\r
194 )\r
195{\r
196 EFI_STATUS Status;\r
197 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;\r
77a6e6c4 198 UINTN BootRecordOffset;\r
5e5bb2a9
SZ
199 UINTN BootRecordSize;\r
200 VOID *BootRecordData;\r
164a9b67 201 UINTN TempCommBufferSize;\r
ccd2f6b0 202\r
203 //\r
204 // If input is invalid, stop processing this SMI\r
205 //\r
206 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
207 return EFI_SUCCESS;\r
208 }\r
209\r
164a9b67
SZ
210 TempCommBufferSize = *CommBufferSize;\r
211\r
212 if(TempCommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {\r
ccd2f6b0 213 return EFI_SUCCESS;\r
214 }\r
1c0cc375 215 \r
842b1242 216 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
5e5bb2a9 217 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data buffer in SMRAM or overflow!\n"));\r
ccd2f6b0 218 return EFI_SUCCESS;\r
1c0cc375
LG
219 }\r
220\r
1c0cc375
LG
221 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;\r
222\r
ccd2f6b0 223 Status = EFI_SUCCESS;\r
224\r
1c0cc375
LG
225 switch (SmmCommData->Function) {\r
226 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :\r
718a937e
DB
227 if (mSmmBootPerformanceTable != NULL) {\r
228 mBootRecordSize = mSmmBootPerformanceTable->Header.Length - sizeof (SMM_BOOT_PERFORMANCE_TABLE);\r
229 }\r
77a6e6c4
LG
230 SmmCommData->BootRecordSize = mBootRecordSize;\r
231 break;\r
1c0cc375
LG
232\r
233 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :\r
77a6e6c4
LG
234 Status = EFI_UNSUPPORTED;\r
235 break;\r
236\r
237 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET :\r
238 BootRecordOffset = SmmCommData->BootRecordOffset;\r
239 BootRecordData = SmmCommData->BootRecordData;\r
240 BootRecordSize = SmmCommData->BootRecordSize;\r
241 if (BootRecordData == NULL || BootRecordOffset >= mBootRecordSize) {\r
242 Status = EFI_INVALID_PARAMETER;\r
243 break;\r
244 }\r
245 \r
246 //\r
247 // Sanity check\r
248 //\r
249 if (BootRecordSize > mBootRecordSize - BootRecordOffset) {\r
250 BootRecordSize = mBootRecordSize - BootRecordOffset;\r
251 }\r
252 SmmCommData->BootRecordSize = BootRecordSize;\r
253 if (!SmmIsBufferOutsideSmmValid ((UINTN)BootRecordData, BootRecordSize)) {\r
254 DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM or overflow!\n"));\r
255 Status = EFI_ACCESS_DENIED;\r
256 break;\r
257 }\r
258 \r
259 CopyMem (\r
260 (UINT8*)BootRecordData, \r
261 mBootRecordBuffer + BootRecordOffset, \r
262 BootRecordSize\r
263 );\r
264 break;\r
1c0cc375
LG
265\r
266 default:\r
77a6e6c4 267 Status = EFI_UNSUPPORTED;\r
1c0cc375
LG
268 }\r
269\r
270 SmmCommData->ReturnStatus = Status;\r
ccd2f6b0 271 \r
1c0cc375
LG
272 return EFI_SUCCESS;\r
273}\r
274\r
0284e90c
LG
275/**\r
276 The module Entry Point of the Firmware Performance Data Table SMM driver.\r
277\r
278 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
279 @param[in] SystemTable A pointer to the EFI System Table.\r
280\r
281 @retval EFI_SUCCESS The entry point is executed successfully.\r
282 @retval Other Some error occurs when executing this entry point.\r
283\r
284**/\r
285EFI_STATUS\r
286EFIAPI\r
287FirmwarePerformanceSmmEntryPoint (\r
288 IN EFI_HANDLE ImageHandle,\r
289 IN EFI_SYSTEM_TABLE *SystemTable\r
290 )\r
291{\r
1c0cc375
LG
292 EFI_STATUS Status;\r
293 EFI_HANDLE Handle;\r
0284e90c 294\r
1c0cc375
LG
295 //\r
296 // Initialize spin lock\r
297 //\r
298 InitializeSpinLock (&mSmmFpdtLock); \r
299 \r
300 //\r
301 // Get SMM Report Status Code Handler Protocol.\r
302 //\r
303 Status = gSmst->SmmLocateProtocol (\r
304 &gEfiSmmRscHandlerProtocolGuid,\r
305 NULL,\r
306 (VOID **) &mRscHandlerProtocol\r
307 );\r
308 ASSERT_EFI_ERROR (Status);\r
0284e90c 309\r
1c0cc375
LG
310 //\r
311 // Register report status code listener for BootRecords and S3 Suspend Start and End.\r
312 //\r
313 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);\r
314 ASSERT_EFI_ERROR (Status);\r
0284e90c 315\r
1c0cc375
LG
316 //\r
317 // Register SMI handler.\r
318 //\r
319 Handle = NULL;\r
320 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);\r
321 ASSERT_EFI_ERROR (Status);\r
322\r
323 return Status;\r
0284e90c 324}\r