xen/riscv: introduce basic vtimer infrastructure for guests

Lay the groundwork for guest timer support by introducing a per-vCPU
virtual timer backed by Xen’s common timer infrastructure.

The virtual timer is programmed in response to the guest SBI
sbi_set_timer() call and injects a virtual supervisor timer interrupt
into the vCPU when it expires.

While a dedicated struct vtimer is not strictly required at present,
it is expected to become necessary once SSTC support is introduced.
In particular, it will need to carry additional state such as whether
SSTC is enabled, the next compare value (e.g. for the VSTIMECMP CSR)
to be saved and restored across context switches, and time delta state
(e.g. HTIMEDELTA) required for use cases such as migration. Introducing
struct vtimer now avoids a later refactoring.

Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
This commit is contained in:
Oleksii Kurochko authored and Jan Beulich committed 2026-03-10 09:25:59 +01:00
1 parent 7379853e0c
commit 2473ddf9b4
5 files changed
+100 -2

No files matched your search

+1
View File
@@ -22,6 +22,7 @@ obj-y += traps.o
obj-y += vmid.o
obj-y += vm_event.o
obj-y += vsbi/
obj-y += vtimer.o
$(TARGET): $(TARGET)-syms
$(OBJCOPY) -O binary -S $< $@
+8 -2
View File
@@ -10,6 +10,7 @@
#include <asm/cpufeature.h>
#include <asm/csr.h>
#include <asm/riscv_encoding.h>
#include <asm/vtimer.h>
struct csr_masks {
register_t hedeleg;
@@ -148,11 +149,14 @@ int arch_vcpu_create(struct vcpu *v)
vcpu_csr_init(v);
if ( (rc = vcpu_vtimer_init(v)) )
goto fail;
/*
* As the vtimer and interrupt controller (IC) are not yet implemented,
* As interrupt controller (IC) is not yet implemented,
* return an error.
*
* TODO: Drop this once the vtimer and IC are implemented.
* TODO: Drop this once IC is implemented.
*/
rc = -EOPNOTSUPP;
goto fail;
@@ -166,6 +170,8 @@ int arch_vcpu_create(struct vcpu *v)
void arch_vcpu_destroy(struct vcpu *v)
{
vcpu_timer_destroy(v);
vfree((void *)&v->arch.cpu_info[1] - STACK_SIZE);
}
+3
View File
@@ -8,6 +8,7 @@
#include <public/hvm/params.h>
#include <asm/p2m.h>
#include <asm/vtimer.h>
struct vcpu_vmid {
uint64_t generation;
@@ -49,6 +50,8 @@ struct arch_vcpu {
struct cpu_info *cpu_info;
struct vtimer vtimer;
register_t hcounteren;
register_t hedeleg;
register_t hideleg;
+17
View File
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef ASM__RISCV__VTIMER_H
#define ASM__RISCV__VTIMER_H
#include <xen/timer.h>
struct vtimer {
struct timer timer;
};
int vcpu_vtimer_init(struct vcpu *v);
void vcpu_timer_destroy(struct vcpu *v);
void vtimer_set_timer(struct vtimer *t, uint64_t ticks);
#endif /* ASM__RISCV__VTIMER_H */
+71
View File
@@ -0,0 +1,71 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <xen/sched.h>
#include <xen/timer.h>
#include <asm/vtimer.h>
static void vtimer_expired(void *data)
{
struct vtimer *t = data;
struct vcpu *v = container_of(t, struct vcpu, arch.vtimer);
vcpu_set_interrupt(v, IRQ_VS_TIMER);
}
int vcpu_vtimer_init(struct vcpu *v)
{
struct vtimer *t = &v->arch.vtimer;
init_timer(&t->timer, vtimer_expired, t, v->processor);
return 0;
}
void vcpu_timer_destroy(struct vcpu *v)
{
struct vtimer *t = &v->arch.vtimer;
if ( t->timer.status == TIMER_STATUS_invalid )
return;
kill_timer(&v->arch.vtimer.timer);
}
void vtimer_set_timer(struct vtimer *t, const uint64_t ticks)
{
struct vcpu *v = container_of(t, struct vcpu, arch.vtimer);
s_time_t expires = ticks_to_ns(ticks - boot_clock_cycles);
vcpu_unset_interrupt(v, IRQ_VS_TIMER);
/*
* According to the RISC-V sbi spec:
* If the supervisor wishes to clear the timer interrupt without
* scheduling the next timer event, it can either request a timer
* interrupt infinitely far into the future (i.e., (uint64_t)-1),
* or it can instead mask the timer interrupt by clearing sie.STIE CSR
* bit.
*/
if ( ticks == ((uint64_t)~0) )
{
stop_timer(&t->timer);
return;
}
if ( expires < NOW() )
{
/*
* Simplify the logic if the timeout has already expired and just
* inject the event.
*/
stop_timer(&t->timer);
vcpu_set_interrupt(v, IRQ_VS_TIMER);
return;
}
migrate_timer(&t->timer, smp_processor_id());
set_timer(&t->timer, expires);
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.