Skip to contentSkip to conversation

First chunk for Arm R82 and MPU support

PATCH Superseded Luca Fancellu 2 participants 19 messages Feb 28, 2025 – Mar 4, 2025

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover First chunk for Arm R82 and MPU support

Hi all,

This is the first chunk of work to support MPU and R82 on Xen, this serie reaches the early boot stages until setup_pagetables() which will be renamed in one of the commit on the serie to have a more generic name between MPU and MMU memory management subsystem.

Luca Fancellu (7):
arm/mpu: Add HYPERVISOR_VIRT_START and avoid a check in xen.lds.S arm/mpu: Provide access to the MPU region from the C code arm/mpu: Introduce utility functions for the pr_t type arm/mpu: Provide a constructor for pr_t type
arm/mpu: Introduce MPU memory mapping flags
arm/mpu: Implement early_fdt_map support in MPU systems arm/mpu: Implement setup_mappings for MPU system

Penny Zheng (2):
arm/mpu: Implement virt/maddr conversion in MPU system arm/mpu: Introduce MPU memory region map structure

 xen/arch/arm/Makefile                 |   1 +
 xen/arch/arm/include/asm/arm64/mpu.h  |  92 ++++++++++
 xen/arch/arm/include/asm/mm.h         |  15 +-
 xen/arch/arm/include/asm/mmu/mm.h     |   7 +
 xen/arch/arm/include/asm/mpu/layout.h |   2 +
 xen/arch/arm/include/asm/mpu/mm.h     |  59 +++++++
 xen/arch/arm/include/asm/page.h       |  25 +++
 xen/arch/arm/mmu/setup.c              |   2 +-
 xen/arch/arm/mpu/Makefile             |   2 +
 xen/arch/arm/mpu/mm.c                 | 236 ++++++++++++++++++++++++++
 xen/arch/arm/mpu/setup.c              | 112 ++++++++++++
 xen/arch/arm/setup.c                  |   2 +-
 xen/arch/arm/xen.lds.S                |   2 +
 13 files changed, 548 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/mpu/mm.h
 create mode 100644 xen/arch/arm/mpu/Makefile
 create mode 100644 xen/arch/arm/mpu/mm.c
 create mode 100644 xen/arch/arm/mpu/setup.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/9 arm/mpu: Add HYPERVISOR_VIRT_START and avoid a check in xen.lds.S

The define HYPERVISOR_VIRT_START is required by the common code, even if MPU system doesn't use virtual memory, define it in mpu/layout.h in order to reuse existing code.

Disable a check in the linker script for arm for !MMU systems.

---
 xen/arch/arm/include/asm/mpu/layout.h | 2 ++
 xen/arch/arm/xen.lds.S                | 2 ++
 2 files changed, 4 insertions(+)
xen/arch/arm/include/asm/mpu/layout.h +2 -0
index d6d397f4c2ac..248e55f8882d 100644
@@ -22,6 +22,8 @@
  */
 #define XEN_VIRT_START         _AT(paddr_t, XEN_START_ADDRESS)
 
+#define HYPERVISOR_VIRT_START  XEN_VIRT_START
+
 #endif /* __ARM_MPU_LAYOUT_H__ */
 /*
  * Local variables:
xen/arch/arm/xen.lds.S +2 -0
index bbccff1a0350..4342e54422a7 100644
@@ -239,4 +239,6 @@ ASSERT(IS_ALIGNED(__bss_start,      POINTER_ALIGN), "__bss_start is misaligned")
 ASSERT(IS_ALIGNED(__bss_end,        POINTER_ALIGN), "__bss_end is misaligned")
 /* To simplify the logic in head.S, we want to _end to be page aligned */
 ASSERT(IS_ALIGNED(_end,             PAGE_SIZE), "_end is not page aligned")
+#ifdef CONFIG_MMU
 ASSERT((_end - _start) <= XEN_VIRT_SIZE, "Xen is too big")
+#endif
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/9 arm/mpu: Implement virt/maddr conversion in MPU system

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

virt_to_maddr and maddr_to_virt are used widely in Xen code. So even there is no VMSA in MPU system, we keep the interface in MPU to don't change the existing common code.

In order to do that, move the virt_to_maddr() definition to mmu/mm.h, instead for maddr_to_virt() it's more difficult to isolate it under mmu/ so it will be protected by #ifdef CONFIG_MMU.

Finally implement virt_to_maddr() and maddr_to_virt() for MPU systems under mpu/mm.h, the MPU version of virt/maddr conversion is simple since VA==PA.

---
 xen/arch/arm/include/asm/mm.h     | 13 +++++++------
 xen/arch/arm/include/asm/mmu/mm.h |  7 +++++++
 xen/arch/arm/include/asm/mpu/mm.h | 27 +++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 6 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/mpu/mm.h
xen/arch/arm/include/asm/mm.h +7 -6
index a0d8e5afe977..e7767cdab493 100644
@@ -16,8 +16,10 @@
 
 #if defined(CONFIG_MMU)
 # include <asm/mmu/mm.h>
-#elif !defined(CONFIG_MPU)
-# error "Unknown memory management layout"
+#elif defined(CONFIG_MPU)
+# include <asm/mpu/mm.h>
+#else
+#error "Unknown memory management layout"
 #endif
 
 /* Align Xen to a 2 MiB boundary. */
@@ -261,10 +263,7 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 /* Page-align address and convert to frame number format */
 #define paddr_to_pfn_aligned(paddr)    paddr_to_pfn(PAGE_ALIGN(paddr))
 
-#define virt_to_maddr(va) ({                                        \
-    vaddr_t va_ = (vaddr_t)(va);                                    \
-    (paddr_t)((va_to_par(va_) & PADDR_MASK & PAGE_MASK) | (va_ & ~PAGE_MASK)); \
-})
+#if defined(CONFIG_MMU)
 
 #ifdef CONFIG_ARM_32
 /**
@@ -310,6 +309,8 @@ static inline void *maddr_to_virt(paddr_t ma)
 }
 #endif
 
+#endif /* CONFIG_MMU */
+
 /*
  * Translate a guest virtual address to a machine address.
  * Return the fault information if the translation has failed else 0.
xen/arch/arm/include/asm/mmu/mm.h +7 -0
index f5a00558c47b..5ff2071133ee 100644
@@ -2,6 +2,8 @@
 #ifndef __ARM_MMU_MM_H__
 #define __ARM_MMU_MM_H__
 
+#include <asm/page.h>
+
 /* Non-boot CPUs use this to find the correct pagetables. */
 extern uint64_t init_ttbr;
 
@@ -14,6 +16,11 @@ extern unsigned long directmap_base_pdx;
 
 #define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)
 
+#define virt_to_maddr(va) ({                                                   \
+    vaddr_t va_ = (vaddr_t)(va);                                               \
+    (paddr_t)((va_to_par(va_) & PADDR_MASK & PAGE_MASK) | (va_ & ~PAGE_MASK)); \
+})
+
 /*
  * Print a walk of a page table or p2m
  *
xen/arch/arm/include/asm/mpu/mm.h new file +27 -0
new file mode 100644
index 000000000000..57f1e558fd44
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_MPU_MM__
+#define __ARM_MPU_MM__
+
+#include <xen/macros.h>
+
+#define virt_to_maddr(va) ({  \
+    (paddr_t)va;              \
+})
+
+/* On MPU systems there is no translation, ma == va. */
+static inline void *maddr_to_virt(paddr_t ma)
+{
+    return _p(ma);
+}
+
+#endif /* __ARM_MPU_MM__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/9 arm/mpu: Introduce MPU memory region map structure

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the aarch64 armv8-r architecture.

Introduce the array 'xen_mpumap' that will store a view of the content of the MPU regions.

Introduce MAX_MPU_REGIONS macro that uses the value of NUM_MPU_REGIONS_MASK just for clarity, because using the latter as number of elements of the xen_mpumap array might be misleading.

---
 xen/arch/arm/Makefile                |  1 +
 xen/arch/arm/include/asm/arm64/mpu.h | 38 ++++++++++++++++++++++++++++
 xen/arch/arm/mpu/Makefile            |  1 +
 xen/arch/arm/mpu/mm.c                | 23 +++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/arm/mpu/Makefile
 create mode 100644 xen/arch/arm/mpu/mm.c
xen/arch/arm/Makefile +1 -0
index 43ab5e8f2550..fb0948f067bd 100644
@@ -1,6 +1,7 @@
 obj-$(CONFIG_ARM_32) += arm32/
 obj-$(CONFIG_ARM_64) += arm64/
 obj-$(CONFIG_MMU) += mmu/
+obj-$(CONFIG_MPU) += mpu/
 obj-$(CONFIG_ACPI) += acpi/
 obj-$(CONFIG_HAS_PCI) += pci/
 ifneq ($(CONFIG_NO_PLAT),y)
xen/arch/arm/include/asm/arm64/mpu.h +38 -0
index f8a029f1a135..38dbf5b2255a 100644
@@ -13,6 +13,44 @@
 #define NUM_MPU_REGIONS_SHIFT   8
 #define NUM_MPU_REGIONS         (_AC(1, UL) << NUM_MPU_REGIONS_SHIFT)
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
+
+#define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
+
+#ifndef __ASSEMBLY__
+
+/* Protection Region Base Address Register */
+typedef union {
+    struct __packed {
+        unsigned long xn:2;       /* Execute-Never */
+        unsigned long ap:2;       /* Acess Permission */
+        unsigned long sh:2;       /* Sharebility */
+        unsigned long base:46;    /* Base Address */
+        unsigned long pad:12;
+    } reg;
+    uint64_t bits;
+} prbar_t;
+
+/* Protection Region Limit Address Register */
+typedef union {
+    struct __packed {
+        unsigned long en:1;     /* Region enable */
+        unsigned long ai:3;     /* Memory Attribute Index */
+        unsigned long ns:1;     /* Not-Secure */
+        unsigned long res:1;    /* Reserved 0 by hardware */
+        unsigned long limit:46; /* Limit Address */
+        unsigned long pad:12;
+    } reg;
+    uint64_t bits;
+} prlar_t;
+
+/* MPU Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
 #endif /* __ARM64_MPU_H__ */
 
 /*
xen/arch/arm/mpu/Makefile new file +1 -0
new file mode 100644
index 000000000000..b18cec483671
@@ -0,0 +1 @@
+obj-y += mm.o
xen/arch/arm/mpu/mm.c new file +23 -0
new file mode 100644
index 000000000000..3ca609ff80cc
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * xen/arch/arm/mpu/mm.c
+ *
+ * MPU-based memory managment code for Armv8-R AArch64.
+ *
+ * Copyright (C) 2023 Arm Ltd.
+ *
+ */
+
+#include <asm/arm64/mpu.h>
+
+/* EL2 Xen MPU memory region mapping table. */
+pr_t xen_mpumap[MAX_MPU_REGIONS];
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 3/9] arm/mpu: Introduce MPU memory region map structure

Hi Luca,

Quoted 46 lines · Luca Fancellu

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]>

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the aarch64 armv8-r architecture.

Introduce the array 'xen_mpumap' that will store a view of the content of the MPU regions.

Introduce MAX_MPU_REGIONS macro that uses the value of NUM_MPU_REGIONS_MASK just for clarity, because using the latter as number of elements of the xen_mpumap array might be misleading.

---
  xen/arch/arm/Makefile                |  1 +
  xen/arch/arm/include/asm/arm64/mpu.h | 38 ++++++++++++++++++++++++++++
  xen/arch/arm/mpu/Makefile            |  1 +
  xen/arch/arm/mpu/mm.c                | 23 +++++++++++++++++
  4 files changed, 63 insertions(+)

create mode 100644 xen/arch/arm/mpu/Makefile
create mode 100644 xen/arch/arm/mpu/mm.c

xen/arch/arm/Makefile +1 -0
index 43ab5e8f2550..fb0948f067bd 100644
@@ -1,6 +1,7 @@
  obj-$(CONFIG_ARM_32) += arm32/
  obj-$(CONFIG_ARM_64) += arm64/
  obj-$(CONFIG_MMU) += mmu/
+obj-$(CONFIG_MPU) += mpu/
  obj-$(CONFIG_ACPI) += acpi/
  obj-$(CONFIG_HAS_PCI) += pci/
  ifneq ($(CONFIG_NO_PLAT),y)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index f8a029f1a135..38dbf5b2255a 100644

xen/arch/arm/include/asm/arm64/mpu.h line 17

Quoted 5 lines
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
@@ -13,6 +13,44 @@
  #define NUM_MPU_REGIONS_SHIFT   8
  #define NUM_MPU_REGIONS         (_AC(1, UL) << NUM_MPU_REGIONS_SHIFT)
  #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
+
+#define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK

Not really a comment, but a note. The above needs to be common between arm64 and arm32.

I had moved this in

"[PATCH 3/5] xen/arm: mpu: Move some of the definitions to common file" .

Depending on whose patch goes in first, this code needs to be moved in the initial patch so that there is less code movement in the latter patch.

Quoted 53 lines
xen/arch/arm/include/asm/arm64/mpu.h +36 -0
+
+#ifndef __ASSEMBLY__
+
+/* Protection Region Base Address Register */
+typedef union {
+    struct __packed {
+        unsigned long xn:2;       /* Execute-Never */
+        unsigned long ap:2;       /* Acess Permission */
+        unsigned long sh:2;       /* Sharebility */
+        unsigned long base:46;    /* Base Address */
+        unsigned long pad:12;
+    } reg;
+    uint64_t bits;
+} prbar_t;
+
+/* Protection Region Limit Address Register */
+typedef union {
+    struct __packed {
+        unsigned long en:1;     /* Region enable */
+        unsigned long ai:3;     /* Memory Attribute Index */
+        unsigned long ns:1;     /* Not-Secure */
+        unsigned long res:1;    /* Reserved 0 by hardware */
+        unsigned long limit:46; /* Limit Address */
+        unsigned long pad:12;
+    } reg;
+    uint64_t bits;
+} prlar_t;
+
+/* MPU Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
  #endif /* __ARM64_MPU_H__ */
 
  /*
xen/arch/arm/mpu/Makefile new file +1 -0
new file mode 100644
index 000000000000..b18cec483671
@@ -0,0 +1 @@
+obj-y += mm.o
xen/arch/arm/mpu/mm.c new file +1 -0
new file mode 100644
index 000000000000..3ca609ff80cc
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c new file +10 -0
+/*
+ * xen/arch/arm/mpu/mm.c
+ *
+ * MPU-based memory managment code for Armv8-R AArch64.
+ *
+ * Copyright (C) 2023 Arm Ltd.
+ *
+ */
+
+#include <asm/arm64/mpu.h>

Can we just include <asm/mpu.h> ?

The arm64/arm32 specific includes will occur in the header file.

Quoted 13 lines
xen/arch/arm/mpu/mm.c new file +12 -1
+
+/* EL2 Xen MPU memory region mapping table. */
+pr_t xen_mpumap[MAX_MPU_REGIONS];
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--
Quoted 1 line

2.34.1

- Ayan

Quoted 2 lines
#5 lore raw

Re: [PATCH 3/9] arm/mpu: Introduce MPU memory region map structure

Hi Ayan,

Quoted 59 lines · Ayan Kumar Halder

Hi Luca,

Quoted 46 lines · Luca Fancellu

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]>

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the aarch64 armv8-r architecture.

Introduce the array 'xen_mpumap' that will store a view of the content of the MPU regions.

Introduce MAX_MPU_REGIONS macro that uses the value of NUM_MPU_REGIONS_MASK just for clarity, because using the latter as number of elements of the xen_mpumap array might be misleading.

---
 xen/arch/arm/Makefile                |  1 +
 xen/arch/arm/include/asm/arm64/mpu.h | 38 ++++++++++++++++++++++++++++
 xen/arch/arm/mpu/Makefile            |  1 +
 xen/arch/arm/mpu/mm.c                | 23 +++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/arm/mpu/Makefile
 create mode 100644 xen/arch/arm/mpu/mm.c
xen/arch/arm/Makefile +1 -0
index 43ab5e8f2550..fb0948f067bd 100644
@@ -1,6 +1,7 @@
 obj-$(CONFIG_ARM_32) += arm32/
 obj-$(CONFIG_ARM_64) += arm64/
 obj-$(CONFIG_MMU) += mmu/
+obj-$(CONFIG_MPU) += mpu/
 obj-$(CONFIG_ACPI) += acpi/
 obj-$(CONFIG_HAS_PCI) += pci/
 ifneq ($(CONFIG_NO_PLAT),y)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index f8a029f1a135..38dbf5b2255a 100644

xen/arch/arm/include/asm/arm64/mpu.h line 17

Quoted 5 lines
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
@@ -13,6 +13,44 @@
 #define NUM_MPU_REGIONS_SHIFT   8
 #define NUM_MPU_REGIONS         (_AC(1, UL) << NUM_MPU_REGIONS_SHIFT)
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
+
+#define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK

Not really a comment, but a note. The above needs to be common between arm64 and arm32.

Quoted 8 lines

I had moved this in

"[PATCH 3/5] xen/arm: mpu: Move some of the definitions to common file" .

Depending on whose patch goes in first, this code needs to be moved in the initial patch so that there is less code movement in the latter patch.

I will move them when your patch will be merged, if your patch is merged first. I will wait for comments on this one by maintainers so that if I have to do changes I will do at once without respinning the series multiple time.

Cheers,
Luca

#6 lore raw

PATCH 4/9 arm/mpu: Provide access to the MPU region from the C code

In reply to Luca Fancellu

Implement some utility function in order to access the MPU regions from the C world.

---
 xen/arch/arm/include/asm/arm64/mpu.h |   1 +
 xen/arch/arm/include/asm/mpu/mm.h    |  29 ++++++
 xen/arch/arm/mpu/mm.c                | 130 ++++++++++++++++++++++++++-
 3 files changed, 159 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
index 38dbf5b2255a..c63a9e6e5455 100644
@@ -9,6 +9,7 @@
 #define MPU_REGION_SHIFT  6
 #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
 #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
+#define MPU_REGION_RES0   (0xFFFULL << 52)
 
 #define NUM_MPU_REGIONS_SHIFT   8
 #define NUM_MPU_REGIONS         (_AC(1, UL) << NUM_MPU_REGIONS_SHIFT)
xen/arch/arm/include/asm/mpu/mm.h +29 -0
index 57f1e558fd44..1287a0f625b5 100644
@@ -5,6 +5,12 @@
 
 #include <xen/macros.h>
 
+#if defined(CONFIG_ARM_64)
+# include <asm/arm64/mpu.h>
+#else
+# error "unknown ARM variant"
+#endif
+
 #define virt_to_maddr(va) ({  \
     (paddr_t)va;              \
 })
@@ -15,6 +21,29 @@ static inline void *maddr_to_virt(paddr_t ma)
     return _p(ma);
 }
 
+/* Utility function to be used whenever MPU regions are modified */
+static inline void context_sync_mpu(void)
+{
+    /*
+     * ARM DDI 0600B.a, C1.7.1
+     * Writes to MPU registers are only guaranteed to be visible following a
+     * Context synchronization event and DSB operation.
+     */
+    dsb(sy);
+    isb();
+}
+
+/*
+ * The following API require context_sync_mpu() after being used to modifiy MPU
+ * regions:
+ *  - write_protection_region
+ */
+
+/* Reads the MPU region with index 'sel' from the HW */
+extern void read_protection_region(pr_t *pr_read, uint8_t sel);
+/* Writes the MPU region with index 'sel' to the HW */
+extern void write_protection_region(const pr_t *pr_write, uint8_t sel);
+
 #endif /* __ARM_MPU_MM__ */
 
 /*
xen/arch/arm/mpu/mm.c +129 -1

140 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

PATCH 5/9 arm/mpu: Introduce utility functions for the pr_t type

In reply to Luca Fancellu

Introduce few utility function to manipulate and handle the pr_t type.

---
 xen/arch/arm/include/asm/arm64/mpu.h | 36 ++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +36 -0
index c63a9e6e5455..3a09339818a0 100644
@@ -50,6 +50,42 @@ typedef struct {
     prlar_t prlar;
 } pr_t;
 
+/* Set base address of MPU protection region(pr_t). */
+static inline void pr_set_base(pr_t *pr, paddr_t base)
+{
+    pr->prbar.reg.base = (base >> MPU_REGION_SHIFT);
+}
+
+/* Set limit address of MPU protection region(pr_t). */
+static inline void pr_set_limit(pr_t *pr, paddr_t limit)
+{
+    pr->prlar.reg.limit = ((limit - 1) >> MPU_REGION_SHIFT);
+}
+
+/*
+ * Access to get base address of MPU protection region(pr_t).
+ * The base address shall be zero extended.
+ */
+static inline paddr_t pr_get_base(pr_t *pr)
+{
+    return (paddr_t)(pr->prbar.reg.base << MPU_REGION_SHIFT);
+}
+
+/*
+ * Access to get limit address of MPU protection region(pr_t).
+ * The limit address shall be concatenated with 0x3f.
+ */
+static inline paddr_t pr_get_limit(pr_t *pr)
+{
+    return (paddr_t)((pr->prlar.reg.limit << MPU_REGION_SHIFT)
+                     | ~MPU_REGION_MASK);
+}
+
+static inline bool region_is_valid(pr_t *pr)
+{
+    return pr->prlar.reg.en;
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* __ARM64_MPU_H__ */
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH 5/9] arm/mpu: Introduce utility functions for the pr_t type

Hi Luca,

Quoted 45 lines · Luca Fancellu

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.

Introduce few utility function to manipulate and handle the pr_t type.

---
  xen/arch/arm/include/asm/arm64/mpu.h | 36 ++++++++++++++++++++++++++++
  1 file changed, 36 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +26 -0
index c63a9e6e5455..3a09339818a0 100644
@@ -50,6 +50,42 @@ typedef struct {
      prlar_t prlar;
  } pr_t;
 
+/* Set base address of MPU protection region(pr_t). */
+static inline void pr_set_base(pr_t *pr, paddr_t base)
+{
+    pr->prbar.reg.base = (base >> MPU_REGION_SHIFT);
+}
+
+/* Set limit address of MPU protection region(pr_t). */
+static inline void pr_set_limit(pr_t *pr, paddr_t limit)
+{
+    pr->prlar.reg.limit = ((limit - 1) >> MPU_REGION_SHIFT);
+}
+
+/*
+ * Access to get base address of MPU protection region(pr_t).
+ * The base address shall be zero extended.
+ */
+static inline paddr_t pr_get_base(pr_t *pr)
+{
+    return (paddr_t)(pr->prbar.reg.base << MPU_REGION_SHIFT);
+}
+
+/*
+ * Access to get limit address of MPU protection region(pr_t).
+ * The limit address shall be concatenated with 0x3f.
+ */
+static inline paddr_t pr_get_limit(pr_t *pr)

xen/arch/arm/include/asm/arm64/mpu.h typedef struct { · line 88

Quoted 10 lines
xen/arch/arm/include/asm/arm64/mpu.h +10 -0
+{
+    return (paddr_t)((pr->prlar.reg.limit << MPU_REGION_SHIFT)
+                     | ~MPU_REGION_MASK);
+}
+
+static inline bool region_is_valid(pr_t *pr)
+{
+    return pr->prlar.reg.en;
+}
+

This is common between arm32 and arm64. This can be moved to a common file.

- Ayan

Quoted 7 lines
xen/arch/arm/include/asm/arm64/mpu.h +0 -1
  #endif /* __ASSEMBLY__ */
 
  #endif /* __ARM64_MPU_H__ */
--

2.34.1

#9 lore raw

PATCH 6/9 arm/mpu: Provide a constructor for pr_t type

In reply to Luca Fancellu

Provide a function that creates a pr_t object from a memory range and some attributes.

---
 xen/arch/arm/include/asm/arm64/mpu.h | 15 ++++++
 xen/arch/arm/include/asm/mpu/mm.h    |  3 ++
 xen/arch/arm/mpu/mm.c                | 73 ++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +15 -0
index 3a09339818a0..dce77da60110 100644
@@ -17,6 +17,21 @@
 
 #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 
+/* Access permission attributes. */
+/* Read/Write at EL2, No Access at EL1/EL0. */
+#define AP_RW_EL2 0x0
+
+/*
+ * Excute never.
+ * Stage 1 EL2 translation regime.
+ * XN[1] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ * Stage 2 EL1/EL0 translation regime.
+ * XN[0] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ */
+#define XN_ENABLED     0x2
+
 #ifndef __ASSEMBLY__
 
 /* Protection Region Base Address Register */
xen/arch/arm/include/asm/mpu/mm.h +3 -0
index 1287a0f625b5..e234f6c26193 100644
@@ -44,6 +44,9 @@ extern void read_protection_region(pr_t *pr_read, uint8_t sel);
 /* Writes the MPU region with index 'sel' to the HW */
 extern void write_protection_region(const pr_t *pr_write, uint8_t sel);
 
+/* Creates a pr_t entry for the MPU data structure */
+extern pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr);
+
 #endif /* __ARM_MPU_MM__ */
 
 /*
xen/arch/arm/mpu/mm.c +73 -0

85 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH 6/9] arm/mpu: Provide a constructor for pr_t type

Hi Luca,

Quoted 18 lines · Luca Fancellu

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.

Provide a function that creates a pr_t object from a memory range and some attributes.

---
  xen/arch/arm/include/asm/arm64/mpu.h | 15 ++++++
  xen/arch/arm/include/asm/mpu/mm.h    |  3 ++
  xen/arch/arm/mpu/mm.c                | 73 ++++++++++++++++++++++++++++
  3 files changed, 91 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 3a09339818a0..dce77da60110 100644

xen/arch/arm/include/asm/arm64/mpu.h line 22

Quoted 6 lines
xen/arch/arm/include/asm/arm64/mpu.h +3 -0
@@ -17,6 +17,21 @@
 
  #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 
+/* Access permission attributes. */
+/* Read/Write at EL2, No Access at EL1/EL0. */
+#define AP_RW_EL2 0x0

This is common as well between arm64 and arm32.

xen/arch/arm/include/asm/arm64/mpu.h line 33

Quoted 11 lines
xen/arch/arm/include/asm/arm64/mpu.h +11 -0
+
+/*
+ * Excute never.
+ * Stage 1 EL2 translation regime.
+ * XN[1] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ * Stage 2 EL1/EL0 translation regime.
+ * XN[0] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ */
+#define XN_ENABLED     0x2

This seems incorrect.

As per ARM DDI 0600A.d ID120821, G1.3.19 PRBAR<n>_EL2 (armv8 R64 supplement)

0b00 Execution of instructions fetched from the region is permitted.

0b01 Execution of instructions fetched from the region is not permitted.

This holds true for 32-bit as well (except for the fact that XN is denoted by 1-bit).

So the correct definition is

#define XN_ENABLED 0x0

And this is common between arm32/64 , thus it can be moved to common file as well.

Quoted 96 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+
  #ifndef __ASSEMBLY__
 
  /* Protection Region Base Address Register */
xen/arch/arm/include/asm/mpu/mm.h +3 -0
index 1287a0f625b5..e234f6c26193 100644
@@ -44,6 +44,9 @@ extern void read_protection_region(pr_t *pr_read, uint8_t sel);
  /* Writes the MPU region with index 'sel' to the HW */
  extern void write_protection_region(const pr_t *pr_write, uint8_t sel);
 
+/* Creates a pr_t entry for the MPU data structure */
+extern pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr);
+
  #endif /* __ARM_MPU_MM__ */
 
  /*
xen/arch/arm/mpu/mm.c +63 -0
index bb8e0c546e7b..fb94f5d1d93d 100644
@@ -9,6 +9,7 @@
   */
 
  #include <asm/mpu/mm.h>
+#include <asm/page.h>
  #include <asm/sysregs.h>
 
  /* EL2 Xen MPU memory region mapping table. */
@@ -141,6 +142,78 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
      }
  }
 
+/*
+ * Standard entry for building up the structure of MPU memory region(pr_t).
+ * It is equivalent to mfn_to_xen_entry in MMU system.
+ * Base and limit refer to exclusive range [start, limit].
+ */
+pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr)
+{
+    prbar_t prbar;
+    prlar_t prlar;
+    pr_t region;
+
+    /* Build up value for PRBAR_EL2. */
+    prbar = (prbar_t) {
+        .reg = {
+            .ap = AP_RW_EL2,  /* Read/Write at EL2, no access at EL1/EL0. */
+            .xn = XN_ENABLED, /* No need to execute outside .text */
+        }};
+
+    switch ( attr )
+    {
+    case MT_NORMAL_NC:
+        /*
+         * ARM ARM: Overlaying the shareability attribute (DDI
+         * 0406C.b B3-1376 to 1377)
+         *
+         * A memory region with a resultant memory type attribute of normal,
+         * and a resultant cacheability attribute of Inner non-cacheable,
+         * outer non-cacheable, must have a resultant shareability attribute
+         * of outer shareable, otherwise shareability is UNPREDICTABLE.
+         *
+         * On ARMv8 sharability is ignored and explicitly treated as outer
+         * shareable for normal inner non-cacheable, outer non-cacheable.
+         */
+        prbar.reg.sh = LPAE_SH_OUTER;
+        break;
+    case MT_DEVICE_nGnRnE:
+    case MT_DEVICE_nGnRE:
+        /*
+         * Shareability is ignored for non-normal memory, Outer is as
+         * good as anything.
+         *
+         * On ARMv8 sharability is ignored and explicitly treated as outer
+         * shareable for any device memory type.
+         */
+        prbar.reg.sh = LPAE_SH_OUTER;
+        break;
+    default:
+        /* Xen mappings are SMP coherent */
+        prbar.reg.sh = LPAE_SH_INNER;
+    }
+
+    /* Build up value for PRLAR_EL2. */
+    prlar = (prlar_t) {
+        .reg = {
+            .ns = 0,        /* Hyp mode is in secure world */
+            .ai = attr,
+            .en = 1,        /* Region enabled */
+        }};
+
+    /* Build up MPU memory region. */
+    region = (pr_t) {
+        .prbar = prbar,

xen/arch/arm/mpu/mm.c void write_protection_region(const pr_t *pr_write, uint8_t sel) · line 216

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+        .prlar = prlar,
+    };
+
+    /* Set base address and limit address. */
+    pr_set_base(&region, base);
+    pr_set_limit(&region, limit);
+
+    return region;
+}
+

- Ayan

Quoted 7 lines
xen/arch/arm/mpu/mm.c +0 -1
  /*
   * Local variables:
   * mode: C
--

2.34.1

#11 lore raw

Re: [PATCH 6/9] arm/mpu: Provide a constructor for pr_t type

Quoted 52 lines · Ayan Kumar Halder

Hi Luca,

Quoted 18 lines · Luca Fancellu

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.

Provide a function that creates a pr_t object from a memory range and some attributes.

---
 xen/arch/arm/include/asm/arm64/mpu.h | 15 ++++++
 xen/arch/arm/include/asm/mpu/mm.h    |  3 ++
 xen/arch/arm/mpu/mm.c                | 73 ++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 3a09339818a0..dce77da60110 100644

xen/arch/arm/include/asm/arm64/mpu.h line 22

Quoted 6 lines
xen/arch/arm/include/asm/arm64/mpu.h +3 -0
@@ -17,6 +17,21 @@
 
 #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 
+/* Access permission attributes. */
+/* Read/Write at EL2, No Access at EL1/EL0. */
+#define AP_RW_EL2 0x0

This is common as well between arm64 and arm32.

xen/arch/arm/include/asm/arm64/mpu.h line 33

Quoted 11 lines
xen/arch/arm/include/asm/arm64/mpu.h +11 -0
+
+/*
+ * Excute never.
+ * Stage 1 EL2 translation regime.
+ * XN[1] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ * Stage 2 EL1/EL0 translation regime.
+ * XN[0] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ */
+#define XN_ENABLED     0x2

This seems incorrect.

As per ARM DDI 0600A.d ID120821, G1.3.19 PRBAR<n>_EL2 (armv8 R64 supplement)

0b00 Execution of instructions fetched from the region is permitted.

0b01 Execution of instructions fetched from the region is not permitted.

Quoted 7 lines

This holds true for 32-bit as well (except for the fact that XN is denoted by 1-bit).

So the correct definition is

#define XN_ENABLED 0x0

And this is common between arm32/64 , thus it can be moved to common file as well.

Ok maybe my understanding is wrong, but from the specifications:

XN, bits [1:0]

Execute Never. For
• Stage 1 EL2 translation regime and
• Stage 2 EL1&0 translation regime when FEAT_XNX is not implemented

XN[1] determines whether execution of the instructions fetched from the MPU memory region is permitted. In this case, XN[0] is RES0

For stage 2 EL1&0 translation regime when FEAT_XNX is implemented, the behavior of XN[1:0] is same as that defined by VMSAv8-64 for EL1&0 stage 2 translation table XN[1:0],bits[54:53] field in Armv8-A architecture.
0b00 Execution of instructions fetched from the region is permitted.
0b01 Execution of instructions fetched from the region is not permitted.
The reset behavior of this field is:
• On a Warm reset, this field resets to an architecturally UNKNOWN value.

So my understanding is that Stage 1 EL2 regime XN=1 means execution of instructions fetched from the region is not permitted, and when that bit is 1, the XN[0] is RES0

Cheers,
Luca

#12 lore raw

PATCH 7/9 arm/mpu: Introduce MPU memory mapping flags

In reply to Luca Fancellu

Introduce the MPU memory mapping flags in asm/page.h.

---
 xen/arch/arm/include/asm/page.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
xen/arch/arm/include/asm/page.h +25 -0
index 69f817d1e68a..22f7d2c6cb30 100644
@@ -62,6 +62,7 @@
 
 #define MAIRVAL (MAIR1VAL << 32 | MAIR0VAL)
 
+#ifdef CONFIG_MMU
 /*
  * Layout of the flags used for updating the hypervisor page tables
  *
@@ -90,6 +91,30 @@
 #define _PAGE_CONTIG_BIT    8
 #define _PAGE_CONTIG        (1U << _PAGE_CONTIG_BIT)
 
+#else /* !CONFIG_MMU */
+
+/*
+ * Layout of the flags used for updating MPU memory region attributes
+ * [0:2] Memory attribute Index
+ * [3:4] Execute Never
+ * [5:6] Access Permission
+ * [7]   Region Present
+ */
+#define _PAGE_AI_BIT            0
+#define _PAGE_XN_BIT            3
+#define _PAGE_AP_BIT            5
+#define _PAGE_PRESENT_BIT       7
+#define _PAGE_AI                (7U << _PAGE_AI_BIT)
+#define _PAGE_XN                (2U << _PAGE_XN_BIT)
+#define _PAGE_RO                (2U << _PAGE_AP_BIT)
+#define _PAGE_PRESENT           (1U << _PAGE_PRESENT_BIT)
+#define PAGE_AI_MASK(x)         (((x) >> _PAGE_AI_BIT) & 0x7U)
+#define PAGE_XN_MASK(x)         (((x) >> _PAGE_XN_BIT) & 0x3U)
+#define PAGE_AP_MASK(x)         (((x) >> _PAGE_AP_BIT) & 0x3U)
+#define PAGE_RO_MASK(x)         (((x) >> _PAGE_AP_BIT) & 0x2U)
+
+#endif /* CONFIG_MMU */
+
 /*
  * _PAGE_DEVICE and _PAGE_NORMAL are convenience defines. They are not
  * meant to be used outside of this header.
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH 7/9] arm/mpu: Introduce MPU memory mapping flags

Hi Luca,

Quoted 37 lines · Luca Fancellu

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.

Introduce the MPU memory mapping flags in asm/page.h.

---
  xen/arch/arm/include/asm/page.h | 25 +++++++++++++++++++++++++
  1 file changed, 25 insertions(+)
xen/arch/arm/include/asm/page.h +12 -0
index 69f817d1e68a..22f7d2c6cb30 100644
@@ -62,6 +62,7 @@
 
  #define MAIRVAL (MAIR1VAL << 32 | MAIR0VAL)
 
+#ifdef CONFIG_MMU
  /*
   * Layout of the flags used for updating the hypervisor page tables
   *
@@ -90,6 +91,30 @@
  #define _PAGE_CONTIG_BIT    8
  #define _PAGE_CONTIG        (1U << _PAGE_CONTIG_BIT)
 
+#else /* !CONFIG_MMU */
+
+/*
+ * Layout of the flags used for updating MPU memory region attributes
+ * [0:2] Memory attribute Index
+ * [3:4] Execute Never
+ * [5:6] Access Permission
+ * [7]   Region Present
+ */
+#define _PAGE_AI_BIT            0
+#define _PAGE_XN_BIT            3

xen/arch/arm/include/asm/page.h line 114

Quoted 10 lines
xen/arch/arm/include/asm/page.h +10 -0
+#define _PAGE_AP_BIT            5
+#define _PAGE_PRESENT_BIT       7
+#define _PAGE_AI                (7U << _PAGE_AI_BIT)
+#define _PAGE_XN                (2U << _PAGE_XN_BIT)
+#define _PAGE_RO                (2U << _PAGE_AP_BIT)
+#define _PAGE_PRESENT           (1U << _PAGE_PRESENT_BIT)
+#define PAGE_AI_MASK(x)         (((x) >> _PAGE_AI_BIT) & 0x7U)
+#define PAGE_XN_MASK(x)         (((x) >> _PAGE_XN_BIT) & 0x3U)
+#define PAGE_AP_MASK(x)         (((x) >> _PAGE_AP_BIT) & 0x3U)
+#define PAGE_RO_MASK(x)         (((x) >> _PAGE_AP_BIT) & 0x2U)

Can you point out to the references (in some Arm doc or somewhere) from where you have picked these values ?

Also, AFAICS you are not using these macros in your current patch serie. So can you send this patch later (along with another patch showing the usage).

- Ayan

Quoted 10 lines
xen/arch/arm/include/asm/page.h +3 -1
+
+#endif /* CONFIG_MMU */
+
  /*
   * _PAGE_DEVICE and _PAGE_NORMAL are convenience defines. They are not
   * meant to be used outside of this header.
--

2.34.1

#14 lore raw

Re: [PATCH 7/9] arm/mpu: Introduce MPU memory mapping flags

Hi Ayan,

Quoted 55 lines · Ayan Kumar Halder

Hi Luca,

Quoted 37 lines · Luca Fancellu

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.

Introduce the MPU memory mapping flags in asm/page.h.

---
 xen/arch/arm/include/asm/page.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
xen/arch/arm/include/asm/page.h +12 -0
index 69f817d1e68a..22f7d2c6cb30 100644
@@ -62,6 +62,7 @@
 
 #define MAIRVAL (MAIR1VAL << 32 | MAIR0VAL)
 
+#ifdef CONFIG_MMU
 /*
  * Layout of the flags used for updating the hypervisor page tables
  *
@@ -90,6 +91,30 @@
 #define _PAGE_CONTIG_BIT    8
 #define _PAGE_CONTIG        (1U << _PAGE_CONTIG_BIT)
 
+#else /* !CONFIG_MMU */
+
+/*
+ * Layout of the flags used for updating MPU memory region attributes
+ * [0:2] Memory attribute Index
+ * [3:4] Execute Never
+ * [5:6] Access Permission
+ * [7]   Region Present
+ */
+#define _PAGE_AI_BIT            0
+#define _PAGE_XN_BIT            3

xen/arch/arm/include/asm/page.h line 114

Quoted 10 lines
xen/arch/arm/include/asm/page.h +10 -0
+#define _PAGE_AP_BIT            5
+#define _PAGE_PRESENT_BIT       7
+#define _PAGE_AI                (7U << _PAGE_AI_BIT)
+#define _PAGE_XN                (2U << _PAGE_XN_BIT)
+#define _PAGE_RO                (2U << _PAGE_AP_BIT)
+#define _PAGE_PRESENT           (1U << _PAGE_PRESENT_BIT)
+#define PAGE_AI_MASK(x)         (((x) >> _PAGE_AI_BIT) & 0x7U)
+#define PAGE_XN_MASK(x)         (((x) >> _PAGE_XN_BIT) & 0x3U)
+#define PAGE_AP_MASK(x)         (((x) >> _PAGE_AP_BIT) & 0x3U)
+#define PAGE_RO_MASK(x)         (((x) >> _PAGE_AP_BIT) & 0x2U)

Can you point out to the references (in some Arm doc or somewhere) from where you have picked these values ?

Quoted 1 line

Also, AFAICS you are not using these macros in your current patch serie. So can you send this patch later (along with another patch showing the usage).

I’ve used all of them in the following patches, the only one not used is _PAGE_PRESENT, but if possible I would like to keep it there as it will be used on the MPU subsystem management in order to signal to the subsystem if the mapping is to be created etc, so here we are trying to use the same interfaces of MMU in order to reuse the most of the common code.

Cheers,
Luca

#15 lore raw

PATCH 8/9 arm/mpu: Implement early_fdt_map support in MPU systems

In reply to Luca Fancellu

Implement early_fdt_map() function, that is responsible to map the device tree blob in the early stages of the boot process, since at this stage the MPU C data structure are not yet initialised, it is using low level APIs to write into the MPU registers at a fixed MPU region number.

The MPU memory management is designed to work on pages of PAGE_SIZE in order to reuse helpers and macros already available on the Xen memory management system.

---
 xen/arch/arm/mpu/Makefile |  1 +
 xen/arch/arm/mpu/setup.c  | 72 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 xen/arch/arm/mpu/setup.c
xen/arch/arm/mpu/Makefile +1 -0
index b18cec483671..04df0b2ee760 100644
@@ -1 +1,2 @@
 obj-y += mm.o
+obj-y += setup.init.o
xen/arch/arm/mpu/setup.c new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH 8/9] arm/mpu: Implement early_fdt_map support in MPU systems

Hi Luca,

Quoted 61 lines · Luca Fancellu

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.

Implement early_fdt_map() function, that is responsible to map the device tree blob in the early stages of the boot process, since at this stage the MPU C data structure are not yet initialised, it is using low level APIs to write into the MPU registers at a fixed MPU region number.

The MPU memory management is designed to work on pages of PAGE_SIZE in order to reuse helpers and macros already available on the Xen memory management system.

---
  xen/arch/arm/mpu/Makefile |  1 +
  xen/arch/arm/mpu/setup.c  | 72 +++++++++++++++++++++++++++++++++++++++
  2 files changed, 73 insertions(+)

create mode 100644 xen/arch/arm/mpu/setup.c

xen/arch/arm/mpu/Makefile +1 -0
index b18cec483671..04df0b2ee760 100644
@@ -1 +1,2 @@
  obj-y += mm.o
+obj-y += setup.init.o
xen/arch/arm/mpu/setup.c new file +28 -0
new file mode 100644
index 000000000000..290baaca9fd7
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * xen/arch/arm/mpu/setup.c
+ *
+ * MPU system boot code for Armv8-R AArch64.
+ *
+ */
+
+#include <xen/bootfdt.h>
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/mm.h>
+
+/* Needs to be kept in sync with the regions programmed in arm64/mpu/head.S */
+#define EARLY_FDT_MAP_REGION_NUMBER 6
+
+void * __init early_fdt_map(paddr_t fdt_paddr)
+{
+    /* Map at least a page containing the DTB address, exclusive range */
+    paddr_t base_paddr = round_pgdown(fdt_paddr);
+    paddr_t end_paddr = round_pgup(fdt_paddr + sizeof(struct fdt_header));
+    unsigned int flags = PAGE_HYPERVISOR_RO;
+    void *fdt_virt = (void *)fdt_paddr; /* virt == paddr for MPU */
+    pr_t fdt_region;
+
+    /*
+     * Check whether the physical FDT address is set and meets the minimum
+     * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be at

xen/arch/arm/mpu/setup.c line 38

Quoted 10 lines
xen/arch/arm/mpu/setup.c new file +10 -0
+     * least 8 bytes so that we always access the magic and size fields
+     * of the FDT header after mapping the first chunk, double check if
+     * that is indeed the case.
+     */
+    BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
+    if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN )
+        return NULL;
+
+    /* Map the device tree blob header  */
+    fdt_region = pr_of_xenaddr(base_paddr, end_paddr, PAGE_AI_MASK(flags));

Instead of this new macro (PAGE_AI_MASK(flags)), can I reuse the existing one (ie MT_NORMAL) ?

xen/arch/arm/mpu/setup.c line 39

Quoted 1 line
xen/arch/arm/mpu/setup.c new file +1 -0
+    fdt_region.prbar.reg.ap = PAGE_AP_MASK(flags);

May be somethng like

s/PAGE_AP_MASK(flags)/AP_RO_EL2

And define this in the common mpu.h

/* Read-only at EL2, No Access at EL1/EL0. */ #define AP_RO_EL2 0x2

xen/arch/arm/mpu/setup.c line 40

Quoted 1 line
xen/arch/arm/mpu/setup.c new file +1 -0
+    fdt_region.prbar.reg.xn = PAGE_XN_MASK(flags);

Similar comment as before.

xen/arch/arm/mpu/setup.c line 52

Quoted 12 lines
xen/arch/arm/mpu/setup.c new file +12 -0
+
+    write_protection_region(&fdt_region, EARLY_FDT_MAP_REGION_NUMBER);
+    context_sync_mpu();
+
+    if ( fdt_magic(fdt_virt) != FDT_MAGIC )
+        return NULL;
+
+    end_paddr = round_pgup(fdt_paddr + fdt_totalsize(fdt_virt));
+
+    /*
+     * If the mapped range is not enough, map the rest of the DTB, pr_get_limit
+     * returns an inclusive address of the range, hence the increment.

Can you explain this a bit more ? Why do we need to create 2 MPU regions for mapping DTB.

xen/arch/arm/mpu/setup.c line 63

Quoted 11 lines
xen/arch/arm/mpu/setup.c new file +11 -0
+     */
+    if ( end_paddr > (pr_get_limit(&fdt_region) + 1) )
+    {
+        pr_set_limit(&fdt_region, end_paddr);
+
+        write_protection_region(&fdt_region, EARLY_FDT_MAP_REGION_NUMBER);
+        context_sync_mpu();
+    }
+
+    return fdt_virt;
+}

- Ayan

Quoted 13 lines
xen/arch/arm/mpu/setup.c new file +9 -1
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--

2.34.1

#17 lore raw

Re: [PATCH 8/9] arm/mpu: Implement early_fdt_map support in MPU systems

Hi Ayan,

Quoted 81 lines · Ayan Kumar Halder

Hi Luca,

Quoted 61 lines · Luca Fancellu

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.

Implement early_fdt_map() function, that is responsible to map the device tree blob in the early stages of the boot process, since at this stage the MPU C data structure are not yet initialised, it is using low level APIs to write into the MPU registers at a fixed MPU region number.

The MPU memory management is designed to work on pages of PAGE_SIZE in order to reuse helpers and macros already available on the Xen memory management system.

---
 xen/arch/arm/mpu/Makefile |  1 +
 xen/arch/arm/mpu/setup.c  | 72 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 xen/arch/arm/mpu/setup.c
xen/arch/arm/mpu/Makefile +1 -0
index b18cec483671..04df0b2ee760 100644
@@ -1 +1,2 @@
 obj-y += mm.o
+obj-y += setup.init.o
xen/arch/arm/mpu/setup.c new file +28 -0
new file mode 100644
index 000000000000..290baaca9fd7
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * xen/arch/arm/mpu/setup.c
+ *
+ * MPU system boot code for Armv8-R AArch64.
+ *
+ */
+
+#include <xen/bootfdt.h>
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/mm.h>
+
+/* Needs to be kept in sync with the regions programmed in arm64/mpu/head.S */
+#define EARLY_FDT_MAP_REGION_NUMBER 6
+
+void * __init early_fdt_map(paddr_t fdt_paddr)
+{
+    /* Map at least a page containing the DTB address, exclusive range */
+    paddr_t base_paddr = round_pgdown(fdt_paddr);
+    paddr_t end_paddr = round_pgup(fdt_paddr + sizeof(struct fdt_header));
+    unsigned int flags = PAGE_HYPERVISOR_RO;
+    void *fdt_virt = (void *)fdt_paddr; /* virt == paddr for MPU */
+    pr_t fdt_region;
+
+    /*
+     * Check whether the physical FDT address is set and meets the minimum
+     * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be at

xen/arch/arm/mpu/setup.c line 38

Quoted 10 lines
xen/arch/arm/mpu/setup.c new file +10 -0
+     * least 8 bytes so that we always access the magic and size fields
+     * of the FDT header after mapping the first chunk, double check if
+     * that is indeed the case.
+     */
+    BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
+    if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN )
+        return NULL;
+
+    /* Map the device tree blob header  */
+    fdt_region = pr_of_xenaddr(base_paddr, end_paddr, PAGE_AI_MASK(flags));

Instead of this new macro (PAGE_AI_MASK(flags)), can I reuse the existing one (ie MT_NORMAL) ?

xen/arch/arm/mpu/setup.c line 39

Quoted 1 line
xen/arch/arm/mpu/setup.c new file +1 -0
+    fdt_region.prbar.reg.ap = PAGE_AP_MASK(flags);

May be somethng like

Quoted 8 lines

s/PAGE_AP_MASK(flags)/AP_RO_EL2

And define this in the common mpu.h

/* Read-only at EL2, No Access at EL1/EL0. */ #define AP_RO_EL2 0x2

xen/arch/arm/mpu/setup.c line 40

Quoted 1 line
xen/arch/arm/mpu/setup.c new file +1 -0
+    fdt_region.prbar.reg.xn = PAGE_XN_MASK(flags);

Similar comment as before.

No I don’t agree on that, I believe we would better use the same “interfaces” as the MMU code in order to reuse most of the code and be consistent on our memory management subsystems.

Unless a technical limitation prevents to use them, I would like to continue in this way as the serie extensively use these.

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/mpu/setup.c new file +5 -0
+
+    write_protection_region(&fdt_region, EARLY_FDT_MAP_REGION_NUMBER);
+    context_sync_mpu();
+
+    if ( fdt_magic(fdt_virt) != FDT_MAGIC )
Quoted 8 lines

xen/arch/arm/mpu/setup.c line 52

Quoted 7 lines
xen/arch/arm/mpu/setup.c new file +7 -0
+        return NULL;
+
+    end_paddr = round_pgup(fdt_paddr + fdt_totalsize(fdt_virt));
+
+    /*
+     * If the mapped range is not enough, map the rest of the DTB, pr_get_limit
+     * returns an inclusive address of the range, hence the increment.

Can you explain this a bit more ? Why do we need to create 2 MPU regions for mapping DTB.

First one will map a page that comprises the DTB header, second one will map the rest. The size of the DTB is not known at first, hence the two operation, anyway here I’m not mapping 2 regions, I’m adjusting the same region with the updated range.

Quoted 25 lines

xen/arch/arm/mpu/setup.c line 63

Quoted 11 lines
xen/arch/arm/mpu/setup.c new file +11 -0
+     */
+    if ( end_paddr > (pr_get_limit(&fdt_region) + 1) )
+    {
+        pr_set_limit(&fdt_region, end_paddr);
+
+        write_protection_region(&fdt_region, EARLY_FDT_MAP_REGION_NUMBER);
+        context_sync_mpu();
+    }
+
+    return fdt_virt;
+}

- Ayan

Quoted 13 lines
xen/arch/arm/mpu/setup.c new file +9 -1
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--

2.34.1

#18 lore raw

PATCH 9/9 arm/mpu: Implement setup_mappings for MPU system

In reply to Luca Fancellu

Currently the function setup_pagetables() take care to initialise the mappings on MMU system; MPU systems don't have page tables, but needs to track the status of the MPU programmed regions.

So rename setup_pagetables() into setup_mappings() and implement the function on MPU systems, start introducing data structures and functions to track the MPU status from the C world.

The xen_mpumap_mask bitmap is used to track which MPU region are enabled at runtime.

---
 xen/arch/arm/include/asm/arm64/mpu.h |  2 ++
 xen/arch/arm/include/asm/mm.h        |  2 +-
 xen/arch/arm/mmu/setup.c             |  2 +-
 xen/arch/arm/mpu/mm.c                | 12 +++++++++
 xen/arch/arm/mpu/setup.c             | 40 ++++++++++++++++++++++++++++
 xen/arch/arm/setup.c                 |  2 +-
 6 files changed, 57 insertions(+), 3 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index dce77da60110..4b7579d0ce18 100644
@@ -17,6 +17,8 @@
 
 #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 
+#define PRENR_MASK              GENMASK(31, 0)
+
 /* Access permission attributes. */
 /* Read/Write at EL2, No Access at EL1/EL0. */
 #define AP_RW_EL2 0x0
xen/arch/arm/include/asm/mm.h +1 -1
index e7767cdab493..370054554432 100644
@@ -206,7 +206,7 @@ extern unsigned long frametable_base_pdx;
 #define PDX_GROUP_SHIFT SECOND_SHIFT
 
 /* Boot-time pagetable setup */
-extern void setup_pagetables(void);
+extern void setup_mappings(void);
 /* Map FDT in boot pagetable */
 extern void *early_fdt_map(paddr_t fdt_paddr);
 /* Remove early mappings */
xen/arch/arm/mmu/setup.c +1 -1
index 30afe9778194..35ffa5479dd3 100644
@@ -354,7 +354,7 @@ static void __init create_llc_coloring_mappings(void)
  * Boot-time pagetable setup.
  * Changes here may need matching changes in head.S
  */
-void __init setup_pagetables(void)
+void __init setup_mappings(void)
 {
     uint64_t ttbr;
     lpae_t pte, *p;
xen/arch/arm/mpu/mm.c +12 -0
index fb94f5d1d93d..4614c36f04cc 100644
@@ -8,10 +8,22 @@
  *
  */
 
+#include <xen/init.h>
 #include <asm/mpu/mm.h>
 #include <asm/page.h>
 #include <asm/sysregs.h>
 
+/* Maximum number of supported MPU memory regions by the EL2 MPU. */
+uint8_t __ro_after_init max_xen_mpumap;
+
+/*
+ * Bitmap xen_mpumap_mask is to record the usage of EL2 MPU memory regions.
+ * Bit 0 represents MPU memory region 0, bit 1 represents MPU memory
+ * region 1, ..., and so on.
+ * If a MPU memory region gets enabled, set the according bit to 1.
+ */
+DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGIONS);
+
 /* EL2 Xen MPU memory region mapping table. */
 pr_t xen_mpumap[MAX_MPU_REGIONS];
 
xen/arch/arm/mpu/setup.c +40 -0
index 290baaca9fd7..b37f309ec8e3 100644
@@ -14,6 +14,46 @@
 /* Needs to be kept in sync with the regions programmed in arm64/mpu/head.S */
 #define EARLY_FDT_MAP_REGION_NUMBER 6
 
+extern uint8_t max_xen_mpumap;
+extern DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGIONS);
+extern pr_t xen_mpumap[MAX_MPU_REGIONS];
+
+/*
+ * The code in this function needs to track the regions programmed in
+ * arm64/mpu/head.S
+ */
+void __init setup_mappings(void)
+{
+    register_t prenr;
+    unsigned int i = 0;
+
+    /*
+     * MPUIR_EL2.Region[0:7] identifies the number of regions supported by
+     * the EL2 MPU.
+     */
+    max_xen_mpumap = (uint8_t)(READ_SYSREG(MPUIR_EL2) & NUM_MPU_REGIONS_MASK);
+
+    /* PRENR_EL2 has the N bit set if the N region is enabled, N < 32 */
+    prenr = (READ_SYSREG(PRENR_EL2) & PRENR_MASK);
+
+    /*
+     * Set the bitfield for regions enabled in assembly boot-time.
+     * This code works under the assumption that the code in head.S has
+     * allocated and enabled regions below 32 (N < 32).
+     */
+    while ( prenr > 0 )
+    {
+        if (prenr & 0x1)
+        {
+            set_bit(i, xen_mpumap_mask);
+            read_protection_region(&xen_mpumap[i], i);
+        }
+
+        prenr >>= 1;
+        i++;
+    }
+}
+
 void * __init early_fdt_map(paddr_t fdt_paddr)
 {
     /* Map at least a page containing the DTB address, exclusive range */
xen/arch/arm/setup.c +1 -1
index c1f2d1b89d43..fc6c9456bc2e 100644
@@ -340,7 +340,7 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)
      * Page tables must be setup after LLC coloring initialization because
      * coloring info are required in order to create colored mappings
      */
-    setup_pagetables();
+    setup_mappings();
     /* Device-tree was mapped in boot page tables, remap it in the new tables */
     device_tree_flattened = early_fdt_map(fdt_paddr);
 
Signature · 1 line
2.34.1