Skip to contentSkip to conversation

Enable early bootup of AArch64 MPU systems

PATCH Superseded Ayan Kumar Halder 8 participants 37 messages Oct 28, 2024 – Nov 1, 2024

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

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

Ayan Kumar Halder (6):
xen/arm: Skip initializing the BSS section when it is empty xen/arm: mpu: Introduce choice between MMU and MPU xen/arm: mpu: Define Xen start address for MPU systems xen/arm: mpu: Create boot-time MPU protection regions xen/arm: mpu: Enable MPU
xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

 CHANGELOG.md                                 |   2 +
 SUPPORT.md                                   |   1 +
 xen/arch/Kconfig                             |   2 +
 xen/arch/arm/Kconfig                         |  27 +++-
 xen/arch/arm/arm32/head.S                    |   3 +
 xen/arch/arm/arm64/Makefile                  |   1 +
 xen/arch/arm/arm64/head.S                    |   2 +
 xen/arch/arm/arm64/mpu/Makefile              |   2 +
 xen/arch/arm/arm64/mpu/head.S                | 158 +++++++++++++++++++
 xen/arch/arm/arm64/mpu/mm.c                  |  15 ++
 xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  30 ++++
 xen/arch/arm/include/asm/config.h            |   4 +-
 xen/arch/arm/include/asm/mm.h                |   2 +
 xen/arch/arm/include/asm/mpu/arm64/mm.h      |  22 +++
 xen/arch/arm/include/asm/mpu/layout.h        |  33 ++++
 xen/arch/arm/include/asm/mpu/mm.h            |  20 +++
 xen/arch/arm/platforms/Kconfig               |   2 +-
 xen/arch/arm/smp.c                           |  11 ++
 xen/arch/arm/xen.lds.S                       |   8 +
 19 files changed, 342 insertions(+), 3 deletions(-)
 create mode 100644 xen/arch/arm/arm64/mpu/Makefile
 create mode 100644 xen/arch/arm/arm64/mpu/head.S
 create mode 100644 xen/arch/arm/arm64/mpu/mm.c
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu/sysregs.h
 create mode 100644 xen/arch/arm/include/asm/mpu/arm64/mm.h
 create mode 100644 xen/arch/arm/include/asm/mpu/layout.h
 create mode 100644 xen/arch/arm/include/asm/mpu/mm.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH v4 1/6 xen/arm: Skip initializing the BSS section when it is empty

If the BSS section is empty, then the function should return.
If one does not check whether the BSS section is empty or not, then there is a risk of writing 0s outside of BSS section (which may contain critical data).

Fixes dac84b66cc9a ("xen: arm64: initial build + config changes, start of day code")

---
Changes from :-

v1..v2 - New patch introduced in v3.

v3 - 1. Update the check in arm32 as well.
2. Drop the R-bs.

 xen/arch/arm/arm32/head.S | 3 +++
 xen/arch/arm/arm64/head.S | 2 ++
 2 files changed, 5 insertions(+)
xen/arch/arm/arm32/head.S +3 -0
index a96d5d3503..4ff5c220bc 100644
@@ -185,12 +185,15 @@ zero_bss:
         PRINT("- Zero BSS -\r\n")
         mov_w r0, __bss_start        /* r0 := vaddr(__bss_start) */
         mov_w r1, __bss_end          /* r1 := vaddr(__bss_end)   */
+        cmp   r1, r0
+        beq   skip_bss
 
         mov   r2, #0
 1:      str   r2, [r0], #4
         cmp   r0, r1
         blo   1b
 
+skip_bss:
         mov   pc, lr
 ENDPROC(zero_bss)
 
xen/arch/arm/arm64/head.S +2 -0
index 14c3720d80..72c7b24498 100644
@@ -346,6 +346,8 @@ FUNC_LOCAL(zero_bss)
         PRINT("- Zero BSS -\r\n")
         ldr   x0, =__bss_start       /* x0 := vaddr(__bss_start) */
         ldr   x1, =__bss_end         /* x1 := vaddr(__bss_end)   */
+        cmp   x1, x0
+        beq   skip_bss
 
 1:      str   xzr, [x0], #8
         cmp   x0, x1
Signature · 1 line
2.25.1
Reviewed-by on v4 1/6
#2 lore raw

Re: [PATCH v4 1/6] xen/arm: Skip initializing the BSS section when it is empty

Hi Ayan,

Quoted 9 lines · Ayan Kumar Halder

If the BSS section is empty, then the function should return.
If one does not check whether the BSS section is empty or not, then there is a risk of writing 0s outside of BSS section (which may contain critical data).

Fixes dac84b66cc9a ("xen: arm64: initial build + config changes, start of day code")

—

Looks good to me

Reviewed-by on v4 1/6
#3 lore raw

Re: [PATCH v4 1/6] xen/arm: Skip initializing the BSS section when it is empty

In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 6 lines · Ayan Kumar Halder

If the BSS section is empty, then the function should return.
If one does not check whether the BSS section is empty or not, then there is a risk of writing 0s outside of BSS section (which may contain critical data).

Fixes dac84b66cc9a ("xen: arm64: initial build + config changes, start of day code")

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

PATCH v4 2/6 xen/arm: mpu: Introduce choice between MMU and MPU

In reply to Ayan Kumar Halder

There are features in the forthcoming patches which are dependent on MPU. For eg fixed start address.
Also, some of the Xen features (eg STATIC_MEMORY) will be selected by the MPU configuration.

Thus, this patch introduces a choice between MMU and MPU for the type of memory management system. By default, MMU is selected. MPU is now gated by UNSUPPORTED.

Update SUPPORT.md to state that the support for MPU is experimental. Also updated CHANGELOG.md as well.

---

Changes from :-

v1 - 1. Reword the help messages.
2. Update Support.md.

v2 - 1. Reword the help message.

v3 - 1. Update Changelog.
2. Add R-b and Ack.

 CHANGELOG.md                   |  2 ++
 SUPPORT.md                     |  1 +
 xen/arch/arm/Kconfig           | 17 ++++++++++++++++-
 xen/arch/arm/platforms/Kconfig |  2 +-
 4 files changed, 20 insertions(+), 2 deletions(-)
CHANGELOG.md +2 -0
index c499d12dc4..79524cc15f 100644
@@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - Prefer ACPI reboot over UEFI ResetSystem() run time service call.
 
 ### Added
+ - On Arm:
+   - Support for earlyboot of Xen on Armv8-R (experimental).
 
 ### Removed
  - On x86:
SUPPORT.md +1 -0
index 23dd7e6424..94610d3c91 100644
@@ -40,6 +40,7 @@ supported in this document.
 
     Status, Xen in AArch64 mode: Supported
     Status, Xen in AArch32 mode: Tech Preview
+    Status, Xen in Armv8-R: Experimental
     Status, Cortex A57 r0p0-r1p1: Supported, not security supported
     Status, Cortex A77 r0p0-r1p0: Supported, not security supported
 
xen/arch/arm/Kconfig +16 -1
index 323c967361..ed92eb67cb 100644
@@ -58,10 +58,25 @@ config PADDR_BITS
 	default 40 if ARM_PA_BITS_40
 	default 48 if ARM_64
 
+choice
+	prompt "Memory management system"
+	default MMU
+	help
+	  User can choose between the different forms of memory management system.
+
 config MMU
-	def_bool y
+	bool "MMU"
 	select HAS_PMAP
 	select HAS_VMAP
+	help
+	  Select it if you plan to run Xen on A-profile Armv7+
+
+config MPU
+	bool "MPU" if UNSUPPORTED
+	help
+	  Memory Protection Unit (MPU). Select if you plan to run Xen on ARMv8-R
+	  systems supporting EL2. (UNSUPPORTED)
+endchoice
 
 source "arch/Kconfig"
 
xen/arch/arm/platforms/Kconfig +1 -1
index 76f7e76b1b..02322c259c 100644
@@ -1,5 +1,5 @@
 choice
-	prompt "Platform Support"
+	prompt "Platform Support" if MMU
 	default ALL_PLAT
 	help
 	Choose which hardware platform to enable in Xen.
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v4 2/6] xen/arm: mpu: Introduce choice between MMU and MPU

CHANGELOG.md The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) · line 16

Quoted 10 lines · Ayan Kumar Halder
CHANGELOG.md +2 -0
index c499d12dc4..79524cc15f 100644
@@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - Prefer ACPI reboot over UEFI ResetSystem() run time service call.
 
 ### Added
+ - On Arm:
+   - Support for earlyboot of Xen on Armv8-R (experimental).

Simply "Experimental support for Armv8-R".

The rest of that sentence is going to go stale quite quickly.

~Andrew

#6 lore raw

Re: [PATCH v4 2/6] xen/arm: mpu: Introduce choice between MMU and MPU

Quoted 16 lines · Andrew Cooper
Quoted 6 lines · Ayan Kumar Halder
CHANGELOG.md +0 -0
index c499d12dc4..79524cc15f 100644
@@ -12,6 +12,8 @@ The format is based on [Keep a
 Changelog](https://keepachangelog.com/en/1.0.0/)
Quoted 6 lines

    - Prefer ACPI reboot over UEFI ResetSystem() run time service call.

 ### Added
+ - On Arm:
+   - Support for earlyboot of Xen on Armv8-R (experimental).

Simply "Experimental support for Armv8-R".

Quoted 1 line

The rest of that sentence is going to go stale quite quickly.

With suggested by Andrew: Acked-by: Oleksii Kurochko <[email protected]>

Thanks.

~ Oleksii

#7 lore raw

PATCH v4 3/6 xen/arm: mpu: Define Xen start address for MPU systems

In reply to Ayan Kumar Halder

From: Wei Chen <[email protected]>

On Armv8-A, Xen has a fixed virtual start address (link address too) for all Armv8-A platforms. In an MMU based system, Xen can map its loaded address to this virtual start address. So, on Armv8-A platforms, the Xen start address does not need to be configurable. But on Armv8-R platforms, there is no MMU to map loaded address to a fixed virtual address and different platforms will have very different address space layout. So Xen cannot use a fixed physical address on MPU based system and need to have it configurable.

So, we introduce a Kconfig option for users to set the start address. The start address needs to be aligned to 4KB. We have a check for this alignment.

MPU allows us to define regions which are 64 bits aligned. This restriction comes from the bitfields of PRBAR, PRLAR (the lower 6 bits are 0 extended to provide the base and limit address of a region). This means that the start address of Xen needs to be at least 64 bits aligned (as it will correspond to the start address of memory protection region).

As for now Xen on MPU tries to use the same memory alignment restrictions as it has been for MMU. We have added a build assertion to ensure that the page size is 4KB. Unlike MMU where the starting virtual address is 2MB, Xen on MPU needs the start address to be 4KB (ie page size) aligned.

In case if the user forgets to set the start address, then 0xffffffff is used as default. This is to trigger the error (on alignment check) and thereby prompt user to set the start address.

Also updated config.h so that it includes mpu/layout.h when CONFIG_MPU is defined.

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

 xen/arch/arm/Kconfig                  | 10 ++++++++
 xen/arch/arm/arm64/Makefile           |  1 +
 xen/arch/arm/arm64/mpu/Makefile       |  1 +
 xen/arch/arm/arm64/mpu/mm.c           | 15 ++++++++++++
 xen/arch/arm/include/asm/config.h     |  4 +++-
 xen/arch/arm/include/asm/mpu/layout.h | 33 +++++++++++++++++++++++++++
 xen/arch/arm/xen.lds.S                |  7 ++++++
 7 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/arm64/mpu/Makefile
 create mode 100644 xen/arch/arm/arm64/mpu/mm.c
 create mode 100644 xen/arch/arm/include/asm/mpu/layout.h
xen/arch/arm/Kconfig +10 -0
index ed92eb67cb..15b2e4a227 100644
@@ -23,6 +23,16 @@ config ARCH_DEFCONFIG
 	default "arch/arm/configs/arm32_defconfig" if ARM_32
 	default "arch/arm/configs/arm64_defconfig" if ARM_64
 
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined address"
+	default 0xFFFFFFFF
+	depends on MPU
+	help
+	  Used to set customized address at which which Xen will be linked on MPU
+	  systems. Must be aligned to 4KB.
+	  0xFFFFFFFF is used as default value to indicate that user has not
+	  customized this address.
+
 menu "Architecture Features"
 
 choice
xen/arch/arm/arm64/Makefile +1 -0
index 72161ff22e..6491c5350b 100644
@@ -1,5 +1,6 @@
 obj-y += lib/
 obj-$(CONFIG_MMU) += mmu/
+obj-$(CONFIG_MPU) += mpu/
 
 obj-y += cache.o
 obj-y += cpufeature.o
xen/arch/arm/arm64/mpu/Makefile new file +1 -0
new file mode 100644
index 0000000000..b18cec4836
@@ -0,0 +1 @@
+obj-y += mm.o
xen/arch/arm/arm64/mpu/mm.c new file +15 -0
new file mode 100644
index 0000000000..0b8748e575
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/sizes.h>
+
+static void __init __maybe_unused build_assertions(void)
+{
+    /*
+     * Unlike MMU, MPU does not use pages for translation. However, we continue
+     * to use PAGE_SIZE to denote 4KB. This is so that the existing memory
+     * management based on pages, continue to work for now.
+     */
+    BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
+}
xen/arch/arm/include/asm/config.h +3 -1
index a2e22b659d..0a51142efd 100644
@@ -69,8 +69,10 @@
 #include <xen/const.h>
 #include <xen/page-size.h>
 
-#ifdef CONFIG_MMU
+#if defined(CONFIG_MMU)
 #include <asm/mmu/layout.h>
+#elif defined(CONFIG_MPU)
+#include <asm/mpu/layout.h>
 #else
 # error "Unknown memory management layout"
 #endif
xen/arch/arm/include/asm/mpu/layout.h new file +33 -0
new file mode 100644
index 0000000000..d6d397f4c2
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_MPU_LAYOUT_H__
+#define __ARM_MPU_LAYOUT_H__
+
+#define XEN_START_ADDRESS CONFIG_XEN_START_ADDRESS
+
+/*
+ * All MPU platforms need to provide a XEN_START_ADDRESS for linker. This
+ * address indicates where Xen image will be loaded and run from. This
+ * address must be aligned to a PAGE_SIZE.
+ */
+#if (XEN_START_ADDRESS % PAGE_SIZE) != 0
+#error "XEN_START_ADDRESS must be aligned to 4KB"
+#endif
+
+/*
+ * For MPU, XEN's virtual start address is same as the physical address.
+ * The reason being MPU treats VA == PA. IOW, it cannot map the physical
+ * address to a different fixed virtual address. So, the virtual start
+ * address is determined by the physical address at which Xen is loaded.
+ */
+#define XEN_VIRT_START         _AT(paddr_t, XEN_START_ADDRESS)
+
+#endif /* __ARM_MPU_LAYOUT_H__ */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/xen.lds.S +7 -0
index 5b9abc9a2d..d1e579e8a8 100644
@@ -213,6 +213,13 @@ SECTIONS
  * match the context.
  */
 ASSERT(_start == XEN_VIRT_START, "_start != XEN_VIRT_START")
+#ifdef CONFIG_MPU
+/*
+ * On MPU based platforms, the starting address is to be provided by user.
+ * One need to check that it is 4KB aligned.
+ */
+ASSERT(IS_ALIGNED(_start,       4096), "starting address should be aligned to 4KB")
+#endif
 
 /*
  * We require that Xen is loaded at a page boundary, so this ensures that any
Signature · 1 line
2.25.1
Reviewed-by on v4 3/6
#8 lore raw

Re: [PATCH v4 3/6] xen/arm: mpu: Define Xen start address for MPU systems

Hi Ayan,

Quoted 30 lines · Ayan Kumar Halder

From: Wei Chen <[email protected]>

On Armv8-A, Xen has a fixed virtual start address (link address too) for all Armv8-A platforms. In an MMU based system, Xen can map its loaded address to this virtual start address. So, on Armv8-A platforms, the Xen start address does not need to be configurable. But on Armv8-R platforms, there is no MMU to map loaded address to a fixed virtual address and different platforms will have very different address space layout. So Xen cannot use a fixed physical address on MPU based system and need to have it configurable.

So, we introduce a Kconfig option for users to set the start address. The start address needs to be aligned to 4KB. We have a check for this alignment.

MPU allows us to define regions which are 64 bits aligned. This restriction comes from the bitfields of PRBAR, PRLAR (the lower 6 bits are 0 extended to provide the base and limit address of a region). This means that the start address of Xen needs to be at least 64 bits aligned (as it will correspond to the start address of memory protection region).

As for now Xen on MPU tries to use the same memory alignment restrictions as it has been for MMU. We have added a build assertion to ensure that the page size is 4KB. Unlike MMU where the starting virtual address is 2MB, Xen on MPU needs the start address to be 4KB (ie page size) aligned.

In case if the user forgets to set the start address, then 0xffffffff is used as default. This is to trigger the error (on alignment check) and thereby prompt user to set the start address.

Quoted 7 lines

Also updated config.h so that it includes mpu/layout.h when CONFIG_MPU is defined.

—

Looks good to me

Reviewed-by on v4 3/6
#9 lore raw

Re: [PATCH v4 3/6] xen/arm: mpu: Define Xen start address for MPU systems

In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 28 lines · Ayan Kumar Halder

From: Wei Chen <[email protected]>

On Armv8-A, Xen has a fixed virtual start address (link address too) for all Armv8-A platforms. In an MMU based system, Xen can map its loaded address to this virtual start address. So, on Armv8-A platforms, the Xen start address does not need to be configurable. But on Armv8-R platforms, there is no MMU to map loaded address to a fixed virtual address and different platforms will have very different address space layout. So Xen cannot use a fixed physical address on MPU based system and need to have it configurable.

So, we introduce a Kconfig option for users to set the start address. The start address needs to be aligned to 4KB. We have a check for this alignment.

MPU allows us to define regions which are 64 bits aligned. This restriction comes from the bitfields of PRBAR, PRLAR (the lower 6 bits are 0 extended to provide the base and limit address of a region). This means that the start address of Xen needs to be at least 64 bits aligned (as it will correspond to the start address of memory protection region).

As for now Xen on MPU tries to use the same memory alignment restrictions as it has been for MMU. We have added a build assertion to ensure that the page size is 4KB. Unlike MMU where the starting virtual address is 2MB, Xen on MPU needs the start address to be 4KB (ie page size) aligned.

In case if the user forgets to set the start address, then 0xffffffff is used as default. This is to trigger the error (on alignment check) and thereby prompt user to set the start address.

Quoted 6 lines

Also updated config.h so that it includes mpu/layout.h when CONFIG_MPU is defined.

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

PATCH v4 4/6 xen/arm: mpu: Create boot-time MPU protection regions

In reply to Ayan Kumar Halder

Define enable_boot_cpu_mm() for the AArch64-V8R system.

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.

MPU specific registers are defined in
xen/arch/arm/include/asm/arm64/mpu/sysregs.h.

---
Changes from :-

v1 - 1. Instead of mapping a (XEN_START_ADDRESS + 2MB) as a single MPU region, we have separate MPU regions for different parts of the Xen binary. The reason being different regions will nned different permissions (as mentioned in the linker script).

2. Introduced a label (__init_data_begin) to mark the beginning of the init data section.

3. Moved MPU specific register definitions to mpu/sysregs.h.

4. Fixed coding style issues.

5. Included page.h in mpu/head.S as page.h includes sysregs.h. I haven't seen sysregs.h included directly from head.S or mmu/head.S. (Outstanding comment not addressed).

v2 - 1. Extracted "enable_mpu()" in a separate patch.

2. Removed alignment for limit address.

3. Merged some of the sections for preparing the early boot regions.

4. Checked for the max limit of MPU regions before creating a new region.

5. Checked for empty regions.

v3 :- 1. Modified prepare_xen_region() so that we check for empty region within this. Also, index of regions (to be programmed in PRSELR_EL2) should start from 0.

2. Removed load_paddr() as the offset is 0.

3. Introduced fail_insufficient_regions() to handle failure caused when the number of regions to be allocated is not sufficient.

 xen/arch/arm/arm64/mpu/Makefile              |   1 +
 xen/arch/arm/arm64/mpu/head.S                | 122 +++++++++++++++++++
 xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  27 ++++
 xen/arch/arm/include/asm/mm.h                |   2 +
 xen/arch/arm/include/asm/mpu/arm64/mm.h      |  22 ++++
 xen/arch/arm/include/asm/mpu/mm.h            |  20 +++
 xen/arch/arm/xen.lds.S                       |   1 +
 7 files changed, 195 insertions(+)
 create mode 100644 xen/arch/arm/arm64/mpu/head.S
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu/sysregs.h
 create mode 100644 xen/arch/arm/include/asm/mpu/arm64/mm.h
 create mode 100644 xen/arch/arm/include/asm/mpu/mm.h
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/sysregs.h new file +27 -0
new file mode 100644
index 0000000000..b0c31a58ec
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ASM_ARM_ARM64_MPU_SYSREGS_H
+#define __ASM_ARM_ARM64_MPU_SYSREGS_H
+
+/* Number of EL2 MPU regions */
+#define MPUIR_EL2   S3_4_C0_C0_4
+
+/* EL2 MPU Protection Region Base Address Register encode */
+#define PRBAR_EL2   S3_4_C6_C8_0
+
+/* EL2 MPU Protection Region Limit Address Register encode */
+#define PRLAR_EL2   S3_4_C6_C8_1
+
+/* MPU Protection Region Selection Register encode */
+#define PRSELR_EL2  S3_4_C6_C2_1
+
+#endif /* __ASM_ARM_ARM64_MPU_SYSREGS_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 +2 -0
index 5abd4b0d1c..7e61f37612 100644
@@ -16,6 +16,8 @@
 
 #if defined(CONFIG_MMU)
 # include <asm/mmu/mm.h>
+#elif defined(CONFIG_MPU)
+# include <asm/mpu/mm.h>
 #else
 # error "Unknown memory management layout"
 #endif
xen/arch/arm/include/asm/mpu/arm64/mm.h new file +22 -0
new file mode 100644
index 0000000000..c2640b50df
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * mm.h: Arm Memory Protection Unit definitions.
+ */
+
+#ifndef __ARM64_MPU_MM_H__
+#define __ARM64_MPU_MM_H__
+
+#define MPU_REGION_SHIFT  6
+#define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
+#define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
+
+#endif /* __ARM64_MPU_MM_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/mpu/mm.h new file +20 -0
new file mode 100644
index 0000000000..92599a1d75
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ARM_MPU_MM__
+#define __ARM_MPU_MM__
+
+#if defined(CONFIG_ARM_64)
+# include <asm/mpu/arm64/mm.h>
+#else
+# error "unknown ARM variant"
+#endif
+
+#endif /* __ARM_MPU_MM__ */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
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
#11 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Hi Ayan,

Quoted 3 lines · Ayan Kumar Halder

Define enable_boot_cpu_mm() for the AArch64-V8R system.

Could you use here "Armv8-R AArch64” instead of “AArch64-V8R"

Quoted 101 lines

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.

MPU specific registers are defined in
xen/arch/arm/include/asm/arm64/mpu/sysregs.h.

---
Changes from :-

v1 - 1. Instead of mapping a (XEN_START_ADDRESS + 2MB) as a single MPU region, we have separate MPU regions for different parts of the Xen binary. The reason being different regions will nned different permissions (as mentioned in the linker script).

2. Introduced a label (__init_data_begin) to mark the beginning of the init data section.

3. Moved MPU specific register definitions to mpu/sysregs.h.

4. Fixed coding style issues.

5. Included page.h in mpu/head.S as page.h includes sysregs.h. I haven't seen sysregs.h included directly from head.S or mmu/head.S. (Outstanding comment not addressed).

v2 - 1. Extracted "enable_mpu()" in a separate patch.

2. Removed alignment for limit address.

3. Merged some of the sections for preparing the early boot regions.

4. Checked for the max limit of MPU regions before creating a new region.

5. Checked for empty regions.

v3 :- 1. Modified prepare_xen_region() so that we check for empty region within this. Also, index of regions (to be programmed in PRSELR_EL2) should start from 0.

2. Removed load_paddr() as the offset is 0.

3. Introduced fail_insufficient_regions() to handle failure caused when the number of regions to be allocated is not sufficient.

xen/arch/arm/arm64/mpu/Makefile              |   1 +
xen/arch/arm/arm64/mpu/head.S                | 122 +++++++++++++++++++
xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  27 ++++
xen/arch/arm/include/asm/mm.h                |   2 +
xen/arch/arm/include/asm/mpu/arm64/mm.h      |  22 ++++
xen/arch/arm/include/asm/mpu/mm.h            |  20 +++
xen/arch/arm/xen.lds.S                       |   1 +
7 files changed, 195 insertions(+)

create mode 100644 xen/arch/arm/arm64/mpu/head.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu/sysregs.h create mode 100644 xen/arch/arm/include/asm/mpu/arm64/mm.h create mode 100644 xen/arch/arm/include/asm/mpu/mm.h

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 +1 -0
new file mode 100644
index 0000000000..9377ae778c
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-2.0-only */

xen/arch/arm/arm64/mpu/head.S line 11

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+/*
+ * Start-of-day code for an Armv8-R MPU system.
+ */
+
+#include <asm/mm.h>
+#include <asm/arm64/mpu/sysregs.h>
+
+#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 */

NIT: alignment

Quoted 9 lines
xen/arch/arm/arm64/mpu/head.S new file +9 -0
+
+#define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+
+/*
+ * 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:

xen/arch/arm/arm64/mpu/head.S line 30

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+ * sel:         region selector
+ * base:        reg storing base address (should be page-aligned)
+ * 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

NIT: shall we also align the text after the colon?

Quoted 26 lines
xen/arch/arm/arm64/mpu/head.S new file +26 -0
+ */
+.macro prepare_xen_region, sel, base, limit, prbar, prlar, maxcount, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR
+    /* Check if the region is empty */
+    cmp   \base, \limit
+    beq   1f
+
+    /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */
+    cmp   \sel, \maxcount
+    bge   fail_insufficient_regions
+
+    /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/
+    and   \base, \base, #MPU_REGION_MASK
+    mov   \prbar, #\attr_prbar
+    orr   \prbar, \prbar, \base
+
+    /* Limit address should be inclusive */
+    sub   \limit, \limit, #1
+    and   \limit, \limit, #MPU_REGION_MASK
+    mov   \prlar, #\attr_prlar
+    orr   \prlar, \prlar, \limit
+
+    msr   PRSELR_EL2, \sel
+    isb
+    msr   PRBAR_EL2, \prbar
+    msr   PRLAR_EL2, \prlar
+    dsb   sy

xen/arch/arm/arm64/mpu/head.S line 66

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+    isb
+
+1:
+.endm
+
+/*
+ * Failure caused due to insufficient MPU regions.
+ */
+FUNC_LOCAL(fail_insufficient_regions)
+    PRINT("- Number of MPU regions set in MPUIR_EL2 is too less -\r\n")

MPUIR_ELx is a read only register, so I would rephrase this message in something like:

“Selected MPU region is above the implemented number in MPUIR_EL2"

Quoted 15 lines
xen/arch/arm/arm64/mpu/head.S new file +15 -0
+1:  wfe
+    b   1b
+END(fail_insufficient_regions)
+
+/*
+ * Maps the various sections of Xen (described in xen.lds.S) as different MPU
+ * regions.
+ *
+ * Inputs:
+ *   lr : Address to return to.
+ *
+ * Clobbers x0 - x5
+ *
+ */
+FUNC(enable_boot_cpu_mm)

xen/arch/arm/arm64/mpu/head.S line 91

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR

After this region is written, there is no code to increment x0, so all the subsequent will override the region 0.

Quoted 14 lines
xen/arch/arm/arm64/mpu/head.S new file +14 -0
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    /* Xen code section. */
+    ldr   x1, =__init_begin
+    ldr   x2, =__init_data_begin

xen/arch/arm/arm64/mpu/head.S line 115

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen data and BSS section. */
+    ldr   x1, =__init_data_begin
+    ldr   x2, =__bss_end
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    ret
+
+END(enable_boot_cpu_mm)

The rest looks on to me

#12 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Hy Ayan,

Quoted 20 lines

xen/arch/arm/arm64/mpu/head.S line 11

Quoted 4 lines
xen/arch/arm/arm64/mpu/head.S new file +4 -0
+
+#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 */

NIT: alignment

Quoted 13 lines
xen/arch/arm/arm64/mpu/head.S new file +13 -0
+
+#define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+
+/*
+ * 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
+ * base:        reg storing base address (should be page-aligned)
+ * limit:       reg storing limit address
+ * prbar:       store computed PRBAR_EL2 value
Quoted 9 lines
Quoted 6 lines
+ * 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

NIT: shall we also align the text after the colon?

Please forget about these comments, I’ve applied your patches and everything looks good in terms of alignment, I was misled by my mail client.

Cheers,
Luca

#13 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

In reply to Luca Fancellu
Quoted 1 line · Luca Fancellu

Hi Ayan,

Hi Luca,

Quoted 5 lines
Quoted 4 lines · Ayan Kumar Halder

Define enable_boot_cpu_mm() for the AArch64-V8R system.

Could you use here "Armv8-R AArch64” instead of “AArch64-V8R"

Yes.

Quoted 172 lines
Quoted 101 lines

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.

MPU specific registers are defined in
xen/arch/arm/include/asm/arm64/mpu/sysregs.h.

---
Changes from :-

v1 - 1. Instead of mapping a (XEN_START_ADDRESS + 2MB) as a single MPU region, we have separate MPU regions for different parts of the Xen binary. The reason being different regions will nned different permissions (as mentioned in the linker script).

2. Introduced a label (__init_data_begin) to mark the beginning of the init data section.

3. Moved MPU specific register definitions to mpu/sysregs.h.

4. Fixed coding style issues.

5. Included page.h in mpu/head.S as page.h includes sysregs.h. I haven't seen sysregs.h included directly from head.S or mmu/head.S. (Outstanding comment not addressed).

v2 - 1. Extracted "enable_mpu()" in a separate patch.

2. Removed alignment for limit address.

3. Merged some of the sections for preparing the early boot regions.

4. Checked for the max limit of MPU regions before creating a new region.

5. Checked for empty regions.

v3 :- 1. Modified prepare_xen_region() so that we check for empty region within this. Also, index of regions (to be programmed in PRSELR_EL2) should start from 0.

2. Removed load_paddr() as the offset is 0.

3. Introduced fail_insufficient_regions() to handle failure caused when the number of regions to be allocated is not sufficient.

xen/arch/arm/arm64/mpu/Makefile              |   1 +
xen/arch/arm/arm64/mpu/head.S                | 122 +++++++++++++++++++
xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  27 ++++
xen/arch/arm/include/asm/mm.h                |   2 +
xen/arch/arm/include/asm/mpu/arm64/mm.h      |  22 ++++
xen/arch/arm/include/asm/mpu/mm.h            |  20 +++
xen/arch/arm/xen.lds.S                       |   1 +
7 files changed, 195 insertions(+)

create mode 100644 xen/arch/arm/arm64/mpu/head.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu/sysregs.h create mode 100644 xen/arch/arm/include/asm/mpu/arm64/mm.h create mode 100644 xen/arch/arm/include/asm/mpu/mm.h

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 +1 -0
new file mode 100644
index 0000000000..9377ae778c
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-2.0-only */

xen/arch/arm/arm64/mpu/head.S line 11

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+/*
+ * Start-of-day code for an Armv8-R MPU system.
+ */
+
+#include <asm/mm.h>
+#include <asm/arm64/mpu/sysregs.h>
+
+#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 */

NIT: alignment

Quoted 9 lines
xen/arch/arm/arm64/mpu/head.S new file +9 -0
+
+#define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+
+/*
+ * 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:

xen/arch/arm/arm64/mpu/head.S line 30

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+ * sel:         region selector
+ * base:        reg storing base address (should be page-aligned)
+ * 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

NIT: shall we also align the text after the colon?

Quoted 26 lines
xen/arch/arm/arm64/mpu/head.S new file +26 -0
+ */
+.macro prepare_xen_region, sel, base, limit, prbar, prlar, maxcount, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR
+    /* Check if the region is empty */
+    cmp   \base, \limit
+    beq   1f
+
+    /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */
+    cmp   \sel, \maxcount
+    bge   fail_insufficient_regions
+
+    /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/
+    and   \base, \base, #MPU_REGION_MASK
+    mov   \prbar, #\attr_prbar
+    orr   \prbar, \prbar, \base
+
+    /* Limit address should be inclusive */
+    sub   \limit, \limit, #1
+    and   \limit, \limit, #MPU_REGION_MASK
+    mov   \prlar, #\attr_prlar
+    orr   \prlar, \prlar, \limit
+
+    msr   PRSELR_EL2, \sel
+    isb
+    msr   PRBAR_EL2, \prbar
+    msr   PRLAR_EL2, \prlar
+    dsb   sy

xen/arch/arm/arm64/mpu/head.S line 66

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+    isb
+
+1:
+.endm
+
+/*
+ * Failure caused due to insufficient MPU regions.
+ */
+FUNC_LOCAL(fail_insufficient_regions)
+    PRINT("- Number of MPU regions set in MPUIR_EL2 is too less -\r\n")

MPUIR_ELx is a read only register, so I would rephrase this message in something like:

Quoted 1 line

“Selected MPU region is above the implemented number in MPUIR_EL2"

Ack.

Quoted 20 lines
Quoted 20 lines
xen/arch/arm/arm64/mpu/head.S new file +19 -0
+1:  wfe
+    b   1b
+END(fail_insufficient_regions)
+
+/*
+ * Maps the various sections of Xen (described in xen.lds.S) as different MPU
+ * regions.
+ *
+ * Inputs:
+ *   lr : Address to return to.
+ *
+ * Clobbers x0 - x5
+ *
+ */
+FUNC(enable_boot_cpu_mm)
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
Quoted 8 lines

xen/arch/arm/arm64/mpu/head.S line 91

Quoted 6 lines
xen/arch/arm/arm64/mpu/head.S new file +6 -0
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR

After this region is written, there is no code to increment x0, so all the subsequent will override the region 0.

Ah yes, you are correct. I should increment \sel in prepare_xen_region().

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/arm64/mpu/head.S new file +17 -0
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    /* Xen code section. */
+    ldr   x1, =__init_begin
+    ldr   x2, =__init_data_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen data and BSS section. */
Quoted 8 lines

xen/arch/arm/arm64/mpu/head.S line 115

Quoted 7 lines
xen/arch/arm/arm64/mpu/head.S new file +7 -0
+    ldr   x1, =__init_data_begin
+    ldr   x2, =__bss_end
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    ret
+
+END(enable_boot_cpu_mm)

The rest looks on to me

Thanks.

- Ayan

#14 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

In reply to Ayan Kumar Halder

Hi Ayan,

I forgot another thing:

xen/arch/arm/arm64/mpu/head.S line 6

Quoted 12 lines
xen/arch/arm/arm64/mpu/head.S new file +6 -0
new file mode 100644
index 0000000000..9377ae778c
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Start-of-day code for an Armv8-R MPU system.
+ */
+
+#include <asm/mm.h>

^— This feels suspicious, this header cannot be included by an assembly file

#15 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

In reply to Ayan Kumar Halder

Hi Ayan,

While I rebased the branch on top of your patches, I saw you’ve changed the number of regions mapped at boot time, can I ask why?

Compared to https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/:

Quoted 11 lines
xen/arch/arm/arm64/mpu/head.S new file +11 -0
+FUNC(enable_boot_cpu_mm)
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR

xen/arch/arm/arm64/mpu/head.S line 101

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5

^— this, for example, will block Xen to call init_done(void) later, I understand this is earlyboot,
but I guess we don’t want to make subsequent changes to this part when introducing the patches to support start_xen()

Quoted 4 lines
xen/arch/arm/arm64/mpu/head.S new file +4 -0
+
+    /* Xen code section. */
+    ldr   x1, =__init_begin
+    ldr   x2, =__init_data_begin

xen/arch/arm/arm64/mpu/head.S line 115

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen data and BSS section. */
+    ldr   x1, =__init_data_begin
+    ldr   x2, =__bss_end
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    ret
+
+END(enable_boot_cpu_mm)

I suggest to keep exactly the regions as are in Penny’s patch.

Cheers,
Luca

#16 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Quoted 5 lines · Luca Fancellu

Hi Ayan,

While I rebased the branch on top of your patches, I saw you’ve changed the number of regions mapped at boot time, can I ask why?

I have asked the change. If you look at the layout...

Quoted 2 lines

Compared to https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/:

... you have two sections with the same permissions:

xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data

During boot, [2] and [3] will share the same permissions. After boot, this will be [1] and [2]. Given the number of MPU regions is limited, this is a bit of a waste.

We also don't want to have a hole in the middle of Xen sections. So folding seemed to be a good idea.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/arm64/mpu/head.S new file +17 -0
+FUNC(enable_boot_cpu_mm)
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
Quoted 8 lines

xen/arch/arm/arm64/mpu/head.S line 101

Quoted 4 lines
xen/arch/arm/arm64/mpu/head.S new file +4 -0
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5

^— this, for example, will block Xen to call init_done(void) later, I understand this is earlyboot,
but I guess we don’t want to make subsequent changes to this part when introducing the patches to support start_xen()

Can you be a bit more descriptive... What will block?

Quoted 17 lines
Quoted 5 lines
xen/arch/arm/arm64/mpu/head.S new file +4 -0
+
+    /* Xen code section. */
+    ldr   x1, =__init_begin
+    ldr   x2, =__init_data_begin

xen/arch/arm/arm64/mpu/head.S line 115

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen data and BSS section. */
+    ldr   x1, =__init_data_begin
+    ldr   x2, =__bss_end
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    ret
+
+END(enable_boot_cpu_mm)

I suggest to keep exactly the regions as are in Penny’s patch.

See above. Without any details on the exact problem, it is difficult to agree on your suggestion.

Cheers,

#17 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Hi Julien,

Quoted 10 lines · Julien Grall
Quoted 5 lines · Luca Fancellu

Hi Ayan,

While I rebased the branch on top of your patches, I saw you’ve changed the number of regions mapped at boot time, can I ask why?

I have asked the change. If you look at the layout...

Apologies, I didn’t see you’ve asked the change

Quoted 45 lines
Quoted 3 lines

Compared to https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/:

... you have two sections with the same permissions:

xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data

During boot, [2] and [3] will share the same permissions. After boot, this will be [1] and [2]. Given the number of MPU regions is limited, this is a bit of a waste.

We also don't want to have a hole in the middle of Xen sections. So folding seemed to be a good idea.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/arm64/mpu/head.S new file +17 -0
+FUNC(enable_boot_cpu_mm)
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
Quoted 8 lines

xen/arch/arm/arm64/mpu/head.S line 101

Quoted 4 lines
xen/arch/arm/arm64/mpu/head.S new file +4 -0
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5

^— this, for example, will block Xen to call init_done(void) later, I understand this is earlyboot,
but I guess we don’t want to make subsequent changes to this part when introducing the patches to support start_xen()

Quoted 1 line

Can you be a bit more descriptive... What will block?

This call in setup.c:
rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
(unsigned long)&__ro_after_init_end, PAGE_HYPERVISOR_RO);

Cannot work anymore because xen_mpumap[2] is wider than only (__ro_after_init_start, __ro_after_init_end).

If that is what we want, then we could wrap the above call into something MMU specific that will execute the above call and something MPU specific that will modify xen_mpumap[1] from (_srodata, _erodata) to (_srodata, __ro_after_init_end) and xen_mpumap[2] from (__ro_after_init_start, __init_begin) to (__ro_after_init_end, __init_begin).

Please, let me know your thoughts.

Cheers,
Luca

#18 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Quoted 6 lines · Luca Fancellu

Hi Julien,

Quoted 4 lines · Julien Grall
Quoted 1 line · Luca Fancellu
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Hi Ayan,

While I rebased the branch on top of your patches, I saw you’ve changed the number of regions mapped at boot time, can I ask why?

I have asked the change. If you look at the layout...

Apologies, I didn’t see you’ve asked the change

No need to apologies! I think I asked a few revisions ago.

Quoted 46 lines
Quoted 46 lines
Quoted 4 lines

Compared to https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/:

... you have two sections with the same permissions:

xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data

During boot, [2] and [3] will share the same permissions. After boot, this will be [1] and [2]. Given the number of MPU regions is limited, this is a bit of a waste.

We also don't want to have a hole in the middle of Xen sections. So folding seemed to be a good idea.

Quoted 26 lines
Quoted 12 lines
xen/arch/arm/arm64/mpu/head.S new file +11 -0
+FUNC(enable_boot_cpu_mm)
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR

xen/arch/arm/arm64/mpu/head.S line 101

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5

^— this, for example, will block Xen to call init_done(void) later, I understand this is earlyboot,
but I guess we don’t want to make subsequent changes to this part when introducing the patches to support start_xen()

Quoted 8 lines
Quoted 1 line

Can you be a bit more descriptive... What will block?

This call in setup.c:
rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
(unsigned long)&__ro_after_init_end, PAGE_HYPERVISOR_RO);

Cannot work anymore because xen_mpumap[2] is wider than only (__ro_after_init_start, __ro_after_init_end).

Is this because the implementation of modify_xen_mappings() is only able to modify a full region? IOW, it would not be able to split regions and/or merge them?

Quoted 4 lines

If that is what we want, then we could wrap the above call into something MMU specific that will execute the above call and something MPU specific that will modify xen_mpumap[1] from (_srodata, _erodata) to (_srodata, __ro_after_init_end) and xen_mpumap[2] from (__ro_after_init_start, __init_begin) to (__ro_after_init_end, __init_begin).

I think it would make sense to have the call mmu/mpu specific. This would allow to limit the number of MPU regions used by Xen itself.

The only problem is IIRC the region is not fixed because we will skip empty regions during earlyboot.

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Hi Julien,

Quoted 63 lines · Julien Grall
Quoted 5 lines · Luca Fancellu

Hi Julien,

Quoted 4 lines · Julien Grall
Quoted 1 line · Luca Fancellu
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

Hi Ayan,

While I rebased the branch on top of your patches, I saw you’ve changed the number of regions mapped at boot time, can I ask why?

I have asked the change. If you look at the layout...

Apologies, I didn’t see you’ve asked the change

No need to apologies! I think I asked a few revisions ago.

Quoted 45 lines
Quoted 45 lines
Quoted 3 lines

Compared to https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/:

... you have two sections with the same permissions:

xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data

During boot, [2] and [3] will share the same permissions. After boot, this will be [1] and [2]. Given the number of MPU regions is limited, this is a bit of a waste.

We also don't want to have a hole in the middle of Xen sections. So folding seemed to be a good idea.

Quoted 26 lines
Quoted 12 lines
xen/arch/arm/arm64/mpu/head.S new file +11 -0
+FUNC(enable_boot_cpu_mm)
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR

xen/arch/arm/arm64/mpu/head.S line 101

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5

^— this, for example, will block Xen to call init_done(void) later, I understand this is earlyboot,
but I guess we don’t want to make subsequent changes to this part when introducing the patches to support start_xen()

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Can you be a bit more descriptive... What will block?

This call in setup.c:
rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
(unsigned long)&__ro_after_init_end,
PAGE_HYPERVISOR_RO);
Cannot work anymore because xen_mpumap[2] is wider than only (__ro_after_init_start, __ro_after_init_end).

Is this because the implementation of modify_xen_mappings() is only able to modify a full region? IOW, it would not be able to split regions and/or merge them?

Yes, the code is, at the moment, not smart enough to do that, it will only modify a full region.

Quoted 8 lines
Quoted 4 lines

If that is what we want, then we could wrap the above call into something MMU specific that will execute the above call and something MPU specific that will modify xen_mpumap[1] from (_srodata, _erodata) to (_srodata, __ro_after_init_end) and xen_mpumap[2] from (__ro_after_init_start, __init_begin) to (__ro_after_init_end, __init_begin).

I think it would make sense to have the call mmu/mpu specific. This would allow to limit the number of MPU regions used by Xen itself.

The only problem is IIRC the region is not fixed because we will skip empty regions during earlyboot.

Yes, but I think we can assume that X(_srodata, _erodata) and Y(__ro_after_init_start, __init_begin) won’t never be empty for Xen?

In that case, the call to mpumap_contain_region() should be able to retrieve the full region X and the partial region Y and a specific function could modify the ranges of both given the respective indexes.

Code in my branch: https://gitlab.com/xen-project/people/lucafancellu/xen/-/blob/r82_rebased/xen/arch/arm/mpu/mm.c#L382

#20 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Quoted 1 line · Luca Fancellu

Hi Julien,

Hi Luca/Julien,

Quoted 69 lines
Quoted 55 lines · Julien Grall
Quoted 10 lines · Luca Fancellu

Hi Julien,

Quoted 8 lines · Julien Grall
Quoted 4 lines · Luca Fancellu

Hi Ayan,

While I rebased the branch on top of your patches, I saw you’ve changed the number of regions mapped at boot time, can I ask why?

I have asked the change. If you look at the layout...

Apologies, I didn’t see you’ve asked the change

No need to apologies! I think I asked a few revisions ago.

Quoted 39 lines
Quoted 39 lines
Quoted 1 line

Compared to https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/:

... you have two sections with the same permissions:

xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data

During boot, [2] and [3] will share the same permissions. After boot, this will be [1] and [2]. Given the number of MPU regions is limited, this is a bit of a waste.

We also don't want to have a hole in the middle of Xen sections. So folding seemed to be a good idea.

Quoted 24 lines
Quoted 11 lines
xen/arch/arm/arm64/mpu/head.S new file +11 -0
+FUNC(enable_boot_cpu_mm)
+
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR

xen/arch/arm/arm64/mpu/head.S line 101

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5

^— this, for example, will block Xen to call init_done(void) later, I understand this is earlyboot,
but I guess we don’t want to make subsequent changes to this part when introducing the patches to support start_xen()

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

Can you be a bit more descriptive... What will block?

This call in setup.c:
rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
(unsigned long)&__ro_after_init_end,
PAGE_HYPERVISOR_RO);
Cannot work anymore because xen_mpumap[2] is wider than only (__ro_after_init_start, __ro_after_init_end).

Is this because the implementation of modify_xen_mappings() is only able to modify a full region? IOW, it would not be able to split regions and/or merge them?

Yes, the code is, at the moment, not smart enough to do that, it will only modify a full region.

Quoted 5 lines
Quoted 3 lines

If that is what we want, then we could wrap the above call into something MMU specific that will execute the above call and something MPU specific that will modify xen_mpumap[1] from (_srodata, _erodata) to (_srodata, __ro_after_init_end) and xen_mpumap[2] from (__ro_after_init_start, __init_begin) to (__ro_after_init_end, __init_begin).

I think it would make sense to have the call mmu/mpu specific. This would allow to limit the number of MPU regions used by Xen itself.

Quoted 7 lines
Quoted 1 line

The only problem is IIRC the region is not fixed because we will skip empty regions during earlyboot.

Yes, but I think we can assume that X(_srodata, _erodata) and Y(__ro_after_init_start, __init_begin) won’t never be empty for Xen?

In that case, the call to mpumap_contain_region() should be able to retrieve the full region X and the partial region Y and a specific function could modify the ranges of both given the respective indexes.

Code in my branch: https://gitlab.com/xen-project/people/lucafancellu/xen/-/blob/r82_rebased/xen/arch/arm/mpu/mm.c#L382

Can we keep the current patch as it is ? We can revisit enable_boot_cpu_mm() when we enable start_xen() for mpu.

I can send a respin once we are aligned.

- Ayan

#21 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 173 lines · Ayan Kumar Halder

Define enable_boot_cpu_mm() for the AArch64-V8R system.

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.

MPU specific registers are defined in
xen/arch/arm/include/asm/arm64/mpu/sysregs.h.

---
Changes from :-

v1 - 1. Instead of mapping a (XEN_START_ADDRESS + 2MB) as a single MPU region, we have separate MPU regions for different parts of the Xen binary. The reason being different regions will nned different permissions (as mentioned in the linker script).

2. Introduced a label (__init_data_begin) to mark the beginning of the init data section.

3. Moved MPU specific register definitions to mpu/sysregs.h.

4. Fixed coding style issues.

5. Included page.h in mpu/head.S as page.h includes sysregs.h. I haven't seen sysregs.h included directly from head.S or mmu/head.S. (Outstanding comment not addressed).

v2 - 1. Extracted "enable_mpu()" in a separate patch.

2. Removed alignment for limit address.

3. Merged some of the sections for preparing the early boot regions.

4. Checked for the max limit of MPU regions before creating a new region.

5. Checked for empty regions.

v3 :- 1. Modified prepare_xen_region() so that we check for empty region within this. Also, index of regions (to be programmed in PRSELR_EL2) should start from 0.

2. Removed load_paddr() as the offset is 0.

3. Introduced fail_insufficient_regions() to handle failure caused when the number of regions to be allocated is not sufficient.

  xen/arch/arm/arm64/mpu/Makefile              |   1 +
  xen/arch/arm/arm64/mpu/head.S                | 122 +++++++++++++++++++
  xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  27 ++++
  xen/arch/arm/include/asm/mm.h                |   2 +
  xen/arch/arm/include/asm/mpu/arm64/mm.h      |  22 ++++
  xen/arch/arm/include/asm/mpu/mm.h            |  20 +++
  xen/arch/arm/xen.lds.S                       |   1 +
  7 files changed, 195 insertions(+)

create mode 100644 xen/arch/arm/arm64/mpu/head.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu/sysregs.h create mode 100644 xen/arch/arm/include/asm/mpu/arm64/mm.h create mode 100644 xen/arch/arm/include/asm/mpu/mm.h

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 +72 -0
new file mode 100644
index 0000000000..9377ae778c
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Start-of-day code for an Armv8-R MPU system.
+ */
+
+#include <asm/mm.h>
+#include <asm/arm64/mpu/sysregs.h>
+
+#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 */
+
+#define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+
+/*
+ * 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
+ * base:        reg storing base address (should be page-aligned)
+ * 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
+ */
+.macro prepare_xen_region, sel, base, limit, prbar, prlar, maxcount, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR
+    /* Check if the region is empty */
+    cmp   \base, \limit
+    beq   1f
+
+    /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */
+    cmp   \sel, \maxcount
+    bge   fail_insufficient_regions
+
+    /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/
+    and   \base, \base, #MPU_REGION_MASK
+    mov   \prbar, #\attr_prbar
+    orr   \prbar, \prbar, \base
+
+    /* Limit address should be inclusive */
+    sub   \limit, \limit, #1
+    and   \limit, \limit, #MPU_REGION_MASK
+    mov   \prlar, #\attr_prlar
+    orr   \prlar, \prlar, \limit
+
+    msr   PRSELR_EL2, \sel
+    isb
+    msr   PRBAR_EL2, \prbar
+    msr   PRLAR_EL2, \prlar
+    dsb   sy
+    isb
+
+1:
+.endm
+
+/*
+ * Failure caused due to insufficient MPU regions.
+ */
+FUNC_LOCAL(fail_insufficient_regions)
+    PRINT("- Number of MPU regions set in MPUIR_EL2 is too less -\r\n")
+1:  wfe
+    b   1b
+END(fail_insufficient_regions)
+
+/*
+ * Maps the various sections of Xen (described in xen.lds.S) as different MPU

xen/arch/arm/arm64/mpu/head.S line 82

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+ * regions.
+ *
+ * Inputs:
+ *   lr : Address to return to.
+ *
+ * Clobbers x0 - x5
+ *
+ */
+FUNC(enable_boot_cpu_mm)
+

NIT: I would remove this empty line.

xen/arch/arm/arm64/mpu/head.S line 84

Quoted 2 lines
xen/arch/arm/arm64/mpu/head.S new file +2 -0
+    /* Get the number of regions specified in MPUIR_EL2 */
+    mrs   x5, MPUIR_EL2

Looking at the spec, the number of regions are only in the first 8-bits. So you want to mask before using it.

Quoted 20 lines
xen/arch/arm/arm64/mpu/head.S new file +20 -0
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    /* Xen code section. */
+    ldr   x1, =__init_begin

xen/arch/arm/arm64/mpu/head.S line 114

Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10 -0
+    ldr   x2, =__init_data_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen data and BSS section. */
+    ldr   x1, =__init_data_begin
+    ldr   x2, =__bss_end
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    ret
+

NIT: I would remove this empty line.

Quoted 14 lines
xen/arch/arm/arm64/mpu/head.S new file +8 -0
+END(enable_boot_cpu_mm)
+
+/*
+ * Local variables:
+ * mode: ASM
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/arm64/mpu/sysregs.h new file +0 -0
new file mode 100644
index 0000000000..b0c31a58ec

xen/arch/arm/include/asm/arm64/mpu/sysregs.h line 7

Quoted 7 lines
xen/arch/arm/include/asm/arm64/mpu/sysregs.h new file +7 -0
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ASM_ARM_ARM64_MPU_SYSREGS_H
+#define __ASM_ARM_ARM64_MPU_SYSREGS_H
+
+/* Number of EL2 MPU regions */
+#define MPUIR_EL2   S3_4_C0_C0_4

Do you know why the compiler doesn't support MPUIR_EL2 & co natively? Any chance this is because we may build Xen with the wrong flags for the MPU port?

Cheers,

Signature · 1 line
Julien Grall
#22 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Quoted 1 line · Julien Grall

Hi Ayan,

Hi Julien,

Quoted 184 lines · Ayan Kumar Halder
Quoted 182 lines

Define enable_boot_cpu_mm() for the AArch64-V8R system.

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.

MPU specific registers are defined in
xen/arch/arm/include/asm/arm64/mpu/sysregs.h.

---
Changes from :-

v1 - 1. Instead of mapping a (XEN_START_ADDRESS + 2MB) as a single MPU region, we have separate MPU regions for different parts of the Xen binary. The reason being different regions will nned different permissions (as mentioned in the linker script).

2. Introduced a label (__init_data_begin) to mark the beginning of the init data section.

3. Moved MPU specific register definitions to mpu/sysregs.h.

4. Fixed coding style issues.

5. Included page.h in mpu/head.S as page.h includes sysregs.h. I haven't seen sysregs.h included directly from head.S or mmu/head.S. (Outstanding comment not addressed).

v2 - 1. Extracted "enable_mpu()" in a separate patch.

2. Removed alignment for limit address.

3. Merged some of the sections for preparing the early boot regions.

4. Checked for the max limit of MPU regions before creating a new region.

5. Checked for empty regions.

v3 :- 1. Modified prepare_xen_region() so that we check for empty region within this. Also, index of regions (to be programmed in PRSELR_EL2) should start from 0.

2. Removed load_paddr() as the offset is 0.

3. Introduced fail_insufficient_regions() to handle failure caused when the number of regions to be allocated is not sufficient.

  xen/arch/arm/arm64/mpu/Makefile              |   1 + xen/arch/arm/arm64/mpu/head.S                | 122 +++++++++++++++++++ xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  27 ++++ xen/arch/arm/include/asm/mm.h                |   2 + xen/arch/arm/include/asm/mpu/arm64/mm.h      |  22 ++++ xen/arch/arm/include/asm/mpu/mm.h            |  20 +++ xen/arch/arm/xen.lds.S                       |   1 +

  7 files changed, 195 insertions(+)

  create mode 100644 xen/arch/arm/arm64/mpu/head.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu/sysregs.h create mode 100644 xen/arch/arm/include/asm/mpu/arm64/mm.h create mode 100644 xen/arch/arm/include/asm/mpu/mm.h

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 +27 -0
new file mode 100644
index 0000000000..9377ae778c
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Start-of-day code for an Armv8-R MPU system.
+ */
+
+#include <asm/mm.h>
+#include <asm/arm64/mpu/sysregs.h>
+
+#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 */
+
+#define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+
+/*
+ * 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
+ * base:        reg storing base address (should be page-aligned)
+ * 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
+ */
+.macro prepare_xen_region, sel, base, limit, prbar, prlar, maxcount, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR +    /* Check if the region is empty */
+    cmp   \base, \limit
+    beq   1f
+
+    /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */
+    cmp   \sel, \maxcount
+    bge   fail_insufficient_regions

xen/arch/arm/arm64/mpu/head.S new file +33 -0
+
+    /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/
+    and   \base, \base, #MPU_REGION_MASK
+    mov   \prbar, #\attr_prbar
+    orr   \prbar, \prbar, \base
+
+    /* Limit address should be inclusive */
+    sub   \limit, \limit, #1
+    and   \limit, \limit, #MPU_REGION_MASK
+    mov   \prlar, #\attr_prlar
+    orr   \prlar, \prlar, \limit
+
+    msr   PRSELR_EL2, \sel
+    isb
+    msr   PRBAR_EL2, \prbar
+    msr   PRLAR_EL2, \prlar
+    dsb   sy
+    isb
+
+1:
+.endm
+
+/*
+ * Failure caused due to insufficient MPU regions.
+ */
+FUNC_LOCAL(fail_insufficient_regions)
+    PRINT("- Number of MPU regions set in MPUIR_EL2 is too less -\r\n")
+1:  wfe
+    b   1b
+END(fail_insufficient_regions)
+
+/*
+ * Maps the various sections of Xen (described in xen.lds.S) as 

different MPU
+ * regions.
+ *
+ * Inputs:
+ *   lr : Address to return to.

Quoted 8 lines

xen/arch/arm/arm64/mpu/head.S

Quoted 6 lines

+ *
+ * Clobbers x0 - x5
+ *
+ */

xen/arch/arm/arm64/mpu/head.S new file +2 -0
+FUNC(enable_boot_cpu_mm)
+

NIT: I would remove this empty line.

Ack

Quoted 5 lines
Quoted 3 lines

+    /* Get the number of regions specified in MPUIR_EL2 */ +    mrs   x5, MPUIR_EL2

Looking at the spec, the number of regions are only in the first 8-bits. So you want to mask before using it.

yes, I will do it.

Quoted 28 lines
Quoted 28 lines

+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    ldr   x1, =_stext
+    ldr   x2, =_etext
+    prepare_xen_region x0, x1, x2, x3, x4, x5,
attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen read-only data section. */
+    ldr   x1, =_srodata
+    ldr   x2, =_erodata
+    prepare_xen_region x0, x1, x2, x3, x4, x5,
attr_prbar=REGION_RO_PRBAR

xen/arch/arm/arm64/mpu/head.S new file +10 -0
+
+    /* Xen read-only after init and data section. (RW data) */
+    ldr   x1, =__ro_after_init_start
+    ldr   x2, =__init_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5
+
+    /* Xen code section. */
+    ldr   x1, =__init_begin
+    ldr   x2, =__init_data_begin
+    prepare_xen_region x0, x1, x2, x3, x4, x5, 

attr_prbar=REGION_TEXT_PRBAR
+
+    /* Xen data and BSS section. */

Quoted 8 lines

xen/arch/arm/arm64/mpu/head.S line 114

Quoted 6 lines

+    ldr   x1, =__init_data_begin
+    ldr   x2, =__bss_end
+    prepare_xen_region x0, x1, x2, x3, x4, x5

xen/arch/arm/arm64/mpu/head.S new file +3 -0
+
+    ret
+

NIT: I would remove this empty line.

ack.

Quoted 16 lines
Quoted 16 lines
xen/arch/arm/arm64/mpu/head.S new file +8 -0
+END(enable_boot_cpu_mm)
+
+/*
+ * Local variables:
+ * mode: ASM
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/arm64/mpu/sysregs.h new file +1 -0
new file mode 100644
index 0000000000..b0c31a58ec
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
Quoted 8 lines

xen/arch/arm/include/asm/arm64/mpu/sysregs.h line 7

Quoted 6 lines
xen/arch/arm/include/asm/arm64/mpu/sysregs.h new file +6 -0
+
+#ifndef __ASM_ARM_ARM64_MPU_SYSREGS_H
+#define __ASM_ARM_ARM64_MPU_SYSREGS_H
+
+/* Number of EL2 MPU regions */
+#define MPUIR_EL2   S3_4_C0_C0_4

Do you know why the compiler doesn't support MPUIR_EL2 & co natively? Any chance this is because we may build Xen with the wrong flags for the MPU port?

Yes, you are correct. We need the following change.

|--- a/xen/arch/arm/arch.mk +++ b/xen/arch/arm/arch.mk @@ -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) - Ayan |

#23 lore raw

Re: [PATCH v4 4/6] xen/arm: mpu: Create boot-time MPU protection regions

Quoted 6 lines · Ayan Kumar Halder
Quoted 1 line · Julien Grall

Hi Ayan,

Hi Julien,

Yes, you are correct. We need the following change.

xen/arch/arm/arch.mk +0 -0
@@ -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)

(Sorry for the misformatted snippet in the previous mail)

- Ayan

#24 lore raw

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

As enable_mpu() is invoked from enable_boot_cpu_mm(), one needs to save and restore the lr.

---
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().

 xen/arch/arm/arm64/mpu/head.S                | 30 ++++++++++++++++++--
 xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  3 ++
 2 files changed, 31 insertions(+), 2 deletions(-)
xen/arch/arm/arm64/mpu/head.S +28 -2
index 9377ae778c..0edadb009c 100644
@@ -68,6 +68,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.
@@ -75,10 +98,11 @@ END(fail_insufficient_regions)
  * Inputs:
  *   lr : Address to return to.
  *
- * Clobbers x0 - x5
+ * Clobbers x0 - x6
  *
  */
 FUNC(enable_boot_cpu_mm)
+    mov   x6, lr
 
     /* Get the number of regions specified in MPUIR_EL2 */
     mrs   x5, MPUIR_EL2
@@ -110,8 +134,10 @@ FUNC(enable_boot_cpu_mm)
     ldr   x2, =__bss_end
     prepare_xen_region x0, x1, x2, x3, x4, x5
 
-    ret
+    bl    enable_mpu
 
+    mov   lr, x6
+    ret
 END(enable_boot_cpu_mm)
 
 /*
xen/arch/arm/include/asm/arm64/mpu/sysregs.h +3 -0
index b0c31a58ec..3769d23c80 100644
@@ -15,6 +15,9 @@
 /* MPU Protection Region Selection Register encode */
 #define PRSELR_EL2  S3_4_C6_C2_1
 
+/* Backgroud region enable/disable */
+#define SCTLR_ELx_BR    BIT(17, UL)
+
 #endif /* __ASM_ARM_ARM64_MPU_SYSREGS_H */
 
 /*
Signature · 1 line
2.25.1
Reviewed-by on v4 5/6
#25 lore raw

Re: [PATCH v4 5/6] xen/arm: mpu: Enable MPU

Hi Ayan,

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

Quoted 6 lines

As enable_mpu() is invoked from enable_boot_cpu_mm(), one needs to save and restore the lr.

---

It looks good to me:

Acked-by on v4 5/6
#26 lore raw

Re: [PATCH v4 5/6] xen/arm: mpu: Enable MPU

In reply to Ayan Kumar Halder

Hi Ayan,

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

As enable_mpu() is invoked from enable_boot_cpu_mm(), one needs to save and restore the lr.

---
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().

  xen/arch/arm/arm64/mpu/head.S                | 30 ++++++++++++++++++--
  xen/arch/arm/include/asm/arm64/mpu/sysregs.h |  3 ++
  2 files changed, 31 insertions(+), 2 deletions(-)
xen/arch/arm/arm64/mpu/head.S +25 -1
index 9377ae778c..0edadb009c 100644
@@ -68,6 +68,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.
@@ -75,10 +98,11 @@ END(fail_insufficient_regions)
   * Inputs:
   *   lr : Address to return to.
   *
- * Clobbers x0 - x5
+ * Clobbers x0 - x6
   *
   */
  FUNC(enable_boot_cpu_mm)
+    mov   x6, lr
 
      /* Get the number of regions specified in MPUIR_EL2 */
      mrs   x5, MPUIR_EL2

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

Quoted 8 lines
xen/arch/arm/arm64/mpu/head.S +3 -1
@@ -110,8 +134,10 @@ FUNC(enable_boot_cpu_mm)
      ldr   x2, =__bss_end
      prepare_xen_region x0, x1, x2, x3, x4, x5
 
-    ret
+    bl    enable_mpu
 
+    mov   lr, x6
+    ret

You should not need to save/restore 'lr'. You could write:

b enable_mpu

So when enable_mpu returns, it will go back to the caller of enable_boot_cpu_mm.

With that fixed:

Cheers,

Signature · 1 line
Julien Grall
#27 lore raw

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

 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..aa383577a4 100644
@@ -6,11 +6,13 @@ 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 "8" if ARM && RCAR3
 	default "4" if ARM && QEMU
 	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
 	default "128" if ARM
 	help
 	  Controls the build-time size of various arrays and bitmaps
xen/arch/arm/arm64/mpu/head.S +10 -0
index 0edadb009c..5a6aaf47cd 100644
@@ -140,6 +140,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
#28 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Quoted 6 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
 config NR_CPUS
 	int "Maximum number of CPUs"

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 10 lines
xen/arch/Kconfig +2 -0
+	range 1 1 if ARM && MPU
 	range 1 16383
 	default "256" if X86
 	default "8" if ARM && RCAR3
 	default "4" if ARM && QEMU
 	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
 	default "128" if ARM
 	help
 	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

Jan

#29 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Hi Jan,

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
Quoted 16 lines

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 12 lines
xen/arch/Kconfig +2 -0
  config NR_CPUS
  	int "Maximum number of CPUs"
+	range 1 1 if ARM && MPU
  	range 1 16383
  	default "256" if X86
  	default "8" if ARM && RCAR3
  	default "4" if ARM && QEMU
  	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
  	default "128" if ARM
  	help
  	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

MPU cannot be used with any of the existing platforms.

There will be patch in the future series where we will introduce a platform specific for MPU. That can be FVP for R82 and R52.

- Ayan

Quoted 2 lines

Jan

#30 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Quoted 23 lines · Ayan Kumar Halder
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
Quoted 16 lines

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 12 lines
xen/arch/Kconfig +2 -0
  config NR_CPUS
  	int "Maximum number of CPUs"
+	range 1 1 if ARM && MPU
  	range 1 16383
  	default "256" if X86
  	default "8" if ARM && RCAR3
  	default "4" if ARM && QEMU
  	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
  	default "128" if ARM
  	help
  	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

Quoted 1 line

MPU cannot be used with any of the existing platforms.

That is - qemu can't emulate such an environment, i.e. even QEMU and MPU don't go together?

Jan

#31 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Hi Jan,

Quoted 23 lines · Jan Beulich
Quoted 22 lines · Ayan Kumar Halder
Quoted 21 lines · Jan Beulich
Quoted 6 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
   config NR_CPUS
   	int "Maximum number of CPUs"

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 10 lines
xen/arch/Kconfig +2 -0
+	range 1 1 if ARM && MPU
   	range 1 16383
   	default "256" if X86
   	default "8" if ARM && RCAR3
   	default "4" if ARM && QEMU
   	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
   	default "128" if ARM
   	help
   	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

Quoted 3 lines
Quoted 1 line

MPU cannot be used with any of the existing platforms.

That is - qemu can't emulate such an environment, i.e. even QEMU and MPU don't go together?

Qemu has support for Aarch32 MPU at EL2 and EL1 (ie R52). As far as I am aware, there is no support for Aarch64 MPU in Qemu (ie R82).

Even for R52, I could not get the upstream Qemu working (emulating some Arm reference platform).

I could get the Xilinx fork of Qemu (https://github.com/Xilinx/qemu) working which emulates AMD's SoC using R52.

However, this should not impact the current patch. There is no Qemu in xen/arch/arm/platforms/*.

- Ayan

#32 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Quoted 31 lines · Ayan Kumar Halder
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Ayan Kumar Halder
Quoted 21 lines · Jan Beulich
Quoted 6 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
   config NR_CPUS
   	int "Maximum number of CPUs"

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 10 lines
xen/arch/Kconfig +2 -0
+	range 1 1 if ARM && MPU
   	range 1 16383
   	default "256" if X86
   	default "8" if ARM && RCAR3
   	default "4" if ARM && QEMU
   	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
   	default "128" if ARM
   	help
   	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

Quoted 3 lines
Quoted 1 line

MPU cannot be used with any of the existing platforms.

That is - qemu can't emulate such an environment, i.e. even QEMU and MPU don't go together?

Qemu has support for Aarch32 MPU at EL2 and EL1 (ie R52). As far as I am aware, there is no support for Aarch64 MPU in Qemu (ie R82).

Quoted 8 lines

Even for R52, I could not get the upstream Qemu working (emulating some Arm reference platform).

I could get the Xilinx fork of Qemu (https://github.com/Xilinx/qemu)
working which emulates AMD's SoC using R52.

However, this should not impact the current patch. There is no Qemu in xen/arch/arm/platforms/*.

Aiui that's not relevant. There is a QEMU item in xen/arch/arm/platforms/Kconfig. I continue to fail to see why that couldn't be selected together with MPU. Yet if it can be, you'd end up with a default of 4, not 1, if it actually _is_ selected. Alternatively QEMU (and maybe also RCAR3 and MPSOC) need to be mutually exclusive with MPU. Hmm, looks like that's already the case, by patch 2 suppressing the "Platform Support" prompt. While that looks fragile to me, I'm sorry for the noise then.

Jan

#33 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Hi Jan,

Quoted 43 lines · Jan Beulich
Quoted 31 lines · Ayan Kumar Halder
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Ayan Kumar Halder
Quoted 21 lines · Jan Beulich
Quoted 6 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
  config NR_CPUS
  	int "Maximum number of CPUs"

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 10 lines
xen/arch/Kconfig +2 -0
+	range 1 1 if ARM && MPU
  	range 1 16383
  	default "256" if X86
  	default "8" if ARM && RCAR3
  	default "4" if ARM && QEMU
  	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
  	default "128" if ARM
  	help
  	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

Quoted 3 lines
Quoted 1 line

MPU cannot be used with any of the existing platforms.

That is - qemu can't emulate such an environment, i.e. even QEMU and MPU don't go together?

Qemu has support for Aarch32 MPU at EL2 and EL1 (ie R52). As far as I am aware, there is no support for Aarch64 MPU in Qemu (ie R82).

Quoted 8 lines

Even for R52, I could not get the upstream Qemu working (emulating some Arm reference platform).

I could get the Xilinx fork of Qemu (https://github.com/Xilinx/qemu)
working which emulates AMD's SoC using R52.

However, this should not impact the current patch. There is no Qemu in xen/arch/arm/platforms/*.

Quoted 7 lines

Aiui that's not relevant. There is a QEMU item in xen/arch/arm/platforms/Kconfig. I continue to fail to see why that couldn't be selected together with MPU. Yet if it can be, you'd end up with a default of 4, not 1, if it actually _is_ selected. Alternatively QEMU (and maybe also RCAR3 and MPSOC) need to be mutually exclusive with MPU. Hmm, looks like that's already the case, by patch 2 suppressing the "Platform Support" prompt. While that looks fragile to me, I'm sorry for the noise then.

Are you suggesting to move "default "1" if ARM && MPU” right after “default "256" if X86”?

Cheers,
Luca

#34 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Quoted 53 lines · Luca Fancellu

Hi Jan,

Quoted 43 lines · Jan Beulich
Quoted 31 lines · Ayan Kumar Halder
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Ayan Kumar Halder
Quoted 21 lines · Jan Beulich
Quoted 6 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
  config NR_CPUS
  	int "Maximum number of CPUs"

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 10 lines
xen/arch/Kconfig +2 -0
+	range 1 1 if ARM && MPU
  	range 1 16383
  	default "256" if X86
  	default "8" if ARM && RCAR3
  	default "4" if ARM && QEMU
  	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
  	default "128" if ARM
  	help
  	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

Quoted 3 lines
Quoted 1 line

MPU cannot be used with any of the existing platforms.

That is - qemu can't emulate such an environment, i.e. even QEMU and MPU don't go together?

Qemu has support for Aarch32 MPU at EL2 and EL1 (ie R52). As far as I am aware, there is no support for Aarch64 MPU in Qemu (ie R82).

Quoted 8 lines

Even for R52, I could not get the upstream Qemu working (emulating some Arm reference platform).

I could get the Xilinx fork of Qemu (https://github.com/Xilinx/qemu)
working which emulates AMD's SoC using R52.

However, this should not impact the current patch. There is no Qemu in xen/arch/arm/platforms/*.

Quoted 7 lines

Aiui that's not relevant. There is a QEMU item in xen/arch/arm/platforms/Kconfig. I continue to fail to see why that couldn't be selected together with MPU. Yet if it can be, you'd end up with a default of 4, not 1, if it actually _is_ selected. Alternatively QEMU (and maybe also RCAR3 and MPSOC) need to be mutually exclusive with MPU. Hmm, looks like that's already the case, by patch 2 suppressing the "Platform Support" prompt. While that looks fragile to me, I'm sorry for the noise then.

Quoted 1 line

Are you suggesting to move "default "1" if ARM && MPU” right after “default "256" if X86”?

Yes.

Jan

Reviewed-by
#35 lore raw

Re: [PATCH v4 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm

Quoted 58 lines · Jan Beulich
Quoted 53 lines · Luca Fancellu

Hi Jan,

Quoted 50 lines · Jan Beulich
Quoted 31 lines · Ayan Kumar Halder
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Ayan Kumar Halder
Quoted 21 lines · Jan Beulich
Quoted 6 lines · Ayan Kumar Halder
xen/arch/Kconfig +0 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32
 
 config NR_CPUS
 	int "Maximum number of CPUs"

xen/arch/Kconfig config PHYS_ADDR_T_32 · line 18

Quoted 10 lines
xen/arch/Kconfig +2 -0
+	range 1 1 if ARM && MPU
 	range 1 16383
 	default "256" if X86
 	default "8" if ARM && RCAR3
 	default "4" if ARM && QEMU
 	default "4" if ARM && MPSOC
+	default "1" if ARM && MPU
 	default "128" if ARM
 	help
 	  Controls the build-time size of various arrays and bitmaps

I'm afraid I can't easily tell whether MPU can be used together with any of RCAR3, QEMU, or MPSOC. If it can, the new default line would need to move up, as it's the first one that has a match on its condition which is being used.

Quoted 3 lines
Quoted 1 line

MPU cannot be used with any of the existing platforms.

That is - qemu can't emulate such an environment, i.e. even QEMU and MPU don't go together?

Qemu has support for Aarch32 MPU at EL2 and EL1 (ie R52). As far as I am aware, there is no support for Aarch64 MPU in Qemu (ie R82).

Quoted 8 lines

Even for R52, I could not get the upstream Qemu working (emulating some Arm reference platform).

I could get the Xilinx fork of Qemu (https://github.com/Xilinx/qemu)
working which emulates AMD's SoC using R52.

However, this should not impact the current patch. There is no Qemu in xen/arch/arm/platforms/*.

Aiui that's not relevant. There is a QEMU item in xen/arch/arm/platforms/Kconfig. I continue to fail to see why that couldn't be selected together with MPU. Yet if it can be, you'd end up with a default of 4, not 1, if it actually _is_ selected. Alternatively QEMU (and maybe also RCAR3 and MPSOC) need to be mutually exclusive with MPU. Hmm, looks like that's already the case, by patch 2 suppressing the "Platform Support" prompt. While that looks fragile to me, I'm sorry for the noise then.

Quoted 3 lines
Quoted 1 line

Are you suggesting to move "default "1" if ARM && MPU” right after “default "256" if X86”?

Yes.

Makes sense!

With that:

Quoted 2 lines

Jan

#36 lore raw
In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 6 lines · Ayan Kumar Halder

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.

Quoted 7 lines

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

Ayan Kumar Halder (6):
xen/arm: Skip initializing the BSS section when it is empty xen/arm: mpu: Introduce choice between MMU and MPU xen/arm: mpu: Define Xen start address for MPU systems

I have committed the first 3 patches. I think the others need a respin.

Cheers,

Signature · 1 line
Julien Grall