2 UEFI Event support functions implemented in this file.
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
15 /// gEfiCurrentTpl - Current Task priority level
17 EFI_TPL gEfiCurrentTpl
= TPL_APPLICATION
;
20 /// gEventQueueLock - Protects the event queues
22 EFI_LOCK gEventQueueLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
);
25 /// gEventQueue - A list of event's to notify for each priority level
27 LIST_ENTRY gEventQueue
[TPL_HIGH_LEVEL
+ 1];
30 /// gEventPending - A bitmask of the EventQueues that are pending
32 UINTN gEventPending
= 0;
35 /// gEventSignalQueue - A list of events to signal based on EventGroup type
37 LIST_ENTRY gEventSignalQueue
= INITIALIZE_LIST_HEAD_VARIABLE (gEventSignalQueue
);
40 /// Enumerate the valid types
42 UINT32 mEventTable
[] = {
44 /// 0x80000200 Timer event with a notification function that is
45 /// queue when the event is signaled with SignalEvent()
47 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
49 /// 0x80000000 Timer event without a notification function. It can be
50 /// signaled with SignalEvent() and checked with CheckEvent() or WaitForEvent().
54 /// 0x00000100 Generic event with a notification function that
55 /// can be waited on with CheckEvent() or WaitForEvent()
59 /// 0x00000200 Generic event with a notification function that
60 /// is queue when the event is signaled with SignalEvent()
64 /// 0x00000201 ExitBootServicesEvent.
66 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
68 /// 0x60000202 SetVirtualAddressMapEvent.
70 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
73 /// 0x00000000 Generic event without a notification function.
74 /// It can be signaled with SignalEvent() and checked with CheckEvent()
75 /// or WaitForEvent().
79 /// 0x80000100 Timer event with a notification function that can be
80 /// waited on with CheckEvent() or WaitForEvent()
82 EVT_TIMER
| EVT_NOTIFY_WAIT
,
86 /// gIdleLoopEvent - Event which is signalled when the core is idle
88 EFI_EVENT gIdleLoopEvent
= NULL
;
92 Enter critical section by acquiring the lock on gEventQueueLock.
96 CoreAcquireEventLock (
100 CoreAcquireLock (&gEventQueueLock
);
105 Exit critical section by releasing the lock on gEventQueueLock.
109 CoreReleaseEventLock (
113 CoreReleaseLock (&gEventQueueLock
);
119 Initializes "event" support.
121 @retval EFI_SUCCESS Always return success
125 CoreInitializeEventServices (
131 for (Index
=0; Index
<= TPL_HIGH_LEVEL
; Index
++) {
132 InitializeListHead (&gEventQueue
[Index
]);
135 CoreInitializeTimer ();
140 EfiEventEmptyFunction
,
152 Dispatches all pending events.
154 @param Priority The task priority level of event notifications
159 CoreDispatchEventNotifies (
166 CoreAcquireEventLock ();
167 ASSERT (gEventQueueLock
.OwnerTpl
== Priority
);
168 Head
= &gEventQueue
[Priority
];
171 // Dispatch all the pending notifications
173 while (!IsListEmpty (Head
)) {
175 Event
= CR (Head
->ForwardLink
, IEVENT
, NotifyLink
, EVENT_SIGNATURE
);
176 RemoveEntryList (&Event
->NotifyLink
);
178 Event
->NotifyLink
.ForwardLink
= NULL
;
181 // Only clear the SIGNAL status if it is a SIGNAL type event.
182 // WAIT type events are only cleared in CheckEvent()
184 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
185 Event
->SignalCount
= 0;
188 CoreReleaseEventLock ();
193 ASSERT (Event
->NotifyFunction
!= NULL
);
194 Event
->NotifyFunction (Event
, Event
->NotifyContext
);
197 // Check for next pending event
199 CoreAcquireEventLock ();
202 gEventPending
&= ~(UINTN
)(1 << Priority
);
203 CoreReleaseEventLock ();
209 Queues the event's notification function to fire.
211 @param Event The Event to notify
221 // Event database must be locked
223 ASSERT_LOCKED (&gEventQueueLock
);
226 // If the event is queued somewhere, remove it
229 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
230 RemoveEntryList (&Event
->NotifyLink
);
231 Event
->NotifyLink
.ForwardLink
= NULL
;
235 // Queue the event to the pending notification list
238 InsertTailList (&gEventQueue
[Event
->NotifyTpl
], &Event
->NotifyLink
);
239 gEventPending
|= (UINTN
)(1 << Event
->NotifyTpl
);
246 Signals all events in the EventGroup.
248 @param EventGroup The list to signal
252 CoreNotifySignalList (
253 IN EFI_GUID
*EventGroup
260 CoreAcquireEventLock ();
262 Head
= &gEventSignalQueue
;
263 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
264 Event
= CR (Link
, IEVENT
, SignalLink
, EVENT_SIGNATURE
);
265 if (CompareGuid (&Event
->EventGroup
, EventGroup
)) {
266 CoreNotifyEvent (Event
);
270 CoreReleaseEventLock ();
277 @param Type The type of event to create and its mode and
279 @param NotifyTpl The task priority level of event notifications
280 @param NotifyFunction Pointer to the events notification function
281 @param NotifyContext Pointer to the notification functions context;
282 corresponds to parameter "Context" in the
283 notification function
284 @param Event Pointer to the newly created event if the call
285 succeeds; undefined otherwise
287 @retval EFI_SUCCESS The event structure was created
288 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
289 @retval EFI_OUT_OF_RESOURCES The event could not be allocated
296 IN EFI_TPL NotifyTpl
,
297 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
298 IN VOID
*NotifyContext
, OPTIONAL
302 return CoreCreateEventEx (Type
, NotifyTpl
, NotifyFunction
, NotifyContext
, NULL
, Event
);
308 Creates an event in a group.
310 @param Type The type of event to create and its mode and
312 @param NotifyTpl The task priority level of event notifications
313 @param NotifyFunction Pointer to the events notification function
314 @param NotifyContext Pointer to the notification functions context;
315 corresponds to parameter "Context" in the
316 notification function
317 @param EventGroup GUID for EventGroup if NULL act the same as
319 @param Event Pointer to the newly created event if the call
320 succeeds; undefined otherwise
322 @retval EFI_SUCCESS The event structure was created
323 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
324 @retval EFI_OUT_OF_RESOURCES The event could not be allocated
331 IN EFI_TPL NotifyTpl
,
332 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
333 IN CONST VOID
*NotifyContext
, OPTIONAL
334 IN CONST EFI_GUID
*EventGroup
, OPTIONAL
339 // If it's a notify type of event, check for invalid NotifyTpl
341 if ((Type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
)) != 0) {
342 if (NotifyTpl
!= TPL_APPLICATION
&&
343 NotifyTpl
!= TPL_CALLBACK
&&
344 NotifyTpl
!= TPL_NOTIFY
) {
345 return EFI_INVALID_PARAMETER
;
349 return CoreCreateEventInternal (Type
, NotifyTpl
, NotifyFunction
, NotifyContext
, EventGroup
, Event
);
353 Creates a general-purpose event structure
355 @param Type The type of event to create and its mode and
357 @param NotifyTpl The task priority level of event notifications
358 @param NotifyFunction Pointer to the events notification function
359 @param NotifyContext Pointer to the notification functions context;
360 corresponds to parameter "Context" in the
361 notification function
362 @param EventGroup GUID for EventGroup if NULL act the same as
364 @param Event Pointer to the newly created event if the call
365 succeeds; undefined otherwise
367 @retval EFI_SUCCESS The event structure was created
368 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
369 @retval EFI_OUT_OF_RESOURCES The event could not be allocated
374 CoreCreateEventInternal (
376 IN EFI_TPL NotifyTpl
,
377 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
378 IN CONST VOID
*NotifyContext
, OPTIONAL
379 IN CONST EFI_GUID
*EventGroup
, OPTIONAL
389 return EFI_INVALID_PARAMETER
;
393 // Check to make sure no reserved flags are set
395 Status
= EFI_INVALID_PARAMETER
;
396 for (Index
= 0; Index
< (sizeof (mEventTable
) / sizeof (UINT32
)); Index
++) {
397 if (Type
== mEventTable
[Index
]) {
398 Status
= EFI_SUCCESS
;
402 if(EFI_ERROR (Status
)) {
403 return EFI_INVALID_PARAMETER
;
407 // Convert Event type for pre-defined Event groups
409 if (EventGroup
!= NULL
) {
411 // For event group, type EVT_SIGNAL_EXIT_BOOT_SERVICES and EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
414 if ((Type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
) || (Type
== EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
)) {
415 return EFI_INVALID_PARAMETER
;
417 if (CompareGuid (EventGroup
, &gEfiEventExitBootServicesGuid
)) {
418 Type
= EVT_SIGNAL_EXIT_BOOT_SERVICES
;
419 } else if (CompareGuid (EventGroup
, &gEfiEventVirtualAddressChangeGuid
)) {
420 Type
= EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
;
424 // Convert EFI 1.10 Events to their UEFI 2.0 CreateEventEx mapping
426 if (Type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
) {
427 EventGroup
= &gEfiEventExitBootServicesGuid
;
428 } else if (Type
== EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
429 EventGroup
= &gEfiEventVirtualAddressChangeGuid
;
434 // If it's a notify type of event, check its parameters
436 if ((Type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
)) != 0) {
438 // Check for an invalid NotifyFunction or NotifyTpl
440 if ((NotifyFunction
== NULL
) ||
441 (NotifyTpl
<= TPL_APPLICATION
) ||
442 (NotifyTpl
>= TPL_HIGH_LEVEL
)) {
443 return EFI_INVALID_PARAMETER
;
448 // No notification needed, zero ignored values
451 NotifyFunction
= NULL
;
452 NotifyContext
= NULL
;
456 // Allocate and initialize a new event structure.
458 if ((Type
& EVT_RUNTIME
) != 0) {
459 IEvent
= AllocateRuntimeZeroPool (sizeof (IEVENT
));
461 IEvent
= AllocateZeroPool (sizeof (IEVENT
));
463 if (IEvent
== NULL
) {
464 return EFI_OUT_OF_RESOURCES
;
467 IEvent
->Signature
= EVENT_SIGNATURE
;
470 IEvent
->NotifyTpl
= NotifyTpl
;
471 IEvent
->NotifyFunction
= NotifyFunction
;
472 IEvent
->NotifyContext
= (VOID
*)NotifyContext
;
473 if (EventGroup
!= NULL
) {
474 CopyGuid (&IEvent
->EventGroup
, EventGroup
);
475 IEvent
->ExFlag
|= EVT_EXFLAG_EVENT_GROUP
;
480 if ((Type
& EVT_RUNTIME
) != 0) {
482 // Keep a list of all RT events so we can tell the RT AP.
484 IEvent
->RuntimeData
.Type
= Type
;
485 IEvent
->RuntimeData
.NotifyTpl
= NotifyTpl
;
486 IEvent
->RuntimeData
.NotifyFunction
= NotifyFunction
;
487 IEvent
->RuntimeData
.NotifyContext
= (VOID
*) NotifyContext
;
488 IEvent
->RuntimeData
.Event
= (EFI_EVENT
*) IEvent
;
489 InsertTailList (&gRuntime
->EventHead
, &IEvent
->RuntimeData
.Link
);
492 CoreAcquireEventLock ();
494 if ((Type
& EVT_NOTIFY_SIGNAL
) != 0x00000000) {
496 // The Event's NotifyFunction must be queued whenever the event is signaled
498 InsertHeadList (&gEventSignalQueue
, &IEvent
->SignalLink
);
501 CoreReleaseEventLock ();
513 Signals the event. Queues the event to be notified if needed.
515 @param UserEvent The event to signal .
517 @retval EFI_INVALID_PARAMETER Parameters are not valid.
518 @retval EFI_SUCCESS The event was signaled.
524 IN EFI_EVENT UserEvent
532 return EFI_INVALID_PARAMETER
;
535 if (Event
->Signature
!= EVENT_SIGNATURE
) {
536 return EFI_INVALID_PARAMETER
;
539 CoreAcquireEventLock ();
542 // If the event is not already signalled, do so
545 if (Event
->SignalCount
== 0x00000000) {
546 Event
->SignalCount
++;
549 // If signalling type is a notify function, queue it
551 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
552 if ((Event
->ExFlag
& EVT_EXFLAG_EVENT_GROUP
) != 0) {
554 // The CreateEventEx() style requires all members of the Event Group
557 CoreReleaseEventLock ();
558 CoreNotifySignalList (&Event
->EventGroup
);
559 CoreAcquireEventLock ();
561 CoreNotifyEvent (Event
);
566 CoreReleaseEventLock ();
573 Check the status of an event.
575 @param UserEvent The event to check
577 @retval EFI_SUCCESS The event is in the signaled state
578 @retval EFI_NOT_READY The event is not in the signaled state
579 @retval EFI_INVALID_PARAMETER Event is of type EVT_NOTIFY_SIGNAL
585 IN EFI_EVENT UserEvent
594 return EFI_INVALID_PARAMETER
;
597 if (Event
->Signature
!= EVENT_SIGNATURE
) {
598 return EFI_INVALID_PARAMETER
;
601 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
602 return EFI_INVALID_PARAMETER
;
605 Status
= EFI_NOT_READY
;
607 if ((Event
->SignalCount
== 0) && ((Event
->Type
& EVT_NOTIFY_WAIT
) != 0)) {
610 // Queue the wait notify function
612 CoreAcquireEventLock ();
613 if (Event
->SignalCount
== 0) {
614 CoreNotifyEvent (Event
);
616 CoreReleaseEventLock ();
620 // If the even looks signalled, get the lock and clear it
623 if (Event
->SignalCount
!= 0) {
624 CoreAcquireEventLock ();
626 if (Event
->SignalCount
!= 0) {
627 Event
->SignalCount
= 0;
628 Status
= EFI_SUCCESS
;
631 CoreReleaseEventLock ();
640 Stops execution until an event is signaled.
642 @param NumberOfEvents The number of events in the UserEvents array
643 @param UserEvents An array of EFI_EVENT
644 @param UserIndex Pointer to the index of the event which
645 satisfied the wait condition
647 @retval EFI_SUCCESS The event indicated by Index was signaled.
648 @retval EFI_INVALID_PARAMETER The event indicated by Index has a notification
649 function or Event was not a valid type
650 @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION
656 IN UINTN NumberOfEvents
,
657 IN EFI_EVENT
*UserEvents
,
665 // Can only WaitForEvent at TPL_APPLICATION
667 if (gEfiCurrentTpl
!= TPL_APPLICATION
) {
668 return EFI_UNSUPPORTED
;
671 if (NumberOfEvents
== 0) {
672 return EFI_INVALID_PARAMETER
;
675 if (UserEvents
== NULL
) {
676 return EFI_INVALID_PARAMETER
;
681 for(Index
= 0; Index
< NumberOfEvents
; Index
++) {
683 Status
= CoreCheckEvent (UserEvents
[Index
]);
686 // provide index of event that caused problem
688 if (Status
!= EFI_NOT_READY
) {
689 if (UserIndex
!= NULL
) {
697 // Signal the Idle event
699 CoreSignalEvent (gIdleLoopEvent
);
705 Closes an event and frees the event structure.
707 @param UserEvent Event to close
709 @retval EFI_INVALID_PARAMETER Parameters are not valid.
710 @retval EFI_SUCCESS The event has been closed
716 IN EFI_EVENT UserEvent
725 return EFI_INVALID_PARAMETER
;
728 if (Event
->Signature
!= EVENT_SIGNATURE
) {
729 return EFI_INVALID_PARAMETER
;
733 // If it's a timer event, make sure it's not pending
735 if ((Event
->Type
& EVT_TIMER
) != 0) {
736 CoreSetTimer (Event
, TimerCancel
, 0);
739 CoreAcquireEventLock ();
742 // If the event is queued somewhere, remove it
745 if (Event
->RuntimeData
.Link
.ForwardLink
!= NULL
) {
746 RemoveEntryList (&Event
->RuntimeData
.Link
);
749 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
750 RemoveEntryList (&Event
->NotifyLink
);
753 if (Event
->SignalLink
.ForwardLink
!= NULL
) {
754 RemoveEntryList (&Event
->SignalLink
);
757 CoreReleaseEventLock ();
760 // If the event is registered on a protocol notify, then remove it from the protocol database
762 if ((Event
->ExFlag
& EVT_EXFLAG_EVENT_PROTOCOL_NOTIFICATION
) != 0) {
763 CoreUnregisterProtocolNotify (Event
);
767 // To avoid the Event to be signalled wrongly after closed,
768 // clear the Signature of Event before free pool.
770 Event
->Signature
= 0;
771 Status
= CoreFreePool (Event
);
772 ASSERT_EFI_ERROR (Status
);