From nobody Fri Jan 9 09:17:22 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; 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; 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=fail(p=none dis=none) header.from=arm.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1767612977124754.8901268661738; Mon, 5 Jan 2026 03:36:17 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1195265.1513243 (Exim 4.92) (envelope-from ) id 1vcisN-0004aE-AS; Mon, 05 Jan 2026 11:35:55 +0000 Received: by outflank-mailman (output) from mailman id 1195265.1513243; Mon, 05 Jan 2026 11:35:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vcisN-0004Zy-7X; Mon, 05 Jan 2026 11:35:55 +0000 Received: by outflank-mailman (input) for mailman id 1195265; Mon, 05 Jan 2026 11:35:53 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vcisL-0004YP-Gt for xen-devel@lists.xenproject.org; Mon, 05 Jan 2026 11:35:53 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id afe15c3e-ea2a-11f0-9ccf-f158ae23cfc8; Mon, 05 Jan 2026 12:35:51 +0100 (CET) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9AB03339; Mon, 5 Jan 2026 03:35:43 -0800 (PST) Received: from e134099.cambridge.arm.com (e134099.arm.com [10.1.198.34]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6F6E13F5A1; Mon, 5 Jan 2026 03:35:49 -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: afe15c3e-ea2a-11f0-9ccf-f158ae23cfc8 From: Harry Ramsey To: xen-devel@lists.xenproject.org Cc: Luca.Fancellu@arm.com, Luca Fancellu , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk Subject: [PATCH v2 2/6] arm/mpu: Implement vmap functions for MPU Date: Mon, 5 Jan 2026 11:34:59 +0000 Message-ID: <20260105113503.2674777-3-harry.ramsey@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260105113503.2674777-1-harry.ramsey@arm.com> References: <20260105113503.2674777-1-harry.ramsey@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1767612979776154100 Content-Type: text/plain; charset="utf-8" From: Luca Fancellu HAS_VMAP is not enabled on MPU systems, but the vmap functions are used in places across common code. In order to keep the existing code and maintain correct functionality, implement the `vmap_contig` and `vunmap` functions for MPU systems. Introduce a helper function `destroy_xen_mapping_containing` to aid with unmapping an entire region when only the start address is known. Signed-off-by: Luca Fancellu Signed-off-by: Harry Ramsey --- v2: - Rename `destroy_entire_xen_mapping` to `destroy_xen_mapping_containing` - Improve code documentation. - Refactor nested code. - Fix ignored rc error code in `destroy_xen_mapping_containing`. --- xen/arch/arm/include/asm/mpu/mm.h | 10 +++++ xen/arch/arm/mpu/mm.c | 74 ++++++++++++++++++++++++++----- xen/arch/arm/mpu/vmap.c | 14 ++++-- 3 files changed, 83 insertions(+), 15 deletions(-) diff --git a/xen/arch/arm/include/asm/mpu/mm.h b/xen/arch/arm/include/asm/m= pu/mm.h index e1ded6521d..1b5ffa5b64 100644 --- a/xen/arch/arm/include/asm/mpu/mm.h +++ b/xen/arch/arm/include/asm/mpu/mm.h @@ -111,6 +111,16 @@ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned = int flags); int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, paddr_t limit, uint8_t *index); =20 + +/* + * Destroys and frees (if reference count is 0) an entire xen mapping on M= PU + * systems where only the start address is known. + * + * @param s Start address of memory region to be destroyed. + * @return: 0 on success, negative on error. + */ +int destroy_xen_mapping_containing(paddr_t s); + #endif /* __ARM_MPU_MM_H__ */ =20 /* diff --git a/xen/arch/arm/mpu/mm.c b/xen/arch/arm/mpu/mm.c index 687dec3bc6..207e8d2d91 100644 --- a/xen/arch/arm/mpu/mm.c +++ b/xen/arch/arm/mpu/mm.c @@ -290,6 +290,42 @@ static void disable_mpu_region_from_index(uint8_t inde= x) write_protection_region(&xen_mpumap[index], index); } =20 +/* + * Free a xen_mpumap entry given the index. A mpu region is actually disab= led + * when the refcount is 0 and the region type is MPUMAP_REGION_FOUND. + * + * @param idx Index of the mpumap entry. + * @param region_found_type MPUMAP_REGION_* value. + * @return 0 on success, otherwise negative on error. + */ +static int xen_mpumap_free_entry(uint8_t idx, int region_found_type) +{ + ASSERT(spin_is_locked(&xen_mpumap_lock)); + ASSERT(idx !=3D INVALID_REGION_IDX); + + if ( MPUMAP_REGION_OVERLAP =3D=3D region_found_type ) + { + printk(XENLOG_ERR "Cannot remove an overlapping region\n"); + return -EINVAL; + } + + if ( xen_mpumap[idx].refcount ) + { + xen_mpumap[idx].refcount -=3D 1; + return 0; + } + + if ( MPUMAP_REGION_FOUND =3D=3D region_found_type ) + disable_mpu_region_from_index(idx); + else + { + printk(XENLOG_ERR "Cannot remove a partial region\n"); + return -EINVAL; + } + + return 0; +} + /* * Update the entry in the MPU memory region mapping table (xen_mpumap) fo= r the * given memory range and flags, creating one if none exists. @@ -357,18 +393,7 @@ static int xen_mpumap_update_entry(paddr_t base, paddr= _t limit, return -EINVAL; } =20 - if ( xen_mpumap[idx].refcount =3D=3D 0 ) - { - if ( MPUMAP_REGION_FOUND =3D=3D rc ) - disable_mpu_region_from_index(idx); - else - { - printk("Cannot remove a partial region\n"); - return -EINVAL; - } - } - else - xen_mpumap[idx].refcount -=3D 1; + return xen_mpumap_free_entry(idx, rc); } =20 return 0; @@ -418,6 +443,31 @@ int destroy_xen_mappings(unsigned long s, unsigned lon= g e) return xen_mpumap_update(s, e, 0); } =20 +int destroy_xen_mapping_containing(paddr_t s) +{ + int rc; + uint8_t idx; + + ASSERT(IS_ALIGNED(s, PAGE_SIZE)); + + spin_lock(&xen_mpumap_lock); + + rc =3D mpumap_contains_region(xen_mpumap, max_mpu_regions, s, s + PAGE= _SIZE, + &idx); + if ( rc =3D=3D MPUMAP_REGION_NOTFOUND ) + { + printk(XENLOG_ERR "Cannot remove entry that does not exist"); + return -EINVAL; + } + + /* As we are unmapping entire region use MPUMAP_REGION_FOUND instead */ + rc =3D xen_mpumap_free_entry(idx, MPUMAP_REGION_FOUND); + + spin_unlock(&xen_mpumap_lock); + + return rc; +} + int map_pages_to_xen(unsigned long virt, mfn_t mfn, unsigned long nr_mfns, unsigned int flags) { diff --git a/xen/arch/arm/mpu/vmap.c b/xen/arch/arm/mpu/vmap.c index f977b79cd4..54d949e7ce 100644 --- a/xen/arch/arm/mpu/vmap.c +++ b/xen/arch/arm/mpu/vmap.c @@ -1,19 +1,27 @@ /* SPDX-License-Identifier: GPL-2.0-only */ =20 #include +#include #include #include #include =20 void *vmap_contig(mfn_t mfn, unsigned int nr) { - BUG_ON("unimplemented"); - return NULL; + paddr_t base =3D mfn_to_maddr(mfn); + + if ( map_pages_to_xen(base, mfn, nr, PAGE_HYPERVISOR ) ) + return NULL; + + return maddr_to_virt(base); } =20 void vunmap(const void *va) { - BUG_ON("unimplemented"); + paddr_t base =3D virt_to_maddr(va); + + if ( destroy_xen_mapping_containing(base) ) + panic("Failed to vunmap region\n"); } =20 /* --=20 2.43.0