From nobody Fri Oct 31 04:14:26 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 1751465682591968.4831716219096; Wed, 2 Jul 2025 07:14:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1031206.1404981 (Exim 4.92) (envelope-from ) id 1uWyEH-0008WL-34; Wed, 02 Jul 2025 14:14:29 +0000 Received: by outflank-mailman (output) from mailman id 1031206.1404981; Wed, 02 Jul 2025 14:14:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uWyEH-0008WA-0B; Wed, 02 Jul 2025 14:14:29 +0000 Received: by outflank-mailman (input) for mailman id 1031206; Wed, 02 Jul 2025 14:14:26 +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 1uWyEE-0007Ob-S6 for xen-devel@lists.xenproject.org; Wed, 02 Jul 2025 14:14:26 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id dbed749b-574e-11f0-a313-13f23c93f187; Wed, 02 Jul 2025 16:14:26 +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 9166B22C7; Wed, 2 Jul 2025 07:14:10 -0700 (PDT) Received: from PWQ0QT7DJ1.arm.com (unknown [10.57.65.245]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D6E233F6A8; Wed, 2 Jul 2025 07:14:23 -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: dbed749b-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 4/6] arm/mpu: Destroy an existing entry in Xen MPU memory mapping table Date: Wed, 2 Jul 2025 15:13:59 +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: 1751465683315116600 Content-Type: text/plain; charset="utf-8" From: Penny Zheng This commit expands xen_mpumap_update/xen_mpumap_update_entry to include destroying an existing entry. We define a new helper "disable_mpu_region_from_index" to disable the MPU region based on index. If region is within [0, 31], we could quickly disable the MPU region through PRENR_EL2 which provides direct access to the PRLAR_EL2.EN bits of EL2 MPU regions. Rignt now, we only support destroying a *WHOLE* MPU memory region, part-region removing is not supported, as in worst case, it will leave two fragments behind. Signed-off-by: Penny Zheng Signed-off-by: Wei Chen Signed-off-by: Luca Fancellu Signed-off-by: Hari Limaye --- Changes from v1: - Move check for part-region removal outside if condition - Use normal printk --- xen/arch/arm/include/asm/mpu.h | 2 + xen/arch/arm/include/asm/mpu/cpregs.h | 4 ++ xen/arch/arm/mpu/mm.c | 69 ++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/xen/arch/arm/include/asm/mpu.h b/xen/arch/arm/include/asm/mpu.h index 63560c613b..5053edaf63 100644 --- a/xen/arch/arm/include/asm/mpu.h +++ b/xen/arch/arm/include/asm/mpu.h @@ -23,6 +23,8 @@ #define NUM_MPU_REGIONS_MASK (NUM_MPU_REGIONS - 1) #define MAX_MPU_REGION_NR NUM_MPU_REGIONS_MASK =20 +#define PRENR_MASK GENMASK(31, 0) + #ifndef __ASSEMBLY__ =20 /* diff --git a/xen/arch/arm/include/asm/mpu/cpregs.h b/xen/arch/arm/include/a= sm/mpu/cpregs.h index bb15e02df6..9f3b32acd7 100644 --- a/xen/arch/arm/include/asm/mpu/cpregs.h +++ b/xen/arch/arm/include/asm/mpu/cpregs.h @@ -6,6 +6,9 @@ /* CP15 CR0: MPU Type Register */ #define HMPUIR p15,4,c0,c0,4 =20 +/* CP15 CR6: Protection Region Enable Register */ +#define HPRENR p15,4,c6,c1,1 + /* CP15 CR6: MPU Protection Region Base/Limit/Select Address Register */ #define HPRSELR p15,4,c6,c2,1 #define HPRBAR p15,4,c6,c3,0 @@ -82,6 +85,7 @@ /* Alphabetically... */ #define MPUIR_EL2 HMPUIR #define PRBAR_EL2 HPRBAR +#define PRENR_EL2 HPRENR #define PRLAR_EL2 HPRLAR #define PRSELR_EL2 HPRSELR #endif /* CONFIG_ARM_32 */ diff --git a/xen/arch/arm/mpu/mm.c b/xen/arch/arm/mpu/mm.c index dd54b66901..2e88c467d5 100644 --- a/xen/arch/arm/mpu/mm.c +++ b/xen/arch/arm/mpu/mm.c @@ -185,6 +185,42 @@ static int xen_mpumap_alloc_entry(uint8_t *idx) return 0; } =20 +/* + * Disable and remove an MPU region from the data structure and MPU regist= ers. + * + * @param index Index of the MPU region to be disabled. + */ +static void disable_mpu_region_from_index(uint8_t index) +{ + ASSERT(spin_is_locked(&xen_mpumap_lock)); + ASSERT(index !=3D INVALID_REGION_IDX); + + if ( !region_is_valid(&xen_mpumap[index]) ) + { + printk(XENLOG_WARNING + "mpu: MPU memory region[%u] is already disabled\n", index); + return; + } + + /* Zeroing the region will also zero the region enable */ + memset(&xen_mpumap[index], 0, sizeof(pr_t)); + clear_bit(index, xen_mpumap_mask); + + /* + * Both Armv8-R AArch64 and AArch32 have direct access to the enable b= it for + * MPU regions numbered from 0 to 31. + */ + if ( (index & PRENR_MASK) !=3D 0 ) + { + /* Clear respective bit */ + uint64_t val =3D READ_SYSREG(PRENR_EL2) & (~(1UL << index)); + + WRITE_SYSREG(val, PRENR_EL2); + } + else + write_protection_region(&xen_mpumap[index], index); +} + /* * 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. @@ -203,11 +239,11 @@ static int xen_mpumap_update_entry(paddr_t base, padd= r_t limit, ASSERT(spin_is_locked(&xen_mpumap_lock)); =20 rc =3D mpumap_contains_region(xen_mpumap, max_mpu_regions, base, limit= , &idx); - if ( !(rc =3D=3D MPUMAP_REGION_NOTFOUND) ) + if ( rc < 0 ) return -EINVAL; =20 /* We are inserting a mapping =3D> Create new region. */ - if ( flags & _PAGE_PRESENT ) + if ( (flags & _PAGE_PRESENT) && (MPUMAP_REGION_NOTFOUND =3D=3D rc) ) { rc =3D xen_mpumap_alloc_entry(&idx); if ( rc ) @@ -218,6 +254,20 @@ static int xen_mpumap_update_entry(paddr_t base, paddr= _t limit, write_protection_region(&xen_mpumap[idx], idx); } =20 + /* + * Currently, we only support destroying a *WHOLE* MPU memory region. + * Part-region removal is not supported as in the worst case it will l= eave + * two fragments behind. + */ + if ( rc =3D=3D MPUMAP_REGION_INCLUSIVE ) + { + printk("mpu: part-region removal is not supported\n"); + return -EINVAL; + } + + if ( !(flags & _PAGE_PRESENT) && (rc >=3D MPUMAP_REGION_FOUND) ) + disable_mpu_region_from_index(idx); + return 0; } =20 @@ -251,6 +301,21 @@ int xen_mpumap_update(paddr_t base, paddr_t limit, uns= igned int flags) return rc; } =20 +int destroy_xen_mappings(unsigned long s, unsigned long e) +{ + int rc; + + ASSERT(IS_ALIGNED(s, PAGE_SIZE)); + ASSERT(IS_ALIGNED(e, PAGE_SIZE)); + ASSERT(s <=3D e); + + rc =3D xen_mpumap_update(virt_to_maddr(s), virt_to_maddr(e), 0); + if ( !rc ) + context_sync_mpu(); + + return rc; +} + int map_pages_to_xen(unsigned long virt, mfn_t mfn, unsigned long nr_mfns, unsigned int flags) { --=20 2.34.1