[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 3/6] arm/mpu: Implement free_init_memory for MPU systems


  • To: Harry Ramsey <harry.ramsey@xxxxxxx>, <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: "Orzel, Michal" <michal.orzel@xxxxxxx>
  • Date: Tue, 13 Jan 2026 11:22:22 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=arm.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cw/MD7tweZ5roVeaoCfXZCAAEFyqM4QLZKYRT14mSzI=; b=UHt64SSXWg8NUMOIMem3dGGm6pza3dQCP2pMEqQ7UM8N5WugJDGSZ//AwGvf4Hl/KTJhjs60uLXGf7y50DtDbsi/IfHVqOUbasfxh87TT/LI2GuK2SaO+BZVRVXq9f4FfRK9kvBPCttOz0rE6hSyUuXzf9+ly1gAuy8hlXUWvjiwo8mQVB753XmZHezWbNHBilOCSkR9/sa37VNGJhqivFpKZAMXfC5eg1H2QOljEgAw7LbqecvjuEAlTLyNdQ7f0ygR9/UNJGiT0QG9QQgRsENX25NGIETzslHIQFjMk17NskFYObEgjS1Sl6JXje1Mj6dbGeD8Vsb9C9AnSh7I4g==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NUluQbvx4VOPpnm3k7ZdEXOq0lCeHcPk0ndsSV2m4gxR2M4KJBXH0Y5q0HjcLJshpv8FV+ZR2kSZXea4Vri7lQMOzmnWjjK1/8Xv0RbFM7wfCInNWpVrxjbP7y4S3iCRQPeO89Ny2aZG8RqnxWnwz5B5Wbjdv1q4WDVoUBYwWNBd0yMWGUsjcX+BJ69OeefZArEWDGOXZAUmRpPLxOqmaDZzAmkiFnWXi/cl/2ciBanIJaaotAp9GFVTvOjj5+8KMfz5HaJMU8JqAcnc75//vuYyu2FN+eHv2yfLBvhafEouB8l5GiAZ6jUVhYYXPkRICsv4fUfvtmM/E2gWkvu8NQ==
  • Cc: <Luca.Fancellu@xxxxxxx>, Penny Zheng <Penny.Zheng@xxxxxxx>, "Stefano Stabellini" <sstabellini@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, "Bertrand Marquis" <bertrand.marquis@xxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, Wei Chen <wei.chen@xxxxxxx>, Hari Limaye <hari.limaye@xxxxxxx>
  • Delivery-date: Tue, 13 Jan 2026 10:22:34 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>


On 05/01/2026 12:35, Harry Ramsey wrote:
> From: Penny Zheng <Penny.Zheng@xxxxxxx>
> 
> Implement the function `free_init_memory` for MPU systems. In order to
> support this, the function `modify_xen_mappings` is implemented.
> 
> On MPU systems, we map the init text and init data sections using
> separate MPU memory regions. Therefore these are removed separately in
> `free_init_memory`.
> 
> Additionally remove warning messages from `is_mm_attr_match` as some
> permissions can now be updated by `xen_mpumap_update_entry`.
> 
> Signed-off-by: Penny Zheng <penny.zheng@xxxxxxx>
> Signed-off-by: Wei Chen <wei.chen@xxxxxxx>
> Signed-off-by: Luca Fancellu <luca.fancellu@xxxxxxx>
> Signed-off-by: Hari Limaye <hari.limaye@xxxxxxx>
> Signed-off-by: Harry Ramsey <harry.ramsey@xxxxxxx>
> ---
> v2:
> - Refactor `is_mm_attr_match` to return logical values regarding the
>   permission mismatch.
> - Improve code documentation.
> ---
>  xen/arch/arm/include/asm/mpu/mm.h |   6 +-
>  xen/arch/arm/include/asm/setup.h  |   2 +
>  xen/arch/arm/mpu/mm.c             | 113 +++++++++++++++++++++++-------
>  3 files changed, 95 insertions(+), 26 deletions(-)
> 
> diff --git a/xen/arch/arm/include/asm/mpu/mm.h 
> b/xen/arch/arm/include/asm/mpu/mm.h
> index 1b5ffa5b64..f0941de295 100644
> --- a/xen/arch/arm/include/asm/mpu/mm.h
> +++ b/xen/arch/arm/include/asm/mpu/mm.h
> @@ -15,7 +15,11 @@
>  #define MPUMAP_REGION_FOUND         1
>  #define MPUMAP_REGION_INCLUSIVE     2
>  
> -#define INVALID_REGION_IDX     0xFFU
> +#define MPU_ATTR_RO_MISMATCH     -1
> +#define MPU_ATTR_XN_MISMATCH     -2
> +#define MPU_ATTR_AI_MISMATCH     -3
You don't seem to use these outside of mm.c, so I would suggest to move them 
there.

> +
> +#define INVALID_REGION_IDX    0xFFU
>  
>  extern struct page_info *frame_table;
>  
> diff --git a/xen/arch/arm/include/asm/setup.h 
> b/xen/arch/arm/include/asm/setup.h
> index 1eaf13bd66..005cf7be59 100644
> --- a/xen/arch/arm/include/asm/setup.h
> +++ b/xen/arch/arm/include/asm/setup.h
> @@ -65,6 +65,8 @@ int map_irq_to_domain(struct domain *d, unsigned int irq,
>  int map_range_to_domain(const struct dt_device_node *dev,
>                          uint64_t addr, uint64_t len, void *data);
>  
> +extern const char __init_data_begin[], __bss_start[], __bss_end[];
> +
>  struct init_info
>  {
>      /* Pointer to the stack, used by head.S when entering in C */
> diff --git a/xen/arch/arm/mpu/mm.c b/xen/arch/arm/mpu/mm.c
> index 207e8d2d91..4194d4fefd 100644
> --- a/xen/arch/arm/mpu/mm.c
> +++ b/xen/arch/arm/mpu/mm.c
> @@ -171,33 +171,18 @@ int mpumap_contains_region(pr_t *table, uint8_t 
> nr_regions, paddr_t base,
>      return MPUMAP_REGION_NOTFOUND;
>  }
>  
> -static bool is_mm_attr_match(pr_t *region, unsigned int attributes)
> +static int is_mm_attr_match(pr_t *region, unsigned int attributes)
>  {
>      if ( region->prbar.reg.ro != PAGE_RO_MASK(attributes) )
> -    {
> -        printk(XENLOG_WARNING
> -               "Mismatched Access Permission attributes (%#x instead of 
> %#x)\n",
> -               region->prbar.reg.ro, PAGE_RO_MASK(attributes));
> -        return false;
> -    }
> +        return MPU_ATTR_RO_MISMATCH;
>  
>      if ( region->prbar.reg.xn != PAGE_XN_MASK(attributes) )
> -    {
> -        printk(XENLOG_WARNING
> -               "Mismatched Execute Never attributes (%#x instead of %#x)\n",
> -               region->prbar.reg.xn, PAGE_XN_MASK(attributes));
> -        return false;
> -    }
> +        return MPU_ATTR_XN_MISMATCH;
Later below you don't seem to differentiate between MPU_ATTR_RO_MISMATCH and
MPU_ATTR_XN_MISMATCH. Therefore I would suggest to keep them as one to simplify
the code.

>  
>      if ( region->prlar.reg.ai != PAGE_AI_MASK(attributes) )
> -    {
> -        printk(XENLOG_WARNING
> -               "Mismatched Memory Attribute Index (%#x instead of %#x)\n",
> -               region->prlar.reg.ai, PAGE_AI_MASK(attributes));
> -        return false;
> -    }
> +        return MPU_ATTR_AI_MISMATCH;
>  
> -    return true;
> +    return 0;
>  }
>  
>  /* Map a frame table to cover physical addresses ps through pe */
> @@ -357,12 +342,45 @@ static int xen_mpumap_update_entry(paddr_t base, 
> paddr_t limit,
>      */
>      if ( flags_has_page_present && (rc >= MPUMAP_REGION_FOUND) )
>      {
> -        if ( !is_mm_attr_match(&xen_mpumap[idx], flags) )
> +        int attr_match = is_mm_attr_match(&xen_mpumap[idx], flags);
> +
> +        /* We do not support modifying AI attribute. */
> +        if ( MPU_ATTR_AI_MISMATCH == attr_match )
>          {
> -            printk("Modifying an existing entry is not supported\n");
> +            printk(XENLOG_ERR
> +                   "Modifying memory attribute is not supported\n");
>              return -EINVAL;
>          }
>  
> +        /*
> +         * Permissions RO and XN can be changed only by the full region.
> +         * Permissions that match can continue and just increment refcount.
> +         */
> +        if ( MPU_ATTR_RO_MISMATCH == attr_match ||
Enlcose in brackets () || ()

> +             MPU_ATTR_XN_MISMATCH == attr_match )
> +        {
> +            if ( rc == MPUMAP_REGION_INCLUSIVE )
> +            {
> +                printk(XENLOG_ERR
> +                       "Cannot modify partial region permissions\n");
> +                return -EINVAL;
> +            }
> +
> +            if ( xen_mpumap[idx].refcount != 0 )
> +            {
> +                printk(XENLOG_ERR
> +                       "Cannot modify memory permissions for a region mapped 
> multiple times\n");
Memory permission? Here you are checking for XN/RO.

> +                return -EINVAL;
> +            }
> +
> +            /* Set new permission */
> +            xen_mpumap[idx].prbar.reg.ro = PAGE_RO_MASK(flags);
> +            xen_mpumap[idx].prbar.reg.xn = PAGE_XN_MASK(flags);
Here you always change both, that's why there is no need to provide two separate
macros as I mentioned above.

~Michal




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.