12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274 |
- /*++
- Copyright (c) 2014 Minoca Corp. All Rights Reserved
- Module Name:
- event.c
- Abstract:
- This module implements UEFI core event services.
- Author:
- Evan Green 4-Mar-2014
- Environment:
- Firmware
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "ueficore.h"
- #include <minoca/uefi/guid/eventgrp.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define EFI_EVENT_MAGIC 0x746F7645 // 'tnvE'
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure stores timing information about a timer event.
- Members:
- ListEntry - Stores pointers to the next and previous timer event structures.
- DueTime - Stores the time when the timer expires.
- Period - Stores the period of the timer, if periodic.
- --*/
- typedef struct _EFI_TIMER_EVENT {
- LIST_ENTRY ListEntry;
- UINT64 DueTime;
- UINT64 Period;
- } EFI_TIMER_EVENT, *PEFI_TIMER_EVENT;
- /*++
- Structure Description:
- This structure stores the internal structure of an EFI event.
- Members:
- Magic - Stores the magic constant EFI_EVENT_MAGIC.
- Type - Stores the type of event.
- SignalCount - Stores the number of times this event has been signaled.
- SignalListEntry - Stores pointers to the next and previous events in the
- signal queue.
- NotifyTpl - Stores the task priority level of the event.
- NotifyFunction - Stores a pointer to a function called when the event fires.
- NotifyContext - Stores a pointer's worth of data passed to the notify
- function.
- EventGroup - Stores the GUID of the event group this timer is in.
- NotifyListEntry - Stores pointers to the next and previous entries in the
- notify list.
- EventEx - Stores a boolean indicating if this event was created with the
- Ex function or the regular one.
- RuntimeData - Stores runtime data about the event.
- TimerData - Stores timer event data.
- --*/
- typedef struct _EFI_EVENT_DATA {
- UINTN Magic;
- UINT32 Type;
- UINT32 SignalCount;
- LIST_ENTRY SignalListEntry;
- EFI_TPL NotifyTpl;
- EFI_EVENT_NOTIFY NotifyFunction;
- VOID *NotifyContext;
- EFI_GUID EventGroup;
- LIST_ENTRY NotifyListEntry;
- BOOLEAN EventEx;
- EFI_RUNTIME_EVENT_ENTRY RuntimeData;
- EFI_TIMER_EVENT TimerData;
- } EFI_EVENT_DATA, *PEFI_EVENT_DATA;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- EFI_STATUS
- EfipCoreCreateEvent (
- UINT32 Type,
- EFI_TPL NotifyTpl,
- EFI_EVENT_NOTIFY NotifyFunction,
- VOID *NotifyContext,
- EFI_GUID *EventGroup,
- EFI_EVENT *Event
- );
- VOID
- EfipCoreNotifyEvent (
- PEFI_EVENT_DATA Event
- );
- VOID
- EfipCoreInsertEventTimer (
- PEFI_EVENT_DATA Event
- );
- EFIAPI
- VOID
- EfipCoreCheckTimers (
- EFI_EVENT CheckEvent,
- VOID *Context
- );
- EFIAPI
- VOID
- EfipCoreEmptyCallbackFunction (
- EFI_EVENT Event,
- VOID *Context
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Store some well known event GUIDs.
- //
- EFI_GUID EfiEventExitBootServicesGuid = EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
- EFI_GUID EfiEventVirtualAddressChangeGuid =
- EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
- EFI_GUID EfiEventMemoryMapChangeGuid = EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
- EFI_GUID EfiEventReadyToBootGuid = EFI_EVENT_GROUP_READY_TO_BOOT;
- //
- // Store the idle loop event, which is signaled when there's nothing to do.
- //
- EFI_GUID EfiIdleLoopEventGuid = EFI_IDLE_LOOP_EVENT_GUID;
- EFI_EVENT EfiIdleLoopEvent;
- //
- // Store the event queue.
- //
- EFI_LOCK EfiEventQueueLock;
- LIST_ENTRY EfiEventQueue[TPL_HIGH_LEVEL + 1];
- UINTN EfiEventsPending;
- LIST_ENTRY EfiEventSignalQueue;
- //
- // Store the timer list.
- //
- EFI_LOCK EfiTimerLock;
- LIST_ENTRY EfiTimerList;
- EFI_EVENT EfiCheckTimerEvent;
- //
- // Store a table of valid event creation flags.
- //
- UINT32 EfiValidEventFlags[] = {
- EVT_TIMER | EVT_NOTIFY_SIGNAL,
- EVT_TIMER,
- EVT_NOTIFY_WAIT,
- EVT_NOTIFY_SIGNAL,
- EVT_SIGNAL_EXIT_BOOT_SERVICES,
- EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
- 0,
- EVT_TIMER | EVT_NOTIFY_WAIT,
- };
- //
- // ------------------------------------------------------------------ Functions
- //
- EFIAPI
- EFI_STATUS
- EfiCoreCreateEvent (
- UINT32 Type,
- EFI_TPL NotifyTpl,
- EFI_EVENT_NOTIFY NotifyFunction,
- VOID *NotifyContext,
- EFI_EVENT *Event
- )
- /*++
- Routine Description:
- This routine creates an event.
- Arguments:
- Type - Supplies the type of event to create, as well as its mode and
- attributes.
- NotifyTpl - Supplies an optional task priority level of event notifications.
- NotifyFunction - Supplies an optional pointer to the event's notification
- function.
- NotifyContext - Supplies an optional context pointer that will be passed
- to the notify function when the event is signaled.
- Event - Supplies a pointer where the new event will be returned on success.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if one or more parameters are not valid.
- EFI_OUT_OF_RESOURCES if memory could not be allocated.
- --*/
- {
- EFI_STATUS Status;
- Status = EfiCoreCreateEventEx(Type,
- NotifyTpl,
- NotifyFunction,
- NotifyContext,
- NULL,
- Event);
- return Status;
- }
- EFIAPI
- EFI_STATUS
- EfiCoreCreateEventEx (
- UINT32 Type,
- EFI_TPL NotifyTpl,
- EFI_EVENT_NOTIFY NotifyFunction,
- VOID *NotifyContext,
- EFI_GUID *EventGroup,
- EFI_EVENT *Event
- )
- /*++
- Routine Description:
- This routine creates an event.
- Arguments:
- Type - Supplies the type of event to create, as well as its mode and
- attributes.
- NotifyTpl - Supplies an optional task priority level of event notifications.
- NotifyFunction - Supplies an optional pointer to the event's notification
- function.
- NotifyContext - Supplies an optional context pointer that will be passed
- to the notify function when the event is signaled.
- EventGroup - Supplies an optional pointer to the unique identifier of the
- group to which this event belongs. If this is NULL, the function
- behaves as if the parameters were passed to the original create event
- function.
- Event - Supplies a pointer where the new event will be returned on success.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if one or more parameters are not valid.
- EFI_OUT_OF_RESOURCES if memory could not be allocated.
- --*/
- {
- EFI_STATUS Status;
- if ((Type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) != 0) {
- if ((NotifyTpl != TPL_APPLICATION) &&
- (NotifyTpl != TPL_CALLBACK) &&
- (NotifyTpl != TPL_NOTIFY)) {
- return EFI_INVALID_PARAMETER;
- }
- }
- Status = EfipCoreCreateEvent(Type,
- NotifyTpl,
- NotifyFunction,
- NotifyContext,
- EventGroup,
- Event);
- return Status;
- }
- EFIAPI
- EFI_STATUS
- EfiCoreCloseEvent (
- EFI_EVENT Event
- )
- /*++
- Routine Description:
- This routine closes an event.
- Arguments:
- Event - Supplies the event to close.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if the given event is invalid.
- --*/
- {
- PEFI_EVENT_DATA EventData;
- EFI_STATUS Status;
- EventData = Event;
- if (EventData == NULL) {
- return EFI_INVALID_PARAMETER;
- }
- if (EventData->Magic != EFI_EVENT_MAGIC) {
- return EFI_INVALID_PARAMETER;
- }
- //
- // If it's a timer event, cancel it.
- //
- if ((EventData->Type & EVT_TIMER) != 0) {
- EfiCoreSetTimer(EventData, TimerCancel, 0);
- }
- EfiCoreAcquireLock(&EfiEventQueueLock);
- if (EventData->RuntimeData.ListEntry.Next != NULL) {
- LIST_REMOVE(&(EventData->RuntimeData.ListEntry));
- }
- if (EventData->NotifyListEntry.Next != NULL) {
- LIST_REMOVE(&(EventData->NotifyListEntry));
- }
- if (EventData->SignalListEntry.Next != NULL) {
- LIST_REMOVE(&(EventData->SignalListEntry));
- }
- EfiCoreReleaseLock(&EfiEventQueueLock);
- //
- // If the event is registered on a protocol notify, remove it from the
- // protocol database.
- //
- EfipCoreUnregisterProtocolNotify(Event);
- Status = EfiCoreFreePool(EventData);
- ASSERT(!EFI_ERROR(Status));
- return Status;
- }
- EFIAPI
- EFI_STATUS
- EfiCoreSignalEvent (
- EFI_EVENT Event
- )
- /*++
- Routine Description:
- This routine signals an event.
- Arguments:
- Event - Supplies the event to signal.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if the given event is not valid.
- --*/
- {
- PEFI_EVENT_DATA EventData;
- EventData = Event;
- if ((EventData == NULL) || (EventData->Magic != EFI_EVENT_MAGIC)) {
- return EFI_INVALID_PARAMETER;
- }
- EfiCoreAcquireLock(&EfiEventQueueLock);
- if (EventData->SignalCount == 0) {
- EventData->SignalCount += 1;
- //
- // If the signal type is a notify function, queue it.
- //
- if ((EventData->Type & EVT_NOTIFY_SIGNAL) != 0) {
- //
- // If it's an event "Ex", then signal all members of the event
- // group.
- //
- if (EventData->EventEx != FALSE) {
- EfiCoreReleaseLock(&EfiEventQueueLock);
- EfipCoreNotifySignalList(&(EventData->EventGroup));
- EfiCoreAcquireLock(&EfiEventQueueLock);
- } else {
- EfipCoreNotifyEvent(EventData);
- }
- }
- }
- EfiCoreReleaseLock(&EfiEventQueueLock);
- return EFI_SUCCESS;
- }
- EFIAPI
- EFI_STATUS
- EfiCoreCheckEvent (
- EFI_EVENT Event
- )
- /*++
- Routine Description:
- This routine checks whether or not an event is in the signaled state.
- Arguments:
- Event - Supplies the event to check.
- Return Value:
- EFI_SUCCESS on success.
- EFI_NOT_READY if the event is not signaled.
- EFI_INVALID_PARAMETER if the event is of type EVT_NOTIFY_SIGNAL.
- --*/
- {
- PEFI_EVENT_DATA EventData;
- EFI_STATUS Status;
- EventData = Event;
- if ((EventData == NULL) || (EventData->Magic != EFI_EVENT_MAGIC) ||
- ((EventData->Type & EVT_NOTIFY_SIGNAL) != 0)) {
- return EFI_INVALID_PARAMETER;
- }
- Status = EFI_NOT_READY;
- if ((EventData->SignalCount == 0) &&
- ((EventData->Type & EVT_NOTIFY_WAIT) != 0)) {
- //
- // Queue the wait notify function.
- //
- EfiCoreAcquireLock(&EfiEventQueueLock);
- if (EventData->SignalCount == 0) {
- EfipCoreNotifyEvent(EventData);
- }
- EfiCoreReleaseLock(&EfiEventQueueLock);
- }
- if (EventData->SignalCount != 0) {
- EfiCoreAcquireLock(&EfiEventQueueLock);
- if (EventData->SignalCount != 0) {
- EventData->SignalCount = 0;
- Status = EFI_SUCCESS;
- }
- EfiCoreReleaseLock(&EfiEventQueueLock);
- }
- return Status;
- }
- EFIAPI
- EFI_STATUS
- EfiCoreWaitForEvent (
- UINTN NumberOfEvents,
- EFI_EVENT *Event,
- UINTN *Index
- )
- /*++
- Routine Description:
- This routine stops execution until an event is signaled.
- Arguments:
- NumberOfEvents - Supplies the number of events in the event array.
- Event - Supplies the array of EFI_EVENTs.
- Index - Supplies a pointer where the index of the event which satisfied the
- wait will be returned.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if the number of events is zero, or the event
- indicated by the index return parameter is of type EVT_NOTIFY_SIGNAL.
- EFI_UNSUPPORTED if the current TPL is not TPL_APPLICATION.
- --*/
- {
- UINTN EventIndex;
- EFI_STATUS Status;
- if (NumberOfEvents == 0) {
- return EFI_INVALID_PARAMETER;
- }
- if (EfiCurrentTpl != TPL_APPLICATION) {
- return EFI_UNSUPPORTED;
- }
- while (TRUE) {
- for (EventIndex = 0; EventIndex < NumberOfEvents; EventIndex += 1) {
- Status = EfiCoreCheckEvent(Event[EventIndex]);
- if (Status != EFI_NOT_READY) {
- *Index = EventIndex;
- return Status;
- }
- }
- EfiCoreSignalEvent(&EfiIdleLoopEvent);
- }
- //
- // Execution never gets here.
- //
- ASSERT(FALSE);
- return EFI_NOT_READY;
- }
- EFIAPI
- EFI_STATUS
- EfiCoreSetTimer (
- EFI_EVENT Event,
- EFI_TIMER_DELAY Type,
- UINT64 TriggerTime
- )
- /*++
- Routine Description:
- This routine sets the type of timer and trigger time for a timer event.
- Arguments:
- Event - Supplies the timer to set.
- Type - Supplies the type of trigger to set.
- TriggerTime - Supplies the number of 100ns units until the timer expires.
- Zero is legal, and means the timer will be signaled on the next timer
- tick.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if the event or type is not valid.
- --*/
- {
- PEFI_EVENT_DATA EventData;
- UINT64 Frequency;
- EventData = Event;
- if ((EventData == NULL) || (EventData->Magic != EFI_EVENT_MAGIC)) {
- return EFI_INVALID_PARAMETER;
- }
- if (((UINT32)Type > TimerRelative) ||
- ((EventData->Type & EVT_TIMER) == 0)) {
- return EFI_INVALID_PARAMETER;
- }
- EfiCoreAcquireLock(&EfiTimerLock);
- //
- // If the timer is queued to a database, remove it.
- //
- if (EventData->TimerData.ListEntry.Next != NULL) {
- LIST_REMOVE(&(EventData->TimerData.ListEntry));
- EventData->TimerData.ListEntry.Next = NULL;
- }
- EventData->TimerData.DueTime = 0;
- EventData->TimerData.Period = 0;
- if (Type != TimerCancel) {
- Frequency = EfiCoreGetTimeCounterFrequency();
- TriggerTime = (TriggerTime * Frequency) / 10000000ULL;
- if (Type == TimerPeriodic) {
- if (TriggerTime == 0) {
- EventData->TimerData.Period = 1;
- } else {
- EventData->TimerData.Period = TriggerTime;
- }
- }
- EventData->TimerData.DueTime = EfiCoreReadTimeCounter() + TriggerTime;
- EfipCoreInsertEventTimer(EventData);
- if (TriggerTime == 0) {
- EfiCoreSignalEvent(&EfiCheckTimerEvent);
- }
- }
- EfiCoreReleaseLock(&EfiTimerLock);
- return EFI_SUCCESS;
- }
- EFI_STATUS
- EfiCoreInitializeEventServices (
- UINTN Phase
- )
- /*++
- Routine Description:
- This routine initializes event support.
- Arguments:
- Phase - Supplies the initialization phase. Valid values are 0 and 1.
- Return Value:
- EFI Status code.
- --*/
- {
- UINTN Index;
- if (Phase == 0) {
- EfiCoreInitializeLock(&EfiEventQueueLock, TPL_HIGH_LEVEL);
- EfiCoreInitializeLock(&EfiTimerLock, TPL_HIGH_LEVEL - 1);
- for (Index = 0; Index <= TPL_HIGH_LEVEL; Index += 1) {
- INITIALIZE_LIST_HEAD(&(EfiEventQueue[Index]));
- }
- INITIALIZE_LIST_HEAD(&EfiEventSignalQueue);
- INITIALIZE_LIST_HEAD(&EfiTimerList);
- } else {
- ASSERT(Phase == 1);
- EfiCoreCreateEventEx(EVT_NOTIFY_SIGNAL,
- TPL_NOTIFY,
- EfipCoreEmptyCallbackFunction,
- NULL,
- &EfiIdleLoopEventGuid,
- &EfiIdleLoopEvent);
- EfipCoreCreateEvent(EVT_NOTIFY_SIGNAL,
- TPL_HIGH_LEVEL - 1,
- EfipCoreCheckTimers,
- NULL,
- NULL,
- &EfiCheckTimerEvent);
- }
- return EFI_SUCCESS;
- }
- VOID
- EfiCoreDispatchEventNotifies (
- EFI_TPL Priority
- )
- /*++
- Routine Description:
- This routine dispatches all pending events.
- Arguments:
- Priority - Supplies the task priority level of the event notifications to
- dispatch.
- Return Value:
- None.
- --*/
- {
- PEFI_EVENT_DATA Event;
- PLIST_ENTRY ListHead;
- EfiCoreAcquireLock(&EfiEventQueueLock);
- ASSERT(EfiEventQueueLock.OwnerTpl == Priority);
- ListHead = &(EfiEventQueue[Priority]);
- while (LIST_EMPTY(ListHead) == FALSE) {
- Event = LIST_VALUE(ListHead->Next, EFI_EVENT_DATA, NotifyListEntry);
- ASSERT(Event->Magic == EFI_EVENT_MAGIC);
- LIST_REMOVE(&(Event->NotifyListEntry));
- Event->NotifyListEntry.Next = NULL;
- //
- // Only clear the signal status if it is a signal type event. Wait type
- // events are cleared in the check event function.
- //
- if ((Event->Type & EVT_NOTIFY_SIGNAL) != 0) {
- Event->SignalCount = 0;
- }
- //
- // Call the notification function without the lock held.
- //
- EfiCoreReleaseLock(&EfiEventQueueLock);
- Event->NotifyFunction(Event, Event->NotifyContext);
- EfiCoreAcquireLock(&EfiEventQueueLock);
- }
- EfiEventsPending &= ~(1 << Priority);
- EfiCoreReleaseLock(&EfiEventQueueLock);
- return;
- }
- VOID
- EfipCoreTimerTick (
- UINT64 CurrentTime
- )
- /*++
- Routine Description:
- This routine is called when a clock interrupt comes in.
- Arguments:
- CurrentTime - Supplies the new current time.
- Return Value:
- None.
- --*/
- {
- PEFI_EVENT_DATA Event;
- if (LIST_EMPTY(&EfiTimerList) == FALSE) {
- Event = LIST_VALUE(EfiTimerList.Next,
- EFI_EVENT_DATA,
- TimerData.ListEntry);
- if (Event->TimerData.DueTime <= CurrentTime) {
- EfiCoreSignalEvent(EfiCheckTimerEvent);
- }
- }
- return;
- }
- VOID
- EfipCoreNotifySignalList (
- EFI_GUID *EventGroup
- )
- /*++
- Routine Description:
- This routine signals all events in the given event group.
- Arguments:
- EventGroup - Supplies a pointer to the GUID identifying the event group
- to signal.
- Return Value:
- None.
- --*/
- {
- PLIST_ENTRY CurrentEntry;
- PEFI_EVENT_DATA Event;
- EfiCoreAcquireLock(&EfiEventQueueLock);
- CurrentEntry = EfiEventSignalQueue.Next;
- while (CurrentEntry != &EfiEventSignalQueue) {
- Event = LIST_VALUE(CurrentEntry, EFI_EVENT_DATA, SignalListEntry);
- CurrentEntry = CurrentEntry->Next;
- if (EfiCoreCompareGuids(&(Event->EventGroup), EventGroup) != FALSE) {
- EfipCoreNotifyEvent(Event);
- }
- }
- EfiCoreReleaseLock(&EfiEventQueueLock);
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- EFI_STATUS
- EfipCoreCreateEvent (
- UINT32 Type,
- EFI_TPL NotifyTpl,
- EFI_EVENT_NOTIFY NotifyFunction,
- VOID *NotifyContext,
- EFI_GUID *EventGroup,
- EFI_EVENT *Event
- )
- /*++
- Routine Description:
- This routine creates an event.
- Arguments:
- Type - Supplies the type of event to create, as well as its mode and
- attributes.
- NotifyTpl - Supplies an optional task priority level of event notifications.
- NotifyFunction - Supplies an optional pointer to the event's notification
- function.
- NotifyContext - Supplies an optional context pointer to pass when the event
- is signaled.
- EventGroup - Supplies an optional pointer to the unique identifier of the
- group to which this event belongs. If this is NULL, the function
- behaves as if the parameters were passed to the original create event
- function.
- Event - Supplies a pointer where the new event will be returned on success.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if one or more parameters are not valid.
- EFI_OUT_OF_RESOURCES if memory could not be allocated.
- --*/
- {
- UINTN EntryCount;
- UINTN Index;
- BOOLEAN Match;
- EFI_EVENT_DATA *NewEvent;
- EFI_STATUS Status;
- if (Event == NULL) {
- return EFI_INVALID_PARAMETER;
- }
- //
- // Check to make sure a valid combination of flags is set.
- //
- EntryCount = sizeof(EfiValidEventFlags) / sizeof(EfiValidEventFlags[0]);
- Status = EFI_INVALID_PARAMETER;
- for (Index = 0; Index < EntryCount; Index += 1) {
- if (Type == EfiValidEventFlags[Index]) {
- Status = EFI_SUCCESS;
- break;
- }
- }
- if (EFI_ERROR(Status)) {
- return Status;
- }
- //
- // Convert the event type for pre-existing event groups.
- //
- if (EventGroup != NULL) {
- if ((Type == EVT_SIGNAL_EXIT_BOOT_SERVICES) ||
- (Type == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)) {
- return EFI_INVALID_PARAMETER;
- }
- if (EfiCoreCompareGuids(EventGroup, &EfiEventExitBootServicesGuid) !=
- FALSE) {
- Type = EVT_SIGNAL_EXIT_BOOT_SERVICES;
- } else {
- Match = EfiCoreCompareGuids(EventGroup,
- &EfiEventVirtualAddressChangeGuid);
- if (Match != FALSE) {
- Type = EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE;
- }
- }
- } else {
- if (Type == EVT_SIGNAL_EXIT_BOOT_SERVICES) {
- EventGroup = &EfiEventExitBootServicesGuid;
- } else if (Type == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {
- EventGroup = &EfiEventVirtualAddressChangeGuid;
- }
- }
- //
- // If it's a notify type event, check parameters.
- //
- if ((Type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) != 0) {
- if ((NotifyFunction == NULL) ||
- (NotifyTpl <= TPL_APPLICATION) ||
- (NotifyTpl >= TPL_HIGH_LEVEL)) {
- return EFI_INVALID_PARAMETER;
- }
- //
- // No notifications are needed.
- //
- } else {
- NotifyTpl = 0;
- NotifyFunction = NULL;
- NotifyContext = NULL;
- }
- //
- // Allocate and initialize the new event.
- //
- if ((Type & EVT_RUNTIME) != 0) {
- NewEvent = EfiCoreAllocateRuntimePool(sizeof(EFI_EVENT_DATA));
- } else {
- NewEvent = EfiCoreAllocateBootPool(sizeof(EFI_EVENT_DATA));
- }
- if (NewEvent == NULL) {
- return EFI_OUT_OF_RESOURCES;
- }
- EfiCoreSetMemory(NewEvent, sizeof(EFI_EVENT_DATA), 0);
- NewEvent->Magic = EFI_EVENT_MAGIC;
- NewEvent->Type = Type;
- NewEvent->NotifyTpl = NotifyTpl;
- NewEvent->NotifyFunction = NotifyFunction;
- NewEvent->NotifyContext = NotifyContext;
- if (EventGroup != NULL) {
- EfiCoreCopyMemory(&(NewEvent->EventGroup),
- EventGroup,
- sizeof(EFI_GUID));
- NewEvent->EventEx = TRUE;
- }
- *Event = NewEvent;
- //
- // Keep a list of all the runtime events specifically.
- //
- if ((Type & EVT_RUNTIME) != 0) {
- NewEvent->RuntimeData.Type = Type;
- NewEvent->RuntimeData.NotifyTpl = NotifyTpl;
- NewEvent->RuntimeData.NotifyFunction = NotifyFunction;
- NewEvent->RuntimeData.NotifyContext = NotifyContext;
- NewEvent->RuntimeData.Event = (EFI_EVENT *)NewEvent;
- INSERT_BEFORE(&(NewEvent->RuntimeData.ListEntry),
- &(EfiRuntimeProtocol->EventListHead));
- }
- EfiCoreAcquireLock(&EfiEventQueueLock);
- if ((Type & EVT_NOTIFY_SIGNAL) != 0) {
- INSERT_AFTER(&(NewEvent->SignalListEntry), &EfiEventSignalQueue);
- }
- EfiCoreReleaseLock(&EfiEventQueueLock);
- return EFI_SUCCESS;
- }
- VOID
- EfipCoreNotifyEvent (
- PEFI_EVENT_DATA Event
- )
- /*++
- Routine Description:
- This routine notifies the given event.
- Arguments:
- Event - Supplies the event to notify.
- Return Value:
- None.
- --*/
- {
- ASSERT(EfiCoreIsLockHeld(&EfiEventQueueLock) != FALSE);
- //
- // If the event is queued somewhere, remove it.
- //
- if (Event->NotifyListEntry.Next != NULL) {
- LIST_REMOVE(&(Event->NotifyListEntry));
- Event->NotifyListEntry.Next = NULL;
- }
- INSERT_BEFORE(&(Event->NotifyListEntry),
- &(EfiEventQueue[Event->NotifyTpl]));
- EfiEventsPending = 1 << Event->NotifyTpl;
- return;
- }
- VOID
- EfipCoreInsertEventTimer (
- PEFI_EVENT_DATA Event
- )
- /*++
- Routine Description:
- This routine inserts the given timer event into the global list.
- Arguments:
- Event - Supplies the timer event to insert.
- Return Value:
- None.
- --*/
- {
- PLIST_ENTRY CurrentEntry;
- PEFI_EVENT_DATA SearchEvent;
- ASSERT(EfiCoreIsLockHeld(&EfiTimerLock) != FALSE);
- CurrentEntry = EfiTimerList.Next;
- while (CurrentEntry != &EfiTimerList) {
- SearchEvent = LIST_VALUE(CurrentEntry,
- EFI_EVENT_DATA,
- TimerData.ListEntry);
- if (SearchEvent->TimerData.DueTime > Event->TimerData.DueTime) {
- break;
- }
- CurrentEntry = CurrentEntry->Next;
- }
- INSERT_BEFORE(&(Event->TimerData.ListEntry), CurrentEntry);
- return;
- }
- EFIAPI
- VOID
- EfipCoreCheckTimers (
- EFI_EVENT CheckEvent,
- VOID *Context
- )
- /*++
- Routine Description:
- This routine checks the sorted timer list against the current system time,
- and signals any expired timers.
- Arguments:
- CheckEvent - Supplies the event that fired, the EFI check timer event.
- Context - Supplies a context pointer associated with the event, which is
- not used.
- Return Value:
- None.
- --*/
- {
- PEFI_EVENT_DATA Event;
- UINT64 TimeCounter;
- TimeCounter = EfiCoreReadTimeCounter();
- EfiCoreAcquireLock(&EfiTimerLock);
- while (LIST_EMPTY(&EfiTimerList) == FALSE) {
- Event = LIST_VALUE(EfiTimerList.Next,
- EFI_EVENT_DATA,
- TimerData.ListEntry);
- //
- // If this timer is not expired, then neither is anything after it,
- // so break.
- //
- if (Event->TimerData.DueTime > TimeCounter) {
- break;
- }
- LIST_REMOVE(&(Event->TimerData.ListEntry));
- Event->TimerData.ListEntry.Next = NULL;
- EfiCoreSignalEvent(Event);
- //
- // If this is a periodic timer, compute the next due time and set it
- // again.
- //
- if (Event->TimerData.Period != 0) {
- Event->TimerData.DueTime += Event->TimerData.Period;
- //
- // If the new due time is still in the past, reset the timer to
- // start from now.
- //
- if (Event->TimerData.DueTime < TimeCounter) {
- Event->TimerData.DueTime = TimeCounter;
- EfiCoreSignalEvent(EfiCheckTimerEvent);
- }
- EfipCoreInsertEventTimer(Event);
- }
- }
- EfiCoreReleaseLock(&EfiTimerLock);
- return;
- }
- EFIAPI
- VOID
- EfipCoreEmptyCallbackFunction (
- EFI_EVENT Event,
- VOID *Context
- )
- /*++
- Routine Description:
- This routine implements a null callback that does nothing but returns.
- Arguments:
- Event - Supplies the event that fired.
- Context - Supplies a context pointer associated with the event.
- Return Value:
- None.
- --*/
- {
- return;
- }
|