[PATCH v2 10/16] xen/riscv: add vtimer context switch helpers

Oleksii Kurochko posted 16 patches 2 weeks, 3 days ago
[PATCH v2 10/16] xen/riscv: add vtimer context switch helpers
Posted by Oleksii Kurochko 2 weeks, 3 days ago
Introduce vtimer_ctx_switch_from() and vtimer_ctx_switch_to() to handle
virtual timer state across vCPU context switches.

At present, vtimer_ctx_switch_from() is a no-op because the RISC-V SSTC
extension, which provides a virtualization-aware timer, is not yet
supported. Xen therefore relies the virtual (SBI-based) timer.

The virtual timer uses Xen's internal timer infrastructure and must be
associated with the pCPU on which the vCPU is currently running so that
timer events can be delivered efficiently. As a result, vtimer_ctx_switch_to()
migrates the timer to the target pCPU when a vCPU is scheduled in.

Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
Changes in v2:
 - Align the parameters names for  vtimer_ctx_switch_from() and vtimer_ctx_switch_to() in
   declarations to match the ones in the defintions to make Misra happy.
 - s/vtimer_save/vtimer_ctx_switch_from.
 - s/vtimer_restore/vtimer_ctx_switch_to.
 - Update the commit message.
---
 xen/arch/riscv/include/asm/vtimer.h |  3 +++
 xen/arch/riscv/vtimer.c             | 15 +++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/xen/arch/riscv/include/asm/vtimer.h b/xen/arch/riscv/include/asm/vtimer.h
index 0d1555511755..52b7fb7b1cbb 100644
--- a/xen/arch/riscv/include/asm/vtimer.h
+++ b/xen/arch/riscv/include/asm/vtimer.h
@@ -17,4 +17,7 @@ void vcpu_timer_destroy(struct vcpu *v);
 
 void vtimer_set_timer(struct vtimer *t, uint64_t ticks);
 
+void vtimer_ctx_switch_from(struct vcpu *p);
+void vtimer_ctx_switch_to(struct vcpu *n);
+
 #endif /* ASM__RISCV__VTIMER_H */
diff --git a/xen/arch/riscv/vtimer.c b/xen/arch/riscv/vtimer.c
index b6599fa383b8..6dfd6d836260 100644
--- a/xen/arch/riscv/vtimer.c
+++ b/xen/arch/riscv/vtimer.c
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/bug.h>
 #include <xen/sched.h>
 #include <xen/timer.h>
 
@@ -71,3 +72,17 @@ void vtimer_set_timer(struct vtimer *t, const uint64_t ticks)
     migrate_timer(&t->timer, smp_processor_id());
     set_timer(&t->timer, expires);
 }
+
+void vtimer_ctx_switch_from(struct vcpu *p)
+{
+    ASSERT(!is_idle_vcpu(p));
+
+    /* Nothing to do at the moment as SSTC isn't supported now. */
+}
+
+void vtimer_ctx_switch_to(struct vcpu *n)
+{
+    ASSERT(!is_idle_vcpu(n));
+
+    migrate_timer(&n->arch.vtimer.timer, n->processor);
+}
-- 
2.52.0
Re: [PATCH v2 10/16] xen/riscv: add vtimer context switch helpers
Posted by Jan Beulich 5 days, 5 hours ago
On 22.01.2026 17:47, Oleksii Kurochko wrote:
> Introduce vtimer_ctx_switch_from() and vtimer_ctx_switch_to() to handle
> virtual timer state across vCPU context switches.
> 
> At present, vtimer_ctx_switch_from() is a no-op because the RISC-V SSTC
> extension, which provides a virtualization-aware timer, is not yet
> supported. Xen therefore relies the virtual (SBI-based) timer.
> 
> The virtual timer uses Xen's internal timer infrastructure and must be
> associated with the pCPU on which the vCPU is currently running so that
> timer events can be delivered efficiently. As a result, vtimer_ctx_switch_to()
> migrates the timer to the target pCPU when a vCPU is scheduled in.
> 
> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>

Acked-by: Jan Beulich <jbeulich@suse.com>

However, ...

> --- a/xen/arch/riscv/include/asm/vtimer.h
> +++ b/xen/arch/riscv/include/asm/vtimer.h
> @@ -17,4 +17,7 @@ void vcpu_timer_destroy(struct vcpu *v);
>  
>  void vtimer_set_timer(struct vtimer *t, uint64_t ticks);
>  
> +void vtimer_ctx_switch_from(struct vcpu *p);
> +void vtimer_ctx_switch_to(struct vcpu *n);

... may I ask that you reconsider naming here? Both Arm and x86 have functions
where the prefix / infix is "ctxt", not just "ctx". Being able to find all by
grep-ing for e.g. ctxt_switch_from might be quite nice.

Jan
Re: [PATCH v2 10/16] xen/riscv: add vtimer context switch helpers
Posted by Oleksii Kurochko 5 days, 4 hours ago
On 2/3/26 5:43 PM, Jan Beulich wrote:
> On 22.01.2026 17:47, Oleksii Kurochko wrote:
>> Introduce vtimer_ctx_switch_from() and vtimer_ctx_switch_to() to handle
>> virtual timer state across vCPU context switches.
>>
>> At present, vtimer_ctx_switch_from() is a no-op because the RISC-V SSTC
>> extension, which provides a virtualization-aware timer, is not yet
>> supported. Xen therefore relies the virtual (SBI-based) timer.
>>
>> The virtual timer uses Xen's internal timer infrastructure and must be
>> associated with the pCPU on which the vCPU is currently running so that
>> timer events can be delivered efficiently. As a result, vtimer_ctx_switch_to()
>> migrates the timer to the target pCPU when a vCPU is scheduled in.
>>
>> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
> Acked-by: Jan Beulich <jbeulich@suse.com>

Thanks.

>
> However, ...
>
>> --- a/xen/arch/riscv/include/asm/vtimer.h
>> +++ b/xen/arch/riscv/include/asm/vtimer.h
>> @@ -17,4 +17,7 @@ void vcpu_timer_destroy(struct vcpu *v);
>>   
>>   void vtimer_set_timer(struct vtimer *t, uint64_t ticks);
>>   
>> +void vtimer_ctx_switch_from(struct vcpu *p);
>> +void vtimer_ctx_switch_to(struct vcpu *n);
> ... may I ask that you reconsider naming here? Both Arm and x86 have functions
> where the prefix / infix is "ctxt", not just "ctx". Being able to find all by
> grep-ing for e.g. ctxt_switch_from might be quite nice.

Sure, I will do that, it makes sense.

~ Oleksii