From nobody Sun Dec 14 11:59:01 2025 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=1764584697; cv=none; d=zohomail.com; s=zohoarc; b=dgloqQOuicA+YCSaDLeVp4sK0F7LK8bPSidXZDL516ykbsLV55AZmltqNJWkOYBg+HYgd0acDKGkScLppWVuKO/F+QrBdNdPz0ni0uo5flms1WhxvYIKF95mJ6Wi33ldeSuBlEE7EhRpUtrJzynujhLrgtVAxjMD9zh9S+bZ5iM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1764584697; h=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=kL4brVLXUEDyfmktWCJt4oVhAjrxxXr8mVfyNYUUh4g=; b=c+cBjAV5bnDWC+5QaUZkSeEcuscHEhqMCaxIz7JGFtF5AdYwDj4wB7yUG3PlToq+qkmD+mdi+Dje49oW5XKtEjj7YcEs4alyBdKDvcnPdwq4i/DzCBkNPqPyij1h6dCS18qNK4Z3MAcNcheSFo367yhDH22D7R+JMzJ09M9WXQg= 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 1764584697148628.5204485553355; Mon, 1 Dec 2025 02:24:57 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1175650.1500270 (Exim 4.92) (envelope-from ) id 1vQ14x-0004FY-Nv; Mon, 01 Dec 2025 10:24:23 +0000 Received: by outflank-mailman (output) from mailman id 1175650.1500270; Mon, 01 Dec 2025 10:24:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vQ14x-0004FR-L5; Mon, 01 Dec 2025 10:24:23 +0000 Received: by outflank-mailman (input) for mailman id 1175650; Mon, 01 Dec 2025 10:24:22 +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 1vQ14w-00041l-8H for xen-devel@lists.xenproject.org; Mon, 01 Dec 2025 10:24:22 +0000 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [2a00:1450:4864:20::531]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id e5df3a98-ce9f-11f0-9d19-b5c5bf9af7f9; Mon, 01 Dec 2025 11:24:20 +0100 (CET) Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-640aa1445c3so6082833a12.1 for ; Mon, 01 Dec 2025 02:24:20 -0800 (PST) Received: from fedora (user-109-243-71-38.play-internet.pl. [109.243.71.38]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-647510508e1sm12104724a12.27.2025.12.01.02.24.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Dec 2025 02:24:18 -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: e5df3a98-ce9f-11f0-9d19-b5c5bf9af7f9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764584659; x=1765189459; 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=kL4brVLXUEDyfmktWCJt4oVhAjrxxXr8mVfyNYUUh4g=; b=QyrNZBAbhL3d18StdX1x9+dFgrViHwJ41ovITKtg7LNT4mYOWdmWeBHIWKsa9WxTyI bJPuMCpLFhT2xBnnyFwLPDaWLwomKwB2XX9wGeVC2/EgPO0Y6SQlyxuOGRA2rYq2JabO CRNocLGx3Ddk4cfobOFK5xe9Je0lmzXxYiqDYoZRO0h6ONzmPBPmPkUM7kTJ1+kcERJh t+CeVvWyvygbLI3UvD1LV5YvQH5UwzC/EgmfW5FQZylK3rdqdMIIJkutycIPCbud+zIx XtWX9Fu8Ap89BPa8uJmiQ6QtAIBVUDdIVLX3EWOMn2aB0tjMbU3zp1lIstFjPtHFBwHN Jf1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764584659; x=1765189459; 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=kL4brVLXUEDyfmktWCJt4oVhAjrxxXr8mVfyNYUUh4g=; b=T4KqQDAhwMwBsSMqbddh0BcF16zyLTS/4bX7JWwwqGccneVzy2VsBLlM5bkMrFsmvX KwmHdgYpU9Z0fu94c7+1pSf55y1SnYTzrEe6SoDSC7Wl2SE5AtusbnqDygDE+H1WDwIX O/IwOi+/Tg6Z/kbLSYvb/e0cZatMyfWKIwMs/gADpnbLfY2h4Lj2cuyQWPYmZIjTlFaO zz4Iii+MrWy//6WSpV3QcRj2sACRgb+ydzBxE46K4e8KhrChjOhCOcaYuGe/YU2doMOV 9j7JQCUifEgws/EPCX0tDHON41ao4tsQuiLiNWQ3jId8xUMzUasobOgQkwB6EKm3W+sW Lj1A== X-Gm-Message-State: AOJu0YzKuGaIU64JtcbampMKOZkE+qYIhrl0yXa0Dc0JUx0dMal/MVtf vnHjoFGTk6b3OWZOm2QNUuar/5xaPSJ1/VeKwsuhLeV/Uun+IROYhXrAuQjAOtkD X-Gm-Gg: ASbGncszsRZOyGoTWQGp1xasgp5YIFm+N1ijxQ4YTbVgVwytCdzvlrcBgSrAaPAEip6 JcVKcPeBviN1/e+C0LQQR72DPXUZo/otUJrdfEpYHGjiokVTJnLg/WIbDLjKU6YTiH4SK9p148a uTLP7szPNtttI31RHmdrfDmog43SGrhxY6bkYakxtlDnaG23mcJXWzi2kLKAk8YTno8ZXP5oTGE SqPPkvvhj7EtDhqzxTVByNFII9Z3c8xgfqDDb3GTmCSUejuM41Bif5nr0TQ9PkRKi0dc4fKWk6J 7U7+oxXplsY6sRhr50BEvuP+jcm4FCyVp60vo/toNvddyUt4dOnJgoVaWxJkeE9x1HDbo5juUYn UgrqybTTJzTlDhcIbL8j9HnbruQIlsVHxig8L6tvj3V72iJK1CZcNO+rMgPPSHTaUCgf3sUK4kl kajXlI0hyWGRXv6RxdAf5YKZl4DmS5Zw2I0O9e4p8geQ/sTvTktGxr/Ps= X-Google-Smtp-Source: AGHT+IFZryrOYkb6E/sYYZM1iPLmqTVJAElhSu2fpr7iBRoTe37+HPbTSir8+/dPNhmm4EjTHBrQVQ== X-Received: by 2002:a05:6402:3494:b0:647:5c27:5440 with SMTP id 4fb4d7f45d1cf-6475c2754b6mr11197062a12.24.1764584659125; Mon, 01 Dec 2025 02:24:19 -0800 (PST) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Oleksii Kurochko , Alistair Francis , Bob Eshleman , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v1 1/3] xen/riscv: introduce vSBI extension framework Date: Mon, 1 Dec 2025 11:24:08 +0100 Message-ID: <3b67330dc4c1aa053eb15261a559e7b4eac3f493.1764582112.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1764584699440019200 Content-Type: text/plain; charset="utf-8" This commit introduces support for handling virtual SBI extensions in Xen. The changes include: - Added new vsbi.c and vsbi.h files to implement virtual SBI extension handling. - Modified traps.c to handle CAUSE_VIRTUAL_SUPERVISOR_ECALL by calling vsbi_handle_ecall() when the trap originates from VS-mode. - Updated xen.lds.S to include a new .vsbi.exts section for virtual SBI extension data. - Updated Makefile to include the new vsbi/ directory in the build. - Add hstatus register to struct cpu_user_regs as it is needed for a check that CAUSE_VIRTUAL_SUPERVISOR_ECALL happens from VS-mode. The implementation allows for registration and handling of SBI extensions via a new vsbi_ext structure and ".vsbi.exts" section, enabling extensible virtual SBI support for RISC-V guests. Signed-off-by: Oleksii Kurochko --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/processor.h | 1 + xen/arch/riscv/include/asm/vsbi.h | 31 +++++++++++++++++ xen/arch/riscv/traps.c | 8 +++++ xen/arch/riscv/vsbi/Makefile | 1 + xen/arch/riscv/vsbi/vsbi.c | 46 ++++++++++++++++++++++++++ xen/arch/riscv/xen.lds.S | 7 ++++ 7 files changed, 95 insertions(+) create mode 100644 xen/arch/riscv/include/asm/vsbi.h create mode 100644 xen/arch/riscv/vsbi/Makefile create mode 100644 xen/arch/riscv/vsbi/vsbi.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index e2b8aa42c8..7bfe7024ef 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -17,6 +17,7 @@ obj-y +=3D stubs.o obj-y +=3D time.o obj-y +=3D traps.o obj-y +=3D vm_event.o +obj-y +=3D vsbi/ =20 $(TARGET): $(TARGET)-syms $(OBJCOPY) -O binary -S $< $@ diff --git a/xen/arch/riscv/include/asm/processor.h b/xen/arch/riscv/includ= e/asm/processor.h index 39696fb58d..79d02c3dd2 100644 --- a/xen/arch/riscv/include/asm/processor.h +++ b/xen/arch/riscv/include/asm/processor.h @@ -49,6 +49,7 @@ struct cpu_user_regs unsigned long t6; unsigned long sepc; unsigned long sstatus; + unsigned long hstatus; /* pointer to previous stack_cpu_regs */ unsigned long pregs; }; diff --git a/xen/arch/riscv/include/asm/vsbi.h b/xen/arch/riscv/include/asm= /vsbi.h new file mode 100644 index 0000000000..984e7acf7b --- /dev/null +++ b/xen/arch/riscv/include/asm/vsbi.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef ASM_RISCV_VSBI_H +#define ASM_RISCV_VSBI_H + +struct regs; +struct vcpu; + +struct vsbi_ext { + const char *name; + unsigned long eid_start; + unsigned long eid_end; + int (*handle)(struct vcpu *vcpu, unsigned long eid, + unsigned long fid, struct cpu_user_regs *regs); +}; + +#define VSBI_EXT_START(ext, extid_start, extid_end, extid_handle) \ +static const struct vsbi_ext vsbi_ext_##ext __used \ +__section(".vsbi.exts") =3D { \ + .name =3D #ext, \ + .eid_start =3D extid_start, \ + .eid_end =3D extid_end, \ + .handle =3D extid_handle, + +#define VSBI_EXT_END \ +}; + +void vsbi_handle_ecall(struct vcpu *vcpu, struct cpu_user_regs *regs); +const struct vsbi_ext *vsbi_find_extension(unsigned long ext_id); + +#endif diff --git a/xen/arch/riscv/traps.c b/xen/arch/riscv/traps.c index f061004d83..dfe1a5a112 100644 --- a/xen/arch/riscv/traps.c +++ b/xen/arch/riscv/traps.c @@ -15,6 +15,7 @@ #include #include #include +#include =20 /* * Initialize the trap handling. @@ -114,6 +115,13 @@ void do_trap(struct cpu_user_regs *cpu_regs) =20 switch ( cause ) { + case CAUSE_VIRTUAL_SUPERVISOR_ECALL: + if ( !(cpu_regs->hstatus & HSTATUS_SPV) ) + panic("CAUSE_VIRTUAL_SUPERVISOR_ECALL came not from VS-mode\n"= ); + + vsbi_handle_ecall(current, cpu_regs); + break; + case CAUSE_ILLEGAL_INSTRUCTION: if ( do_bug_frame(cpu_regs, pc) >=3D 0 ) { diff --git a/xen/arch/riscv/vsbi/Makefile b/xen/arch/riscv/vsbi/Makefile new file mode 100644 index 0000000000..574c8ff78d --- /dev/null +++ b/xen/arch/riscv/vsbi/Makefile @@ -0,0 +1 @@ +obj-y +=3D vsbi.o diff --git a/xen/arch/riscv/vsbi/vsbi.c b/xen/arch/riscv/vsbi/vsbi.c new file mode 100644 index 0000000000..cd119ce0d6 --- /dev/null +++ b/xen/arch/riscv/vsbi/vsbi.c @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include + +#include +#include +#include + +extern const struct vsbi_ext _svsbi_exts[], _evsbi_exts[]; + +const struct vsbi_ext *vsbi_find_extension(unsigned long ext_id) +{ + const struct vsbi_ext *vsbi_ext; + + for ( vsbi_ext =3D _svsbi_exts; vsbi_ext !=3D _evsbi_exts; vsbi_ext++ ) + if ( ext_id >=3D vsbi_ext->eid_start && + ext_id <=3D vsbi_ext->eid_end ) + return vsbi_ext; + + return NULL; +} + +void vsbi_handle_ecall(struct vcpu *vcpu, struct cpu_user_regs *regs) +{ + const unsigned long eid =3D regs->a7; + const unsigned long fid =3D regs->a6; + const struct vsbi_ext *ext =3D vsbi_find_extension(eid); + int ret; + + if ( ext && ext->handle ) + ret =3D ext->handle(vcpu, eid, fid, regs); + else + { + printk("Unsupported Guest SBI EID #%#lx, FID #%lu\n", eid, regs->a= 1); + ret =3D SBI_ERR_NOT_SUPPORTED; + } + + /* + * The ecall instruction is not part of the RISC-V C extension (compre= ssed + * instructions), so it is always 4 bytes long. Therefore, it is safe = to + * use a fixed length of 4 bytes instead of reading guest memory to + * determine the instruction length. + */ + regs->sepc +=3D 4; + regs->a0 =3D ret; +} diff --git a/xen/arch/riscv/xen.lds.S b/xen/arch/riscv/xen.lds.S index edcadff90b..2967f00ac5 100644 --- a/xen/arch/riscv/xen.lds.S +++ b/xen/arch/riscv/xen.lds.S @@ -91,6 +91,13 @@ SECTIONS =20 DT_DEV_INFO /* Devicetree based device info */ =20 + . =3D ALIGN(POINTER_ALIGN); + DECL_SECTION(.vsbi.exts) { + _svsbi_exts =3D .; + *(.vsbi.exts) + _evsbi_exts =3D .; + } :text + . =3D ALIGN(PAGE_SIZE); /* Init code and data */ __init_begin =3D .; .init.text : { --=20 2.52.0