Skip to contentSkip to conversation

First chunk for Arm R82 and MPU support

PATCH Superseded Luca Fancellu 2 participants 22 messages Apr 7, 2025 – Apr 9, 2025

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 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 is not used for MPU but it is just to give an idea about which stage of the boot is reached.

v2 changes for this serie:
- rebased serie on the MPU skeleton that allow compilation
- removed some patches already merged in the MPU skeleton

Luca Fancellu (6):
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_mpu for MPU system

Penny Zheng (1):
arm/mpu: Introduce MPU memory region map structure

 xen/arch/arm/include/asm/arm64/mpu.h |  57 ++++++
 xen/arch/arm/include/asm/mpu.h       |  50 ++++++
 xen/arch/arm/include/asm/mpu/mm.h    |  27 +++
 xen/arch/arm/include/asm/page.h      |  25 +++
 xen/arch/arm/mpu/mm.c                | 253 ++++++++++++++++++++++++++-
 xen/arch/arm/mpu/setup.c             |  54 +++++-
 6 files changed, 463 insertions(+), 3 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/7 arm/mpu: Introduce MPU memory region map structure

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/include/asm/arm64/mpu.h | 44 ++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/mpu.h       |  5 ++++
 xen/arch/arm/mpu/mm.c                |  4 +++
 3 files changed, 53 insertions(+)
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
xen/arch/arm/include/asm/arm64/mpu.h new file +44 -0
new file mode 100644
index 000000000000..4d2bd7d7877f
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.
+ */
+
+#ifndef __ARM_ARM64_MPU_H__
+#define __ARM_ARM64_MPU_H__
+
+#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 /* __ARM_ARM64_MPU_H__ */
\ No newline at end of file
xen/arch/arm/include/asm/mpu.h +5 -0
index d4ec4248b62b..e148c705b82c 100644
@@ -6,6 +6,10 @@
 #ifndef __ARM_MPU_H__
 #define __ARM_MPU_H__
 
+#if defined(CONFIG_ARM_64)
+# include <asm/arm64/mpu.h>
+#endif
+
 #define MPU_REGION_SHIFT  6
 #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
 #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
@@ -13,6 +17,7 @@
 #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
 
 #endif /* __ARM_MPU_H__ */
 
xen/arch/arm/mpu/mm.c +4 -0
index 07c8959f4ee9..f83ce04fef8a 100644
@@ -7,9 +7,13 @@
 #include <xen/mm.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
+#include <asm/mpu.h>
 
 struct page_info *frame_table;
 
+/* EL2 Xen MPU memory region mapping table. */
+pr_t xen_mpumap[MAX_MPU_REGIONS];
+
 static void __init __maybe_unused build_assertions(void)
 {
     /*
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/7 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/mpu.h    |   1 +
 xen/arch/arm/include/asm/mpu/mm.h |  24 ++++++
 xen/arch/arm/mpu/mm.c             | 127 ++++++++++++++++++++++++++++++
 3 files changed, 152 insertions(+)
xen/arch/arm/include/asm/mpu.h +1 -0
index e148c705b82c..59ff22c804c1 100644
@@ -13,6 +13,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 +24 -0
index 86f33d9836b7..5cabe9d111ce 100644
@@ -8,6 +8,7 @@
 #include <xen/page-size.h>
 #include <xen/types.h>
 #include <asm/mm.h>
+#include <asm/mpu.h>
 
 extern struct page_info *frame_table;
 
@@ -29,6 +30,29 @@ static inline struct page_info *virt_to_page(const void *v)
     return mfn_to_page(mfn);
 }
 
+/* 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_H__ */
 
 /*
xen/arch/arm/mpu/mm.c +127 -0

145 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Hi Luca,

Quoted 113 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 some utility function in order to access the MPU regions from the C world.

---
  xen/arch/arm/include/asm/mpu.h    |   1 +
  xen/arch/arm/include/asm/mpu/mm.h |  24 ++++++
  xen/arch/arm/mpu/mm.c             | 127 ++++++++++++++++++++++++++++++
  3 files changed, 152 insertions(+)
xen/arch/arm/include/asm/mpu.h +1 -0
index e148c705b82c..59ff22c804c1 100644
@@ -13,6 +13,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 +24 -0
index 86f33d9836b7..5cabe9d111ce 100644
@@ -8,6 +8,7 @@
  #include <xen/page-size.h>
  #include <xen/types.h>
  #include <asm/mm.h>
+#include <asm/mpu.h>
 
  extern struct page_info *frame_table;
 
@@ -29,6 +30,29 @@ static inline struct page_info *virt_to_page(const void *v)
      return mfn_to_page(mfn);
  }
 
+/* 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_H__ */
 
  /*
xen/arch/arm/mpu/mm.c +25 -0
index f83ce04fef8a..bf281f67fb6b 100644
@@ -8,12 +8,34 @@
  #include <xen/sizes.h>
  #include <xen/types.h>
  #include <asm/mpu.h>
+#include <asm/mpu/mm.h>
+#include <asm/sysregs.h>
 
  struct page_info *frame_table;
 
  /* EL2 Xen MPU memory region mapping table. */
  pr_t xen_mpumap[MAX_MPU_REGIONS];
 
+/* The following are needed for the case generator with num==0 */
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2
+
+#define GENERATE_WRITE_PR_REG_CASE(num, pr)                                 \
+    case num:                                                               \
+    {                                                                       \
+        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR##num##_EL2);  \
+        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR##num##_EL2);  \
+        break;                                                              \
+    }
+
+#define GENERATE_READ_PR_REG_CASE(num, pr)                      \
+    case num:                                                   \
+    {                                                           \
+        pr->prbar.bits = READ_SYSREG(PRBAR##num##_EL2);         \
+        pr->prlar.bits = READ_SYSREG(PRLAR##num##_EL2);         \
+        break;                                                  \
+    }
+
  static void __init __maybe_unused build_assertions(void)
  {
      /*
@@ -24,6 +46,111 @@ static void __init __maybe_unused build_assertions(void)
      BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
  }
 
+static void prepare_selector(uint8_t sel)
+{
+    /*

xen/arch/arm/mpu/mm.c static void __init __maybe_unused build_assertions(void) · line 61

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+     * {read,write}_protection_region works using the direct access to the 0..15
+     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
+     * only when needed, so when the upper 4 bits of the selector will change.
+     */
+    sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    {
+        WRITE_SYSREG(sel, PRSELR_EL2);
+        isb();
+    }

This needs to be arm64 specific. Refer ARM DDI 0600A.d ID120821

G1.3.19  PRBAR<n>_EL2, /* I guess this is what you are following */

Provides access to the base address for the MPU region determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.

Whereas for arm32 . Refer ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n>,

Provides access to the base addresses for the first 32 defined EL2 MPU regions.

/* Here we do not need to use HPRSELR for region selection */

If you want to make the code similar between arm32 and arm64, then I can suggest you can use these registers.

G1.3.17 PRBAR_EL2

Provides access to the base addresses for the EL2 MPU region. PRSELR_EL2.REGION determines which MPU region is selected.

E2.2.2 HPRBAR

Provides indirect access to the base address of the EL2 MPU region currently defined by HPRSELR.

Let me know if it makes sense.

- Ayan

Quoted 99 lines
xen/arch/arm/mpu/mm.c +92 -1
+}
+
+/*
+ * Armv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for Armv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provide access to the EL2 MPU region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR_EL2 and PRLAR_EL2
+ * - Region 17 configuration is accessible through PRBAR1_EL2 and PRLAR1_EL2
+ * - Region 18 configuration is accessible through PRBAR2_EL2 and PRLAR2_EL2
+ * - ...
+ * - Region 31 configuration is accessible through PRBAR15_EL2 and PRLAR15_EL2
+ */
+/*
+ * Read EL2 MPU Protection Region.
+ *
+ * @pr_read: mpu protection region returned by read op.
+ * @sel: mpu protection region selector
+ */
+void read_protection_region(pr_t *pr_read, uint8_t sel)
+{
+    /*
+     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
+     * make sure PRSELR_EL2 is set, as it determines which MPU region
+     * is selected.
+     */
+    prepare_selector(sel);
+
+    switch ( sel & 0xFU )
+    {
+        GENERATE_READ_PR_REG_CASE(0, pr_read);
+        GENERATE_READ_PR_REG_CASE(1, pr_read);
+        GENERATE_READ_PR_REG_CASE(2, pr_read);
+        GENERATE_READ_PR_REG_CASE(3, pr_read);
+        GENERATE_READ_PR_REG_CASE(4, pr_read);
+        GENERATE_READ_PR_REG_CASE(5, pr_read);
+        GENERATE_READ_PR_REG_CASE(6, pr_read);
+        GENERATE_READ_PR_REG_CASE(7, pr_read);
+        GENERATE_READ_PR_REG_CASE(8, pr_read);
+        GENERATE_READ_PR_REG_CASE(9, pr_read);
+        GENERATE_READ_PR_REG_CASE(10, pr_read);
+        GENERATE_READ_PR_REG_CASE(11, pr_read);
+        GENERATE_READ_PR_REG_CASE(12, pr_read);
+        GENERATE_READ_PR_REG_CASE(13, pr_read);
+        GENERATE_READ_PR_REG_CASE(14, pr_read);
+        GENERATE_READ_PR_REG_CASE(15, pr_read);
+    default:
+        BUG(); /* Can't happen */
+    }
+}
+
+/*
+ * Write EL2 MPU Protection Region.
+ *
+ * @pr_write: const mpu protection region passed through write op.
+ * @sel: mpu protection region selector
+ */
+void write_protection_region(const pr_t *pr_write, uint8_t sel)
+{
+    /*
+     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
+     * make sure PRSELR_EL2 is set, as it determines which MPU region
+     * is selected.
+     */
+    prepare_selector(sel);
+
+    switch ( sel & 0xFU )
+    {
+        GENERATE_WRITE_PR_REG_CASE(0, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(1, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(2, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(3, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(4, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(5, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(6, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(7, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(8, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(9, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(10, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(11, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(12, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(13, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(14, pr_write);
+        GENERATE_WRITE_PR_REG_CASE(15, pr_write);
+    default:
+        BUG(); /* Can't happen */
+    }
+}
+
  void __init setup_mm(void)
  {
      BUG_ON("unimplemented");
--

2.34.1

#4 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Hi Ayan,

Quoted 43 lines · Ayan Kumar Halder

Hi Luca,

Quoted 4 lines
(quoted diff) +3 -0
+static void prepare_selector(uint8_t sel)
+{
+    /*

Review

Quoted 10 lines
(quoted diff) +10 -0
+     * {read,write}_protection_region works using the direct access to the 0..15
+     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
+     * only when needed, so when the upper 4 bits of the selector will change.
+     */
+    sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    {
+        WRITE_SYSREG(sel, PRSELR_EL2);
+        isb();
+    }

This needs to be arm64 specific. Refer ARM DDI 0600A.d ID120821

G1.3.19 PRBAR<n>_EL2, /* I guess this is what you are following */

Provides access to the base address for the MPU region determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.

Whereas for arm32 . Refer ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n>,

Provides access to the base addresses for the first 32 defined EL2 MPU regions.

/* Here we do not need to use HPRSELR for region selection */

If you want to make the code similar between arm32 and arm64, then I can suggest you can use these registers.

G1.3.17 PRBAR_EL2

Provides access to the base addresses for the EL2 MPU region. PRSELR_EL2.REGION determines which MPU region is selected.

Quoted 8 lines

E2.2.2 HPRBAR

Provides indirect access to the base address of the EL2 MPU region currently defined by HPRSELR.

Let me know if it makes sense.

- Ayan

Ok I didin’t get that before, what do you think if I modify the code as in this diff (not tested):

xen/arch/arm/mpu/mm.c +8 -6
index fe05c8097155..1bc6d7a77296 100644
@@ -58,19 +58,21 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
 }
 
-static void prepare_selector(uint8_t sel)
+static void prepare_selector(uint8_t *sel)
 {
+    uint8_t cur_sel = *sel;
     /*
      * {read,write}_protection_region works using the direct access to the 0..15
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
-    sel &= 0xF0U;
-    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    cur_sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
     {
-        WRITE_SYSREG(sel, PRSELR_EL2);
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
         isb();
     }
+    *sel = *sel & 0xFU;
 }
 
 /*
@@ -102,7 +104,7 @@ void read_protection_region(pr_t *pr_read, uint8_t sel)
      */
     prepare_selector(sel);
 
-    switch ( sel & 0xFU )
+    switch ( sel )
     {
         GENERATE_READ_PR_REG_CASE(0, pr_read);
         GENERATE_READ_PR_REG_CASE(1, pr_read);
@@ -140,7 +142,7 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
      */
     prepare_selector(sel);
 
-    switch ( sel & 0xFU )
+    switch ( sel )
     {
         GENERATE_WRITE_PR_REG_CASE(0, pr_write);
         GENERATE_WRITE_PR_REG_CASE(1, pr_write);

And later you will use some #ifdef CONFIG_ARM_32 inside prepare_selector() to check that the code is passing up to the max supported region or panic.
And in {read,write}_protection_region() you could add some #ifdef CONFIG_ARM_32 to add the case generators for regions from 16 to 23 since R52 can address them directly.

What do you think?

Cheers,
Luca

#5 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 101 lines
Quoted 42 lines · Ayan Kumar Halder

Hi Luca,

Review

Quoted 13 lines
(quoted diff) +13 -0
+static void prepare_selector(uint8_t sel)
+{
+    /*
+     * {read,write}_protection_region works using the direct access to the 0..15
+     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
+     * only when needed, so when the upper 4 bits of the selector will change.
+     */
+    sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    {
+        WRITE_SYSREG(sel, PRSELR_EL2);
+        isb();
+    }

This needs to be arm64 specific. Refer ARM DDI 0600A.d ID120821

G1.3.19 PRBAR<n>_EL2, /* I guess this is what you are following */

Provides access to the base address for the MPU region determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.

Whereas for arm32 . Refer ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n>,

Provides access to the base addresses for the first 32 defined EL2 MPU regions.

/* Here we do not need to use HPRSELR for region selection */

If you want to make the code similar between arm32 and arm64, then I can suggest you can use these registers.

G1.3.17 PRBAR_EL2

Provides access to the base addresses for the EL2 MPU region. PRSELR_EL2.REGION determines which MPU region is selected.

Quoted 8 lines

E2.2.2 HPRBAR

Provides indirect access to the base address of the EL2 MPU region currently defined by HPRSELR.

Let me know if it makes sense.

- Ayan

Ok I didin’t get that before, what do you think if I modify the code as in this diff (not tested):

xen/arch/arm/mpu/mm.c +8 -6
index fe05c8097155..1bc6d7a77296 100644
@@ -58,19 +58,21 @@ static void __init __maybe_unused build_assertions(void)
      BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
  }
 
-static void prepare_selector(uint8_t sel)
+static void prepare_selector(uint8_t *sel)
  {
+    uint8_t cur_sel = *sel;
      /*
       * {read,write}_protection_region works using the direct access to the 0..15
       * regions, so in order to save the isb() overhead, change the PRSELR_EL2
       * only when needed, so when the upper 4 bits of the selector will change.
       */
-    sel &= 0xF0U;
-    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    cur_sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
      {
-        WRITE_SYSREG(sel, PRSELR_EL2);
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
          isb();
      }
+    *sel = *sel & 0xFU;
  }
 
  /*
@@ -102,7 +104,7 @@ void read_protection_region(pr_t *pr_read, uint8_t sel)
       */
      prepare_selector(sel);
 
-    switch ( sel & 0xFU )
+    switch ( sel )
      {
          GENERATE_READ_PR_REG_CASE(0, pr_read);
          GENERATE_READ_PR_REG_CASE(1, pr_read);
@@ -140,7 +142,7 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
       */
      prepare_selector(sel);
 
-    switch ( sel & 0xFU )
+    switch ( sel )
      {
          GENERATE_WRITE_PR_REG_CASE(0, pr_write);
          GENERATE_WRITE_PR_REG_CASE(1, pr_write);
Quoted 6 lines

And later you will use some #ifdef CONFIG_ARM_32 inside prepare_selector() to check that the code is passing up to the max supported region or panic.
And in {read,write}_protection_region() you could add some #ifdef CONFIG_ARM_32 to add the case generators for regions from 16 to 23 since R52 can address them directly.

What do you think?

I got this diff working as it is for R82. This looks much lesser code

xen/arch/arm/mpu/mm.c +0 -0
index fe05c80971..63627c85dc 100644
@@ -28,23 +28,19 @@ DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGIONS);

 /* EL2 Xen MPU memory region mapping table. */
 pr_t xen_mpumap[MAX_MPU_REGIONS];

-/* The following are needed for the case generator with num==0 */ -#define PRBAR0_EL2 PRBAR_EL2
-#define PRLAR0_EL2 PRLAR_EL2
-
 #define GENERATE_WRITE_PR_REG_CASE(num, pr)                                 \
     case num: \
{ \
-        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR##num##_EL2);  \
-        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR##num##_EL2);  \
+        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR_EL2);  \
+        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR_EL2);  \
break; \
     }

 #define GENERATE_READ_PR_REG_CASE(num, pr)                      \
     case num:                                                   \
     {                                                           \
-        pr->prbar.bits = READ_SYSREG(PRBAR##num##_EL2);         \
-        pr->prlar.bits = READ_SYSREG(PRLAR##num##_EL2);         \
+        pr->prbar.bits = READ_SYSREG(PRBAR_EL2);         \
+        pr->prlar.bits = READ_SYSREG(PRLAR_EL2);         \
         break;                                                  \
     }

@@ -65,7 +61,6 @@ static void prepare_selector(uint8_t sel)
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change. */
-    sel &= 0xF0U;
     if ( READ_SYSREG(PRSELR_EL2) != sel )
     {
         WRITE_SYSREG(sel, PRSELR_EL2);

Please give it a try to see if it works at your end.

And then, the code can be reduced further as

void read_protection_region(pr_t *pr_read, uint8_t sel)
{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(sel);

    pr_read->prbar.bits = READ_SYSREG(PRBAR_EL2);

    pr_read->prlar.bits = READ_SYSREG(PRLAR_EL2);

}

The same can be done for write_protection_region(...)

- Ayan

Quoted 7 lines

Cheers,
Luca

#6 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Quoted 173 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 97 lines
Quoted 42 lines · Ayan Kumar Halder

Hi Luca,

Review

Quoted 13 lines
(quoted diff) +13 -0
+static void prepare_selector(uint8_t sel)
+{
+    /*
+     * {read,write}_protection_region works using the direct access to the 0..15
+     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
+     * only when needed, so when the upper 4 bits of the selector will change.
+     */
+    sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    {
+        WRITE_SYSREG(sel, PRSELR_EL2);
+        isb();
+    }

This needs to be arm64 specific. Refer ARM DDI 0600A.d ID120821

G1.3.19 PRBAR<n>_EL2, /* I guess this is what you are following */

Provides access to the base address for the MPU region determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.

Whereas for arm32 . Refer ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n>,

Provides access to the base addresses for the first 32 defined EL2 MPU regions.

/* Here we do not need to use HPRSELR for region selection */

If you want to make the code similar between arm32 and arm64, then I can suggest you can use these registers.

G1.3.17 PRBAR_EL2

Provides access to the base addresses for the EL2 MPU region. PRSELR_EL2.REGION determines which MPU region is selected.

Quoted 8 lines

E2.2.2 HPRBAR

Provides indirect access to the base address of the EL2 MPU region currently defined by HPRSELR.

Let me know if it makes sense.

- Ayan

Ok I didin’t get that before, what do you think if I modify the code as in this diff (not tested):

xen/arch/arm/mpu/mm.c +8 -3
index fe05c8097155..1bc6d7a77296 100644
@@ -58,19 +58,21 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
 }
 -static void prepare_selector(uint8_t sel)
+static void prepare_selector(uint8_t *sel)
 {
+    uint8_t cur_sel = *sel;
     /*
      * {read,write}_protection_region works using the direct access to the 0..15
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
-    sel &= 0xF0U;
-    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    cur_sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
     {
-        WRITE_SYSREG(sel, PRSELR_EL2);
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
         isb();
     }
+    *sel = *sel & 0xFU;
 }
   /*
@@ -102,7 +104,7 @@ void read_protection_region(pr_t *pr_read, uint8_t sel)
      */
     prepare_selector(sel);
 -    switch ( sel & 0xFU )
+    switch ( sel )
     {
         GENERATE_READ_PR_REG_CASE(0, pr_read);
         GENERATE_READ_PR_REG_CASE(1, pr_read);
@@ -140,7 +142,7 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
      */
     prepare_selector(sel);
 -    switch ( sel & 0xFU )
+    switch ( sel )
     {
         GENERATE_WRITE_PR_REG_CASE(0, pr_write);
         GENERATE_WRITE_PR_REG_CASE(1, pr_write);
Quoted 6 lines

And later you will use some #ifdef CONFIG_ARM_32 inside prepare_selector() to check that the code is passing up to the max supported region or panic.
And in {read,write}_protection_region() you could add some #ifdef CONFIG_ARM_32 to add the case generators for regions from 16 to 23 since R52 can address them directly.

What do you think?

I got this diff working as it is for R82. This looks much lesser code

xen/arch/arm/mpu/mm.c +4 -9
index fe05c80971..63627c85dc 100644
@@ -28,23 +28,19 @@ DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGIONS);
 /* EL2 Xen MPU memory region mapping table. */
 pr_t xen_mpumap[MAX_MPU_REGIONS];
 
-/* The following are needed for the case generator with num==0 */
-#define PRBAR0_EL2 PRBAR_EL2
-#define PRLAR0_EL2 PRLAR_EL2
-
 #define GENERATE_WRITE_PR_REG_CASE(num, pr)                                 \
     case num: \
 { \
-        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR##num##_EL2);  \
-        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR##num##_EL2);  \
+        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR_EL2);  \
+        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR_EL2);  \
 break; \
     }
 
 #define GENERATE_READ_PR_REG_CASE(num, pr)                      \
     case num:                                                   \
     {                                                           \
-        pr->prbar.bits = READ_SYSREG(PRBAR##num##_EL2);         \
-        pr->prlar.bits = READ_SYSREG(PRLAR##num##_EL2);         \
+        pr->prbar.bits = READ_SYSREG(PRBAR_EL2);         \
+        pr->prlar.bits = READ_SYSREG(PRLAR_EL2);         \
         break;                                                  \
     }
 
@@ -65,7 +61,6 @@ static void prepare_selector(uint8_t sel)
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
-    sel &= 0xF0U;
     if ( READ_SYSREG(PRSELR_EL2) != sel )
     {
         WRITE_SYSREG(sel, PRSELR_EL2);

Please give it a try to see if it works at your end.

And then, the code can be reduced further as

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(sel);

    pr_read->prbar.bits = READ_SYSREG(PRBAR_EL2);

    pr_read->prlar.bits = READ_SYSREG(PRLAR_EL2);
Quoted 5 lines

}

The same can be done for write_protection_region(...)

- Ayan

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Quoted 8 lines
Quoted 8 lines

Cheers,
Luca

#7 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Hi,

Quoted 170 lines · Luca Fancellu
Quoted 170 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 96 lines
Quoted 41 lines · Ayan Kumar Halder

Hi Luca,

Review

Quoted 13 lines
(quoted diff) +13 -0
+static void prepare_selector(uint8_t sel)
+{
+    /*
+     * {read,write}_protection_region works using the direct access to the 0..15
+     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
+     * only when needed, so when the upper 4 bits of the selector will change.
+     */
+    sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    {
+        WRITE_SYSREG(sel, PRSELR_EL2);
+        isb();
+    }

This needs to be arm64 specific. Refer ARM DDI 0600A.d ID120821

G1.3.19 PRBAR<n>_EL2, /* I guess this is what you are following */

Provides access to the base address for the MPU region determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.

Whereas for arm32 . Refer ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n>,

Provides access to the base addresses for the first 32 defined EL2 MPU regions.

/* Here we do not need to use HPRSELR for region selection */

If you want to make the code similar between arm32 and arm64, then I can suggest you can use these registers.

G1.3.17 PRBAR_EL2

Provides access to the base addresses for the EL2 MPU region. PRSELR_EL2.REGION determines which MPU region is selected.

Quoted 8 lines

E2.2.2 HPRBAR

Provides indirect access to the base address of the EL2 MPU region currently defined by HPRSELR.

Let me know if it makes sense.

- Ayan

Ok I didin’t get that before, what do you think if I modify the code as in this diff (not tested):

xen/arch/arm/mpu/mm.c +8 -3
index fe05c8097155..1bc6d7a77296 100644
@@ -58,19 +58,21 @@ static void __init __maybe_unused build_assertions(void)
      BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
  }
  -static void prepare_selector(uint8_t sel)
+static void prepare_selector(uint8_t *sel)
  {
+    uint8_t cur_sel = *sel;
      /*
       * {read,write}_protection_region works using the direct access to the 0..15
       * regions, so in order to save the isb() overhead, change the PRSELR_EL2
       * only when needed, so when the upper 4 bits of the selector will change.
       */
-    sel &= 0xF0U;
-    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    cur_sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
      {
-        WRITE_SYSREG(sel, PRSELR_EL2);
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
          isb();
      }
+    *sel = *sel & 0xFU;
  }
    /*
@@ -102,7 +104,7 @@ void read_protection_region(pr_t *pr_read, uint8_t sel)
       */
      prepare_selector(sel);
  -    switch ( sel & 0xFU )
+    switch ( sel )
      {
          GENERATE_READ_PR_REG_CASE(0, pr_read);
          GENERATE_READ_PR_REG_CASE(1, pr_read);
@@ -140,7 +142,7 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
       */
      prepare_selector(sel);
  -    switch ( sel & 0xFU )
+    switch ( sel )
      {
          GENERATE_WRITE_PR_REG_CASE(0, pr_write);
          GENERATE_WRITE_PR_REG_CASE(1, pr_write);
Quoted 6 lines

And later you will use some #ifdef CONFIG_ARM_32 inside prepare_selector() to check that the code is passing up to the max supported region or panic.
And in {read,write}_protection_region() you could add some #ifdef CONFIG_ARM_32 to add the case generators for regions from 16 to 23 since R52 can address them directly.

What do you think?

I got this diff working as it is for R82. This looks much lesser code

xen/arch/arm/mpu/mm.c +4 -9
index fe05c80971..63627c85dc 100644
@@ -28,23 +28,19 @@ DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGIONS);
  /* EL2 Xen MPU memory region mapping table. */
  pr_t xen_mpumap[MAX_MPU_REGIONS];
 
-/* The following are needed for the case generator with num==0 */
-#define PRBAR0_EL2 PRBAR_EL2
-#define PRLAR0_EL2 PRLAR_EL2
-
  #define GENERATE_WRITE_PR_REG_CASE(num, pr)                                 \
      case num: \
 { \
-        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR##num##_EL2);  \
-        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR##num##_EL2);  \
+        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR_EL2);  \
+        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR_EL2);  \
 break; \
      }
 
  #define GENERATE_READ_PR_REG_CASE(num, pr)                      \
      case num:                                                   \
      {                                                           \
-        pr->prbar.bits = READ_SYSREG(PRBAR##num##_EL2);         \
-        pr->prlar.bits = READ_SYSREG(PRLAR##num##_EL2);         \
+        pr->prbar.bits = READ_SYSREG(PRBAR_EL2);         \
+        pr->prlar.bits = READ_SYSREG(PRLAR_EL2);         \
          break;                                                  \
      }
 
@@ -65,7 +61,6 @@ static void prepare_selector(uint8_t sel)
       * regions, so in order to save the isb() overhead, change the PRSELR_EL2
       * only when needed, so when the upper 4 bits of the selector will change.
       */
-    sel &= 0xF0U;
      if ( READ_SYSREG(PRSELR_EL2) != sel )
      {
          WRITE_SYSREG(sel, PRSELR_EL2);

Please give it a try to see if it works at your end.

And then, the code can be reduced further as

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(sel);

     pr_read->prbar.bits = READ_SYSREG(PRBAR_EL2);

     pr_read->prlar.bits = READ_SYSREG(PRLAR_EL2);
Quoted 7 lines
Quoted 5 lines

}

The same can be done for write_protection_region(...)

- Ayan

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Not sure if it is beneficial as you would need to use isb() from region16 onwards.

If you are going to keep the code as it is, then the following needs to be moved to arm64 specific header as well

#define PRBAR0_EL2 PRBAR_EL2
#define PRLAR0_EL2 PRLAR_EL2

In arm32 , they have different definitions.

I could not follow your earlier snippet, can you test it and send it as a patch ?

- Ayan

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Cheers,
Luca

#8 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Hi Ayan,

Quoted 177 lines · Ayan Kumar Halder

Hi,

Quoted 172 lines · Luca Fancellu
Quoted 172 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 96 lines
Quoted 41 lines · Ayan Kumar Halder

Hi Luca,

Review

Quoted 13 lines
(quoted diff) +13 -0
+static void prepare_selector(uint8_t sel)
+{
+    /*
+     * {read,write}_protection_region works using the direct access to the 0..15
+     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
+     * only when needed, so when the upper 4 bits of the selector will change.
+     */
+    sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    {
+        WRITE_SYSREG(sel, PRSELR_EL2);
+        isb();
+    }

This needs to be arm64 specific. Refer ARM DDI 0600A.d ID120821

G1.3.19 PRBAR<n>_EL2, /* I guess this is what you are following */

Provides access to the base address for the MPU region determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.

Whereas for arm32 . Refer ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n>,

Provides access to the base addresses for the first 32 defined EL2 MPU regions.

/* Here we do not need to use HPRSELR for region selection */

If you want to make the code similar between arm32 and arm64, then I can suggest you can use these registers.

G1.3.17 PRBAR_EL2

Provides access to the base addresses for the EL2 MPU region. PRSELR_EL2.REGION determines which MPU region is selected.

Quoted 8 lines

E2.2.2 HPRBAR

Provides indirect access to the base address of the EL2 MPU region currently defined by HPRSELR.

Let me know if it makes sense.

- Ayan

Ok I didin’t get that before, what do you think if I modify the code as in this diff (not tested):

xen/arch/arm/mpu/mm.c +8 -3
index fe05c8097155..1bc6d7a77296 100644
@@ -58,19 +58,21 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
 }
 -static void prepare_selector(uint8_t sel)
+static void prepare_selector(uint8_t *sel)
 {
+    uint8_t cur_sel = *sel;
     /*
      * {read,write}_protection_region works using the direct access to the 0..15
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
-    sel &= 0xF0U;
-    if ( READ_SYSREG(PRSELR_EL2) != sel )
+    cur_sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
     {
-        WRITE_SYSREG(sel, PRSELR_EL2);
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
         isb();
     }
+    *sel = *sel & 0xFU;
 }
   /*
@@ -102,7 +104,7 @@ void read_protection_region(pr_t *pr_read, uint8_t sel)
      */
     prepare_selector(sel);
 -    switch ( sel & 0xFU )
+    switch ( sel )
     {
         GENERATE_READ_PR_REG_CASE(0, pr_read);
         GENERATE_READ_PR_REG_CASE(1, pr_read);
@@ -140,7 +142,7 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
      */
     prepare_selector(sel);
 -    switch ( sel & 0xFU )
+    switch ( sel )
     {
         GENERATE_WRITE_PR_REG_CASE(0, pr_write);
         GENERATE_WRITE_PR_REG_CASE(1, pr_write);
Quoted 6 lines

And later you will use some #ifdef CONFIG_ARM_32 inside prepare_selector() to check that the code is passing up to the max supported region or panic.
And in {read,write}_protection_region() you could add some #ifdef CONFIG_ARM_32 to add the case generators for regions from 16 to 23 since R52 can address them directly.

What do you think?

I got this diff working as it is for R82. This looks much lesser code

xen/arch/arm/mpu/mm.c +4 -9
index fe05c80971..63627c85dc 100644
@@ -28,23 +28,19 @@ DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGIONS);
 /* EL2 Xen MPU memory region mapping table. */
 pr_t xen_mpumap[MAX_MPU_REGIONS];
 
-/* The following are needed for the case generator with num==0 */
-#define PRBAR0_EL2 PRBAR_EL2
-#define PRLAR0_EL2 PRLAR_EL2
-
 #define GENERATE_WRITE_PR_REG_CASE(num, pr)                                 \
     case num: \
 { \
-        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR##num##_EL2);  \
-        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR##num##_EL2);  \
+        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR_EL2);  \
+        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR_EL2);  \
 break; \
     }
 
 #define GENERATE_READ_PR_REG_CASE(num, pr)                      \
     case num:                                                   \
     {                                                           \
-        pr->prbar.bits = READ_SYSREG(PRBAR##num##_EL2);         \
-        pr->prlar.bits = READ_SYSREG(PRLAR##num##_EL2);         \
+        pr->prbar.bits = READ_SYSREG(PRBAR_EL2);         \
+        pr->prlar.bits = READ_SYSREG(PRLAR_EL2);         \
         break;                                                  \
     }
 
@@ -65,7 +61,6 @@ static void prepare_selector(uint8_t sel)
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
-    sel &= 0xF0U;
     if ( READ_SYSREG(PRSELR_EL2) != sel )
     {
         WRITE_SYSREG(sel, PRSELR_EL2);

Please give it a try to see if it works at your end.

And then, the code can be reduced further as

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(sel);

    pr_read->prbar.bits = READ_SYSREG(PRBAR_EL2);

    pr_read->prlar.bits = READ_SYSREG(PRLAR_EL2);

}

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The same can be done for write_protection_region(...)

- Ayan

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Not sure if it is beneficial as you would need to use isb() from region16 onwards.

The isb() is issued only when changing the selector, so when you go from reading/writing regions from 0-15 to 16-31 for example, of course there is a case that if you continuously write on region 15 and 16 you would have to always change the selector, but it’s the less impact we could have.

armv8-R is even better since it’s able to address regions from 0 to 23 without flushing the pipeline, so I would say we should exploit this big advantage.

I’ll send shortly in this thread the code I would use and the code I was thinking you could use.

Quoted 5 lines

If you are going to keep the code as it is, then the following needs to be moved to arm64 specific header as well

#define PRBAR0_EL2 PRBAR_EL2
#define PRLAR0_EL2 PRLAR_EL2

ok I’ll move them.

Cheers,
Luca

#9 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Hi Ayan,

Quoted 10 lines
Quoted 4 lines
Quoted 2 lines

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Not sure if it is beneficial as you would need to use isb() from region16 onwards.

The isb() is issued only when changing the selector, so when you go from reading/writing regions from 0-15 to 16-31 for example, of course there is a case that if you continuously write on region 15 and 16 you would have to always change the selector, but it’s the less impact we could have.

armv8-R is even better since it’s able to address regions from 0 to 23 without flushing the pipeline,

                 ^— aarch32 :)
Quoted 3 lines

so I would say we should exploit this big advantage.

I’ll send shortly in this thread the code I would use and the code I was thinking you could use.

Here the code I have in mind:

static void prepare_selector(uint8_t *sel)

{
    uint8_t cur_sel = *sel;
    /*
     * {read,write}_protection_region works using the direct access to the 0..15
     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
     * only when needed, so when the upper 4 bits of the selector will change.
     */
    cur_sel &= 0xF0U;
    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
    {
        WRITE_SYSREG(cur_sel, PRSELR_EL2);
        isb();
    }
    *sel = *sel & 0xFU;
}

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_READ_PR_REG_CASE(0, pr_read);
        GENERATE_READ_PR_REG_CASE(1, pr_read);
        GENERATE_READ_PR_REG_CASE(2, pr_read);
        GENERATE_READ_PR_REG_CASE(3, pr_read);
        GENERATE_READ_PR_REG_CASE(4, pr_read);
        GENERATE_READ_PR_REG_CASE(5, pr_read);
        GENERATE_READ_PR_REG_CASE(6, pr_read);
        GENERATE_READ_PR_REG_CASE(7, pr_read);
        GENERATE_READ_PR_REG_CASE(8, pr_read);
        GENERATE_READ_PR_REG_CASE(9, pr_read);
        GENERATE_READ_PR_REG_CASE(10, pr_read);
        GENERATE_READ_PR_REG_CASE(11, pr_read);
        GENERATE_READ_PR_REG_CASE(12, pr_read);
        GENERATE_READ_PR_REG_CASE(13, pr_read);
        GENERATE_READ_PR_REG_CASE(14, pr_read);
        GENERATE_READ_PR_REG_CASE(15, pr_read);
    default:
        BUG(); /* Can't happen */
    }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_WRITE_PR_REG_CASE(0, pr_write);
        GENERATE_WRITE_PR_REG_CASE(1, pr_write);
        GENERATE_WRITE_PR_REG_CASE(2, pr_write);
        GENERATE_WRITE_PR_REG_CASE(3, pr_write);
        GENERATE_WRITE_PR_REG_CASE(4, pr_write);
        GENERATE_WRITE_PR_REG_CASE(5, pr_write);
        GENERATE_WRITE_PR_REG_CASE(6, pr_write);
        GENERATE_WRITE_PR_REG_CASE(7, pr_write);
        GENERATE_WRITE_PR_REG_CASE(8, pr_write);
        GENERATE_WRITE_PR_REG_CASE(9, pr_write);
        GENERATE_WRITE_PR_REG_CASE(10, pr_write);
        GENERATE_WRITE_PR_REG_CASE(11, pr_write);
        GENERATE_WRITE_PR_REG_CASE(12, pr_write);
        GENERATE_WRITE_PR_REG_CASE(13, pr_write);
        GENERATE_WRITE_PR_REG_CASE(14, pr_write);
        GENERATE_WRITE_PR_REG_CASE(15, pr_write);
    default:
        BUG(); /* Can't happen */
    }
}

Here the code I thought you could add for arm32:

static void prepare_selector(uint8_t *sel)

{
    uint8_t cur_sel = *sel;
#ifdef CONFIG_ARM_64
    /*
     * {read,write}_protection_region works using the direct access to the 0..15
     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
     * only when needed, so when the upper 4 bits of the selector will change.
     */
    cur_sel &= 0xF0U;
    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
    {
        WRITE_SYSREG(cur_sel, PRSELR_EL2);
        isb();
    }
    *sel = *sel & 0xFU;
#else
    if ( cur_sel > 23 )
        panic("Armv8-R AArch32 region selector exceeds maximum allowed range!");

#endif
}

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_READ_PR_REG_CASE(0, pr_read);
        GENERATE_READ_PR_REG_CASE(1, pr_read);
        GENERATE_READ_PR_REG_CASE(2, pr_read);
        GENERATE_READ_PR_REG_CASE(3, pr_read);
        GENERATE_READ_PR_REG_CASE(4, pr_read);
        GENERATE_READ_PR_REG_CASE(5, pr_read);
        GENERATE_READ_PR_REG_CASE(6, pr_read);
        GENERATE_READ_PR_REG_CASE(7, pr_read);
        GENERATE_READ_PR_REG_CASE(8, pr_read);
        GENERATE_READ_PR_REG_CASE(9, pr_read);
        GENERATE_READ_PR_REG_CASE(10, pr_read);
        GENERATE_READ_PR_REG_CASE(11, pr_read);
        GENERATE_READ_PR_REG_CASE(12, pr_read);
        GENERATE_READ_PR_REG_CASE(13, pr_read);
        GENERATE_READ_PR_REG_CASE(14, pr_read);
        GENERATE_READ_PR_REG_CASE(15, pr_read);
#ifdef CONFIG_ARM_32
        GENERATE_READ_PR_REG_CASE(16, pr_read);
        GENERATE_READ_PR_REG_CASE(17, pr_read);
        GENERATE_READ_PR_REG_CASE(18, pr_read);
        GENERATE_READ_PR_REG_CASE(19, pr_read);
        GENERATE_READ_PR_REG_CASE(20, pr_read);
        GENERATE_READ_PR_REG_CASE(21, pr_read);
        GENERATE_READ_PR_REG_CASE(22, pr_read);
        GENERATE_READ_PR_REG_CASE(23, pr_read);
#endif
    default:
        BUG(); /* Can't happen */
    }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_WRITE_PR_REG_CASE(0, pr_write);
        GENERATE_WRITE_PR_REG_CASE(1, pr_write);
        GENERATE_WRITE_PR_REG_CASE(2, pr_write);
        GENERATE_WRITE_PR_REG_CASE(3, pr_write);
        GENERATE_WRITE_PR_REG_CASE(4, pr_write);
        GENERATE_WRITE_PR_REG_CASE(5, pr_write);
        GENERATE_WRITE_PR_REG_CASE(6, pr_write);
        GENERATE_WRITE_PR_REG_CASE(7, pr_write);
        GENERATE_WRITE_PR_REG_CASE(8, pr_write);
        GENERATE_WRITE_PR_REG_CASE(9, pr_write);
        GENERATE_WRITE_PR_REG_CASE(10, pr_write);
        GENERATE_WRITE_PR_REG_CASE(11, pr_write);
        GENERATE_WRITE_PR_REG_CASE(12, pr_write);
        GENERATE_WRITE_PR_REG_CASE(13, pr_write);
        GENERATE_WRITE_PR_REG_CASE(14, pr_write);
        GENERATE_WRITE_PR_REG_CASE(15, pr_write);
#ifdef CONFIG_ARM_32
        GENERATE_WRITE_PR_REG_CASE(16, pr_write);
        GENERATE_WRITE_PR_REG_CASE(17, pr_write);
        GENERATE_WRITE_PR_REG_CASE(18, pr_write);
        GENERATE_WRITE_PR_REG_CASE(19, pr_write);
        GENERATE_WRITE_PR_REG_CASE(20, pr_write);
        GENERATE_WRITE_PR_REG_CASE(21, pr_write);
        GENERATE_WRITE_PR_REG_CASE(22, pr_write);
        GENERATE_WRITE_PR_REG_CASE(23, pr_write);
#endif
    default:
        BUG(); /* Can't happen */
    }
}

Please let me know your thoughts.

Cheers,
Luca

#10 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Quoted 3 lines

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Not sure if it is beneficial as you would need to use isb() from region16 onwards.

The isb() is issued only when changing the selector, so when you go from reading/writing regions from 0-15 to 16-31 for example, of course there is a case that if you continuously write on region 15 and 16 you would have to always change the selector, but it’s the less impact we could have.

armv8-R is even better since it’s able to address regions from 0 to 23 without flushing the pipeline,

                  ^— aarch32 :)

Can you point me to the document where you got this info ?

Quoted 78 lines
Quoted 4 lines

so I would say we should exploit this big advantage.

I’ll send shortly in this thread the code I would use and the code I was thinking you could use.

Here the code I have in mind:

static void prepare_selector(uint8_t *sel)

{
     uint8_t cur_sel = *sel;
     /*
      * {read,write}_protection_region works using the direct access to the 0..15
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
     cur_sel &= 0xF0U;
     if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
     {
         WRITE_SYSREG(cur_sel, PRSELR_EL2);
         isb();
     }
     *sel = *sel & 0xFU;
}

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_READ_PR_REG_CASE(0, pr_read);
         GENERATE_READ_PR_REG_CASE(1, pr_read);
         GENERATE_READ_PR_REG_CASE(2, pr_read);
         GENERATE_READ_PR_REG_CASE(3, pr_read);
         GENERATE_READ_PR_REG_CASE(4, pr_read);
         GENERATE_READ_PR_REG_CASE(5, pr_read);
         GENERATE_READ_PR_REG_CASE(6, pr_read);
         GENERATE_READ_PR_REG_CASE(7, pr_read);
         GENERATE_READ_PR_REG_CASE(8, pr_read);
         GENERATE_READ_PR_REG_CASE(9, pr_read);
         GENERATE_READ_PR_REG_CASE(10, pr_read);
         GENERATE_READ_PR_REG_CASE(11, pr_read);
         GENERATE_READ_PR_REG_CASE(12, pr_read);
         GENERATE_READ_PR_REG_CASE(13, pr_read);
         GENERATE_READ_PR_REG_CASE(14, pr_read);
         GENERATE_READ_PR_REG_CASE(15, pr_read);
     default:
         BUG(); /* Can't happen */
     }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_WRITE_PR_REG_CASE(0, pr_write);
         GENERATE_WRITE_PR_REG_CASE(1, pr_write);
         GENERATE_WRITE_PR_REG_CASE(2, pr_write);
         GENERATE_WRITE_PR_REG_CASE(3, pr_write);
         GENERATE_WRITE_PR_REG_CASE(4, pr_write);
         GENERATE_WRITE_PR_REG_CASE(5, pr_write);
         GENERATE_WRITE_PR_REG_CASE(6, pr_write);
         GENERATE_WRITE_PR_REG_CASE(7, pr_write);
         GENERATE_WRITE_PR_REG_CASE(8, pr_write);
         GENERATE_WRITE_PR_REG_CASE(9, pr_write);
         GENERATE_WRITE_PR_REG_CASE(10, pr_write);
         GENERATE_WRITE_PR_REG_CASE(11, pr_write);
Quoted 8 lines
         GENERATE_WRITE_PR_REG_CASE(12, pr_write);
         GENERATE_WRITE_PR_REG_CASE(13, pr_write);
         GENERATE_WRITE_PR_REG_CASE(14, pr_write);
         GENERATE_WRITE_PR_REG_CASE(15, pr_write);
     default:
         BUG(); /* Can't happen */
     }
}

Till here I am fine if you think this is the correct approach for arm64.

Quoted 7 lines

Here the code I thought you could add for arm32:

static void prepare_selector(uint8_t *sel)

{
     uint8_t cur_sel = *sel;
Quoted 16 lines
#ifdef CONFIG_ARM_64
     /*
      * {read,write}_protection_region works using the direct access to the 0..15
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
     cur_sel &= 0xF0U;
     if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
     {
         WRITE_SYSREG(cur_sel, PRSELR_EL2);
         isb();
     }
     *sel = *sel & 0xFU;
#else
     if ( cur_sel > 23 )
         panic("Armv8-R AArch32 region selector exceeds maximum allowed range!");

I am not sure about this. See my question before. However ...

Quoted 2 lines

#endif
}

From ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n> - Provides access to the base addresses for the first 32 defined EL2 MPU regions.

E2.2.6 HPRLAR<n> - Provides access to the limit addresses for the first 32 defined EL2 MPU regions.

I understand that HPRSELR is not used when directly accessing the above two registers. Thus, this function will be a nop for Arm32. Please let me know if I am mistaken.

Quoted 29 lines

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_READ_PR_REG_CASE(0, pr_read);
         GENERATE_READ_PR_REG_CASE(1, pr_read);
         GENERATE_READ_PR_REG_CASE(2, pr_read);
         GENERATE_READ_PR_REG_CASE(3, pr_read);
         GENERATE_READ_PR_REG_CASE(4, pr_read);
         GENERATE_READ_PR_REG_CASE(5, pr_read);
         GENERATE_READ_PR_REG_CASE(6, pr_read);
         GENERATE_READ_PR_REG_CASE(7, pr_read);
         GENERATE_READ_PR_REG_CASE(8, pr_read);
         GENERATE_READ_PR_REG_CASE(9, pr_read);
         GENERATE_READ_PR_REG_CASE(10, pr_read);
         GENERATE_READ_PR_REG_CASE(11, pr_read);
         GENERATE_READ_PR_REG_CASE(12, pr_read);
         GENERATE_READ_PR_REG_CASE(13, pr_read);
         GENERATE_READ_PR_REG_CASE(14, pr_read);
         GENERATE_READ_PR_REG_CASE(15, pr_read);
#ifdef CONFIG_ARM_32
Quoted 8 lines
         GENERATE_READ_PR_REG_CASE(16, pr_read);
         GENERATE_READ_PR_REG_CASE(17, pr_read);
         GENERATE_READ_PR_REG_CASE(18, pr_read);
         GENERATE_READ_PR_REG_CASE(19, pr_read);
         GENERATE_READ_PR_REG_CASE(20, pr_read);
         GENERATE_READ_PR_REG_CASE(21, pr_read);
         GENERATE_READ_PR_REG_CASE(22, pr_read);
         GENERATE_READ_PR_REG_CASE(23, pr_read);

At least 32 regions are directly accessible, thus thisn should go till 31 (0-31). Same ..

Quoted 34 lines
#endif
     default:
         BUG(); /* Can't happen */
     }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_WRITE_PR_REG_CASE(0, pr_write);
         GENERATE_WRITE_PR_REG_CASE(1, pr_write);
         GENERATE_WRITE_PR_REG_CASE(2, pr_write);
         GENERATE_WRITE_PR_REG_CASE(3, pr_write);
         GENERATE_WRITE_PR_REG_CASE(4, pr_write);
         GENERATE_WRITE_PR_REG_CASE(5, pr_write);
         GENERATE_WRITE_PR_REG_CASE(6, pr_write);
         GENERATE_WRITE_PR_REG_CASE(7, pr_write);
         GENERATE_WRITE_PR_REG_CASE(8, pr_write);
         GENERATE_WRITE_PR_REG_CASE(9, pr_write);
         GENERATE_WRITE_PR_REG_CASE(10, pr_write);
         GENERATE_WRITE_PR_REG_CASE(11, pr_write);
         GENERATE_WRITE_PR_REG_CASE(12, pr_write);
         GENERATE_WRITE_PR_REG_CASE(13, pr_write);
         GENERATE_WRITE_PR_REG_CASE(14, pr_write);
         GENERATE_WRITE_PR_REG_CASE(15, pr_write);
#ifdef CONFIG_ARM_32
Quoted 8 lines
         GENERATE_WRITE_PR_REG_CASE(16, pr_write);
         GENERATE_WRITE_PR_REG_CASE(17, pr_write);
         GENERATE_WRITE_PR_REG_CASE(18, pr_write);
         GENERATE_WRITE_PR_REG_CASE(19, pr_write);
         GENERATE_WRITE_PR_REG_CASE(20, pr_write);
         GENERATE_WRITE_PR_REG_CASE(21, pr_write);
         GENERATE_WRITE_PR_REG_CASE(22, pr_write);
         GENERATE_WRITE_PR_REG_CASE(23, pr_write);

for here.

Quoted 7 lines
#endif
     default:
         BUG(); /* Can't happen */
     }
}

Please let me know your thoughts.

- Ayan

Quoted 6 lines

Cheers,
Luca

#11 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Hi Ayan,

Quoted 9 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

Not sure if it is beneficial as you would need to use isb() from region16 onwards.

The isb() is issued only when changing the selector, so when you go from reading/writing regions from 0-15 to 16-31 for example, of course there is a case that if you continuously write on region 15 and 16 you would have to always change the selector, but it’s the less impact we could have.

armv8-R is even better since it’s able to address regions from 0 to 23 without flushing the pipeline,

                 ^— aarch32 :)

Can you point me to the document where you got this info ?

was referring to the r52, of course you are more knowledgeable on the armv8-r aarch32 part, so ...

Quoted 86 lines
Quoted 78 lines
Quoted 4 lines

so I would say we should exploit this big advantage.

I’ll send shortly in this thread the code I would use and the code I was thinking you could use.

Here the code I have in mind:

static void prepare_selector(uint8_t *sel)

{
    uint8_t cur_sel = *sel;
    /*
     * {read,write}_protection_region works using the direct access to the 0..15
     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
     * only when needed, so when the upper 4 bits of the selector will change.
     */
    cur_sel &= 0xF0U;
    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
    {
        WRITE_SYSREG(cur_sel, PRSELR_EL2);
        isb();
    }
    *sel = *sel & 0xFU;
}

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_READ_PR_REG_CASE(0, pr_read);
        GENERATE_READ_PR_REG_CASE(1, pr_read);
        GENERATE_READ_PR_REG_CASE(2, pr_read);
        GENERATE_READ_PR_REG_CASE(3, pr_read);
        GENERATE_READ_PR_REG_CASE(4, pr_read);
        GENERATE_READ_PR_REG_CASE(5, pr_read);
        GENERATE_READ_PR_REG_CASE(6, pr_read);
        GENERATE_READ_PR_REG_CASE(7, pr_read);
        GENERATE_READ_PR_REG_CASE(8, pr_read);
        GENERATE_READ_PR_REG_CASE(9, pr_read);
        GENERATE_READ_PR_REG_CASE(10, pr_read);
        GENERATE_READ_PR_REG_CASE(11, pr_read);
        GENERATE_READ_PR_REG_CASE(12, pr_read);
        GENERATE_READ_PR_REG_CASE(13, pr_read);
        GENERATE_READ_PR_REG_CASE(14, pr_read);
        GENERATE_READ_PR_REG_CASE(15, pr_read);
    default:
        BUG(); /* Can't happen */
    }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_WRITE_PR_REG_CASE(0, pr_write);
        GENERATE_WRITE_PR_REG_CASE(1, pr_write);
        GENERATE_WRITE_PR_REG_CASE(2, pr_write);
        GENERATE_WRITE_PR_REG_CASE(3, pr_write);
        GENERATE_WRITE_PR_REG_CASE(4, pr_write);
        GENERATE_WRITE_PR_REG_CASE(5, pr_write);
        GENERATE_WRITE_PR_REG_CASE(6, pr_write);
        GENERATE_WRITE_PR_REG_CASE(7, pr_write);
        GENERATE_WRITE_PR_REG_CASE(8, pr_write);
        GENERATE_WRITE_PR_REG_CASE(9, pr_write);
        GENERATE_WRITE_PR_REG_CASE(10, pr_write);
        GENERATE_WRITE_PR_REG_CASE(11, pr_write);
Quoted 8 lines
        GENERATE_WRITE_PR_REG_CASE(12, pr_write);
        GENERATE_WRITE_PR_REG_CASE(13, pr_write);
        GENERATE_WRITE_PR_REG_CASE(14, pr_write);
        GENERATE_WRITE_PR_REG_CASE(15, pr_write);
    default:
        BUG(); /* Can't happen */
    }
}
Quoted 1 line

Till here I am fine if you think this is the correct approach for arm64.

Ok

Quoted 27 lines
Quoted 7 lines

Here the code I thought you could add for arm32:

static void prepare_selector(uint8_t *sel)

{
    uint8_t cur_sel = *sel;
Quoted 16 lines
#ifdef CONFIG_ARM_64
    /*
     * {read,write}_protection_region works using the direct access to the 0..15
     * regions, so in order to save the isb() overhead, change the PRSELR_EL2
     * only when needed, so when the upper 4 bits of the selector will change.
     */
    cur_sel &= 0xF0U;
    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
    {
        WRITE_SYSREG(cur_sel, PRSELR_EL2);
        isb();
    }
    *sel = *sel & 0xFU;
#else
    if ( cur_sel > 23 )
        panic("Armv8-R AArch32 region selector exceeds maximum allowed range!");

I am not sure about this. See my question before. However ...

Quoted 2 lines

#endif
}

Quoted 7 lines

From ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n> - Provides access to the base addresses for the first 32 defined EL2 MPU regions.

E2.2.6 HPRLAR<n> - Provides access to the limit addresses for the first 32 defined EL2 MPU regions.

I understand that HPRSELR is not used when directly accessing the above two registers. Thus, this function will be a nop for Arm32. Please let me know if I am mistaken.

yes you are right, you can decide if doing something or not in that function, you don’t need to change the selector.

Quoted 38 lines
Quoted 30 lines

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_READ_PR_REG_CASE(0, pr_read);
        GENERATE_READ_PR_REG_CASE(1, pr_read);
        GENERATE_READ_PR_REG_CASE(2, pr_read);
        GENERATE_READ_PR_REG_CASE(3, pr_read);
        GENERATE_READ_PR_REG_CASE(4, pr_read);
        GENERATE_READ_PR_REG_CASE(5, pr_read);
        GENERATE_READ_PR_REG_CASE(6, pr_read);
        GENERATE_READ_PR_REG_CASE(7, pr_read);
        GENERATE_READ_PR_REG_CASE(8, pr_read);
        GENERATE_READ_PR_REG_CASE(9, pr_read);
        GENERATE_READ_PR_REG_CASE(10, pr_read);
        GENERATE_READ_PR_REG_CASE(11, pr_read);
        GENERATE_READ_PR_REG_CASE(12, pr_read);
        GENERATE_READ_PR_REG_CASE(13, pr_read);
        GENERATE_READ_PR_REG_CASE(14, pr_read);
        GENERATE_READ_PR_REG_CASE(15, pr_read);
#ifdef CONFIG_ARM_32
Quoted 8 lines
        GENERATE_READ_PR_REG_CASE(16, pr_read);
        GENERATE_READ_PR_REG_CASE(17, pr_read);
        GENERATE_READ_PR_REG_CASE(18, pr_read);
        GENERATE_READ_PR_REG_CASE(19, pr_read);
        GENERATE_READ_PR_REG_CASE(20, pr_read);
        GENERATE_READ_PR_REG_CASE(21, pr_read);
        GENERATE_READ_PR_REG_CASE(22, pr_read);
        GENERATE_READ_PR_REG_CASE(23, pr_read);
Quoted 1 line

At least 32 regions are directly accessible, thus thisn should go till 31 (0-31). Same ..

yeah you can add until the 31st here and ...

Quoted 42 lines
Quoted 34 lines
#endif
    default:
        BUG(); /* Can't happen */
    }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
    /*
     * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
     * make sure PRSELR_EL2 is set, as it determines which MPU region
     * is selected.
     */
    prepare_selector(&sel);

    switch ( sel )
    {
        GENERATE_WRITE_PR_REG_CASE(0, pr_write);
        GENERATE_WRITE_PR_REG_CASE(1, pr_write);
        GENERATE_WRITE_PR_REG_CASE(2, pr_write);
        GENERATE_WRITE_PR_REG_CASE(3, pr_write);
        GENERATE_WRITE_PR_REG_CASE(4, pr_write);
        GENERATE_WRITE_PR_REG_CASE(5, pr_write);
        GENERATE_WRITE_PR_REG_CASE(6, pr_write);
        GENERATE_WRITE_PR_REG_CASE(7, pr_write);
        GENERATE_WRITE_PR_REG_CASE(8, pr_write);
        GENERATE_WRITE_PR_REG_CASE(9, pr_write);
        GENERATE_WRITE_PR_REG_CASE(10, pr_write);
        GENERATE_WRITE_PR_REG_CASE(11, pr_write);
        GENERATE_WRITE_PR_REG_CASE(12, pr_write);
        GENERATE_WRITE_PR_REG_CASE(13, pr_write);
        GENERATE_WRITE_PR_REG_CASE(14, pr_write);
        GENERATE_WRITE_PR_REG_CASE(15, pr_write);
#ifdef CONFIG_ARM_32
Quoted 8 lines
        GENERATE_WRITE_PR_REG_CASE(16, pr_write);
        GENERATE_WRITE_PR_REG_CASE(17, pr_write);
        GENERATE_WRITE_PR_REG_CASE(18, pr_write);
        GENERATE_WRITE_PR_REG_CASE(19, pr_write);
        GENERATE_WRITE_PR_REG_CASE(20, pr_write);
        GENERATE_WRITE_PR_REG_CASE(21, pr_write);
        GENERATE_WRITE_PR_REG_CASE(22, pr_write);
        GENERATE_WRITE_PR_REG_CASE(23, pr_write);
Quoted 1 line

for here.

also here.

So I guess we are aligned for this patch right? I will update this patch as the code above and you will modify the code for arm32 to support the direct access up to 32 region. Ok?

Cheers,
Luca

#12 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi,

Quoted 10 lines
Quoted 10 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Not sure if it is beneficial as you would need to use isb() from region16 onwards.

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

The isb() is issued only when changing the selector, so when you go from reading/writing regions from 0-15 to 16-31 for example, of course there is a case that if you continuously write on region 15 and 16 you would have to always change the selector, but it’s the less impact we could have.

armv8-R is even better since it’s able to address regions from 0 to 23 without flushing the pipeline,

                  ^— aarch32 :)

Can you point me to the document where you got this info ?

was referring to the r52, of course you are more knowledgeable on the armv8-r aarch32 part, so ...

If you can point me to the page and section within R52 TRM, I can take a look to see if I missed something (which isn't suprising as the docs are huge :))

Quoted 205 lines
Quoted 86 lines
Quoted 78 lines
Quoted 4 lines

so I would say we should exploit this big advantage.

I’ll send shortly in this thread the code I would use and the code I was thinking you could use.

Here the code I have in mind:

static void prepare_selector(uint8_t *sel)

{
     uint8_t cur_sel = *sel;
     /*
      * {read,write}_protection_region works using the direct access to the 0..15
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
     cur_sel &= 0xF0U;
     if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
     {
         WRITE_SYSREG(cur_sel, PRSELR_EL2);
         isb();
     }
     *sel = *sel & 0xFU;
}

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_READ_PR_REG_CASE(0, pr_read);
         GENERATE_READ_PR_REG_CASE(1, pr_read);
         GENERATE_READ_PR_REG_CASE(2, pr_read);
         GENERATE_READ_PR_REG_CASE(3, pr_read);
         GENERATE_READ_PR_REG_CASE(4, pr_read);
         GENERATE_READ_PR_REG_CASE(5, pr_read);
         GENERATE_READ_PR_REG_CASE(6, pr_read);
         GENERATE_READ_PR_REG_CASE(7, pr_read);
         GENERATE_READ_PR_REG_CASE(8, pr_read);
         GENERATE_READ_PR_REG_CASE(9, pr_read);
         GENERATE_READ_PR_REG_CASE(10, pr_read);
         GENERATE_READ_PR_REG_CASE(11, pr_read);
         GENERATE_READ_PR_REG_CASE(12, pr_read);
         GENERATE_READ_PR_REG_CASE(13, pr_read);
         GENERATE_READ_PR_REG_CASE(14, pr_read);
         GENERATE_READ_PR_REG_CASE(15, pr_read);
     default:
         BUG(); /* Can't happen */
     }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_WRITE_PR_REG_CASE(0, pr_write);
         GENERATE_WRITE_PR_REG_CASE(1, pr_write);
         GENERATE_WRITE_PR_REG_CASE(2, pr_write);
         GENERATE_WRITE_PR_REG_CASE(3, pr_write);
         GENERATE_WRITE_PR_REG_CASE(4, pr_write);
         GENERATE_WRITE_PR_REG_CASE(5, pr_write);
         GENERATE_WRITE_PR_REG_CASE(6, pr_write);
         GENERATE_WRITE_PR_REG_CASE(7, pr_write);
         GENERATE_WRITE_PR_REG_CASE(8, pr_write);
         GENERATE_WRITE_PR_REG_CASE(9, pr_write);
         GENERATE_WRITE_PR_REG_CASE(10, pr_write);
         GENERATE_WRITE_PR_REG_CASE(11, pr_write);
Quoted 8 lines
         GENERATE_WRITE_PR_REG_CASE(12, pr_write);
         GENERATE_WRITE_PR_REG_CASE(13, pr_write);
         GENERATE_WRITE_PR_REG_CASE(14, pr_write);
         GENERATE_WRITE_PR_REG_CASE(15, pr_write);
     default:
         BUG(); /* Can't happen */
     }
}
Quoted 1 line

Till here I am fine if you think this is the correct approach for arm64.

Ok

Quoted 25 lines
Quoted 6 lines

Here the code I thought you could add for arm32:

static void prepare_selector(uint8_t *sel)

{
     uint8_t cur_sel = *sel;
Quoted 16 lines
#ifdef CONFIG_ARM_64
     /*
      * {read,write}_protection_region works using the direct access to the 0..15
      * regions, so in order to save the isb() overhead, change the PRSELR_EL2
      * only when needed, so when the upper 4 bits of the selector will change.
      */
     cur_sel &= 0xF0U;
     if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
     {
         WRITE_SYSREG(cur_sel, PRSELR_EL2);
         isb();
     }
     *sel = *sel & 0xFU;
#else
     if ( cur_sel > 23 )
         panic("Armv8-R AArch32 region selector exceeds maximum allowed range!");

I am not sure about this. See my question before. However ...

Quoted 2 lines

#endif
}

Quoted 7 lines

From ARM DDI 0568A.c ID110520

E2.2.3 HPRBAR<n> - Provides access to the base addresses for the first 32 defined EL2 MPU regions.

E2.2.6 HPRLAR<n> - Provides access to the limit addresses for the first 32 defined EL2 MPU regions.

I understand that HPRSELR is not used when directly accessing the above two registers. Thus, this function will be a nop for Arm32. Please let me know if I am mistaken.

yes you are right, you can decide if doing something or not in that function, you don’t need to change the selector.

Quoted 36 lines
Quoted 28 lines

void read_protection_region(pr_t *pr_read, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_READ_PR_REG_CASE(0, pr_read);
         GENERATE_READ_PR_REG_CASE(1, pr_read);
         GENERATE_READ_PR_REG_CASE(2, pr_read);
         GENERATE_READ_PR_REG_CASE(3, pr_read);
         GENERATE_READ_PR_REG_CASE(4, pr_read);
         GENERATE_READ_PR_REG_CASE(5, pr_read);
         GENERATE_READ_PR_REG_CASE(6, pr_read);
         GENERATE_READ_PR_REG_CASE(7, pr_read);
         GENERATE_READ_PR_REG_CASE(8, pr_read);
         GENERATE_READ_PR_REG_CASE(9, pr_read);
         GENERATE_READ_PR_REG_CASE(10, pr_read);
         GENERATE_READ_PR_REG_CASE(11, pr_read);
         GENERATE_READ_PR_REG_CASE(12, pr_read);
         GENERATE_READ_PR_REG_CASE(13, pr_read);
         GENERATE_READ_PR_REG_CASE(14, pr_read);
         GENERATE_READ_PR_REG_CASE(15, pr_read);
#ifdef CONFIG_ARM_32
Quoted 8 lines
         GENERATE_READ_PR_REG_CASE(16, pr_read);
         GENERATE_READ_PR_REG_CASE(17, pr_read);
         GENERATE_READ_PR_REG_CASE(18, pr_read);
         GENERATE_READ_PR_REG_CASE(19, pr_read);
         GENERATE_READ_PR_REG_CASE(20, pr_read);
         GENERATE_READ_PR_REG_CASE(21, pr_read);
         GENERATE_READ_PR_REG_CASE(22, pr_read);
         GENERATE_READ_PR_REG_CASE(23, pr_read);
Quoted 1 line

At least 32 regions are directly accessible, thus thisn should go till 31 (0-31). Same ..

yeah you can add until the 31st here and ...

Quoted 42 lines
Quoted 42 lines
#endif
     default:
         BUG(); /* Can't happen */
     }
}

void write_protection_region(const pr_t *pr_write, uint8_t sel)

{
     /*
      * Before accessing EL2 MPU region register PRBAR_EL2/PRLAR_EL2,
      * make sure PRSELR_EL2 is set, as it determines which MPU region
      * is selected.
      */
     prepare_selector(&sel);

     switch ( sel )
     {
         GENERATE_WRITE_PR_REG_CASE(0, pr_write);
         GENERATE_WRITE_PR_REG_CASE(1, pr_write);
         GENERATE_WRITE_PR_REG_CASE(2, pr_write);
         GENERATE_WRITE_PR_REG_CASE(3, pr_write);
         GENERATE_WRITE_PR_REG_CASE(4, pr_write);
         GENERATE_WRITE_PR_REG_CASE(5, pr_write);
         GENERATE_WRITE_PR_REG_CASE(6, pr_write);
         GENERATE_WRITE_PR_REG_CASE(7, pr_write);
         GENERATE_WRITE_PR_REG_CASE(8, pr_write);
         GENERATE_WRITE_PR_REG_CASE(9, pr_write);
         GENERATE_WRITE_PR_REG_CASE(10, pr_write);
         GENERATE_WRITE_PR_REG_CASE(11, pr_write);
         GENERATE_WRITE_PR_REG_CASE(12, pr_write);
         GENERATE_WRITE_PR_REG_CASE(13, pr_write);
         GENERATE_WRITE_PR_REG_CASE(14, pr_write);
         GENERATE_WRITE_PR_REG_CASE(15, pr_write);
#ifdef CONFIG_ARM_32
         GENERATE_WRITE_PR_REG_CASE(16, pr_write);
         GENERATE_WRITE_PR_REG_CASE(17, pr_write);
         GENERATE_WRITE_PR_REG_CASE(18, pr_write);
         GENERATE_WRITE_PR_REG_CASE(19, pr_write);
         GENERATE_WRITE_PR_REG_CASE(20, pr_write);
         GENERATE_WRITE_PR_REG_CASE(21, pr_write);
         GENERATE_WRITE_PR_REG_CASE(22, pr_write);
         GENERATE_WRITE_PR_REG_CASE(23, pr_write);
Quoted 5 lines
Quoted 1 line

for here.

also here.

So I guess we are aligned for this patch right? I will update this patch as the code above and you will modify the code for arm32 to support the direct access up to 32 region. Ok?

This is ok.

- Ayan

Quoted 5 lines

Cheers,
Luca

#13 lore raw

Re: [PATCH v2 2/7] arm/mpu: Provide access to the MPU region from the C code

Hi Ayan,

Quoted 20 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi,

Quoted 14 lines
Quoted 14 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

The point of the code was to don’t issue an isb() every time we change the selector, of course the code would be easier otherwise, but do we want to do that?

Not sure if it is beneficial as you would need to use isb() from region16 onwards.

The isb() is issued only when changing the selector, so when you go from reading/writing regions from 0-15 to 16-31 for example, of course there is a case that if you continuously write on region 15 and 16 you would have to always change the selector, but it’s the less impact we could have.

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

armv8-R is even better since it’s able to address regions from 0 to 23 without flushing the pipeline,

                 ^— aarch32 :)

Can you point me to the document where you got this info ?

was referring to the r52, of course you are more knowledgeable on the armv8-r aarch32 part, so ...

If you can point me to the page and section within R52 TRM, I can take a look to see if I missed something (which isn't suprising as the docs are huge :))

Arm® Cortex®-R52 Processor TRM, r1p1, 8.5 MPU Register summary, I can see HPRBAR going from 0 to 24 (so I was wrong above, there should be 25 regions?). Anyway we should follow the armv8-r aarch32 specs here as you mentioned before, so max 32 region. Anyway you are the developer of the arm32 code so I’m ok with your choices!

Cheers,
Luca

#14 lore raw

PATCH v2 3/7 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/mpu.h | 40 ++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
xen/arch/arm/include/asm/mpu.h +40 -0
index 59ff22c804c1..6971507457fb 100644
@@ -20,6 +20,46 @@
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
 #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 
+#ifndef __ASSEMBLY__
+
+/* 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 /* __ARM_MPU_H__ */
 
 /*
Signature · 1 line
2.34.1
#15 lore raw

PATCH v2 4/7 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 | 11 +++++
 xen/arch/arm/include/asm/mpu.h       |  4 ++
 xen/arch/arm/include/asm/mpu/mm.h    |  3 ++
 xen/arch/arm/mpu/mm.c                | 73 ++++++++++++++++++++++++++++
 4 files changed, 91 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +11 -0
index 4d2bd7d7877f..7cf8d355a1af 100644
@@ -6,6 +6,17 @@
 #ifndef __ARM_ARM64_MPU_H__
 #define __ARM_ARM64_MPU_H__
 
+/*
+ * 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_EL2_ENABLED  0x2
+
 #ifndef __ASSEMBLY__
 
 /* Protection Region Base Address Register */
xen/arch/arm/include/asm/mpu.h +4 -0
index 6971507457fb..eba5086cde97 100644
@@ -20,6 +20,10 @@
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
 #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
+
 #ifndef __ASSEMBLY__
 
 /* Set base address of MPU protection region(pr_t). */
xen/arch/arm/include/asm/mpu/mm.h +3 -0
index 5cabe9d111ce..9c7a01d6bd58 100644
@@ -53,6 +53,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_H__ */
 
 /*
xen/arch/arm/mpu/mm.c +73 -0

85 lines not on this page · raw message

Signature · 1 line
2.34.1
#16 lore raw

PATCH v2 5/7 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
#17 lore raw

PATCH v2 6/7 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/setup.c | 54 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
xen/arch/arm/mpu/setup.c +52 -2
index b4da77003f47..5969065250d4 100644
@@ -1,17 +1,67 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/bootfdt.h>
 #include <xen/bug.h>
 #include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
 #include <xen/mm.h>
+#include <xen/pfn.h>
 #include <xen/types.h>
+#include <asm/mpu.h>
+#include <asm/page.h>
 #include <asm/setup.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 setup_pagetables(void) {}
 
 void * __init early_fdt_map(paddr_t fdt_paddr)
 {
-    BUG_ON("unimplemented");
-    return NULL;
+    /* 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
+     * 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));
+    fdt_region.prbar.reg.ap = PAGE_AP_MASK(flags);
+    fdt_region.prbar.reg.xn = PAGE_XN_MASK(flags);
+
+    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.
+     */
+    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;
 }
 
 /*
Signature · 1 line
2.34.1
#18 lore raw

PATCH v2 7/7 arm/mpu: Implement setup_mpu for MPU system

In reply to Luca Fancellu

Implement the function setup_mpu that will logically track the MPU regions defined by hardware registers, start introducing data structures and functions to track the status from the C world.

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

This function is called from setup_mm() which full implementation will be provided in a later stage.

---
 xen/arch/arm/include/asm/arm64/mpu.h |  2 ++
 xen/arch/arm/mpu/mm.c                | 49 +++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index 7cf8d355a1af..ff5a957bf085 100644
@@ -6,6 +6,8 @@
 #ifndef __ARM_ARM64_MPU_H__
 #define __ARM_ARM64_MPU_H__
 
+#define PRENR_MASK  GENMASK(31, 0)
+
 /*
  * Excute never.
  * Stage 1 EL2 translation regime.
xen/arch/arm/mpu/mm.c +48 -1

63 lines not on this page · raw message

Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH v2 7/7] arm/mpu: Implement setup_mpu for MPU system

Hi Luca,

Quoted 24 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 the function setup_mpu that will logically track the MPU regions defined by hardware registers, start introducing data structures and functions to track the status from the C world.

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

This function is called from setup_mm() which full implementation will be provided in a later stage.

---
  xen/arch/arm/include/asm/arm64/mpu.h |  2 ++
  xen/arch/arm/mpu/mm.c                | 49 +++++++++++++++++++++++++++-
  2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 7cf8d355a1af..ff5a957bf085 100644

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

Quoted 4 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
@@ -6,6 +6,8 @@
  #ifndef __ARM_ARM64_MPU_H__
  #define __ARM_ARM64_MPU_H__
 
+#define PRENR_MASK  GENMASK(31, 0)

This can be moved to xen/arch/arm/include/asm/mpu.h

Quoted 74 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+
  /*
   * Excute never.
   * Stage 1 EL2 translation regime.
xen/arch/arm/mpu/mm.c +48 -2
index 2c5820a44f13..fe05c8097155 100644
@@ -14,6 +14,17 @@
 
  struct page_info *frame_table;
 
+/* 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];
 
@@ -224,9 +235,45 @@ pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr)
      return region;
  }
 
+/*
+ * The code in this function needs to track the regions programmed in
+ * arm64/mpu/head.S
+ */
+static void __init setup_mpu(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 setup_mm(void)
  {
-    BUG_ON("unimplemented");
+    setup_mpu();
  }
 
  int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
--
Quoted 1 line

2.34.1

- Ayan

Quoted 1 line
#20 lore raw

Re: [PATCH v2 7/7] arm/mpu: Implement setup_mpu for MPU system

Hi Ayan,

Quoted 32 lines · Ayan Kumar Halder

Hi Luca,

Quoted 27 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 the function setup_mpu that will logically track the MPU regions defined by hardware registers, start introducing data structures and functions to track the status from the C world.

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

This function is called from setup_mm() which full implementation will be provided in a later stage.

---
 xen/arch/arm/include/asm/arm64/mpu.h |  2 ++
 xen/arch/arm/mpu/mm.c                | 49 +++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 7cf8d355a1af..ff5a957bf085 100644
@@ -6,6 +6,8 @@
 #ifndef __ARM_ARM64_MPU_H__
 #define __ARM_ARM64_MPU_H__
 
Quoted 2 lines

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

Quoted 1 line
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+#define PRENR_MASK  GENMASK(31, 0)

This can be moved to xen/arch/arm/include/asm/mpu.h

I can see from the Arm® Cortex®-R52 Processor TRM, r1p1, 3.3.46 Hyp MPU Region Enable Register, that HPRENR can have 16/20/24 enabled bits, the rest is RAZ.

Because of that I’ve put that here, maybe you should implement it in the arm32 part since you have this difference from Arm64.

Cheers,
Luca

#21 lore raw

Re: [PATCH v2 7/7] arm/mpu: Implement setup_mpu for MPU system

Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 33 lines
Quoted 33 lines · Ayan Kumar Halder

Hi Luca,

Quoted 27 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 the function setup_mpu that will logically track the MPU regions defined by hardware registers, start introducing data structures and functions to track the status from the C world.

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

This function is called from setup_mm() which full implementation will be provided in a later stage.

---
  xen/arch/arm/include/asm/arm64/mpu.h |  2 ++
  xen/arch/arm/mpu/mm.c                | 49 +++++++++++++++++++++++++++-
  2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 7cf8d355a1af..ff5a957bf085 100644
@@ -6,6 +6,8 @@
  #ifndef __ARM_ARM64_MPU_H__
  #define __ARM_ARM64_MPU_H__
 
Quoted 4 lines
Quoted 2 lines

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

Quoted 1 line
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+#define PRENR_MASK  GENMASK(31, 0)

This can be moved to xen/arch/arm/include/asm/mpu.h

I can see from the Arm® Cortex®-R52 Processor TRM, r1p1, 3.3.46 Hyp MPU Region Enable Register, that HPRENR can have 16/20/24 enabled bits, the rest is RAZ.

Yes, I see that. However, if you see ARM DDI 0568A.c ID110520 ArmV8-R32 supplement

E2.2.4 HPRENR

Provides direct access to the HPRLAR.EN bits for EL2 MPU regions 0 to 31.

AFAIU, the maximum number of EL2 MPU regions will always be 32. This doesnot seem to be configurable.

The R52 TRM isn't clear when it says "{no,16,20,24} EL2-controlled MPU regions implemented" as there is no register to set/check how many EL2 MPU regions are implemented.

So, I suggest we can keep this in the common file for now.

- Ayan

Quoted 7 lines

Because of that I’ve put that here, maybe you should implement it in the arm32 part since you have this difference from Arm64.

Cheers,
Luca