Skip to contentSkip to conversation

Enable early bootup of AArch64 MPU systems

PATCH Superseded Ayan Kumar Halder 4 participants 8 messages Nov 7, 2024 – Nov 18, 2024

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Enable early bootup of AArch64 MPU systems

We have enabled early booting of R82.

Changes from v2 :-
1. Added a new patch "xen/arm: Skip initializing the BSS section when it is empty".
2. Split "xen/arm: mpu: Create boot-time MPU protection regions" into 2 patches.

Changes from v3 :-
1. Removed some of the R-b as the patches have been modified.

Changes from v4 :-
1. First 3 patches have been committed. Last 3 patches have been rebased.

Ayan Kumar Halder (3):
xen/arm: mpu: Create boot-time MPU protection regions xen/arm: mpu: Enable MPU
xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

 xen/arch/Kconfig                     |   2 +
 xen/arch/arm/arch.mk                 |   4 +
 xen/arch/arm/arm64/mpu/Makefile      |   1 +
 xen/arch/arm/arm64/mpu/head.S        | 159 +++++++++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/mpu.h |  25 +++++
 xen/arch/arm/include/asm/mm.h        |   2 +-
 xen/arch/arm/smp.c                   |  11 ++
 xen/arch/arm/xen.lds.S               |   1 +
 8 files changed, 204 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/arm64/mpu/head.S
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH v5 1/3 xen/arm: mpu: Create boot-time MPU protection regions

Define enable_boot_cpu_mm() for the Armv8-R AArch64.

Like boot-time page table in MMU system, we need a boot-time MPU protection region configuration in MPU system so Xen can fetch code and data from normal memory.

To do this, Xen maps the following sections of the binary as separate regions (with permissions) :-
1. Text (Read only at EL2, execution is permitted)
2. RO data (Read only at EL2)
3. RO after init data and RW data (Read/Write at EL2)
4. Init Text (Read only at EL2, execution is permitted)
5. Init data and BSS (Read/Write at EL2)

Before creating a region, we check if the count exceeds the number defined in MPUIR_EL2. If so, then the boot fails.

Also we check if the region is empty or not. IOW, if the start and end address are same, we skip mapping the region.

To map a region, Xen uses the PRBAR_EL2, PRLAR_EL2 and PRSELR_EL2 registers. One can refer to ARM DDI 0600B.a ID062922 G1.3 "General System Control Registers", to get the definitions of these registers. Also, refer to G1.2 "Accessing MPU memory region registers", the following

```
The MPU provides two register interfaces to program the MPU regions:
- Access to any of the MPU regions via PRSELR_ELx, PRBAR<n>_ELx, and
PRLAR<n>_ELx.
```

We use the above mechanism to create the MPU memory regions.

Also, the compiler needs the flag ("-march=armv8-r") in order to build Xen for Armv8-R AArch64 MPU based systems. There will be no need for us to explicitly define MPU specific registers.

---
Changes from :-

v1 - 1. Fix some of the coding style issues.
2. Reword the help message.
3. Updat the commit message.

v2 - Add clarification for the use of page and page size.

v3 - 1. Add a new file arm64/mpu/mm.c to contain the build assertion for page size.
2. Enclosed the check for the start address within "#ifdef CONFIG_MPU".

v4 - 1. Increment the region selector in prepare_xen_region
2. Ensure that the first 8 bits of MPUIR_EL2 are read, to determine the maximum number of supported regions.
3. Remove the inclusion of mm.h. *MPU_REGION* macros have been moved from mm.h to mpu.h. The reason being mm.h cannot be included in an assembly file.
4. Add the build flags for "Armv8-R AArch64 MPU". As a result, we don't need to define MPU registers. So, removed xen/arch/arm/include/asm/arm64/mpu/sysregs.h.

 xen/arch/arm/arch.mk                 |   4 +
 xen/arch/arm/arm64/mpu/Makefile      |   1 +
 xen/arch/arm/arm64/mpu/head.S        | 122 +++++++++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/mpu.h |  25 ++++++
 xen/arch/arm/include/asm/mm.h        |   2 +-
 xen/arch/arm/xen.lds.S               |   1 +
 6 files changed, 154 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/arm64/mpu/head.S
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
xen/arch/arm/arch.mk +4 -0
index 022dcda192..9c4bedfb3b 100644
@@ -9,7 +9,11 @@ CFLAGS-$(CONFIG_ARM_32) += -msoft-float
 CFLAGS-$(CONFIG_ARM_32) += -mcpu=cortex-a15
 CFLAGS-$(CONFIG_ARM_32) += -mno-unaligned-access
 
+ifeq ($(CONFIG_MPU),y)
+CFLAGS-$(CONFIG_ARM_64) += -march=armv8-r
+else
 CFLAGS-$(CONFIG_ARM_64) += -mcpu=generic
+endif
 CFLAGS-$(CONFIG_ARM_64) += -mgeneral-regs-only # No fp registers etc
 $(call cc-option-add,CFLAGS-$(CONFIG_ARM_64),CC,-mno-outline-atomics)
 
xen/arch/arm/arm64/mpu/Makefile +1 -0
index b18cec4836..a8a750a3d0 100644
@@ -1 +1,2 @@
+obj-y += head.o
 obj-y += mm.o
xen/arch/arm/arm64/mpu/head.S new file +122 -0

122 lines not on this page · raw message

xen/arch/arm/include/asm/arm64/mpu.h new file +25 -0
new file mode 100644
index 0000000000..f8a029f1a1
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mpu.h: Arm Memory Protection Unit definitions.
+ */
+
+#ifndef __ARM64_MPU_H__
+#define __ARM64_MPU_H__
+
+#define MPU_REGION_SHIFT  6
+#define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
+#define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
+
+#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)
+#endif /* __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/mm.h +1 -1
index 5abd4b0d1c..59b774b7b8 100644
@@ -16,7 +16,7 @@
 
 #if defined(CONFIG_MMU)
 # include <asm/mmu/mm.h>
-#else
+#elif !defined(CONFIG_MPU)
 # error "Unknown memory management layout"
 #endif
 
xen/arch/arm/xen.lds.S +1 -0
index d1e579e8a8..bbccff1a03 100644
@@ -147,6 +147,7 @@ SECTIONS
        *(.altinstr_replacement)
   } :text
   . = ALIGN(PAGE_SIZE);
+  __init_data_begin = .;
   .init.data : {
        *(.init.rodata)
        *(.init.rodata.*)
Signature · 1 line
2.25.1
Reviewed-by on v5 1/3
#2 lore raw

Re: [PATCH v5 1/3] xen/arm: mpu: Create boot-time MPU protection regions

Hi Ayan,

On 7 Nov 2024, at 15:03, Ayan Kumar Halder <[email protected]> wrote:

Define enable_boot_cpu_mm() for the Armv8-R AArch64.

Like boot-time page table in MMU system, we need a boot-time MPU protection region configuration in MPU system so Xen can fetch code and data from normal memory.

To do this, Xen maps the following sections of the binary as separate regions (with permissions) :-
1. Text (Read only at EL2, execution is permitted)
2. RO data (Read only at EL2)
3. RO after init data and RW data (Read/Write at EL2)
4. Init Text (Read only at EL2, execution is permitted)
5. Init data and BSS (Read/Write at EL2)

Before creating a region, we check if the count exceeds the number defined in MPUIR_EL2. If so, then the boot fails.

Also we check if the region is empty or not. IOW, if the start and end address are same, we skip mapping the region.

To map a region, Xen uses the PRBAR_EL2, PRLAR_EL2 and PRSELR_EL2 registers. One can refer to ARM DDI 0600B.a ID062922 G1.3 "General System Control Registers", to get the definitions of these registers. Also, refer to G1.2 "Accessing MPU memory region registers", the following

```
The MPU provides two register interfaces to program the MPU regions:
- Access to any of the MPU regions via PRSELR_ELx, PRBAR<n>_ELx, and
PRLAR<n>_ELx.
```

We use the above mechanism to create the MPU memory regions.

Also, the compiler needs the flag ("-march=armv8-r") in order to build Xen for Armv8-R AArch64 MPU based systems. There will be no need for us to explicitly define MPU specific registers.

---

Changes looks ok to me and I’ve also built and tested, maybe one NIT below

xen/arch/arm/include/asm/mm.h +0 -0
index 5abd4b0d1c..59b774b7b8 100644
@@ -16,7 +16,7 @@
 

#if defined(CONFIG_MMU)

# include <asm/mmu/mm.h>
-#else
+#elif !defined(CONFIG_MPU)
# error "Unknown memory management layout"
#endif

^— maybe this change is not needed at this stage, it will be soon though

Anyway:

Cheers,
Luca

#3 lore raw

Re: [PATCH v5 1/3] xen/arm: mpu: Create boot-time MPU protection regions

In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 7 lines · Ayan Kumar Halder

+/*
+ * Macro to prepare and set a EL2 MPU memory region.
+ * We will also create an according MPU memory region entry, which
+ * is a structure of pr_t, in table \prmap.
+ *
+ * Inputs:
+ * sel: region selector

``sel`` is now also an output. But looking at the description, we don't seem to say which registers are preserved or not. For instance, both ..

Quoted 1 line

+ * base: reg storing base address (should be page-aligned) > + * limit: reg storing limit address

... the two registers above will be clobbered. On the other hand...

Quoted 3 lines
+ * prbar:       store computed PRBAR_EL2 value
+ * prlar:       store computed PRLAR_EL2 value
+ * maxcount:    maximum number of EL2 regions supported

... this will be preserved. So I think we need to clarify it. For instance, this is the description of create_table_entry_from_paddr:

/*
* Macro to create a page table entry in \ptbl to \tbl

 * ptbl:    table symbol where the entry will be created
 * tbl:     physical address of the table to point to
 * virt:    virtual address
 * lvl:     page-table level
 * tmp1:    scratch register
 * tmp2:    scratch register

*
* Preserves \virt
* Clobbers \tbl, \tmp1, \tmp2
*
* Note that all parameters using registers should be distinct. */

Signature · 1 line
Julien Grall
#4 lore raw

Re: [PATCH v5 1/3] xen/arm: mpu: Create boot-time MPU protection regions

Quoted 1 line · Julien Grall

Hi Ayan,

Hi Julien,

Quoted 8 lines · Ayan Kumar Halder
Quoted 6 lines

+/*
+ * Macro to prepare and set a EL2 MPU memory region.
+ * We will also create an according MPU memory region entry, which
+ * is a structure of pr_t,  in table \prmap.
+ *
+ * Inputs:

This needs to be removed as some parameters are both input and output (eg sel) and some parameters are for temporary storage (eg prbar, prlar).

Quoted 7 lines
Quoted 1 line

+ * sel:         region selector

``sel`` is now also an output. But looking at the description, we don't seem to say which registers are preserved or not. For instance, both ..

Quoted 2 lines

+ * base:        reg storing base address (should be page-aligned) >
+ * limit:       reg storing limit address

Quoted 7 lines

... the two registers above will be clobbered. On the other hand...

Quoted 3 lines

+ * prbar:       store computed PRBAR_EL2 value
+ * prlar:       store computed PRLAR_EL2 value
+ * maxcount:    maximum number of EL2 regions supported

... this will be preserved. So I think we need to clarify it. For instance, this is the description of create_table_entry_from_paddr:

Actually prbar, prlar are similar to tmp1, tmp2 (ie scratch registers). I used the names so as to make it more meaningful.

Quoted 16 lines

/*
 * Macro to create a page table entry in \ptbl to \tbl
 * ptbl:    table symbol where the entry will be created
 * tbl:     physical address of the table to point to
 * virt:    virtual address
 * lvl:     page-table level
 * tmp1:    scratch register
 * tmp2:    scratch register
 *
 * Preserves \virt
 * Clobbers \tbl, \tmp1, \tmp2
 *
 * Note that all parameters using registers should be distinct. */

In our case, it will be

/*
 * Macro to prepare and set a EL2 MPU memory region.
 * We will also create an according MPU memory region entry, which
 * is a structure of pr_t,  in table \prmap.
 *
 * sel:         region selector
 * base:        reg storing base address
 * limit:       reg storing limit address
 * prbar:       store computed PRBAR_EL2 value
 * prlar:       store computed PRLAR_EL2 value
 * maxcount:    maximum number of EL2 regions supported
 * attr_prbar:  PRBAR_EL2-related memory attributes. If not specified it will be
 * REGION_DATA_PRBAR
 * attr_prlar:  PRLAR_EL2-related memory attributes. If not specified it will be *              REGION_NORMAL_PRLAR
 *
 * Preserves \maxcount
 * Clobbers \sel, \base, \limit, \prbar, \prlar
 *
 * Note that all parameters using registers should be distinct.
 */

- Ayan

#5 lore raw

PATCH v5 2/3 xen/arm: mpu: Enable MPU

In reply to Ayan Kumar Halder

After the regions have been created, now we enable the MPU. For this we disable the background region so that the new memory map created for the regions take effect. Also, we treat all RW regions as non executable and the data cache is enabled.

---
Changes from :-

v2 - 1. Extracted from the previous patch into a new one.

2. Disabled background region.

v3 - 1. Removed dsb before setting SCTLR_EL2. The reason being
From ARM DDI 0487K.a D23-7349:
"Direct writes to these registers (includes SCTLR_EL2) are not allowed to affect any instructions appearing in program order before the direct write." So, we don't need a synchronization barrier before writing to SCTLR_EL2. Further, we do have synchronization barriers after writing the MPU region registers (which happens before we read SCTLR_EL2). So, SCTLR_EL2 is written after the MPU registers are synchronized. And, thus adding a 'isb' to flush the instruction pipeline ensures that the subsequent instructions are fetched after the MPU has been enabled.

2. Saved and restored lr in enable_boot_cpu_mm().

v4 - 1. Moved the definition of SCTLR_ELx_BR from sysregs.h from head.S. The reason being sysregs.h does not exist any longer (refer to previous patch for details) and SCTLR_ELx_BR is used in head.S only.
(I have preserved the R-b abd A-b, let me know if that is ok).

 xen/arch/arm/arm64/mpu/head.S | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
xen/arch/arm/arm64/mpu/head.S +27 -0
index 37e4b455bb..a449aeca67 100644
@@ -5,6 +5,9 @@
 
 #include <asm/arm64/mpu.h>
 
+/* Backgroud region enable/disable */
+#define SCTLR_ELx_BR    BIT(17, UL)
+
 #define REGION_TEXT_PRBAR       0x38    /* SH=11 AP=10 XN=00 */
 #define REGION_RO_PRBAR         0x3A    /* SH=11 AP=10 XN=10 */
 #define REGION_DATA_PRBAR       0x32    /* SH=11 AP=00 XN=10 */
@@ -69,6 +72,29 @@ FUNC_LOCAL(fail_insufficient_regions)
     b   1b
 END(fail_insufficient_regions)
 
+/*
+ * Enable EL2 MPU and data cache
+ * If the Background region is enabled, then the MPU uses the default memory
+ * map as the Background region for generating the memory
+ * attributes when MPU is disabled.
+ * Since the default memory map of the Armv8-R AArch64 architecture is
+ * IMPLEMENTATION DEFINED, we intend to turn off the Background region here.
+ *
+ * Clobbers x0
+ *
+ */
+FUNC_LOCAL(enable_mpu)
+    mrs   x0, SCTLR_EL2
+    bic   x0, x0, #SCTLR_ELx_BR       /* Disable Background region */
+    orr   x0, x0, #SCTLR_Axx_ELx_M    /* Enable MPU */
+    orr   x0, x0, #SCTLR_Axx_ELx_C    /* Enable D-cache */
+    orr   x0, x0, #SCTLR_Axx_ELx_WXN  /* Enable WXN */
+    msr   SCTLR_EL2, x0
+    isb
+
+    ret
+END(enable_mpu)
+
 /*
  * Maps the various sections of Xen (described in xen.lds.S) as different MPU
  * regions.
@@ -111,6 +137,7 @@ FUNC(enable_boot_cpu_mm)
     ldr   x2, =__bss_end
     prepare_xen_region x0, x1, x2, x3, x4, x5
 
+    b    enable_mpu
     ret
 END(enable_boot_cpu_mm)
 
Signature · 1 line
2.25.1
#6 lore raw

PATCH v5 3/3 xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

In reply to Ayan Kumar Halder

Secondary cpus initialization is not yet supported. Thus, we print an appropriate message and put the secondary cpus in WFE state. And we introduce to BUILD_BUG_ON to prevent users using from building Xen on multiprocessor based MPU systems.

In Arm, there is no clean way to disable SMP. As of now, we wish to support MPU on UNP only. So, we have defined the default range of NR_CPUs to be 1 for MPU.

---
Changes from :-

v1 - 1. NR_CPUS is defined as 1 for MPU

2. Added a message in enable_secondary_cpu_mm()

v2 - 1. Added the range

2. Clarified in the commit message why/how we have disabled SMP.

v3 - 1. BUILD_BUG_ON() is moved to smp.c.

v4 - 1. Moved "default "1" if ARM && MPU” right after “default "256" if X86”.

 xen/arch/Kconfig              |  2 ++
 xen/arch/arm/arm64/mpu/head.S | 10 ++++++++++
 xen/arch/arm/smp.c            | 11 +++++++++++
 3 files changed, 23 insertions(+)
xen/arch/Kconfig +2 -0
index 308ce129a8..9f4835e37f 100644
@@ -6,8 +6,10 @@ config PHYS_ADDR_T_32
 
 config NR_CPUS
 	int "Maximum number of CPUs"
+	range 1 1 if ARM && MPU
 	range 1 16383
 	default "256" if X86
+	default "1" if ARM && MPU
 	default "8" if ARM && RCAR3
 	default "4" if ARM && QEMU
 	default "4" if ARM && MPSOC
xen/arch/arm/arm64/mpu/head.S +10 -0
index a449aeca67..731698aa3b 100644
@@ -141,6 +141,16 @@ FUNC(enable_boot_cpu_mm)
     ret
 END(enable_boot_cpu_mm)
 
+/*
+ * We don't yet support secondary CPUs bring-up. Implement a dummy helper to
+ * please the common code.
+ */
+ENTRY(enable_secondary_cpu_mm)
+    PRINT("- SMP not enabled yet -\r\n")
+1:  wfe
+    b 1b
+ENDPROC(enable_secondary_cpu_mm)
+
 /*
  * Local variables:
  * mode: ASM
xen/arch/arm/smp.c +11 -0
index c11bba93ad..b372472188 100644
@@ -1,4 +1,5 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
+#include <xen/init.h>
 #include <xen/mm.h>
 #include <asm/system.h>
 #include <asm/smp.h>
@@ -6,6 +7,16 @@
 #include <asm/gic.h>
 #include <asm/flushtlb.h>
 
+static void __init __maybe_unused build_assertions(void)
+{
+#ifdef CONFIG_MPU
+    /*
+     * Currently, SMP is not enabled on MPU based systems.
+     */
+    BUILD_BUG_ON(NR_CPUS > 1);
+#endif
+}
+
 void arch_flush_tlb_mask(const cpumask_t *mask)
 {
     /* No need to IPI other processors on ARM, the processor takes care of it. */
Signature · 1 line
2.25.1
Acked-by on v5 3/3
#7 lore raw

Re: [PATCH v5 3/3] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Hi Ayan,

Quoted 11 lines · Ayan Kumar Halder

Secondary cpus initialization is not yet supported. Thus, we print an appropriate message and put the secondary cpus in WFE state. And we introduce to BUILD_BUG_ON to prevent users using from building Xen on multiprocessor based MPU systems.

In Arm, there is no clean way to disable SMP. As of now, we wish to support MPU on UNP only. So, we have defined the default range of NR_CPUs to be 1 for MPU.

Cheers,

Signature · 1 line
Julien Grall