From nobody Tue Mar 3 04:56:06 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1772106715; cv=none; d=zohomail.com; s=zohoarc; b=LwJ54kgqRDOehFlXGcnbHriwcDZh0naCIDAa4qKhS4/ZCqB/8DytCTkJ7BXxzq/X06pNW+a/W7ANYy+rclsYvgIjPYQCHB37WNoPn8ExEtdNvy/M8E27cl7LaHCi2emYW2viJrVo/1xMlJOu992v4HwZjkoF34LBC4TgyJR8QxE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1772106715; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WQbN7SP3fjaaAEDIkqEkmnk4ywUJEbwgcsMMvUSaPN0=; b=bev7WuDD0oCcZX2ZVdeI3Zbb+H69grJb3T9ta3x+x75nqVXK0GZvKnF01FKKwf9iRWnuCbl9HZxq48uhJU5F2fPMCpSH1ei/k06cGRRguM07y2UAA+3vjCmXuOejleW6HNE9g/HcZGOAsnCqzifSHC49xFTX2FeR6C0stYyDhdY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1772106715346128.96571390199358; Thu, 26 Feb 2026 03:51:55 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1241457.1542534 (Exim 4.92) (envelope-from ) id 1vvZu3-0007cg-PV; Thu, 26 Feb 2026 11:51:35 +0000 Received: by outflank-mailman (output) from mailman id 1241457.1542534; Thu, 26 Feb 2026 11:51:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vvZu3-0007cW-Kh; Thu, 26 Feb 2026 11:51:35 +0000 Received: by outflank-mailman (input) for mailman id 1241457; Thu, 26 Feb 2026 11:51:34 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vvZu2-0006E8-93 for xen-devel@lists.xenproject.org; Thu, 26 Feb 2026 11:51:34 +0000 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [2a00:1450:4864:20::329]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7ef86abe-1309-11f1-b164-2bf370ae4941; Thu, 26 Feb 2026 12:51:33 +0100 (CET) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-483770e0b25so7370705e9.0 for ; Thu, 26 Feb 2026 03:51:33 -0800 (PST) Received: from fedora (user-109-243-67-101.play-internet.pl. [109.243.67.101]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-483bd68826asm220079295e9.0.2026.02.26.03.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Feb 2026 03:51:30 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7ef86abe-1309-11f1-b164-2bf370ae4941 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772106693; x=1772711493; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WQbN7SP3fjaaAEDIkqEkmnk4ywUJEbwgcsMMvUSaPN0=; b=ceT4UyAZvKTKCGcH9s35BtWNIBBKYwaDKDnDLDlhDBOUtMeDt9SWjpJwoo7FyFP24o TLM87+A6N8iFS1k4km0NeekHUDFh5v/VF/NokR9UjtPnHw9A838Pu4nynv7BHKK6yhF8 tA9nreJa0csrx9g5X8g9880N+v4PuLfWOuBUz9M/cQ5b9+sHSwj58SmRiBZak0mn+8IQ vBUitBNpDCKKbOeCSu34bnqukE9Dj76AUCg60g6PePZxwMxqBv3Du3UGdnYATKJAKWsn ok83wV4Hk9ncIxr/XE3yEmIin/VugFJ2Fkkx/rfRMrKV+dIjZx0VnGRuQy1mI9+mmOw5 /yXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772106693; x=1772711493; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=WQbN7SP3fjaaAEDIkqEkmnk4ywUJEbwgcsMMvUSaPN0=; b=vpCQ+cGiEXaJvn6m7xlvsmK+EhdRjcuC/k7Rs40IJsP1iRyKSsxrf6vMSiUWZOmBp8 tzR886kk1wv5mXIpSpr5peC0w0RLPJjONcn3HpdA+hrrZ/fNZ71Dk0ZOBusOU9egD7iY 9lK1tNYUDgMmyR6BvGuXPV9v4o+lmEnGvqZheI5S8WsowoQIpWCy589nR9Huq6Awssp2 hnZd3Go2Gc6S4bxoGEFkD4U5uLMCzR5xB+b1zpsDfzJnxiQNumeSgbRvWFOgczjfJ9Ew /k4YhUot/c1NwETSF59ScHrEOLa4LbbsCVXaKif2JjV4UWcWUVWmxLF9FZ6LFtLUXKPS +uJw== X-Gm-Message-State: AOJu0Ywel0ggIB9upZaem4zAmfdTifIt2WwAZNBPmt74JMS3Bzf87S1G wnze+mCwP9KocFDWKt34dAf0vxZDDpkafAuaFO+smQrOHoJNUyADHEqAs4HkqA== X-Gm-Gg: ATEYQzy/e1HZcPwbIaoGXAmC6YTvrPsqF/OIr/VQcblk62o6UnoCdK8z0UHCknEdXV7 rkJYta2fDvVPl10VAurxpJ275zkh6tF+GBaPP4Qao4M78W30zRU991rANVr0IfM6VKOncVnHtX6 DSe0Vu9U444T5Iajx1W3DGqj+HHry3KIAx1ndU6tlNv889eIiZrFqD0Tti9WXrqBlNn6U6uS1Wu +7kD521+852Us9dL6uRXhPidgwsRAR/pp6VeD5qJGJH6c2Qjqb6/5USWvNstMdXBCg+AiyI9YMW hhhzA6ejhElkdZHJFAekkLoOC16lq6aOZZ3jAplTmoSmacscTySfKlR70sG1suuL8/LEBNb3935 J9gdQvPkpG24JL55FkYSjPAy8hglf6sJ8fK3j+YY55Jco3JM7tvMC+Ik6OMv3bTTEa3dH2fDkZr aHw12ye1j1t/ojcfxi1uXq7Osp4aXLsgYVeOTfPoVEYldjtEPmjh8x8cdLS8PyPcaTw4aHLYCGx iri X-Received: by 2002:a05:600c:4751:b0:475:de12:d3b5 with SMTP id 5b1f17b1804b1-483a963df99mr310294475e9.34.1772106692483; Thu, 26 Feb 2026 03:51:32 -0800 (PST) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Romain Caritey , Oleksii Kurochko , Alistair Francis , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v6 06/14] xen/riscv: introduce basic vtimer infrastructure for guests Date: Thu, 26 Feb 2026 12:51:06 +0100 Message-ID: X-Mailer: git-send-email 2.53.0 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1772106717052158500 Lay the groundwork for guest timer support by introducing a per-vCPU virtual timer backed by Xen=E2=80=99s 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 Acked-by: Jan Beulich --- Changes in v6: - Nothing changed. Only rebase. --- Changes in v5: - Drop copyright line from asm/vtimer.h. - Add Acked-by: Jan Beulich . --- Changes in v4: - Add vcpu_timer_destroy() to void arch_vcpu_destroy(). --- Changes in v3: - use one container_of() to get vcpu instead of two container_of()s. --- Changes in v2: - Drop domain_vtimer_init() as it does nothing. - Drop "struct vcpu *v;" from struct vtimer as it could be taken from arch_vcpu using container_of(). - Drop vtimer_initialized, use t->status =3D=3D TIMER_STATUS_invalid instead to understand if timer was or wasn't initialized. - Drop inclusion of xen/domain.h as xen/sched.h already includes it. - s/ xen/time.h/ xen.timer.h in vtimer.c. - Drop ULL in if-conidtion in vtimer_set_timer() as with the cast it isn't necessary to have suffix ULL. - Add migrate timer to vtimer_set_timer() to be sure that vtimer will occur on pCPU it was ran, so the signalling to that vCPU will (commonly) be cheaper. - Check if the timeout has already expired and just inject the event in vtimer_vtimer_set_timer(). - Drop const for ticks argument of vtimer_set_timer(). - Merge two patches to one: - xen/riscv: introduce vtimer - xen/riscv: introduce vtimer_set_timer() and vtimer_expired() --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/domain.c | 10 +++- xen/arch/riscv/include/asm/domain.h | 3 ++ xen/arch/riscv/include/asm/vtimer.h | 17 +++++++ xen/arch/riscv/vtimer.c | 71 +++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 xen/arch/riscv/include/asm/vtimer.h create mode 100644 xen/arch/riscv/vtimer.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index bc47e83b26d7..ffbd7062e214 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -22,6 +22,7 @@ obj-y +=3D traps.o obj-y +=3D vmid.o obj-y +=3D vm_event.o obj-y +=3D vsbi/ +obj-y +=3D vtimer.o =20 $(TARGET): $(TARGET)-syms $(OBJCOPY) -O binary -S $< $@ diff --git a/xen/arch/riscv/domain.c b/xen/arch/riscv/domain.c index 6988d7032059..56b28750d3aa 100644 --- a/xen/arch/riscv/domain.c +++ b/xen/arch/riscv/domain.c @@ -10,6 +10,7 @@ #include #include #include +#include =20 struct csr_masks { register_t hedeleg; @@ -138,11 +139,14 @@ int arch_vcpu_create(struct vcpu *v) =20 vcpu_csr_init(v); =20 + if ( (rc =3D 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 =3D -EOPNOTSUPP; goto fail; @@ -156,6 +160,8 @@ int arch_vcpu_create(struct vcpu *v) =20 void arch_vcpu_destroy(struct vcpu *v) { + vcpu_timer_destroy(v); + vfree((void *)&v->arch.cpu_info[1] - STACK_SIZE); } =20 diff --git a/xen/arch/riscv/include/asm/domain.h b/xen/arch/riscv/include/a= sm/domain.h index 5373a498d80b..c0de8612cc25 100644 --- a/xen/arch/riscv/include/asm/domain.h +++ b/xen/arch/riscv/include/asm/domain.h @@ -8,6 +8,7 @@ #include =20 #include +#include =20 struct vcpu_vmid { uint64_t generation; @@ -49,6 +50,8 @@ struct arch_vcpu { =20 struct cpu_info *cpu_info; =20 + struct vtimer vtimer; + register_t hcounteren; register_t hedeleg; register_t hideleg; diff --git a/xen/arch/riscv/include/asm/vtimer.h b/xen/arch/riscv/include/a= sm/vtimer.h new file mode 100644 index 000000000000..111863610a92 --- /dev/null +++ b/xen/arch/riscv/include/asm/vtimer.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef ASM__RISCV__VTIMER_H +#define ASM__RISCV__VTIMER_H + +#include + +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 */ diff --git a/xen/arch/riscv/vtimer.c b/xen/arch/riscv/vtimer.c new file mode 100644 index 000000000000..32d142bcdfcd --- /dev/null +++ b/xen/arch/riscv/vtimer.c @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include + +#include + +static void vtimer_expired(void *data) +{ + struct vtimer *t =3D data; + struct vcpu *v =3D container_of(t, struct vcpu, arch.vtimer); + + vcpu_set_interrupt(v, IRQ_VS_TIMER); +} + +int vcpu_vtimer_init(struct vcpu *v) +{ + struct vtimer *t =3D &v->arch.vtimer; + + init_timer(&t->timer, vtimer_expired, t, v->processor); + + return 0; +} + +void vcpu_timer_destroy(struct vcpu *v) +{ + struct vtimer *t =3D &v->arch.vtimer; + + if ( t->timer.status =3D=3D TIMER_STATUS_invalid ) + return; + + kill_timer(&v->arch.vtimer.timer); +} + +void vtimer_set_timer(struct vtimer *t, const uint64_t ticks) +{ + struct vcpu *v =3D container_of(t, struct vcpu, arch.vtimer); + s_time_t expires =3D 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 C= SR + * bit. + */ + if ( ticks =3D=3D ((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); +} --=20 2.53.0