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
|
/** @file
UEFI Event support functions implemented in this file.
Copyright (c) 2006 - 2012, 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 "DxeMain.h"
#include "Event.h"
///
/// gEfiCurrentTpl - Current Task priority level
///
EFI_TPL gEfiCurrentTpl = TPL_APPLICATION;
///
/// gEventQueueLock - Protects the event queues
///
EFI_LOCK gEventQueueLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL);
///
/// gEventQueue - A list of event's to notify for each priority level
///
LIST_ENTRY gEventQueue[TPL_HIGH_LEVEL + 1];
///
/// gEventPending - A bitmask of the EventQueues that are pending
///
UINTN gEventPending = 0;
///
/// gEventSignalQueue - A list of events to signal based on EventGroup type
///
LIST_ENTRY gEventSignalQueue = INITIALIZE_LIST_HEAD_VARIABLE (gEventSignalQueue);
///
/// Enumerate the valid types
///
UINT32 mEventTable[] = {
///
/// 0x80000200 Timer event with a notification function that is
/// queue when the event is signaled with SignalEvent()
///
EVT_TIMER | EVT_NOTIFY_SIGNAL,
///
/// 0x80000000 Timer event without a notification function. It can be
/// signaled with SignalEvent() and checked with CheckEvent() or WaitForEvent().
///
EVT_TIMER,
///
/// 0x00000100 Generic event with a notification function that
/// can be waited on with CheckEvent() or WaitForEvent()
///
EVT_NOTIFY_WAIT,
///
/// 0x00000200 Generic event with a notification function that
/// is queue when the event is signaled with SignalEvent()
///
EVT_NOTIFY_SIGNAL,
///
/// 0x00000201 ExitBootServicesEvent.
///
EVT_SIGNAL_EXIT_BOOT_SERVICES,
///
/// 0x60000202 SetVirtualAddressMapEvent.
///
EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
///
/// 0x00000000 Generic event without a notification function.
/// It can be signaled with SignalEvent() and checked with CheckEvent()
/// or WaitForEvent().
///
0x00000000,
///
/// 0x80000100 Timer event with a notification function that can be
/// waited on with CheckEvent() or WaitForEvent()
///
EVT_TIMER | EVT_NOTIFY_WAIT,
};
///
/// gIdleLoopEvent - Event which is signalled when the core is idle
///
EFI_EVENT gIdleLoopEvent = NULL;
/**
Enter critical section by acquiring the lock on gEventQueueLock.
**/
VOID
CoreAcquireEventLock (
VOID
)
{
CoreAcquireLock (&gEventQueueLock);
}
/**
Exit critical section by releasing the lock on gEventQueueLock.
**/
VOID
CoreReleaseEventLock (
VOID
)
{
CoreReleaseLock (&gEventQueueLock);
}
/**
Initializes "event" support.
@retval EFI_SUCCESS Always return success
**/
EFI_STATUS
CoreInitializeEventServices (
VOID
)
{
UINTN Index;
for (Index=0; Index <= TPL_HIGH_LEVEL; Index++) {
InitializeListHead (&gEventQueue[Index]);
}
CoreInitializeTimer ();
CoreCreateEventEx (
EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
CoreEmptyCallbackFunction,
NULL,
&gIdleLoopEventGuid,
&gIdleLoopEvent
);
return EFI_SUCCESS;
}
/**
Dispatches all pending events.
@param Priority The task priority level of event notifications
to dispatch
**/
VOID
CoreDispatchEventNotifies (
IN EFI_TPL Priority
)
{
IEVENT *Event;
LIST_ENTRY *Head;
CoreAcquireEventLock ();
ASSERT (gEventQueueLock.OwnerTpl == Priority);
Head = &gEventQueue[Priority];
//
// Dispatch all the pending notifications
//
while (!IsListEmpty (Head)) {
Event = CR (Head->ForwardLink, IEVENT, NotifyLink, EVENT_SIGNATURE);
RemoveEntryList (&Event->NotifyLink);
Event->NotifyLink.ForwardLink = NULL;
//
// Only clear the SIGNAL status if it is a SIGNAL type event.
// WAIT type events are only cleared in CheckEvent()
//
if ((Event->Type & EVT_NOTIFY_SIGNAL) != 0) {
Event->SignalCount = 0;
}
CoreReleaseEventLock ();
//
// Notify this event
//
ASSERT (Event->NotifyFunction != NULL);
Event->NotifyFunction (Event, Event->NotifyContext);
//
// Check for next pending event
//
CoreAcquireEventLock ();
}
gEventPending &= ~(1 << Priority);
CoreReleaseEventLock ();
}
/**
Queues the event's notification function to fire.
@param Event The Event to notify
**/
VOID
CoreNotifyEvent (
IN IEVENT *Event
)
{
//
// Event database must be locked
//
ASSERT_LOCKED (&gEventQueueLock);
//
// If the event is queued somewhere, remove it
//
if (Event->NotifyLink.ForwardLink != NULL) {
RemoveEntryList (&Event->NotifyLink);
Event->NotifyLink.ForwardLink = NULL;
}
//
// Queue the event to the pending notification list
//
InsertTailList (&gEventQueue[Event->NotifyTpl], &Event->NotifyLink);
gEventPending |= (UINTN)(1 << Event->NotifyTpl);
}
/**
Signals all events in the EventGroup.
@param EventGroup The list to signal
**/
VOID
CoreNotifySignalList (
IN EFI_GUID *EventGroup
)
{
LIST_ENTRY *Link;
LIST_ENTRY *Head;
IEVENT *Event;
CoreAcquireEventLock ();
Head = &gEventSignalQueue;
for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
Event = CR (Link, IEVENT, SignalLink, EVENT_SIGNATURE);
if (CompareGuid (&Event->EventGroup, EventGroup)) {
CoreNotifyEvent (Event);
}
}
CoreReleaseEventLock ();
}
/**
Creates an event.
@param Type The type of event to create and its mode and
attributes
@param NotifyTpl The task priority level of event notifications
@param NotifyFunction Pointer to the events notification function
@param NotifyContext Pointer to the notification functions context;
corresponds to parameter "Context" in the
notification function
@param Event Pointer to the newly created event if the call
succeeds; undefined otherwise
@retval EFI_SUCCESS The event structure was created
@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
@retval EFI_OUT_OF_RESOURCES The event could not be allocated
**/
EFI_STATUS
EFIAPI
CoreCreateEvent (
IN UINT32 Type,
IN EFI_TPL NotifyTpl,
IN EFI_EVENT_NOTIFY NotifyFunction, OPTIONAL
IN VOID *NotifyContext, OPTIONAL
OUT EFI_EVENT *Event
)
{
return CoreCreateEventEx (Type, NotifyTpl, NotifyFunction, NotifyContext, NULL, Event);
}
/**
Creates an event in a group.
@param Type The type of event to create and its mode and
attributes
@param NotifyTpl The task priority level of event notifications
@param NotifyFunction Pointer to the events notification function
@param NotifyContext Pointer to the notification functions context;
corresponds to parameter "Context" in the
notification function
@param EventGroup GUID for EventGroup if NULL act the same as
gBS->CreateEvent().
@param Event Pointer to the newly created event if the call
succeeds; undefined otherwise
@retval EFI_SUCCESS The event structure was created
@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
@retval EFI_OUT_OF_RESOURCES The event could not be allocated
**/
EFI_STATUS
EFIAPI
CoreCreateEventEx (
IN UINT32 Type,
IN EFI_TPL NotifyTpl,
IN EFI_EVENT_NOTIFY NotifyFunction, OPTIONAL
IN CONST VOID *NotifyContext, OPTIONAL
IN CONST EFI_GUID *EventGroup, OPTIONAL
OUT EFI_EVENT *Event
)
{
//
// If it's a notify type of event, check for invalid NotifyTpl
//
if ((Type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) != 0) {
if (NotifyTpl != TPL_APPLICATION &&
NotifyTpl != TPL_CALLBACK &&
NotifyTpl != TPL_NOTIFY) {
return EFI_INVALID_PARAMETER;
}
}
return CoreCreateEventInternal (Type, NotifyTpl, NotifyFunction, NotifyContext, EventGroup, Event);
}
/**
Creates a general-purpose event structure
@param Type The type of event to create and its mode and
attributes
@param NotifyTpl The task priority level of event notifications
@param NotifyFunction Pointer to the events notification function
@param NotifyContext Pointer to the notification functions context;
corresponds to parameter "Context" in the
notification function
@param EventGroup GUID for EventGroup if NULL act the same as
gBS->CreateEvent().
@param Event Pointer to the newly created event if the call
succeeds; undefined otherwise
@retval EFI_SUCCESS The event structure was created
@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
@retval EFI_OUT_OF_RESOURCES The event could not be allocated
**/
EFI_STATUS
EFIAPI
CoreCreateEventInternal (
IN UINT32 Type,
IN EFI_TPL NotifyTpl,
IN EFI_EVENT_NOTIFY NotifyFunction, OPTIONAL
IN CONST VOID *NotifyContext, OPTIONAL
IN CONST EFI_GUID *EventGroup, OPTIONAL
OUT EFI_EVENT *Event
)
{
EFI_STATUS Status;
IEVENT *IEvent;
INTN Index;
if (Event == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// Check to make sure no reserved flags are set
//
Status = EFI_INVALID_PARAMETER;
for (Index = 0; Index < (sizeof (mEventTable) / sizeof (UINT32)); Index++) {
if (Type == mEventTable[Index]) {
Status = EFI_SUCCESS;
break;
}
}
if(EFI_ERROR (Status)) {
return EFI_INVALID_PARAMETER;
}
//
// Convert Event type for pre-defined Event groups
//
if (EventGroup != NULL) {
//
// For event group, type EVT_SIGNAL_EXIT_BOOT_SERVICES and EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
// are not valid
//
if ((Type == EVT_SIGNAL_EXIT_BOOT_SERVICES) || (Type == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)) {
return EFI_INVALID_PARAMETER;
}
if (CompareGuid (EventGroup, &gEfiEventExitBootServicesGuid)) {
Type = EVT_SIGNAL_EXIT_BOOT_SERVICES;
} else if (CompareGuid (EventGroup, &gEfiEventVirtualAddressChangeGuid)) {
Type = EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE;
}
} else {
//
// Convert EFI 1.10 Events to their UEFI 2.0 CreateEventEx mapping
//
if (Type == EVT_SIGNAL_EXIT_BOOT_SERVICES) {
EventGroup = &gEfiEventExitBootServicesGuid;
} else if (Type == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {
EventGroup = &gEfiEventVirtualAddressChangeGuid;
}
}
//
// If it's a notify type of event, check its parameters
//
if ((Type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) != 0) {
//
// Check for an invalid NotifyFunction or NotifyTpl
//
if ((NotifyFunction == NULL) ||
(NotifyTpl <= TPL_APPLICATION) ||
(NotifyTpl >= TPL_HIGH_LEVEL)) {
return EFI_INVALID_PARAMETER;
}
} else {
//
// No notification needed, zero ignored values
//
NotifyTpl = 0;
NotifyFunction = NULL;
NotifyContext = NULL;
}
//
// Allocate and initialize a new event structure.
//
if ((Type & EVT_RUNTIME) != 0) {
IEvent = AllocateRuntimeZeroPool (sizeof (IEVENT));
} else {
IEvent = AllocateZeroPool (sizeof (IEVENT));
}
if (IEvent == NULL) {
return EFI_OUT_OF_RESOURCES;
}
IEvent->Signature = EVENT_SIGNATURE;
IEvent->Type = Type;
IEvent->NotifyTpl = NotifyTpl;
IEvent->NotifyFunction = NotifyFunction;
IEvent->NotifyContext = (VOID *)NotifyContext;
if (EventGroup != NULL) {
CopyGuid (&IEvent->EventGroup, EventGroup);
IEvent->ExFlag = TRUE;
}
*Event = IEvent;
if ((Type & EVT_RUNTIME) != 0) {
//
// Keep a list of all RT events so we can tell the RT AP.
//
IEvent->RuntimeData.Type = Type;
IEvent->RuntimeData.NotifyTpl = NotifyTpl;
IEvent->RuntimeData.NotifyFunction = NotifyFunction;
IEvent->RuntimeData.NotifyContext = (VOID *) NotifyContext;
IEvent->RuntimeData.Event = (EFI_EVENT *) IEvent;
InsertTailList (&gRuntime->EventHead, &IEvent->RuntimeData.Link);
}
CoreAcquireEventLock ();
if ((Type & EVT_NOTIFY_SIGNAL) != 0x00000000) {
//
// The Event's NotifyFunction must be queued whenever the event is signaled
//
InsertHeadList (&gEventSignalQueue, &IEvent->SignalLink);
}
CoreReleaseEventLock ();
//
// Done
//
return EFI_SUCCESS;
}
/**
Signals the event. Queues the event to be notified if needed.
@param UserEvent The event to signal .
@retval EFI_INVALID_PARAMETER Parameters are not valid.
@retval EFI_SUCCESS The event was signaled.
**/
EFI_STATUS
EFIAPI
CoreSignalEvent (
IN EFI_EVENT UserEvent
)
{
IEVENT *Event;
Event = UserEvent;
if (Event == NULL) {
return EFI_INVALID_PARAMETER;
}
if (Event->Signature != EVENT_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
CoreAcquireEventLock ();
//
// If the event is not already signalled, do so
//
if (Event->SignalCount == 0x00000000) {
Event->SignalCount++;
//
// If signalling type is a notify function, queue it
//
if ((Event->Type & EVT_NOTIFY_SIGNAL) != 0) {
if (Event->ExFlag) {
//
// The CreateEventEx() style requires all members of the Event Group
// to be signaled.
//
CoreReleaseEventLock ();
CoreNotifySignalList (&Event->EventGroup);
CoreAcquireEventLock ();
} else {
CoreNotifyEvent (Event);
}
}
}
CoreReleaseEventLock ();
return EFI_SUCCESS;
}
/**
Check the status of an event.
@param UserEvent The event to check
@retval EFI_SUCCESS The event is in the signaled state
@retval EFI_NOT_READY The event is not in the signaled state
@retval EFI_INVALID_PARAMETER Event is of type EVT_NOTIFY_SIGNAL
**/
EFI_STATUS
EFIAPI
CoreCheckEvent (
IN EFI_EVENT UserEvent
)
{
IEVENT *Event;
EFI_STATUS Status;
Event = UserEvent;
if (Event == NULL) {
return EFI_INVALID_PARAMETER;
}
if (Event->Signature != EVENT_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
if ((Event->Type & EVT_NOTIFY_SIGNAL) != 0) {
return EFI_INVALID_PARAMETER;
}
Status = EFI_NOT_READY;
if ((Event->SignalCount == 0) && ((Event->Type & EVT_NOTIFY_WAIT) != 0)) {
//
// Queue the wait notify function
//
CoreAcquireEventLock ();
if (Event->SignalCount == 0) {
CoreNotifyEvent (Event);
}
CoreReleaseEventLock ();
}
//
// If the even looks signalled, get the lock and clear it
//
if (Event->SignalCount != 0) {
CoreAcquireEventLock ();
if (Event->SignalCount != 0) {
Event->SignalCount = 0;
Status = EFI_SUCCESS;
}
CoreReleaseEventLock ();
}
return Status;
}
/**
Stops execution until an event is signaled.
@param NumberOfEvents The number of events in the UserEvents array
@param UserEvents An array of EFI_EVENT
@param UserIndex Pointer to the index of the event which
satisfied the wait condition
@retval EFI_SUCCESS The event indicated by Index was signaled.
@retval EFI_INVALID_PARAMETER The event indicated by Index has a notification
function or Event was not a valid type
@retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION
**/
EFI_STATUS
EFIAPI
CoreWaitForEvent (
IN UINTN NumberOfEvents,
IN EFI_EVENT *UserEvents,
OUT UINTN *UserIndex
)
{
EFI_STATUS Status;
UINTN Index;
//
// Can only WaitForEvent at TPL_APPLICATION
//
if (gEfiCurrentTpl != TPL_APPLICATION) {
return EFI_UNSUPPORTED;
}
for(;;) {
for(Index = 0; Index < NumberOfEvents; Index++) {
Status = CoreCheckEvent (UserEvents[Index]);
//
// provide index of event that caused problem
//
if (Status != EFI_NOT_READY) {
*UserIndex = Index;
return Status;
}
}
//
// Signal the Idle event
//
CoreSignalEvent (gIdleLoopEvent);
}
}
/**
Closes an event and frees the event structure.
@param UserEvent Event to close
@retval EFI_INVALID_PARAMETER Parameters are not valid.
@retval EFI_SUCCESS The event has been closed
**/
EFI_STATUS
EFIAPI
CoreCloseEvent (
IN EFI_EVENT UserEvent
)
{
EFI_STATUS Status;
IEVENT *Event;
Event = UserEvent;
if (Event == NULL) {
return EFI_INVALID_PARAMETER;
}
if (Event->Signature != EVENT_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
//
// If it's a timer event, make sure it's not pending
//
if ((Event->Type & EVT_TIMER) != 0) {
CoreSetTimer (Event, TimerCancel, 0);
}
CoreAcquireEventLock ();
//
// If the event is queued somewhere, remove it
//
if (Event->RuntimeData.Link.ForwardLink != NULL) {
RemoveEntryList (&Event->RuntimeData.Link);
}
if (Event->NotifyLink.ForwardLink != NULL) {
RemoveEntryList (&Event->NotifyLink);
}
if (Event->SignalLink.ForwardLink != NULL) {
RemoveEntryList (&Event->SignalLink);
}
CoreReleaseEventLock ();
//
// If the event is registered on a protocol notify, then remove it from the protocol database
//
CoreUnregisterProtocolNotify (Event);
Status = CoreFreePool (Event);
ASSERT_EFI_ERROR (Status);
return Status;
}
|