1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
|
/** @file
SMM Periodic SMI Library.
Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <PiSmm.h>
#include <Protocol/SmmPeriodicTimerDispatch2.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/SynchronizationLib.h>
#include <Library/DebugLib.h>
#include <Library/TimerLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/SmmServicesTableLib.h>
#include <Library/SmmPeriodicSmiLib.h>
///
/// Define the number of periodic SMI handler entries that should be allocated to the list
/// of free periodic SMI handlers when the list of free periodic SMI handlers is empty.
///
#define PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE 0x08
///
/// Signature for a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure
///
#define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE SIGNATURE_32 ('P', 'S', 'M', 'I')
///
/// Structure that contains state information for an enabled periodic SMI handler
///
typedef struct {
///
/// Signature value that must be set to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE
///
UINT32 Signature;
///
/// The link entry to be inserted to the list of periodic SMI handlers.
///
LIST_ENTRY Link;
///
/// The dispatch function to called to invoke an enabled periodic SMI handler.
///
PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction;
///
/// The context to pass into DispatchFunction
///
VOID *Context;
///
/// The tick period in 100 ns units that DispatchFunction should be called.
///
UINT64 TickPeriod;
///
/// The Cpu number that is required to execute DispatchFunction. If Cpu is
/// set to PERIODIC_SMI_LIBRARY_ANY_CPU, then DispatchFunction may be executed
/// on any CPU.
///
UINTN Cpu;
///
/// The size, in bytes, of the stack allocated for a periodic SMI handler.
/// This value must be a multiple of EFI_PAGE_SIZE.
///
UINTN StackSize;
///
/// A pointer to the stack allocated using AllocatePages(). This field will
/// be NULL if StackSize is 0.
///
VOID *Stack;
///
/// Spin lock used to wait for an AP to complete the execution of a periodic SMI handler
///
SPIN_LOCK DispatchLock;
///
/// The rate in Hz of the performance counter that is used to measure the
/// amount of time that a periodic SMI handler executes.
///
UINT64 PerfomanceCounterRate;
///
/// The start count value of the performance counter that is used to measure
/// the amount of time that a periodic SMI handler executes.
///
UINT64 PerfomanceCounterStartValue;
///
/// The end count value of the performance counter that is used to measure
/// the amount of time that a periodic SMI handler executes.
///
UINT64 PerfomanceCounterEndValue;
///
/// The context record passed into the Register() function of the SMM Periodic
/// Timer Dispatch Protocol when a periodic SMI handler is enabled.
///
EFI_SMM_PERIODIC_TIMER_REGISTER_CONTEXT RegisterContext;
///
/// The handle returned from the Register() function of the SMM Periodic
/// Timer Dispatch Protocol when a periodic SMI handler is enabled.
///
EFI_HANDLE DispatchHandle;
///
/// The total number of performance counter ticks that the periodic SMI handler
/// has been executing in its current invocation.
///
UINT64 DispatchTotalTime;
///
/// The performance counter value that was captured the last time that the
/// periodic SMI handler called PeriodcSmiExecutionTime(). This allows the
/// time value returned by PeriodcSmiExecutionTime() to be accurate even when
/// the performance counter rolls over.
///
UINT64 DispatchCheckPointTime;
///
/// Buffer used to save the context when control is transfer from this library
/// to an enabled periodic SMI handler. This saved context is used when the
/// periodic SMI handler exits or yields.
///
BASE_LIBRARY_JUMP_BUFFER DispatchJumpBuffer;
///
/// Flag that is set to TRUE when a periodic SMI handler requests to yield
/// using PeriodicSmiYield(). When this flag IS TRUE, YieldJumpBuffer is
/// valid. When this flag is FALSE, YieldJumpBuffer is not valid.
///
BOOLEAN YieldFlag;
///
/// Buffer used to save the context when a periodic SMI handler requests to
/// yield using PeriodicSmiYield(). This context is used to resume the
/// execution of a periodic SMI handler the next time control is transferd
/// to the periodic SMI handler that yielded.
///
BASE_LIBRARY_JUMP_BUFFER YieldJumpBuffer;
///
/// The amount of time, in 100 ns units, that have elapsed since the last
/// time the periodic SMI handler was invoked.
///
UINT64 ElapsedTime;
} PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT;
/**
Macro that returns a pointer to a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
structure based on a pointer to a RegisterContext field.
**/
#define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT(a) \
CR ( \
a, \
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT, \
RegisterContext, \
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE \
)
/**
Macro that returns a pointer to a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
structure based on a pointer to a Link field.
**/
#define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK(a) \
CR ( \
a, \
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT, \
Link, \
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE \
)
///
/// Pointer to the SMM Periodic Timer Disatch Protocol that was located in the constuctor.
///
EFI_SMM_PERIODIC_TIMER_DISPATCH2_PROTOCOL *gSmmPeriodicTimerDispatch2 = NULL;
///
/// Pointer to a table of supported periodic SMI tick periods in 100 ns units
/// sorted from largest to smallest terminated by a tick period value of 0.
/// This table is allocated using AllocatePool() in the constructor and filled
/// in based on the values returned from the SMM Periodic Timer Dispatch 2 Protocol
/// function GetNextShorterInterval().
///
UINT64 *gSmiTickPeriodTable = NULL;
///
/// Linked list of free periodic SMI handlers that this library can use.
///
LIST_ENTRY gFreePeriodicSmiLibraryHandlers =
INITIALIZE_LIST_HEAD_VARIABLE (gFreePeriodicSmiLibraryHandlers);
///
/// Linked list of periodic SMI handlers that this library is currently managing.
///
LIST_ENTRY gPeriodicSmiLibraryHandlers =
INITIALIZE_LIST_HEAD_VARIABLE (gPeriodicSmiLibraryHandlers);
///
/// Pointer to the periodic SMI handler that is currently being executed.
/// Is set to NULL if no periodic SMI handler is currently being executed.
///
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *gActivePeriodicSmiLibraryHandler = NULL;
/**
Internal worker function that returns a pointer to the
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure associated with the periodic
SMI handler that is currently being executed. If a periodic SMI handler is
not currently being executed, the NULL is returned.
@retval NULL A periodic SMI handler is not currently being executed.
@retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
associated with the active periodic SMI handler.
**/
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
GetActivePeriodicSmiLibraryHandler (
VOID
)
{
return gActivePeriodicSmiLibraryHandler;
}
/**
Internal worker function that returns a pointer to the
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure associated with the
DispatchHandle that was returned when the periodic SMI handler was enabled
with PeriodicSmiEnable(). If DispatchHandle is NULL, then the active
periodic SMI handler is returned. If DispatchHandle is NULL and there is
no active periodic SMI handler, then NULL is returned.
@param[in] DispatchHandle DispatchHandle that was returned when the periodic
SMI handler was enabled with PeriodicSmiEnable().
This is an optional parameter that may be NULL.
If this parameter is NULL, then the active periodic
SMI handler is returned.
@retval NULL DispatchHandle is NULL and there is no active periodic SMI
handler.
@retval NULL DispatchHandle does not match any of the periodic SMI handlers
that have been enabled with PeriodicSmiEnable().
@retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
associated with the DispatchHandle.
**/
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
LookupPeriodicSmiLibraryHandler (
IN EFI_HANDLE DispatchHandle OPTIONAL
)
{
LIST_ENTRY *Link;
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
//
// If DispatchHandle is NULL, then return the active periodic SMI handler
//
if (DispatchHandle == NULL) {
return GetActivePeriodicSmiLibraryHandler ();
}
//
// Search the periodic SMI handler entries for a a matching DispatchHandle
//
for ( Link = GetFirstNode (&gPeriodicSmiLibraryHandlers)
; !IsNull (&gPeriodicSmiLibraryHandlers, Link)
; Link = GetNextNode (&gPeriodicSmiLibraryHandlers, Link)
) {
PeriodicSmiLibraryHandler = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (Link);
if (PeriodicSmiLibraryHandler->DispatchHandle == DispatchHandle) {
return PeriodicSmiLibraryHandler;
}
}
//
// No entries match DispatchHandle, so return NULL
//
return NULL;
}
/**
Internal worker function that sets that active periodic SMI handler based on
the Context used when the periodic SMI handler was registered with the
SMM Periodic Timer Dispatch 2 Protocol. If Context is NULL, then the
state is updated to show that there is not active periodic SMI handler.
A pointer to the active PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure
is returned.
@retval NULL Context is NULL.
@retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
associated with Context.
**/
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
SetActivePeriodicSmiLibraryHandler (
IN CONST VOID *Context OPTIONAL
)
{
if (Context == NULL) {
gActivePeriodicSmiLibraryHandler = NULL;
} else {
gActivePeriodicSmiLibraryHandler = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT (Context);
}
return gActivePeriodicSmiLibraryHandler;
}
/**
Internal worker function that moves the specified periodic SMI handler from the
list of managed periodic SMI handlers to the list of free periodic SMI handlers.
@param[in] PeriodicSmiLibraryHandler Pointer to the periodic SMI handler to be reclaimed.
**/
VOID
ReclaimPeriodicSmiLibraryHandler (
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler
)
{
ASSERT (PeriodicSmiLibraryHandler->DispatchHandle == NULL);
if (PeriodicSmiLibraryHandler->Stack != NULL) {
FreePages (
PeriodicSmiLibraryHandler->Stack,
EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler->StackSize)
);
PeriodicSmiLibraryHandler->Stack = NULL;
}
RemoveEntryList (&PeriodicSmiLibraryHandler->Link);
InsertHeadList (&gFreePeriodicSmiLibraryHandlers, &PeriodicSmiLibraryHandler->Link);
}
/**
Add the additional entries to the list of free periodic SMI handlers.
The function is assumed to be called only when the list of free periodic SMI
handlers is empty.
@retval TRUE The additional entries were added.
@retval FALSE There was no available resource for the additional entries.
**/
BOOLEAN
EnlargeFreePeriodicSmiLibraryHandlerList (
VOID
)
{
UINTN Index;
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
//
// Add the entries to the list
//
for (Index = 0; Index < PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE; Index++) {
PeriodicSmiLibraryHandler = AllocatePool (sizeof (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT));
if (PeriodicSmiLibraryHandler == NULL) {
break;
}
PeriodicSmiLibraryHandler->Signature = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE;
InsertHeadList (&gFreePeriodicSmiLibraryHandlers, &PeriodicSmiLibraryHandler->Link);
}
return (BOOLEAN) (Index > 0);
}
/**
Internal worker function that returns a free entry for a new periodic
SMI handler. If no free entries are available, then additional
entries are allocated.
@retval NULL There are not enough resources available to to allocate a free entry.
@retval other Pointer to a free PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure.
**/
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
FindFreePeriodicSmiLibraryHandler (
VOID
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
if (IsListEmpty (&gFreePeriodicSmiLibraryHandlers)) {
if (!EnlargeFreePeriodicSmiLibraryHandlerList ()) {
return NULL;
}
}
//
// Get one from the list of free periodic SMI handlers.
//
PeriodicSmiLibraryHandler = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (
GetFirstNode (&gFreePeriodicSmiLibraryHandlers)
);
RemoveEntryList (&PeriodicSmiLibraryHandler->Link);
InsertTailList (&gPeriodicSmiLibraryHandlers, &PeriodicSmiLibraryHandler->Link);
return PeriodicSmiLibraryHandler;
}
/**
This function returns a pointer to a table of supported periodic
SMI tick periods in 100 ns units sorted from largest to smallest.
The table contains a array of UINT64 values terminated by a tick
period value of 0. The returned table must be treated as read-only
data and must not be freed.
@return A pointer to a table of UINT64 tick period values in
100ns units sorted from largest to smallest terminated
by a tick period of 0.
**/
UINT64 *
EFIAPI
PeriodicSmiSupportedTickPeriod (
VOID
)
{
//
// Return the table allocated and populated by SmmPeriodicSmiLibConstructor()
//
return gSmiTickPeriodTable;
}
/**
This function returns the time in 100ns units since the periodic SMI
handler function was called. If the periodic SMI handler was resumed
through PeriodicSmiYield(), then the time returned is the time in
100ns units since PeriodicSmiYield() returned.
@return The actual time in 100ns units that the periodic SMI handler
has been executing. If this function is not called from within
an enabled periodic SMI handler, then 0 is returned.
**/
UINT64
EFIAPI
PeriodicSmiExecutionTime (
VOID
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
UINT64 Current;
UINT64 Count;
//
// If there is no active periodic SMI handler, then return 0
//
PeriodicSmiLibraryHandler = GetActivePeriodicSmiLibraryHandler ();
if (PeriodicSmiLibraryHandler == NULL) {
return 0;
}
//
// Get the current performance counter value
//
Current = GetPerformanceCounter ();
//
// Count the number of performance counter ticks since the periodic SMI handler
// was dispatched or the last time this function was called.
//
if (PeriodicSmiLibraryHandler->PerfomanceCounterEndValue > PeriodicSmiLibraryHandler->PerfomanceCounterStartValue) {
//
// The performance counter counts up. Check for roll over condition.
//
if (Current > PeriodicSmiLibraryHandler->DispatchCheckPointTime) {
Count = Current - PeriodicSmiLibraryHandler->DispatchCheckPointTime;
} else {
Count = (Current - PeriodicSmiLibraryHandler->PerfomanceCounterStartValue) + (PeriodicSmiLibraryHandler->PerfomanceCounterEndValue - PeriodicSmiLibraryHandler->DispatchCheckPointTime);
}
} else {
//
// The performance counter counts down. Check for roll over condition.
//
if (PeriodicSmiLibraryHandler->DispatchCheckPointTime > Current) {
Count = PeriodicSmiLibraryHandler->DispatchCheckPointTime - Current;
} else {
Count = (PeriodicSmiLibraryHandler->DispatchCheckPointTime - PeriodicSmiLibraryHandler->PerfomanceCounterEndValue) + (PeriodicSmiLibraryHandler->PerfomanceCounterStartValue - Current);
}
}
//
// Accumulate the total number of performance counter ticks since the periodic
// SMI handler was dispatched or resumed.
//
PeriodicSmiLibraryHandler->DispatchTotalTime += Count;
//
// Update the checkpoint value to the current performance counter value
//
PeriodicSmiLibraryHandler->DispatchCheckPointTime = Current;
//
// Convert the total number of performance counter ticks to 100 ns units
//
return DivU64x64Remainder (
MultU64x32 (PeriodicSmiLibraryHandler->DispatchTotalTime, 10000000),
PeriodicSmiLibraryHandler->PerfomanceCounterRate,
NULL
);
}
/**
This function returns control back to the SMM Foundation. When the next
periodic SMI for the currently executing handler is triggered, the periodic
SMI handler will restarted from its registered DispatchFunction entry point.
If this function is not called from within an enabled periodic SMI handler,
then control is returned to the calling function.
**/
VOID
EFIAPI
PeriodicSmiExit (
VOID
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
//
// If there is no active periodic SMI handler, then return
//
PeriodicSmiLibraryHandler = GetActivePeriodicSmiLibraryHandler ();
if (PeriodicSmiLibraryHandler == NULL) {
return;
}
//
// Perform a long jump back to the point when the currently executing dispatch
// function was dispatched.
//
LongJump (&PeriodicSmiLibraryHandler->DispatchJumpBuffer, 1);
//
// Must never return
//
ASSERT (FALSE);
CpuDeadLoop();
}
/**
This function yields control back to the SMM Foundation. When the next
periodic SMI for the currently executing handler is triggered, the periodic
SMI handler will be resumed and this function will return. Use of this
function requires a seperate stack for the periodic SMI handler. A non zero
stack size must be specified in PeriodicSmiEnable() for this function to be
used.
If the stack size passed into PeriodicSmiEnable() was zero, the 0 is returned.
If this function is not called from within an enabled periodic SMI handler,
then 0 is returned.
@return The actual time in 100ns units elasped since this function was
called. A value of 0 indicates an unknown amount of time.
**/
UINT64
EFIAPI
PeriodicSmiYield (
VOID
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
UINTN SetJumpFlag;
//
// If there is no active periodic SMI handler, then return
//
PeriodicSmiLibraryHandler = GetActivePeriodicSmiLibraryHandler ();
if (PeriodicSmiLibraryHandler == NULL) {
return 0;
}
//
// If PeriodicSmiYield() is called without an allocated stack, then just return
// immediately with an elapsed time of 0.
//
if (PeriodicSmiLibraryHandler->Stack == NULL) {
return 0;
}
//
// Set a flag so the next periodic SMI event will resume at where SetJump()
// is called below.
//
PeriodicSmiLibraryHandler->YieldFlag = TRUE;
//
// Save context in YieldJumpBuffer
//
SetJumpFlag = SetJump (&PeriodicSmiLibraryHandler->YieldJumpBuffer);
if (SetJumpFlag == 0) {
//
// The intial call to SetJump() always returns 0.
// If this is the initial call, then exit the current periodic SMI handler
//
PeriodicSmiExit ();
}
//
// We get here when a LongJump is performed from PeriodicSmiDispatchFunctionOnCpu()
// to resume a periodic SMI handler that called PeriodicSmiYield() on the
// previous time this periodic SMI handler was dispatched.
//
// Clear the flag so the next periodic SMI dispatch will not resume.
//
PeriodicSmiLibraryHandler->YieldFlag = FALSE;
//
// Return the amount elapsed time that occured while yielded
//
return PeriodicSmiLibraryHandler->ElapsedTime;
}
/**
Internal worker function that transfers control to an enabled periodic SMI
handler. If the enabled periodic SMI handler was allocated its own stack,
then this function is called on that allocated stack through the BaseLin
function SwitchStack().
@param[in] Context1 Context1 parameter passed into SwitchStack().
@param[in] Context2 Context2 parameter passed into SwitchStack().
**/
VOID
EFIAPI
PeriodicSmiDispatchFunctionSwitchStack (
IN VOID *Context1, OPTIONAL
IN VOID *Context2 OPTIONAL
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
//
// Convert Context1 to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
//
PeriodicSmiLibraryHandler = (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *)Context1;
//
// Dispatch the registered handler passing in the context that was registered
// and the amount of time that has elapsed since the previous time this
// periodic SMI handler was dispacthed.
//
PeriodicSmiLibraryHandler->DispatchFunction (
PeriodicSmiLibraryHandler->Context,
PeriodicSmiLibraryHandler->ElapsedTime
);
//
// If this DispatchFunction() returns, then unconditially call PeriodicSmiExit()
// to perform a LongJump() back to PeriodicSmiDispatchFunctionOnCpu(). The
// LongJump() will resume exection on the original stack.
//
PeriodicSmiExit ();
}
/**
Internal worker function that transfers control to an enabled periodic SMI
handler on the specified logial CPU. This function determines if the periodic
SMI handler yielded and needs to be resumed. It also and switches to an
allocated stack if one was allocated in PeriodicSmiEnable().
@param[in] PeriodicSmiLibraryHandler A pointer to the context for the periodic
SMI handler to execute.
**/
VOID
EFIAPI
PeriodicSmiDispatchFunctionOnCpu (
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler
)
{
//
// Save context in DispatchJumpBuffer. The intial call to SetJump() always
// returns 0. If this is the initial call, then either resume from a prior
// call to PeriodicSmiYield() or call the DispatchFunction registerd in
// PeriodicSmiEnable() using an allocated stack if one was specified.
//
if (SetJump (&PeriodicSmiLibraryHandler->DispatchJumpBuffer) != 0) {
return;
}
//
// Capture the performance counter value just before the periodic SMI handler
// is resumed so the amount of time the periodic SMI handler executes can be
// calculated.
//
PeriodicSmiLibraryHandler->DispatchTotalTime = 0;
PeriodicSmiLibraryHandler->DispatchCheckPointTime = GetPerformanceCounter();
if (PeriodicSmiLibraryHandler->YieldFlag) {
//
// Perform a long jump back to the point where the previously dispatched
// function called PeriodicSmiYield().
//
LongJump (&PeriodicSmiLibraryHandler->YieldJumpBuffer, 1);
} else if (PeriodicSmiLibraryHandler->Stack == NULL) {
//
// If Stack is NULL then call DispatchFunction using current stack passing
// in the context that was registered and the amount of time that has
// elapsed since the previous time this periodic SMI handler was dispacthed.
//
PeriodicSmiLibraryHandler->DispatchFunction (
PeriodicSmiLibraryHandler->Context,
PeriodicSmiLibraryHandler->ElapsedTime
);
//
// If this DispatchFunction() returns, then unconditially call PeriodicSmiExit()
// to perform a LongJump() back to this function.
//
PeriodicSmiExit ();
} else {
//
// If Stack is not NULL then call DispatchFunction switching to the allocated stack
//
SwitchStack (
PeriodicSmiDispatchFunctionSwitchStack,
PeriodicSmiLibraryHandler,
NULL,
(UINT8 *)PeriodicSmiLibraryHandler->Stack + PeriodicSmiLibraryHandler->StackSize
);
}
//
// Must never return
//
ASSERT (FALSE);
CpuDeadLoop();
}
/**
Internal worker function that transfers control to an enabled periodic SMI
handler on the specified logial CPU. This worker function is only called
using the SMM Services Table function SmmStartupThisAp() to execute the
periodic SMI handler on a logical CPU that is different than the one that is
running the SMM Foundation. When the periodic SMI handler returns, a lock is
released to notify the CPU that is running the SMM Foundation that the periodic
SMI handler execution has finished its execution.
@param[in, out] Buffer A pointer to the context for the periodic SMI handler.
**/
VOID
EFIAPI
PeriodicSmiDispatchFunctionWithLock (
IN OUT VOID *Buffer
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
//
// Get context
//
PeriodicSmiLibraryHandler = (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *)Buffer;
//
// Execute dispatch function on the currently excuting logical CPU
//
PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler);
//
// Release the dispatch spin lock
//
ReleaseSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
}
/**
Internal worker function that transfers control to a periodic SMI handler that
was enabled using PeriodicSmiEnable().
@param[in] DispatchHandle The unique handle assigned to this handler by
SmiHandlerRegister().
@param[in] Context Points to an optional handler context which was
specified when the handler was registered.
@param[in, out] CommBuffer A pointer to a collection of data in memory that
will be conveyed from a non-SMM environment into
an SMM environment.
@param[in, out] CommBufferSize The size of the CommBuffer.
@retval EFI_SUCCESS The interrupt was handled and quiesced.
No other handlers should still be called.
@retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other
handlers should still be called.
@retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other
handlers should still be called.
@retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
**/
EFI_STATUS
EFIAPI
PeriodicSmiDispatchFunction (
IN EFI_HANDLE DispatchHandle,
IN CONST VOID *Context OPTIONAL,
IN OUT VOID *CommBuffer OPTIONAL,
IN OUT UINTN *CommBufferSize OPTIONAL
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
EFI_SMM_PERIODIC_TIMER_CONTEXT *TimerContext;
EFI_STATUS Status;
//
// Set the active periodic SMI handler
//
PeriodicSmiLibraryHandler = SetActivePeriodicSmiLibraryHandler (Context);
if (PeriodicSmiLibraryHandler == NULL) {
return EFI_NOT_FOUND;
}
//
// Retrieve the elapsed time since the last time this periodic SMI handler was called
//
PeriodicSmiLibraryHandler->ElapsedTime = 0;
if (CommBuffer != NULL) {
TimerContext = (EFI_SMM_PERIODIC_TIMER_CONTEXT *)CommBuffer;
PeriodicSmiLibraryHandler->ElapsedTime = TimerContext->ElapsedTime;
}
//
// Dispatch the periodic SMI handler
//
if ((PeriodicSmiLibraryHandler->Cpu == PERIODIC_SMI_LIBRARY_ANY_CPU) ||
(PeriodicSmiLibraryHandler->Cpu == gSmst->CurrentlyExecutingCpu) ) {
//
// Dispatch on the currently execution CPU if the CPU specified in PeriodicSmiEnable()
// was PERIODIC_SMI_LIBARRY_ANY_CPU or the currently executing CPU matches the CPU
// that was specified in PeriodicSmiEnable().
//
PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler);
} else {
//
// Acquire spin lock for ths periodic SMI handler. The AP will release the
// spin lock when it is done executing the periodic SMI handler.
//
AcquireSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
//
// Execute the periodic SMI handler on the CPU that was specified in
// PeriodicSmiEnable().
//
Status = gSmst->SmmStartupThisAp (
PeriodicSmiDispatchFunctionWithLock,
PeriodicSmiLibraryHandler->Cpu,
PeriodicSmiLibraryHandler
);
if (!EFI_ERROR (Status)) {
//
// Wait for the AP to release the spin lock.
//
while (!AcquireSpinLockOrFail (&PeriodicSmiLibraryHandler->DispatchLock)) {
CpuPause ();
}
}
//
// Release the spin lock for the periodic SMI handler.
//
ReleaseSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
}
//
// Reclaim the active periodic SMI handler if it was disabled during the current dispatch.
//
if (PeriodicSmiLibraryHandler->DispatchHandle == NULL) {
ReclaimPeriodicSmiLibraryHandler (PeriodicSmiLibraryHandler);
}
//
// Update state to show that there is no active periodic SMI handler
//
SetActivePeriodicSmiLibraryHandler (NULL);
return EFI_SUCCESS;
}
/**
This function enables a periodic SMI handler.
@param[in, out] DispatchHandle A pointer to the handle associated with the
enabled periodic SMI handler. This is an
optional parameter that may be NULL. If it is
NULL, then the handle will not be returned,
which means that the periodic SMI handler can
never be disabled.
@param[in] DispatchFunction A pointer to a periodic SMI handler function.
@param[in] Context Optional content to pass into DispatchFunction.
@param[in] TickPeriod The requested tick period in 100ns units that
control should be givien to the periodic SMI
handler. Must be one of the supported values
returned by PeriodicSmiSupportedPickPeriod().
@param[in] Cpu Specifies the CPU that is required to execute
the periodic SMI handler. If Cpu is
PERIODIC_SMI_LIBRARY_ANY_CPU, then the periodic
SMI handler will always be executed on the SMST
CurrentlyExecutingCpu, which may vary across
periodic SMIs. If Cpu is between 0 and the SMST
NumberOfCpus, then the periodic SMI will always
be executed on the requested CPU.
@param[in] StackSize The size, in bytes, of the stack to allocate for
use by the periodic SMI handler. If 0, then the
default stack will be used.
@retval EFI_INVALID_PARAMETER DispatchFunction is NULL.
@retval EFI_UNSUPPORTED TickPeriod is not a supported tick period. The
supported tick periods can be retrieved using
PeriodicSmiSupportedTickPeriod().
@retval EFI_INVALID_PARAMETER Cpu is not PERIODIC_SMI_LIBRARY_ANY_CPU or in
the range 0 to SMST NumberOfCpus.
@retval EFI_OUT_OF_RESOURCES There are not enough resources to enable the
periodic SMI handler.
@retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the
stack speficied by StackSize.
@retval EFI_SUCCESS The periodic SMI handler was enabled.
**/
EFI_STATUS
EFIAPI
PeriodicSmiEnable (
IN OUT EFI_HANDLE *DispatchHandle, OPTIONAL
IN PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction,
IN CONST VOID *Context, OPTIONAL
IN UINT64 TickPeriod,
IN UINTN Cpu,
IN UINTN StackSize
)
{
EFI_STATUS Status;
UINTN Index;
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
//
// Make sure all the input parameters are valid
//
if (DispatchFunction == NULL) {
return EFI_INVALID_PARAMETER;
}
for (Index = 0; gSmiTickPeriodTable[Index] != 0; Index++) {
if (gSmiTickPeriodTable[Index] == TickPeriod) {
break;
}
}
if (gSmiTickPeriodTable[Index] == 0) {
return EFI_UNSUPPORTED;
}
if (Cpu != PERIODIC_SMI_LIBRARY_ANY_CPU && Cpu >= gSmst->NumberOfCpus) {
return EFI_INVALID_PARAMETER;
}
//
// Find a free periodic SMI handler entry
//
PeriodicSmiLibraryHandler = FindFreePeriodicSmiLibraryHandler();
if (PeriodicSmiLibraryHandler == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Initialize a new periodic SMI handler entry
//
PeriodicSmiLibraryHandler->YieldFlag = FALSE;
PeriodicSmiLibraryHandler->DispatchHandle = NULL;
PeriodicSmiLibraryHandler->DispatchFunction = DispatchFunction;
PeriodicSmiLibraryHandler->Context = (VOID *)Context;
PeriodicSmiLibraryHandler->Cpu = Cpu;
PeriodicSmiLibraryHandler->StackSize = ALIGN_VALUE (StackSize, EFI_PAGE_SIZE);
if (PeriodicSmiLibraryHandler->StackSize > 0) {
PeriodicSmiLibraryHandler->Stack = AllocatePages (EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler->StackSize));
if (PeriodicSmiLibraryHandler->Stack == NULL) {
return EFI_OUT_OF_RESOURCES;
}
ZeroMem (PeriodicSmiLibraryHandler->Stack, PeriodicSmiLibraryHandler->StackSize);
} else {
PeriodicSmiLibraryHandler->Stack = NULL;
}
InitializeSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
PeriodicSmiLibraryHandler->PerfomanceCounterRate = GetPerformanceCounterProperties (
&PeriodicSmiLibraryHandler->PerfomanceCounterStartValue,
&PeriodicSmiLibraryHandler->PerfomanceCounterEndValue
);
PeriodicSmiLibraryHandler->RegisterContext.Period = TickPeriod;
PeriodicSmiLibraryHandler->RegisterContext.SmiTickInterval = TickPeriod;
Status = gSmmPeriodicTimerDispatch2->Register (
gSmmPeriodicTimerDispatch2,
PeriodicSmiDispatchFunction,
&PeriodicSmiLibraryHandler->RegisterContext,
&PeriodicSmiLibraryHandler->DispatchHandle
);
if (EFI_ERROR (Status)) {
PeriodicSmiLibraryHandler->DispatchHandle = NULL;
ReclaimPeriodicSmiLibraryHandler (PeriodicSmiLibraryHandler);
return EFI_OUT_OF_RESOURCES;
}
//
// Return the registered handle if the optional DispatchHandle parameter is not NULL
//
if (DispatchHandle != NULL) {
*DispatchHandle = PeriodicSmiLibraryHandler->DispatchHandle;
}
return EFI_SUCCESS;
}
/**
This function disables a periodic SMI handler that has been previously
enabled with PeriodicSmiEnable().
@param[in] DispatchHandle A handle associated with a previously enabled periodic
SMI handler. This is an optional parameter that may
be NULL. If it is NULL, then the active periodic SMI
handlers is disabled.
@retval FALSE DispatchHandle is NULL and there is no active periodic SMI handler.
@retval FALSE The periodic SMI handler specified by DispatchHandle has
not been enabled with PeriodicSmiEnable().
@retval TRUE The periodic SMI handler specified by DispatchHandle has
been disabled. If DispatchHandle is NULL, then the active
periodic SMI handler has been disabled.
**/
BOOLEAN
EFIAPI
PeriodicSmiDisable (
IN EFI_HANDLE DispatchHandle OPTIONAL
)
{
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
EFI_STATUS Status;
//
// Lookup the periodic SMI handler specified by DispatchHandle
//
PeriodicSmiLibraryHandler = LookupPeriodicSmiLibraryHandler (DispatchHandle);
if (PeriodicSmiLibraryHandler == NULL) {
return FALSE;
}
//
// Unregister the periodic SMI handler from the SMM Periodic Timer Dispatch 2 Protocol
//
Status = gSmmPeriodicTimerDispatch2->UnRegister (
gSmmPeriodicTimerDispatch2,
PeriodicSmiLibraryHandler->DispatchHandle
);
if (EFI_ERROR (Status)) {
return FALSE;
}
//
// Mark the entry for the disabled periodic SMI handler as free, and
// call ReclaimPeriodicSmiLibraryHandler to move it to the list of free
// periodic SMI handlers.
//
PeriodicSmiLibraryHandler->DispatchHandle = NULL;
if (PeriodicSmiLibraryHandler != GetActivePeriodicSmiLibraryHandler ()) {
ReclaimPeriodicSmiLibraryHandler (PeriodicSmiLibraryHandler);
}
return TRUE;
}
/**
This constructor function caches the pointer to the SMM Periodic Timer
Dispatch 2 Protocol and collects the list SMI tick rates that the hardware
supports.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
**/
EFI_STATUS
EFIAPI
SmmPeriodicSmiLibConstructor (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
UINT64 *SmiTickInterval;
UINTN Count;
//
// Locate the SMM Periodic Timer Dispatch 2 Protocol
//
Status = gSmst->SmmLocateProtocol (
&gEfiSmmPeriodicTimerDispatch2ProtocolGuid,
NULL,
(VOID **)&gSmmPeriodicTimerDispatch2
);
ASSERT_EFI_ERROR (Status);
ASSERT (gSmmPeriodicTimerDispatch2 != NULL);
//
// Count the number of periodic SMI tick intervals that the SMM Periodic Timer
// Dipatch 2 Protocol supports.
//
SmiTickInterval = NULL;
Count = 0;
do {
Status = gSmmPeriodicTimerDispatch2->GetNextShorterInterval (
gSmmPeriodicTimerDispatch2,
&SmiTickInterval
);
Count++;
} while (SmiTickInterval != NULL);
//
// Allocate a buffer for the table of supported periodic SMI tick periods.
//
gSmiTickPeriodTable = AllocateZeroPool (Count * sizeof (UINT64));
ASSERT (gSmiTickPeriodTable != NULL);
//
// Fill in the table of supported periodic SMI tick periods.
//
SmiTickInterval = NULL;
Count = 0;
do {
gSmiTickPeriodTable[Count] = 0;
Status = gSmmPeriodicTimerDispatch2->GetNextShorterInterval (
gSmmPeriodicTimerDispatch2,
&SmiTickInterval
);
if (SmiTickInterval != NULL) {
gSmiTickPeriodTable[Count] = *SmiTickInterval;
}
Count++;
} while (SmiTickInterval != NULL);
//
// Allocate buffer for initial set of periodic SMI handlers
//
EnlargeFreePeriodicSmiLibraryHandlerList ();
return EFI_SUCCESS;
}
/**
The constructor function caches the pointer to the SMM Periodic Timer Dispatch 2
Protocol and collects the list SMI tick rates that the hardware supports.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
**/
EFI_STATUS
EFIAPI
SmmPeriodicSmiLibDestructor (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
LIST_ENTRY *Link;
PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
//
// Free the table of supported periodic SMI tick rates
//
if (gSmiTickPeriodTable != NULL) {
FreePool (gSmiTickPeriodTable);
}
//
// Disable all periodic SMI handlers
//
for (Link = GetFirstNode (&gPeriodicSmiLibraryHandlers); !IsNull (&gPeriodicSmiLibraryHandlers, Link);) {
PeriodicSmiLibraryHandler = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (Link);
Link = GetNextNode (&gPeriodicSmiLibraryHandlers, Link);
PeriodicSmiDisable (PeriodicSmiLibraryHandler->DispatchHandle);
}
//
// Free all the periodic SMI handler entries
//
for (Link = GetFirstNode (&gFreePeriodicSmiLibraryHandlers); !IsNull (&gFreePeriodicSmiLibraryHandlers, Link);) {
PeriodicSmiLibraryHandler = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (Link);
Link = RemoveEntryList (Link);
FreePool (PeriodicSmiLibraryHandler);
}
return EFI_SUCCESS;
}
|