From nobody Fri Oct 31 04:22: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 1751465676356366.28363610728707; Wed, 2 Jul 2025 07:14:36 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1031203.1404961 (Exim 4.92) (envelope-from ) id 1uWyEC-0007vr-Gy; Wed, 02 Jul 2025 14:14:24 +0000 Received: by outflank-mailman (output) from mailman id 1031203.1404961; Wed, 02 Jul 2025 14:14:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uWyEC-0007vg-AT; Wed, 02 Jul 2025 14:14:24 +0000 Received: by outflank-mailman (input) for mailman id 1031203; Wed, 02 Jul 2025 14:14:23 +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 1uWyEB-0007en-1n for xen-devel@lists.xenproject.org; Wed, 02 Jul 2025 14:14:23 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id d6e9efb0-574e-11f0-b894-0df219b8e170; Wed, 02 Jul 2025 16:14:17 +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 25E2B22C7; Wed, 2 Jul 2025 07:14:02 -0700 (PDT) Received: from PWQ0QT7DJ1.arm.com (unknown [10.57.65.245]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C6A183F6A8; Wed, 2 Jul 2025 07:14:15 -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: d6e9efb0-574e-11f0-b894-0df219b8e170 From: Hari Limaye To: xen-devel@lists.xenproject.org Cc: luca.fancellu@arm.com, Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk Subject: [PATCH v2 1/6] arm/mpu: Find MPU region by range Date: Wed, 2 Jul 2025 15:13:56 +0100 Message-ID: X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1751465677181116600 Content-Type: text/plain; charset="utf-8" From: Luca Fancellu Implement a function to find the index of a MPU region in the xen_mpumap MPU region array. This function will be used in future commits to implement creating and destroying MPU regions. Signed-off-by: Luca Fancellu Signed-off-by: Hari Limaye --- Changes from v1: - Update commit message - Remove internal _index variable - Simplify logic by disallowing NULL index parameter - Use normal printk - Reorder conditional checks - Update some comments --- xen/arch/arm/include/asm/mpu/mm.h | 29 +++++++++++++++++ xen/arch/arm/mpu/mm.c | 52 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/xen/arch/arm/include/asm/mpu/mm.h b/xen/arch/arm/include/asm/m= pu/mm.h index a7f970b465..81e47b9d0b 100644 --- a/xen/arch/arm/include/asm/mpu/mm.h +++ b/xen/arch/arm/include/asm/mpu/mm.h @@ -10,6 +10,13 @@ #include #include =20 +#define MPUMAP_REGION_OVERLAP -1 +#define MPUMAP_REGION_NOTFOUND 0 +#define MPUMAP_REGION_FOUND 1 +#define MPUMAP_REGION_INCLUSIVE 2 + +#define INVALID_REGION_IDX 0xFFU + extern struct page_info *frame_table; =20 extern uint8_t max_mpu_regions; @@ -75,6 +82,28 @@ void write_protection_region(const pr_t *pr_write, uint8= _t sel); */ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags); =20 +/* + * Checks whether a given memory range is present in the provided table of + * MPU protection regions. + * + * @param table Array of pr_t protection regions. + * @param r_regions Number of elements in `table`. + * @param base Start of the memory region to be checked (inclusiv= e). + * @param limit End of the memory region to be checked (exclusive). + * @param index Set to the index of the region if an exact or incl= usive + * match is found, and INVALID_REGION otherwise. + * @return: Return code indicating the result of the search: + * MPUMAP_REGION_NOTFOUND: no part of the range is present in `ta= ble` + * MPUMAP_REGION_FOUND: found an exact match in `table` + * MPUMAP_REGION_INCLUSIVE: found an inclusive match in `table` + * MPUMAP_REGION_OVERLAP: found an overlap with a mapping in `tab= le` + * + * Note: make sure that the range [`base`, `limit`) refers to the memory r= egion + * inclusive of `base` and exclusive of `limit`. + */ +int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, + paddr_t limit, uint8_t *index); + #endif /* __ARM_MPU_MM_H__ */ =20 /* diff --git a/xen/arch/arm/mpu/mm.c b/xen/arch/arm/mpu/mm.c index ccfb37a67b..25e7f36c1e 100644 --- a/xen/arch/arm/mpu/mm.c +++ b/xen/arch/arm/mpu/mm.c @@ -110,6 +110,58 @@ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned = int flags) return region; } =20 +int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, + paddr_t limit, uint8_t *index) +{ + ASSERT(index); + *index =3D INVALID_REGION_IDX; + + /* Convert [base, limit) to [base, limit - 1] for inclusive comparison= */ + limit =3D limit - 1; + + if ( limit < base ) + { + printk("Base address %#"PRIpaddr" must be smaller than limit addre= ss %#"PRIpaddr"\n", + base, limit); + return -EINVAL; + } + + for (uint8_t i =3D 0; i < nr_regions; i++ ) + { + paddr_t iter_base =3D pr_get_base(&table[i]); + paddr_t iter_limit =3D pr_get_limit(&table[i]); + + /* Skip invalid (disabled) regions */ + if ( !region_is_valid(&table[i]) ) + continue; + + /* No match */ + if ( (iter_limit < base) || (iter_base > limit) ) + continue; + + /* Exact match */ + if ( (iter_base =3D=3D base) && (iter_limit =3D=3D limit) ) + { + *index =3D i; + return MPUMAP_REGION_FOUND; + } + + /* Inclusive match */ + if ( (base >=3D iter_base) && (limit <=3D iter_limit) ) + { + *index =3D i; + return MPUMAP_REGION_INCLUSIVE; + } + + /* Overlap */ + printk("Range %#"PRIpaddr" - %#"PRIpaddr" overlaps with the existi= ng region %#"PRIpaddr" - %#"PRIpaddr"\n", + base, limit + 1, iter_base, iter_limit + 1); + return MPUMAP_REGION_OVERLAP; + } + + return MPUMAP_REGION_NOTFOUND; +} + void __init setup_mm(void) { BUG_ON("unimplemented"); --=20 2.34.1