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