Skip to contentSkip to conversation

xen/arm: Split MMU code as the prepration of MPU work

PATCH Superseded Henry Wang 5 participants 37 messages Aug 28, 2023 – Sep 26, 2023

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Henry Wang

author
#0 lore raw

v6 cover xen/arm: Split MMU code as the prepration of MPU work

Based on the discussion in the Xen Summit [1], sending this series out after addressing the comments in v5 [2] as the preparation work to add MPU support. The series passed the GitLab CI check in [3].

Mostly code movement and function folding, with some of Kconfig and build system (mainly Makefiles) adjustment.

This series is based on:
ec272d8d4c CI: Always move the bisect build log back

[1] https://lore.kernel.org/xen-devel/AS8PR08MB799122F8B0CB841DED64F4819226A@AS8PR08MB7991.eurprd08.prod.outlook.com/
[2] https://lore.kernel.org/xen-devel/[email protected]/
[3] https://gitlab.com/xen-project/people/henryw/xen/-/pipelines/982592897

Henry Wang (9):
xen/arm: Introduce CONFIG_MMU Kconfig option xen/arm64: Split and move MMU-specific head.S to mmu/head.S xen/arm64: Fold setup_fixmap() to create_page_tables() xen/arm: Split page table related code to mmu/pt.c xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c xen/arm: Fold mmu_init_secondary_cpu() to head.S xen/arm: Extract MMU-specific MM code
xen/arm: Split MMU-specific setup_mm() and related code out xen/arm: Fold pmap and fixmap into MMU system

Penny Zheng (2):
xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm() xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Wei Chen (2):
xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm() xen/arm: Move MMU related definitions from config.h to mmu/layout.h

 xen/arch/arm/Kconfig                    |    5 +-
 xen/arch/arm/Makefile                   |    1 +
 xen/arch/arm/arm32/Makefile             |    1 +
 xen/arch/arm/arm32/head.S               |   22 +-
 xen/arch/arm/arm32/mmu/Makefile         |    1 +
 xen/arch/arm/arm32/mmu/mm.c             |  301 ++++
 xen/arch/arm/arm64/Makefile             |    2 +-
 xen/arch/arm/arm64/head.S               |  460 +-----
 xen/arch/arm/arm64/mmu/Makefile         |    2 +
 xen/arch/arm/arm64/mmu/head.S           |  481 ++++++
 xen/arch/arm/arm64/{ => mmu}/mm.c       |   84 ++
 xen/arch/arm/include/asm/arm32/mm.h     |    1 +
 xen/arch/arm/include/asm/arm64/macros.h |   36 +
 xen/arch/arm/include/asm/config.h       |  132 +-
 xen/arch/arm/include/asm/mm.h           |   28 +-
 xen/arch/arm/include/asm/mmu/layout.h   |  146 ++
 xen/arch/arm/include/asm/mmu/mm.h       |   41 +
 xen/arch/arm/include/asm/mmu/p2m.h      |   18 +
 xen/arch/arm/include/asm/p2m.h          |   26 +-
 xen/arch/arm/include/asm/page.h         |   15 -
 xen/arch/arm/include/asm/setup.h        |    5 +
 xen/arch/arm/kernel.c                   |   28 -
 xen/arch/arm/mm.c                       | 1212 ---------------
 xen/arch/arm/mmu/Makefile               |    4 +
 xen/arch/arm/mmu/p2m.c                  | 1736 +++++++++++++++++++++
 xen/arch/arm/mmu/pt.c                   |  743 +++++++++
 xen/arch/arm/mmu/setup.c                |  372 +++++
 xen/arch/arm/mmu/smpboot.c              |  121 ++
 xen/arch/arm/p2m.c                      | 1837 +----------------------
 xen/arch/arm/setup.c                    |  324 +---
 xen/arch/arm/smpboot.c                  |    4 +-
 xen/arch/arm/xen.lds.S                  |    1 +
 32 files changed, 4222 insertions(+), 3968 deletions(-)
 create mode 100644 xen/arch/arm/arm32/mmu/Makefile
 create mode 100644 xen/arch/arm/arm32/mmu/mm.c
 create mode 100644 xen/arch/arm/arm64/mmu/Makefile
 create mode 100644 xen/arch/arm/arm64/mmu/head.S
 rename xen/arch/arm/arm64/{ => mmu}/mm.c (60%)
 create mode 100644 xen/arch/arm/include/asm/mmu/layout.h
 create mode 100644 xen/arch/arm/include/asm/mmu/mm.h
 create mode 100644 xen/arch/arm/include/asm/mmu/p2m.h
 create mode 100644 xen/arch/arm/mmu/Makefile
 create mode 100644 xen/arch/arm/mmu/p2m.c
 create mode 100644 xen/arch/arm/mmu/pt.c
 create mode 100644 xen/arch/arm/mmu/setup.c
 create mode 100644 xen/arch/arm/mmu/smpboot.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v6 1/13 xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm()

From: Wei Chen <[email protected]>

At the moment, on MMU system, enable_mmu() 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 on the boot CPU to remove all 1:1 mapping.

Since remove_identity_mapping() is not necessary on Non-MMU system, and we also avoid creating empty function for Non-MMU system, trying to keep only one codeflow in arm64/head.S, we move path switch and remove_identity_mapping() in enable_mmu() on MMU system.

As the remove_identity_mapping should only be called for the boot CPU only, so we introduce enable_boot_cpu_mm() for boot CPU and enable_secondary_cpu_mm() for secondary CPUs in this patch.

---
v6:
- Add Julien's Reviewed-by tag.
v5:
- Add missing "()" in title.
- Use more generic comment in enable_{boot,secondary}_cpu_mm() to mention function will return to the vaddr requested by the caller.
- Move 'mov lr, x5' closer to 'b remove_identity_mapping'.
- Drop the 'b fail' for unreachable code in enable_boot_cpu_mm(). v4:
- Clarify remove_identity_mapping() is called on boot CPU and keep the function/proc format consistent in commit msg.
- Drop inaccurate (due to the refactor) in-code comment.
- Rename enable_{boot,runtime}_mmu to enable_{boot,secondary}_cpu_mm.
- Reword the in-code comment on top of enable_{boot,secondary}_cpu_mm.
- Call "fail" for unreachable code.
v3:
- new patch

---
 xen/arch/arm/arm64/head.S | 83 ++++++++++++++++++++++++++++++---------
 1 file changed, 64 insertions(+), 19 deletions(-)
xen/arch/arm/arm64/head.S +64 -19
index 5029013a14..f25a41d36c 100644
@@ -325,21 +325,11 @@ real_start_efi:
 
         bl    check_cpu_mode
         bl    cpu_init
-        bl    create_page_tables
-        load_paddr x0, boot_pgtable
-        bl    enable_mmu
 
-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =primary_switched
-        br    x0
+        ldr   lr, =primary_switched
+        b     enable_boot_cpu_mm
+
 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_fixmap
 #ifdef CONFIG_EARLY_PRINTK
         /* Use a virtual address to access the UART. */
@@ -384,13 +374,10 @@ GLOBAL(init_secondary)
 #endif
         bl    check_cpu_mode
         bl    cpu_init
-        load_paddr x0, init_ttbr
-        ldr   x0, [x0]
-        bl    enable_mmu
 
-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =secondary_switched
-        br    x0
+        ldr   lr, =secondary_switched
+        b     enable_secondary_cpu_mm
+
 secondary_switched:
 #ifdef CONFIG_EARLY_PRINTK
         /* Use a virtual address to access the UART. */
@@ -748,6 +735,64 @@ enable_mmu:
         ret
 ENDPROC(enable_mmu)
 
+/*
+ * Enable mm (turn on the data cache and the MMU) for secondary CPUs.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */
+enable_secondary_cpu_mm:
+        mov   x5, lr
+
+        load_paddr x0, init_ttbr
+        ldr   x0, [x0]
+
+        bl    enable_mmu
+        mov   lr, x5
+
+        /* Return to the virtual address requested by the caller. */
+        ret
+ENDPROC(enable_secondary_cpu_mm)
+
+/*
+ * Enable mm (turn on the data cache and the MMU) for the boot CPU.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */
+enable_boot_cpu_mm:
+        mov   x5, lr
+
+        bl    create_page_tables
+        load_paddr x0, boot_pgtable
+
+        bl    enable_mmu
+
+        /*
+         * The MMU is turned on and we are in the 1:1 mapping. Switch
+         * to the runtime mapping.
+         */
+        ldr   x0, =1f
+        br    x0
+1:
+        mov   lr, x5
+        /*
+         * 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.
+         * Function will return to the virtual address requested by the caller.
+         */
+        b     remove_identity_mapping
+ENDPROC(enable_boot_cpu_mm)
+
 /*
  * Remove the 1:1 map from the page-tables. It is not easy to keep track
  * where the 1:1 map was mapped, so we will look for the top-level entry
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v6 01/13] xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm()

Hi Henry,

Quoted 141 lines · Henry Wang

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: Wei Chen <[email protected]>

At the moment, on MMU system, enable_mmu() 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 on the boot CPU to remove all 1:1 mapping.

Since remove_identity_mapping() is not necessary on Non-MMU system, and we also avoid creating empty function for Non-MMU system, trying to keep only one codeflow in arm64/head.S, we move path switch and remove_identity_mapping() in enable_mmu() on MMU system.

As the remove_identity_mapping should only be called for the boot CPU only, so we introduce enable_boot_cpu_mm() for boot CPU and enable_secondary_cpu_mm() for secondary CPUs in this patch.

---
v6:
- Add Julien's Reviewed-by tag.
v5:
- Add missing "()" in title.
- Use more generic comment in enable_{boot,secondary}_cpu_mm() to mention function will return to the vaddr requested by the caller.
- Move 'mov lr, x5' closer to 'b remove_identity_mapping'.
- Drop the 'b fail' for unreachable code in enable_boot_cpu_mm(). v4:
- Clarify remove_identity_mapping() is called on boot CPU and keep the function/proc format consistent in commit msg.
- Drop inaccurate (due to the refactor) in-code comment.
- Rename enable_{boot,runtime}_mmu to enable_{boot,secondary}_cpu_mm.
- Reword the in-code comment on top of enable_{boot,secondary}_cpu_mm.
- Call "fail" for unreachable code.
v3:
- new patch

---
  xen/arch/arm/arm64/head.S | 83 ++++++++++++++++++++++++++++++---------
  1 file changed, 64 insertions(+), 19 deletions(-)
xen/arch/arm/arm64/head.S +52 -19
index 5029013a14..f25a41d36c 100644
@@ -325,21 +325,11 @@ real_start_efi:
 
          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
-        load_paddr x0, boot_pgtable
-        bl    enable_mmu
 
-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =primary_switched
-        br    x0
+        ldr   lr, =primary_switched
+        b     enable_boot_cpu_mm
+
  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_fixmap
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */
@@ -384,13 +374,10 @@ GLOBAL(init_secondary)
  #endif
          bl    check_cpu_mode
          bl    cpu_init
-        load_paddr x0, init_ttbr
-        ldr   x0, [x0]
-        bl    enable_mmu
 
-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =secondary_switched
-        br    x0
+        ldr   lr, =secondary_switched
+        b     enable_secondary_cpu_mm
+
  secondary_switched:
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */
@@ -748,6 +735,64 @@ enable_mmu:
          ret
  ENDPROC(enable_mmu)
 
+/*
+ * Enable mm (turn on the data cache and the MMU) for secondary CPUs.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */
+enable_secondary_cpu_mm:
+        mov   x5, lr
+
+        load_paddr x0, init_ttbr
+        ldr   x0, [x0]
+
+        bl    enable_mmu
+        mov   lr, x5
+
+        /* Return to the virtual address requested by the caller. */
+        ret
+ENDPROC(enable_secondary_cpu_mm)
+
+/*
+ * Enable mm (turn on the data cache and the MMU) for the boot CPU.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */
+enable_boot_cpu_mm:
+        mov   x5, lr
+
+        bl    create_page_tables
+        load_paddr x0, boot_pgtable
+
+        bl    enable_mmu
+
+        /*
+         * The MMU is turned on and we are in the 1:1 mapping. Switch
+         * to the runtime mapping.
+         */
+        ldr   x0, =1f

xen/arch/arm/arm64/head.S enable_mmu: · line 793

Quoted 10 lines
xen/arch/arm/arm64/head.S +10 -0
+        br    x0
+1:
+        mov   lr, x5
+        /*
+         * 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.
+         * Function will return to the virtual address requested by the caller.
+         */
+        b     remove_identity_mapping

                ret

I guess you also need this ^^^ (Similar to enable_secondary_cpu_mm() ). Otherwise PC will not switch to the caller.

It will again invoke remove_identity_mapping() (which is redundant) and then jump to the caller.

- Ayan

Quoted 9 lines
xen/arch/arm/arm64/head.S +2 -1
+ENDPROC(enable_boot_cpu_mm)
+
  /*
   * Remove the 1:1 map from the page-tables. It is not easy to keep track
   * where the 1:1 map was mapped, so we will look for the top-level entry
--

2.25.1

#3 lore raw

Re: [PATCH v6 01/13] xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm()

Hi Ayan

Quoted 165 lines · Ayan Kumar Halder

Hi Henry,

Quoted 151 lines · Henry Wang

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: Wei Chen <[email protected]>

At the moment, on MMU system, enable_mmu() 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 on the boot CPU to remove all 1:1 mapping.

Since remove_identity_mapping() is not necessary on Non-MMU system, and we also avoid creating empty function for Non-MMU system, trying to keep only one codeflow in arm64/head.S, we move path switch and remove_identity_mapping() in enable_mmu() on MMU system.

As the remove_identity_mapping should only be called for the boot CPU only, so we introduce enable_boot_cpu_mm() for boot CPU and enable_secondary_cpu_mm() for secondary CPUs in this patch.

---
v6:
- Add Julien's Reviewed-by tag.
v5:
- Add missing "()" in title.
- Use more generic comment in enable_{boot,secondary}_cpu_mm() to mention function will return to the vaddr requested by the caller.
- Move 'mov lr, x5' closer to 'b remove_identity_mapping'.
- Drop the 'b fail' for unreachable code in enable_boot_cpu_mm(). v4:
- Clarify remove_identity_mapping() is called on boot CPU and keep the function/proc format consistent in commit msg.
- Drop inaccurate (due to the refactor) in-code comment.
- Rename enable_{boot,runtime}_mmu to enable_{boot,secondary}_cpu_mm.
- Reword the in-code comment on top of enable_{boot,secondary}_cpu_mm.
- Call "fail" for unreachable code.
v3:
- new patch
---
  xen/arch/arm/arm64/head.S | 83 ++++++++++++++++++++++++++++++---------

  1 file changed, 64 insertions(+), 19 deletions(-)
xen/arch/arm/arm64/head.S +0 -0
index 5029013a14..f25a41d36c 100644
@@ -325,21 +325,11 @@ real_start_efi:
 

          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
-        load_paddr x0, boot_pgtable
-        bl    enable_mmu

-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =primary_switched
-        br    x0
+        ldr   lr, =primary_switched
+        b     enable_boot_cpu_mm
+
  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_fixmap
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */

xen/arch/arm/arm64/head.S +0 -0
@@ -384,13 +374,10 @@ GLOBAL(init_secondary)
   #endif

          bl    check_cpu_mode
          bl    cpu_init
-        load_paddr x0, init_ttbr
-        ldr   x0, [x0]
-        bl    enable_mmu

-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =secondary_switched
-        br    x0
+        ldr   lr, =secondary_switched
+        b     enable_secondary_cpu_mm
+
  secondary_switched:
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */

xen/arch/arm/arm64/head.S +0 -0
@@ -748,6 +735,64 @@ enable_mmu:

          ret
  ENDPROC(enable_mmu)

+/*
+ * Enable mm (turn on the data cache and the MMU) for secondary CPUs.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */

xen/arch/arm/arm64/head.S +16 -0
+enable_secondary_cpu_mm:
+        mov   x5, lr
+
+        load_paddr x0, init_ttbr
+        ldr   x0, [x0]
+
+        bl    enable_mmu
+        mov   lr, x5
+
+        /* Return to the virtual address requested by the caller. */
+        ret
+ENDPROC(enable_secondary_cpu_mm)
+
+/*
+ * Enable mm (turn on the data cache and the MMU) for the boot CPU.
+ * The function will return to the virtual address provided in LR 

(e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */

xen/arch/arm/arm64/head.S +18 -0
+enable_boot_cpu_mm:
+        mov   x5, lr
+
+        bl    create_page_tables
+        load_paddr x0, boot_pgtable
+
+        bl    enable_mmu
+
+        /*
+         * The MMU is turned on and we are in the 1:1 mapping. Switch
+         * to the runtime mapping.
+         */
+        ldr   x0, =1f
+        br    x0
+1:
+        mov   lr, x5
+        /*
+         * The 1:1 map may clash with other parts of the Xen virtual 
Quoted 8 lines

memory
+         * layout. As it is not used anymore, remove it completely to avoid
+         * having to worry about replacing existing mapping afterwards. +         * Function will return to the virtual address requested by the caller.
+         */
+        b     remove_identity_mapping

                ret

Quoted 2 lines

I guess you also need this ^^^ (Similar to enable_secondary_cpu_mm() ). Otherwise PC will not switch to the caller.

We have once talked about whether adding ret here in enable_boot_cpu_mm in previous serie.

Since the "ret" in remove_identity_mapping will make user jump to the "lr" we defined before "b remove_identity_mapping", it is redundant to add another "ret" here. We will never reach the "ret" here

Quoted 13 lines

It will again invoke remove_identity_mapping() (which is redundant) and then jump to the caller.

- Ayan

Quoted 9 lines
xen/arch/arm/arm64/head.S +2 -0
+ENDPROC(enable_boot_cpu_mm)
+

  /*
   * Remove the 1:1 map from the page-tables. It is not easy to keep track
   * where the 1:1 map was mapped, so we will look for the top-level entry

Signature · 1 line
2.25.1
Reviewed-by
#4 lore raw

Re: [PATCH v6 01/13] xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm()

Quoted 1 line · Penny Zheng

Hi Ayan

Hi Penny,

Quoted 20 lines · Ayan Kumar Halder
Quoted 18 lines

Hi Henry,

Quoted 15 lines · Henry Wang

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: Wei Chen <[email protected]>

At the moment, on MMU system, enable_mmu() 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 on the boot CPU to remove all 1:1 mapping.

Since remove_identity_mapping() is not necessary on Non-MMU system, and we also avoid creating empty function for Non-MMU system, trying to keep only one codeflow in arm64/head.S, we move path switch and remove_identity_mapping() in enable_mmu() on MMU system.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

As the remove_identity_mapping should only be called for the boot CPU only, so we introduce enable_boot_cpu_mm() for boot CPU and enable_secondary_cpu_mm() for secondary CPUs in this patch.

Quoted 141 lines
Quoted 141 lines
Quoted 130 lines

---
v6:
- Add Julien's Reviewed-by tag.
v5:
- Add missing "()" in title.
- Use more generic comment in enable_{boot,secondary}_cpu_mm() to mention function will return to the vaddr requested by the caller.
- Move 'mov lr, x5' closer to 'b remove_identity_mapping'.
- Drop the 'b fail' for unreachable code in enable_boot_cpu_mm(). v4:
- Clarify remove_identity_mapping() is called on boot CPU and keep the function/proc format consistent in commit msg.
- Drop inaccurate (due to the refactor) in-code comment.
- Rename enable_{boot,runtime}_mmu to enable_{boot,secondary}_cpu_mm.
- Reword the in-code comment on top of enable_{boot,secondary}_cpu_mm.
- Call "fail" for unreachable code.
v3:
- new patch
---
  xen/arch/arm/arm64/head.S | 83
++++++++++++++++++++++++++++++---------

  1 file changed, 64 insertions(+), 19 deletions(-)
xen/arch/arm/arm64/head.S +0 -0
index 5029013a14..f25a41d36c 100644
@@ -325,21 +325,11 @@ real_start_efi:
 

          bl    check_cpu_mode
          bl    cpu_init
-        bl    create_page_tables
-        load_paddr x0, boot_pgtable
-        bl    enable_mmu

-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =primary_switched
-        br    x0
+        ldr   lr, =primary_switched
+        b     enable_boot_cpu_mm
+
  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_fixmap
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */

xen/arch/arm/arm64/head.S +0 -0
@@ -384,13 +374,10 @@ GLOBAL(init_secondary)
   #endif

          bl    check_cpu_mode
          bl    cpu_init
-        load_paddr x0, init_ttbr
-        ldr   x0, [x0]
-        bl    enable_mmu

-        /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
-        ldr   x0, =secondary_switched
-        br    x0
+        ldr   lr, =secondary_switched
+        b     enable_secondary_cpu_mm
+
  secondary_switched:
  #ifdef CONFIG_EARLY_PRINTK
          /* Use a virtual address to access the UART. */

xen/arch/arm/arm64/head.S +0 -0
@@ -748,6 +735,64 @@ enable_mmu:

          ret
  ENDPROC(enable_mmu)

+/*
+ * Enable mm (turn on the data cache and the MMU) for secondary CPUs.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */

xen/arch/arm/arm64/head.S +16 -0
+enable_secondary_cpu_mm:
+        mov   x5, lr
+
+        load_paddr x0, init_ttbr
+        ldr   x0, [x0]
+
+        bl    enable_mmu
+        mov   lr, x5
+
+        /* Return to the virtual address requested by the caller. */
+        ret
+ENDPROC(enable_secondary_cpu_mm)
+
+/*
+ * Enable mm (turn on the data cache and the MMU) for the boot CPU.
+ * The function will return to the virtual address provided in LR 

(e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */

xen/arch/arm/arm64/head.S +18 -0
+enable_boot_cpu_mm:
+        mov   x5, lr
+
+        bl    create_page_tables
+        load_paddr x0, boot_pgtable
+
+        bl    enable_mmu
+
+        /*
+         * The MMU is turned on and we are in the 1:1 mapping. Switch
+         * to the runtime mapping.
+         */
+        ldr   x0, =1f
+        br    x0
+1:
+        mov   lr, x5
+        /*
+         * The 1:1 map may clash with other parts of the Xen 

virtual memory

Quoted 8 lines

+         * layout. As it is not used anymore, remove it completely to avoid
+         * having to worry about replacing existing mapping afterwards.
+         * Function will return to the virtual address requested by the caller.
+         */
+        b     remove_identity_mapping

                 ret

Quoted 8 lines
Quoted 2 lines

I guess you also need this ^^^ (Similar to enable_secondary_cpu_mm() ). Otherwise PC will not switch to the caller.

We have once talked about whether adding ret here in enable_boot_cpu_mm in previous serie.

Since the "ret" in remove_identity_mapping will make user jump to the "lr" we defined before "b remove_identity_mapping", it is redundant to add another "ret" here. We will never reach the "ret" here

My bad, I see what you mean.

Unlike arm32/head.S enable_mmu(),

remove_identity_mapping() in not called inside enable_mmu() for arm64.

Sorry for the noise.

You can add my RB.

- Ayan

Quoted 14 lines
Quoted 14 lines

It will again invoke remove_identity_mapping() (which is redundant) and then jump to the caller.

- Ayan

Quoted 9 lines
xen/arch/arm/arm64/head.S +2 -0
+ENDPROC(enable_boot_cpu_mm)
+

  /*
   * Remove the 1:1 map from the page-tables. It is not easy to keep track
   * where the 1:1 map was mapped, so we will look for the top-level entry

Signature · 1 line
2.25.1
author
#5 lore raw

PATCH v6 2/13 xen/arm: Introduce CONFIG_MMU Kconfig option

In reply to Henry Wang

There are two types of memory system architectures available for Arm-based systems, namely the Virtual Memory System Architecture (VMSA) and the Protected Memory System Architecture (PMSA). According to ARM DDI 0487G.a, A VMSA provides a Memory Management Unit (MMU) that controls address translation, access permissions, and memory attribute determination and checking, for memory accesses made by the PE. And refer to ARM DDI 0600A.c, the PMSA supports a unified memory protection scheme where an Memory Protection Unit (MPU) manages instruction and data access. Currently, Xen only supports VMSA.

Introduce a Kconfig option CONFIG_MMU, which is currently default set to y and unselectable because currently only VMSA is supported. CONFIG_MMU will be used in follow-up patches.

---
v6:
- Correct typo in commit message, explicitly mention CONFIG_MMU will be used in follow-up patches.
- Add Julien's Acked-by tag.
v5:
- Only introduce the unselectable CONFIG_MMU, add the 'choice' in future commits.
v4:
- Completely rework "[v3,06/52] xen/arm: introduce CONFIG_HAS_MMU"

---
 xen/arch/arm/Kconfig | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/Kconfig +3 -0
index 57bd1d01d7..eb0413336b 100644
@@ -59,6 +59,9 @@ config PADDR_BITS
 	default 40 if ARM_PA_BITS_40
 	default 48 if ARM_64
 
+config MMU
+	def_bool y
+
 source "arch/Kconfig"
 
 config ACPI
Signature · 1 line
2.25.1
author
#6 lore raw

PATCH v6 3/13 xen/arm64: Split and move MMU-specific head.S to mmu/head.S

In reply to Henry Wang

The MMU specific code in head.S will not be used on MPU systems. Instead of introducing more #ifdefs which will bring complexity to the code, move MMU related code to mmu/head.S and keep common code in head.S. Two notes while moving:
- As "fail" in original head.S is very simple and this name is too easy to be conflicted, duplicate it in mmu/head.S instead of exporting it.
- Use ENTRY() for enable_secondary_cpu_mm, enable_boot_cpu_mm and setup_fixmap as they will be used externally.

Also move the assembly macros shared by head.S and mmu/head.S to macros.h.

Note that, 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 .text.header section of head.S, and the size will be checked by _idmap_start and _idmap_end at link time if this fits in 4KB. Since we are introducing a new head.S in this patch, although we can add .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 toolchains. Hence, introduce a new section named .text.idmap in the region between _idmap_start and _idmap_end. And in Xen linker script, we force the .text.idmap contents to linked after .text.header. This will ensure code of head.S always be at the top of Xen binary.

---
v6:
- Reword the reason of adding "ENTRY()" in commit message.
- Add Julien's Reviewed-by tag.
v5:
- Rebase on top of commit
"xen/arm64: head: Introduce a helper to flush local TLBs".
v4:
- Rework "[v3,08/52] xen/arm64: move MMU related code from head.S to mmu/head.S"
- Don't move the "yet to shared" macro such as print_reg.
- Fold "[v3,04/52] xen/arm: add .text.idmap in ld script for Xen identity map sections" to this patch. Rework commit msg.

---
 xen/arch/arm/arm64/Makefile             |   1 +
 xen/arch/arm/arm64/head.S               | 492 +-----------------------
 xen/arch/arm/arm64/mmu/Makefile         |   1 +
 xen/arch/arm/arm64/mmu/head.S           | 488 +++++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/macros.h |  36 ++
 xen/arch/arm/xen.lds.S                  |   1 +
 6 files changed, 528 insertions(+), 491 deletions(-)
 create mode 100644 xen/arch/arm/arm64/mmu/Makefile
 create mode 100644 xen/arch/arm/arm64/mmu/head.S
xen/arch/arm/arm64/Makefile +1 -0
index 54ad55c75c..f89d5fb4fb 100644
@@ -1,4 +1,5 @@
 obj-y += lib/
+obj-$(CONFIG_MMU) += mmu/
 
 obj-y += cache.o
 obj-y += cpufeature.o
xen/arch/arm/arm64/head.S +1 -491

527 lines not on this page · raw message

xen/arch/arm/arm64/mmu/Makefile new file +1 -0
new file mode 100644
index 0000000000..3340058c08
@@ -0,0 +1 @@
+obj-y += head.o
xen/arch/arm/arm64/mmu/head.S new file +488 -0

488 lines not on this page · raw message

xen/arch/arm/include/asm/arm64/macros.h +36 -0
index 140e223b4c..99c401fcaf 100644
@@ -32,6 +32,42 @@
         hint    #22
     .endm
 
+#ifdef CONFIG_EARLY_PRINTK
+/*
+ * Macro to print a string to the UART, if there is one.
+ *
+ * Clobbers x0 - x3
+ */
+#define PRINT(_s)          \
+        mov   x3, lr ;     \
+        adr_l x0, 98f ;    \
+        bl    asm_puts ;   \
+        mov   lr, x3 ;     \
+        RODATA_STR(98, _s)
+
+#else /* CONFIG_EARLY_PRINTK */
+#define PRINT(s)
+
+#endif /* !CONFIG_EARLY_PRINTK */
+
+/*
+ * Pseudo-op for PC relative adr <reg>, <symbol> where <symbol> is
+ * within the range +/- 4GB of the PC.
+ *
+ * @dst: destination register (64 bit wide)
+ * @sym: name of the symbol
+ */
+.macro  adr_l, dst, sym
+        adrp \dst, \sym
+        add  \dst, \dst, :lo12:\sym
+.endm
+
+/* Load the physical address of a symbol into xb */
+.macro load_paddr xb, sym
+        ldr \xb, =\sym
+        add \xb, \xb, x20
+.endm
+
 /*
  * Register aliases.
  */
xen/arch/arm/xen.lds.S +1 -0
index a3c90ca823..59b80d122f 100644
@@ -34,6 +34,7 @@ SECTIONS
        _stext = .;             /* Text section */
        _idmap_start = .;
        *(.text.header)
+       *(.text.idmap)
        _idmap_end = .;
 
        *(.text.cold)
Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH v6 03/13] xen/arm64: Split and move MMU-specific head.S to mmu/head.S

Hi Henry,

I realize that this was already committed. But something went wrong during the code movement.

Quoted 29 lines · Henry Wang
xen/arch/arm/arm64/mmu/head.S new file +29 -0
+/*
+ * Enable mm (turn on the data cache and the MMU) for the boot CPU.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */
+ENTRY(enable_boot_cpu_mm)
+        mov   x5, lr
+
+        bl    create_page_tables
+        load_paddr x0, boot_pgtable
+
+        bl    enable_mmu
+        mov   lr, x5
+
+        /*
+         * 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

xen/arch/arm/arm64/mmu/head.S line 348

Quoted 10 lines
xen/arch/arm/arm64/mmu/head.S new file +10 -0
+         * avoid having to worry about replacing existing mapping
+         * afterwards. Function will return to primary_switched.
+         */
+        b     remove_identity_mapping
+
+        /*
+         * Below is supposed to be unreachable code, as "ret" in
+         * remove_identity_mapping will use the return address in LR in advance.
+         */
+        b     fail

The "b fail" didn't exist in head.S. I guess this was due to a wrong rebase? Can you check if there is something else that went missing?

Cheers,

Signature · 1 line
Julien Grall
author
#8 lore raw

Re: [PATCH v6 03/13] xen/arm64: Split and move MMU-specific head.S to mmu/head.S

Hi Julien,

Quoted 40 lines · Julien Grall

Hi Henry,

I realize that this was already committed. But something went wrong during the code movement.

Quoted 33 lines · Henry Wang
xen/arch/arm/arm64/mmu/head.S new file +33 -0
+/*
+ * Enable mm (turn on the data cache and the MMU) for the boot CPU.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */
+ENTRY(enable_boot_cpu_mm)
+        mov   x5, lr
+
+        bl    create_page_tables
+        load_paddr x0, boot_pgtable
+
+        bl    enable_mmu
+        mov   lr, x5
+
+        /*
+         * 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. Function will return to primary_switched.
+         */
+        b     remove_identity_mapping
Quoted 8 lines

xen/arch/arm/arm64/mmu/head.S line 348

Quoted 6 lines
xen/arch/arm/arm64/mmu/head.S new file +6 -0
+
+        /*
+         * Below is supposed to be unreachable code, as "ret" in
+         * remove_identity_mapping will use the return address in LR in advance.
+         */
+        b     fail

The "b fail" didn't exist in head.S. I guess this was due to a wrong rebase? Can you check if there is something else that went missing?

Please correct me if I am wrong but I think the “b fail” of enable_boot_cpu_mm() is in the mmu head.S, see line 348 [1].

[1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/arch/arm/arm64/mmu/head.S;h=d71fdc69a531780501387fc5c717b4c41bb1b66a;hb=6734327d76be38d20f280ecc96392e385fbc1d8b#l348

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#9 lore raw

Re: [PATCH v6 03/13] xen/arm64: Split and move MMU-specific head.S to mmu/head.S

Hi,

Quoted 53 lines · Henry Wang

Hi Julien,

Quoted 40 lines · Julien Grall

Hi Henry,

I realize that this was already committed. But something went wrong during the code movement.

Quoted 33 lines · Henry Wang
xen/arch/arm/arm64/mmu/head.S new file +33 -0
+/*
+ * Enable mm (turn on the data cache and the MMU) for the boot CPU.
+ * The function will return to the virtual address provided in LR (e.g. the
+ * runtime mapping).
+ *
+ * Inputs:
+ *   lr : Virtual address to return to.
+ *
+ * Clobbers x0 - x5
+ */
+ENTRY(enable_boot_cpu_mm)
+        mov   x5, lr
+
+        bl    create_page_tables
+        load_paddr x0, boot_pgtable
+
+        bl    enable_mmu
+        mov   lr, x5
+
+        /*
+         * 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. Function will return to primary_switched.
+         */
+        b     remove_identity_mapping
Quoted 8 lines

xen/arch/arm/arm64/mmu/head.S line 348

Quoted 6 lines
xen/arch/arm/arm64/mmu/head.S new file +6 -0
+
+        /*
+         * Below is supposed to be unreachable code, as "ret" in
+         * remove_identity_mapping will use the return address in LR in advance.
+         */
+        b     fail

The "b fail" didn't exist in head.S. I guess this was due to a wrong rebase? Can you check if there is something else that went missing?

Quoted 4 lines

Please correct me if I am wrong but I think the “b fail” of enable_boot_cpu_mm() is in the mmu head.S, see line 348 [1].

[1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/arch/arm/arm64/mmu/head.S;h=d71fdc69a531780501387fc5c717b4c41bb1b66a;hb=6734327d76be38d20f280ecc96392e385fbc1d8b#l348

Oh I realized we agreed to remove this line as it is unreachable, I will send a patch. Sorry about it.

Kind regards,
Henry

Quoted 11 lines

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#10 lore raw

PATCH v6 4/13 xen/arm: Move MMU related definitions from config.h to mmu/layout.h

In reply to Henry Wang

From: Wei Chen <[email protected]>

Xen defines some global configuration macros for Arm in config.h. However there are some address layout related definitions that are defined for MMU systems only, and these definitions could not be used by MPU systems. Adding ifdefs to differentiate the MPU from MMU layout will result in a messy and hard-to-read/maintain code.

So move all memory layout definitions to a new file, i.e. mmu/layout.h to avoid spreading "#ifdef" everywhere.

---
v6:
- No change.
v5:
- Rework commit message.
- Add Reviewed-by tag from Julien
v4:
- Rebase on top of latest staging to pick the recent UBSAN change to the layout.
- Use #ifdef CONFIG_HAS_MMU instead of #ifndef CONFIG_HAS_MPU, add a #else case.
- Rework commit message.
v3:
- name the new header layout.h
v2:
- Remove duplicated FIXMAP definitions from config_mmu.h

---
 xen/arch/arm/include/asm/config.h     | 132 +----------------------
 xen/arch/arm/include/asm/mmu/layout.h | 146 ++++++++++++++++++++++++++
 2 files changed, 149 insertions(+), 129 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/mmu/layout.h
xen/arch/arm/include/asm/config.h +3 -129
index 83cbf6b0cb..e1dcec4dd7 100644
@@ -71,136 +71,10 @@
 #include <xen/const.h>
 #include <xen/page-size.h>
 
-/*
- * ARM32 layout:
- *   0  -   2M   Unmapped
- *   2M -  10M   Xen text, data, bss
- *  10M -  12M   Fixmap: special-purpose 4K mapping slots
- *  12M -  16M   Early boot mapping of FDT
- *  16M -  18M   Livepatch vmap (if compiled in)
- *
- *  32M - 128M   Frametable: 32 bytes per page for 12GB 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 - 0x000001ffffffffff (2TB, L0 slots [0..3])
- *
- *  Reserved to identity map Xen
- *
- * 0x0000020000000000 - 0x0000027fffffffff (512GB, L0 slot [4])
- *  (Relative offsets)
- *   0  -   2M   Unmapped
- *   2M -  10M   Xen text, data, bss
- *  10M -  12M   Fixmap: special-purpose 4K mapping slots
- *  12M -  16M   Early boot mapping of FDT
- *  16M -  18M   Livepatch vmap (if compiled in)
- *
- *   1G -   2G   VMAP: ioremap and early_ioremap
- *
- *  32G -  64G   Frametable: 56 bytes per page for 2TB of RAM
- *
- * 0x0000028000000000 - 0x00007fffffffffff (125TB, L0 slots [5..255])
- *  Unused
- *
- * 0x0000800000000000 - 0x000084ffffffffff (5TB, L0 slots [256..265])
- *  1:1 mapping of RAM
- *
- * 0x0000850000000000 - 0x0000ffffffffffff (123TB, L0 slots [266..511])
- *  Unused
- */
-
-#ifdef CONFIG_ARM_32
-#define XEN_VIRT_START          _AT(vaddr_t, MB(2))
+#ifdef CONFIG_MMU
+#include <asm/mmu/layout.h>
 #else
-
-#define SLOT0_ENTRY_BITS  39
-#define SLOT0(slot) (_AT(vaddr_t,slot) << SLOT0_ENTRY_BITS)
-#define SLOT0_ENTRY_SIZE  SLOT0(1)
-
-#define XEN_VIRT_START          (SLOT0(4) + _AT(vaddr_t, MB(2)))
-#endif
-
-/*
- * Reserve enough space so both UBSAN and GCOV can be enabled together
- * plus some slack for future growth.
- */
-#define XEN_VIRT_SIZE           _AT(vaddr_t, MB(8))
-#define XEN_NR_ENTRIES(lvl)     (XEN_VIRT_SIZE / XEN_PT_LEVEL_SIZE(lvl))
-
-#define FIXMAP_VIRT_START       (XEN_VIRT_START + XEN_VIRT_SIZE)
-#define FIXMAP_VIRT_SIZE        _AT(vaddr_t, MB(2))
-
-#define FIXMAP_ADDR(n)          (FIXMAP_VIRT_START + (n) * PAGE_SIZE)
-
-#define BOOT_FDT_VIRT_START     (FIXMAP_VIRT_START + FIXMAP_VIRT_SIZE)
-#define BOOT_FDT_VIRT_SIZE      _AT(vaddr_t, MB(4))
-
-#ifdef CONFIG_LIVEPATCH
-#define LIVEPATCH_VMAP_START    (BOOT_FDT_VIRT_START + BOOT_FDT_VIRT_SIZE)
-#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, MB(32))
-#define FRAMETABLE_SIZE        MB(128-32)
-#define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
-
-#define VMAP_VIRT_START        _AT(vaddr_t, MB(256))
-#define VMAP_VIRT_SIZE         _AT(vaddr_t, GB(1) - MB(256))
-
-#define XENHEAP_VIRT_START     _AT(vaddr_t, GB(1))
-#define XENHEAP_VIRT_SIZE      _AT(vaddr_t, GB(1))
-
-#define DOMHEAP_VIRT_START     _AT(vaddr_t, GB(2))
-#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)
-
-/*
- * The temporary area is overlapping with the domheap area. This may
- * be used to create an alias of the first slot containing Xen mappings
- * when turning on/off the MMU.
- */
-#define TEMPORARY_AREA_FIRST_SLOT    (first_table_offset(DOMHEAP_VIRT_START))
-
-/* Calculate the address in the temporary area */
-#define TEMPORARY_AREA_ADDR(addr)                           \
-     (((addr) & ~XEN_PT_LEVEL_MASK(1)) |                    \
-      (TEMPORARY_AREA_FIRST_SLOT << XEN_PT_LEVEL_SHIFT(1)))
-
-#define TEMPORARY_XEN_VIRT_START    TEMPORARY_AREA_ADDR(XEN_VIRT_START)
-
-#else /* ARM_64 */
-
-#define IDENTITY_MAPPING_AREA_NR_L0  4
-
-#define VMAP_VIRT_START  (SLOT0(4) + GB(1))
-#define VMAP_VIRT_SIZE   GB(1)
-
-#define FRAMETABLE_VIRT_START  (SLOT0(4) + 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 * (266 - 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
-
+# error "Unknown memory management layout"
 #endif
 
 #define NR_hypercalls 64
xen/arch/arm/include/asm/mmu/layout.h new file +146 -0

146 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#11 lore raw

PATCH v6 5/13 xen/arm64: Fold setup_fixmap() to create_page_tables()

In reply to Henry Wang

The original assembly setup_fixmap() is actually doing two seperate tasks, one is enabling the early UART when earlyprintk on, and the other is to set up the fixmap (even when earlyprintk is off).

Per discussion in [1], since commit
9d267c049d92 ("xen/arm64: Rework the memory layout"), there is no chance that the fixmap and the mapping of early UART will clash with the 1:1 mapping. Therefore the mapping of both the fixmap and the early UART can be moved to the end of create_pagetables().

[1] https://lore.kernel.org/xen-devel/[email protected]/

---
v6:
- Drop the "No functional change intended." sentence in commit msg.
- Add Julien's Reviewed-by tag.
v5:
- Refine the title and commit message.
- Drop the "not applied" in-code comment about the 1:1 mapping clash on top of create_page_tables().
- Drop the unnecessary dsb and isb from the original setup_fixmap(). v4:
- Rework "[v3,12/52] xen/mmu: extract early uart mapping from setup_fixmap"

---
 xen/arch/arm/arm64/head.S     |  1 -
 xen/arch/arm/arm64/mmu/head.S | 48 ++++++++---------------------------
 2 files changed, 10 insertions(+), 39 deletions(-)
xen/arch/arm/arm64/head.S +0 -1
index 3c8a12eda7..4ad85dcf58 100644
@@ -275,7 +275,6 @@ real_start_efi:
         b     enable_boot_cpu_mm
 
 primary_switched:
-        bl    setup_fixmap
 #ifdef CONFIG_EARLY_PRINTK
         /* Use a virtual address to access the UART. */
         ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
xen/arch/arm/arm64/mmu/head.S +10 -38

66 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#12 lore raw

PATCH v6 6/13 xen/arm: Split page table related code to mmu/pt.c

In reply to Henry Wang

The extraction of MMU related code is the basis of MPU support. This commit starts this work by firstly splitting the page table related code to mmu/pt.c, so that we will not end up with again massive mm.c files.

Introduce a mmu specific directory and setup the Makefiles for it. Move the page table related functions and macros from arch/arm/mm.c to arch/arm/mmu/pt.c. Expose the previously static global variable "phys_offset".

While moving, mark pte_of_xenaddr() as __init to make clear that this helper is only intended to be used during early boot.

Take the opportunity to fix the in-code comment coding styles when possible, and drop the unnecessary #include headers in the original arch/arm/mm.c.

---
v6:
- Rework the original patch "[v5,07/13] xen/arm: Extract MMU-specific code", only split the page table related code out in this patch.

---
 xen/arch/arm/Makefile         |   1 +
 xen/arch/arm/include/asm/mm.h |   2 +
 xen/arch/arm/mm.c             | 726 +--------------------------------
 xen/arch/arm/mmu/Makefile     |   1 +
 xen/arch/arm/mmu/pt.c         | 743 ++++++++++++++++++++++++++++++++++
 5 files changed, 748 insertions(+), 725 deletions(-)
 create mode 100644 xen/arch/arm/mmu/Makefile
 create mode 100644 xen/arch/arm/mmu/pt.c
xen/arch/arm/Makefile +1 -0
index 7bf07e9920..c45b08b31e 100644
@@ -1,5 +1,6 @@
 obj-$(CONFIG_ARM_32) += arm32/
 obj-$(CONFIG_ARM_64) += arm64/
+obj-$(CONFIG_MMU) += mmu/
 obj-$(CONFIG_ACPI) += acpi/
 obj-$(CONFIG_HAS_PCI) += pci/
 ifneq ($(CONFIG_NO_PLAT),y)
xen/arch/arm/include/asm/mm.h +2 -0
index aaacba3f04..bf2fe26f9e 100644
@@ -168,6 +168,8 @@ struct page_info
 /* Non-boot CPUs use this to find the correct pagetables. */
 extern uint64_t init_ttbr;
 
+extern paddr_t phys_offset;
+
 extern mfn_t directmap_mfn_start, directmap_mfn_end;
 extern vaddr_t directmap_virt_end;
 #ifdef CONFIG_ARM_64
xen/arch/arm/mm.c +1 -725

780 lines not on this page · raw message

xen/arch/arm/mmu/Makefile new file +1 -0
new file mode 100644
index 0000000000..bdfc2e077d
@@ -0,0 +1 @@
+obj-y += pt.o
xen/arch/arm/mmu/pt.c new file +743 -0

743 lines not on this page · raw message

Signature · 1 line
2.25.1
#13 lore raw

Re: [PATCH v6 06/13] xen/arm: Split page table related code to mmu/pt.c

Hi Henry,

I haven't checked that the code movement is just a code movement. For now, I am mainly looking at the split.

Quoted 9 lines · Henry Wang

The extraction of MMU related code is the basis of MPU support. This commit starts this work by firstly splitting the page table related code to mmu/pt.c, so that we will not end up with again massive mm.c files.

Introduce a mmu specific directory and setup the Makefiles for it. Move the page table related functions and macros from arch/arm/mm.c to arch/arm/mmu/pt.c. Expose the previously static global variable "phys_offset".

I don't much like the idea to expose phys_offset everywhere. Looking at the code there are two users:
* pte_of_xenaddr(): I realize you suggested to add it in pt.c and I
agreed. However, looking at the split, this function is exclusively used for boot (and you confirmed below). So I think it would be preferable to move it in mmu/setup.c.
* prepare_secondary_mm(): I think we could switch to virt_to_mfn().

I can understand if you don't want to make the second change. So I would at least request to ...

Quoted 10 lines

While moving, mark pte_of_xenaddr() as __init to make clear that this helper is only intended to be used during early boot.

Take the opportunity to fix the in-code comment coding styles when possible, and drop the unnecessary #include headers in the original arch/arm/mm.c.

Quoted 7 lines

---
v6:
- Rework the original patch "[v5,07/13] xen/arm: Extract MMU-specific code", only split the page table related code out in this patch.

---
  xen/arch/arm/Makefile         |   1 +
  xen/arch/arm/include/asm/mm.h |   2 +

... declare phys_offset in asm/mmu/mm.h because this variable doesn't make a lot of sense when the MPU is used (it will always be equal to 0).

The rest of the split looks good to me.

[...]

xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 401

Quoted 7 lines
xen/arch/arm/mm.c +0 -7
-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);
-}
-

See above. I think this should stay here for now and the be moved to setup.c in a follow-up patch.

Cheers,

Signature · 1 line
Julien Grall
author
#14 lore raw

Re: [PATCH v6 06/13] xen/arm: Split page table related code to mmu/pt.c

Hi Julien,

Quoted 5 lines · Julien Grall

Hi Henry,

I haven't checked that the code movement is just a code movement. For now, I am mainly looking at the split.

No problem, thank you. I will rebase v6 and use text comparison tools to check if the code movement is just code movement before sending v7 to make sure I won’t make the same mistake again. You can double check the final version.

Quoted 11 lines · Henry Wang
Quoted 8 lines

The extraction of MMU related code is the basis of MPU support. This commit starts this work by firstly splitting the page table related code to mmu/pt.c, so that we will not end up with again massive mm.c files.
Introduce a mmu specific directory and setup the Makefiles for it. Move the page table related functions and macros from arch/arm/mm.c to arch/arm/mmu/pt.c. Expose the previously static global variable "phys_offset".

Quoted 2 lines

I don't much like the idea to expose phys_offset everywhere. Looking at the code there are two users:
* pte_of_xenaddr(): I realize you suggested to add it in pt.c and I agreed. However, looking at the split, this function is exclusively used for boot (and you confirmed below). So I think it would be preferable to move it in mmu/setup.c.

Sounds good, I will fix this in v7.

Quoted 1 line

* prepare_secondary_mm(): I think we could switch to virt_to_mfn().

Actually I found the third use of phys_offset in setup_pagetables(), but it looks like the usage is similar as the usage here, so probably these two are the same case.

Also, please correct me if I am wrong, by suggesting the “switch”, do you mean using virt_to_mfn() on xen_pgtable to change it to min, then change the mfn to PA and delete the phys_offset? If so, why not use virt_to_maddr()?

Quoted 18 lines

I can understand if you don't want to make the second change. So I would at least request to ...

Quoted 7 lines

While moving, mark pte_of_xenaddr() as __init to make clear that this helper is only intended to be used during early boot. Take the opportunity to fix the in-code comment coding styles when possible, and drop the unnecessary #include headers in the original arch/arm/mm.c.

Quoted 7 lines

---
v6:
- Rework the original patch "[v5,07/13] xen/arm: Extract MMU-specific code", only split the page table related code out in this patch.

---
 xen/arch/arm/Makefile         |   1 +
 xen/arch/arm/include/asm/mm.h |   2 +
Quoted 3 lines

... declare phys_offset in asm/mmu/mm.h because this variable doesn't make a lot of sense when the MPU is used (it will always be equal to 0).

The rest of the split looks good to me.

Thanks for confirming, I will see what the above discussion ends and then do the change accordingly.

Quoted 4 lines

[...]

Quoted 9 lines

xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 401

Quoted 7 lines
xen/arch/arm/mm.c +0 -7
-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);
-}
-

See above. I think this should stay here for now and the be moved to setup.c in a follow-up patch.

Sure.

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#15 lore raw

PATCH v6 7/13 xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c

In reply to Henry Wang

Move the code related to secondary page table initialization, clear boot page tables and the global variable definitions of these boot page tables from arch/arm/mm.c to arch/arm/mmu/smpboot.c

Drop the "static" attribute of arm32 global variable cpu0_pgtable, and declare it in the arm32 mm.h header, because this variable will be used by both arch/arm/mm.c and arch/arm/mmu/smpboot.c.

Take the opportunity to fix the in-code comment coding styles when possible.

---
v6:
- Rework the original patch "[v5,07/13] xen/arm: Extract MMU-specific code", only split the smpboot related code out in this patch.

---
 xen/arch/arm/include/asm/arm32/mm.h |   1 +
 xen/arch/arm/mm.c                   | 103 +----------------------
 xen/arch/arm/mmu/Makefile           |   1 +
 xen/arch/arm/mmu/smpboot.c          | 121 ++++++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 102 deletions(-)
 create mode 100644 xen/arch/arm/mmu/smpboot.c
xen/arch/arm/include/asm/arm32/mm.h +1 -0
index 856f2dbec4..0e149945f9 100644
@@ -6,6 +6,7 @@
 #include <asm/lpae.h>
 
 DECLARE_PER_CPU(lpae_t *, xen_pgtable);
+extern DEFINE_PAGE_TABLE(cpu0_pgtable);
 
 /*
  * Only a limited amount of RAM, called xenheap, is always mapped on ARM32.
xen/arch/arm/mm.c +1 -102

127 lines not on this page · raw message

xen/arch/arm/mmu/Makefile +1 -0
index bdfc2e077d..0e82015ee1 100644
@@ -1 +1,2 @@
 obj-y += pt.o
+obj-y += smpboot.o
xen/arch/arm/mmu/smpboot.c new file +121 -0

121 lines not on this page · raw message

Signature · 1 line
2.25.1
#16 lore raw

Re: [PATCH v6 07/13] xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c

Hi Henry,

Quoted 7 lines · Henry Wang

Move the code related to secondary page table initialization, clear boot page tables and the global variable definitions of these boot page tables from arch/arm/mm.c to arch/arm/mmu/smpboot.c

Drop the "static" attribute of arm32 global variable cpu0_pgtable, and declare it in the arm32 mm.h header, because this variable will be used by both arch/arm/mm.c and arch/arm/mmu/smpboot.c.

I don't think you need to export cpu0_pgtable. You can instead replace use with per_cpu(xen_pgtable, 0).

Quoted 210 lines

Take the opportunity to fix the in-code comment coding styles when possible.

---
v6:
- Rework the original patch "[v5,07/13] xen/arm: Extract MMU-specific code", only split the smpboot related code out in this patch.

---
  xen/arch/arm/include/asm/arm32/mm.h |   1 +
  xen/arch/arm/mm.c                   | 103 +----------------------
  xen/arch/arm/mmu/Makefile           |   1 +
  xen/arch/arm/mmu/smpboot.c          | 121 ++++++++++++++++++++++++++++
  4 files changed, 124 insertions(+), 102 deletions(-)

create mode 100644 xen/arch/arm/mmu/smpboot.c

xen/arch/arm/include/asm/arm32/mm.h +1 -0
index 856f2dbec4..0e149945f9 100644
@@ -6,6 +6,7 @@
  #include <asm/lpae.h>
 
  DECLARE_PER_CPU(lpae_t *, xen_pgtable);
+extern DEFINE_PAGE_TABLE(cpu0_pgtable);
 
  /*
   * Only a limited amount of RAM, called xenheap, is always mapped on ARM32.
xen/arch/arm/mm.c +1 -102
index b42814077c..f3ef0da0e3 100644
@@ -27,39 +27,6 @@
  #undef mfn_to_virt
  #define mfn_to_virt(mfn) __mfn_to_virt(mfn_x(mfn))
 
-/* 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:
- *  - XEN_VIRT_SIZE worth of L3 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_TABLES(boot_third, XEN_NR_ENTRIES(2));
-
  /* Main runtime page tables */
 
  /*
@@ -80,7 +47,7 @@ static DEFINE_PAGE_TABLE(xen_first);
  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);
+DEFINE_PAGE_TABLE(cpu0_pgtable);
  #endif
 
  /* Common pagetable leaves */
@@ -277,13 +244,6 @@ static void xen_pt_enforce_wnx(void)
      flush_xen_tlb_local();
  }
 
-/* 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)
@@ -362,67 +322,6 @@ void __init setup_pagetables(unsigned long boot_phys_offset)
  #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/arch/arm/mmu/Makefile +1 -0
index bdfc2e077d..0e82015ee1 100644
@@ -1 +1,2 @@
  obj-y += pt.o
+obj-y += smpboot.o
xen/arch/arm/mmu/smpboot.c new file +32 -0
new file mode 100644
index 0000000000..a673989d09
@@ -0,0 +1,121 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * xen/arch/arm/mmu/smpboot.c
+ *
+ * MMU system secondary CPUs MM bringup code.
+ */
+
+#include <xen/domain_page.h>
+
+/*
+ * 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:
+ *  - XEN_VIRT_SIZE worth of L3 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.

xen/arch/arm/mmu/smpboot.c line 42

Quoted 10 lines
xen/arch/arm/mmu/smpboot.c new file +10 -0
+ */
+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_TABLES(boot_third, XEN_NR_ENTRIES(2));

Strictly speaking, boot_* are also used for CPU0 but not in C code. That said, I am ok if there are moved here.

In the longer term, I would actually like to remove boot_*. This should already be possible for arm64 (in fact they are not used for secondary CPU bring-up). For arm32, it requires a bit more work.

Anyway, that's not a request for your series. :)

Quoted 55 lines
xen/arch/arm/mmu/smpboot.c new file +55 -0
+
+/* 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);
+}
+
+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) )
+    {

xen/arch/arm/mmu/smpboot.c line 107

Quoted 10 lines
xen/arch/arm/mmu/smpboot.c new file +10 -0
+        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);

init_ttbr will only be used by smpboot.c and assembly code. So I think the declaration needs to also be moved and the prototype moved to mmu/mm.h.

Cheers,

Signature · 1 line
Julien Grall
author
#17 lore raw

Re: [PATCH v6 07/13] xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c

Hi Julien,

Quoted 5 lines · Julien Grall

Hi Henry,

On 28/08/2023 02:32, Henry Wang wrote:

Quoted 8 lines
Quoted 6 lines

Move the code related to secondary page table initialization, clear boot page tables and the global variable definitions of these boot page tables from arch/arm/mm.c to arch/arm/mmu/smpboot.c Drop the "static" attribute of arm32 global variable cpu0_pgtable, and declare it in the arm32 mm.h header, because this variable will be used by both arch/arm/mm.c and arch/arm/mmu/smpboot.c.

I don't think you need to export cpu0_pgtable. You can instead replace use with per_cpu(xen_pgtable, 0).

You mean using

```
memcpy(first, per_cpu(xen_pgtable, 0), PAGE_SIZE);
```

in the arm32 implementation of prepare_secondary_mm() in mmu/smpboot.c?

That sounds good to me and I will fix in v7.

Quoted 14 lines

xen/arch/arm/mmu/smpboot.c line 42

Quoted 11 lines

+ */

xen/arch/arm/mmu/smpboot.c new file +9 -0
+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_TABLES(boot_third, XEN_NR_ENTRIES(2));

Strictly speaking, boot_* are also used for CPU0 but not in C code. That said, I am ok if there are moved here.

Quoted 3 lines

In the longer term, I would actually like to remove boot_*. This should already be possible for arm64 (in fact they are not used for secondary CPU bring-up). For arm32, it requires a bit more work.

Anyway, that's not a request for your series. :)

Let’s see if we can do that in 4.19 dev cycle then :)

Quoted 5 lines

xen/arch/arm/mmu/smpboot.c line 107

Quoted 3 lines
xen/arch/arm/mmu/smpboot.c new file +2 -0
+    /* Set init_ttbr for this CPU coming up */
+    init_ttbr = __pa(first);

init_ttbr will only be used by smpboot.c and assembly code. So I think the declaration needs to also be moved and the prototype moved to mmu/mm.h.

Ok, will do in v7.

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#18 lore raw

PATCH v6 8/13 xen/arm: Fold mmu_init_secondary_cpu() to head.S

In reply to Henry Wang

Currently mmu_init_secondary_cpu() only enforces the page table should not contain mapping that are both Writable and eXecutables after boot. To ease the arch/arm/mm.c split work, fold this function to head.S.

Introduce assembly macro pt_enforce_wxn for both arm32 and arm64. For arm64, the macro is called at the end of enable_secondary_cpu_mm(). For arm32, the macro is called before secondary CPUs jumping into the C world.

---
v6:
- New patch.

---
 xen/arch/arm/arm32/head.S     | 20 ++++++++++++++++++++
 xen/arch/arm/arm64/mmu/head.S | 21 +++++++++++++++++++++
 xen/arch/arm/include/asm/mm.h |  2 --
 xen/arch/arm/mm.c             |  6 ------
 xen/arch/arm/smpboot.c        |  2 --
 5 files changed, 41 insertions(+), 10 deletions(-)
xen/arch/arm/arm32/head.S +20 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@
         isb
 .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
 /*
  * Common register usage in this file:
  *   r0  -
@@ -254,6 +273,7 @@ secondary_switched:
         /* Use a virtual address to access the UART. */
         mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
 #endif
+        pt_enforce_wxn r0
         PRINT("- Ready -\r\n")
         /* Jump to C world */
         mov_w r2, start_secondary
xen/arch/arm/arm64/mmu/head.S +21 -0
index a5271e3880..25028bdf07 100644
@@ -31,6 +31,25 @@
         isb
 .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+       mrs   \tmp, SCTLR_EL2
+       orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+       dsb   sy
+       msr   SCTLR_EL2, \tmp
+       /*
+        * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+        * before flushing the TLBs.
+        */
+       isb
+       flush_xen_tlb_local
+.endm
+
 /*
  * Macro to find the slot number at a given page-table level
  *
@@ -308,6 +327,8 @@ ENTRY(enable_secondary_cpu_mm)
         bl    enable_mmu
         mov   lr, x5
 
+        pt_enforce_wxn x0
+
         /* Return to the virtual address requested by the caller. */
         ret
 ENDPROC(enable_secondary_cpu_mm)
xen/arch/arm/include/asm/mm.h +0 -2
index bf2fe26f9e..a66aa219b1 100644
@@ -216,8 +216,6 @@ extern void remove_early_mappings(void);
 /* Allocate and initialise pagetables for a secondary CPU. Sets init_ttbr to the
  * new page table */
 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.
xen/arch/arm/mm.c +0 -6
index f3ef0da0e3..3ee74542ba 100644
@@ -322,12 +322,6 @@ void __init setup_pagetables(unsigned long boot_phys_offset)
 #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:
xen/arch/arm/smpboot.c +0 -2
index e107b86b7b..ade2c77cf9 100644
@@ -359,8 +359,6 @@ void start_secondary(void)
      */
     update_system_features(&current_cpu_data);
 
-    mmu_init_secondary_cpu();
-
     gic_init_secondary_cpu();
 
     set_current(idle_vcpu[cpuid]);
Signature · 1 line
2.25.1
#19 lore raw

Re: [PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S

Hi Henry,

Quoted 56 lines · Henry Wang

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.

Currently mmu_init_secondary_cpu() only enforces the page table should not contain mapping that are both Writable and eXecutables after boot. To ease the arch/arm/mm.c split work, fold this function to head.S.

Introduce assembly macro pt_enforce_wxn for both arm32 and arm64. For arm64, the macro is called at the end of enable_secondary_cpu_mm(). For arm32, the macro is called before secondary CPUs jumping into the C world.

---
v6:
- New patch.

---
  xen/arch/arm/arm32/head.S     | 20 ++++++++++++++++++++
  xen/arch/arm/arm64/mmu/head.S | 21 +++++++++++++++++++++
  xen/arch/arm/include/asm/mm.h |  2 --
  xen/arch/arm/mm.c             |  6 ------
  xen/arch/arm/smpboot.c        |  2 --
  5 files changed, 41 insertions(+), 10 deletions(-)
xen/arch/arm/arm32/head.S +19 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@
          isb
  .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
  /*
   * Common register usage in this file:
   *   r0  -

xen/arch/arm/arm32/head.S secondary_switched: · line 277

Quoted 5 lines
xen/arch/arm/arm32/head.S +1 -0
@@ -254,6 +273,7 @@ secondary_switched:
          /* Use a virtual address to access the UART. */
          mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
  #endif
+        pt_enforce_wxn
 

Can you move ^^^ to before "#ifdef CONFIG_EARLY_PRINTK" so that the MMU related functionality are bundled together?

Also AFAIU, mov_w has not effect on pt_enforce_wxn().

So that I can create a function "enable_secondary_cpu_mm()" - similar to one you introduced for arm64

/* This will contain all the MMU related function for secondary cpu */

enable_secondary_cpu_mm:

bl    create_page_tables

mov_w lr, secondary_switched

....

flush_xen_tlb_local r0

pt_enforce_wxn r0

ENDPROC(enable_secondary_cpu_mm)

- Ayan

Quoted 89 lines
xen/arch/arm/arm32/head.S +0 -0
          PRINT("- Ready -\r\n")
          /* Jump to C world */

mov_w r2, start_secondary

xen/arch/arm/arm64/mmu/head.S +21 -0
index a5271e3880..25028bdf07 100644
@@ -31,6 +31,25 @@
          isb
  .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+       mrs   \tmp, SCTLR_EL2
+       orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+       dsb   sy
+       msr   SCTLR_EL2, \tmp
+       /*
+        * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+        * before flushing the TLBs.
+        */
+       isb
+       flush_xen_tlb_local
+.endm
+
  /*
   * Macro to find the slot number at a given page-table level
   *
@@ -308,6 +327,8 @@ ENTRY(enable_secondary_cpu_mm)
          bl    enable_mmu
          mov   lr, x5
 
+        pt_enforce_wxn x0
+
          /* Return to the virtual address requested by the caller. */
          ret
  ENDPROC(enable_secondary_cpu_mm)
xen/arch/arm/include/asm/mm.h +0 -2
index bf2fe26f9e..a66aa219b1 100644
@@ -216,8 +216,6 @@ extern void remove_early_mappings(void);
  /* Allocate and initialise pagetables for a secondary CPU. Sets init_ttbr to the
   * new page table */
  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.
xen/arch/arm/mm.c +0 -6
index f3ef0da0e3..3ee74542ba 100644
@@ -322,12 +322,6 @@ void __init setup_pagetables(unsigned long boot_phys_offset)
  #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:
xen/arch/arm/smpboot.c +0 -3
index e107b86b7b..ade2c77cf9 100644
@@ -359,8 +359,6 @@ void start_secondary(void)
       */
      update_system_features(&current_cpu_data);
 
-    mmu_init_secondary_cpu();
-
      gic_init_secondary_cpu();
 
      set_current(idle_vcpu[cpuid]);
--

2.25.1

author
#20 lore raw

Re: [PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S

Hi Ayan,

Quoted 45 lines · Ayan Kumar Halder

Hi Henry,

Quoted 32 lines · Henry Wang
xen/arch/arm/arm32/head.S +19 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@
         isb
 .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
 /*
  * Common register usage in this file:
  *   r0  -

xen/arch/arm/arm32/head.S secondary_switched: · line 277

Quoted 5 lines
xen/arch/arm/arm32/head.S +1 -0
@@ -254,6 +273,7 @@ secondary_switched:
         /* Use a virtual address to access the UART. */
         mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
 #endif
+        pt_enforce_wxn
 

Can you move ^^^ to before "#ifdef CONFIG_EARLY_PRINTK" so that the MMU related functionality are bundled together?

Quoted 3 lines

Also AFAIU, mov_w has not effect on pt_enforce_wxn().

So that I can create a function "enable_secondary_cpu_mm()" - similar to one you introduced for arm64

Sure, I am good with this if other maintainers do not have any objections.

Kind regards,
Henry

Quoted 19 lines

/* This will contain all the MMU related function for secondary cpu */

enable_secondary_cpu_mm:

bl create_page_tables

mov_w lr, secondary_switched

....

flush_xen_tlb_local r0

pt_enforce_wxn r0

ENDPROC(enable_secondary_cpu_mm)

- Ayan

#21 lore raw

Re: [PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S

Quoted 45 lines · Henry Wang
Quoted 45 lines · Ayan Kumar Halder

Hi Henry,

Quoted 32 lines · Henry Wang
xen/arch/arm/arm32/head.S +19 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@
          isb
  .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
  /*
   * Common register usage in this file:
   *   r0  -

xen/arch/arm/arm32/head.S secondary_switched: · line 277

Quoted 5 lines
xen/arch/arm/arm32/head.S +1 -0
@@ -254,6 +273,7 @@ secondary_switched:
          /* Use a virtual address to access the UART. */
          mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
  #endif
+        pt_enforce_wxn
 

Can you move ^^^ to before "#ifdef CONFIG_EARLY_PRINTK" so that the MMU related functionality are bundled together?

Quoted 5 lines
Quoted 3 lines

Also AFAIU, mov_w has not effect on pt_enforce_wxn().

So that I can create a function "enable_secondary_cpu_mm()" - similar to one you introduced for arm64

Sure, I am good with this if other maintainers do not have any objections.

I am objecting. It would be quite handy to print a message on the console to indicate that we are enforce WXN. So we want to update UART address (stored in r11) before hand.

Cheers,

Signature · 1 line
Julien Grall
#22 lore raw

Re: [PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S

Hi Julien,

Quoted 48 lines · Julien Grall
Quoted 45 lines · Henry Wang
Quoted 45 lines · Ayan Kumar Halder

Hi Henry,

Quoted 34 lines · Henry Wang
xen/arch/arm/arm32/head.S +0 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@

          isb
  .endm

+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
  /*
   * Common register usage in this file:
   *   r0  -

xen/arch/arm/arm32/head.S +0 -0
@@ -254,6 +273,7 @@ secondary_switched:
           /* Use a virtual address to access the UART. */
Quoted 3 lines

          mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
  #endif
+        pt_enforce_wxn

Can you move ^^^ to before "#ifdef CONFIG_EARLY_PRINTK" so that the MMU related functionality are bundled together?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Also AFAIU, mov_w has not effect on pt_enforce_wxn().

So that I can create a function "enable_secondary_cpu_mm()" - similar to one you introduced for arm64

Sure, I am good with this if other maintainers do not have any objections.

I am objecting. It would be quite handy to print a message on the console to indicate that we are enforce WXN. So we want to update UART address (stored in r11) before hand.

You mean you want to add this snippet in the current patch

xen/arch/arm/arm32/head.S +0 -0
index 39218cf15f..282b89a96e 100644
@@ -273,6 +273,7 @@ secondary_switched:

         /* Use a virtual address to access the UART. */ mov_w r11, EARLY_UART_VIRTUAL_ADDRESS #endif
+        PRINT("- Enforce WXN -\r\n") pt_enforce_wxn r0
         PRINT("- Ready -\r\n")
         /* Jump to C world */

- Ayan

Quoted 3 lines

Cheers,

author
#23 lore raw

Re: [PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S

In reply to Julien Grall

Hi Julien,

Quoted 47 lines · Julien Grall
Quoted 45 lines · Henry Wang
Quoted 45 lines · Ayan Kumar Halder

Hi Henry,

Quoted 32 lines · Henry Wang
xen/arch/arm/arm32/head.S +19 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@
         isb
 .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
 /*
  * Common register usage in this file:
  *   r0  -

xen/arch/arm/arm32/head.S secondary_switched: · line 277

Quoted 5 lines
xen/arch/arm/arm32/head.S +1 -0
@@ -254,6 +273,7 @@ secondary_switched:
         /* Use a virtual address to access the UART. */
         mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
 #endif
+        pt_enforce_wxn
 

Can you move ^^^ to before "#ifdef CONFIG_EARLY_PRINTK" so that the MMU related functionality are bundled together?

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

Also AFAIU, mov_w has not effect on pt_enforce_wxn().

So that I can create a function "enable_secondary_cpu_mm()" - similar to one you introduced for arm64

Sure, I am good with this if other maintainers do not have any objections.

I am objecting. It would be quite handy to print a message on the console to indicate that we are enforce WXN. So we want to update UART address (stored in r11) before hand.

Good idea about the printing, I am happy to add a printed message on top of the macro saying that we are enforcing WXN from here if you agree.

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#24 lore raw

Re: [PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S

In reply to Henry Wang

Hi Henry,

Quoted 77 lines · Henry Wang

Currently mmu_init_secondary_cpu() only enforces the page table should not contain mapping that are both Writable and eXecutables after boot. To ease the arch/arm/mm.c split work, fold this function to head.S.

Introduce assembly macro pt_enforce_wxn for both arm32 and arm64. For arm64, the macro is called at the end of enable_secondary_cpu_mm(). For arm32, the macro is called before secondary CPUs jumping into the C world.

---
v6:
- New patch.

---
  xen/arch/arm/arm32/head.S     | 20 ++++++++++++++++++++
  xen/arch/arm/arm64/mmu/head.S | 21 +++++++++++++++++++++
  xen/arch/arm/include/asm/mm.h |  2 --
  xen/arch/arm/mm.c             |  6 ------
  xen/arch/arm/smpboot.c        |  2 --
  5 files changed, 41 insertions(+), 10 deletions(-)
xen/arch/arm/arm32/head.S +20 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@
          isb
  .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
  /*
   * Common register usage in this file:
   *   r0  -
@@ -254,6 +273,7 @@ secondary_switched:
          /* Use a virtual address to access the UART. */
          mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
  #endif
+        pt_enforce_wxn r0
          PRINT("- Ready -\r\n")
          /* Jump to C world */
          mov_w r2, start_secondary
xen/arch/arm/arm64/mmu/head.S +9 -0
index a5271e3880..25028bdf07 100644
@@ -31,6 +31,25 @@
          isb
  .endm
 
+/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+       mrs   \tmp, SCTLR_EL2
+       orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN

xen/arch/arm/arm64/mmu/head.S line 52

Quoted 10 lines
xen/arch/arm/arm64/mmu/head.S +10 -0
+       dsb   sy
+       msr   SCTLR_EL2, \tmp
+       /*
+        * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+        * before flushing the TLBs.
+        */
+       isb
+       flush_xen_tlb_local
+.endm
+

It would be preferable if we can set the flag right when the MMU is initialized enabled configured. This would avoid the extra TLB flush and SCTLR dance. How about the following (not compiled/cleaned) code:

xen/arch/arm/arm64/mmu/head.S +3 -1
index a5271e388071..6b19d15ff89f 100644
@@ -264,10 +264,11 @@ ENDPROC(create_page_tables)
  * Inputs:
  *   x0 : Physical address of the page tables.
  *
- * Clobbers x0 - x4
+ * Clobbers x0 - x6
  */
 enable_mmu:
         mov   x4, x0
+        mov   x5, x1
         PRINT("- Turning on paging -\r\n")
 
         /*
@@ -283,6 +284,7 @@ enable_mmu:
         mrs   x0, SCTLR_EL2
         orr   x0, x0, #SCTLR_Axx_ELx_M  /* Enable MMU */
         orr   x0, x0, #SCTLR_Axx_ELx_C  /* Enable D-cache */
+        orr   x0, x0, x5                /* Enable extra flags */
         dsb   sy                     /* Flush PTE writes and finish 

reads */
msr SCTLR_EL2, x0 /* now paging is enabled */
isb /* Now, flush the icache */
@@ -297,16 +299,17 @@ ENDPROC(enable_mmu)
* Inputs:
* lr : Virtual address to return to.
*
- * Clobbers x0 - x5
+ * Clobbers x0 - x6
*/
ENTRY(enable_secondary_cpu_mm)

-        mov   x5, lr
+        mov   x6, lr

load_paddr x0, init_ttbr
ldr x0, [x0]

+        mov   x1, #SCTLR_Axx_ELx_WXN        /* Enable WxN from the start */
         bl    enable_mmu
-        mov   lr, x5
+        mov   lr, x6
         /* Return to the virtual address requested by the caller. */
         ret

@@ -320,16 +323,17 @@ ENDPROC(enable_secondary_cpu_mm)
* Inputs:
* lr : Virtual address to return to.
*
- * Clobbers x0 - x5
+ * Clobbers x0 - x6
*/
ENTRY(enable_boot_cpu_mm)

-        mov   x5, lr
+        mov   x6, lr

bl create_page_tables
load_paddr x0, boot_pgtable

+        mov   x1, #0        /* No extra SCTLR flags */
         bl    enable_mmu
-        mov   lr, x5
+        mov   lr, x6

/*
* The MMU is turned on and we are in the 1:1 mapping. Switch

The same logic could be used for arm32.

Cheers,

Signature · 1 line
Julien Grall
author
#25 lore raw

Re: [PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S

Hi Julien,

Quoted 81 lines · Julien Grall

Hi Henry,

Quoted 76 lines · Henry Wang

Currently mmu_init_secondary_cpu() only enforces the page table should not contain mapping that are both Writable and eXecutables after boot. To ease the arch/arm/mm.c split work, fold this function to head.S.
Introduce assembly macro pt_enforce_wxn for both arm32 and arm64. For arm64, the macro is called at the end of enable_secondary_cpu_mm(). For arm32, the macro is called before secondary CPUs jumping into the C world.

---
v6:
- New patch.

---
 xen/arch/arm/arm32/head.S     | 20 ++++++++++++++++++++
 xen/arch/arm/arm64/mmu/head.S | 21 +++++++++++++++++++++
 xen/arch/arm/include/asm/mm.h |  2 --
 xen/arch/arm/mm.c             |  6 ------
 xen/arch/arm/smpboot.c        |  2 --
 5 files changed, 41 insertions(+), 10 deletions(-)
xen/arch/arm/arm32/head.S +19 -0
index 33b038e7e0..39218cf15f 100644
@@ -83,6 +83,25 @@
         isb
 .endm
 +/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+        mrc   CP32(\tmp, HSCTLR)
+        orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+        dsb
+        mcr   CP32(\tmp, HSCTLR)
+        /*
+         * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
+         * before flushing the TLBs.
+         */
+        isb
+        flush_xen_tlb_local \tmp
+.endm
+
 /*
  * Common register usage in this file:
  *   r0  -
@@ -254,6 +273,7 @@ secondary_switched:
         /* Use a virtual address to access the UART. */
         mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
 #endif
+        pt_enforce_wxn r0
         PRINT("- Ready -\r\n")
         /* Jump to C world */
         mov_w r2, start_secondary
xen/arch/arm/arm64/mmu/head.S +12 -0
index a5271e3880..25028bdf07 100644
@@ -31,6 +31,25 @@
         isb
 .endm
 +/*
+ * Enforce Xen page-tables do not contain mapping that are both
+ * Writable and eXecutables.
+ *
+ * This should be called on each secondary CPU.
+ */
+.macro pt_enforce_wxn tmp
+       mrs   \tmp, SCTLR_EL2
+       orr   \tmp, \tmp, #SCTLR_Axx_ELx_WXN
+       dsb   sy
+       msr   SCTLR_EL2, \tmp
+       /*
+        * The TLBs may cache SCTLR_EL2.WXN. So ensure it is synchronized
Quoted 8 lines

xen/arch/arm/arm64/mmu/head.S line 51

Quoted 6 lines
xen/arch/arm/arm64/mmu/head.S +6 -0
+        * before flushing the TLBs.
+        */
+       isb
+       flush_xen_tlb_local
+.endm
+

It would be preferable if we can set the flag right when the MMU is initialized enabled configured. This would avoid the extra TLB flush and SCTLR dance. How about the following (not compiled/cleaned) code:

Thank you for the detailed information. Sure, I will try below code and keep you updated about if it works. Will update the patch accordingly.

Quoted 69 lines
xen/arch/arm/arm64/mmu/head.S +1 -1
index a5271e388071..6b19d15ff89f 100644
@@ -264,10 +264,11 @@ ENDPROC(create_page_tables)
 * Inputs:
 *   x0 : Physical address of the page tables.
 *
- * Clobbers x0 - x4
+ * Clobbers x0 - x6
 */

enable_mmu:
mov x4, x0

xen/arch/arm/arm64/mmu/head.S +10 -6
+        mov   x5, x1
        PRINT("- Turning on paging -\r\n")
 
        /*
@@ -283,6 +284,7 @@ enable_mmu:
        mrs   x0, SCTLR_EL2
        orr   x0, x0, #SCTLR_Axx_ELx_M  /* Enable MMU */
        orr   x0, x0, #SCTLR_Axx_ELx_C  /* Enable D-cache */
+        orr   x0, x0, x5                /* Enable extra flags */
        dsb   sy                     /* Flush PTE writes and finish reads */
        msr   SCTLR_EL2, x0          /* now paging is enabled */
        isb                          /* Now, flush the icache */
@@ -297,16 +299,17 @@ ENDPROC(enable_mmu)
 * Inputs:
 *   lr : Virtual address to return to.
 *
- * Clobbers x0 - x5
+ * Clobbers x0 - x6
 */
 ENTRY(enable_secondary_cpu_mm)
-        mov   x5, lr
+        mov   x6, lr
 
        load_paddr x0, init_ttbr
        ldr   x0, [x0]
 
+        mov   x1, #SCTLR_Axx_ELx_WXN        /* Enable WxN from the start */
        bl    enable_mmu
-        mov   lr, x5
+        mov   lr, x6
 
        /* Return to the virtual address requested by the caller. */
        ret
@@ -320,16 +323,17 @@ ENDPROC(enable_secondary_cpu_mm)
 * Inputs:
 *   lr : Virtual address to return to.
 *
- * Clobbers x0 - x5
+ * Clobbers x0 - x6
 */
 ENTRY(enable_boot_cpu_mm)
-        mov   x5, lr
+        mov   x6, lr
 
        bl    create_page_tables
        load_paddr x0, boot_pgtable
 
+        mov   x1, #0        /* No extra SCTLR flags */
        bl    enable_mmu
-        mov   lr, x5
+        mov   lr, x6
 
        /*
         * The MMU is turned on and we are in the 1:1 mapping. Switch
Quoted 1 line

The same logic could be used for arm32.

Sure. Will do that together.

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#26 lore raw

PATCH v6 9/13 xen/arm: Extract MMU-specific MM code

In reply to Henry Wang

Currently, most of the code is in arm/mm.{c,h} and arm/arm64/mm.c is MMU-specific. To make the MM code extendable, this commit extracts the MMU-specific MM code.

Extract the boot CPU MM bringup code from arm/mm.c to mmu/setup.c. Move arm/arm64/mm.c to arm/arm64/mmu/mm.c. Since the function setup_directmap_mappings() has different implementations between arm32 and arm64, move their arch-specific implementation to arch-specific arm{32,64}/mmu/mm.c instead using #ifdef again.

For header files, move MMU-related function declarations in asm/mm.h and the declaration of dump_pt_walk() in asm/page.h to asm/mmu/mm.h

Also modify the build system (Makefiles in this case) to pick above mentioned code changes.

Take the opportunity to fix the in-code comment coding styles when possible, and drop the unnecessary #include headers in the original arm/mm.c.

---
v6:
- Rework the original patch
"[v5,07/13] xen/arm: Extract MMU-specific code"

---
 xen/arch/arm/arm32/Makefile       |   1 +
 xen/arch/arm/arm32/mmu/Makefile   |   1 +
 xen/arch/arm/arm32/mmu/mm.c       |  31 +++
 xen/arch/arm/arm64/Makefile       |   1 -
 xen/arch/arm/arm64/mmu/Makefile   |   1 +
 xen/arch/arm/arm64/{ => mmu}/mm.c |  37 +++
 xen/arch/arm/include/asm/mm.h     |  22 +-
 xen/arch/arm/include/asm/mmu/mm.h |  47 ++++
 xen/arch/arm/include/asm/page.h   |  15 --
 xen/arch/arm/mm.c                 | 381 ------------------------------
 xen/arch/arm/mmu/Makefile         |   1 +
 xen/arch/arm/mmu/setup.c          | 345 +++++++++++++++++++++++++++
 12 files changed, 470 insertions(+), 413 deletions(-)
 create mode 100644 xen/arch/arm/arm32/mmu/Makefile
 create mode 100644 xen/arch/arm/arm32/mmu/mm.c
 rename xen/arch/arm/arm64/{ => mmu}/mm.c (75%)
 create mode 100644 xen/arch/arm/include/asm/mmu/mm.h
 create mode 100644 xen/arch/arm/mmu/setup.c
xen/arch/arm/arm32/Makefile +1 -0
index 520fb42054..40a2b4803f 100644
@@ -1,4 +1,5 @@
 obj-y += lib/
+obj-$(CONFIG_MMU) += mmu/
 
 obj-$(CONFIG_EARLY_PRINTK) += debug.o
 obj-y += domctl.o
xen/arch/arm/arm32/mmu/Makefile new file +1 -0
new file mode 100644
index 0000000000..b18cec4836
@@ -0,0 +1 @@
+obj-y += mm.o
xen/arch/arm/arm32/mmu/mm.c new file +31 -0
new file mode 100644
index 0000000000..647baf4a81
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <xen/init.h>
+#include <asm/fixmap.h>
+
+/*
+ * 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;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/arm64/Makefile +0 -1
index f89d5fb4fb..72161ff22e 100644
@@ -11,7 +11,6 @@ obj-y += entry.o
 obj-y += head.o
 obj-y += insn.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
-obj-y += mm.o
 obj-y += smc.o
 obj-y += smpboot.o
 obj-$(CONFIG_ARM64_SVE) += sve.o sve-asm.o
xen/arch/arm/arm64/mmu/Makefile +1 -0
index 3340058c08..a8a750a3d0 100644
@@ -1 +1,2 @@
 obj-y += head.o
+obj-y += mm.o
xen/arch/arm/arm64/mmu/mm.c renamed from xen/arch/arm/arm64/mm.c +37 -0
similarity index 75%
rename from xen/arch/arm/arm64/mm.c
rename to xen/arch/arm/arm64/mmu/mm.c
index 78b7c7eb00..36073041ed 100644
@@ -151,6 +151,43 @@ void __init switch_ttbr(uint64_t ttbr)
     update_identity_mapping(false);
 }
 
+/* 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");
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/include/asm/mm.h +6 -16
index a66aa219b1..ded6d076c5 100644
@@ -14,6 +14,12 @@
 # error "unknown ARM variant"
 #endif
 
+#if defined(CONFIG_MMU)
+# include <asm/mmu/mm.h>
+#else
+# error "Unknown memory management layout"
+#endif
+
 /* Align Xen to a 2 MiB boundary. */
 #define XEN_PADDR_ALIGN (1 << 21)
 
@@ -170,13 +176,6 @@ extern uint64_t init_ttbr;
 
 extern paddr_t phys_offset;
 
-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) ({                                 \
@@ -199,7 +198,6 @@ extern unsigned long directmap_base_pdx;
 
 #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;
 
@@ -209,19 +207,11 @@ extern unsigned long frametable_base_pdx;
 extern void setup_pagetables(unsigned long boot_phys_offset);
 /* Map FDT in boot pagetable */
 extern void *early_fdt_map(paddr_t fdt_paddr);
-/* Switch to a new root page-tables */
-extern void switch_ttbr(uint64_t ttbr);
 /* Remove early mappings */
 extern void remove_early_mappings(void);
 /* Allocate and initialise pagetables for a secondary CPU. Sets init_ttbr to the
  * new page table */
 extern int init_secondary_pagetables(int cpu);
-/*
- * 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 */
xen/arch/arm/include/asm/mmu/mm.h new file +47 -0

47 lines not on this page · raw message

xen/arch/arm/include/asm/page.h +0 -15
index 657c4b33db..ac65f0277a 100644
@@ -257,21 +257,6 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
 /* Flush the dcache for an entire page. */
 void flush_page_to_ram(unsigned long mfn, bool sync_icache);
 
-/*
- * Print a walk of a page table or p2m
- *
- * ttbr is the base address register (TTBR0_EL2 or VTTBR_EL2)
- * addr is the PA or IPA to translate
- * root_level is the starting level of the page table
- *   (e.g. TCR_EL2.SL0 or VTCR_EL2.SL0 )
- * nr_root_tables is the number of concatenated tables at the root.
- *   this can only be != 1 for P2M walks starting at the first or
- *   subsequent level.
- */
-void dump_pt_walk(paddr_t ttbr, paddr_t addr,
-                  unsigned int root_level,
-                  unsigned int nr_root_tables);
-
 /* Print a walk of the hypervisor's page tables for a virtual addr. */
 extern void dump_hyp_walk(vaddr_t addr);
 /* Print a walk of the p2m for a domain for a physical address. */
xen/arch/arm/mm.c +0 -381

412 lines not on this page · raw message

xen/arch/arm/mmu/Makefile +1 -0
index 0e82015ee1..98aea965df 100644
@@ -1,2 +1,3 @@
 obj-y += pt.o
+obj-y += setup.o
 obj-y += smpboot.o
xen/arch/arm/mmu/setup.c new file +345 -0

345 lines not on this page · raw message

Signature · 1 line
2.25.1
#27 lore raw

Re: [PATCH v6 09/13] xen/arm: Extract MMU-specific MM code

Hi Henry,

Quoted 239 lines · Henry Wang

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.

Currently, most of the code is in arm/mm.{c,h} and arm/arm64/mm.c is MMU-specific. To make the MM code extendable, this commit extracts the MMU-specific MM code.

Extract the boot CPU MM bringup code from arm/mm.c to mmu/setup.c. Move arm/arm64/mm.c to arm/arm64/mmu/mm.c. Since the function setup_directmap_mappings() has different implementations between arm32 and arm64, move their arch-specific implementation to arch-specific arm{32,64}/mmu/mm.c instead using #ifdef again.

For header files, move MMU-related function declarations in asm/mm.h and the declaration of dump_pt_walk() in asm/page.h to asm/mmu/mm.h

Also modify the build system (Makefiles in this case) to pick above mentioned code changes.

Take the opportunity to fix the in-code comment coding styles when possible, and drop the unnecessary #include headers in the original arm/mm.c.

---
v6:
- Rework the original patch
"[v5,07/13] xen/arm: Extract MMU-specific code"

---
  xen/arch/arm/arm32/Makefile       |   1 +
  xen/arch/arm/arm32/mmu/Makefile   |   1 +
  xen/arch/arm/arm32/mmu/mm.c       |  31 +++
  xen/arch/arm/arm64/Makefile       |   1 -
  xen/arch/arm/arm64/mmu/Makefile   |   1 +
  xen/arch/arm/arm64/{ => mmu}/mm.c |  37 +++
  xen/arch/arm/include/asm/mm.h     |  22 +-
  xen/arch/arm/include/asm/mmu/mm.h |  47 ++++
  xen/arch/arm/include/asm/page.h   |  15 --
  xen/arch/arm/mm.c                 | 381 ------------------------------
  xen/arch/arm/mmu/Makefile         |   1 +
  xen/arch/arm/mmu/setup.c          | 345 +++++++++++++++++++++++++++
  12 files changed, 470 insertions(+), 413 deletions(-)

create mode 100644 xen/arch/arm/arm32/mmu/Makefile create mode 100644 xen/arch/arm/arm32/mmu/mm.c rename xen/arch/arm/arm64/{ => mmu}/mm.c (75%) create mode 100644 xen/arch/arm/include/asm/mmu/mm.h create mode 100644 xen/arch/arm/mmu/setup.c

xen/arch/arm/arm32/Makefile +1 -0
index 520fb42054..40a2b4803f 100644
@@ -1,4 +1,5 @@
  obj-y += lib/
+obj-$(CONFIG_MMU) += mmu/
 
  obj-$(CONFIG_EARLY_PRINTK) += debug.o
  obj-y += domctl.o
xen/arch/arm/arm32/mmu/Makefile new file +1 -0
new file mode 100644
index 0000000000..b18cec4836
@@ -0,0 +1 @@
+obj-y += mm.o
xen/arch/arm/arm32/mmu/mm.c new file +31 -0
new file mode 100644
index 0000000000..647baf4a81
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <xen/init.h>
+#include <asm/fixmap.h>
+
+/*
+ * 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;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/arm64/Makefile +0 -1
index f89d5fb4fb..72161ff22e 100644
@@ -11,7 +11,6 @@ obj-y += entry.o
  obj-y += head.o
  obj-y += insn.o
  obj-$(CONFIG_LIVEPATCH) += livepatch.o
-obj-y += mm.o
  obj-y += smc.o
  obj-y += smpboot.o
  obj-$(CONFIG_ARM64_SVE) += sve.o sve-asm.o
xen/arch/arm/arm64/mmu/Makefile +1 -0
index 3340058c08..a8a750a3d0 100644
@@ -1 +1,2 @@
  obj-y += head.o
+obj-y += mm.o
xen/arch/arm/arm64/mmu/mm.c renamed from xen/arch/arm/arm64/mm.c +37 -0
similarity index 75%
rename from xen/arch/arm/arm64/mm.c
rename to xen/arch/arm/arm64/mmu/mm.c
index 78b7c7eb00..36073041ed 100644
@@ -151,6 +151,43 @@ void __init switch_ttbr(uint64_t ttbr)
      update_identity_mapping(false);
  }
 
+/* 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");
+}
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/include/asm/mm.h +6 -16
index a66aa219b1..ded6d076c5 100644
@@ -14,6 +14,12 @@
  # error "unknown ARM variant"
  #endif
 
+#if defined(CONFIG_MMU)
+# include <asm/mmu/mm.h>
+#else
+# error "Unknown memory management layout"
+#endif
+
  /* Align Xen to a 2 MiB boundary. */
  #define XEN_PADDR_ALIGN (1 << 21)
 
@@ -170,13 +176,6 @@ extern uint64_t init_ttbr;
 
  extern paddr_t phys_offset;
 
-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) ({                                 \
@@ -199,7 +198,6 @@ extern unsigned long directmap_base_pdx;
 
  #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;
 
@@ -209,19 +207,11 @@ extern unsigned long frametable_base_pdx;
  extern void setup_pagetables(unsigned long boot_phys_offset);
  /* Map FDT in boot pagetable */
  extern void *early_fdt_map(paddr_t fdt_paddr);
-/* Switch to a new root page-tables */
-extern void switch_ttbr(uint64_t ttbr);
  /* Remove early mappings */
  extern void remove_early_mappings(void);
  /* Allocate and initialise pagetables for a secondary CPU. Sets init_ttbr to the
   * new page table */
  extern int init_secondary_pagetables(int cpu);
-/*
- * 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 */
xen/arch/arm/include/asm/mmu/mm.h new file +0 -0
new file mode 100644
index 0000000000..5e3b14519b

xen/arch/arm/include/asm/mmu/mm.h line 5

Quoted 5 lines
xen/arch/arm/include/asm/mmu/mm.h new file +5 -0
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __ARM_MMU_MM_H__
+#define __ARM_MMU_MM_H__
+
+extern mfn_t directmap_mfn_start, directmap_mfn_end;

As you are declaring them for MMU specific , you also need this change :-

xen/arch/arm/setup.c +0 -0
index 89ecb54be2..19b60c5d1b 100644
@@ -670,7 +670,7 @@ void __init populate_boot_allocator(void)
 

             s = bootinfo.reserved_mem.bank[i].start;
             e = s + bootinfo.reserved_mem.bank[i].size;
-#ifdef CONFIG_ARM_32
+#if (CONFIG_ARM_32 && CONFIG_MMU)
             /* Avoid the xenheap, note that the xenheap cannot across a bank */ if ( s <= mfn_to_maddr(directmap_mfn_start) &&
                  e >= mfn_to_maddr(directmap_mfn_end) )
@@ -708,7 +708,7 @@ void __init populate_boot_allocator(void)
             if ( e > bank_end )
                 e = bank_end;

-#ifdef CONFIG_ARM_32
+#if (CONFIG_ARM_32 && CONFIG_MMU)
             /* Avoid the xenheap */
             if ( s < mfn_to_maddr(directmap_mfn_end) && mfn_to_maddr(directmap_mfn_start) < e )

So that directmap_mfn_end and directmap_mfn_start is used only when MMU is enabled.

- Ayan

Quoted 850 lines
xen/arch/arm/include/asm/mmu/mm.h new file +42 -0
+extern vaddr_t directmap_virt_end;
+#ifdef CONFIG_ARM_64
+extern vaddr_t directmap_virt_start;
+extern unsigned long directmap_base_pdx;
+#endif
+
+#define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)
+
+/*
+ * Print a walk of a page table or p2m
+ *
+ * ttbr is the base address register (TTBR0_EL2 or VTTBR_EL2)
+ * addr is the PA or IPA to translate
+ * root_level is the starting level of the page table
+ *   (e.g. TCR_EL2.SL0 or VTCR_EL2.SL0 )
+ * nr_root_tables is the number of concatenated tables at the root.
+ *   this can only be != 1 for P2M walks starting at the first or
+ *   subsequent level.
+ */
+void dump_pt_walk(paddr_t ttbr, paddr_t addr,
+                  unsigned int root_level,
+                  unsigned int nr_root_tables);
+
+/* Switch to a new root page-tables */
+extern void switch_ttbr(uint64_t 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 /* __ARM_MMU_MM_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/page.h +0 -15
index 657c4b33db..ac65f0277a 100644
@@ -257,21 +257,6 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
  /* Flush the dcache for an entire page. */
  void flush_page_to_ram(unsigned long mfn, bool sync_icache);
 
-/*
- * Print a walk of a page table or p2m
- *
- * ttbr is the base address register (TTBR0_EL2 or VTTBR_EL2)
- * addr is the PA or IPA to translate
- * root_level is the starting level of the page table
- *   (e.g. TCR_EL2.SL0 or VTCR_EL2.SL0 )
- * nr_root_tables is the number of concatenated tables at the root.
- *   this can only be != 1 for P2M walks starting at the first or
- *   subsequent level.
- */
-void dump_pt_walk(paddr_t ttbr, paddr_t addr,
-                  unsigned int root_level,
-                  unsigned int nr_root_tables);
-
  /* Print a walk of the hypervisor's page tables for a virtual addr. */
  extern void dump_hyp_walk(vaddr_t addr);
  /* Print a walk of the p2m for a domain for a physical address. */
xen/arch/arm/mm.c +0 -381
index 3ee74542ba..eeb65ca6bb 100644
@@ -11,139 +11,19 @@
  #include <xen/domain_page.h>
  #include <xen/grant_table.h>
  #include <xen/guest_access.h>
-#include <xen/libfdt/libfdt.h>
  #include <xen/mm.h>
-#include <xen/sizes.h>
 
  #include <xsm/xsm.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))
-
-/* 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_PAGE_TABLE(xen_pgtable);
-static DEFINE_PAGE_TABLE(xen_first);
-#define THIS_CPU_PGTABLE xen_pgtable
-#else
-/* 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 */
-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_TABLES(xen_xenmap, XEN_NR_ENTRIES(2));
-
-/* Non-boot CPUs use this to find the correct pagetables. */
-uint64_t init_ttbr;
-
-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;
 
-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
-    /*
-     * The first few slots of the L0 table is reserved for the identity
-     * mapping. Check that none of the other regions are overlapping
-     * with it.
-     */
-#define CHECK_OVERLAP_WITH_IDMAP(virt) \
-    BUILD_BUG_ON(zeroeth_table_offset(virt) < IDENTITY_MAPPING_AREA_NR_L0)
-
-    CHECK_OVERLAP_WITH_IDMAP(XEN_VIRT_START);
-    CHECK_OVERLAP_WITH_IDMAP(VMAP_VIRT_START);
-    CHECK_OVERLAP_WITH_IDMAP(FRAMETABLE_VIRT_START);
-    CHECK_OVERLAP_WITH_IDMAP(DIRECTMAP_VIRT_START);
-#undef CHECK_OVERLAP_WITH_IDMAP
-#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))
-
-#define CHECK_DIFFERENT_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);
-
-    /*
-     * For arm32, the temporary mapping will re-use the domheap
-     * first slot and the second slots will match.
-     */
-#ifdef CONFIG_ARM_32
-    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START, DOMHEAP_VIRT_START);
-    CHECK_DIFFERENT_SLOT(first, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
-    CHECK_SAME_SLOT(second, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
-#endif
-
-#undef CHECK_SAME_SLOT
-#undef CHECK_DIFFERENT_SLOT
-}
-
  void flush_page_to_ram(unsigned long mfn, bool sync_icache)
  {
      void *v = map_domain_page(_mfn(mfn));
@@ -163,222 +43,6 @@ void flush_page_to_ram(unsigned long mfn, bool sync_icache)
          invalidate_icache();
  }
 
-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();
-}
-
-/* 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;
-
-    arch_setup_page_tables();
-
-#ifdef CONFIG_ARM_64
-    pte = pte_of_xenaddr((uintptr_t)xen_first);
-    pte.pt.table = 1;
-    pte.pt.xn = 0;
-    xen_pgtable[zeroeth_table_offset(XEN_VIRT_START)] = pte;
-
-    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 pages and protect them separately. */
-    for ( i = 0; i < XEN_NR_ENTRIES(3); i++ )
-    {
-        vaddr_t va = XEN_VIRT_START + (i << PAGE_SHIFT);
-
-        if ( !is_kernel(va) )
-            break;
-        pte = pte_of_xenaddr(va);
-        pte.pt.table = 1; /* third level 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 */
-    for ( i = 0; i < XEN_NR_ENTRIES(2); i++ )
-    {
-        vaddr_t va = XEN_VIRT_START + (i << XEN_PT_LEVEL_SHIFT(2));
-
-        pte = pte_of_xenaddr((vaddr_t)(xen_xenmap + i * XEN_PT_LPAE_ENTRIES));
-        pte.pt.table = 1;
-        xen_second[second_table_offset(va)] = 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
-}
-
-#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)
  {
@@ -418,51 +82,6 @@ void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
      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);
-}
-
-/* Release all __init and __initdata ranges to be reused */
-void free_init_memory(void)
-{
-    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);
-}
-
  int steal_page(
      struct domain *d, struct page_info *page, unsigned int memflags)
  {
xen/arch/arm/mmu/Makefile +1 -0
index 0e82015ee1..98aea965df 100644
@@ -1,2 +1,3 @@
  obj-y += pt.o
+obj-y += setup.o
  obj-y += smpboot.o
xen/arch/arm/mmu/setup.c new file +345 -1
new file mode 100644
index 0000000000..eb0dda00dc
@@ -0,0 +1,345 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * xen/arch/arm/mmu/setup.c
+ *
+ * MMU system boot CPU MM bringup code.
+ */
+
+#include <xen/init.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/sizes.h>
+
+#include <asm/fixmap.h>
+
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef mfn_to_virt
+#define mfn_to_virt(mfn) __mfn_to_virt(mfn_x(mfn))
+
+/* 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_PAGE_TABLE(xen_pgtable);
+static DEFINE_PAGE_TABLE(xen_first);
+#define THIS_CPU_PGTABLE xen_pgtable
+#else
+/* 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 */
+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_TABLES(xen_xenmap, XEN_NR_ENTRIES(2));
+
+/* Non-boot CPUs use this to find the correct pagetables. */
+uint64_t init_ttbr;
+
+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
+
+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
+    /*
+     * The first few slots of the L0 table is reserved for the identity
+     * mapping. Check that none of the other regions are overlapping
+     * with it.
+     */
+#define CHECK_OVERLAP_WITH_IDMAP(virt) \
+    BUILD_BUG_ON(zeroeth_table_offset(virt) < IDENTITY_MAPPING_AREA_NR_L0)
+
+    CHECK_OVERLAP_WITH_IDMAP(XEN_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(VMAP_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(FRAMETABLE_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(DIRECTMAP_VIRT_START);
+#undef CHECK_OVERLAP_WITH_IDMAP
+#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))
+
+#define CHECK_DIFFERENT_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);
+
+    /*
+     * For arm32, the temporary mapping will re-use the domheap
+     * first slot and the second slots will match.
+     */
+#ifdef CONFIG_ARM_32
+    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START, DOMHEAP_VIRT_START);
+    CHECK_DIFFERENT_SLOT(first, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
+    CHECK_SAME_SLOT(second, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
+#endif
+
+#undef CHECK_SAME_SLOT
+#undef CHECK_DIFFERENT_SLOT
+}
+
+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();
+}
+
+/*
+ * 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;
+
+    arch_setup_page_tables();
+
+#ifdef CONFIG_ARM_64
+    pte = pte_of_xenaddr((uintptr_t)xen_first);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+    xen_pgtable[zeroeth_table_offset(XEN_VIRT_START)] = pte;
+
+    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 pages and protect them separately. */
+    for ( i = 0; i < XEN_NR_ENTRIES(3); i++ )
+    {
+        vaddr_t va = XEN_VIRT_START + (i << PAGE_SHIFT);
+
+        if ( !is_kernel(va) )
+            break;
+        pte = pte_of_xenaddr(va);
+        pte.pt.table = 1; /* third level 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 */
+    for ( i = 0; i < XEN_NR_ENTRIES(2); i++ )
+    {
+        vaddr_t va = XEN_VIRT_START + (i << XEN_PT_LEVEL_SHIFT(2));
+
+        pte = pte_of_xenaddr((vaddr_t)(xen_xenmap + i * XEN_PT_LPAE_ENTRIES));
+        pte.pt.table = 1;
+        xen_second[second_table_offset(va)] = 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
+}
+
+void *__init arch_vmap_virt_end(void)
+{
+    return (void *)(VMAP_VIRT_START + VMAP_VIRT_SIZE);
+}
+
+/* Release all __init and __initdata ranges to be reused */
+void free_init_memory(void)
+{
+    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);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--

2.25.1

author
#28 lore raw

Re: [PATCH v6 09/13] xen/arm: Extract MMU-specific MM code

Hi Ayan,

Quoted 32 lines · Ayan Kumar Halder

Hi Henry,

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+extern mfn_t directmap_mfn_start, directmap_mfn_end;

As you are declaring them for MMU specific , you also need this change :-

xen/arch/arm/setup.c +2 -2
index 89ecb54be2..19b60c5d1b 100644
@@ -670,7 +670,7 @@ void __init populate_boot_allocator(void)
 
             s = bootinfo.reserved_mem.bank[i].start;
             e = s + bootinfo.reserved_mem.bank[i].size;
-#ifdef CONFIG_ARM_32
+#if (CONFIG_ARM_32 && CONFIG_MMU)
             /* Avoid the xenheap, note that the xenheap cannot across a bank */
             if ( s <= mfn_to_maddr(directmap_mfn_start) &&
                  e >= mfn_to_maddr(directmap_mfn_end) )
@@ -708,7 +708,7 @@ void __init populate_boot_allocator(void)
             if ( e > bank_end )
                 e = bank_end;
 
-#ifdef CONFIG_ARM_32
+#if (CONFIG_ARM_32 && CONFIG_MMU)
             /* Avoid the xenheap */
             if ( s < mfn_to_maddr(directmap_mfn_end) &&
                  mfn_to_maddr(directmap_mfn_start) < e )
Quoted 1 line

So that directmap_mfn_end and directmap_mfn_start is used only when MMU is enabled.

I am not 100% sure on this, because currently there is no MPU code at all, indicating all setup.c is MMU specific. In this case adding “&& CONFIG_MMU” seems a little bit redundant to me. But I agree you made a point and it is correct that when the MPU code is in, these “directmap” part should be gated with CONFIG_MMU (or maybe split the code between arm32/arm64 to different helpers to avoid #ifdef). Hence I would prefer doing these change when the MPU code is added.

Let’s see what maintainers will say. I am happy to do the change once we have an agreement.

Kind regards,
Henry

Quoted 2 lines

- Ayan

#29 lore raw

Re: [PATCH v6 09/13] xen/arm: Extract MMU-specific MM code

Quoted 43 lines · Henry Wang

Hi Ayan,

Quoted 32 lines · Ayan Kumar Halder

Hi Henry,

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+extern mfn_t directmap_mfn_start, directmap_mfn_end;

As you are declaring them for MMU specific , you also need this change :-

xen/arch/arm/setup.c +2 -2
index 89ecb54be2..19b60c5d1b 100644
@@ -670,7 +670,7 @@ void __init populate_boot_allocator(void)
 
             s = bootinfo.reserved_mem.bank[i].start;
             e = s + bootinfo.reserved_mem.bank[i].size;
-#ifdef CONFIG_ARM_32
+#if (CONFIG_ARM_32 && CONFIG_MMU)
             /* Avoid the xenheap, note that the xenheap cannot across a bank */
             if ( s <= mfn_to_maddr(directmap_mfn_start) &&
                  e >= mfn_to_maddr(directmap_mfn_end) )
@@ -708,7 +708,7 @@ void __init populate_boot_allocator(void)
             if ( e > bank_end )
                 e = bank_end;
 
-#ifdef CONFIG_ARM_32
+#if (CONFIG_ARM_32 && CONFIG_MMU)
             /* Avoid the xenheap */
             if ( s < mfn_to_maddr(directmap_mfn_end) &&
                  mfn_to_maddr(directmap_mfn_start) < e )
Quoted 1 line

So that directmap_mfn_end and directmap_mfn_start is used only when MMU is enabled.

I am not 100% sure on this, because currently there is no MPU code at all, indicating all setup.c is MMU specific. In this case adding “&& CONFIG_MMU” seems a little bit redundant to me. But I agree you made a point and it is correct that when the MPU code is in, these “directmap” part should be gated with CONFIG_MMU (or maybe split the code between arm32/arm64 to different helpers to avoid #ifdef). Hence I would prefer doing these change when the MPU code is added.

Quoted 2 lines

Let’s see what maintainers will say. I am happy to do the change once we have an agreement.

It might be wiser to add && CONFIG_MMU when the MPU code is added in case we decide to move it / shape it differently.

author
#30 lore raw

PATCH v6 10/13 xen/arm: Split MMU-specific setup_mm() and related code out

In reply to Henry Wang

setup_mm() is used for Xen to setup memory management subsystem, such as boot allocator, direct-mapping, xenheap initialization, frametable and static memory pages, at boot time.

We could inherit some components seamlessly for MPU support, such as the setup of boot allocator, whilst we need to implement some components differently for MPU, such as xenheap, etc. Also, there are some components that is specific to MMU only, for example the direct-mapping.

Therefore in this commit, we split the MMU-specific setup_mm() and related code out. Since arm32 and arm64 have completely different setup_mm() implementation, take the opportunity to split the arch-specific setup_mm() to arch-specific files, so that we can avoid #ifdef. Also, make init_pdx(), init_staticmem_pages(), setup_mm(), and populate_boot_allocator() public for future MPU implementation.

With above code movement, mark setup_directmap_mappings() as static because the only caller of this function is now in the same file with it. Drop the original setup_directmap_mappings() declaration and move the in-code comment on top of the declaration on top of the function implementation.

---
v6:
- Rework the original patch:
[v5,10/13] xen/arm: mmu: move MMU-specific setup_mm to mmu/setup.c

---
 xen/arch/arm/arm32/mmu/mm.c       | 278 ++++++++++++++++++++++++-
 xen/arch/arm/arm64/mmu/mm.c       |  51 ++++-
 xen/arch/arm/include/asm/mmu/mm.h |   6 -
 xen/arch/arm/include/asm/setup.h  |   5 +
 xen/arch/arm/setup.c              | 324 +-----------------------------
 5 files changed, 331 insertions(+), 333 deletions(-)
xen/arch/arm/arm32/mmu/mm.c +274 -4
index 647baf4a81..94d6cab49c 100644
@@ -1,14 +1,21 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 
 #include <xen/init.h>
+#include <xen/lib.h>
+#include <xen/libfdt/libfdt-xen.h>
+#include <xen/param.h>
+#include <xen/pfn.h>
 #include <asm/fixmap.h>
 
+static unsigned long opt_xenheap_megabytes __initdata;
+integer_param("xenheap_megabytes", opt_xenheap_megabytes);
+
 /*
- * Set up the direct-mapped xenheap:
- * up to 1GB of contiguous, always-mapped memory.
+ * 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.
  */
-void __init setup_directmap_mappings(unsigned long base_mfn,
-                                     unsigned long nr_mfns)
+static void __init setup_directmap_mappings(unsigned long base_mfn,
+                                            unsigned long nr_mfns)
 {
     int rc;
 
@@ -21,6 +28,269 @@ void __init setup_directmap_mappings(unsigned long base_mfn,
     directmap_virt_end = XENHEAP_VIRT_START + nr_mfns * PAGE_SIZE;
 }
 
+/*
+ * Returns the end address of the highest region in the range s..e
+ * with required size and alignment that does not conflict with the
+ * modules from first_mod to nr_modules.
+ *
+ * For non-recursive callers first_mod should normally be 0 (all
+ * modules and Xen itself) or 1 (all modules but not Xen).
+ */
+static paddr_t __init consider_modules(paddr_t s, paddr_t e,
+                                       uint32_t size, paddr_t align,
+                                       int first_mod)
+{
+    const struct bootmodules *mi = &bootinfo.modules;
+    int i;
+    int nr;
+
+    s = (s+align-1) & ~(align-1);
+    e = e & ~(align-1);
+
+    if ( s > e ||  e - s < size )
+        return 0;
+
+    /* First check the boot modules */
+    for ( i = first_mod; i < mi->nr_mods; i++ )
+    {
+        paddr_t mod_s = mi->module[i].start;
+        paddr_t mod_e = mod_s + mi->module[i].size;
+
+        if ( s < mod_e && mod_s < e )
+        {
+            mod_e = consider_modules(mod_e, e, size, align, i+1);
+            if ( mod_e )
+                return mod_e;
+
+            return consider_modules(s, mod_s, size, align, i+1);
+        }
+    }
+
+    /* Now check any fdt reserved areas. */
+
+    nr = fdt_num_mem_rsv(device_tree_flattened);
+
+    for ( ; i < mi->nr_mods + nr; i++ )
+    {
+        paddr_t mod_s, mod_e;
+
+        if ( fdt_get_mem_rsv_paddr(device_tree_flattened,
+                                   i - mi->nr_mods,
+                                   &mod_s, &mod_e ) < 0 )
+            /* If we can't read it, pretend it doesn't exist... */
+            continue;
+
+        /* fdt_get_mem_rsv_paddr returns length */
+        mod_e += mod_s;
+
+        if ( s < mod_e && mod_s < e )
+        {
+            mod_e = consider_modules(mod_e, e, size, align, i+1);
+            if ( mod_e )
+                return mod_e;
+
+            return consider_modules(s, mod_s, size, align, i+1);
+        }
+    }
+
+    /*
+     * i is the current bootmodule we are evaluating, across all
+     * possible kinds of bootmodules.
+     *
+     * When retrieving the corresponding reserved-memory addresses, we
+     * need to index the bootinfo.reserved_mem bank starting from 0, and
+     * only counting the reserved-memory modules. Hence, we need to use
+     * i - nr.
+     */
+    nr += mi->nr_mods;
+    for ( ; i - nr < bootinfo.reserved_mem.nr_banks; i++ )
+    {
+        paddr_t r_s = bootinfo.reserved_mem.bank[i - nr].start;
+        paddr_t r_e = r_s + bootinfo.reserved_mem.bank[i - nr].size;
+
+        if ( s < r_e && r_s < e )
+        {
+            r_e = consider_modules(r_e, e, size, align, i + 1);
+            if ( r_e )
+                return r_e;
+
+            return consider_modules(s, r_s, size, align, i + 1);
+        }
+    }
+    return e;
+}
+
+/*
+ * Find a contiguous region that fits in the static heap region with
+ * required size and alignment, and return the end address of the region
+ * if found otherwise 0.
+ */
+static paddr_t __init fit_xenheap_in_static_heap(uint32_t size, paddr_t align)
+{
+    unsigned int i;
+    paddr_t end = 0, aligned_start, aligned_end;
+    paddr_t bank_start, bank_size, bank_end;
+
+    for ( i = 0 ; i < bootinfo.reserved_mem.nr_banks; i++ )
+    {
+        if ( bootinfo.reserved_mem.bank[i].type != MEMBANK_STATIC_HEAP )
+            continue;
+
+        bank_start = bootinfo.reserved_mem.bank[i].start;
+        bank_size = bootinfo.reserved_mem.bank[i].size;
+        bank_end = bank_start + bank_size;
+
+        if ( bank_size < size )
+            continue;
+
+        aligned_end = bank_end & ~(align - 1);
+        aligned_start = (aligned_end - size) & ~(align - 1);
+
+        if ( aligned_start > bank_start )
+            /*
+             * Allocate the xenheap as high as possible to keep low-memory
+             * available (assuming the admin supplied region below 4GB)
+             * for other use (e.g. domain memory allocation).
+             */
+            end = max(end, aligned_end);
+    }
+
+    return end;
+}
+
+void __init setup_mm(void)
+{
+    paddr_t ram_start, ram_end, ram_size, e, bank_start, bank_end, bank_size;
+    paddr_t static_heap_end = 0, static_heap_size = 0;
+    unsigned long heap_pages, xenheap_pages, domheap_pages;
+    unsigned int i;
+    const uint32_t ctr = READ_CP32(CTR);
+
+    if ( !bootinfo.mem.nr_banks )
+        panic("No memory bank\n");
+
+    /* We only supports instruction caches implementing the IVIPT extension. */
+    if ( ((ctr >> CTR_L1IP_SHIFT) & CTR_L1IP_MASK) == ICACHE_POLICY_AIVIVT )
+        panic("AIVIVT instruction cache not supported\n");
+
+    init_pdx();
+
+    ram_start = bootinfo.mem.bank[0].start;
+    ram_size  = bootinfo.mem.bank[0].size;
+    ram_end   = ram_start + ram_size;
+
+    for ( i = 1; i < bootinfo.mem.nr_banks; i++ )
+    {
+        bank_start = bootinfo.mem.bank[i].start;
+        bank_size = bootinfo.mem.bank[i].size;
+        bank_end = bank_start + bank_size;
+
+        ram_size  = ram_size + bank_size;
+        ram_start = min(ram_start,bank_start);
+        ram_end   = max(ram_end,bank_end);
+    }
+
+    total_pages = ram_size >> PAGE_SHIFT;
+
+    if ( bootinfo.static_heap )
+    {
+        for ( i = 0 ; i < bootinfo.reserved_mem.nr_banks; i++ )
+        {
+            if ( bootinfo.reserved_mem.bank[i].type != MEMBANK_STATIC_HEAP )
+                continue;
+
+            bank_start = bootinfo.reserved_mem.bank[i].start;
+            bank_size = bootinfo.reserved_mem.bank[i].size;
+            bank_end = bank_start + bank_size;
+
+            static_heap_size += bank_size;
+            static_heap_end = max(static_heap_end, bank_end);
+        }
+
+        heap_pages = static_heap_size >> PAGE_SHIFT;
+    }
+    else
+        heap_pages = total_pages;
+
+    /*
+     * If the user has not requested otherwise via the command line
+     * then locate the xenheap using these constraints:
+     *
+     *  - must be contiguous
+     *  - must be 32 MiB aligned
+     *  - must not include Xen itself or the boot modules
+     *  - must be at most 1GB or 1/32 the total RAM in the system (or static
+          heap if enabled) if less
+     *  - must be at least 32M
+     *
+     * We try to allocate the largest xenheap possible within these
+     * constraints.
+     */
+    if ( opt_xenheap_megabytes )
+        xenheap_pages = opt_xenheap_megabytes << (20-PAGE_SHIFT);
+    else
+    {
+        xenheap_pages = (heap_pages/32 + 0x1fffUL) & ~0x1fffUL;
+        xenheap_pages = max(xenheap_pages, 32UL<<(20-PAGE_SHIFT));
+        xenheap_pages = min(xenheap_pages, 1UL<<(30-PAGE_SHIFT));
+    }
+
+    do
+    {
+        e = bootinfo.static_heap ?
+            fit_xenheap_in_static_heap(pfn_to_paddr(xenheap_pages), MB(32)) :
+            consider_modules(ram_start, ram_end,
+                             pfn_to_paddr(xenheap_pages),
+                             32<<20, 0);
+        if ( e )
+            break;
+
+        xenheap_pages >>= 1;
+    } while ( !opt_xenheap_megabytes && xenheap_pages > 32<<(20-PAGE_SHIFT) );
+
+    if ( ! e )
+        panic("Not enough space for xenheap\n");
+
+    domheap_pages = heap_pages - xenheap_pages;
+
+    printk("Xen heap: %"PRIpaddr"-%"PRIpaddr" (%lu pages%s)\n",
+           e - (pfn_to_paddr(xenheap_pages)), e, xenheap_pages,
+           opt_xenheap_megabytes ? ", from command-line" : "");
+    printk("Dom heap: %lu pages\n", domheap_pages);
+
+    /*
+     * We need some memory to allocate the page-tables used for the
+     * directmap mappings. So populate the boot allocator first.
+     *
+     * This requires us to set directmap_mfn_{start, end} first so the
+     * direct-mapped Xenheap region can be avoided.
+     */
+    directmap_mfn_start = _mfn((e >> PAGE_SHIFT) - xenheap_pages);
+    directmap_mfn_end = mfn_add(directmap_mfn_start, xenheap_pages);
+
+    populate_boot_allocator();
+
+    setup_directmap_mappings(mfn_x(directmap_mfn_start), xenheap_pages);
+
+    /* Frame table covers all of RAM region, including holes */
+    setup_frametable_mappings(ram_start, ram_end);
+    max_page = PFN_DOWN(ram_end);
+
+    /*
+     * The allocators may need to use map_domain_page() (such as for
+     * scrubbing pages). So we need to prepare the domheap area first.
+     */
+    if ( !init_domheap_mappings(smp_processor_id()) )
+        panic("CPU%u: Unable to prepare the domheap page-tables\n",
+              smp_processor_id());
+
+    /* Add xenheap memory that was not already added to the boot allocator. */
+    init_xenheap_pages(mfn_to_maddr(directmap_mfn_start),
+                       mfn_to_maddr(directmap_mfn_end));
+
+    init_staticmem_pages();
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/arm64/mmu/mm.c +49 -2

69 lines not on this page · raw message

xen/arch/arm/include/asm/mmu/mm.h +0 -6
index 5e3b14519b..2615fd43ab 100644
@@ -28,12 +28,6 @@ void dump_pt_walk(paddr_t ttbr, paddr_t addr,
 
 /* Switch to a new root page-tables */
 extern void switch_ttbr(uint64_t 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 /* __ARM_MMU_MM_H__ */
 
xen/arch/arm/include/asm/setup.h +5 -0
index 19dc637d55..8241fbee99 100644
@@ -156,6 +156,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);
 
+void init_pdx(void);
+void init_staticmem_pages(void);
+void populate_boot_allocator(void);
+void setup_mm(void);
+
 extern uint32_t hyp_traps_vector[];
 void init_traps(void);
 
xen/arch/arm/setup.c +3 -321

360 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#31 lore raw

PATCH v6 11/13 xen/arm: Fold pmap and fixmap into MMU system

In reply to Henry Wang

fixmap and pmap are MMU-specific features, so fold them to the MMU system. Do the folding for pmap by moving the HAS_PMAP Kconfig selection under MMU. Since none of the definitions in asm/fixmap.h actually makes sense for the MPU, so do the folding for fixmap by limiting the inclusion of asm/fixmap.h for MPU code when necessary. To guarantee that, moving the implementation of copy_from_paddr() from kernel.c to mmu/setup.c, so that inclusion of asm/fixmap.h in the kernel.c can be dropped.

---
v6:
- Rework original patch:
[v5,08/13] xen/arm: Fold pmap and fixmap into MMU system and fold in the original patch:
[v5,12/13] xen/arm: mmu: relocate copy_from_paddr() to setup.c

---
 xen/arch/arm/Kconfig     |  2 +-
 xen/arch/arm/kernel.c    | 28 ----------------------------
 xen/arch/arm/mmu/setup.c | 27 +++++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 29 deletions(-)
xen/arch/arm/Kconfig +1 -1
index eb0413336b..8a7b79b4b5 100644
@@ -15,7 +15,6 @@ config ARM
 	select HAS_DEVICE_TREE
 	select HAS_PASSTHROUGH
 	select HAS_PDX
-	select HAS_PMAP
 	select HAS_UBSAN
 	select IOMMU_FORCE_PT_SHARE
 
@@ -61,6 +60,7 @@ config PADDR_BITS
 
 config MMU
 	def_bool y
+	select HAS_PMAP
 
 source "arch/Kconfig"
 
xen/arch/arm/kernel.c +0 -28
index 508c54824d..bc3e5bd6f9 100644
@@ -16,7 +16,6 @@
 #include <xen/vmap.h>
 
 #include <asm/byteorder.h>
-#include <asm/fixmap.h>
 #include <asm/kernel.h>
 #include <asm/setup.h>
 
@@ -41,33 +40,6 @@ struct minimal_dtb_header {
 
 #define DTB_MAGIC 0xd00dfeedU
 
-/**
- * copy_from_paddr - copy data from a physical address
- * @dst: destination virtual address
- * @paddr: source physical address
- * @len: length to copy
- */
-void __init copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)
-{
-    void *src = (void *)FIXMAP_ADDR(FIXMAP_MISC);
-
-    while (len) {
-        unsigned long l, s;
-
-        s = paddr & (PAGE_SIZE-1);
-        l = min(PAGE_SIZE - s, len);
-
-        set_fixmap(FIXMAP_MISC, maddr_to_mfn(paddr), PAGE_HYPERVISOR_WC);
-        memcpy(dst, src + s, l);
-        clean_dcache_va_range(dst, l);
-        clear_fixmap(FIXMAP_MISC);
-
-        paddr += l;
-        dst += l;
-        len -= l;
-    }
-}
-
 static void __init place_modules(struct kernel_info *info,
                                  paddr_t kernbase, paddr_t kernend)
 {
xen/arch/arm/mmu/setup.c +27 -0
index eb0dda00dc..c9a4e8f700 100644
@@ -335,6 +335,33 @@ void free_init_memory(void)
     printk("Freed %ldkB init memory.\n", (long)(__init_end-__init_begin)>>10);
 }
 
+/**
+ * copy_from_paddr - copy data from a physical address
+ * @dst: destination virtual address
+ * @paddr: source physical address
+ * @len: length to copy
+ */
+void __init copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)
+{
+    void *src = (void *)FIXMAP_ADDR(FIXMAP_MISC);
+
+    while (len) {
+        unsigned long l, s;
+
+        s = paddr & (PAGE_SIZE-1);
+        l = min(PAGE_SIZE - s, len);
+
+        set_fixmap(FIXMAP_MISC, maddr_to_mfn(paddr), PAGE_HYPERVISOR_WC);
+        memcpy(dst, src + s, l);
+        clean_dcache_va_range(dst, l);
+        clear_fixmap(FIXMAP_MISC);
+
+        paddr += l;
+        dst += l;
+        len -= l;
+    }
+}
+
 /*
  * Local variables:
  * mode: C
Signature · 1 line
2.25.1
author
#32 lore raw

PATCH v6 12/13 xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm()

In reply to Henry Wang

From: Penny Zheng <[email protected]>

init_secondary_pagetables() is a function in the common code path of both MMU and future MPU support. Since "page table" is a MMU specific concept, rename init_secondary_pagetables() to a generic name prepare_secondary_mm() as the preparation for MPU support.

Take the opportunity to fix the incorrect coding style of the in-code comments.

---
v6:
- Only rename init_secondary_pagetables() to prepare_secondary_mm().

---
 xen/arch/arm/arm32/head.S     | 2 +-
 xen/arch/arm/include/asm/mm.h | 8 +++++---
 xen/arch/arm/mmu/smpboot.c    | 4 ++--
 xen/arch/arm/smpboot.c        | 2 +-
 4 files changed, 9 insertions(+), 7 deletions(-)
xen/arch/arm/arm32/head.S +1 -1
index 39218cf15f..c7b2efb8f0 100644
@@ -257,7 +257,7 @@ GLOBAL(init_secondary)
 secondary_switched:
         /*
          * Non-boot CPUs need to move on to the proper pagetables, which were
-         * setup in init_secondary_pagetables.
+         * setup in prepare_secondary_mm.
          *
          * XXX: This is not compliant with the Arm Arm.
          */
xen/arch/arm/include/asm/mm.h +5 -3
index ded6d076c5..c1ccedc4d7 100644
@@ -209,9 +209,11 @@ extern void setup_pagetables(unsigned long boot_phys_offset);
 extern void *early_fdt_map(paddr_t fdt_paddr);
 /* Remove early mappings */
 extern void remove_early_mappings(void);
-/* Allocate and initialise pagetables for a secondary CPU. Sets init_ttbr to the
- * new page table */
-extern int init_secondary_pagetables(int cpu);
+/*
+ * Allocate and initialise pagetables for a secondary CPU.
+ * Sets init_ttbr to the new page table.
+ */
+extern int prepare_secondary_mm(int cpu);
 /* 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 */
xen/arch/arm/mmu/smpboot.c +2 -2
index a673989d09..f489a19f70 100644
@@ -64,7 +64,7 @@ static void clear_boot_pagetables(void)
 }
 
 #ifdef CONFIG_ARM_64
-int init_secondary_pagetables(int cpu)
+int prepare_secondary_mm(int cpu)
 {
     clear_boot_pagetables();
 
@@ -77,7 +77,7 @@ int init_secondary_pagetables(int cpu)
     return 0;
 }
 #else
-int init_secondary_pagetables(int cpu)
+int prepare_secondary_mm(int cpu)
 {
     lpae_t *first;
 
xen/arch/arm/smpboot.c +1 -1
index ade2c77cf9..f7558b2c8c 100644
@@ -446,7 +446,7 @@ int __cpu_up(unsigned int cpu)
 
     printk("Bringing up CPU%d\n", cpu);
 
-    rc = init_secondary_pagetables(cpu);
+    rc = prepare_secondary_mm(cpu);
     if ( rc < 0 )
         return rc;
 
Signature · 1 line
2.25.1
author
#33 lore raw

PATCH v6 13/13 xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

In reply to Henry Wang

From: Penny Zheng <[email protected]>

Current P2M implementation is designed for MMU system only. We move the MMU-specific codes into mmu/p2m.c, and only keep generic codes in p2m.c, like VMID allocator, etc. We also move MMU-specific definitions and declarations to mmu/p2m.h, such as p2m_tlb_flush_sync(). Also expose previously static functions p2m_vmid_allocator_init(), p2m_alloc_vmid(), and setup_virt_paging_one() for further MPU usage.

With the code movement, global variable max_vmid is used in multiple files instead of a single file (and will be used in MPU P2M implementation), declare it in the header and remove the "static" of this variable.

---
v6:
- Also move relinquish_p2m_mapping() to mmu/p2m.c, make __p2m_set_entry() static.
- Also move p2m_clear_root_pages() and p2m_flush_vm() to mmu/p2m.c.
- Don't add #ifdef CONFIG_MMU to the p2m_tlb_flush_sync() in p2m_write_unlock(), this need further discussion.
- Correct typo in commit message.
v5:
- No change
v4:
- Rework the patch to drop the unnecessary changes.
- Rework the commit msg a bit.
v3:
- remove MPU stubs
- adapt to the introduction of new directories: mmu/
v2:
- new commit

---
 xen/arch/arm/include/asm/mmu/p2m.h |   18 +
 xen/arch/arm/include/asm/p2m.h     |   26 +-
 xen/arch/arm/mmu/Makefile          |    1 +
 xen/arch/arm/mmu/p2m.c             | 1736 ++++++++++++++++++++++++++
 xen/arch/arm/p2m.c                 | 1837 +---------------------------
 5 files changed, 1832 insertions(+), 1786 deletions(-)
 create mode 100644 xen/arch/arm/include/asm/mmu/p2m.h
 create mode 100644 xen/arch/arm/mmu/p2m.c
xen/arch/arm/include/asm/mmu/p2m.h new file +18 -0
new file mode 100644
index 0000000000..f829e325ce
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __ARM_MMU_P2M_H__
+#define __ARM_MMU_P2M_H__
+
+struct p2m_domain;
+void p2m_force_tlb_flush_sync(struct p2m_domain *p2m);
+void p2m_tlb_flush_sync(struct p2m_domain *p2m);
+
+#endif /* __ARM_MMU_P2M_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/include/asm/p2m.h +24 -2
index 940495d42b..a9622dac9a 100644
@@ -19,6 +19,22 @@ extern unsigned int p2m_root_level;
 #define P2M_ROOT_ORDER    p2m_root_order
 #define P2M_ROOT_LEVEL p2m_root_level
 
+#define MAX_VMID_8_BIT  (1UL << 8)
+#define MAX_VMID_16_BIT (1UL << 16)
+
+#define INVALID_VMID 0 /* VMID 0 is reserved */
+
+#ifdef CONFIG_ARM_64
+extern unsigned int max_vmid;
+/* VMID is by default 8 bit width on AArch64 */
+#define MAX_VMID       max_vmid
+#else
+/* VMID is always 8 bit width on AArch32 */
+#define MAX_VMID        MAX_VMID_8_BIT
+#endif
+
+#define P2M_ROOT_PAGES    (1<<P2M_ROOT_ORDER)
+
 struct domain;
 
 extern void memory_type_changed(struct domain *);
@@ -156,6 +172,10 @@ typedef enum {
 #endif
 #include <xen/p2m-common.h>
 
+#ifdef CONFIG_MMU
+#include <asm/mmu/p2m.h>
+#endif
+
 static inline bool arch_acquire_resource_check(struct domain *d)
 {
     /*
@@ -180,7 +200,11 @@ void p2m_altp2m_check(struct vcpu *v, uint16_t idx)
  */
 void p2m_restrict_ipa_bits(unsigned int ipa_bits);
 
+void p2m_vmid_allocator_init(void);
+int p2m_alloc_vmid(struct domain *d);
+
 /* Second stage paging setup, to be called on all CPUs */
+void setup_virt_paging_one(void *data);
 void setup_virt_paging(void);
 
 /* Init the datastructures for later use by the p2m code */
@@ -242,8 +266,6 @@ static inline int p2m_is_write_locked(struct p2m_domain *p2m)
     return rw_is_write_locked(&p2m->lock);
 }
 
-void p2m_tlb_flush_sync(struct p2m_domain *p2m);
-
 /* Look up the MFN corresponding to a domain's GFN. */
 mfn_t p2m_lookup(struct domain *d, gfn_t gfn, p2m_type_t *t);
 
xen/arch/arm/mmu/Makefile +1 -0
index 98aea965df..67475fcd80 100644
@@ -1,3 +1,4 @@
+obj-y += p2m.o
 obj-y += pt.o
 obj-y += setup.o
 obj-y += smpboot.o
xen/arch/arm/mmu/p2m.c new file +1736 -0

1,736 lines not on this page · raw message

xen/arch/arm/p2m.c +53 -1784

1,191 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.25.1

Part of the diff is not shown here. View the raw message.

#34 lore raw
In reply to Henry Wang

I committed patches 1-5

Quoted 83 lines · Henry Wang

Based on the discussion in the Xen Summit [1], sending this series out after addressing the comments in v5 [2] as the preparation work to add MPU support. The series passed the GitLab CI check in [3].

Mostly code movement and function folding, with some of Kconfig and build system (mainly Makefiles) adjustment.

This series is based on:
ec272d8d4c CI: Always move the bisect build log back

[1] https://lore.kernel.org/xen-devel/AS8PR08MB799122F8B0CB841DED64F4819226A@AS8PR08MB7991.eurprd08.prod.outlook.com/
[2] https://lore.kernel.org/xen-devel/[email protected]/
[3] https://gitlab.com/xen-project/people/henryw/xen/-/pipelines/982592897

Henry Wang (9):
xen/arm: Introduce CONFIG_MMU Kconfig option xen/arm64: Split and move MMU-specific head.S to mmu/head.S xen/arm64: Fold setup_fixmap() to create_page_tables() xen/arm: Split page table related code to mmu/pt.c xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c xen/arm: Fold mmu_init_secondary_cpu() to head.S xen/arm: Extract MMU-specific MM code
xen/arm: Split MMU-specific setup_mm() and related code out xen/arm: Fold pmap and fixmap into MMU system

Penny Zheng (2):
xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm() xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Wei Chen (2):
xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm() xen/arm: Move MMU related definitions from config.h to mmu/layout.h

 xen/arch/arm/Kconfig                    |    5 +-
 xen/arch/arm/Makefile                   |    1 +
 xen/arch/arm/arm32/Makefile             |    1 +
 xen/arch/arm/arm32/head.S               |   22 +-
 xen/arch/arm/arm32/mmu/Makefile         |    1 +
 xen/arch/arm/arm32/mmu/mm.c             |  301 ++++
 xen/arch/arm/arm64/Makefile             |    2 +-
 xen/arch/arm/arm64/head.S               |  460 +-----
 xen/arch/arm/arm64/mmu/Makefile         |    2 +
 xen/arch/arm/arm64/mmu/head.S           |  481 ++++++
 xen/arch/arm/arm64/{ => mmu}/mm.c       |   84 ++
 xen/arch/arm/include/asm/arm32/mm.h     |    1 +
 xen/arch/arm/include/asm/arm64/macros.h |   36 +
 xen/arch/arm/include/asm/config.h       |  132 +-
 xen/arch/arm/include/asm/mm.h           |   28 +-
 xen/arch/arm/include/asm/mmu/layout.h   |  146 ++
 xen/arch/arm/include/asm/mmu/mm.h       |   41 +
 xen/arch/arm/include/asm/mmu/p2m.h      |   18 +
 xen/arch/arm/include/asm/p2m.h          |   26 +-
 xen/arch/arm/include/asm/page.h         |   15 -
 xen/arch/arm/include/asm/setup.h        |    5 +
 xen/arch/arm/kernel.c                   |   28 -
 xen/arch/arm/mm.c                       | 1212 ---------------
 xen/arch/arm/mmu/Makefile               |    4 +
 xen/arch/arm/mmu/p2m.c                  | 1736 +++++++++++++++++++++
 xen/arch/arm/mmu/pt.c                   |  743 +++++++++
 xen/arch/arm/mmu/setup.c                |  372 +++++
 xen/arch/arm/mmu/smpboot.c              |  121 ++
 xen/arch/arm/p2m.c                      | 1837 +----------------------
 xen/arch/arm/setup.c                    |  324 +---
 xen/arch/arm/smpboot.c                  |    4 +-
 xen/arch/arm/xen.lds.S                  |    1 +
 32 files changed, 4222 insertions(+), 3968 deletions(-)
 create mode 100644 xen/arch/arm/arm32/mmu/Makefile
 create mode 100644 xen/arch/arm/arm32/mmu/mm.c
 create mode 100644 xen/arch/arm/arm64/mmu/Makefile
 create mode 100644 xen/arch/arm/arm64/mmu/head.S
 rename xen/arch/arm/arm64/{ => mmu}/mm.c (60%)
 create mode 100644 xen/arch/arm/include/asm/mmu/layout.h
 create mode 100644 xen/arch/arm/include/asm/mmu/mm.h
 create mode 100644 xen/arch/arm/include/asm/mmu/p2m.h
 create mode 100644 xen/arch/arm/mmu/Makefile
 create mode 100644 xen/arch/arm/mmu/p2m.c
 create mode 100644 xen/arch/arm/mmu/pt.c
 create mode 100644 xen/arch/arm/mmu/setup.c
 create mode 100644 xen/arch/arm/mmu/smpboot.c
Signature · 1 line
2.25.1
#35 lore raw

Hi Henry,

Quoted 38 lines · Stefano Stabellini

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.

I committed patches 1-5

Quoted 32 lines · Henry Wang

Based on the discussion in the Xen Summit [1], sending this series out after addressing the comments in v5 [2] as the preparation work to add MPU support. The series passed the GitLab CI check in [3].

Mostly code movement and function folding, with some of Kconfig and build system (mainly Makefiles) adjustment.

This series is based on:
ec272d8d4c CI: Always move the bisect build log back

[1] https://lore.kernel.org/xen-devel/AS8PR08MB799122F8B0CB841DED64F4819226A@AS8PR08MB7991.eurprd08.prod.outlook.com/
[2] https://lore.kernel.org/xen-devel/[email protected]/
[3] https://gitlab.com/xen-project/people/henryw/xen/-/pipelines/982592897

Henry Wang (9):
xen/arm: Introduce CONFIG_MMU Kconfig option xen/arm64: Split and move MMU-specific head.S to mmu/head.S xen/arm64: Fold setup_fixmap() to create_page_tables() xen/arm: Split page table related code to mmu/pt.c xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c xen/arm: Fold mmu_init_secondary_cpu() to head.S xen/arm: Extract MMU-specific MM code
xen/arm: Split MMU-specific setup_mm() and related code out xen/arm: Fold pmap and fixmap into MMU system

Penny Zheng (2):
xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm() xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Wei Chen (2):
xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm() xen/arm: Move MMU related definitions from config.h to mmu/layout.h

Except for "[PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S " where I have a small comment, I am happy with the current set.

I have made the corresponding changes for Arm32 MMU (See https://gitlab.com/xen-project/people/ayankuma/xen/-/commits/mmu_arm32_on_arm64?ref_type=heads ),

and triggered the CI. It looks all good (qemu-smoke-ppc64le-pseries-gcc failure seems unrelated).

I have sent out the patches for review so that the maintainers/you can have a look and review them

("[XEN v1 0/4] xen/arm: Split MMU code as the prepration of MPU work form Arm32").

- Ayan

Quoted 51 lines
Quoted 51 lines
  xen/arch/arm/Kconfig                    |    5 +-
  xen/arch/arm/Makefile                   |    1 +
  xen/arch/arm/arm32/Makefile             |    1 +
  xen/arch/arm/arm32/head.S               |   22 +-
  xen/arch/arm/arm32/mmu/Makefile         |    1 +
  xen/arch/arm/arm32/mmu/mm.c             |  301 ++++
  xen/arch/arm/arm64/Makefile             |    2 +-
  xen/arch/arm/arm64/head.S               |  460 +-----
  xen/arch/arm/arm64/mmu/Makefile         |    2 +
  xen/arch/arm/arm64/mmu/head.S           |  481 ++++++
  xen/arch/arm/arm64/{ => mmu}/mm.c       |   84 ++
  xen/arch/arm/include/asm/arm32/mm.h     |    1 +
  xen/arch/arm/include/asm/arm64/macros.h |   36 +
  xen/arch/arm/include/asm/config.h       |  132 +-
  xen/arch/arm/include/asm/mm.h           |   28 +-
  xen/arch/arm/include/asm/mmu/layout.h   |  146 ++
  xen/arch/arm/include/asm/mmu/mm.h       |   41 +
  xen/arch/arm/include/asm/mmu/p2m.h      |   18 +
  xen/arch/arm/include/asm/p2m.h          |   26 +-
  xen/arch/arm/include/asm/page.h         |   15 -
  xen/arch/arm/include/asm/setup.h        |    5 +
  xen/arch/arm/kernel.c                   |   28 -
  xen/arch/arm/mm.c                       | 1212 ---------------
  xen/arch/arm/mmu/Makefile               |    4 +
  xen/arch/arm/mmu/p2m.c                  | 1736 +++++++++++++++++++++
  xen/arch/arm/mmu/pt.c                   |  743 +++++++++
  xen/arch/arm/mmu/setup.c                |  372 +++++
  xen/arch/arm/mmu/smpboot.c              |  121 ++
  xen/arch/arm/p2m.c                      | 1837 +----------------------
  xen/arch/arm/setup.c                    |  324 +---
  xen/arch/arm/smpboot.c                  |    4 +-
  xen/arch/arm/xen.lds.S                  |    1 +
  32 files changed, 4222 insertions(+), 3968 deletions(-)

create mode 100644 xen/arch/arm/arm32/mmu/Makefile create mode 100644 xen/arch/arm/arm32/mmu/mm.c create mode 100644 xen/arch/arm/arm64/mmu/Makefile create mode 100644 xen/arch/arm/arm64/mmu/head.S rename xen/arch/arm/arm64/{ => mmu}/mm.c (60%) create mode 100644 xen/arch/arm/include/asm/mmu/layout.h create mode 100644 xen/arch/arm/include/asm/mmu/mm.h create mode 100644 xen/arch/arm/include/asm/mmu/p2m.h create mode 100644 xen/arch/arm/mmu/Makefile create mode 100644 xen/arch/arm/mmu/p2m.c create mode 100644 xen/arch/arm/mmu/pt.c
create mode 100644 xen/arch/arm/mmu/setup.c create mode 100644 xen/arch/arm/mmu/smpboot.c

--
2.25.1

author
#36 lore raw

Hi Ayan,

Quoted 25 lines · Ayan Kumar Halder

Hi Henry,

Quoted 20 lines · Stefano Stabellini

I committed patches 1-5

Quoted 16 lines · Henry Wang

Henry Wang (9):
xen/arm: Introduce CONFIG_MMU Kconfig option xen/arm64: Split and move MMU-specific head.S to mmu/head.S xen/arm64: Fold setup_fixmap() to create_page_tables() xen/arm: Split page table related code to mmu/pt.c xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c xen/arm: Fold mmu_init_secondary_cpu() to head.S xen/arm: Extract MMU-specific MM code
xen/arm: Split MMU-specific setup_mm() and related code out xen/arm: Fold pmap and fixmap into MMU system

Penny Zheng (2):
xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm() xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Wei Chen (2):
xen/arm64: head.S: Introduce enable_{boot,secondary}_cpu_mm() xen/arm: Move MMU related definitions from config.h to mmu/layout.h

Except for "[PATCH v6 08/13] xen/arm: Fold mmu_init_secondary_cpu() to head.S " where I have a small comment, I am happy with the current set.

Thanks!

Quoted 6 lines

I have made the corresponding changes for Arm32 MMU (See https://gitlab.com/xen-project/people/ayankuma/xen/-/commits/mmu_arm32_on_arm64?ref_type=heads ),

and triggered the CI. It looks all good (qemu-smoke-ppc64le-pseries-gcc failure seems unrelated).

I have sent out the patches for review so that the maintainers/you can have a look and review them

Sure, me and Penny will have a look.

Kind regards,
Henry

Quoted 4 lines

("[XEN v1 0/4] xen/arm: Split MMU code as the prepration of MPU work form Arm32").

- Ayan