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