From nobody Wed Aug 26 08:50:41 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=1778144370; cv=none; d=zohomail.com; s=zohoarc; b=hZ9hNOATn3VSMpY18OByLxWTiQl4yvorrvwVsOlDplk5FaBZ6iPznrwYvGxK/sTj1Z1jd2PSZ/+X4103445DverrtVuQZKdPvgaGU39GWFfRbXUsZ2nE1XhAoI8p0W1pCVBfMuyyMI1slp2WxGc5Dg2q4Kn59xIbPXyW7Dw6dOU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1778144370; 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=wGQ7zO8AKfHP1ag+tsRcXVHwApXHfJapq9I/qFco5NE=; b=OT6lJK14sZtGJePyZLNznyxOAhF1oo22DEFft7ofdXTnX5BQL690wtV5Z/tAnSDPNKICVNx4dnCbwsOuzXoCcFQ1O7k5dAnk2NAi93QGduKSFYQNwnBLBbligGU+1w135/JAqCmKTWxkXAyZx6NoK32eOvkF7nFlHCNJydF5NYI= 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 1778144370389266.34201505791; Thu, 7 May 2026 01:59:30 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1302336.1576330 (Exim 4.92) (envelope-from ) id 1wKuZf-0003ft-K4; Thu, 07 May 2026 08:59:15 +0000 Received: by outflank-mailman (output) from mailman id 1302336.1576330; Thu, 07 May 2026 08:59:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wKuZf-0003fe-H9; Thu, 07 May 2026 08:59:15 +0000 Received: by outflank-mailman (input) for mailman id 1302336; Thu, 07 May 2026 08:59:13 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) id 1wKuZd-0003LL-Gs for xen-devel@lists.xenproject.org; Thu, 07 May 2026 08:59:13 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wKuZc-002yCt-Sp for xen-devel@lists.xenproject.org; Thu, 07 May 2026 10:59:12 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69fc545c-2eae-0a2a0a5409dd-0a2a4508d59a-10 for ; Thu, 07 May 2026 10:59:12 +0200 Received: from [209.85.218.43] (helo=mail-ej1-f43.google.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 69fc5460-63b5-0a2a45080019-d155da2bcd56-3 for ; Thu, 07 May 2026 10:59:12 +0200 Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-bb962ce4dcfso87521966b.1 for ; Thu, 07 May 2026 01:59:12 -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-bc81d5e9583sm57549966b.17.2026.05.07.01.59.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 May 2026 01:59:10 -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=20251104 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=20251104; t=1778144352; x=1778749152; 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=wGQ7zO8AKfHP1ag+tsRcXVHwApXHfJapq9I/qFco5NE=; b=jf/i/+rDqRPS5qI4SAfOdsRF1r5QN2gNCYxHQELqmI2ZZg8Rsb53/D3Ip+UTOCQcS5 CfXtUDO7e4+FdnWLChhjcjvUbg61o7yLnL4Ba5w9iEUnbVwpuRg8nLhWKoLIodCm+uOq mIYjZTdf3SXed18DglZG8pEY8grbL5bYUPw/24lC/4gkTHIJPX8w1WEsRTm/kFoqaY6l aFrVKe8AAr4HWj6BvKRNgT0+Iwj6O1/RD43Wf9eXJUI9vC36X5ji2KIXXeUx44K9pQc4 DyGDPGy0CvpH80EHEdaamqhJ9p/ZMc3JVjVtPFEBEVYrHk3FKH+YBMxLgnUe2yXYG6GW LqDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778144352; x=1778749152; 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=wGQ7zO8AKfHP1ag+tsRcXVHwApXHfJapq9I/qFco5NE=; b=LrViiuvETZVScqRUCoTxeUTXhSpdrQ9+rb9pUjFkMFzHodmztqONg7bsfnqymy9Axf rS1lAZF+wvxanvk8Li6UGlTc0BNPlErutSLwiMGG/rDnutIvp+3Us4+nJ6TZKH5umrua GMV1E+UmJdWUgpuw3UKlWlt+4FkQU+8v7HfXeNck+uUeJysO0VFDxvjLLoL0uMcD5i5W k7+NcACX7QmCN2mZjMkry1eQxCFRx+alCjM4bMl9AU6FgMfAXX6Tiy9h1kf0uVdyg/vi w+vJl86eTvvnB0oDji8AUb6Kzr14iczts/bMRSSGQd5NHw7NLdZKMyy6FRxwt8uELVJw RwZw== X-Gm-Message-State: AOJu0Yx2ZQ9z6y/Cjcp+BKtsow6ZsbJQLPODVTqOGlH4RJoS4BHss2Rm DFzgmn8VkPplDqzq1An2Q0EXJglP++Yqf2yYjPV58LXGk8DjXPvMreqGFcSkHA== X-Gm-Gg: AeBDievIuMwkYYm2oLehyGfMySXZQwnnWrzjCiPVtJ5XYM4ciebV1toxoS2CX/wzMbf Mjtlq7Q0ZWI1FikTEnwSxEVDXwy6d7DzS1LEstmxUMDcb89rAV9n5Y7dtAzCDtkgER4W8qOf4MM 3smdKmj8bedXTCdUjxsvW3B4dVWQW02Xu6piUlfOym0lTYCpEytZ4lA2VA+c4pSiWeT87ZFUd6e 4Fcxg9AeXaKYeUjkOgLlkVQQbDbVbUhLao2uuVL5f98lZ9OTXu2LE7kBUVx0dFzYKdsCa1o9Lny +gUJVANqN3fH7y5IQYGtg02T8M89JtY6gCTUjYkT2qay0sUnI7yIPx5qj3r9ujPDkdGXUBd+zQR TMfvQkoHihpBGxwaDqP7I14sBtTlR+28q8e0vlqAZjGsIiNP7axz1D02AQM8TERitBMdVYG69GK tmwJjkFT4x7JOzTS1g5i08xz9XHaxApqrlrEMFcsFytbGIgc3PXIJo8mQeD9Qn6wo8z1VRQacfv NJt X-Received: by 2002:a17:907:1ca8:b0:bc3:783f:1da9 with SMTP id a640c23a62f3a-bc56d13123amr367776966b.33.1778144351614; Thu, 07 May 2026 01:59:11 -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 v5 06/11] xen/riscv: implement copy_to_guest_phys() Date: Thu, 7 May 2026 10:58:45 +0200 Message-ID: <90ff94daad0c3910b1398a7bd956f0d0bd6a2041.1778140240.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c1860d/1778144352-B7D7BDB1-F2C646D4/10/73395122804 X-purgate-type: spam X-purgate-size: 6574 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1778144371420158501 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 Acked-by: Jan Beulich --- Changes in v5: - Nothing changed. Only rebase. --- Changes in v4: - Avoid using of min_t. (sync with Arm) - Add Acked-by: Jan Beulich . --- Changes in v3: - s/if (page =3D=3D NULL)/if ( !page ). - Drop " - offset" for unmap_domain_page() inside copy_guest() function. unmap_domain_page() is expected to mask (or something similar) the page offset bits. - Change some types for functions as copy_to_guest_phys_cb function pointer prototype was changed too in the prev. commit. --- 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 04f02ad89cba..cfc3fdf7d208 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-y +=3D extable.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..8a89212e0bea --- /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 long copy_guest(void *buf, uint64_t addr, unsigned long le= n, + 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 long size =3D min(len, PAGE_SIZE + 0UL - offset); + struct page_info *page; + + page =3D translate_get_page(info, addr, flags & COPY_gva, + flags & COPY_to_guest); + if ( !page ) + 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); + 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 long copy_to_guest_phys(struct domain *d, paddr_t gpa, void *buf, + unsigned long 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 7cd51fbbdead..8d679319ded0 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 long raw_copy_to_guest(void *to, const void *from, unsigned len); unsigned long raw_copy_from_guest(void *to, const void *from, unsigned len= ); unsigned long raw_clear_guest(void *to, unsigned int len); @@ -18,6 +22,9 @@ unsigned long 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 long copy_to_guest_phys(struct domain *d, paddr_t gpa, void *buf, + unsigned long len); + #endif /* ASM__RISCV__GUEST_ACCESS_H */ /* * Local variables: --=20 2.54.0