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