Timer

Because ACRN is a flexible, lightweight reference hypervisor, we provide limited timer management services:

  • Only lapic tsc-deadline timer is supported as the clock source.
  • A timer can only be added on the logical CPU for a process or thread. Timer scheduling or timer migrating are not supported.

How it works

When the system boots, we check that the hardware supports lapic tsc-deadline timer by checking CPUID.01H:ECX.TSC_Deadline[bit 24]. If support is missing, we output an error message and panic the hypervisor. If supported, we register the timer interrupt callback that raises a timer softirq on each logical CPU and set the lapic timer mode to tsc-deadline timer mode by writing the local APIC LVT register.

Data Structures and APIs

Interfaces Design

static void initialize_timer(struct hv_timer *timer, timer_handle_t func, void *priv_data, uint64_t fire_tsc, int32_t mode, uint64_t period_in_cycle)

Initialize a timer structure.

Remark
Don’t initialize a timer twice if it has been added to the timer list after calling add_timer. If you want to, delete the timer from the list first.
Return
None
Parameters
  • timer: Pointer to timer.
  • func: irq callback if time reached.
  • priv_data: func private data.
  • fire_tsc: tsc deadline to interrupt.
  • mode: timer mode.
  • period_in_cycle: period of the periodic timer in unit of TSC cycles.

static bool timer_expired(const struct hv_timer *timer)

Check a timer whether expired.

Parameters
  • timer: Pointer to timer.
Return Value
  • true: if the timer is expired, false otherwise.

int32_t add_timer(struct hv_timer *timer)

Add a timer.

Remark
Don’t call it in the timer callback function or interrupt content.
Parameters
  • timer: Pointer to timer.
Return Value
  • 0: on success
  • -EINVAL: timer has an invalid value

void del_timer(struct hv_timer *timer)

Delete a timer.

Return
None
Remark
Don’t call it in the timer callback function or interrupt content.
Parameters
  • timer: Pointer to timer.

void timer_init(void)

Initialize timer.

Return
None

void calibrate_tsc(void)

Calibrate tsc.

Return
None

uint64_t us_to_ticks(uint32_t us)

convert us to ticks.

Return
ticks

uint64_t ticks_to_us(uint64_t ticks)

convert ticks to us.

Return
microsecond

uint64_t ticks_to_ms(uint64_t ticks)

convert ticks to ms.

Return
millisecond

uint64_t rdtsc(void)

read tsc.

Return
tsc value

uint32_t get_tsc_khz(void)

Get tsc.

Return
tsc(KHz)