Skip to contentSkip to conversation

First chunk for Arm R82 and MPU support

PATCH In review Luca Fancellu 3 participants 11 messages May 23, 2025 – May 29, 2025

lore
Share & tools
lore
#0 lore raw

v6 cover First chunk for Arm R82 and MPU support

Hi all,

This is the first chunk of work to support MPU and R82 on Xen, this serie reaches the early boot stages just before early_fdt_map(), just to give an idea about which stage of the boot is reached.

v6:
- general fixes listed on each patch
v5:
- dropped patch that touches page.h, it is not needed
- general fixes listed on each patch
v4:
- dropped setup_mpu() patch and early_fdt_map() patch (needs rework)
- add new patches: boot protocol and early asm MPU structure update
- general fixes listed on each patch
v3 changes:
- stated on each patch
v2 changes for this serie:
- rebased serie on the MPU skeleton that allow compilation
- removed some patches already merged in the MPU skeleton

Luca Fancellu (5):
docs/arm: Document Xen booting protocol on Armv8-R arm/mpu: Provide and populate MPU C data structures arm/mpu: Provide access to the MPU region from the C code arm/mpu: Introduce utility functions for the pr_t type arm/mpu: Provide a constructor for pr_t type

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

 docs/misc/arm/booting.txt                |  11 +-
 xen/arch/arm/arm64/asm-offsets.c         |   7 +
 xen/arch/arm/arm64/cache.S               |  21 +++
 xen/arch/arm/arm64/mpu/head.S            |  25 +++
 xen/arch/arm/include/asm/arm32/mpu.h     |  25 +++
 xen/arch/arm/include/asm/arm64/mpu.h     |  54 ++++++
 xen/arch/arm/include/asm/bitmap-op.inc   |  63 +++++++
 xen/arch/arm/include/asm/mpu.h           |  75 +++++++++
 xen/arch/arm/include/asm/mpu/mm.h        |  41 +++++
 xen/arch/arm/include/asm/mpu/regions.inc |  38 +++++
 xen/arch/arm/mpu/mm.c                    | 205 +++++++++++++++++++++++
 11 files changed, 562 insertions(+), 3 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
 create mode 100644 xen/arch/arm/include/asm/bitmap-op.inc
Signature · 1 line
2.34.1
#1 lore raw

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

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

---
v6 changes:
- Add Julien's Ack-by, add " Cache state shall follow [1], [2] for MPU."
v5 changes:
- restructured and removed some EL3 reference that might not be there on Armv8-R aarch64
- add R-by Ayan and Michal
v4 changes:
- New patch

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

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

In reply to Luca Fancellu

From: Penny Zheng <[email protected]>

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

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

---
 xen/arch/arm/include/asm/arm64/mpu.h | 52 ++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/mpu.h       |  4 +++
 2 files changed, 56 insertions(+)
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
xen/arch/arm/include/asm/arm64/mpu.h new file +52 -0
new file mode 100644
index 000000000000..4737868507d9
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ARM_ARM64_MPU_H
+#define ARM_ARM64_MPU_H
+
+#ifndef __ASSEMBLY__
+
+/* Protection Region Base Address Register */
+typedef union {
+    struct __packed {
+        unsigned long xn_0:1;     /* Execute-Never XN[0] */
+        unsigned long xn:1;       /* Execute-Never XN[1] */
+        unsigned long ap_0:1;     /* Access Permission AP[0] */
+        unsigned long ro:1;       /* Access Permission AP[1] */
+        unsigned long sh:2;       /* Shareability */
+        unsigned long base:42;    /* Base Address */
+        unsigned long res0:16;    /* RES0 */
+    } reg;
+    uint64_t bits;
+} prbar_t;
+
+/* Protection Region Limit Address Register */
+typedef union {
+    struct __packed {
+        unsigned long en:1;     /* Region enable */
+        unsigned long ai:3;     /* Memory Attribute Index */
+        unsigned long ns:1;     /* Not-Secure */
+        unsigned long res0:1;   /* RES0 */
+        unsigned long limit:42; /* Limit Address */
+        unsigned long res1:16;  /* RES0 */
+    } reg;
+    uint64_t bits;
+} prlar_t;
+
+/* MPU Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* ARM_ARM64_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu.h +4 -0
index d4ec4248b62b..bb83f5a5f580 100644
@@ -6,6 +6,10 @@
 #ifndef __ARM_MPU_H__
 #define __ARM_MPU_H__
 
+#if defined(CONFIG_ARM_64)
+# include <asm/arm64/mpu.h>
+#endif
+
 #define MPU_REGION_SHIFT  6
 #define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
 #define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
Signature · 1 line
2.34.1
#3 lore raw

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

In reply to Luca Fancellu

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

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

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

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

---
v6 changes:
- Improved comments on bitmap-op.inc
- invalidate cache for max_mpu_regions, provide a function to invalidate a range, used for xen_mpumap_mask and xen_mpumap
- code style fixes
v5 changes:
- changed variable name from 'max_xen_mpumap' to 'max_mpu_regions'
- code style fixes
- reverted back changes about parameter name of prepare_xen_region
- optimised address computation for xen_mpumap entries
- modified MAX_MPU_REGION_NR
v4 changes:
- new patch

---
 xen/arch/arm/arm64/asm-offsets.c         |  7 +++
 xen/arch/arm/arm64/cache.S               | 21 ++++++++
 xen/arch/arm/arm64/mpu/head.S            | 25 ++++++++++
 xen/arch/arm/include/asm/arm32/mpu.h     | 25 ++++++++++
 xen/arch/arm/include/asm/bitmap-op.inc   | 63 ++++++++++++++++++++++++
 xen/arch/arm/include/asm/mpu.h           |  5 ++
 xen/arch/arm/include/asm/mpu/mm.h        |  7 +++
 xen/arch/arm/include/asm/mpu/regions.inc | 38 ++++++++++++++
 xen/arch/arm/mpu/mm.c                    | 16 ++++++
 9 files changed, 207 insertions(+)
 create mode 100644 xen/arch/arm/include/asm/arm32/mpu.h
 create mode 100644 xen/arch/arm/include/asm/bitmap-op.inc
xen/arch/arm/arm64/asm-offsets.c +7 -0
index 602ffa5b5472..320289b281c5 100644
@@ -10,6 +10,7 @@
 #include <xen/bitops.h>
 #include <public/xen.h>
 #include <asm/current.h>
+#include <asm/mm.h>
 #include <asm/setup.h>
 #include <asm/smccc.h>
 
@@ -68,6 +69,12 @@ void __dummy__(void)
    OFFSET(ARM_SMCCC_1_2_REGS_X14_OFFS, struct arm_smccc_1_2_regs, a14);
    OFFSET(ARM_SMCCC_1_2_REGS_X16_OFFS, struct arm_smccc_1_2_regs, a16);
    BLANK();
+
+#ifdef CONFIG_MPU
+   DEFINE(XEN_MPUMAP_MASK_sizeof, sizeof(xen_mpumap_mask));
+   DEFINE(XEN_MPUMAP_sizeof, sizeof(xen_mpumap));
+   BLANK();
+#endif
 }
 
 /*
xen/arch/arm/arm64/cache.S +21 -0
index c0a8ca163a47..d3c13d98cc12 100644
@@ -50,3 +50,24 @@ FUNC(__flush_dcache_area)
 	dsb	sy
 	ret
 END(__flush_dcache_area)
+
+/*
+ *	__invalidate_dcache_area(addr, size)
+ *
+ *	Ensure that the data held in the cache for the buffer is invalidated.
+ *
+ *	- addr    - start address of the buffer
+ *	- size    - size of the buffer
+ */
+FUNC(__invalidate_dcache_area)
+	dcache_line_size x2, x3
+	add	x1, x0, x1
+	sub	x3, x2, #1
+	bic	x0, x0, x3
+1:	dc	ivac, x0			/* invalidate D line / unified line */
+	add	x0, x0, x2
+	cmp	x0, x1
+	b.lo	1b
+	dsb	sy
+	ret
+END(__invalidate_dcache_area)
xen/arch/arm/arm64/mpu/head.S +25 -0
index 6d336cafbbaf..5df0af571e1c 100644
@@ -40,6 +40,10 @@ FUNC(enable_boot_cpu_mm)
     mrs   x5, MPUIR_EL2
     and   x5, x5, #NUM_MPU_REGIONS_MASK
 
+    ldr   x0, =max_mpu_regions
+    strb  w5, [x0]
+    dc ivac, x0                 /* Invalidate cache for max_mpu_regions addr */
+
     /* x0: region sel */
     mov   x0, xzr
     /* Xen text section. */
@@ -74,6 +78,27 @@ FUNC(enable_boot_cpu_mm)
     prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_DEVICE_PRBAR, attr_prlar=REGION_DEVICE_PRLAR
 #endif
 
+zero_mpu:
+    /* Reset remaining MPU regions */
+    cmp   x0, x5
+    beq   out_zero_mpu
+    mov   x1, #0
+    mov   x2, #1
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prlar=REGION_DISABLED_PRLAR
+    b     zero_mpu
+
+out_zero_mpu:
+    /* Invalidate data cache for MPU data structures */
+    mov x4, lr
+    ldr x0, =xen_mpumap_mask
+    mov x1, #XEN_MPUMAP_MASK_sizeof
+    bl __invalidate_dcache_area
+
+    ldr x0, =xen_mpumap
+    mov x1, #XEN_MPUMAP_sizeof
+    bl __invalidate_dcache_area
+    mov lr, x4
+
     b    enable_mpu
     ret
 END(enable_boot_cpu_mm)
xen/arch/arm/include/asm/arm32/mpu.h new file +25 -0
new file mode 100644
index 000000000000..f0d4d4055cb2
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ARM_ARM32_MPU_H
+#define ARM_ARM32_MPU_H
+
+#ifndef __ASSEMBLY__
+
+/* MPU Protection Region */
+typedef struct {
+    uint32_t prbar;
+    uint32_t prlar;
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* ARM_ARM32_MPU_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/bitmap-op.inc new file +63 -0

63 lines not on this page · raw message

xen/arch/arm/include/asm/mpu.h +5 -0
index bb83f5a5f580..fb93b8b19d24 100644
@@ -8,6 +8,10 @@
 
 #if defined(CONFIG_ARM_64)
 # include <asm/arm64/mpu.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/mpu.h>
+#else
+# error "unknown ARM variant"
 #endif
 
 #define MPU_REGION_SHIFT  6
@@ -17,6 +21,7 @@
 #define NUM_MPU_REGIONS_SHIFT   8
 #define NUM_MPU_REGIONS         (_AC(1, UL) << NUM_MPU_REGIONS_SHIFT)
 #define NUM_MPU_REGIONS_MASK    (NUM_MPU_REGIONS - 1)
+#define MAX_MPU_REGION_NR       NUM_MPU_REGIONS_MASK
 
 #endif /* __ARM_MPU_H__ */
 
xen/arch/arm/include/asm/mpu/mm.h +7 -0
index bfd840fa5d31..409b4dd53dc6 100644
@@ -8,9 +8,16 @@
 #include <xen/page-size.h>
 #include <xen/types.h>
 #include <asm/mm.h>
+#include <asm/mpu.h>
 
 extern struct page_info *frame_table;
 
+extern uint8_t max_mpu_regions;
+
+extern DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR);
+
+extern pr_t xen_mpumap[MAX_MPU_REGION_NR];
+
 #define virt_to_maddr(va) ((paddr_t)((vaddr_t)(va) & PADDR_MASK))
 
 #ifdef CONFIG_ARM_32
xen/arch/arm/include/asm/mpu/regions.inc +38 -0
index 47868a152662..aab2246ee72b 100644
@@ -1,14 +1,34 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <asm/bitmap-op.inc>
 #include <asm/mpu.h>
 #include <asm/sysregs.h>
 
 /* Backgroud region enable/disable */
 #define SCTLR_ELx_BR    BIT(17, UL)
 
+#define REGION_DISABLED_PRLAR   0x00    /* NS=0 ATTR=000 EN=0 */
 #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
 #define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 
+#define PRLAR_ELx_EN            0x1
+
+#ifdef CONFIG_ARM_64
+#define XEN_MPUMAP_ENTRY_SHIFT  0x4     /* 16 byte structure */
+
+.macro store_pair reg1, reg2, dst
+    stp \reg1, \reg2, [\dst]
+.endm
+
+#else
+#define XEN_MPUMAP_ENTRY_SHIFT  0x3     /* 8 byte structure */
+
+.macro store_pair reg1, reg2, dst
+    .word 0xe7f000f0                    /* unimplemented */
+.endm
+
+#endif
+
 /*
  * Macro to prepare and set a EL2 MPU memory region.
  * We will also create an according MPU memory region entry, which
@@ -59,6 +79,24 @@
     dsb   sy
     isb
 
+    /* Load pair into xen_mpumap and invalidate cache */
+    adr_l \base, xen_mpumap
+    add   \base, \base, \sel, LSL #XEN_MPUMAP_ENTRY_SHIFT
+    store_pair \prbar, \prlar, \base
+
+    /* Set/clear xen_mpumap_mask bitmap */
+    tst   \prlar, #PRLAR_ELx_EN
+    bne   2f
+    /* Region is disabled, clear the bit in the bitmap */
+    bitmap_clear_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+    b     3f
+
+2:
+    /* Region is enabled, set the bit in the bitmap */
+    bitmap_set_bit xen_mpumap_mask, \sel, \base, \limit, \prbar, \prlar
+
+3:
+
     add   \sel, \sel, #1
 
 1:
xen/arch/arm/mpu/mm.c +16 -0
index 07c8959f4ee9..2f31b7b78763 100644
@@ -7,9 +7,25 @@
 #include <xen/mm.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
+#include <asm/mpu.h>
 
 struct page_info *frame_table;
 
+/* Maximum number of supported MPU memory regions by the EL2 MPU. */
+uint8_t __ro_after_init max_mpu_regions;
+
+/*
+ * Bitmap xen_mpumap_mask is to record the usage of EL2 MPU memory regions.
+ * Bit 0 represents MPU memory region 0, bit 1 represents MPU memory
+ * region 1, ..., and so on.
+ * If a MPU memory region gets enabled, set the according bit to 1.
+ */
+DECLARE_BITMAP(xen_mpumap_mask, MAX_MPU_REGION_NR) \
+    __cacheline_aligned __section(".data");
+
+/* EL2 Xen MPU memory region mapping table. */
+pr_t __cacheline_aligned __section(".data") xen_mpumap[MAX_MPU_REGION_NR];
+
 static void __init __maybe_unused build_assertions(void)
 {
     /*
Signature · 1 line
2.34.1
#4 lore raw

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

Hi Luca,

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

Quoted 8 lines · Luca Fancellu

/*
* Macro to prepare and set a EL2 MPU memory region.
* We will also create an according MPU memory region entry, which

xen/arch/arm/include/asm/mpu/regions.inc +1 -0
@@ -59,6 +79,24 @@
      dsb   sy
      isb
 
+    /* Load pair into xen_mpumap and invalidate cache */

AFAICT, you don't invalidate the cache below. What did I miss?

The rest of the patch LGTM.

Quoted 8 lines
xen/arch/arm/include/asm/mpu/regions.inc +8 -0
+    adr_l \base, xen_mpumap
+    add   \base, \base, \sel, LSL #XEN_MPUMAP_ENTRY_SHIFT
+    store_pair \prbar, \prlar, \base
+
+    /* Set/clear xen_mpumap_mask bitmap */
+    tst   \prlar, #PRLAR_ELx_EN
+    bne   2f
+    /* Region is disabled, clear the bit in the bitmap */

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

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

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

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

Hi Julien,

Quoted 5 lines · Julien Grall

Hi Luca,

On 23/05/2025 07:54, Luca Fancellu wrote:

Quoted 9 lines

Review line 81

Quoted 7 lines

/*
* Macro to prepare and set a EL2 MPU memory region.
* We will also create an according MPU memory region entry, which

(quoted diff) +0 -0
@@ -59,6 +79,24 @@
     dsb   sy
     isb
 +    /* Load pair into xen_mpumap and invalidate cache */

AFAICT, you don't invalidate the cache below. What did I miss?

oh right I forgot to update this comment, Should I respin the serie or could it be addressed on commit?

I would amend the comment as:
/* Load pair into xen_mpumap */

Cheers,
Luca

Acked-by
#6 lore raw

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

Quoted 12 lines · Luca Fancellu
Quoted 11 lines · Julien Grall

Review line 81

Quoted 7 lines · Luca Fancellu

/*
* Macro to prepare and set a EL2 MPU memory region.
* We will also create an according MPU memory region entry, which

(quoted diff) +0 -0
@@ -59,6 +79,24 @@
      dsb   sy
      isb
  +    /* Load pair into xen_mpumap and invalidate cache */

AFAICT, you don't invalidate the cache below. What did I miss?

Quoted 5 lines

oh right I forgot to update this comment, Should I respin the serie or could it be addressed on commit?

I would amend the comment as:
/* Load pair into xen_mpumap */

It can be done on commit.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

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

In reply to Luca Fancellu

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

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

---
 xen/arch/arm/include/asm/arm64/mpu.h |   2 +
 xen/arch/arm/include/asm/mpu/mm.h    |  24 ++++++
 xen/arch/arm/mpu/mm.c                | 121 +++++++++++++++++++++++++++
 3 files changed, 147 insertions(+)
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
index 4737868507d9..f0ce344e789b 100644
@@ -5,6 +5,8 @@
 
 #ifndef __ASSEMBLY__
 
+#define MPU_REGION_RES0        (0xFFFFULL << 48)
+
 /* Protection Region Base Address Register */
 typedef union {
     struct __packed {
xen/arch/arm/include/asm/mpu/mm.h +24 -0
index 409b4dd53dc6..d7950d9b4fbb 100644
@@ -41,6 +41,30 @@ static inline struct page_info *virt_to_page(const void *v)
     return mfn_to_page(mfn);
 }
 
+/* Utility function to be used whenever MPU regions are modified */
+static inline void context_sync_mpu(void)
+{
+    /*
+     * ARM DDI 0600B.a, C1.7.1
+     * Writes to MPU registers are only guaranteed to be visible following a
+     * Context synchronization event and DSB operation.
+     */
+    dsb(sy);
+    isb();
+}
+
+/*
+ * The following API requires context_sync_mpu() after being used to modify MPU
+ * regions:
+ *  - write_protection_region
+ */
+
+/* Reads the MPU region (into @pr_read) with index @sel from the HW */
+void read_protection_region(pr_t *pr_read, uint8_t sel);
+
+/* Writes the MPU region (from @pr_write) with index @sel to the HW */
+void write_protection_region(const pr_t *pr_write, uint8_t sel);
+
 #endif /* __ARM_MPU_MM_H__ */
 
 /*
xen/arch/arm/mpu/mm.c +121 -0

139 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

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

In reply to Luca Fancellu

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

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

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

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

In reply to Luca Fancellu

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

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

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

80 lines not on this page · raw message

Signature · 1 line
2.34.1