Skip to contentSkip to conversation

First chunk for Arm R82 and MPU support

PATCH Superseded Luca Fancellu 3 participants 35 messages Apr 11, 2025 – May 7, 2025

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 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.

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 (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 |  62 +++++++
 xen/arch/arm/include/asm/mpu.h       |  52 ++++++
 xen/arch/arm/include/asm/mpu/mm.h    |  27 +++
 xen/arch/arm/include/asm/page.h      |  25 +++
 xen/arch/arm/mpu/mm.c                | 251 ++++++++++++++++++++++++++-
 xen/arch/arm/mpu/setup.c             |  54 +++++-
 6 files changed, 468 insertions(+), 3 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 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

Re: [PATCH v3 1/7] arm/mpu: Introduce MPU memory region map structure

Quoted 6 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.

Quoted 7 lines

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.

What should be the size of this array? I thought NUM_MPU_REGIONS indicates how many regions there can be (i.e. 256) and this should be the size. Yet you use MASK for size which is odd.

Quoted 17 lines
---
 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 +0 -0
new file mode 100644
index 000000000000..4d2bd7d7877f

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

Quoted 3 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +3 -0
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions for aarch64.

NIT: Do we really see the benefit in having such generic comments? What if you add a prototype of some function here. Will it fit into a definition scope?

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

Quoted 12 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +12 -0
+ */
+
+#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 */

s/Acess/Access/

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

Quoted 1 line
xen/arch/arm/include/asm/arm64/mpu.h new file +1 -0
+        unsigned long sh:2;       /* Sharebility */

s/Sharebility/Shareability/

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

Quoted 2 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +2 -0
+        unsigned long base:46;    /* Base Address */
+        unsigned long pad:12;

If you describe the register 1:1, why "pad" and not "res" or "res0"?

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

Quoted 11 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +11 -0
+    } 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 */

res0 /* RES0 */

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

Quoted 2 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +2 -0
+        unsigned long limit:46; /* Limit Address */
+        unsigned long pad:12;

res1 /* RES0 */

Quoted 4 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +4 -0
+    } reg;
+    uint64_t bits;
+} prlar_t;
+

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

Quoted 10 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +9 -0
+/* 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

Please add a new line at the end

Also, EMACS comment is missing.

Quoted 31 lines
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 +0 -0
index 07c8959f4ee9..f83ce04fef8a 100644
@@ -7,9 +7,13 @@
 #include <xen/mm.h>
 #include <xen/sizes.h>
 #include <xen/types.h>

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

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

~Michal

#3 lore raw

Re: [PATCH v3 1/7] arm/mpu: Introduce MPU memory region map structure

Hi Michal,

Quoted 14 lines · Michal
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.

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

Quoted 7 lines
Quoted 4 lines

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.

What should be the size of this array? I thought NUM_MPU_REGIONS indicates how many regions there can be (i.e. 256) and this should be the size. Yet you use MASK for size which is odd.

So the maximum number of regions for aarch64 armv8-r are 255, MPUIR_EL2.REGION is an 8 bit field advertising the number of region supported.

Is it better if I use just the below?

#define MAX_MPU_REGIONS 255

Quoted 12 lines
Quoted 12 lines

---

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

Quoted 11 lines

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

Quoted 9 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +3 -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.

NIT: Do we really see the benefit in having such generic comments? What if you add a prototype of some function here. Will it fit into a definition scope?

I can remove the comment, but I would say that if I put some function prototype here it should be related to arm64, being this file under arm64.

Quoted 55 lines

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

Quoted 13 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +12 -0
+ */
+
+#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 */

s/Acess/Access/

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

Quoted 1 line
xen/arch/arm/include/asm/arm64/mpu.h new file +1 -0
+        unsigned long sh:2;       /* Sharebility */

s/Sharebility/Shareability/

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

Quoted 2 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +2 -0
+        unsigned long base:46;    /* Base Address */
+        unsigned long pad:12;

If you describe the register 1:1, why "pad" and not "res" or "res0"?

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

Quoted 11 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +11 -0
+    } 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 */

res0 /* RES0 */

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

Quoted 2 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +2 -0
+        unsigned long limit:46; /* Limit Address */
+        unsigned long pad:12;

res1 /* RES0 */

Quoted 4 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +4 -0
+    } reg;
+    uint64_t bits;
+} prlar_t;
+

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

Quoted 10 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +9 -0
+/* 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

Please add a new line at the end

Quoted 1 line

Also, EMACS comment is missing.

Thanks I will fix all these findings

Cheers,
Luca

#4 lore raw

Re: [PATCH v3 1/7] arm/mpu: Introduce MPU memory region map structure

Quoted 20 lines · Luca Fancellu

Hi Michal,

Quoted 18 lines · Michal
Quoted 13 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.

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.

Quoted 6 lines
Quoted 3 lines

What should be the size of this array? I thought NUM_MPU_REGIONS indicates how many regions there can be (i.e. 256) and this should be the size. Yet you use MASK for size which is odd.

So the maximum number of regions for aarch64 armv8-r are 255, MPUIR_EL2.REGION is an 8 bit field advertising the number of region supported.

So there can be max 255 regions. Ok.

Quoted 4 lines

Is it better if I use just the below?

#define MAX_MPU_REGIONS 255

If there are 255 regions, what NUM_MPU_REGIONS macro is for which stores 256? These two macros confuse me. Or is it that by your macro you want to denote the max region number? In that case, the macro should be named MAX_MPU_REGION_NR or alike.

Quoted 25 lines
Quoted 13 lines
Quoted 13 lines

---

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

Quoted 11 lines

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

Quoted 9 lines
xen/arch/arm/include/asm/arm64/mpu.h new file +3 -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.

NIT: Do we really see the benefit in having such generic comments? What if you add a prototype of some function here. Will it fit into a definition scope?

Quoted 2 lines

I can remove the comment, but I would say that if I put some function prototype here it should be related to arm64, being this file under arm64.

Sorry, I don't see why you mention arm64 here. It is under arm64 directory, so it's clear. I was referring to the word "definition".

~Michal

#5 lore raw

Re: [PATCH v3 1/7] arm/mpu: Introduce MPU memory region map structure

Hi Michal,

Quoted 34 lines · Michal
Quoted 20 lines · Luca Fancellu

Hi Michal,

Quoted 18 lines · Michal
Quoted 13 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.

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.

Quoted 6 lines
Quoted 3 lines

What should be the size of this array? I thought NUM_MPU_REGIONS indicates how many regions there can be (i.e. 256) and this should be the size. Yet you use MASK for size which is odd.

So the maximum number of regions for aarch64 armv8-r are 255, MPUIR_EL2.REGION is an 8 bit field advertising the number of region supported.

So there can be max 255 regions. Ok.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

Is it better if I use just the below?

#define MAX_MPU_REGIONS 255

If there are 255 regions, what NUM_MPU_REGIONS macro is for which stores 256? These two macros confuse me. Or is it that by your macro you want to denote the max region number? In that case, the macro should be named MAX_MPU_REGION_NR or alike.

I know, NUM_MPU_REGIONS should have a different name as it’s a bit misleading, ok I’ll name the macro I use here as MAX_MPU_REGION_NR.

Cheers,
Luca

#6 lore raw

PATCH v3 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.

---
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 |   7 ++
 xen/arch/arm/include/asm/mpu.h       |   1 +
 xen/arch/arm/include/asm/mpu/mm.h    |  24 +++++
 xen/arch/arm/mpu/mm.c                | 125 +++++++++++++++++++++++++++
 4 files changed, 157 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +7 -0
index 4d2bd7d7877f..b4e1ecdf741d 100644
@@ -8,6 +8,13 @@
 
 #ifndef __ASSEMBLY__
 
+/*
+ * The following are needed for the case generators GENERATE_WRITE_PR_REG_CASE
+ * and GENERATE_READ_PR_REG_CASE with num==0
+ */
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2
+
 /* Protection Region Base Address Register */
 typedef union {
     struct __packed {
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 +125 -0

143 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

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

Hi Luca,

Quoted 24 lines · Luca Fancellu

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

---
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 |   7 ++
  xen/arch/arm/include/asm/mpu.h       |   1 +
  xen/arch/arm/include/asm/mpu/mm.h    |  24 +++++
  xen/arch/arm/mpu/mm.c                | 125 +++++++++++++++++++++++++++
  4 files changed, 157 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 4d2bd7d7877f..b4e1ecdf741d 100644

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

Quoted 9 lines
xen/arch/arm/include/asm/arm64/mpu.h +6 -0
@@ -8,6 +8,13 @@
 
  #ifndef __ASSEMBLY__
 
+/*
+ * The following are needed for the case generators GENERATE_WRITE_PR_REG_CASE
+ * and GENERATE_READ_PR_REG_CASE with num==0
+ */
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2

Rather than aliasing, shouldn't we just rename PR{B,L}AR_EL2 to PR{B,L}AR0_EL2? This would the code mixing between the two.

Quoted 29 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+
  /* Protection Region Base Address Register */
  typedef union {
      struct __packed {
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 +1 -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;
 

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

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

I know we discussed about this before. I find odd that the specification says "context synchronization event and DSB operation". At least to me, it implies "isb + dsb" not the other way around. Has this been clarified in newer version of the specification?

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

Quoted 7 lines
xen/arch/arm/include/asm/mpu/mm.h +7 -0
+     */
+    dsb(sy);
+    isb();
+}
+
+/*
+ * The following API require context_sync_mpu() after being used to modifiy MPU

typo: s/require/requires/ and s/modifiy/modify/

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

Quoted 6 lines
xen/arch/arm/include/asm/mpu/mm.h +6 -0
+ * 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);

I am probably missing something. But don't you have a copy of pr_t in xen_mpumap? If so, can't we use the cached version to avoid accessing the system registers?

Quoted 42 lines
xen/arch/arm/include/asm/mpu/mm.h +3 -0
+/* 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 +18 -0
index f83ce04fef8a..e522ce53c357 100644
@@ -8,12 +8,30 @@
  #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];
 
+#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)
  {
      /*

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

Quoted 6 lines
xen/arch/arm/mpu/mm.c +3 -0
@@ -24,6 +42,113 @@ static void __init __maybe_unused build_assertions(void)
      BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
  }
 
+static void prepare_selector(uint8_t *sel)
+{
+    uint8_t cur_sel = *sel;

Coding style: Missing newline.

Quoted 18 lines
xen/arch/arm/mpu/mm.c +18 -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.
+     */
+    cur_sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
+    {
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
+        isb();
+    }
+    *sel = *sel & 0xFU;
+}
+
+/*
+ * 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

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

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

I am a bit confused. This function is implemented in the common MPU code. Yet, then comment only refer to 64-bit. Is the code the same on 32-bit? If not, then I think this function wants to be moved in arm64/mpu/

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

Quoted 6 lines
xen/arch/arm/mpu/mm.c +6 -0
+/*
+ * Read EL2 MPU Protection Region.
+ *
+ * @pr_read: mpu protection region returned by read op.
+ * @sel: mpu protection region selector
+ */

NIT: Usually we add documentation on the prototype in the header and not in the definition.

Quoted 63 lines
xen/arch/arm/mpu/mm.c +63 -0
+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 */
+    }
+}
+
+/*
+ * 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 )
+    {
+        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);

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

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

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

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

HI Julien,

Quoted 28 lines · Julien Grall

Hi Luca,

Quoted 23 lines · Luca Fancellu

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

---
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 |   7 ++
 xen/arch/arm/include/asm/mpu.h       |   1 +
 xen/arch/arm/include/asm/mpu/mm.h    |  24 +++++
 xen/arch/arm/mpu/mm.c                | 125 +++++++++++++++++++++++++++
 4 files changed, 157 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 4d2bd7d7877f..b4e1ecdf741d 100644
@@ -8,6 +8,13 @@
   #ifndef __ASSEMBLY__
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/include/asm/arm64/mpu.h +5 -0
 +/*
+ * The following are needed for the case generators GENERATE_WRITE_PR_REG_CASE
+ * and GENERATE_READ_PR_REG_CASE with num==0
+ */
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2

Rather than aliasing, shouldn't we just rename PR{B,L}AR_EL2 to PR{B,L}AR0_EL2? This would the code mixing between the two.

PR{B,L}AR0_ELx does not exists really, the PR{B,L}AR<n>_ELx exists for n=1..15, here I’m only using this “alias” for the generator, but PR{B,L}AR_EL2 are the real register.

Quoted 30 lines
Quoted 30 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+
 /* Protection Region Base Address Register */
 typedef union {
     struct __packed {
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 +1 -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 */

Quoted 8 lines

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

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

I know we discussed about this before. I find odd that the specification says "context synchronization event and DSB operation". At least to me, it implies "isb + dsb" not the other way around. Has this been clarified in newer version of the specification?

unfortunately no, I’m looking into the latest one (Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture 0600B.a) but it has the same wording, however I spoke internally with Cortex-R architects and they told me to use DSB+ISB

Quoted 11 lines

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

Quoted 8 lines
xen/arch/arm/include/asm/mpu/mm.h +7 -0
+     */
+    dsb(sy);
+    isb();
+}
+
+/*
+ * The following API require context_sync_mpu() after being used to modifiy MPU

typo: s/require/requires/ and s/modifiy/modify/

Quoted 8 lines

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

Quoted 6 lines

+ * regions:
+ * - write_protection_region
+ */

xen/arch/arm/include/asm/mpu/mm.h +3 -0
+
+/* Reads the MPU region with index 'sel' from the HW */
+extern void read_protection_region(pr_t *pr_read, uint8_t sel);

I am probably missing something. But don't you have a copy of pr_t in xen_mpumap? If so, can't we use the cached version to avoid accessing the system registers?

This API is meant to read/write registers, last patch uses it to populate xen_mpumap, along the tree it is also used in dump_hyp_walk, probably given your comment to the last patch, if we need to update the xen_mpumap from the asm code, this could change.

Quoted 38 lines
Quoted 38 lines
xen/arch/arm/include/asm/mpu/mm.h +3 -0
+/* 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 +17 -0
index f83ce04fef8a..e522ce53c357 100644
@@ -8,12 +8,30 @@
 #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];
 +#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)
 {
     /*
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/mpu/mm.c +2 -0
@@ -24,6 +42,113 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
 }
 +static void prepare_selector(uint8_t *sel)
+{
+    uint8_t cur_sel = *sel;

Coding style: Missing newline.

will fix

Quoted 23 lines
Quoted 23 lines
xen/arch/arm/mpu/mm.c +22 -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.
+     */
+    cur_sel &= 0xF0U;
+    if ( READ_SYSREG(PRSELR_EL2) != cur_sel )
+    {
+        WRITE_SYSREG(cur_sel, PRSELR_EL2);
+        isb();
+    }
+    *sel = *sel & 0xFU;
+}
+
+/*
+ * 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
Quoted 8 lines

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

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

I am a bit confused. This function is implemented in the common MPU code. Yet, then comment only refer to 64-bit. Is the code the same on 32-bit? If not, then I think this function wants to be moved in arm64/mpu/

I’ll try to reword the comment removing the arm64-only parts

Quoted 9 lines

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

Quoted 7 lines
xen/arch/arm/mpu/mm.c +6 -0
+/*
+ * Read EL2 MPU Protection Region.
+ *
+ * @pr_read: mpu protection region returned by read op.
+ * @sel: mpu protection region selector
+ */

NIT: Usually we add documentation on the prototype in the header and not in the definition.

I’ll change

Quoted 80 lines
Quoted 64 lines
xen/arch/arm/mpu/mm.c +63 -0
+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 */
+    }
+}
+
+/*
+ * 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 )
+    {
+        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);

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

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

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

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

Hi Luca,

Quoted 39 lines · Luca Fancellu

HI Julien,

Quoted 28 lines · Julien Grall

Hi Luca,

Quoted 23 lines · Luca Fancellu

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

---
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 |   7 ++
  xen/arch/arm/include/asm/mpu.h       |   1 +
  xen/arch/arm/include/asm/mpu/mm.h    |  24 +++++
  xen/arch/arm/mpu/mm.c                | 125 +++++++++++++++++++++++++++
  4 files changed, 157 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index 4d2bd7d7877f..b4e1ecdf741d 100644
@@ -8,6 +8,13 @@
    #ifndef __ASSEMBLY__
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/include/asm/arm64/mpu.h +5 -0
  +/*
+ * The following are needed for the case generators GENERATE_WRITE_PR_REG_CASE
+ * and GENERATE_READ_PR_REG_CASE with num==0
+ */
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2

Rather than aliasing, shouldn't we just rename PR{B,L}AR_EL2 to PR{B,L}AR0_EL2? This would the code mixing between the two.

Quoted 2 lines

PR{B,L}AR0_ELx does not exists really, the PR{B,L}AR<n>_ELx exists for n=1..15, here I’m only using this “alias” for the generator, but PR{B,L}AR_EL2 are the real register.

In this case, can PR{B,L}AR0_EL2 defined in mm.c so they are not used anywhere else?

Quoted 40 lines
Quoted 31 lines
Quoted 31 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+
  /* Protection Region Base Address Register */
  typedef union {
      struct __packed {
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 +1 -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 */

Quoted 8 lines

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

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

I know we discussed about this before. I find odd that the specification says "context synchronization event and DSB operation". At least to me, it implies "isb + dsb" not the other way around. Has this been clarified in newer version of the specification?

Quoted 2 lines

unfortunately no, I’m looking into the latest one (Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture 0600B.a) but it has the same wording, however I spoke internally with Cortex-R architects and they told me to use DSB+ISB

So you didn't speak with the ArmV8-R architects? Asking because we are writing code for ArmV8-R (so not only Cortex-R).

In any case, I still think this is something that needs to be clarified
in the specification. So people that don't have access to the Arm internal architects know the correct sequence. Is this something you can follow-up on?

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

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

Hi Julien,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 6 lines

+/*
+ * The following are needed for the case generators GENERATE_WRITE_PR_REG_CASE
+ * and GENERATE_READ_PR_REG_CASE with num==0
+ */

xen/arch/arm/include/asm/arm64/mpu.h +2 -0
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2

Rather than aliasing, shouldn't we just rename PR{B,L}AR_EL2 to PR{B,L}AR0_EL2? This would the code mixing between the two.

Quoted 4 lines
Quoted 2 lines

PR{B,L}AR0_ELx does not exists really, the PR{B,L}AR<n>_ELx exists for n=1..15, here I’m only using this “alias” for the generator, but PR{B,L}AR_EL2 are the real register.

In this case, can PR{B,L}AR0_EL2 defined in mm.c so they are not used anywhere else?

So this was the case in my previous serie, but Ayan asked me to put them in here because PRBAR_EL2 is arm64 specific, not sure now, shall we move it back and protect it with CONFIG_ARM_64?

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

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

Quoted 9 lines

}
+/* Utility function to be used whenever MPU regions are modified */

xen/arch/arm/include/asm/mpu/mm.h +6 -0
+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.

I know we discussed about this before. I find odd that the specification says "context synchronization event and DSB operation". At least to me, it implies "isb + dsb" not the other way around. Has this been clarified in newer version of the specification?

Quoted 7 lines
Quoted 2 lines

unfortunately no, I’m looking into the latest one (Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture 0600B.a) but it has the same wording, however I spoke internally with Cortex-R architects and they told me to use DSB+ISB

So you didn't speak with the ArmV8-R architects? Asking because we are writing code for ArmV8-R (so not only Cortex-R).

In any case, I still think this is something that needs to be clarified
in the specification. So people that don't have access to the Arm internal architects know the correct sequence. Is this something you can follow-up on?

Yes I will follow up this one

Cheers,
Luca

#11 lore raw

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

Quoted 10 lines · Luca Fancellu

Hi Julien,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 6 lines

+/*
+ * The following are needed for the case generators GENERATE_WRITE_PR_REG_CASE
+ * and GENERATE_READ_PR_REG_CASE with num==0
+ */

xen/arch/arm/include/asm/arm64/mpu.h +2 -0
+#define PRBAR0_EL2 PRBAR_EL2
+#define PRLAR0_EL2 PRLAR_EL2

Rather than aliasing, shouldn't we just rename PR{B,L}AR_EL2 to PR{B,L}AR0_EL2? This would the code mixing between the two.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

PR{B,L}AR0_ELx does not exists really, the PR{B,L}AR<n>_ELx exists for n=1..15, here I’m only using this “alias” for the generator, but PR{B,L}AR_EL2 are the real register.

In this case, can PR{B,L}AR0_EL2 defined in mm.c so they are not used anywhere else?

So this was the case in my previous serie, but Ayan asked me to put them in here because PRBAR_EL2 is arm64 specific, not sure now, shall we move it back and protect it with CONFIG_ARM_64?

Yes. I don't thikn PRBAR0_EL2 should be exposed.

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

PATCH v3 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
#13 lore raw

Re: [PATCH v3 3/7] arm/mpu: Introduce utility functions for the pr_t type

Quoted 13 lines · 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 +0 -0
index 59ff22c804c1..6971507457fb 100644

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

Quoted 6 lines
xen/arch/arm/include/asm/mpu.h +3 -0
@@ -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). */

What's the use of (pr_t) in this comment? pr_t is a data type. If at all, it would want to be ...region @pr but I think you can skip it.

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

Quoted 3 lines
xen/arch/arm/include/asm/mpu.h +3 -0
+static inline void pr_set_base(pr_t *pr, paddr_t base)
+{
+    pr->prbar.reg.base = (base >> MPU_REGION_SHIFT);

Looking at pr_t definition, base/limit is 46 bits wide. However the spec says that last 4bits are reserved (i.e. you should not write to them) unless FEAT_LPA is implemented. What's our plan here?

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

Quoted 6 lines
xen/arch/arm/include/asm/mpu.h +6 -0
+}
+
+/* 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);

Why -1? AFAIR these registers take inclusive addresses, so is it because you want caller to pass limit as exclusive and you convert it to inclusive? I think it's quite error prone.

~Michal

#14 lore raw

Re: [PATCH v3 3/7] arm/mpu: Introduce utility functions for the pr_t type

Hi Michal,

Quoted 21 lines · Michal
Quoted 16 lines · 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 +0 -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
 
Quoted 5 lines

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

Quoted 3 lines
xen/arch/arm/include/asm/mpu.h +3 -0
+#ifndef __ASSEMBLY__
+
+/* Set base address of MPU protection region(pr_t). */

What's the use of (pr_t) in this comment? pr_t is a data type. If at all, it would want to be ...region @pr but I think you can skip it.

ok

Quoted 7 lines

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

Quoted 4 lines
xen/arch/arm/include/asm/mpu.h +3 -0
+static inline void pr_set_base(pr_t *pr, paddr_t base)
+{
+    pr->prbar.reg.base = (base >> MPU_REGION_SHIFT);

Looking at pr_t definition, base/limit is 46 bits wide. However the spec says that last 4bits are reserved (i.e. you should not write to them) unless FEAT_LPA is implemented. What's our plan here?

So we’re currently supporting max 1TB, so probably this one needs to be on the case when FEAT_LPA is considered not implemented, so I’ll change and if we will later support more than 42 bit we could do something?

Quoted 10 lines

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

Quoted 7 lines
xen/arch/arm/include/asm/mpu.h +6 -0
+}
+
+/* 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);

Why -1? AFAIR these registers take inclusive addresses, so is it because you want caller to pass limit as exclusive and you convert it to inclusive? I think it's quite error prone.

Yes it’s meant to be used with exclusive range, shall we document it or use Inclusive range instead?

Cheers,
Luca

#15 lore raw

Re: [PATCH v3 3/7] arm/mpu: Introduce utility functions for the pr_t type

Quoted 39 lines · Luca Fancellu

Hi Michal,

Quoted 21 lines · Michal
Quoted 16 lines · 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 +0 -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
 
Quoted 5 lines

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

Quoted 3 lines
xen/arch/arm/include/asm/mpu.h +3 -0
+#ifndef __ASSEMBLY__
+
+/* Set base address of MPU protection region(pr_t). */

What's the use of (pr_t) in this comment? pr_t is a data type. If at all, it would want to be ...region @pr but I think you can skip it.

ok

Quoted 7 lines

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

Quoted 4 lines
xen/arch/arm/include/asm/mpu.h +3 -0
+static inline void pr_set_base(pr_t *pr, paddr_t base)
+{
+    pr->prbar.reg.base = (base >> MPU_REGION_SHIFT);

Looking at pr_t definition, base/limit is 46 bits wide. However the spec says that last 4bits are reserved (i.e. you should not write to them) unless FEAT_LPA is implemented. What's our plan here?

Quoted 3 lines

So we’re currently supporting max 1TB, so probably this one needs to be on the case when FEAT_LPA is considered not implemented, so I’ll change and if we will later support more than 42 bit we could do something?

I think yes.

Quoted 12 lines
Quoted 11 lines

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

Quoted 8 lines
xen/arch/arm/include/asm/mpu.h +6 -0
+}
+
+/* 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);

Why -1? AFAIR these registers take inclusive addresses, so is it because you want caller to pass limit as exclusive and you convert it to inclusive? I think it's quite error prone.

Quoted 2 lines

Yes it’s meant to be used with exclusive range, shall we document it or use Inclusive range instead?

What's the expected behavior of callers? Are we going to set up protection region based on regular start+size pair (like with MMU) or start+end? If the latter for some reason (with size there are less issues), then end usually is inclusive and you would not need conversion.

~Michal

#16 lore raw

Re: [PATCH v3 3/7] arm/mpu: Introduce utility functions for the pr_t type

Hi Michal,

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

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

Quoted 8 lines
xen/arch/arm/include/asm/mpu.h +6 -0
+}
+
+/* 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);

Why -1? AFAIR these registers take inclusive addresses, so is it because you want caller to pass limit as exclusive and you convert it to inclusive? I think it's quite error prone.

Quoted 6 lines
Quoted 2 lines

Yes it’s meant to be used with exclusive range, shall we document it or use Inclusive range instead?

What's the expected behavior of callers? Are we going to set up protection region based on regular start+size pair (like with MMU) or start+end? If the latter for some reason (with size there are less issues), then end usually is inclusive and you would not need conversion.

I think we have a mix because for example destroy_xen_mappings and modify_xen_mappings are start and end, map_pages_to_xen instead is kind of start+size?

I moved the -1 inside pr_set_limit because it was open-coded in all the places, for example when referencing linker symbols or output of mfn_to_maddr(mfn_add(smfn, nr_mfn)), for this reason I
thought: let’s call this one with exclusive ranges and modify internally for inclusive.

#17 lore raw

Re: [PATCH v3 3/7] arm/mpu: Introduce utility functions for the pr_t type

Quoted 21 lines · Luca Fancellu

Hi Michal,

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

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

Quoted 8 lines
xen/arch/arm/include/asm/mpu.h +6 -0
+}
+
+/* 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);

Why -1? AFAIR these registers take inclusive addresses, so is it because you want caller to pass limit as exclusive and you convert it to inclusive? I think it's quite error prone.

Quoted 6 lines
Quoted 2 lines

Yes it’s meant to be used with exclusive range, shall we document it or use Inclusive range instead?

What's the expected behavior of callers? Are we going to set up protection region based on regular start+size pair (like with MMU) or start+end? If the latter for some reason (with size there are less issues), then end usually is inclusive and you would not need conversion.

Quoted 6 lines

I think we have a mix because for example destroy_xen_mappings and modify_xen_mappings are start and end, map_pages_to_xen instead is kind of start+size?

I moved the -1 inside pr_set_limit because it was open-coded in all the places, for example when referencing linker symbols or output of mfn_to_maddr(mfn_add(smfn, nr_mfn)), for this reason I
thought: let’s call this one with exclusive ranges and modify internally for inclusive.

Hmm, yes. Indeed we have a mix of places where end is inclusive or exclusive. I think we can stick with exclusive address being passed to this helper unless others have a different opinion. I know we tried to convert some places to start+size but I don't remember the future plans.

~Michal

#18 lore raw

Re: [PATCH v3 3/7] arm/mpu: Introduce utility functions for the pr_t type

Quoted 31 lines · Michal
Quoted 24 lines · Luca Fancellu

Hi Michal,

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

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

Quoted 8 lines
xen/arch/arm/include/asm/mpu.h +6 -0
+}
+
+/* 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);

Why -1? AFAIR these registers take inclusive addresses, so is it because you want caller to pass limit as exclusive and you convert it to inclusive? I think it's quite error prone.

Quoted 6 lines
Quoted 2 lines

Yes it’s meant to be used with exclusive range, shall we document it or use Inclusive range instead?

What's the expected behavior of callers? Are we going to set up protection region based on regular start+size pair (like with MMU) or start+end? If the latter for some reason (with size there are less issues), then end usually is inclusive and you would not need conversion.

I think we have a mix because for example destroy_xen_mappings and modify_xen_mappings are start and end, map_pages_to_xen instead is kind of start+size?

Quoted 7 lines
Quoted 3 lines

I moved the -1 inside pr_set_limit because it was open-coded in all the places, for example when referencing linker symbols or output of mfn_to_maddr(mfn_add(smfn, nr_mfn)), for this reason I
thought: let’s call this one with exclusive ranges and modify internally for inclusive.

Hmm, yes. Indeed we have a mix of places where end is inclusive or exclusive. I think we can stick with exclusive address being passed to this helper unless others have a different opinion. I know we tried to convert some places to start+size but I don't remember the future plans.

Ok, I'll document on top of the helper that @limit needs to be exclusive just to be sure it will be used in this way.

#19 lore raw

PATCH v3 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 b4e1ecdf741d..eb0add5c6c2a 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__
 
 /*
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
#20 lore raw

PATCH v3 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
#21 lore raw

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

Hi Luca,

Quoted 20 lines · 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 +1 -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
   *

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

Quoted 10 lines
xen/arch/arm/include/asm/page.h +7 -0
@@ -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

I am rather confused why we are splitting Execute Never from the Access Permission. I guess you tried to match the HW, but it also means we need to duplicate a lot of define between the MMU and MPU code.

Instead, I would rather try to re-use the existing ones and ignore the ones we don't need (e.g. BLOCK_BIT and CONTIG).

Cheers,

Signature · 1 line
Julien Grall
#22 lore raw

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

Hi Julien,

Quoted 33 lines · Julien Grall

Hi Luca,

Quoted 16 lines · 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 +0 -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
  *

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

Quoted 9 lines
xen/arch/arm/include/asm/page.h +6 -0
@@ -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

I am rather confused why we are splitting Execute Never from the Access Permission. I guess you tried to match the HW, but it also means we need to duplicate a lot of define between the MMU and MPU code.

Quoted 1 line

Instead, I would rather try to re-use the existing ones and ignore the ones we don't need (e.g. BLOCK_BIT and CONTIG).

I’m having a bit of trouble understanding the MMU part:

/*
* Layout of the flags used for updating the hypervisor page tables *
* [0:2] Memory Attribute Index
* [3:4] Permission flags
* [5] Page present
* [6] Only populate page tables
* [7] Superpage mappings is allowed
* [8] Set contiguous bit (internal flag)
*/
#define PAGE_AI_MASK(x) ((x) & 0x7U)

#define _PAGE_XN_BIT 3
#define _PAGE_RO_BIT 4
#define _PAGE_XN (1U << _PAGE_XN_BIT)
#define _PAGE_RO (1U << _PAGE_RO_BIT)
#define PAGE_XN_MASK(x) (((x) >> _PAGE_XN_BIT) & 0x1U) #define PAGE_RO_MASK(x) (((x) >> _PAGE_RO_BIT) & 0x1U)

I can see on the MMU basically AP[1] means RO or not, AP[0] means XN or not, from the arm spec (verison L.a, D8.4.2.1.1 Stage 2 data accesses using Direct permissions) I can see stage 2 AP[1:0] is:
- 00: no access
- 01: RO
- 10: WO
- 11: RW

So:
- 00: read-only is zero and execution is allowed
- 01: read-only is zero and execution is not allowed
- 10: read-only is one (??), execution is allowed
- 11: read-only is one (??), execution is not allowed

I’m clearly missing something because everything works, so could you help me to understand this part?

#23 lore raw

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

Quoted 1 line · Luca Fancellu

Hi Julien,

Hi Luca,

Sorry for the late answer.

Quoted 57 lines
Quoted 34 lines · Julien Grall

Hi Luca,

Quoted 16 lines · 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 +0 -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
   *

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

Quoted 9 lines
xen/arch/arm/include/asm/page.h +6 -0
@@ -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

I am rather confused why we are splitting Execute Never from the Access Permission. I guess you tried to match the HW, but it also means we need to duplicate a lot of define between the MMU and MPU code.

Quoted 1 line

Instead, I would rather try to re-use the existing ones and ignore the ones we don't need (e.g. BLOCK_BIT and CONTIG).

I’m having a bit of trouble understanding the MMU part:

/*
* Layout of the flags used for updating the hypervisor page tables *
* [0:2] Memory Attribute Index
* [3:4] Permission flags
* [5] Page present
* [6] Only populate page tables
* [7] Superpage mappings is allowed
* [8] Set contiguous bit (internal flag)
*/
#define PAGE_AI_MASK(x) ((x) & 0x7U)

#define _PAGE_XN_BIT 3
#define _PAGE_RO_BIT 4
#define _PAGE_XN (1U << _PAGE_XN_BIT)
#define _PAGE_RO (1U << _PAGE_RO_BIT)
#define PAGE_XN_MASK(x) (((x) >> _PAGE_XN_BIT) & 0x1U) #define PAGE_RO_MASK(x) (((x) >> _PAGE_RO_BIT) & 0x1U)

Quoted 6 lines

I can see on the MMU basically AP[1] means RO or not, AP[0] means XN or not, from the arm spec (verison L.a, D8.4.2.1.1 Stage 2 data accesses using Direct permissions) I can see stage 2 AP[1:0] is:
- 00: no access
- 01: RO
- 10: WO
- 11: RW

This is describing the stage-2 S2AP field. Whereas the flags above are only used for EL2 stage-1. So you want to look at the table Table D8-62. That said...

Quoted 6 lines

So:
- 00: read-only is zero and execution is allowed
- 01: read-only is zero and execution is not allowed
- 10: read-only is one (??), execution is allowed
- 11: read-only is one (??), execution is not allowed

... part of flags doesn't directly correspond to AP. Instead, we individually set AP[2] (called ro for Xen), AP[1] is RES1 (as we have only one exception level). And then 'xn' is set separately.

See mfn_to_xen_entry() and xen_pt_update_entry():

        /* Set permission */
        pte.pt.ro = PAGE_RO_MASK(flags);
        pte.pt.xn = PAGE_XN_MASK(flags);

Let me know if it makes sense.

Cheers,

Signature · 1 line
Julien Grall
#24 lore raw

PATCH v3 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
#25 lore raw

Re: [PATCH v3 6/7] arm/mpu: Implement early_fdt_map support in MPU systems

Hi Luca,

Quoted 24 lines · 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 +1 -0
index b4da77003f47..5969065250d4 100644
@@ -1,17 +1,67 @@
  /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/bootfdt.h>
  #include <xen/bug.h>

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

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

This is common code. So you will need to keep in sync for both 32-bit and 64-bit. That said, someone modifying head.S will not necessarily remember that this needs to be kept in sync. Such comment would be more effective in head.S just after the last section.

However, the number of regions mapped can change. So I am not sure why we are...

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

Quoted 1 line
xen/arch/arm/mpu/setup.c +1 -0
+#define EARLY_FDT_MAP_REGION_NUMBER 6

... hardcoding to 6 rather than using the next available region.

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

Quoted 9 lines
xen/arch/arm/mpu/setup.c +3 -2
+
  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);

NIT: IIRC, the minimum for the MPU is 64-byte. So is there any reason we are enforcing a bigger alignment?

Quoted 35 lines
xen/arch/arm/mpu/setup.c +35 -0
+    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) )
+    {

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

Quoted 10 lines
xen/arch/arm/mpu/setup.c +7 -0
+        pr_set_limit(&fdt_region, end_paddr);
+
+        write_protection_region(&fdt_region, EARLY_FDT_MAP_REGION_NUMBER);
+        context_sync_mpu();
+    }
+
+    return fdt_virt;
  }
 
  /*

Cheers,

Signature · 1 line
Julien Grall
#26 lore raw

Re: [PATCH v3 6/7] arm/mpu: Implement early_fdt_map support in MPU systems

Hi Julien,

Quoted 11 lines

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

Quoted 9 lines
xen/arch/arm/mpu/setup.c +3 -2
+
 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);

NIT: IIRC, the minimum for the MPU is 64-byte. So is there any reason we are enforcing a bigger alignment?

So the original work starts from the assumption that we would like to reuse some of the helper used for MMU, so everything is developed to work on pages of 4kb, do you think it should change?

Cheers,
Luca

#27 lore raw

PATCH v3 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.

---
v3 changes:
- Moved PRENR_MASK define to common.
---

---
 xen/arch/arm/include/asm/mpu.h |  2 ++
 xen/arch/arm/mpu/mm.c          | 49 +++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/mpu.h +2 -0
index eba5086cde97..77d0566f9780 100644
@@ -20,6 +20,8 @@
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
 #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 
+#define PRENR_MASK  GENMASK(31, 0)
+
 /* Access permission attributes. */
 /* Read/Write at EL2, No Access at EL1/EL0. */
 #define AP_RW_EL2 0x0
xen/arch/arm/mpu/mm.c +48 -1

63 lines not on this page · raw message

Signature · 1 line
2.34.1
#28 lore raw

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

Hi Luca,

Quoted 38 lines · 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.

---
v3 changes:
- Moved PRENR_MASK define to common.
---

---
  xen/arch/arm/include/asm/mpu.h |  2 ++
  xen/arch/arm/mpu/mm.c          | 49 +++++++++++++++++++++++++++++++++-
  2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/mpu.h +2 -0
index eba5086cde97..77d0566f9780 100644
@@ -20,6 +20,8 @@
  #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
  #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 
+#define PRENR_MASK  GENMASK(31, 0)
+
  /* Access permission attributes. */
  /* Read/Write at EL2, No Access at EL1/EL0. */
  #define AP_RW_EL2 0x0
xen/arch/arm/mpu/mm.c +0 -0
index 635d1f5a2ba0..e0a40489a7fc 100644

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

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

Are this variable and ...

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

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

... this one meant to be global? If yes, then they need to have a declaration in the header. If not, then you want to add 'static'.

Quoted 21 lines
xen/arch/arm/mpu/mm.c +14 -0
+
  /* EL2 Xen MPU memory region mapping table. */
  pr_t xen_mpumap[MAX_MPU_REGIONS];
 
@@ -222,9 +233,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.
+     */

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 258

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

This is a bit fragile. I think it would be better if the bitmap is set by head.S as we add the regions. Same for ...

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 264

Quoted 6 lines
xen/arch/arm/mpu/mm.c +6 -0
+    while ( prenr > 0 )
+    {
+        if (prenr & 0x1)
+        {
+            set_bit(i, xen_mpumap_mask);
+            read_protection_region(&xen_mpumap[i], i);

... xen_mpumap.

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 277

Quoted 13 lines
xen/arch/arm/mpu/mm.c +8 -1
+        }
+
+        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)

Cheers,

Signature · 1 line
Julien Grall
#29 lore raw

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

HI Julien,

Quoted 44 lines · Julien Grall

Hi Luca,

Quoted 33 lines · 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.

---
v3 changes:
- Moved PRENR_MASK define to common.
---

---
 xen/arch/arm/include/asm/mpu.h |  2 ++
 xen/arch/arm/mpu/mm.c          | 49 +++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/mpu.h +1 -0
index eba5086cde97..77d0566f9780 100644
@@ -20,6 +20,8 @@
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
 #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
 +#define PRENR_MASK  GENMASK(31, 0)
+
 /* Access permission attributes. */
 /* Read/Write at EL2, No Access at EL1/EL0. */
 #define AP_RW_EL2 0x0
xen/arch/arm/mpu/mm.c +0 -0
index 635d1f5a2ba0..e0a40489a7fc 100644

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

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

Are this variable and ...

Quoted 10 lines

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

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

... this one meant to be global? If yes, then they need to have a declaration in the header. If not, then you want to add 'static'.

yes they are meant to be global, I’ll add a declaration in the header.

Quoted 23 lines
Quoted 23 lines
xen/arch/arm/mpu/mm.c +16 -0
+
 /* EL2 Xen MPU memory region mapping table. */
 pr_t xen_mpumap[MAX_MPU_REGIONS];
 @@ -222,9 +233,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 */
Quoted 8 lines

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 266

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

This is a bit fragile. I think it would be better if the bitmap is set by head.S as we add the regions. Same for ...

So, I was trying to avoid that because in that case we need to place xen_mpumap out of the BSS and start manipulating the bitmap from asm, instead I was hoping to use the C code, I understand that if someone wants to have more than 31 region as boot region this might break, but it’s also a bit unlikely?

So I was balancing the pros to manipulate everything from the C world against the cons (boot region > 31).

Is it still your preferred way to handle everything from asm?

Cheers,
Luca

#30 lore raw

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

Hi Luca,

Quoted 91 lines · Luca Fancellu

HI Julien,

Quoted 44 lines · Julien Grall

Hi Luca,

Quoted 33 lines · 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.

---
v3 changes:
- Moved PRENR_MASK define to common.
---

---
  xen/arch/arm/include/asm/mpu.h |  2 ++
  xen/arch/arm/mpu/mm.c          | 49 +++++++++++++++++++++++++++++++++-
  2 files changed, 50 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/mpu.h +1 -0
index eba5086cde97..77d0566f9780 100644
@@ -20,6 +20,8 @@
  #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
  #define MAX_MPU_REGIONS         NUM_MPU_REGIONS_MASK
  +#define PRENR_MASK  GENMASK(31, 0)
+
  /* Access permission attributes. */
  /* Read/Write at EL2, No Access at EL1/EL0. */
  #define AP_RW_EL2 0x0
xen/arch/arm/mpu/mm.c +0 -0
index 635d1f5a2ba0..e0a40489a7fc 100644

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

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

Are this variable and ...

Quoted 10 lines

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

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

... this one meant to be global? If yes, then they need to have a declaration in the header. If not, then you want to add 'static'.

yes they are meant to be global, I’ll add a declaration in the header.

Quoted 23 lines
Quoted 23 lines
xen/arch/arm/mpu/mm.c +16 -0
+
  /* EL2 Xen MPU memory region mapping table. */
  pr_t xen_mpumap[MAX_MPU_REGIONS];
  @@ -222,9 +233,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 */
Quoted 8 lines

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 266

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

This is a bit fragile. I think it would be better if the bitmap is set by head.S as we add the regions. Same for ...

Quoted 3 lines

So, I was trying to avoid that because in that case we need to place xen_mpumap out of the BSS and start manipulating the bitmap from asm, instead I was hoping to use the C code, I understand that if someone wants to have more than 31 region as boot region this might break, but it’s also a bit unlikely?

The 31 is only part of the problem. The other problem is there is at least one other places in Xen (i.e. as early_fdt_map()) which will also create an entry in the MPU before setup_mm()/setup_mpu() is called. I am a bit concerned that the assumption is going to spread and yet we have no way to programmatically check if this will be broken.

Furthermore, right now, you are hardcoding the slot used and updating the MPU. But if you had the bitmap updated, you could just look up for a free slot.

Quoted 4 lines

So I was balancing the pros to manipulate everything from the C world against the cons (boot region > 31).

Is it still your preferred way to handle everything from asm?

Yes. I don't think the change in asm will be large and this would allow to remove other assumptions (like in the FDT mapping code).

As a side note, I noticed that the MPU entries are not cleared before we enable the MPU. Is there anything in the boot protocol that guarantee all the entries will be invalid? If not, then I think we need to clear the entries.

Otherwise, your current logic doesn't work. That said, I think it would still be necessary even if we get rid of your logic because we don't know the content of the MPU entries.

Cheers,

Signature · 1 line
Julien Grall
#31 lore raw

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

Hi Julien,

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

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 266

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

This is a bit fragile. I think it would be better if the bitmap is set by head.S as we add the regions. Same for ...

Quoted 5 lines
Quoted 3 lines

So, I was trying to avoid that because in that case we need to place xen_mpumap out of the BSS and start manipulating the bitmap from asm, instead I was hoping to use the C code, I understand that if someone wants to have more than 31 region as boot region this might break, but it’s also a bit unlikely?

The 31 is only part of the problem. The other problem is there is at least one other places in Xen (i.e. as early_fdt_map()) which will also create an entry in the MPU before setup_mm()/setup_mpu() is called. I am a bit concerned that the assumption is going to spread and yet we have no way to programmatically check if this will be broken.

If we would like to find ways, we could check eventually for clashes on enabled MPU regions;
so the only part where a region is created in the C world without the assistance of xen_mpumap is early_fdt_map(), asserts etc could be used in one or both setup_mpu and early_fdt_map to prevent breakage.

Quoted 2 lines

Furthermore, right now, you are hardcoding the slot used and updating the MPU. But if you had the bitmap updated, you could just look up for a free slot.

of course, but still the early DTB map is temporary and it will be gone after boot, so it won’t impact much unless I’m missing something.

Quoted 5 lines
Quoted 3 lines

So I was balancing the pros to manipulate everything from the C world against the cons (boot region > 31). Is it still your preferred way to handle everything from asm?

Yes. I don't think the change in asm will be large and this would allow to remove other assumptions (like in the FDT mapping code).

not large, but still something to be maintained, we will need arm64/arm32 code to set/clear bits on the bitmap
(causing duplication with bitops.c), code to save things on the xen_mpumap, code to clean/invalidate dcache for the entries in xen_mpumap and finally we will need to keep the code aligned to the implementation of the bitmap (which is fairly stable, but still needs to be taken into account).

Quoted 4 lines

As a side note, I noticed that the MPU entries are not cleared before we enable the MPU. Is there anything in the boot protocol that guarantee all the entries will be invalid? If not, then I think we need to clear the entries.

Otherwise, your current logic doesn't work. That said, I think it would still be necessary even if we get rid of your logic because we don't know the content of the MPU entries.

The PRLAR.EN bit resets to zero on a warm reset, so any region will be always disabled unless programmed, I thought it is enough.

Cheers,
Luca

#32 lore raw

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

Hi Luca,

Quoted 28 lines · Luca Fancellu

Hi Julien,

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

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 266

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

This is a bit fragile. I think it would be better if the bitmap is set by head.S as we add the regions. Same for ...

Quoted 5 lines
Quoted 3 lines

So, I was trying to avoid that because in that case we need to place xen_mpumap out of the BSS and start manipulating the bitmap from asm, instead I was hoping to use the C code, I understand that if someone wants to have more than 31 region as boot region this might break, but it’s also a bit unlikely?

The 31 is only part of the problem. The other problem is there is at least one other places in Xen (i.e. as early_fdt_map()) which will also create an entry in the MPU before setup_mm()/setup_mpu() is called. I am a bit concerned that the assumption is going to spread and yet we have no way to programmatically check if this will be broken.

Quoted 8 lines

If we would like to find ways, we could check eventually for clashes on enabled MPU regions;
so the only part where a region is created in the C world without the assistance of xen_mpumap is early_fdt_map(), asserts etc could be used in one or both setup_mpu and early_fdt_map to prevent breakage.

Quoted 2 lines
Quoted 1 line

Furthermore, right now, you are hardcoding the slot used and updating the MPU. But if you had the bitmap updated, you could just look up for a free slot.

of course, but still the early DTB map is temporary and it will be gone after boot, so it won’t impact much unless I’m missing something.

It doesn't really matter whether the region is temporary or not. My concern is you are making assumption that are difficult to track (they are not documented where a developper would most likely look at).

So if we still want to hardcode the value, then this should be documented in head.S and probably in a layout.h (or similar) so there is a single place where the MPU layout is described.

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

So I was balancing the pros to manipulate everything from the C world against the cons (boot region > 31). Is it still your preferred way to handle everything from asm?

Yes. I don't think the change in asm will be large and this would allow to remove other assumptions (like in the FDT mapping code).

not large, but still something to be maintained, we will need arm64/arm32 code to set/clear bits on the bitmap
(causing duplication with bitops.c), code to save things on the xen_mpumap, code to clean/invalidate dcache for the entries in xen_mpumap and finally we will need to keep the code aligned to the implementation of the bitmap (which is fairly stable, but still needs to be taken into account).

I understand the changes and risks, but I still think this is the right approach. Let see what the other maintainers think.

Quoted 7 lines
Quoted 5 lines

As a side note, I noticed that the MPU entries are not cleared before we enable the MPU. Is there anything in the boot protocol that guarantee all the entries will be invalid? If not, then I think we need to clear the entries.

Otherwise, your current logic doesn't work. That said, I think it would still be necessary even if we get rid of your logic because we don't know the content of the MPU entries.

The PRLAR.EN bit resets to zero on a warm reset, so any region will be always disabled unless programmed, I thought it is enough.

This is only telling me the state PRLAR.EN when the CPU is initially turn on. This doesn't tell me the value of the bit when jumping in Xen.

I am making the difference because there might be another software running at EL2 before jumping into Xen (e.g. bootloader, or even a previous Xen if we were using Kexec) which could use the MPU.

So I am looking for some details on how the expected state of the system when jumping to an OS/hypervisor. For a comparison, on the MMU side, we have the Linux arm64 Image protocol that will specific how a bootloader needs to configure the system.

Cheers,

Signature · 1 line
Julien Grall
#33 lore raw

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

Hi Julien,

Quoted 35 lines · Julien Grall

Hi Luca,

Quoted 30 lines · Luca Fancellu

Hi Julien,

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

xen/arch/arm/mpu/mm.c pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr) · line 266

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

This is a bit fragile. I think it would be better if the bitmap is set by head.S as we add the regions. Same for ...

Quoted 5 lines
Quoted 3 lines

So, I was trying to avoid that because in that case we need to place xen_mpumap out of the BSS and start manipulating the bitmap from asm, instead I was hoping to use the C code, I understand that if someone wants to have more than 31 region as boot region this might break, but it’s also a bit unlikely?

The 31 is only part of the problem. The other problem is there is at least one other places in Xen (i.e. as early_fdt_map()) which will also create an entry in the MPU before setup_mm()/setup_mpu() is called. I am a bit concerned that the assumption is going to spread and yet we have no way to programmatically check if this will be broken.

If we would like to find ways, we could check eventually for clashes on enabled MPU regions;
so the only part where a region is created in the C world without the assistance of xen_mpumap is early_fdt_map(), asserts etc could be used in one or both setup_mpu and early_fdt_map to prevent breakage.

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Furthermore, right now, you are hardcoding the slot used and updating the MPU. But if you had the bitmap updated, you could just look up for a free slot.

of course, but still the early DTB map is temporary and it will be gone after boot, so it won’t impact much unless I’m missing something.

It doesn't really matter whether the region is temporary or not. My concern is you are making assumption that are difficult to track (they are not documented where a developper would most likely look at).

So if we still want to hardcode the value, then this should be documented in head.S and probably in a layout.h (or similar) so there is a single place where the MPU layout is described.

Sure, I’m fine with documenting everything, let’s see ...

Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

So I was balancing the pros to manipulate everything from the C world against the cons (boot region > 31). Is it still your preferred way to handle everything from asm?

Yes. I don't think the change in asm will be large and this would allow to remove other assumptions (like in the FDT mapping code).

not large, but still something to be maintained, we will need arm64/arm32 code to set/clear bits on the bitmap
(causing duplication with bitops.c), code to save things on the xen_mpumap, code to clean/invalidate dcache for the entries in xen_mpumap and finally we will need to keep the code aligned to the implementation of the bitmap (which is fairly stable, but still needs to be taken into account).

I understand the changes and risks, but I still think this is the right approach. Let see what the other maintainers think.

what the other maintainers thinks about this one.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

As a side note, I noticed that the MPU entries are not cleared before we enable the MPU. Is there anything in the boot protocol that guarantee all the entries will be invalid? If not, then I think we need to clear the entries.

Otherwise, your current logic doesn't work. That said, I think it would still be necessary even if we get rid of your logic because we don't know the content of the MPU entries.

Quoted 8 lines
Quoted 1 line

The PRLAR.EN bit resets to zero on a warm reset, so any region will be always disabled unless programmed, I thought it is enough.

This is only telling me the state PRLAR.EN when the CPU is initially turn on. This doesn't tell me the value of the bit when jumping in Xen.

I am making the difference because there might be another software running at EL2 before jumping into Xen (e.g. bootloader, or even a previous Xen if we were using Kexec) which could use the MPU.

So I am looking for some details on how the expected state of the system when jumping to an OS/hypervisor. For a comparison, on the MMU side, we have the Linux arm64 Image protocol that will specific how a bootloader needs to configure the system.

Ok I now understand the question, so I think we still could use the Linux arm64 Image protocol, but we will need to define what we expect for the MPU, is docs/misc/arm/booting.txt the right place for it? Shall we start a different thread?

So I could use your help to define it in the best way possible, since unfortunately we don’t have anything available. The only implementation of a bootloader is the boot-wrapper-aarch64 which keeps the MPU off, I/D cache off.

So shall we add in the "Firmware/bootloader requirements” that on Armv8-R we should enter Xen with MPU off and D cache off, Xen shall use spin-table as enable method for the CPUs?

We should clear all the available MPU region before enabling the MPU then.

Please let me know your thoughts.

Cheers,
Luca

#34 lore raw

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

Quoted 52 lines · Luca Fancellu

Hi Julien,

Quoted 35 lines · Julien Grall

Hi Luca,

Quoted 30 lines · Luca Fancellu

Hi Julien,

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 9 lines
(quoted diff) +9 -0
+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.
+     */

Review

Quoted 10 lines
(quoted diff) +10 -0
+    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).
+

This is a bit fragile. I think it would be better if the bitmap is set by head.S as we add the regions. Same for ...

Quoted 5 lines
Quoted 3 lines

So, I was trying to avoid that because in that case we need to place xen_mpumap out of the BSS and start manipulating the bitmap from asm, instead I was hoping to use the C code, I understand that if someone wants to have more than 31 region as boot region this might break, but it’s also a bit unlikely?

The 31 is only part of the problem. The other problem is there is at least one other places in Xen (i.e. as early_fdt_map()) which will also create an entry in the MPU before setup_mm()/setup_mpu() is called. I am a bit concerned that the assumption is going to spread and yet we have no way to programmatically check if this will be broken.

If we would like to find ways, we could check eventually for clashes on enabled MPU regions;
so the only part where a region is created in the C world without the assistance of xen_mpumap is early_fdt_map(), asserts etc could be used in one or both setup_mpu and early_fdt_map to prevent breakage.

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Furthermore, right now, you are hardcoding the slot used and updating the MPU. But if you had the bitmap updated, you could just look up for a free slot.

of course, but still the early DTB map is temporary and it will be gone after boot, so it won’t impact much unless I’m missing something.

It doesn't really matter whether the region is temporary or not. My concern is you are making assumption that are difficult to track (they are not documented where a developper would most likely look at).

So if we still want to hardcode the value, then this should be documented in head.S and probably in a layout.h (or similar) so there is a single place where the MPU layout is described.

Sure, I’m fine with documenting everything, let’s see ...

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

So I was balancing the pros to manipulate everything from the C world against the cons (boot region > 31). Is it still your preferred way to handle everything from asm?

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Yes. I don't think the change in asm will be large and this would allow to remove other assumptions (like in the FDT mapping code).

not large, but still something to be maintained, we will need arm64/arm32 code to set/clear bits on the bitmap
(causing duplication with bitops.c), code to save things on the xen_mpumap, code to clean/invalidate dcache for the entries in xen_mpumap and finally we will need to keep the code aligned to the implementation of the bitmap (which is fairly stable, but still needs to be taken into account).

I understand the changes and risks, but I still think this is the right approach. Let see what the other maintainers think.

what the other maintainers thinks about this one.

I read the thread and my opinion is that we should do that in ASM even it if means more code and possible duplication. From maintainer perspective this is easier to handle than assumptions which are more error prone and difficult to change in the future.

Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 6 lines

As a side note, I noticed that the MPU entries are not cleared before we enable the MPU. Is there anything in the boot protocol that guarantee all the entries will be invalid? If not, then I think we need to clear the entries.

Otherwise, your current logic doesn't work. That said, I think it would still be necessary even if we get rid of your logic because we don't know the content of the MPU entries.

The PRLAR.EN bit resets to zero on a warm reset, so any region will be always disabled unless programmed, I thought it is enough.

This is only telling me the state PRLAR.EN when the CPU is initially turn on. This doesn't tell me the value of the bit when jumping in Xen.

Quoted 7 lines
Quoted 4 lines

I am making the difference because there might be another software running at EL2 before jumping into Xen (e.g. bootloader, or even a previous Xen if we were using Kexec) which could use the MPU.

So I am looking for some details on how the expected state of the system when jumping to an OS/hypervisor. For a comparison, on the MMU side, we have the Linux arm64 Image protocol that will specific how a bootloader needs to configure the system.

Ok I now understand the question, so I think we still could use the Linux arm64 Image protocol, but we will need to define what we expect for the MPU, is docs/misc/arm/booting.txt the right place for it? Shall we start a different thread?

Please start a new thread for documenting booting protocol for ARMv8-R.

~Michal