Skip to contentSkip to conversation

First chunk for Arm R82 and MPU support

PATCH Superseded Luca Fancellu 3 participants 21 messages May 13, 2025 – May 22, 2025

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 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 just before early_fdt_map(), just to give an idea about which stage of the boot is reached.

v5:
- dropped patch that touches page.h, it is not needed
- general fixes listed on each patch
v4:
- dropped setup_mpu() patch and early_fdt_map() patch (needs rework)
- add new patches: boot protocol and early asm MPU structure update
- general fixes listed on each patch
v3 changes:
- stated on each patch
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 (5):
docs/arm: Document Xen booting protocol on Armv8-R arm/mpu: Provide and populate MPU C data structures 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

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

 docs/misc/arm/booting.txt                |  11 +-
 xen/arch/arm/arm64/mpu/head.S            |  13 ++
 xen/arch/arm/include/asm/arm32/mpu.h     |  25 +++
 xen/arch/arm/include/asm/arm64/mpu.h     |  54 ++++++
 xen/arch/arm/include/asm/bitmap-op.inc   |  67 ++++++++
 xen/arch/arm/include/asm/mpu.h           |  74 +++++++++
 xen/arch/arm/include/asm/mpu/mm.h        |  51 ++++++
 xen/arch/arm/include/asm/mpu/regions.inc |  49 ++++++
 xen/arch/arm/mpu/mm.c                    | 201 +++++++++++++++++++++++
 9 files changed, 542 insertions(+), 3 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
 create mode 100644 xen/arch/arm/include/asm/bitmap-op.inc
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/6 docs/arm: Document Xen booting protocol on Armv8-R

Document the requirement needed to boot Xen on Armv8-R platforms.

---
v5 changes:
- restructured and removed some EL3 reference that might not be there on Armv8-R aarch64
- add R-by Ayan and Michal
v4 changes:
- New patch

---
 docs/misc/arm/booting.txt | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
docs/misc/arm/booting.txt +8 -3
index 21ae74837dcc..ca05274392be 100644
@@ -56,12 +56,17 @@ image header to determine the load address, entry point, etc.
 Firmware/bootloader requirements
 --------------------------------
 
-Xen relies on some settings the firmware has to configure in EL3 before starting Xen.
+Xen relies on some settings the firmware has to configure before starting Xen.
 
-* Xen must be entered in NS EL2 mode
+* Xen must be entered in:
+  * Non-Secure EL2 mode for Armv8-A Arm64 and Arm32, Armv8-R Arm32.
+  * Secure EL2 mode for Armv8-R Arm64.
 
-* The bit SCR_EL3.HCE (resp. SCR.HCE for 32-bit ARM) must be set to 1.
+* When EL3 is supported, the bit SCR_EL3.HCE (resp. SCR.HCE for 32-bit ARM) must
+  be set to 1.
 
+* Xen must be entered with MMU/MPU off and data cache disabled (SCTLR_EL2.M bit
+  and SCTLR_EL2.C set to 0).
 
 [1] linux/Documentation/arm/booting.rst
 Latest version: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/arch/arm/booting.rst
Signature · 1 line
2.34.1
Acked-by on v5 1/6
#2 lore raw

Re: [PATCH v5 1/6] docs/arm: Document Xen booting protocol on Armv8-R

Hi Luca,

Quoted 27 lines · Luca Fancellu

Document the requirement needed to boot Xen on Armv8-R platforms.

---> v5 changes:
- restructured and removed some EL3 reference that might not be there on Armv8-R aarch64
- add R-by Ayan and Michal
v4 changes:
- New patch

---
  docs/misc/arm/booting.txt | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
docs/misc/arm/booting.txt +1 -2
index 21ae74837dcc..ca05274392be 100644
@@ -56,12 +56,17 @@ image header to determine the load address, entry point, etc.
  Firmware/bootloader requirements
  --------------------------------
 
-Xen relies on some settings the firmware has to configure in EL3 before starting Xen.
+Xen relies on some settings the firmware has to configure before starting Xen.
 
-* Xen must be entered in NS EL2 mode

docs/misc/arm/booting.txt image header to determine the load address, entry point, etc. · line 69

Quoted 10 lines
docs/misc/arm/booting.txt +7 -1
+* Xen must be entered in:
+  * Non-Secure EL2 mode for Armv8-A Arm64 and Arm32, Armv8-R Arm32.
+  * Secure EL2 mode for Armv8-R Arm64.
 
-* The bit SCR_EL3.HCE (resp. SCR.HCE for 32-bit ARM) must be set to 1.
+* When EL3 is supported, the bit SCR_EL3.HCE (resp. SCR.HCE for 32-bit ARM) must
+  be set to 1.
 
+* Xen must be entered with MMU/MPU off and data cache disabled (SCTLR_EL2.M bit
+  and SCTLR_EL2.C set to 0).

The document read much better. Thanks for the update:

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

PATCH v5 2/6 arm/mpu: Introduce MPU memory region map structure

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch64 Armv8-R architecture.

---
Changes in v5:
- Given some comments on the page.h flags, I had to rework some fields here to better match the flags usage and avoid duplication
Changes in v4:
- Fixed typos, changed name for reserved bitfields, add emacs bits to arm64/mpu.h.
Now base and limit are 42 bits as we consider FEAT_LPA disabled, since we support max 1TB of memory.
Moved data structure in commit that uses it

---
 xen/arch/arm/include/asm/arm64/mpu.h | 52 ++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/mpu.h       |  4 +++
 2 files changed, 56 insertions(+)
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
xen/arch/arm/include/asm/arm64/mpu.h new file +52 -0
new file mode 100644
index 000000000000..d3c055a2e53b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_ARM64_MPU_H__
+#define __ARM_ARM64_MPU_H__
+
+#ifndef __ASSEMBLY__
+
+/* Protection Region Base Address Register */
+typedef union {
+    struct __packed {
+        unsigned long xn_0:1;     /* Execute-Never XN[0] */
+        unsigned long xn:1;       /* Execute-Never XN[1] */
+        unsigned long ap_0:1;     /* Access Permission AP[0] */
+        unsigned long ro:1;       /* Access Permission AP[1] */
+        unsigned long sh:2;       /* Shareability */
+        unsigned long base:42;    /* Base Address */
+        unsigned long res0:16;    /* RES0 */
+    } 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 res0:1;   /* RES0 */
+        unsigned long limit:42; /* Limit Address */
+        unsigned long res1:16;  /* RES0 */
+    } 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__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +4 -0
index d4ec4248b62b..bb83f5a5f580 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))
Signature · 1 line
2.34.1
Acked-by on v5 2/6
#4 lore raw

Re: [PATCH v5 2/6] arm/mpu: Introduce MPU memory region map structure

Hi Luca,

Quoted 9 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch64 Armv8-R architecture.

Cheers,

Quoted 89 lines

---
Changes in v5:
- Given some comments on the page.h flags, I had to rework some fields here to better match the flags usage and avoid duplication
Changes in v4:
- Fixed typos, changed name for reserved bitfields, add emacs bits to arm64/mpu.h.
Now base and limit are 42 bits as we consider FEAT_LPA disabled, since we support max 1TB of memory.
Moved data structure in commit that uses it

---
  xen/arch/arm/include/asm/arm64/mpu.h | 52 ++++++++++++++++++++++++++++
  xen/arch/arm/include/asm/mpu.h       |  4 +++
  2 files changed, 56 insertions(+)

create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h

xen/arch/arm/include/asm/arm64/mpu.h new file +52 -0
new file mode 100644
index 000000000000..d3c055a2e53b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_ARM64_MPU_H__
+#define __ARM_ARM64_MPU_H__
+
+#ifndef __ASSEMBLY__
+
+/* Protection Region Base Address Register */
+typedef union {
+    struct __packed {
+        unsigned long xn_0:1;     /* Execute-Never XN[0] */
+        unsigned long xn:1;       /* Execute-Never XN[1] */
+        unsigned long ap_0:1;     /* Access Permission AP[0] */
+        unsigned long ro:1;       /* Access Permission AP[1] */
+        unsigned long sh:2;       /* Shareability */
+        unsigned long base:42;    /* Base Address */
+        unsigned long res0:16;    /* RES0 */
+    } 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 res0:1;   /* RES0 */
+        unsigned long limit:42; /* Limit Address */
+        unsigned long res1:16;  /* RES0 */
+    } 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__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +4 -0
index d4ec4248b62b..bb83f5a5f580 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))
Signature · 1 line
Julien Grall
Reviewed-by on v5 2/6
#5 lore raw

Re: [PATCH v5 2/6] arm/mpu: Introduce MPU memory region map structure

In reply to Luca Fancellu
Quoted 31 lines · Luca Fancellu

From: Penny Zheng <[email protected]>

Introduce pr_t typedef which is a structure having the prbar and prlar members, each being structured as the registers of the AArch64 Armv8-R architecture.

---
Changes in v5:
- Given some comments on the page.h flags, I had to rework some fields here to better match the flags usage and avoid duplication
Changes in v4:
- Fixed typos, changed name for reserved bitfields, add emacs bits to arm64/mpu.h.
Now base and limit are 42 bits as we consider FEAT_LPA disabled, since we support max 1TB of memory.
Moved data structure in commit that uses it

---
 xen/arch/arm/include/asm/arm64/mpu.h | 52 ++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/mpu.h       |  4 +++
 2 files changed, 56 insertions(+)
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
xen/arch/arm/include/asm/arm64/mpu.h new file +0 -0
new file mode 100644
index 000000000000..d3c055a2e53b

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

Quoted 4 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +4 -0
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_ARM64_MPU_H__
+#define __ARM_ARM64_MPU_H__

Given that Andrew's CODING_STYLE update \wrt headers went it, I think we should adhere to it in new files. Other than that:

~Michal

#6 lore raw

Re: [PATCH v5 2/6] arm/mpu: Introduce MPU memory region map structure

Hi Michal,

Quoted 14 lines

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

Quoted 11 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +4 -0
new file mode 100644
index 000000000000..d3c055a2e53b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_ARM64_MPU_H__
+#define __ARM_ARM64_MPU_H__

Given that Andrew's CODING_STYLE update \wrt headers went it, I think we should adhere to it in new files. Other than that:

Quoted 1 line

Oh I missed that, I’ll update the headers in this serie

Cheers,
Luca

#7 lore raw

PATCH v5 3/6 arm/mpu: Provide and populate MPU C data structures

In reply to Luca Fancellu

Provide some data structure in the C world to track the MPU status, these structures will be filled at boot by the assembly early code with the boot MPU regions and afterwards they will be used at runtime.

Provide methods to update a bitmap created with DECLARE_BITMAP from the assembly code for both Arm32 and Arm64.

Modify Arm64 assembly boot code to reset any unused MPU region, initialise 'max_mpu_regions' with the number of supported MPU regions and modify the common asm macro 'prepare_xen_region' to load into xen_mpumap the MPU status and set/clear the bitmap 'xen_mpumap_mask' used to track the enabled regions.

Provide a stub implementation for the pr_t type and few asm macro for the Arm32 to prevent compilation break, they will be implemented later.

---
v5 changes:
- changed variable name from 'max_xen_mpumap' to 'max_mpu_regions'
- code style fixes
- reverted back changes about parameter name of prepare_xen_region
- optimised address computation for xen_mpumap entries
- modified MAX_MPU_REGION_NR
v4 changes:
- new patch

---
 xen/arch/arm/arm64/mpu/head.S            | 13 +++++
 xen/arch/arm/include/asm/arm32/mpu.h     | 25 +++++++++
 xen/arch/arm/include/asm/bitmap-op.inc   | 67 ++++++++++++++++++++++++
 xen/arch/arm/include/asm/mpu.h           |  5 ++
 xen/arch/arm/include/asm/mpu/mm.h        |  7 +++
 xen/arch/arm/include/asm/mpu/regions.inc | 49 +++++++++++++++++
 xen/arch/arm/mpu/mm.c                    | 16 ++++++
 7 files changed, 182 insertions(+)
 create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h
 create mode 100644 xen/arch/arm/include/asm/bitmap-op.inc
xen/arch/arm/arm64/mpu/head.S +13 -0
index 6d336cafbbaf..59ddc46526eb 100644
@@ -40,6 +40,9 @@ FUNC(enable_boot_cpu_mm)
     mrs   x5, MPUIR_EL2
     and   x5, x5, #NUM_MPU_REGIONS_MASK
 
+    ldr   x0, =max_mpu_regions
+    strb  w5, [x0]
+
     /* x0: region sel */
     mov   x0, xzr
     /* Xen text section. */
@@ -74,6 +77,16 @@ FUNC(enable_boot_cpu_mm)
     prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_DEVICE_PRBAR, attr_prlar=REGION_DEVICE_PRLAR
 #endif
 
+zero_mpu:
+    /* Reset remaining MPU regions */
+    cmp   x0, x5
+    beq   out_zero_mpu
+    mov   x1, #0
+    mov   x2, #1
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prlar=REGION_DISABLED_PRLAR
+    b     zero_mpu
+
+out_zero_mpu:
     b    enable_mpu
     ret
 END(enable_boot_cpu_mm)
xen/arch/arm/include/asm/arm32/mpu.h new file +25 -0
new file mode 100644
index 000000000000..1bdae4c309dc
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_ARM32_MPU_H__
+#define __ARM_ARM32_MPU_H__
+
+#ifndef __ASSEMBLY__
+
+/* MPU Protection Region */
+typedef struct {
+    uint32_t prbar;
+    uint32_t prlar;
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/bitmap-op.inc new file +67 -0

67 lines not on this page · raw message

xen/arch/arm/include/asm/mpu.h +5 -0
index bb83f5a5f580..fb93b8b19d24 100644
@@ -8,6 +8,10 @@
 
 #if defined(CONFIG_ARM_64)
 # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
+#else
+# error "unknown ARM variant"
 #endif
 
 #define MPU_REGION_SHIFT  6
@@ -17,6 +21,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_REGION_NR       NUM_MPU_REGIONS_MASK
 
 #endif /* __ARM_MPU_H__ */
 
xen/arch/arm/include/asm/mpu/mm.h +7 -0
index bfd840fa5d31..409b4dd53dc6 100644
@@ -8,9 +8,16 @@
 #include <xen/page-size.h>
 #include <xen/types.h>
 #include <asm/mm.h>
+#include <asm/mpu.h>
 
 extern struct page_info *frame_table;
 
+extern uint8_t max_mpu_regions;
+
+extern DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR);
+
+extern pr_t xen_mpumap[MAX_MPU_REGION_NR];
+
 #define virt_to_maddr(va) ((paddr_t)((vaddr_t)(va) & PADDR_MASK))
 
 #ifdef CONFIG_ARM_32
xen/arch/arm/include/asm/mpu/regions.inc +49 -0

69 lines not on this page · raw message

xen/arch/arm/mpu/mm.c +16 -0
index 07c8959f4ee9..ee035a54b942 100644
@@ -7,9 +7,25 @@
 #include <xen/mm.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
+#include <asm/mpu.h>
 
 struct page_info *frame_table;
 
+/* Maximum number of supported MPU memory regions by the EL2 MPU. */
+uint8_t __ro_after_init max_mpu_regions;
+
+/*
+ * 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_REGION_NR) \
+    __section(".data.page_aligned");
+
+/* EL2 Xen MPU memory region mapping table. */
+pr_t __section(".data.page_aligned") xen_mpumap[MAX_MPU_REGION_NR];
+
 static void __init __maybe_unused build_assertions(void)
 {
     /*
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v5 3/6] arm/mpu: Provide and populate MPU C data structures

Hi Luca,

Quoted 45 lines · Luca Fancellu

Provide some data structure in the C world to track the MPU status, these structures will be filled at boot by the assembly early code with the boot MPU regions and afterwards they will be used at runtime.

Provide methods to update a bitmap created with DECLARE_BITMAP from the assembly code for both Arm32 and Arm64.

Modify Arm64 assembly boot code to reset any unused MPU region, initialise 'max_mpu_regions' with the number of supported MPU regions and modify the common asm macro 'prepare_xen_region' to load into xen_mpumap the MPU status and set/clear the bitmap 'xen_mpumap_mask' used to track the enabled regions.

Provide a stub implementation for the pr_t type and few asm macro for the Arm32 to prevent compilation break, they will be implemented later.

---
v5 changes:
- changed variable name from 'max_xen_mpumap' to 'max_mpu_regions'
- code style fixes
- reverted back changes about parameter name of prepare_xen_region
- optimised address computation for xen_mpumap entries
- modified MAX_MPU_REGION_NR
v4 changes:
- new patch

---
  xen/arch/arm/arm64/mpu/head.S            | 13 +++++
  xen/arch/arm/include/asm/arm32/mpu.h     | 25 +++++++++
  xen/arch/arm/include/asm/bitmap-op.inc   | 67 ++++++++++++++++++++++++
  xen/arch/arm/include/asm/mpu.h           |  5 ++
  xen/arch/arm/include/asm/mpu/mm.h        |  7 +++
  xen/arch/arm/include/asm/mpu/regions.inc | 49 +++++++++++++++++
  xen/arch/arm/mpu/mm.c                    | 16 ++++++
  7 files changed, 182 insertions(+)

create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h create mode 100644 xen/arch/arm/include/asm/bitmap-op.inc

xen/arch/arm/arm64/mpu/head.S +0 -0
index 6d336cafbbaf..59ddc46526eb 100644

xen/arch/arm/arm64/mpu/head.S FUNC(enable_boot_cpu_mm) · line 44

Quoted 5 lines
xen/arch/arm/arm64/mpu/head.S +2 -0
@@ -40,6 +40,9 @@ FUNC(enable_boot_cpu_mm)
      mrs   x5, MPUIR_EL2
      and   x5, x5, #NUM_MPU_REGIONS_MASK
 
+    ldr   x0, =max_mpu_regions
+    strb  w5, [x0]

Below you are handling cache invalidation when writing to the bitmap. But you don't do one here. Is this just an overlook?

Quoted 61 lines
xen/arch/arm/arm64/mpu/head.S +11 -0
+
      /* x0: region sel */
      mov   x0, xzr
      /* Xen text section. */
@@ -74,6 +77,16 @@ FUNC(enable_boot_cpu_mm)
      prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_DEVICE_PRBAR, attr_prlar=REGION_DEVICE_PRLAR
  #endif
 
+zero_mpu:
+    /* Reset remaining MPU regions */
+    cmp   x0, x5
+    beq   out_zero_mpu
+    mov   x1, #0
+    mov   x2, #1
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prlar=REGION_DISABLED_PRLAR
+    b     zero_mpu
+
+out_zero_mpu:
      b    enable_mpu
      ret
  END(enable_boot_cpu_mm)
xen/arch/arm/include/asm/arm32/mpu.h new file +25 -0
new file mode 100644
index 000000000000..1bdae4c309dc
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_ARM32_MPU_H__
+#define __ARM_ARM32_MPU_H__
+
+#ifndef __ASSEMBLY__
+
+/* MPU Protection Region */
+typedef struct {
+    uint32_t prbar;
+    uint32_t prlar;
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM_ARM32_MPU_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/bitmap-op.inc new file +3 -0
new file mode 100644
index 000000000000..ea7c8abbf6f0
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*

xen/arch/arm/include/asm/bitmap-op.inc line 13

Quoted 10 lines
xen/arch/arm/include/asm/bitmap-op.inc new file +10 -0
+ * Sets a bit in a bitmap declared by DECLARE_BITMAP, symbol name passed through
+ * bitmap_symbol.
+ *
+ * bitmap_set_bit:      symbol of the bitmap declared by DECLARE_BITMAP
+ * bit:                 bit number to be set in the bitmap
+ * tmp1-tmp4:           temporary registers used for the computation
+ *
+ * Preserves bit.
+ * Output:
+ *  tmp1: Address of the word containing the changed bit.

If the register is used for output, then I think it needs a better name. But looking at the code, it is not entirely clear how the output will be used.

xen/arch/arm/include/asm/bitmap-op.inc line 22

Quoted 8 lines
xen/arch/arm/include/asm/bitmap-op.inc new file +8 -0
+ * Clobbers: tmp1, tmp2, tmp3, tmp4.
+ */
+.macro bitmap_set_bit bitmap_symbol, bit, tmp1, tmp2, tmp3, tmp4
+    adr_l   \tmp1, \bitmap_symbol> +    mov     \tmp2, #(BYTES_PER_LONG - 1)
+    mvn     \tmp2, \tmp2
+    lsr     \tmp3, \bit, #3
+    and     \tmp2, \tmp3, \tmp2
+    add     \tmp1, \tmp1, \tmp2                 /* bitmap_symbol + (bit/BITS_PER_LONG)*BYTES_PER_LONG */

Style: missing some spaces.

But is the comment explaining the logic above? If so, I think I would put it right at the beginning to make easier to understand the logic. I would also consider to split in smaller comments on each line e.g.:

* ... = ... + ...

Quoted 115 lines
xen/arch/arm/include/asm/bitmap-op.inc new file +45 -0
+    and     \tmp2, \bit, #(BITS_PER_LONG - 1)   /* bit offset inside word */
+
+    ldr     \tmp3, [\tmp1]
+    mov     \tmp4, #1
+    lsl     \tmp4, \tmp4, \tmp2                 /* (1 << offset) */
+    orr     \tmp3, \tmp3, \tmp4                 /* set the bit */
+    str     \tmp3, [\tmp1]
+.endm
+
+/*
+ * Clears a bit in a bitmap declared by DECLARE_BITMAP, symbol name passed
+ * through bitmap_symbol.
+ *
+ * bitmap_set_bit:      symbol of the bitmap declared by DECLARE_BITMAP
+ * bit:                 bit number to be set in the bitmap
+ * tmp1-tmp4:           temporary registers used for the computation
+ *
+ * Preserves bit.
+ * Output:
+ *  tmp1: Address of the word containing the changed bit.
+ * Clobbers: tmp1, tmp2, tmp3, tmp4.
+ */
+.macro bitmap_clear_bit bitmap_symbol, bit, tmp1, tmp2, tmp3, tmp4
+    adr_l   \tmp1, \bitmap_symbol
+    mov     \tmp2, #(BYTES_PER_LONG - 1)
+    mvn     \tmp2, \tmp2
+    lsr     \tmp3, \bit, #3
+    and     \tmp2, \tmp3, \tmp2
+    add     \tmp1, \tmp1, \tmp2                 /* bitmap_symbol + (bit/BITS_PER_LONG)*BYTES_PER_LONG */
+    and     \tmp2, \bit, #(BITS_PER_LONG - 1)   /* bit offset inside word */
+
+    ldr     \tmp3, [\tmp1]
+    mov     \tmp4, #1
+    lsl     \tmp4, \tmp4, \tmp2                 /* (1 << offset) */
+    mvn     \tmp4, \tmp4                        /* ~(1 << offset) */
+    and     \tmp3, \tmp3, \tmp4                 /* clear the bit */
+    str     \tmp3, [\tmp1]
+.endm
+
+/*
+ * Local variables:
+ * mode: ASM
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +5 -0
index bb83f5a5f580..fb93b8b19d24 100644
@@ -8,6 +8,10 @@
 
  #if defined(CONFIG_ARM_64)
  # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
+#else
+# error "unknown ARM variant"
  #endif
 
  #define MPU_REGION_SHIFT  6
@@ -17,6 +21,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_REGION_NR       NUM_MPU_REGIONS_MASK
 
  #endif /* __ARM_MPU_H__ */
 
xen/arch/arm/include/asm/mpu/mm.h +7 -0
index bfd840fa5d31..409b4dd53dc6 100644
@@ -8,9 +8,16 @@
  #include <xen/page-size.h>
  #include <xen/types.h>
  #include <asm/mm.h>
+#include <asm/mpu.h>
 
  extern struct page_info *frame_table;
 
+extern uint8_t max_mpu_regions;
+
+extern DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR);
+
+extern pr_t xen_mpumap[MAX_MPU_REGION_NR];
+
  #define virt_to_maddr(va) ((paddr_t)((vaddr_t)(va) & PADDR_MASK))
 
  #ifdef CONFIG_ARM_32
xen/arch/arm/include/asm/mpu/regions.inc +10 -0
index 47868a152662..ba38213ffff1 100644
@@ -1,14 +1,42 @@
  /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <asm/bitmap-op.inc>
  #include <asm/mpu.h>
  #include <asm/sysregs.h>
 
  /* Backgroud region enable/disable */
  #define SCTLR_ELx_BR    BIT(17, UL)
 
+#define REGION_DISABLED_PRLAR   0x00    /* NS=0 ATTR=000 EN=0 */
  #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
  #define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 
+#define PRLAR_ELx_EN            0x1
+
+#ifdef CONFIG_ARM_64
+#define XEN_MPUMAP_ENTRY_SHIFT  0x4     /* 16 byte structure */
+
+.macro store_pair reg1, reg2, dst
+    stp \reg1, \reg2, [\dst]
+.endm

xen/arch/arm/include/asm/mpu/regions.inc line 31

Quoted 10 lines
xen/arch/arm/include/asm/mpu/regions.inc +10 -0
+
+.macro invalidate_dcache_one reg
+    dc ivac, \reg
+.endm
+
+#else
+#define XEN_MPUMAP_ENTRY_SHIFT  0x3     /* 8 byte structure */
+
+.macro store_pair reg1, reg2, dst
+    nop

Can we use a function that will trigger a fault? So we will remember this will needed to be updated. Maybe re-use the BUG_OPCODE?

xen/arch/arm/include/asm/mpu/regions.inc line 31

Quoted 4 lines
xen/arch/arm/include/asm/mpu/regions.inc +4 -0
+.endm
+
+.macro invalidate_dcache_one reg
+    nop

Same here.

xen/arch/arm/include/asm/mpu/regions.inc line 88

Quoted 12 lines
xen/arch/arm/include/asm/mpu/regions.inc +5 -0
+.endm
+> +#endif
+
  /*
   * Macro to prepare and set a EL2 MPU memory region.
   * We will also create an according MPU memory region entry, which
@@ -56,6 +84,27 @@
      isb
      WRITE_SYSREG_ASM(\prbar, PRBAR_EL2)
      WRITE_SYSREG_ASM(\prlar, PRLAR_EL2)
+
+    /* Load pair into xen_mpumap and invalidate cache */

To confirm my understanding, xen_mpumap is part of the loaded binary. So we expect the bootloader to have clean the cache for us. Therefore, we only need to invalidate the entries afterwards. Is it correct?

xen/arch/arm/include/asm/mpu/regions.inc line 91

Quoted 3 lines
xen/arch/arm/include/asm/mpu/regions.inc +3 -0
+    adr_l \base, xen_mpumap
+    add   \base, \base, \sel, LSL #XEN_MPUMAP_ENTRY_SHIFT
+    store_pair \prbar, \prlar, \base

I think you want a comment on top of pr_t to mention the structure will not changed and

xen/arch/arm/include/asm/mpu/regions.inc line 92

Quoted 1 line
xen/arch/arm/include/asm/mpu/regions.inc +1 -0
+    invalidate_dcache_one \base

This will invalidate a single line in the data cache. The size depends on the HW, but typically it will be 64 - 128 bytes. Do we have any check that will confirm the data will fit in an cache line?

Quoted 4 lines
xen/arch/arm/include/asm/mpu/regions.inc +4 -0
+
+    /* Set/clear xen_mpumap_mask bitmap */
+    tst   \prlar, #PRLAR_ELx_EN
+    bne   2f

xen/arch/arm/include/asm/mpu/regions.inc line 106

Quoted 10 lines
xen/arch/arm/include/asm/mpu/regions.inc +10 -0
+    /* Region is disabled, clear the bit in the bitmap */
+    bitmap_clear_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+    b     3f
+
+2:
+    /* Region is enabled, set the bit in the bitmap */
+    bitmap_set_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+
+3:
+    invalidate_dcache_one \base

You want to a comment to explain what this invalidate does. AFAICT, this is for the bitmap. But given the bitmap will be typically small, wouldn't it better to do it in one go at the end?

Same comment here.

Quoted 17 lines
xen/arch/arm/include/asm/mpu/regions.inc +1 -0
+
      dsb   sy
      isb
 
xen/arch/arm/mpu/mm.c +2 -0
index 07c8959f4ee9..ee035a54b942 100644
@@ -7,9 +7,25 @@
  #include <xen/mm.h>
  #include <xen/sizes.h>
  #include <xen/types.h>
+#include <asm/mpu.h>
 
  struct page_info *frame_table;
 
+/* Maximum number of supported MPU memory regions by the EL2 MPU. */

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+uint8_t __ro_after_init max_mpu_regions;
+
+/*
+ * 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_REGION_NR) \
+    __section(".data.page_aligned");

Why does this need to be page_aligned?

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

Quoted 3 lines
xen/arch/arm/mpu/mm.c +3 -0
+
+/* EL2 Xen MPU memory region mapping table. */
+pr_t __section(".data.page_aligned") xen_mpumap[MAX_MPU_REGION_NR];

I guess for this one this is mandated by the HW?

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

Quoted 4 lines
xen/arch/arm/mpu/mm.c +1 -0
+
  static void __init __maybe_unused build_assertions(void)
  {
      /*

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v5 3/6] arm/mpu: Provide and populate MPU C data structures

Hi Julien,

Quoted 11 lines

xen/arch/arm/arm64/mpu/head.S FUNC(enable_boot_cpu_mm) · line 43

Quoted 9 lines
xen/arch/arm/arm64/mpu/head.S +1 -0
index 6d336cafbbaf..59ddc46526eb 100644
@@ -40,6 +40,9 @@ FUNC(enable_boot_cpu_mm)
     mrs   x5, MPUIR_EL2
     and   x5, x5, #NUM_MPU_REGIONS_MASK
 +    ldr   x0, =max_mpu_regions
+    strb  w5, [x0]

Below you are handling cache invalidation when writing to the bitmap. But you don't do one here. Is this just an overlook?

oh yes, overlooked, thanks

Quoted 15 lines
Quoted 15 lines
xen/arch/arm/include/asm/bitmap-op.inc new file +8 -0
new file mode 100644
index 000000000000..ea7c8abbf6f0
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * Sets a bit in a bitmap declared by DECLARE_BITMAP, symbol name passed through
+ * bitmap_symbol.
+ *
+ * bitmap_set_bit:      symbol of the bitmap declared by DECLARE_BITMAP
+ * bit:                 bit number to be set in the bitmap
Quoted 8 lines

xen/arch/arm/include/asm/bitmap-op.inc line 13

Quoted 5 lines
xen/arch/arm/include/asm/bitmap-op.inc new file +5 -0
+ * tmp1-tmp4:           temporary registers used for the computation
+ *
+ * Preserves bit.
+ * Output:
+ *  tmp1: Address of the word containing the changed bit.

If the register is used for output, then I think it needs a better name. But looking at the code, it is not entirely clear how the output will be used.

So currently it will give the address of the word containing the bit, so that we will pass it to the invalidate cache function.

Quoted 10 lines

xen/arch/arm/include/asm/bitmap-op.inc line 22

Quoted 9 lines
xen/arch/arm/include/asm/bitmap-op.inc new file +8 -0
+ * Clobbers: tmp1, tmp2, tmp3, tmp4.
+ */
+.macro bitmap_set_bit bitmap_symbol, bit, tmp1, tmp2, tmp3, tmp4
+    adr_l   \tmp1, \bitmap_symbol> +    mov     \tmp2, #(BYTES_PER_LONG - 1)
+    mvn     \tmp2, \tmp2
+    lsr     \tmp3, \bit, #3
+    and     \tmp2, \tmp3, \tmp2
+    add     \tmp1, \tmp1, \tmp2                 /* bitmap_symbol + (bit/BITS_PER_LONG)*BYTES_PER_LONG */
Quoted 5 lines

Style: missing some spaces.

But is the comment explaining the logic above? If so, I think I would put it right at the beginning to make easier to understand the logic. I would also consider to split in smaller comments on each line e.g.:

* ... = ... + ...

Ok I’ll do a line by line explanation

Quoted 27 lines
Quoted 27 lines
xen/arch/arm/include/asm/mpu/regions.inc +11 -0
index 47868a152662..ba38213ffff1 100644
@@ -1,14 +1,42 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 +#include <asm/bitmap-op.inc>
 #include <asm/mpu.h>
 #include <asm/sysregs.h>
   /* Backgroud region enable/disable */
 #define SCTLR_ELx_BR    BIT(17, UL)
 +#define REGION_DISABLED_PRLAR   0x00    /* NS=0 ATTR=000 EN=0 */
 #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
 #define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 +#define PRLAR_ELx_EN            0x1
+
+#ifdef CONFIG_ARM_64
+#define XEN_MPUMAP_ENTRY_SHIFT  0x4     /* 16 byte structure */
+
+.macro store_pair reg1, reg2, dst
+    stp \reg1, \reg2, [\dst]
+.endm
+
+.macro invalidate_dcache_one reg
+    dc ivac, \reg
+.endm
Quoted 8 lines

xen/arch/arm/include/asm/mpu/regions.inc line 31

Quoted 6 lines
xen/arch/arm/include/asm/mpu/regions.inc +6 -0
+
+#else
+#define XEN_MPUMAP_ENTRY_SHIFT  0x3     /* 8 byte structure */
+
+.macro store_pair reg1, reg2, dst
+    nop

Can we use a function that will trigger a fault? So we will remember this will needed to be updated. Maybe re-use the BUG_OPCODE?

Sure, I’ll look into it

Quoted 8 lines

xen/arch/arm/include/asm/mpu/regions.inc line 31

Quoted 5 lines
xen/arch/arm/include/asm/mpu/regions.inc +4 -0
+.endm
+
+.macro invalidate_dcache_one reg
+    nop

Same here.

Quoted 15 lines

xen/arch/arm/include/asm/mpu/regions.inc line 88

Quoted 12 lines
xen/arch/arm/include/asm/mpu/regions.inc +5 -0
+.endm
+> +#endif
+
 /*
  * Macro to prepare and set a EL2 MPU memory region.
  * We will also create an according MPU memory region entry, which
@@ -56,6 +84,27 @@
     isb
     WRITE_SYSREG_ASM(\prbar, PRBAR_EL2)
     WRITE_SYSREG_ASM(\prlar, PRLAR_EL2)
+
+    /* Load pair into xen_mpumap and invalidate cache */

To confirm my understanding, xen_mpumap is part of the loaded binary. So we expect the bootloader to have clean the cache for us. Therefore, we only need to invalidate the entries afterwards. Is it correct?

yes xen_mpumap is part of the binary, are you suggesting we should use clean and invalidate here? So for example boot-wrapper-aarch64 is not cleaning the cache.

Quoted 7 lines

xen/arch/arm/include/asm/mpu/regions.inc line 91

Quoted 4 lines
xen/arch/arm/include/asm/mpu/regions.inc +3 -0
+    adr_l \base, xen_mpumap
+    add   \base, \base, \sel, LSL #XEN_MPUMAP_ENTRY_SHIFT
+    store_pair \prbar, \prlar, \base

I think you want a comment on top of pr_t to mention the structure will not changed and

can you explain better this part, what should I write on top of the typedef struct {...} pt_t?

Quoted 5 lines

xen/arch/arm/include/asm/mpu/regions.inc line 92

Quoted 2 lines
xen/arch/arm/include/asm/mpu/regions.inc +1 -0
+    invalidate_dcache_one \base

This will invalidate a single line in the data cache. The size depends on the HW, but typically it will be 64 - 128 bytes. Do we have any check that will confirm the data will fit in an cache line?

You are right, so we are gonna write 16 bytes at most for Arm64 and (for now) 8 bytes for Arm32, so I think we will be way below 64 bytes, shall we have a BUILD_BUG_ON inside build_assertions() in arm/mpu/mm.c to check sizeof(pr_t) <= 64?

Quoted 17 lines
Quoted 5 lines
xen/arch/arm/include/asm/mpu/regions.inc +4 -0
+
+    /* Set/clear xen_mpumap_mask bitmap */
+    tst   \prlar, #PRLAR_ELx_EN
+    bne   2f

xen/arch/arm/include/asm/mpu/regions.inc line 106

Quoted 10 lines
xen/arch/arm/include/asm/mpu/regions.inc +10 -0
+    /* Region is disabled, clear the bit in the bitmap */
+    bitmap_clear_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+    b     3f
+
+2:
+    /* Region is enabled, set the bit in the bitmap */
+    bitmap_set_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+
+3:
+    invalidate_dcache_one \base

You want to a comment to explain what this invalidate does. AFAICT, this is for the bitmap. But given the bitmap will be typically small, wouldn't it better to do it in one go at the end?

Yes this invalidate is a bit overkill because it will invalidate 64-128 bytes starting from the address of the last changed word where the bit was.

Should I invalidate everything outside this macro, inside enable_boot_cpu_mm in arm64/mpu/head.S instead?

Quoted 21 lines

Same comment here.

Quoted 18 lines
xen/arch/arm/include/asm/mpu/regions.inc +1 -0
+
     dsb   sy
     isb
 diff --git a/xen/arch/arm/mpu/mm.c b/xen/arch/arm/mpu/mm.c

index 07c8959f4ee9..ee035a54b942 100644

xen/arch/arm/mpu/mm.c +5 -0
@@ -7,9 +7,25 @@
 #include <xen/mm.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
+#include <asm/mpu.h>
   struct page_info *frame_table;
 +/* Maximum number of supported MPU memory regions by the EL2 MPU. */
+uint8_t __ro_after_init max_mpu_regions;
+
+/*
+ * Bitmap xen_mpumap_mask is to record the usage of EL2 MPU memory regions.
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/mpu/mm.c +6 -0
+ * 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_REGION_NR) \
+    __section(".data.page_aligned");

Why does this need to be page_aligned?

I see from the linker file that this section is aligned to SMP_CACHE_BYTES, which is L1_CACHE_BYTES for Arm, because we are using the invalidate cache I was under the impression that it’s better to have it aligned on the cache line

Quoted 6 lines

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

Quoted 4 lines
xen/arch/arm/mpu/mm.c +3 -0
+
+/* EL2 Xen MPU memory region mapping table. */
+pr_t __section(".data.page_aligned") xen_mpumap[MAX_MPU_REGION_NR];

I guess for this one this is mandated by the HW?

same reason above, no other reason (I’m aware of).

Cheers,
Luca

#10 lore raw

Re: [PATCH v5 3/6] arm/mpu: Provide and populate MPU C data structures

Hi Luca,

Quoted 24 lines · Luca Fancellu
Quoted 8 lines

xen/arch/arm/include/asm/mpu/regions.inc line 31

Quoted 5 lines

+.endm

xen/arch/arm/include/asm/mpu/regions.inc +3 -0
+
+.macro invalidate_dcache_one reg
+    nop

Same here.

Quoted 15 lines

xen/arch/arm/include/asm/mpu/regions.inc line 88

Quoted 12 lines

+.endm
+> +#endif

xen/arch/arm/include/asm/mpu/regions.inc +3 -0
+
  /*
   * Macro to prepare and set a EL2 MPU memory region.
   * We will also create an according MPU memory region entry, which
@@ -56,6 +84,27 @@
      isb
      WRITE_SYSREG_ASM(\prbar, PRBAR_EL2)
      WRITE_SYSREG_ASM(\prlar, PRLAR_EL2)
+
+    /* Load pair into xen_mpumap and invalidate cache */

To confirm my understanding, xen_mpumap is part of the loaded binary. So we expect the bootloader to have clean the cache for us. Therefore, we only need to invalidate the entries afterwards. Is it correct?

Quoted 2 lines

yes xen_mpumap is part of the binary, are you suggesting we should use clean and invalidate here? So for example boot-wrapper-aarch64 is not cleaning the cache.

I am mainly asking clarification of the expected state of the cache before the write. From what you wrote, the cache line will be cleaned. So we only need to invalidate it after the write and there is no cache maintenance necessary before writing.

Quoted 10 lines
Quoted 8 lines

xen/arch/arm/include/asm/mpu/regions.inc line 91

Quoted 5 lines
xen/arch/arm/include/asm/mpu/regions.inc +3 -0
+    adr_l \base, xen_mpumap
+    add   \base, \base, \sel, LSL #XEN_MPUMAP_ENTRY_SHIFT
+    store_pair \prbar, \prlar, \base

I think you want a comment on top of pr_t to mention the structure will not changed and

can you explain better this part, what should I write on top of the typedef struct {...} pt_t?

Hmm, my previous sentence made no sense. Let me retry. Today, you are relying on the layout of pr_t to never change. But this is not obvious when someone is reading the structure pr_t. So it would be good to have a comment such as below on top of pr_t:

"
/!\ The assembly code (see ...) relies on the pr_t. If the structure is modified, then the assembly code mostly likely needs to be updated "

Quoted 9 lines
Quoted 6 lines

Review line 92

Quoted 3 lines
(quoted diff) +1 -0
+    invalidate_dcache_one \base

This will invalidate a single line in the data cache. The size depends on the HW, but typically it will be 64 - 128 bytes. Do we have any check that will confirm the data will fit in an cache line?

You are right, so we are gonna write 16 bytes at most for Arm64 and (for now) 8 bytes for Arm32, so I think we will be way below 64 bytes, shall we have a BUILD_BUG_ON inside build_assertions() in arm/mpu/mm.c to check sizeof(pr_t) <= 64?

I wrote "typically" because there is no guarantee that the cache line will be equal or bigger than 64-byte. Looking at the Arm Arm (CTR_EL0.DminLine), if I am not mistaken, the minimum is 16-byte, so you could check that sizeof() is always smaller or equal to 16-byte (should be the case today).

Alternatively, you could implement another helper in cache.S that would invalidate the cache and then don't rely on the size or alignment of the structure.

Quoted 19 lines
Quoted 18 lines
Quoted 6 lines
xen/arch/arm/include/asm/mpu/regions.inc +4 -0
+
+    /* Set/clear xen_mpumap_mask bitmap */
+    tst   \prlar, #PRLAR_ELx_EN
+    bne   2f

xen/arch/arm/include/asm/mpu/regions.inc line 106

Quoted 10 lines
xen/arch/arm/include/asm/mpu/regions.inc +10 -0
+    /* Region is disabled, clear the bit in the bitmap */
+    bitmap_clear_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+    b     3f
+
+2:
+    /* Region is enabled, set the bit in the bitmap */
+    bitmap_set_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+
+3:
+    invalidate_dcache_one \base

You want to a comment to explain what this invalidate does. AFAICT, this is for the bitmap. But given the bitmap will be typically small, wouldn't it better to do it in one go at the end?

Quoted 3 lines

Yes this invalidate is a bit overkill because it will invalidate 64-128 bytes starting from the address of the last changed word where the bit was.

Should I invalidate everything outside this macro, inside enable_boot_cpu_mm in arm64/mpu/head.S instead?

Yes. I think it will be easier if we end up to use a function to clean the cache as I suggested above.

Quoted 31 lines
Quoted 22 lines

Same comment here.

Quoted 18 lines
xen/arch/arm/include/asm/mpu/regions.inc +1 -0
+
      dsb   sy
      isb
  diff --git a/xen/arch/arm/mpu/mm.c b/xen/arch/arm/mpu/mm.c

index 07c8959f4ee9..ee035a54b942 100644

xen/arch/arm/mpu/mm.c +5 -0
@@ -7,9 +7,25 @@
  #include <xen/mm.h>
  #include <xen/sizes.h>
  #include <xen/types.h>
+#include <asm/mpu.h>
    struct page_info *frame_table;
  +/* Maximum number of supported MPU memory regions by the EL2 MPU. */
+uint8_t __ro_after_init max_mpu_regions;
+
+/*
+ * Bitmap xen_mpumap_mask is to record the usage of EL2 MPU memory regions.
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/mpu/mm.c +6 -0
+ * 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_REGION_NR) \
+    __section(".data.page_aligned");

Why does this need to be page_aligned?

Quoted 1 line

I see from the linker file that this section is aligned to SMP_CACHE_BYTES

The start of the section is cache aligned. But that may not be the case for the content within the section itself.

Also, .data.page_aligned is used for data that are page size aligned (currently 4KB). So everything within that section needs to be declared with __aligned(PAGE_SIZE). But AFAIU, the bitmap is only going to be a few bytes, correct?. So think it would be a bad idea for this variable to be page aligned because it would end up to be a massive waste of memory.

Quoted 3 lines

, which is L1_CACHE_BYTES
for Arm, because we are using the invalidate cache I was under the impression that it’s better to have it aligned on the cache line

To clarify, L1_CACHE_BYTES is fixed to 128-byte. If I got correctly, this is less than the maximum cache line that could exist (256-bytes).

L1_CACHE_BYTES is mainly used as a hint for Xen to try to allocate structure in separate cache line. But when cleaning/invalidating a cache line, the code can't rely on the data not crossing a cache line. So you need a loop to invalidate each line.

Anyway, I think it would make sense to use __cacheline_aligned for both as the two variables are going to be used often. We should also make force the section to be .data, otherwise the compiler will typically put the variable in .bss which cannot be used during early boot. This is because the BSS region is not loaded in memory by the bootloader and the Image protocol doesn't specify the state of the region, so for simplicity, it is zeroed after the MMU/MPU and cache is turned on.

Quoted 9 lines
Quoted 7 lines

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

Quoted 5 lines
xen/arch/arm/mpu/mm.c +3 -0
+
+/* EL2 Xen MPU memory region mapping table. */
+pr_t __section(".data.page_aligned") xen_mpumap[MAX_MPU_REGION_NR];

I guess for this one this is mandated by the HW?

same reason above, no other reason (I’m aware of).

.data..page_aligned makes a bit more sense here (along with __aligned(PAGE_SIZE)). However, it seems a bit overkill when we only need the region to be cache aligned. So I would follow same approach as I suggested for the bitmap.

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

PATCH v5 4/6 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.

---
v5 changes:
- move MPU_REGION_RES0 to arm64, fixed typos and code style.
v4 changes:
- moved back PRBAR0_EL2/PRLAR0_EL2 to mm.c and protect them with CONFIG_ARM_64, changed comments, fixed typos and code style
- Add PRBAR_EL2_(n) definition, to be overriden by Arm32
- protect prepare_selector, read_protection_region, write_protection_region by Arm64 to ensure compilation on both arm32 and arm64, Arm32 will modify that later while introducing the arm32 bits.
v3 changes:
- Moved PRBAR0_EL2/PRLAR0_EL2 to arm64 specific
- Modified prepare_selector() to be easily made a NOP for Arm32, which can address up to 32 region without changing selector and it is also its maximum amount of MPU regions.
---

---
 xen/arch/arm/include/asm/arm64/mpu.h |   2 +
 xen/arch/arm/include/asm/mpu/mm.h    |  34 ++++++++
 xen/arch/arm/mpu/mm.c                | 119 +++++++++++++++++++++++++++
 3 files changed, 155 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index d3c055a2e53b..0fed6c8e5828 100644
@@ -5,6 +5,8 @@
 
 #ifndef __ASSEMBLY__
 
+#define MPU_REGION_RES0        (0xFFFFULL << 48)
+
 /* Protection Region Base Address Register */
 typedef union {
     struct __packed {
xen/arch/arm/include/asm/mpu/mm.h +34 -0
index 409b4dd53dc6..2ee908801665 100644
@@ -41,6 +41,40 @@ 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 requires context_sync_mpu() after being used to modify MPU
+ * regions:
+ *  - write_protection_region
+ */
+
+/*
+ * Reads the MPU region with index @sel from the HW.
+ *
+ * @pr_read: mpu protection region returned by read operation.
+ * @sel: which mpu protection region to read
+ */
+void read_protection_region(pr_t *pr_read, uint8_t sel);
+
+/*
+ * Writes the MPU region with index @sel to the HW.
+ *
+ * @pr_write: mpu protection region passed through write operation.
+ * @sel: which mpu protection region to write
+ */
+void write_protection_region(const pr_t *pr_write, uint8_t sel);
+
 #endif /* __ARM_MPU_MM_H__ */
 
 /*
xen/arch/arm/mpu/mm.c +119 -0

137 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v5 4/6
#12 lore raw

Re: [PATCH v5 4/6] arm/mpu: Provide access to the MPU region from the C code

Quoted 1 line · Luca Fancellu

Implement some utility function in order to access the MPU regions

NIT: s/function/functions

Quoted 46 lines

from the C world.

---
v5 changes:
- move MPU_REGION_RES0 to arm64, fixed typos and code style.
v4 changes:
- moved back PRBAR0_EL2/PRLAR0_EL2 to mm.c and protect them with CONFIG_ARM_64, changed comments, fixed typos and code style
- Add PRBAR_EL2_(n) definition, to be overriden by Arm32
- protect prepare_selector, read_protection_region, write_protection_region by Arm64 to ensure compilation on both arm32 and arm64, Arm32 will modify that later while introducing the arm32 bits.
v3 changes:
- Moved PRBAR0_EL2/PRLAR0_EL2 to arm64 specific
- Modified prepare_selector() to be easily made a NOP for Arm32, which can address up to 32 region without changing selector and it is also its maximum amount of MPU regions.
---

---
 xen/arch/arm/include/asm/arm64/mpu.h |   2 +
 xen/arch/arm/include/asm/mpu/mm.h    |  34 ++++++++
 xen/arch/arm/mpu/mm.c                | 119 +++++++++++++++++++++++++++
 3 files changed, 155 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index d3c055a2e53b..0fed6c8e5828 100644
@@ -5,6 +5,8 @@
 
 #ifndef __ASSEMBLY__
 
+#define MPU_REGION_RES0        (0xFFFFULL << 48)
+
 /* Protection Region Base Address Register */
 typedef union {
     struct __packed {
xen/arch/arm/include/asm/mpu/mm.h +0 -0
index 409b4dd53dc6..2ee908801665 100644

xen/arch/arm/include/asm/mpu/mm.h static inline struct page_info *virt_to_page(const void *v) · line 50

Quoted 10 lines
xen/arch/arm/include/asm/mpu/mm.h +7 -0
@@ -41,6 +41,40 @@ 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.

Isn't it misleading to people reading this code that does not match when it comes to order of operations?

Quoted 6 lines
xen/arch/arm/include/asm/mpu/mm.h +6 -0
+     */
+    dsb(sy);
+    isb();
+}
+
+/*

xen/arch/arm/include/asm/mpu/mm.h static inline struct page_info *virt_to_page(const void *v) · line 66

Quoted 10 lines
xen/arch/arm/include/asm/mpu/mm.h +10 -0
+ * The following API requires context_sync_mpu() after being used to modify MPU
+ * regions:
+ *  - write_protection_region
+ */
+
+/*
+ * Reads the MPU region with index @sel from the HW.
+ *
+ * @pr_read: mpu protection region returned by read operation.
+ * @sel: which mpu protection region to read

NIT: I mentioned that in the past that I find it a bit too much duplicated information in the comment. It could very well be:
/* Reads the MPU region (into @pr_read) with index @sel from the HW */

Quoted 82 lines
xen/arch/arm/include/asm/mpu/mm.h +11 -0
+ */
+void read_protection_region(pr_t *pr_read, uint8_t sel);
+
+/*
+ * Writes the MPU region with index @sel to the HW.
+ *
+ * @pr_write: mpu protection region passed through write operation.
+ * @sel: which mpu protection region to write
+ */
+void write_protection_region(const pr_t *pr_write, uint8_t sel);
+
 #endif /* __ARM_MPU_MM_H__ */
 
 /*
xen/arch/arm/mpu/mm.c +46 -0
index ee035a54b942..46883cbd4af9 100644
@@ -8,6 +8,8 @@
 #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;
 
@@ -26,6 +28,35 @@ DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR) \
 /* EL2 Xen MPU memory region mapping table. */
 pr_t __section(".data.page_aligned") xen_mpumap[MAX_MPU_REGION_NR];
 
+#ifdef CONFIG_ARM_64
+/*
+ * The following are needed for the cases: GENERATE_WRITE_PR_REG_CASE
+ * and GENERATE_READ_PR_REG_CASE with num==0
+ */
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2
+
+#define PRBAR_EL2_(n)   PRBAR##n##_EL2
+#define PRLAR_EL2_(n)   PRLAR##n##_EL2
+
+#endif
+
+#define GENERATE_WRITE_PR_REG_CASE(num, pr)                                 \
+    case num:                                                               \
+    {                                                                       \
+        WRITE_SYSREG(pr->prbar.bits & ~MPU_REGION_RES0, PRBAR_EL2_(num));   \
+        WRITE_SYSREG(pr->prlar.bits & ~MPU_REGION_RES0, PRLAR_EL2_(num));   \
+        break;                                                              \
+    }
+
+#define GENERATE_READ_PR_REG_CASE(num, pr)                      \
+    case num:                                                   \
+    {                                                           \
+        pr->prbar.bits = READ_SYSREG(PRBAR_EL2_(num));          \
+        pr->prlar.bits = READ_SYSREG(PRLAR_EL2_(num));          \
+        break;                                                  \
+    }
+
 static void __init __maybe_unused build_assertions(void)
 {
     /*
@@ -36,6 +67,94 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
 }
 
+#ifdef CONFIG_ARM_64
+/*
+ * Armv8-R supports direct access and indirect access to the MPU regions through
+ * registers:
+ *  - indirect access involves changing the MPU region selector, issuing an isb
+ *    barrier and accessing the selected region through specific registers
+ *  - direct access involves accessing specific registers that point to
+ *    specific MPU regions, without changing the selector, avoiding the use of
+ *    a barrier.
+ * For Arm64 the PR{B,L}AR_ELx (for n=0) and PR{B,L}AR<n>_ELx (for n=1..15) are
+ * used for the direct access to the regions selected by
+ * PRSELR_EL2.REGION<7:4>:n, so 16 regions can be directly accessed when the
+ * selector is a multiple of 16, giving access to all the supported memory
+ * regions.
+ */

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

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

Here you use &=
+NIT: you could do that at the definition (but maybe it's clearer this way in your opinion)

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

Quoted 6 lines
xen/arch/arm/mpu/mm.c +6 -0
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
+    {
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
+        isb();
+    }
+    *sel = *sel & 0xFU;

but here you don't.

Quoted 16 lines
xen/arch/arm/mpu/mm.c +16 -0
+}
+
+void read_protection_region(pr_t *pr_read, uint8_t sel)
+{
+    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);

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+        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 */

I think that MISRA requires adding break even for impossible default cases.

Quoted 20 lines
xen/arch/arm/mpu/mm.c +20 -0
+    }
+}
+
+void write_protection_region(const pr_t *pr_write, uint8_t sel)
+{
+    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);

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+        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 */
+    }
+}
+#endif

Please add /* CONFIG_ARM_64 */

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

Quoted 4 lines
xen/arch/arm/mpu/mm.c +1 -0
+
 void __init setup_mm(void)
 {
     BUG_ON("unimplemented");

Other than that:

~Michal

#13 lore raw

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

In reply to Luca Fancellu

Introduce a few utility functions to manipulate and handle the pr_t type.

---
v5 changes:
- Don't rely on bitfield and use the mask MPU_REGION_RES0 for pr_set_base and pr_set_limit to make it explicit. Fixed typos in commit message.
v4 changes:
- Modify comment on top of the helpers. Clarify pr_set_limit takes exclusive address.
Protected common code with #ifdef Arm64 until Arm32 is ready with pr_t

---
 xen/arch/arm/include/asm/mpu.h | 65 ++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
xen/arch/arm/include/asm/mpu.h +65 -0
index fb93b8b19d24..b90ae8eab662 100644
@@ -23,6 +23,71 @@
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
 #define MAX_MPU_REGION_NR       NUM_MPU_REGIONS_MASK
 
+#ifndef __ASSEMBLY__
+
+#ifdef CONFIG_ARM_64
+/*
+ * Set base address of MPU protection region.
+ *
+ * @pr: pointer to the protection region structure.
+ * @base: base address as base of the protection region.
+ */
+static inline void pr_set_base(pr_t *pr, paddr_t base)
+{
+    pr->prbar.reg.base = ((base & ~MPU_REGION_RES0) >> MPU_REGION_SHIFT);
+}
+
+/*
+ * Set limit address of MPU protection region.
+ *
+ * @pr: pointer to the protection region structure.
+ * @limit: exclusive address as limit of the protection region.
+ */
+static inline void pr_set_limit(pr_t *pr, paddr_t limit)
+{
+    pr->prlar.reg.limit = (((limit - 1) & ~MPU_REGION_RES0)
+                           >> MPU_REGION_SHIFT);
+}
+
+/*
+ * Access to get base address of MPU protection region.
+ * The base address shall be zero extended.
+ *
+ * @pr: pointer to the protection region structure.
+ * @return: Base address configured for the passed protection region.
+ */
+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.
+ * The limit address shall be concatenated with 0x3f.
+ *
+ * @pr: pointer to the protection region structure.
+ * @return: Inclusive limit address configured for the passed protection region.
+ */
+static inline paddr_t pr_get_limit(pr_t *pr)
+{
+    return (paddr_t)((pr->prlar.reg.limit << MPU_REGION_SHIFT)
+                     | ~MPU_REGION_MASK);
+}
+
+/*
+ * Checks if the protection region is valid (enabled).
+ *
+ * @pr: pointer to the protection region structure.
+ * @return: True if the region is valid (enabled), false otherwise.
+ */
+static inline bool region_is_valid(pr_t *pr)
+{
+    return pr->prlar.reg.en;
+}
+#endif
+
+#endif /* __ASSEMBLY__ */
+
 #endif /* __ARM_MPU_H__ */
 
 /*
Signature · 1 line
2.34.1
#14 lore raw

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

Quoted 39 lines · Luca Fancellu

Introduce a few utility functions to manipulate and handle the pr_t type.

---
v5 changes:
- Don't rely on bitfield and use the mask MPU_REGION_RES0 for pr_set_base and pr_set_limit to make it explicit. Fixed typos in commit message.
v4 changes:
- Modify comment on top of the helpers. Clarify pr_set_limit takes exclusive address.
Protected common code with #ifdef Arm64 until Arm32 is ready with pr_t

---
 xen/arch/arm/include/asm/mpu.h | 65 ++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
xen/arch/arm/include/asm/mpu.h +13 -0
index fb93b8b19d24..b90ae8eab662 100644
@@ -23,6 +23,71 @@
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
 #define MAX_MPU_REGION_NR       NUM_MPU_REGIONS_MASK
 
+#ifndef __ASSEMBLY__
+
+#ifdef CONFIG_ARM_64
+/*
+ * Set base address of MPU protection region.
+ *
+ * @pr: pointer to the protection region structure.
+ * @base: base address as base of the protection region.
+ */
+static inline void pr_set_base(pr_t *pr, paddr_t base)
+{
+    pr->prbar.reg.base = ((base & ~MPU_REGION_RES0) >> MPU_REGION_SHIFT);
+}

xen/arch/arm/include/asm/mpu.h line 48

Quoted 10 lines
xen/arch/arm/include/asm/mpu.h +10 -0
+
+/*
+ * Set limit address of MPU protection region.
+ *
+ * @pr: pointer to the protection region structure.
+ * @limit: exclusive address as limit of the protection region.
+ */
+static inline void pr_set_limit(pr_t *pr, paddr_t limit)
+{
+    pr->prlar.reg.limit = (((limit - 1) & ~MPU_REGION_RES0)

Might be worth adding a comment that PRLAR expects inclusive limit hence (limit -1).

xen/arch/arm/include/asm/mpu.h line 59

Quoted 11 lines
xen/arch/arm/include/asm/mpu.h +11 -0
+                           >> MPU_REGION_SHIFT);
+}
+
+/*
+ * Access to get base address of MPU protection region.
+ * The base address shall be zero extended.
+ *
+ * @pr: pointer to the protection region structure.
+ * @return: Base address configured for the passed protection region.
+ */
+static inline paddr_t pr_get_base(pr_t *pr)

Why not const?

xen/arch/arm/include/asm/mpu.h line 71

Quoted 12 lines
xen/arch/arm/include/asm/mpu.h +12 -0
+{
+    return (paddr_t)(pr->prbar.reg.base << MPU_REGION_SHIFT);
+}
+
+/*
+ * Access to get limit address of MPU protection region.
+ * The limit address shall be concatenated with 0x3f.
+ *
+ * @pr: pointer to the protection region structure.
+ * @return: Inclusive limit address configured for the passed protection region.
+ */
+static inline paddr_t pr_get_limit(pr_t *pr)

Why not const?

xen/arch/arm/include/asm/mpu.h line 78

Quoted 7 lines
xen/arch/arm/include/asm/mpu.h +7 -0
+{
+    return (paddr_t)((pr->prlar.reg.limit << MPU_REGION_SHIFT)
+                     | ~MPU_REGION_MASK);
+}
+
+/*
+ * Checks if the protection region is valid (enabled).

NIT: in other helpers you use imperative mood, so this should be "Check if"

xen/arch/arm/include/asm/mpu.h line 83

Quoted 5 lines
xen/arch/arm/include/asm/mpu.h +5 -0
+ *
+ * @pr: pointer to the protection region structure.
+ * @return: True if the region is valid (enabled), false otherwise.
+ */
+static inline bool region_is_valid(pr_t *pr)

Why not const?

xen/arch/arm/include/asm/mpu.h line 87

Quoted 4 lines
xen/arch/arm/include/asm/mpu.h +4 -0
+{
+    return pr->prlar.reg.en;
+}
+#endif

Please add /* CONFIG_ARM_64 */

xen/arch/arm/include/asm/mpu.h line 93

Quoted 6 lines
xen/arch/arm/include/asm/mpu.h +3 -0
+
+#endif /* __ASSEMBLY__ */
+
 #endif /* __ARM_MPU_H__ */
 
 /*

~Michal

#15 lore raw

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

Hi Michal,

Quoted 11 lines

xen/arch/arm/include/asm/mpu.h line 48

Quoted 10 lines
xen/arch/arm/include/asm/mpu.h +10 -0
+
+/*
+ * Set limit address of MPU protection region.
+ *
+ * @pr: pointer to the protection region structure.
+ * @limit: exclusive address as limit of the protection region.
+ */
+static inline void pr_set_limit(pr_t *pr, paddr_t limit)
+{
+    pr->prlar.reg.limit = (((limit - 1) & ~MPU_REGION_RES0)

Might be worth adding a comment that PRLAR expects inclusive limit hence (limit -1).

You mean on top of the assignment? I’ve probably misunderstood you comment in the past version and thought that the @limit description was enough, I’m ok to add also this comment.

I’ll fix your other findings.

Cheers,
Luca

#16 lore raw

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

Quoted 14 lines · Luca Fancellu

Hi Michal,

Quoted 11 lines

xen/arch/arm/include/asm/mpu.h line 48

Quoted 10 lines
xen/arch/arm/include/asm/mpu.h +10 -0
+
+/*
+ * Set limit address of MPU protection region.
+ *
+ * @pr: pointer to the protection region structure.
+ * @limit: exclusive address as limit of the protection region.
+ */
+static inline void pr_set_limit(pr_t *pr, paddr_t limit)
+{
+    pr->prlar.reg.limit = (((limit - 1) & ~MPU_REGION_RES0)

Might be worth adding a comment that PRLAR expects inclusive limit hence (limit -1).

Quoted 2 lines

You mean on top of the assignment? I’ve probably misunderstood you comment in the past version and thought that the @limit description was enough, I’m ok to add also this comment.

The comment says that the address must be exclusive. The code then transforms it into inclusive before the write, so one might wonder why.

~Michal

#17 lore raw

PATCH v5 6/6 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.

---
v5 changes:
- removed AP_RW_EL2 used only by pr_of_xenaddr(), fixed comments and typos
- Given some comments to the page.h flags and modifications to the prbar_t fields ap, xn, the constructor now takes flags instead of attr_idx, which I believe it's better, deleted PRBAR_EL2_XN_ENABLED since now the PAGE_XN_MASK() is used instead.
- renamed to pr_of_addr since it will be used also in p2m
v4 changes:
- update helper comments
- rename XN_EL2_ENABLED to PRBAR_EL2_XN_ENABLED
- protected pr_of_xenaddr() with #ifdef Arm64 until Arm32 can build with it

---
 xen/arch/arm/include/asm/mpu/mm.h | 10 +++++
 xen/arch/arm/mpu/mm.c             | 66 +++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
xen/arch/arm/include/asm/mpu/mm.h +10 -0
index 2ee908801665..f0facee51692 100644
@@ -75,6 +75,16 @@ void read_protection_region(pr_t *pr_read, uint8_t sel);
  */
 void write_protection_region(const pr_t *pr_write, uint8_t sel);
 
+/*
+ * Creates a pr_t structure describing a protection region.
+ *
+ * @base: base address as base of the protection region.
+ * @limit: exclusive address as limit of the protection region.
+ * @flags: memory flags for the mapping.
+ * @return: pr_t structure describing a protection region.
+ */
+pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags);
+
 #endif /* __ARM_MPU_MM_H__ */
 
 /*
xen/arch/arm/mpu/mm.c +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v5 6/6
#18 lore raw

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

Quoted 64 lines · Luca Fancellu

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

---
v5 changes:
- removed AP_RW_EL2 used only by pr_of_xenaddr(), fixed comments and typos
- Given some comments to the page.h flags and modifications to the prbar_t fields ap, xn, the constructor now takes flags instead of attr_idx, which I believe it's better, deleted PRBAR_EL2_XN_ENABLED since now the PAGE_XN_MASK() is used instead.
- renamed to pr_of_addr since it will be used also in p2m
v4 changes:
- update helper comments
- rename XN_EL2_ENABLED to PRBAR_EL2_XN_ENABLED
- protected pr_of_xenaddr() with #ifdef Arm64 until Arm32 can build with it

---
 xen/arch/arm/include/asm/mpu/mm.h | 10 +++++
 xen/arch/arm/mpu/mm.c             | 66 +++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
xen/arch/arm/include/asm/mpu/mm.h +10 -0
index 2ee908801665..f0facee51692 100644
@@ -75,6 +75,16 @@ void read_protection_region(pr_t *pr_read, uint8_t sel);
  */
 void write_protection_region(const pr_t *pr_write, uint8_t sel);
 
+/*
+ * Creates a pr_t structure describing a protection region.
+ *
+ * @base: base address as base of the protection region.
+ * @limit: exclusive address as limit of the protection region.
+ * @flags: memory flags for the mapping.
+ * @return: pr_t structure describing a protection region.
+ */
+pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags);
+
 #endif /* __ARM_MPU_MM_H__ */
 
 /*
xen/arch/arm/mpu/mm.c +4 -0
index 46883cbd4af9..ac83227e607e 100644
@@ -9,6 +9,7 @@
 #include <xen/types.h>
 #include <asm/mpu.h>
 #include <asm/mpu/mm.h>
+#include <asm/page.h>
 #include <asm/sysregs.h>
 
 struct page_info *frame_table;
@@ -153,6 +154,71 @@ void write_protection_region(const pr_t *pr_write, uint8_t sel)
         BUG(); /* Can't happen */
     }
 }
+
+pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags)
+{

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c +10 -0
+    unsigned int attr_idx = PAGE_AI_MASK(flags);
+    prbar_t prbar;
+    prlar_t prlar;
+    pr_t region;
+
+    /* Build up value for PRBAR_EL2. */
+    prbar = (prbar_t) {
+        .reg = {
+            .ro = PAGE_RO_MASK(flags),
+            .xn = PAGE_XN_MASK(flags),

Shouldn't you also initialize .xn_0 and .ap_0 or you rely on compound literal implicit zero initialization of unspecified fields? But then you do initialize .ns to 0 fror prlar...

Quoted 45 lines
xen/arch/arm/mpu/mm.c +45 -0
+        }};
+
+    switch ( attr_idx )
+    {
+    /*
+     * ARM ARM: Shareable, Inner Shareable, and Outer Shareable Normal memory
+     * (DDI 0487L.a B2.10.1.1.1 Note section):
+     *
+     * Because all data accesses to Non-cacheable locations are data coherent
+     * to all observers, Non-cacheable locations are always treated as Outer
+     * Shareable
+     *
+     * ARM ARM: Device memory (DDI 0487L.a B2.10.2)
+     *
+     * All of these memory types have the following properties:
+     * [...]
+     *  - Data accesses to memory locations are coherent for all observers in
+     *    the system, and correspondingly are treated as being Outer Shareable
+     */
+    case MT_NORMAL_NC:
+        /* Fall through */
+    case MT_DEVICE_nGnRnE:
+        /* Fall through */
+    case MT_DEVICE_nGnRE:
+        prbar.reg.sh = LPAE_SH_OUTER;
+        break;
+    default:
+        /* Xen mappings are SMP coherent */
+        prbar.reg.sh = LPAE_SH_INNER;
+        break;
+    }
+
+    /* Build up value for PRLAR_EL2. */
+    prlar = (prlar_t) {
+        .reg = {
+            .ns = 0,        /* Hyp mode is in secure world */
+            .ai = attr_idx,
+            .en = 1,        /* Region enabled */
+        }};
+
+    /* Build up MPU memory region. */
+    region = (pr_t) {
+        .prbar = prbar,
+        .prlar = prlar,
+    };

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

Quoted 10 lines
xen/arch/arm/mpu/mm.c +7 -0
+
+    /* Set base address and limit address. */
+    pr_set_base(&region, base);
+    pr_set_limit(&region, limit);
+
+    return region;
+}
 #endif
 
 void __init setup_mm(void)

Other than that:

~Michal

#19 lore raw

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

Hi Michal,

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/mpu/mm.c +8 -0
+
+pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags)
+{
+    unsigned int attr_idx = PAGE_AI_MASK(flags);
+    prbar_t prbar;
+    prlar_t prlar;
+    pr_t region;
+
Quoted 8 lines

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

Quoted 5 lines
xen/arch/arm/mpu/mm.c +5 -0
+    /* Build up value for PRBAR_EL2. */
+    prbar = (prbar_t) {
+        .reg = {
+            .ro = PAGE_RO_MASK(flags),
+            .xn = PAGE_XN_MASK(flags),

Shouldn't you also initialize .xn_0 and .ap_0 or you rely on compound literal implicit zero initialization of unspecified fields? But then you do initialize .ns to 0 fror prlar...

Yes, because there I would like to specify that value 0 means Hyp in secure world, but of course if you want I can explicitly initialise also these two

Quoted 55 lines
Quoted 55 lines
xen/arch/arm/mpu/mm.c +52 -0
+        }};
+
+    switch ( attr_idx )
+    {
+    /*
+     * ARM ARM: Shareable, Inner Shareable, and Outer Shareable Normal memory
+     * (DDI 0487L.a B2.10.1.1.1 Note section):
+     *
+     * Because all data accesses to Non-cacheable locations are data coherent
+     * to all observers, Non-cacheable locations are always treated as Outer
+     * Shareable
+     *
+     * ARM ARM: Device memory (DDI 0487L.a B2.10.2)
+     *
+     * All of these memory types have the following properties:
+     * [...]
+     *  - Data accesses to memory locations are coherent for all observers in
+     *    the system, and correspondingly are treated as being Outer Shareable
+     */
+    case MT_NORMAL_NC:
+        /* Fall through */
+    case MT_DEVICE_nGnRnE:
+        /* Fall through */
+    case MT_DEVICE_nGnRE:
+        prbar.reg.sh = LPAE_SH_OUTER;
+        break;
+    default:
+        /* Xen mappings are SMP coherent */
+        prbar.reg.sh = LPAE_SH_INNER;
+        break;
+    }
+
+    /* Build up value for PRLAR_EL2. */
+    prlar = (prlar_t) {
+        .reg = {
+            .ns = 0,        /* Hyp mode is in secure world */
+            .ai = attr_idx,
+            .en = 1,        /* Region enabled */
+        }};
+
+    /* Build up MPU memory region. */
+    region = (pr_t) {
+        .prbar = prbar,
+        .prlar = prlar,
+    };
+
+    /* Set base address and limit address. */
+    pr_set_base(&region, base);
+    pr_set_limit(&region, limit);
+
+    return region;
+}

#endif

Quoted 7 lines
Quoted 1 line

void __init setup_mm(void)

Other than that:

~Michal

Cheers,
Luca

#20 lore raw

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

Quoted 19 lines · Luca Fancellu

Hi Michal,

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/mpu/mm.c +8 -0
+
+pr_t pr_of_addr(paddr_t base, paddr_t limit, unsigned int flags)
+{
+    unsigned int attr_idx = PAGE_AI_MASK(flags);
+    prbar_t prbar;
+    prlar_t prlar;
+    pr_t region;
+
Quoted 8 lines

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

Quoted 5 lines
xen/arch/arm/mpu/mm.c +5 -0
+    /* Build up value for PRBAR_EL2. */
+    prbar = (prbar_t) {
+        .reg = {
+            .ro = PAGE_RO_MASK(flags),
+            .xn = PAGE_XN_MASK(flags),

Shouldn't you also initialize .xn_0 and .ap_0 or you rely on compound literal implicit zero initialization of unspecified fields? But then you do initialize .ns to 0 fror prlar...

Quoted 2 lines

Yes, because there I would like to specify that value 0 means Hyp in secure world, but of course if you want I can explicitly initialise also these two

I'd prefer that way (so that it's more clear what are all the fields initialized to) but I can understand that this is very subjective so you have a freedom of choice here.

~Michal