]>
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 | |
9d00d20e | 14 | Copyright (c) 2011 - 2013, 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 | 27 | #include <Protocol/SmmReportStatusCodeHandler.h>\r |
1c0cc375 | 28 | #include <Protocol/SmmAccess2.h>\r |
0284e90c LG |
29 | \r |
30 | #include <Guid/FirmwarePerformance.h>\r | |
31 | \r | |
32 | #include <Library/SmmServicesTableLib.h>\r | |
33 | #include <Library/BaseLib.h>\r | |
34 | #include <Library/DebugLib.h>\r | |
35 | #include <Library/TimerLib.h>\r | |
36 | #include <Library/LockBoxLib.h>\r | |
37 | #include <Library/PcdLib.h>\r | |
1c0cc375 LG |
38 | #include <Library/BaseMemoryLib.h>\r |
39 | #include <Library/MemoryAllocationLib.h>\r | |
40 | #include <Library/UefiBootServicesTableLib.h>\r | |
41 | #include <Library/SynchronizationLib.h>\r | |
42 | \r | |
43 | #define EXTENSION_RECORD_SIZE 0x1000\r | |
0284e90c LG |
44 | \r |
45 | EFI_SMM_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;\r | |
46 | UINT64 mSuspendStartTime = 0;\r | |
47 | BOOLEAN mS3SuspendLockBoxSaved = FALSE;\r | |
1c0cc375 LG |
48 | UINT32 mBootRecordSize = 0;\r |
49 | UINT32 mBootRecordMaxSize = 0;\r | |
50 | UINT8 *mBootRecordBuffer = NULL;\r | |
51 | \r | |
52 | EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r | |
53 | UINTN mSmramRangeCount;\r | |
54 | SPIN_LOCK mSmmFpdtLock;\r | |
55 | BOOLEAN mSmramIsOutOfResource = FALSE;\r | |
0284e90c LG |
56 | \r |
57 | /**\r | |
58 | Report status code listener for SMM. This is used to record the performance\r | |
59 | data for S3 Suspend Start and S3 Suspend End in FPDT.\r | |
60 | \r | |
61 | @param[in] CodeType Indicates the type of status code being reported.\r | |
62 | @param[in] Value Describes the current status of a hardware or software entity.\r | |
63 | This included information about the class and subclass that is used to\r | |
64 | classify the entity as well as an operation.\r | |
65 | @param[in] Instance The enumeration of a hardware or software entity within\r | |
66 | the system. Valid instance numbers start with 1.\r | |
67 | @param[in] CallerId This optional parameter may be used to identify the caller.\r | |
68 | This parameter allows the status code driver to apply different rules to\r | |
69 | different callers.\r | |
70 | @param[in] Data This optional parameter may be used to pass additional data.\r | |
71 | \r | |
72 | @retval EFI_SUCCESS Status code is what we expected.\r | |
73 | @retval EFI_UNSUPPORTED Status code not supported.\r | |
74 | \r | |
75 | **/\r | |
76 | EFI_STATUS\r | |
77 | EFIAPI\r | |
78 | FpdtStatusCodeListenerSmm (\r | |
79 | IN EFI_STATUS_CODE_TYPE CodeType,\r | |
80 | IN EFI_STATUS_CODE_VALUE Value,\r | |
81 | IN UINT32 Instance,\r | |
82 | IN EFI_GUID *CallerId,\r | |
83 | IN EFI_STATUS_CODE_DATA *Data\r | |
84 | )\r | |
85 | {\r | |
86 | EFI_STATUS Status;\r | |
87 | UINT64 CurrentTime;\r | |
88 | EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;\r | |
1c0cc375 | 89 | UINT8 *NewRecordBuffer;\r |
0284e90c LG |
90 | \r |
91 | //\r | |
92 | // Check whether status code is what we are interested in.\r | |
93 | //\r | |
94 | if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {\r | |
95 | return EFI_UNSUPPORTED;\r | |
96 | }\r | |
1c0cc375 LG |
97 | \r |
98 | //\r | |
99 | // Collect one or more Boot records in boot time\r | |
100 | //\r | |
101 | if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {\r | |
102 | AcquireSpinLock (&mSmmFpdtLock);\r | |
103 | \r | |
104 | if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {\r | |
105 | //\r | |
106 | // Try to allocate big SMRAM data to store Boot record. \r | |
107 | //\r | |
108 | if (mSmramIsOutOfResource) {\r | |
d69a7bc7 | 109 | ReleaseSpinLock (&mSmmFpdtLock);\r |
1c0cc375 LG |
110 | return EFI_OUT_OF_RESOURCES;\r |
111 | }\r | |
d69a7bc7 | 112 | NewRecordBuffer = ReallocatePool (mBootRecordSize, mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE, mBootRecordBuffer); \r |
1c0cc375 | 113 | if (NewRecordBuffer == NULL) {\r |
d69a7bc7 | 114 | ReleaseSpinLock (&mSmmFpdtLock);\r |
1c0cc375 LG |
115 | mSmramIsOutOfResource = TRUE;\r |
116 | return EFI_OUT_OF_RESOURCES;\r | |
117 | }\r | |
1c0cc375 LG |
118 | mBootRecordBuffer = NewRecordBuffer;\r |
119 | mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;\r | |
120 | }\r | |
121 | //\r | |
122 | // Save boot record into the temp memory space.\r | |
123 | //\r | |
124 | CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);\r | |
125 | mBootRecordSize += Data->Size;\r | |
126 | \r | |
127 | ReleaseSpinLock (&mSmmFpdtLock);\r | |
128 | return EFI_SUCCESS;\r | |
129 | }\r | |
130 | \r | |
0284e90c LG |
131 | if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&\r |
132 | (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {\r | |
133 | return EFI_UNSUPPORTED;\r | |
134 | }\r | |
135 | \r | |
136 | //\r | |
137 | // Retrieve current time.\r | |
138 | //\r | |
139 | CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());\r | |
140 | \r | |
141 | if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {\r | |
142 | //\r | |
143 | // S3 Suspend started, record the performance data and return.\r | |
144 | //\r | |
145 | mSuspendStartTime = CurrentTime;\r | |
146 | return EFI_SUCCESS;\r | |
147 | }\r | |
148 | \r | |
149 | //\r | |
150 | // We are going to S3 sleep, record S3 Suspend End performance data.\r | |
151 | //\r | |
152 | S3SuspendRecord.SuspendStart = mSuspendStartTime;\r | |
153 | S3SuspendRecord.SuspendEnd = CurrentTime;\r | |
154 | \r | |
155 | //\r | |
156 | // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.\r | |
157 | //\r | |
158 | if (!mS3SuspendLockBoxSaved) {\r | |
159 | Status = SaveLockBox (\r | |
160 | &gEfiFirmwarePerformanceGuid,\r | |
161 | &S3SuspendRecord,\r | |
162 | sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r | |
163 | );\r | |
164 | ASSERT_EFI_ERROR (Status);\r | |
165 | \r | |
166 | mS3SuspendLockBoxSaved = TRUE;\r | |
167 | } else {\r | |
168 | Status = UpdateLockBox (\r | |
169 | &gEfiFirmwarePerformanceGuid,\r | |
170 | 0,\r | |
171 | &S3SuspendRecord,\r | |
172 | sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r | |
173 | );\r | |
174 | ASSERT_EFI_ERROR (Status);\r | |
175 | }\r | |
176 | \r | |
177 | return EFI_SUCCESS;\r | |
178 | }\r | |
179 | \r | |
1c0cc375 LG |
180 | /**\r |
181 | This function check if the address is in SMRAM.\r | |
182 | \r | |
183 | @param Buffer the buffer address to be checked.\r | |
184 | @param Length the buffer length to be checked.\r | |
185 | \r | |
186 | @retval TRUE this address is in SMRAM.\r | |
187 | @retval FALSE this address is NOT in SMRAM.\r | |
188 | **/\r | |
189 | BOOLEAN\r | |
190 | InternalIsAddressInSmram (\r | |
191 | IN EFI_PHYSICAL_ADDRESS Buffer,\r | |
192 | IN UINT64 Length\r | |
193 | )\r | |
194 | {\r | |
195 | UINTN Index;\r | |
196 | \r | |
197 | for (Index = 0; Index < mSmramRangeCount; Index ++) {\r | |
198 | if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||\r | |
199 | ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {\r | |
200 | return TRUE;\r | |
201 | }\r | |
202 | }\r | |
203 | \r | |
204 | return FALSE;\r | |
205 | }\r | |
206 | \r | |
9d00d20e SZ |
207 | /**\r |
208 | This function check if the address refered by Buffer and Length is valid.\r | |
209 | \r | |
210 | @param Buffer the buffer address to be checked.\r | |
211 | @param Length the buffer length to be checked.\r | |
212 | \r | |
213 | @retval TRUE this address is valid.\r | |
214 | @retval FALSE this address is NOT valid.\r | |
215 | **/\r | |
216 | BOOLEAN\r | |
217 | InternalIsAddressValid (\r | |
218 | IN UINTN Buffer,\r | |
219 | IN UINTN Length\r | |
220 | )\r | |
221 | {\r | |
222 | if (Buffer > (MAX_ADDRESS - Length)) {\r | |
223 | //\r | |
224 | // Overflow happen\r | |
225 | //\r | |
226 | return FALSE;\r | |
227 | }\r | |
228 | if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {\r | |
229 | return FALSE;\r | |
230 | }\r | |
231 | return TRUE;\r | |
232 | }\r | |
233 | \r | |
1c0cc375 LG |
234 | /**\r |
235 | Communication service SMI Handler entry.\r | |
236 | \r | |
237 | This SMI handler provides services for report SMM boot records. \r | |
238 | \r | |
ccd2f6b0 | 239 | Caution: This function may receive untrusted input.\r |
240 | Communicate buffer and buffer size are external input, so this function will do basic validation.\r | |
241 | \r | |
1c0cc375 LG |
242 | @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r |
243 | @param[in] RegisterContext Points to an optional handler context which was specified when the\r | |
244 | handler was registered.\r | |
245 | @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r | |
246 | be conveyed from a non-SMM environment into an SMM environment.\r | |
247 | @param[in, out] CommBufferSize The size of the CommBuffer.\r | |
248 | \r | |
ccd2f6b0 | 249 | @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r |
250 | should still be called.\r | |
251 | @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r | |
252 | still be called.\r | |
253 | @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r | |
254 | be called.\r | |
255 | @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r | |
256 | \r | |
1c0cc375 LG |
257 | **/\r |
258 | EFI_STATUS\r | |
259 | EFIAPI\r | |
260 | FpdtSmiHandler (\r | |
261 | IN EFI_HANDLE DispatchHandle,\r | |
262 | IN CONST VOID *RegisterContext,\r | |
263 | IN OUT VOID *CommBuffer,\r | |
264 | IN OUT UINTN *CommBufferSize\r | |
265 | )\r | |
266 | {\r | |
267 | EFI_STATUS Status;\r | |
268 | SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;\r | |
5e5bb2a9 SZ |
269 | UINTN BootRecordSize;\r |
270 | VOID *BootRecordData;\r | |
ccd2f6b0 | 271 | \r |
272 | //\r | |
273 | // If input is invalid, stop processing this SMI\r | |
274 | //\r | |
275 | if (CommBuffer == NULL || CommBufferSize == NULL) {\r | |
276 | return EFI_SUCCESS;\r | |
277 | }\r | |
278 | \r | |
279 | if(*CommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {\r | |
280 | return EFI_SUCCESS;\r | |
281 | }\r | |
1c0cc375 | 282 | \r |
9d00d20e | 283 | if (!InternalIsAddressValid ((UINTN)CommBuffer, *CommBufferSize)) {\r |
5e5bb2a9 | 284 | DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data buffer in SMRAM or overflow!\n"));\r |
ccd2f6b0 | 285 | return EFI_SUCCESS;\r |
1c0cc375 LG |
286 | }\r |
287 | \r | |
1c0cc375 LG |
288 | SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;\r |
289 | \r | |
ccd2f6b0 | 290 | Status = EFI_SUCCESS;\r |
291 | \r | |
1c0cc375 LG |
292 | switch (SmmCommData->Function) {\r |
293 | case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :\r | |
294 | SmmCommData->BootRecordSize = mBootRecordSize;\r | |
295 | break;\r | |
296 | \r | |
297 | case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :\r | |
5e5bb2a9 SZ |
298 | BootRecordData = SmmCommData->BootRecordData;\r |
299 | BootRecordSize = SmmCommData->BootRecordSize;\r | |
300 | if (BootRecordData == NULL || BootRecordSize < mBootRecordSize) {\r | |
1c0cc375 LG |
301 | Status = EFI_INVALID_PARAMETER;\r |
302 | break;\r | |
303 | } \r | |
ccd2f6b0 | 304 | \r |
1c0cc375 LG |
305 | //\r |
306 | // Sanity check\r | |
307 | //\r | |
308 | SmmCommData->BootRecordSize = mBootRecordSize;\r | |
5e5bb2a9 SZ |
309 | if (!InternalIsAddressValid ((UINTN)BootRecordData, mBootRecordSize)) {\r |
310 | DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM or overflow!\n"));\r | |
1c0cc375 LG |
311 | Status = EFI_ACCESS_DENIED;\r |
312 | break;\r | |
313 | }\r | |
314 | \r | |
315 | CopyMem (\r | |
5e5bb2a9 | 316 | (UINT8*)BootRecordData, \r |
1c0cc375 LG |
317 | mBootRecordBuffer, \r |
318 | mBootRecordSize\r | |
319 | );\r | |
320 | break;\r | |
321 | \r | |
322 | default:\r | |
1c0cc375 LG |
323 | Status = EFI_UNSUPPORTED;\r |
324 | }\r | |
325 | \r | |
326 | SmmCommData->ReturnStatus = Status;\r | |
ccd2f6b0 | 327 | \r |
1c0cc375 LG |
328 | return EFI_SUCCESS;\r |
329 | }\r | |
330 | \r | |
0284e90c LG |
331 | /**\r |
332 | The module Entry Point of the Firmware Performance Data Table SMM driver.\r | |
333 | \r | |
334 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
335 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
336 | \r | |
337 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
338 | @retval Other Some error occurs when executing this entry point.\r | |
339 | \r | |
340 | **/\r | |
341 | EFI_STATUS\r | |
342 | EFIAPI\r | |
343 | FirmwarePerformanceSmmEntryPoint (\r | |
344 | IN EFI_HANDLE ImageHandle,\r | |
345 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
346 | )\r | |
347 | {\r | |
1c0cc375 LG |
348 | EFI_STATUS Status;\r |
349 | EFI_HANDLE Handle;\r | |
350 | EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r | |
351 | UINTN Size;\r | |
0284e90c | 352 | \r |
1c0cc375 LG |
353 | //\r |
354 | // Initialize spin lock\r | |
355 | //\r | |
356 | InitializeSpinLock (&mSmmFpdtLock); \r | |
357 | \r | |
358 | //\r | |
359 | // Get SMM Report Status Code Handler Protocol.\r | |
360 | //\r | |
361 | Status = gSmst->SmmLocateProtocol (\r | |
362 | &gEfiSmmRscHandlerProtocolGuid,\r | |
363 | NULL,\r | |
364 | (VOID **) &mRscHandlerProtocol\r | |
365 | );\r | |
366 | ASSERT_EFI_ERROR (Status);\r | |
0284e90c | 367 | \r |
1c0cc375 LG |
368 | //\r |
369 | // Register report status code listener for BootRecords and S3 Suspend Start and End.\r | |
370 | //\r | |
371 | Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);\r | |
372 | ASSERT_EFI_ERROR (Status);\r | |
0284e90c | 373 | \r |
1c0cc375 LG |
374 | //\r |
375 | // Get SMRAM information\r | |
376 | //\r | |
377 | Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r | |
378 | ASSERT_EFI_ERROR (Status);\r | |
379 | \r | |
380 | Size = 0;\r | |
381 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r | |
382 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
383 | \r | |
384 | Status = gSmst->SmmAllocatePool (\r | |
385 | EfiRuntimeServicesData,\r | |
386 | Size,\r | |
387 | (VOID **)&mSmramRanges\r | |
388 | );\r | |
389 | ASSERT_EFI_ERROR (Status);\r | |
390 | \r | |
391 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r | |
392 | ASSERT_EFI_ERROR (Status);\r | |
393 | \r | |
394 | mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r | |
395 | \r | |
396 | //\r | |
397 | // Register SMI handler.\r | |
398 | //\r | |
399 | Handle = NULL;\r | |
400 | Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);\r | |
401 | ASSERT_EFI_ERROR (Status);\r | |
402 | \r | |
403 | return Status;\r | |
0284e90c | 404 | }\r |