iPXE
efi_timer.c File Reference

iPXE timer API for EFI More...

#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <ipxe/timer.h>
#include <ipxe/init.h>
#include <ipxe/efi/efi.h>

Go to the source code of this file.

Macros

#define EFI_JIFFIES_PER_SEC   32
 Number of jiffies per second.
#define colour   &efi_jiffies
 Colour for debug messages.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static void efi_udelay (unsigned long usecs)
 Delay for a fixed number of microseconds.
static unsigned long efi_currticks (void)
 Get current system time in ticks.
static EFIAPI void efi_tick (EFI_EVENT event __unused, void *context __unused)
 Timer tick.
static void efi_tick_startup (void)
 Start timer tick.
static void efi_tick_shutdown (int booting __unused)
 Stop timer tick.
struct startup_fn efi_tick_startup_fn __startup_fn (STARTUP_EARLY)
 Timer tick startup function.
struct timer efi_timer __timer (TIMER_NORMAL)
 EFI timer.

Variables

static unsigned long efi_jiffies
 Current tick count.
static EFI_EVENT efi_tick_event
 Timer tick event.

Detailed Description

iPXE timer API for EFI

Definition in file efi_timer.c.

Macro Definition Documentation

◆ EFI_JIFFIES_PER_SEC

#define EFI_JIFFIES_PER_SEC   32

Number of jiffies per second.

This is a policy decision.

Definition at line 45 of file efi_timer.c.

Referenced by efi_currticks(), and efi_tick_startup().

◆ colour

#define colour   &efi_jiffies

Colour for debug messages.

Definition at line 54 of file efi_timer.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ efi_udelay()

void efi_udelay ( unsigned long usecs)
static

Delay for a fixed number of microseconds.

Parameters
usecsNumber of microseconds for which to delay

Definition at line 61 of file efi_timer.c.

61 {
62 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
63 EFI_STATUS efirc;
64 int rc;
65
66 if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
67 rc = -EEFI ( efirc );
68 DBGC ( colour, "EFI could not delay for %ldus: %s\n",
69 usecs, strerror ( rc ) );
70 /* Probably screwed */
71 }
72}
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
#define colour
Colour for debug messages.
Definition acpi.c:42
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define DBGC(...)
Definition compiler.h:505
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition efi.h:175
EFI_SYSTEM_TABLE * efi_systab
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
EFI Boot Services Table.
Definition UefiSpec.h:1931
EFI_STALL Stall
Definition UefiSpec.h:1988

References colour, DBGC, EEFI, efi_systab, rc, EFI_BOOT_SERVICES::Stall, and strerror().

Referenced by __timer().

◆ efi_currticks()

unsigned long efi_currticks ( void )
static

Get current system time in ticks.

Return values
ticksCurrent time, in ticks

Definition at line 79 of file efi_timer.c.

79 {
80 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
81
82 /* UEFI manages to ingeniously combine the worst aspects of
83 * both polling and interrupt-driven designs. There is no way
84 * to support proper interrupt-driven operation, since there
85 * is no way to hook in an interrupt service routine. A
86 * mockery of interrupts is provided by UEFI timers, which
87 * trigger at a preset rate and can fire at any time.
88 *
89 * We therefore have all of the downsides of a polling design
90 * (inefficiency and inability to sleep until something
91 * interesting happens) combined with all of the downsides of
92 * an interrupt-driven design (the complexity of code that
93 * could be preempted at any time).
94 *
95 * The UEFI specification expects us to litter the entire
96 * codebase with calls to RaiseTPL() as needed for sections of
97 * code that are not reentrant. Since this doesn't actually
98 * gain us any substantive benefits (since even with such
99 * calls we would still be suffering from the limitations of a
100 * polling design), we instead choose to run at TPL_CALLBACK
101 * almost all of the time, dropping to a lower TPL to allow
102 * timer ticks to occur.
103 *
104 * We record the external TPL at the point of entry into iPXE,
105 * and drop back only as far as this external TPL. This
106 * avoids the unexpected behaviour that may arise from having
107 * iPXE temporarily drop to TPL_APPLICATION in the middle of
108 * an entry point invoked at TPL_CALLBACK. The side effect is
109 * that iPXE's view of the system time is effectively frozen
110 * for the duration of any call made in to iPXE at
111 * TPL_CALLBACK or higher.
112 *
113 *
114 * For added excitement, UEFI provides no clean way for device
115 * drivers to shut down in preparation for handover to a
116 * booted operating system. The platform firmware simply
117 * doesn't bother to call the drivers' Stop() methods.
118 * Instead, all non-trivial drivers must register an
119 * EVT_SIGNAL_EXIT_BOOT_SERVICES event to be signalled when
120 * ExitBootServices() is called, and clean up without any
121 * reference to the EFI driver model.
122 *
123 * Unfortunately, all timers silently stop working when
124 * ExitBootServices() is called. Even more unfortunately, and
125 * for no discernible reason, this happens before any
126 * EVT_SIGNAL_EXIT_BOOT_SERVICES events are signalled. The
127 * net effect of this entertaining design choice is that any
128 * timeout loops on the shutdown path (e.g. for gracefully
129 * closing outstanding TCP connections) may wait indefinitely.
130 *
131 * There is no way to report failure from currticks(), since
132 * the API lazily assumes that the host system continues to
133 * travel through time in the usual direction. Work around
134 * EFI's violation of this assumption by falling back to a
135 * simple free-running monotonic counter during shutdown.
136 */
138 efi_jiffies++;
139 } else {
142 }
143
145}
EFI_TPL efi_external_tpl
External task priority level.
Definition efi_init.c:57
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition efi_init.c:54
int efi_shutdown_in_progress
EFI shutdown is in progress.
Definition efi_init.c:60
static unsigned long efi_jiffies
Current tick count.
Definition efi_timer.c:48
#define EFI_JIFFIES_PER_SEC
Number of jiffies per second.
Definition efi_timer.c:45
#define TICKS_PER_SEC
Number of ticks per second.
Definition timer.h:16
EFI_RESTORE_TPL RestoreTPL
Definition UefiSpec.h:1941
EFI_RAISE_TPL RaiseTPL
Definition UefiSpec.h:1940

References efi_external_tpl, efi_internal_tpl, efi_jiffies, EFI_JIFFIES_PER_SEC, efi_shutdown_in_progress, efi_systab, EFI_BOOT_SERVICES::RaiseTPL, EFI_BOOT_SERVICES::RestoreTPL, and TICKS_PER_SEC.

Referenced by __timer().

◆ efi_tick()

EFIAPI void efi_tick ( EFI_EVENT event __unused,
void *context __unused )
static

Timer tick.

Parameters
eventTimer tick event
contextEvent context

Definition at line 153 of file efi_timer.c.

154 {
155
156 /* Increment tick count */
157 efi_jiffies++;
158}

References __unused, EFI_EVENT, efi_jiffies, and EFIAPI.

Referenced by efi_tick_startup().

◆ efi_tick_startup()

void efi_tick_startup ( void )
static

Start timer tick.

Definition at line 164 of file efi_timer.c.

164 {
165 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
166 EFI_STATUS efirc;
167 int rc;
168
169 /* Create timer tick event */
170 if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
172 &efi_tick_event ) ) != 0 ) {
173 rc = -EEFI ( efirc );
174 DBGC ( colour, "EFI could not create timer tick: %s\n",
175 strerror ( rc ) );
176 /* Nothing we can do about it */
177 return;
178 }
179
180 /* Start timer tick */
181 if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerPeriodic,
182 ( 10000000 / EFI_JIFFIES_PER_SEC ) ))!=0){
183 rc = -EEFI ( efirc );
184 DBGC ( colour, "EFI could not start timer tick: %s\n",
185 strerror ( rc ) );
186 /* Nothing we can do about it */
187 return;
188 }
189 DBGC ( colour, "EFI timer started at %d ticks per second\n",
191}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define TPL_CALLBACK
Definition UefiSpec.h:649
@ TimerPeriodic
An event is to be signaled periodically at a specified interval from the current time.
Definition UefiSpec.h:548
#define EVT_TIMER
Definition UefiSpec.h:451
#define EVT_NOTIFY_SIGNAL
Definition UefiSpec.h:454
static EFI_EVENT efi_tick_event
Timer tick event.
Definition efi_timer.c:51
static EFIAPI void efi_tick(EFI_EVENT event __unused, void *context __unused)
Timer tick.
Definition efi_timer.c:153
EFI_SET_TIMER SetTimer
Definition UefiSpec.h:1956
EFI_CREATE_EVENT CreateEvent
Definition UefiSpec.h:1955

References colour, EFI_BOOT_SERVICES::CreateEvent, DBGC, EEFI, EFI_JIFFIES_PER_SEC, efi_systab, efi_tick(), efi_tick_event, EVT_NOTIFY_SIGNAL, EVT_TIMER, NULL, rc, EFI_BOOT_SERVICES::SetTimer, strerror(), TimerPeriodic, and TPL_CALLBACK.

Referenced by __startup_fn().

◆ efi_tick_shutdown()

void efi_tick_shutdown ( int booting __unused)
static

Stop timer tick.

Parameters
bootingSystem is shutting down in order to boot

Definition at line 198 of file efi_timer.c.

198 {
199 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
200 EFI_STATUS efirc;
201 int rc;
202
203 /* Stop timer tick */
204 if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerCancel, 0 ) ) != 0 ){
205 rc = -EEFI ( efirc );
206 DBGC ( colour, "EFI could not stop timer tick: %s\n",
207 strerror ( rc ) );
208 /* Self-destruct initiated */
209 return;
210 }
211 DBGC ( colour, "EFI timer stopped\n" );
212
213 /* Destroy timer tick event */
214 if ( ( efirc = bs->CloseEvent ( efi_tick_event ) ) != 0 ) {
215 rc = -EEFI ( efirc );
216 DBGC ( colour, "EFI could not destroy timer tick: %s\n",
217 strerror ( rc ) );
218 /* Probably non-fatal */
219 return;
220 }
221}
@ TimerCancel
An event's timer settings is to be cancelled and not trigger time is to be set/.
Definition UefiSpec.h:544
EFI_CLOSE_EVENT CloseEvent
Definition UefiSpec.h:1959

References __unused, EFI_BOOT_SERVICES::CloseEvent, colour, DBGC, EEFI, efi_systab, efi_tick_event, rc, EFI_BOOT_SERVICES::SetTimer, strerror(), and TimerCancel.

Referenced by __startup_fn().

◆ __startup_fn()

struct startup_fn efi_tick_startup_fn __startup_fn ( STARTUP_EARLY )

Timer tick startup function.

References __startup_fn, efi_tick_shutdown(), efi_tick_startup(), and STARTUP_EARLY.

◆ __timer()

struct timer efi_timer __timer ( TIMER_NORMAL )

EFI timer.

References __timer, efi_currticks(), efi_udelay(), and TIMER_NORMAL.

Variable Documentation

◆ efi_jiffies

unsigned long efi_jiffies
static

Current tick count.

Definition at line 48 of file efi_timer.c.

Referenced by efi_currticks(), and efi_tick().

◆ efi_tick_event

EFI_EVENT efi_tick_event
static

Timer tick event.

Definition at line 51 of file efi_timer.c.

Referenced by efi_tick_shutdown(), and efi_tick_startup().