Skip to contentSkip to conversation

Third series for R82 MPU support

PATCH Superseded Hari Limaye 3 participants 22 messages Jul 30, 2025 – Aug 28, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#4 lore raw

PATCH 2/5 arm/mpu: Implement setup_mm for MPU systems

In reply to Hari Limaye

From: Luca Fancellu <[email protected]>

Implement setup_mm for MPU systems. This variant doesn't need to set up the direct map.

---
 xen/arch/arm/mpu/mm.c | 64 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
xen/arch/arm/mpu/mm.c +63 -1
index 6a16fa348d..0b05103180 100644
@@ -8,9 +8,12 @@
 #include <xen/sizes.h>
 #include <xen/spinlock.h>
 #include <xen/types.h>
+#include <xen/static-memory.h>
+#include <xen/static-shmem.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;
@@ -364,9 +367,68 @@ 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);
 }
 
+/*
+ * Heap must be statically configured in Device Tree through "xen,static-heap"
+ * on MPU systems.
+ */
+static void __init setup_staticheap_mappings(void)
+{
+    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");
+}
+
 void __init setup_mm(void)
 {
-    BUG_ON("unimplemented");
+    const struct membanks *mem = bootinfo_get_mem();
+    paddr_t ram_start = INVALID_PADDR, ram_end = 0, ram_size = 0;
+
+    if ( !mem->nr_banks )
+        panic("No memory bank\n");
+
+    init_pdx();
+
+    populate_boot_allocator();
+
+    total_pages = 0;
+    for ( unsigned int bank = 0 ; bank < mem->nr_banks; bank++ )
+    {
+        paddr_t bank_start = round_pgup(mem->bank[bank].start);
+        paddr_t bank_size = round_pgdown(mem->bank[bank].size);
+        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);
+    }
+
+    setup_staticheap_mappings();
+
+    total_pages += ram_size >> PAGE_SHIFT;
+    max_page = PFN_DOWN(ram_end);
+
+    setup_frametable_mappings(ram_start, ram_end);
+
+    init_staticmem_pages();
+    init_sharedmem_pages();
 }
 
 int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
Signature · 1 line
2.34.1
#0 lore raw

cover Third series for R82 MPU support

Hi all,

This series is the third set of patches in the ongoing work to introduce support for MPU systems and Cortex R82 in Xen.

The patches in this series implement the necessary logic to transiently map and unmap the static memory banks for initialization.

Cheers,
Hari

Luca Fancellu (4):
arm/mpu: Implement setup_frametable_mappings for MPU systems arm/mpu: Implement setup_mm for MPU systems arm/mpu: Implement transient mapping
arm/mpu: Implement ioremap_attr for MPU

Penny Zheng (1):
xen/arm: map static memory on demand

 xen/arch/arm/include/asm/arm32/mpu.h     |   2 +
 xen/arch/arm/include/asm/arm64/mpu.h     |   2 +
 xen/arch/arm/include/asm/mmu/mm.h        |   3 +
 xen/arch/arm/include/asm/mpu/mm.h        |  40 +++-
 xen/arch/arm/include/asm/mpu/regions.inc |  19 +-
 xen/arch/arm/mpu/mm.c                    | 250 +++++++++++++++++++++--
 xen/arch/arm/mpu/setup.c                 |  11 +
 xen/include/xen/static-memory.h          |   8 +
 8 files changed, 316 insertions(+), 19 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/5 arm/mpu: Implement setup_frametable_mappings for MPU systems

From: Luca Fancellu <[email protected]>

Implement the MPU variant of setup_frametable_mappings. This function will be called by setup_mm when an implementation for MPU systems is added in a follow up commit.

---
 xen/arch/arm/mpu/mm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
xen/arch/arm/mpu/mm.c +19 -0
index c6891607ec..6a16fa348d 100644
@@ -168,6 +168,25 @@ int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base,
     return MPUMAP_REGION_NOTFOUND;
 }
 
+/* Map a frame table to cover physical addresses ps through pe */
+void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
+{
+    mfn_t base_mfn;
+    unsigned long nr_pdxs = mfn_to_pdx(mfn_add(maddr_to_mfn(pe), -1)) -
+                            mfn_to_pdx(maddr_to_mfn(ps)) + 1;
+    unsigned long frametable_size = nr_pdxs * sizeof(struct page_info);
+
+    frametable_base_pdx = paddr_to_pdx(ps);
+    frametable_size = ROUNDUP(frametable_size, PAGE_SIZE);
+
+    base_mfn = alloc_boot_pages(frametable_size >> PAGE_SHIFT, 1);
+    frame_table = (struct page_info *)mfn_to_virt(mfn_x(base_mfn));
+
+    memset(&frame_table[0], 0, nr_pdxs * sizeof(struct page_info));
+    memset(&frame_table[nr_pdxs], -1,
+           frametable_size - (nr_pdxs * sizeof(struct page_info)));
+}
+
 /*
  * Allocate an entry for a new EL2 MPU region in the bitmap xen_mpumap_mask.
  * @param idx   Set to the index of the allocated EL2 MPU region on success.
Signature · 1 line
2.34.1
#9 lore raw

PATCH 3/5 arm/mpu: Implement transient mapping

In reply to Hari Limaye

From: Luca Fancellu <[email protected]>

Add a scheme to distinguish transient MPU regions, to identify MPU regions which will be mapped for a short period of time.

---
 xen/arch/arm/include/asm/arm32/mpu.h     |  2 ++
 xen/arch/arm/include/asm/arm64/mpu.h     |  2 ++
 xen/arch/arm/include/asm/mpu/mm.h        | 14 +++++++++++++-
 xen/arch/arm/include/asm/mpu/regions.inc | 19 +++++++++++++++++--
 xen/arch/arm/mpu/mm.c                    | 23 ++++++++++++++---------
 5 files changed, 48 insertions(+), 12 deletions(-)
xen/arch/arm/include/asm/arm32/mpu.h +2 -0
index 0a6930b3a0..9906d98809 100644
@@ -39,6 +39,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    bool transient;
+    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..1d1843eda0 100644
@@ -38,6 +38,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    bool transient;
+    uint8_t pad[15]; /* Pad structure to 32 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/mpu/mm.h +13 -1
index c32fac8905..56ca411af4 100644
@@ -60,6 +60,16 @@ static inline void context_sync_mpu(void)
     isb();
 }
 
+static inline bool region_is_transient(pr_t *pr)
+{
+    return pr->transient;
+}
+
+static inline void region_set_transient(pr_t *pr, bool transient)
+{
+    pr->transient = transient;
+}
+
 /*
  * The following API requires context_sync_mpu() after being used to modify MPU
  * regions:
@@ -80,9 +90,11 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel);
  * @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.
+ * @param transient True for a temporary mapping, otherwise False.
  * @return          0 on success, negative on error.
  */
-int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags);
+int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags,
+                      bool transient);
 
 /*
  * Creates a pr_t structure describing a protection region.
xen/arch/arm/include/asm/mpu/regions.inc +17 -2
index 23fead3b21..f9892fe3d8 100644
@@ -14,19 +14,31 @@
 #define PRLAR_ELx_EN            0x1
 
 #ifdef CONFIG_ARM_64
-#define XEN_MPUMAP_ENTRY_SHIFT  0x4     /* 16 byte structure */
+#define XEN_MPUMAP_ENTRY_SHIFT        0x5   /* 32 byte structure */
+#define XEN_MPUMAP_ENTRY_ZERO_OFFSET  0x10  /* {PRBAR, PRLAR} is 16 bytes  */
 
 .macro store_pair reg1, reg2, dst
     stp \reg1, \reg2, [\dst]
 .endm
 
+.macro zero_pair dst, offset, tmp1, tmp2
+    stp xzr, xzr, [\dst, \offset]
+.endm
+
 #else
-#define XEN_MPUMAP_ENTRY_SHIFT  0x3     /* 8 byte structure */
+#define XEN_MPUMAP_ENTRY_SHIFT        0x4   /* 16 byte structure */
+#define XEN_MPUMAP_ENTRY_ZERO_OFFSET  0x8   /* {PRBAR, PRLAR} is 8 bytes  */
 
 .macro store_pair reg1, reg2, dst
     strd  \reg1, \reg2, [\dst]
 .endm
 
+.macro zero_pair dst, offset, tmp1, tmp2
+    mov \tmp1, #0
+    mov \tmp2, #0
+    strd \tmp1, \tmp2, [\dst, \offset]
+.endm
+
 #endif
 
 /*
@@ -97,6 +109,9 @@
 
 3:
 
+    /* Clear the rest of the xen_mpumap entry. Clobbers prbar and prlar. */
+    zero_pair \base, #XEN_MPUMAP_ENTRY_ZERO_OFFSET, \prbar, \prlar
+
     add   \sel, \sel, #1
 
 1:
xen/arch/arm/mpu/mm.c +14 -9
index 0b05103180..38474bcfa2 100644
@@ -251,13 +251,14 @@ static void disable_mpu_region_from_index(uint8_t index)
  * Update the entry in the MPU memory region mapping table (xen_mpumap) for 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)
+ * @param base      Base address (inclusive).
+ * @param limit     Limit address (exclusive).
+ * @param flags     Region attributes (a combination of PAGE_HYPERVISOR_XXX)
+ * @param transient True for a temporary mapping, otherwise False.
  * @return      0 on success, otherwise negative on error.
  */
 static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
-                                   unsigned int flags)
+                                   unsigned int flags, bool transient)
 {
     bool flags_has_page_present;
     uint8_t idx;
@@ -297,6 +298,7 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
             return -ENOENT;
 
         xen_mpumap[idx] = pr_of_addr(base, limit, flags);
+        region_set_transient(&xen_mpumap[idx], transient);
 
         write_protection_region(&xen_mpumap[idx], idx);
     }
@@ -316,7 +318,8 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
     return 0;
 }
 
-int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags)
+int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags,
+                      bool transient)
 {
     int rc;
 
@@ -342,7 +345,7 @@ int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags)
 
     spin_lock(&xen_mpumap_lock);
 
-    rc = xen_mpumap_update_entry(base, limit, flags);
+    rc = xen_mpumap_update_entry(base, limit, flags, transient);
     if ( !rc )
         context_sync_mpu();
 
@@ -357,14 +360,15 @@ int destroy_xen_mappings(unsigned long s, unsigned long e)
     ASSERT(IS_ALIGNED(e, PAGE_SIZE));
     ASSERT(s < e);
 
-    return xen_mpumap_update(s, e, 0);
+    return xen_mpumap_update(s, e, 0, false);
 }
 
 int map_pages_to_xen(unsigned long virt, mfn_t mfn, unsigned long nr_mfns,
                      unsigned int flags)
 {
     /* MPU systems have no translation, ma == va, so pass virt directly */
-    return xen_mpumap_update(virt, mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
+    return xen_mpumap_update(virt, mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags,
+                             false);
 }
 
 /*
@@ -385,7 +389,8 @@ static void __init setup_staticheap_mappings(void)
             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) )
+            if ( xen_mpumap_update(bank_start, bank_end, PAGE_HYPERVISOR,
+                                   false) )
                 panic("Failed to map static heap\n");
 
             break;
Signature · 1 line
2.34.1
#12 lore raw

PATCH 4/5 arm/mpu: Implement ioremap_attr for MPU

In reply to Hari Limaye

From: Luca Fancellu <[email protected]>

Introduce helpers (un)map_mm_range() in order to allow the temporary mapping of a range of memory, and use these to implement the function ioremap_attr for MPU systems.

---
 xen/arch/arm/include/asm/mpu/mm.h |  22 +++++
 xen/arch/arm/mpu/mm.c             | 150 ++++++++++++++++++++++++++++--
 2 files changed, 163 insertions(+), 9 deletions(-)
xen/arch/arm/include/asm/mpu/mm.h +22 -0
index 56ca411af4..177550f5bd 100644
@@ -106,6 +106,28 @@ int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags,
  */
 pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags);
 
+/*
+ * Maps a temporary range of memory with attributes `flags`; if the range is
+ * already mapped with the same attributes, including an inclusive match, the
+ * existing mapping is returned. This API is intended for mappings that exist
+ * transiently for a short period between calls to this function and
+ * `unmap_mm_range`.
+ *
+ * @param start     Base address of the range to map (inclusive).
+ * @param end       Limit address of the range to map (exclusive).
+ * @param flags     Flags for the memory range to map.
+ * @return          Pointer to start of region on success, NULL on error.
+ */
+void *map_mm_range(paddr_t start, paddr_t end, unsigned int flags);
+
+/*
+ * Unmaps a temporary range of memory if it was previously mapped by
+ * map_mm_range, otherwise it does not remove the mapping.
+ *
+ * @param start     Base address of the range to map (inclusive).
+ */
+void unmap_mm_range(paddr_t start);
+
 /*
  * Checks whether a given memory range is present in the provided table of
  * MPU protection regions.
xen/arch/arm/mpu/mm.c +141 -9

182 lines not on this page · raw message

Signature · 1 line
2.34.1
#16 lore raw

PATCH 5/5 xen/arm: map static memory on demand

In reply to Hari Limaye

From: Penny Zheng <[email protected]>

In the function init_staticmem_pages we need to have mapped static memory banks for initialization. Unlike on an MMU system, we cannot map the entire RAM on an MPU system as we have a limited number of MPU memory regions. To solve this, transiently map the static memory banks for initialization.

---
 xen/arch/arm/include/asm/mmu/mm.h |  3 +++
 xen/arch/arm/include/asm/mpu/mm.h |  4 ++++
 xen/arch/arm/mpu/setup.c          | 11 +++++++++++
 xen/include/xen/static-memory.h   |  8 ++++++++
 4 files changed, 26 insertions(+)
xen/arch/arm/include/asm/mmu/mm.h +3 -0
index 7f4d59137d..645a0ea3cb 100644
@@ -110,6 +110,9 @@ void dump_pt_walk(paddr_t ttbr, paddr_t addr,
 extern void switch_ttbr(uint64_t ttbr);
 extern void relocate_and_switch_ttbr(uint64_t ttbr);
 
+static inline void map_staticmem_pages_to_xen(paddr_t start, paddr_t end) {}
+static inline void unmap_staticmem_pages_to_xen(paddr_t start, paddr_t end) {}
+
 #endif /* __ARM_MMU_MM_H__ */
 
 /*
xen/arch/arm/include/asm/mpu/mm.h +4 -0
index 177550f5bd..118034bbdc 100644
@@ -128,6 +128,10 @@ void *map_mm_range(paddr_t start, paddr_t end, unsigned int flags);
  */
 void unmap_mm_range(paddr_t start);
 
+/* {un}map_staticmem_pages_to_xen used while initializing static memory banks */
+void map_staticmem_pages_to_xen(paddr_t start, paddr_t end);
+void unmap_staticmem_pages_to_xen(paddr_t start, paddr_t end);
+
 /*
  * Checks whether a given memory range is present in the provided table of
  * MPU protection regions.
xen/arch/arm/mpu/setup.c +11 -0
index 163573b932..dbc3107333 100644
@@ -83,6 +83,17 @@ void * __init early_fdt_map(paddr_t fdt_paddr)
     return fdt_virt;
 }
 
+void __init map_staticmem_pages_to_xen(paddr_t start, paddr_t end)
+{
+    if ( !map_mm_range(start, end, PAGE_HYPERVISOR) )
+        panic("Unable to map staticmem pages to Xen!");
+}
+
+void __init unmap_staticmem_pages_to_xen(paddr_t start, paddr_t end)
+{
+    unmap_mm_range(start);
+}
+
 /*
  * copy_from_paddr - copy data from a physical address
  * @dst: destination virtual address
xen/include/xen/static-memory.h +8 -0
index e445aa8057..d99abac113 100644
@@ -18,7 +18,15 @@ static inline void init_staticmem_bank(const struct membank *bank)
     if ( mfn_x(bank_end) <= mfn_x(bank_start) )
         return;
 
+    /* Map temporarily before initialization */
+    map_staticmem_pages_to_xen(mfn_to_maddr(bank_start),
+                               mfn_to_maddr(bank_end));
+
     unprepare_staticmem_pages(mfn_to_page(bank_start), bank_pages, false);
+
+    /* Unmap immediately after initialization */
+    unmap_staticmem_pages_to_xen(mfn_to_maddr(bank_start),
+                                 mfn_to_maddr(bank_end));
 }
 
 void allocate_static_memory(struct domain *d, struct kernel_info *kinfo,
Signature · 1 line
2.34.1
Reviewed-by Ayan Kumarb Halder Tested-by Ayan Kumarb Halder on v1 1/5
#2 lore raw

Re: [PATCH 1/5] arm/mpu: Implement setup_frametable_mappings for MPU systems

In reply to Hari Limaye

Hi Hari,

Quoted 11 lines · Hari Limaye

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

From: Luca Fancellu <[email protected]>

Implement the MPU variant of setup_frametable_mappings. This function will be called by setup_mm when an implementation for MPU systems is added in a follow up commit.

(On R82 and R52 with some additional patches)

- Ayan

Reviewed-by Tested-by on v1 3/5
#10 lore raw

Re: [PATCH 3/5] arm/mpu: Implement transient mapping

In reply to Hari Limaye

Hi Hari,

Quoted 10 lines · Hari Limaye

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

From: Luca Fancellu <[email protected]>

Add a scheme to distinguish transient MPU regions, to identify MPU regions which will be mapped for a short period of time.

(On R82 and R52 with some additional patches)

- Ayan

Reviewed-by Ayan Kumarb Halder Tested-by Ayan Kumarb Halder on v1 2/5
#5 lore raw

Re: [PATCH 2/5] arm/mpu: Implement setup_mm for MPU systems

In reply to Hari Limaye

Hi Hari,

Quoted 10 lines · Hari Limaye

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

From: Luca Fancellu <[email protected]>

Implement setup_mm for MPU systems. This variant doesn't need to set up the direct map.

(On R82 and R52 with some additional patches)

- Ayan

Reviewed-by Tested-by on v1 4/5
#13 lore raw

Re: [PATCH 4/5] arm/mpu: Implement ioremap_attr for MPU

In reply to Hari Limaye

Hi Hari,

Quoted 11 lines · Hari Limaye

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

From: Luca Fancellu <[email protected]>

Introduce helpers (un)map_mm_range() in order to allow the temporary mapping of a range of memory, and use these to implement the function ioremap_attr for MPU systems.

(On R82 and R52 with some additional patches)

- Ayan

Reviewed-by Tested-by on v1 5/5
#17 lore raw

Re: [PATCH 5/5] xen/arm: map static memory on demand

In reply to Hari Limaye

Hi Hari,

Quoted 11 lines · Hari Limaye

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

From: Penny Zheng <[email protected]>

In the function init_staticmem_pages we need to have mapped static memory banks for initialization. Unlike on an MMU system, we cannot map the entire RAM on an MPU system as we have a limited number of MPU memory regions. To solve this, transiently map the static memory banks for initialization.

Quoted 4 lines

(On R82 and R52 with some additional patches)

Reviewed-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] arm/mpu: Implement setup_frametable_mappings for MPU systems

In reply to Hari Limaye
Quoted 17 lines · Hari Limaye

From: Luca Fancellu <[email protected]>

Implement the MPU variant of setup_frametable_mappings. This function will be called by setup_mm when an implementation for MPU systems is added in a follow up commit.

---
 xen/arch/arm/mpu/mm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
xen/arch/arm/mpu/mm.c +0 -0
index c6891607ec..6a16fa348d 100644

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

Quoted 9 lines
xen/arch/arm/mpu/mm.c +6 -0
@@ -168,6 +168,25 @@ int mpumap_contains_region(pr_t *table, uint8_t nr_regions, paddr_t base,
     return MPUMAP_REGION_NOTFOUND;
 }
 
+/* Map a frame table to cover physical addresses ps through pe */
+void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
+{
+    mfn_t base_mfn;
+    unsigned long nr_pdxs = mfn_to_pdx(mfn_add(maddr_to_mfn(pe), -1)) -
+                            mfn_to_pdx(maddr_to_mfn(ps)) + 1;

Don't you need to make sure that ps is rounded up to page size and e rounded down?

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

Quoted 1 line
xen/arch/arm/mpu/mm.c +1 -0
+    unsigned long frametable_size = nr_pdxs * sizeof(struct page_info);

Why don't you need sanity checking with BUILD_BUG_ON to check frametable size?

Quoted 5 lines
xen/arch/arm/mpu/mm.c +5 -0
+
+    frametable_base_pdx = paddr_to_pdx(ps);
+    frametable_size = ROUNDUP(frametable_size, PAGE_SIZE);
+
+    base_mfn = alloc_boot_pages(frametable_size >> PAGE_SHIFT, 1);

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c +7 -0
+    frame_table = (struct page_info *)mfn_to_virt(mfn_x(base_mfn));
+
+    memset(&frame_table[0], 0, nr_pdxs * sizeof(struct page_info));
+    memset(&frame_table[nr_pdxs], -1,
+           frametable_size - (nr_pdxs * sizeof(struct page_info)));
+}
+
 /*
  * Allocate an entry for a new EL2 MPU region in the bitmap xen_mpumap_mask.
  * @param idx   Set to the index of the allocated EL2 MPU region on success.

Other than that:

~Michal

#6 lore raw

Re: [PATCH 2/5] arm/mpu: Implement setup_mm for MPU systems

In reply to Hari Limaye
Quoted 65 lines · Hari Limaye

From: Luca Fancellu <[email protected]>

Implement setup_mm for MPU systems. This variant doesn't need to set up the direct map.

---
 xen/arch/arm/mpu/mm.c | 64 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
xen/arch/arm/mpu/mm.c +33 -1
index 6a16fa348d..0b05103180 100644
@@ -8,9 +8,12 @@
 #include <xen/sizes.h>
 #include <xen/spinlock.h>
 #include <xen/types.h>
+#include <xen/static-memory.h>
+#include <xen/static-shmem.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;
@@ -364,9 +367,68 @@ 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);
 }
 
+/*
+ * Heap must be statically configured in Device Tree through "xen,static-heap"
+ * on MPU systems.
+ */
+static void __init setup_staticheap_mappings(void)
+{
+    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");
+}
+
 void __init setup_mm(void)
 {
-    BUG_ON("unimplemented");
+    const struct membanks *mem = bootinfo_get_mem();

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+    paddr_t ram_start = INVALID_PADDR, ram_end = 0, ram_size = 0;
+
+    if ( !mem->nr_banks )
+        panic("No memory bank\n");
+
+    init_pdx();
+
+    populate_boot_allocator();
+
+    total_pages = 0;

There's no other place setting up total_pages between this and += ram_size so I consider it not needed assignment. And actually, this could be calculated in init_pdx() next to max_page to avoid requiring each arch (arm32, arm64, mpu) to set it exactly the same.

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

Quoted 1 line
xen/arch/arm/mpu/mm.c +1 -0
+    for ( unsigned int bank = 0 ; bank < mem->nr_banks; bank++ )

; should be added right after a field, so bank = 0; > + {

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

Quoted 13 lines
xen/arch/arm/mpu/mm.c +13 -0
+        paddr_t bank_start = round_pgup(mem->bank[bank].start);
+        paddr_t bank_size = round_pgdown(mem->bank[bank].size);
+        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);
+    }
+
+    setup_staticheap_mappings();
+
+    total_pages += ram_size >> PAGE_SHIFT;
+    max_page = PFN_DOWN(ram_end);

This is already calculated in init_pdx() after my recent changes

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

Quoted 8 lines
xen/arch/arm/mpu/mm.c +5 -0
+
+    setup_frametable_mappings(ram_start, ram_end);
+
+    init_staticmem_pages();
+    init_sharedmem_pages();
 }
 
 int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)

~Michal

#11 lore raw

Re: [PATCH 3/5] arm/mpu: Implement transient mapping

In reply to Hari Limaye
Quoted 4 lines · Hari Limaye

From: Luca Fancellu <[email protected]>

Add a scheme to distinguish transient MPU regions, to identify MPU regions which will be mapped for a short period of time.

The commit msg lacks description why this is needed.

Quoted 42 lines
---
 xen/arch/arm/include/asm/arm32/mpu.h     |  2 ++
 xen/arch/arm/include/asm/arm64/mpu.h     |  2 ++
 xen/arch/arm/include/asm/mpu/mm.h        | 14 +++++++++++++-
 xen/arch/arm/include/asm/mpu/regions.inc | 19 +++++++++++++++++--
 xen/arch/arm/mpu/mm.c                    | 23 ++++++++++++++---------
 5 files changed, 48 insertions(+), 12 deletions(-)
xen/arch/arm/include/asm/arm32/mpu.h +2 -0
index 0a6930b3a0..9906d98809 100644
@@ -39,6 +39,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    bool transient;
+    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..1d1843eda0 100644
@@ -38,6 +38,8 @@ typedef union {
 typedef struct {
     prbar_t prbar;
     prlar_t prlar;
+    bool transient;
+    uint8_t pad[15]; /* Pad structure to 32 Bytes */
 } pr_t;
 
 #endif /* __ASSEMBLY__ */
xen/arch/arm/include/asm/mpu/mm.h +0 -0
index c32fac8905..56ca411af4 100644

xen/arch/arm/include/asm/mpu/mm.h static inline void context_sync_mpu(void) · line 63

Quoted 4 lines
xen/arch/arm/include/asm/mpu/mm.h +1 -0
@@ -60,6 +60,16 @@ static inline void context_sync_mpu(void)
     isb();
 }
 
+static inline bool region_is_transient(pr_t *pr)

As this is just a read helper, pr could be const?

~Michal

#14 lore raw

Re: [PATCH 4/5] arm/mpu: Implement ioremap_attr for MPU

In reply to Hari Limaye
Quoted 18 lines · Hari Limaye

From: Luca Fancellu <[email protected]>

Introduce helpers (un)map_mm_range() in order to allow the temporary mapping of a range of memory, and use these to implement the function ioremap_attr for MPU systems.

---
 xen/arch/arm/include/asm/mpu/mm.h |  22 +++++
 xen/arch/arm/mpu/mm.c             | 150 ++++++++++++++++++++++++++++--
 2 files changed, 163 insertions(+), 9 deletions(-)
xen/arch/arm/include/asm/mpu/mm.h +0 -0
index 56ca411af4..177550f5bd 100644

xen/arch/arm/include/asm/mpu/mm.h int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, · line 110

Quoted 5 lines
xen/arch/arm/include/asm/mpu/mm.h +2 -0
@@ -106,6 +106,28 @@ int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags,
  */
 pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags);
 
+/*
+ * Maps a temporary range of memory with attributes `flags`; if the range is

Why do you always mention 'temporary' in the context of these functions? What prevents us from using them to map a region for a longer period of time? Also, temporary range is a bit confusing term and should better be replaced with 'Maps temporarily a range of memory ...'

xen/arch/arm/include/asm/mpu/mm.h int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, · line 112

Quoted 2 lines
xen/arch/arm/include/asm/mpu/mm.h +2 -0
+ * already mapped with the same attributes, including an inclusive match, the
+ * existing mapping is returned. This API is intended for mappings that exist

What are the use cases you want to cover to try to map the same range with the same attributes more than once (without unmapping in the meantime)?

xen/arch/arm/include/asm/mpu/mm.h int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags, · line 121

Quoted 9 lines
xen/arch/arm/include/asm/mpu/mm.h +9 -0
+ * transiently for a short period between calls to this function and
+ * `unmap_mm_range`.
+ *
+ * @param start     Base address of the range to map (inclusive).
+ * @param end       Limit address of the range to map (exclusive).
+ * @param flags     Flags for the memory range to map.
+ * @return          Pointer to start of region on success, NULL on error.
+ */
+void *map_mm_range(paddr_t start, paddr_t end, unsigned int flags);

So far, all the MPU related functions use [base, limit) instead of [start, end). Do we see the benefit of diverging here?

~Michal

#18 lore raw

Re: [PATCH 5/5] xen/arm: map static memory on demand

In reply to Hari Limaye
Quoted 7 lines · Hari Limaye

From: Penny Zheng <[email protected]>

In the function init_staticmem_pages we need to have mapped static memory banks for initialization. Unlike on an MMU system, we cannot map the entire RAM on an MPU system as we have a limited number of MPU memory regions. To solve this, transiently map the static memory banks for initialization.

How does this correspond to Arm32 MPU (R52), where all of the RAM will be covered? I don't think 'transient' mappings will be needed there but all of your work targets common (i.e. not only Arm64) MPU code.

~Michal

#15 lore raw

Re: [PATCH 4/5] arm/mpu: Implement ioremap_attr for MPU

In reply to Orzel, Michal

Hi Michal,

Quoted 5 lines
Quoted 1 line

+ * Maps a temporary range of memory with attributes flags; if the range is

Why do you always mention 'temporary' in the context of these functions? What prevents us from using them to map a region for a longer period of time? Also, temporary range is a bit confusing term and should better be replaced with 'Maps temporarily a range of memory ...'

The intended meaning of ’temporary’ in the context of these functions is that the mapping is created, used, and then destroyed in a sequence e.g. where map_domain_page and unmap_domain_page are used. The term transient is possibly more fitting than temporary, and this is the term used in xen/common/page_alloc.c. Nothing actually prevents us from using them to map a region for a longer period of time, it is just that the intended use of the API is for transient mapping. Regarding the confusing comment - noted I will fix this in the next version of the series.

Quoted 4 lines
Quoted 2 lines

+ * already mapped with the same attributes, including an inclusive match, the
+ * existing mapping is returned. This API is intended for mappings that exist

What are the use cases you want to cover to try to map the same range with the same attributes more than once (without unmapping in the meantime)?

The use case here is some places where a memory region is requested that has already been mapped, so we want to simply return the mapping. For example when the fdt is relocated in start_xen this uses a call to xmalloc, which eventually calls map_domain_page for an address in the static heap which has already been mapped.

Quoted 9 lines
Quoted 9 lines

+ * transiently for a short period between calls to this function and
+ * unmap_mm_range.
+ *

+ * @param start     Base address of the range to map (inclusive).
+ * @param end       Limit address of the range to map (exclusive).
+ * @param flags     Flags for the memory range to map.
+ * @return          Pointer to start of region on success, NULL on error.

+ */
+void *map_mm_range(paddr_t start, paddr_t end, unsigned int flags);

Quoted 4 lines

So far, all the MPU related functions use [base, limit) instead of [start, end). Do we see the benefit of diverging here?

~Michal

Noted, I will align this to use [base, limit) in the next version of the series.

Many thanks,
Hari

#19 lore raw

Re: [PATCH 5/5] xen/arm: map static memory on demand

In reply to Orzel, Michal

Hi Michal,

Quoted 12 lines · Michal
Quoted 7 lines · Hari Limaye

From: Penny Zheng <[email protected]>

In the function init_staticmem_pages we need to have mapped static memory banks for initialization. Unlike on an MMU system, we cannot map the entire RAM on an MPU system as we have a limited number of MPU memory regions. To solve this, transiently map the static memory banks for initialization.

Quoted 6 lines

How does this correspond to Arm32 MPU (R52), where all of the RAM will be covered? I don't think 'transient' mappings will be needed there but all of your work targets common (i.e. not only Arm64) MPU code.

~Michal

Hm fair point - when this series was submitted to the ML, I had no knowledge of the intention for all of the RAM to be mapped on Arm32 MPU systems and imagined that things would be more aligned here.

Are happy for this to remain common for now? Or would you prefer it to be split off?

Many thanks,
Hari

#20 lore raw

Re: [PATCH 5/5] xen/arm: map static memory on demand

Quoted 4 lines · Hari Limaye

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

Hi Michal,

Hi Hari, Michal,

Quoted 17 lines
Quoted 17 lines · Michal
Quoted 7 lines · Hari Limaye

From: Penny Zheng <[email protected]>

In the function init_staticmem_pages we need to have mapped static memory banks for initialization. Unlike on an MMU system, we cannot map the entire RAM on an MPU system as we have a limited number of MPU memory regions. To solve this, transiently map the static memory banks for initialization.

How does this correspond to Arm32 MPU (R52), where all of the RAM will be covered? I don't think 'transient' mappings will be needed there but all of your work targets common (i.e. not only Arm64) MPU code.

Quoted 5 lines
Quoted 2 lines

~Michal

Hm fair point - when this series was submitted to the ML, I had no knowledge of the intention for all of the RAM to be mapped on Arm32 MPU systems and imagined that things would be more aligned here.

Are happy for this to remain common for now? Or would you prefer it to be split off?

Let's split here between Arm_32 and Arm_64.

As discussed on Matrix, for Arm_32 we are mapping the entire static memory as a permanent MPU region. In light of Julien's comments and Alejandro's patch, I will revisit the approach in some time. However, I don't want to create a dependency for this series.

We can realign later if needed. :)

- Ayan

Quoted 3 lines

Many thanks,
Hari

#21 lore raw

Re: [PATCH 5/5] xen/arm: map static memory on demand

In reply to Hari Limaye
Quoted 18 lines · Hari Limaye

Hi Michal,

Quoted 16 lines · Michal
Quoted 7 lines · Hari Limaye

From: Penny Zheng <[email protected]>

In the function init_staticmem_pages we need to have mapped static memory banks for initialization. Unlike on an MMU system, we cannot map the entire RAM on an MPU system as we have a limited number of MPU memory regions. To solve this, transiently map the static memory banks for initialization.

How does this correspond to Arm32 MPU (R52), where all of the RAM will be covered? I don't think 'transient' mappings will be needed there but all of your work targets common (i.e. not only Arm64) MPU code.

Quoted 6 lines
Quoted 2 lines

~Michal

Hm fair point - when this series was submitted to the ML, I had no knowledge of the intention for all of the RAM to be mapped on Arm32 MPU systems and imagined that things would be more aligned here.

Are happy for this to remain common for now? Or would you prefer it to be split off?

Let it be common for now. We can revisit it after the release to align on the final approach.

~Michal

#7 lore raw

Re: [PATCH 2/5] arm/mpu: Implement setup_mm for MPU systems

In reply to Orzel, Michal

Hi Michal,

Quoted 3 lines

And actually, this could be calculated
in init_pdx() next to max_page to avoid requiring each arch (arm32, arm64, mpu) to set it exactly the same.

I have not implemented this in this series, as it seems like an unrelated change that should go separately. However if you prefer I can re-spin with this change also?

Cheers,
Hari

#8 lore raw

Re: [PATCH 2/5] arm/mpu: Implement setup_mm for MPU systems

Quoted 7 lines · Hari Limaye

Hi Michal,

Quoted 3 lines

And actually, this could be calculated
in init_pdx() next to max_page to avoid requiring each arch (arm32, arm64, mpu) to set it exactly the same.

I have not implemented this in this series, as it seems like an unrelated change that should go separately. However if you prefer I can re-spin with this change also?

No need, it can be done in the future.

~Michal