From nobody Fri Oct 31 09:28:38 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; 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 1750413076085443.0694443342147; Fri, 20 Jun 2025 02:51:16 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1020737.1396875 (Exim 4.92) (envelope-from ) id 1uSYOl-0007nU-2x; Fri, 20 Jun 2025 09:51:03 +0000 Received: by outflank-mailman (output) from mailman id 1020737.1396875; Fri, 20 Jun 2025 09:51:03 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uSYOk-0007nM-Vz; Fri, 20 Jun 2025 09:51:02 +0000 Received: by outflank-mailman (input) for mailman id 1020737; Fri, 20 Jun 2025 09:51:01 +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 1uSYOj-0004kQ-55 for xen-devel@lists.xenproject.org; Fri, 20 Jun 2025 09:51:01 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 11716f90-4dbc-11f0-b894-0df219b8e170; Fri, 20 Jun 2025 11:50:59 +0200 (CEST) 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 19E34176A; Fri, 20 Jun 2025 02:50:39 -0700 (PDT) Received: from PWQ0QT7DJ1.arm.com (unknown [10.57.67.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2A5FA3F673; Fri, 20 Jun 2025 02:50:57 -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" X-Inumbo-ID: 11716f90-4dbc-11f0-b894-0df219b8e170 From: Hari Limaye To: xen-devel@lists.xenproject.org Cc: luca.fancellu@arm.com, Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Penny Zheng , Wei Chen Subject: [PATCH 3/6] arm/mpu: Populate a new region in Xen MPU mapping table Date: Fri, 20 Jun 2025 10:49:21 +0100 Message-ID: <840ec219278c2fcb4f8592303eb671b5271b0a95.1750411205.git.hari.limaye@arm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1750413076829116600 Content-Type: text/plain; charset="utf-8" From: Penny Zheng Introduce map_pages_to_xen() that is implemented using a new helper, xen_mpumap_update(), which is responsible for updating Xen MPU memory mapping table(xen_mpumap), including creating a new entry, updating or destroying an existing one, it is equivalent to xen_pt_update in MMU. This commit only implements populating a new entry in Xen MPU memory mapping table(xen_mpumap). Signed-off-by: Penny Zheng Signed-off-by: Wei Chen Signed-off-by: Luca Fancellu Signed-off-by: Hari Limaye --- xen/arch/arm/include/asm/mpu/mm.h | 12 ++++ xen/arch/arm/mpu/mm.c | 96 +++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/xen/arch/arm/include/asm/mpu/mm.h b/xen/arch/arm/include/asm/m= pu/mm.h index a0f0d86d4a..f0f41db210 100644 --- a/xen/arch/arm/include/asm/mpu/mm.h +++ b/xen/arch/arm/include/asm/mpu/mm.h @@ -64,6 +64,7 @@ static inline void context_sync_mpu(void) * The following API requires context_sync_mpu() after being used to modif= y MPU * regions: * - write_protection_region + * - xen_mpumap_update */ =20 /* Reads the MPU region (into @pr_read) with index @sel from the HW */ @@ -72,6 +73,17 @@ void read_protection_region(pr_t *pr_read, uint8_t sel); /* Writes the MPU region (from @pr_write) with index @sel to the HW */ void write_protection_region(const pr_t *pr_write, uint8_t sel); =20 +/* + * Maps an address range into the MPU data structure and updates the HW. + * Equivalent to xen_pt_update in an MMU system. + * + * @param base Base address of the range to map (inclusive). + * @param limit Limit address of the range to map (exclusive). + * @param flags Flags for the memory range to map. + * @return 0 on success, negative on error. + */ +int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags); + /* * Creates a pr_t structure describing a protection region. * diff --git a/xen/arch/arm/mpu/mm.c b/xen/arch/arm/mpu/mm.c index 15197339b1..1de28d2120 100644 --- a/xen/arch/arm/mpu/mm.c +++ b/xen/arch/arm/mpu/mm.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,8 @@ DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR) \ /* EL2 Xen MPU memory region mapping table. */ pr_t __cacheline_aligned __section(".data") xen_mpumap[MAX_MPU_REGION_NR]; =20 +static DEFINE_SPINLOCK(xen_mpumap_lock); + static void __init __maybe_unused build_assertions(void) { /* @@ -176,6 +179,99 @@ int mpumap_contain_region(pr_t *table, uint8_t nr_regi= ons, paddr_t base, return MPUMAP_REGION_NOTFOUND; } =20 +/* + * Allocate a new free EL2 MPU memory region, based on bitmap xen_mpumap_m= ask. + * @param idx Set to the index of the allocated EL2 MPU region on succes= s. + * @return 0 on success, otherwise -ENOENT on failure. + */ +static int xen_mpumap_alloc_entry(uint8_t *idx) +{ + ASSERT(spin_is_locked(&xen_mpumap_lock)); + + *idx =3D find_first_zero_bit(xen_mpumap_mask, max_mpu_regions); + if ( *idx =3D=3D max_mpu_regions ) + { + printk(XENLOG_ERR "mpu: EL2 MPU memory region mapping pool exhaust= ed\n"); + return -ENOENT; + } + + set_bit(*idx, xen_mpumap_mask); + 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. + * + * @param base Base address (inclusive). + * @param limit Limit address (exclusive). + * @param flags Region attributes (a combination of PAGE_HYPERVISOR_XXX) + * @return 0 on success, otherwise negative on error. + */ +static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, + unsigned int flags) +{ + uint8_t idx; + int rc; + + ASSERT(spin_is_locked(&xen_mpumap_lock)); + + rc =3D mpumap_contain_region(xen_mpumap, max_mpu_regions, base, limit,= &idx); + if ( (rc < 0) || (rc > MPUMAP_REGION_NOTFOUND) ) + return -EINVAL; + + /* We are inserting a mapping =3D> Create new region. */ + if ( flags & _PAGE_PRESENT ) + { + rc =3D xen_mpumap_alloc_entry(&idx); + if ( rc ) + return -ENOENT; + + xen_mpumap[idx] =3D pr_of_addr(base, limit, flags); + + write_protection_region(&xen_mpumap[idx], idx); + } + + return 0; +} + +int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags) +{ + int rc; + + if ( flags_has_rwx(flags) ) + { + region_printk("Mappings should not be both Writeable and Executabl= e\n"); + return -EINVAL; + } + + if ( !IS_ALIGNED(base, PAGE_SIZE) || !IS_ALIGNED(limit, PAGE_SIZE) ) + { + region_printk("base address 0x%"PRIpaddr", or limit address 0x%"PR= Ipaddr" is not page aligned\n", + base, limit); + return -EINVAL; + } + + spin_lock(&xen_mpumap_lock); + + rc =3D xen_mpumap_update_entry(base, limit, flags); + + 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) +{ + int rc =3D xen_mpumap_update(mfn_to_maddr(mfn), + mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags); + if ( !rc ) + context_sync_mpu(); + + return rc; +} + void __init setup_mm(void) { BUG_ON("unimplemented"); --=20 2.34.1