From nobody Fri Oct 31 04:31:52 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 1751465679214542.8969477150239; Wed, 2 Jul 2025 07:14:39 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1031204.1404971 (Exim 4.92) (envelope-from ) id 1uWyED-0008By-MI; Wed, 02 Jul 2025 14:14:25 +0000 Received: by outflank-mailman (output) from mailman id 1031204.1404971; Wed, 02 Jul 2025 14:14:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uWyED-0008Bl-I0; Wed, 02 Jul 2025 14:14:25 +0000 Received: by outflank-mailman (input) for mailman id 1031204; Wed, 02 Jul 2025 14:14:24 +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 1uWyEC-0007Ob-BA for xen-devel@lists.xenproject.org; Wed, 02 Jul 2025 14:14:24 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id da64d48e-574e-11f0-a313-13f23c93f187; Wed, 02 Jul 2025 16:14:23 +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 E541122C7; Wed, 2 Jul 2025 07:14:07 -0700 (PDT) Received: from PWQ0QT7DJ1.arm.com (unknown [10.57.65.245]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 36FF73F6A8; Wed, 2 Jul 2025 07:14:21 -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: da64d48e-574e-11f0-a313-13f23c93f187 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 v2 3/6] arm/mpu: Populate a new region in Xen MPU mapping table Date: Wed, 2 Jul 2025 15:13:58 +0100 Message-ID: <5b67459af62106197fb551d35ef1708f0bb5beb6.1751464757.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: 1751465681223116600 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 --- Changes from v1: - Simplify if condition - Use normal printk - Use %# over 0x% - Add same asserts as in Patch 4 --- xen/arch/arm/include/asm/mpu/mm.h | 12 ++++ xen/arch/arm/mpu/mm.c | 100 ++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/xen/arch/arm/include/asm/mpu/mm.h b/xen/arch/arm/include/asm/m= pu/mm.h index 81e47b9d0b..101002f7d4 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 25e7f36c1e..dd54b66901 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 @@ -29,6 +30,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) { /* @@ -162,6 +165,103 @@ int mpumap_contains_region(pr_t *table, uint8_t nr_re= gions, 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_contains_region(xen_mpumap, max_mpu_regions, base, limit= , &idx); + if ( !(rc =3D=3D 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; + + ASSERT(IS_ALIGNED(base, PAGE_SIZE)); + ASSERT(IS_ALIGNED(limit, PAGE_SIZE)); + ASSERT(base <=3D limit); + + if ( flags_has_rwx(flags) ) + { + printk("Mappings should not be both Writeable and Executable\n"); + return -EINVAL; + } + + if ( !IS_ALIGNED(base, PAGE_SIZE) || !IS_ALIGNED(limit, PAGE_SIZE) ) + { + printk("base address %#"PRIpaddr", or limit address %#"PRIpaddr" i= s 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