Skip to contentSkip to conversation

Third MPU series

PATCH Superseded Harry Ramsey 3 participants 8 messages Oct 24, 2025 – Nov 4, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Third MPU series

Hello,

This series aims to further the ongoing work to introduce support for MPU systems in xen.

The patches in this series implement the logic to map MPU regions.

Thanks,
Harry Ramsey

Harry Ramsey (2):
arm/mpu: Implement setup_mm for MPU systems arm/mpu: Implement reference counting for overlapping regions

Luca Fancellu (1):
arm/mpu: Implement ioremap_attr for MPU

 xen/arch/arm/arm32/asm-offsets.c         |   2 +
 xen/arch/arm/arm64/asm-offsets.c         |   2 +
 xen/arch/arm/arm64/mmu/mm.c              |  25 +----
 xen/arch/arm/include/asm/arm32/mpu.h     |   2 +
 xen/arch/arm/include/asm/arm64/mpu.h     |   2 +
 xen/arch/arm/include/asm/mm.h            |   2 +
 xen/arch/arm/include/asm/mpu/regions.inc |  11 ++-
 xen/arch/arm/mm.c                        |  45 +++++++++
 xen/arch/arm/mpu/mm.c                    | 121 +++++++++++++++++++----
 9 files changed, 166 insertions(+), 46 deletions(-)

--
2.43.0

#1 lore raw

PATCH 1/3 arm: Implement setup_mm for MPU systems

From: Harry Ramsey <your [email protected]>

Implement setup_mm for MPU systems. This variant does not require setting up a direct map.

To reduce code duplication the common initalisation code for both MPU and MMU Arm64 configurations is refactored into setup_mm. Platform-specific setup steps are now handled by a new helper function setup_mm_helper.

---
 xen/arch/arm/arm64/mmu/mm.c   | 26 +-------------------
 xen/arch/arm/include/asm/mm.h |  2 ++
 xen/arch/arm/mm.c             | 45 +++++++++++++++++++++++++++++++++++
 xen/arch/arm/mpu/mm.c         | 30 +++++++++++++++++++++--
 4 files changed, 76 insertions(+), 27 deletions(-)
xen/arch/arm/arm64/mmu/mm.c +1 -25
index 3e64be6ae6..70b53be032 100644
@@ -4,8 +4,6 @@
 #include <xen/llc-coloring.h>
 #include <xen/mm.h>
 #include <xen/pfn.h>
-#include <xen/static-memory.h>
-#include <xen/static-shmem.h>
 
 #include <asm/setup.h>
 
@@ -240,33 +238,18 @@ static void __init setup_directmap_mappings(unsigned long base_mfn,
         panic("Unable to setup the directmap mappings.\n");
 }
 
-void __init setup_mm(void)
+void __init setup_mm_helper(void)
 {
     const struct membanks *banks = bootinfo_get_mem();
     paddr_t ram_start = INVALID_PADDR;
     paddr_t ram_end = 0;
-    paddr_t ram_size = 0;
     unsigned int i;
 
-    init_pdx();
-
-    /*
-     * We need some memory to allocate the page-tables used for the directmap
-     * mappings. But some regions may contain memory already allocated
-     * for other uses (e.g. modules, reserved-memory...).
-     *
-     * For simplicity, add all the free regions in the boot allocator.
-     */
-    populate_boot_allocator();
-
-    total_pages = 0;
-
     for ( i = 0; i < banks->nr_banks; i++ )
     {
         const struct membank *bank = &banks->bank[i];
         paddr_t bank_end = bank->start + bank->size;
 
-        ram_size = ram_size + bank->size;
         ram_start = min(ram_start, bank->start);
         ram_end = max(ram_end, bank_end);
 
@@ -274,16 +257,9 @@ void __init setup_mm(void)
                                  PFN_DOWN(bank->size));
     }
 
-    total_pages += ram_size >> PAGE_SHIFT;
-
     directmap_virt_end = XENHEAP_VIRT_START + ram_end - ram_start;
     directmap_mfn_start = maddr_to_mfn(ram_start);
     directmap_mfn_end = maddr_to_mfn(ram_end);
-
-    setup_frametable_mappings(ram_start, ram_end);
-
-    init_staticmem_pages();
-    init_sharedmem_pages();
 }
 
 /*
xen/arch/arm/include/asm/mm.h +2 -0
index 7a93dad2ed..1f5b41e602 100644
@@ -202,6 +202,8 @@ extern void remove_early_mappings(void);
 extern int prepare_secondary_mm(int cpu);
 /* Map a frame table to cover physical addresses ps through pe */
 extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
+/* Helper function to setup memory management */
+extern void setup_mm_helper(void);
 /* map a physical range in virtual memory */
 void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int attributes);
 
xen/arch/arm/mm.c +45 -0

59 lines not on this page · raw message

xen/arch/arm/mpu/mm.c +28 -2
index 3f155b7db2..a058db19ef 100644
@@ -8,9 +8,11 @@
 #include <xen/sizes.h>
 #include <xen/spinlock.h>
 #include <xen/types.h>
+#include <xen/pfn.h>
 #include <asm/mpu.h>
 #include <asm/mpu/mm.h>
 #include <asm/page.h>
+#include <asm/setup.h>
 #include <asm/sysregs.h>
 
 struct page_info *frame_table;
@@ -378,9 +380,33 @@ int map_pages_to_xen(unsigned long virt, mfn_t mfn, unsigned long nr_mfns,
     return xen_mpumap_update(virt, mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
 }
 
-void __init setup_mm(void)
+/*
+ * Heap must be statically configured in Device Tree through "xen,static-heap"
+ * on MPU systems, use setup_mm_helper() for that.
+ */
+void __init setup_mm_helper(void)
 {
-    BUG_ON("unimplemented");
+    const struct membanks *reserved_mem = bootinfo_get_reserved_mem();
+    unsigned int bank = 0;
+
+    for ( ; bank < reserved_mem->nr_banks; bank++ )
+    {
+        if ( reserved_mem->bank[bank].type == MEMBANK_STATIC_HEAP )
+        {
+            paddr_t bank_start = round_pgup(reserved_mem->bank[bank].start);
+            paddr_t bank_size = round_pgdown(reserved_mem->bank[bank].size);
+            paddr_t bank_end = bank_start + bank_size;
+
+            /* Map static heap with one MPU protection region */
+            if ( xen_mpumap_update(bank_start, bank_end, PAGE_HYPERVISOR) )
+                panic("Failed to map static heap\n");
+
+            break;
+        }
+    }
+
+    if ( bank == reserved_mem->nr_banks )
+        panic("No static heap memory bank found\n");
 }
 
 int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] arm: Implement setup_mm for MPU systems

Hi Harry,

Quoted 3 lines · Harry Ramsey

From: Harry Ramsey <your [email protected]>

we will fix the authorship in v2, for now let’s leave this serie to collect the comments from the mailing list.

Cheers,
Luca

#3 lore raw

Re: [PATCH 1/3] arm: Implement setup_mm for MPU systems

In reply to Harry Ramsey
Quoted 87 lines · Harry Ramsey

From: Harry Ramsey <your [email protected]>

Implement setup_mm for MPU systems. This variant does not require setting up a direct map.

To reduce code duplication the common initalisation code for both MPU and MMU Arm64 configurations is refactored into setup_mm. Platform-specific setup steps are now handled by a new helper function setup_mm_helper.

---
 xen/arch/arm/arm64/mmu/mm.c   | 26 +-------------------
 xen/arch/arm/include/asm/mm.h |  2 ++
 xen/arch/arm/mm.c             | 45 +++++++++++++++++++++++++++++++++++
 xen/arch/arm/mpu/mm.c         | 30 +++++++++++++++++++++--
 4 files changed, 76 insertions(+), 27 deletions(-)
xen/arch/arm/arm64/mmu/mm.c +1 -25
index 3e64be6ae6..70b53be032 100644
@@ -4,8 +4,6 @@
 #include <xen/llc-coloring.h>
 #include <xen/mm.h>
 #include <xen/pfn.h>
-#include <xen/static-memory.h>
-#include <xen/static-shmem.h>
 
 #include <asm/setup.h>
 
@@ -240,33 +238,18 @@ static void __init setup_directmap_mappings(unsigned long base_mfn,
         panic("Unable to setup the directmap mappings.\n");
 }
 
-void __init setup_mm(void)
+void __init setup_mm_helper(void)
 {
     const struct membanks *banks = bootinfo_get_mem();
     paddr_t ram_start = INVALID_PADDR;
     paddr_t ram_end = 0;
-    paddr_t ram_size = 0;
     unsigned int i;
 
-    init_pdx();
-
-    /*
-     * We need some memory to allocate the page-tables used for the directmap
-     * mappings. But some regions may contain memory already allocated
-     * for other uses (e.g. modules, reserved-memory...).
-     *
-     * For simplicity, add all the free regions in the boot allocator.
-     */
-    populate_boot_allocator();
-
-    total_pages = 0;
-
     for ( i = 0; i < banks->nr_banks; i++ )
     {
         const struct membank *bank = &banks->bank[i];
         paddr_t bank_end = bank->start + bank->size;
 
-        ram_size = ram_size + bank->size;
         ram_start = min(ram_start, bank->start);
         ram_end = max(ram_end, bank_end);
 
@@ -274,16 +257,9 @@ void __init setup_mm(void)
                                  PFN_DOWN(bank->size));
     }
 
-    total_pages += ram_size >> PAGE_SHIFT;
-
     directmap_virt_end = XENHEAP_VIRT_START + ram_end - ram_start;
     directmap_mfn_start = maddr_to_mfn(ram_start);
     directmap_mfn_end = maddr_to_mfn(ram_end);
-
-    setup_frametable_mappings(ram_start, ram_end);
-
-    init_staticmem_pages();
-    init_sharedmem_pages();
 }
 
 /*
xen/arch/arm/include/asm/mm.h +0 -0
index 7a93dad2ed..1f5b41e602 100644

xen/arch/arm/include/asm/mm.h extern void remove_early_mappings(void); · line 206

Quoted 5 lines
xen/arch/arm/include/asm/mm.h +2 -0
@@ -202,6 +202,8 @@ extern void remove_early_mappings(void);
 extern int prepare_secondary_mm(int cpu);
 /* Map a frame table to cover physical addresses ps through pe */
 extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
+/* Helper function to setup memory management */
+extern void setup_mm_helper(void);

No extern for prototypes, please.

Quoted 21 lines
xen/arch/arm/include/asm/mm.h +0 -0
 /* map a physical range in virtual memory */
 void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int attributes);
 
xen/arch/arm/mm.c +4 -0
index 3b05b46ee0..f26c28aaf5 100644
@@ -12,8 +12,12 @@
 #include <xen/grant_table.h>
 #include <xen/guest_access.h>
 #include <xen/mm.h>
+#include <xen/static-memory.h>
+#include <xen/static-shmem.h>
 #include <xen/vmap.h>
 
+#include <asm/setup.h>
+
 #include <xsm/xsm.h>
 
 #include <public/memory.h>

xen/arch/arm/mm.c line 31

Quoted 4 lines
xen/arch/arm/mm.c +1 -0
@@ -24,6 +28,47 @@
 
 unsigned long frametable_base_pdx __read_mostly;
 
+#if !defined(CONFIG_ARM_32) || defined(CONFIG_MPU)

NIT: Why negation instead of CONFIG_ARM_64?

xen/arch/arm/mm.c line 43

Quoted 12 lines
xen/arch/arm/mm.c +12 -0
+void __init setup_mm(void)
+{
+    const struct membanks *banks = bootinfo_get_mem();
+    paddr_t ram_start = INVALID_PADDR;
+    paddr_t ram_end = 0;
+    paddr_t ram_size = 0;
+    unsigned int i;
+
+    init_pdx();
+
+    /*
+     * We need some memory to allocate the page-tables used for the directmap

Isn't this comment here now a bit misleading in MPU case?

xen/arch/arm/mm.c line 49

Quoted 6 lines
xen/arch/arm/mm.c +6 -0
+     * mappings. But some regions may contain memory already allocated
+     * for other uses (e.g. modules, reserved-memory...).
+     *
+     * For simplicity, add all the free regions in the boot allocator.
+     */
+    populate_boot_allocator();

NIT: I would suggest to move this right before setup_mm_helper() with a comment tweaked.

Quoted 30 lines
xen/arch/arm/mm.c +22 -0
+
+    for ( i = 0; i < banks->nr_banks; i++ )
+    {
+        const struct membank *bank = &banks->bank[i];
+        paddr_t bank_end = bank->start + bank->size;
+
+        ram_size = ram_size + bank->size;
+        ram_start = min(ram_start, bank->start);
+        ram_end = max(ram_end, bank_end);
+    }
+
+    total_pages = ram_size >> PAGE_SHIFT;
+
+    setup_mm_helper();
+
+    setup_frametable_mappings(ram_start, ram_end);
+
+    init_staticmem_pages();
+    init_sharedmem_pages();
+}
+#endif
+
 bool flags_has_rwx(unsigned int flags)
 {
     /*
xen/arch/arm/mpu/mm.c +0 -0
index 3f155b7db2..a058db19ef 100644

xen/arch/arm/mpu/mm.c line 11

Quoted 4 lines
xen/arch/arm/mpu/mm.c +1 -0
@@ -8,9 +8,11 @@
 #include <xen/sizes.h>
 #include <xen/spinlock.h>
 #include <xen/types.h>
+#include <xen/pfn.h>

Alphabetic sorting, please.

Quoted 33 lines
xen/arch/arm/mpu/mm.c +20 -2
 #include <asm/mpu.h>
 #include <asm/mpu/mm.h>
 #include <asm/page.h>
+#include <asm/setup.h>
 #include <asm/sysregs.h>
 
 struct page_info *frame_table;
@@ -378,9 +380,33 @@ int map_pages_to_xen(unsigned long virt, mfn_t mfn, unsigned long nr_mfns,
     return xen_mpumap_update(virt, mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
 }
 
-void __init setup_mm(void)
+/*
+ * Heap must be statically configured in Device Tree through "xen,static-heap"
+ * on MPU systems, use setup_mm_helper() for that.
+ */
+void __init setup_mm_helper(void)
 {
-    BUG_ON("unimplemented");
+    const struct membanks *reserved_mem = bootinfo_get_reserved_mem();
+    unsigned int bank = 0;
+
+    for ( ; bank < reserved_mem->nr_banks; bank++ )
+    {
+        if ( reserved_mem->bank[bank].type == MEMBANK_STATIC_HEAP )
+        {
+            paddr_t bank_start = round_pgup(reserved_mem->bank[bank].start);
+            paddr_t bank_size = round_pgdown(reserved_mem->bank[bank].size);
+            paddr_t bank_end = bank_start + bank_size;
+
+            /* Map static heap with one MPU protection region */
+            if ( xen_mpumap_update(bank_start, bank_end, PAGE_HYPERVISOR) )
+                panic("Failed to map static heap\n");

xen/arch/arm/mpu/mm.c int map_pages_to_xen(unsigned long virt, mfn_t mfn, unsigned long nr_mfns, · line 412

Quoted 10 lines
xen/arch/arm/mpu/mm.c +7 -0
+
+            break;
+        }
+    }
+
+    if ( bank == reserved_mem->nr_banks )
+        panic("No static heap memory bank found\n");
 }
 
 int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)

Apart from that, the changes LGTM.

~Michal

#4 lore raw

PATCH 2/3 arm: Implement reference counting for overlapping regions

In reply to Harry Ramsey

Implement reference counting to enable overlapping MPU regions. References are incremented and decremented in xen_mpumap_update_entry.

A region will be destoryed if the reference count is 0 upon calling destroy_xen_mappings and if the full region range is specified.

Additionally XEN_MPUMAP_ENTRY_SHIFT and XEN_MPUMAP_ENTRY_SHIFT_ZERO are no longer hardcoded and defined inside asm-offsets.c.

---
 xen/arch/arm/arm32/asm-offsets.c         |  2 +
 xen/arch/arm/arm64/asm-offsets.c         |  2 +
 xen/arch/arm/include/asm/arm32/mpu.h     |  2 +
 xen/arch/arm/include/asm/arm64/mpu.h     |  2 +
 xen/arch/arm/include/asm/mpu/regions.inc | 11 +++-
 xen/arch/arm/mpu/mm.c                    | 73 +++++++++++++++++++-----
 6 files changed, 77 insertions(+), 15 deletions(-)
xen/arch/arm/arm32/asm-offsets.c +2 -0
index c203ce269d..951f8d03f3 100644
@@ -79,6 +79,8 @@ void __dummy__(void)
 #ifdef CONFIG_MPU
    DEFINE(XEN_MPUMAP_MASK_sizeof, sizeof(xen_mpumap_mask));
    DEFINE(XEN_MPUMAP_sizeof, sizeof(xen_mpumap));
+   DEFINE(XEN_MPUMAP_ENTRY_SHIFT, ilog2(sizeof(pr_t)));
+   DEFINE(XEN_MPUMAP_ENTRY_ZERO_OFFSET, sizeof(prbar_t) + sizeof(prlar_t));
    BLANK();
 #endif
 }
xen/arch/arm/arm64/asm-offsets.c +2 -0
index 320289b281..38a3894a3b 100644
@@ -73,6 +73,8 @@ void __dummy__(void)
 #ifdef CONFIG_MPU
    DEFINE(XEN_MPUMAP_MASK_sizeof, sizeof(xen_mpumap_mask));
    DEFINE(XEN_MPUMAP_sizeof, sizeof(xen_mpumap));
+   DEFINE(XEN_MPUMAP_ENTRY_SHIFT, ilog2(sizeof(pr_t)));
+   DEFINE(XEN_MPUMAP_ENTRY_ZERO_OFFSET, sizeof(prbar_t) + sizeof(prlar_t));
    BLANK();
 #endif
 }
xen/arch/arm/include/asm/arm32/mpu.h +2 -0
index 0a6930b3a0..137022d922 100644
@@ -39,6 +39,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    uint8_t refcount;
+    uint8_t pad[7];     /* Pad structure to 16 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index f0ce344e78..17f62ccaf6 100644
@@ -38,6 +38,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    uint8_t refcount;
+    uint8_t pad[15];    /* Pad structure to 32 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/mpu/regions.inc +9 -2
index 23fead3b21..0cdbb17bc3 100644
@@ -14,14 +14,12 @@
 #define PRLAR_ELx_EN            0x1
 
 #ifdef CONFIG_ARM_64
-#define XEN_MPUMAP_ENTRY_SHIFT  0x4     /* 16 byte structure */
 
 .macro store_pair reg1, reg2, dst
     stp \reg1, \reg2, [\dst]
 .endm
 
 #else
-#define XEN_MPUMAP_ENTRY_SHIFT  0x3     /* 8 byte structure */
 
 .macro store_pair reg1, reg2, dst
     strd  \reg1, \reg2, [\dst]
@@ -97,6 +95,15 @@
 
 3:
 
+    /* Clear the rest of the xen_mpumap entry. */
+#ifdef CONFIG_ARM_64
+    stp xzr, xzr, [\base, #XEN_MPUMAP_ENTRY_ZERO_OFFSET]
+#else
+    mov \prbar, #0
+    mov \prlar, #0
+    strd \prbar, \prlar, [\base, #XEN_MPUMAP_ENTRY_ZERO_OFFSET]
+#endif
+
     add   \sel, \sel, #1
 
 1:
xen/arch/arm/mpu/mm.c +60 -13

98 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/3] arm: Implement reference counting for overlapping regions

Quoted 2 lines · Harry Ramsey

Implement reference counting to enable overlapping MPU regions. References are incremented and decremented in xen_mpumap_update_entry.

AFAICT, looking at the code, you would return -EINVAL early on overlap (i.e. mpumap_contains_region() returning MPUMAP_REGION_OVERLAP). If so, can you clearly explain what this change intend to do and why we need refcounting?

Quoted 2 lines

A region will be destoryed if the reference count is 0 upon calling

s/destoryed/destroyed/

Quoted 116 lines

destroy_xen_mappings and if the full region range is specified.

Additionally XEN_MPUMAP_ENTRY_SHIFT and XEN_MPUMAP_ENTRY_SHIFT_ZERO are no longer hardcoded and defined inside asm-offsets.c.

---
 xen/arch/arm/arm32/asm-offsets.c         |  2 +
 xen/arch/arm/arm64/asm-offsets.c         |  2 +
 xen/arch/arm/include/asm/arm32/mpu.h     |  2 +
 xen/arch/arm/include/asm/arm64/mpu.h     |  2 +
 xen/arch/arm/include/asm/mpu/regions.inc | 11 +++-
 xen/arch/arm/mpu/mm.c                    | 73 +++++++++++++++++++-----
 6 files changed, 77 insertions(+), 15 deletions(-)
xen/arch/arm/arm32/asm-offsets.c +2 -0
index c203ce269d..951f8d03f3 100644
@@ -79,6 +79,8 @@ void __dummy__(void)
 #ifdef CONFIG_MPU
    DEFINE(XEN_MPUMAP_MASK_sizeof, sizeof(xen_mpumap_mask));
    DEFINE(XEN_MPUMAP_sizeof, sizeof(xen_mpumap));
+   DEFINE(XEN_MPUMAP_ENTRY_SHIFT, ilog2(sizeof(pr_t)));
+   DEFINE(XEN_MPUMAP_ENTRY_ZERO_OFFSET, sizeof(prbar_t) + sizeof(prlar_t));
    BLANK();
 #endif
 }
xen/arch/arm/arm64/asm-offsets.c +2 -0
index 320289b281..38a3894a3b 100644
@@ -73,6 +73,8 @@ void __dummy__(void)
 #ifdef CONFIG_MPU
    DEFINE(XEN_MPUMAP_MASK_sizeof, sizeof(xen_mpumap_mask));
    DEFINE(XEN_MPUMAP_sizeof, sizeof(xen_mpumap));
+   DEFINE(XEN_MPUMAP_ENTRY_SHIFT, ilog2(sizeof(pr_t)));
+   DEFINE(XEN_MPUMAP_ENTRY_ZERO_OFFSET, sizeof(prbar_t) + sizeof(prlar_t));
    BLANK();
 #endif
 }
xen/arch/arm/include/asm/arm32/mpu.h +2 -0
index 0a6930b3a0..137022d922 100644
@@ -39,6 +39,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    uint8_t refcount;
+    uint8_t pad[7];     /* Pad structure to 16 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index f0ce344e78..17f62ccaf6 100644
@@ -38,6 +38,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    uint8_t refcount;
+    uint8_t pad[15];    /* Pad structure to 32 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/mpu/regions.inc +9 -2
index 23fead3b21..0cdbb17bc3 100644
@@ -14,14 +14,12 @@
 #define PRLAR_ELx_EN            0x1
 
 #ifdef CONFIG_ARM_64
-#define XEN_MPUMAP_ENTRY_SHIFT  0x4     /* 16 byte structure */
 
 .macro store_pair reg1, reg2, dst
     stp \reg1, \reg2, [\dst]
 .endm
 
 #else
-#define XEN_MPUMAP_ENTRY_SHIFT  0x3     /* 8 byte structure */
 
 .macro store_pair reg1, reg2, dst
     strd  \reg1, \reg2, [\dst]
@@ -97,6 +95,15 @@
 
 3:
 
+    /* Clear the rest of the xen_mpumap entry. */
+#ifdef CONFIG_ARM_64
+    stp xzr, xzr, [\base, #XEN_MPUMAP_ENTRY_ZERO_OFFSET]
+#else
+    mov \prbar, #0
+    mov \prlar, #0
+    strd \prbar, \prlar, [\base, #XEN_MPUMAP_ENTRY_ZERO_OFFSET]
+#endif
+
     add   \sel, \sel, #1
 
 1:
xen/arch/arm/mpu/mm.c +1 -0
index a058db19ef..c5128244b7 100644
@@ -106,6 +106,7 @@ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags)
     region = (pr_t) {
         .prbar = prbar,
         .prlar = prlar,
+        .refcount = 0,
     };
 
     /* Set base address and limit address. */
@@ -170,6 +171,37 @@ int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base,
     return MPUMAP_REGION_NOTFOUND;

xen/arch/arm/mpu/mm.c int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, · line 181

Quoted 10 lines
xen/arch/arm/mpu/mm.c +8 -0
 }
 
+static bool is_mm_attr_match(pr_t *region, unsigned int attributes)
+{
+    bool ret = true;
+
+    if ( region->prbar.reg.ro != PAGE_RO_MASK(attributes) )
+    {
+        printk(XENLOG_WARNING
+               "Mismatched Access Permission attributes (%#x0 instead of %#x0)\n",

Why %#x0 and not %#x?

Quoted 9 lines
xen/arch/arm/mpu/mm.c +9 -0
+               region->prbar.reg.ro, PAGE_RO_MASK(attributes));
+        ret = false;
+    }
+
+    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));

xen/arch/arm/mpu/mm.c int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, · line 200

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+        ret = false;
+    }
+
+    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));
+        ret = false;
+    }

What about shareability?

Quoted 8 lines
xen/arch/arm/mpu/mm.c +4 -0
+
+    return ret;
+}
+
 /* Map a frame table to cover physical addresses ps through pe */
 void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
 {

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 322

Quoted 7 lines
xen/arch/arm/mpu/mm.c +1 -3
@@ -287,19 +319,19 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
     /* Currently we don't support modifying an existing entry. */
     if ( flags_has_page_present && (rc >= MPUMAP_REGION_FOUND) )
     {
-        printk("Modifying an existing entry is not supported\n");
-        return -EINVAL;
-    }
+        if ( !is_mm_attr_match(&xen_mpumap[idx], flags) )

Do I understand correctly that this change (not mentioned in commit msg) is here so that when we call xen_mpumap_update_entry() with existing matching or inclusive region we will increment refcount only if the attributes match?

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 298

Quoted 10 lines
xen/arch/arm/mpu/mm.c +4 -5
+        {
+            printk("Modifying an existing entry is not supported\n");
+            return -EINVAL;
+        }
 
-    /*
-     * Currently, we only support removing/modifying a *WHOLE* MPU memory
-     * region. Part-region removal/modification is not supported as in the worst
-     * case it will leave two/three fragments behind.
-     */

Hmm, I think that we still don't support removing/modifying regions partially. Why is this comment removed?

Quoted 15 lines
xen/arch/arm/mpu/mm.c +7 -4
-    if ( rc == MPUMAP_REGION_INCLUSIVE )
-    {
-        printk("Part-region removal/modification is not supported\n");
-        return -EINVAL;
+        /* Check for overflow of refcount before incrementing.  */
+        if ( xen_mpumap[idx].refcount == 0xFF )
+        {
+            printk("Cannot allocate region as it would cause reference overflow\n");
+            return -ENOENT;
+        }
+        xen_mpumap[idx].refcount += 1;
     }
 
     /* We are inserting a mapping => Create new region. */

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 360

Quoted 7 lines
xen/arch/arm/mpu/mm.c +3 -1
@@ -323,7 +355,22 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
             return -EINVAL;
         }
 
-        disable_mpu_region_from_index(idx);
+        if ( xen_mpumap[idx].refcount == 0 )
+        {
+            if (MPUMAP_REGION_FOUND == rc)

Missing spaces around ().

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 361

Quoted 1 line
xen/arch/arm/mpu/mm.c +1 -0
+            {

No need for brackets for single instruction

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 371

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+                disable_mpu_region_from_index(idx);
+            }
+            else
+            {
+                printk("Cannot remove a partial region\n");
+                return -EINVAL;
+            }
+        }
+        else
+        {

Same here.

~Michal

#6 lore raw

Re: [PATCH 2/3] arm: Implement reference counting for overlapping regions

Hello,

Quoted 6 lines · Michal
Quoted 2 lines · Harry Ramsey

Implement reference counting to enable overlapping MPU regions. References are incremented and decremented in xen_mpumap_update_entry.

AFAICT, looking at the code, you would return -EINVAL early on overlap (i.e. mpumap_contains_region() returning MPUMAP_REGION_OVERLAP). If so, can you clearly explain what this change intend to do and why we need refcounting?

Perhaps overlap is a bit misleading/confusing in this context and instead I should have said inclusive regions. Reference counting is used in this context to prevent us from attempting to remove a portion of the region whilst it is still in use by other regions.

Quoted 121 lines
Quoted 1 line

A region will be destoryed if the reference count is 0 upon calling

s/destoryed/destroyed/

Quoted 118 lines

destroy_xen_mappings and if the full region range is specified.

Additionally XEN_MPUMAP_ENTRY_SHIFT and XEN_MPUMAP_ENTRY_SHIFT_ZERO are no longer hardcoded and defined inside asm-offsets.c.

---
  xen/arch/arm/arm32/asm-offsets.c         |  2 +
  xen/arch/arm/arm64/asm-offsets.c         |  2 +
  xen/arch/arm/include/asm/arm32/mpu.h     |  2 +
  xen/arch/arm/include/asm/arm64/mpu.h     |  2 +
  xen/arch/arm/include/asm/mpu/regions.inc | 11 +++-
  xen/arch/arm/mpu/mm.c                    | 73 +++++++++++++++++++-----
  6 files changed, 77 insertions(+), 15 deletions(-)
xen/arch/arm/arm32/asm-offsets.c +2 -0
index c203ce269d..951f8d03f3 100644
@@ -79,6 +79,8 @@ void __dummy__(void)
  #ifdef CONFIG_MPU
     DEFINE(XEN_MPUMAP_MASK_sizeof, sizeof(xen_mpumap_mask));
     DEFINE(XEN_MPUMAP_sizeof, sizeof(xen_mpumap));
+   DEFINE(XEN_MPUMAP_ENTRY_SHIFT, ilog2(sizeof(pr_t)));
+   DEFINE(XEN_MPUMAP_ENTRY_ZERO_OFFSET, sizeof(prbar_t) + sizeof(prlar_t));
     BLANK();
  #endif
  }
xen/arch/arm/arm64/asm-offsets.c +2 -0
index 320289b281..38a3894a3b 100644
@@ -73,6 +73,8 @@ void __dummy__(void)
  #ifdef CONFIG_MPU
     DEFINE(XEN_MPUMAP_MASK_sizeof, sizeof(xen_mpumap_mask));
     DEFINE(XEN_MPUMAP_sizeof, sizeof(xen_mpumap));
+   DEFINE(XEN_MPUMAP_ENTRY_SHIFT, ilog2(sizeof(pr_t)));
+   DEFINE(XEN_MPUMAP_ENTRY_ZERO_OFFSET, sizeof(prbar_t) + sizeof(prlar_t));
     BLANK();
  #endif
  }
xen/arch/arm/include/asm/arm32/mpu.h +2 -0
index 0a6930b3a0..137022d922 100644
@@ -39,6 +39,8 @@ typedef union {
  typedef struct {
      prbar_t prbar;
      prlar_t prlar;
+    uint8_t refcount;
+    uint8_t pad[7];     /* Pad structure to 16 Bytes */
  } pr_t;
 
  #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index f0ce344e78..17f62ccaf6 100644
@@ -38,6 +38,8 @@ typedef union {
  typedef struct {
      prbar_t prbar;
      prlar_t prlar;
+    uint8_t refcount;
+    uint8_t pad[15];    /* Pad structure to 32 Bytes */
  } pr_t;
 
  #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/mpu/regions.inc +9 -2
index 23fead3b21..0cdbb17bc3 100644
@@ -14,14 +14,12 @@
  #define PRLAR_ELx_EN            0x1
 
  #ifdef CONFIG_ARM_64
-#define XEN_MPUMAP_ENTRY_SHIFT  0x4     /* 16 byte structure */
 
  .macro store_pair reg1, reg2, dst
      stp \reg1, \reg2, [\dst]
  .endm
 
  #else
-#define XEN_MPUMAP_ENTRY_SHIFT  0x3     /* 8 byte structure */
 
  .macro store_pair reg1, reg2, dst
      strd  \reg1, \reg2, [\dst]
@@ -97,6 +95,15 @@
 
  3:
 
+    /* Clear the rest of the xen_mpumap entry. */
+#ifdef CONFIG_ARM_64
+    stp xzr, xzr, [\base, #XEN_MPUMAP_ENTRY_ZERO_OFFSET]
+#else
+    mov \prbar, #0
+    mov \prlar, #0
+    strd \prbar, \prlar, [\base, #XEN_MPUMAP_ENTRY_ZERO_OFFSET]
+#endif
+
      add   \sel, \sel, #1
 
  1:
xen/arch/arm/mpu/mm.c +1 -0
index a058db19ef..c5128244b7 100644
@@ -106,6 +106,7 @@ pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags)
      region = (pr_t) {
          .prbar = prbar,
          .prlar = prlar,
+        .refcount = 0,
      };
 
      /* Set base address and limit address. */
@@ -170,6 +171,37 @@ int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base,
      return MPUMAP_REGION_NOTFOUND;
  }
 
Quoted 9 lines

xen/arch/arm/mpu/mm.c int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, · line 181

Quoted 8 lines
xen/arch/arm/mpu/mm.c +8 -0
+static bool is_mm_attr_match(pr_t *region, unsigned int attributes)
+{
+    bool ret = true;
+
+    if ( region->prbar.reg.ro != PAGE_RO_MASK(attributes) )
+    {
+        printk(XENLOG_WARNING
+               "Mismatched Access Permission attributes (%#x0 instead of %#x0)\n",

Why %#x0 and not %#x?

I will remove this as I do not think it is necessary to understand the permission attributes.

Quoted 12 lines
Quoted 12 lines
xen/arch/arm/mpu/mm.c +12 -0
+               region->prbar.reg.ro, PAGE_RO_MASK(attributes));
+        ret = false;
+    }
+
+    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));
+        ret = false;
+    }
+
Quoted 8 lines

xen/arch/arm/mpu/mm.c int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base, · line 200

Quoted 7 lines
xen/arch/arm/mpu/mm.c +7 -0
+    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));
+        ret = false;
+    }

What about shareability?

Unless I am mistaken, inside page.h we define 8 unique regions which have their own sharability/permissions, so if prlar.reg.ai does not match, the sharability/permissions are incorrect. Thus we should not require a seperate check.

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/mpu/mm.c +4 -0
+
+    return ret;
+}
+
  /* Map a frame table to cover physical addresses ps through pe */
  void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
  {
@@ -287,19 +319,19 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
      /* Currently we don't support modifying an existing entry. */
      if ( flags_has_page_present && (rc >= MPUMAP_REGION_FOUND) )
Quoted 8 lines

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 322

Quoted 5 lines
xen/arch/arm/mpu/mm.c +1 -3
      {
-        printk("Modifying an existing entry is not supported\n");
-        return -EINVAL;
-    }
+        if ( !is_mm_attr_match(&xen_mpumap[idx], flags) )

Do I understand correctly that this change (not mentioned in commit msg) is here so that when we call xen_mpumap_update_entry() with existing matching or inclusive region we will increment refcount only if the attributes match?

That would be correct, I will update the commit message to provide additional context especially with regards to what we mean by allocating inclusive regions.

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/mpu/mm.c +4 -0
+        {
+            printk("Modifying an existing entry is not supported\n");
+            return -EINVAL;
+        }
 
Quoted 7 lines

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 298

Quoted 5 lines
xen/arch/arm/mpu/mm.c +0 -5
-    /*
-     * Currently, we only support removing/modifying a *WHOLE* MPU memory
-     * region. Part-region removal/modification is not supported as in the worst
-     * case it will leave two/three fragments behind.
-     */

Hmm, I think that we still don't support removing/modifying regions partially. Why is this comment removed?

Sorry, this is a mistake and I will update the comment to reflect to better reflect our implementation with reference counting.

Quoted 37 lines
Quoted 15 lines
xen/arch/arm/mpu/mm.c +7 -4
-    if ( rc == MPUMAP_REGION_INCLUSIVE )
-    {
-        printk("Part-region removal/modification is not supported\n");
-        return -EINVAL;
+        /* Check for overflow of refcount before incrementing.  */
+        if ( xen_mpumap[idx].refcount == 0xFF )
+        {
+            printk("Cannot allocate region as it would cause reference overflow\n");
+            return -ENOENT;
+        }
+        xen_mpumap[idx].refcount += 1;
      }
 
      /* We are inserting a mapping => Create new region. */

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 360

Quoted 7 lines
xen/arch/arm/mpu/mm.c +3 -1
@@ -323,7 +355,22 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
              return -EINVAL;
          }
 
-        disable_mpu_region_from_index(idx);
+        if ( xen_mpumap[idx].refcount == 0 )
+        {
+            if (MPUMAP_REGION_FOUND == rc)

Missing spaces around ().

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 361

Quoted 1 line
xen/arch/arm/mpu/mm.c +1 -0
+            {

No need for brackets for single instruction

xen/arch/arm/mpu/mm.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit, · line 371

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+                disable_mpu_region_from_index(idx);
+            }
+            else
+            {
+                printk("Cannot remove a partial region\n");
+                return -EINVAL;
+            }
+        }
+        else
+        {
Quoted 3 lines

Same here.

~Michal

Thanks for the feedback, I will address the rest of these changes in v2.

Thanks,
Harry Ramsey.

#7 lore raw

PATCH 3/3 arm: Implement ioremap_attr for MPU

In reply to Harry Ramsey

Implement the function ioremap_attr for MPU systems.

---
 xen/arch/arm/mpu/mm.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
xen/arch/arm/mpu/mm.c +7 -2
index c5128244b7..3570ce6b27 100644
@@ -475,8 +475,13 @@ void free_init_memory(void)
 
 void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags)
 {
-    BUG_ON("unimplemented");
-    return NULL;
+    paddr_t start_pg = round_pgdown(start);
+    paddr_t end_pg = round_pgup(start_pg + len);
+    if ( xen_mpumap_update(start_pg, end_pg, flags) )
+        return NULL;
+
+    /* Mapped or already mapped */
+    return maddr_to_virt(start_pg);
 }
 
 /*
Signature · 1 line
2.43.0