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