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