From nobody Mon Mar 23 21:24:36 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=1774283460; cv=none; d=zohomail.com; s=zohoarc; b=kZkJ4OGQO0g7+rUAifxV8ZOcBhT6r/yZIF5ZxY++AOEAAMUC/+Gtli7I3+ciYsQnpd56lhDdVi8GezWmLRPTosPRhB9V6qzto9YwTakZisk96fJ6/ti89+Rhr5Qd+Ua4MsjHt7Gj7kXDYSnnauV1dh/nIKCV13+XefD3cuFMxME= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1774283460; 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=RkR/V7e+x0LZ5vy6oulB2qy/2ayyRS6OQM1v0e6EP4s=; b=lLmD6tKccaLWqo7h/KeTdmwatP8M1Tb+WOkq4+VPBUaJyohOOnuSVEaUvBNzyxMy3dkLAloXBMTqDgHrkRf8zOwbnuFAeksUO0ho5k+mO9ifRpDs17eB550RScuZYySRU9AgDQiI7fxefjyRvhFfYmRi4/WUkkT+Ow4hbGjjrnw= 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 17742834601045.662077406630829; Mon, 23 Mar 2026 09:31:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1259447.1552762 (Exim 4.92) (envelope-from ) id 1w4iAv-0005k3-2S; Mon, 23 Mar 2026 16:30:45 +0000 Received: by outflank-mailman (output) from mailman id 1259447.1552762; Mon, 23 Mar 2026 16:30:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1w4iAu-0005ju-VR; Mon, 23 Mar 2026 16:30:44 +0000 Received: by outflank-mailman (input) for mailman id 1259447; Mon, 23 Mar 2026 16:30:43 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1w4iAt-0005jA-Kb for xen-devel@lists.xenproject.org; Mon, 23 Mar 2026 16:30:43 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1w4iAr-005upU-Ey for xen-devel@lists.xenproject.org; Mon, 23 Mar 2026 17:30:43 +0100 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69c16aa9-e002-0a2a0a5209dd-0a2a45059b5c-28 for ; Mon, 23 Mar 2026 17:30:42 +0100 Received: from [209.85.218.52] (helo=mail-ej1-f52.google.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.55.2) (envelope-from ) id 69c16ab2-5aeb-0a2a45050019-d155da34e82d-3 for ; Mon, 23 Mar 2026 17:30:42 +0100 Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-b79f8f7ea43so63035366b.2 for ; Mon, 23 Mar 2026 09:30:42 -0700 (PDT) Received: from fedora (user-109-243-69-121.play-internet.pl. [109.243.69.121]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b9832f8be14sm536248166b.17.2026.03.23.09.30.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Mar 2026 09:30:32 -0700 (PDT) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20230601 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1774283442; x=1774888242; 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=RkR/V7e+x0LZ5vy6oulB2qy/2ayyRS6OQM1v0e6EP4s=; b=kQONQ+ql+0NfUwfI+2YTrOyGjlQXtHKYc8N9K1vzadtKjj31evo5OpQgo/JcKqARpG aTLm5GjYzAfJEdmth0czFIWoZhUMBZJTneElC16iC8lDkZVhDtreBCEDpMc9eOaMg9Pz LZhL4Eoosw/A3PA9HO3k0aSvbBpY2X1oE0fz6T+7i+flL7YRaYYeN7AlK0hhHOwFjntb GlhuR7AfZSKkDbZ9VINyQeG7LJAb9H0nRRMLrYaZvkBVk0f6ByoexsqyKB5U8CRPArMx D9skUlQHGMDFDlIPS27u4gO6m0zagHUhaFZt3+UH+As5siZ8bKc9+T6S8t9hFCIzAdl5 LIxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774283442; x=1774888242; 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=RkR/V7e+x0LZ5vy6oulB2qy/2ayyRS6OQM1v0e6EP4s=; b=r91PBURODfBE1Hy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1774283441888; Mon, 23 Mar 2026 09:30:41 -0700 (PDT) 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 v2 03/11] xen/riscv: implement copy_to_guest_phys() Date: Mon, 23 Mar 2026 17:29:44 +0100 Message-ID: <062e50c596b9271858d2d63e101bfde5311a3817.1774281309.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c201ff/1774283442-23082488-FF239CAA/0/0 X-purgate-type: clean X-purgate-size: 6033 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1774283462531154100 Content-Type: text/plain; charset="utf-8" Introduce copy_to_guest_phys() for RISC-V, based on the Arm implementation. Add a generic copy_guest() helper for copying to and from guest physical (and potentially virtual addresses in the future), and implement translate_get_page() to translate a guest physical address into a struct page_info via the domain p2m. Compared to the Arm code: - Drop COPY_flush_dcache(), as no such use cases exist on RISC-V. - Do not implement the linear mapping case, which is currently unused. - Use PAGE_OFFSET() to initialize the local offset variable in copy_guest(). Signed-off-by: Oleksii Kurochko --- Changes in v2: - Use BIT() instead of open-coding. - Rename COPY_ipa to COPY_gpa. - Rename COPY_linear to COPY_gva. - Use BUG_ON(linear) instead if (lineer) + BUG_ON. - Rename arg liner to gva for translate_get_page(). - Update translate_get_page() to properly handling write argument. - Return unsigned int for copy_guest() and copy_to_guest_phys() as len function parameter is only 'unsigned int'. - Reformat function arguments for alignment --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/guestcopy.c | 116 ++++++++++++++++++++++ xen/arch/riscv/include/asm/guest_access.h | 7 ++ 3 files changed, 124 insertions(+) create mode 100644 xen/arch/riscv/guestcopy.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index 6b3f3ed90bdb..6d3c822409b8 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -4,6 +4,7 @@ obj-y +=3D domain.o obj-$(CONFIG_EARLY_PRINTK) +=3D early_printk.o obj-y +=3D entry.o obj-$(CONFIG_HAS_EX_TABLE) +=3D extables.o +obj-y +=3D guestcopy.o obj-y +=3D imsic.o obj-y +=3D intc.o obj-y +=3D irq.o diff --git a/xen/arch/riscv/guestcopy.c b/xen/arch/riscv/guestcopy.c new file mode 100644 index 000000000000..d774a90bff92 --- /dev/null +++ b/xen/arch/riscv/guestcopy.c @@ -0,0 +1,116 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include +#include + +#include + +#define COPY_from_guest 0U +#define COPY_to_guest BIT(0, U) +#define COPY_gpa 0U +#define COPY_gva BIT(1, U) + +typedef union +{ + struct + { + struct vcpu *v; + } gva; + + struct + { + struct domain *d; + } gpa; +} copy_info_t; + +#define GVA_INFO(vcpu) ((copy_info_t) { .gva =3D { vcpu } }) +#define GPA_INFO(domain) ((copy_info_t) { .gpa =3D { domain } }) + +static struct page_info *translate_get_page(copy_info_t info, uint64_t add= r, + bool gva, bool write) +{ + p2m_type_t p2mt; + struct page_info *page; + + /* + * Not implemented yet. + * + * If gva =3D=3D true, the operation will likely require a struct vcpu + * rather than just a struct domain. For this reason copy_info_t is + * already passed here instead of only struct domain. + */ + BUG_ON(gva); + + page =3D get_page_from_gfn(info.gpa.d, paddr_to_pfn(addr), &p2mt, P2M_= ALLOC); + + if ( !page ) + return NULL; + + if ( write ? p2mt !=3D p2m_ram_rw : !p2m_is_ram(p2mt) ) + { + put_page(page); + return NULL; + } + + return page; +} + +static unsigned int copy_guest(void *buf, uint64_t addr, unsigned int len, + copy_info_t info, unsigned int flags) +{ + unsigned int offset =3D PAGE_OFFSET(addr); + + BUILD_BUG_ON((sizeof(addr)) < sizeof(vaddr_t)); + BUILD_BUG_ON((sizeof(addr)) < sizeof(paddr_t)); + + while ( len ) + { + void *p; + unsigned int size =3D min(len, (unsigned int)PAGE_SIZE - offset); + struct page_info *page; + + page =3D translate_get_page(info, addr, flags & COPY_gva, + flags & COPY_to_guest); + if ( page =3D=3D NULL ) + return len; + + p =3D __map_domain_page(page); + p +=3D offset; + if ( flags & COPY_to_guest ) + { + /* + * buf will be NULL when the caller request to zero the + * guest memory. + */ + if ( buf ) + memcpy(p, buf, size); + else + memset(p, 0, size); + } + else + memcpy(buf, p, size); + + unmap_domain_page(p - offset); + put_page(page); + len -=3D size; + buf +=3D size; + addr +=3D size; + + /* + * After the first iteration, guest virtual address is correctly + * aligned to PAGE_SIZE. + */ + offset =3D 0; + } + + return 0; +} + +unsigned int copy_to_guest_phys(struct domain *d, paddr_t gpa, void *buf, + unsigned int len) +{ + return copy_guest(buf, gpa, len, GPA_INFO(d), + COPY_to_guest | COPY_gpa); +} diff --git a/xen/arch/riscv/include/asm/guest_access.h b/xen/arch/riscv/inc= lude/asm/guest_access.h index 3f4c68e4da20..f0a42745330e 100644 --- a/xen/arch/riscv/include/asm/guest_access.h +++ b/xen/arch/riscv/include/asm/guest_access.h @@ -2,6 +2,10 @@ #ifndef ASM__RISCV__GUEST_ACCESS_H #define ASM__RISCV__GUEST_ACCESS_H =20 +#include + +struct domain; + unsigned int raw_copy_to_guest(void *to, const void *from, unsigned len); unsigned int raw_copy_from_guest(void *to, const void *from, unsigned len); unsigned int raw_clear_guest(void *to, unsigned int len); @@ -18,6 +22,9 @@ unsigned int raw_clear_guest(void *to, unsigned int len); #define guest_handle_okay(hnd, nr) (1) #define guest_handle_subrange_okay(hnd, first, last) (1) =20 +unsigned int copy_to_guest_phys(struct domain *d, paddr_t gpa, void *buf, + unsigned int len); + #endif /* ASM__RISCV__GUEST_ACCESS_H */ /* * Local variables: --=20 2.53.0