Enable early bootup of AArch64 MPU systems
v3 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.
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
SUPPORT.md | 1 + xen/arch/Kconfig | 2 + xen/arch/arm/Kconfig | 27 ++- xen/arch/arm/Makefile | 1 + xen/arch/arm/arm64/head.S | 2 + xen/arch/arm/arm64/mpu/Makefile | 1 + xen/arch/arm/arm64/mpu/head.S | 165 +++++++++++++++++++ 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/setup.c | 13 ++ xen/arch/arm/xen.lds.S | 7 + 16 files changed, 329 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/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
PATCH v3 3/6 xen/arm: mpu: Define Xen start address for MPU systems
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. 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. Fixed some of the coding style issues.
2. Reworded the help message.
3. Updated the commit message.
v2 - Added clarification for the use of page and page size.
xen/arch/arm/Kconfig | 10 ++++++++ xen/arch/arm/include/asm/config.h | 4 +++- xen/arch/arm/include/asm/mpu/layout.h | 33 +++++++++++++++++++++++++++ xen/arch/arm/setup.c | 8 +++++++ xen/arch/arm/xen.lds.S | 6 +++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 xen/arch/arm/include/asm/mpu/layout.h
xen/arch/arm/Kconfig +10 -0
@@ -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/include/asm/config.h +3 -1
@@ -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
@@ -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/setup.c +8 -0
@@ -295,6 +295,14 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)struct domain *d;int rc, i; +#ifdef CONFIG_MPU +/* +* 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 ); +#endif dcache_line_bytes =read_dcache_line_bytes ();percpu_init_areas ();
xen/arch/arm/xen.lds.S +6 -0
@@ -231,6 +231,12 @@ SECTIONS */ ASSERT(_start == XEN_VIRT_START, "_start != XEN_VIRT_START") +/* + * 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 is misaligned") + /* * We require that Xen is loaded at a page boundary, so this ensures that any * code running on the identity map cannot cross a section boundary.
Signature · 1 line
2.25.1
PATCH v3 1/6 xen/arm: Skip initializing the BSS section when it is empty
In reply to Ayan Kumar HalderIf the BSS section is empty, then the function can just return.
---
Changes from :-
v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S | 2 ++ 1 file changed, 2 insertions(+)
xen/arch/arm/arm64/head.S +2 -0
@@ -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
PATCH v3 2/6 xen/arm: mpu: Introduce choice between MMU and MPU
In reply to Ayan Kumar HalderThere 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.
---
Changes from :-
v1 - 1. Reworded the help messages.
2. Updated Support.md.
v2 - 1. Reworded the help message.
SUPPORT.md | 1 + xen/arch/arm/Kconfig | 17 ++++++++++++++++- xen/arch/arm/platforms/Kconfig | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-)
SUPPORT.md +1 -0
@@ -40,6 +40,7 @@ supported in this document. Status, Xen in AArch64 mode: Supported Status, Xen in AArch32 mode: Tech Preview + Status, Xen with MPU: 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
@@ -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
@@ -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
PATCH v3 6/6 xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm
In reply to Ayan Kumar HalderSecondary 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.
xen/arch/Kconfig | 2 ++ xen/arch/arm/arm64/mpu/head.S | 10 ++++++++++ xen/arch/arm/setup.c | 5 +++++ 3 files changed, 17 insertions(+)
xen/arch/Kconfig +2 -0
@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32 config NR_CPUS int "Maximum number of CPUs" + range 1 1 if 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
@@ -147,6 +147,16 @@ fail: b 1b 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/setup.c +5 -0
@@ -296,6 +296,11 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)int rc, i;#ifdef CONFIG_MPU +/* +* Currently, SMP is not enabled on MPU based systems. +*/ +BUILD_BUG_ON (NR_CPUS >1 ); +/* * 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
Signature · 1 line
2.25.1
PATCH v3 5/6 xen/arm: mpu: Enable MPU
In reply to Ayan Kumar HalderAfter the regions have been created, now we enable the MPU. For this we disable the background region so that the new memory map created for the regions take effect. Also, we treat all RW regions as non executable and the data cache is enabled.
---
Changes from :-
v2 - 1. Extracted from the previous patch into a new one.
2. Disabled background region.
xen/arch/arm/arm64/mpu/head.S | 29 ++++++++++++++++++-- xen/arch/arm/include/asm/arm64/mpu/sysregs.h | 3 ++ 2 files changed, 30 insertions(+), 2 deletions(-)
xen/arch/arm/arm64/mpu/head.S +27 -2
@@ -61,6 +61,30 @@ add \xb, \xb, x20 /* x20 - Phys offset */ .endm +/* + * 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 */ + dsb sy + msr SCTLR_EL2, x0 + isb + + ret +END(enable_mpu) + /* * Maps the various sections of Xen (described in xen.lds.S) as different MPU * regions.
@@ -68,10 +92,11 @@ * Inputs: * lr : Address to return to. * - * Clobbers x0 - x5 + * Clobbers x0 - x6 * */ FUNC(enable_boot_cpu_mm) + mov x6, lr /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */ mrs x5, MPUIR_EL2
@@ -113,7 +138,7 @@ FUNC(enable_boot_cpu_mm) beq 5f prepare_xen_region x0, x1, x2, x3, x4, x5 -5: +5: mov lr, x6 ret fail:
xen/arch/arm/include/asm/arm64/mpu/sysregs.h +3 -0
@@ -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
PATCH v3 4/6 xen/arm: mpu: Create boot-time MPU protection regions
In reply to Ayan Kumar HalderDefine 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 of a section is the 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 for mapping sections to MPU memory regions.
MPU specific registers are defined inxen/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.
xen/arch/arm/Makefile | 1 + xen/arch/arm/arm64/mpu/Makefile | 1 + xen/arch/arm/arm64/mpu/head.S | 130 +++++++++++++++++++ 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 + 8 files changed, 204 insertions(+) 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/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/Makefile +1 -0
@@ -1,6 +1,7 @@ obj-$(CONFIG_ARM_32) += arm32/ obj-$(CONFIG_ARM_64) += arm64/ obj-$(CONFIG_MMU) += mmu/ +obj-$(CONFIG_MPU) += mpu/ obj-$(CONFIG_ACPI) += acpi/ obj-$(CONFIG_HAS_PCI) += pci/ ifneq ($(CONFIG_NO_PLAT),y)
xen/arch/arm/arm64/mpu/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-y += head.o
xen/arch/arm/arm64/mpu/head.S new file +130 -0
130 lines not on this page · raw message
xen/arch/arm/include/asm/arm64/mpu/sysregs.h new file +27 -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 + +/* 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
@@ -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
@@ -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
@@ -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
@@ -157,6 +157,7 @@ SECTIONS *(.altinstr_replacement) } :text . = ALIGN(PAGE_SIZE); + __init_data_begin = .; .init.data : { *(.init.rodata) *(.init.rodata.*)
Signature · 1 line
2.25.1
Re: [PATCH v3 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm
In reply to Ayan Kumar Halderxen/arch/Kconfig config PHYS_ADDR_T_32 · line 9
Quoted 7 lines · Ayan Kumar Halder
xen/arch/Kconfig +1
-0@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32 config NR_CPUS int "Maximum number of CPUs" + range 1 1 if MPU
I think this "if" and ...
xen/arch/Kconfig config PHYS_ADDR_T_32 · line 15
Quoted 6 lines
xen/arch/Kconfig +1
-0range 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
... this one need to have the same condition.
Jan
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Ayan Kumar HalderQuoted 17 lines · Ayan Kumar Halder
If the BSS section is empty, then the function can just return.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S | 2 ++ 1 file changed, 2 insertions(+)xen/arch/arm/arm64/head.S +0
-0
xen/arch/arm/arm64/head.S FUNC_LOCAL(zero_bss) · line 353
Quoted 8 lines
xen/arch/arm/arm64/head.S +2
-0@@ -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
Why not just transforming the "do while" loop into a "while" one and just jump to cmp?
Something like (not tested)
xen/arch/arm/arm64/head.S +1 -0
@@ -346,9 +346,10 @@ 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) */ + b 2f
1: str xzr, [x0], #8
- cmp x0, x1 +2: cmp x0, x1
b.lo 1b
skip_bss:
Frediano
Hello Ayan,
I think that we have to mention in CHANGELOG.md that experimental support of Xen with MPU for Arm is added. ( if I understand correctly what is this patch series about ).
Thanks.
~ Oleksii
Quoted 41 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.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 dummyenable_secondary_cpu_mmSUPPORT.md | 1 + xen/arch/Kconfig | 2 + xen/arch/arm/Kconfig | 27 ++- xen/arch/arm/Makefile | 1 + xen/arch/arm/arm64/head.S | 2 + xen/arch/arm/arm64/mpu/Makefile | 1 + xen/arch/arm/arm64/mpu/head.S | 165
+++++++++++++++++++
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/setup.c | 13 ++ xen/arch/arm/xen.lds.S | 7 +16 files changed, 329 insertions(+), 3 deletions(-)create mode 100644 xen/arch/arm/arm64/mpu/Makefile create mode 100644
xen/arch/arm/arm64/mpu/head.Screate mode 100644xen/arch/arm/include/asm/arm64/mpu/sysregs.hcreate mode 100644xen/arch/arm/include/asm/mpu/arm64/mm.hcreate mode 100644xen/arch/arm/include/asm/mpu/layout.hcreate mode 100644xen/arch/arm/include/asm/mpu/mm.h
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Frediano ZiglioHi Frediano,
Appreciate your feedback.
Quoted 42 lines · Frediano Ziglio
Quoted 16 lines · Ayan Kumar Halder
If the BSS section is empty, then the function can just return.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S | 2 ++ 1 file changed, 2 insertions(+)xen/arch/arm/arm64/head.S +0
-0xen/arch/arm/arm64/head.S FUNC_LOCAL(zero_bss) · line 353
Quoted 8 lines
xen/arch/arm/arm64/head.S +2
-0@@ -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, x1Why not just transforming the "do while" loop into a "while" one and just jump to cmp?
Something like (not tested)
xen/arch/arm/arm64/head.S +1
-0@@ -346,9 +346,10 @@ 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) */ + b 2f1: str xzr, [x0], #8
Quoted 3 lines
- cmp x0, x1
+2: cmp x0, x1
b.lo 1bI am not really sure if this implementation is better than the previous one. The drawback of my implementation is that we have an extra 'cmp' instruction. The drawback of this implementation is that we need an extra label and there is an un-conditional branch after ldr (readability is difficult). May be I am biased. :)
How does this look ?
FUNC_LOCAL(zero_bss)
/* Zero BSS only when requested */
cbnz x26, skip_bss
PRINT("- Zero BSS -\r\n")
ldr x0, =__bss_start /* x0 := vaddr(__bss_start) */ ldr x1, =__bss_end /* x1 := vaddr(__bss_end) */ 1: cmp x1, x0
beq skip_bss
str xzr, [x0], #8
b 1b
skip_bss:
retEND(zero_bss)
- Ayan
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
Quoted 73 lines · Ayan Kumar Halder
Hi Frediano,
Appreciate your feedback.
Quoted 42 lines · Frediano Ziglio
Quoted 16 lines · Ayan Kumar Halder
If the BSS section is empty, then the function can just return.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S | 2 ++ 1 file changed, 2 insertions(+)xen/arch/arm/arm64/head.S +0
-0xen/arch/arm/arm64/head.S FUNC_LOCAL(zero_bss) · line 353
Quoted 8 lines
xen/arch/arm/arm64/head.S +2
-0@@ -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, x1Why not just transforming the "do while" loop into a "while" one and just jump to cmp?
Something like (not tested)
xen/arch/arm/arm64/head.S +1
-0@@ -346,9 +346,10 @@ 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) */ + b 2f1: str xzr, [x0], #8
Quoted 3 lines
- cmp x0, x1 +2: cmp x0, x1 b.lo 1bI am not really sure if this implementation is better than the previous one. The drawback of my implementation is that we have an extra 'cmp' instruction. The drawback of this implementation is that we need an extra label and there is an un-conditional branch after ldr (readability is difficult). May be I am biased. :)
How does this look ?
FUNC_LOCAL(zero_bss)/* Zero BSS only when requested */ cbnz x26, skip_bss PRINT("- Zero BSS -\r\n") ldr x0, =__bss_start /* x0 := vaddr(__bss_start) */ ldr x1, =__bss_end /* x1 := vaddr(__bss_end) */ 1: cmp x1, x0 beq skip_bss str xzr, [x0], #8 b 1b
Quoted 6 lines
skip_bss:
retEND(zero_bss)- Ayan
Not strong about. Your first version was fine too. I suppose just personal preferences, being not a hard path if you think first version was more readable fine with it. Between your 2 versions, I prefer the first. It has fewer instructions in the loop. Probably I've seen too much compiler generated code (jumping to the condition at the end is common there) that for me, it got more readable.
Frediano
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Ayan Kumar HalderQuoted 17 lines · Ayan Kumar Halder
If the BSS section is empty, then the function can just return.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S | 2 ++ 1 file changed, 2 insertions(+)xen/arch/arm/arm64/head.S +0
-0
xen/arch/arm/arm64/head.S FUNC_LOCAL(zero_bss) · line 353
Quoted 8 lines
xen/arch/arm/arm64/head.S +2
-0@@ -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
Frediano
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
In reply to Ayan Kumar HalderQuoted 86 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 of a section is the same, we skip mapping the region.
To map a region, Xen uses the
PRBAR_EL2,PRLAR_EL2andPRSELR_EL2registers. 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 for mapping sections to MPU memory regions.
MPU specific registers are defined in
xen/arch/arm/include/asm/arm64/mpu/sysregs.h.Signed-off-by Ayan Kumar Halder <[email protected]>---
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.hinmpu/head.Saspage.hincludessysregs.h. I haven't seensysregs.hincluded directly fromhead.Sormmu/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.
xen/arch/arm/Makefile | 1 + xen/arch/arm/arm64/mpu/Makefile | 1 + xen/arch/arm/arm64/mpu/head.S | 130 +++++++++++++++++++ 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 + 8 files changed, 204 insertions(+)create mode 100644 xen/arch/arm/arm64/mpu/Makefile create mode 100644
xen/arch/arm/arm64/mpu/head.Screate mode 100644xen/arch/arm/include/asm/arm64/mpu/sysregs.hcreate mode 100644xen/arch/arm/include/asm/mpu/arm64/mm.hcreate mode 100644xen/arch/arm/include/asm/mpu/mm.hxen/arch/arm/Makefile +0
-0
xen/arch/arm/Makefile line 4
Quoted 4 lines
xen/arch/arm/Makefile +1
-0@@ -1,6 +1,7 @@ obj-$(CONFIG_ARM_32) += arm32/ obj-$(CONFIG_ARM_64) += arm64/ obj-$(CONFIG_MMU) += mmu/ +obj-$(CONFIG_MPU) += mpu/
This change is incorrect. The correct change should have been in :-
xen/arch/arm/arm64/Makefile +0 -0
@@ -1,5 +1,6 @@
obj-y += lib/
obj-$(CONFIG_MMU) += mmu/
+obj-$(CONFIG_MPU) += mpu/
obj-y += cache.o
I will wait for comments from reviewers before re-spinning the patch.
- Ayan
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Ayan Kumar HalderHi Ayan,
Quoted 5 lines · Ayan Kumar Halder
If the BSS section is empty, then the function can just return.
Signed-off-by Ayan Kumar Halder <[email protected]>
Re: [PATCH v3 2/6] xen/arm: mpu: Introduce choice between MMU and MPU
In reply to Ayan Kumar HalderHi Ayan,
Quoted 7 lines · 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 (egSTATIC_MEMORY) will be selected by the MPU configuration.
Quoted 5 lines
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.
Maybe I’m wrong, but shouldn’t we add this only when MPU is supported? In this case we are just having the Kconfig setting.
Apart from that, the rest looks good to me:
Re: [PATCH v3 3/6] xen/arm: mpu: Define Xen start address for MPU systems
In reply to Ayan Kumar Halder+ Frediano for suggestion about header protection define name
xen/arch/arm/include/asm/mpu/layout.h line 4
Quoted 6 lines
+++
b/xen/arch/arm/include/asm/mpu/layout.hxen/arch/arm/include/asm/mpu/layout.h new file +4
-0@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ARM_MPU_LAYOUT_H__ +#define __ARM_MPU_LAYOUT_H__
I think we are moving away from this notation:
https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
Shall this be ASM___ARM__MPU__LAYOUT_H ? @Frediano
Quoted 11 lines
xen/arch/arm/include/asm/mpu/layout.h new file +11
-0+ +#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
xen/arch/arm/include/asm/mpu/layout.h line 25
Quoted 10 lines
xen/arch/arm/include/asm/mpu/layout.h new file +10
-0+ +/* +* 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__ */
^-- ASM___ARM__MPU__LAYOUT_H ?
Apart from that, the rest looks ok to me:
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
In reply to Ayan Kumar HalderHi Ayan,
Quoted 6 lines
xen/arch/arm/arm64/mpu/head.S new file +0
-0
xen/arch/arm/arm64/mpu/head.S line 10
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0@@ -0,0 +1,130 @@ +/* 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 */
NIT alignment
Quoted 15 lines
xen/arch/arm/arm64/mpu/head.S new file +15
-0+#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
xen/arch/arm/arm64/mpu/head.S line 35
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0+ * 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 number of regions exceeded the count specified in MPUIR_EL2 */ + add \sel, \sel, #1
I think there is an issue adding 1 here, because the very first region we are going to fill will be the 1st even if we intended the 0th. Probably moving this one at the end will fix the issue
Quoted 101 lines
xen/arch/arm/arm64/mpu/head.S new file +95
-0+ cmp \sel, \maxcount + bgt fail + + /* 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 +.endm + +/* Load the physical address of a symbol into xb */ +.macro load_paddr xb, sym + ldr \xb, =\sym + add \xb, \xb, x20 /* x20 - Phys offset */ +.endm + +/* + * 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) + + /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */ + mrs x5, MPUIR_EL2 + + /* x0: region sel */ + mov x0, xzr + /* Xen text section. */ + load_paddr x1, _stext + load_paddr x2, _etext + cmp x1, x2 + beq 1f + prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR + +1: /* Xen read-only data section. */ + load_paddr x1, _srodata + load_paddr x2, _erodata + cmp x1, x2 + beq 2f + prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR + +2: /* Xen read-only after init and data section. (RW data) */ + load_paddr x1, __ro_after_init_start + load_paddr x2, __init_begin + cmp x1, x2 + beq 3f + prepare_xen_region x0, x1, x2, x3, x4, x5 + +3: /* Xen code section. */ + load_paddr x1, __init_begin + load_paddr x2, __init_data_begin + cmp x1, x2 + beq 4f + prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR + +4: /* Xen data and BSS section. */ + load_paddr x1, __init_data_begin + load_paddr x2, __bss_end + cmp x1, x2 + beq 5f + prepare_xen_region x0, x1, x2, x3, x4, x5 + +5: + ret + +fail: + PRINT("- Number of MPU regions set in MPUIR_EL2 is too less -\r\n") + wfe + b 1b +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
xen/arch/arm/include/asm/arm64/mpu/sysregs.h line 4
Quoted 4 lines
xen/arch/arm/include/asm/arm64/mpu/sysregs.h new file +4
-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
Same comment about define name as in patch 3, here and in every new file of this patch
Cheers,
Luca
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
In reply to Ayan Kumar HalderHi Ayan,
Quoted 17 lines
/*
* Maps the various sections of Xen (described inxen.lds.S) as different MPU
* regions.xen/arch/arm/arm64/mpu/head.S +2
-1@@ -68,10 +92,11 @@ * Inputs: * lr : Address to return to. * - * Clobbers x0 - x5 + * Clobbers x0 - x6 * */ FUNC(enable_boot_cpu_mm) + mov x6, lr /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */ mrs x5, MPUIR_EL2
xen/arch/arm/arm64/mpu/head.S FUNC(enable_boot_cpu_mm) · line 141
Quoted 5 lines
xen/arch/arm/arm64/mpu/head.S +1
-1@@ -113,7 +138,7 @@ FUNC(enable_boot_cpu_mm) beq 5f prepare_xen_region x0, x1, x2, x3, x4, x5 -5: +5: mov lr, x6
Shall these changes to enable_boot_cpu_mm be part of the previous commit?
The rest looks good to me:
Re: [PATCH v3 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm
In reply to Ayan Kumar HalderHi Ayan,
Quoted 7 lines · Ayan Kumar Halder
Secondary cpus initialization is not yet supported. Thus, we print an appropriate message and put the secondary cpus in WFE state. And we introduce to
BUILD_BUG_ONto prevent users using from building Xen on multiprocessor based MPU systems.
Quoted 5 lines
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_CPUsto be 1 for MPU.Signed-off-by Ayan Kumar Halder <[email protected]>
Apart from Jan’s comment, the rest of the changes to the arch/arm part looks ok to me:
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
In reply to Luca FancelluQuoted 1 line · Luca Fancellu
Hi Ayan,
Hi Luca,
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/arm64/mpu/head.S new file +3
-0@@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Start-of-day code for an Armv8-R MPU system.
Quoted 8 lines
xen/arch/arm/arm64/mpu/head.S line 10
Quoted 7 lines
xen/arch/arm/arm64/mpu/head.S new file +7
-0+ */ + +#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 */NIT alignment
Ack
Quoted 20 lines
Quoted 20 lines
xen/arch/arm/arm64/mpu/head.S new file +19
-0+#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
Quoted 8 lines
xen/arch/arm/arm64/mpu/head.S line 35
Quoted 6 lines
xen/arch/arm/arm64/mpu/head.S new file +6
-0+ * 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 number of regions exceeded the count specified in MPUIR_EL2 */ + add \sel, \sel, #1I think there is an issue adding 1 here, because the very first region we are going to fill will be the 1st even if we intended the 0th. Probably moving this one at the end will fix the issue
We are also using 'sel' to compare against the maximum number of regions supported. So, for the first region it needs to be 1 otherwise there is a risk of comparing 0 (ie first region) with 0 (max supported regions).
May be what I can do is ...
Quoted 15 lines
Quoted 5 lines
xen/arch/arm/arm64/mpu/head.S new file +4
-0+ cmp \sel, \maxcount + bgt fail + + /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/xen/arch/arm/arm64/mpu/head.S line 49
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0+ 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 +
/* Regions should start from 0 */
sub \sel, \sel, #1
- Ayan
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
In reply to Luca FancelluQuoted 1 line · Luca Fancellu
Hi Ayan,
Hi Luca,
Sorry I missed something.
Quoted 15 lines
Quoted 15 lines
/*
* Maps the various sections of Xen (described inxen.lds.S) as different MPU
* regions.xen/arch/arm/arm64/mpu/head.S +2
-1@@ -68,10 +92,11 @@ * Inputs: * lr : Address to return to. * - * Clobbers x0 - x5 + * Clobbers x0 - x6 * */ FUNC(enable_boot_cpu_mm) + mov x6, lr
Quoted 5 lines
xen/arch/arm/arm64/mpu/head.S FUNC(enable_boot_cpu_mm) · line 139
Quoted 5 lines
xen/arch/arm/arm64/mpu/head.S +0
-0/* Check if the number of regions exceeded the count specified in MPUIR_EL2 */ mrs x5, MPUIR_EL2@@ -113,7 +138,7 @@ FUNC(enable_boot_cpu_mm) beq 5f prepare_xen_region x0, x1, x2, x3, x4, x5
bl enable_mpu
Quoted 3 lines
xen/arch/arm/arm64/mpu/head.S FUNC(enable_boot_cpu_mm) · line 141
Quoted 2 lines
xen/arch/arm/arm64/mpu/head.S +1
-1-5: +5: mov lr, x6Shall these changes to
enable_boot_cpu_mmbe part of the previous commit?
This is why I had to save and restore the LR.
- Ayan
Re: [PATCH v3 2/6] xen/arm: mpu: Introduce choice between MMU and MPU
In reply to Luca FancelluQuoted 1 line · Luca Fancellu
Hi Ayan,
Hi Luca,
Quoted 8 lines
Quoted 8 lines · 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 (egSTATIC_MEMORY) will be selected by the MPU configuration.
Quoted 7 lines
Quoted 5 lines
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.
Maybe I’m wrong, but shouldn’t we add this only when MPU is supported? In this case we are just having the
Kconfigsetting.
Actually we need this config option for the subsequent patches. I am assuming 'UNSUPPORTED' also means work in progress or partially supported.
- Ayan
Re: [PATCH v3 3/6] xen/arm: mpu: Define Xen start address for MPU systems
In reply to Luca FancelluHi,
Quoted 11 lines · Luca Fancellu
+ Frediano for suggestion about header protection define name
xen/arch/arm/include/asm/mpu/layout.h line 4
Quoted 6 lines
+++
b/xen/arch/arm/include/asm/mpu/layout.hxen/arch/arm/include/asm/mpu/layout.h new file +4
-0@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ARM_MPU_LAYOUT_H__ +#define __ARM_MPU_LAYOUT_H__I think we are moving away from this notation:
https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
Shall this beASM___ARM__MPU__LAYOUT_H? @Frediano
I could see that the patch hasn't been Rb yet and as per https://patchwork.kernel.org/project/xen-devel/cover/[email protected]/ , the new coding style is still under discussion.
May be the Arm maintainers (Stefano, Julien, Bertrand, Michal) can provide their opinion on this.
- Ayan
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
In reply to Ayan Kumar HalderHi Ayan,
Quoted 79 lines · Ayan Kumar Halder
Quoted 1 line · Luca Fancellu
Hi Ayan,
Hi Luca,
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/arm64/mpu/head.S new file +3
-0@@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Start-of-day code for an Armv8-R MPU system.Quoted 8 lines
xen/arch/arm/arm64/mpu/head.S line 10
Quoted 7 lines
xen/arch/arm/arm64/mpu/head.S new file +7
-0+ */ + +#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 */NIT alignment
Ack
Quoted 24 lines
Quoted 24 lines
xen/arch/arm/arm64/mpu/head.S new file +17
-0+#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 notspecified it will be
+ *REGION_DATA_PRBAR
+ * attr_prlar: PRLAR_EL2-related memory attributes. If not specified it will be
+ *REGION_NORMAL_PRLAR
+ */Quoted 8 lines
Quoted 6 lines
+.macro
prepare_xen_region, sel, base, limit, prbar, prlar, maxcount, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR +
+ /* Check if the number of regions exceeded the count specified inMPUIR_EL2*/
+ add \sel, \sel, #1I think there is an issue adding 1 here, because the very first region we are going to fill will be the 1st even if we intended the 0th. Probably moving this one at the end will fix the issue
We are also using 'sel' to compare against the maximum number of regions supported. So, for the first region it needs to be 1 otherwise there is a risk of comparing 0 (ie first region) with 0 (max supported regions).
May be what I can do is ...
Quoted 15 lines
Quoted 5 lines
+ cmp \sel, \maxcount
+ bgt failxen/arch/arm/arm64/mpu/head.S new file +2
-0+ + /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/xen/arch/arm/arm64/mpu/head.S line 49
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0+ 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 +/* Regions should start from 0 */
Quoted 1 line
sub \sel, \sel, #1
I didn't review the full patch yet. But couldn't we use ``bge``? This would cover "maxcount == 0" and avoid to increment and then decrement \sel.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
Quoted 1 line · Julien Grall
Hi Ayan,
Hi Julien,
Quoted 81 lines · Ayan Kumar Halder
Quoted 79 lines · Luca Fancellu
Quoted 1 line
Hi Ayan,
Hi Luca,
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/arm64/mpu/head.S new file +3
-0@@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Start-of-day code for an Armv8-R MPU system.Quoted 8 lines
xen/arch/arm/arm64/mpu/head.S line 10
Quoted 7 lines
xen/arch/arm/arm64/mpu/head.S new file +7
-0+ */ + +#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 */NIT alignment
Ack
Quoted 24 lines
Quoted 24 lines
xen/arch/arm/arm64/mpu/head.S new file +17
-0+#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 notspecified it will be
+ *REGION_DATA_PRBAR
+ * attr_prlar: PRLAR_EL2-related memory attributes. If not specified it will be
+ *REGION_NORMAL_PRLAR
+ */Quoted 8 lines
Quoted 6 lines
+.macro
prepare_xen_region, sel, base, limit, prbar, prlar, maxcount, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR +
+ /* Check if the number of regions exceeded the count specified inMPUIR_EL2*/
+ add \sel, \sel, #1I think there is an issue adding 1 here, because the very first region we are going to fill will be the 1st even if we intended the 0th. Probably moving this one at the end will fix the issue
We are also using 'sel' to compare against the maximum number of regions supported. So, for the first region it needs to be 1 otherwise there is a risk of comparing 0 (ie first region) with 0 (max supported regions).
May be what I can do is ...
Quoted 15 lines
Quoted 5 lines
+ cmp \sel, \maxcount
+ bgt failxen/arch/arm/arm64/mpu/head.S new file +2
-0+ + /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/xen/arch/arm/arm64/mpu/head.S line 49
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0+ 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 +/* Regions should start from 0 */
Quoted 3 lines
Quoted 1 line
sub \sel, \sel, #1
I didn't review the full patch yet. But couldn't we use ``bge``? This would cover "maxcount == 0" and avoid to increment and then decrement \sel.
Oh yes. My bad , I missed this.
- Ayan
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Ayan Kumar HalderHi Ayan,
Quoted 1 line · Ayan Kumar Halder
If the BSS section is empty, then the function can just return.
This is more than "can", right? If we don't do it, we will end up to zero outside of BSS. This could be critical data...
Also, I am tempted to suggest to add a Fixes tag because even if it is unlikely BSS will be zero in the current Xen, it is also not unlikely.
The tag would be:
Quoted 2 lines
Signed-off-by Ayan Kumar Halder <[email protected]>
I saw the discussion. I don't have a strong opinion on the exact approach choosen for zeroing. With the commit message updated:
Quoted 6 lines
---
Changes from :-v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S | 2 ++
Don't we need a similar change on the arm32 code?
Quoted 7 lines
1 file changed, 2 insertions(+)xen/arch/arm/arm64/head.S +0
-0
xen/arch/arm/arm64/head.S FUNC_LOCAL(zero_bss) · line 353
Quoted 8 lines
xen/arch/arm/arm64/head.S +2
-0@@ -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
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 2/6] xen/arm: mpu: Introduce choice between MMU and MPU
In reply to Luca FancelluQuoted 9 lines · Luca Fancellu
Hi Ayan,
Quoted 7 lines · 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 (egSTATIC_MEMORY) will be selected by the MPU configuration.
Quoted 7 lines
Quoted 5 lines
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.
Maybe I’m wrong, but shouldn’t we add this only when MPU is supported?
The support of the MPU will happen quite a long way down the line. We will first start with experimental (which implies the code may or may not work) and I think this is correct to do that now.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 2/6] xen/arm: mpu: Introduce choice between MMU and MPU
In reply to Ayan Kumar HalderHi,
Quoted 5 lines · 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 (egSTATIC_MEMORY) will be selected by the MPU configuration.
Quoted 7 lines
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.
Signed-off-by Ayan Kumar Halder <[email protected]>
With one remark below:
Quoted 18 lines
---
Changes from :-v1 - 1. Reworded the help messages.
2. Updated Support.md.v2 - 1. Reworded the help message.
SUPPORT.md | 1 + xen/arch/arm/Kconfig | 17 ++++++++++++++++- xen/arch/arm/platforms/Kconfig | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-)SUPPORT.md +0
-0
SUPPORT.md supported in this document. · line 43
Quoted 4 lines
SUPPORT.md +1
-0@@ -40,6 +40,7 @@ supported in this document. Status, Xen in AArch64 mode: Supported Status, Xen in AArch32 mode: Tech Preview + Status, Xen with MPU: Experimental
Maybe mention Armv8-R?
Quoted 45 lines
SUPPORT.md +0
-0Status, Cortex A57 r0p0-r1p1: Supported, not security supported Status, Cortex A77 r0p0-r1p0: Supported, not security supportedxen/arch/arm/Kconfig +16
-1@@ -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@@ -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
Julien Grall
Re: [PATCH v3 3/6] xen/arm: mpu: Define Xen start address for MPU systems
In reply to Ayan Kumar HalderQuoted 1 line · Ayan Kumar Halder
Hi,
Hi Ayan & Luca,
Quoted 13 lines · Luca Fancellu
Quoted 11 lines
+ Frediano for suggestion about header protection define name
xen/arch/arm/include/asm/mpu/layout.h line 4
Quoted 6 lines
+++
b/xen/arch/arm/include/asm/mpu/layout.hxen/arch/arm/include/asm/mpu/layout.h new file +4
-0@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ARM_MPU_LAYOUT_H__ +#define __ARM_MPU_LAYOUT_H__I think we are moving away from this notation:
https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
Shall this beASM___ARM__MPU__LAYOUT_H? @Frediano
Quoted 3 lines
I could see that the patch hasn't been Rb yet and as per https://patchwork.kernel.org/project/xen-devel/cover/[email protected]/ , the new coding style is still under discussion.
May be the Arm maintainers (Stefano, Julien, Bertrand, Michal) can provide their opinion on this.
The current guideline is been debating within among the committers. For now, please ignore the guideline.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 3/6] xen/arm: mpu: Define Xen start address for MPU systems
In reply to Ayan Kumar HalderHi,
Quoted 132 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
Kconfigoption 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. 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
0xffffffffis used as default. This is to trigger the error (on alignment check) and thereby prompt user to set the start address.Also updated
config.hso that it includesmpu/layout.hwhenCONFIG_MPUis defined.Signed-off-by Wei Chen <[email protected]>Signed-off-by Jiamei.Xie <[email protected]>Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v1 - 1. Fixed some of the coding style issues.
2. Reworded the help message.
3. Updated the commit message.v2 - Added clarification for the use of page and page size.
xen/arch/arm/Kconfig | 10 ++++++++ xen/arch/arm/include/asm/config.h | 4 +++- xen/arch/arm/include/asm/mpu/layout.h | 33 +++++++++++++++++++++++++++ xen/arch/arm/setup.c | 8 +++++++ xen/arch/arm/xen.lds.S | 6 +++++ 5 files changed, 60 insertions(+), 1 deletion(-)create mode 100644
xen/arch/arm/include/asm/mpu/layout.hxen/arch/arm/Kconfig +10
-0@@ -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" choicexen/arch/arm/include/asm/config.h +3
-1@@ -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@@ -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/setup.c +0
-0@@ -295,6 +295,14 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)struct domain *d;
xen/arch/arm/setup.c void asmlinkage __init start_xen(unsigned long fdt_paddr) · line 305
Quoted 10 lines
xen/arch/arm/setup.c +8
-0int rc, i; +#ifdef CONFIG_MPU +/* +* 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 ); +#endif
I think it would be better suited in mm.c or mpu/*.c.
Quoted 8 lines
xen/arch/arm/setup.c +0
-0dcache_line_bytes =read_dcache_line_bytes ();percpu_init_areas ();xen/arch/arm/xen.lds.S +0
-0
xen/arch/arm/xen.lds.S SECTIONS · line 239
Quoted 9 lines
xen/arch/arm/xen.lds.S +6
-0@@ -231,6 +231,12 @@ SECTIONS */ ASSERT(_start == XEN_VIRT_START, "_start != XEN_VIRT_START") +/* + * 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 is misaligned") +
Shouldn't this be protected with #ifdef CONFIG_MPU? Also, it would probably be useful to start the exact size in the error message.
xen/arch/arm/xen.lds.S SECTIONS · line 242
Quoted 3 lines
xen/arch/arm/xen.lds.S +0
-0/* * We require that Xen is loaded at a page boundary, so this ensures that any * code running on the identity map cannot cross a section boundary.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
In reply to Ayan Kumar HalderHi Ayan,
Quoted 64 lines · Ayan Kumar Halder
xen/arch/arm/arm64/mpu/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-y += head.oxen/arch/arm/arm64/mpu/head.S new file +51
-0@@ -0,0 +1,130 @@ +/* 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 number of regions exceeded the count specified in MPUIR_EL2 */ + add \sel, \sel, #1 + cmp \sel, \maxcount + bgt fail + + /* 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
xen/arch/arm/arm64/mpu/head.S line 61
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0+ msr PRBAR_EL2, \prbar + msr PRLAR_EL2, \prlar + dsb sy + isb +.endm + +/* Load the physical address of a symbol into xb */ +.macro load_paddr xb, sym + ldr \xb, =\sym + add \xb, \xb, x20 /* x20 - Phys offset */
Sorry I didn't spot this until now. Xen will be linked to a specific physical address, so why do we need to add an offset?
Quoted 5 lines
xen/arch/arm/arm64/mpu/head.S new file +5
-0+.endm + +/* + * Maps the various sections of Xen (described in xen.lds.S) as different MPU + * regions.
xen/arch/arm/arm64/mpu/head.S line 76
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0+ * + * Inputs: + * lr : Address to return to. + * + * Clobbers x0 - x5 + * + */ +FUNC(enable_boot_cpu_mm) + + /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */
AFAIU, this doesn't match what the instruction is doing below.
xen/arch/arm/arm64/mpu/head.S line 85
Quoted 9 lines
xen/arch/arm/arm64/mpu/head.S new file +9
-0+ mrs x5, MPUIR_EL2 + + /* x0: region sel */ + mov x0, xzr + /* Xen text section. */ + load_paddr x1, _stext + load_paddr x2, _etext + cmp x1, x2 + beq 1f
This check seems to be excessive... I can't think of a reason why there would be no text at all... Same for a lot of the checks below.
Quoted 24 lines
xen/arch/arm/arm64/mpu/head.S new file +24
-0+ prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR + +1: /* Xen read-only data section. */ + load_paddr x1, _srodata + load_paddr x2, _erodata + cmp x1, x2 + beq 2f + prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_RO_PRBAR + +2: /* Xen read-only after init and data section. (RW data) */ + load_paddr x1, __ro_after_init_start + load_paddr x2, __init_begin + cmp x1, x2 + beq 3f + prepare_xen_region x0, x1, x2, x3, x4, x5 + +3: /* Xen code section. */ + load_paddr x1, __init_begin + load_paddr x2, __init_data_begin + cmp x1, x2 + beq 4f + prepare_xen_region x0, x1, x2, x3, x4, x5, attr_prbar=REGION_TEXT_PRBAR + +4: /* Xen data and BSS section. */
xen/arch/arm/arm64/mpu/head.S line 119
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S new file +10
-0+ load_paddr x1, __init_data_begin + load_paddr x2, __bss_end + cmp x1, x2 + beq 5f + prepare_xen_region x0, x1, x2, x3, x4, x5 + +5: + ret + +fail:
This name is a bit too generic given you use within a macro. Also, I think it should be its own local function because the macro can be used anywhere.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
In reply to Ayan Kumar HalderHi,
Quoted 32 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.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v2 - 1. Extracted from the previous patch into a new one.
2. Disabled background region.
xen/arch/arm/arm64/mpu/head.S | 29 ++++++++++++++++++-- xen/arch/arm/include/asm/arm64/mpu/sysregs.h | 3 ++ 2 files changed, 30 insertions(+), 2 deletions(-)xen/arch/arm/arm64/mpu/head.S +7
-0@@ -61,6 +61,30 @@ add \xb, \xb, x20 /* x20 - Phys offset */ .endm +/* + * 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.
xen/arch/arm/arm64/mpu/head.S line 80
Quoted 10 lines
xen/arch/arm/arm64/mpu/head.S +10
-0+ * + * 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 */
NIT: Can't we have a single "orr" instruction to set all the flags?
xen/arch/arm/arm64/mpu/head.S line 81
Quoted 1 line
xen/arch/arm/arm64/mpu/head.S +1
-0+ dsb sy
I still question this use of "dsb sy"...
Quoted 36 lines
xen/arch/arm/arm64/mpu/head.S +9
-2+ msr SCTLR_EL2, x0 + isb + + ret +END(enable_mpu) + /* * Maps the various sections of Xen (described in xen.lds.S) as different MPU * regions.@@ -68,10 +92,11 @@ * Inputs: * lr : Address to return to. * - * Clobbers x0 - x5 + * Clobbers x0 - x6 * */ FUNC(enable_boot_cpu_mm) + mov x6, lr /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */ mrs x5, MPUIR_EL2@@ -113,7 +138,7 @@ FUNC(enable_boot_cpu_mm) beq 5f prepare_xen_region x0, x1, x2, x3, x4, x5 -5: +5: mov lr, x6 ret fail:xen/arch/arm/include/asm/arm64/mpu/sysregs.h +0
-0
xen/arch/arm/include/asm/arm64/mpu/sysregs.h line 23
Quoted 9 lines
xen/arch/arm/include/asm/arm64/mpu/sysregs.h +3
-0@@ -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 */ /*
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 6/6] xen/arm: mpu: Implement a dummy enable_secondary_cpu_mm
In reply to Ayan Kumar HalderHi Ayan,
Quoted 70 lines · Ayan Kumar Halder
Secondary cpus initialization is not yet supported. Thus, we print an appropriate message and put the secondary cpus in WFE state. And we introduce to
BUILD_BUG_ONto 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_CPUsto be 1 for MPU.Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v1 - 1.
NR_CPUSis defined as 1 for MPU2. 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.
xen/arch/Kconfig | 2 ++ xen/arch/arm/arm64/mpu/head.S | 10 ++++++++++ xen/arch/arm/setup.c | 5 +++++ 3 files changed, 17 insertions(+)xen/arch/Kconfig +2
-0@@ -6,11 +6,13 @@ config PHYS_ADDR_T_32 config NR_CPUS int "Maximum number of CPUs" + range 1 1 if 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 bitmapsxen/arch/arm/arm64/mpu/head.S +10
-0@@ -147,6 +147,16 @@ fail: b 1b 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: ASMxen/arch/arm/setup.c +0
-0
xen/arch/arm/setup.c void asmlinkage __init start_xen(unsigned long fdt_paddr) · line 302
Quoted 7 lines
xen/arch/arm/setup.c +4
-0@@ -296,6 +296,11 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)int rc, i;#ifdef CONFIG_MPU +/* +* Currently, SMP is not enabled on MPU based systems. +*/ +BUILD_BUG_ON (NR_CPUS >1 );
I think this is a odd place to add the BUILD_BUG_ON. It would better belong to smp.c (if not mpu/smpboot.c). Maybe in a build_assertions() function.
Cheers,
Signature · 1 line
Julien Grall
Quoted 1 line · On 10/10/2024 16:40, [email protected] wrote:
Hello Ayan,
Hi Oleksii,
Quoted 4 lines
I think that we have to mention in CHANGELOG.md that experimental support of Xen with MPU for Arm is added. ( if I understand correctly what is this patch series about ).
Sure, I will add this. I will send this in v4.
- Ayan
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Julien GrallQuoted 1 line · Julien Grall
Hi Ayan,
Hi Julien,
Quoted 5 lines · Ayan Kumar Halder
Quoted 1 line
If the BSS section is empty, then the function can just return.
This is more than "can", right? If we don't do it, we will end up to zero outside of BSS. This could be critical data...
s/can just/should
Quoted 6 lines
Also, I am tempted to suggest to add a Fixes tag because even if it is unlikely BSS will be zero in the current Xen, it is also not unlikely.
The tag would be:
Fixes dac84b66cc9a ("xen: arm64: initial build + config changes, start of day code")
Ack.
Quoted 7 lines
Quoted 3 lines
Signed-off-by Ayan Kumar Halder <[email protected]>I saw the discussion. I don't have a strong opinion on the exact approach choosen for zeroing. With the commit message updated:
Acked-by Julien Grall <[email protected]>
I propose that this patch to be committed. The changes to the commit message can be done on commit.
Quoted 9 lines
Quoted 7 lines
---
Changes from :-v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S| 2 ++Don't we need a similar change on the arm32 code?
I haven't looked at the arm32 code. My idea is to get the earlyboot (ie the asm part) of Xen working on R82 and then do the similar changes for R52 (ie arm32).
So that when we start plumbing the lateboot (ie start_xen() onwards), we can test it for both R82 + R52 as a significant part of the code will be common.
Let me know if this sounds ok.
- Ayan
Quoted 18 lines
Quoted 9 lines
1 file changed, 2 insertions(+)xen/arch/arm/arm64/head.S +0
-0@@ -346,6 +346,8 @@ FUNC_LOCAL(zero_bss) PRINT("- Zero BSS -\r\n")Quoted 6 lines
ldr x0, =__bss_start /* x0 :=
vaddr(__bss_start)*/ ldr x1, =__bss_end /* x1 := vaddr(__bss_end) */ + cmp x1, x0
+ beqskip_bss
1: str xzr, [x0], #8
cmp x0, x1Cheers,
Re: [PATCH v3 2/6] xen/arm: mpu: Introduce choice between MMU and MPU
In reply to Julien GrallQuoted 1 line · Julien Grall
Hi,
Hi,
Quoted 11 lines · Ayan Kumar Halder
Quoted 9 lines
There are features in the forthcoming patches which are dependent on MPU. For eg fixed start address.
Also, some of the Xen features (egSTATIC_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.
Quoted 7 lines
Quoted 3 lines
Update SUPPORT.md to state that the support for MPU is experimental.
Signed-off-by Ayan Kumar Halder <[email protected]>With one remark below:
Acked-by Julien Grall <[email protected]>
Thanks.
Quoted 14 lines
Quoted 14 lines
---
Changes from :-v1 - 1. Reworded the help messages.
2. Updated Support.md.v2 - 1. Reworded the help message.
SUPPORT.md | 1 + xen/arch/arm/Kconfig | 17 ++++++++++++++++- xen/arch/arm/platforms/Kconfig | 2 +-
3 files changed, 18 insertions(+), 2 deletions(-)
Quoted 10 lines
Quoted 8 lines
SUPPORT.md +0
-0@@ -40,6 +40,7 @@ supported in this document.Status, Xen in AArch64 mode: Supported
Status, Xen in AArch32 mode: Tech Preview
+ Status, Xen with MPU: ExperimentalMaybe mention Armv8-R?
Yes.
I propose this change be done on commit as well.
And then I can respin the next 4 patches.
- Ayan
Re: [PATCH v3 3/6] xen/arm: mpu: Define Xen start address for MPU systems
In reply to Julien GrallQuoted 1 line · Julien Grall
Hi,
Hi Julien,
Few clarifications.
Quoted 143 lines · Ayan Kumar Halder
Quoted 141 lines
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
Kconfigoption 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. 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
0xffffffffis used as default. This is to trigger the error (on alignment check) and thereby prompt user to set the start address.Also updated
config.hso that it includesmpu/layout.hwhenCONFIG_MPUis defined.Signed-off-by Wei Chen <[email protected]>Signed-off-by Jiamei.Xie <[email protected]>Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v1 - 1. Fixed some of the coding style issues.
2. Reworded the help message.
3. Updated the commit message.v2 - Added clarification for the use of page and page size.
xen/arch/arm/Kconfig | 10 ++++++++ xen/arch/arm/include/asm/config.h | 4 +++-
xen/arch/arm/include/asm/mpu/layout.h| 33 +++++++++++++++++++++++++++ xen/arch/arm/setup.c | 8 +++++++ xen/arch/arm/xen.lds.S | 6 +++++5 files changed, 60 insertions(+), 1 deletion(-)create mode 100644
xen/arch/arm/include/asm/mpu/layout.hxen/arch/arm/Kconfig +0
-0@@ -23,6 +23,16 @@ config ARCH_DEFCONFIGdefault "
arch/arm/configs/arm32_defconfig" ifARM_32default "arch/arm/configs/arm64_defconfig" ifARM_64+configXEN_START_ADDRESS
+ hex "Xen start address: keep default to use platform defined address"
+ default0xFFFFFFFF
+ depends on MPU
+ help
+ Used to set customized address at which which Xen will be linked on MPU
+ systems. Must be aligned to 4KB.
+0xFFFFFFFFis used as default value to indicate that user has not + customized this address.
+
menu "Architecture Features"
choicexen/arch/arm/include/asm/config.h +0
-0@@ -69,8 +69,10 @@ #include <xen/const.h> #include <xen/page-size.h>-#ifdef
CONFIG_MMU
+#ifdefined(CONFIG_MMU)
#include <asm/mmu/layout.h>xen/arch/arm/include/asm/config.h +2
-0+#elif defined(CONFIG_MPU ) +#include <asm/mpu/layout.h> #else
# error "Unknown memory management layout" #endifxen/arch/arm/include/asm/mpu/layout.h new file +9
-0@@ -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 aPAGE_SIZE.
+ */xen/arch/arm/include/asm/mpu/layout.h new file +6
-0+#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.
+ */xen/arch/arm/include/asm/mpu/layout.h new file +11
-0+#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/setup.c +0
-0@@ -295,6 +295,14 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr) struct domain *d; int rc, i;+#ifdef
CONFIG_MPU
+ /*
+ * Unlike MMU, MPU does not use pages for translation. However, we continue
Quoted 8 lines
Quoted 6 lines
+ * to use
PAGE_SIZEto 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);
+#endifI think it would be better suited in
mm.cor mpu/*.c.
We do not have mpu/*.c at the moment. Also, I am not sure within which function in mm.c, should this change be incorporated.
Can we take out this change from the current patch and put it in a later series when we support the lateboot of Xen (ie start_xen() onwards) for MPU ?
Quoted 12 lines
Quoted 12 lines
dcache_line_bytes=read_dcache_line_bytes();percpu_init_areas();xen/arch/arm/xen.lds.S +0
-0@@ -231,6 +231,12 @@ SECTIONS */ ASSERT(_start == XEN_VIRT_START, "_start != XEN_VIRT_START")+/*
+ * On MPU based platforms, the starting address is to be provided by
Quoted 8 lines
Quoted 6 lines
user.
+ * One need to check that it is 4KB aligned.
+ */
+ASSERT(IS_ALIGNED(_start, 4096), "starting address is misaligned")
+Shouldn't this be protected with #ifdef
CONFIG_MPU?
yes
Quoted 1 line
Also, it would probably be useful to start the exact size in the error message.
Do you mean ?
ASSERT(IS_ALIGNED(_start, 4096), "starting address should be aligned to 4KB")
- Ayan
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
In reply to Julien GrallQuoted 1 line · Julien Grall
Hi Ayan,
Hi Julien,
Just one clarification.
Quoted 74 lines · Ayan Kumar Halder
Quoted 72 lines
xen/arch/arm/arm64/mpu/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-y += head.oxen/arch/arm/arm64/mpu/head.S new file +27
-0@@ -0,0 +1,130 @@ +/* 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 notspecified it will be
+ *REGION_DATA_PRBAR
+ * attr_prlar: PRLAR_EL2-related memory attributes. If not specified it will be
+ *REGION_NORMAL_PRLAR
+ */
+.macroprepare_xen_region, sel, base, limit, prbar, prlar, maxcount, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR +
+ /* Check if the number of regions exceeded the count specified inMPUIR_EL2*/
+ add \sel, \sel, #1
+ cmp \sel, \maxcount
+ bgt failxen/arch/arm/arm64/mpu/head.S new file +18
-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
Quoted 8 lines
xen/arch/arm/arm64/mpu/head.S line 61
Quoted 6 lines
+.endm
xen/arch/arm/arm64/mpu/head.S new file +5
-0+ +/* Load the physical address of a symbol into xb */ +.macro load_paddr xb, sym + ldr \xb, =\sym + add \xb, \xb, x20 /* x20 - Phys offset */Sorry I didn't spot this until now. Xen will be linked to a specific physical address, so why do we need to add an offset?
Yes, this needs to be removed. x20 contains 0.
Quoted 12 lines
Quoted 12 lines
+.endm
xen/arch/arm/arm64/mpu/head.S new file +3
-0+ +/* + * Maps the various sections of Xen (described in xen.lds.S) asdifferent MPU
+ * regions.
+ *
+ * Inputs:
+ * lr : Address to return to.
+ *
+ * Clobbers x0 - x5
Quoted 8 lines
Quoted 6 lines
+ *
+ */xen/arch/arm/arm64/mpu/head.S new file +3
-0+FUNC(enable_boot_cpu_mm) + + /* Check if the number of regions exceeded the count specifiedin
MPUIR_EL2*/AFAIU, this doesn't match what the instruction is doing below.
Sorry, this needs to be removed.
Quoted 11 lines
Quoted 10 lines
+ mrs x5,
MPUIR_EL2
+
+ /* x0: region sel */
+ mov x0, xzr
+ /* Xen text section. */
+load_paddrx1,_stext
+load_paddrx2,_etext
+ cmp x1, x2
+ beq 1f
Quoted 1 line
This check seems to be excessive... I can't think of a reason why there would be no text at all... Same for a lot of the checks below.
Is it ok if we have this excess check ? The downsides are only a small increase in code size and boot time. Otherwise, I need to justify why we have this checks in some places, but not in others.
Quoted 32 lines
Quoted 32 lines
+
prepare_xen_regionx0, x1, x2, x3, x4, x5,
attr_prbar=REGION_TEXT_PRBAR
+
+1: /* Xen read-only data section. */
+load_paddrx1,_srodata
+load_paddrx2,_erodata
+ cmp x1, x2
+ beq 2f
+prepare_xen_regionx0, x1, x2, x3, x4, x5,
attr_prbar=REGION_RO_PRBARxen/arch/arm/arm64/mpu/head.S new file +14
-0+ +2: /* Xen read-only after init and data section. (RW data) */ + load_paddr x1, __ro_after_init_start + load_paddr x2, __init_begin + cmp x1, x2 + beq 3f + prepare_xen_region x0, x1, x2, x3, x4, x5 + +3: /* Xen code section. */ + load_paddr x1, __init_begin + load_paddr x2, __init_data_begin + cmp x1, x2 + beq 4f + prepare_xen_region x0, x1, x2, x3, x4, x5,attr_prbar=REGION_TEXT_PRBAR
+
+4: /* Xen data and BSS section. */
+load_paddrx1,__init_data_begin
+load_paddrx2,__bss_end
+ cmp x1, x2
+ beq 5f
Quoted 8 lines
xen/arch/arm/arm64/mpu/head.S line 119
Quoted 6 lines
+
prepare_xen_regionx0, x1, x2, x3, x4, x5xen/arch/arm/arm64/mpu/head.S new file +5
-0+ +5: + ret + +fail:This name is a bit too generic given you use within a macro. Also, I think it should be its own local function because the macro can be used anywhere.
Ack. I will convert this to a function.
- Ayan
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Ayan Kumar HalderHi Ayan,
Quoted 31 lines · Ayan Kumar Halder
Quoted 1 line · Julien Grall
Hi Ayan,
Hi Julien,
Quoted 5 lines · Ayan Kumar Halder
Quoted 1 line
If the BSS section is empty, then the function can just return.
This is more than "can", right? If we don't do it, we will end up to zero outside of BSS. This could be critical data...
s/can just/should
Quoted 6 lines
Also, I am tempted to suggest to add a Fixes tag because even if it is unlikely BSS will be zero in the current Xen, it is also not unlikely.
The tag would be:
Fixes dac84b66cc9a ("xen: arm64: initial build + config changes, start of day code")Ack.
Quoted 7 lines
Quoted 3 lines
Signed-off-by Ayan Kumar Halder <[email protected]>I saw the discussion. I don't have a strong opinion on the exact approach choosen for zeroing. With the commit message updated:
Acked-by Julien Grall <[email protected]>I propose that this patch to be committed. The changes to the commit message can be done on commit.
Quoted 4 lines
Quoted 4 lines
---
Changes from :-
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
v1..v2 - New patch introduced in v3.
xen/arch/arm/arm64/head.S| 2 ++Don't we need a similar change on the arm32 code?
I haven't looked at the arm32 code. My idea is to get the earlyboot (ie the asm part) of Xen working on R82 and then do the similar changes for R52 (ie arm32).
AFAIU, this change is not related to the MPU. So I would rather prefer if we keep this change in sync. I am happy to send a patch for it if you don't have time.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 2/6] xen/arm: mpu: Introduce choice between MMU and MPU
In reply to Ayan Kumar HalderHi Ayan,
Quoted 48 lines · Ayan Kumar Halder
Quoted 1 line · Julien Grall
Hi,
Hi,
Quoted 11 lines · Ayan Kumar Halder
Quoted 9 lines
There are features in the forthcoming patches which are dependent on MPU. For eg fixed start address.
Also, some of the Xen features (egSTATIC_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.
Quoted 7 lines
Quoted 3 lines
Update SUPPORT.md to state that the support for MPU is experimental.
Signed-off-by Ayan Kumar Halder <[email protected]>With one remark below:
Acked-by Julien Grall <[email protected]>Thanks.
Quoted 14 lines
Quoted 14 lines
---
Changes from :-v1 - 1. Reworded the help messages.
2. Updated Support.md.v2 - 1. Reworded the help message.
SUPPORT.md | 1 + xen/arch/arm/Kconfig | 17 ++++++++++++++++- xen/arch/arm/platforms/Kconfig | 2 +-
3 files changed, 18 insertions(+), 2 deletions(-)Quoted 10 lines
Quoted 8 lines
SUPPORT.md +0
-0@@ -40,6 +40,7 @@ supported in this document.Status, Xen in AArch64 mode: Supported
Status, Xen in AArch32 mode: Tech Preview
+ Status, Xen with MPU: ExperimentalMaybe mention Armv8-R?
Quoted 3 lines
Yes.
I propose this change be done on commit as well.
Unless Stefano wants to do, I would rather not as you are going to send a new version of the series. Same applies for patch #1.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 3/6] xen/arm: mpu: Define Xen start address for MPU systems
In reply to Ayan Kumar HalderQuoted 23 lines · Ayan Kumar Halder
Quoted 12 lines · Julien Grall
Quoted 12 lines
xen/arch/arm/setup.c +0
-0@@ -295,6 +295,14 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr) struct domain *d; int rc, i;+#ifdef
CONFIG_MPU
+ /*
+ * Unlike MMU, MPU does not use pages for translation. However, we continueQuoted 8 lines
Quoted 6 lines
+ * to use
PAGE_SIZEto 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);
+#endifI think it would be better suited in
mm.cor mpu/*.c.
Quoted 1 line
We do not have mpu/*.c at the moment.
But we are going to get some and most likely a mpu/mm.c, right? If so, I don't see why we can't create one right now. Anyway...
Quoted 1 line
Also, I am not sure within which > function in
mm.c, should this change be incorporated.
in arm/mm.c, I could create a new function:
static void __init __maybe_unused build_assertions(void)
Quoted 2 lines
Can we take out this change from the current patch and put it in a later series when we support the lateboot of Xen (ie
start_xen()onwards) for MPU ?
I would say no. The BUILD_BUG_ON() is suited here.
Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
dcache_line_bytes=read_dcache_line_bytes();percpu_init_areas();xen/arch/arm/xen.lds.S +0
-0@@ -231,6 +231,12 @@ SECTIONS */ ASSERT(_start == XEN_VIRT_START, "_start != XEN_VIRT_START")+/*
+ * On MPU based platforms, the starting address is to be provided byQuoted 8 lines
Quoted 6 lines
user.
+ * One need to check that it is 4KB aligned.
+ */
+ASSERT(IS_ALIGNED(_start, 4096), "starting address is misaligned")
+Shouldn't this be protected with #ifdef
CONFIG_MPU?
Quoted 6 lines
yes
Quoted 1 line
Also, it would probably be useful to start the exact size in the error message.
Do you mean ?
ASSERT(IS_ALIGNED(_start, 4096), "starting address should be aligned to 4KB")
Yes.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
In reply to Julien GrallQuoted 1 line · Julien Grall
Hi,
Hi Julien,
Quoted 39 lines · Ayan Kumar Halder
Quoted 37 lines
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.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v2 - 1. Extracted from the previous patch into a new one.
2. Disabled background region.
xen/arch/arm/arm64/mpu/head.S | 29 ++++++++++++++++++--
xen/arch/arm/include/asm/arm64/mpu/sysregs.h| 3 ++2 files changed, 30 insertions(+), 2 deletions(-)xen/arch/arm/arm64/mpu/head.S +0
-0@@ -61,6 +61,30 @@add \xb, \xb, x20 /* x20 - Phys offset */
.endm
+/*
+ * 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
+ *
+ */
Quoted 8 lines
Quoted 6 lines
+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 */NIT: Can't we have a single "orr" instruction to set all the flags?
Yes, I will change this.
Quoted 4 lines
Quoted 2 lines
+ dsb sy
I still question this use of "dsb sy"...
Actually I kept this to ensure that all outstanding memory access are completed before MPU is enabled.
However, prepare_xen_region() is invoked before this and it has a 'dsb sy' at the end.
So we can drop this barrier.
- Ayan
Quoted 45 lines
Quoted 39 lines
+ msr
SCTLR_EL2, x0
+ isbxen/arch/arm/arm64/mpu/head.S +4
-0+ + ret +END(enable_mpu) +/*
* Maps the various sections of Xen (described inxen.lds.S) as different MPU
* regions.xen/arch/arm/arm64/mpu/head.S +2
-1@@ -68,10 +92,11 @@ * Inputs: * lr : Address to return to. * - * Clobbers x0 - x5 + * Clobbers x0 - x6 * */ FUNC(enable_boot_cpu_mm) + mov x6, lr /* Check if the number of regions exceeded the count specified in MPUIR_EL2 */mrs x5,
MPUIR_EL2xen/arch/arm/arm64/mpu/head.S +0
-0@@ -113,7 +138,7 @@ FUNC(enable_boot_cpu_mm)beq 5f
prepare_xen_regionx0, x1, x2, x3, x4, x5
-5:
+5: mov lr, x6
ret
fail:xen/arch/arm/include/asm/arm64/mpu/sysregs.h +0
-0@@ -15,6 +15,9 @@ /* MPU Protection Region Selection Register encode */ #define PRSELR_EL2 S3_4_C6_C2_1+/* Backgroud region enable/disable */
xen/arch/arm/include/asm/arm64/mpu/sysregs.h +2
-0+#define SCTLR_ELx_BR BIT(17, UL) +Quoted 2 lines
#endif /*
__ASM_ARM_ARM64_MPU_SYSREGS_H*/
/*Cheers,
Re: [PATCH v3 4/6] xen/arm: mpu: Create boot-time MPU protection regions
In reply to Ayan Kumar HalderHi Ayan,
Quoted 34 lines · Ayan Kumar Halder
Quoted 12 lines · Julien Grall
Hi Ayan,
Quoted 11 lines
+.endm
xen/arch/arm/arm64/mpu/head.S new file +3
-0+ +/* + * Maps the various sections of Xen (described in xen.lds.S) asdifferent MPU
+ * regions.
+ *
+ * Inputs:
+ * lr : Address to return to.
+ *
+ * Clobbers x0 - x5Quoted 8 lines
Quoted 6 lines
+ *
+ */xen/arch/arm/arm64/mpu/head.S new file +3
-0+FUNC(enable_boot_cpu_mm) + + /* Check if the number of regions exceeded the count specifiedin
MPUIR_EL2*/AFAIU, this doesn't match what the instruction is doing below.
Sorry, this needs to be removed.
Quoted 11 lines
Quoted 10 lines
+ mrs x5,
MPUIR_EL2
+
+ /* x0: region sel */
+ mov x0, xzr
+ /* Xen text section. */
+load_paddrx1,_stext
+load_paddrx2,_etext
+ cmp x1, x2
+ beq 1f
Quoted 2 lines
Quoted 1 line
This check seems to be excessive... I can't think of a reason why there would be no text at all... Same for a lot of the checks below.
Is it ok if we have this excess check ? The downsides are only a small increase in code size and boot time. Otherwise, I need to justify why we have this checks in some places, but not in others.
How about moving the check in prepare_xen_region? This should solve the concerns on both the current approach and my proposed approach.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
In reply to Ayan Kumar HalderHi Ayan,
Quoted 51 lines · Ayan Kumar Halder
Quoted 1 line · Julien Grall
Hi,
Hi Julien,
Quoted 39 lines · Ayan Kumar Halder
Quoted 37 lines
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.
Signed-off-by Ayan Kumar Halder <[email protected]>---
Changes from :-v2 - 1. Extracted from the previous patch into a new one.
2. Disabled background region.
xen/arch/arm/arm64/mpu/head.S | 29 ++++++++++++++++++--
xen/arch/arm/include/asm/arm64/mpu/sysregs.h| 3 ++2 files changed, 30 insertions(+), 2 deletions(-)xen/arch/arm/arm64/mpu/head.S +0
-0@@ -61,6 +61,30 @@add \xb, \xb, x20 /* x20 - Phys offset */
.endm
+/*
+ * 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
+ *
+ */Quoted 8 lines
Quoted 6 lines
+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 */NIT: Can't we have a single "orr" instruction to set all the flags?
Quoted 7 lines
Yes, I will change this.
Quoted 4 lines
Quoted 2 lines
+ dsb sy
I still question this use of "dsb sy"...
Actually I kept this to ensure that all outstanding memory access are completed before MPU is enabled.
I don't mind to keep it for this reason so long it is documented.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 1/6] xen/arm: Skip initializing the BSS section when it is empty
In reply to Julien GrallQuoted 1 line · Julien Grall
Hi Ayan,
Hi Julien,
Quoted 35 lines · Ayan Kumar Halder
Quoted 33 lines · Julien Grall
Quoted 1 line
Hi Ayan,
Hi Julien,
Quoted 5 lines · Ayan Kumar Halder
Quoted 1 line
If the BSS section is empty, then the function can just return.
This is more than "can", right? If we don't do it, we will end up to zero outside of BSS. This could be critical data...
s/can just/should
Quoted 6 lines
Also, I am tempted to suggest to add a Fixes tag because even if it is unlikely BSS will be zero in the current Xen, it is also not unlikely.
The tag would be:
Fixes dac84b66cc9a ("xen: arm64: initial build + config changes, start of day code")Ack.
Quoted 7 lines
Quoted 3 lines
Signed-off-by Ayan Kumar Halder <[email protected]>I saw the discussion. I don't have a strong opinion on the exact approach choosen for zeroing. With the commit message updated:
Acked-by Julien Grall <[email protected]>I propose that this patch to be committed. The changes to the commit message can be done on commit.
Quoted 6 lines
Quoted 6 lines
---
Changes from :-v1..v2 - New patch introduced in v3.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
xen/arch/arm/arm64/head.S| 2 ++Don't we need a similar change on the arm32 code?
I haven't looked at the arm32 code. My idea is to get the earlyboot (ie the asm part) of Xen working on R82 and then do the similar changes for R52 (ie arm32).
AFAIU, this change is not related to the MPU. So I would rather prefer if we keep this change in sync. I am happy to send a patch for it if you don't have time.
No worries, I can do it.
I will include the arm32 changes in this patch. So that we have a single patch covering the issue for both arm64 and arm32.
- Ayan
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
In reply to Ayan Kumar HalderHi Ayan,
Quoted 13 lines
Quoted 9 lines
xen/arch/arm/arm64/mpu/head.S line 80
Quoted 7 lines
+ */
xen/arch/arm/arm64/mpu/head.S +6
-0+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 */NIT: Can't we have a single "orr" instruction to set all the flags?
Yes, I will change this.
Quoted 3 lines
Quoted 2 lines
+ dsb sy
Quoted 7 lines
Quoted 1 line
I still question this use of "dsb sy"...
Actually I kept this to ensure that all outstanding memory access are completed before MPU is enabled.
However,
prepare_xen_region()is invoked before this and it has a 'dsb sy' at the end.So we can drop this barrier.
I suggest to keep the barrier here and drop the one in prepare_xen_region instead,
have a look in my branch: https://gitlab.com/xen-project/xen/-/merge_requests/7/diffs?commit_id=f42a2816f9bd95f2f6957887be910cb9acd140b5
During my testing I was having trouble without this barrier.
Cheers,
Luca
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Quoted 17 lines · Luca Fancellu
Hi Ayan,
Quoted 15 lines
Quoted 9 lines
xen/arch/arm/arm64/mpu/head.S line 80
Quoted 7 lines
+ */
xen/arch/arm/arm64/mpu/head.S +6
-0+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 */NIT: Can't we have a single "orr" instruction to set all the flags?
Yes, I will change this.
Quoted 4 lines
Quoted 2 lines
+ dsb sy
I still question this use of "dsb sy"...
Quoted 7 lines
Quoted 5 lines
Actually I kept this to ensure that all outstanding memory access are completed before MPU is enabled.
However,
prepare_xen_region()is invoked before this and it has a 'dsb sy' at the end.So we can drop this barrier.
I suggest to keep the barrier here and drop the one in
prepare_xen_regioninstead,
I think the barriers in prepare_xen_region() should be kept because we may want to use the helper *after* the MPU is turned on.
Quoted 3 lines
have a look in my branch: https://gitlab.com/xen-project/xen/-/merge_requests/7/diffs?commit_id=f42a2816f9bd95f2f6957887be910cb9acd140b5
During my testing I was having trouble without this barrier.
Was it before or after removing the barriers in prepare_xen_region()? If the latter, then I am a bit puzzled why you need it. Did you investigate a bit more?
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Hi Julien,
Quoted 21 lines · Julien Grall
Quoted 16 lines · Luca Fancellu
Hi Ayan,
Quoted 15 lines
Quoted 9 lines
xen/arch/arm/arm64/mpu/head.S line 80
Quoted 7 lines
+ */
xen/arch/arm/arm64/mpu/head.S +6
-0+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 */NIT: Can't we have a single "orr" instruction to set all the flags?
Yes, I will change this.
Quoted 4 lines
Quoted 2 lines
+ dsb sy
I still question this use of "dsb sy"...
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Actually I kept this to ensure that all outstanding memory access are completed before MPU is enabled.
However,
prepare_xen_region()is invoked before this and it has a 'dsb sy' at the end.So we can drop this barrier.
I suggest to keep the barrier here and drop the one in
prepare_xen_regioninstead,I think the barriers in
prepare_xen_region()should be kept because we may want to use the helper *after* the MPU is turned on.
Sure I agree, given that the current code was only called before enabling the MPU I was ok to drop the barrier in prepare_xen_region, but given that the macro could be reused, it’s safer to keep them both.
Quoted 5 lines
Quoted 3 lines
have a look in my branch: https://gitlab.com/xen-project/xen/-/merge_requests/7/diffs?commit_id=f42a2816f9bd95f2f6957887be910cb9acd140b5
During my testing I was having trouble without this barrier.Was it before or after removing the barriers in
prepare_xen_region()? If the latter, then I am a bit puzzled why you need it. Did you investigate a bit more?
I don’t recall unfortunately, I tried to reproduce the issue removing the barrier from enable_mpu and adding it into prepare_xen_region only but it’s working fine or at least I’m not able to reproduce the issue I was having, of course I wouldn’t remove it from both since it goes against the arm arm, so I think the logic here should be keeping both the barriers:
1) dsb+isb barrier after writing prbar/prlar as the arm arm recommends (in case the macro is used with MPU enabled in the future)
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
Cheers,
Luca
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Quoted 11 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 6 lines
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Quoted 7 lines · Luca Fancellu
Quoted 7 lines · Julien Grall
Hi Luca,
On 22/10/2024 10:41, Luca Fancellu wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isb
Enabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Hi Julien,
Quoted 18 lines · Julien Grall
Quoted 4 lines · Luca Fancellu
Quoted 4 lines · Julien Grall
Hi Luca,
Quoted 8 lines
Quoted 5 lines · Luca Fancellu
Quoted 2 lines
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
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 */
dsb sy ^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before
this instruction complete, so we can safely turn ON the MPU and disable the background region.
msr SCTLR_EL2, x0
isb
retEND(enable_mpu)
I didn’t understand if your point is that it is not needed or if it is needed but in a different location.
Quoted 6 lines
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Hi Luca/Julien,
Quoted 28 lines · Luca Fancellu
Hi Julien,
Quoted 16 lines · Julien Grall
Quoted 11 lines · Luca Fancellu
Quoted 8 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
FUNC_LOCAL(enable_mpu)
Quoted 8 lines
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 */ dsb sy^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before
this instruction complete, so we can safely turn ON the MPU and disable the background region.
I think Julien agreed to this in a previous email as long as we have an appropriate comment. So we will keep this as it is.
- Ayan
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Quoted 39 lines · Ayan Kumar Halder
Hi Luca/Julien,
Quoted 36 lines · Luca Fancellu
Hi Julien,
Quoted 16 lines · Julien Grall
Quoted 11 lines · Luca Fancellu
Quoted 8 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
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 */
dsb sy
^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before this instruction complete, so we can safely turn ON the MPU and disable the background region.
I think
Quoted 2 lines
I think Julien agreed to this in a previous email as long as we have an appropriate comment. So we will keep this as it is.
Sorry I didn't manage to answer yet. But yes, I am ok with the barrier for now, but I am not agree on the reasoning used.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Quoted 43 lines · Julien Grall
Quoted 39 lines · Ayan Kumar Halder
Hi Luca/Julien,
Quoted 36 lines · Luca Fancellu
Hi Julien,
Quoted 16 lines · Julien Grall
Quoted 11 lines · Luca Fancellu
Quoted 8 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
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 */
dsb sy
^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before this instruction complete, so we can safely turn ON the MPU and disable the background region.
Quoted 1 line
I think
Sorry I fat fingered and pressed send too early. I think this is the part I disagree with. All explicit accesses don't need to be complete (in the sense observed by everyone in the system). They only need to have gone through the permissions check.
We also need the change in the MPU registers to be visible to the MPU.
For instance, for the MMU, we only need to use "dsb nsh" because the walker is within the non-shareable domain. Personally, I think "sy" is a bit excessive here, but as this is boot code, that's ok.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Quoted 46 lines · Julien Grall
Quoted 43 lines · Julien Grall
Quoted 39 lines · Ayan Kumar Halder
Hi Luca/Julien,
Quoted 36 lines · Luca Fancellu
Hi Julien,
Quoted 16 lines · Julien Grall
Quoted 11 lines · Luca Fancellu
Quoted 8 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
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 */
dsb sy
^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before this instruction complete, so we can safely turn ON the MPU and disable the background region.
Quoted 3 lines
Quoted 1 line
I think
Sorry I fat fingered and pressed send too early. I think this is the part I disagree with. All explicit accesses don't need to be complete (in the sense observed by everyone in the system). They only need to have gone through the permissions check.
I think I managed to find again the wording that would justify why a "dsb" is not necessary for the permission checks. From ARM DDI 0487K.a D23-7349:
``` Direct writes using the instructions in Table D22-2 require synchronization before software can rely on the effects of changes to the System registers to affect instructions appearing in program order after the direct write to the System register. Direct writes to these registers are not allowed to affect any instructions appearing in program order before the direct write. ```
I understand the paragraph as enabling the MPU via SCTLR_EL2 will not affect any instruction before hand.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Hi Julien,
Quoted 55 lines · Julien Grall
Quoted 46 lines · Julien Grall
Quoted 43 lines · Julien Grall
Quoted 39 lines · Ayan Kumar Halder
Hi Luca/Julien,
Quoted 36 lines · Luca Fancellu
Hi Julien,
Quoted 16 lines · Julien Grall
Quoted 11 lines · Luca Fancellu
Quoted 8 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
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 */
dsb sy
^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before this instruction complete, so we can safely turn ON the MPU and disable the background region.Quoted 3 lines
Quoted 1 line
I think
Sorry I fat fingered and pressed send too early. I think this is the part I disagree with. All explicit accesses don't need to be complete (in the sense observed by everyone in the system). They only need to have gone through the permissions check.
I think I managed to find again the wording that would justify why a "dsb" is not necessary for the permission checks. From ARM DDI 0487K.a D23-7349:
Quoted 8 lines
``` Direct writes using the instructions in Table D22-2 require synchronization before software can rely on the effects of changes to the System registers to affect instructions appearing in program order after the direct write to the System register. Direct writes to these registers are not allowed to affect any instructions appearing in program order before the direct write. ```I understand the paragraph as enabling the MPU via
SCTLR_EL2will not affect any instruction before hand.
Yes, I agree.
However, as the line states
"Direct writes using the instructions in Table D22-2 require synchronization before software can rely on the effects"
This means synchronization is required after the write to SCTLR_EL2.
However, this synchronization seems to imply dsb sy + isb.
FromARM DDI 0487K.a ID032224 B2-274
"A DSB instruction ordered after a direct write to a System PMU register does not complete until all observers observe the direct write"
So, a write to SCTLR_EL2 needs to be followed by a dsb to ensure the write is observed on all the processors (as SCTLR_EL2 and PMU registers belong to Table D22-2, so the behavior should be same).
And then it needs to be followed by an ISB to ensure any instruction fetch observes that MPU is enabled.
So the code needs to be
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
dsb sy /* ensure the writes to SCTLR_EL2 are observed on all the processors */isb /* any instruction fetch observes that MPU is enabled. So force flush the pre-existing instruction pipeline */
retEND(enable_mpu)
This will be similar to what Zephyr does https://github.com/zephyrproject-rtos/zephyr/blob/a30270668d4b90bac932794ef75df12a2b6f6f78/arch/arm/core/mpu/arm_mpu.c#L258 .
Let me know if you are ok with the rationale.
Also, I would prefer to have 3 orr instructions instead of one for the sake of readability. However, this is not a strong preference so if you feel otherwise, I can change to have a single orr instruction.
- Ayan
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
Hi Ayan,
Quoted 75 lines · Ayan Kumar Halder
Hi Julien,
Quoted 55 lines · Julien Grall
Quoted 46 lines · Julien Grall
Quoted 43 lines · Julien Grall
Quoted 39 lines · Ayan Kumar Halder
Hi Luca/Julien,
Quoted 36 lines · Luca Fancellu
Hi Julien,
Quoted 16 lines · Julien Grall
Quoted 11 lines · Luca Fancellu
Quoted 8 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
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 */ dsb sy^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before
this instruction complete, so we can safely turn ON the MPU and disable the background region.Quoted 3 lines
Quoted 1 line
I think
Sorry I fat fingered and pressed send too early. I think this is the part I disagree with. All explicit accesses don't need to be complete (in the sense observed by everyone in the system). They only need to have gone through the permissions check.
I think I managed to find again the wording that would justify why a "dsb" is not necessary for the permission checks. From ARM DDI 0487K.a D23-7349:
Quoted 8 lines
``` Direct writes using the instructions in Table D22-2 require synchronization before software can rely on the effects of changes to the System registers to affect instructions appearing in program order after the direct write to the System register. Direct writes to these registers are not allowed to affect any instructions appearing in program order before the direct write. ```I understand the paragraph as enabling the MPU via
SCTLR_EL2will not affect any instruction before hand.Yes, I agree.
However, as the line states
"Direct writes using the instructions in Table D22-2 require synchronization before software can rely on the effects"
Quoted 7 lines
This means synchronization is required after the write to
SCTLR_EL2.However, this synchronization seems to imply dsb sy + isb.
FromARM DDI 0487K.a ID032224 B2-274
"A DSB instruction ordered after a direct write to a System PMU register does not complete until all observers observe the direct write"
I think this is related only to the System PMU register, not to the registers in table D22-2 (which SCTLR_ELx are part)
Anyway we could discuss this in person at the Xen meet-up and write a summary in the thread later.
Cheers,
Luca
Re: [PATCH v3 5/6] xen/arm: mpu: Enable MPU
In reply to Ayan Kumar HalderHi,
Quoted 73 lines · Ayan Kumar Halder
Quoted 55 lines · Julien Grall
Quoted 46 lines · Julien Grall
Quoted 43 lines · Julien Grall
Quoted 39 lines · Ayan Kumar Halder
Hi Luca/Julien,
Quoted 36 lines · Luca Fancellu
Hi Julien,
Quoted 16 lines · Julien Grall
Quoted 11 lines · Luca Fancellu
Quoted 8 lines · Julien Grall
Hi Luca,
Quoted 2 lines · Luca Fancellu
Quoted 1 line · Julien Grall
2) dsb+isb barrier after enabling the MPU, since we are enabling the MPU but also because we are disabling the background region
TBH, I don't understand this one. Why would disabling the background region requires a dsb + isb? Do you have any pointer in the Armv8-R specification?
I’m afraid this is only my deduction, Section C1.4 of the Arm® Architecture Reference Manual Supplement Armv8, for R-profile AArch64 architecture, (DDI 0600B.a) explains what is the background region, it says it implements physical address range(s), so when we disable it, we would like any data access to complete before changing this implementation defined range with the ranges defined by us tweaking PRBAR/PRLAR, am I right?
Quoted 7 lines
My mental model for the ordering is similar to a TLB flush which is:
1/ dsb nsh
2/ tlbi
3/ dsb nsh
4/ isbEnabling the MPU is effectively 2. AFAIK, 1 is only necessary to ensure the update to the page-tables. But it is not a requirement to ensure any data access are completed (otherwise, we would have needed a dsb sy because we don't know how far the access are going...).
Uhm… I’m not sure we are on the same page, probably I explained that wrongly, so my point is that:
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 */
dsb sy
^— This data barrier is needed in order to complete any data access, which guarantees that all explicit memory accesses before this instruction complete, so we can safely turn ON the MPU and disable the background region.Quoted 3 lines
Quoted 1 line
I think
Sorry I fat fingered and pressed send too early. I think this is the part I disagree with. All explicit accesses don't need to be complete (in the sense observed by everyone in the system). They only need to have gone through the permissions check.
I think I managed to find again the wording that would justify why a "dsb" is not necessary for the permission checks. From ARM DDI 0487K.a D23-7349:
Quoted 8 lines
``` Direct writes using the instructions in Table D22-2 require synchronization before software can rely on the effects of changes to the System registers to affect instructions appearing in program order after the direct write to the System register. Direct writes to these registers are not allowed to affect any instructions appearing in program order before the direct write. ```I understand the paragraph as enabling the MPU via
SCTLR_EL2will not affect any instruction before hand.Yes, I agree.
However, as the line states
"Direct writes using the instructions in Table D22-2 require synchronization before software can rely on the effects"
This means synchronization is required after the write to
SCTLR_EL2.
Quoted 7 lines
However, this synchronization seems to imply dsb sy + isb.
FromARM DDI 0487K.a ID032224 B2-274
"A DSB instruction ordered after a direct write to a System PMU register does not complete until all observers observe the direct write"
So, a write to
SCTLR_EL2needs to be followed by a dsb to ensure the write is observed on all the processors (asSCTLR_EL2and PMU registers belong to Table D22-2, so the behavior should be same).
Aside what Luca already said, I don't understand why the write to SCTLR_EL2 needs to be seen by all the processors (which BTW would imply "ish" rather than "sy"). The MPU and SCTLR are per-processor.
Quoted 17 lines
And then it needs to be followed by an ISB to ensure any instruction fetch observes that MPU is enabled.
So the code needs to be
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 */
msrSCTLR_EL2, x0
dsb sy /* ensure the writes toSCTLR_EL2are
observed on all the processors */
isb /* any instruction fetch observes
that MPU is enabled. So force flush the pre-existing instruction pipeline */
Quoted 4 lines
ret
END(enable_mpu)This will be similar to what Zephyr does https://github.com/ zephyrproject-rtos/zephyr/blob/a30270668d4b90bac932794ef75df12a2b6f6f78/ arch/arm/core/mpu/arm_mpu.c#L258 .
I guess you mean https://github.com/zephyrproject-rtos/zephyr/blob/a30270668d4b90bac932794ef75df12a2b6f6f78/arch/arm/core/mpu/arm_mpu.c#L223? But the reasoning there is seem quite different. AFAIU The "dsb" is to ensure that the changes to the MPU registers are visible. That said, I am not convinced that the "dsb" belongs after. Even if "SCTLR_EL2" may not be fully synchronized until the next "isb", it may happen before and therefore you want the MPU registers to be visible before SCTLR_EL2 is visible.
Quoted 1 line
Let me know if you are ok with the rationale.
See above.
Quoted 2 lines
Quoted 1 line
Also, I would prefer to have 3 orr instructions instead of one for the
sake of readability. However, this is not a strong preference so if you feel otherwise, I can change to have a single orr instruction.
I am ok with that.
Cheers,
Signature · 1 line
Julien Grall