Skip to contentSkip to conversation

xen/arm: Add Armv8-R64 MPU support to Xen - Part#1

PATCH In review Penny Zheng 5 participants 122 messages Jan 13, 2023 – Jun 5, 2023 Page 1 of 2

lore
Share & tools
lore
#0 lore raw

v2 cover xen/arm: Add Armv8-R64 MPU support to Xen - Part#1

The Armv8-R architecture profile was designed to support use cases that have a high sensitivity to deterministic execution. (e.g. Fuel Injection, Brake control, Drive trains, Motor control etc)

Arm announced Armv8-R in 2013, it is the latest generation Arm architecture targeted at the Real-time profile. It introduces virtualization at the highest security level while retaining the Protected Memory System Architecture (PMSA) based on a Memory Protection Unit (MPU). In 2020, Arm announced Cortex-R82, which is the first Arm 64-bit Cortex-R processor based on Armv8-R64. The latest Armv8-R64 document can be found [1]. And the features of Armv8-R64 architecture:
- An exception model that is compatible with the Armv8-A model
- Virtualization with support for guest operating systems
- PMSA virtualization using MPUs In EL2.
- Adds support for the 64-bit A64 instruction set.
- Supports up to 48-bit physical addressing.
- Supports three Exception Levels (ELs)
- Secure EL2 - The Highest Privilege
- Secure EL1 - RichOS (MMU) or RTOS (MPU)
- Secure EL0 - Application Workloads
- Supports only a single Security state - Secure.
- MPU in EL1 & EL2 is configurable, MMU in EL1 is configurable.

These patch series are implementing the Armv8-R64 MPU support for Xen, which are based on the discussion of "Proposal for Porting Xen to Armv8-R64 - DraftC" [2].

We will implement the Armv8-R64 and MPU support in three stages:
1. Boot Xen itself to idle thread, do not create any guests on it.
2. Support to boot MPU and MMU domains on Armv8-R64 Xen.
3. SMP and other advanced features of Xen support on Armv8-R64.

As we have not implemented guest support in part#1 series of MPU support, Xen can not create any guest in boot time. So in this patch serie, we provide an extra DNM-commit in the last for users to test Xen boot to idle on MPU system.

We will split these patches to several parts, this series is the part#1, v1 is in [3], the full PoC can be found in [4]. More software for Armv8-R64 can be found in [5];

[1] https://developer.arm.com/documentation/ddi0600/latest
[2] https://lists.xenproject.org/archives/html/xen-devel/2022-05/msg00643.html
[3] https://lists.xenproject.org/archives/html/xen-devel/2022-11/msg00289.html
[4] https://gitlab.com/xen-project/people/weic/xen/-/tree/integration/mpu_v2
[5] https://armv8r64-refstack.docs.arm.com/en/v5.0/

Penny Zheng (28):
xen/mpu: build up start-of-day Xen MPU memory region map xen/mpu: introduce helpers for MPU enablement
xen/mpu: introduce unified function setup_early_uart to map early UART xen/arm64: head: Jump to the runtime mapping in enable_mm() xen/arm: introduce setup_mm_mappings
xen/mpu: plump virt/maddr/mfn convertion in MPU system xen/mpu: introduce helper access_protection_region xen/mpu: populate a new region in Xen MPU mapping table xen/mpu: plump early_fdt_map in MPU systems
xen/arm: move MMU-specific setup_mm to setup_mmu.c xen/mpu: implement MPU version of setup_mm in setup_mpu.c xen/mpu: initialize frametable in MPU system
xen/mpu: introduce "mpu,xxx-memory-section"
xen/mpu: map MPU guest memory section before static memory
initialization
xen/mpu: destroy an existing entry in Xen MPU memory mapping table xen/mpu: map device memory resource in MPU system
xen/mpu: map boot module section in MPU system
xen/mpu: introduce mpu_memory_section_contains for address range check xen/mpu: disable VMAP sub-system for MPU systems
xen/mpu: disable FIXMAP in MPU system
xen/mpu: implement MPU version of ioremap_xxx
xen/mpu: free init memory in MPU system
xen/mpu: destroy boot modules and early FDT mapping in MPU system xen/mpu: Use secure hypervisor timer for AArch64v8R xen/mpu: move MMU specific P2M code to p2m_mmu.c
xen/mpu: implement setup_virt_paging for MPU system xen/mpu: re-order xen_mpumap in arch_init_finialize xen/mpu: add Kconfig option to enable Armv8-R AArch64 support

Wei Chen (13):
xen/arm: remove xen_phys_start and xenheap_phys_end from config.h xen/arm: make ARM_EFI selectable for Arm64
xen/arm: adjust Xen TLB helpers for Armv8-R64 PMSA xen/arm: add an option to define Xen start address for Armv8-R xen/arm64: prepare for moving MMU related code from head.S xen/arm64: move MMU related code from head.S to head_mmu.S xen/arm64: add .text.idmap for Xen identity map sections xen/arm: use PA == VA for EARLY_UART_VIRTUAL_ADDRESS on Armv-8R xen/arm: decouple copy_from_paddr with FIXMAP
xen/arm: split MMU and MPU config files from config.h xen/arm: move MMU-specific memory management code to mm_mmu.c/mm_mmu.h xen/arm: check mapping status and attributes for MPU copy_from_paddr xen/mpu: make Xen boot to idle on MPU systems(DNM)

 xen/arch/arm/Kconfig                      |   44 +-
 xen/arch/arm/Makefile                     |   17 +-
 xen/arch/arm/arm64/Makefile               |    5 +
 xen/arch/arm/arm64/head.S                 |  466 +----
 xen/arch/arm/arm64/head_mmu.S             |  399 ++++
 xen/arch/arm/arm64/head_mpu.S             |  394 ++++
 xen/arch/arm/bootfdt.c                    |   13 +-
 xen/arch/arm/domain_build.c               |    4 +
 xen/arch/arm/include/asm/alternative.h    |   15 +
 xen/arch/arm/include/asm/arm64/flushtlb.h |   25 +
 xen/arch/arm/include/asm/arm64/macros.h   |   51 +
 xen/arch/arm/include/asm/arm64/mpu.h      |  174 ++
 xen/arch/arm/include/asm/arm64/sysregs.h  |   77 +
 xen/arch/arm/include/asm/config.h         |  105 +-
 xen/arch/arm/include/asm/config_mmu.h     |  112 +
 xen/arch/arm/include/asm/config_mpu.h     |   25 +
 xen/arch/arm/include/asm/cpregs.h         |    4 +-
 xen/arch/arm/include/asm/cpuerrata.h      |   12 +
 xen/arch/arm/include/asm/cpufeature.h     |    7 +
 xen/arch/arm/include/asm/early_printk.h   |   13 +
 xen/arch/arm/include/asm/fixmap.h         |   28 +-
 xen/arch/arm/include/asm/flushtlb.h       |   22 +
 xen/arch/arm/include/asm/mm.h             |   78 +-
 xen/arch/arm/include/asm/mm_mmu.h         |   77 +
 xen/arch/arm/include/asm/mm_mpu.h         |   54 +
 xen/arch/arm/include/asm/p2m.h            |   27 +-
 xen/arch/arm/include/asm/p2m_mmu.h        |   28 +
 xen/arch/arm/include/asm/processor.h      |   13 +
 xen/arch/arm/include/asm/setup.h          |   39 +
 xen/arch/arm/kernel.c                     |   31 +-
 xen/arch/arm/mm.c                         | 1340 +-----------
 xen/arch/arm/mm_mmu.c                     | 1376 +++++++++++++
 xen/arch/arm/mm_mpu.c                     | 1056 ++++++++++
 xen/arch/arm/p2m.c                        | 2282 +--------------------
 xen/arch/arm/p2m_mmu.c                    | 2257 ++++++++++++++++++++
 xen/arch/arm/p2m_mpu.c                    |  274 +++
 xen/arch/arm/platforms/Kconfig            |   16 +-
 xen/arch/arm/setup.c                      |  394 +---
 xen/arch/arm/setup_mmu.c                  |  391 ++++
 xen/arch/arm/setup_mpu.c                  |  208 ++
 xen/arch/arm/time.c                       |   14 +-
 xen/arch/arm/traps.c                      |    2 +
 xen/arch/arm/xen.lds.S                    |   10 +-
 xen/arch/x86/Kconfig                      |    1 +
 xen/common/Kconfig                        |    6 +
 xen/common/Makefile                       |    2 +-
 xen/include/xen/vmap.h                    |   93 +-
 47 files changed, 7500 insertions(+), 4581 deletions(-)
 create mode 100644 xen/arch/arm/arm64/head_mmu.S
 create mode 100644 xen/arch/arm/arm64/head_mpu.S
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
 create mode 100644 xen/arch/arm/include/asm/config_mmu.h
 create mode 100644 xen/arch/arm/include/asm/config_mpu.h
 create mode 100644 xen/arch/arm/include/asm/mm_mmu.h
 create mode 100644 xen/arch/arm/include/asm/mm_mpu.h
 create mode 100644 xen/arch/arm/include/asm/p2m_mmu.h
 create mode 100644 xen/arch/arm/mm_mmu.c
 create mode 100644 xen/arch/arm/mm_mpu.c
 create mode 100644 xen/arch/arm/p2m_mmu.c
 create mode 100644 xen/arch/arm/p2m_mpu.c
 create mode 100644 xen/arch/arm/setup_mmu.c
 create mode 100644 xen/arch/arm/setup_mpu.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/40 xen/arm: remove xen_phys_start and xenheap_phys_end from config.h

From: Wei Chen <[email protected]>

These two variables are stale variables, they only have declarations in config.h, they don't have any definition and no any code is using these two variables. So in this patch, we remove them from config.h.

---
v1 -> v2:
1. Add Ab.

---
 xen/arch/arm/include/asm/config.h | 2 --
 1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/config.h +0 -2
index 0fefed1b8a..25a625ff08 100644
@@ -172,8 +172,6 @@
 #define STACK_SIZE  (PAGE_SIZE << STACK_ORDER)
 
 #ifndef __ASSEMBLY__
-extern unsigned long xen_phys_start;
-extern unsigned long xenheap_phys_end;
 extern unsigned long frametable_virt_end;
 #endif
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 01/40] xen/arm: remove xen_phys_start and xenheap_phys_end from config.h

Hi Penny,

Quoted 8 lines · Penny Zheng

From: Wei Chen <[email protected]>

These two variables are stale variables, they only have declarations in config.h, they don't have any definition and no any code is using these two variables. So in this patch, we remove them from config.h.

I was going to commit this patch, however this technically needs your signed-off-by as the sender of this new version.

If you confirm your signed-off-by, then I can commit without a resending.

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

RE: [PATCH v2 01/40] xen/arm: remove xen_phys_start and xenheap_phys_end from config.h

Quoted 11 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Friday, January 13, 2023 6:07 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Julien Grall <[email protected]>
Subject: Re: [PATCH v2 01/40] xen/arm: remove xen_phys_start and xenheap_phys_end from config.h

Quoted 1 line

Hi Penny,

Hi Julien

Quoted 8 lines · Penny Zheng
Quoted 6 lines

From: Wei Chen <[email protected]>

These two variables are stale variables, they only have declarations in config.h, they don't have any definition and no any code is using these two variables. So in this patch, we remove them from config.h.

Quoted 8 lines
Quoted 2 lines

I was going to commit this patch, however this technically needs your signed- off-by as the sender of this new version.

If you confirm your signed-off-by, then I can commit without a resending.

Yes, I confirm, thx

Quoted 4 lines

Cheers,

--
Julien Grall

#4 lore raw

PATCH v2 2/40 xen/arm: make ARM_EFI selectable for Arm64

In reply to Penny Zheng

From: Wei Chen <[email protected]>

Currently, ARM_EFI will mandatorily selected by Arm64. Even if the user knows for sure that their images will not start in the EFI environment, they can't disable the EFI support for Arm64. This means there will be about 3K lines unused code in their images.

So in this patch, we make ARM_EFI selectable for Arm64, and based on that, we can use CONFIG_ARM_EFI to gate the EFI specific code in head.S for those images that will not be booted in EFI environment.

---
v1 -> v2:
1. New patch

---
 xen/arch/arm/Kconfig      | 10 ++++++++--
 xen/arch/arm/arm64/head.S | 15 +++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
xen/arch/arm/Kconfig +8 -2
index 239d3aed3c..ace7178c9a 100644
@@ -7,7 +7,6 @@ config ARM_64
 	def_bool y
 	depends on !ARM_32
 	select 64BIT
-	select ARM_EFI
 	select HAS_FAST_MULTIPLY
 
 config ARM
@@ -37,7 +36,14 @@ config ACPI
 	  an alternative to device tree on ARM64.
 
 config ARM_EFI
-	bool
+	bool "UEFI boot service support"
+	depends on ARM_64
+	default y
+	help
+	  This option provides support for boot services through
+	  UEFI firmware. A UEFI stub is provided to allow Xen to
+	  be booted as an EFI application. This is only useful for
+	  Xen that may run on systems that have UEFI firmware.
 
 config GICV3
 	bool "GICv3 driver"
xen/arch/arm/arm64/head.S +13 -2
index ad014716db..93f9b0b9d5 100644
@@ -22,8 +22,11 @@
 
 #include <asm/page.h>
 #include <asm/early_printk.h>
+
+#ifdef CONFIG_ARM_EFI
 #include <efi/efierr.h>
 #include <asm/arm64/efibind.h>
+#endif
 
 #define PT_PT     0xf7f /* nG=1 AF=1 SH=11 AP=01 NS=1 ATTR=111 T=1 P=1 */
 #define PT_MEM    0xf7d /* nG=1 AF=1 SH=11 AP=01 NS=1 ATTR=111 T=0 P=1 */
@@ -172,8 +175,10 @@ efi_head:
         .byte   0x52
         .byte   0x4d
         .byte   0x64
-        .long   pe_header - efi_head        /* Offset to the PE header. */
-
+#ifndef CONFIG_ARM_EFI
+        .long   0                    /* 0 means no PE header. */
+#else
+        .long   pe_header - efi_head /* Offset to the PE header. */
         /*
          * Add the PE/COFF header to the file.  The address of this header
          * is at offset 0x3c in the file, and is part of Linux "Image"
@@ -279,6 +284,8 @@ section_table:
         .short  0                /* NumberOfLineNumbers  (0 for executables) */
         .long   0xe0500020       /* Characteristics (section flags) */
         .align  5
+#endif /* CONFIG_ARM_EFI */
+
 real_start:
         /* BSS should be zeroed when booting without EFI */
         mov   x26, #0                /* x26 := skip_zero_bss */
@@ -913,6 +920,8 @@ putn:   ret
 ENTRY(lookup_processor_type)
         mov  x0, #0
         ret
+
+#ifdef CONFIG_ARM_EFI
 /*
  *  Function to transition from EFI loader in C, to Xen entry point.
  *  void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
@@ -971,6 +980,8 @@ ENTRY(efi_xen_start)
         b     real_start_efi
 ENDPROC(efi_xen_start)
 
+#endif /* CONFIG_ARM_EFI */
+
 /*
  * Local variables:
  * mode: ASM
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v2 02/40] xen/arm: make ARM_EFI selectable for Arm64

Hi Penny,

Quoted 8 lines · Penny Zheng

From: Wei Chen <[email protected]>

Currently, ARM_EFI will mandatorily selected by Arm64. Even if the user knows for sure that their images will not start in the EFI environment, they can't disable the EFI support for Arm64. This means there will be about 3K lines unused code in their images.

Quoted 6 lines

So in this patch, we make ARM_EFI selectable for Arm64, and based on that, we can use CONFIG_ARM_EFI to gate the EFI specific code in head.S for those images that will not be booted in EFI environment.

Your signed-off-by is missing.

Quoted 23 lines

---
v1 -> v2:
1. New patch

---
  xen/arch/arm/Kconfig      | 10 ++++++++--
  xen/arch/arm/arm64/head.S | 15 +++++++++++++--
  2 files changed, 21 insertions(+), 4 deletions(-)
xen/arch/arm/Kconfig +0 -1
index 239d3aed3c..ace7178c9a 100644
@@ -7,7 +7,6 @@ config ARM_64
  	def_bool y
  	depends on !ARM_32
  	select 64BIT
-	select ARM_EFI
  	select HAS_FAST_MULTIPLY
 
  config ARM
@@ -37,7 +36,14 @@ config ACPI
  	  an alternative to device tree on ARM64.
 

xen/arch/arm/Kconfig config ACPI · line 46

Quoted 10 lines
xen/arch/arm/Kconfig +8 -1
  config ARM_EFI
-	bool
+	bool "UEFI boot service support"
+	depends on ARM_64
+	default y
+	help
+	  This option provides support for boot services through
+	  UEFI firmware. A UEFI stub is provided to allow Xen to
+	  be booted as an EFI application. This is only useful for
+	  Xen that may run on systems that have UEFI firmware.

I would drop the last sentence as this is implied with the rest of the paragraph.

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

RE: [PATCH v2 02/40] xen/arm: make ARM_EFI selectable for Arm64

Hi Julien,

Quoted 52 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:09
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 02/40] xen/arm: make ARM_EFI selectable for Arm64

Hi Penny,

Quoted 8 lines · Penny Zheng

From: Wei Chen <[email protected]>

Currently, ARM_EFI will mandatorily selected by Arm64. Even if the user knows for sure that their images will not start in the EFI environment, they can't disable the EFI support for Arm64. This means there will be about 3K lines unused code in their images.

Quoted 6 lines

So in this patch, we make ARM_EFI selectable for Arm64, and based on that, we can use CONFIG_ARM_EFI to gate the EFI specific code in head.S for those images that will not be booted in EFI environment.

Your signed-off-by is missing.

Quoted 23 lines

---
v1 -> v2:
1. New patch

---
  xen/arch/arm/Kconfig      | 10 ++++++++--
  xen/arch/arm/arm64/head.S | 15 +++++++++++++--
  2 files changed, 21 insertions(+), 4 deletions(-)
xen/arch/arm/Kconfig +0 -1
index 239d3aed3c..ace7178c9a 100644
@@ -7,7 +7,6 @@ config ARM_64
  	def_bool y
  	depends on !ARM_32
  	select 64BIT
-	select ARM_EFI
  	select HAS_FAST_MULTIPLY
 
  config ARM
@@ -37,7 +36,14 @@ config ACPI
  	  an alternative to device tree on ARM64.
 
Quoted 14 lines

xen/arch/arm/Kconfig config ACPI · line 46

Quoted 10 lines
xen/arch/arm/Kconfig +8 -1
  config ARM_EFI
-	bool
+	bool "UEFI boot service support"
+	depends on ARM_64
+	default y
+	help
+	  This option provides support for boot services through
+	  UEFI firmware. A UEFI stub is provided to allow Xen to
+	  be booted as an EFI application. This is only useful for
+	  Xen that may run on systems that have UEFI firmware.

I would drop the last sentence as this is implied with the rest of the paragraph.

Ok.

Cheers,
Wei Chen

Quoted 4 lines

Cheers,

--
Julien Grall

#7 lore raw

PATCH v2 3/40 xen/arm: adjust Xen TLB helpers for Armv8-R64 PMSA

In reply to Penny Zheng

From: Wei Chen <[email protected]>

From Arm ARM Supplement of Armv8-R AArch64 (DDI 0600A) [1], section D1.6.2 TLB maintenance instructions, we know that Armv8-R AArch64 permits an implementation to cache stage 1 VMSAv8-64 and stage 2 PMSAv8-64 attributes as a common entry for the Secure EL1&0 translation regime. But for Xen itself, it's running with stage 1 PMSAv8-64 on Armv8-R AArch64. The EL2 MPU updates for stage 1 PMSAv8-64 will not be cached in TLB entries. So we don't need any TLB invalidation for Xen itself in EL2.

So in this patch, we use empty macros to stub Xen TLB helpers for MPU system (PMSA), but still keep the Guest TLB helpers. Because when a guest running in EL1 with VMSAv8-64 (MMU), guest TLB invalidation is still needed. But we need some policy to distinguish MPU and MMU guest, this will be done in guest support of Armv8-R AArch64 later.

[1] https://developer.arm.com/documentation/ddi0600/ac

---
v1 -> v2:
1. No change.

---
 xen/arch/arm/include/asm/arm64/flushtlb.h | 25 +++++++++++++++++++++++
 xen/arch/arm/include/asm/flushtlb.h       | 22 ++++++++++++++++++++
 2 files changed, 47 insertions(+)
xen/arch/arm/include/asm/arm64/flushtlb.h +25 -0
index 7c54315187..fe445f6831 100644
@@ -51,6 +51,8 @@ TLB_HELPER(flush_all_guests_tlb_local, alle1);
 /* Flush innershareable TLBs, all VMIDs, non-hypervisor mode */
 TLB_HELPER(flush_all_guests_tlb, alle1is);
 
+#ifndef CONFIG_HAS_MPU
+
 /* Flush all hypervisor mappings from the TLB of the local processor. */
 TLB_HELPER(flush_xen_tlb_local, alle2);
 
@@ -66,6 +68,29 @@ static inline void __flush_xen_tlb_one(vaddr_t va)
     asm volatile("tlbi vae2is, %0;" : : "r" (va>>PAGE_SHIFT) : "memory");
 }
 
+#else
+
+/*
+ * When Xen is running with stage 1 PMSAv8-64 on MPU systems. The EL2 MPU
+ * updates for stage1 PMSAv8-64 will not be cached in TLB entries. So we
+ * don't need any TLB invalidation for Xen itself in EL2. See Arm ARM
+ * Supplement of Armv8-R AArch64 (DDI 0600A), section D1.6.2 TLB maintenance
+ * instructions for more details.
+ */
+static inline void flush_xen_tlb_local(void)
+{
+}
+
+static inline void  __flush_xen_tlb_one_local(vaddr_t va)
+{
+}
+
+static inline void __flush_xen_tlb_one(vaddr_t va)
+{
+}
+
+#endif /* CONFIG_HAS_MPU */
+
 #endif /* __ASM_ARM_ARM64_FLUSHTLB_H__ */
 /*
  * Local variables:
xen/arch/arm/include/asm/flushtlb.h +22 -0
index 125a141975..4b8bf65281 100644
@@ -28,6 +28,7 @@ static inline void page_set_tlbflush_timestamp(struct page_info *page)
 /* Flush specified CPUs' TLBs */
 void arch_flush_tlb_mask(const cpumask_t *mask);
 
+#ifndef CONFIG_HAS_MPU
 /*
  * Flush a range of VA's hypervisor mappings from the TLB of the local
  * processor.
@@ -66,6 +67,27 @@ static inline void flush_xen_tlb_range_va(vaddr_t va,
     isb();
 }
 
+#else
+
+/*
+ * When Xen is running with stage 1 PMSAv8-64 on MPU systems. The EL2 MPU
+ * updates for stage1 PMSAv8-64 will not be cached in TLB entries. So we
+ * don't need any TLB invalidation for Xen itself in EL2. See Arm ARM
+ * Supplement of Armv8-R AArch64 (DDI 0600A), section D1.6.2 TLB maintenance
+ * instructions for more details.
+ */
+static inline void flush_xen_tlb_range_va_local(vaddr_t va,
+                                                unsigned long size)
+{
+}
+
+static inline void flush_xen_tlb_range_va(vaddr_t va,
+                                          unsigned long size)
+{
+}
+
+#endif /* CONFIG_HAS_MPU */
+
 #endif /* __ASM_ARM_FLUSHTLB_H__ */
 /*
  * Local variables:
Signature · 1 line
2.25.1
#8 lore raw

Re: [PATCH v2 03/40] xen/arm: adjust Xen TLB helpers for Armv8-R64 PMSA

Hi,

Quoted 11 lines · Penny Zheng

From: Wei Chen <[email protected]>

From Arm ARM Supplement of Armv8-R AArch64 (DDI 0600A) [1],
section D1.6.2 TLB maintenance instructions, we know that Armv8-R AArch64 permits an implementation to cache stage 1 VMSAv8-64 and stage 2 PMSAv8-64 attributes as a common entry for the Secure EL1&0 translation regime. But for Xen itself, it's running with stage 1 PMSAv8-64 on Armv8-R AArch64. The EL2 MPU updates for stage 1 PMSAv8-64 will not be cached in TLB entries. So we don't need any TLB invalidation for Xen itself in EL2.

So I understand the theory here. But I would expect that none of the common code will call any of those helpers. Therefore the #ifdef should be unnecessary.

Can you clarify if my understanding is correct?

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

RE: [PATCH v2 03/40] xen/arm: adjust Xen TLB helpers for Armv8-R64 PMSA

Hi Julien,

Quoted 25 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:17
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 03/40] xen/arm: adjust Xen TLB helpers for Armv8- R64 PMSA

Hi,

Quoted 11 lines · Penny Zheng

From: Wei Chen <[email protected]>

From Arm ARM Supplement of Armv8-R AArch64 (DDI 0600A) [1],
section D1.6.2 TLB maintenance instructions, we know that Armv8-R AArch64 permits an implementation to cache stage 1 VMSAv8-64 and stage 2 PMSAv8-64 attributes as a common entry for the Secure EL1&0 translation regime. But for Xen itself, it's running with stage 1 PMSAv8-64 on Armv8-R AArch64. The EL2 MPU updates for stage 1 PMSAv8-64 will not be cached in TLB entries. So we don't need any TLB invalidation for Xen itself in EL2.

Quoted 6 lines

So I understand the theory here. But I would expect that none of the common code will call any of those helpers. Therefore the #ifdef should be unnecessary.

Can you clarify if my understanding is correct?

Yes, you're right, after we separate common code and MMU code, these helpers will be called in MMU specific code only. We will drop this patch in next version.

Cheers,
Wei Chen

Quoted 4 lines

Cheers,

--
Julien Grall

#10 lore raw

PATCH v2 4/40 xen/arm: add an option to define Xen start address for Armv8-R

In reply to Penny Zheng

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.

In this patch we introduce one Kconfig option for users to define the default Xen start address for Armv8-R. Users can enter the address in config time, or select the tailored platform config file from arch/arm/configs.

And as we introduced Armv8-R platforms to Xen, that means the existed Arm64 platforms should not be listed in Armv8-R platform list, so we add !ARM_V8R dependency for these platforms.

---
v1 -> v2:
1. Remove the platform header fvp_baser.h.
2. Remove the default start address for fvp_baser64.
3. Remove the description of default address from commit log.
4. Change HAS_MPU to ARM_V8R for Xen start address dependency. No matter Arm-v8r board has MPU or not, it always need to specify the start address.

---
 xen/arch/arm/Kconfig           |  8 ++++++++
 xen/arch/arm/platforms/Kconfig | 16 +++++++++++++---
 2 files changed, 21 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +8 -0
index ace7178c9a..c6b6b612d1 100644
@@ -145,6 +145,14 @@ config TEE
 	  This option enables generic TEE mediators support. It allows guests
 	  to access real TEE via one of TEE mediators implemented in XEN.
 
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined address"
+	default 0
+	depends on ARM_V8R
+	help
+	  This option allows to set the customized address at which Xen will be
+	  linked on MPU systems. This address must be aligned to a page size.
+
 source "arch/arm/tee/Kconfig"
 
 config STATIC_SHM
xen/arch/arm/platforms/Kconfig +13 -3
index c93a6b2756..0904793a0b 100644
@@ -1,6 +1,7 @@
 choice
 	prompt "Platform Support"
 	default ALL_PLAT
+	default FVP_BASER if ARM_V8R
 	---help---
 	Choose which hardware platform to enable in Xen.
 
@@ -8,13 +9,14 @@ choice
 
 config ALL_PLAT
 	bool "All Platforms"
+	depends on !ARM_V8R
 	---help---
 	Enable support for all available hardware platforms. It doesn't
 	automatically select any of the related drivers.
 
 config QEMU
 	bool "QEMU aarch virt machine support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
 	select GICV3
 	select HAS_PL011
 	---help---
@@ -23,7 +25,7 @@ config QEMU
 
 config RCAR3
 	bool "Renesas RCar3 support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
 	select HAS_SCIF
 	select IPMMU_VMSA
 	---help---
@@ -31,14 +33,22 @@ config RCAR3
 
 config MPSOC
 	bool "Xilinx Ultrascale+ MPSoC support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
 	select HAS_CADENCE_UART
 	select ARM_SMMU
 	---help---
 	Enable all the required drivers for Xilinx Ultrascale+ MPSoC
 
+config FVP_BASER
+	bool "Fixed Virtual Platform BaseR support"
+	depends on ARM_V8R
+	help
+	  Enable platform specific configurations for Fixed Virtual
+	  Platform BaseR
+
 config NO_PLAT
 	bool "No Platforms"
+	depends on !ARM_V8R
 	---help---
 	Do not enable specific support for any platform.
 
Signature · 1 line
2.25.1
#11 lore raw

Re: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi Penny,

Quoted 17 lines · Penny Zheng

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.

In this patch we introduce one Kconfig option for users to define the default Xen start address for Armv8-R. Users can enter the address in config time, or select the tailored platform config file from arch/arm/configs.

Quoted 6 lines

And as we introduced Armv8-R platforms to Xen, that means the existed Arm64 platforms should not be listed in Armv8-R platform list, so we add !ARM_V8R dependency for these platforms.

Your signed-off-by is missing.

Quoted 8 lines

---
v1 -> v2:
1. Remove the platform header fvp_baser.h.
2. Remove the default start address for fvp_baser64.
3. Remove the description of default address from commit log.
4. Change HAS_MPU to ARM_V8R for Xen start address dependency. No matter Arm-v8r board has MPU or not, it always need to specify the start address.

I don't quite understand the last sentence. Are you saying that it is possible to have an ARMv8-R system with an MPU nor a page-table?

Quoted 10 lines
---
  xen/arch/arm/Kconfig           |  8 ++++++++
  xen/arch/arm/platforms/Kconfig | 16 +++++++++++++---
  2 files changed, 21 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index ace7178c9a..c6b6b612d1 100644

xen/arch/arm/Kconfig config TEE · line 151

Quoted 7 lines
xen/arch/arm/Kconfig +4 -0
@@ -145,6 +145,14 @@ config TEE
  	  This option enables generic TEE mediators support. It allows guests
  	  to access real TEE via one of TEE mediators implemented in XEN.
 
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined address"
+	default 0
+	depends on ARM_V8R

It is still pretty unclear to me what would be the difference between HAS_MPU and ARM_V8R.

Quoted 51 lines
xen/arch/arm/Kconfig +4 -0
+	help
+	  This option allows to set the customized address at which Xen will be
+	  linked on MPU systems. This address must be aligned to a page size.
+
  source "arch/arm/tee/Kconfig"
 
  config STATIC_SHM
xen/arch/arm/platforms/Kconfig +5 -3
index c93a6b2756..0904793a0b 100644
@@ -1,6 +1,7 @@
  choice
  	prompt "Platform Support"
  	default ALL_PLAT
+	default FVP_BASER if ARM_V8R
  	---help---
  	Choose which hardware platform to enable in Xen.
 
@@ -8,13 +9,14 @@ choice
 
  config ALL_PLAT
  	bool "All Platforms"
+	depends on !ARM_V8R
  	---help---
  	Enable support for all available hardware platforms. It doesn't
  	automatically select any of the related drivers.
 
  config QEMU
  	bool "QEMU aarch virt machine support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
  	select GICV3
  	select HAS_PL011
  	---help---
@@ -23,7 +25,7 @@ config QEMU
 
  config RCAR3
  	bool "Renesas RCar3 support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
  	select HAS_SCIF
  	select IPMMU_VMSA
  	---help---
@@ -31,14 +33,22 @@ config RCAR3
 
  config MPSOC
  	bool "Xilinx Ultrascale+ MPSoC support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
  	select HAS_CADENCE_UART

xen/arch/arm/platforms/Kconfig config RCAR3 · line 47

Quoted 10 lines
xen/arch/arm/platforms/Kconfig +6 -0
  	select ARM_SMMU
  	---help---
  	Enable all the required drivers for Xilinx Ultrascale+ MPSoC
 
+config FVP_BASER
+	bool "Fixed Virtual Platform BaseR support"
+	depends on ARM_V8R
+	help
+	  Enable platform specific configurations for Fixed Virtual
+	  Platform BaseR

This seems unrelated to this patch.

xen/arch/arm/platforms/Kconfig config RCAR3 · line 54

Quoted 7 lines
xen/arch/arm/platforms/Kconfig +2 -0
+
  config NO_PLAT
  	bool "No Platforms"
+	depends on !ARM_V8R
  	---help---
  	Do not enable specific support for any platform.
 

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

RE: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi Julien,

Quoted 49 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:24
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Jiamei Xie <[email protected]>
Subject: Re: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi Penny,

Quoted 17 lines · Penny Zheng

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.

In this patch we introduce one Kconfig option for users to define the default Xen start address for Armv8-R. Users can enter the address in config time, or select the tailored platform config file from arch/arm/configs.

Quoted 6 lines

And as we introduced Armv8-R platforms to Xen, that means the existed Arm64 platforms should not be listed in Armv8-R platform list, so we add !ARM_V8R dependency for these platforms.

Your signed-off-by is missing.

Quoted 8 lines

---
v1 -> v2:
1. Remove the platform header fvp_baser.h.
2. Remove the default start address for fvp_baser64.
3. Remove the description of default address from commit log.
4. Change HAS_MPU to ARM_V8R for Xen start address dependency. No matter Arm-v8r board has MPU or not, it always need to specify the start address.

Quoted 3 lines

I don't quite understand the last sentence. Are you saying that it is possible to have an ARMv8-R system with an MPU nor a page-table?

Yes, from the Cortex-R82 page [1], you can see the MPU is optional in EL1 and EL2:
"Two optional and programmable MPUs controlled from EL1 and EL2 respectively."

Although it is unlikely that vendors using the Armv8-R IP will do so, it is indeed an option. In the ID register, there are also related bits in ID_AA64MMFR0_EL1 (MSA_frac) to indicate this.

Quoted 14 lines

xen/arch/arm/Kconfig config TEE · line 145

Quoted 11 lines
---
  xen/arch/arm/Kconfig           |  8 ++++++++
  xen/arch/arm/platforms/Kconfig | 16 +++++++++++++---
  2 files changed, 21 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index ace7178c9a..c6b6b612d1 100644
@@ -145,6 +145,14 @@ config TEE
  	  This option enables generic TEE mediators support. It allows

guests

Quoted 2 lines
xen/arch/arm/Kconfig +0 -0
  	  to access real TEE via one of TEE mediators implemented in XEN.
 
Quoted 9 lines

xen/arch/arm/Kconfig config TEE · line 149

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined

address"

xen/arch/arm/Kconfig config TEE · line 151

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+	default 0
+	depends on ARM_V8R

It is still pretty unclear to me what would be the difference between HAS_MPU and ARM_V8R.

If we don't want to support non-MPU supported Armv8-R, I think they are the same. IMO, non-MPU supported Armv8-R is meaningless to Xen.

Quoted 57 lines

xen/arch/arm/Kconfig config TEE · line 153

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+	help
+	  This option allows to set the customized address at which Xen will

be

Review

Quoted 6 lines
xen/arch/arm/Kconfig +2 -0
+	  linked on MPU systems. This address must be aligned to a page size.
+
  source "arch/arm/tee/Kconfig"
 
  config STATIC_SHM
(quoted diff) +0 -0

b/xen/arch/arm/platforms/Kconfig

Quoted 47 lines

index c93a6b2756..0904793a0b 100644

xen/arch/arm/platforms/Kconfig +5 -3
@@ -1,6 +1,7 @@
  choice
  	prompt "Platform Support"
  	default ALL_PLAT
+	default FVP_BASER if ARM_V8R
  	---help---
  	Choose which hardware platform to enable in Xen.
 
@@ -8,13 +9,14 @@ choice
 
  config ALL_PLAT
  	bool "All Platforms"
+	depends on !ARM_V8R
  	---help---
  	Enable support for all available hardware platforms. It doesn't
  	automatically select any of the related drivers.
 
  config QEMU
  	bool "QEMU aarch virt machine support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
  	select GICV3
  	select HAS_PL011
  	---help---
@@ -23,7 +25,7 @@ config QEMU
 
  config RCAR3
  	bool "Renesas RCar3 support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
  	select HAS_SCIF
  	select IPMMU_VMSA
  	---help---
@@ -31,14 +33,22 @@ config RCAR3
 
  config MPSOC
  	bool "Xilinx Ultrascale+ MPSoC support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
  	select HAS_CADENCE_UART
  	select ARM_SMMU
  	---help---
  	Enable all the required drivers for Xilinx Ultrascale+ MPSoC
 
Quoted 9 lines

xen/arch/arm/platforms/Kconfig config RCAR3 · line 47

Quoted 6 lines
xen/arch/arm/platforms/Kconfig +6 -0
+config FVP_BASER
+	bool "Fixed Virtual Platform BaseR support"
+	depends on ARM_V8R
+	help
+	  Enable platform specific configurations for Fixed Virtual
+	  Platform BaseR

This seems unrelated to this patch.

Can we add some descriptions in commit log for this change, or we Should move it to a new patch? We had preferred to use separate patches for this kind of changes, but we found the number of patches would become more and more. This problem has been bothering us for organizing patches.

[1] https://developer.arm.com/Processors/Cortex-R82

Cheers,
Wei Chen

Quoted 12 lines

xen/arch/arm/platforms/Kconfig config RCAR3 · line 54

Quoted 7 lines
xen/arch/arm/platforms/Kconfig +2 -0
+
  config NO_PLAT
  	bool "No Platforms"
+	depends on !ARM_V8R
  	---help---
  	Do not enable specific support for any platform.
 

Cheers,

--
Julien Grall

#13 lore raw

Re: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Quoted 1 line · Wei Chen

Hi Julien,

Hi Wei,

Quoted 49 lines
Quoted 49 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:24
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Jiamei Xie <[email protected]>
Subject: Re: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi Penny,

Quoted 17 lines · Penny Zheng

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.

In this patch we introduce one Kconfig option for users to define the default Xen start address for Armv8-R. Users can enter the address in config time, or select the tailored platform config file from arch/arm/configs.

Quoted 6 lines

And as we introduced Armv8-R platforms to Xen, that means the existed Arm64 platforms should not be listed in Armv8-R platform list, so we add !ARM_V8R dependency for these platforms.

Your signed-off-by is missing.

Quoted 8 lines

---
v1 -> v2:
1. Remove the platform header fvp_baser.h.
2. Remove the default start address for fvp_baser64.
3. Remove the description of default address from commit log.
4. Change HAS_MPU to ARM_V8R for Xen start address dependency. No matter Arm-v8r board has MPU or not, it always need to specify the start address.

Quoted 7 lines
Quoted 3 lines

I don't quite understand the last sentence. Are you saying that it is possible to have an ARMv8-R system with an MPU nor a page-table?

Yes, from the Cortex-R82 page [1], you can see the MPU is optional in EL1 and EL2:
"Two optional and programmable MPUs controlled from EL1 and EL2 respectively."

Would this mean a vendor may provide their custom solution to protect the memory?

Quoted 29 lines

Although it is unlikely that vendors using the Armv8-R IP will do so, it is indeed an option. In the ID register, there are also related bits in ID_AA64MMFR0_EL1 (MSA_frac) to indicate this.

Quoted 14 lines

xen/arch/arm/Kconfig config TEE · line 145

Quoted 11 lines
---
   xen/arch/arm/Kconfig           |  8 ++++++++
   xen/arch/arm/platforms/Kconfig | 16 +++++++++++++---
   2 files changed, 21 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index ace7178c9a..c6b6b612d1 100644
@@ -145,6 +145,14 @@ config TEE
   	  This option enables generic TEE mediators support. It allows

guests

Quoted 2 lines
xen/arch/arm/Kconfig +0 -0
   	  to access real TEE via one of TEE mediators implemented in XEN.
 
Quoted 9 lines

xen/arch/arm/Kconfig config TEE · line 149

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined

address"

xen/arch/arm/Kconfig config TEE · line 151

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+	default 0
+	depends on ARM_V8R

It is still pretty unclear to me what would be the difference between HAS_MPU and ARM_V8R.

Quoted 2 lines

If we don't want to support non-MPU supported Armv8-R, I think they are the same. IMO, non-MPU supported Armv8-R is meaningless to Xen.

OOI, why do you think this is meaningless?

Quoted 68 lines
Quoted 58 lines

xen/arch/arm/Kconfig config TEE · line 153

Quoted 3 lines
xen/arch/arm/Kconfig +2 -0
+	help
+	  This option allows to set the customized address at which Xen will

be

Review

Quoted 6 lines
xen/arch/arm/Kconfig +2 -0
+	  linked on MPU systems. This address must be aligned to a page size.
+
   source "arch/arm/tee/Kconfig"
 
   config STATIC_SHM
(quoted diff) +0 -0

b/xen/arch/arm/platforms/Kconfig

Quoted 47 lines

index c93a6b2756..0904793a0b 100644

xen/arch/arm/platforms/Kconfig +5 -3
@@ -1,6 +1,7 @@
   choice
   	prompt "Platform Support"
   	default ALL_PLAT
+	default FVP_BASER if ARM_V8R
   	---help---
   	Choose which hardware platform to enable in Xen.
 
@@ -8,13 +9,14 @@ choice
 
   config ALL_PLAT
   	bool "All Platforms"
+	depends on !ARM_V8R
   	---help---
   	Enable support for all available hardware platforms. It doesn't
   	automatically select any of the related drivers.
 
   config QEMU
   	bool "QEMU aarch virt machine support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
   	select GICV3
   	select HAS_PL011
   	---help---
@@ -23,7 +25,7 @@ config QEMU
 
   config RCAR3
   	bool "Renesas RCar3 support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
   	select HAS_SCIF
   	select IPMMU_VMSA
   	---help---
@@ -31,14 +33,22 @@ config RCAR3
 
   config MPSOC
   	bool "Xilinx Ultrascale+ MPSoC support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
   	select HAS_CADENCE_UART
   	select ARM_SMMU
   	---help---
   	Enable all the required drivers for Xilinx Ultrascale+ MPSoC
 
Quoted 9 lines

xen/arch/arm/platforms/Kconfig config RCAR3 · line 47

Quoted 6 lines
xen/arch/arm/platforms/Kconfig +6 -0
+config FVP_BASER
+	bool "Fixed Virtual Platform BaseR support"
+	depends on ARM_V8R
+	help
+	  Enable platform specific configurations for Fixed Virtual
+	  Platform BaseR

This seems unrelated to this patch.

Quoted 2 lines

Can we add some descriptions in commit log for this change, or we Should move it to a new patch?

New patch please or introduce it in the patch where you need it.

We had preferred to use separate

Quoted 3 lines

patches for this kind of changes, but we found the number of patches would become more and more. This problem has been bothering us for organizing patches.

I understand the concern of increasing the number of patches. However, this also needs to weight against the review.

In this case, it is very difficult for me to understand why we need to introduce FVP_BASER.

In fact, on the previous version, we discussed to not introduce any new platform specific config. So I am a bit surprised this is actually needed.

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

RE: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi Julien,

Quoted 72 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 17:44
To: Wei Chen <[email protected]>; Penny Zheng <[email protected]>; xen- [email protected]
Cc: Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Jiamei Xie <[email protected]>
Subject: Re: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Quoted 1 line · Wei Chen

Hi Julien,

Hi Wei,

Quoted 9 lines
Quoted 9 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:24
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Jiamei Xie <[email protected]>
Subject: Re: [PATCH v2 04/40] xen/arm: add an option to define Xen

start

Quoted 44 lines
Quoted 43 lines

address for Armv8-R

Hi Penny,

Quoted 17 lines · Penny Zheng

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.

In this patch we introduce one Kconfig option for users to define the default Xen start address for Armv8-R. Users can enter the address in config time, or select the tailored platform config file from arch/arm/configs.

Quoted 6 lines

And as we introduced Armv8-R platforms to Xen, that means the existed Arm64 platforms should not be listed in Armv8-R platform list, so we add !ARM_V8R dependency for these platforms.

Your signed-off-by is missing.

Quoted 8 lines

---
v1 -> v2:
1. Remove the platform header fvp_baser.h.
2. Remove the default start address for fvp_baser64.
3. Remove the description of default address from commit log.
4. Change HAS_MPU to ARM_V8R for Xen start address dependency. No matter Arm-v8r board has MPU or not, it always need to specify the start address.

I don't quite understand the last sentence. Are you saying that it is possible to have an ARMv8-R system with an MPU nor a page-table?

Quoted 8 lines
Quoted 1 line

Yes, from the Cortex-R82 page [1], you can see the MPU is optional in

EL1

Quoted 2 lines

and EL2:
"Two optional and programmable MPUs controlled from EL1 and EL2

respectively."
Would this mean a vendor may provide their custom solution to protect the memory?

Ah, you gave me a new idea, yes in the "ARM DDI 0600A.c G1.3.7" MSA_frac of ID_AA64MMFR0_EL1 says:
0b0000 PMSAv8-64 not supported in any translation regime. 0b0000 is not permitted value.

So maybe you're right, on Armv8-R64, we always have MPU in EL1&EL2, the optional is for MPU customization.

Quoted 30 lines
Quoted 18 lines

Although it is unlikely that vendors using the Armv8-R IP will do so, it is indeed an option. In the ID register, there are also related bits in ID_AA64MMFR0_EL1 (MSA_frac) to indicate this.

Quoted 13 lines

xen/arch/arm/Kconfig config TEE · line 145

Quoted 11 lines
---
   xen/arch/arm/Kconfig           |  8 ++++++++
   xen/arch/arm/platforms/Kconfig | 16 +++++++++++++---
   2 files changed, 21 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index ace7178c9a..c6b6b612d1 100644
@@ -145,6 +145,14 @@ config TEE
   	  This option enables generic TEE mediators support. It allows

guests

Quoted 1 line
xen/arch/arm/Kconfig +0 -0
   	  to access real TEE via one of TEE mediators implemented in

XEN.

Quoted 11 lines
Quoted 10 lines

xen/arch/arm/Kconfig config TEE · line 148

Quoted 3 lines
xen/arch/arm/Kconfig +2 -0
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined

address"

xen/arch/arm/Kconfig config TEE · line 151

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+	default 0
+	depends on ARM_V8R

It is still pretty unclear to me what would be the difference between HAS_MPU and ARM_V8R.

Quoted 4 lines
Quoted 1 line

If we don't want to support non-MPU supported Armv8-R, I think they are

the

Quoted 1 line

same. IMO, non-MPU supported Armv8-R is meaningless to Xen.

OOI, why do you think this is meaningless?

If there is Armv8-R board without EL2 MPU, how can we protect Xen? Of course, if users don't care about security, Xen still can support it.

Quoted 75 lines
Quoted 23 lines
Quoted 23 lines

xen/arch/arm/Kconfig config TEE · line 152

Quoted 4 lines
xen/arch/arm/Kconfig +2 -0
+	help
+	  This option allows to set the customized address at which Xen will

be

Review

Quoted 6 lines
xen/arch/arm/Kconfig +2 -0
+	  linked on MPU systems. This address must be aligned to a page size.
+
   source "arch/arm/tee/Kconfig"
 
   config STATIC_SHM
(quoted diff) +0 -0

b/xen/arch/arm/platforms/Kconfig

Quoted 11 lines

index c93a6b2756..0904793a0b 100644

xen/arch/arm/platforms/Kconfig +1 -0
@@ -1,6 +1,7 @@
   choice
   	prompt "Platform Support"
   	default ALL_PLAT
+	default FVP_BASER if ARM_V8R
   	---help---
   	Choose which hardware platform to enable in Xen.
 
Quoted 7 lines
Quoted 7 lines

xen/arch/arm/platforms/Kconfig choice · line 14

Quoted 7 lines
xen/arch/arm/platforms/Kconfig +1 -0
@@ -8,13 +9,14 @@ choice
 
   config ALL_PLAT
   	bool "All Platforms"
+	depends on !ARM_V8R
   	---help---
   	Enable support for all available hardware platforms. It

doesn't

Quoted 39 lines
Quoted 29 lines
Quoted 29 lines
xen/arch/arm/platforms/Kconfig +3 -3
   	automatically select any of the related drivers.
 
   config QEMU
   	bool "QEMU aarch virt machine support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
   	select GICV3
   	select HAS_PL011
   	---help---
@@ -23,7 +25,7 @@ config QEMU
 
   config RCAR3
   	bool "Renesas RCar3 support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
   	select HAS_SCIF
   	select IPMMU_VMSA
   	---help---
@@ -31,14 +33,22 @@ config RCAR3
 
   config MPSOC
   	bool "Xilinx Ultrascale+ MPSoC support"
-	depends on ARM_64
+	depends on ARM_64 && !ARM_V8R
   	select HAS_CADENCE_UART
   	select ARM_SMMU
   	---help---
   	Enable all the required drivers for Xilinx Ultrascale+ MPSoC
 
Quoted 9 lines

xen/arch/arm/platforms/Kconfig config RCAR3 · line 47

Quoted 6 lines
xen/arch/arm/platforms/Kconfig +6 -0
+config FVP_BASER
+	bool "Fixed Virtual Platform BaseR support"
+	depends on ARM_V8R
+	help
+	  Enable platform specific configurations for Fixed Virtual
+	  Platform BaseR

This seems unrelated to this patch.

Quoted 2 lines

Can we add some descriptions in commit log for this change, or we Should move it to a new patch?

New patch please or introduce it in the patch where you need it.

Quoted 8 lines

We had preferred to use separate

Quoted 3 lines

patches for this kind of changes, but we found the number of patches would become more and more. This problem has been bothering us for organizing patches.

I understand the concern of increasing the number of patches. However, this also needs to weight against the review.

Understand.

Quoted 6 lines

In this case, it is very difficult for me to understand why we need to introduce FVP_BASER.

In fact, on the previous version, we discussed to not introduce any new platform specific config. So I am a bit surprised this is actually needed.

No, this is no true, it's my mistake, I forgot to remove FVP_BASER from this Kconfig. Actually, we do not need this one. We also don't need a new patch for it.

Cheers,
Wei Chen

Quoted 4 lines

Cheers,

--
Julien Grall

#15 lore raw

Re: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi,

Quoted 29 lines · Wei Chen
Quoted 29 lines
Quoted 17 lines

Although it is unlikely that vendors using the Armv8-R IP will do so, it is indeed an option. In the ID register, there are also related bits in ID_AA64MMFR0_EL1 (MSA_frac) to indicate this.

Quoted 13 lines

xen/arch/arm/Kconfig config TEE · line 145

Quoted 11 lines
---
    xen/arch/arm/Kconfig           |  8 ++++++++
    xen/arch/arm/platforms/Kconfig | 16 +++++++++++++---
    2 files changed, 21 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index ace7178c9a..c6b6b612d1 100644
@@ -145,6 +145,14 @@ config TEE
    	  This option enables generic TEE mediators support. It allows

guests

Quoted 1 line
xen/arch/arm/Kconfig +0 -0
    	  to access real TEE via one of TEE mediators implemented in

XEN.

Quoted 11 lines
Quoted 10 lines

xen/arch/arm/Kconfig config TEE · line 148

Quoted 3 lines
xen/arch/arm/Kconfig +2 -0
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined

address"

xen/arch/arm/Kconfig config TEE · line 151

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+	default 0
+	depends on ARM_V8R

It is still pretty unclear to me what would be the difference between HAS_MPU and ARM_V8R.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

If we don't want to support non-MPU supported Armv8-R, I think they are

the

Quoted 1 line

same. IMO, non-MPU supported Armv8-R is meaningless to Xen.

OOI, why do you think this is meaningless?

If there is Armv8-R board without EL2 MPU, how can we protect Xen?

So what you call EL2 MPU is an MPU that is following the Arm specification. In theory, you could have a proprietary mechanism for that.

So the question is whether a system not following the Arm specification is allowed.

Cheers,

Signature · 1 line
Julien Grall
#16 lore raw

RE: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi Julien,

Quoted 52 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Julien Grall
Sent: 2023年1月18日 19:00
To: Wei Chen <[email protected]>; Penny Zheng <[email protected]>; xen- [email protected]
Cc: Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Jiamei Xie <[email protected]>
Subject: Re: [PATCH v2 04/40] xen/arm: add an option to define Xen start address for Armv8-R

Hi,

Quoted 1 line · Wei Chen
Quoted 1 line
Quoted 1 line

Although it is unlikely that vendors using the Armv8-R IP will do so,

it

Quoted 1 line
Quoted 1 line
Quoted 1 line

is indeed an option. In the ID register, there are also related bits

in

Quoted 20 lines
Quoted 20 lines
Quoted 15 lines

ID_AA64MMFR0_EL1 (MSA_frac) to indicate this.

Quoted 13 lines

xen/arch/arm/Kconfig config TEE · line 145

Quoted 11 lines
---
    xen/arch/arm/Kconfig           |  8 ++++++++
    xen/arch/arm/platforms/Kconfig | 16 +++++++++++++---
    2 files changed, 21 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index ace7178c9a..c6b6b612d1 100644
@@ -145,6 +145,14 @@ config TEE
    	  This option enables generic TEE mediators support. It allows

guests

Quoted 1 line
xen/arch/arm/Kconfig +0 -0
    	  to access real TEE via one of TEE mediators implemented in

XEN.

Quoted 4 lines
Quoted 4 lines

xen/arch/arm/Kconfig config TEE · line 148

Quoted 3 lines
xen/arch/arm/Kconfig +2 -0
+config XEN_START_ADDRESS
+	hex "Xen start address: keep default to use platform defined

address"

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

xen/arch/arm/Kconfig config TEE · line 151

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+	default 0
+	depends on ARM_V8R

It is still pretty unclear to me what would be the difference between HAS_MPU and ARM_V8R.

If we don't want to support non-MPU supported Armv8-R, I think they

are

Quoted 4 lines
Quoted 3 lines

the

Quoted 1 line

same. IMO, non-MPU supported Armv8-R is meaningless to Xen.

OOI, why do you think this is meaningless?

Quoted 8 lines
Quoted 1 line

If there is Armv8-R board without EL2 MPU, how can we protect Xen?

So what you call EL2 MPU is an MPU that is following the Arm specification. In theory, you could have a proprietary mechanism for that.

So the question is whether a system not following the Arm specification is allowed.

I think no, the PMSA is an architectural feature, the spec contains CPU and MPU interfaces. Vendors can have their own hardware implementation, but need follow the Arm spec.

But I agree that, here we could change to "depends on HAS_MPU" which will make It easier to used by other Arm Architecture or other architecture in the future.

Cheers,
Wei Chen

Quoted 4 lines

Cheers,

--
Julien Grall

#17 lore raw

PATCH v2 5/40 xen/arm64: prepare for moving MMU related code from head.S

In reply to Penny Zheng

From: Wei Chen <[email protected]>

We want to reuse head.S for MPU systems, but there are some code implemented for MMU systems only. We will move such code to another MMU specific file. But before that, we will do some preparations in this patch to make them easier for reviewing:
1. Fix the indentations of code comments.
2. Export some symbols that will be accessed out of file scope.

---
v1 -> v2:
1. New patch.

---
 xen/arch/arm/arm64/head.S | 40 +++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
xen/arch/arm/arm64/head.S +20 -20
index 93f9b0b9d5..b2214bc5e3 100644
@@ -136,22 +136,22 @@
         add \xb, \xb, x20
 .endm
 
-        .section .text.header, "ax", %progbits
-        /*.aarch64*/
+.section .text.header, "ax", %progbits
+/*.aarch64*/
 
-        /*
-         * Kernel startup entry point.
-         * ---------------------------
-         *
-         * The requirements are:
-         *   MMU = off, D-cache = off, I-cache = on or off,
-         *   x0 = physical address to the FDT blob.
-         *
-         * This must be the very first address in the loaded image.
-         * It should be linked at XEN_VIRT_START, and loaded at any
-         * 4K-aligned address.  All of text+data+bss must fit in 2MB,
-         * or the initial pagetable code below will need adjustment.
-         */
+/*
+ * Kernel startup entry point.
+ * ---------------------------
+ *
+ * The requirements are:
+ *   MMU = off, D-cache = off, I-cache = on or off,
+ *   x0 = physical address to the FDT blob.
+ *
+ * This must be the very first address in the loaded image.
+ * It should be linked at XEN_VIRT_START, and loaded at any
+ * 4K-aligned address.  All of text+data+bss must fit in 2MB,
+ * or the initial pagetable code below will need adjustment.
+ */
 
 GLOBAL(start)
         /*
@@ -586,7 +586,7 @@ ENDPROC(cpu_init)
  *
  * Clobbers x0 - x4
  */
-create_page_tables:
+ENTRY(create_page_tables)
         /* Prepare the page-tables for mapping Xen */
         ldr   x0, =XEN_VIRT_START
         create_table_entry boot_pgtable, boot_first, x0, 0, x1, x2, x3
@@ -680,7 +680,7 @@ ENDPROC(create_page_tables)
  *
  * Clobbers x0 - x3
  */
-enable_mmu:
+ENTRY(enable_mmu)
         PRINT("- Turning on paging -\r\n")
 
         /*
@@ -714,7 +714,7 @@ ENDPROC(enable_mmu)
  *
  * Clobbers x0 - x1
  */
-remove_identity_mapping:
+ENTRY(remove_identity_mapping)
         /*
          * Find the zeroeth slot used. Remove the entry from zeroeth
          * table if the slot is not XEN_ZEROETH_SLOT.
@@ -775,7 +775,7 @@ ENDPROC(remove_identity_mapping)
  *
  * Clobbers x0 - x3
  */
-setup_fixmap:
+ENTRY(setup_fixmap)
 #ifdef CONFIG_EARLY_PRINTK
         /* Add UART to the fixmap table */
         ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -871,7 +871,7 @@ ENDPROC(init_uart)
  * x0: Nul-terminated string to print.
  * x23: Early UART base address
  * Clobbers x0-x1 */
-puts:
+ENTRY(puts)
         early_uart_ready x23, 1
         ldrb  w1, [x0], #1           /* Load next char */
         cbz   w1, 1f                 /* Exit on nul */
Signature · 1 line
2.25.1
#18 lore raw

Re: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Hi Penny,

Quoted 7 lines · Penny Zheng

From: Wei Chen <[email protected]>

We want to reuse head.S for MPU systems, but there are some code implemented for MMU systems only. We will move such code to another MMU specific file. But before that, we will do some preparations in this patch to make them easier for reviewing:

Well, I agree that...

Quoted 1 line

1. Fix the indentations of code comments.

... changing the indentation is better here. But...

Quoted 2 lines

2. Export some symbols that will be accessed out of file scope.

... I have no idea which functions are going to be used in a separate file. So I think they should belong to the patch moving the code.

Quoted 2 lines

Your signed-off-by is missing.

Quoted 12 lines

---
v1 -> v2:
1. New patch.

---
  xen/arch/arm/arm64/head.S | 40 +++++++++++++++++++--------------------
  1 file changed, 20 insertions(+), 20 deletions(-)
xen/arch/arm/arm64/head.S +0 -0
index 93f9b0b9d5..b2214bc5e3 100644

xen/arch/arm/arm64/head.S line 140

Quoted 7 lines
xen/arch/arm/arm64/head.S +2 -2
@@ -136,22 +136,22 @@
          add \xb, \xb, x20
  .endm
 
-        .section .text.header, "ax", %progbits
-        /*.aarch64*/
+.section .text.header, "ax", %progbits
+/*.aarch64*/

This change is not mentioned.

Quoted 31 lines
xen/arch/arm/arm64/head.S +13 -13
-        /*
-         * Kernel startup entry point.
-         * ---------------------------
-         *
-         * The requirements are:
-         *   MMU = off, D-cache = off, I-cache = on or off,
-         *   x0 = physical address to the FDT blob.
-         *
-         * This must be the very first address in the loaded image.
-         * It should be linked at XEN_VIRT_START, and loaded at any
-         * 4K-aligned address.  All of text+data+bss must fit in 2MB,
-         * or the initial pagetable code below will need adjustment.
-         */
+/*
+ * Kernel startup entry point.
+ * ---------------------------
+ *
+ * The requirements are:
+ *   MMU = off, D-cache = off, I-cache = on or off,
+ *   x0 = physical address to the FDT blob.
+ *
+ * This must be the very first address in the loaded image.
+ * It should be linked at XEN_VIRT_START, and loaded at any
+ * 4K-aligned address.  All of text+data+bss must fit in 2MB,
+ * or the initial pagetable code below will need adjustment.
+ */
 
  GLOBAL(start)
          /*

xen/arch/arm/arm64/head.S ENDPROC(cpu_init) · line 589

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -586,7 +586,7 @@ ENDPROC(cpu_init)
   *
   * Clobbers x0 - x4
   */
-create_page_tables:
+ENTRY(create_page_tables)

I am not sure about keeping this name. Now we have create_page_tables() and arch_setup_page_tables().

I would conside to name it create_boot_page_tables().

Quoted 13 lines
xen/arch/arm/arm64/head.S +1 -1
          /* Prepare the page-tables for mapping Xen */
          ldr   x0, =XEN_VIRT_START
          create_table_entry boot_pgtable, boot_first, x0, 0, x1, x2, x3
@@ -680,7 +680,7 @@ ENDPROC(create_page_tables)
   *
   * Clobbers x0 - x3
   */
-enable_mmu:
+ENTRY(enable_mmu)
          PRINT("- Turning on paging -\r\n")
 
          /*

xen/arch/arm/arm64/head.S ENDPROC(enable_mmu) · line 717

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -714,7 +714,7 @@ ENDPROC(enable_mmu)
   *
   * Clobbers x0 - x1
   */
-remove_identity_mapping:
+ENTRY(remove_identity_mapping)

Patch #14 should be before this patch. So you don't have to export remove_identity_mapping temporarily.

This will also avoid (transient) naming confusing with my work (see [1]).

Quoted 13 lines
xen/arch/arm/arm64/head.S +1 -1
          /*
           * Find the zeroeth slot used. Remove the entry from zeroeth
           * table if the slot is not XEN_ZEROETH_SLOT.
@@ -775,7 +775,7 @@ ENDPROC(remove_identity_mapping)
   *
   * Clobbers x0 - x3
   */
-setup_fixmap:
+ENTRY(setup_fixmap)
  #ifdef CONFIG_EARLY_PRINTK
          /* Add UART to the fixmap table */
          ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS

xen/arch/arm/arm64/head.S ENDPROC(init_uart) · line 874

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -871,7 +871,7 @@ ENDPROC(init_uart)
   * x0: Nul-terminated string to print.
   * x23: Early UART base address
   * Clobbers x0-x1 */
-puts:
+ENTRY(puts)

This name is a bit too generic to be globally exported. It is also now quite confusing because we have "early_puts" and "puts".

I would consider to name it asm_puts(). It is still not great but hopefully it would give a hint that should be call from assembly code.

xen/arch/arm/arm64/head.S ENDPROC(init_uart) · line 877

Quoted 3 lines
xen/arch/arm/arm64/head.S +0 -0
          early_uart_ready x23, 1
          ldrb  w1, [x0], #1           /* Load next char */
          cbz   w1, 1f                 /* Exit on nul */

Cheers,

[1] https://lore.kernel.org/all/[email protected]/

Signature · 1 line
Julien Grall
#19 lore raw

RE: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Hi Julien,

Quoted 25 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:37
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Hi Penny,

Quoted 7 lines · Penny Zheng

From: Wei Chen <[email protected]>

We want to reuse head.S for MPU systems, but there are some code implemented for MMU systems only. We will move such code to another MMU specific file. But before that, we will do some preparations in this patch to make them easier for reviewing:

Well, I agree that...

Quoted 1 line

1. Fix the indentations of code comments.

Quoted 8 lines

... changing the indentation is better here. But...

Quoted 2 lines

2. Export some symbols that will be accessed out of file scope.

... I have no idea which functions are going to be used in a separate file. So I think they should belong to the patch moving the code.

Ok, I will move these changes to the moving code patches.

Quoted 20 lines
Quoted 2 lines

Your signed-off-by is missing.

Quoted 15 lines

---
v1 -> v2:
1. New patch.

---
  xen/arch/arm/arm64/head.S | 40 +++++++++++++++++++--------------------
  1 file changed, 20 insertions(+), 20 deletions(-)
xen/arch/arm/arm64/head.S +0 -0
index 93f9b0b9d5..b2214bc5e3 100644
@@ -136,22 +136,22 @@
          add \xb, \xb, x20
  .endm
 
Quoted 7 lines

xen/arch/arm/arm64/head.S line 140

Quoted 4 lines
xen/arch/arm/arm64/head.S +2 -2
-        .section .text.header, "ax", %progbits
-        /*.aarch64*/
+.section .text.header, "ax", %progbits
+/*.aarch64*/

This change is not mentioned.

I will add the description in commit message.

Quoted 40 lines
Quoted 31 lines
xen/arch/arm/arm64/head.S +13 -13
-        /*
-         * Kernel startup entry point.
-         * ---------------------------
-         *
-         * The requirements are:
-         *   MMU = off, D-cache = off, I-cache = on or off,
-         *   x0 = physical address to the FDT blob.
-         *
-         * This must be the very first address in the loaded image.
-         * It should be linked at XEN_VIRT_START, and loaded at any
-         * 4K-aligned address.  All of text+data+bss must fit in 2MB,
-         * or the initial pagetable code below will need adjustment.
-         */
+/*
+ * Kernel startup entry point.
+ * ---------------------------
+ *
+ * The requirements are:
+ *   MMU = off, D-cache = off, I-cache = on or off,
+ *   x0 = physical address to the FDT blob.
+ *
+ * This must be the very first address in the loaded image.
+ * It should be linked at XEN_VIRT_START, and loaded at any
+ * 4K-aligned address.  All of text+data+bss must fit in 2MB,
+ * or the initial pagetable code below will need adjustment.
+ */
 
  GLOBAL(start)
          /*

xen/arch/arm/arm64/head.S ENDPROC(cpu_init) · line 589

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -586,7 +586,7 @@ ENDPROC(cpu_init)
   *
   * Clobbers x0 - x4
   */
-create_page_tables:
+ENTRY(create_page_tables)

I am not sure about keeping this name. Now we have create_page_tables() and arch_setup_page_tables().

Quoted 2 lines

I would conside to name it create_boot_page_tables().

Do you need me to rename it in this patch?

Quoted 22 lines
Quoted 13 lines
xen/arch/arm/arm64/head.S +1 -1
          /* Prepare the page-tables for mapping Xen */
          ldr   x0, =XEN_VIRT_START
          create_table_entry boot_pgtable, boot_first, x0, 0, x1, x2, x3
@@ -680,7 +680,7 @@ ENDPROC(create_page_tables)
   *
   * Clobbers x0 - x3
   */
-enable_mmu:
+ENTRY(enable_mmu)
          PRINT("- Turning on paging -\r\n")
 
          /*

xen/arch/arm/arm64/head.S ENDPROC(enable_mmu) · line 717

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -714,7 +714,7 @@ ENDPROC(enable_mmu)
   *
   * Clobbers x0 - x1
   */
-remove_identity_mapping:
+ENTRY(remove_identity_mapping)

Patch #14 should be before this patch. So you don't have to export remove_identity_mapping temporarily.

Quoted 2 lines

This will also avoid (transient) naming confusing with my work (see [1]).

Ok, we will do it.

Quoted 22 lines
Quoted 13 lines
xen/arch/arm/arm64/head.S +1 -1
          /*
           * Find the zeroeth slot used. Remove the entry from zeroeth
           * table if the slot is not XEN_ZEROETH_SLOT.
@@ -775,7 +775,7 @@ ENDPROC(remove_identity_mapping)
   *
   * Clobbers x0 - x3
   */
-setup_fixmap:
+ENTRY(setup_fixmap)
  #ifdef CONFIG_EARLY_PRINTK
          /* Add UART to the fixmap table */
          ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS

xen/arch/arm/arm64/head.S ENDPROC(init_uart) · line 874

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -871,7 +871,7 @@ ENDPROC(init_uart)
   * x0: Nul-terminated string to print.
   * x23: Early UART base address
   * Clobbers x0-x1 */
-puts:
+ENTRY(puts)

This name is a bit too generic to be globally exported. It is also now quite confusing because we have "early_puts" and "puts".

Quoted 3 lines

I would consider to name it asm_puts(). It is still not great but hopefully it would give a hint that should be call from assembly code.

Yes, I had the same concern. I will rename it in next version.

Cheers,
Wei Chen

Quoted 10 lines

xen/arch/arm/arm64/head.S ENDPROC(init_uart) · line 877

Quoted 3 lines
xen/arch/arm/arm64/head.S +0 -0
          early_uart_ready x23, 1
          ldrb  w1, [x0], #1           /* Load next char */
          cbz   w1, 1f                 /* Exit on nul */

Cheers,

[1] https://lore.kernel.org/all/[email protected]/

--
Julien Grall

#20 lore raw

Re: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Quoted 108 lines · Wei Chen

Hi Julien,

Quoted 25 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:37
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Hi Penny,

Quoted 7 lines · Penny Zheng

From: Wei Chen <[email protected]>

We want to reuse head.S for MPU systems, but there are some code implemented for MMU systems only. We will move such code to another MMU specific file. But before that, we will do some preparations in this patch to make them easier for reviewing:

Well, I agree that...

Quoted 1 line

1. Fix the indentations of code comments.

Quoted 8 lines

... changing the indentation is better here. But...

Quoted 2 lines

2. Export some symbols that will be accessed out of file scope.

... I have no idea which functions are going to be used in a separate file. So I think they should belong to the patch moving the code.

Ok, I will move these changes to the moving code patches.

Quoted 20 lines
Quoted 2 lines

Your signed-off-by is missing.

Quoted 15 lines

---
v1 -> v2:
1. New patch.

---
   xen/arch/arm/arm64/head.S | 40 +++++++++++++++++++--------------------
   1 file changed, 20 insertions(+), 20 deletions(-)
xen/arch/arm/arm64/head.S +0 -0
index 93f9b0b9d5..b2214bc5e3 100644
@@ -136,22 +136,22 @@
           add \xb, \xb, x20
   .endm
 
Quoted 7 lines

xen/arch/arm/arm64/head.S line 140

Quoted 4 lines
xen/arch/arm/arm64/head.S +2 -2
-        .section .text.header, "ax", %progbits
-        /*.aarch64*/
+.section .text.header, "ax", %progbits
+/*.aarch64*/

This change is not mentioned.

I will add the description in commit message.

Quoted 40 lines
Quoted 31 lines
xen/arch/arm/arm64/head.S +13 -13
-        /*
-         * Kernel startup entry point.
-         * ---------------------------
-         *
-         * The requirements are:
-         *   MMU = off, D-cache = off, I-cache = on or off,
-         *   x0 = physical address to the FDT blob.
-         *
-         * This must be the very first address in the loaded image.
-         * It should be linked at XEN_VIRT_START, and loaded at any
-         * 4K-aligned address.  All of text+data+bss must fit in 2MB,
-         * or the initial pagetable code below will need adjustment.
-         */
+/*
+ * Kernel startup entry point.
+ * ---------------------------
+ *
+ * The requirements are:
+ *   MMU = off, D-cache = off, I-cache = on or off,
+ *   x0 = physical address to the FDT blob.
+ *
+ * This must be the very first address in the loaded image.
+ * It should be linked at XEN_VIRT_START, and loaded at any
+ * 4K-aligned address.  All of text+data+bss must fit in 2MB,
+ * or the initial pagetable code below will need adjustment.
+ */
 
   GLOBAL(start)
           /*

xen/arch/arm/arm64/head.S ENDPROC(cpu_init) · line 589

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -586,7 +586,7 @@ ENDPROC(cpu_init)
    *
    * Clobbers x0 - x4
    */
-create_page_tables:
+ENTRY(create_page_tables)

I am not sure about keeping this name. Now we have create_page_tables() and arch_setup_page_tables().

Quoted 4 lines
Quoted 2 lines

I would conside to name it create_boot_page_tables().

Do you need me to rename it in this patch?

So looking at the rest of the series, I see you are already renaming the helper in patch #11. I think it would be better if the naming is done earlier.

That said, I am not convinced that create_page_tables() should actually be called externally.

In fact, you have something like:

bl create_page_tables
bl enable_mmu

Both will need a MMU/MPU specific implementation. So it would be better if we provide a wrapper to limit the number of external functions.

Cheers,

Signature · 1 line
Julien Grall
#21 lore raw

RE: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Hi Julien,

Quoted 134 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 17:50
To: Wei Chen <[email protected]>; Penny Zheng <[email protected]>; xen- [email protected]
Cc: Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Quoted 29 lines · Wei Chen

Hi Julien,

Quoted 27 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:37
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 05/40] xen/arm64: prepare for moving MMU related code from head.S

Hi Penny,

Quoted 7 lines · Penny Zheng

From: Wei Chen <[email protected]>

We want to reuse head.S for MPU systems, but there are some code implemented for MMU systems only. We will move such code to another MMU specific file. But before that, we will do some preparations in this patch to make them easier for reviewing:

Well, I agree that...

Quoted 1 line

1. Fix the indentations of code comments.

... changing the indentation is better here. But...

Quoted 19 lines
Quoted 6 lines
Quoted 2 lines

2. Export some symbols that will be accessed out of file scope.

... I have no idea which functions are going to be used in a separate file. So I think they should belong to the patch moving the code.

Ok, I will move these changes to the moving code patches.

Quoted 10 lines
Quoted 2 lines

Your signed-off-by is missing.

Quoted 5 lines

---
v1 -> v2:
1. New patch.

---
   xen/arch/arm/arm64/head.S | 40 +++++++++++++++++++-----------------

---

Quoted 60 lines
Quoted 10 lines
Quoted 10 lines
   1 file changed, 20 insertions(+), 20 deletions(-)
xen/arch/arm/arm64/head.S +0 -0
index 93f9b0b9d5..b2214bc5e3 100644
@@ -136,22 +136,22 @@
           add \xb, \xb, x20
   .endm
 
Quoted 7 lines

xen/arch/arm/arm64/head.S line 140

Quoted 4 lines
xen/arch/arm/arm64/head.S +2 -2
-        .section .text.header, "ax", %progbits
-        /*.aarch64*/
+.section .text.header, "ax", %progbits
+/*.aarch64*/

This change is not mentioned.

I will add the description in commit message.

Quoted 40 lines
Quoted 31 lines
xen/arch/arm/arm64/head.S +13 -13
-        /*
-         * Kernel startup entry point.
-         * ---------------------------
-         *
-         * The requirements are:
-         *   MMU = off, D-cache = off, I-cache = on or off,
-         *   x0 = physical address to the FDT blob.
-         *
-         * This must be the very first address in the loaded image.
-         * It should be linked at XEN_VIRT_START, and loaded at any
-         * 4K-aligned address.  All of text+data+bss must fit in 2MB,
-         * or the initial pagetable code below will need adjustment.
-         */
+/*
+ * Kernel startup entry point.
+ * ---------------------------
+ *
+ * The requirements are:
+ *   MMU = off, D-cache = off, I-cache = on or off,
+ *   x0 = physical address to the FDT blob.
+ *
+ * This must be the very first address in the loaded image.
+ * It should be linked at XEN_VIRT_START, and loaded at any
+ * 4K-aligned address.  All of text+data+bss must fit in 2MB,
+ * or the initial pagetable code below will need adjustment.
+ */
 
   GLOBAL(start)
           /*

xen/arch/arm/arm64/head.S ENDPROC(cpu_init) · line 589

Quoted 5 lines
xen/arch/arm/arm64/head.S +1 -1
@@ -586,7 +586,7 @@ ENDPROC(cpu_init)
    *
    * Clobbers x0 - x4
    */
-create_page_tables:
+ENTRY(create_page_tables)

I am not sure about keeping this name. Now we have create_page_tables() and arch_setup_page_tables().

Quoted 4 lines
Quoted 2 lines

I would conside to name it create_boot_page_tables().

Do you need me to rename it in this patch?

So looking at the rest of the series, I see you are already renaming the helper in patch #11. I think it would be better if the naming is done earlier.

That said, I am not convinced that create_page_tables() should actually be called externally.

Quoted 8 lines

In fact, you have something like:

bl create_page_tables
bl enable_mmu

Both will need a MMU/MPU specific implementation. So it would be better if we provide a wrapper to limit the number of external functions.

I agree with you, we will try to wrapper some functions instead of export them.

Cheers,
Wei Chen

Quoted 4 lines

Cheers,

--
Julien Grall

#22 lore raw

PATCH v2 6/40 xen/arm64: move MMU related code from head.S to head_mmu.S

In reply to Penny Zheng

From: Wei Chen <[email protected]>

There are lots of MMU specific code in head.S. This code will not be used in MPU systems. If we use #ifdef to gate them, the code will become messy and hard to maintain. So we move MMU related code to head_mmu.S, and keep common code still in head.S.

And some assembly macros that will be shared by MMU and MPU later, we move them to macros.h.

---
v1 -> v2:
1. Move macros to macros.h
2. Remove the indention modification
3. Duplicate "fail" instead of exporting it.

---
 xen/arch/arm/arm64/Makefile             |   3 +
 xen/arch/arm/arm64/head.S               | 383 ------------------------
 xen/arch/arm/arm64/head_mmu.S           | 372 +++++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/macros.h |  51 ++++
 4 files changed, 426 insertions(+), 383 deletions(-)
 create mode 100644 xen/arch/arm/arm64/head_mmu.S
xen/arch/arm/arm64/Makefile +3 -0
index 6d507da0d4..22da2f54b5 100644
@@ -8,6 +8,9 @@ obj-y += domctl.o
 obj-y += domain.o
 obj-y += entry.o
 obj-y += head.o
+ifneq ($(CONFIG_HAS_MPU),y)
+obj-y += head_mmu.o
+endif
 obj-y += insn.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-y += smc.o
xen/arch/arm/arm64/head.S +0 -383

407 lines not on this page · raw message

xen/arch/arm/arm64/head_mmu.S new file +372 -0

372 lines not on this page · raw message

xen/arch/arm/include/asm/arm64/macros.h +51 -0

57 lines not on this page · raw message

Signature · 1 line
2.25.1
#23 lore raw

PATCH v2 7/40 xen/arm64: add .text.idmap for Xen identity map sections

In reply to Penny Zheng

From: Wei Chen <[email protected]>

Only the first 4KB of Xen image will be mapped as identity (PA == VA). At the moment, Xen guarantees this by having everything that needs to be used in the identity mapping in head.S before _end_boot and checking at link time if this fits in 4KB.

In previous patch, we have moved the MMU code outside of head.S. Although we have added .text.header to the new file to guarantee all identity map code still in the first 4KB. However, the order of these two files on this 4KB depends on the build tools. Currently, we use the build tools to process the order of objs in the Makefile to ensure that head.S must be at the top. But if you change to another build tools, it may not be the same result.

In this patch we introduce .text.idmap to head_mmu.S, and add this section after .text.header. to ensure code of head_mmu.S after the code of header.S.

After this, we will still include some code that does not belong to identity map before _end_boot. Because we have moved _end_boot to head_mmu.S. That means all code in head.S will be included before _end_boot. In this patch, we also added .text flag in the place of original _end_boot in head.S. All the code after .text in head.S will not be included in identity map section.

---
v1 -> v2:
1. New patch.

---
 xen/arch/arm/arm64/head.S     | 6 ++++++
 xen/arch/arm/arm64/head_mmu.S | 2 +-
 xen/arch/arm/xen.lds.S        | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/head.S +6 -0
index 5cfa47279b..782bd1f94c 100644
@@ -466,6 +466,12 @@ fail:   PRINT("- Boot failed -\r\n")
         b     1b
 ENDPROC(fail)
 
+/*
+ * For the code that do not need in indentity map section,
+ * we put them back to normal .text section
+ */
+.section .text, "ax", %progbits
+
 #ifdef CONFIG_EARLY_PRINTK
 /*
  * Initialize the UART. Should only be called on the boot CPU.
xen/arch/arm/arm64/head_mmu.S +1 -1
index e2c8f07140..6ff13c751c 100644
@@ -105,7 +105,7 @@
         str   \tmp2, [\tmp3, \tmp1, lsl #3]
 .endm
 
-.section .text.header, "ax", %progbits
+.section .text.idmap, "ax", %progbits
 /*.aarch64*/
 
 /*
xen/arch/arm/xen.lds.S +1 -0
index 92c2984052..bc45ea2c65 100644
@@ -33,6 +33,7 @@ SECTIONS
   .text : {
         _stext = .;            /* Text section */
        *(.text.header)
+       *(.text.idmap)
 
        *(.text.cold)
        *(.text.unlikely .text.*_unlikely .text.unlikely.*)
Signature · 1 line
2.25.1
#24 lore raw

Re: [PATCH v2 07/40] xen/arm64: add .text.idmap for Xen identity map sections

Hi,

Quoted 8 lines · Penny Zheng

From: Wei Chen <[email protected]>

Only the first 4KB of Xen image will be mapped as identity (PA == VA). At the moment, Xen guarantees this by having everything that needs to be used in the identity mapping in head.S before _end_boot and checking at link time if this fits in 4KB.

Quoted 8 lines

In previous patch, we have moved the MMU code outside of head.S. Although we have added .text.header to the new file to guarantee all identity map code still in the first 4KB. However, the order of these two files on this 4KB depends on the build tools. Currently, we use the build tools to process the order of objs in the Makefile to ensure that head.S must be at the top. But if you change to another build tools, it may not be the same result.

Right, so this is fixing a bug you introduced in the previous patch. We should really avoid introducing (latent) regression in a series. So please re-order the patches.

Quoted 8 lines

In this patch we introduce .text.idmap to head_mmu.S, and add this section after .text.header. to ensure code of head_mmu.S after the code of header.S.

After this, we will still include some code that does not belong to identity map before _end_boot. Because we have moved _end_boot to head_mmu.S.

I dislike this approach because you are expecting that only head_mmu.S will be part of .text.idmap. If it is not, everything could blow up again.

That said, if you look at staging, you will notice that now _end_boot is defined in the linker script to avoid any issue.

Quoted 21 lines

That means all code in head.S
will be included before _end_boot. In this patch, we also added .text flag in the place of original _end_boot in head.S. All the code after .text in head.S will not be included in identity map section.

---
v1 -> v2:
1. New patch.

---
  xen/arch/arm/arm64/head.S     | 6 ++++++
  xen/arch/arm/arm64/head_mmu.S | 2 +-
  xen/arch/arm/xen.lds.S        | 1 +
  3 files changed, 8 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/head.S +0 -0
index 5cfa47279b..782bd1f94c 100644

xen/arch/arm/arm64/head.S fail: PRINT("- Boot failed -\r\n") · line 474

Quoted 9 lines
xen/arch/arm/arm64/head.S +6 -0
@@ -466,6 +466,12 @@ fail:   PRINT("- Boot failed -\r\n")
          b     1b
  ENDPROC(fail)
 
+/*
+ * For the code that do not need in indentity map section,
+ * we put them back to normal .text section
+ */
+.section .text, "ax", %progbits
+

I would argue that puts wants to be part of the idmap.

Quoted 21 lines
xen/arch/arm/arm64/head.S +0 -0
  #ifdef CONFIG_EARLY_PRINTK
  /*
   * Initialize the UART. Should only be called on the boot CPU.
xen/arch/arm/arm64/head_mmu.S +1 -1
index e2c8f07140..6ff13c751c 100644
@@ -105,7 +105,7 @@
          str   \tmp2, [\tmp3, \tmp1, lsl #3]
  .endm
 
-.section .text.header, "ax", %progbits
+.section .text.idmap, "ax", %progbits
  /*.aarch64*/
 
  /*
xen/arch/arm/xen.lds.S +0 -0
index 92c2984052..bc45ea2c65 100644

xen/arch/arm/xen.lds.S SECTIONS · line 39

Quoted 7 lines
xen/arch/arm/xen.lds.S +1 -0
@@ -33,6 +33,7 @@ SECTIONS
    .text : {
          _stext = .;            /* Text section */
         *(.text.header)
+       *(.text.idmap)
 
         *(.text.cold)
         *(.text.unlikely .text.*_unlikely .text.unlikely.*)

Cheers,

Signature · 1 line
Julien Grall
#25 lore raw

RE: [PATCH v2 07/40] xen/arm64: add .text.idmap for Xen identity map sections

Hi Julien,

Quoted 30 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:46
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 07/40] xen/arm64: add .text.idmap for Xen identity map sections

Hi,

Quoted 8 lines · Penny Zheng

From: Wei Chen <[email protected]>

Only the first 4KB of Xen image will be mapped as identity (PA == VA). At the moment, Xen guarantees this by having everything that needs to be used in the identity mapping in head.S before _end_boot and checking at link time if this fits in 4KB.

Quoted 8 lines

In previous patch, we have moved the MMU code outside of head.S. Although we have added .text.header to the new file to guarantee all identity map code still in the first 4KB. However, the order of these two files on this 4KB depends on the build tools. Currently, we use the build tools to process the order of objs in the Makefile to ensure that head.S must be at the top. But if you change to another build tools, it may not be the same result.

Quoted 4 lines

Right, so this is fixing a bug you introduced in the previous patch. We should really avoid introducing (latent) regression in a series. So please re-order the patches.

Ok.

Quoted 5 lines
Quoted 5 lines

In this patch we introduce .text.idmap to head_mmu.S, and add this section after .text.header. to ensure code of head_mmu.S after the code of header.S.

Quoted 7 lines
Quoted 3 lines

After this, we will still include some code that does not belong to identity map before _end_boot. Because we have moved _end_boot to head_mmu.S.

I dislike this approach because you are expecting that only head_mmu.S will be part of .text.idmap. If it is not, everything could blow up again.

I agree.

Quoted 3 lines

That said, if you look at staging, you will notice that now _end_boot is defined in the linker script to avoid any issue.

Sorry, I am not quite clear about this comment. The _end_boot of original staging branch is defined in head.S. And I am not quite sure how this _end_boot solve multiple files contain idmap code.

Cheers,
Wei Chen

Quoted 24 lines
Quoted 24 lines

That means all code in head.S
will be included before _end_boot. In this patch, we also added .text flag in the place of original _end_boot in head.S. All the code after .text in head.S will not be included in identity map section.

---
v1 -> v2:
1. New patch.

---
  xen/arch/arm/arm64/head.S     | 6 ++++++
  xen/arch/arm/arm64/head_mmu.S | 2 +-
  xen/arch/arm/xen.lds.S        | 1 +
  3 files changed, 8 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/head.S +0 -0
index 5cfa47279b..782bd1f94c 100644
@@ -466,6 +466,12 @@ fail:   PRINT("- Boot failed -\r\n")
          b     1b
  ENDPROC(fail)
 
Quoted 9 lines

xen/arch/arm/arm64/head.S fail: PRINT("- Boot failed -\r\n") · line 474

Quoted 6 lines
xen/arch/arm/arm64/head.S +6 -0
+/*
+ * For the code that do not need in indentity map section,
+ * we put them back to normal .text section
+ */
+.section .text, "ax", %progbits
+

I would argue that puts wants to be part of the idmap.

I am ok to move puts to idmap. But from the original head.S, puts is placed after _end_boot, and from the xen.ld.S, we can see idmap is area is the section of "_end_boot - start". The reason of moving puts to idmap is because we're using it in idmap?

Cheers,
Wei Chen

Quoted 34 lines

Review

Quoted 4 lines
xen/arch/arm/arm64/head.S +0 -0
  #ifdef CONFIG_EARLY_PRINTK
  /*
   * Initialize the UART. Should only be called on the boot CPU.
(quoted diff) +0 -0

b/xen/arch/arm/arm64/head_mmu.S

Quoted 17 lines

index e2c8f07140..6ff13c751c 100644

xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -105,7 +105,7 @@
          str   \tmp2, [\tmp3, \tmp1, lsl #3]
  .endm
 
-.section .text.header, "ax", %progbits
+.section .text.idmap, "ax", %progbits
  /*.aarch64*/
 
  /*
xen/arch/arm/xen.lds.S +0 -0
index 92c2984052..bc45ea2c65 100644

xen/arch/arm/xen.lds.S SECTIONS · line 39

Quoted 7 lines
xen/arch/arm/xen.lds.S +1 -0
@@ -33,6 +33,7 @@ SECTIONS
    .text : {
          _stext = .;            /* Text section */
         *(.text.header)
+       *(.text.idmap)
 
         *(.text.cold)
         *(.text.unlikely .text.*_unlikely .text.unlikely.*)

Cheers,

--
Julien Grall

#26 lore raw

Re: [PATCH v2 07/40] xen/arm64: add .text.idmap for Xen identity map sections

Quoted 1 line · Wei Chen

Hi Julien,

Hi Wei,

Quoted 52 lines
Quoted 30 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:46
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 07/40] xen/arm64: add .text.idmap for Xen identity map sections

Hi,

Quoted 8 lines · Penny Zheng

From: Wei Chen <[email protected]>

Only the first 4KB of Xen image will be mapped as identity (PA == VA). At the moment, Xen guarantees this by having everything that needs to be used in the identity mapping in head.S before _end_boot and checking at link time if this fits in 4KB.

Quoted 8 lines

In previous patch, we have moved the MMU code outside of head.S. Although we have added .text.header to the new file to guarantee all identity map code still in the first 4KB. However, the order of these two files on this 4KB depends on the build tools. Currently, we use the build tools to process the order of objs in the Makefile to ensure that head.S must be at the top. But if you change to another build tools, it may not be the same result.

Quoted 4 lines

Right, so this is fixing a bug you introduced in the previous patch. We should really avoid introducing (latent) regression in a series. So please re-order the patches.

Ok.

Quoted 5 lines
Quoted 5 lines

In this patch we introduce .text.idmap to head_mmu.S, and add this section after .text.header. to ensure code of head_mmu.S after the code of header.S.

Quoted 7 lines
Quoted 3 lines

After this, we will still include some code that does not belong to identity map before _end_boot. Because we have moved _end_boot to head_mmu.S.

I dislike this approach because you are expecting that only head_mmu.S will be part of .text.idmap. If it is not, everything could blow up again.

I agree.

Quoted 7 lines
Quoted 3 lines

That said, if you look at staging, you will notice that now _end_boot is defined in the linker script to avoid any issue.

Sorry, I am not quite clear about this comment. The _end_boot of original staging branch is defined in head.S. And I am not quite sure how this _end_boot solve multiple files contain idmap code.

If you look at the latest staging, there is a commit (229ebd517b9d) that now define _end_boot in the linker script.

The .text.idmap section can be added before the definition of _end_boot.

Quoted 38 lines

Cheers,
Wei Chen

Quoted 24 lines
Quoted 24 lines

That means all code in head.S
will be included before _end_boot. In this patch, we also added .text flag in the place of original _end_boot in head.S. All the code after .text in head.S will not be included in identity map section.

---
v1 -> v2:
1. New patch.

---
   xen/arch/arm/arm64/head.S     | 6 ++++++
   xen/arch/arm/arm64/head_mmu.S | 2 +-
   xen/arch/arm/xen.lds.S        | 1 +
   3 files changed, 8 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/head.S +0 -0
index 5cfa47279b..782bd1f94c 100644
@@ -466,6 +466,12 @@ fail:   PRINT("- Boot failed -\r\n")
           b     1b
   ENDPROC(fail)
 
Quoted 9 lines

xen/arch/arm/arm64/head.S fail: PRINT("- Boot failed -\r\n") · line 474

Quoted 6 lines
xen/arch/arm/arm64/head.S +6 -0
+/*
+ * For the code that do not need in indentity map section,
+ * we put them back to normal .text section
+ */
+.section .text, "ax", %progbits
+

I would argue that puts wants to be part of the idmap.

Quoted 3 lines

I am ok to move puts to idmap. But from the original head.S, puts is placed after _end_boot, and from the xen.ld.S, we can see idmap is area is the section of "_end_boot - start".

The original position of _end_boot is wrong. It didn't take into account the literal pool (there are at the end of the unit). So they would be past _end_boot.

Quoted 2 lines

The reason of moving puts
to idmap is because we're using it in idmap?

I guess it depends of what idmap really mean here. If you only interpret as the MMU is on and VA == PA. Then not yet (I was thinking to introduce a few calls).

If you also include the MMU off. Then yes.

Also, in the context of cache coloring, we will need to have a trampoline for cache coloring. So it would be better to keep everything close together as it is easier to copy.

Cheers,

Signature · 1 line
Julien Grall
#27 lore raw

RE: [PATCH v2 07/40] xen/arm64: add .text.idmap for Xen identity map sections

Hi Julien,

Quoted 23 lines

-----Original Message-----

Quoted 11 lines
Quoted 11 lines
Quoted 8 lines

In this patch we introduce .text.idmap to head_mmu.S, and add this section after .text.header. to ensure code of head_mmu.S after the code of header.S.

After this, we will still include some code that does not belong to identity map before _end_boot. Because we have moved _end_boot to head_mmu.S.

I dislike this approach because you are expecting that only head_mmu.S will be part of .text.idmap. If it is not, everything could blow up

again.

Quoted 5 lines
Quoted 1 line

I agree.

Quoted 1 line

That said, if you look at staging, you will notice that now _end_boot

is

Quoted 4 lines
Quoted 2 lines

defined in the linker script to avoid any issue.

Sorry, I am not quite clear about this comment. The _end_boot of

Quoted 9 lines

original

Quoted 2 lines

staging branch is defined in head.S. And I am not quite sure how this _end_boot solve multiple files contain idmap code.

If you look at the latest staging, there is a commit (229ebd517b9d) that now define _end_boot in the linker script.

The .text.idmap section can be added before the definition of _end_boot.

Oh, my branch was a little old, I have seen this new definition in xen.ld.S after I update the branch. I understand now.

Quoted 38 lines
Quoted 38 lines

Cheers,
Wei Chen

Quoted 33 lines
Quoted 21 lines

That means all code in head.S
will be included before _end_boot. In this patch, we also added .text flag in the place of original _end_boot in head.S. All the code after .text in head.S will not be included in identity map section.

---
v1 -> v2:
1. New patch.

---
   xen/arch/arm/arm64/head.S     | 6 ++++++
   xen/arch/arm/arm64/head_mmu.S | 2 +-
   xen/arch/arm/xen.lds.S        | 1 +
   3 files changed, 8 insertions(+), 1 deletion(-)
xen/arch/arm/arm64/head.S +0 -0
index 5cfa47279b..782bd1f94c 100644

xen/arch/arm/arm64/head.S fail: PRINT("- Boot failed -\r\n") · line 474

Quoted 9 lines
xen/arch/arm/arm64/head.S +6 -0
@@ -466,6 +466,12 @@ fail:   PRINT("- Boot failed -\r\n")
           b     1b
   ENDPROC(fail)
 
+/*
+ * For the code that do not need in indentity map section,
+ * we put them back to normal .text section
+ */
+.section .text, "ax", %progbits
+

I would argue that puts wants to be part of the idmap.

Quoted 8 lines
Quoted 3 lines

I am ok to move puts to idmap. But from the original head.S, puts is placed after _end_boot, and from the xen.ld.S, we can see idmap is area is the section of "_end_boot - start".

The original position of _end_boot is wrong. It didn't take into account the literal pool (there are at the end of the unit). So they would be past _end_boot.

Ok.

Quoted 7 lines
Quoted 2 lines

The reason of moving puts
to idmap is because we're using it in idmap?

I guess it depends of what idmap really mean here. If you only interpret as the MMU is on and VA == PA. Then not yet (I was thinking to introduce a few calls).

Quoted 6 lines

If you also include the MMU off. Then yes.

Also, in the context of cache coloring, we will need to have a trampoline for cache coloring. So it would be better to keep everything close together as it is easier to copy.

Understand, thanks!

Cheers,
Wei Chen

Quoted 4 lines

Cheers,

--
Julien Grall

#28 lore raw

PATCH v2 8/40 xen/arm: use PA == VA for EARLY_UART_VIRTUAL_ADDRESS on Armv-8R

In reply to Penny Zheng

From: Wei Chen <[email protected]>

There is no VMSA support on Armv8-R AArch64, so we can not map early UART to FIXMAP_CONSOLE. Instead, we use PA == VA to define EARLY_UART_VIRTUAL_ADDRESS on Armv8-R AArch64.

---
1. New patch

---
 xen/arch/arm/include/asm/early_printk.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
xen/arch/arm/include/asm/early_printk.h +12 -0
index c5149b2976..44a230853f 100644
@@ -15,10 +15,22 @@
 
 #ifdef CONFIG_EARLY_PRINTK
 
+#ifdef CONFIG_ARM_V8R
+
+/*
+ * For Armv-8r, there is not VMSA support in EL2, so we use VA == PA
+ * for EARLY_UART_VIRTUAL_ADDRESS.
+ */
+#define EARLY_UART_VIRTUAL_ADDRESS CONFIG_EARLY_UART_BASE_ADDRESS
+
+#else
+
 /* need to add the uart address offset in page to the fixmap address */
 #define EARLY_UART_VIRTUAL_ADDRESS \
     (FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
 
+#endif /* CONFIG_ARM_V8R */
+
 #endif /* !CONFIG_EARLY_PRINTK */
 
 #endif
Signature · 1 line
2.25.1
#29 lore raw

Re: [PATCH v2 08/40] xen/arm: use PA == VA for EARLY_UART_VIRTUAL_ADDRESS on Armv-8R

Hi Penny,

Quoted 7 lines · Penny Zheng

From: Wei Chen <[email protected]>

There is no VMSA support on Armv8-R AArch64, so we can not map early UART to FIXMAP_CONSOLE. Instead, we use PA == VA to define EARLY_UART_VIRTUAL_ADDRESS on Armv8-R AArch64.

Your signed-off-by is missing.

Quoted 11 lines

---
1. New patch

---
  xen/arch/arm/include/asm/early_printk.h | 12 ++++++++++++
  1 file changed, 12 insertions(+)
xen/arch/arm/include/asm/early_printk.h +0 -0
index c5149b2976..44a230853f 100644

xen/arch/arm/include/asm/early_printk.h line 18

Quoted 4 lines
xen/arch/arm/include/asm/early_printk.h +1 -0
@@ -15,10 +15,22 @@
 
  #ifdef CONFIG_EARLY_PRINTK
 
+#ifdef CONFIG_ARM_V8R

Shouldn't this be CONFIG_HAS_MPU?

xen/arch/arm/include/asm/early_printk.h line 21

Quoted 3 lines
xen/arch/arm/include/asm/early_printk.h +3 -0
+
+/*
+ * For Armv-8r, there is not VMSA support in EL2, so we use VA == PA

s/not/no/

Quoted 4 lines
xen/arch/arm/include/asm/early_printk.h +4 -0
+ * for EARLY_UART_VIRTUAL_ADDRESS. > + */
+#define EARLY_UART_VIRTUAL_ADDRESS CONFIG_EARLY_UART_BASE_ADDRESS
+
+#else

xen/arch/arm/include/asm/early_printk.h line 36

Quoted 10 lines
xen/arch/arm/include/asm/early_printk.h +3 -0
+
  /* need to add the uart address offset in page to the fixmap address */
  #define EARLY_UART_VIRTUAL_ADDRESS \
      (FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
 
+#endif /* CONFIG_ARM_V8R */
+
  #endif /* !CONFIG_EARLY_PRINTK */
 
  #endif

Cheers,

Signature · 1 line
Julien Grall
#30 lore raw

RE: [PATCH v2 08/40] xen/arm: use PA == VA for EARLY_UART_VIRTUAL_ADDRESS on Armv-8R

Hi Julien,

Quoted 36 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2023年1月18日 7:49
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 08/40] xen/arm: use PA == VA for EARLY_UART_VIRTUAL_ADDRESS on Armv-8R

Hi Penny,

Quoted 7 lines · Penny Zheng

From: Wei Chen <[email protected]>

There is no VMSA support on Armv8-R AArch64, so we can not map early UART to FIXMAP_CONSOLE. Instead, we use PA == VA to define EARLY_UART_VIRTUAL_ADDRESS on Armv8-R AArch64.

Your signed-off-by is missing.

Review

Quoted 7 lines

---
1. New patch

---
  xen/arch/arm/include/asm/early_printk.h | 12 ++++++++++++
  1 file changed, 12 insertions(+)
(quoted diff) +0 -0

b/xen/arch/arm/include/asm/early_printk.h

Quoted 5 lines

index c5149b2976..44a230853f 100644

xen/arch/arm/include/asm/early_printk.h +0 -0
@@ -15,10 +15,22 @@
 
Quoted 6 lines

xen/arch/arm/include/asm/early_printk.h line 18

Quoted 3 lines
xen/arch/arm/include/asm/early_printk.h +1 -0
  #ifdef CONFIG_EARLY_PRINTK
 
+#ifdef CONFIG_ARM_V8R

Shouldn't this be CONFIG_HAS_MPU?

We had considered that there may be an implementation of Arm8R without an MPU, so we used CONFIG_ARM_V8R here. But you're right, we have not support non-MPU scenario in this series, so use CONFIG_HAS_MPU here would be better to indicate this is a feature based code section. We will change it to CONFIG_HAS_MPU in next version.

Quoted 6 lines

xen/arch/arm/include/asm/early_printk.h line 21

Quoted 3 lines
xen/arch/arm/include/asm/early_printk.h +3 -0
+
+/*
+ * For Armv-8r, there is not VMSA support in EL2, so we use VA == PA

s/not/no/

Ok.

Cheers,
Wei Chen

Quoted 21 lines

xen/arch/arm/include/asm/early_printk.h

Quoted 6 lines
xen/arch/arm/include/asm/early_printk.h +5 -0
+ * for EARLY_UART_VIRTUAL_ADDRESS. > + */
+#define EARLY_UART_VIRTUAL_ADDRESS CONFIG_EARLY_UART_BASE_ADDRESS
+
+#else
+
  /* need to add the uart address offset in page to the fixmap address

*/

xen/arch/arm/include/asm/early_printk.h line 29

Quoted 2 lines
xen/arch/arm/include/asm/early_printk.h +0 -0
  #define EARLY_UART_VIRTUAL_ADDRESS \
      (FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS &

~PAGE_MASK))

xen/arch/arm/include/asm/early_printk.h line 36

Quoted 6 lines
xen/arch/arm/include/asm/early_printk.h +2 -0
+#endif /* CONFIG_ARM_V8R */
+
  #endif /* !CONFIG_EARLY_PRINTK */
 
  #endif

Cheers,

--
Julien Grall

#31 lore raw

PATCH v2 9/40 xen/arm: decouple copy_from_paddr with FIXMAP

In reply to Penny Zheng

From: Wei Chen <[email protected]>

copy_from_paddr will map a page to Xen's FIXMAP_MISC area for temporary access. But for those systems do not support VMSA, they can not implement set_fixmap/clear_fixmap, that means they can't always use the same virtual address for source address.

In this case, we introduce to helpers to decouple copy_from_paddr with set_fixmap/clear_fixmap. map_page_to_xen_misc can always return the same virtual address as before for VMSA systems. It also can return different address for non-VMSA systems.

---
v1 -> v2:
1. New patch

---
 xen/arch/arm/include/asm/setup.h |  4 ++++
 xen/arch/arm/kernel.c            | 13 +++++++------
 xen/arch/arm/mm.c                | 12 ++++++++++++
 3 files changed, 23 insertions(+), 6 deletions(-)
xen/arch/arm/include/asm/setup.h +4 -0
index a926f30a2b..4f39a1aa0a 100644
@@ -119,6 +119,10 @@ extern struct bootinfo bootinfo;
 
 extern domid_t max_init_domid;
 
+/* Map a page to misc area */
+void *map_page_to_xen_misc(mfn_t mfn, unsigned int attributes);
+/* Unmap the page from misc area */
+void unmap_page_from_xen_misc(void);
 void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len);
 
 size_t estimate_efi_size(unsigned int mem_nr_banks);
xen/arch/arm/kernel.c +7 -6
index 23b840ea9e..0475d8fae7 100644
@@ -49,18 +49,19 @@ struct minimal_dtb_header {
  */
 void __init copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)
 {
-    void *src = (void *)FIXMAP_ADDR(FIXMAP_MISC);
-
-    while (len) {
+    while ( len )
+    {
+        void *src;
         unsigned long l, s;
 
-        s = paddr & (PAGE_SIZE-1);
+        s = paddr & (PAGE_SIZE - 1);
         l = min(PAGE_SIZE - s, len);
 
-        set_fixmap(FIXMAP_MISC, maddr_to_mfn(paddr), PAGE_HYPERVISOR_WC);
+        src = map_page_to_xen_misc(maddr_to_mfn(paddr), PAGE_HYPERVISOR_WC);
+        ASSERT(src != NULL);
         memcpy(dst, src + s, l);
         clean_dcache_va_range(dst, l);
-        clear_fixmap(FIXMAP_MISC);
+        unmap_page_from_xen_misc();
 
         paddr += l;
         dst += l;
xen/arch/arm/mm.c +12 -0
index 0fc6f2992d..8f15814c5e 100644
@@ -355,6 +355,18 @@ void clear_fixmap(unsigned int map)
     BUG_ON(res != 0);
 }
 
+void *map_page_to_xen_misc(mfn_t mfn, unsigned int attributes)
+{
+    set_fixmap(FIXMAP_MISC, mfn, attributes);
+
+    return fix_to_virt(FIXMAP_MISC);
+}
+
+void unmap_page_from_xen_misc(void)
+{
+    clear_fixmap(FIXMAP_MISC);
+}
+
 void flush_page_to_ram(unsigned long mfn, bool sync_icache)
 {
     void *v = map_domain_page(_mfn(mfn));
Signature · 1 line
2.25.1
#32 lore raw

PATCH v2 10/40 xen/arm: split MMU and MPU config files from config.h

In reply to Penny Zheng

From: Wei Chen <[email protected]>

Xen defines some global configuration macros for Arm in config.h. We still want to use it for Armv8-R systems, but there are some address related macros that are defined for MMU systems. These macros will not be used by MPU systems, Adding ifdefery with CONFIG_HAS_MPU to gate these macros will result in a messy and hard-to-read/maintain code.

So we keep some common definitions still in config.h, but move virtual address related definitions to a new file - config_mmu.h. And use a new file config_mpu.h to store definitions for MPU systems. To avoid spreading #ifdef everywhere, we keep the same definition names for MPU systems, like XEN_VIRT_START and HYPERVISOR_VIRT_START, but the definition contents are MPU specific.

---
v1 -> v2:
1. Remove duplicated FIXMAP definitions from config_mmu.h

---
 xen/arch/arm/include/asm/config.h     | 103 +++--------------------
 xen/arch/arm/include/asm/config_mmu.h | 112 ++++++++++++++++++++++++++
 xen/arch/arm/include/asm/config_mpu.h |  25 ++++++
 3 files changed, 147 insertions(+), 93 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/config_mmu.h
 create mode 100644 xen/arch/arm/include/asm/config_mpu.h
xen/arch/arm/include/asm/config.h +10 -93
index 25a625ff08..86d8142959 100644
@@ -48,6 +48,12 @@
 
 #define INVALID_VCPU_ID MAX_VIRT_CPUS
 
+/* Used for calculating PDX */
+#ifdef CONFIG_ARM_64
+#define FRAMETABLE_SIZE        GB(32)
+#define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
+#endif
+
 #define __LINUX_ARM_ARCH__ 7
 #define CONFIG_AEABI
 
@@ -71,99 +77,10 @@
 #include <xen/const.h>
 #include <xen/page-size.h>
 
-/*
- * Common ARM32 and ARM64 layout:
- *   0  -   2M   Unmapped
- *   2M -   4M   Xen text, data, bss
- *   4M -   6M   Fixmap: special-purpose 4K mapping slots
- *   6M -  10M   Early boot mapping of FDT
- *   10M - 12M   Livepatch vmap (if compiled in)
- *
- * ARM32 layout:
- *   0  -  12M   <COMMON>
- *
- *  32M - 128M   Frametable: 24 bytes per page for 16GB of RAM
- * 256M -   1G   VMAP: ioremap and early_ioremap use this virtual address
- *                    space
- *
- *   1G -   2G   Xenheap: always-mapped memory
- *   2G -   4G   Domheap: on-demand-mapped
- *
- * ARM64 layout:
- * 0x0000000000000000 - 0x0000007fffffffff (512GB, L0 slot [0])
- *   0  -  12M   <COMMON>
- *
- *   1G -   2G   VMAP: ioremap and early_ioremap
- *
- *  32G -  64G   Frametable: 24 bytes per page for 5.3TB of RAM
- *
- * 0x0000008000000000 - 0x00007fffffffffff (127.5TB, L0 slots [1..255])
- *  Unused
- *
- * 0x0000800000000000 - 0x000084ffffffffff (5TB, L0 slots [256..265])
- *  1:1 mapping of RAM
- *
- * 0x0000850000000000 - 0x0000ffffffffffff (123TB, L0 slots [266..511])
- *  Unused
- */
-
-#define XEN_VIRT_START         _AT(vaddr_t,0x00200000)
-#define FIXMAP_ADDR(n)        (_AT(vaddr_t,0x00400000) + (n) * PAGE_SIZE)
-
-#define BOOT_FDT_VIRT_START    _AT(vaddr_t,0x00600000)
-#define BOOT_FDT_VIRT_SIZE     _AT(vaddr_t, MB(4))
-
-#ifdef CONFIG_LIVEPATCH
-#define LIVEPATCH_VMAP_START   _AT(vaddr_t,0x00a00000)
-#define LIVEPATCH_VMAP_SIZE    _AT(vaddr_t, MB(2))
-#endif
-
-#define HYPERVISOR_VIRT_START  XEN_VIRT_START
-
-#ifdef CONFIG_ARM_32
-
-#define CONFIG_SEPARATE_XENHEAP 1
-
-#define FRAMETABLE_VIRT_START  _AT(vaddr_t,0x02000000)
-#define FRAMETABLE_SIZE        MB(128-32)
-#define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
-#define FRAMETABLE_VIRT_END    (FRAMETABLE_VIRT_START + FRAMETABLE_SIZE - 1)
-
-#define VMAP_VIRT_START        _AT(vaddr_t,0x10000000)
-#define VMAP_VIRT_SIZE         _AT(vaddr_t, GB(1) - MB(256))
-
-#define XENHEAP_VIRT_START     _AT(vaddr_t,0x40000000)
-#define XENHEAP_VIRT_SIZE      _AT(vaddr_t, GB(1))
-
-#define DOMHEAP_VIRT_START     _AT(vaddr_t,0x80000000)
-#define DOMHEAP_VIRT_SIZE      _AT(vaddr_t, GB(2))
-
-#define DOMHEAP_ENTRIES        1024  /* 1024 2MB mapping slots */
-
-/* Number of domheap pagetable pages required at the second level (2MB mappings) */
-#define DOMHEAP_SECOND_PAGES (DOMHEAP_VIRT_SIZE >> FIRST_SHIFT)
-
-#else /* ARM_64 */
-
-#define SLOT0_ENTRY_BITS  39
-#define SLOT0(slot) (_AT(vaddr_t,slot) << SLOT0_ENTRY_BITS)
-#define SLOT0_ENTRY_SIZE  SLOT0(1)
-
-#define VMAP_VIRT_START  GB(1)
-#define VMAP_VIRT_SIZE   GB(1)
-
-#define FRAMETABLE_VIRT_START  GB(32)
-#define FRAMETABLE_SIZE        GB(32)
-#define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
-
-#define DIRECTMAP_VIRT_START   SLOT0(256)
-#define DIRECTMAP_SIZE         (SLOT0_ENTRY_SIZE * (265-256))
-#define DIRECTMAP_VIRT_END     (DIRECTMAP_VIRT_START + DIRECTMAP_SIZE - 1)
-
-#define XENHEAP_VIRT_START     directmap_virt_start
-
-#define HYPERVISOR_VIRT_END    DIRECTMAP_VIRT_END
-
+#ifdef CONFIG_HAS_MPU
+#include <asm/config_mpu.h>
+#else
+#include <asm/config_mmu.h>
 #endif
 
 #define NR_hypercalls 64
xen/arch/arm/include/asm/config_mmu.h new file +112 -0

112 lines not on this page · raw message

xen/arch/arm/include/asm/config_mpu.h new file +25 -0
new file mode 100644
index 0000000000..6b52b11ef7
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * config_mpu.h: A Linux-style configuration list for Arm MPU systems,
+ *               only can be included by config.h
+ */
+
+#ifndef __ARM_CONFIG_MPU_H__
+#define __ARM_CONFIG_MPU_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 PAGE_SIZE"
+#endif
+
+#define XEN_VIRT_START         _AT(paddr_t, XEN_START_ADDRESS)
+
+#define HYPERVISOR_VIRT_START  XEN_VIRT_START
+
+#endif /* __ARM_CONFIG_MPU_H__ */
Signature · 1 line
2.25.1
#33 lore raw

Re: [PATCH v2 10/40] xen/arm: split MMU and MPU config files from config.h

Hi,

Quoted 17 lines · Penny Zheng

From: Wei Chen <[email protected]>

Xen defines some global configuration macros for Arm in config.h. We still want to use it for Armv8-R systems, but there are some address related macros that are defined for MMU systems. These macros will not be used by MPU systems, Adding ifdefery with CONFIG_HAS_MPU to gate these macros will result in a messy and hard-to-read/maintain code.

So we keep some common definitions still in config.h, but move virtual address related definitions to a new file - config_mmu.h. And use a new file config_mpu.h to store definitions for MPU systems. To avoid spreading #ifdef everywhere, we keep the same definition names for MPU systems, like XEN_VIRT_START and HYPERVISOR_VIRT_START, but the definition contents are MPU specific.

Quoted 8 lines

---
v1 -> v2:
1. Remove duplicated FIXMAP definitions from config_mmu.h

---
  xen/arch/arm/include/asm/config.h     | 103 +++--------------------
  xen/arch/arm/include/asm/config_mmu.h | 112 ++++++++++++++++++++++++++
  xen/arch/arm/include/asm/config_mpu.h |  25 ++++++

I think this patch wants to be split in two. So we keep code movement separate from the introduction of new feature (e.g. MPU).

Furthermore, I think it would be better to name the new header layout_* (or similar).

Lastly, you are going to introduce several file with _mmu or _mpu. I would rather prefer if we create directory instead.

xen/arch/arm/include/asm/config.h line 51

Quoted 13 lines
  3 files changed, 147 insertions(+), 93 deletions(-)

create mode 100644 xen/arch/arm/include/asm/config_mmu.h create mode 100644 xen/arch/arm/include/asm/config_mpu.h

xen/arch/arm/include/asm/config.h +1 -0
index 25a625ff08..86d8142959 100644
@@ -48,6 +48,12 @@
 
  #define INVALID_VCPU_ID MAX_VIRT_CPUS
 
+/* Used for calculating PDX */

I am not entirely sure to understand the purpose of this comment.

xen/arch/arm/include/asm/config.h line 56

Quoted 5 lines
xen/arch/arm/include/asm/config.h +5 -0
+#ifdef CONFIG_ARM_64
+#define FRAMETABLE_SIZE        GB(32)
+#define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
+#endif
+

Why do you only keep the 64-bit version in config.h?

However... the frametable size is limited by the space we reserve in the virtual address space. This would not be the case for the MPU.

So having the limit in common seems a bit odd. In fact, I think we should look at getting rid of the limit for the MPU.

[...]

xen/arch/arm/include/asm/config_mmu.h line 5

Quoted 11 lines
xen/arch/arm/include/asm/config_mmu.h new file +5 -0
new file mode 100644
index 0000000000..c12ff25cf4
@@ -0,0 +1,112 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/******************************************************************************
+ * config_mmu.h
+ *
+ * A Linux-style configuration list, only can be included by config.h

Why do you need to restrict where this is included? And if you really need it, then you should check it.

Cheers,

Signature · 1 line
Julien Grall
#34 lore raw

Re: [PATCH v2 10/40] xen/arm: split MMU and MPU config files from config.h

Hi,

Sorry for the late reply. Got sidetracked by other tasks...

Quoted 58 lines · Julien Grall

Hi,

Quoted 17 lines · Penny Zheng

From: Wei Chen <[email protected]>

Xen defines some global configuration macros for Arm in config.h. We still want to use it for Armv8-R systems, but there are some address related macros that are defined for MMU systems. These macros will not be used by MPU systems, Adding ifdefery with CONFIG_HAS_MPU to gate these macros will result in a messy and hard-to-read/maintain code.

So we keep some common definitions still in config.h, but move virtual address related definitions to a new file - config_mmu.h. And use a new file config_mpu.h to store definitions for MPU systems. To avoid spreading #ifdef everywhere, we keep the same definition names for MPU systems, like XEN_VIRT_START and HYPERVISOR_VIRT_START, but the definition contents are MPU specific.

Quoted 8 lines

---
v1 -> v2:
1. Remove duplicated FIXMAP definitions from config_mmu.h
---
  xen/arch/arm/include/asm/config.h     | 103 +++-------------------- xen/arch/arm/include/asm/config_mmu.h | 112 ++++++++++++++++++++++++++ xen/arch/arm/include/asm/config_mpu.h |  25 ++++++

I think this patch wants to be split in two. So we keep code movement separate from the introduction of new feature (e.g. MPU).

Furthermore, I think it would be better to name the new header layout_* (or similar).

Lastly, you are going to introduce several file with _mmu or _mpu. I would rather prefer if we create directory instead.

Quoted 11 lines
  3 files changed, 147 insertions(+), 93 deletions(-)

  create mode 100644 xen/arch/arm/include/asm/config_mmu.h create mode 100644 xen/arch/arm/include/asm/config_mpu.h

xen/arch/arm/include/asm/config.h +0 -0
index 25a625ff08..86d8142959 100644
@@ -48,6 +48,12 @@
     #define INVALID_VCPU_ID MAX_VIRT_CPUS

  +/* Used for calculating PDX */

I am not entirely sure to understand the purpose of this comment.

xen/arch/arm/include/asm/config.h line 56

Quoted 5 lines
xen/arch/arm/include/asm/config.h +5 -0
+#ifdef CONFIG_ARM_64
+#define FfaRAMETABLE_SIZE        GB(32)
+#define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
+#endif
+

Why do you only keep the 64-bit version in config.h?

Quoted 2 lines

However... the frametable size is limited by the space we reserve in the virtual address space. This would not be the case for the MPU.

Yes, but when calculating variable /pdx_group_valid/, which is defined as '''

/unsigned long __read_mostly pdx_group_valid[BITS_TO_LONGS(/
                      (/FRAMETABLE_NR + PDX_GROUP_COUNT - 1) / PDX_GROUP_COUNT)] = { [0] = 1 }/ ''',

it relies on FRAMETABLE_NR to limit array length. If we are trying to get rid of the limit for the MPU, hmmm,

it may bring a lot of changes in pdx common codes, for example, maybe variable /pdx_group_valid/ needs to

be allocated in runtime, according actual frametable size, at least for MPU case.

So, here, I intend to keep the same limit as MMU has for MPU too, or any suggestion from you?

Quoted 3 lines

So having the limit in common seems a bit odd. In fact, I think we should look at getting rid of the limit for the MPU.

[...]

[...]

Quoted 2 lines

Cheers,

Cheers,

Penny Zheng

#35 lore raw

PATCH v2 11/40 xen/mpu: build up start-of-day Xen MPU memory region map

In reply to Penny Zheng

From: Penny Zheng <[email protected]>

The start-of-day Xen MPU memory region layout shall be like as follows:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
......
xen_mpumap[max_xen_mpumap - 2]: Xen init data
xen_mpumap[max_xen_mpumap - 1]: Xen init text

max_xen_mpumap refers to the number of regions supported by the EL2 MPU. The layout shall be compliant with what we describe in xen.lds.S, or the codes need adjustment.

As MMU system and MPU system have different functions to create
the boot MMU/MPU memory management data, instead of introducing extra #ifdef in main code flow, we introduce a neutral name prepare_early_mappings for both, and also to replace create_page_tables for MMU.

---
 xen/arch/arm/arm64/Makefile              |   2 +
 xen/arch/arm/arm64/head.S                |  17 +-
 xen/arch/arm/arm64/head_mmu.S            |   4 +-
 xen/arch/arm/arm64/head_mpu.S            | 323 +++++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/mpu.h     |  63 +++++
 xen/arch/arm/include/asm/arm64/sysregs.h |  49 ++++
 xen/arch/arm/mm_mpu.c                    |  48 ++++
 xen/arch/arm/xen.lds.S                   |   4 +
 8 files changed, 502 insertions(+), 8 deletions(-)
 create mode 100644 xen/arch/arm/arm64/head_mpu.S
 create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h
 create mode 100644 xen/arch/arm/mm_mpu.c
xen/arch/arm/arm64/Makefile +2 -0
index 22da2f54b5..438c9737ad 100644
@@ -10,6 +10,8 @@ obj-y += entry.o
 obj-y += head.o
 ifneq ($(CONFIG_HAS_MPU),y)
 obj-y += head_mmu.o
+else
+obj-y += head_mpu.o
 endif
 obj-y += insn.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
xen/arch/arm/arm64/head.S +11 -6
index 782bd1f94c..145e3d53dc 100644
@@ -68,9 +68,9 @@
  *  x24 -
  *  x25 -
  *  x26 - skip_zero_bss (boot cpu only)
- *  x27 -
- *  x28 -
- *  x29 -
+ *  x27 - region selector (mpu only)
+ *  x28 - prbar (mpu only)
+ *  x29 - prlar (mpu only)
  *  x30 - lr
  */
 
@@ -82,7 +82,7 @@
  * ---------------------------
  *
  * The requirements are:
- *   MMU = off, D-cache = off, I-cache = on or off,
+ *   MMU/MPU = off, D-cache = off, I-cache = on or off,
  *   x0 = physical address to the FDT blob.
  *
  * This must be the very first address in the loaded image.
@@ -252,7 +252,12 @@ real_start_efi:
 
         bl    check_cpu_mode
         bl    cpu_init
-        bl    create_page_tables
+
+        /*
+         * Create boot memory management data, pagetable for MMU systems
+         * and memory regions for MPU systems.
+         */
+        bl    prepare_early_mappings
         bl    enable_mmu
 
         /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
@@ -310,7 +315,7 @@ GLOBAL(init_secondary)
 #endif
         bl    check_cpu_mode
         bl    cpu_init
-        bl    create_page_tables
+        bl    prepare_early_mappings
         bl    enable_mmu
 
         /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
xen/arch/arm/arm64/head_mmu.S +2 -2
index 6ff13c751c..2346f755df 100644
@@ -123,7 +123,7 @@
  *
  * Clobbers x0 - x4
  */
-ENTRY(create_page_tables)
+ENTRY(prepare_early_mappings)
         /* Prepare the page-tables for mapping Xen */
         ldr   x0, =XEN_VIRT_START
         create_table_entry boot_pgtable, boot_first, x0, 0, x1, x2, x3
@@ -208,7 +208,7 @@ virtphys_clash:
         /* Identity map clashes with boot_third, which we cannot handle yet */
         PRINT("- Unable to build boot page tables - virt and phys addresses clash. -\r\n")
         b     fail
-ENDPROC(create_page_tables)
+ENDPROC(prepare_early_mappings)
 
 /*
  * Turn on the Data Cache and the MMU. The function will return on the 1:1
xen/arch/arm/arm64/head_mpu.S new file +323 -0

323 lines not on this page · raw message

xen/arch/arm/include/asm/arm64/mpu.h new file +63 -0

63 lines not on this page · raw message

xen/arch/arm/include/asm/arm64/sysregs.h +49 -0

55 lines not on this page · raw message

xen/arch/arm/mm_mpu.c new file +48 -0

48 lines not on this page · raw message

xen/arch/arm/xen.lds.S +4 -0
index bc45ea2c65..79965a3c17 100644
@@ -91,6 +91,8 @@ SECTIONS
       __ro_after_init_end = .;
   } : text
 
+  . = ALIGN(PAGE_SIZE);
+  __data_begin = .;
   .data.read_mostly : {
        /* Exception table */
        __start___ex_table = .;
@@ -157,7 +159,9 @@ SECTIONS
        *(.altinstr_replacement)
   } :text
   . = ALIGN(PAGE_SIZE);
+
   .init.data : {
+       __init_data_begin = .;            /* Init data */
        *(.init.rodata)
        *(.init.rodata.*)
 
Signature · 1 line
2.25.1
#36 lore raw

Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 117 lines · Penny Zheng

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

From: Penny Zheng <[email protected]>

The start-of-day Xen MPU memory region layout shall be like as follows:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
......
xen_mpumap[max_xen_mpumap - 2]: Xen init data
xen_mpumap[max_xen_mpumap - 1]: Xen init text

max_xen_mpumap refers to the number of regions supported by the EL2 MPU. The layout shall be compliant with what we describe in xen.lds.S, or the codes need adjustment.

As MMU system and MPU system have different functions to create
the boot MMU/MPU memory management data, instead of introducing extra #ifdef in main code flow, we introduce a neutral name prepare_early_mappings for both, and also to replace create_page_tables for MMU.

---
  xen/arch/arm/arm64/Makefile              |   2 +
  xen/arch/arm/arm64/head.S                |  17 +-
  xen/arch/arm/arm64/head_mmu.S            |   4 +-
  xen/arch/arm/arm64/head_mpu.S            | 323 +++++++++++++++++++++++
  xen/arch/arm/include/asm/arm64/mpu.h     |  63 +++++
  xen/arch/arm/include/asm/arm64/sysregs.h |  49 ++++
  xen/arch/arm/mm_mpu.c                    |  48 ++++
  xen/arch/arm/xen.lds.S                   |   4 +
  8 files changed, 502 insertions(+), 8 deletions(-)

create mode 100644 xen/arch/arm/arm64/head_mpu.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h create mode 100644 xen/arch/arm/mm_mpu.c

xen/arch/arm/arm64/Makefile +2 -0
index 22da2f54b5..438c9737ad 100644
@@ -10,6 +10,8 @@ obj-y += entry.o
  obj-y += head.o
  ifneq ($(CONFIG_HAS_MPU),y)
  obj-y += head_mmu.o
+else
+obj-y += head_mpu.o
  endif
  obj-y += insn.o
  obj-$(CONFIG_LIVEPATCH) += livepatch.o
xen/arch/arm/arm64/head.S +11 -6
index 782bd1f94c..145e3d53dc 100644
@@ -68,9 +68,9 @@
   *  x24 -
   *  x25 -
   *  x26 - skip_zero_bss (boot cpu only)
- *  x27 -
- *  x28 -
- *  x29 -
+ *  x27 - region selector (mpu only)
+ *  x28 - prbar (mpu only)
+ *  x29 - prlar (mpu only)
   *  x30 - lr
   */
 
@@ -82,7 +82,7 @@
   * ---------------------------
   *
   * The requirements are:
- *   MMU = off, D-cache = off, I-cache = on or off,
+ *   MMU/MPU = off, D-cache = off, I-cache = on or off,
   *   x0 = physical address to the FDT blob.
   *
   * This must be the very first address in the loaded image.
@@ -252,7 +252,12 @@ real_start_efi:
 
          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
+
+        /*
+         * Create boot memory management data, pagetable for MMU systems
+         * and memory regions for MPU systems.
+         */
+        bl    prepare_early_mappings
          bl    enable_mmu
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
@@ -310,7 +315,7 @@ GLOBAL(init_secondary)
  #endif
          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
+        bl    prepare_early_mappings
          bl    enable_mmu
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
xen/arch/arm/arm64/head_mmu.S +1 -1
index 6ff13c751c..2346f755df 100644
@@ -123,7 +123,7 @@
   *
   * Clobbers x0 - x4
   */
-ENTRY(create_page_tables)
+ENTRY(prepare_early_mappings)
          /* Prepare the page-tables for mapping Xen */
          ldr   x0, =XEN_VIRT_START
          create_table_entry boot_pgtable, boot_first, x0, 0, x1, x2, x3

xen/arch/arm/arm64/head_mmu.S virtphys_clash: · line 211

Quoted 5 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -208,7 +208,7 @@ virtphys_clash:
          /* Identity map clashes with boot_third, which we cannot handle yet */
          PRINT("- Unable to build boot page tables - virt and phys addresses clash. -\r\n")
          b     fail
-ENDPROC(create_page_tables)
+ENDPROC(prepare_early_mappings)

NIT:- Can this renaming be done in a separate patch of its own (before this patch).

So that this patch can be only about the new functionality introduced.

Quoted 12 lines
xen/arch/arm/arm64/head_mmu.S +0 -0
  /*
   * Turn on the Data Cache and the MMU. The function will return on the 1:1
xen/arch/arm/arm64/head_mpu.S new file +3 -0
new file mode 100644
index 0000000000..0b97ce4646
@@ -0,0 +1,323 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Start-of-day code for an Armv8-R AArch64 MPU system.

xen/arch/arm/arm64/head_mpu.S line 13

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ */
+
+#include <asm/arm64/mpu.h>
+#include <asm/early_printk.h>
+#include <asm/page.h>
+
+/*
+ * One entry in Xen MPU memory region mapping table(xen_mpumap) is a structure
+ * of pr_t, which is 16-bytes size, so the entry offset is the order of 4.
+ */

NIT :- It would be good to quote Arm ARM from which can be referred for the definitions.

Quoted 88 lines
xen/arch/arm/arm64/head_mpu.S new file +88 -0
+#define MPU_ENTRY_SHIFT         0x4
+
+#define REGION_SEL_MASK         0xf
+
+#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 round up the section address to be PAGE_SIZE aligned
+ * Each section(e.g. .text, .data, etc) in xen.lds.S is page-aligned,
+ * which is usually guarded with ". = ALIGN(PAGE_SIZE)" in the head,
+ * or in the end
+ */
+.macro roundup_section, xb
+        add   \xb, \xb, #(PAGE_SIZE-1)
+        and   \xb, \xb, #PAGE_MASK
+.endm
+
+/*
+ * Macro to create a new MPU memory region entry, which is a structure
+ * of pr_t,  in \prmap.
+ *
+ * Inputs:
+ * prmap:   mpu memory region map table symbol
+ * sel:     region selector
+ * prbar:   preserve value for PRBAR_EL2
+ * prlar    preserve value for PRLAR_EL2
+ *
+ * Clobbers \tmp1, \tmp2
+ *
+ */
+.macro create_mpu_entry prmap, sel, prbar, prlar, tmp1, tmp2
+    mov   \tmp2, \sel
+    lsl   \tmp2, \tmp2, #MPU_ENTRY_SHIFT
+    adr_l \tmp1, \prmap
+    /* Write the first 8 bytes(prbar_t) of pr_t */
+    str   \prbar, [\tmp1, \tmp2]
+
+    add   \tmp2, \tmp2, #8
+    /* Write the last 8 bytes(prlar_t) of pr_t */
+    str   \prlar, [\tmp1, \tmp2]
+.endm
+
+/*
+ * Macro to store the maximum number of regions supported by the EL2 MPU
+ * in max_xen_mpumap, which is identified by MPUIR_EL2.
+ *
+ * Outputs:
+ * nr_regions: preserve the maximum number of regions supported by the EL2 MPU
+ *
+ * Clobbers \tmp1
+ *
+ */
+.macro read_max_el2_regions, nr_regions, tmp1
+    load_paddr \tmp1, max_xen_mpumap
+    mrs   \nr_regions, MPUIR_EL2
+    isb
+    str   \nr_regions, [\tmp1]
+.endm
+
+/*
+ * Macro to prepare and set a MPU memory region
+ *
+ * Inputs:
+ * base:        base address symbol (should be page-aligned)
+ * limit:       limit address symbol
+ * sel:         region selector
+ * prbar:       store computed PRBAR_EL2 value
+ * prlar:       store computed PRLAR_EL2 value
+ * 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
+ *
+ * Clobber \tmp1
+ *
+ */
+.macro prepare_xen_region, base, limit, sel, prbar, prlar, tmp1, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR
+    /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/
+    load_paddr \prbar, \base
+    and   \prbar, \prbar, #MPU_REGION_MASK
+    mov   \tmp1, #\attr_prbar
+    orr   \prbar, \prbar, \tmp1
+
+    /* Prepare value for PRLAR_EL2 reg and preserve it in \prlar.*/
+    load_paddr \prlar, \limit
+    /* Round up limit address to be PAGE_SIZE aligned */

xen/arch/arm/arm64/head_mpu.S line 111

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+    roundup_section \prlar
+    /* Limit address should be inclusive */
+    sub   \prlar, \prlar, #1
+    and   \prlar, \prlar, #MPU_REGION_MASK
+    mov   \tmp1, #\attr_prlar
+    orr   \prlar, \prlar, \tmp1
+
+    mov   x27, \sel
+    mov   x28, \prbar
+    mov   x29, \prlar

Any reasons for using x27, x28, x29 to pass function parameters.

https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst states x0..x7 should be used (Table 2, General-purpose registers and AAPCS64 usage).

Quoted 419 lines
xen/arch/arm/arm64/head_mpu.S new file +212 -0
+    /*
+     * x27, x28, x29 are special registers designed as
+     * inputs for function write_pr
+     */
+    bl    write_pr
+.endm
+
+.section .text.idmap, "ax", %progbits
+
+/*
+ * ENTRY to configure a EL2 MPU memory region
+ * ARMv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for ARMv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provides access to the EL2 MPU region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n.(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR0_EL2 and PRLAR0_EL2
+ * - Region 17 configuration is accessible through PRBAR1_EL2 and PRLAR1_EL2
+ * - Region 18 configuration is accessible through PRBAR2_EL2 and PRLAR2_EL2
+ * - ...
+ * - Region 31 configuration is accessible through PRBAR15_EL2 and PRLAR15_EL2
+ *
+ * Inputs:
+ * x27: region selector
+ * x28: preserve value for PRBAR_EL2
+ * x29: preserve value for PRLAR_EL2
+ *
+ */
+ENTRY(write_pr)
+    msr   PRSELR_EL2, x27
+    dsb   sy
+    and   x27, x27, #REGION_SEL_MASK
+    cmp   x27, #0
+    bne   1f
+    msr   PRBAR0_EL2, x28
+    msr   PRLAR0_EL2, x29
+    b     out
+1:
+    cmp   x27, #1
+    bne   2f
+    msr   PRBAR1_EL2, x28
+    msr   PRLAR1_EL2, x29
+    b     out
+2:
+    cmp   x27, #2
+    bne   3f
+    msr   PRBAR2_EL2, x28
+    msr   PRLAR2_EL2, x29
+    b     out
+3:
+    cmp   x27, #3
+    bne   4f
+    msr   PRBAR3_EL2, x28
+    msr   PRLAR3_EL2, x29
+    b     out
+4:
+    cmp   x27, #4
+    bne   5f
+    msr   PRBAR4_EL2, x28
+    msr   PRLAR4_EL2, x29
+    b     out
+5:
+    cmp   x27, #5
+    bne   6f
+    msr   PRBAR5_EL2, x28
+    msr   PRLAR5_EL2, x29
+    b     out
+6:
+    cmp   x27, #6
+    bne   7f
+    msr   PRBAR6_EL2, x28
+    msr   PRLAR6_EL2, x29
+    b     out
+7:
+    cmp   x27, #7
+    bne   8f
+    msr   PRBAR7_EL2, x28
+    msr   PRLAR7_EL2, x29
+    b     out
+8:
+    cmp   x27, #8
+    bne   9f
+    msr   PRBAR8_EL2, x28
+    msr   PRLAR8_EL2, x29
+    b     out
+9:
+    cmp   x27, #9
+    bne   10f
+    msr   PRBAR9_EL2, x28
+    msr   PRLAR9_EL2, x29
+    b     out
+10:
+    cmp   x27, #10
+    bne   11f
+    msr   PRBAR10_EL2, x28
+    msr   PRLAR10_EL2, x29
+    b     out
+11:
+    cmp   x27, #11
+    bne   12f
+    msr   PRBAR11_EL2, x28
+    msr   PRLAR11_EL2, x29
+    b     out
+12:
+    cmp   x27, #12
+    bne   13f
+    msr   PRBAR12_EL2, x28
+    msr   PRLAR12_EL2, x29
+    b     out
+13:
+    cmp   x27, #13
+    bne   14f
+    msr   PRBAR13_EL2, x28
+    msr   PRLAR13_EL2, x29
+    b     out
+14:
+    cmp   x27, #14
+    bne   15f
+    msr   PRBAR14_EL2, x28
+    msr   PRLAR14_EL2, x29
+    b     out
+15:
+    msr   PRBAR15_EL2, x28
+    msr   PRLAR15_EL2, x29
+out:
+    isb
+    ret
+ENDPROC(write_pr)
+
+/*
+ * Static start-of-day Xen EL2 MPU memory region layout.
+ *
+ *     xen_mpumap[0] : Xen text
+ *     xen_mpumap[1] : Xen read-only data
+ *     xen_mpumap[2] : Xen read-only after init data
+ *     xen_mpumap[3] : Xen read-write data
+ *     xen_mpumap[4] : Xen BSS
+ *     ......
+ *     xen_mpumap[max_xen_mpumap - 2]: Xen init data
+ *     xen_mpumap[max_xen_mpumap - 1]: Xen init text
+ *
+ * Clobbers x0 - x6
+ *
+ * It shall be compliant with what describes in xen.lds.S, or the below
+ * codes need adjustment.
+ * It shall also follow the rules of putting fixed MPU memory region in
+ * the front, and the others in the rear, which, here, mainly refers to
+ * boot-only region, like Xen init text region.
+ */
+ENTRY(prepare_early_mappings)
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x6, lr
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    prepare_xen_region _stext, _etext, x0, x1, x2, x3, attr_prbar=REGION_TEXT_PRBAR
+    create_mpu_entry xen_mpumap, x0, x1, x2, x3, x4
+
+    add   x0, x0, #1
+    /* Xen read-only data section. */
+    prepare_xen_region _srodata, _erodata, x0, x1, x2, x3, attr_prbar=REGION_RO_PRBAR
+    create_mpu_entry xen_mpumap, x0, x1, x2, x3, x4
+
+    add   x0, x0, #1
+    /* Xen read-only after init data section. */
+    prepare_xen_region __ro_after_init_start, __ro_after_init_end, x0, x1, x2, x3
+    create_mpu_entry xen_mpumap, x0, x1, x2, x3, x4
+
+    add   x0, x0, #1
+    /* Xen read-write data section. */
+    prepare_xen_region __data_begin, __init_begin, x0, x1, x2, x3
+    create_mpu_entry xen_mpumap, x0, x1, x2, x3, x4
+
+    read_max_el2_regions x5, x3 /* x5: max_mpumap */
+    sub   x5, x5, #1
+    /* Xen init text section. */
+    prepare_xen_region _sinittext, _einittext, x5, x1, x2, x3, attr_prbar=REGION_TEXT_PRBAR
+    create_mpu_entry xen_mpumap, x5, x1, x2, x3, x4
+
+    sub   x5, x5, #1
+    /* Xen init data section. */
+    prepare_xen_region __init_data_begin, __init_end, x5, x1, x2, x3
+    create_mpu_entry xen_mpumap, x5, x1, x2, x3, x4
+
+    add   x0, x0, #1
+    /* Xen BSS section. */
+    prepare_xen_region __bss_start, __bss_end, x0, x1, x2, x3
+    create_mpu_entry xen_mpumap, x0, x1, x2, x3, x4
+
+    /* Update next_fixed_region_idx and next_transient_region_idx */
+    load_paddr x3, next_fixed_region_idx
+    add   x0, x0, #1
+    str   x0, [x3]
+    load_paddr x4, next_transient_region_idx
+    sub   x5, x5, #1
+    str   x5, [x4]
+
+    mov   lr, x6
+    ret
+ENDPROC(prepare_early_mappings)
+
+GLOBAL(_end_boot)
+
+/*
+ * Local variables:
+ * mode: ASM
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/arm64/mpu.h new file +63 -0
new file mode 100644
index 0000000000..c945dd53db
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * mpu.h: Arm Memory Protection Region definitions.
+ */
+
+#ifndef __ARM64_MPU_H__
+#define __ARM64_MPU_H__
+
+#define MPU_REGION_SHIFT  6
+#define MPU_REGION_ALIGN  (_AC(1, UL) << MPU_REGION_SHIFT)
+#define MPU_REGION_MASK   (~(MPU_REGION_ALIGN - 1))
+
+/*
+ * MPUIR_EL2.Region identifies the number of regions supported by the EL2 MPU.
+ * It is a 8-bit field, so 255 MPU memory regions at most.
+ */
+#define ARM_MAX_MPU_MEMORY_REGIONS 255
+
+#ifndef __ASSEMBLY__
+
+/* Protection Region Base Address Register */
+typedef union {
+    struct __packed {
+        unsigned long xn:2;       /* Execute-Never */
+        unsigned long ap:2;       /* Acess Permission */
+        unsigned long sh:2;       /* Sharebility */
+        unsigned long base:42;    /* Base Address */
+        unsigned long pad:16;
+    } reg;
+    uint64_t bits;
+} prbar_t;
+
+/* Protection Region Limit Address Register */
+typedef union {
+    struct __packed {
+        unsigned long en:1;     /* Region enable */
+        unsigned long ai:3;     /* Memory Attribute Index */
+        unsigned long ns:1;     /* Not-Secure */
+        unsigned long res:1;    /* Reserved 0 by hardware */
+        unsigned long limit:42; /* Limit Address */
+        unsigned long pad:16;
+    } reg;
+    uint64_t bits;
+} prlar_t;
+
+/* MPU Protection Region */
+typedef struct {
+    prbar_t prbar;
+    prlar_t prlar;
+} pr_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ARM64_MPU_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/arm64/sysregs.h +49 -0
index 4638999514..aca9bca5b1 100644
@@ -458,6 +458,55 @@
  #define ZCR_ELx_LEN_SIZE             9
  #define ZCR_ELx_LEN_MASK             0x1ff
 
+/* System registers for Armv8-R AArch64 */
+#ifdef CONFIG_HAS_MPU
+
+/* EL2 MPU Protection Region Base Address Register encode */
+#define PRBAR_EL2   S3_4_C6_C8_0
+#define PRBAR0_EL2  S3_4_C6_C8_0
+#define PRBAR1_EL2  S3_4_C6_C8_4
+#define PRBAR2_EL2  S3_4_C6_C9_0
+#define PRBAR3_EL2  S3_4_C6_C9_4
+#define PRBAR4_EL2  S3_4_C6_C10_0
+#define PRBAR5_EL2  S3_4_C6_C10_4
+#define PRBAR6_EL2  S3_4_C6_C11_0
+#define PRBAR7_EL2  S3_4_C6_C11_4
+#define PRBAR8_EL2  S3_4_C6_C12_0
+#define PRBAR9_EL2  S3_4_C6_C12_4
+#define PRBAR10_EL2 S3_4_C6_C13_0
+#define PRBAR11_EL2 S3_4_C6_C13_4
+#define PRBAR12_EL2 S3_4_C6_C14_0
+#define PRBAR13_EL2 S3_4_C6_C14_4
+#define PRBAR14_EL2 S3_4_C6_C15_0
+#define PRBAR15_EL2 S3_4_C6_C15_4
+
+/* EL2 MPU Protection Region Limit Address Register encode */
+#define PRLAR_EL2   S3_4_C6_C8_1
+#define PRLAR0_EL2  S3_4_C6_C8_1
+#define PRLAR1_EL2  S3_4_C6_C8_5
+#define PRLAR2_EL2  S3_4_C6_C9_1
+#define PRLAR3_EL2  S3_4_C6_C9_5
+#define PRLAR4_EL2  S3_4_C6_C10_1
+#define PRLAR5_EL2  S3_4_C6_C10_5
+#define PRLAR6_EL2  S3_4_C6_C11_1
+#define PRLAR7_EL2  S3_4_C6_C11_5
+#define PRLAR8_EL2  S3_4_C6_C12_1
+#define PRLAR9_EL2  S3_4_C6_C12_5
+#define PRLAR10_EL2 S3_4_C6_C13_1
+#define PRLAR11_EL2 S3_4_C6_C13_5
+#define PRLAR12_EL2 S3_4_C6_C14_1
+#define PRLAR13_EL2 S3_4_C6_C14_5
+#define PRLAR14_EL2 S3_4_C6_C15_1
+#define PRLAR15_EL2 S3_4_C6_C15_5
+
+/* MPU Protection Region Selection Register encode */
+#define PRSELR_EL2 S3_4_C6_C2_1
+
+/* MPU Type registers encode */
+#define MPUIR_EL2 S3_4_C0_C0_4
+
+#endif
+
  /* Access to system registers */
 
  #define WRITE_SYSREG64(v, name) do {                    \
xen/arch/arm/mm_mpu.c new file +48 -0
new file mode 100644
index 0000000000..43e9a1be4d
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * xen/arch/arm/mm_mpu.c
+ *
+ * MPU based memory managment code for Armv8-R AArch64.
+ *
+ * Copyright (C) 2022 Arm Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <xen/init.h>
+#include <xen/page-size.h>
+#include <asm/arm64/mpu.h>
+
+/* Xen MPU memory region mapping table. */
+pr_t __aligned(PAGE_SIZE) __section(".data.page_aligned")
+     xen_mpumap[ARM_MAX_MPU_MEMORY_REGIONS];
+
+/* Index into MPU memory region map for fixed regions, ascending from zero. */
+uint64_t __ro_after_init next_fixed_region_idx;
+/*
+ * Index into MPU memory region map for transient regions, like boot-only
+ * region, which descends from max_xen_mpumap.
+ */
+uint64_t __ro_after_init next_transient_region_idx;
+
+/* Maximum number of supported MPU memory regions by the EL2 MPU. */
+uint64_t __ro_after_init max_xen_mpumap;
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/xen.lds.S +4 -1
index bc45ea2c65..79965a3c17 100644
@@ -91,6 +91,8 @@ SECTIONS
        __ro_after_init_end = .;
    } : text
 
+  . = ALIGN(PAGE_SIZE);
+  __data_begin = .;
    .data.read_mostly : {
         /* Exception table */
         __start___ex_table = .;
@@ -157,7 +159,9 @@ SECTIONS
         *(.altinstr_replacement)
    } :text
    . = ALIGN(PAGE_SIZE);
+
    .init.data : {
+       __init_data_begin = .;            /* Init data */
         *(.init.rodata)
         *(.init.rodata.*)
 
--
Quoted 2 lines

2.25.1

NIT:- Would you consider splitting this patch, something like this :-

1. Renaming of the mmu function

2. Define sysregs, prlar_t, prbar_t and other other hardware specific macros.

3. Define write_pr

4. The rest of the changes (ie prepare_early_mappings(), xen.lds.S, etc)

- Ayan

#37 lore raw

RE: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Ayan

Quoted 138 lines

-----Original Message-----
From: Ayan Kumar Halder <[email protected]>
Sent: Thursday, January 19, 2023 6:19 PM
To: [email protected]
Cc: Wei Chen <[email protected]>; Penny Zheng <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Bertrand Marquis <[email protected]>; [email protected]
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 1 line · Penny Zheng

CAUTION: This message has originated from an External Source. Please use

proper judgment and caution when opening attachments, clicking links, or responding to this email.

Quoted 6 lines

From: Penny Zheng <[email protected]>

The start-of-day Xen MPU memory region layout shall be like as follows:

Quoted 8 lines

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS ...... xen_mpumap[max_xen_mpumap - 2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

max_xen_mpumap refers to the number of regions supported by the EL2

MPU.

Quoted 7 lines

The layout shall be compliant with what we describe in xen.lds.S, or the codes need adjustment.

As MMU system and MPU system have different functions to create the boot MMU/MPU memory management data, instead of introducing extra #ifdef in main code flow, we introduce a neutral name prepare_early_mappings for both, and also to replace create_page_tables

for MMU.

Quoted 8 lines
---
  xen/arch/arm/arm64/Makefile              |   2 +
  xen/arch/arm/arm64/head.S                |  17 +-
  xen/arch/arm/arm64/head_mmu.S            |   4 +-
  xen/arch/arm/arm64/head_mpu.S            | 323

+++++++++++++++++++++++

Review

Quoted 10 lines
  xen/arch/arm/include/asm/arm64/mpu.h     |  63 +++++
  xen/arch/arm/include/asm/arm64/sysregs.h |  49 ++++
  xen/arch/arm/mm_mpu.c                    |  48 ++++
  xen/arch/arm/xen.lds.S                   |   4 +
  8 files changed, 502 insertions(+), 8 deletions(-)

create mode 100644 xen/arch/arm/arm64/head_mpu.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h create mode 100644 xen/arch/arm/mm_mpu.c

(quoted diff) +0 -0

b/xen/arch/arm/arm64/Makefile

Quoted 39 lines

index 22da2f54b5..438c9737ad 100644

xen/arch/arm/arm64/Makefile +2 -0
@@ -10,6 +10,8 @@ obj-y += entry.o
  obj-y += head.o
  ifneq ($(CONFIG_HAS_MPU),y)
  obj-y += head_mmu.o
+else
+obj-y += head_mpu.o
  endif
  obj-y += insn.o
  obj-$(CONFIG_LIVEPATCH) += livepatch.o diff --git

a/xen/arch/arm/arm64/head.S b/xen/arch/arm/arm64/head.S index 782bd1f94c..145e3d53dc 100644

xen/arch/arm/arm64/head.S +4 -4
@@ -68,9 +68,9 @@
   *  x24 -
   *  x25 -
   *  x26 - skip_zero_bss (boot cpu only)
- *  x27 -
- *  x28 -
- *  x29 -
+ *  x27 - region selector (mpu only)
+ *  x28 - prbar (mpu only)
+ *  x29 - prlar (mpu only)
   *  x30 - lr
   */
 
@@ -82,7 +82,7 @@
   * ---------------------------
   *
   * The requirements are:
- *   MMU = off, D-cache = off, I-cache = on or off,
+ *   MMU/MPU = off, D-cache = off, I-cache = on or off,
   *   x0 = physical address to the FDT blob.
   *
   * This must be the very first address in the loaded image.

xen/arch/arm/arm64/head.S real_start_efi: · line 257

Quoted 7 lines
xen/arch/arm/arm64/head.S +3 -1
@@ -252,7 +252,12 @@ real_start_efi:
 
          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
+
+        /*
+         * Create boot memory management data, pagetable for MMU

systems

Quoted 15 lines
xen/arch/arm/arm64/head.S +3 -0
+         * and memory regions for MPU systems.
+         */
+        bl    prepare_early_mappings
          bl    enable_mmu
 
          /* We are still in the 1:1 mapping. Jump to the runtime

Virtual Address. */ @@ -310,7 +315,7 @@ GLOBAL(init_secondary)
#endif
bl check_cpu_mode
bl cpu_init

xen/arch/arm/arm64/head.S +1 -1
-        bl    create_page_tables
+        bl    prepare_early_mappings
          bl    enable_mmu
 
          /* We are still in the 1:1 mapping. Jump to the runtime
Quoted 2 lines

Virtual Address. */ diff --git a/xen/arch/arm/arm64/head_mmu.S b/xen/arch/arm/arm64/head_mmu.S index 6ff13c751c..2346f755df

100644

Quoted 12 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -123,7 +123,7 @@
   *
   * Clobbers x0 - x4
   */
-ENTRY(create_page_tables)
+ENTRY(prepare_early_mappings)
          /* Prepare the page-tables for mapping Xen */
          ldr   x0, =XEN_VIRT_START
          create_table_entry boot_pgtable, boot_first, x0, 0, x1, x2,

x3 @@ -208,7 +208,7 @@ virtphys_clash:

Quoted 1 line
          /* Identity map clashes with boot_third, which we cannot handle yet

*/

Quoted 9 lines
Quoted 1 line

PRINT("- Unable to build boot page tables - virt and phys addresses

clash. -\r\n")

xen/arch/arm/arm64/head_mmu.S virtphys_clash: · line 211

Quoted 3 lines

b fail

xen/arch/arm/arm64/head_mmu.S +1 -1
-ENDPROC(create_page_tables)
+ENDPROC(prepare_early_mappings)

NIT:- Can this renaming be done in a separate patch of its own (before this patch).

Yay, you're right. I'll put it in different commit.

Quoted 135 lines

So that this patch can be only about the new functionality introduced.

Quoted 10 lines

/*
* Turn on the Data Cache and the MMU. The function will return on
the 1:1 diff --git a/xen/arch/arm/arm64/head_mpu.S b/xen/arch/arm/arm64/head_mpu.S new file mode 100644 index 0000000000..0b97ce4646

xen/arch/arm/arm64/head_mpu.S new file +1 -0
@@ -0,0 +1,323 @@
+/* SPDX-License-Identifier: GPL-2.0-only */

xen/arch/arm/arm64/head_mpu.S line 11

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+/*
+ * Start-of-day code for an Armv8-R AArch64 MPU system.
+ */
+
+#include <asm/arm64/mpu.h>
+#include <asm/early_printk.h>
+#include <asm/page.h>
+
+/*
+ * One entry in Xen MPU memory region mapping table(xen_mpumap) is

a

xen/arch/arm/arm64/head_mpu.S line 13

Quoted 3 lines
xen/arch/arm/arm64/head_mpu.S new file +3 -0
+structure
+ * of pr_t, which is 16-bytes size, so the entry offset is the order of 4.
+ */

NIT :- It would be good to quote Arm ARM from which can be referred for the definitions.

Quoted 74 lines
xen/arch/arm/arm64/head_mpu.S new file +74 -0
+#define MPU_ENTRY_SHIFT         0x4
+
+#define REGION_SEL_MASK         0xf
+
+#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 round up the section address to be PAGE_SIZE aligned
+ * Each section(e.g. .text, .data, etc) in xen.lds.S is page-aligned,
+ * which is usually guarded with ". = ALIGN(PAGE_SIZE)" in the head,
+ * or in the end
+ */
+.macro roundup_section, xb
+        add   \xb, \xb, #(PAGE_SIZE-1)
+        and   \xb, \xb, #PAGE_MASK
+.endm
+
+/*
+ * Macro to create a new MPU memory region entry, which is a
+structure
+ * of pr_t,  in \prmap.
+ *
+ * Inputs:
+ * prmap:   mpu memory region map table symbol
+ * sel:     region selector
+ * prbar:   preserve value for PRBAR_EL2
+ * prlar    preserve value for PRLAR_EL2
+ *
+ * Clobbers \tmp1, \tmp2
+ *
+ */
+.macro create_mpu_entry prmap, sel, prbar, prlar, tmp1, tmp2
+    mov   \tmp2, \sel
+    lsl   \tmp2, \tmp2, #MPU_ENTRY_SHIFT
+    adr_l \tmp1, \prmap
+    /* Write the first 8 bytes(prbar_t) of pr_t */
+    str   \prbar, [\tmp1, \tmp2]
+
+    add   \tmp2, \tmp2, #8
+    /* Write the last 8 bytes(prlar_t) of pr_t */
+    str   \prlar, [\tmp1, \tmp2]
+.endm
+
+/*
+ * Macro to store the maximum number of regions supported by the EL2
+MPU
+ * in max_xen_mpumap, which is identified by MPUIR_EL2.
+ *
+ * Outputs:
+ * nr_regions: preserve the maximum number of regions supported by
+the EL2 MPU
+ *
+ * Clobbers \tmp1
+ *
+ */
+.macro read_max_el2_regions, nr_regions, tmp1
+    load_paddr \tmp1, max_xen_mpumap
+    mrs   \nr_regions, MPUIR_EL2
+    isb
+    str   \nr_regions, [\tmp1]
+.endm
+
+/*
+ * Macro to prepare and set a MPU memory region
+ *
+ * Inputs:
+ * base:        base address symbol (should be page-aligned)
+ * limit:       limit address symbol
+ * sel:         region selector
+ * prbar:       store computed PRBAR_EL2 value

xen/arch/arm/arm64/head_mpu.S

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ * prlar:       store computed PRLAR_EL2 value
+ * 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
+ *
+ * Clobber \tmp1
+ *
+ */
+.macro prepare_xen_region, base, limit, sel, prbar, prlar, tmp1,

attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR

Quoted 9 lines
xen/arch/arm/arm64/head_mpu.S new file +9 -0
+    /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/
+    load_paddr \prbar, \base
+    and   \prbar, \prbar, #MPU_REGION_MASK
+    mov   \tmp1, #\attr_prbar
+    orr   \prbar, \prbar, \tmp1
+
+    /* Prepare value for PRLAR_EL2 reg and preserve it in \prlar.*/
+    load_paddr \prlar, \limit
+    /* Round up limit address to be PAGE_SIZE aligned */

xen/arch/arm/arm64/head_mpu.S line 111

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+    roundup_section \prlar
+    /* Limit address should be inclusive */
+    sub   \prlar, \prlar, #1
+    and   \prlar, \prlar, #MPU_REGION_MASK
+    mov   \tmp1, #\attr_prlar
+    orr   \prlar, \prlar, \tmp1
+
+    mov   x27, \sel
+    mov   x28, \prbar
+    mov   x29, \prlar

Any reasons for using x27, x28, x29 to pass function parameters.

Quoted 4 lines

https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst
states x0..x7 should be used (Table 2, General-purpose registers and AAPCS64 usage).

These registers are documented and reserved in xen/arch/arm/arm64/head.S, like how we reserve x26 to pass function parameter in skip_zero_bss, see

```
diff --git a/xen/arch/arm/arm64/head.S b/xen/arch/arm/arm64/head.S
index 782bd1f94c..145e3d53dc 100644
--- a/xen/arch/arm/arm64/head.S
+++ b/xen/arch/arm/arm64/head.S
@@ -68,9 +68,9 @@
  *  x24 -
  *  x25 -
  *  x26 - skip_zero_bss (boot cpu only)
- *  x27 -
- *  x28 -
- *  x29 -
+ *  x27 - region selector (mpu only)
+ *  x28 - prbar (mpu only)
+ *  x29 - prlar (mpu only)
  *  x30 - lr
  */
```

x0...x7 are already commonly used in xen/arch/arm/arm64/head.S, it is difficult for me to preserve them only for write_pr.

If we are using x0...x7 as function parameter, I need to stack/pop them to mutate stack operation in write_pr to avoid corruption.

Quoted 7 lines
Quoted 7 lines
xen/arch/arm/arm64/head_mpu.S new file +7 -0
+    /*
+     * x2skip_zero7, x28, x29 are special registers designed as
+     * inputs for function write_pr
+     */
+    bl    write_pr
+.endm
+

[...]

Quoted 5 lines
Quoted 3 lines

--
2.25.1

NIT:- Would you consider splitting this patch, something like this :-

Quoted 8 lines

1. Renaming of the mmu function

2. Define sysregs, prlar_t, prbar_t and other other hardware specific macros.

3. Define write_pr

4. The rest of the changes (ie prepare_early_mappings(), xen.lds.S, etc)

For 2, 3 and 4, it will break the rule of "Always define and introduce at the first usage".
However, I know that this commit is very big ;/, so as long as maintainers are also in favor of your splitting suggestion, I'm happy to do the split too~

Quoted 1 line

- Ayan

#38 lore raw

Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

In reply to Penny Zheng

Hi Penny,

Quoted 4 lines · Penny Zheng

From: Penny Zheng <[email protected]>

The start-of-day Xen MPU memory region layout shall be like as follows:

Quoted 8 lines

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
......
xen_mpumap[max_xen_mpumap - 2]: Xen init data
xen_mpumap[max_xen_mpumap - 1]: Xen init text

Can you explain why the init region should be at the end of the MPU?

Quoted 162 lines

max_xen_mpumap refers to the number of regions supported by the EL2 MPU. The layout shall be compliant with what we describe in xen.lds.S, or the codes need adjustment.

As MMU system and MPU system have different functions to create
the boot MMU/MPU memory management data, instead of introducing extra #ifdef in main code flow, we introduce a neutral name prepare_early_mappings for both, and also to replace create_page_tables for MMU.

---
  xen/arch/arm/arm64/Makefile              |   2 +
  xen/arch/arm/arm64/head.S                |  17 +-
  xen/arch/arm/arm64/head_mmu.S            |   4 +-
  xen/arch/arm/arm64/head_mpu.S            | 323 +++++++++++++++++++++++
  xen/arch/arm/include/asm/arm64/mpu.h     |  63 +++++
  xen/arch/arm/include/asm/arm64/sysregs.h |  49 ++++
  xen/arch/arm/mm_mpu.c                    |  48 ++++
  xen/arch/arm/xen.lds.S                   |   4 +
  8 files changed, 502 insertions(+), 8 deletions(-)

create mode 100644 xen/arch/arm/arm64/head_mpu.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h create mode 100644 xen/arch/arm/mm_mpu.c

xen/arch/arm/arm64/Makefile +2 -0
index 22da2f54b5..438c9737ad 100644
@@ -10,6 +10,8 @@ obj-y += entry.o
  obj-y += head.o
  ifneq ($(CONFIG_HAS_MPU),y)
  obj-y += head_mmu.o
+else
+obj-y += head_mpu.o
  endif
  obj-y += insn.o
  obj-$(CONFIG_LIVEPATCH) += livepatch.o
xen/arch/arm/arm64/head.S +11 -6
index 782bd1f94c..145e3d53dc 100644
@@ -68,9 +68,9 @@
   *  x24 -
   *  x25 -
   *  x26 - skip_zero_bss (boot cpu only)
- *  x27 -
- *  x28 -
- *  x29 -
+ *  x27 - region selector (mpu only)
+ *  x28 - prbar (mpu only)
+ *  x29 - prlar (mpu only) >    *  x30 - lr
   */
 
@@ -82,7 +82,7 @@
   * ---------------------------
   *
   * The requirements are:
- *   MMU = off, D-cache = off, I-cache = on or off,
+ *   MMU/MPU = off, D-cache = off, I-cache = on or off,
   *   x0 = physical address to the FDT blob.
   *
   * This must be the very first address in the loaded image.
@@ -252,7 +252,12 @@ real_start_efi:
 
          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
+
+        /*
+         * Create boot memory management data, pagetable for MMU systems
+         * and memory regions for MPU systems.
+         */
+        bl    prepare_early_mappings
          bl    enable_mmu
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
@@ -310,7 +315,7 @@ GLOBAL(init_secondary)
  #endif
          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
+        bl    prepare_early_mappings
          bl    enable_mmu
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
xen/arch/arm/arm64/head_mmu.S +2 -2
index 6ff13c751c..2346f755df 100644
@@ -123,7 +123,7 @@
   *
   * Clobbers x0 - x4
   */
-ENTRY(create_page_tables)
+ENTRY(prepare_early_mappings)
          /* Prepare the page-tables for mapping Xen */
          ldr   x0, =XEN_VIRT_START
          create_table_entry boot_pgtable, boot_first, x0, 0, x1, x2, x3
@@ -208,7 +208,7 @@ virtphys_clash:
          /* Identity map clashes with boot_third, which we cannot handle yet */
          PRINT("- Unable to build boot page tables - virt and phys addresses clash. -\r\n")
          b     fail
-ENDPROC(create_page_tables)
+ENDPROC(prepare_early_mappings)
 
  /*
   * Turn on the Data Cache and the MMU. The function will return on the 1:1
xen/arch/arm/arm64/head_mpu.S new file +47 -0
new file mode 100644
index 0000000000..0b97ce4646
@@ -0,0 +1,323 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Start-of-day code for an Armv8-R AArch64 MPU system.
+ */
+
+#include <asm/arm64/mpu.h>
+#include <asm/early_printk.h>
+#include <asm/page.h>
+
+/*
+ * One entry in Xen MPU memory region mapping table(xen_mpumap) is a structure
+ * of pr_t, which is 16-bytes size, so the entry offset is the order of 4.
+ */
+#define MPU_ENTRY_SHIFT         0x4
+
+#define REGION_SEL_MASK         0xf
+
+#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 round up the section address to be PAGE_SIZE aligned
+ * Each section(e.g. .text, .data, etc) in xen.lds.S is page-aligned,
+ * which is usually guarded with ". = ALIGN(PAGE_SIZE)" in the head,
+ * or in the end
+ */
+.macro roundup_section, xb
+        add   \xb, \xb, #(PAGE_SIZE-1)
+        and   \xb, \xb, #PAGE_MASK
+.endm
+
+/*
+ * Macro to create a new MPU memory region entry, which is a structure
+ * of pr_t,  in \prmap.
+ *
+ * Inputs:
+ * prmap:   mpu memory region map table symbol
+ * sel:     region selector
+ * prbar:   preserve value for PRBAR_EL2
+ * prlar    preserve value for PRLAR_EL2
+ *
+ * Clobbers \tmp1, \tmp2
+ *
+ */

xen/arch/arm/arm64/head_mpu.S line 57

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+.macro create_mpu_entry prmap, sel, prbar, prlar, tmp1, tmp2
+    mov   \tmp2, \sel
+    lsl   \tmp2, \tmp2, #MPU_ENTRY_SHIFT
+    adr_l \tmp1, \prmap
+    /* Write the first 8 bytes(prbar_t) of pr_t */
+    str   \prbar, [\tmp1, \tmp2]
+
+    add   \tmp2, \tmp2, #8
+    /* Write the last 8 bytes(prlar_t) of pr_t */
+    str   \prlar, [\tmp1, \tmp2]

Any particular reason to not use 'stp'?

Also, AFAICT, with data cache disabled. But at least on ARMv8-A, the cache is never really off. So don't need some cache maintainance?

FAOD, I know the existing MMU code has the same issue. But I would rather prefer if the new code introduced is compliant to the Arm Arm.

xen/arch/arm/arm64/head_mpu.S

Quoted 11 lines
xen/arch/arm/arm64/head_mpu.S new file +11 -0
+.endm
+
+/*
+ * Macro to store the maximum number of regions supported by the EL2 MPU
+ * in max_xen_mpumap, which is identified by MPUIR_EL2.
+ *
+ * Outputs:
+ * nr_regions: preserve the maximum number of regions supported by the EL2 MPU
+ *
+ * Clobbers \tmp1
+ * > + */

Are you going to have multiple users? If not, then I would prefer if this is folded in the only caller.

xen/arch/arm/arm64/head_mpu.S line 71

Quoted 2 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+.macro read_max_el2_regions, nr_regions, tmp1
+    load_paddr \tmp1, max_xen_mpumap

I would rather prefer if we restrict the use of global while the MMU if off (see why above).

xen/arch/arm/arm64/head_mpu.S line 73

Quoted 2 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+    mrs   \nr_regions, MPUIR_EL2
+    isb

What's that isb for?

Quoted 6 lines
xen/arch/arm/arm64/head_mpu.S new file +6 -0
+    str   \nr_regions, [\tmp1]
+.endm
+
+/*
+ * Macro to prepare and set a MPU memory region
+ *

xen/arch/arm/arm64/head_mpu.S line 89

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ * Inputs:
+ * base:        base address symbol (should be page-aligned)
+ * limit:       limit address symbol
+ * sel:         region selector
+ * prbar:       store computed PRBAR_EL2 value
+ * prlar:       store computed PRLAR_EL2 value
+ * 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
+ *
+ * Clobber \tmp1

This macro will also clobber x27, x28, x29.

Quoted 43 lines
xen/arch/arm/arm64/head_mpu.S new file +43 -0
+ *
+ */
+.macro prepare_xen_region, base, limit, sel, prbar, prlar, tmp1, attr_prbar=REGION_DATA_PRBAR, attr_prlar=REGION_NORMAL_PRLAR
+    /* Prepare value for PRBAR_EL2 reg and preserve it in \prbar.*/
+    load_paddr \prbar, \base
+    and   \prbar, \prbar, #MPU_REGION_MASK
+    mov   \tmp1, #\attr_prbar
+    orr   \prbar, \prbar, \tmp1
+
+    /* Prepare value for PRLAR_EL2 reg and preserve it in \prlar.*/
+    load_paddr \prlar, \limit
+    /* Round up limit address to be PAGE_SIZE aligned */
+    roundup_section \prlar
+    /* Limit address should be inclusive */
+    sub   \prlar, \prlar, #1
+    and   \prlar, \prlar, #MPU_REGION_MASK
+    mov   \tmp1, #\attr_prlar
+    orr   \prlar, \prlar, \tmp1
+
+    mov   x27, \sel
+    mov   x28, \prbar
+    mov   x29, \prlar
+    /*
+     * x27, x28, x29 are special registers designed as
+     * inputs for function write_pr
+     */
+    bl    write_pr
+.endm
+
+.section .text.idmap, "ax", %progbits
+
+/*
+ * ENTRY to configure a EL2 MPU memory region
+ * ARMv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for ARMv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provides access to the EL2 MPU region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n.(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR0_EL2 and PRLAR0_EL2
+ * - Region 17 configuration is accessible through PRBAR1_EL2 and PRLAR1_EL2
+ * - Region 18 configuration is accessible through PRBAR2_EL2 and PRLAR2_EL2

xen/arch/arm/arm64/head_mpu.S line 142

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ * - ...
+ * - Region 31 configuration is accessible through PRBAR15_EL2 and PRLAR15_EL2
+ *
+ * Inputs:
+ * x27: region selector
+ * x28: preserve value for PRBAR_EL2
+ * x29: preserve value for PRLAR_EL2
+ *
+ */
+ENTRY(write_pr)

AFAICT, this function would not be necessary if the index for the init sections were hardcoded.

So I would like to understand why the index cannot be hardcoded.

xen/arch/arm/arm64/head_mpu.S line 144

Quoted 2 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+    msr   PRSELR_EL2, x27
+    dsb   sy

What is this 'dsb' for? Also why 'sy'?

Quoted 85 lines
xen/arch/arm/arm64/head_mpu.S new file +85 -0
+    and   x27, x27, #REGION_SEL_MASK
+    cmp   x27, #0
+    bne   1f
+    msr   PRBAR0_EL2, x28
+    msr   PRLAR0_EL2, x29
+    b     out
+1:
+    cmp   x27, #1
+    bne   2f
+    msr   PRBAR1_EL2, x28
+    msr   PRLAR1_EL2, x29
+    b     out
+2:
+    cmp   x27, #2
+    bne   3f
+    msr   PRBAR2_EL2, x28
+    msr   PRLAR2_EL2, x29
+    b     out
+3:
+    cmp   x27, #3
+    bne   4f
+    msr   PRBAR3_EL2, x28
+    msr   PRLAR3_EL2, x29
+    b     out
+4:
+    cmp   x27, #4
+    bne   5f
+    msr   PRBAR4_EL2, x28
+    msr   PRLAR4_EL2, x29
+    b     out
+5:
+    cmp   x27, #5
+    bne   6f
+    msr   PRBAR5_EL2, x28
+    msr   PRLAR5_EL2, x29
+    b     out
+6:
+    cmp   x27, #6
+    bne   7f
+    msr   PRBAR6_EL2, x28
+    msr   PRLAR6_EL2, x29
+    b     out
+7:
+    cmp   x27, #7
+    bne   8f
+    msr   PRBAR7_EL2, x28
+    msr   PRLAR7_EL2, x29
+    b     out
+8:
+    cmp   x27, #8
+    bne   9f
+    msr   PRBAR8_EL2, x28
+    msr   PRLAR8_EL2, x29
+    b     out
+9:
+    cmp   x27, #9
+    bne   10f
+    msr   PRBAR9_EL2, x28
+    msr   PRLAR9_EL2, x29
+    b     out
+10:
+    cmp   x27, #10
+    bne   11f
+    msr   PRBAR10_EL2, x28
+    msr   PRLAR10_EL2, x29
+    b     out
+11:
+    cmp   x27, #11
+    bne   12f
+    msr   PRBAR11_EL2, x28
+    msr   PRLAR11_EL2, x29
+    b     out
+12:
+    cmp   x27, #12
+    bne   13f
+    msr   PRBAR12_EL2, x28
+    msr   PRLAR12_EL2, x29
+    b     out
+13:
+    cmp   x27, #13
+    bne   14f
+    msr   PRBAR13_EL2, x28
+    msr   PRLAR13_EL2, x29
+    b     out
+14:

xen/arch/arm/arm64/head_mpu.S line 239

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+    cmp   x27, #14
+    bne   15f
+    msr   PRBAR14_EL2, x28
+    msr   PRLAR14_EL2, x29
+    b     out
+15:
+    msr   PRBAR15_EL2, x28
+    msr   PRLAR15_EL2, x29
+out:
+    isb

What is this 'isb' for?

Quoted 22 lines
xen/arch/arm/arm64/head_mpu.S new file +22 -0
+    ret
+ENDPROC(write_pr)
+
+/*
+ * Static start-of-day Xen EL2 MPU memory region layout.
+ *
+ *     xen_mpumap[0] : Xen text
+ *     xen_mpumap[1] : Xen read-only data
+ *     xen_mpumap[2] : Xen read-only after init data
+ *     xen_mpumap[3] : Xen read-write data
+ *     xen_mpumap[4] : Xen BSS
+ *     ......
+ *     xen_mpumap[max_xen_mpumap - 2]: Xen init data
+ *     xen_mpumap[max_xen_mpumap - 1]: Xen init text
+ *
+ * Clobbers x0 - x6
+ *
+ * It shall be compliant with what describes in xen.lds.S, or the below
+ * codes need adjustment.
+ * It shall also follow the rules of putting fixed MPU memory region in
+ * the front, and the others in the rear, which, here, mainly refers to
+ * boot-only region, like Xen init text region.

xen/arch/arm/arm64/head_mpu.S line 271

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ */
+ENTRY(prepare_early_mappings)
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x6, lr
+
+    /* x0: region sel */
+    mov   x0, xzr
+    /* Xen text section. */
+    prepare_xen_region _stext, _etext, x0, x1, x2, x3, attr_prbar=REGION_TEXT_PRBAR
+    create_mpu_entry xen_mpumap, x0, x1, x2, x3, x4

You always seem to call prepare_xen_region and create_mpu_entry. Can they be combined?

Also, will the first parameter of create_mpu_entry always be xen_mpumap? If so, I will remove it from the parameter.

[...]

xen/arch/arm/xen.lds.S SECTIONS · line 94

Quoted 9 lines
xen/arch/arm/xen.lds.S +1 -0
index bc45ea2c65..79965a3c17 100644
@@ -91,6 +91,8 @@ SECTIONS
        __ro_after_init_end = .;
    } : text
 
+  . = ALIGN(PAGE_SIZE);

Why do you need this ALIGN?

xen/arch/arm/xen.lds.S SECTIONS · line 159

Quoted 6 lines
xen/arch/arm/xen.lds.S +1 -0
+  __data_begin = .;
    .data.read_mostly : {
         /* Exception table */
         __start___ex_table = .;
@@ -157,7 +159,9 @@ SECTIONS
         *(.altinstr_replacement)

I know you are not using alternative instructions yet. But, you should make sure they are included. So I think rather than introduce __init_data_begin, you want to use "_einitext" for the start of the "Init data" section.

xen/arch/arm/xen.lds.S SECTIONS · line 162

Quoted 3 lines
xen/arch/arm/xen.lds.S +1 -0
    } :text
    . = ALIGN(PAGE_SIZE);
+

Spurious?

xen/arch/arm/xen.lds.S SECTIONS · line 167

Quoted 5 lines
xen/arch/arm/xen.lds.S +1 -0
    .init.data : {
+       __init_data_begin = .;            /* Init data */
         *(.init.rodata)
         *(.init.rodata.*)
 

Cheers,

Signature · 1 line
Julien Grall
#39 lore raw

RE: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 10 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, January 19, 2023 11:04 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 2 lines

Hi Penny,

Hi Julien

Sorry for the late response, just come back from Chinese Spring Festival Holiday~

Quoted 5 lines · Penny Zheng
Quoted 4 lines

From: Penny Zheng <[email protected]>

The start-of-day Xen MPU memory region layout shall be like as follows:

Quoted 9 lines
Quoted 6 lines

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS ...... xen_mpumap[max_xen_mpumap - 2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

Can you explain why the init region should be at the end of the MPU?

As discussed in the v1 Serie, I'd like to put all transient MPU regions, like boot-only region, at the end of the MPU.
Since they will get removed at the end of the boot, I am trying not to leave holes in the MPU map by putting all transient MPU regions at rear.

Quoted 56 lines
Quoted 2 lines

max_xen_mpumap refers to the number of regions supported by the EL2

MPU.

Quoted 7 lines

The layout shall be compliant with what we describe in xen.lds.S, or the codes need adjustment.

As MMU system and MPU system have different functions to create the boot MMU/MPU memory management data, instead of introducing extra #ifdef in main code flow, we introduce a neutral name prepare_early_mappings for both, and also to replace create_page_tables

for MMU.

Quoted 8 lines
---
  xen/arch/arm/arm64/Makefile              |   2 +
  xen/arch/arm/arm64/head.S                |  17 +-
  xen/arch/arm/arm64/head_mmu.S            |   4 +-
  xen/arch/arm/arm64/head_mpu.S            | 323

+++++++++++++++++++++++

Quoted 23 lines
  xen/arch/arm/include/asm/arm64/mpu.h     |  63 +++++
  xen/arch/arm/include/asm/arm64/sysregs.h |  49 ++++
  xen/arch/arm/mm_mpu.c                    |  48 ++++
  xen/arch/arm/xen.lds.S                   |   4 +
  8 files changed, 502 insertions(+), 8 deletions(-)

create mode 100644 xen/arch/arm/arm64/head_mpu.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h create mode 100644 xen/arch/arm/mm_mpu.c

xen/arch/arm/arm64/head_mpu.S new file +14 -0
+/*
+ * Macro to create a new MPU memory region entry, which is a
+structure
+ * of pr_t,  in \prmap.
+ *
+ * Inputs:
+ * prmap:   mpu memory region map table symbol
+ * sel:     region selector
+ * prbar:   preserve value for PRBAR_EL2
+ * prlar    preserve value for PRLAR_EL2
+ *
+ * Clobbers \tmp1, \tmp2
+ *
+ */

xen/arch/arm/arm64/head_mpu.S line 57

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+.macro create_mpu_entry prmap, sel, prbar, prlar, tmp1, tmp2
+    mov   \tmp2, \sel
+    lsl   \tmp2, \tmp2, #MPU_ENTRY_SHIFT
+    adr_l \tmp1, \prmap
+    /* Write the first 8 bytes(prbar_t) of pr_t */
+    str   \prbar, [\tmp1, \tmp2]
+
+    add   \tmp2, \tmp2, #8
+    /* Write the last 8 bytes(prlar_t) of pr_t */
+    str   \prlar, [\tmp1, \tmp2]

Any particular reason to not use 'stp'?

Quoted 6 lines

Also, AFAICT, with data cache disabled. But at least on ARMv8-A, the cache is never really off. So don't need some cache maintainance?

FAOD, I know the existing MMU code has the same issue. But I would rather prefer if the new code introduced is compliant to the Arm Arm.

True, stp is better and I will clean data cache to be compliant to the Arm Arm. I write the following example to see if I catch what you suggested:

```
add \tmp1, \tmp1, \tmp2
stp \prbar, \prlar, [\tmp1]
dc cvau, \tmp1
isb
dsb sy
```
Quoted 8 lines
Quoted 8 lines

+.endm

xen/arch/arm/arm64/head_mpu.S new file +7 -0
+
+/*
+ * Macro to store the maximum number of regions supported by the EL2
+MPU
+ * in max_xen_mpumap, which is identified by MPUIR_EL2.
+ *
+ * Outputs:
Quoted 9 lines
Quoted 5 lines

+ * nr_regions: preserve the maximum number of regions supported by +the EL2 MPU
+ *
+ * Clobbers \tmp1
+ * > + */

Are you going to have multiple users? If not, then I would prefer if this is folded in the only caller.

Ok. I will fold since I think it is one-time reading thingy.

Quoted 6 lines
Quoted 2 lines

+.macro read_max_el2_regions, nr_regions, tmp1
+ load_paddr \tmp1, max_xen_mpumap

I would rather prefer if we restrict the use of global while the MMU if off (see why above).

If we don't use global here, then after MPU enabled, we need to re-read MPUIR_EL2 to get the number of maximum EL2 regions.

Or I put data cache clean after accessing global, is it better?

```
str   \nr_regions, [\tmp1]
dc cvau, \tmp1
isb
dsb sy
```
Quoted 39 lines

xen/arch/arm/arm64/head_mpu.S line 73

Quoted 2 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+    mrs   \nr_regions, MPUIR_EL2
+    isb

What's that isb for?

Quoted 20 lines
xen/arch/arm/arm64/head_mpu.S new file +20 -0
+    str   \nr_regions, [\tmp1]
+.endm
+
+/*
+ * ENTRY to configure a EL2 MPU memory region
+ * ARMv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for ARMv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provides access to the EL2 MPU
+region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n.(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR0_EL2 and
+PRLAR0_EL2
+ * - Region 17 configuration is accessible through PRBAR1_EL2 and
+PRLAR1_EL2
+ * - Region 18 configuration is accessible through PRBAR2_EL2 and
+PRLAR2_EL2
+ * - ...

xen/arch/arm/arm64/head_mpu.S line 142

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ * - Region 31 configuration is accessible through PRBAR15_EL2 and
+PRLAR15_EL2
+ *
+ * Inputs:
+ * x27: region selector
+ * x28: preserve value for PRBAR_EL2
+ * x29: preserve value for PRLAR_EL2
+ *
+ */
+ENTRY(write_pr)

AFAICT, this function would not be necessary if the index for the init sections were hardcoded.

Quoted 2 lines

So I would like to understand why the index cannot be hardcoded.

The reason is that we are putting init sections at the *end* of the MPU map, and the length of the whole MPU map is platform-specific. We read it from MPUIR_EL2.

Quoted 13 lines
Quoted 2 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+    msr   PRSELR_EL2, x27
+    dsb   sy

[...]

Quoted 8 lines
xen/arch/arm/xen.lds.S index renamed from xen/arch/arm/xen.lds.S +0 -0

bc45ea2c65..79965a3c17 100644

xen/arch/arm/xen.lds.S +0 -0
@@ -91,6 +91,8 @@ SECTIONS
        __ro_after_init_end = .;
    } : text
 
Quoted 4 lines

xen/arch/arm/xen.lds.S SECTIONS · line 94

Quoted 1 line
xen/arch/arm/xen.lds.S +1 -0
+  . = ALIGN(PAGE_SIZE);

Why do you need this ALIGN?

I need a symbol as the start of the data section, so I introduce "__data_begin = .;".
If I use "__ro_after_init_end = .;" instead, I'm afraid in the future, if someone introduces a new section after ro-after-init section, this part also needs modification too.

When we define MPU regions for each section in xen.lds.S, we always treat these sections page-aligned.
I checked each section in xen.lds.S, and ". = ALIGN(PAGE_SIZE);" is either added at section head, or at the tail of the previous section, to make sure starting address symbol page-aligned.

And if we don't put this ALIGN, if "__ro_after_init_end " symbol itself is not page-aligned, the two adjacent sections will overlap in MPU.

Quoted 23 lines

xen/arch/arm/xen.lds.S SECTIONS · line 159

Quoted 6 lines
xen/arch/arm/xen.lds.S +1 -0
+  __data_begin = .;
    .data.read_mostly : {
         /* Exception table */
         __start___ex_table = .;
@@ -157,7 +159,9 @@ SECTIONS
         *(.altinstr_replacement)

I know you are not using alternative instructions yet. But, you should make sure they are included. So I think rather than introduce __init_data_begin, you want to use "_einitext" for the start of the "Init data" section.

xen/arch/arm/xen.lds.S SECTIONS · line 162

Quoted 3 lines
xen/arch/arm/xen.lds.S +1 -0
    } :text
    . = ALIGN(PAGE_SIZE);
+

Spurious?

xen/arch/arm/xen.lds.S SECTIONS · line 167

Quoted 5 lines
xen/arch/arm/xen.lds.S +1 -0
    .init.data : {
+       __init_data_begin = .;            /* Init data */
         *(.init.rodata)
         *(.init.rodata.*)
 
Quoted 4 lines

Cheers,

--
Julien Grall

Cheers,

--
Penny Zheng

#40 lore raw

Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 29 lines · Penny Zheng
Quoted 10 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, January 19, 2023 11:04 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 2 lines

Hi Penny,

Hi Julien

Sorry for the late response, just come back from Chinese Spring Festival Holiday~

Quoted 12 lines · Penny Zheng
Quoted 10 lines

From: Penny Zheng <[email protected]>

The start-of-day Xen MPU memory region layout shall be like as follows:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS ...... xen_mpumap[max_xen_mpumap - 2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

Quoted 5 lines
Quoted 2 lines

Can you explain why the init region should be at the end of the MPU?

As discussed in the v1 Serie, I'd like to put all transient MPU regions, like boot-only region, at the end of the MPU.

I vaguely recall the discussion but can't seem to find the thread. Do you have a link? (A summary in the patch would have been nice)

Quoted 2 lines

Since they will get removed at the end of the boot, I am trying not to leave holes in the MPU map by putting all transient MPU regions at rear.

I understand the principle, but I am not convinced this is worth it because of the increase complexity in the assembly code.

What would be the problem with reshuffling partially the MPU once we booted?

Quoted 64 lines
Quoted 57 lines
Quoted 3 lines

max_xen_mpumap refers to the number of regions supported by the EL2

MPU.

Quoted 7 lines

The layout shall be compliant with what we describe in xen.lds.S, or the codes need adjustment.

As MMU system and MPU system have different functions to create the boot MMU/MPU memory management data, instead of introducing extra #ifdef in main code flow, we introduce a neutral name prepare_early_mappings for both, and also to replace create_page_tables

for MMU.

Quoted 8 lines
---
   xen/arch/arm/arm64/Makefile              |   2 +
   xen/arch/arm/arm64/head.S                |  17 +-
   xen/arch/arm/arm64/head_mmu.S            |   4 +-
   xen/arch/arm/arm64/head_mpu.S            | 323

+++++++++++++++++++++++

Quoted 23 lines
   xen/arch/arm/include/asm/arm64/mpu.h     |  63 +++++
   xen/arch/arm/include/asm/arm64/sysregs.h |  49 ++++
   xen/arch/arm/mm_mpu.c                    |  48 ++++
   xen/arch/arm/xen.lds.S                   |   4 +
   8 files changed, 502 insertions(+), 8 deletions(-)

create mode 100644 xen/arch/arm/arm64/head_mpu.S create mode 100644 xen/arch/arm/include/asm/arm64/mpu.h create mode 100644 xen/arch/arm/mm_mpu.c

xen/arch/arm/arm64/head_mpu.S new file +14 -0
+/*
+ * Macro to create a new MPU memory region entry, which is a
+structure
+ * of pr_t,  in \prmap.
+ *
+ * Inputs:
+ * prmap:   mpu memory region map table symbol
+ * sel:     region selector
+ * prbar:   preserve value for PRBAR_EL2
+ * prlar    preserve value for PRLAR_EL2
+ *
+ * Clobbers \tmp1, \tmp2
+ *
+ */

xen/arch/arm/arm64/head_mpu.S line 57

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+.macro create_mpu_entry prmap, sel, prbar, prlar, tmp1, tmp2
+    mov   \tmp2, \sel
+    lsl   \tmp2, \tmp2, #MPU_ENTRY_SHIFT
+    adr_l \tmp1, \prmap
+    /* Write the first 8 bytes(prbar_t) of pr_t */
+    str   \prbar, [\tmp1, \tmp2]
+
+    add   \tmp2, \tmp2, #8
+    /* Write the last 8 bytes(prlar_t) of pr_t */
+    str   \prlar, [\tmp1, \tmp2]

Any particular reason to not use 'stp'?

Quoted 6 lines

Also, AFAICT, with data cache disabled. But at least on ARMv8-A, the cache is never really off. So don't need some cache maintainance?

FAOD, I know the existing MMU code has the same issue. But I would rather prefer if the new code introduced is compliant to the Arm Arm.

Quoted 6 lines

True, stp is better and I will clean data cache to be compliant to the Arm Arm. I write the following example to see if I catch what you suggested:
```
add \tmp1, \tmp1, \tmp2
stp \prbar, \prlar, [\tmp1]
dc cvau, \tmp1

I think this wants to be invalidate rather than clean because the cache is off.

Quoted 27 lines

isb
dsb sy
```

Quoted 8 lines
Quoted 8 lines

+.endm

xen/arch/arm/arm64/head_mpu.S new file +7 -0
+
+/*
+ * Macro to store the maximum number of regions supported by the EL2
+MPU
+ * in max_xen_mpumap, which is identified by MPUIR_EL2.
+ *
+ * Outputs:
Quoted 9 lines
Quoted 5 lines

+ * nr_regions: preserve the maximum number of regions supported by +the EL2 MPU
+ *
+ * Clobbers \tmp1
+ * > + */

Are you going to have multiple users? If not, then I would prefer if this is folded in the only caller.

Ok. I will fold since I think it is one-time reading thingy.

Quoted 3 lines
Quoted 2 lines

+.macro read_max_el2_regions, nr_regions, tmp1
+ load_paddr \tmp1, max_xen_mpumap

Quoted 6 lines
Quoted 3 lines

I would rather prefer if we restrict the use of global while the MMU if off (see why above).

If we don't use global here, then after MPU enabled, we need to re-read MPUIR_EL2 to get the number of maximum EL2 regions.

Which, IMHO, is better than having to think about cache.

Quoted 48 lines

Or I put data cache clean after accessing global, is it better?

```
str   \nr_regions, [\tmp1]
dc cvau, \tmp1
isb
dsb sy
```
Quoted 39 lines

xen/arch/arm/arm64/head_mpu.S line 73

Quoted 2 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+    mrs   \nr_regions, MPUIR_EL2
+    isb

What's that isb for?

Quoted 20 lines
xen/arch/arm/arm64/head_mpu.S new file +20 -0
+    str   \nr_regions, [\tmp1]
+.endm
+
+/*
+ * ENTRY to configure a EL2 MPU memory region
+ * ARMv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for ARMv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provides access to the EL2 MPU
+region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n.(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR0_EL2 and
+PRLAR0_EL2
+ * - Region 17 configuration is accessible through PRBAR1_EL2 and
+PRLAR1_EL2
+ * - Region 18 configuration is accessible through PRBAR2_EL2 and
+PRLAR2_EL2
+ * - ...

xen/arch/arm/arm64/head_mpu.S line 142

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ * - Region 31 configuration is accessible through PRBAR15_EL2 and
+PRLAR15_EL2
+ *
+ * Inputs:
+ * x27: region selector
+ * x28: preserve value for PRBAR_EL2
+ * x29: preserve value for PRLAR_EL2
+ *
+ */
+ENTRY(write_pr)

AFAICT, this function would not be necessary if the index for the init sections were hardcoded.

Quoted 5 lines
Quoted 2 lines

So I would like to understand why the index cannot be hardcoded.

The reason is that we are putting init sections at the *end* of the MPU map, and the length of the whole MPU map is platform-specific. We read it from MPUIR_EL2.

Right, I got that bit from the code. What I would like to understand is why all the initial address cannot be hardocoded?

From a brief look, this would simplify a lot the assembly code.

Quoted 19 lines
Quoted 14 lines
Quoted 3 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+    msr   PRSELR_EL2, x27
+    dsb   sy

[...]

Quoted 8 lines
xen/arch/arm/xen.lds.S index renamed from xen/arch/arm/xen.lds.S +0 -0

bc45ea2c65..79965a3c17 100644

xen/arch/arm/xen.lds.S +0 -0
@@ -91,6 +91,8 @@ SECTIONS
         __ro_after_init_end = .;
     } : text
 
Quoted 4 lines

xen/arch/arm/xen.lds.S SECTIONS · line 94

Quoted 1 line
xen/arch/arm/xen.lds.S +1 -0
+  . = ALIGN(PAGE_SIZE);

Why do you need this ALIGN?

Quoted 5 lines

I need a symbol as the start of the data section, so I introduce "__data_begin = .;".
If I use "__ro_after_init_end = .;" instead, I'm afraid in the future, if someone introduces a new section after ro-after-init section, this part also needs modification too.

I haven't suggested there is a problem to define a new symbol. I am merely asking about the align.

Quoted 9 lines

When we define MPU regions for each section in xen.lds.S, we always treat these sections page-aligned.
I checked each section in xen.lds.S, and ". = ALIGN(PAGE_SIZE);" is either added at section head, or at the tail of the previous section, to make sure starting address symbol page-aligned.

And if we don't put this ALIGN, if "__ro_after_init_end " symbol itself is not page-aligned, the two adjacent sections will overlap in MPU.

__ro_after_init_end *has* to be page aligned because the permissions are different than for __data_begin.

If we were going to add a new section, then either it has the same permission as .data.read.mostly and we will bundle them or it doesn't and we would need a .align.

But today, the extra .ALIGN seems unnecessary (at least in the context of this patch).

Cheers,

Signature · 1 line
Julien Grall
#41 lore raw

RE: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 10 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Sunday, January 29, 2023 3:37 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 2 lines

Hi Penny,

Hi Julien,

Quoted 42 lines · Penny Zheng
Quoted 4 lines
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, January 19, 2023 11:04 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 27 lines
Quoted 9 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Hi Julien

Sorry for the late response, just come back from Chinese Spring Festival Holiday~

Quoted 12 lines · Penny Zheng
Quoted 10 lines

From: Penny Zheng <[email protected]>

The start-of-day Xen MPU memory region layout shall be like as follows:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS ...... xen_mpumap[max_xen_mpumap - 2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

Quoted 5 lines
Quoted 2 lines

Can you explain why the init region should be at the end of the MPU?

As discussed in the v1 Serie, I'd like to put all transient MPU regions, like boot-only region, at the end of the MPU.

I vaguely recall the discussion but can't seem to find the thread. Do you have a link? (A summary in the patch would have been nice)

Quoted 8 lines
Quoted 2 lines

Since they will get removed at the end of the boot, I am trying not to leave holes in the MPU map by putting all transient MPU regions at rear.

I understand the principle, but I am not convinced this is worth it because of the increase complexity in the assembly code.

What would be the problem with reshuffling partially the MPU once we booted?

There are three types of MPU regions during boot-time:
1. Fixed MPU region
Regions like Xen text section, Xen heap section, etc.
2. Boot-only MPU region
Regions like Xen init sections, etc. It will be removed at the end of booting.
3. Regions need switching in/out during vcpu context switch
Region like system device memory map.
For example, for FVP_BaseR_AEMv8R, we have [0x80000000, 0xfffff000) as the whole system device memory map for Xen(idle vcpu) in EL2, when context switching to guest vcpu, it shall be replaced with guest-specific device mapping, like vgic, vpl011, passthrough device, etc.

We don't have two mappings for different stage translations in MPU, like we had in MMU. Xen stage 1 EL2 mapping and stage 2 mapping are both sharing one MPU memory mapping(xen_mpumap) So to save the trouble of hunting down each switching regions in time-sensitive context switch, we must re-order xen_mpumap to keep fixed regions in the front, and switching ones in the heels of them.

In Patch Serie v1, I was adding MPU regions in sequence, and I introduced a set of bitmaps to record the location of same type regions. At the end of booting, I need to *disable* MPU to do the reshuffling, as I can't move regions like xen heap while MPU on.

And we discussed that it is too risky to disable MPU, and you suggested [1] "

Quoted 2 lines

You should not need any reorg if you map the boot-only section towards in the higher slot index (or just after the fixed ones).

"

Maybe in assembly, we know exactly how many fixed regions are, boot-only regions are, but in C codes, we parse FDT to get static configuration, like we don't know how many fixed regions for xen static heap is enough. Approximation is not suggested in MPU system with limited MPU regions, some platform may only have 16 MPU regions, IMHO, it is not worthy wasting in approximation.

So I take the suggestion of putting regions in the higher slot index. Putting fixed regions in the front, and putting boot-only and switching ones at tail. Then, at the end of booting, when we reorg the mpu mapping, we remove all boot-only regions, and for switching ones, we disable-relocate(after fixed ones)-enable them. Specific codes in [2].

[1] https://lists.xenproject.org/archives/html/xen-devel/2022-11/msg00457.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2023-01/msg00795.html

Quoted 1 line

[...]

Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 17 lines
xen/arch/arm/arm64/head_mpu.S new file +17 -0
+/*
+ * ENTRY to configure a EL2 MPU memory region
+ * ARMv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for ARMv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provides access to the EL2 MPU
+region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n.(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR0_EL2 and
+PRLAR0_EL2
+ * - Region 17 configuration is accessible through PRBAR1_EL2 and
+PRLAR1_EL2
+ * - Region 18 configuration is accessible through PRBAR2_EL2 and
+PRLAR2_EL2
+ * - ...

xen/arch/arm/arm64/head_mpu.S line 142

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S new file +10 -0
+ * - Region 31 configuration is accessible through PRBAR15_EL2 and
+PRLAR15_EL2
+ *
+ * Inputs:
+ * x27: region selector
+ * x28: preserve value for PRBAR_EL2
+ * x29: preserve value for PRLAR_EL2
+ *
+ */
+ENTRY(write_pr)

AFAICT, this function would not be necessary if the index for the init sections were hardcoded.

So I would like to understand why the index cannot be hardcoded.

Quoted 9 lines
Quoted 2 lines

The reason is that we are putting init sections at the *end* of the MPU map, and the length of the whole MPU map is platform-specific. We

read it from MPUIR_EL2.

Right, I got that bit from the code. What I would like to understand is why all the initial address cannot be hardocoded?

From a brief look, this would simplify a lot the assembly code.

Like I said before, "map towards higher slot", if it is not the tail, it is hard to decide another number to meet different platforms and various FDT static configuration.

If we, in assembly, put fixed regions in front and boot-only regions after, then, when we enter C world, we immediately do a simple reshuffle, which means that we need to relocate these init sections to tail, it is workable only when MPU is disabled, unless we're sure that "reshuffling part" is not using any init codes or data.

Quoted 41 lines
Quoted 19 lines
Quoted 14 lines
Quoted 3 lines
xen/arch/arm/arm64/head_mpu.S new file +2 -0
+    msr   PRSELR_EL2, x27
+    dsb   sy

[...]

Quoted 8 lines
xen/arch/arm/xen.lds.S index renamed from xen/arch/arm/xen.lds.S +0 -0

bc45ea2c65..79965a3c17 100644

xen/arch/arm/xen.lds.S +0 -0
@@ -91,6 +91,8 @@ SECTIONS
         __ro_after_init_end = .;
     } : text
 
Quoted 4 lines

xen/arch/arm/xen.lds.S SECTIONS · line 94

Quoted 1 line
xen/arch/arm/xen.lds.S +1 -0
+  . = ALIGN(PAGE_SIZE);

Why do you need this ALIGN?

Quoted 5 lines

I need a symbol as the start of the data section, so I introduce "__data_begin = .;".
If I use "__ro_after_init_end = .;" instead, I'm afraid in the future, if someone introduces a new section after ro-after-init section, this part also needs modification too.

I haven't suggested there is a problem to define a new symbol. I am merely asking about the align.

Quoted 9 lines

When we define MPU regions for each section in xen.lds.S, we always treat these sections page-aligned.
I checked each section in xen.lds.S, and ". = ALIGN(PAGE_SIZE);" is either added at section head, or at the tail of the previous section, to make sure starting address symbol page-aligned.

And if we don't put this ALIGN, if "__ro_after_init_end " symbol itself is not page-aligned, the two adjacent sections will overlap in MPU.

__ro_after_init_end *has* to be page aligned because the permissions are different than for __data_begin.

Quoted 7 lines

If we were going to add a new section, then either it has the same permission as .data.read.mostly and we will bundle them or it doesn't and we would need a .align.

But today, the extra .ALIGN seems unnecessary (at least in the context of this patch).

Understood, I'll remove

Quoted 4 lines

Cheers,

--
Julien Grall

Cheers,

--
Penny Zheng

--
Penny Zheng

#42 lore raw

Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 12 lines · Penny Zheng

There are three types of MPU regions during boot-time:
1. Fixed MPU region
Regions like Xen text section, Xen heap section, etc.
2. Boot-only MPU region
Regions like Xen init sections, etc. It will be removed at the end of booting.
3. Regions need switching in/out during vcpu context switch
Region like system device memory map.
For example, for FVP_BaseR_AEMv8R, we have [0x80000000, 0xfffff000) as the whole system device memory map for Xen(idle vcpu) in EL2, when context switching to guest vcpu, it shall be replaced with guest-specific device mapping, like vgic, vpl011, passthrough device, etc.

Quoted 4 lines

We don't have two mappings for different stage translations in MPU, like we had in MMU. Xen stage 1 EL2 mapping and stage 2 mapping are both sharing one MPU memory mapping(xen_mpumap) So to save the trouble of hunting down each switching regions in time-sensitive context switch, we must re-order xen_mpumap to keep fixed regions in the front, and switching ones in the heels of them.

From my understanding, hunting down each switching regions would be a matter to loop over a bitmap. There will be a slight increase in the number of instructions executed, but I don't believe it will be noticeable.

Quoted 10 lines

In Patch Serie v1, I was adding MPU regions in sequence, and I introduced a set of bitmaps to record the location of same type regions. At the end of booting, I need to *disable* MPU to do the reshuffling, as I can't move regions like xen heap while MPU on.

And we discussed that it is too risky to disable MPU, and you suggested [1] "

Quoted 2 lines

You should not need any reorg if you map the boot-only section towards in the higher slot index (or just after the fixed ones).

"

Right, looking at the new code. I realize that this was probably a bad idea because we are adding complexity in the assembly code.

Quoted 5 lines

Maybe in assembly, we know exactly how many fixed regions are, boot-only regions are, but in C codes, we parse FDT to get static configuration, like we don't know how many fixed regions for xen static heap is enough. Approximation is not suggested in MPU system with limited MPU regions, some platform may only have 16 MPU regions, IMHO, it is not worthy wasting in approximation.

I haven't suggested to use approximation anywhere here. I will answer about the limited number of entries in the other thread.

Quoted 4 lines

So I take the suggestion of putting regions in the higher slot index. Putting fixed regions in the front, and putting boot-only and switching ones at tail. Then, at the end of booting, when we reorg the mpu mapping, we remove all boot-only regions, and for switching ones, we disable-relocate(after fixed ones)-enable them. Specific codes in [2].

From this discussion, it feels to me that you are trying to make the code more complicated just to keep the split and save a few cycles (see above).

I would suggest to investigate the cost of "hunting down each section". Depending on the result, we can discuss what the best approach.

Cheers,

Signature · 1 line
Julien Grall
#43 lore raw

RE: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Julien

Quoted 69 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, January 30, 2023 5:40 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 12 lines · Penny Zheng

There are three types of MPU regions during boot-time:
1. Fixed MPU region
Regions like Xen text section, Xen heap section, etc.
2. Boot-only MPU region
Regions like Xen init sections, etc. It will be removed at the end of booting.
3. Regions need switching in/out during vcpu context switch
Region like system device memory map.
For example, for FVP_BaseR_AEMv8R, we have [0x80000000, 0xfffff000) as the whole system device memory map for Xen(idle vcpu) in EL2, when context switching to guest vcpu, it shall be replaced with guest-specific device mapping, like vgic, vpl011, passthrough device, etc.

Quoted 1 line

We don't have two mappings for different stage translations in MPU, like

we had in MMU.

Quoted 2 lines

Xen stage 1 EL2 mapping and stage 2 mapping are both sharing one MPU memory mapping(xen_mpumap) So to save the trouble of hunting down

each

Quoted 1 line

switching regions in time-sensitive context switch, we must re-order

xen_mpumap to keep fixed regions in the front, and switching ones in the heels of them.

From my understanding, hunting down each switching regions would be a
matter to loop over a bitmap. There will be a slight increase in the number of instructions executed, but I don't believe it will be noticeable.

Quoted 4 lines

In Patch Serie v1, I was adding MPU regions in sequence, and I introduced a set of bitmaps to record the location of same type regions. At the end of booting, I need to *disable* MPU to do the

reshuffling, as I can't move regions like xen heap while MPU on.

Quoted 6 lines

And we discussed that it is too risky to disable MPU, and you suggested [1] "

Quoted 2 lines

You should not need any reorg if you map the boot-only section towards in the higher slot index (or just after the fixed ones).

"

Right, looking at the new code. I realize that this was probably a bad idea because we are adding complexity in the assembly code.

Quoted 3 lines

Maybe in assembly, we know exactly how many fixed regions are, boot-only regions are, but in C codes, we parse FDT to get static

configuration, like we don't know how many fixed regions for xen static heap is enough.

Quoted 2 lines

Approximation is not suggested in MPU system with limited MPU regions, some platform may only have 16 MPU regions, IMHO, it is not worthy

wasting in approximation.

I haven't suggested to use approximation anywhere here. I will answer about the limited number of entries in the other thread.

Quoted 4 lines

So I take the suggestion of putting regions in the higher slot index. Putting fixed regions in the front, and putting boot-only and switching ones at tail. Then, at the end of booting, when we reorg the

Quoted 9 lines

mpu mapping, we remove all boot-only regions, and for switching ones, we disable-relocate(after fixed ones)-enable them. Specific codes in [2].

From this discussion, it feels to me that you are trying to make the code
more complicated just to keep the split and save a few cycles (see above).

I would suggest to investigate the cost of "hunting down each section". Depending on the result, we can discuss what the best approach.

Correct me if I'm wrong, the complicated things in assembly you are worried about is that we couldn't define the index for initial sections, no hardcoded to keep simple. And function write_pr, ik, is really a big chunk of codes, however the logic is simple there, just a bunch of "switch-cases".

If we are adding MPU regions in sequence as you suggested, while using bitmap at the same time to record used entry.
TBH, this is how I designed at the very beginning internally. We found that if we don't do reorg late-boot to keep fixed in front and switching ones after, each time when we do vcpu context switch, not only we need to hunt down switching ones to disable, while we add new switch-in regions, using bitmap to find free entry is saying that the process is unpredictable. Uncertainty is what we want to avoid in Armv8-R architecture.

Hmmm, TBH, I really really like your suggestion to put boot-only/switching regions into higher slot. It really saved a lot trouble in late-init reorg and also avoids disabling MPU at the same time. The split is a simple and easy-to-understand construction compared with bitmap too.

IMHO, reorg is really worth doing. We put all complicated things in boot-time to make runtime context-switch simple and fast, even for a few cycles.
As the Armv8-R architecture profile from the beginning is designed to support use cases that have a high sensitivity to deterministic execution. (e.g. Fuel Injection, Brake control, Drive trains, Motor control etc).
However, when talking about architecture thing, I need more professional opinions, @Wei Chen @Bertrand Marquis
Also, Will R52 implementation encounter the same issue. @[email protected] @Stefano Stabellini

Quoted 4 lines

Cheers,

--
Julien Grall

Cheers,

--
Penny Zheng

#44 lore raw

Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 77 lines · Penny Zheng

Hi Julien

Quoted 75 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, January 30, 2023 5:40 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 12 lines · Penny Zheng

There are three types of MPU regions during boot-time:
1. Fixed MPU region
Regions like Xen text section, Xen heap section, etc.
2. Boot-only MPU region
Regions like Xen init sections, etc. It will be removed at the end of booting.
3. Regions need switching in/out during vcpu context switch
Region like system device memory map.
For example, for FVP_BaseR_AEMv8R, we have [0x80000000, 0xfffff000) as the whole system device memory map for Xen(idle vcpu) in EL2, when context switching to guest vcpu, it shall be replaced with guest-specific device mapping, like vgic, vpl011, passthrough device, etc.

Quoted 1 line

We don't have two mappings for different stage translations in MPU, like

we had in MMU.

Quoted 2 lines

Xen stage 1 EL2 mapping and stage 2 mapping are both sharing one MPU memory mapping(xen_mpumap) So to save the trouble of hunting down

each

Quoted 1 line

switching regions in time-sensitive context switch, we must re-order

xen_mpumap to keep fixed regions in the front, and switching ones in the heels of them.

From my understanding, hunting down each switching regions would be a
matter to loop over a bitmap. There will be a slight increase in the number of instructions executed, but I don't believe it will be noticeable.

Quoted 4 lines

In Patch Serie v1, I was adding MPU regions in sequence, and I introduced a set of bitmaps to record the location of same type regions. At the end of booting, I need to *disable* MPU to do the

reshuffling, as I can't move regions like xen heap while MPU on.

Quoted 6 lines

And we discussed that it is too risky to disable MPU, and you suggested [1] "

Quoted 2 lines

You should not need any reorg if you map the boot-only section towards in the higher slot index (or just after the fixed ones).

"

Right, looking at the new code. I realize that this was probably a bad idea because we are adding complexity in the assembly code.

Quoted 3 lines

Maybe in assembly, we know exactly how many fixed regions are, boot-only regions are, but in C codes, we parse FDT to get static

configuration, like we don't know how many fixed regions for xen static heap is enough.

Quoted 2 lines

Approximation is not suggested in MPU system with limited MPU regions, some platform may only have 16 MPU regions, IMHO, it is not worthy

wasting in approximation.

I haven't suggested to use approximation anywhere here. I will answer about the limited number of entries in the other thread.

Quoted 4 lines

So I take the suggestion of putting regions in the higher slot index. Putting fixed regions in the front, and putting boot-only and switching ones at tail. Then, at the end of booting, when we reorg the

mpu mapping, we remove all boot-only regions, and for switching ones, we disable-relocate(after fixed ones)-enable them. Specific codes in [2].

From this discussion, it feels to me that you are trying to make the code
more complicated just to keep the split and save a few cycles (see above).

Quoted 6 lines
Quoted 3 lines

I would suggest to investigate the cost of "hunting down each section". Depending on the result, we can discuss what the best approach.

Correct me if I'm wrong, the complicated things in assembly you are worried about is that we couldn't define the index for initial sections, no hardcoded to keep simple.

Correct.

Quoted 2 lines

And function write_pr, ik, is really a big chunk of codes, however the logic is simple there, just a bunch of "switch-cases".

I agree that write_pr() is a bunch of switch-cases. But there are a lot of duplication in it and the interface to use it is, IMHO, not intuitive.

Quoted 8 lines

If we are adding MPU regions in sequence as you suggested, while using bitmap at the same time to record used entry.
TBH, this is how I designed at the very beginning internally. We found that if we don't do reorg late-boot to keep fixed in front and switching ones after, each time when we do vcpu context switch, not only we need to hunt down switching ones to disable, while we add new switch-in regions, using bitmap to find free entry is saying that the process is unpredictable. Uncertainty is what we want to avoid in Armv8-R architecture.

I don't understand why it would be unpredictable. For a given combination of platform/device-tree, the bitmap will always look the same. So the number of cycles/instructions will always be the same.

This is not very different from the case where you split the MPU in two because

Quoted 5 lines

Hmmm, TBH, I really really like your suggestion to put boot-only/switching regions into higher slot. It really saved a lot trouble in late-init reorg and also avoids disabling MPU at the same time. The split is a simple and easy-to-understand construction compared with bitmap too.

I would like to propose another split. I will reply to that in the thread where you provided the MPU layout.

Cheers,

Signature · 1 line
Julien Grall
#45 lore raw

RE: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Julien,

Quoted 30 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Tuesday, January 31, 2023 5:28 PM
To: Penny Zheng <[email protected]>; [email protected]; Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; [email protected]
Cc: Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 6 lines · Penny Zheng

Hi Julien

Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, January 30, 2023 5:40 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 9 lines
Quoted 9 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

[...]

Quoted 25 lines
Quoted 7 lines
Quoted 4 lines

I would suggest to investigate the cost of "hunting down each section". Depending on the result, we can discuss what the best approach.

Correct me if I'm wrong, the complicated things in assembly you are worried about is that we couldn't define the index for initial sections, no

hardcoded to keep simple.

Correct.

Quoted 2 lines

And function write_pr, ik, is really a big chunk of codes, however the logic is simple there, just a bunch of "switch-cases".

I agree that write_pr() is a bunch of switch-cases. But there are a lot of duplication in it and the interface to use it is, IMHO, not intuitive.

Quoted 8 lines

If we are adding MPU regions in sequence as you suggested, while using bitmap at the same time to record used entry.
TBH, this is how I designed at the very beginning internally. We found that if we don't do reorg late-boot to keep fixed in front and switching ones after, each time when we do vcpu context switch, not only we need to hunt down switching ones to disable, while we add new switch-in regions, using bitmap to find free entry is saying that the

Quoted 7 lines

process is unpredictable. Uncertainty is what we want to avoid in Armv8-R architecture.

I don't understand why it would be unpredictable. For a given combination of platform/device-tree, the bitmap will always look the same. So the number of cycles/instructions will always be the same.

In boot-time, it will be always the same. But if we still use bitmap to find free entry(for switching MPU regions) on runtime, hmmm, I thought this part will be unpredictable.

Quoted 17 lines

This is not very different from the case where you split the MPU in two because

Quoted 5 lines

Hmmm, TBH, I really really like your suggestion to put boot-only/switching regions into higher slot. It really saved a lot trouble in late-init reorg and also avoids disabling MPU at the same time. The split is a simple and easy-to-understand construction compared

with bitmap too.

I would like to propose another split. I will reply to that in the thread where you provided the MPU layout.

Cheers,

--
Julien Grall

#46 lore raw

Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 10 lines · Penny Zheng
Quoted 10 lines
Quoted 7 lines

If we are adding MPU regions in sequence as you suggested, while using bitmap at the same time to record used entry.
TBH, this is how I designed at the very beginning internally. We found that if we don't do reorg late-boot to keep fixed in front and switching ones after, each time when we do vcpu context switch, not only we need to hunt down switching ones to disable, while we add new switch-in regions, using bitmap to find free entry is saying that the

process is unpredictable. Uncertainty is what we want to avoid in Armv8-R architecture.

Quoted 8 lines
Quoted 4 lines

I don't understand why it would be unpredictable. For a given combination of platform/device-tree, the bitmap will always look the same. So the number of cycles/instructions will always be the same.

In boot-time, it will be always the same. But if we still use bitmap to find free entry(for switching MPU regions) on runtime, hmmm, I thought this part will be unpredictable.

I know this point is now moot as we agreed on not using a bitmap but I wanted to answer on the unpredictability part.

It depends on whether you decide to allocate more entry at runtime. My assumption is you won't and therefore the the time to walk the bitmap will always be consistent.

Cheers,

Signature · 1 line
Julien Grall
#47 lore raw

RE: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Julien,

Quoted 34 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, February 2, 2023 2:57 AM
To: Penny Zheng <[email protected]>; [email protected]; Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; [email protected]
Cc: Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 11 lines · Penny Zheng
Quoted 11 lines
Quoted 8 lines

If we are adding MPU regions in sequence as you suggested, while using bitmap at the same time to record used entry.
TBH, this is how I designed at the very beginning internally. We found that if we don't do reorg late-boot to keep fixed in front and switching ones after, each time when we do vcpu context switch, not only we need to hunt down switching ones to disable, while we add new switch-in regions, using bitmap to find free entry is saying that the

process is unpredictable. Uncertainty is what we want to avoid in Armv8-R architecture.

Quoted 8 lines
Quoted 4 lines

I don't understand why it would be unpredictable. For a given combination of platform/device-tree, the bitmap will always look the same. So the number of cycles/instructions will always be the same.

In boot-time, it will be always the same. But if we still use bitmap to find free entry(for switching MPU regions) on runtime, hmmm, I thought this part will be unpredictable.

Quoted 7 lines

I know this point is now moot as we agreed on not using a bitmap but I wanted to answer on the unpredictability part.

It depends on whether you decide to allocate more entry at runtime. My assumption is you won't and therefore the the time to walk the bitmap will always be consistent.

In MPU, we don't have something like vttbr_el2 in MMU, to store stage 2 EL1/EL0 translation table. Xen stage 1 EL2 mapping and stage 2 EL1/EL0 mapping are both sharing one table.
So when context switching into different guest, the current design is to disable DOM1's guest RAM mapping firstly, then enable DOM2's guest RAM mapping, to ensure isolation and safety.

Quoted 4 lines

Cheers,

--
Julien Grall

#48 lore raw

Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 1 line · Penny Zheng

Hi Julien,

Hi,

Quoted 42 lines
Quoted 34 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, February 2, 2023 2:57 AM
To: Penny Zheng <[email protected]>; [email protected]; Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; [email protected]
Cc: Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 11 lines · Penny Zheng
Quoted 11 lines
Quoted 8 lines

If we are adding MPU regions in sequence as you suggested, while using bitmap at the same time to record used entry.
TBH, this is how I designed at the very beginning internally. We found that if we don't do reorg late-boot to keep fixed in front and switching ones after, each time when we do vcpu context switch, not only we need to hunt down switching ones to disable, while we add new switch-in regions, using bitmap to find free entry is saying that the

process is unpredictable. Uncertainty is what we want to avoid in Armv8-R architecture.

Quoted 8 lines
Quoted 4 lines

I don't understand why it would be unpredictable. For a given combination of platform/device-tree, the bitmap will always look the same. So the number of cycles/instructions will always be the same.

In boot-time, it will be always the same. But if we still use bitmap to find free entry(for switching MPU regions) on runtime, hmmm, I thought this part will be unpredictable.

Quoted 7 lines

I know this point is now moot as we agreed on not using a bitmap but I wanted to answer on the unpredictability part.

It depends on whether you decide to allocate more entry at runtime. My assumption is you won't and therefore the the time to walk the bitmap will always be consistent.

Quoted 6 lines

In MPU, we don't have something like vttbr_el2 in MMU, to store stage 2 EL1/EL0 translation table. Xen stage 1 EL2 mapping and stage 2 EL1/EL0 mapping are both sharing one table.
So when context switching into different guest, the current design is to disable DOM1's guest RAM mapping firstly, then enable DOM2's guest RAM mapping, to ensure isolation and safety.

I understood that but I don't understand how this is related to my point here. The entries you are replacing are always going to be the same after boot.

So if you have a bitmap indicate the fixed entries and you don't add more fixed one at runtime, then it will always take the same time to walk it.

Cheers,

Signature · 1 line
Julien Grall
#49 lore raw

RE: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi, Julien

Quoted 70 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, February 2, 2023 6:58 PM
To: Penny Zheng <[email protected]>; [email protected]; Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; [email protected]
Cc: Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Quoted 1 line · Penny Zheng

Hi Julien,

Hi,

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, February 2, 2023 2:57 AM
To: Penny Zheng <[email protected]>; [email protected]; Wei Chen <[email protected]>;

Stefano

Quoted 17 lines
Quoted 17 lines

Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; [email protected]
Cc: Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 11/40] xen/mpu: build up start-of-day Xen MPU memory region map

Hi Penny,

Quoted 8 lines · Penny Zheng
Quoted 8 lines
Quoted 8 lines

If we are adding MPU regions in sequence as you suggested, while using bitmap at the same time to record used entry. TBH, this is how I designed at the very beginning internally. We found that if we don't do reorg late-boot to keep fixed in front and switching ones after, each time when we do vcpu context switch, not only we need to hunt down switching ones to disable, while we add new switch-in regions, using bitmap to find free entry is saying that the

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

process is unpredictable. Uncertainty is what we want to avoid in Armv8-R architecture.

I don't understand why it would be unpredictable. For a given combination of platform/device-tree, the bitmap will always look the same. So the number of cycles/instructions will always be the

same.

Quoted 14 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

In boot-time, it will be always the same. But if we still use bitmap to find free entry(for switching MPU regions) on runtime, hmmm, I thought this part will be unpredictable.

Quoted 7 lines

I know this point is now moot as we agreed on not using a bitmap but I wanted to answer on the unpredictability part.

It depends on whether you decide to allocate more entry at runtime. My assumption is you won't and therefore the the time to walk the bitmap will always be consistent.

Quoted 7 lines

In MPU, we don't have something like vttbr_el2 in MMU, to store stage 2
EL1/EL0 translation table. Xen stage 1 EL2 mapping and stage 2 EL1/EL0 mapping are both sharing one table.
So when context switching into different guest, the current design is to disable DOM1's guest RAM mapping firstly, then enable DOM2's guest RAM mapping, to ensure isolation and safety.

Quoted 7 lines

I understood that but I don't understand how this is related to my point here. The entries you are replacing are always going to be the same after boot.

So if you have a bitmap indicate the fixed entries and you don't add more fixed one at runtime, then it will always take the same time to walk it.

Ah, sorry for taking so long to understand ;/. True, the fixed entries will never change after boot-time, each time when switching to guest vcpu, we always choose the same entry.

Quoted 4 lines

Cheers,

--
Julien Grall

Cheers,

--
Penny Zheng

#50 lore raw

PATCH v2 12/40 xen/mpu: introduce helpers for MPU enablement

In reply to Penny Zheng

We need a new helper for Xen to enable MPU in boot-time.
The new helper is semantically consistent with the original enable_mmu.

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 always turn off the Background region.

In this patch, we also introduce a neutral name enable_mm for Xen to enable MMU/MPU. This can help us to keep one code flow in head.S

---
 xen/arch/arm/arm64/head.S     |  5 +++--
 xen/arch/arm/arm64/head_mmu.S |  4 ++--
 xen/arch/arm/arm64/head_mpu.S | 19 +++++++++++++++++++
 3 files changed, 24 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/head.S +3 -2
index 145e3d53dc..7f3f973468 100644
@@ -258,7 +258,8 @@ real_start_efi:
          * and memory regions for MPU systems.
          */
         bl    prepare_early_mappings
-        bl    enable_mmu
+        /* Turn on MMU or MPU */
+        bl    enable_mm
 
         /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
         ldr   x0, =primary_switched
@@ -316,7 +317,7 @@ GLOBAL(init_secondary)
         bl    check_cpu_mode
         bl    cpu_init
         bl    prepare_early_mappings
-        bl    enable_mmu
+        bl    enable_mm
 
         /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
         ldr   x0, =secondary_switched
xen/arch/arm/arm64/head_mmu.S +2 -2
index 2346f755df..b59c40495f 100644
@@ -217,7 +217,7 @@ ENDPROC(prepare_early_mappings)
  *
  * Clobbers x0 - x3
  */
-ENTRY(enable_mmu)
+ENTRY(enable_mm)
         PRINT("- Turning on paging -\r\n")
 
         /*
@@ -239,7 +239,7 @@ ENTRY(enable_mmu)
         msr   SCTLR_EL2, x0          /* now paging is enabled */
         isb                          /* Now, flush the icache */
         ret
-ENDPROC(enable_mmu)
+ENDPROC(enable_mm)
 
 /*
  * Remove the 1:1 map from the page-tables. It is not easy to keep track
xen/arch/arm/arm64/head_mpu.S +19 -0
index 0b97ce4646..e2ac69b0cc 100644
@@ -315,6 +315,25 @@ ENDPROC(prepare_early_mappings)
 
 GLOBAL(_end_boot)
 
+/*
+ * 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.
+ */
+ENTRY(enable_mm)
+    mrs   x0, SCTLR_EL2
+    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
+ENDPROC(enable_mm)
+
 /*
  * Local variables:
  * mode: ASM
Signature · 1 line
2.25.1
#51 lore raw

Re: [PATCH v2 12/40] xen/mpu: introduce helpers for MPU enablement

Hi Penny,

Quoted 85 lines · Penny Zheng

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

We need a new helper for Xen to enable MPU in boot-time.
The new helper is semantically consistent with the original enable_mmu.

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 always turn off the Background region.

In this patch, we also introduce a neutral name enable_mm for Xen to enable MMU/MPU. This can help us to keep one code flow in head.S

---
  xen/arch/arm/arm64/head.S     |  5 +++--
  xen/arch/arm/arm64/head_mmu.S |  4 ++--
  xen/arch/arm/arm64/head_mpu.S | 19 +++++++++++++++++++
  3 files changed, 24 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/head.S +3 -2
index 145e3d53dc..7f3f973468 100644
@@ -258,7 +258,8 @@ real_start_efi:
           * and memory regions for MPU systems.
           */
          bl    prepare_early_mappings
-        bl    enable_mmu
+        /* Turn on MMU or MPU */
+        bl    enable_mm
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
          ldr   x0, =primary_switched
@@ -316,7 +317,7 @@ GLOBAL(init_secondary)
          bl    check_cpu_mode
          bl    cpu_init
          bl    prepare_early_mappings
-        bl    enable_mmu
+        bl    enable_mm
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
          ldr   x0, =secondary_switched
xen/arch/arm/arm64/head_mmu.S +2 -2
index 2346f755df..b59c40495f 100644
@@ -217,7 +217,7 @@ ENDPROC(prepare_early_mappings)
   *
   * Clobbers x0 - x3
   */
-ENTRY(enable_mmu)
+ENTRY(enable_mm)
          PRINT("- Turning on paging -\r\n")
 
          /*
@@ -239,7 +239,7 @@ ENTRY(enable_mmu)
          msr   SCTLR_EL2, x0          /* now paging is enabled */
          isb                          /* Now, flush the icache */
          ret
-ENDPROC(enable_mmu)
+ENDPROC(enable_mm)
 
  /*
   * Remove the 1:1 map from the page-tables. It is not easy to keep track
xen/arch/arm/arm64/head_mpu.S +8 -0
index 0b97ce4646..e2ac69b0cc 100644
@@ -315,6 +315,25 @@ ENDPROC(prepare_early_mappings)
 
  GLOBAL(_end_boot)
 
+/*
+ * 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/head_mpu.S ENDPROC(prepare_early_mappings) · line 335

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S +10 -0
+ENTRY(enable_mm)
+    mrs   x0, SCTLR_EL2
+    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
+ENDPROC(enable_mm)

Can this be renamed to enable_mpu or enable_mpu_and_cache() ?

Can we also have the corresponding disable function in this patch ?

Also (compared with "[PATCH v6 10/11] xen/arm64: introduce helpers for MPU enable/disable"), I see that you have added #SCTLR_Axx_ELx_WXN. What is the reason for this ?

- Ayan

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +1 -1
+
  /*
   * Local variables:
   * mode: ASM
--

2.25.1

#52 lore raw

Re: [PATCH v2 12/40] xen/mpu: introduce helpers for MPU enablement

In reply to Penny Zheng

Hi Penny,

Quoted 8 lines · Penny Zheng

We need a new helper for Xen to enable MPU in boot-time.
The new helper is semantically consistent with the original enable_mmu.

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 always turn off the Background region.

You are saying this. But I don't see any code below clearing SCTLR_EL2.BR. Can you clarify?

Quoted 4 lines

In this patch, we also introduce a neutral name enable_mm for Xen to enable MMU/MPU. This can help us to keep one code flow in head.S

NIT: Missing full stop.

Quoted 59 lines
---
  xen/arch/arm/arm64/head.S     |  5 +++--
  xen/arch/arm/arm64/head_mmu.S |  4 ++--
  xen/arch/arm/arm64/head_mpu.S | 19 +++++++++++++++++++
  3 files changed, 24 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/head.S +3 -2
index 145e3d53dc..7f3f973468 100644
@@ -258,7 +258,8 @@ real_start_efi:
           * and memory regions for MPU systems.
           */
          bl    prepare_early_mappings
-        bl    enable_mmu
+        /* Turn on MMU or MPU */
+        bl    enable_mm
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
          ldr   x0, =primary_switched
@@ -316,7 +317,7 @@ GLOBAL(init_secondary)
          bl    check_cpu_mode
          bl    cpu_init
          bl    prepare_early_mappings
-        bl    enable_mmu
+        bl    enable_mm
 
          /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
          ldr   x0, =secondary_switched
xen/arch/arm/arm64/head_mmu.S +2 -2
index 2346f755df..b59c40495f 100644
@@ -217,7 +217,7 @@ ENDPROC(prepare_early_mappings)
   *
   * Clobbers x0 - x3
   */
-ENTRY(enable_mmu)
+ENTRY(enable_mm)
          PRINT("- Turning on paging -\r\n")
 
          /*
@@ -239,7 +239,7 @@ ENTRY(enable_mmu)
          msr   SCTLR_EL2, x0          /* now paging is enabled */
          isb                          /* Now, flush the icache */
          ret
-ENDPROC(enable_mmu)
+ENDPROC(enable_mm)
 
  /*
   * Remove the 1:1 map from the page-tables. It is not easy to keep track
xen/arch/arm/arm64/head_mpu.S +0 -0
index 0b97ce4646..e2ac69b0cc 100644

xen/arch/arm/arm64/head_mpu.S ENDPROC(prepare_early_mappings) · line 324

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S +7 -0
@@ -315,6 +315,25 @@ ENDPROC(prepare_early_mappings)
 
  GLOBAL(_end_boot)
 
+/*
+ * 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.

Please document which register you are clobberring. See the MMU code for examples how to do you.

xen/arch/arm/arm64/head_mpu.S ENDPROC(prepare_early_mappings) · line 331

Quoted 7 lines
xen/arch/arm/arm64/head_mpu.S +7 -0
+ */
+ENTRY(enable_mm)
+    mrs   x0, SCTLR_EL2
+    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

Please document the reason of each dsb. In this case, it is not entirely clear what this is for.

xen/arch/arm/arm64/head_mpu.S ENDPROC(prepare_early_mappings) · line 333

Quoted 2 lines
xen/arch/arm/arm64/head_mpu.S +2 -0
+    msr   SCTLR_EL2, x0
+    isb

Likely for isb.

xen/arch/arm/arm64/head_mpu.S ENDPROC(prepare_early_mappings) · line 336

Quoted 3 lines
xen/arch/arm/arm64/head_mpu.S +3 -0
+    ret
+ENDPROC(enable_mm)
+

Cheers,

Signature · 1 line
Julien Grall
#53 lore raw

PATCH v2 13/40 xen/mpu: introduce unified function setup_early_uart to map early UART

In reply to Penny Zheng

In MMU system, we map the UART in the fixmap (when earlyprintk is used). However in MPU system, we map the UART with a transient MPU memory region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
 xen/arch/arm/arm64/head.S               |  2 +-
 xen/arch/arm/arm64/head_mmu.S           |  4 +-
 xen/arch/arm/arm64/head_mpu.S           | 52 +++++++++++++++++++++++++
 xen/arch/arm/include/asm/early_printk.h |  1 +
 4 files changed, 56 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/head.S +1 -1
index 7f3f973468..a92883319d 100644
@@ -272,7 +272,7 @@ primary_switched:
          * afterwards.
          */
         bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
 #ifdef CONFIG_EARLY_PRINTK
         /* Use a virtual address to access the UART. */
         ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
xen/arch/arm/arm64/head_mmu.S +2 -2
index b59c40495f..a19b7c873d 100644
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
  *
  * Clobbers x0 - x3
  */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)
 #ifdef CONFIG_EARLY_PRINTK
         /* Add UART to the fixmap table */
         ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
         dsb   nshst
 
         ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
 /* Fail-stop */
 fail:   PRINT("- Boot failed -\r\n")
xen/arch/arm/arm64/head_mpu.S +52 -0

66 lines not on this page · raw message

xen/arch/arm/include/asm/early_printk.h +1 -0
index 44a230853f..d87623e6d5 100644
@@ -22,6 +22,7 @@
  * for EARLY_UART_VIRTUAL_ADDRESS.
  */
 #define EARLY_UART_VIRTUAL_ADDRESS CONFIG_EARLY_UART_BASE_ADDRESS
+#define EARLY_UART_SIZE            0x1000
 
 #else
 
Signature · 1 line
2.25.1
#54 lore raw

Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Peny,

Quoted 34 lines · Penny Zheng

In MMU system, we map the UART in the fixmap (when earlyprintk is used). However in MPU system, we map the UART with a transient MPU memory region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
  xen/arch/arm/arm64/head.S               |  2 +-
  xen/arch/arm/arm64/head_mmu.S           |  4 +-
  xen/arch/arm/arm64/head_mpu.S           | 52 +++++++++++++++++++++++++
  xen/arch/arm/include/asm/early_printk.h |  1 +
  4 files changed, 56 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/head.S +1 -1
index 7f3f973468..a92883319d 100644
@@ -272,7 +272,7 @@ primary_switched:
           * afterwards.
           */
          bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */
          ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
xen/arch/arm/arm64/head_mmu.S +0 -0
index b59c40495f..a19b7c873d 100644

xen/arch/arm/arm64/head_mmu.S ENDPROC(remove_identity_mapping) · line 315

Quoted 5 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
   *
   * Clobbers x0 - x3
   */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)

This function is doing more than enable the early UART. It also setups the fixmap even earlyprintk is not configured.

I am not entirely sure what could be the name. Maybe this needs to be split further.

Quoted 28 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
  #ifdef CONFIG_EARLY_PRINTK
          /* Add UART to the fixmap table */
          ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
          dsb   nshst
 
          ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
  /* Fail-stop */
  fail:   PRINT("- Boot failed -\r\n")
xen/arch/arm/arm64/head_mpu.S +2 -0
index e2ac69b0cc..72d1e0863d 100644
@@ -18,8 +18,10 @@
  #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_DEVICE_PRBAR     0x22    /* SH=10 AP=00 XN=10 */
 
  #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+#define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 
  /*
   * Macro to round up the section address to be PAGE_SIZE aligned

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 341

Quoted 6 lines
xen/arch/arm/arm64/head_mpu.S +3 -0
@@ -334,6 +336,56 @@ ENTRY(enable_mm)
      ret
  ENDPROC(enable_mm)
 
+/*
+ * Map the early UART with a new transient MPU memory region.
+ *

Missing "Inputs: "

Quoted 5 lines
xen/arch/arm/arm64/head_mpu.S +5 -0
+ * x27: region selector
+ * x28: prbar
+ * x29: prlar
+ *
+ * Clobbers x0 - x4

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 356

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S +10 -0
+ *
+ */
+ENTRY(setup_early_uart)
+#ifdef CONFIG_EARLY_PRINTK
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x3, lr
+
+    /*
+     * MPU region for early UART is a transient region, since it will be
+     * replaced by specific device memory layout when FDT gets parsed.

I would rather not mention "FDT" here because this code is independent to the firmware table used.

However, any reason to use a transient region rather than the one that will be used for the UART driver?

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 364

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+     */
+    load_paddr x0, next_transient_region_idx
+    ldr   x4, [x0]
+
+    ldr   x28, =CONFIG_EARLY_UART_BASE_ADDRESS
+    and   x28, x28, #MPU_REGION_MASK
+    mov   x1, #REGION_DEVICE_PRBAR
+    orr   x28, x28, x1

This needs some documentation to explain the logic. Maybe even a macro.

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 367

Quoted 3 lines
xen/arch/arm/arm64/head_mpu.S +3 -0
+
+    ldr x29, =(CONFIG_EARLY_UART_BASE_ADDRESS + EARLY_UART_SIZE)
+    roundup_section x29

Does this mean we could give access to more than necessary? Shouldn't instead prevent compilation if the size doesn't align with the section size?

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 374

Quoted 7 lines
xen/arch/arm/arm64/head_mpu.S +7 -0
+    /* Limit address is inclusive */
+    sub   x29, x29, #1
+    and   x29, x29, #MPU_REGION_MASK
+    mov   x2, #REGION_DEVICE_PRLAR
+    orr   x29, x29, x2
+
+    mov   x27, x4

This needs some documentation like:

x27: region selector

See how we documented the existing helpers.

Quoted 22 lines
xen/arch/arm/arm64/head_mpu.S +14 -0
+    bl    write_pr
+
+    /* Create a new entry in xen_mpumap for early UART */
+    create_mpu_entry xen_mpumap, x4, x28, x29, x1, x2
+
+    /* Update next_transient_region_idx */
+    sub   x4, x4, #1
+    str   x4, [x0]
+
+    mov   lr, x3
+    ret
+#endif
+ENDPROC(setup_early_uart)
+
  /*
   * Local variables:
   * mode: ASM
xen/arch/arm/include/asm/early_printk.h +0 -0
index 44a230853f..d87623e6d5 100644

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

Quoted 4 lines
xen/arch/arm/include/asm/early_printk.h +1 -0
@@ -22,6 +22,7 @@
   * for EARLY_UART_VIRTUAL_ADDRESS.
   */
  #define EARLY_UART_VIRTUAL_ADDRESS CONFIG_EARLY_UART_BASE_ADDRESS
+#define EARLY_UART_SIZE            0x1000

Shouldn't this be PAGE_SIZE? If not, how did you come up with the number?

Cheers,

Signature · 1 line
Julien Grall
#55 lore raw

RE: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 11 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Peny,

Hi Julien,

Quoted 35 lines · Penny Zheng
Quoted 7 lines

In MMU system, we map the UART in the fixmap (when earlyprintk is used). However in MPU system, we map the UART with a transient MPU memory region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

Quoted 6 lines
---
  xen/arch/arm/arm64/head.S               |  2 +-
  xen/arch/arm/arm64/head_mmu.S           |  4 +-
  xen/arch/arm/arm64/head_mpu.S           | 52

+++++++++++++++++++++++++

Quoted 17 lines
  xen/arch/arm/include/asm/early_printk.h |  1 +
  4 files changed, 56 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/head.S +1 -1
index 7f3f973468..a92883319d 100644
@@ -272,7 +272,7 @@ primary_switched:
           * afterwards.
           */
          bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */
          ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mmu.S index b59c40495f..a19b7c873d

100644

Quoted 11 lines

xen/arch/arm/arm64/head_mmu.S ENDPROC(remove_identity_mapping) · line 315

Quoted 8 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
   *
   * Clobbers x0 - x3
   */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)

This function is doing more than enable the early UART. It also setups the fixmap even earlyprintk is not configured.

True, true.
I've thoroughly read the MMU implementation of setup_fixmap, and I'll try to split it up.

Quoted 61 lines

I am not entirely sure what could be the name. Maybe this needs to be split further.

Quoted 13 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
  #ifdef CONFIG_EARLY_PRINTK
          /* Add UART to the fixmap table */
          ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
          dsb   nshst
 
          ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
  /* Fail-stop */
  fail:   PRINT("- Boot failed -\r\n")
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mpu.S index e2ac69b0cc..72d1e0863d

100644

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S +2 -0
@@ -18,8 +18,10 @@
  #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_DEVICE_PRBAR     0x22    /* SH=10 AP=00 XN=10 */
 
  #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+#define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */

xen/arch/arm/arm64/head_mpu.S

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S +3 -1
 
  /*
   * Macro to round up the section address to be PAGE_SIZE aligned @@
-334,6 +336,56 @@ ENTRY(enable_mm)
      ret
  ENDPROC(enable_mm)
 
+/*
+ * Map the early UART with a new transient MPU memory region.
+ *

Missing "Inputs: "

Quoted 7 lines

+ * x27: region selector
+ * x28: prbar
+ * x29: prlar
+ *
+ * Clobbers x0 - x4
+ *
+ */

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 356

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+ENTRY(setup_early_uart)
+#ifdef CONFIG_EARLY_PRINTK
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x3, lr
+
+    /*
+     * MPU region for early UART is a transient region, since it will be
+     * replaced by specific device memory layout when FDT gets parsed.

I would rather not mention "FDT" here because this code is independent to the firmware table used.

Quoted 3 lines

However, any reason to use a transient region rather than the one that will be used for the UART driver?

We don’t want to define a MPU region for each device driver. It will exhaust
MPU regions very quickly.
In commit " [PATCH v2 28/40] xen/mpu: map boot module section in MPU system",
A new FDT property mpu,device-memory-section will be introduced for users to statically configure the whole system device memory with the least number of memory regions in Device Tree. This section shall cover all devices that will be used in Xen, like UART, GIC, etc. For FVP_BaseR_AEMv8R, we have the following definition:

```
mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;
```
Quoted 11 lines

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 364

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+     */
+    load_paddr x0, next_transient_region_idx
+    ldr   x4, [x0]
+
+    ldr   x28, =CONFIG_EARLY_UART_BASE_ADDRESS
+    and   x28, x28, #MPU_REGION_MASK
+    mov   x1, #REGION_DEVICE_PRBAR
+    orr   x28, x28, x1

This needs some documentation to explain the logic. Maybe even a macro.

Do you suggest that I shall explain how we compose PRBAR_EL2 register?

Quoted 7 lines

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 367

Quoted 3 lines
xen/arch/arm/arm64/head_mpu.S +3 -0
+
+    ldr x29, =(CONFIG_EARLY_UART_BASE_ADDRESS + EARLY_UART_SIZE)
+    roundup_section x29

Does this mean we could give access to more than necessary? Shouldn't instead prevent compilation if the size doesn't align with the section size?

True, we could not treat uart section like we do for the section defined in xen.lds.S. CONFIG_EARLY_UART_BASE_ADDRESS and EARLY_UART_SIZE shall both be checked if it is aligned with PAGE_SIZE.

Quoted 49 lines

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 374

Quoted 7 lines
xen/arch/arm/arm64/head_mpu.S +7 -0
+    /* Limit address is inclusive */
+    sub   x29, x29, #1
+    and   x29, x29, #MPU_REGION_MASK
+    mov   x2, #REGION_DEVICE_PRLAR
+    orr   x29, x29, x2
+
+    mov   x27, x4

This needs some documentation like:

x27: region selector

See how we documented the existing helpers.

Quoted 23 lines
xen/arch/arm/arm64/head_mpu.S +14 -0
+    bl    write_pr
+
+    /* Create a new entry in xen_mpumap for early UART */
+    create_mpu_entry xen_mpumap, x4, x28, x29, x1, x2
+
+    /* Update next_transient_region_idx */
+    sub   x4, x4, #1
+    str   x4, [x0]
+
+    mov   lr, x3
+    ret
+#endif
+ENDPROC(setup_early_uart)
+
  /*
   * Local variables:
   * mode: ASM
xen/arch/arm/include/asm/early_printk.h +0 -0
index 44a230853f..d87623e6d5 100644

xen/arch/arm/include/asm/early_printk.h line 24

Quoted 3 lines
xen/arch/arm/include/asm/early_printk.h +0 -0
@@ -22,6 +22,7 @@
   * for EARLY_UART_VIRTUAL_ADDRESS.
   */
  #define EARLY_UART_VIRTUAL_ADDRESS

CONFIG_EARLY_UART_BASE_ADDRESS

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

Quoted 1 line
xen/arch/arm/include/asm/early_printk.h +1 -0
+#define EARLY_UART_SIZE            0x1000

Shouldn't this be PAGE_SIZE? If not, how did you come up with the number?

Cheers,

--
Julien Grall

#56 lore raw

Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 126 lines · Penny Zheng
Quoted 11 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Peny,

Hi Julien,

Quoted 35 lines · Penny Zheng
Quoted 7 lines

In MMU system, we map the UART in the fixmap (when earlyprintk is used). However in MPU system, we map the UART with a transient MPU memory region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

Quoted 6 lines
---
   xen/arch/arm/arm64/head.S               |  2 +-
   xen/arch/arm/arm64/head_mmu.S           |  4 +-
   xen/arch/arm/arm64/head_mpu.S           | 52

+++++++++++++++++++++++++

Quoted 17 lines
   xen/arch/arm/include/asm/early_printk.h |  1 +
   4 files changed, 56 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/head.S +1 -1
index 7f3f973468..a92883319d 100644
@@ -272,7 +272,7 @@ primary_switched:
            * afterwards.
            */
           bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
   #ifdef CONFIG_EARLY_PRINTK
           /* Use a virtual address to access the UART. */
           ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mmu.S index b59c40495f..a19b7c873d

100644

Quoted 11 lines

xen/arch/arm/arm64/head_mmu.S ENDPROC(remove_identity_mapping) · line 315

Quoted 8 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
    *
    * Clobbers x0 - x3
    */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)

This function is doing more than enable the early UART. It also setups the fixmap even earlyprintk is not configured.

True, true.
I've thoroughly read the MMU implementation of setup_fixmap, and I'll try to split it up.

Quoted 61 lines

I am not entirely sure what could be the name. Maybe this needs to be split further.

Quoted 13 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
   #ifdef CONFIG_EARLY_PRINTK
           /* Add UART to the fixmap table */
           ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
           dsb   nshst
 
           ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
   /* Fail-stop */
   fail:   PRINT("- Boot failed -\r\n")
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mpu.S index e2ac69b0cc..72d1e0863d

100644

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S +2 -0
@@ -18,8 +18,10 @@
   #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_DEVICE_PRBAR     0x22    /* SH=10 AP=00 XN=10 */
 
   #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+#define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */

xen/arch/arm/arm64/head_mpu.S

Quoted 10 lines
xen/arch/arm/arm64/head_mpu.S +3 -1
 
   /*
    * Macro to round up the section address to be PAGE_SIZE aligned @@
-334,6 +336,56 @@ ENTRY(enable_mm)
       ret
   ENDPROC(enable_mm)
 
+/*
+ * Map the early UART with a new transient MPU memory region.
+ *

Missing "Inputs: "

Quoted 7 lines

+ * x27: region selector
+ * x28: prbar
+ * x29: prlar
+ *
+ * Clobbers x0 - x4
+ *
+ */

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 356

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+ENTRY(setup_early_uart)
+#ifdef CONFIG_EARLY_PRINTK
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x3, lr
+
+    /*
+     * MPU region for early UART is a transient region, since it will be
+     * replaced by specific device memory layout when FDT gets parsed.

I would rather not mention "FDT" here because this code is independent to the firmware table used.

Quoted 6 lines
Quoted 3 lines

However, any reason to use a transient region rather than the one that will be used for the UART driver?

We don’t want to define a MPU region for each device driver. It will exhaust MPU regions very quickly.

What the usual size of an MPU?

However, even if you don't want to define one for every device, it still seem to be sensible to define a fixed temporary one for the early UART as this would simplify the assembly code.

Quoted 1 line

In commit " [PATCH v2 28/40] xen/mpu: map boot module section in MPU system",

Did you mean patch #27?

Quoted 7 lines

A new FDT property mpu,device-memory-section will be introduced for users to statically configure the whole system device memory with the least number of memory regions in Device Tree. This section shall cover all devices that will be used in Xen, like UART, GIC, etc. For FVP_BaseR_AEMv8R, we have the following definition:

```
mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;
```

I am a bit worry this will be a recipe for mistake. Do you have an example where the MPU will be exhausted if we reserve some entries for each device (or some)?

Cheers,

Signature · 1 line
Julien Grall
#57 lore raw

RE: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi, Julien

Quoted 148 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Sunday, January 29, 2023 3:43 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 7 lines
Quoted 7 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 7 lines
Quoted 1 line

Hi Peny,

Hi Julien,

Quoted 3 lines · Penny Zheng
Quoted 1 line

In MMU system, we map the UART in the fixmap (when earlyprintk is

used).

Quoted 1 line
Quoted 1 line
Quoted 1 line

However in MPU system, we map the UART with a transient MPU

memory

Quoted 113 lines
Quoted 31 lines
Quoted 11 lines

region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
   xen/arch/arm/arm64/head.S               |  2 +-
   xen/arch/arm/arm64/head_mmu.S           |  4 +-
   xen/arch/arm/arm64/head_mpu.S           | 52

+++++++++++++++++++++++++

Quoted 17 lines
   xen/arch/arm/include/asm/early_printk.h |  1 +
   4 files changed, 56 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/head.S +1 -1
index 7f3f973468..a92883319d 100644
@@ -272,7 +272,7 @@ primary_switched:
            * afterwards.
            */
           bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
   #ifdef CONFIG_EARLY_PRINTK
           /* Use a virtual address to access the UART. */
           ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mmu.S index b59c40495f..a19b7c873d

100644

Quoted 11 lines

xen/arch/arm/arm64/head_mmu.S ENDPROC(remove_identity_mapping) · line 315

Quoted 8 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
    *
    * Clobbers x0 - x3
    */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)

This function is doing more than enable the early UART. It also setups the fixmap even earlyprintk is not configured.

True, true.
I've thoroughly read the MMU implementation of setup_fixmap, and I'll try to split it up.

Quoted 65 lines

I am not entirely sure what could be the name. Maybe this needs to be split further.

Quoted 13 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
   #ifdef CONFIG_EARLY_PRINTK
           /* Add UART to the fixmap table */
           ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
           dsb   nshst
 
           ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
   /* Fail-stop */
   fail:   PRINT("- Boot failed -\r\n")
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mpu.S index e2ac69b0cc..72d1e0863d

100644

Quoted 18 lines
xen/arch/arm/arm64/head_mpu.S +2 -0
@@ -18,8 +18,10 @@
   #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_DEVICE_PRBAR     0x22    /* SH=10 AP=00 XN=10 */
 
   #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+#define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 
   /*
    * Macro to round up the section address to be PAGE_SIZE aligned

@@
-334,6 +336,56 @@ ENTRY(enable_mm)
ret
ENDPROC(enable_mm)

Quoted 3 lines

+/*
+ * Map the early UART with a new transient MPU memory region.
+ *

Missing "Inputs: "

Quoted 7 lines

+ * x27: region selector
+ * x28: prbar
+ * x29: prlar
+ *
+ * Clobbers x0 - x4
+ *
+ */

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 356

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+ENTRY(setup_early_uart)
+#ifdef CONFIG_EARLY_PRINTK
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x3, lr
+
+    /*
+     * MPU region for early UART is a transient region, since it will be
+     * replaced by specific device memory layout when FDT gets parsed.

I would rather not mention "FDT" here because this code is independent to the firmware table used.

However, any reason to use a transient region rather than the one that will be used for the UART driver?

Quoted 8 lines
Quoted 2 lines

We don’t want to define a MPU region for each device driver. It will exhaust MPU regions very quickly.

What the usual size of an MPU?

However, even if you don't want to define one for every device, it still seem to be sensible to define a fixed temporary one for the early UART as this would simplify the assembly code.

We will add fixed MPU regions for Xen static heap in function setup_mm. If we put early uart region in front(fixed region place), it will leave holes later after removing it.

Quoted 10 lines
Quoted 3 lines

In commit " [PATCH v2 28/40] xen/mpu: map boot module section in MPU system",

Did you mean patch #27?

Quoted 2 lines

A new FDT property mpu,device-memory-section will be introduced for users to statically configure the whole system device memory with the

least number of memory regions in Device Tree.

Quoted 1 line

This section shall cover all devices that will be used in Xen, like UART,

Quoted 9 lines

GIC, etc.

Quoted 3 lines

For FVP_BaseR_AEMv8R, we have the following definition:
```
mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>; ```

I am a bit worry this will be a recipe for mistake. Do you have an example where the MPU will be exhausted if we reserve some entries for each device (or some)?

Yes, we have internal platform where MPU regions are only 16. It will almost eat up all MPU regions based on current implementation, when launching two guests in platform.

Let's calculate the most simple scenario:
The following is MPU-related static configuration in device tree:

```
        mpu,boot-module-section = <0x0 0x10000000 0x0 0x10000000>;
        mpu,guest-memory-section = <0x0 0x20000000 0x0 0x40000000>;
        mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;
        mpu,shared-memory-section = <0x0 0x7a000000 0x0 0x02000000>;

        xen,static-heap = <0x0 0x60000000 0x0 0x1a000000>;
```

At the end of the boot, before reshuffling, the MPU region usage will be as follows:
7 (defined in assembly) + FDT(early_fdt_map) + 5 (at least one region for each "mpu,xxx-memory-section").

That will be already at least 13 MPU regions ;\.

Quoted 4 lines

Cheers,

--
Julien Grall

Cheers,

--
Penny Zheng

#58 lore raw

Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 1 line · Penny Zheng

Hi, Julien

Hi Penny,

Quoted 152 lines
Quoted 152 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Sunday, January 29, 2023 3:43 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 7 lines
Quoted 7 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 7 lines
Quoted 1 line

Hi Peny,

Hi Julien,

Quoted 3 lines · Penny Zheng
Quoted 1 line

In MMU system, we map the UART in the fixmap (when earlyprintk is

used).

Quoted 1 line
Quoted 1 line
Quoted 1 line

However in MPU system, we map the UART with a transient MPU

memory

Quoted 109 lines
Quoted 31 lines
Quoted 11 lines

region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
    xen/arch/arm/arm64/head.S               |  2 +-
    xen/arch/arm/arm64/head_mmu.S           |  4 +-
    xen/arch/arm/arm64/head_mpu.S           | 52

+++++++++++++++++++++++++

Quoted 17 lines
    xen/arch/arm/include/asm/early_printk.h |  1 +
    4 files changed, 56 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/head.S +1 -1
index 7f3f973468..a92883319d 100644
@@ -272,7 +272,7 @@ primary_switched:
             * afterwards.
             */
            bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
    #ifdef CONFIG_EARLY_PRINTK
            /* Use a virtual address to access the UART. */
            ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mmu.S index b59c40495f..a19b7c873d

100644

Quoted 11 lines

xen/arch/arm/arm64/head_mmu.S ENDPROC(remove_identity_mapping) · line 315

Quoted 8 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
     *
     * Clobbers x0 - x3
     */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)

This function is doing more than enable the early UART. It also setups the fixmap even earlyprintk is not configured.

True, true.
I've thoroughly read the MMU implementation of setup_fixmap, and I'll try to split it up.

Quoted 62 lines

I am not entirely sure what could be the name. Maybe this needs to be split further.

Quoted 13 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
    #ifdef CONFIG_EARLY_PRINTK
            /* Add UART to the fixmap table */
            ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
            dsb   nshst
 
            ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
    /* Fail-stop */
    fail:   PRINT("- Boot failed -\r\n")
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mpu.S index e2ac69b0cc..72d1e0863d

100644

Quoted 18 lines
xen/arch/arm/arm64/head_mpu.S +2 -0
@@ -18,8 +18,10 @@
    #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_DEVICE_PRBAR     0x22    /* SH=10 AP=00 XN=10 */
 
    #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1 */
+#define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 
    /*
     * Macro to round up the section address to be PAGE_SIZE aligned

@@
-334,6 +336,56 @@ ENTRY(enable_mm)
ret
ENDPROC(enable_mm)

Quoted 3 lines

+/*
+ * Map the early UART with a new transient MPU memory region.
+ *

Missing "Inputs: "

Quoted 7 lines

+ * x27: region selector
+ * x28: prbar
+ * x29: prlar
+ *
+ * Clobbers x0 - x4
+ *
+ */

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm) · line 356

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+ENTRY(setup_early_uart)
+#ifdef CONFIG_EARLY_PRINTK
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x3, lr
+
+    /*
+     * MPU region for early UART is a transient region, since it will be
+     * replaced by specific device memory layout when FDT gets parsed.

I would rather not mention "FDT" here because this code is independent to the firmware table used.

Quoted 6 lines
Quoted 3 lines

However, any reason to use a transient region rather than the one that will be used for the UART driver?

We don’t want to define a MPU region for each device driver. It will exhaust MPU regions very quickly.

What the usual size of an MPU?

Quoted 8 lines
Quoted 4 lines

However, even if you don't want to define one for every device, it still seem to be sensible to define a fixed temporary one for the early UART as this would simplify the assembly code.

We will add fixed MPU regions for Xen static heap in function setup_mm. If we put early uart region in front(fixed region place), it will leave holes later after removing it.

Why? The entry could be re-used to map the devices entry.

Quoted 16 lines
Quoted 16 lines
Quoted 4 lines

In commit " [PATCH v2 28/40] xen/mpu: map boot module section in MPU system",

Did you mean patch #27?

Quoted 2 lines

A new FDT property mpu,device-memory-section will be introduced for users to statically configure the whole system device memory with the

least number of memory regions in Device Tree.

Quoted 1 line

This section shall cover all devices that will be used in Xen, like UART,

GIC, etc.

Quoted 3 lines

For FVP_BaseR_AEMv8R, we have the following definition:
```
mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>; ```

Quoted 6 lines
Quoted 4 lines

I am a bit worry this will be a recipe for mistake. Do you have an example where the MPU will be exhausted if we reserve some entries for each device (or some)?

Yes, we have internal platform where MPU regions are only 16.

Internal is in silicon (e.g. real) or virtual platform?

Quoted 13 lines

It will almost eat up
all MPU regions based on current implementation, when launching two guests in platform.

Let's calculate the most simple scenario:
The following is MPU-related static configuration in device tree:

```
         mpu,boot-module-section = <0x0 0x10000000 0x0 0x10000000>;
         mpu,guest-memory-section = <0x0 0x20000000 0x0 0x40000000>;
         mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;
         mpu,shared-memory-section = <0x0 0x7a000000 0x0 0x02000000>;

         xen,static-heap = <0x0 0x60000000 0x0 0x1a000000>;
```
Quoted 2 lines

At the end of the boot, before reshuffling, the MPU region usage will be as follows:
7 (defined in assembly) + FDT(early_fdt_map) + 5 (at least one region for each "mpu,xxx-memory-section").

Can you list the 7 sections? Is it including the init section?

Quoted 2 lines

That will be already at least 13 MPU regions ;\.

The section I am the most concern of is mpu,device-memory-section because it would likely mean that all the devices will be mapped in Xen. Is there any risk that the guest may use different memory attribute?

On the platform you are describing, what are the devices you are expected to be used by Xen?

Cheers,

Signature · 1 line
Julien Grall
#59 lore raw

RE: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Julien

Quoted 204 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, January 30, 2023 6:00 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 1 line · Penny Zheng

Hi, Julien

Hi Penny,

Quoted 4 lines
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Sunday, January 29, 2023 3:43 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 43 lines
Quoted 43 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 7 lines
Quoted 7 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 7 lines
Quoted 1 line

Hi Peny,

Hi Julien,

Quoted 3 lines · Penny Zheng
Quoted 1 line

In MMU system, we map the UART in the fixmap (when earlyprintk is

used).

Quoted 1 line
Quoted 1 line
Quoted 1 line

However in MPU system, we map the UART with a transient MPU

memory

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
    xen/arch/arm/arm64/head.S               |  2 +-
    xen/arch/arm/arm64/head_mmu.S           |  4 +-
    xen/arch/arm/arm64/head_mpu.S           | 52
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

+++++++++++++++++++++++++

Review

Quoted 4 lines
    xen/arch/arm/include/asm/early_printk.h |  1 +
    4 files changed, 56 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

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

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 15 lines
Quoted 13 lines

index 7f3f973468..a92883319d 100644

xen/arch/arm/arm64/head.S +1 -1
@@ -272,7 +272,7 @@ primary_switched:
             * afterwards.
             */
            bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
    #ifdef CONFIG_EARLY_PRINTK
            /* Use a virtual address to access the UART. */
            ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mmu.S index b59c40495f..a19b7c873d

100644

Quoted 11 lines

xen/arch/arm/arm64/head_mmu.S ENDPROC(remove_identity_mapping) · line 315

Quoted 8 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
     *
     * Clobbers x0 - x3
     */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)

This function is doing more than enable the early UART. It also setups the fixmap even earlyprintk is not configured.

True, true.
I've thoroughly read the MMU implementation of setup_fixmap, and I'll try to split it up.

Quoted 27 lines

I am not entirely sure what could be the name. Maybe this needs to be split further.

Quoted 13 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
    #ifdef CONFIG_EARLY_PRINTK
            /* Add UART to the fixmap table */
            ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
            dsb   nshst
 
            ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
    /* Fail-stop */
    fail:   PRINT("- Boot failed -\r\n")
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mpu.S index e2ac69b0cc..72d1e0863d

100644

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +1 -0
@@ -18,8 +18,10 @@
    #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_DEVICE_PRBAR     0x22    /* SH=10 AP=00 XN=10 */
 
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

xen/arch/arm/arm64/head_mpu.S line 23

Quoted 1 line
xen/arch/arm/arm64/head_mpu.S +0 -0
    #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 9 lines
xen/arch/arm/arm64/head_mpu.S +1 -0
+#define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 
    /*
     * Macro to round up the section address to be PAGE_SIZE

aligned @@
-334,6 +336,56 @@ ENTRY(enable_mm)
ret
ENDPROC(enable_mm)

Quoted 3 lines

+/*
+ * Map the early UART with a new transient MPU memory region.
+ *

Missing "Inputs: "

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

+ * x27: region selector
+ * x28: prbar
+ * x29: prlar
+ *
+ * Clobbers x0 - x4
+ *
+ */

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/arm/arm64/head_mpu.S ENTRY(enable_mm)

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+ENTRY(setup_early_uart)
+#ifdef CONFIG_EARLY_PRINTK
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x3, lr
+
+    /*
+     * MPU region for early UART is a transient region, since it will be
+     * replaced by specific device memory layout when FDT gets

parsed.

Quoted 12 lines
Quoted 12 lines
Quoted 10 lines
Quoted 7 lines

I would rather not mention "FDT" here because this code is independent to the firmware table used.

However, any reason to use a transient region rather than the one that will be used for the UART driver?

We don’t want to define a MPU region for each device driver. It will exhaust MPU regions very quickly.

What the usual size of an MPU?

Quoted 8 lines
Quoted 4 lines

However, even if you don't want to define one for every device, it still seem to be sensible to define a fixed temporary one for the early UART as this would simplify the assembly code.

We will add fixed MPU regions for Xen static heap in function setup_mm. If we put early uart region in front(fixed region place), it will leave holes later after removing it.

Why? The entry could be re-used to map the devices entry.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In commit " [PATCH v2 28/40] xen/mpu: map boot module section in

MPU

Quoted 15 lines
Quoted 15 lines
Quoted 1 line

system",

Did you mean patch #27?

Quoted 3 lines

A new FDT property mpu,device-memory-section will be introduced for users to statically configure the whole system device memory with the

least number of memory regions in Device Tree.

Quoted 2 lines

This section shall cover all devices that will be used in Xen, like UART,

GIC, etc.

Quoted 3 lines

For FVP_BaseR_AEMv8R, we have the following definition:
```
mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>; ```

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

I am a bit worry this will be a recipe for mistake. Do you have an example where the MPU will be exhausted if we reserve some entries for each device (or some)?

Yes, we have internal platform where MPU regions are only 16.

Internal is in silicon (e.g. real) or virtual platform?

Sorry, we met this kind of type platform is all I'm allowed to say. Due to NDA, I couldn’t tell more.

Quoted 12 lines
Quoted 2 lines

It will almost eat up
all MPU regions based on current implementation, when launching two

guests in platform.

Quoted 9 lines

Let's calculate the most simple scenario:
The following is MPU-related static configuration in device tree:
```

         mpu,boot-module-section = <0x0 0x10000000 0x0 0x10000000>;
         mpu,guest-memory-section = <0x0 0x20000000 0x0 0x40000000>;
         mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;
         mpu,shared-memory-section = <0x0 0x7a000000 0x0 0x02000000>;
Quoted 8 lines
Quoted 2 lines
         xen,static-heap = <0x0 0x60000000 0x0 0x1a000000>; ``` At the

end of the boot, before reshuffling, the MPU region usage will be as

follows:

Quoted 1 line

7 (defined in assembly) + FDT(early_fdt_map) + 5 (at least one region for

each "mpu,xxx-memory-section").

Can you list the 7 sections? Is it including the init section?

Yes, I'll draw the layout for you:
'''
Xen MPU Map before reorg:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
......
xen_mpumap[max_xen_mpumap - 7]: Static shared memory section xen_mpumap[max_xen_mpumap - 6]: Boot Module memory section(kernel, initramfs, etc) xen_mpumap[max_xen_mpumap - 5]: Device memory section xen_mpumap[max_xen_mpumap - 4]: Guest memory section xen_mpumap[max_xen_mpumap - 3]: Early FDT
xen_mpumap[max_xen_mpumap - 2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

In the end of boot, function init_done will do the reorg and boot-only region clean-up:

Xen MPU Map after reorg(idle vcpu):

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
xen_mpumap[6] : Guest memory section
xen_mpumap[7] : Device memory section
xen_mpumap[6] : Static shared memory section

Xen MPU Map on runtime(guest vcpu):

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
xen_mpumap[6] : Guest memory
xen_mpumap[7] : vGIC map
xen_mpumap[8] : vPL011 map
xen_mpumap[9] : Passthrough device map(UART, etc)
xen_mpumap[10] : Static shared memory section

Quoted 7 lines
Quoted 2 lines

That will be already at least 13 MPU regions ;\.

The section I am the most concern of is mpu,device-memory-section because it would likely mean that all the devices will be mapped in Xen. Is there any risk that the guest may use different memory attribute?

Yes, on current implementation, per-domain vgic, vpl011, and passthrough device map will be individually added into per-domain P2M mapping, then when switching into guest vcpu from xen idle vcpu, device memory section will be replaced by vgic, vpl011, passthrough device map.

Quoted 7 lines

On the platform you are describing, what are the devices you are expected to be used by Xen?

Cheers,

--
Julien Grall

#60 lore raw

Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 231 lines · Penny Zheng
Quoted 204 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, January 30, 2023 6:00 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 1 line · Penny Zheng

Hi, Julien

Hi Penny,

Quoted 4 lines
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Sunday, January 29, 2023 3:43 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 43 lines
Quoted 43 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 7 lines
Quoted 7 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 7 lines
Quoted 1 line

Hi Peny,

Hi Julien,

Quoted 3 lines · Penny Zheng
Quoted 1 line

In MMU system, we map the UART in the fixmap (when earlyprintk is

used).

Quoted 1 line
Quoted 1 line
Quoted 1 line

However in MPU system, we map the UART with a transient MPU

memory

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
     xen/arch/arm/arm64/head.S               |  2 +-
     xen/arch/arm/arm64/head_mmu.S           |  4 +-
     xen/arch/arm/arm64/head_mpu.S           | 52
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

+++++++++++++++++++++++++

Review

Quoted 4 lines
     xen/arch/arm/include/asm/early_printk.h |  1 +
     4 files changed, 56 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

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

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 15 lines
Quoted 13 lines

index 7f3f973468..a92883319d 100644

xen/arch/arm/arm64/head.S +1 -1
@@ -272,7 +272,7 @@ primary_switched:
              * afterwards.
              */
             bl    remove_identity_mapping
-        bl    setup_fixmap
+        bl    setup_early_uart
     #ifdef CONFIG_EARLY_PRINTK
             /* Use a virtual address to access the UART. */
             ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mmu.S index b59c40495f..a19b7c873d

100644

Quoted 11 lines

xen/arch/arm/arm64/head_mmu.S ENDPROC(remove_identity_mapping) · line 315

Quoted 8 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
@@ -312,7 +312,7 @@ ENDPROC(remove_identity_mapping)
      *
      * Clobbers x0 - x3
      */
-ENTRY(setup_fixmap)
+ENTRY(setup_early_uart)

This function is doing more than enable the early UART. It also setups the fixmap even earlyprintk is not configured.

True, true.
I've thoroughly read the MMU implementation of setup_fixmap, and I'll try to split it up.

Quoted 27 lines

I am not entirely sure what could be the name. Maybe this needs to be split further.

Quoted 13 lines
xen/arch/arm/arm64/head_mmu.S +1 -1
     #ifdef CONFIG_EARLY_PRINTK
             /* Add UART to the fixmap table */
             ldr   x0, =EARLY_UART_VIRTUAL_ADDRESS
@@ -325,7 +325,7 @@ ENTRY(setup_fixmap)
             dsb   nshst
 
             ret
-ENDPROC(setup_fixmap)
+ENDPROC(setup_early_uart)
 
     /* Fail-stop */
     fail:   PRINT("- Boot failed -\r\n")
(quoted diff) +0 -0
Quoted 1 line

b/xen/arch/arm/arm64/head_mpu.S index e2ac69b0cc..72d1e0863d

100644

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +1 -0
@@ -18,8 +18,10 @@
     #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_DEVICE_PRBAR     0x22    /* SH=10 AP=00 XN=10 */
 
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

xen/arch/arm/arm64/head_mpu.S line 23

Quoted 1 line
xen/arch/arm/arm64/head_mpu.S +0 -0
     #define REGION_NORMAL_PRLAR     0x0f    /* NS=0 ATTR=111 EN=1

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 9 lines
xen/arch/arm/arm64/head_mpu.S +1 -0
+#define REGION_DEVICE_PRLAR     0x09    /* NS=0 ATTR=100 EN=1 */
 
     /*
      * Macro to round up the section address to be PAGE_SIZE

aligned @@
-334,6 +336,56 @@ ENTRY(enable_mm)
ret
ENDPROC(enable_mm)

Quoted 3 lines

+/*
+ * Map the early UART with a new transient MPU memory region.
+ *

Missing "Inputs: "

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

+ * x27: region selector
+ * x28: prbar
+ * x29: prlar
+ *
+ * Clobbers x0 - x4
+ *
+ */

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/arm/arm64/head_mpu.S

Quoted 8 lines
xen/arch/arm/arm64/head_mpu.S +8 -0
+ENTRY(setup_early_uart)
+#ifdef CONFIG_EARLY_PRINTK
+    /* stack LR as write_pr will be called later like nested function */
+    mov   x3, lr
+
+    /*
+     * MPU region for early UART is a transient region, since it will be
+     * replaced by specific device memory layout when FDT gets

parsed.

Quoted 12 lines
Quoted 12 lines
Quoted 10 lines
Quoted 7 lines

I would rather not mention "FDT" here because this code is independent to the firmware table used.

However, any reason to use a transient region rather than the one that will be used for the UART driver?

We don’t want to define a MPU region for each device driver. It will exhaust MPU regions very quickly.

What the usual size of an MPU?

Quoted 8 lines
Quoted 4 lines

However, even if you don't want to define one for every device, it still seem to be sensible to define a fixed temporary one for the early UART as this would simplify the assembly code.

We will add fixed MPU regions for Xen static heap in function setup_mm. If we put early uart region in front(fixed region place), it will leave holes later after removing it.

Why? The entry could be re-used to map the devices entry.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In commit " [PATCH v2 28/40] xen/mpu: map boot module section in

MPU

Quoted 15 lines
Quoted 15 lines
Quoted 1 line

system",

Did you mean patch #27?

Quoted 3 lines

A new FDT property mpu,device-memory-section will be introduced for users to statically configure the whole system device memory with the

least number of memory regions in Device Tree.

Quoted 2 lines

This section shall cover all devices that will be used in Xen, like UART,

GIC, etc.

Quoted 3 lines

For FVP_BaseR_AEMv8R, we have the following definition:
```
mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>; ```

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

I am a bit worry this will be a recipe for mistake. Do you have an example where the MPU will be exhausted if we reserve some entries for each device (or some)?

Yes, we have internal platform where MPU regions are only 16.

Internal is in silicon (e.g. real) or virtual platform?

Sorry, we met this kind of type platform is all I'm allowed to say. Due to NDA, I couldn’t tell more.

Quoted 14 lines
Quoted 2 lines

It will almost eat up
all MPU regions based on current implementation, when launching two

guests in platform.

Quoted 11 lines

Let's calculate the most simple scenario:
The following is MPU-related static configuration in device tree:
```

          mpu,boot-module-section = <0x0 0x10000000 0x0 0x10000000>;
          mpu,guest-memory-section = <0x0 0x20000000 0x0 0x40000000>;
          mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;
          mpu,shared-memory-section = <0x0 0x7a000000 0x0 0x02000000>;

          xen,static-heap = <0x0 0x60000000 0x0 0x1a000000>; ``` At the

end of the boot, before reshuffling, the MPU region usage will be as

Quoted 8 lines
Quoted 6 lines

follows:

Quoted 1 line

7 (defined in assembly) + FDT(early_fdt_map) + 5 (at least one region for

each "mpu,xxx-memory-section").

Can you list the 7 sections? Is it including the init section?

Yes, I'll draw the layout for you:

Thanks!

Quoted 18 lines

'''
Xen MPU Map before reorg:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
......
xen_mpumap[max_xen_mpumap - 7]: Static shared memory section xen_mpumap[max_xen_mpumap - 6]: Boot Module memory section(kernel, initramfs, etc) xen_mpumap[max_xen_mpumap - 5]: Device memory section xen_mpumap[max_xen_mpumap - 4]: Guest memory section xen_mpumap[max_xen_mpumap - 3]: Early FDT
xen_mpumap[max_xen_mpumap - 2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

Quoted 7 lines

In the end of boot, function init_done will do the reorg and boot-only region clean-up:

Xen MPU Map after reorg(idle vcpu):

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data

In theory 1 and 2 could be merged after boot. But I guess it might be complicated?

Quoted 4 lines

xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
xen_mpumap[6] : Guest memory section

Why do you need to map the "Guest memory section" for the idle vCPU?

Quoted 1 line

xen_mpumap[7] : Device memory section

I might be missing some context here. But why this section is not also mapped in the context of the guest vCPU?

For instance, how would you write to the serial console when the context is the guest vCPU?

Quoted 4 lines

xen_mpumap[6] : Static shared memory section

Xen MPU Map on runtime(guest vcpu):

Quoted 9 lines

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
xen_mpumap[6] : Guest memory
xen_mpumap[7] : vGIC map
xen_mpumap[8] : vPL011 map

I was expected the PL011 to be fully emulated. So why is this necessary?

Quoted 11 lines

xen_mpumap[9] : Passthrough device map(UART, etc)
xen_mpumap[10] : Static shared memory section

Quoted 7 lines
Quoted 2 lines

That will be already at least 13 MPU regions ;\.

The section I am the most concern of is mpu,device-memory-section because it would likely mean that all the devices will be mapped in Xen. Is there any risk that the guest may use different memory attribute?

Quoted 4 lines

Yes, on current implementation, per-domain vgic, vpl011, and passthrough device map will be individually added into per-domain P2M mapping, then when switching into guest vcpu from xen idle vcpu, device memory section will be replaced by vgic, vpl011, passthrough device map.

Per above, I am not entirely sure how you could remove the device memory section when using the guest vCPU.

Now about the layout between init and runtime. From previous discussion, you said you didn't want to have init section to be fixed because of the section "Xen static heap".

Furthermore, you also mention that you didn't want a bitmap. So how about the following for the assembly part:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5]: Early FDT
xen_mpumap[6]: Xen init data
xen_mpumap[7]: Xen init text
xen_mpumap[8]: Early UART (optional)

Then when you switch to C, you could have:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5]: Early FDT
xen_mpumap[6]: Xen init data
xen_mpumap[7]: Xen init text

xen_mpumap[max_xen_mpumap - 4]: Device memory section xen_mpumap[max_xen_mpumap - 3]: Guest memory section xen_mpumap[max_xen_mpumap - 2]: Static shared memory section xen_mpumap[max_xen_mpumap - 1] : Xen static heap

And at runtime, you would keep the "Xen static heap" right at the end of the MPU and keep the middle entries as the switchable one.

There would be not bitmap with this solution and all the entries for the assembly code would be fixed.

Cheers,

Signature · 1 line
Julien Grall
#61 lore raw

RE: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Julien

Quoted 110 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Tuesday, January 31, 2023 5:42 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, January 30, 2023 6:00 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 63 lines
Quoted 63 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 1 line · Penny Zheng

Hi, Julien

Hi Penny,

Quoted 4 lines
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Sunday, January 29, 2023 3:43 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 44 lines
Quoted 44 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 7 lines
Quoted 7 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 8 lines
Quoted 1 line

Hi Peny,

Hi Julien,

Quoted 4 lines · Penny Zheng
Quoted 2 lines

In MMU system, we map the UART in the fixmap (when earlyprintk is

used).

Quoted 1 line
Quoted 1 line
Quoted 1 line

However in MPU system, we map the UART with a transient MPU

memory

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
     xen/arch/arm/arm64/head.S               |  2 +-
     xen/arch/arm/arm64/head_mmu.S           |  4 +-
     xen/arch/arm/arm64/head_mpu.S           | 52
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+++++++++++++++++++++++++

Quoted 3 lines
     xen/arch/arm/include/asm/early_printk.h |  1 +
     4 files changed, 56 insertions(+), 3 deletions(-)

Yes, I'll draw the layout for you:

Thanks!

Quoted 10 lines

'''
Xen MPU Map before reorg:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS xen_mpumap[5] : Xen static heap ......
xen_mpumap[max_xen_mpumap - 7]: Static shared memory section xen_mpumap[max_xen_mpumap - 6]: Boot Module memory

section(kernel,

Quoted 1 line

initramfs, etc) xen_mpumap[max_xen_mpumap - 5]: Device memory

section

Quoted 2 lines

xen_mpumap[max_xen_mpumap - 4]: Guest memory section xen_mpumap[max_xen_mpumap - 3]: Early FDT

xen_mpumap[max_xen_mpumap -

Quoted 3 lines

2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

In the end of boot, function init_done will do the reorg and boot-only

region clean-up:

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

Xen MPU Map after reorg(idle vcpu):

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data

In theory 1 and 2 could be merged after boot. But I guess it might be complicated?

In theory, if in C merging codes, we do not use any read-only data or read-only-after-init data, then, ig, it will be ok.
Since, In MPU system, when we implement C merging codes, we need to disable region 1 and 2 firstly, and enable the merged region after. The reason is that two MPU regions with address overlapping is not allowed when MPU on.

Quoted 7 lines
Quoted 4 lines

xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
xen_mpumap[6] : Guest memory section

Why do you need to map the "Guest memory section" for the idle vCPU?

Hmmm, "Guest memory section" here refers to *ALL* guest RAM address range with only EL2 read/write access.

For guest vcpu, this section will be replaced by guest itself own RAM with both EL1/EL2 access.

Quoted 8 lines
Quoted 1 line

xen_mpumap[7] : Device memory section

I might be missing some context here. But why this section is not also mapped in the context of the guest vCPU?

For instance, how would you write to the serial console when the context is the guest vCPU?

I think, as Xen itself, it shall have access to all system device memory on EL2. Ik, it is not accurate in current MMU implementation, only devices with supported driver will get ioremap.

But like we discussed before, if following the same strategy as MMU does, with limited
MPU regions, we could not afford mapping a MPU region for each device.
For example, On FVPv8R model, we have four uarts, and a GICv3. At most, we may provide
four MPU regions for uarts, and two MPU regions for Distributor and one Redistributor region. So, I thought up this new device tree property “mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;“ to roughly map all system device memory for Xen itself.

For guest, it shall only see vgic, vpl011, and its own passthrough device. And here, to maintain safe and isolation, we will be mapping a MPU region for each device for guest vcpu.
For example, vgic and vpl011 are emulated and direct-map in MPU. Relevant device mapping(GFN == MFN with only EL2 access)will be added to its *P2M mapping table*, in vgic_v3_domain_init [1].

Later, on vcpu context switching, when switching from idle vcpu, device memory section gets disabled and switched out in ctxt_switch_from [2], later when switching into guest vcpu, vgic and vpl011 device mapping will be switched in along with the whole P2M mapping table [3].

Words might be ambiguous, but all related code implementation is on MPU patch serie part II - guest initialization, you may have to check the gitlab link:
[1] https://gitlab.com/xen-project/people/weic/xen/-/commit/a51d5b25eb17a50a36b27987a2f48e14793ac585
[2] https://gitlab.com/xen-project/people/weic/xen/-/commit/c6a069d777d9407aeda42b7e5b08a086a1c15976
[3] https://gitlab.com/xen-project/people/weic/xen/-/commit/d8c6408b6eef1190d75c9bd4e58557d34fc8b4df

Quoted 64 lines
Quoted 10 lines

xen_mpumap[6] : Static shared memory section

Xen MPU Map on runtime(guest vcpu):

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS xen_mpumap[5] : Xen static heap xen_mpumap[6] : Guest memory xen_mpumap[7] : vGIC map xen_mpumap[8] : vPL011 map

I was expected the PL011 to be fully emulated. So why is this necessary?

Quoted 11 lines

xen_mpumap[9] : Passthrough device map(UART, etc) xen_mpumap[10] :
Static shared memory section

Quoted 7 lines
Quoted 2 lines

That will be already at least 13 MPU regions ;\.

The section I am the most concern of is mpu,device-memory-section because it would likely mean that all the devices will be mapped in Xen. Is there any risk that the guest may use different memory attribute?

Quoted 4 lines

Yes, on current implementation, per-domain vgic, vpl011, and passthrough device map will be individually added into per-domain P2M mapping, then when switching into guest vcpu from xen idle vcpu, device memory section will be replaced by vgic, vpl011, passthrough

device map.

Per above, I am not entirely sure how you could remove the device memory section when using the guest vCPU.

Now about the layout between init and runtime. From previous discussion, you said you didn't want to have init section to be fixed because of the section "Xen static heap".

Furthermore, you also mention that you didn't want a bitmap. So how about the following for the assembly part:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read- write data xen_mpumap[4] : Xen BSS
xen_mpumap[5]: Early FDT
xen_mpumap[6]: Xen init data
xen_mpumap[7]: Xen init text
xen_mpumap[8]: Early UART (optional)

Then when you switch to C, you could have:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read- write data xen_mpumap[4] : Xen BSS
xen_mpumap[5]: Early FDT
xen_mpumap[6]: Xen init data
xen_mpumap[7]: Xen init text

xen_mpumap[max_xen_mpumap - 4]: Device memory section xen_mpumap[max_xen_mpumap - 3]: Guest memory section xen_mpumap[max_xen_mpumap - 2]: Static shared memory section xen_mpumap[max_xen_mpumap - 1] : Xen static heap

Quoted 6 lines

And at runtime, you would keep the "Xen static heap" right at the end of the MPU and keep the middle entries as the switchable one.

There would be not bitmap with this solution and all the entries for the assembly code would be fixed.

That's really smart! Thanks!

I've done a little twist on your design: I've put all switchable ones in the middle after regions defined in assembly. If near Xen heap region, I'm afraid if one day in the future, someone is introducing another fixed region in C, we will leave holes there.

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
( Fixed MPU region defined in assembly )
--------------------------------------------------------------------------
xen_mpumap[5]: Xen init data
xen_mpumap[6]: Xen init text
xen_mpumap[7]: Early FDT
xen_mpumap[8]: Guest memory section
xen_mpumap[9]: Device memory section
xen_mpumap[10]: Static shared memory section
( boot-only and switching regions defined in C )
--------------------------------------------------------------------------
...
xen_mpumap[max_xen_mpumap - 1] : Xen static heap
( Fixed MPU region defined in C )
--------------------------------------------------------------------------

After re-org:
xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
( Fixed MPU region defined in assembly )
--------------------------------------------------------------------------
xen_mpumap[8]: Guest memory section
xen_mpumap[9]: Device memory section
xen_mpumap[10]: Static shared memory section
( Switching region )
--------------------------------------------------------------------------
...
xen_mpumap[max_xen_mpumap - 1] : Xen static heap
( Fixed MPU region defined in C )

If you're fine with it, then next serie, I'll use this layout, to keep both simple assembly and re-org process.

Quoted 4 lines

Cheers,

--
Julien Grall

Cheers,

--
Penny Zheng

#62 lore raw

Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 1 line · Penny Zheng

Hi Julien

Hi Penny,

Quoted 121 lines
Quoted 111 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Tuesday, January 31, 2023 5:42 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, January 30, 2023 6:00 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 63 lines
Quoted 63 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 1 line · Penny Zheng

Hi, Julien

Hi Penny,

Quoted 4 lines
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Sunday, January 29, 2023 3:43 PM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 44 lines
Quoted 44 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Wednesday, January 25, 2023 3:09 AM
To: Penny Zheng <[email protected]>; xen-

[email protected]

Quoted 7 lines
Quoted 7 lines

Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 8 lines
Quoted 1 line

Hi Peny,

Hi Julien,

Quoted 4 lines · Penny Zheng
Quoted 2 lines

In MMU system, we map the UART in the fixmap (when earlyprintk is

used).

Quoted 1 line
Quoted 1 line
Quoted 1 line

However in MPU system, we map the UART with a transient MPU

memory

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

region.

So we introduce a new unified function setup_early_uart to replace the previous setup_fixmap.

---
      xen/arch/arm/arm64/head.S               |  2 +-
      xen/arch/arm/arm64/head_mmu.S           |  4 +-
      xen/arch/arm/arm64/head_mpu.S           | 52
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+++++++++++++++++++++++++

Quoted 3 lines
      xen/arch/arm/include/asm/early_printk.h |  1 +
      4 files changed, 56 insertions(+), 3 deletions(-)

Yes, I'll draw the layout for you:

Thanks!

Quoted 10 lines

'''
Xen MPU Map before reorg:

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS xen_mpumap[5] : Xen static heap ......
xen_mpumap[max_xen_mpumap - 7]: Static shared memory section xen_mpumap[max_xen_mpumap - 6]: Boot Module memory

section(kernel,

Quoted 1 line

initramfs, etc) xen_mpumap[max_xen_mpumap - 5]: Device memory

section

Quoted 2 lines

xen_mpumap[max_xen_mpumap - 4]: Guest memory section xen_mpumap[max_xen_mpumap - 3]: Early FDT

xen_mpumap[max_xen_mpumap -

Quoted 3 lines

2]: Xen init data xen_mpumap[max_xen_mpumap - 1]: Xen init text

In the end of boot, function init_done will do the reorg and boot-only

region clean-up:

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

Xen MPU Map after reorg(idle vcpu):

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data

In theory 1 and 2 could be merged after boot. But I guess it might be complicated?

Quoted 5 lines

In theory, if in C merging codes, we do not use any read-only data or read-only-after-init data, then, ig, it will be ok.
Since, In MPU system, when we implement C merging codes, we need to disable region 1 and 2 firstly, and enable the merged region after. The reason is that two MPU regions with address overlapping is not allowed when MPU on.

Good to know! I think it should be feasible to avoid accessing read-only variable while doing the merge.

Anyway, this looks more like a potential optimization for the future.

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
xen_mpumap[5] : Xen static heap
xen_mpumap[6] : Guest memory section

Why do you need to map the "Guest memory section" for the idle vCPU?

Hmmm, "Guest memory section" here refers to *ALL* guest RAM address range with only EL2 read/write access.

For what purpose? Earlier, you said you had a setup with a limited number of MPU entries. So it may not be possible to map all the guests RAM.

Xen should only need to access the guest memory in hypercalls and scrubbing. In both cases you could map/unmap on demand.

Quoted 9 lines

For guest vcpu, this section will be replaced by guest itself own RAM with both EL1/EL2 access.

Quoted 5 lines
Quoted 1 line

xen_mpumap[7] : Device memory section

I might be missing some context here. But why this section is not also mapped in the context of the guest vCPU?

Quoted 7 lines
Quoted 3 lines

For instance, how would you write to the serial console when the context is the guest vCPU?

I think, as Xen itself, it shall have access to all system device memory on EL2. Ik, it is not accurate in current MMU implementation, only devices with supported driver will get ioremap.

So in the MMU case, we are not mapping all the devices in Xen because we don't exactly know which memory attributes will be used by the guest.

If we are using different attributes, then we are risking to break coherency. Could the same issue happen with the MPU?

If so, then you should not mapped those regions in Xen.

Quoted 7 lines

But like we discussed before, if following the same strategy as MMU does, with limited
MPU regions, we could not afford mapping a MPU region for each device.
For example, On FVPv8R model, we have four uarts, and a GICv3. At most, we may provide
four MPU regions for uarts, and two MPU regions for Distributor and one Redistributor region. So, I thought up this new device tree property “mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;“ to roughly map all system device memory for Xen itself.

Why do you say "roughly"? Is it possible that you have non-device region in the range?

Quoted 4 lines

For guest, it shall only see vgic, vpl011, and its own passthrough device. And here, to maintain safe and isolation, we will be mapping a MPU region for each device for guest vcpu.
For example, vgic and vpl011 are emulated and direct-map in MPU. Relevant device

I am confused. If the vGIC/vPL011 is emulated then why do you need to map it in the MPU? IOW, wouldn't you receive a fault in the hypervisor if the guest is trying to access a region not present in the MPU?

Quoted 11 lines

mapping(GFN == MFN with only EL2 access)will be added to its *P2M mapping table*, in vgic_v3_domain_init [1].

Later, on vcpu context switching, when switching from idle vcpu, device memory section gets disabled and switched out in ctxt_switch_from [2], later when switching into guest vcpu, vgic and vpl011 device mapping will be switched in along with the whole P2M mapping table [3].

Words might be ambiguous, but all related code implementation is on MPU patch serie part II - guest initialization, you may have to check the gitlab link:
[1] https://gitlab.com/xen-project/people/weic/xen/-/commit/a51d5b25eb17a50a36b27987a2f48e14793ac585
[2] https://gitlab.com/xen-project/people/weic/xen/-/commit/c6a069d777d9407aeda42b7e5b08a086a1c15976
[3] https://gitlab.com/xen-project/people/weic/xen/-/commit/d8c6408b6eef1190d75c9bd4e58557d34fc8b4df

I have looked at the code and this doesn't entirely answer my question. So let me provide an example.

Xen can print to the serial console at any time. So Xen should be able to access the physical UART even when it has context switched to the guest vCPU.

But above you said that the physical device would not be accessible and instead you map the virtual UART. So how Xen is supported to access the physical UART?

Or by vpl011 did you actually mean the physical UART? If so, then if you map the device one by one in the MPU context, then it would likely mean to have space to map them one by one in the idle context.

Quoted 37 lines

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
( Fixed MPU region defined in assembly )
--------------------------------------------------------------------------
xen_mpumap[5]: Xen init data
xen_mpumap[6]: Xen init text
xen_mpumap[7]: Early FDT
xen_mpumap[8]: Guest memory section
xen_mpumap[9]: Device memory section
xen_mpumap[10]: Static shared memory section
( boot-only and switching regions defined in C )
--------------------------------------------------------------------------
...
xen_mpumap[max_xen_mpumap - 1] : Xen static heap
( Fixed MPU region defined in C )
--------------------------------------------------------------------------

After re-org:
xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data
xen_mpumap[3] : Xen read-write data
xen_mpumap[4] : Xen BSS
( Fixed MPU region defined in assembly )
--------------------------------------------------------------------------
xen_mpumap[8]: Guest memory section
xen_mpumap[9]: Device memory section
xen_mpumap[10]: Static shared memory section
( Switching region )
--------------------------------------------------------------------------
...
xen_mpumap[max_xen_mpumap - 1] : Xen static heap
( Fixed MPU region defined in C )

Quoted 2 lines

If you're fine with it, then next serie, I'll use this layout, to keep both simple assembly and re-org process.

I am ok in principle with the layout you propose. My main requirement is that the region used in assembly are fixed.

Cheers,

Signature · 1 line
Julien Grall
#63 lore raw

RE: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 12 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, February 2, 2023 3:27 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 5 lines
Quoted 1 line · Penny Zheng

Hi Julien

Hi Penny,

Hi Julien,

Quoted 1 line
Quoted 1 line

[...]

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

xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS xen_mpumap[5] : Xen static heap xen_mpumap[6] : Guest memory section

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Why do you need to map the "Guest memory section" for the idle vCPU?

Hmmm, "Guest memory section" here refers to *ALL* guest RAM address

range with only EL2 read/write access.

For what purpose? Earlier, you said you had a setup with a limited number of MPU entries. So it may not be possible to map all the guests RAM.

The "Guest memory section" I referred here is the memory section defined in my new introducing device tree property, "mpu,guest-memory-section = <...>". It will include "ALL" guest memory.

Let me find an example to illustrate why I introduced it and how it shall work:
In MPU system, all guest RAM *MUST* be statically configured through "xen,static-mem" under domain node. We found that with more and more guests in, the scattering of "xen,static-mem" may exhaust MPU regions very quickly. TBH, at that time, I didn't figure out that I could map/unmap on demand. And in MMU system, We will never encounter this issue, setup_directmap_mappings will map the whole system RAM to the directmap area for Xen to access in EL2.
So instead, "mpu,guest-memory-section" is introduced to limit the scattering and map in advance, we enforce users to ensure all guest RAM(through "xen,static-mem") must be included within "mpu,guest-memory-section". e.g.
mpu,guest-memory-section = <0x0 0x20000000 0x0 0x40000000>;
DomU1:
xen,static-mem = <0x0 0x40000000 0x0 0x20000000>;
DomU2:
xen,static-mem = <0x0 0x20000000 0x0 0x20000000>;

Quoted 3 lines

Xen should only need to access the guest memory in hypercalls and scrubbing. In both cases you could map/unmap on demand.

thanks for the explanation.
In my understanding, during boot-up, there are two spots where Xen may touch the guest memory:
one is that doing synchronous scrubbing in function unprepare_staticmem_pages.
Another is coping and pasting kernel image to guest memory.
In both cases, we could map/unmap on demand.
And if you think map/unmap on demand is better than "mpu,guest-memory-section", I'll try to fix it in next serie

Quoted 73 lines
Quoted 2 lines

For guest vcpu, this section will be replaced by guest itself own RAM with

both EL1/EL2 access.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

xen_mpumap[7] : Device memory section

Quoted 8 lines
Quoted 6 lines

I might be missing some context here. But why this section is not also mapped in the context of the guest vCPU?

For instance, how would you write to the serial console when the context is the guest vCPU?

I think, as Xen itself, it shall have access to all system device memory on

EL2.

Quoted 2 lines

Ik, it is not accurate in current MMU implementation, only devices with supported driver will get ioremap.

So in the MMU case, we are not mapping all the devices in Xen because we don't exactly know which memory attributes will be used by the guest.

If we are using different attributes, then we are risking to break coherency. Could the same issue happen with the MPU?

If so, then you should not mapped those regions in Xen.

Quoted 3 lines

But like we discussed before, if following the same strategy as MMU does, with limited MPU regions, we could not afford mapping a MPU

region for each device.

Quoted 2 lines

For example, On FVPv8R model, we have four uarts, and a GICv3. At most, we may provide four MPU regions for uarts, and two MPU regions

for Distributor and one Redistributor region.

Quoted 2 lines

So, I thought up this new device tree property “mpu,device-memory-section = <0x0 0x80000000 0x0 0x7ffff000>;“ to

roughly map all system device memory for Xen itself.

Why do you say "roughly"? Is it possible that you have non-device region in the range?

Quoted 3 lines

For guest, it shall only see vgic, vpl011, and its own passthrough device. And here, to maintain safe and isolation, we will be mapping a

MPU region for each device for guest vcpu.

Quoted 2 lines

For example, vgic and vpl011 are emulated and direct-map in MPU. Relevant device

I am confused. If the vGIC/vPL011 is emulated then why do you need to map it in the MPU? IOW, wouldn't you receive a fault in the hypervisor if the guest is trying to access a region not present in the MPU?

Quoted 1 line

mapping(GFN == MFN with only EL2 access)will be added to its *P2M

mapping table*, in vgic_v3_domain_init [1].

Quoted 4 lines

Later, on vcpu context switching, when switching from idle vcpu, device memory section gets disabled and switched out in ctxt_switch_from [2], later when switching into guest vcpu, vgic and

vpl011 device mapping will be switched in along with the whole P2M mapping table [3].

Quoted 3 lines

Words might be ambiguous, but all related code implementation is on MPU patch serie part II - guest initialization, you may have to check the

gitlab link:

Quoted 2 lines

[1]
https://gitlab.com/xen-project/people/weic/xen/-

/commit/a51d5b25eb17a5

Quoted 2 lines

0a36b27987a2f48e14793ac585 [2]
https://gitlab.com/xen-project/people/weic/xen/-

/commit/c6a069d777d940

Quoted 2 lines

7aeda42b7e5b08a086a1c15976 [3]
https://gitlab.com/xen-project/people/weic/xen/-

/commit/d8c6408b6eef11

Quoted 9 lines
Quoted 1 line

90d75c9bd4e58557d34fc8b4df

I have looked at the code and this doesn't entirely answer my question. So let me provide an example.

Xen can print to the serial console at any time. So Xen should be able to access the physical UART even when it has context switched to the guest vCPU.

I understand your concern on "device memory section" with your Example here. True, the current implementation is buggy.

Yes, if vpl011 is not enabled in guest and we instead passthrough a UART to guest, in current design, Xen is not able to access the physical UART on guest mode.

All guests in MPU are direct-map, So like you said after, the mapping for vpl011 on guest mode is the same with the physical UART. And this hides the problem, to let Xen being able to access to the physical UART.

I'll drop the design on "device memory section", and let device driver map on demand in boot-time.

Quoted 50 lines

But above you said that the physical device would not be accessible and instead you map the virtual UART. So how Xen is supported to access the physical UART?

Or by vpl011 did you actually mean the physical UART? If so, then if you map the device one by one in the MPU context, then it would likely mean to have space to map them one by one in the idle context.

Quoted 39 lines

xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS ( Fixed MPU region defined in assembly )
----------------------------------------------------------------------
----
xen_mpumap[5]: Xen init data
xen_mpumap[6]: Xen init text
xen_mpumap[7]: Early FDT
xen_mpumap[8]: Guest memory section
xen_mpumap[9]: Device memory section
xen_mpumap[10]: Static shared memory section ( boot-only and switching regions defined in C )
----------------------------------------------------------------------
----
...
xen_mpumap[max_xen_mpumap - 1] : Xen static heap ( Fixed MPU region defined in C )
----------------------------------------------------------------------
----

After re-org:
xen_mpumap[0] : Xen text
xen_mpumap[1] : Xen read-only data
xen_mpumap[2] : Xen read-only after init data xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS ( Fixed MPU region defined in assembly )
----------------------------------------------------------------------
----
xen_mpumap[8]: Guest memory section
xen_mpumap[9]: Device memory section
xen_mpumap[10]: Static shared memory section ( Switching region )
----------------------------------------------------------------------
----
...
xen_mpumap[max_xen_mpumap - 1] : Xen static heap ( Fixed MPU region defined in C )

Quoted 2 lines

If you're fine with it, then next serie, I'll use this layout, to keep both simple assembly and re-org process.

Quoted 7 lines

I am ok in principle with the layout you propose. My main requirement is that the region used in assembly are fixed.

Cheers,

--
Julien Grall

Cheers,

--
Penny Zheng

#64 lore raw

Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Hi Penny,

Quoted 60 lines · Penny Zheng
Quoted 12 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Thursday, February 2, 2023 3:27 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 13/40] xen/mpu: introduce unified function setup_early_uart to map early UART

Quoted 5 lines
Quoted 1 line · Penny Zheng

Hi Julien

Hi Penny,

Hi Julien,

Quoted 1 line
Quoted 1 line

[...]

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

xen_mpumap[3] : Xen read-write data xen_mpumap[4] : Xen BSS xen_mpumap[5] : Xen static heap xen_mpumap[6] : Guest memory section

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Why do you need to map the "Guest memory section" for the idle vCPU?

Hmmm, "Guest memory section" here refers to *ALL* guest RAM address

range with only EL2 read/write access.

For what purpose? Earlier, you said you had a setup with a limited number of MPU entries. So it may not be possible to map all the guests RAM.

The "Guest memory section" I referred here is the memory section defined in my new introducing device tree property, "mpu,guest-memory-section = <...>". It will include "ALL" guest memory.

Let me find an example to illustrate why I introduced it and how it shall work:
In MPU system, all guest RAM *MUST* be statically configured through "xen,static-mem" under domain node. We found that with more and more guests in, the scattering of "xen,static-mem" may exhaust MPU regions very quickly. TBH, at that time, I didn't figure out that I could map/unmap on demand. And in MMU system, We will never encounter this issue, setup_directmap_mappings will map the whole system RAM to the directmap area for Xen to access in EL2.
So instead, "mpu,guest-memory-section" is introduced to limit the scattering and map in advance, we enforce users to ensure all guest RAM(through "xen,static-mem") must be included within "mpu,guest-memory-section". e.g.
mpu,guest-memory-section = <0x0 0x20000000 0x0 0x40000000>;
DomU1:
xen,static-mem = <0x0 0x40000000 0x0 0x20000000>;
DomU2:
xen,static-mem = <0x0 0x20000000 0x0 0x20000000>;

Quoted 3 lines

Xen should only need to access the guest memory in hypercalls and scrubbing. In both cases you could map/unmap on demand.

Quoted 6 lines

thanks for the explanation.
In my understanding, during boot-up, there are two spots where Xen may touch the guest memory:
one is that doing synchronous scrubbing in function unprepare_staticmem_pages.
Another is coping and pasting kernel image to guest memory.
In both cases, we could map/unmap on demand.
And if you think map/unmap on demand is better than "mpu,guest-memory-section", I'll try to fix it in next serie

I think it would be better for a few reasons:
1) You are making the assumption that all the RAM for the guests are
contiguous. This may not be true for various reason (i.e. split bank...).
2) It reduces the amount of work for the integrator
3) You increase the defense in the hypervisor but it is more difficult
to access the guest memory if there is a breakage

I don't expect major rework because you could plug the update to the MPU in map_domain_page().

Quoted 8 lines
Quoted 7 lines

I have looked at the code and this doesn't entirely answer my question. So let me provide an example.

Xen can print to the serial console at any time. So Xen should be able to access the physical UART even when it has context switched to the guest vCPU.

Quoted 5 lines

I understand your concern on "device memory section" with your Example here. True, the current implementation is buggy.

Yes, if vpl011 is not enabled in guest and we instead passthrough a UART to guest, in current design, Xen is not able to access the physical UART on guest mode.

So all the guests are using the same UART?

Cheers,

Signature · 1 line
Julien Grall
#65 lore raw

PATCH v2 14/40 xen/arm64: head: Jump to the runtime mapping in enable_mm()

In reply to Penny Zheng

At the moment, on MMU system, enable_mm() will return to an address in the 1:1 mapping, then each path is responsible to switch to virtual runtime mapping. Then remove_identity_mapping() is called to remove all 1:1 mapping.

Since remove_identity_mapping() is not necessary on MPU system, and we also avoid creating empty function for MPU system, trying to keep only one codeflow in arm64/head.S, we move path switch and remove_identity_mapping() in enable_mm() on MMU system.

---
 xen/arch/arm/arm64/head.S     | 28 +++++++++++++---------------
 xen/arch/arm/arm64/head_mmu.S | 33 ++++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 18 deletions(-)
xen/arch/arm/arm64/head.S +13 -15
index a92883319d..6358305f03 100644
@@ -258,20 +258,15 @@ real_start_efi:
          * and memory regions for MPU systems.
          */
         bl    prepare_early_mappings
+        /*
+         * Address in the runtime mapping to jump to after the
+         * MMU/MPU is enabled
+         */
+        ldr   lr, =primary_switched
         /* Turn on MMU or MPU */
-        bl    enable_mm
+        b    enable_mm
 
-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =primary_switched
-        br    x0
 primary_switched:
-        /*
-         * The 1:1 map may clash with other parts of the Xen virtual memory
-         * layout. As it is not used anymore, remove it completely to
-         * avoid having to worry about replacing existing mapping
-         * afterwards.
-         */
-        bl    remove_identity_mapping
         bl    setup_early_uart
 #ifdef CONFIG_EARLY_PRINTK
         /* Use a virtual address to access the UART. */
@@ -317,11 +312,14 @@ GLOBAL(init_secondary)
         bl    check_cpu_mode
         bl    cpu_init
         bl    prepare_early_mappings
-        bl    enable_mm
 
-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =secondary_switched
-        br    x0
+        /*
+         * Address in the runtime mapping to jump to after the
+         * MMU/MPU is enabled
+         */
+        ldr   lr, =secondary_switched
+        b    enable_mm
+
 secondary_switched:
         /*
          * Non-boot CPUs need to move on to the proper pagetables, which were
xen/arch/arm/arm64/head_mmu.S +30 -3
index a19b7c873d..c9e83bbe2d 100644
@@ -211,9 +211,11 @@ virtphys_clash:
 ENDPROC(prepare_early_mappings)
 
 /*
- * Turn on the Data Cache and the MMU. The function will return on the 1:1
- * mapping. In other word, the caller is responsible to switch to the runtime
- * mapping.
+ * Turn on the Data Cache and the MMU. The function will return
+ * to the virtual address provided in LR (e.g. the runtime mapping).
+ *
+ * Inputs:
+ * lr(x30): Virtual address to return to
  *
  * Clobbers x0 - x3
  */
@@ -238,6 +240,31 @@ ENTRY(enable_mm)
         dsb   sy                     /* Flush PTE writes and finish reads */
         msr   SCTLR_EL2, x0          /* now paging is enabled */
         isb                          /* Now, flush the icache */
+
+        /*
+         * The MMU is turned on and we are in the 1:1 mapping. Switch
+         * to the runtime mapping.
+         */
+        ldr   x0, =1f
+        br    x0
+1:
+        /*
+         * The 1:1 map may clash with other parts of the Xen virtual memory
+         * layout. As it is not used anymore, remove it completely to
+         * avoid having to worry about replacing existing mapping
+         * afterwards.
+         *
+         * On return this will jump to the virtual address requested by
+         * the caller
+         */
+        b     remove_identity_mapping
+
+        /*
+         * Here might not be reached, as "ret" in remove_identity_mapping
+         * will use the return address in LR in advance. But keep ret here
+         * might be more safe if "ret" in remove_identity_mapping is removed
+         * in future.
+         */
         ret
 ENDPROC(enable_mm)
 
Signature · 1 line
2.25.1
#66 lore raw

Re: [PATCH v2 14/40] xen/arm64: head: Jump to the runtime mapping in enable_mm()

Hi Penny,

Quoted 8 lines · Penny Zheng

At the moment, on MMU system, enable_mm() will return to an address in the 1:1 mapping, then each path is responsible to switch to virtual runtime mapping. Then remove_identity_mapping() is called to remove all 1:1 mapping.

Since remove_identity_mapping() is not necessary on MPU system, and we also avoid creating empty function for MPU system, trying to keep only one codeflow in arm64/head.S, we move path switch and remove_identity_mapping() in enable_mm() on MMU system.

AFAICT, remove_identity_mapping() is still using ENTRY(). But you could avoid to introduce ENTRY() if you re-order your series so this patch happens before the MMU specific code is moved in a separate helper.

Quoted 7 lines
---
  xen/arch/arm/arm64/head.S     | 28 +++++++++++++---------------
  xen/arch/arm/arm64/head_mmu.S | 33 ++++++++++++++++++++++++++++++---
  2 files changed, 43 insertions(+), 18 deletions(-)

This will need to be rebased on top of [1] (which will be merged pretty soon). There are two main differences:

1) enable_mmu has an extra parameter to take the root page-tables
2) the remove_identity_mapping should only be called for the boot CPU.

So I think we want to introduce two functions:
1) enable_boot_mmu
2) enable_runtime_mmu

You might need the same for the MPU as I would expect it would be per-CPU.

Cheers,

[1] [email protected]

Signature · 1 line
Julien Grall
#67 lore raw

PATCH v2 15/40 xen/arm: move MMU-specific memory management code to mm_mmu.c/mm_mmu.h

In reply to Penny Zheng

From: Wei Chen <[email protected]>

To make the code readable and maintainable, we move MMU-specific memory management code from mm.c to mm_mmu.c and move MMU-specific definitions from mm.h to mm_mmu.h.
Later we will create mm_mpu.h and mm_mpu.c for MPU-specific memory management code.
This will avoid lots of #ifdef in memory management code and header files.

---
 xen/arch/arm/Makefile             |    5 +
 xen/arch/arm/include/asm/mm.h     |   19 +-
 xen/arch/arm/include/asm/mm_mmu.h |   35 +
 xen/arch/arm/mm.c                 | 1352 +---------------------------
 xen/arch/arm/mm_mmu.c             | 1376 +++++++++++++++++++++++++++++
 xen/arch/arm/mm_mpu.c             |   67 ++
 6 files changed, 1488 insertions(+), 1366 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/mm_mmu.h
 create mode 100644 xen/arch/arm/mm_mmu.c
xen/arch/arm/Makefile +5 -0
index 4d076b278b..21188b207f 100644
@@ -37,6 +37,11 @@ obj-y += kernel.init.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-y += mem_access.o
 obj-y += mm.o
+ifneq ($(CONFIG_HAS_MPU), y)
+obj-y += mm_mmu.o
+else
+obj-y += mm_mpu.o
+endif
 obj-y += monitor.o
 obj-y += p2m.o
 obj-y += percpu.o
xen/arch/arm/include/asm/mm.h +4 -15
index 68adcac9fa..1b9fdb6ff5 100644
@@ -154,13 +154,6 @@ struct page_info
 #define _PGC_need_scrub   _PGC_allocated
 #define PGC_need_scrub    PGC_allocated
 
-extern mfn_t directmap_mfn_start, directmap_mfn_end;
-extern vaddr_t directmap_virt_end;
-#ifdef CONFIG_ARM_64
-extern vaddr_t directmap_virt_start;
-extern unsigned long directmap_base_pdx;
-#endif
-
 #ifdef CONFIG_ARM_32
 #define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
 #define is_xen_heap_mfn(mfn) ({                                 \
@@ -192,8 +185,6 @@ extern unsigned long total_pages;
 
 #define PDX_GROUP_SHIFT SECOND_SHIFT
 
-/* Boot-time pagetable setup */
-extern void setup_pagetables(unsigned long boot_phys_offset);
 /* Map FDT in boot pagetable */
 extern void *early_fdt_map(paddr_t fdt_paddr);
 /* Remove early mappings */
@@ -203,12 +194,6 @@ extern void remove_early_mappings(void);
 extern int init_secondary_pagetables(int cpu);
 /* Switch secondary CPUS to its own pagetables and finalise MMU setup */
 extern void mmu_init_secondary_cpu(void);
-/*
- * For Arm32, set up the direct-mapped xenheap: up to 1GB of contiguous,
- * always-mapped memory. Base must be 32MB aligned and size a multiple of 32MB.
- * For Arm64, map the region in the directmap area.
- */
-extern void setup_directmap_mappings(unsigned long base_mfn, unsigned long nr_mfns);
 /* Map a frame table to cover physical addresses ps through pe */
 extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
 /* map a physical range in virtual memory */
@@ -256,6 +241,10 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 #define vmap_to_mfn(va)     maddr_to_mfn(virt_to_maddr((vaddr_t)va))
 #define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
 
+#ifndef CONFIG_HAS_MPU
+#include <asm/mm_mmu.h>
+#endif
+
 /* Page-align address and convert to frame number format */
 #define paddr_to_pfn_aligned(paddr)    paddr_to_pfn(PAGE_ALIGN(paddr))
 
xen/arch/arm/include/asm/mm_mmu.h new file +35 -0
new file mode 100644
index 0000000000..a5e63d8af8
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __ARCH_ARM_MM_MMU__
+#define __ARCH_ARM_MM_MMU__
+
+extern mfn_t directmap_mfn_start, directmap_mfn_end;
+extern vaddr_t directmap_virt_end;
+#ifdef CONFIG_ARM_64
+extern vaddr_t directmap_virt_start;
+extern unsigned long directmap_base_pdx;
+#endif
+
+/* Boot-time pagetable setup */
+extern void setup_pagetables(unsigned long boot_phys_offset);
+#define setup_mm_mappings(boot_phys_offset) setup_pagetables(boot_phys_offset)
+
+/* Non-boot CPUs use this to find the correct pagetables. */
+extern uint64_t init_ttbr;
+/*
+ * For Arm32, set up the direct-mapped xenheap: up to 1GB of contiguous,
+ * always-mapped memory. Base must be 32MB aligned and size a multiple of 32MB.
+ * For Arm64, map the region in the directmap area.
+ */
+extern void setup_directmap_mappings(unsigned long base_mfn,
+                                     unsigned long nr_mfns);
+
+#endif /* __ARCH_ARM_MM_MMU__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/mm.c +1 -1351

1,387 lines not on this page · raw message

xen/arch/arm/mm_mmu.c new file +1376 -0

1,376 lines not on this page · raw message

xen/arch/arm/mm_mpu.c +67 -0

81 lines not on this page · raw message

Signature · 1 line
2.25.1
#68 lore raw

Re: [PATCH v2 15/40] xen/arm: move MMU-specific memory management code to mm_mmu.c/mm_mmu.h

Hi,

Quoted 7 lines · Penny Zheng

From: Wei Chen <[email protected]>

To make the code readable and maintainable, we move MMU-specific memory management code from mm.c to mm_mmu.c and move MMU-specific definitions from mm.h to mm_mmu.h.
Later we will create mm_mpu.h and mm_mpu.c for MPU-specific memory management code.

This sentence implies there is no mm_mpu.{c, h} yet and this is not touched within this patch. However...

Quoted 11 lines

This will avoid lots of #ifdef in memory management code and header files.

---
  xen/arch/arm/Makefile             |    5 +
  xen/arch/arm/include/asm/mm.h     |   19 +-
  xen/arch/arm/include/asm/mm_mmu.h |   35 +
  xen/arch/arm/mm.c                 | 1352 +---------------------------
  xen/arch/arm/mm_mmu.c             | 1376 +++++++++++++++++++++++++++++
  xen/arch/arm/mm_mpu.c             |   67 ++

... It looks like they already exists and you are modifying them. That said, it would be better if this patch only contains code movement (IOW no MPU changes).

Quoted 3 lines
  6 files changed, 1488 insertions(+), 1366 deletions(-)

create mode 100644 xen/arch/arm/include/asm/mm_mmu.h create mode 100644 xen/arch/arm/mm_mmu.c

I don't particular like the naming. I think it would make more sense to introduce two directories: "mmu" and "mpu" which includes code specific to each flavor of Xen.

Quoted 6 lines
xen/arch/arm/Makefile +0 -0
index 4d076b278b..21188b207f 100644

xen/arch/arm/Makefile obj-y += kernel.init.o · line 44

Quoted 8 lines
xen/arch/arm/Makefile +5 -0
@@ -37,6 +37,11 @@ obj-y += kernel.init.o
  obj-$(CONFIG_LIVEPATCH) += livepatch.o
  obj-y += mem_access.o
  obj-y += mm.o
+ifneq ($(CONFIG_HAS_MPU), y)
+obj-y += mm_mmu.o
+else
+obj-y += mm_mpu.o
+endif

Shouldn't mm_mpu.o have been incldued where it was first introduced?

Quoted 67 lines
xen/arch/arm/Makefile +0 -0
  obj-y += monitor.o
  obj-y += p2m.o
  obj-y += percpu.o
xen/arch/arm/include/asm/mm.h +4 -15
index 68adcac9fa..1b9fdb6ff5 100644
@@ -154,13 +154,6 @@ struct page_info
  #define _PGC_need_scrub   _PGC_allocated
  #define PGC_need_scrub    PGC_allocated
 
-extern mfn_t directmap_mfn_start, directmap_mfn_end;
-extern vaddr_t directmap_virt_end;
-#ifdef CONFIG_ARM_64
-extern vaddr_t directmap_virt_start;
-extern unsigned long directmap_base_pdx;
-#endif
-
  #ifdef CONFIG_ARM_32
  #define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
  #define is_xen_heap_mfn(mfn) ({                                 \
@@ -192,8 +185,6 @@ extern unsigned long total_pages;
 
  #define PDX_GROUP_SHIFT SECOND_SHIFT
 
-/* Boot-time pagetable setup */
-extern void setup_pagetables(unsigned long boot_phys_offset);
  /* Map FDT in boot pagetable */
  extern void *early_fdt_map(paddr_t fdt_paddr);
  /* Remove early mappings */
@@ -203,12 +194,6 @@ extern void remove_early_mappings(void);
  extern int init_secondary_pagetables(int cpu);
  /* Switch secondary CPUS to its own pagetables and finalise MMU setup */
  extern void mmu_init_secondary_cpu(void);
-/*
- * For Arm32, set up the direct-mapped xenheap: up to 1GB of contiguous,
- * always-mapped memory. Base must be 32MB aligned and size a multiple of 32MB.
- * For Arm64, map the region in the directmap area.
- */
-extern void setup_directmap_mappings(unsigned long base_mfn, unsigned long nr_mfns);
  /* Map a frame table to cover physical addresses ps through pe */
  extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
  /* map a physical range in virtual memory */
@@ -256,6 +241,10 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
  #define vmap_to_mfn(va)     maddr_to_mfn(virt_to_maddr((vaddr_t)va))
  #define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
 
+#ifndef CONFIG_HAS_MPU
+#include <asm/mm_mmu.h>
+#endif
+
  /* Page-align address and convert to frame number format */
  #define paddr_to_pfn_aligned(paddr)    paddr_to_pfn(PAGE_ALIGN(paddr))
 
xen/arch/arm/include/asm/mm_mmu.h new file +7 -0
new file mode 100644
index 0000000000..a5e63d8af8
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __ARCH_ARM_MM_MMU__
+#define __ARCH_ARM_MM_MMU__
+
+extern mfn_t directmap_mfn_start, directmap_mfn_end;
+extern vaddr_t directmap_virt_end;
+#ifdef CONFIG_ARM_64

xen/arch/arm/include/asm/mm_mmu.h line 17

Quoted 10 lines
xen/arch/arm/include/asm/mm_mmu.h new file +10 -0
+extern vaddr_t directmap_virt_start;
+extern unsigned long directmap_base_pdx;
+#endif
+
+/* Boot-time pagetable setup */
+extern void setup_pagetables(unsigned long boot_phys_offset);
+#define setup_mm_mappings(boot_phys_offset) setup_pagetables(boot_phys_offset)
+
+/* Non-boot CPUs use this to find the correct pagetables. */
+extern uint64_t init_ttbr;

Newline here please.

Quoted 1,223 lines
xen/arch/arm/include/asm/mm_mmu.h new file +18 -0
+/*
+ * For Arm32, set up the direct-mapped xenheap: up to 1GB of contiguous,
+ * always-mapped memory. Base must be 32MB aligned and size a multiple of 32MB.
+ * For Arm64, map the region in the directmap area.
+ */
+extern void setup_directmap_mappings(unsigned long base_mfn,
+                                     unsigned long nr_mfns);
+
+#endif /* __ARCH_ARM_MM_MMU__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/mm.c +1 -1172
index 8f15814c5e..e1ce2a62dc 100644
@@ -2,371 +2,24 @@
  /*
   * xen/arch/arm/mm.c
   *
- * MMU code for an ARMv7-A with virt extensions.
+ * Memory management common code for MMU and MPU system.
   *
   * Tim Deegan <[email protected]>
   * Copyright (c) 2011 Citrix Systems.
   */
 
  #include <xen/domain_page.h>
-#include <xen/errno.h>
  #include <xen/grant_table.h>
-#include <xen/guest_access.h>
-#include <xen/init.h>
-#include <xen/libfdt/libfdt.h>
-#include <xen/mm.h>
-#include <xen/pfn.h>
-#include <xen/pmap.h>
  #include <xen/sched.h>
-#include <xen/sizes.h>
  #include <xen/types.h>
-#include <xen/vmap.h>
 
  #include <xsm/xsm.h>
 
-#include <asm/fixmap.h>
-#include <asm/setup.h>
-
-#include <public/memory.h>
-
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef virt_to_mfn
-#define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
-#undef mfn_to_virt
-#define mfn_to_virt(mfn) __mfn_to_virt(mfn_x(mfn))
-
-#ifdef NDEBUG
-static inline void
-__attribute__ ((__format__ (__printf__, 1, 2)))
-mm_printk(const char *fmt, ...) {}
-#else
-#define mm_printk(fmt, args...)             \
-    do                                      \
-    {                                       \
-        dprintk(XENLOG_ERR, fmt, ## args);  \
-        WARN();                             \
-    } while (0)
-#endif
-
-/* Static start-of-day pagetables that we use before the allocators
- * are up. These are used by all CPUs during bringup before switching
- * to the CPUs own pagetables.
- *
- * These pagetables have a very simple structure. They include:
- *  - 2MB worth of 4K mappings of xen at XEN_VIRT_START, boot_first and
- *    boot_second are used to populate the tables down to boot_third
- *    which contains the actual mapping.
- *  - a 1:1 mapping of xen at its current physical address. This uses a
- *    section mapping at whichever of boot_{pgtable,first,second}
- *    covers that physical address.
- *
- * For the boot CPU these mappings point to the address where Xen was
- * loaded by the bootloader. For secondary CPUs they point to the
- * relocated copy of Xen for the benefit of secondary CPUs.
- *
- * In addition to the above for the boot CPU the device-tree is
- * initially mapped in the boot misc slot. This mapping is not present
- * for secondary CPUs.
- *
- * Finally, if EARLY_PRINTK is enabled then xen_fixmap will be mapped
- * by the CPU once it has moved off the 1:1 mapping.
- */
-DEFINE_BOOT_PAGE_TABLE(boot_pgtable);
-#ifdef CONFIG_ARM_64
-DEFINE_BOOT_PAGE_TABLE(boot_first);
-DEFINE_BOOT_PAGE_TABLE(boot_first_id);
-#endif
-DEFINE_BOOT_PAGE_TABLE(boot_second_id);
-DEFINE_BOOT_PAGE_TABLE(boot_third_id);
-DEFINE_BOOT_PAGE_TABLE(boot_second);
-DEFINE_BOOT_PAGE_TABLE(boot_third);
-
-/* Main runtime page tables */
-
-/*
- * For arm32 xen_pgtable are per-PCPU and are allocated before
- * bringing up each CPU. For arm64 xen_pgtable is common to all PCPUs.
- *
- * xen_second, xen_fixmap and xen_xenmap are always shared between all
- * PCPUs.
- */
-
-#ifdef CONFIG_ARM_64
-#define HYP_PT_ROOT_LEVEL 0
-static DEFINE_PAGE_TABLE(xen_pgtable);
-static DEFINE_PAGE_TABLE(xen_first);
-#define THIS_CPU_PGTABLE xen_pgtable
-#else
-#define HYP_PT_ROOT_LEVEL 1
-/* Per-CPU pagetable pages */
-/* xen_pgtable == root of the trie (zeroeth level on 64-bit, first on 32-bit) */
-DEFINE_PER_CPU(lpae_t *, xen_pgtable);
-#define THIS_CPU_PGTABLE this_cpu(xen_pgtable)
-/* Root of the trie for cpu0, other CPU's PTs are dynamically allocated */
-static DEFINE_PAGE_TABLE(cpu0_pgtable);
-#endif
-
-/* Common pagetable leaves */
-/* Second level page table used to cover Xen virtual address space */
-static DEFINE_PAGE_TABLE(xen_second);
-/* Third level page table used for fixmap */
-DEFINE_BOOT_PAGE_TABLE(xen_fixmap);
-/*
- * Third level page table used to map Xen itself with the XN bit set
- * as appropriate.
- */
-static DEFINE_PAGE_TABLE(xen_xenmap);
-
-/* Non-boot CPUs use this to find the correct pagetables. */
-uint64_t init_ttbr;
-
-static paddr_t phys_offset;
-
-/* Limits of the Xen heap */
-mfn_t directmap_mfn_start __read_mostly = INVALID_MFN_INITIALIZER;
-mfn_t directmap_mfn_end __read_mostly;
-vaddr_t directmap_virt_end __read_mostly;
-#ifdef CONFIG_ARM_64
-vaddr_t directmap_virt_start __read_mostly;
-unsigned long directmap_base_pdx __read_mostly;
-#endif
-
  unsigned long frametable_base_pdx __read_mostly;
-unsigned long frametable_virt_end __read_mostly;
 
  unsigned long max_page;
  unsigned long total_pages;
 
-extern char __init_begin[], __init_end[];
-
-/* Checking VA memory layout alignment. */
-static void __init __maybe_unused build_assertions(void)
-{
-    /* 2MB aligned regions */
-    BUILD_BUG_ON(XEN_VIRT_START & ~SECOND_MASK);
-    BUILD_BUG_ON(FIXMAP_ADDR(0) & ~SECOND_MASK);
-    /* 1GB aligned regions */
-#ifdef CONFIG_ARM_32
-    BUILD_BUG_ON(XENHEAP_VIRT_START & ~FIRST_MASK);
-#else
-    BUILD_BUG_ON(DIRECTMAP_VIRT_START & ~FIRST_MASK);
-#endif
-    /* Page table structure constraints */
-#ifdef CONFIG_ARM_64
-    BUILD_BUG_ON(zeroeth_table_offset(XEN_VIRT_START));
-#endif
-    BUILD_BUG_ON(first_table_offset(XEN_VIRT_START));
-#ifdef CONFIG_ARCH_MAP_DOMAIN_PAGE
-    BUILD_BUG_ON(DOMHEAP_VIRT_START & ~FIRST_MASK);
-#endif
-    /*
-     * The boot code expects the regions XEN_VIRT_START, FIXMAP_ADDR(0),
-     * BOOT_FDT_VIRT_START to use the same 0th (arm64 only) and 1st
-     * slot in the page tables.
-     */
-#define CHECK_SAME_SLOT(level, virt1, virt2) \
-    BUILD_BUG_ON(level##_table_offset(virt1) != level##_table_offset(virt2))
-
-#ifdef CONFIG_ARM_64
-    CHECK_SAME_SLOT(zeroeth, XEN_VIRT_START, FIXMAP_ADDR(0));
-    CHECK_SAME_SLOT(zeroeth, XEN_VIRT_START, BOOT_FDT_VIRT_START);
-#endif
-    CHECK_SAME_SLOT(first, XEN_VIRT_START, FIXMAP_ADDR(0));
-    CHECK_SAME_SLOT(first, XEN_VIRT_START, BOOT_FDT_VIRT_START);
-
-#undef CHECK_SAME_SLOT
-}
-
-static lpae_t *xen_map_table(mfn_t mfn)
-{
-    /*
-     * During early boot, map_domain_page() may be unusable. Use the
-     * PMAP to map temporarily a page-table.
-     */
-    if ( system_state == SYS_STATE_early_boot )
-        return pmap_map(mfn);
-
-    return map_domain_page(mfn);
-}
-
-static void xen_unmap_table(const lpae_t *table)
-{
-    /*
-     * During early boot, xen_map_table() will not use map_domain_page()
-     * but the PMAP.
-     */
-    if ( system_state == SYS_STATE_early_boot )
-        pmap_unmap(table);
-    else
-        unmap_domain_page(table);
-}
-
-void dump_pt_walk(paddr_t ttbr, paddr_t addr,
-                  unsigned int root_level,
-                  unsigned int nr_root_tables)
-{
-    static const char *level_strs[4] = { "0TH", "1ST", "2ND", "3RD" };
-    const mfn_t root_mfn = maddr_to_mfn(ttbr);
-    const unsigned int offsets[4] = {
-        zeroeth_table_offset(addr),
-        first_table_offset(addr),
-        second_table_offset(addr),
-        third_table_offset(addr)
-    };
-    lpae_t pte, *mapping;
-    unsigned int level, root_table;
-
-#ifdef CONFIG_ARM_32
-    BUG_ON(root_level < 1);
-#endif
-    BUG_ON(root_level > 3);
-
-    if ( nr_root_tables > 1 )
-    {
-        /*
-         * Concatenated root-level tables. The table number will be
-         * the offset at the previous level. It is not possible to
-         * concatenate a level-0 root.
-         */
-        BUG_ON(root_level == 0);
-        root_table = offsets[root_level - 1];
-        printk("Using concatenated root table %u\n", root_table);
-        if ( root_table >= nr_root_tables )
-        {
-            printk("Invalid root table offset\n");
-            return;
-        }
-    }
-    else
-        root_table = 0;
-
-    mapping = xen_map_table(mfn_add(root_mfn, root_table));
-
-    for ( level = root_level; ; level++ )
-    {
-        if ( offsets[level] > XEN_PT_LPAE_ENTRIES )
-            break;
-
-        pte = mapping[offsets[level]];
-
-        printk("%s[0x%03x] = 0x%"PRIpaddr"\n",
-               level_strs[level], offsets[level], pte.bits);
-
-        if ( level == 3 || !pte.walk.valid || !pte.walk.table )
-            break;
-
-        /* For next iteration */
-        xen_unmap_table(mapping);
-        mapping = xen_map_table(lpae_get_mfn(pte));
-    }
-
-    xen_unmap_table(mapping);
-}
-
-void dump_hyp_walk(vaddr_t addr)
-{
-    uint64_t ttbr = READ_SYSREG64(TTBR0_EL2);
-
-    printk("Walking Hypervisor VA 0x%"PRIvaddr" "
-           "on CPU%d via TTBR 0x%016"PRIx64"\n",
-           addr, smp_processor_id(), ttbr);
-
-    dump_pt_walk(ttbr, addr, HYP_PT_ROOT_LEVEL, 1);
-}
-
-lpae_t mfn_to_xen_entry(mfn_t mfn, unsigned int attr)
-{
-    lpae_t e = (lpae_t) {
-        .pt = {
-            .valid = 1,           /* Mappings are present */
-            .table = 0,           /* Set to 1 for links and 4k maps */
-            .ai = attr,
-            .ns = 1,              /* Hyp mode is in the non-secure world */
-            .up = 1,              /* See below */
-            .ro = 0,              /* Assume read-write */
-            .af = 1,              /* No need for access tracking */
-            .ng = 1,              /* Makes TLB flushes easier */
-            .contig = 0,          /* Assume non-contiguous */
-            .xn = 1,              /* No need to execute outside .text */
-            .avail = 0,           /* Reference count for domheap mapping */
-        }};
-    /*
-     * For EL2 stage-1 page table, up (aka AP[1]) is RES1 as the translation
-     * regime applies to only one exception level (see D4.4.4 and G4.6.1
-     * in ARM DDI 0487B.a). If this changes, remember to update the
-     * hard-coded values in head.S too.
-     */
-
-    switch ( attr )
-    {
-    case MT_NORMAL_NC:
-        /*
-         * ARM ARM: Overlaying the shareability attribute (DDI
-         * 0406C.b B3-1376 to 1377)
-         *
-         * A memory region with a resultant memory type attribute of Normal,
-         * and a resultant cacheability attribute of Inner Non-cacheable,
-         * Outer Non-cacheable, must have a resultant shareability attribute
-         * of Outer Shareable, otherwise shareability is UNPREDICTABLE.
-         *
-         * On ARMv8 sharability is ignored and explicitly treated as Outer
-         * Shareable for Normal Inner Non_cacheable, Outer Non-cacheable.
-         */
-        e.pt.sh = LPAE_SH_OUTER;
-        break;
-    case MT_DEVICE_nGnRnE:
-    case MT_DEVICE_nGnRE:
-        /*
-         * Shareability is ignored for non-Normal memory, Outer is as
-         * good as anything.
-         *
-         * On ARMv8 sharability is ignored and explicitly treated as Outer
-         * Shareable for any device memory type.
-         */
-        e.pt.sh = LPAE_SH_OUTER;
-        break;
-    default:
-        e.pt.sh = LPAE_SH_INNER;  /* Xen mappings are SMP coherent */
-        break;
-    }
-
-    ASSERT(!(mfn_to_maddr(mfn) & ~PADDR_MASK));
-
-    lpae_set_mfn(e, mfn);
-
-    return e;
-}
-
-/* Map a 4k page in a fixmap entry */
-void set_fixmap(unsigned int map, mfn_t mfn, unsigned int flags)
-{
-    int res;
-
-    res = map_pages_to_xen(FIXMAP_ADDR(map), mfn, 1, flags);
-    BUG_ON(res != 0);
-}
-
-/* Remove a mapping from a fixmap entry */
-void clear_fixmap(unsigned int map)
-{
-    int res;
-
-    res = destroy_xen_mappings(FIXMAP_ADDR(map), FIXMAP_ADDR(map) + PAGE_SIZE);
-    BUG_ON(res != 0);
-}
-
-void *map_page_to_xen_misc(mfn_t mfn, unsigned int attributes)
-{
-    set_fixmap(FIXMAP_MISC, mfn, attributes);
-
-    return fix_to_virt(FIXMAP_MISC);
-}
-
-void unmap_page_from_xen_misc(void)
-{
-    clear_fixmap(FIXMAP_MISC);
-}
-
  void flush_page_to_ram(unsigned long mfn, bool sync_icache)
  {
      void *v = map_domain_page(_mfn(mfn));
@@ -386,878 +39,6 @@ void flush_page_to_ram(unsigned long mfn, bool sync_icache)
          invalidate_icache();
  }
 
-static inline lpae_t pte_of_xenaddr(vaddr_t va)
-{
-    paddr_t ma = va + phys_offset;
-
-    return mfn_to_xen_entry(maddr_to_mfn(ma), MT_NORMAL);
-}
-
-void * __init early_fdt_map(paddr_t fdt_paddr)
-{
-    /* We are using 2MB superpage for mapping the FDT */
-    paddr_t base_paddr = fdt_paddr & SECOND_MASK;
-    paddr_t offset;
-    void *fdt_virt;
-    uint32_t size;
-    int rc;
-
-    /*
-     * Check whether the physical FDT address is set and meets the minimum
-     * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be at
-     * least 8 bytes so that we always access the magic and size fields
-     * of the FDT header after mapping the first chunk, double check if
-     * that is indeed the case.
-     */
-    BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
-    if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN )
-        return NULL;
-
-    /* The FDT is mapped using 2MB superpage */
-    BUILD_BUG_ON(BOOT_FDT_VIRT_START % SZ_2M);
-
-    rc = map_pages_to_xen(BOOT_FDT_VIRT_START, maddr_to_mfn(base_paddr),
-                          SZ_2M >> PAGE_SHIFT,
-                          PAGE_HYPERVISOR_RO | _PAGE_BLOCK);
-    if ( rc )
-        panic("Unable to map the device-tree.\n");
-
-
-    offset = fdt_paddr % SECOND_SIZE;
-    fdt_virt = (void *)BOOT_FDT_VIRT_START + offset;
-
-    if ( fdt_magic(fdt_virt) != FDT_MAGIC )
-        return NULL;
-
-    size = fdt_totalsize(fdt_virt);
-    if ( size > MAX_FDT_SIZE )
-        return NULL;
-
-    if ( (offset + size) > SZ_2M )
-    {
-        rc = map_pages_to_xen(BOOT_FDT_VIRT_START + SZ_2M,
-                              maddr_to_mfn(base_paddr + SZ_2M),
-                              SZ_2M >> PAGE_SHIFT,
-                              PAGE_HYPERVISOR_RO | _PAGE_BLOCK);
-        if ( rc )
-            panic("Unable to map the device-tree\n");
-    }
-
-    return fdt_virt;
-}
-
-void __init remove_early_mappings(void)
-{
-    int rc;
-
-    /* destroy the _PAGE_BLOCK mapping */
-    rc = modify_xen_mappings(BOOT_FDT_VIRT_START,
-                             BOOT_FDT_VIRT_START + BOOT_FDT_VIRT_SIZE,
-                             _PAGE_BLOCK);
-    BUG_ON(rc);
-}
-
-/*
- * After boot, Xen page-tables should not contain mapping that are both
- * Writable and eXecutables.
- *
- * This should be called on each CPU to enforce the policy.
- */
-static void xen_pt_enforce_wnx(void)
-{
-    WRITE_SYSREG(READ_SYSREG(SCTLR_EL2) | SCTLR_Axx_ELx_WXN, SCTLR_EL2);
-    /*
-     * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
-     * before flushing the TLBs.
-     */
-    isb();
-    flush_xen_tlb_local();
-}
-
-extern void switch_ttbr(uint64_t ttbr);
-
-/* Clear a translation table and clean & invalidate the cache */
-static void clear_table(void *table)
-{
-    clear_page(table);
-    clean_and_invalidate_dcache_va_range(table, PAGE_SIZE);
-}
-
-/* Boot-time pagetable setup.
- * Changes here may need matching changes in head.S */
-void __init setup_pagetables(unsigned long boot_phys_offset)
-{
-    uint64_t ttbr;
-    lpae_t pte, *p;
-    int i;
-
-    phys_offset = boot_phys_offset;
-
-#ifdef CONFIG_ARM_64
-    p = (void *) xen_pgtable;
-    p[0] = pte_of_xenaddr((uintptr_t)xen_first);
-    p[0].pt.table = 1;
-    p[0].pt.xn = 0;
-    p = (void *) xen_first;
-#else
-    p = (void *) cpu0_pgtable;
-#endif
-
-    /* Map xen second level page-table */
-    p[0] = pte_of_xenaddr((uintptr_t)(xen_second));
-    p[0].pt.table = 1;
-    p[0].pt.xn = 0;
-
-    /* Break up the Xen mapping into 4k pages and protect them separately. */
-    for ( i = 0; i < XEN_PT_LPAE_ENTRIES; i++ )
-    {
-        vaddr_t va = XEN_VIRT_START + (i << PAGE_SHIFT);
-
-        if ( !is_kernel(va) )
-            break;
-        pte = pte_of_xenaddr(va);
-        pte.pt.table = 1; /* 4k mappings always have this bit set */
-        if ( is_kernel_text(va) || is_kernel_inittext(va) )
-        {
-            pte.pt.xn = 0;
-            pte.pt.ro = 1;
-        }
-        if ( is_kernel_rodata(va) )
-            pte.pt.ro = 1;
-        xen_xenmap[i] = pte;
-    }
-
-    /* Initialise xen second level entries ... */
-    /* ... Xen's text etc */
-
-    pte = pte_of_xenaddr((vaddr_t)xen_xenmap);
-    pte.pt.table = 1;
-    xen_second[second_table_offset(XEN_VIRT_START)] = pte;
-
-    /* ... Fixmap */
-    pte = pte_of_xenaddr((vaddr_t)xen_fixmap);
-    pte.pt.table = 1;
-    xen_second[second_table_offset(FIXMAP_ADDR(0))] = pte;
-
-#ifdef CONFIG_ARM_64
-    ttbr = (uintptr_t) xen_pgtable + phys_offset;
-#else
-    ttbr = (uintptr_t) cpu0_pgtable + phys_offset;
-#endif
-
-    switch_ttbr(ttbr);
-
-    xen_pt_enforce_wnx();
-
-#ifdef CONFIG_ARM_32
-    per_cpu(xen_pgtable, 0) = cpu0_pgtable;
-#endif
-}
-
-static void clear_boot_pagetables(void)
-{
-    /*
-     * Clear the copy of the boot pagetables. Each secondary CPU
-     * rebuilds these itself (see head.S).
-     */
-    clear_table(boot_pgtable);
-#ifdef CONFIG_ARM_64
-    clear_table(boot_first);
-    clear_table(boot_first_id);
-#endif
-    clear_table(boot_second);
-    clear_table(boot_third);
-}
-
-#ifdef CONFIG_ARM_64
-int init_secondary_pagetables(int cpu)
-{
-    clear_boot_pagetables();
-
-    /* Set init_ttbr for this CPU coming up. All CPus share a single setof
-     * pagetables, but rewrite it each time for consistency with 32 bit. */
-    init_ttbr = (uintptr_t) xen_pgtable + phys_offset;
-    clean_dcache(init_ttbr);
-    return 0;
-}
-#else
-int init_secondary_pagetables(int cpu)
-{
-    lpae_t *first;
-
-    first = alloc_xenheap_page(); /* root == first level on 32-bit 3-level trie */
-
-    if ( !first )
-    {
-        printk("CPU%u: Unable to allocate the first page-table\n", cpu);
-        return -ENOMEM;
-    }
-
-    /* Initialise root pagetable from root of boot tables */
-    memcpy(first, cpu0_pgtable, PAGE_SIZE);
-    per_cpu(xen_pgtable, cpu) = first;
-
-    if ( !init_domheap_mappings(cpu) )
-    {
-        printk("CPU%u: Unable to prepare the domheap page-tables\n", cpu);
-        per_cpu(xen_pgtable, cpu) = NULL;
-        free_xenheap_page(first);
-        return -ENOMEM;
-    }
-
-    clear_boot_pagetables();
-
-    /* Set init_ttbr for this CPU coming up */
-    init_ttbr = __pa(first);
-    clean_dcache(init_ttbr);
-
-    return 0;
-}
-#endif
-
-/* MMU setup for secondary CPUS (which already have paging enabled) */
-void mmu_init_secondary_cpu(void)
-{
-    xen_pt_enforce_wnx();
-}
-
-#ifdef CONFIG_ARM_32
-/*
- * Set up the direct-mapped xenheap:
- * up to 1GB of contiguous, always-mapped memory.
- */
-void __init setup_directmap_mappings(unsigned long base_mfn,
-                                     unsigned long nr_mfns)
-{
-    int rc;
-
-    rc = map_pages_to_xen(XENHEAP_VIRT_START, _mfn(base_mfn), nr_mfns,
-                          PAGE_HYPERVISOR_RW | _PAGE_BLOCK);
-    if ( rc )
-        panic("Unable to setup the directmap mappings.\n");
-
-    /* Record where the directmap is, for translation routines. */
-    directmap_virt_end = XENHEAP_VIRT_START + nr_mfns * PAGE_SIZE;
-}
-#else /* CONFIG_ARM_64 */
-/* Map the region in the directmap area. */
-void __init setup_directmap_mappings(unsigned long base_mfn,
-                                     unsigned long nr_mfns)
-{
-    int rc;
-
-    /* First call sets the directmap physical and virtual offset. */
-    if ( mfn_eq(directmap_mfn_start, INVALID_MFN) )
-    {
-        unsigned long mfn_gb = base_mfn & ~((FIRST_SIZE >> PAGE_SHIFT) - 1);
-
-        directmap_mfn_start = _mfn(base_mfn);
-        directmap_base_pdx = mfn_to_pdx(_mfn(base_mfn));
-        /*
-         * The base address may not be aligned to the first level
-         * size (e.g. 1GB when using 4KB pages). This would prevent
-         * superpage mappings for all the regions because the virtual
-         * address and machine address should both be suitably aligned.
-         *
-         * Prevent that by offsetting the start of the directmap virtual
-         * address.
-         */
-        directmap_virt_start = DIRECTMAP_VIRT_START +
-            (base_mfn - mfn_gb) * PAGE_SIZE;
-    }
-
-    if ( base_mfn < mfn_x(directmap_mfn_start) )
-        panic("cannot add directmap mapping at %lx below heap start %lx\n",
-              base_mfn, mfn_x(directmap_mfn_start));
-
-    rc = map_pages_to_xen((vaddr_t)__mfn_to_virt(base_mfn),
-                          _mfn(base_mfn), nr_mfns,
-                          PAGE_HYPERVISOR_RW | _PAGE_BLOCK);
-    if ( rc )
-        panic("Unable to setup the directmap mappings.\n");
-}
-#endif
-
-/* Map a frame table to cover physical addresses ps through pe */
-void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
-{
-    unsigned long nr_pdxs = mfn_to_pdx(mfn_add(maddr_to_mfn(pe), -1)) -
-                            mfn_to_pdx(maddr_to_mfn(ps)) + 1;
-    unsigned long frametable_size = nr_pdxs * sizeof(struct page_info);
-    mfn_t base_mfn;
-    const unsigned long mapping_size = frametable_size < MB(32) ? MB(2) : MB(32);
-    int rc;
-
-    frametable_base_pdx = mfn_to_pdx(maddr_to_mfn(ps));
-    /* Round up to 2M or 32M boundary, as appropriate. */
-    frametable_size = ROUNDUP(frametable_size, mapping_size);
-    base_mfn = alloc_boot_pages(frametable_size >> PAGE_SHIFT, 32<<(20-12));
-
-    rc = map_pages_to_xen(FRAMETABLE_VIRT_START, base_mfn,
-                          frametable_size >> PAGE_SHIFT,
-                          PAGE_HYPERVISOR_RW | _PAGE_BLOCK);
-    if ( rc )
-        panic("Unable to setup the frametable mappings.\n");
-
-    memset(&frame_table[0], 0, nr_pdxs * sizeof(struct page_info));
-    memset(&frame_table[nr_pdxs], -1,
-           frametable_size - (nr_pdxs * sizeof(struct page_info)));
-
-    frametable_virt_end = FRAMETABLE_VIRT_START + (nr_pdxs * sizeof(struct page_info));
-}
-
-void *__init arch_vmap_virt_end(void)
-{
-    return (void *)(VMAP_VIRT_START + VMAP_VIRT_SIZE);
-}
-
-/*
- * This function should only be used to remap device address ranges
- * TODO: add a check to verify this assumption
- */
-void *ioremap_attr(paddr_t pa, size_t len, unsigned int attributes)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *ptr = __vmap(&mfn, nr, 1, 1, attributes, VMAP_DEFAULT);
-
-    if ( ptr == NULL )
-        return NULL;
-
-    return ptr + offs;
-}
-
-void *ioremap(paddr_t pa, size_t len)
-{
-    return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE);
-}
-
-static int create_xen_table(lpae_t *entry)
-{
-    mfn_t mfn;
-    void *p;
-    lpae_t pte;
-
-    if ( system_state != SYS_STATE_early_boot )
-    {
-        struct page_info *pg = alloc_domheap_page(NULL, 0);
-
-        if ( pg == NULL )
-            return -ENOMEM;
-
-        mfn = page_to_mfn(pg);
-    }
-    else
-        mfn = alloc_boot_pages(1, 1);
-
-    p = xen_map_table(mfn);
-    clear_page(p);
-    xen_unmap_table(p);
-
-    pte = mfn_to_xen_entry(mfn, MT_NORMAL);
-    pte.pt.table = 1;
-    write_pte(entry, pte);
-
-    return 0;
-}
-
-#define XEN_TABLE_MAP_FAILED 0
-#define XEN_TABLE_SUPER_PAGE 1
-#define XEN_TABLE_NORMAL_PAGE 2
-
-/*
- * Take the currently mapped table, find the corresponding entry,
- * and map the next table, if available.
- *
- * The read_only parameters indicates whether intermediate tables should
- * be allocated when not present.
- *
- * Return values:
- *  XEN_TABLE_MAP_FAILED: Either read_only was set and the entry
- *  was empty, or allocating a new page failed.
- *  XEN_TABLE_NORMAL_PAGE: next level mapped normally
- *  XEN_TABLE_SUPER_PAGE: The next entry points to a superpage.
- */
-static int xen_pt_next_level(bool read_only, unsigned int level,
-                             lpae_t **table, unsigned int offset)
-{
-    lpae_t *entry;
-    int ret;
-    mfn_t mfn;
-
-    entry = *table + offset;
-
-    if ( !lpae_is_valid(*entry) )
-    {
-        if ( read_only )
-            return XEN_TABLE_MAP_FAILED;
-
-        ret = create_xen_table(entry);
-        if ( ret )
-            return XEN_TABLE_MAP_FAILED;
-    }
-
-    /* The function xen_pt_next_level is never called at the 3rd level */
-    if ( lpae_is_mapping(*entry, level) )
-        return XEN_TABLE_SUPER_PAGE;
-
-    mfn = lpae_get_mfn(*entry);
-
-    xen_unmap_table(*table);
-    *table = xen_map_table(mfn);
-
-    return XEN_TABLE_NORMAL_PAGE;
-}
-
-/* Sanity check of the entry */
-static bool xen_pt_check_entry(lpae_t entry, mfn_t mfn, unsigned int level,
-                               unsigned int flags)
-{
-    /* Sanity check when modifying an entry. */
-    if ( (flags & _PAGE_PRESENT) && mfn_eq(mfn, INVALID_MFN) )
-    {
-        /* We don't allow modifying an invalid entry. */
-        if ( !lpae_is_valid(entry) )
-        {
-            mm_printk("Modifying invalid entry is not allowed.\n");
-            return false;
-        }
-
-        /* We don't allow modifying a table entry */
-        if ( !lpae_is_mapping(entry, level) )
-        {
-            mm_printk("Modifying a table entry is not allowed.\n");
-            return false;
-        }
-
-        /* We don't allow changing memory attributes. */
-        if ( entry.pt.ai != PAGE_AI_MASK(flags) )
-        {
-            mm_printk("Modifying memory attributes is not allowed (0x%x -> 0x%x).\n",
-                      entry.pt.ai, PAGE_AI_MASK(flags));
-            return false;
-        }
-
-        /* We don't allow modifying entry with contiguous bit set. */
-        if ( entry.pt.contig )
-        {
-            mm_printk("Modifying entry with contiguous bit set is not allowed.\n");
-            return false;
-        }
-    }
-    /* Sanity check when inserting a mapping */
-    else if ( flags & _PAGE_PRESENT )
-    {
-        /* We should be here with a valid MFN. */
-        ASSERT(!mfn_eq(mfn, INVALID_MFN));
-
-        /*
-         * We don't allow replacing any valid entry.
-         *
-         * Note that the function xen_pt_update() relies on this
-         * assumption and will skip the TLB flush. The function will need
-         * to be updated if the check is relaxed.
-         */
-        if ( lpae_is_valid(entry) )
-        {
-            if ( lpae_is_mapping(entry, level) )
-                mm_printk("Changing MFN for a valid entry is not allowed (%#"PRI_mfn" -> %#"PRI_mfn").\n",
-                          mfn_x(lpae_get_mfn(entry)), mfn_x(mfn));
-            else
-                mm_printk("Trying to replace a table with a mapping.\n");
-            return false;
-        }
-    }
-    /* Sanity check when removing a mapping. */
-    else if ( (flags & (_PAGE_PRESENT|_PAGE_POPULATE)) == 0 )
-    {
-        /* We should be here with an invalid MFN. */
-        ASSERT(mfn_eq(mfn, INVALID_MFN));
-
-        /* We don't allow removing a table */
-        if ( lpae_is_table(entry, level) )
-        {
-            mm_printk("Removing a table is not allowed.\n");
-            return false;
-        }
-
-        /* We don't allow removing a mapping with contiguous bit set. */
-        if ( entry.pt.contig )
-        {
-            mm_printk("Removing entry with contiguous bit set is not allowed.\n");
-            return false;
-        }
-    }
-    /* Sanity check when populating the page-table. No check so far. */
-    else
-    {
-        ASSERT(flags & _PAGE_POPULATE);
-        /* We should be here with an invalid MFN */
-        ASSERT(mfn_eq(mfn, INVALID_MFN));
-    }
-
-    return true;
-}
-
-/* Update an entry at the level @target. */
-static int xen_pt_update_entry(mfn_t root, unsigned long virt,
-                               mfn_t mfn, unsigned int target,
-                               unsigned int flags)
-{
-    int rc;
-    unsigned int level;
-    lpae_t *table;
-    /*
-     * The intermediate page tables are read-only when the MFN is not valid
-     * and we are not populating page table.
-     * This means we either modify permissions or remove an entry.
-     */
-    bool read_only = mfn_eq(mfn, INVALID_MFN) && !(flags & _PAGE_POPULATE);
-    lpae_t pte, *entry;
-
-    /* convenience aliases */
-    DECLARE_OFFSETS(offsets, (paddr_t)virt);
-
-    /* _PAGE_POPULATE and _PAGE_PRESENT should never be set together. */
-    ASSERT((flags & (_PAGE_POPULATE|_PAGE_PRESENT)) != (_PAGE_POPULATE|_PAGE_PRESENT));
-
-    table = xen_map_table(root);
-    for ( level = HYP_PT_ROOT_LEVEL; level < target; level++ )
-    {
-        rc = xen_pt_next_level(read_only, level, &table, offsets[level]);
-        if ( rc == XEN_TABLE_MAP_FAILED )
-        {
-            /*
-             * We are here because xen_pt_next_level has failed to map
-             * the intermediate page table (e.g the table does not exist
-             * and the pt is read-only). It is a valid case when
-             * removing a mapping as it may not exist in the page table.
-             * In this case, just ignore it.
-             */
-            if ( flags & (_PAGE_PRESENT|_PAGE_POPULATE) )
-            {
-                mm_printk("%s: Unable to map level %u\n", __func__, level);
-                rc = -ENOENT;
-                goto out;
-            }
-            else
-            {
-                rc = 0;
-                goto out;
-            }
-        }
-        else if ( rc != XEN_TABLE_NORMAL_PAGE )
-            break;
-    }
-
-    if ( level != target )
-    {
-        mm_printk("%s: Shattering superpage is not supported\n", __func__);
-        rc = -EOPNOTSUPP;
-        goto out;
-    }
-
-    entry = table + offsets[level];
-
-    rc = -EINVAL;
-    if ( !xen_pt_check_entry(*entry, mfn, level, flags) )
-        goto out;
-
-    /* If we are only populating page-table, then we are done. */
-    rc = 0;
-    if ( flags & _PAGE_POPULATE )
-        goto out;
-
-    /* We are removing the page */
-    if ( !(flags & _PAGE_PRESENT) )
-        memset(&pte, 0x00, sizeof(pte));
-    else
-    {
-        /* We are inserting a mapping => Create new pte. */
-        if ( !mfn_eq(mfn, INVALID_MFN) )
-        {
-            pte = mfn_to_xen_entry(mfn, PAGE_AI_MASK(flags));
-
-            /*
-             * First and second level pages set pte.pt.table = 0, but
-             * third level entries set pte.pt.table = 1.
-             */
-            pte.pt.table = (level == 3);
-        }
-        else /* We are updating the permission => Copy the current pte. */
-            pte = *entry;
-
-        /* Set permission */
-        pte.pt.ro = PAGE_RO_MASK(flags);
-        pte.pt.xn = PAGE_XN_MASK(flags);
-        /* Set contiguous bit */
-        pte.pt.contig = !!(flags & _PAGE_CONTIG);
-    }
-
-    write_pte(entry, pte);
-
-    rc = 0;
-
-out:
-    xen_unmap_table(table);
-
-    return rc;
-}
-
-/* Return the level where mapping should be done */
-static int xen_pt_mapping_level(unsigned long vfn, mfn_t mfn, unsigned long nr,
-                                unsigned int flags)
-{
-    unsigned int level;
-    unsigned long mask;
-
-    /*
-      * Don't take into account the MFN when removing mapping (i.e
-      * MFN_INVALID) to calculate the correct target order.
-      *
-      * Per the Arm Arm, `vfn` and `mfn` must be both superpage aligned.
-      * They are or-ed together and then checked against the size of
-      * each level.
-      *
-      * `left` is not included and checked separately to allow
-      * superpage mapping even if it is not properly aligned (the
-      * user may have asked to map 2MB + 4k).
-      */
-     mask = !mfn_eq(mfn, INVALID_MFN) ? mfn_x(mfn) : 0;
-     mask |= vfn;
-
-     /*
-      * Always use level 3 mapping unless the caller request block
-      * mapping.
-      */
-     if ( likely(!(flags & _PAGE_BLOCK)) )
-         level = 3;
-     else if ( !(mask & (BIT(FIRST_ORDER, UL) - 1)) &&
-               (nr >= BIT(FIRST_ORDER, UL)) )
-         level = 1;
-     else if ( !(mask & (BIT(SECOND_ORDER, UL) - 1)) &&
-               (nr >= BIT(SECOND_ORDER, UL)) )
-         level = 2;
-     else
-         level = 3;
-
-     return level;
-}
-
-#define XEN_PT_4K_NR_CONTIG 16
-
-/*
- * Check whether the contiguous bit can be set. Return the number of
- * contiguous entry allowed. If not allowed, return 1.
- */
-static unsigned int xen_pt_check_contig(unsigned long vfn, mfn_t mfn,
-                                        unsigned int level, unsigned long left,
-                                        unsigned int flags)
-{
-    unsigned long nr_contig;
-
-    /*
-     * Allow the contiguous bit to set when the caller requests block
-     * mapping.
-     */
-    if ( !(flags & _PAGE_BLOCK) )
-        return 1;
-
-    /*
-     * We don't allow to remove mapping with the contiguous bit set.
-     * So shortcut the logic and directly return 1.
-     */
-    if ( mfn_eq(mfn, INVALID_MFN) )
-        return 1;
-
-    /*
-     * The number of contiguous entries varies depending on the page
-     * granularity used. The logic below assumes 4KB.
-     */
-    BUILD_BUG_ON(PAGE_SIZE != SZ_4K);
-
-    /*
-     * In order to enable the contiguous bit, we should have enough entries
-     * to map left and both the virtual and physical address should be
-     * aligned to the size of 16 translation tables entries.
-     */
-    nr_contig = BIT(XEN_PT_LEVEL_ORDER(level), UL) * XEN_PT_4K_NR_CONTIG;
-
-    if ( (left < nr_contig) || ((mfn_x(mfn) | vfn) & (nr_contig - 1)) )
-        return 1;
-
-    return XEN_PT_4K_NR_CONTIG;
-}
-
-static DEFINE_SPINLOCK(xen_pt_lock);
-
-static int xen_pt_update(unsigned long virt,
-                         mfn_t mfn,
-                         /* const on purpose as it is used for TLB flush */
-                         const unsigned long nr_mfns,
-                         unsigned int flags)
-{
-    int rc = 0;
-    unsigned long vfn = virt >> PAGE_SHIFT;
-    unsigned long left = nr_mfns;
-
-    /*
-     * For arm32, page-tables are different on each CPUs. Yet, they share
-     * some common mappings. It is assumed that only common mappings
-     * will be modified with this function.
-     *
-     * XXX: Add a check.
-     */
-    const mfn_t root = maddr_to_mfn(READ_SYSREG64(TTBR0_EL2));
-
-    /*
-     * The hardware was configured to forbid mapping both writeable and
-     * executable.
-     * When modifying/creating mapping (i.e _PAGE_PRESENT is set),
-     * prevent any update if this happen.
-     */
-    if ( (flags & _PAGE_PRESENT) && !PAGE_RO_MASK(flags) &&
-         !PAGE_XN_MASK(flags) )
-    {
-        mm_printk("Mappings should not be both Writeable and Executable.\n");
-        return -EINVAL;
-    }
-
-    if ( flags & _PAGE_CONTIG )
-    {
-        mm_printk("_PAGE_CONTIG is an internal only flag.\n");
-        return -EINVAL;
-    }
-
-    if ( !IS_ALIGNED(virt, PAGE_SIZE) )
-    {
-        mm_printk("The virtual address is not aligned to the page-size.\n");
-        return -EINVAL;
-    }
-
-    spin_lock(&xen_pt_lock);
-
-    while ( left )
-    {
-        unsigned int order, level, nr_contig, new_flags;
-
-        level = xen_pt_mapping_level(vfn, mfn, left, flags);
-        order = XEN_PT_LEVEL_ORDER(level);
-
-        ASSERT(left >= BIT(order, UL));
-
-        /*
-         * Check if we can set the contiguous mapping and update the
-         * flags accordingly.
-         */
-        nr_contig = xen_pt_check_contig(vfn, mfn, level, left, flags);
-        new_flags = flags | ((nr_contig > 1) ? _PAGE_CONTIG : 0);
-
-        for ( ; nr_contig > 0; nr_contig-- )
-        {
-            rc = xen_pt_update_entry(root, vfn << PAGE_SHIFT, mfn, level,
-                                     new_flags);
-            if ( rc )
-                break;
-
-            vfn += 1U << order;
-            if ( !mfn_eq(mfn, INVALID_MFN) )
-                mfn = mfn_add(mfn, 1U << order);
-
-            left -= (1U << order);
-        }
-
-        if ( rc )
-            break;
-    }
-
-    /*
-     * The TLBs flush can be safely skipped when a mapping is inserted
-     * as we don't allow mapping replacement (see xen_pt_check_entry()).
-     *
-     * For all the other cases, the TLBs will be flushed unconditionally
-     * even if the mapping has failed. This is because we may have
-     * partially modified the PT. This will prevent any unexpected
-     * behavior afterwards.
-     */
-    if ( !((flags & _PAGE_PRESENT) && !mfn_eq(mfn, INVALID_MFN)) )
-        flush_xen_tlb_range_va(virt, PAGE_SIZE * nr_mfns);
-
-    spin_unlock(&xen_pt_lock);
-
-    return rc;
-}
-
-int map_pages_to_xen(unsigned long virt,
-                     mfn_t mfn,
-                     unsigned long nr_mfns,
-                     unsigned int flags)
-{
-    return xen_pt_update(virt, mfn, nr_mfns, flags);
-}
-
-int populate_pt_range(unsigned long virt, unsigned long nr_mfns)
-{
-    return xen_pt_update(virt, INVALID_MFN, nr_mfns, _PAGE_POPULATE);
-}
-
-int destroy_xen_mappings(unsigned long s, unsigned long e)
-{
-    ASSERT(IS_ALIGNED(s, PAGE_SIZE));
-    ASSERT(IS_ALIGNED(e, PAGE_SIZE));
-    ASSERT(s <= e);
-    return xen_pt_update(s, INVALID_MFN, (e - s) >> PAGE_SHIFT, 0);
-}
-

xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 1222

Quoted 10 lines
xen/arch/arm/mm.c +0 -10
-int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int flags)
-{
-    ASSERT(IS_ALIGNED(s, PAGE_SIZE));
-    ASSERT(IS_ALIGNED(e, PAGE_SIZE));
-    ASSERT(s <= e);
-    return xen_pt_update(s, INVALID_MFN, (e - s) >> PAGE_SHIFT, flags);
-}
-
-/* Release all __init and __initdata ranges to be reused */
-void free_init_memory(void)

This function doesn't look specific to the MMU.

Quoted 38 lines
xen/arch/arm/mm.c +0 -38
-{
-    paddr_t pa = virt_to_maddr(__init_begin);
-    unsigned long len = __init_end - __init_begin;
-    uint32_t insn;
-    unsigned int i, nr = len / sizeof(insn);
-    uint32_t *p;
-    int rc;
-
-    rc = modify_xen_mappings((unsigned long)__init_begin,
-                             (unsigned long)__init_end, PAGE_HYPERVISOR_RW);
-    if ( rc )
-        panic("Unable to map RW the init section (rc = %d)\n", rc);
-
-    /*
-     * From now on, init will not be used for execution anymore,
-     * so nuke the instruction cache to remove entries related to init.
-     */
-    invalidate_icache_local();
-
-#ifdef CONFIG_ARM_32
-    /* udf instruction i.e (see A8.8.247 in ARM DDI 0406C.c) */
-    insn = 0xe7f000f0;
-#else
-    insn = AARCH64_BREAK_FAULT;
-#endif
-    p = (uint32_t *)__init_begin;
-    for ( i = 0; i < nr; i++ )
-        *(p + i) = insn;
-
-    rc = destroy_xen_mappings((unsigned long)__init_begin,
-                              (unsigned long)__init_end);
-    if ( rc )
-        panic("Unable to remove the init section (rc = %d)\n", rc);
-
-    init_domheap_pages(pa, pa + len);
-    printk("Freed %ldkB init memory.\n", (long)(__init_end-__init_begin)>>10);
-}
-

[...]

xen/arch/arm/mm_mpu.c line 26

Quoted 12 lines
xen/arch/arm/mm_mpu.c +2 -0
index 43e9a1be4d..87a12042cc 100644
@@ -20,8 +20,10 @@
   */
 
  #include <xen/init.h>
+#include <xen/mm.h>
  #include <xen/page-size.h>
  #include <asm/arm64/mpu.h>
+#include <asm/page.h>

Regardless of what I wrote above, none of the code you add seems to require <asm/page.h>

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 43

Quoted 8 lines
xen/arch/arm/mm_mpu.c +1 -0
  /* Xen MPU memory region mapping table. */
  pr_t __aligned(PAGE_SIZE) __section(".data.page_aligned")
@@ -38,6 +40,71 @@ uint64_t __ro_after_init next_transient_region_idx;
  /* Maximum number of supported MPU memory regions by the EL2 MPU. */
  uint64_t __ro_after_init max_xen_mpumap;
 
+/* TODO: Implementation on the first usage */

It is not clear what you mean given there are some callers.

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 45

Quoted 2 lines
xen/arch/arm/mm_mpu.c +2 -0
+void dump_hyp_walk(vaddr_t addr)
+{

Please add ASSERT_UNREACHABLE() for any dummy helper you have introduced any plan to implement later. This will be helpful to track down any function you haven't implemented.

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 54

Quoted 9 lines
xen/arch/arm/mm_mpu.c +9 -0
+}
+
+void * __init early_fdt_map(paddr_t fdt_paddr)
+{
+    return NULL;
+}
+
+void __init remove_early_mappings(void)
+{

Ditto

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 63

Quoted 9 lines
xen/arch/arm/mm_mpu.c +9 -0
+}
+
+int init_secondary_pagetables(int cpu)
+{
+    return -ENOSYS;
+}
+
+void mmu_init_secondary_cpu(void)
+{

Ditto. The name of the function is also a bit odd given this is an MPU specific file. This likely want to be renamed to mm_init_secondary_cpu().

Quoted 22 lines
xen/arch/arm/mm_mpu.c +22 -0
+}
+
+void *ioremap_attr(paddr_t pa, size_t len, unsigned int attributes)
+{
+    return NULL;
+}
+
+void *ioremap(paddr_t pa, size_t len)
+{
+    return NULL;
+}
+
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)
+{
+    return -ENOSYS;
+}
+
+int destroy_xen_mappings(unsigned long s, unsigned long e)
+{

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 95

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+    return -ENOSYS;
+}
+
+int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int flags)
+{
+    return -ENOSYS;
+}
+
+void free_init_memory(void)
+{

Ditto.

Quoted 15 lines
xen/arch/arm/mm_mpu.c +12 -0
+}
+
+int xenmem_add_to_physmap_one(
+    struct domain *d,
+    unsigned int space,
+    union add_to_physmap_extra extra,
+    unsigned long idx,
+    gfn_t gfn)
+{
+    return -ENOSYS;
+}
+
  /*
   * Local variables:
   * mode: C
Signature · 1 line
Julien Grall
#69 lore raw

RE: [PATCH v2 15/40] xen/arm: move MMU-specific memory management code to mm_mmu.c/mm_mmu.h

Hi,

Quoted 51 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, February 6, 2023 5:30 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 15/40] xen/arm: move MMU-specific memory management code to mm_mmu.c/mm_mmu.h

Hi,

Quoted 4 lines · Penny Zheng

From: Wei Chen <[email protected]>

To make the code readable and maintainable, we move MMU-specific memory management code from mm.c to mm_mmu.c and move MMU-

specific

Quoted 3 lines

definitions from mm.h to mm_mmu.h.
Later we will create mm_mpu.h and mm_mpu.c for MPU-specific memory management code.

This sentence implies there is no mm_mpu.{c, h} yet and this is not touched within this patch. However...

Quoted 10 lines

This will avoid lots of #ifdef in memory management code and header files.

---
  xen/arch/arm/Makefile             |    5 +
  xen/arch/arm/include/asm/mm.h     |   19 +-
  xen/arch/arm/include/asm/mm_mmu.h |   35 +
  xen/arch/arm/mm.c                 | 1352 +---------------------------
  xen/arch/arm/mm_mmu.c             | 1376

+++++++++++++++++++++++++++++

Quoted 1 line
  xen/arch/arm/mm_mpu.c             |   67 ++

... It looks like they already exists and you are modifying them. That said, it would be better if this patch only contains code movement (IOW no MPU changes).

Quoted 3 lines
  6 files changed, 1488 insertions(+), 1366 deletions(-)

create mode 100644 xen/arch/arm/include/asm/mm_mmu.h create mode 100644 xen/arch/arm/mm_mmu.c

I don't particular like the naming. I think it would make more sense to introduce two directories: "mmu" and "mpu" which includes code specific to each flavor of Xen.

[...]

Quoted 7 lines

xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 1222

Quoted 4 lines
xen/arch/arm/mm.c +0 -3
-
-/* Release all __init and __initdata ranges to be reused */
-void free_init_memory(void)

This function doesn't look specific to the MMU.

Functions like, early_fdt_map[1] / setup_frametable_mappings[2] / free_init_memory [3] ... they both share quite the same logic as MMU does in MPU system, the difference could only be address translation regime. Still, in order to avoid putting too much #ifdef here and there, I implement different MMU and MPU version of them.

Or I keep them in generic file here, then in future commits when we implement MPU version of them(I list related commits below), I transfer them to MMU file there.

Wdyt?

[1] https://lists.xenproject.org/archives/html/xen-devel/2023-01/msg00774.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2023-01/msg00787.html
[3] https://lists.xenproject.org/archives/html/xen-devel/2023-01/msg00786.html

Quoted 159 lines
Quoted 26 lines
xen/arch/arm/mm.c +0 -26
-{
-    paddr_t pa = virt_to_maddr(__init_begin);
-    unsigned long len = __init_end - __init_begin;
-    uint32_t insn;
-    unsigned int i, nr = len / sizeof(insn);
-    uint32_t *p;
-    int rc;
-
-    rc = modify_xen_mappings((unsigned long)__init_begin,
-                             (unsigned long)__init_end, PAGE_HYPERVISOR_RW);
-    if ( rc )
-        panic("Unable to map RW the init section (rc = %d)\n", rc);
-
-    /*
-     * From now on, init will not be used for execution anymore,
-     * so nuke the instruction cache to remove entries related to init.
-     */
-    invalidate_icache_local();
-
-#ifdef CONFIG_ARM_32
-    /* udf instruction i.e (see A8.8.247 in ARM DDI 0406C.c) */
-    insn = 0xe7f000f0;
-#else
-    insn = AARCH64_BREAK_FAULT;
-#endif
-    p = (uint32_t *)__init_begin;

xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn, bool sync_icache)

Quoted 10 lines
xen/arch/arm/mm.c +0 -10
-    for ( i = 0; i < nr; i++ )
-        *(p + i) = insn;
-
-    rc = destroy_xen_mappings((unsigned long)__init_begin,
-                              (unsigned long)__init_end);
-    if ( rc )
-        panic("Unable to remove the init section (rc = %d)\n", rc);
-
-    init_domheap_pages(pa, pa + len);
-    printk("Freed %ldkB init memory.\n", (long)(__init_end-

__init_begin)>>10);

Quoted 2 lines
(quoted diff) +0 -2
-}
-

[...]

xen/arch/arm/mm_mpu.c line 26

Quoted 12 lines
xen/arch/arm/mm_mpu.c +2 -0
index 43e9a1be4d..87a12042cc 100644
@@ -20,8 +20,10 @@
   */
 
  #include <xen/init.h>
+#include <xen/mm.h>
  #include <xen/page-size.h>
  #include <asm/arm64/mpu.h>
+#include <asm/page.h>

Regardless of what I wrote above, none of the code you add seems to require <asm/page.h>

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 40

Quoted 5 lines
xen/arch/arm/mm_mpu.c +0 -0
  /* Xen MPU memory region mapping table. */
  pr_t __aligned(PAGE_SIZE) __section(".data.page_aligned")
@@ -38,6 +40,71 @@ uint64_t __ro_after_init next_transient_region_idx;
  /* Maximum number of supported MPU memory regions by the EL2 MPU.

*/

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 43

Quoted 3 lines
xen/arch/arm/mm_mpu.c +1 -0
  uint64_t __ro_after_init max_xen_mpumap;
 
+/* TODO: Implementation on the first usage */

It is not clear what you mean given there are some callers.

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 45

Quoted 2 lines
xen/arch/arm/mm_mpu.c +2 -0
+void dump_hyp_walk(vaddr_t addr)
+{

Please add ASSERT_UNREACHABLE() for any dummy helper you have introduced
any plan to implement later. This will be helpful to track down any function you haven't implemented.

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 54

Quoted 9 lines
xen/arch/arm/mm_mpu.c +9 -0
+}
+
+void * __init early_fdt_map(paddr_t fdt_paddr)
+{
+    return NULL;
+}
+
+void __init remove_early_mappings(void)
+{

Ditto

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 63

Quoted 9 lines
xen/arch/arm/mm_mpu.c +9 -0
+}
+
+int init_secondary_pagetables(int cpu)
+{
+    return -ENOSYS;
+}
+
+void mmu_init_secondary_cpu(void)
+{

Ditto. The name of the function is also a bit odd given this is an MPU specific file. This likely want to be renamed to mm_init_secondary_cpu().

Quoted 16 lines
xen/arch/arm/mm_mpu.c +16 -0
+}
+
+void *ioremap_attr(paddr_t pa, size_t len, unsigned int attributes)
+{
+    return NULL;
+}
+
+void *ioremap(paddr_t pa, size_t len)
+{
+    return NULL;
+}
+
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx;

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+{
+    return -ENOSYS;
+}
+
+int destroy_xen_mappings(unsigned long s, unsigned long e)
+{
+    return -ENOSYS;
+}
+
+int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int

flags)

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 95

Quoted 6 lines
xen/arch/arm/mm_mpu.c +6 -0
+{
+    return -ENOSYS;
+}
+
+void free_init_memory(void)
+{

Ditto.

Quoted 5 lines
xen/arch/arm/mm_mpu.c +5 -0
+}
+
+int xenmem_add_to_physmap_one(
+    struct domain *d,
+    unsigned int space,

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 110

Quoted 10 lines
xen/arch/arm/mm_mpu.c +7 -0
+    union add_to_physmap_extra extra,
+    unsigned long idx,
+    gfn_t gfn)
+{
+    return -ENOSYS;
+}
+
  /*
   * Local variables:
   * mode: C

--
Julien Grall

#70 lore raw

Re: [PATCH v2 15/40] xen/arm: move MMU-specific memory management code to mm_mmu.c/mm_mmu.h

Hi,

Quoted 53 lines · Penny Zheng
Quoted 51 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, February 6, 2023 5:30 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 15/40] xen/arm: move MMU-specific memory management code to mm_mmu.c/mm_mmu.h

Hi,

Quoted 4 lines · Penny Zheng

From: Wei Chen <[email protected]>

To make the code readable and maintainable, we move MMU-specific memory management code from mm.c to mm_mmu.c and move MMU-

specific

Quoted 3 lines

definitions from mm.h to mm_mmu.h.
Later we will create mm_mpu.h and mm_mpu.c for MPU-specific memory management code.

This sentence implies there is no mm_mpu.{c, h} yet and this is not touched within this patch. However...

Quoted 10 lines

This will avoid lots of #ifdef in memory management code and header files.

---
   xen/arch/arm/Makefile             |    5 +
   xen/arch/arm/include/asm/mm.h     |   19 +-
   xen/arch/arm/include/asm/mm_mmu.h |   35 +
   xen/arch/arm/mm.c                 | 1352 +---------------------------
   xen/arch/arm/mm_mmu.c             | 1376

+++++++++++++++++++++++++++++

Quoted 1 line
   xen/arch/arm/mm_mpu.c             |   67 ++

... It looks like they already exists and you are modifying them. That said, it would be better if this patch only contains code movement (IOW no MPU changes).

Quoted 3 lines
   6 files changed, 1488 insertions(+), 1366 deletions(-)

create mode 100644 xen/arch/arm/include/asm/mm_mmu.h create mode 100644 xen/arch/arm/mm_mmu.c

I don't particular like the naming. I think it would make more sense to introduce two directories: "mmu" and "mpu" which includes code specific to each flavor of Xen.

[...]

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

xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 1222

Quoted 3 lines
xen/arch/arm/mm.c +0 -3
-
-/* Release all __init and __initdata ranges to be reused */
-void free_init_memory(void)

This function doesn't look specific to the MMU.

Functions like, early_fdt_map[1] / setup_frametable_mappings[2] / free_init_memory [3] ...

I looked at setup_frametable_mappings() and didn't think it was possible to share much code. But I agree for early_fdt_map and free_init_memory().

Quoted 3 lines

they both share quite the same logic as MMU does in MPU system, the difference could only be address translation regime. Still, in order to avoid putting too much #ifdef here and there, I implement different MMU and MPU version of them.

I am not sure why you would need to put #ifdef in the code. Looking at it, there is usually only a chunk that is different for the mapping. So you could provide an helper that will be implemented in the MMU/MPU code.

Quoted 3 lines

Or I keep them in generic file here, then in future commits when we implement MPU version of them(I list related commits below), I transfer them to MMU file there.

I am not entirely sure. In one way it help to figure out the commonality but on the other way we are mixing code movement and new code.

In this case, the code movement would probably small, so that might be better for the review.

Cheers,

Signature · 1 line
Julien Grall
#71 lore raw

PATCH v2 16/40 xen/arm: introduce setup_mm_mappings

In reply to Penny Zheng

Function setup_pagetables is responsible for boot-time pagetable setup in MMU system.
But in MPU system, we have already built up start-of-day Xen MPU memory region mapping at the very beginning in assembly.

So in order to keep only one codeflow in arm/setup.c, setup_mm_mappings , with a more generic name, is introduced and act as an empty stub in MPU system.

---
 xen/arch/arm/include/asm/mm.h     |  2 ++
 xen/arch/arm/include/asm/mm_mpu.h | 16 ++++++++++++++++
 xen/arch/arm/setup.c              |  2 +-
 3 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/include/asm/mm_mpu.h
xen/arch/arm/include/asm/mm.h +2 -0
index 1b9fdb6ff5..9b4c07d965 100644
@@ -243,6 +243,8 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 
 #ifndef CONFIG_HAS_MPU
 #include <asm/mm_mmu.h>
+#else
+#include <asm/mm_mpu.h>
 #endif
 
 /* Page-align address and convert to frame number format */
xen/arch/arm/include/asm/mm_mpu.h new file +16 -0
new file mode 100644
index 0000000000..1f3cff7743
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __ARCH_ARM_MM_MPU__
+#define __ARCH_ARM_MM_MPU__
+
+#define setup_mm_mappings(boot_phys_offset) ((void)(boot_phys_offset))
+
+#endif /* __ARCH_ARM_MM_MPU__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/setup.c +1 -1
index 1f26f67b90..d7d200179c 100644
@@ -1003,7 +1003,7 @@ void __init start_xen(unsigned long boot_phys_offset,
     /* Initialize traps early allow us to get backtrace when an error occurred */
     init_traps();
 
-    setup_pagetables(boot_phys_offset);
+    setup_mm_mappings(boot_phys_offset);
 
     smp_clear_cpu_maps();
 
Signature · 1 line
2.25.1
#72 lore raw

Re: [PATCH v2 16/40] xen/arm: introduce setup_mm_mappings

Hi,

Quoted 8 lines · Penny Zheng

Function setup_pagetables is responsible for boot-time pagetable setup in MMU system.
But in MPU system, we have already built up start-of-day Xen MPU memory region mapping at the very beginning in assembly.

So in order to keep only one codeflow in arm/setup.c, setup_mm_mappings , with a more generic name, is introduced and act as an empty stub in MPU system.

is the empty stub temporarily?

Quoted 50 lines
---
  xen/arch/arm/include/asm/mm.h     |  2 ++
  xen/arch/arm/include/asm/mm_mpu.h | 16 ++++++++++++++++
  xen/arch/arm/setup.c              |  2 +-
  3 files changed, 19 insertions(+), 1 deletion(-)

create mode 100644 xen/arch/arm/include/asm/mm_mpu.h

xen/arch/arm/include/asm/mm.h +2 -0
index 1b9fdb6ff5..9b4c07d965 100644
@@ -243,6 +243,8 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 
  #ifndef CONFIG_HAS_MPU
  #include <asm/mm_mmu.h>
+#else
+#include <asm/mm_mpu.h>
  #endif
 
  /* Page-align address and convert to frame number format */
xen/arch/arm/include/asm/mm_mpu.h new file +16 -0
new file mode 100644
index 0000000000..1f3cff7743
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __ARCH_ARM_MM_MPU__
+#define __ARCH_ARM_MM_MPU__
+
+#define setup_mm_mappings(boot_phys_offset) ((void)(boot_phys_offset))
+
+#endif /* __ARCH_ARM_MM_MPU__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/setup.c +0 -0
index 1f26f67b90..d7d200179c 100644

xen/arch/arm/setup.c void __init start_xen(unsigned long boot_phys_offset, · line 1006

Quoted 5 lines
xen/arch/arm/setup.c +1 -1
@@ -1003,7 +1003,7 @@ void __init start_xen(unsigned long boot_phys_offset,
      /* Initialize traps early allow us to get backtrace when an error occurred */
      init_traps();
 
-    setup_pagetables(boot_phys_offset);
+    setup_mm_mappings(boot_phys_offset);

You are renaming the caller but not the function. Why?

xen/arch/arm/setup.c void __init start_xen(unsigned long boot_phys_offset, · line 1009

Quoted 3 lines
xen/arch/arm/setup.c +0 -0
      smp_clear_cpu_maps();
 

Cheers,

Signature · 1 line
Julien Grall
#73 lore raw

RE: [PATCH v2 16/40] xen/arm: introduce setup_mm_mappings

Hi Julien

Quoted 76 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, February 6, 2023 5:32 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 16/40] xen/arm: introduce setup_mm_mappings

Hi,

Quoted 8 lines · Penny Zheng

Function setup_pagetables is responsible for boot-time pagetable setup in MMU system.
But in MPU system, we have already built up start-of-day Xen MPU memory region mapping at the very beginning in assembly.

So in order to keep only one codeflow in arm/setup.c, setup_mm_mappings , with a more generic name, is introduced and act as an empty stub in MPU system.

is the empty stub temporarily?

xen/arch/arm/setup.c void __init start_xen(unsigned long

Quoted 52 lines
---
  xen/arch/arm/include/asm/mm.h     |  2 ++
  xen/arch/arm/include/asm/mm_mpu.h | 16 ++++++++++++++++
  xen/arch/arm/setup.c              |  2 +-
  3 files changed, 19 insertions(+), 1 deletion(-)

create mode 100644 xen/arch/arm/include/asm/mm_mpu.h

(quoted diff) +0 -0

b/xen/arch/arm/include/asm/mm.h index 1b9fdb6ff5..9b4c07d965 100644

xen/arch/arm/include/asm/mm.h +2 -0
@@ -243,6 +243,8 @@ static inline void __iomem *ioremap_wc(paddr_t
 start, size_t len)
 
  #ifndef CONFIG_HAS_MPU
  #include <asm/mm_mmu.h>
+#else
+#include <asm/mm_mpu.h>
  #endif
 

/* Page-align address and convert to frame number format */ diff
--git a/xen/arch/arm/include/asm/mm_mpu.h b/xen/arch/arm/include/asm/mm_mpu.h
new file mode 100644
index 0000000000..1f3cff7743

xen/arch/arm/include/asm/mm_mpu.h new file +16 -0
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef
+__ARCH_ARM_MM_MPU__ #define __ARCH_ARM_MM_MPU__
+
+#define setup_mm_mappings(boot_phys_offset)
+((void)(boot_phys_offset))
+
+#endif /* __ARCH_ARM_MM_MPU__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/setup.c index renamed from xen/arch/arm/setup.c +0 -0

1f26f67b90..d7d200179c 100644

xen/arch/arm/setup.c +0 -0
@@ -1003,7 +1003,7 @@ void __init start_xen(unsigned long

boot_phys_offset,

Quoted 9 lines

xen/arch/arm/setup.c void __init start_xen(unsigned long · line 1003

Quoted 1 line
xen/arch/arm/setup.c +0 -0
      /* Initialize traps early allow us to get backtrace when an error occurred

*/

xen/arch/arm/setup.c void __init start_xen(unsigned long boot_phys_offset, · line 1006

Quoted 4 lines
xen/arch/arm/setup.c +1 -1
      init_traps();
 
-    setup_pagetables(boot_phys_offset);
+    setup_mm_mappings(boot_phys_offset);

You are renaming the caller but not the function. Why?

It is a reorg mistake. MMU-related implementation has been mistakenly
put in previous commit "[PATCH v2 15/40] xen/arm: move MMU-specific
memory management code to mm_mmu.c/mm_mmu.h"(https://lists.xenproject.org/archives/html/xen-devel/2023-01/msg00776.html ) Sorry for that.
I'll extract the relative codes from the previous commit:
'''
+/* Boot-time pagetable setup */
+#define setup_mm_mappings(boot_phys_offset) setup_pagetables(boot_phys_offset)
'''

Quoted 8 lines

xen/arch/arm/setup.c void __init start_xen(unsigned long boot_phys_offset, · line 1009

Quoted 3 lines
xen/arch/arm/setup.c +0 -0
      smp_clear_cpu_maps();
 

Cheers,

--
Julien Grall

#74 lore raw

PATCH v2 17/40 xen/mpu: plump virt/maddr/mfn convertion in MPU system

In reply to Penny Zheng

virt_to_maddr and maddr_to_virt are used widely in Xen code. So even there is no VMSA in MPU system, we keep the interface name to stay the same code flow.

We move the existing virt/maddr convertion from mm.h to mm_mmu.h. And the MPU version of virt/maddr convertion is simple, returning the input address as the output.

We should overide virt_to_mfn/mfn_to_virt in source file mm_mpu.c the same way in mm_mmu.c.

---
 xen/arch/arm/include/asm/mm.h     | 26 --------------------------
 xen/arch/arm/include/asm/mm_mmu.h | 26 ++++++++++++++++++++++++++
 xen/arch/arm/include/asm/mm_mpu.h | 13 +++++++++++++
 xen/arch/arm/mm_mpu.c             |  6 ++++++
 4 files changed, 45 insertions(+), 26 deletions(-)
xen/arch/arm/include/asm/mm.h +0 -26
index 9b4c07d965..e29158028a 100644
@@ -250,32 +250,6 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 /* Page-align address and convert to frame number format */
 #define paddr_to_pfn_aligned(paddr)    paddr_to_pfn(PAGE_ALIGN(paddr))
 
-static inline paddr_t __virt_to_maddr(vaddr_t va)
-{
-    uint64_t par = va_to_par(va);
-    return (par & PADDR_MASK & PAGE_MASK) | (va & ~PAGE_MASK);
-}
-#define virt_to_maddr(va)   __virt_to_maddr((vaddr_t)(va))
-
-#ifdef CONFIG_ARM_32
-static inline void *maddr_to_virt(paddr_t ma)
-{
-    ASSERT(is_xen_heap_mfn(maddr_to_mfn(ma)));
-    ma -= mfn_to_maddr(directmap_mfn_start);
-    return (void *)(unsigned long) ma + XENHEAP_VIRT_START;
-}
-#else
-static inline void *maddr_to_virt(paddr_t ma)
-{
-    ASSERT((mfn_to_pdx(maddr_to_mfn(ma)) - directmap_base_pdx) <
-           (DIRECTMAP_SIZE >> PAGE_SHIFT));
-    return (void *)(XENHEAP_VIRT_START -
-                    (directmap_base_pdx << PAGE_SHIFT) +
-                    ((ma & ma_va_bottom_mask) |
-                     ((ma & ma_top_mask) >> pfn_pdx_hole_shift)));
-}
-#endif
-
 /*
  * Translate a guest virtual address to a machine address.
  * Return the fault information if the translation has failed else 0.
xen/arch/arm/include/asm/mm_mmu.h +26 -0
index a5e63d8af8..6d7e5ddde7 100644
@@ -23,6 +23,32 @@ extern uint64_t init_ttbr;
 extern void setup_directmap_mappings(unsigned long base_mfn,
                                      unsigned long nr_mfns);
 
+static inline paddr_t __virt_to_maddr(vaddr_t va)
+{
+    uint64_t par = va_to_par(va);
+    return (par & PADDR_MASK & PAGE_MASK) | (va & ~PAGE_MASK);
+}
+#define virt_to_maddr(va)   __virt_to_maddr((vaddr_t)(va))
+
+#ifdef CONFIG_ARM_32
+static inline void *maddr_to_virt(paddr_t ma)
+{
+    ASSERT(is_xen_heap_mfn(maddr_to_mfn(ma)));
+    ma -= mfn_to_maddr(directmap_mfn_start);
+    return (void *)(unsigned long) ma + XENHEAP_VIRT_START;
+}
+#else
+static inline void *maddr_to_virt(paddr_t ma)
+{
+    ASSERT((mfn_to_pdx(maddr_to_mfn(ma)) - directmap_base_pdx) <
+           (DIRECTMAP_SIZE >> PAGE_SHIFT));
+    return (void *)(XENHEAP_VIRT_START -
+                    (directmap_base_pdx << PAGE_SHIFT) +
+                    ((ma & ma_va_bottom_mask) |
+                     ((ma & ma_top_mask) >> pfn_pdx_hole_shift)));
+}
+#endif
+
 #endif /* __ARCH_ARM_MM_MMU__ */
 
 /*
xen/arch/arm/include/asm/mm_mpu.h +13 -0
index 1f3cff7743..3a4b07f187 100644
@@ -4,6 +4,19 @@
 
 #define setup_mm_mappings(boot_phys_offset) ((void)(boot_phys_offset))
 
+static inline paddr_t __virt_to_maddr(vaddr_t va)
+{
+    /* In MPU system, VA == PA. */
+    return (paddr_t)va;
+}
+#define virt_to_maddr(va)   __virt_to_maddr((vaddr_t)(va))
+
+static inline void *maddr_to_virt(paddr_t ma)
+{
+    /* In MPU system, VA == PA. */
+    return (void *)ma;
+}
+
 #endif /* __ARCH_ARM_MM_MPU__ */
 
 /*
xen/arch/arm/mm_mpu.c +6 -0
index 87a12042cc..c9e17ab6da 100644
@@ -29,6 +29,12 @@
 pr_t __aligned(PAGE_SIZE) __section(".data.page_aligned")
      xen_mpumap[ARM_MAX_MPU_MEMORY_REGIONS];
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef virt_to_mfn
+#define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
+#undef mfn_to_virt
+#define mfn_to_virt(mfn) __mfn_to_virt(mfn_x(mfn))
+
 /* Index into MPU memory region map for fixed regions, ascending from zero. */
 uint64_t __ro_after_init next_fixed_region_idx;
 /*
Signature · 1 line
2.25.1
#75 lore raw

Re: [PATCH v2 17/40] xen/mpu: plump virt/maddr/mfn convertion in MPU system

Hi,

title: typo: s/convertion/conversion/

Quoted 6 lines · Penny Zheng

virt_to_maddr and maddr_to_virt are used widely in Xen code. So even there is no VMSA in MPU system, we keep the interface name to stay the same code flow.

We move the existing virt/maddr convertion from mm.h to mm_mmu.h. And the MPU version of virt/maddr convertion is simple, returning

ditto.

Quoted 3 lines

the input address as the output.

We should overide virt_to_mfn/mfn_to_virt in source file mm_mpu.c the

ditto: s/overide/override/

Quoted 90 lines

same way in mm_mmu.c.

---
  xen/arch/arm/include/asm/mm.h     | 26 --------------------------
  xen/arch/arm/include/asm/mm_mmu.h | 26 ++++++++++++++++++++++++++
  xen/arch/arm/include/asm/mm_mpu.h | 13 +++++++++++++
  xen/arch/arm/mm_mpu.c             |  6 ++++++
  4 files changed, 45 insertions(+), 26 deletions(-)
xen/arch/arm/include/asm/mm.h +0 -26
index 9b4c07d965..e29158028a 100644
@@ -250,32 +250,6 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
  /* Page-align address and convert to frame number format */
  #define paddr_to_pfn_aligned(paddr)    paddr_to_pfn(PAGE_ALIGN(paddr))
 
-static inline paddr_t __virt_to_maddr(vaddr_t va)
-{
-    uint64_t par = va_to_par(va);
-    return (par & PADDR_MASK & PAGE_MASK) | (va & ~PAGE_MASK);
-}
-#define virt_to_maddr(va)   __virt_to_maddr((vaddr_t)(va))
-
-#ifdef CONFIG_ARM_32
-static inline void *maddr_to_virt(paddr_t ma)
-{
-    ASSERT(is_xen_heap_mfn(maddr_to_mfn(ma)));
-    ma -= mfn_to_maddr(directmap_mfn_start);
-    return (void *)(unsigned long) ma + XENHEAP_VIRT_START;
-}
-#else
-static inline void *maddr_to_virt(paddr_t ma)
-{
-    ASSERT((mfn_to_pdx(maddr_to_mfn(ma)) - directmap_base_pdx) <
-           (DIRECTMAP_SIZE >> PAGE_SHIFT));
-    return (void *)(XENHEAP_VIRT_START -
-                    (directmap_base_pdx << PAGE_SHIFT) +
-                    ((ma & ma_va_bottom_mask) |
-                     ((ma & ma_top_mask) >> pfn_pdx_hole_shift)));
-}
-#endif
-
  /*
   * Translate a guest virtual address to a machine address.
   * Return the fault information if the translation has failed else 0.
xen/arch/arm/include/asm/mm_mmu.h +26 -0
index a5e63d8af8..6d7e5ddde7 100644
@@ -23,6 +23,32 @@ extern uint64_t init_ttbr;
  extern void setup_directmap_mappings(unsigned long base_mfn,
                                       unsigned long nr_mfns);
 
+static inline paddr_t __virt_to_maddr(vaddr_t va)
+{
+    uint64_t par = va_to_par(va);
+    return (par & PADDR_MASK & PAGE_MASK) | (va & ~PAGE_MASK);
+}
+#define virt_to_maddr(va)   __virt_to_maddr((vaddr_t)(va))
+
+#ifdef CONFIG_ARM_32
+static inline void *maddr_to_virt(paddr_t ma)
+{
+    ASSERT(is_xen_heap_mfn(maddr_to_mfn(ma)));
+    ma -= mfn_to_maddr(directmap_mfn_start);
+    return (void *)(unsigned long) ma + XENHEAP_VIRT_START;
+}
+#else
+static inline void *maddr_to_virt(paddr_t ma)
+{
+    ASSERT((mfn_to_pdx(maddr_to_mfn(ma)) - directmap_base_pdx) <
+           (DIRECTMAP_SIZE >> PAGE_SHIFT));
+    return (void *)(XENHEAP_VIRT_START -
+                    (directmap_base_pdx << PAGE_SHIFT) +
+                    ((ma & ma_va_bottom_mask) |
+                     ((ma & ma_top_mask) >> pfn_pdx_hole_shift)));
+}
+#endif
+
  #endif /* __ARCH_ARM_MM_MMU__ */
 
  /*
xen/arch/arm/include/asm/mm_mpu.h +0 -0
index 1f3cff7743..3a4b07f187 100644

xen/arch/arm/include/asm/mm_mpu.h line 12

Quoted 9 lines
xen/arch/arm/include/asm/mm_mpu.h +6 -0
@@ -4,6 +4,19 @@
 
  #define setup_mm_mappings(boot_phys_offset) ((void)(boot_phys_offset))
 
+static inline paddr_t __virt_to_maddr(vaddr_t va)
+{
+    /* In MPU system, VA == PA. */
+    return (paddr_t)va;
+}
+#define virt_to_maddr(va)   __virt_to_maddr((vaddr_t)(va))

This define is exactly the same as the MMU version. Can it be implemented in mm.h?

Quoted 15 lines
xen/arch/arm/include/asm/mm_mpu.h +7 -0
+
+static inline void *maddr_to_virt(paddr_t ma)
+{
+    /* In MPU system, VA == PA. */
+    return (void *)ma;
+}
+
  #endif /* __ARCH_ARM_MM_MPU__ */
 
  /*
xen/arch/arm/mm_mpu.c +0 -0
index 87a12042cc..c9e17ab6da 100644

xen/arch/arm/mm_mpu.c line 36

Quoted 8 lines
xen/arch/arm/mm_mpu.c +5 -0
@@ -29,6 +29,12 @@
  pr_t __aligned(PAGE_SIZE) __section(".data.page_aligned")
       xen_mpumap[ARM_MAX_MPU_MEMORY_REGIONS];
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef virt_to_mfn
+#define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
+#undef mfn_to_virt
+#define mfn_to_virt(mfn) __mfn_to_virt(mfn_x(mfn))

They should be implemented when you need them.

xen/arch/arm/mm_mpu.c line 40

Quoted 4 lines
xen/arch/arm/mm_mpu.c +1 -0
+
  /* Index into MPU memory region map for fixed regions, ascending from zero. */
  uint64_t __ro_after_init next_fixed_region_idx;
  /*

Cheers,

Signature · 1 line
Julien Grall
#76 lore raw

PATCH v2 18/40 xen/mpu: introduce helper access_protection_region

In reply to Penny Zheng

Each EL2 MPU protection region could be configured using PRBAR<n>_EL2 and PRLAR<n>_EL2.

This commit introduces a new helper access_protection_region() to access EL2 MPU protection region, including both read/write operations.

As explained in section G1.3.18 of the reference manual for AArch64v8R, a set of system register PRBAR<n>_EL2 and PRLAR<n>_EL2 provide access to the EL2 MPU region which is determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.(n = 0, 1, 2, ... , 15) For example to access regions from 16 to 31:
- Set PRSELR_EL2 to 0b1xxxx
- Region 16 configuration is accessible through PRBAR0_EL2 and PRLAR0_EL2
- Region 17 configuration is accessible through PRBAR1_EL2 and PRLAR1_EL2
- Region 18 configuration is accessible through PRBAR2_EL2 and PRLAR2_EL2
- ...
- Region 31 configuration is accessible through PRBAR15_EL2 and PRLAR15_EL2

---
 xen/arch/arm/mm_mpu.c | 151 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
xen/arch/arm/mm_mpu.c +151 -0
index c9e17ab6da..f2b494449c 100644
@@ -46,6 +46,157 @@ uint64_t __ro_after_init next_transient_region_idx;
 /* Maximum number of supported MPU memory regions by the EL2 MPU. */
 uint64_t __ro_after_init max_xen_mpumap;
 
+/* Write a MPU protection region */
+#define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
+    uint64_t _sel = sel;                                                \
+    const pr_t *_pr = pr;                                               \
+    asm volatile(                                                       \
+        "msr "__stringify(PRSELR_EL2)", %0;" /* Selects the region */   \
+        "dsb sy;"                                                       \
+        "msr "__stringify(prbar_el2)", %1;" /* Write PRBAR<n>_EL2 */    \
+        "msr "__stringify(prlar_el2)", %2;" /* Write PRLAR<n>_EL2 */    \
+        "dsb sy;"                                                       \
+        : : "r" (_sel), "r" (_pr->prbar.bits), "r" (_pr->prlar.bits));  \
+})
+
+/* Read a MPU protection region */
+#define READ_PROTECTION_REGION(sel, prbar_el2, prlar_el2) ({            \
+    uint64_t _sel = sel;                                                \
+    pr_t _pr;                                                           \
+    asm volatile(                                                       \
+        "msr "__stringify(PRSELR_EL2)", %2;" /* Selects the region */   \
+        "dsb sy;"                                                       \
+        "mrs %0, "__stringify(prbar_el2)";" /* Read PRBAR<n>_EL2 */     \
+        "mrs %1, "__stringify(prlar_el2)";" /* Read PRLAR<n>_EL2 */     \
+        "dsb sy;"                                                       \
+        : "=r" (_pr.prbar.bits), "=r" (_pr.prlar.bits) : "r" (_sel));   \
+    _pr;                                                                \
+})
+
+/*
+ * Access MPU protection region, including both read/write operations.
+ * Armv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for Armv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provide access to the EL2 MPU region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR0_ELx and PRLAR0_ELx
+ * - Region 17 configuration is accessible through PRBAR1_ELx and PRLAR1_ELx
+ * - Region 18 configuration is accessible through PRBAR2_ELx and PRLAR2_ELx
+ * - ...
+ * - Region 31 configuration is accessible through PRBAR15_ELx and PRLAR15_ELx
+ *
+ * @read: if it is read operation.
+ * @pr_read: mpu protection region returned by read op.
+ * @pr_write: const mpu protection region passed through write op.
+ * @sel: mpu protection region selector
+ */
+static void access_protection_region(bool read, pr_t *pr_read,
+                                     const pr_t *pr_write, uint64_t sel)
+{
+    switch ( sel & 0xf )
+    {
+    case 0:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR0_EL2, PRLAR0_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR0_EL2, PRLAR0_EL2);
+        break;
+    case 1:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR1_EL2, PRLAR1_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR1_EL2, PRLAR1_EL2);
+        break;
+    case 2:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR2_EL2, PRLAR2_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR2_EL2, PRLAR2_EL2);
+        break;
+    case 3:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR3_EL2, PRLAR3_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR3_EL2, PRLAR3_EL2);
+        break;
+    case 4:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR4_EL2, PRLAR4_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR4_EL2, PRLAR4_EL2);
+        break;
+    case 5:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR5_EL2, PRLAR5_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR5_EL2, PRLAR5_EL2);
+        break;
+    case 6:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR6_EL2, PRLAR6_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR6_EL2, PRLAR6_EL2);
+        break;
+    case 7:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR7_EL2, PRLAR7_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR7_EL2, PRLAR7_EL2);
+        break;
+    case 8:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR8_EL2, PRLAR8_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR8_EL2, PRLAR8_EL2);
+        break;
+    case 9:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR9_EL2, PRLAR9_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR9_EL2, PRLAR9_EL2);
+        break;
+    case 10:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR10_EL2, PRLAR10_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR10_EL2, PRLAR10_EL2);
+        break;
+    case 11:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR11_EL2, PRLAR11_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR11_EL2, PRLAR11_EL2);
+        break;
+    case 12:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR12_EL2, PRLAR12_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR12_EL2, PRLAR12_EL2);
+        break;
+    case 13:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR13_EL2, PRLAR13_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR13_EL2, PRLAR13_EL2);
+        break;
+    case 14:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR14_EL2, PRLAR14_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR14_EL2, PRLAR14_EL2);
+        break;
+    case 15:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR15_EL2, PRLAR15_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR15_EL2, PRLAR15_EL2);
+        break;
+    }
+}
+
 /* TODO: Implementation on the first usage */
 void dump_hyp_walk(vaddr_t addr)
 {
Signature · 1 line
2.25.1
#77 lore raw

Re: [PATCH v2 18/40] xen/mpu: introduce helper access_protection_region

Hi Penny,

Quoted 29 lines · Penny Zheng

Each EL2 MPU protection region could be configured using PRBAR<n>_EL2 and PRLAR<n>_EL2.

This commit introduces a new helper access_protection_region() to access EL2 MPU protection region, including both read/write operations.

As explained in section G1.3.18 of the reference manual for AArch64v8R, a set of system register PRBAR<n>_EL2 and PRLAR<n>_EL2 provide access to the EL2 MPU region which is determined by the value of 'n' and PRSELR_EL2.REGION as PRSELR_EL2.REGION<7:4>:n.(n = 0, 1, 2, ... , 15) For example to access regions from 16 to 31:
- Set PRSELR_EL2 to 0b1xxxx
- Region 16 configuration is accessible through PRBAR0_EL2 and PRLAR0_EL2
- Region 17 configuration is accessible through PRBAR1_EL2 and PRLAR1_EL2
- Region 18 configuration is accessible through PRBAR2_EL2 and PRLAR2_EL2
- ...
- Region 31 configuration is accessible through PRBAR15_EL2 and PRLAR15_EL2

---
  xen/arch/arm/mm_mpu.c | 151 ++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 151 insertions(+)
xen/arch/arm/mm_mpu.c +0 -0
index c9e17ab6da..f2b494449c 100644

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 54

Quoted 9 lines
xen/arch/arm/mm_mpu.c +6 -0
@@ -46,6 +46,157 @@ uint64_t __ro_after_init next_transient_region_idx;
  /* Maximum number of supported MPU memory regions by the EL2 MPU. */
  uint64_t __ro_after_init max_xen_mpumap;
 
+/* Write a MPU protection region */
+#define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
+    uint64_t _sel = sel;                                                \
+    const pr_t *_pr = pr;                                               \
+    asm volatile(                                                       \
+        "msr "__stringify(PRSELR_EL2)", %0;" /* Selects the region */   \

This is an open-coding version of WRITE_SYSREG(). Can we use it instead?

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 55

Quoted 1 line
xen/arch/arm/mm_mpu.c +1 -0
+        "dsb sy;"                                                       \

What is this dsb for? Also is the 'sy' really necessary?

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 56

Quoted 1 line
xen/arch/arm/mm_mpu.c +1 -0
+        "msr "__stringify(prbar_el2)", %1;" /* Write PRBAR<n>_EL2 */    \

WRITE_SYSREG()?

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 57

Quoted 1 line
xen/arch/arm/mm_mpu.c +1 -0
+        "msr "__stringify(prlar_el2)", %2;" /* Write PRLAR<n>_EL2 */    \

WRITE_SYSREG()?

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 58

Quoted 1 line
xen/arch/arm/mm_mpu.c +1 -0
+        "dsb sy;"                                                       \

Same about dsb. But I would consider to move the dsb and selection part outside of the macro. So they could outside of the switch and reduce the code generation.

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 63

Quoted 5 lines
xen/arch/arm/mm_mpu.c +5 -0
+        : : "r" (_sel), "r" (_pr->prbar.bits), "r" (_pr->prlar.bits));  \
+})
+
+/* Read a MPU protection region */
+#define READ_PROTECTION_REGION(sel, prbar_el2, prlar_el2) ({            \

My comment on READ_PROTECTION also applies here. But you would want to use READ_SYSREG() for 'mrs'.

Quoted 24 lines
xen/arch/arm/mm_mpu.c +24 -0
+    uint64_t _sel = sel;                                                \
+    pr_t _pr;                                                           \
+    asm volatile(                                                       \
+        "msr "__stringify(PRSELR_EL2)", %2;" /* Selects the region */   \
+        "dsb sy;"                                                       \
+        "mrs %0, "__stringify(prbar_el2)";" /* Read PRBAR<n>_EL2 */     \
+        "mrs %1, "__stringify(prlar_el2)";" /* Read PRLAR<n>_EL2 */     \
+        "dsb sy;"                                                       \
+        : "=r" (_pr.prbar.bits), "=r" (_pr.prlar.bits) : "r" (_sel));   \
+    _pr;                                                                \
+})
+
+/*
+ * Access MPU protection region, including both read/write operations.
+ * Armv8-R AArch64 at most supports 255 MPU protection regions.
+ * See section G1.3.18 of the reference manual for Armv8-R AArch64,
+ * PRBAR<n>_EL2 and PRLAR<n>_EL2 provide access to the EL2 MPU region
+ * determined by the value of 'n' and PRSELR_EL2.REGION as
+ * PRSELR_EL2.REGION<7:4>:n(n = 0, 1, 2, ... , 15)
+ * For example to access regions from 16 to 31 (0b10000 to 0b11111):
+ * - Set PRSELR_EL2 to 0b1xxxx
+ * - Region 16 configuration is accessible through PRBAR0_ELx and PRLAR0_ELx
+ * - Region 17 configuration is accessible through PRBAR1_ELx and PRLAR1_ELx
+ * - Region 18 configuration is accessible through PRBAR2_ELx and PRLAR2_ELx

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 97

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+ * - ...
+ * - Region 31 configuration is accessible through PRBAR15_ELx and PRLAR15_ELx
+ *
+ * @read: if it is read operation.
+ * @pr_read: mpu protection region returned by read op.
+ * @pr_write: const mpu protection region passed through write op.
+ * @sel: mpu protection region selector
+ */
+static void access_protection_region(bool read, pr_t *pr_read,
+                                     const pr_t *pr_write, uint64_t sel)

I would rather prefer if we introduce two helpers (one for the read operation, the other for the write operation). This would make the code a bit easier to read.

Quoted 89 lines
xen/arch/arm/mm_mpu.c +89 -0
+{
+    switch ( sel & 0xf )
+    {
+    case 0:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR0_EL2, PRLAR0_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR0_EL2, PRLAR0_EL2);
+        break;
+    case 1:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR1_EL2, PRLAR1_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR1_EL2, PRLAR1_EL2);
+        break;
+    case 2:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR2_EL2, PRLAR2_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR2_EL2, PRLAR2_EL2);
+        break;
+    case 3:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR3_EL2, PRLAR3_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR3_EL2, PRLAR3_EL2);
+        break;
+    case 4:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR4_EL2, PRLAR4_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR4_EL2, PRLAR4_EL2);
+        break;
+    case 5:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR5_EL2, PRLAR5_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR5_EL2, PRLAR5_EL2);
+        break;
+    case 6:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR6_EL2, PRLAR6_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR6_EL2, PRLAR6_EL2);
+        break;
+    case 7:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR7_EL2, PRLAR7_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR7_EL2, PRLAR7_EL2);
+        break;
+    case 8:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR8_EL2, PRLAR8_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR8_EL2, PRLAR8_EL2);
+        break;
+    case 9:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR9_EL2, PRLAR9_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR9_EL2, PRLAR9_EL2);
+        break;
+    case 10:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR10_EL2, PRLAR10_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR10_EL2, PRLAR10_EL2);
+        break;
+    case 11:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR11_EL2, PRLAR11_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR11_EL2, PRLAR11_EL2);
+        break;
+    case 12:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR12_EL2, PRLAR12_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR12_EL2, PRLAR12_EL2);
+        break;
+    case 13:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR13_EL2, PRLAR13_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR13_EL2, PRLAR13_EL2);
+        break;
+    case 14:
+        if ( read )

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 196

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR14_EL2, PRLAR14_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR14_EL2, PRLAR14_EL2);
+        break;
+    case 15:
+        if ( read )
+            *pr_read = READ_PROTECTION_REGION(sel, PRBAR15_EL2, PRLAR15_EL2);
+        else
+            WRITE_PROTECTION_REGION(sel, pr_write, PRBAR15_EL2, PRLAR15_EL2);
+        break;

What if the caller pass a number higher than 15?

xen/arch/arm/mm_mpu.c uint64_t __ro_after_init next_transient_region_idx; · line 202

Quoted 6 lines
xen/arch/arm/mm_mpu.c +3 -0
+    }
+}
+
  /* TODO: Implementation on the first usage */
  void dump_hyp_walk(vaddr_t addr)
  {

Cheers,

Signature · 1 line
Julien Grall
#78 lore raw

PATCH v2 19/40 xen/mpu: populate a new region in Xen MPU mapping table

In reply to Penny Zheng

The new helper xen_mpumap_update() is responsible for updating an entry in Xen MPU memory mapping table, including creating a new entry, updating or destroying an existing one.

This commit only talks about populating a new entry in Xen MPU mapping table( xen_mpumap). Others will be introduced in the following commits.

In xen_mpumap_update_entry(), firstly, we shall check if requested address range [base, limit) is not mapped. Then we use pr_of_xenaddr() to build up the structure of MPU memory region(pr_t).
In the last, we set memory attribute and permission based on variable @flags.

To summarize all region attributes in one variable @flags, layout of the flags is elaborated as follows:
[0:2] Memory attribute Index
[3:4] Execute Never
[5:6] Access Permission
[7] Region Present
Also, we provide a set of definitions(REGION_HYPERVISOR_RW, etc) that combine the memory attribute and permission for common combinations.

---
 xen/arch/arm/include/asm/arm64/mpu.h |  72 +++++++
 xen/arch/arm/mm_mpu.c                | 276 ++++++++++++++++++++++++++-
 2 files changed, 340 insertions(+), 8 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +72 -0
index c945dd53db..fcde6ad0db 100644
@@ -16,6 +16,61 @@
  */
 #define ARM_MAX_MPU_MEMORY_REGIONS 255
 
+/* Access permission attributes. */
+/* Read/Write at EL2, No Access at EL1/EL0. */
+#define AP_RW_EL2 0x0
+/* Read/Write at EL2/EL1/EL0 all levels. */
+#define AP_RW_ALL 0x1
+/* Read-only at EL2, No Access at EL1/EL0. */
+#define AP_RO_EL2 0x2
+/* Read-only at EL2/EL1/EL0 all levels. */
+#define AP_RO_ALL 0x3
+
+/*
+ * Excute never.
+ * Stage 1 EL2 translation regime.
+ * XN[1] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ * Stage 2 EL1/EL0 translation regime.
+ * XN[0] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ */
+#define XN_DISABLED    0x0
+#define XN_P2M_ENABLED 0x1
+#define XN_ENABLED     0x2
+
+/*
+ * Layout of the flags used for updating Xen MPU region attributes
+ * [0:2] Memory attribute Index
+ * [3:4] Execute Never
+ * [5:6] Access Permission
+ * [7]   Region Present
+ */
+#define _REGION_AI_BIT            0
+#define _REGION_XN_BIT            3
+#define _REGION_AP_BIT            5
+#define _REGION_PRESENT_BIT       7
+#define _REGION_XN                (2U << _REGION_XN_BIT)
+#define _REGION_RO                (2U << _REGION_AP_BIT)
+#define _REGION_PRESENT           (1U << _REGION_PRESENT_BIT)
+#define REGION_AI_MASK(x)         (((x) >> _REGION_AI_BIT) & 0x7U)
+#define REGION_XN_MASK(x)         (((x) >> _REGION_XN_BIT) & 0x3U)
+#define REGION_AP_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x3U)
+#define REGION_RO_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x2U)
+
+/*
+ * _REGION_NORMAL is convenience define. It is not meant to be used
+ * outside of this header.
+ */
+#define _REGION_NORMAL            (MT_NORMAL|_REGION_PRESENT)
+
+#define REGION_HYPERVISOR_RW      (_REGION_NORMAL|_REGION_XN)
+#define REGION_HYPERVISOR_RO      (_REGION_NORMAL|_REGION_XN|_REGION_RO)
+
+#define REGION_HYPERVISOR         REGION_HYPERVISOR_RW
+
+#define INVALID_REGION            (~0UL)
+
 #ifndef __ASSEMBLY__
 
 /* Protection Region Base Address Register */
@@ -49,6 +104,23 @@ typedef struct {
     prlar_t prlar;
 } pr_t;
 
+/* Access to set base address of MPU protection region(pr_t). */
+#define pr_set_base(pr, paddr) ({                           \
+    pr_t *_pr = pr;                                         \
+    _pr->prbar.reg.base = (paddr >> MPU_REGION_SHIFT);      \
+})
+
+/* Access to set limit address of MPU protection region(pr_t). */
+#define pr_set_limit(pr, paddr) ({                          \
+    pr_t *_pr = pr;                                         \
+    _pr->prlar.reg.limit = (paddr >> MPU_REGION_SHIFT);     \
+})
+
+#define region_is_valid(pr) ({                              \
+    pr_t *_pr = pr;                                         \
+    _pr->prlar.reg.en;                                      \
+})
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* __ARM64_MPU_H__ */
xen/arch/arm/mm_mpu.c +268 -8

309 lines not on this page · raw message

Signature · 1 line
2.25.1
#79 lore raw

Re: [PATCH v2 19/40] xen/mpu: populate a new region in Xen MPU mapping table

Hi,

Quoted 104 lines · Penny Zheng

The new helper xen_mpumap_update() is responsible for updating an entry in Xen MPU memory mapping table, including creating a new entry, updating or destroying an existing one.

This commit only talks about populating a new entry in Xen MPU mapping table( xen_mpumap). Others will be introduced in the following commits.

In xen_mpumap_update_entry(), firstly, we shall check if requested address range [base, limit) is not mapped. Then we use pr_of_xenaddr() to build up the structure of MPU memory region(pr_t).
In the last, we set memory attribute and permission based on variable @flags.

To summarize all region attributes in one variable @flags, layout of the flags is elaborated as follows:
[0:2] Memory attribute Index
[3:4] Execute Never
[5:6] Access Permission
[7] Region Present
Also, we provide a set of definitions(REGION_HYPERVISOR_RW, etc) that combine the memory attribute and permission for common combinations.

---
  xen/arch/arm/include/asm/arm64/mpu.h |  72 +++++++
  xen/arch/arm/mm_mpu.c                | 276 ++++++++++++++++++++++++++-
  2 files changed, 340 insertions(+), 8 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +61 -0
index c945dd53db..fcde6ad0db 100644
@@ -16,6 +16,61 @@
   */
  #define ARM_MAX_MPU_MEMORY_REGIONS 255
 
+/* Access permission attributes. */
+/* Read/Write at EL2, No Access at EL1/EL0. */
+#define AP_RW_EL2 0x0
+/* Read/Write at EL2/EL1/EL0 all levels. */
+#define AP_RW_ALL 0x1
+/* Read-only at EL2, No Access at EL1/EL0. */
+#define AP_RO_EL2 0x2
+/* Read-only at EL2/EL1/EL0 all levels. */
+#define AP_RO_ALL 0x3
+
+/*
+ * Excute never.
+ * Stage 1 EL2 translation regime.
+ * XN[1] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ * Stage 2 EL1/EL0 translation regime.
+ * XN[0] determines whether execution of the instruction fetched from the MPU
+ * memory region is permitted.
+ */
+#define XN_DISABLED    0x0
+#define XN_P2M_ENABLED 0x1
+#define XN_ENABLED     0x2
+
+/*
+ * Layout of the flags used for updating Xen MPU region attributes
+ * [0:2] Memory attribute Index
+ * [3:4] Execute Never
+ * [5:6] Access Permission
+ * [7]   Region Present
+ */
+#define _REGION_AI_BIT            0
+#define _REGION_XN_BIT            3
+#define _REGION_AP_BIT            5
+#define _REGION_PRESENT_BIT       7
+#define _REGION_XN                (2U << _REGION_XN_BIT)
+#define _REGION_RO                (2U << _REGION_AP_BIT)
+#define _REGION_PRESENT           (1U << _REGION_PRESENT_BIT)
+#define REGION_AI_MASK(x)         (((x) >> _REGION_AI_BIT) & 0x7U)
+#define REGION_XN_MASK(x)         (((x) >> _REGION_XN_BIT) & 0x3U)
+#define REGION_AP_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x3U)
+#define REGION_RO_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x2U)
+
+/*
+ * _REGION_NORMAL is convenience define. It is not meant to be used
+ * outside of this header.
+ */
+#define _REGION_NORMAL            (MT_NORMAL|_REGION_PRESENT)
+
+#define REGION_HYPERVISOR_RW      (_REGION_NORMAL|_REGION_XN)
+#define REGION_HYPERVISOR_RO      (_REGION_NORMAL|_REGION_XN|_REGION_RO)
+
+#define REGION_HYPERVISOR         REGION_HYPERVISOR_RW
+
+#define INVALID_REGION            (~0UL)
+
  #ifndef __ASSEMBLY__
 
  /* Protection Region Base Address Register */
@@ -49,6 +104,23 @@ typedef struct {
      prlar_t prlar;
  } pr_t;
 
+/* Access to set base address of MPU protection region(pr_t). */
+#define pr_set_base(pr, paddr) ({                           \
+    pr_t *_pr = pr;                                         \
+    _pr->prbar.reg.base = (paddr >> MPU_REGION_SHIFT);      \
+})
+

xen/arch/arm/include/asm/arm64/mpu.h typedef struct { · line 122

Quoted 10 lines
xen/arch/arm/include/asm/arm64/mpu.h +10 -0
+/* Access to set limit address of MPU protection region(pr_t). */
+#define pr_set_limit(pr, paddr) ({                          \
+    pr_t *_pr = pr;                                         \
+    _pr->prlar.reg.limit = (paddr >> MPU_REGION_SHIFT);     \
+})
+
+#define region_is_valid(pr) ({                              \
+    pr_t *_pr = pr;                                         \
+    _pr->prlar.reg.en;                                      \
+})

Can they all be implemented using static inline?

Quoted 160 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
+
  #endif /* __ASSEMBLY__ */
 
  #endif /* __ARM64_MPU_H__ */
xen/arch/arm/mm_mpu.c +124 -0
index f2b494449c..08720a7c19 100644
@@ -22,9 +22,23 @@
  #include <xen/init.h>
  #include <xen/mm.h>
  #include <xen/page-size.h>
+#include <xen/spinlock.h>
  #include <asm/arm64/mpu.h>
  #include <asm/page.h>
 
+#ifdef NDEBUG
+static inline void
+__attribute__ ((__format__ (__printf__, 1, 2)))
+region_printk(const char *fmt, ...) {}
+#else
+#define region_printk(fmt, args...)         \
+    do                                      \
+    {                                       \
+        dprintk(XENLOG_ERR, fmt, ## args);  \
+        WARN();                             \
+    } while (0)
+#endif
+
  /* Xen MPU memory region mapping table. */
  pr_t __aligned(PAGE_SIZE) __section(".data.page_aligned")
       xen_mpumap[ARM_MAX_MPU_MEMORY_REGIONS];
@@ -46,6 +60,8 @@ uint64_t __ro_after_init next_transient_region_idx;
  /* Maximum number of supported MPU memory regions by the EL2 MPU. */
  uint64_t __ro_after_init max_xen_mpumap;
 
+static DEFINE_SPINLOCK(xen_mpumap_lock);
+
  /* Write a MPU protection region */
  #define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
      uint64_t _sel = sel;                                                \
@@ -73,6 +89,28 @@ uint64_t __ro_after_init max_xen_mpumap;
      _pr;                                                                \
  })
 
+/*
+ * In boot-time, fixed MPU regions(e.g. Xen text section) are added
+ * at the front, indexed by next_fixed_region_idx, the others like
+ * boot-only regions(e.g. early FDT) should be added at the rear,
+ * indexed by next_transient_region_idx.
+ * With more and more MPU regions added-in, when the two indexes
+ * meet and pass with each other, we would run out of the whole
+ * EL2 MPU memory regions.
+ */
+static bool __init xen_boot_mpu_regions_is_full(void)
+{
+    return next_transient_region_idx < next_fixed_region_idx;
+}
+
+static void __init update_boot_xen_mpumap_idx(uint64_t idx)
+{
+    if ( idx == next_transient_region_idx )
+        next_transient_region_idx--;
+    else
+        next_fixed_region_idx++;
+}
+
  /*
   * Access MPU protection region, including both read/write operations.
   * Armv8-R AArch64 at most supports 255 MPU protection regions.
@@ -197,6 +235,236 @@ static void access_protection_region(bool read, pr_t *pr_read,
      }
  }
 
+/*
+ * Standard entry for building up the structure of MPU memory region(pr_t).
+ * It is equivalent to mfn_to_xen_entry in MMU system.
+ * base and limit both refer to inclusive address.
+ */
+static inline pr_t pr_of_xenaddr(paddr_t base, paddr_t limit, unsigned attr)
+{
+    prbar_t prbar;
+    prlar_t prlar;
+    pr_t region;
+
+    /* Build up value for PRBAR_EL2. */
+    prbar = (prbar_t) {
+        .reg = {
+            .ap = AP_RW_EL2,  /* Read/Write at EL2, no access at EL1/EL0. */
+            .xn = XN_ENABLED, /* No need to execute outside .text */
+        }};
+
+    switch ( attr )
+    {
+    case MT_NORMAL_NC:
+        /*
+         * ARM ARM: Overlaying the shareability attribute (DDI
+         * 0406C.b B3-1376 to 1377)
+         *
+         * A memory region with a resultant memory type attribute of normal,
+         * and a resultant cacheability attribute of Inner non-cacheable,
+         * outer non-cacheable, must have a resultant shareability attribute
+         * of outer shareable, otherwise shareability is UNPREDICTABLE.
+         *
+         * On ARMv8 sharability is ignored and explicitly treated as outer
+         * shareable for normal inner non-cacheable, outer non-cacheable.
+         */
+        prbar.reg.sh = LPAE_SH_OUTER;
+        break;
+    case MT_DEVICE_nGnRnE:
+    case MT_DEVICE_nGnRE:
+        /*
+         * Shareability is ignored for non-normal memory, Outer is as
+         * good as anything.
+         *
+         * On ARMv8 sharability is ignored and explicitly treated as outer
+         * shareable for any device memory type.
+         */
+        prbar.reg.sh = LPAE_SH_OUTER;
+        break;
+    default:
+        /* Xen mappings are SMP coherent */
+        prbar.reg.sh = LPAE_SH_INNER;
+        break;
+    }
+
+    /* Build up value for PRLAR_EL2. */
+    prlar = (prlar_t) {
+        .reg = {
+            .ns = 0,        /* Hyp mode is in secure world */
+            .ai = attr,
+            .en = 1,        /* Region enabled */
+        }};
+
+    /* Build up MPU memory region. */
+    region = (pr_t) {
+        .prbar = prbar,
+        .prlar = prlar,
+    };
+
+    /* Set base address and limit address. */
+    pr_set_base(&region, base);
+    pr_set_limit(&region, limit);
+
+    return region;
+}
+
+#define MPUMAP_REGION_FAILED    0
+#define MPUMAP_REGION_FOUND     1
+#define MPUMAP_REGION_INCLUSIVE 2
+#define MPUMAP_REGION_OVERLAP   3
+
+/*
+ * Check whether memory range [base, limit] is mapped in MPU memory
+ * region table \mpu. Only address range is considered, memory attributes
+ * and permission are not considered here.
+ * If we find the match, the associated index will be filled up.
+ * If the entry is not present, INVALID_REGION will be set in \index
+ *
+ * Make sure that parameter \base and \limit are both referring

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read, · line 333

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+ * inclusive addresss
+ *
+ * Return values:
+ *  MPUMAP_REGION_FAILED: no mapping and no overmapping
+ *  MPUMAP_REGION_FOUND: find an exact match in address
+ *  MPUMAP_REGION_INCLUSIVE: find an inclusive match in address
+ *  MPUMAP_REGION_OVERLAP: overlap with the existing mapping
+ */
+static int mpumap_contain_region(pr_t *mpu, uint64_t nr_regions,
+                                 paddr_t base, paddr_t limit, uint64_t *index)

Is it really possible to support 2^64 - 1 region? If so, is that the case on arm32 as well?

Quoted 139 lines
xen/arch/arm/mm_mpu.c +134 -0
+{
+    uint64_t i = 0;
+    uint64_t _index = INVALID_REGION;
+
+    /* Allow index to be NULL */
+    index = index ?: &_index;
+
+    for ( ; i < nr_regions; i++ )
+    {
+        paddr_t iter_base = pr_get_base(&mpu[i]);
+        paddr_t iter_limit = pr_get_limit(&mpu[i]);
+
+        /* Found an exact valid match */
+        if ( (iter_base == base) && (iter_limit == limit) &&
+             region_is_valid(&mpu[i]) )
+        {
+            *index = i;
+            return MPUMAP_REGION_FOUND;
+        }
+
+        /* No overlapping */
+        if ( (iter_limit < base) || (iter_base > limit) )
+            continue;
+        /* Inclusive and valid */
+        else if ( (base >= iter_base) && (limit <= iter_limit) &&
+                  region_is_valid(&mpu[i]) )
+        {
+            *index = i;
+            return MPUMAP_REGION_INCLUSIVE;
+        }
+        else
+        {
+            region_printk("Range 0x%"PRIpaddr" - 0x%"PRIpaddr" overlaps with the existing region 0x%"PRIpaddr" - 0x%"PRIpaddr"\n",
+                          base, limit, iter_base, iter_limit);
+            return MPUMAP_REGION_OVERLAP;
+        }
+    }
+
+    return MPUMAP_REGION_FAILED;
+}
+
+/*
+ * Update an entry at the index @idx.
+ * @base:  base address
+ * @limit: limit address(exclusive)
+ * @flags: region attributes, should be the combination of REGION_HYPERVISOR_xx
+ */
+static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
+                                   unsigned int flags)
+{
+    uint64_t idx;
+    int rc;
+
+    rc = mpumap_contain_region(xen_mpumap, max_xen_mpumap, base, limit - 1,
+                               &idx);
+    if ( rc == MPUMAP_REGION_OVERLAP )
+        return -EINVAL;
+
+    /* We are inserting a mapping => Create new region. */
+    if ( flags & _REGION_PRESENT )
+    {
+        if ( rc != MPUMAP_REGION_FAILED )
+            return -EINVAL;
+
+        if ( xen_boot_mpu_regions_is_full() )
+        {
+            region_printk("There is no room left in EL2 MPU memory region mapping\n");
+            return -ENOMEM;
+        }
+
+        /* During boot time, the default index is next_fixed_region_idx. */
+        if ( system_state <= SYS_STATE_active )
+            idx = next_fixed_region_idx;
+
+        xen_mpumap[idx] = pr_of_xenaddr(base, limit - 1, REGION_AI_MASK(flags));
+        /* Set permission */
+        xen_mpumap[idx].prbar.reg.ap = REGION_AP_MASK(flags);
+        xen_mpumap[idx].prbar.reg.xn = REGION_XN_MASK(flags);
+
+        /* Update and enable the region */
+        access_protection_region(false, NULL, (const pr_t*)(&xen_mpumap[idx]),
+                                 idx);
+
+        if ( system_state <= SYS_STATE_active )
+            update_boot_xen_mpumap_idx(idx);
+    }
+
+    return 0;
+}
+
+static int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned int flags)
+{
+    int rc;
+
+    /*
+     * The hardware was configured to forbid mapping both writeable and
+     * executable.
+     * When modifying/creating mapping (i.e _REGION_PRESENT is set),
+     * prevent any update if this happen.
+     */
+    if ( (flags & _REGION_PRESENT) && !REGION_RO_MASK(flags) &&
+         !REGION_XN_MASK(flags) )
+    {
+        region_printk("Mappings should not be both Writeable and Executable.\n");
+        return -EINVAL;
+    }
+
+    if ( !IS_ALIGNED(base, PAGE_SIZE) || !IS_ALIGNED(limit, PAGE_SIZE) )
+    {
+        region_printk("base address 0x%"PRIpaddr", or limit address 0x%"PRIpaddr" is not page aligned.\n",
+                      base, limit);
+        return -EINVAL;
+    }
+
+    spin_lock(&xen_mpumap_lock);
+
+    rc = xen_mpumap_update_entry(base, limit, flags);
+
+    spin_unlock(&xen_mpumap_lock);
+
+    return rc;
+}
+
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)
+{
+    ASSERT(virt == mfn_to_maddr(mfn));
+
+    return xen_mpumap_update(mfn_to_maddr(mfn),
+                             mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
+}
+
  /* TODO: Implementation on the first usage */
  void dump_hyp_walk(vaddr_t addr)
  {
@@ -230,14 +498,6 @@ void *ioremap(paddr_t pa, size_t len)
      return NULL;

xen/arch/arm/mm_mpu.c void *ioremap(paddr_t pa, size_t len) · line 240

Quoted 10 lines
xen/arch/arm/mm_mpu.c +0 -8
  }
 
-int map_pages_to_xen(unsigned long virt,
-                     mfn_t mfn,
-                     unsigned long nr_mfns,
-                     unsigned int flags)
-{
-    return -ENOSYS;
-}
-

Why do you implement map_pages_to_xen() at a different place?

xen/arch/arm/mm_mpu.c void *ioremap(paddr_t pa, size_t len) · line 503

Quoted 3 lines
xen/arch/arm/mm_mpu.c +0 -0
  int destroy_xen_mappings(unsigned long s, unsigned long e)
  {
      return -ENOSYS;

Cheers,

Signature · 1 line
Julien Grall
#80 lore raw

RE: [PATCH v2 19/40] xen/mpu: populate a new region in Xen MPU mapping table

Hi Julien

Quoted 42 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, February 6, 2023 5:46 AM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 19/40] xen/mpu: populate a new region in Xen MPU mapping table

Hi,

Quoted 11 lines · Penny Zheng

The new helper xen_mpumap_update() is responsible for updating an entry in Xen MPU memory mapping table, including creating a new entry, updating or destroying an existing one.

This commit only talks about populating a new entry in Xen MPU mapping table( xen_mpumap). Others will be introduced in the following commits.

In xen_mpumap_update_entry(), firstly, we shall check if requested address range [base, limit) is not mapped. Then we use pr_of_xenaddr() to build up the structure of MPU memory region(pr_t).
In the last, we set memory attribute and permission based on variable

@flags.

Quoted 17 lines

To summarize all region attributes in one variable @flags, layout of the flags is elaborated as follows:
[0:2] Memory attribute Index
[3:4] Execute Never
[5:6] Access Permission
[7] Region Present
Also, we provide a set of definitions(REGION_HYPERVISOR_RW, etc) that combine the memory attribute and permission for common combinations.

---
  xen/arch/arm/include/asm/arm64/mpu.h |  72 +++++++
  xen/arch/arm/mm_mpu.c                | 276 ++++++++++++++++++++++++++-
  2 files changed, 340 insertions(+), 8 deletions(-)

[...]

Quoted 20 lines
Quoted 20 lines
xen/arch/arm/mm_mpu.c +20 -0
+
+#define MPUMAP_REGION_FAILED    0
+#define MPUMAP_REGION_FOUND     1
+#define MPUMAP_REGION_INCLUSIVE 2
+#define MPUMAP_REGION_OVERLAP   3
+
+/*
+ * Check whether memory range [base, limit] is mapped in MPU memory
+ * region table \mpu. Only address range is considered, memory
+attributes
+ * and permission are not considered here.
+ * If we find the match, the associated index will be filled up.
+ * If the entry is not present, INVALID_REGION will be set in \index
+ *
+ * Make sure that parameter \base and \limit are both referring
+ * inclusive addresss
+ *
+ * Return values:
+ *  MPUMAP_REGION_FAILED: no mapping and no overmapping
+ *  MPUMAP_REGION_FOUND: find an exact match in address
Quoted 9 lines

Review

Quoted 5 lines

+ * MPUMAP_REGION_INCLUSIVE: find an inclusive match in address
+ * MPUMAP_REGION_OVERLAP: overlap with the existing mapping */

(quoted diff) +3 -0
+static int mpumap_contain_region(pr_t *mpu, uint64_t nr_regions,
+                                 paddr_t base, paddr_t limit,
+uint64_t *index)

Is it really possible to support 2^64 - 1 region? If so, is that the case on arm32 as well?

No, the allowable bitwidth is 8 bit. I'll change it uint8_t instead

Quoted 167 lines
Quoted 23 lines
xen/arch/arm/mm_mpu.c +23 -0
+{
+    uint64_t i = 0;
+    uint64_t _index = INVALID_REGION;
+
+    /* Allow index to be NULL */
+    index = index ?: &_index;
+
+    for ( ; i < nr_regions; i++ )
+    {
+        paddr_t iter_base = pr_get_base(&mpu[i]);
+        paddr_t iter_limit = pr_get_limit(&mpu[i]);
+
+        /* Found an exact valid match */
+        if ( (iter_base == base) && (iter_limit == limit) &&
+             region_is_valid(&mpu[i]) )
+        {
+            *index = i;
+            return MPUMAP_REGION_FOUND;
+        }
+
+        /* No overlapping */
+        if ( (iter_limit < base) || (iter_base > limit) )
+            continue;

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+        /* Inclusive and valid */
+        else if ( (base >= iter_base) && (limit <= iter_limit) &&
+                  region_is_valid(&mpu[i]) )
+        {
+            *index = i;
+            return MPUMAP_REGION_INCLUSIVE;
+        }
+        else
+        {
+            region_printk("Range 0x%"PRIpaddr" - 0x%"PRIpaddr" overlaps

with the existing region 0x%"PRIpaddr" - 0x%"PRIpaddr"\n",

Quoted 4 lines
xen/arch/arm/mm_mpu.c +4 -0
+                          base, limit, iter_base, iter_limit);
+            return MPUMAP_REGION_OVERLAP;
+        }
+    }

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+
+    return MPUMAP_REGION_FAILED;
+}
+
+/*
+ * Update an entry at the index @idx.
+ * @base:  base address
+ * @limit: limit address(exclusive)
+ * @flags: region attributes, should be the combination of
+REGION_HYPERVISOR_xx  */ static int

xen_mpumap_update_entry(paddr_t

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 6 lines
xen/arch/arm/mm_mpu.c +6 -0
+base, paddr_t limit,
+                                   unsigned int flags) {
+    uint64_t idx;
+    int rc;
+
+    rc = mpumap_contain_region(xen_mpumap, max_xen_mpumap, base,

limit - 1,

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 13 lines
xen/arch/arm/mm_mpu.c +13 -0
+                               &idx);
+    if ( rc == MPUMAP_REGION_OVERLAP )
+        return -EINVAL;
+
+    /* We are inserting a mapping => Create new region. */
+    if ( flags & _REGION_PRESENT )
+    {
+        if ( rc != MPUMAP_REGION_FAILED )
+            return -EINVAL;
+
+        if ( xen_boot_mpu_regions_is_full() )
+        {
+            region_printk("There is no room left in EL2 MPU memory region

mapping\n");

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 8 lines
xen/arch/arm/mm_mpu.c +8 -0
+            return -ENOMEM;
+        }
+
+        /* During boot time, the default index is next_fixed_region_idx. */
+        if ( system_state <= SYS_STATE_active )
+            idx = next_fixed_region_idx;
+
+        xen_mpumap[idx] = pr_of_xenaddr(base, limit - 1,

REGION_AI_MASK(flags));

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 6 lines
xen/arch/arm/mm_mpu.c +6 -0
+        /* Set permission */
+        xen_mpumap[idx].prbar.reg.ap = REGION_AP_MASK(flags);
+        xen_mpumap[idx].prbar.reg.xn = REGION_XN_MASK(flags);
+
+        /* Update and enable the region */
+        access_protection_region(false, NULL, (const

pr_t*)(&xen_mpumap[idx]),

Quoted 13 lines
xen/arch/arm/mm_mpu.c +13 -0
+                                 idx);
+
+        if ( system_state <= SYS_STATE_active )
+            update_boot_xen_mpumap_idx(idx);
+    }
+
+    return 0;
+}
+
+static int xen_mpumap_update(paddr_t base, paddr_t limit, unsigned
+int flags) {
+    int rc;
+

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+    /*
+     * The hardware was configured to forbid mapping both writeable and
+     * executable.
+     * When modifying/creating mapping (i.e _REGION_PRESENT is set),
+     * prevent any update if this happen.
+     */
+    if ( (flags & _REGION_PRESENT) && !REGION_RO_MASK(flags) &&
+         !REGION_XN_MASK(flags) )
+    {
+        region_printk("Mappings should not be both Writeable and

Executable.\n");

xen/arch/arm/mm_mpu.c static void access_protection_region(bool read, pr_t *pr_read,

Quoted 6 lines
xen/arch/arm/mm_mpu.c +6 -0
+        return -EINVAL;
+    }
+
+    if ( !IS_ALIGNED(base, PAGE_SIZE) || !IS_ALIGNED(limit, PAGE_SIZE) )
+    {
+        region_printk("base address 0x%"PRIpaddr", or limit address

0x%"PRIpaddr" is not page aligned.\n",

Quoted 28 lines
xen/arch/arm/mm_mpu.c +23 -0
+                      base, limit);
+        return -EINVAL;
+    }
+
+    spin_lock(&xen_mpumap_lock);
+
+    rc = xen_mpumap_update_entry(base, limit, flags);
+
+    spin_unlock(&xen_mpumap_lock);
+
+    return rc;
+}
+
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags) {
+    ASSERT(virt == mfn_to_maddr(mfn));
+
+    return xen_mpumap_update(mfn_to_maddr(mfn),
+                             mfn_to_maddr(mfn_add(mfn, nr_mfns)),
+flags); }
+
  /* TODO: Implementation on the first usage */
  void dump_hyp_walk(vaddr_t addr)
  {
@@ -230,14 +498,6 @@ void *ioremap(paddr_t pa, size_t len)
      return NULL;

xen/arch/arm/mm_mpu.c void *ioremap(paddr_t pa, size_t len) · line 240

Quoted 10 lines
xen/arch/arm/mm_mpu.c +0 -8
  }
 
-int map_pages_to_xen(unsigned long virt,
-                     mfn_t mfn,
-                     unsigned long nr_mfns,
-                     unsigned int flags)
-{
-    return -ENOSYS;
-}
-

Why do you implement map_pages_to_xen() at a different place?

xen/arch/arm/mm_mpu.c void *ioremap(paddr_t pa, size_t len) · line 503

Quoted 3 lines
xen/arch/arm/mm_mpu.c +0 -0
  int destroy_xen_mappings(unsigned long s, unsigned long e)
  {
      return -ENOSYS;

Cheers,

--
Julien Grall

#81 lore raw

PATCH v2 20/40 xen/mpu: plump early_fdt_map in MPU systems

In reply to Penny Zheng

In MPU system, device tree binary can be packed with Xen image through CONFIG_DTB_FILE, or provided by bootloader through x0.

In MPU system, each section in xen.lds.S is PAGE_SIZE aligned. So in order to not overlap with the previous BSS section, dtb section should be made page-aligned too.
We add . = ALIGN(PAGE_SIZE); in the head of dtb section to make it happen.

In this commit, we map early FDT with a transient MPU memory region at rear with REGION_HYPERVISOR_BOOT.

---
 xen/arch/arm/include/asm/arm64/mpu.h |  5 +++
 xen/arch/arm/mm_mpu.c                | 63 +++++++++++++++++++++++++---
 xen/arch/arm/xen.lds.S               |  5 ++-
 3 files changed, 67 insertions(+), 6 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +5 -0
index fcde6ad0db..b85e420a90 100644
@@ -45,18 +45,22 @@
  * [3:4] Execute Never
  * [5:6] Access Permission
  * [7]   Region Present
+ * [8]   Boot-only Region
  */
 #define _REGION_AI_BIT            0
 #define _REGION_XN_BIT            3
 #define _REGION_AP_BIT            5
 #define _REGION_PRESENT_BIT       7
+#define _REGION_BOOTONLY_BIT      8
 #define _REGION_XN                (2U << _REGION_XN_BIT)
 #define _REGION_RO                (2U << _REGION_AP_BIT)
 #define _REGION_PRESENT           (1U << _REGION_PRESENT_BIT)
+#define _REGION_BOOTONLY          (1U << _REGION_BOOTONLY_BIT)
 #define REGION_AI_MASK(x)         (((x) >> _REGION_AI_BIT) & 0x7U)
 #define REGION_XN_MASK(x)         (((x) >> _REGION_XN_BIT) & 0x3U)
 #define REGION_AP_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x3U)
 #define REGION_RO_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x2U)
+#define REGION_BOOTONLY_MASK(x)   (((x) >> _REGION_BOOTONLY_BIT) & 0x1U)
 
 /*
  * _REGION_NORMAL is convenience define. It is not meant to be used
@@ -68,6 +72,7 @@
 #define REGION_HYPERVISOR_RO      (_REGION_NORMAL|_REGION_XN|_REGION_RO)
 
 #define REGION_HYPERVISOR         REGION_HYPERVISOR_RW
+#define REGION_HYPERVISOR_BOOT    (REGION_HYPERVISOR_RW|_REGION_BOOTONLY)
 
 #define INVALID_REGION            (~0UL)
 
xen/arch/arm/mm_mpu.c +58 -5

96 lines not on this page · raw message

xen/arch/arm/xen.lds.S +4 -1
index 79965a3c17..0565e22a1f 100644
@@ -218,7 +218,10 @@ SECTIONS
   _end = . ;
 
   /* Section for the device tree blob (if any). */
-  .dtb : { *(.dtb) } :text
+  .dtb : {
+      . = ALIGN(PAGE_SIZE);
+      *(.dtb)
+  } :text
 
   DWARF2_DEBUG_SECTIONS
 
Signature · 1 line
2.25.1
#82 lore raw

Re: [PATCH v2 20/40] xen/mpu: plump early_fdt_map in MPU systems

Hi,

Quoted 24 lines · Penny Zheng

In MPU system, device tree binary can be packed with Xen image through CONFIG_DTB_FILE, or provided by bootloader through x0.

In MPU system, each section in xen.lds.S is PAGE_SIZE aligned. So in order to not overlap with the previous BSS section, dtb section should be made page-aligned too.
We add . = ALIGN(PAGE_SIZE); in the head of dtb section to make it happen.

In this commit, we map early FDT with a transient MPU memory region at rear with REGION_HYPERVISOR_BOOT.

---
  xen/arch/arm/include/asm/arm64/mpu.h |  5 +++
  xen/arch/arm/mm_mpu.c                | 63 +++++++++++++++++++++++++---
  xen/arch/arm/xen.lds.S               |  5 ++-
  3 files changed, 67 insertions(+), 6 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
index fcde6ad0db..b85e420a90 100644

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

Quoted 10 lines
xen/arch/arm/include/asm/arm64/mpu.h +2 -0
@@ -45,18 +45,22 @@
   * [3:4] Execute Never
   * [5:6] Access Permission
   * [7]   Region Present
+ * [8]   Boot-only Region
   */
  #define _REGION_AI_BIT            0
  #define _REGION_XN_BIT            3
  #define _REGION_AP_BIT            5
  #define _REGION_PRESENT_BIT       7
+#define _REGION_BOOTONLY_BIT      8

In a follow-up patch, you are replacing BOOTONLY with TRANSIENT. Please avoid renaming new functions and instead introduce them with the correct name from the start.

Quoted 69 lines
xen/arch/arm/include/asm/arm64/mpu.h +3 -0
  #define _REGION_XN                (2U << _REGION_XN_BIT)
  #define _REGION_RO                (2U << _REGION_AP_BIT)
  #define _REGION_PRESENT           (1U << _REGION_PRESENT_BIT)
+#define _REGION_BOOTONLY          (1U << _REGION_BOOTONLY_BIT)
  #define REGION_AI_MASK(x)         (((x) >> _REGION_AI_BIT) & 0x7U)
  #define REGION_XN_MASK(x)         (((x) >> _REGION_XN_BIT) & 0x3U)
  #define REGION_AP_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x3U)
  #define REGION_RO_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x2U)
+#define REGION_BOOTONLY_MASK(x)   (((x) >> _REGION_BOOTONLY_BIT) & 0x1U)
 
  /*
   * _REGION_NORMAL is convenience define. It is not meant to be used
@@ -68,6 +72,7 @@
  #define REGION_HYPERVISOR_RO      (_REGION_NORMAL|_REGION_XN|_REGION_RO)
 
  #define REGION_HYPERVISOR         REGION_HYPERVISOR_RW
+#define REGION_HYPERVISOR_BOOT    (REGION_HYPERVISOR_RW|_REGION_BOOTONLY)
 
  #define INVALID_REGION            (~0UL)
 
xen/arch/arm/mm_mpu.c +17 -1
index 08720a7c19..b34dbf4515 100644
@@ -20,11 +20,16 @@
   */
 
  #include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
  #include <xen/mm.h>
  #include <xen/page-size.h>
+#include <xen/pfn.h>
+#include <xen/sizes.h>
  #include <xen/spinlock.h>
  #include <asm/arm64/mpu.h>
+#include <asm/early_printk.h>
  #include <asm/page.h>
+#include <asm/setup.h>
 
  #ifdef NDEBUG
  static inline void
@@ -62,6 +67,8 @@ uint64_t __ro_after_init max_xen_mpumap;
 
  static DEFINE_SPINLOCK(xen_mpumap_lock);
 
+static paddr_t dtb_paddr;
+
  /* Write a MPU protection region */
  #define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
      uint64_t _sel = sel;                                                \
@@ -403,7 +410,16 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
 
          /* During boot time, the default index is next_fixed_region_idx. */
          if ( system_state <= SYS_STATE_active )
-            idx = next_fixed_region_idx;
+        {
+            /*
+             * If it is a boot-only region (i.e. region for early FDT),
+             * it shall be added from the tail for late init re-organizing
+             */
+            if ( REGION_BOOTONLY_MASK(flags) )
+                idx = next_transient_region_idx;
+            else
+                idx = next_fixed_region_idx;
+        }
 
          xen_mpumap[idx] = pr_of_xenaddr(base, limit - 1, REGION_AI_MASK(flags));
          /* Set permission */

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 469

Quoted 5 lines
xen/arch/arm/mm_mpu.c +0 -2
@@ -465,14 +481,51 @@ int map_pages_to_xen(unsigned long virt,
                               mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
  }
 
-/* TODO: Implementation on the first usage */
-void dump_hyp_walk(vaddr_t addr) > +void * __init early_fdt_map(paddr_t fdt_paddr)

A fair amount of this code is the same as the MMU version. Can we share some code?

Quoted 32 lines
xen/arch/arm/mm_mpu.c +31 -0
  {
+    void *fdt_virt;
+    uint32_t size;
+
+    /*
+     * Check whether the physical FDT address is set and meets the minimum
+     * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be at
+     * least 8 bytes so that we always access the magic and size fields
+     * of the FDT header after mapping the first chunk, double check if
+     * that is indeed the case.
+     */
+     BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
+     if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN )
+         return NULL;
+
+    dtb_paddr = fdt_paddr;
+    /*
+     * In MPU system, device tree binary can be packed with Xen image
+     * through CONFIG_DTB_FILE, or provided by bootloader through x0.
+     * Map FDT with a transient MPU memory region of MAX_FDT_SIZE.
+     * After that, we can do some magic check.
+     */
+    if ( map_pages_to_xen(round_pgdown(fdt_paddr),
+                          maddr_to_mfn(round_pgdown(fdt_paddr)),
+                          round_pgup(MAX_FDT_SIZE) >> PAGE_SHIFT,
+                          REGION_HYPERVISOR_BOOT) )
+        panic("Unable to map the device-tree.\n");
+
+    /* VA == PA */
+    fdt_virt = maddr_to_virt(fdt_paddr);
+
+    if ( fdt_magic(fdt_virt) != FDT_MAGIC )

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 473

Quoted 10 lines
xen/arch/arm/mm_mpu.c +7 -1
+        return NULL;
+
+    size = fdt_totalsize(fdt_virt);
+    if ( size > MAX_FDT_SIZE )
+        return NULL;
+
+    return fdt_virt;
  }
 
-void * __init early_fdt_map(paddr_t fdt_paddr)

Same as the previous patch, why do you implmenet early_fdt_map() at a different place in the file?

Quoted 13 lines
xen/arch/arm/mm_mpu.c +2 -1
+/* TODO: Implementation on the first usage */
+void dump_hyp_walk(vaddr_t addr)
  {
-    return NULL;
  }
 
  void __init remove_early_mappings(void)
xen/arch/arm/xen.lds.S +0 -0
index 79965a3c17..0565e22a1f 100644
@@ -218,7 +218,10 @@ SECTIONS
    _end = . ;

xen/arch/arm/xen.lds.S SECTIONS · line 227

Quoted 10 lines
xen/arch/arm/xen.lds.S +4 -1
 
    /* Section for the device tree blob (if any). */
-  .dtb : { *(.dtb) } :text
+  .dtb : {
+      . = ALIGN(PAGE_SIZE);
+      *(.dtb)
+  } :text
 
    DWARF2_DEBUG_SECTIONS
 

Cheers,

Signature · 1 line
Julien Grall
#83 lore raw

Re: [PATCH v2 20/40] xen/mpu: plump early_fdt_map in MPU systems

In reply to Penny Zheng

Hi,

A few more remarks.

Quoted 118 lines · Penny Zheng

In MPU system, device tree binary can be packed with Xen image through CONFIG_DTB_FILE, or provided by bootloader through x0.

In MPU system, each section in xen.lds.S is PAGE_SIZE aligned. So in order to not overlap with the previous BSS section, dtb section should be made page-aligned too.
We add . = ALIGN(PAGE_SIZE); in the head of dtb section to make it happen.

In this commit, we map early FDT with a transient MPU memory region at rear with REGION_HYPERVISOR_BOOT.

---
  xen/arch/arm/include/asm/arm64/mpu.h |  5 +++
  xen/arch/arm/mm_mpu.c                | 63 +++++++++++++++++++++++++---
  xen/arch/arm/xen.lds.S               |  5 ++-
  3 files changed, 67 insertions(+), 6 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +5 -0
index fcde6ad0db..b85e420a90 100644
@@ -45,18 +45,22 @@
   * [3:4] Execute Never
   * [5:6] Access Permission
   * [7]   Region Present
+ * [8]   Boot-only Region
   */
  #define _REGION_AI_BIT            0
  #define _REGION_XN_BIT            3
  #define _REGION_AP_BIT            5
  #define _REGION_PRESENT_BIT       7
+#define _REGION_BOOTONLY_BIT      8
  #define _REGION_XN                (2U << _REGION_XN_BIT)
  #define _REGION_RO                (2U << _REGION_AP_BIT)
  #define _REGION_PRESENT           (1U << _REGION_PRESENT_BIT)
+#define _REGION_BOOTONLY          (1U << _REGION_BOOTONLY_BIT)
  #define REGION_AI_MASK(x)         (((x) >> _REGION_AI_BIT) & 0x7U)
  #define REGION_XN_MASK(x)         (((x) >> _REGION_XN_BIT) & 0x3U)
  #define REGION_AP_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x3U)
  #define REGION_RO_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x2U)
+#define REGION_BOOTONLY_MASK(x)   (((x) >> _REGION_BOOTONLY_BIT) & 0x1U)
 
  /*
   * _REGION_NORMAL is convenience define. It is not meant to be used
@@ -68,6 +72,7 @@
  #define REGION_HYPERVISOR_RO      (_REGION_NORMAL|_REGION_XN|_REGION_RO)
 
  #define REGION_HYPERVISOR         REGION_HYPERVISOR_RW
+#define REGION_HYPERVISOR_BOOT    (REGION_HYPERVISOR_RW|_REGION_BOOTONLY)
 
  #define INVALID_REGION            (~0UL)
 
xen/arch/arm/mm_mpu.c +26 -3
index 08720a7c19..b34dbf4515 100644
@@ -20,11 +20,16 @@
   */
 
  #include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
  #include <xen/mm.h>
  #include <xen/page-size.h>
+#include <xen/pfn.h>
+#include <xen/sizes.h>
  #include <xen/spinlock.h>
  #include <asm/arm64/mpu.h>
+#include <asm/early_printk.h>
  #include <asm/page.h>
+#include <asm/setup.h>
 
  #ifdef NDEBUG
  static inline void
@@ -62,6 +67,8 @@ uint64_t __ro_after_init max_xen_mpumap;
 
  static DEFINE_SPINLOCK(xen_mpumap_lock);
 
+static paddr_t dtb_paddr;
+
  /* Write a MPU protection region */
  #define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
      uint64_t _sel = sel;                                                \
@@ -403,7 +410,16 @@ static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,
 
          /* During boot time, the default index is next_fixed_region_idx. */
          if ( system_state <= SYS_STATE_active )
-            idx = next_fixed_region_idx;
+        {
+            /*
+             * If it is a boot-only region (i.e. region for early FDT),
+             * it shall be added from the tail for late init re-organizing
+             */
+            if ( REGION_BOOTONLY_MASK(flags) )
+                idx = next_transient_region_idx;
+            else
+                idx = next_fixed_region_idx;
+        }
 
          xen_mpumap[idx] = pr_of_xenaddr(base, limit - 1, REGION_AI_MASK(flags));
          /* Set permission */
@@ -465,14 +481,51 @@ int map_pages_to_xen(unsigned long virt,
                               mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
  }
 
-/* TODO: Implementation on the first usage */
-void dump_hyp_walk(vaddr_t addr)
+void * __init early_fdt_map(paddr_t fdt_paddr)
  {
+    void *fdt_virt;
+    uint32_t size;
+
+    /*
+     * Check whether the physical FDT address is set and meets the minimum
+     * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be at
+     * least 8 bytes so that we always access the magic and size fields
+     * of the FDT header after mapping the first chunk, double check if

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 503

Quoted 10 lines
xen/arch/arm/mm_mpu.c +10 -0
+     * that is indeed the case.
+     */
+     BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
+     if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN )
+         return NULL;
+
+    dtb_paddr = fdt_paddr;
+    /*
+     * In MPU system, device tree binary can be packed with Xen image
+     * through CONFIG_DTB_FILE, or provided by bootloader through x0.

The behavior you describe is not specific to the MPU system. I also don't quite understand how describing the method to pass the DT actually matters here.

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 507

Quoted 4 lines
xen/arch/arm/mm_mpu.c +4 -0
+     * Map FDT with a transient MPU memory region of MAX_FDT_SIZE.
+     * After that, we can do some magic check.
+     */
+    if ( map_pages_to_xen(round_pgdown(fdt_paddr),

I haven't looked at the rest of the series. But from here, it seems a bit strange to use map_pages_to_xen() because the virt and the phys should be the same.

Do you plan to share some code where map_pages_to_xen() will be used?

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 509

Quoted 2 lines
xen/arch/arm/mm_mpu.c +2 -0
+                          maddr_to_mfn(round_pgdown(fdt_paddr)),
+                          round_pgup(MAX_FDT_SIZE) >> PAGE_SHIFT,

This will not work properly is the Device-Tree is MAX_FDT_SIZE (could already be page-aligned) but the start address is not page-aligned.

But I think trying to map the maximum size from the start could potentially result to some issue. Below the excerpt from the Image documentation:

"The device tree blob (dtb) must be placed on an 8-byte boundary and must not exceed 2 megabytes in size. Since the dtb will be mapped cacheable using blocks of up to 2 megabytes in size, it must not be placed within any 2M region which must be mapped with any specific attributes."

So it would be better to map the first 2MB. Check the size and then re-map with an extra 2MB if needed.

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 513

Quoted 3 lines
xen/arch/arm/mm_mpu.c +3 -0
+                          REGION_HYPERVISOR_BOOT) ) > +        panic("Unable to map the device-tree.\n");
+
+    /* VA == PA */

I have seen in a few places where you add a similar comment. But I am not sure to understand how this help to describe the implementation of maddr_to_virt().

Quoted 26 lines
xen/arch/arm/mm_mpu.c +12 -2
+    fdt_virt = maddr_to_virt(fdt_paddr);
+
+    if ( fdt_magic(fdt_virt) != FDT_MAGIC )
+        return NULL;
+
+    size = fdt_totalsize(fdt_virt);
+    if ( size > MAX_FDT_SIZE )
+        return NULL;
+
+    return fdt_virt;
  }
 
-void * __init early_fdt_map(paddr_t fdt_paddr)
+/* TODO: Implementation on the first usage */
+void dump_hyp_walk(vaddr_t addr)
  {
-    return NULL;
  }
 
  void __init remove_early_mappings(void)
xen/arch/arm/xen.lds.S +0 -0
index 79965a3c17..0565e22a1f 100644
@@ -218,7 +218,10 @@ SECTIONS
    _end = . ;

xen/arch/arm/xen.lds.S SECTIONS · line 227

Quoted 10 lines
xen/arch/arm/xen.lds.S +4 -1
 
    /* Section for the device tree blob (if any). */
-  .dtb : { *(.dtb) } :text
+  .dtb : {
+      . = ALIGN(PAGE_SIZE);
+      *(.dtb)
+  } :text
 
    DWARF2_DEBUG_SECTIONS
 

Cheers,

Signature · 1 line
Julien Grall
#84 lore raw

RE: [PATCH v2 20/40] xen/mpu: plump early_fdt_map in MPU systems

Hi Julien

Quoted 157 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, February 6, 2023 6:11 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 20/40] xen/mpu: plump early_fdt_map in MPU systems

Hi,

A few more remarks.

Quoted 34 lines · Penny Zheng

In MPU system, device tree binary can be packed with Xen image through CONFIG_DTB_FILE, or provided by bootloader through x0.

In MPU system, each section in xen.lds.S is PAGE_SIZE aligned. So in order to not overlap with the previous BSS section, dtb section should be made page-aligned too.
We add . = ALIGN(PAGE_SIZE); in the head of dtb section to make it happen.

In this commit, we map early FDT with a transient MPU memory region at rear with REGION_HYPERVISOR_BOOT.

---
  xen/arch/arm/include/asm/arm64/mpu.h |  5 +++
  xen/arch/arm/mm_mpu.c                | 63 +++++++++++++++++++++++++---
  xen/arch/arm/xen.lds.S               |  5 ++-
  3 files changed, 67 insertions(+), 6 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
index fcde6ad0db..b85e420a90 100644
@@ -45,18 +45,22 @@
   * [3:4] Execute Never
   * [5:6] Access Permission
   * [7]   Region Present
+ * [8]   Boot-only Region
   */
  #define _REGION_AI_BIT            0
  #define _REGION_XN_BIT            3
  #define _REGION_AP_BIT            5
  #define _REGION_PRESENT_BIT       7

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

Quoted 10 lines
xen/arch/arm/include/asm/arm64/mpu.h +3 -0
+#define _REGION_BOOTONLY_BIT      8
  #define _REGION_XN                (2U << _REGION_XN_BIT)
  #define _REGION_RO                (2U << _REGION_AP_BIT)
  #define _REGION_PRESENT           (1U << _REGION_PRESENT_BIT)
+#define _REGION_BOOTONLY          (1U << _REGION_BOOTONLY_BIT)
  #define REGION_AI_MASK(x)         (((x) >> _REGION_AI_BIT) & 0x7U)
  #define REGION_XN_MASK(x)         (((x) >> _REGION_XN_BIT) & 0x3U)
  #define REGION_AP_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x3U)
  #define REGION_RO_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x2U)
+#define REGION_BOOTONLY_MASK(x)   (((x) >> _REGION_BOOTONLY_BIT)

& 0x1U)

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

Quoted 5 lines
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
  /*
   * _REGION_NORMAL is convenience define. It is not meant to be used
@@ -68,6 +72,7 @@
  #define REGION_HYPERVISOR_RO

(_REGION_NORMAL|_REGION_XN|_REGION_RO)

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

Quoted 3 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
  #define REGION_HYPERVISOR         REGION_HYPERVISOR_RW
+#define REGION_HYPERVISOR_BOOT

(REGION_HYPERVISOR_RW|_REGION_BOOTONLY)

xen/arch/arm/mm_mpu.c static int xen_mpumap_update_entry(paddr_t

Quoted 34 lines
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
  #define INVALID_REGION            (~0UL)
 
xen/arch/arm/mm_mpu.c index renamed from xen/arch/arm/mm_mpu.c +0 -0

08720a7c19..b34dbf4515 100644

xen/arch/arm/mm_mpu.c +7 -0
@@ -20,11 +20,16 @@
   */
 
  #include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
  #include <xen/mm.h>
  #include <xen/page-size.h>
+#include <xen/pfn.h>
+#include <xen/sizes.h>
  #include <xen/spinlock.h>
  #include <asm/arm64/mpu.h>
+#include <asm/early_printk.h>
  #include <asm/page.h>
+#include <asm/setup.h>
 
  #ifdef NDEBUG
  static inline void
@@ -62,6 +67,8 @@ uint64_t __ro_after_init max_xen_mpumap;
 
  static DEFINE_SPINLOCK(xen_mpumap_lock);
 
+static paddr_t dtb_paddr;
+
  /* Write a MPU protection region */
  #define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
      uint64_t _sel = sel;                                                \
@@ -403,7 +410,16 @@ static int xen_mpumap_update_entry(paddr_t

base,

Quoted 7 lines
xen/arch/arm/mm_mpu.c +2 -1
 paddr_t limit,
 
          /* During boot time, the default index is next_fixed_region_idx. */
          if ( system_state <= SYS_STATE_active )
-            idx = next_fixed_region_idx;
+        {
+            /*

xen/arch/arm/mm_mpu.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,

Quoted 10 lines
xen/arch/arm/mm_mpu.c +8 -0
+             * If it is a boot-only region (i.e. region for early FDT),
+             * it shall be added from the tail for late init re-organizing
+             */
+            if ( REGION_BOOTONLY_MASK(flags) )
+                idx = next_transient_region_idx;
+            else
+                idx = next_fixed_region_idx;
+        }
 
          xen_mpumap[idx] = pr_of_xenaddr(base, limit - 1,

REGION_AI_MASK(flags));

Quoted 4 lines
          /* Set permission */
xen/arch/arm/mm_mpu.c +0 -0
@@ -465,14 +481,51 @@ int map_pages_to_xen(unsigned long virt,
                               mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
  }

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 491

Quoted 10 lines
xen/arch/arm/mm_mpu.c +6 -1
 
-/* TODO: Implementation on the first usage */ -void
 dump_hyp_walk(vaddr_t addr)
+void * __init early_fdt_map(paddr_t fdt_paddr)
  {
+    void *fdt_virt;
+    uint32_t size;
+
+    /*
+     * Check whether the physical FDT address is set and meets the

minimum

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 492

Quoted 1 line
xen/arch/arm/mm_mpu.c +1 -0
+     * alignment requirement. Since we are relying on MIN_FDT_ALIGN to

be at

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 503

Quoted 12 lines
xen/arch/arm/mm_mpu.c +12 -0
+     * least 8 bytes so that we always access the magic and size fields
+     * of the FDT header after mapping the first chunk, double check if
+     * that is indeed the case.
+     */
+     BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
+     if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN )
+         return NULL;
+
+    dtb_paddr = fdt_paddr;
+    /*
+     * In MPU system, device tree binary can be packed with Xen image
+     * through CONFIG_DTB_FILE, or provided by bootloader through x0.

The behavior you describe is not specific to the MPU system. I also don't quite understand how describing the method to pass the DT actually matters here.

Quoted 9 lines

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 507

Quoted 4 lines
xen/arch/arm/mm_mpu.c +4 -0
+     * Map FDT with a transient MPU memory region of MAX_FDT_SIZE.
+     * After that, we can do some magic check.
+     */
+    if ( map_pages_to_xen(round_pgdown(fdt_paddr),

I haven't looked at the rest of the series. But from here, it seems a bit strange to use map_pages_to_xen() because the virt and the phys should be the same.

Hmm, t thought map_pages_to_xen, is to set up memory mapping for access. In MPU, we also need to set up a MPU memory region for the FDT, even without virt-to-phys conversion

Quoted 2 lines

Do you plan to share some code where map_pages_to_xen() will be used?

Each time, in C boot-time, we build up a new MPU memory region for stage 1 EL2 memory mapping, we use this map_pages_to_xen to complete.
I think it has the same effect as it has in MMU, other than MMU sets up virt-to-phys memory mapping and MPU always sets up identity memory mapping.

Quoted 10 lines

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 509

Quoted 2 lines
xen/arch/arm/mm_mpu.c +2 -0
+                          maddr_to_mfn(round_pgdown(fdt_paddr)),
+                          round_pgup(MAX_FDT_SIZE) >> PAGE_SHIFT,

This will not work properly is the Device-Tree is MAX_FDT_SIZE (could already be page-aligned) but the start address is not page-aligned.

But I think trying to map the maximum size from the start could potentially result to some issue. Below the excerpt from the Image documentation:

Quoted 8 lines

"The device tree blob (dtb) must be placed on an 8-byte boundary and must not exceed 2 megabytes in size. Since the dtb will be mapped cacheable using blocks of up to 2 megabytes in size, it must not be placed within any 2M region which must be mapped with any specific attributes."

So it would be better to map the first 2MB. Check the size and then re-map with an extra 2MB if needed.

Oh, under special circumstances, the current implementation will map exceeding 2MB. Thanks for explanation!
I will map as you suggested.

Quoted 50 lines

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 511

Quoted 1 line
xen/arch/arm/mm_mpu.c +1 -0
+                          REGION_HYPERVISOR_BOOT) ) > +        panic("Unable to

map the device-tree.\n");

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 513

Quoted 2 lines
xen/arch/arm/mm_mpu.c +2 -0
+
+    /* VA == PA */

I have seen in a few places where you add a similar comment. But I am not sure to understand how this help to describe the implementation of maddr_to_virt().

Quoted 26 lines
xen/arch/arm/mm_mpu.c +12 -2
+    fdt_virt = maddr_to_virt(fdt_paddr);
+
+    if ( fdt_magic(fdt_virt) != FDT_MAGIC )
+        return NULL;
+
+    size = fdt_totalsize(fdt_virt);
+    if ( size > MAX_FDT_SIZE )
+        return NULL;
+
+    return fdt_virt;
  }
 
-void * __init early_fdt_map(paddr_t fdt_paddr)
+/* TODO: Implementation on the first usage */ void
+dump_hyp_walk(vaddr_t addr)
  {
-    return NULL;
  }
 
  void __init remove_early_mappings(void) diff --git

a/xen/arch/arm/xen.lds.S b/xen/arch/arm/xen.lds.S index 79965a3c17..0565e22a1f 100644

xen/arch/arm/xen.lds.S +0 -0
@@ -218,7 +218,10 @@ SECTIONS
    _end = . ;

xen/arch/arm/xen.lds.S SECTIONS · line 227

Quoted 10 lines
xen/arch/arm/xen.lds.S +4 -1
 
    /* Section for the device tree blob (if any). */
-  .dtb : { *(.dtb) } :text
+  .dtb : {
+      . = ALIGN(PAGE_SIZE);
+      *(.dtb)
+  } :text
 
    DWARF2_DEBUG_SECTIONS
 

Cheers,

--
Julien Grall

#85 lore raw

Re: [PATCH v2 20/40] xen/mpu: plump early_fdt_map in MPU systems

Quoted 1 line · Penny Zheng

Hi Julien

Hi Penny,

Quoted 162 lines
Quoted 162 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: Monday, February 6, 2023 6:11 PM
To: Penny Zheng <[email protected]>; [email protected]
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: Re: [PATCH v2 20/40] xen/mpu: plump early_fdt_map in MPU systems

Hi,

A few more remarks.

Quoted 34 lines · Penny Zheng

In MPU system, device tree binary can be packed with Xen image through CONFIG_DTB_FILE, or provided by bootloader through x0.

In MPU system, each section in xen.lds.S is PAGE_SIZE aligned. So in order to not overlap with the previous BSS section, dtb section should be made page-aligned too.
We add . = ALIGN(PAGE_SIZE); in the head of dtb section to make it happen.

In this commit, we map early FDT with a transient MPU memory region at rear with REGION_HYPERVISOR_BOOT.

---
   xen/arch/arm/include/asm/arm64/mpu.h |  5 +++
   xen/arch/arm/mm_mpu.c                | 63 +++++++++++++++++++++++++---
   xen/arch/arm/xen.lds.S               |  5 ++-
   3 files changed, 67 insertions(+), 6 deletions(-)
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
index fcde6ad0db..b85e420a90 100644
@@ -45,18 +45,22 @@
    * [3:4] Execute Never
    * [5:6] Access Permission
    * [7]   Region Present
+ * [8]   Boot-only Region
    */
   #define _REGION_AI_BIT            0
   #define _REGION_XN_BIT            3
   #define _REGION_AP_BIT            5
   #define _REGION_PRESENT_BIT       7

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

Quoted 10 lines
xen/arch/arm/include/asm/arm64/mpu.h +3 -0
+#define _REGION_BOOTONLY_BIT      8
   #define _REGION_XN                (2U << _REGION_XN_BIT)
   #define _REGION_RO                (2U << _REGION_AP_BIT)
   #define _REGION_PRESENT           (1U << _REGION_PRESENT_BIT)
+#define _REGION_BOOTONLY          (1U << _REGION_BOOTONLY_BIT)
   #define REGION_AI_MASK(x)         (((x) >> _REGION_AI_BIT) & 0x7U)
   #define REGION_XN_MASK(x)         (((x) >> _REGION_XN_BIT) & 0x3U)
   #define REGION_AP_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x3U)
   #define REGION_RO_MASK(x)         (((x) >> _REGION_AP_BIT) & 0x2U)
+#define REGION_BOOTONLY_MASK(x)   (((x) >> _REGION_BOOTONLY_BIT)

& 0x1U)

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

Quoted 5 lines
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
   /*
    * _REGION_NORMAL is convenience define. It is not meant to be used
@@ -68,6 +72,7 @@
   #define REGION_HYPERVISOR_RO

(_REGION_NORMAL|_REGION_XN|_REGION_RO)

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

Quoted 3 lines
xen/arch/arm/include/asm/arm64/mpu.h +1 -0
   #define REGION_HYPERVISOR         REGION_HYPERVISOR_RW
+#define REGION_HYPERVISOR_BOOT

(REGION_HYPERVISOR_RW|_REGION_BOOTONLY)

xen/arch/arm/mm_mpu.c static int xen_mpumap_update_entry(paddr_t

Quoted 34 lines
xen/arch/arm/include/asm/arm64/mpu.h +0 -0
   #define INVALID_REGION            (~0UL)
 
xen/arch/arm/mm_mpu.c index renamed from xen/arch/arm/mm_mpu.c +0 -0

08720a7c19..b34dbf4515 100644

xen/arch/arm/mm_mpu.c +7 -0
@@ -20,11 +20,16 @@
    */
 
   #include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
   #include <xen/mm.h>
   #include <xen/page-size.h>
+#include <xen/pfn.h>
+#include <xen/sizes.h>
   #include <xen/spinlock.h>
   #include <asm/arm64/mpu.h>
+#include <asm/early_printk.h>
   #include <asm/page.h>
+#include <asm/setup.h>
 
   #ifdef NDEBUG
   static inline void
@@ -62,6 +67,8 @@ uint64_t __ro_after_init max_xen_mpumap;
 
   static DEFINE_SPINLOCK(xen_mpumap_lock);
 
+static paddr_t dtb_paddr;
+
   /* Write a MPU protection region */
   #define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
       uint64_t _sel = sel;                                                \
@@ -403,7 +410,16 @@ static int xen_mpumap_update_entry(paddr_t

base,

Quoted 7 lines
xen/arch/arm/mm_mpu.c +2 -1
 paddr_t limit,
 
           /* During boot time, the default index is next_fixed_region_idx. */
           if ( system_state <= SYS_STATE_active )
-            idx = next_fixed_region_idx;
+        {
+            /*

xen/arch/arm/mm_mpu.c static int xen_mpumap_update_entry(paddr_t base, paddr_t limit,

Quoted 10 lines
xen/arch/arm/mm_mpu.c +8 -0
+             * If it is a boot-only region (i.e. region for early FDT),
+             * it shall be added from the tail for late init re-organizing
+             */
+            if ( REGION_BOOTONLY_MASK(flags) )
+                idx = next_transient_region_idx;
+            else
+                idx = next_fixed_region_idx;
+        }
 
           xen_mpumap[idx] = pr_of_xenaddr(base, limit - 1,

REGION_AI_MASK(flags));

Quoted 4 lines
           /* Set permission */
xen/arch/arm/mm_mpu.c +0 -0
@@ -465,14 +481,51 @@ int map_pages_to_xen(unsigned long virt,
                                mfn_to_maddr(mfn_add(mfn, nr_mfns)), flags);
   }

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 491

Quoted 10 lines
xen/arch/arm/mm_mpu.c +6 -1
 
-/* TODO: Implementation on the first usage */ -void
 dump_hyp_walk(vaddr_t addr)
+void * __init early_fdt_map(paddr_t fdt_paddr)
   {
+    void *fdt_virt;
+    uint32_t size;
+
+    /*
+     * Check whether the physical FDT address is set and meets the

minimum

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 492

Quoted 1 line
xen/arch/arm/mm_mpu.c +1 -0
+     * alignment requirement. Since we are relying on MIN_FDT_ALIGN to

be at

xen/arch/arm/mm_mpu.c int map_pages_to_xen(unsigned long virt, · line 503

Quoted 12 lines
xen/arch/arm/mm_mpu.c +12 -0
+     * least 8 bytes so that we always access the magic and size fields
+     * of the FDT header after mapping the first chunk, double check if
+     * that is indeed the case.
+     */
+     BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
+     if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN )
+         return NULL;
+
+    dtb_paddr = fdt_paddr;
+    /*
+     * In MPU system, device tree binary can be packed with Xen image
+     * through CONFIG_DTB_FILE, or provided by bootloader through x0.

The behavior you describe is not specific to the MPU system. I also don't quite understand how describing the method to pass the DT actually matters here.

Quoted 4 lines
xen/arch/arm/mm_mpu.c +4 -0
+     * Map FDT with a transient MPU memory region of MAX_FDT_SIZE.
+     * After that, we can do some magic check.
+     */
+    if ( map_pages_to_xen(round_pgdown(fdt_paddr),
Quoted 8 lines
Quoted 4 lines

I haven't looked at the rest of the series. But from here, it seems a bit strange to use map_pages_to_xen() because the virt and the phys should be the same.

Hmm, t thought map_pages_to_xen, is to set up memory mapping for access. In MPU, we also need to set up a MPU memory region for the FDT, even without virt-to-phys conversion

I think my point was misunderstood. I agree that we need a function to update the MPU. Instead I was asking whether using map_pages_to_xen() rather than creating a new helper with an MPU specific would not be better so we don't have to pass a pointless parameter (virt). That's why...

Quoted 2 lines
Quoted 2 lines

Do you plan to share some code where map_pages_to_xen() will be used?

... I was asking if you were going to share code with the MMU that may end up to use this function.

If yes, then I agree in common code, it would be best to use map_pages_to_xen(). For MPU specific code, I would consider to provide an helper that doesn't need the virt to reduce the amount of unnecessary code.

Cheers,

Signature · 1 line
Julien Grall
#86 lore raw

PATCH v2 21/40 xen/arm: move MMU-specific setup_mm to setup_mmu.c

In reply to Penny Zheng

setup_mm is used for Xen to setup memory management subsystem, like boot allocator, direct-mapping, xenheap, frametable and static memory pages. We could inherit some components seamlessly in MPU system like boot allocator, whilst we need to implement some components differently in MPU, like xenheap, and some components could not be applied in MPU system, like direct-mapping.

In the commit, we move setup_mm and its related functions and variables to setup_mmu.c in preparation of implementing MPU version of setup_mm later in future commits

---
 xen/arch/arm/Makefile            |   3 +
 xen/arch/arm/include/asm/setup.h |   5 +
 xen/arch/arm/setup.c             | 326 +---------------------------
 xen/arch/arm/setup_mmu.c         | 350 +++++++++++++++++++++++++++++++
 4 files changed, 362 insertions(+), 322 deletions(-)
 create mode 100644 xen/arch/arm/setup_mmu.c
xen/arch/arm/Makefile +3 -0
index 21188b207f..adeb17b7ab 100644
@@ -51,6 +51,9 @@ obj-y += physdev.o
 obj-y += processor.o
 obj-y += psci.o
 obj-y += setup.o
+ifneq ($(CONFIG_HAS_MPU), y)
+obj-y += setup_mmu.o
+endif
 obj-y += shutdown.o
 obj-y += smp.o
 obj-y += smpboot.o
xen/arch/arm/include/asm/setup.h +5 -0
index 4f39a1aa0a..8f353b67f8 100644
@@ -158,6 +158,11 @@ struct bootcmdline *boot_cmdline_find_by_kind(bootmodule_kind kind);
 struct bootcmdline * boot_cmdline_find_by_name(const char *name);
 const char *boot_module_kind_as_string(bootmodule_kind kind);
 
+extern void init_pdx(void);
+extern void init_staticmem_pages(void);
+extern void populate_boot_allocator(void);
+extern void setup_mm(void);
+
 extern uint32_t hyp_traps_vector[];
 void init_traps(void);
 
xen/arch/arm/setup.c +4 -322

368 lines not on this page · raw message

xen/arch/arm/setup_mmu.c new file +350 -0

350 lines not on this page · raw message

Signature · 1 line
2.25.1
#87 lore raw

PATCH v2 22/40 xen/mpu: implement MPU version of setup_mm in setup_mpu.c

In reply to Penny Zheng

In MPU system, system RAM shall be statically partitioned into different functionality section in Device Tree at the very beginning, including static xenheap, guest memory section, boot-module section, etc. So using a virtual contigious memory region to do direct-mapping for the whole system RAM is not applicable in MPU system.

Function setup_static_mappings is introduced to set up MPU memory region mapping section by section based on static configuration in Device Tree.
And this commit is only responsible for static xenheap mapping, which is implemented in setup_staticheap_mappings. All the other static memory section mapping will be introduced later.

---
 xen/arch/arm/Makefile             |  2 +
 xen/arch/arm/include/asm/mm_mpu.h |  5 +++
 xen/arch/arm/mm_mpu.c             | 41 ++++++++++++++++++
 xen/arch/arm/setup_mpu.c          | 70 +++++++++++++++++++++++++++++++
 4 files changed, 118 insertions(+)
 create mode 100644 xen/arch/arm/setup_mpu.c
xen/arch/arm/Makefile +2 -0
index adeb17b7ab..23dfbc3333 100644
@@ -53,6 +53,8 @@ obj-y += psci.o
 obj-y += setup.o
 ifneq ($(CONFIG_HAS_MPU), y)
 obj-y += setup_mmu.o
+else
+obj-y += setup_mpu.o
 endif
 obj-y += shutdown.o
 obj-y += smp.o
xen/arch/arm/include/asm/mm_mpu.h +5 -0
index 3a4b07f187..fe6a828a50 100644
@@ -3,6 +3,11 @@
 #define __ARCH_ARM_MM_MPU__
 
 #define setup_mm_mappings(boot_phys_offset) ((void)(boot_phys_offset))
+/*
+ * Function setup_static_mappings() sets up MPU memory region mapping
+ * section by section based on static configuration in Device Tree.
+ */
+extern void setup_static_mappings(void);
 
 static inline paddr_t __virt_to_maddr(vaddr_t va)
 {
xen/arch/arm/mm_mpu.c +41 -0

47 lines not on this page · raw message

xen/arch/arm/setup_mpu.c new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.25.1
#88 lore raw

PATCH v2 23/40 xen/mpu: initialize frametable in MPU system

In reply to Penny Zheng

Xen is using page as the smallest granularity for memory managment. And we want to follow the same concept in MPU system.
That is, structure page_info and the frametable which is used for storing and managing page_info is also required in MPU system.

In MPU system, since there is no virtual address translation (VA == PA), we can not use a fixed VA address(FRAMETABLE_VIRT_START) to map frametable like MMU system does.
Instead, we define a variable "struct page_info *frame_table" as frametable pointer, and ask boot allocator to allocate memory for frametable.

As frametable is successfully initialized, the convertion between machine frame number/machine address/"virtual address" and page-info structure is ready too, like mfn_to_page/maddr_to_page/virt_to_page, etc

---
 xen/arch/arm/include/asm/mm.h     | 15 ---------------
 xen/arch/arm/include/asm/mm_mmu.h | 16 ++++++++++++++++
 xen/arch/arm/include/asm/mm_mpu.h | 17 +++++++++++++++++
 xen/arch/arm/mm_mpu.c             | 25 +++++++++++++++++++++++++
 4 files changed, 58 insertions(+), 15 deletions(-)
xen/arch/arm/include/asm/mm.h +0 -15
index e29158028a..7969ec9f98 100644
@@ -176,7 +176,6 @@ struct page_info
 
 #define maddr_get_owner(ma)   (page_get_owner(maddr_to_page((ma))))
 
-#define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)
 /* PDX of the first page in the frame table. */
 extern unsigned long frametable_base_pdx;
 
@@ -280,20 +279,6 @@ static inline uint64_t gvirt_to_maddr(vaddr_t va, paddr_t *pa,
 #define virt_to_mfn(va)     __virt_to_mfn(va)
 #define mfn_to_virt(mfn)    __mfn_to_virt(mfn)
 
-/* Convert between Xen-heap virtual addresses and page-info structures. */
-static inline struct page_info *virt_to_page(const void *v)
-{
-    unsigned long va = (unsigned long)v;
-    unsigned long pdx;
-
-    ASSERT(va >= XENHEAP_VIRT_START);
-    ASSERT(va < directmap_virt_end);
-
-    pdx = (va - XENHEAP_VIRT_START) >> PAGE_SHIFT;
-    pdx += mfn_to_pdx(directmap_mfn_start);
-    return frame_table + pdx - frametable_base_pdx;
-}
-
 static inline void *page_to_virt(const struct page_info *pg)
 {
     return mfn_to_virt(mfn_x(page_to_mfn(pg)));
xen/arch/arm/include/asm/mm_mmu.h +16 -0
index 6d7e5ddde7..bc1b04c4c7 100644
@@ -23,6 +23,8 @@ extern uint64_t init_ttbr;
 extern void setup_directmap_mappings(unsigned long base_mfn,
                                      unsigned long nr_mfns);
 
+#define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)
+
 static inline paddr_t __virt_to_maddr(vaddr_t va)
 {
     uint64_t par = va_to_par(va);
@@ -49,6 +51,20 @@ static inline void *maddr_to_virt(paddr_t ma)
 }
 #endif
 
+/* Convert between Xen-heap virtual addresses and page-info structures. */
+static inline struct page_info *virt_to_page(const void *v)
+{
+    unsigned long va = (unsigned long)v;
+    unsigned long pdx;
+
+    ASSERT(va >= XENHEAP_VIRT_START);
+    ASSERT(va < directmap_virt_end);
+
+    pdx = (va - XENHEAP_VIRT_START) >> PAGE_SHIFT;
+    pdx += mfn_to_pdx(directmap_mfn_start);
+    return frame_table + pdx - frametable_base_pdx;
+}
+
 #endif /* __ARCH_ARM_MM_MMU__ */
 
 /*
xen/arch/arm/include/asm/mm_mpu.h +17 -0
index fe6a828a50..eebd5b5d35 100644
@@ -9,6 +9,8 @@
  */
 extern void setup_static_mappings(void);
 
+extern struct page_info *frame_table;
+
 static inline paddr_t __virt_to_maddr(vaddr_t va)
 {
     /* In MPU system, VA == PA. */
@@ -22,6 +24,21 @@ static inline void *maddr_to_virt(paddr_t ma)
     return (void *)ma;
 }
 
+/* Convert between virtual address to page-info structure. */
+static inline struct page_info *virt_to_page(const void *v)
+{
+    unsigned long va = (unsigned long)v;
+    unsigned long pdx;
+
+    /*
+     * In MPU system, VA == PA, virt_to_maddr() outputs the
+     * exact input address.
+     */
+    pdx = mfn_to_pdx(maddr_to_mfn(virt_to_maddr(va)));
+
+    return frame_table + pdx - frametable_base_pdx;
+}
+
 #endif /* __ARCH_ARM_MM_MPU__ */
 
 /*
xen/arch/arm/mm_mpu.c +25 -0
index f057ee26df..7b282be4fb 100644
@@ -69,6 +69,8 @@ static DEFINE_SPINLOCK(xen_mpumap_lock);
 
 static paddr_t dtb_paddr;
 
+struct page_info *frame_table;
+
 /* Write a MPU protection region */
 #define WRITE_PROTECTION_REGION(sel, pr, prbar_el2, prlar_el2) ({       \
     uint64_t _sel = sel;                                                \
@@ -564,6 +566,29 @@ void __init setup_static_mappings(void)
     /* TODO: guest memory section, device memory section, boot-module section, etc */
 }
 
+/* Map a frame table to cover physical addresses ps through pe */
+void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
+{
+    mfn_t base_mfn;
+    unsigned long nr_pdxs = mfn_to_pdx(mfn_add(maddr_to_mfn(pe), -1)) -
+                            mfn_to_pdx(maddr_to_mfn(ps)) + 1;
+    unsigned long frametable_size = nr_pdxs * sizeof(struct page_info);
+
+    frametable_base_pdx = mfn_to_pdx(maddr_to_mfn(ps));
+    frametable_size = ROUNDUP(frametable_size, PAGE_SIZE);
+    /*
+     * Since VA == PA in MPU and we've already setup Xenheap mapping
+     * in setup_staticheap_mappings(), we could easily deduce the
+     * "virtual address" of frame table.
+     */
+    base_mfn = alloc_boot_pages(frametable_size >> PAGE_SHIFT, 1);
+    frame_table = (struct page_info*)mfn_to_virt(base_mfn);
+
+    memset(&frame_table[0], 0, nr_pdxs * sizeof(struct page_info));
+    memset(&frame_table[nr_pdxs], -1,
+           frametable_size - (nr_pdxs * sizeof(struct page_info)));
+}
+
 /* TODO: Implementation on the first usage */
 void dump_hyp_walk(vaddr_t addr)
 {
Signature · 1 line
2.25.1