]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
MdeModulePkg: Add match2 opcode support in SetupBrowserDxe and sample code in DriverS...
[mirror_edk2.git] / MdeModulePkg / Library / DxeSmmPerformanceLib / DxeSmmPerformanceLib.c
1 /** @file
2 Performance library instance used in DXE phase to dump SMM performance data.
3
4 This library instance allows a DXE driver or UEFI application to dump the SMM performance data.
5 StartPerformanceMeasurement(), EndPerformanceMeasurement(), StartPerformanceMeasurementEx()
6 and EndPerformanceMeasurementEx() are not implemented.
7
8 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19
20 #include <PiDxe.h>
21
22 #include <Guid/Performance.h>
23
24 #include <Library/PerformanceLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/UefiBootServicesTableLib.h>
27 #include <Library/UefiRuntimeServicesTableLib.h>
28 #include <Library/PcdLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/MemoryAllocationLib.h>
32
33 #include <Protocol/SmmCommunication.h>
34
35 #define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof (SMM_PERF_COMMUNICATE))
36 //
37 // The cached performance protocol interface.
38 //
39 EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;
40 UINT8 mSmmPerformanceBuffer[SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE];
41 GAUGE_DATA_ENTRY *mGaugeData = NULL;
42 UINTN mGaugeNumberOfEntries = 0;
43 GAUGE_DATA_ENTRY_EX *mGaugeDataEx = NULL;
44 UINTN mGaugeNumberOfEntriesEx = 0;
45
46 /**
47 The function caches the pointer to SMM Communication protocol.
48
49 The function locates SMM Communication protocol from protocol database.
50
51 @retval EFI_SUCCESS SMM Communication protocol is successfully located.
52 @retval Other SMM Communication protocol is not located to log performance.
53
54 **/
55 EFI_STATUS
56 GetCommunicationProtocol (
57 VOID
58 )
59 {
60 EFI_STATUS Status;
61 EFI_SMM_COMMUNICATION_PROTOCOL *Communication;
62
63 if (mSmmCommunication != NULL) {
64 return EFI_SUCCESS;
65 }
66
67 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);
68 if (!EFI_ERROR (Status)) {
69 ASSERT (Communication != NULL);
70 //
71 // Cache SMM Communication protocol.
72 //
73 mSmmCommunication = Communication;
74 }
75
76 return Status;
77 }
78
79 /**
80 Creates a record for the beginning of a performance measurement.
81
82 Creates a record that contains the Handle, Token, Module and Identifier.
83 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
84 If TimeStamp is zero, then this function reads the current time stamp
85 and adds that time stamp value to the record as the start time.
86
87 @param Handle Pointer to environment specific context used
88 to identify the component being measured.
89 @param Token Pointer to a Null-terminated ASCII string
90 that identifies the component being measured.
91 @param Module Pointer to a Null-terminated ASCII string
92 that identifies the module being measured.
93 @param TimeStamp 64-bit time stamp.
94 @param Identifier 32-bit identifier. If the value is 0, the created record
95 is same as the one created by StartPerformanceMeasurement.
96
97 @retval RETURN_SUCCESS The start of the measurement was recorded.
98 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
99
100 **/
101 RETURN_STATUS
102 EFIAPI
103 StartPerformanceMeasurementEx (
104 IN CONST VOID *Handle, OPTIONAL
105 IN CONST CHAR8 *Token, OPTIONAL
106 IN CONST CHAR8 *Module, OPTIONAL
107 IN UINT64 TimeStamp,
108 IN UINT32 Identifier
109 )
110 {
111 return RETURN_SUCCESS;
112 }
113
114 /**
115 Fills in the end time of a performance measurement.
116
117 Looks up the record that matches Handle, Token, Module and Identifier.
118 If the record can not be found then return RETURN_NOT_FOUND.
119 If the record is found and TimeStamp is not zero,
120 then TimeStamp is added to the record as the end time.
121 If the record is found and TimeStamp is zero, then this function reads
122 the current time stamp and adds that time stamp value to the record as the end time.
123
124 @param Handle Pointer to environment specific context used
125 to identify the component being measured.
126 @param Token Pointer to a Null-terminated ASCII string
127 that identifies the component being measured.
128 @param Module Pointer to a Null-terminated ASCII string
129 that identifies the module being measured.
130 @param TimeStamp 64-bit time stamp.
131 @param Identifier 32-bit identifier. If the value is 0, the found record
132 is same as the one found by EndPerformanceMeasurement.
133
134 @retval RETURN_SUCCESS The end of the measurement was recorded.
135 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
136
137 **/
138 RETURN_STATUS
139 EFIAPI
140 EndPerformanceMeasurementEx (
141 IN CONST VOID *Handle, OPTIONAL
142 IN CONST CHAR8 *Token, OPTIONAL
143 IN CONST CHAR8 *Module, OPTIONAL
144 IN UINT64 TimeStamp,
145 IN UINT32 Identifier
146 )
147 {
148 return RETURN_SUCCESS;
149 }
150
151 /**
152 Creates a record for the beginning of a performance measurement.
153
154 Creates a record that contains the Handle, Token, and Module.
155 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
156 If TimeStamp is zero, then this function reads the current time stamp
157 and adds that time stamp value to the record as the start time.
158
159 @param Handle Pointer to environment specific context used
160 to identify the component being measured.
161 @param Token Pointer to a Null-terminated ASCII string
162 that identifies the component being measured.
163 @param Module Pointer to a Null-terminated ASCII string
164 that identifies the module being measured.
165 @param TimeStamp 64-bit time stamp.
166
167 @retval RETURN_SUCCESS The start of the measurement was recorded.
168 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
169
170 **/
171 RETURN_STATUS
172 EFIAPI
173 StartPerformanceMeasurement (
174 IN CONST VOID *Handle, OPTIONAL
175 IN CONST CHAR8 *Token, OPTIONAL
176 IN CONST CHAR8 *Module, OPTIONAL
177 IN UINT64 TimeStamp
178 )
179 {
180 return RETURN_SUCCESS;
181 }
182
183 /**
184 Fills in the end time of a performance measurement.
185
186 Looks up the record that matches Handle, Token, and Module.
187 If the record can not be found then return RETURN_NOT_FOUND.
188 If the record is found and TimeStamp is not zero,
189 then TimeStamp is added to the record as the end time.
190 If the record is found and TimeStamp is zero, then this function reads
191 the current time stamp and adds that time stamp value to the record as the end time.
192
193 @param Handle Pointer to environment specific context used
194 to identify the component being measured.
195 @param Token Pointer to a Null-terminated ASCII string
196 that identifies the component being measured.
197 @param Module Pointer to a Null-terminated ASCII string
198 that identifies the module being measured.
199 @param TimeStamp 64-bit time stamp.
200
201 @retval RETURN_SUCCESS The end of the measurement was recorded.
202 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
203
204 **/
205 RETURN_STATUS
206 EFIAPI
207 EndPerformanceMeasurement (
208 IN CONST VOID *Handle, OPTIONAL
209 IN CONST CHAR8 *Token, OPTIONAL
210 IN CONST CHAR8 *Module, OPTIONAL
211 IN UINT64 TimeStamp
212 )
213 {
214 return RETURN_SUCCESS;
215 }
216
217 /**
218 Retrieves all previous logged performance measurement.
219 Function will use SMM communicate protocol to get all previous SMM performance measurement data.
220 If success, data buffer will be returned. If fail function will return NULL.
221
222 @retval !NULL Get all gauge data success.
223 @retval NULL Get all guage data failed.
224 **/
225 GAUGE_DATA_ENTRY *
226 EFIAPI
227 GetAllSmmGaugeData (VOID)
228 {
229 EFI_STATUS Status;
230 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;
231 SMM_PERF_COMMUNICATE *SmmPerfCommData;
232 UINTN CommSize;
233 UINTN DataSize;
234
235 if (mGaugeData != NULL) {
236 return mGaugeData;
237 }
238
239 Status = GetCommunicationProtocol ();
240 if (EFI_ERROR (Status)) {
241 return NULL;
242 }
243
244 //
245 // Initialize communicate buffer
246 //
247 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER *)mSmmPerformanceBuffer;
248 SmmPerfCommData = (SMM_PERF_COMMUNICATE *)SmmCommBufferHeader->Data;
249 ZeroMem((UINT8*)SmmPerfCommData, sizeof(SMM_PERF_COMMUNICATE));
250
251 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gSmmPerformanceProtocolGuid);
252 SmmCommBufferHeader->MessageLength = sizeof(SMM_PERF_COMMUNICATE);
253 CommSize = SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE;
254
255 //
256 // Get totol number of SMM gauge entries
257 //
258 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;
259 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);
260 if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {
261 return NULL;
262 }
263
264 mGaugeNumberOfEntries = SmmPerfCommData->NumberOfEntries;
265
266 DataSize = mGaugeNumberOfEntries * sizeof(GAUGE_DATA_ENTRY);
267 mGaugeData = AllocateZeroPool(DataSize);
268 ASSERT (mGaugeData != NULL);
269
270 //
271 // Get all SMM gauge data
272 //
273 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;
274 SmmPerfCommData->LogEntryKey = 0;
275 SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntries;
276 SmmPerfCommData->GaugeData = mGaugeData;
277 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);
278 if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus)) {
279 FreePool (mGaugeData);
280 mGaugeData = NULL;
281 mGaugeNumberOfEntries = 0;
282 }
283
284 return mGaugeData;
285 }
286
287 /**
288 Retrieves all previous logged performance measurement.
289 Function will use SMM communicate protocol to get all previous SMM performance measurement data.
290 If success, data buffer will be returned. If fail function will return NULL.
291
292 @retval !NULL Get all gauge data success.
293 @retval NULL Get all guage data failed.
294 **/
295 GAUGE_DATA_ENTRY_EX *
296 EFIAPI
297 GetAllSmmGaugeDataEx (VOID)
298 {
299 EFI_STATUS Status;
300 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;
301 SMM_PERF_COMMUNICATE_EX *SmmPerfCommData;
302 UINTN CommSize;
303 UINTN DataSize;
304
305 if (mGaugeDataEx != NULL) {
306 return mGaugeDataEx;
307 }
308
309 Status = GetCommunicationProtocol ();
310 if (EFI_ERROR (Status)) {
311 return NULL;
312 }
313
314 //
315 // Initialize communicate buffer
316 //
317 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER *)mSmmPerformanceBuffer;
318 SmmPerfCommData = (SMM_PERF_COMMUNICATE_EX *)SmmCommBufferHeader->Data;
319 ZeroMem((UINT8*)SmmPerfCommData, sizeof(SMM_PERF_COMMUNICATE_EX));
320
321 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gSmmPerformanceExProtocolGuid);
322 SmmCommBufferHeader->MessageLength = sizeof(SMM_PERF_COMMUNICATE_EX);
323 CommSize = SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE;
324
325 //
326 // Get totol number of SMM gauge entries
327 //
328 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;
329 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);
330 if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {
331 return NULL;
332 }
333
334 mGaugeNumberOfEntriesEx = SmmPerfCommData->NumberOfEntries;
335
336 DataSize = mGaugeNumberOfEntriesEx * sizeof(GAUGE_DATA_ENTRY_EX);
337 mGaugeDataEx = AllocateZeroPool(DataSize);
338 ASSERT (mGaugeDataEx != NULL);
339
340 //
341 // Get all SMM gauge data
342 //
343 SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;
344 SmmPerfCommData->LogEntryKey = 0;
345 SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntriesEx;
346 SmmPerfCommData->GaugeDataEx = mGaugeDataEx;
347 Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);
348 if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus)) {
349 FreePool (mGaugeDataEx);
350 mGaugeDataEx = NULL;
351 mGaugeNumberOfEntriesEx = 0;
352 }
353
354 return mGaugeDataEx;
355 }
356
357 /**
358 Attempts to retrieve a performance measurement log entry from the performance measurement log.
359 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
360 and then assign the Identifier with 0.
361
362 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
363 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
364 and the key for the second entry in the log is returned. If the performance log is empty,
365 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
366 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
367 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
368 retrieved and an implementation specific non-zero key value that specifies the end of the performance
369 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
370 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
371 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
372 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
373 If Handle is NULL, then ASSERT().
374 If Token is NULL, then ASSERT().
375 If Module is NULL, then ASSERT().
376 If StartTimeStamp is NULL, then ASSERT().
377 If EndTimeStamp is NULL, then ASSERT().
378 If Identifier is NULL, then ASSERT().
379
380 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
381 0, then the first performance measurement log entry is retrieved.
382 On exit, the key of the next performance log entry.
383 @param Handle Pointer to environment specific context used to identify the component
384 being measured.
385 @param Token Pointer to a Null-terminated ASCII string that identifies the component
386 being measured.
387 @param Module Pointer to a Null-terminated ASCII string that identifies the module
388 being measured.
389 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
390 was started.
391 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
392 was ended.
393 @param Identifier Pointer to the 32-bit identifier that was recorded.
394
395 @return The key for the next performance log entry (in general case).
396
397 **/
398 UINTN
399 EFIAPI
400 GetPerformanceMeasurementEx (
401 IN UINTN LogEntryKey,
402 OUT CONST VOID **Handle,
403 OUT CONST CHAR8 **Token,
404 OUT CONST CHAR8 **Module,
405 OUT UINT64 *StartTimeStamp,
406 OUT UINT64 *EndTimeStamp,
407 OUT UINT32 *Identifier
408 )
409 {
410 GAUGE_DATA_ENTRY_EX *GaugeData;
411
412 GaugeData = NULL;
413
414 ASSERT (Handle != NULL);
415 ASSERT (Token != NULL);
416 ASSERT (Module != NULL);
417 ASSERT (StartTimeStamp != NULL);
418 ASSERT (EndTimeStamp != NULL);
419 ASSERT (Identifier != NULL);
420
421 mGaugeDataEx = GetAllSmmGaugeDataEx();
422 if (mGaugeDataEx != NULL) {
423 //
424 // Make sure that LogEntryKey is a valid log entry key.
425 //
426 ASSERT (LogEntryKey <= mGaugeNumberOfEntriesEx);
427
428 if (LogEntryKey == mGaugeNumberOfEntriesEx) {
429 return 0;
430 }
431
432 GaugeData = &mGaugeDataEx[LogEntryKey++];
433 *Identifier = GaugeData->Identifier;
434 } else {
435 mGaugeData = GetAllSmmGaugeData();
436 if (mGaugeData != NULL) {
437 //
438 // Make sure that LogEntryKey is a valid log entry key.
439 //
440 ASSERT (LogEntryKey <= mGaugeNumberOfEntries);
441
442 if (LogEntryKey == mGaugeNumberOfEntries) {
443 return 0;
444 }
445
446 GaugeData = (GAUGE_DATA_ENTRY_EX *) &mGaugeData[LogEntryKey++];
447 *Identifier = 0;
448 } else {
449 return 0;
450 }
451 }
452
453 *Handle = (VOID *) (UINTN) GaugeData->Handle;
454 *Token = GaugeData->Token;
455 *Module = GaugeData->Module;
456 *StartTimeStamp = GaugeData->StartTimeStamp;
457 *EndTimeStamp = GaugeData->EndTimeStamp;
458
459 return LogEntryKey;
460 }
461
462 /**
463 Attempts to retrieve a performance measurement log entry from the performance measurement log.
464 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
465 and then eliminate the Identifier.
466
467 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
468 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
469 and the key for the second entry in the log is returned. If the performance log is empty,
470 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
471 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
472 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
473 retrieved and an implementation specific non-zero key value that specifies the end of the performance
474 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
475 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
476 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
477 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
478 If Handle is NULL, then ASSERT().
479 If Token is NULL, then ASSERT().
480 If Module is NULL, then ASSERT().
481 If StartTimeStamp is NULL, then ASSERT().
482 If EndTimeStamp is NULL, then ASSERT().
483
484 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
485 0, then the first performance measurement log entry is retrieved.
486 On exit, the key of the next performance log entry.
487 @param Handle Pointer to environment specific context used to identify the component
488 being measured.
489 @param Token Pointer to a Null-terminated ASCII string that identifies the component
490 being measured.
491 @param Module Pointer to a Null-terminated ASCII string that identifies the module
492 being measured.
493 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
494 was started.
495 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
496 was ended.
497
498 @return The key for the next performance log entry (in general case).
499
500 **/
501 UINTN
502 EFIAPI
503 GetPerformanceMeasurement (
504 IN UINTN LogEntryKey,
505 OUT CONST VOID **Handle,
506 OUT CONST CHAR8 **Token,
507 OUT CONST CHAR8 **Module,
508 OUT UINT64 *StartTimeStamp,
509 OUT UINT64 *EndTimeStamp
510 )
511 {
512 UINT32 Identifier;
513 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);
514 }
515
516 /**
517 Returns TRUE if the performance measurement macros are enabled.
518
519 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
520 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
521
522 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
523 PcdPerformanceLibraryPropertyMask is set.
524 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
525 PcdPerformanceLibraryPropertyMask is clear.
526
527 **/
528 BOOLEAN
529 EFIAPI
530 PerformanceMeasurementEnabled (
531 VOID
532 )
533 {
534 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
535 }