Skip to contentSkip to conversation

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

PATCH Superseded Henry Wang 2 participants 31 messages Oct 9, 2023 – Oct 18, 2023

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Henry Wang

author
#0 lore raw

v7 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 v6 [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:
ffa1437686 tools/xenpvboot: remove as unable to convert to Python 3

[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/1029280530

Henry Wang (6):
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}

 xen/arch/arm/Kconfig               |    2 +-
 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        |    1 -
 xen/arch/arm/arm64/mmu/Makefile    |    1 +
 xen/arch/arm/arm64/mmu/head.S      |   21 +
 xen/arch/arm/arm64/{ => mmu}/mm.c  |   84 ++
 xen/arch/arm/include/asm/mm.h      |   31 +-
 xen/arch/arm/include/asm/mmu/mm.h  |   44 +
 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              |  736 +++++++++++
 xen/arch/arm/mmu/setup.c           |  376 ++++++
 xen/arch/arm/mmu/smpboot.c         |  124 ++
 xen/arch/arm/p2m.c                 | 1837 +---------------------------
 xen/arch/arm/setup.c               |  324 +----
 xen/arch/arm/smpboot.c             |    4 +-
 26 files changed, 3565 insertions(+), 3390 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 (60%)
 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 v7 1/8 xen/arm: Split page table related code to mmu/pt.c

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.

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.

---
v7:
- Do not move pte_of_xenaddr() to mmu/pt.c.
- Do not expose global variable phys_offset.
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/mm.c         | 717 -------------------------------------
 xen/arch/arm/mmu/Makefile |   1 +
 xen/arch/arm/mmu/pt.c     | 736 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 738 insertions(+), 717 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/mm.c +0 -717

759 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 +736 -0

736 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v7 1/8
#2 lore raw

Re: [PATCH v7 1/8] xen/arm: Split page table related code to mmu/pt.c

Hi Henry,

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.

Quoted 6 lines

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.

Cheers,

Signature · 1 line
Julien Grall
author
#3 lore raw

PATCH v7 2/8 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

Since arm32 global variable cpu0_pgtable will be used by both arch/arm/mm.c and arch/arm/mmu/smpboot.c, to avoid exporting this variable, change the variable usage in arch/arm/mmu/smpboot.c to per_cpu(xen_pgtable, 0).

To avoid exposing global variable phys_offset, use virt_to_maddr() to calculate init_ttbr for arm64.

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

---
v7:
- Do not export cpu0_pgtable, replace the variable usage in arch/arm/mmu/smpboot.c to per_cpu(xen_pgtable, 0).
- Also move global variable init_ttbr to arch/arm/mmu/smpboot.c.
- Use virt_to_maddr() instead of phys_offset to calculate init_ttbr in arm64 implementation of init_secondary_pagetables().
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/mm.c          | 104 -------------------------------
 xen/arch/arm/mmu/Makefile  |   1 +
 xen/arch/arm/mmu/smpboot.c | 124 +++++++++++++++++++++++++++++++++++++
 3 files changed, 125 insertions(+), 104 deletions(-)
 create mode 100644 xen/arch/arm/mmu/smpboot.c
xen/arch/arm/mm.c +0 -104
index fd02493564..b7eb3a6e08 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 */
 
 /*
@@ -94,9 +61,6 @@ DEFINE_BOOT_PAGE_TABLE(xen_fixmap);
  */
 static DEFINE_PAGE_TABLES(xen_xenmap, XEN_NR_ENTRIES(2));
 
-/* Non-boot CPUs use this to find the correct pagetables. */
-uint64_t init_ttbr;
-
 static paddr_t phys_offset;
 
 /* Limits of the Xen heap */
@@ -284,13 +248,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)
@@ -369,67 +326,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 +124 -0

124 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v7 2/8
#4 lore raw

Re: [PATCH v7 2/8] xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c

Hi Henry,

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

Quoted 10 lines · Henry Wang
xen/arch/arm/mmu/smpboot.c new file +10 -0
+#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 = virt_to_maddr((uintptr_t) xen_pgtable);

The cast should be necessary. With this dropped:

Cheers,

Signature · 1 line
Julien Grall
author
#5 lore raw

Re: [PATCH v7 2/8] xen/arm: Split MMU system SMP MM bringup code to mmu/smpboot.c

Hi Julien,

Quoted 5 lines · Julien Grall

Hi Henry,

On 09/10/2023 02:03, Henry Wang wrote:

Quoted 12 lines

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

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

The cast should be necessary. With this dropped:

Nice catch, my mistake, will change in v8.

Quoted 2 lines

Thanks!

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#6 lore raw

PATCH v7 3/8 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.

---
v7:
- No change.
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 88075ef083..e4b07594b5 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 d25e59f828..163d22ecd3 100644
@@ -214,8 +214,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 b7eb3a6e08..923a90925c 100644
@@ -326,12 +326,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 ec76de3cac..beb137d06e 100644
@@ -361,8 +361,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
#7 lore raw

Re: [PATCH v7 3/8] xen/arm: Fold mmu_init_secondary_cpu() to head.S

Hi Henry,

Quoted 10 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.

Quoted 4 lines

---
v7:
- No change.

Given the discusion on v6, I was expecting some changes here at least on arm64 side.

For arm32, my proposal would not yet work because sadly the temporary page-tables for secondary bring-up will contain writable and executable mappings.

Quoted 122 lines

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 88075ef083..e4b07594b5 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 d25e59f828..163d22ecd3 100644
@@ -214,8 +214,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 b7eb3a6e08..923a90925c 100644
@@ -326,12 +326,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 -0
index ec76de3cac..beb137d06e 100644

xen/arch/arm/smpboot.c void start_secondary(void) · line 366

Quoted 8 lines
xen/arch/arm/smpboot.c +0 -2
@@ -361,8 +361,6 @@ void start_secondary(void)
       */
      update_system_features(&current_cpu_data);
 
-    mmu_init_secondary_cpu();
-
      gic_init_secondary_cpu();
 
      set_current(idle_vcpu[cpuid]);

Cheers,

Signature · 1 line
Julien Grall
author
#8 lore raw

Re: [PATCH v7 3/8] xen/arm: Fold mmu_init_secondary_cpu() to head.S

Hi Julien,

Quoted 18 lines · Julien Grall

Hi Henry,

Quoted 8 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.

Quoted 4 lines

---
v7:
- No change.

Quoted 1 line

Given the discusion on v6, I was expecting some changes here at least on arm64 side.

Oh I am so sorry, I completely misunderstood the “leave the code as is” discussion and now I revisit that discussion and noticed that you did say the “leave the code as is” is for arm32 only ^^'

I will fix the arm64 side in v8 following [1].

Quoted 2 lines

For arm32, my proposal would not yet work because sadly the temporary page-tables for secondary bring-up will contain writable and executable mappings.

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

Kind regards,
Henry

author
#9 lore raw

PATCH v7 4/8 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. While moving, mark pte_of_xenaddr() as __init to make clear that this helper is only intended to be used during early boot.

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, declaration of global variable init_ttbr 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.

---
v7:
- Move pte_of_xenaddr() to mmu/setup.c and mark it as __init.
- Also move the declaration of init_ttbr to asm/mmu/mm.h. 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     |  25 +-
 xen/arch/arm/include/asm/mmu/mm.h |  50 ++++
 xen/arch/arm/include/asm/page.h   |  15 --
 xen/arch/arm/mm.c                 | 385 ------------------------------
 xen/arch/arm/mmu/Makefile         |   1 +
 xen/arch/arm/mmu/setup.c          | 349 +++++++++++++++++++++++++++
 12 files changed, 477 insertions(+), 420 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 -19
index 163d22ecd3..d23ebc7df6 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)
 
@@ -165,16 +171,6 @@ struct page_info
 #define _PGC_need_scrub   _PGC_allocated
 #define PGC_need_scrub    PGC_allocated
 
-/* Non-boot CPUs use this to find the correct pagetables. */
-extern uint64_t init_ttbr;
-
-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) ({                                 \
@@ -197,7 +193,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;
 
@@ -207,19 +202,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 +50 -0

50 lines not on this page · raw message

xen/arch/arm/include/asm/page.h +0 -15
index aa0080e8d7..ebaf5964f1 100644
@@ -264,21 +264,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 -385

416 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 +349 -0

349 lines not on this page · raw message

Signature · 1 line
2.25.1
Acked-by on v7 4/8
#10 lore raw

Re: [PATCH v7 4/8] xen/arm: Extract MMU-specific MM code

Hi Henry,

Quoted 20 lines · 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. While moving, mark pte_of_xenaddr() as __init to make clear that this helper is only intended to be used during early boot.

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, declaration of global variable init_ttbr 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.

Quoted 6 lines

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.

Cheers,

Signature · 1 line
Julien Grall
author
#11 lore raw

PATCH v7 5/8 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.

---
v7:
- No change.
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 439ae314fd..c5e03a66bf 100644
@@ -31,12 +31,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 b8866c20f4..863e9b88cd 100644
@@ -159,6 +159,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
Acked-by on v7 5/8
#12 lore raw

Re: [PATCH v7 5/8] xen/arm: Split MMU-specific setup_mm() and related code out

Hi Henry,

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

Quoted 7 lines

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.

I am a bit confused with the last sentence. AFAICT, even without the MPU work, I think you already need to export init_pdx() & co because it is called from two different units.

Quoted 10 lines

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.

With the above comment clarified:

Cheers,

Signature · 1 line
Julien Grall
author
#13 lore raw

Re: [PATCH v7 5/8] xen/arm: Split MMU-specific setup_mm() and related code out

Hi Julien,

Quoted 21 lines · Julien Grall

Hi Henry,

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

Quoted 8 lines

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.

Quoted 1 line

I am a bit confused with the last sentence. AFAICT, even without the MPU work, I think you already need to export init_pdx() & co because it is called from two different units.

Yeah you are correct, I will reword the sentence to:
"Also, make init_pdx(), init_staticmem_pages(), and populate_boot_allocator() public as these functions are now called from two different units, make setup_mm() public for future MPU implementation."

Quoted 10 lines
Quoted 9 lines

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.

Quoted 3 lines

With the above comment clarified:

Thanks!

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#14 lore raw

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

---
v7:
- No change.
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 632dd9792e..4b21a12b8b 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 c2df976ab2..dff81f6125 100644
@@ -339,6 +339,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
Reviewed-by on v7 6/8
#15 lore raw

Re: [PATCH v7 6/8] xen/arm: Fold pmap and fixmap into MMU system

Hi Henry,

Quoted 12 lines · Henry Wang
xen/arch/arm/mmu/setup.c +4 -0
index c2df976ab2..dff81f6125 100644
@@ -339,6 +339,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

xen/arch/arm/mmu/setup.c void free_init_memory(void) · line 355

Quoted 10 lines
xen/arch/arm/mmu/setup.c +10 -0
+ * @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);

NIT: I would take the opportunity to add a missing space before/after '-'.

Cheers,

Signature · 1 line
Julien Grall
author
#16 lore raw

Re: [PATCH v7 6/8] xen/arm: Fold pmap and fixmap into MMU system

Hi Julien,

Quoted 20 lines · Julien Grall

Hi Henry,

Quoted 15 lines · Henry Wang
xen/arch/arm/mmu/setup.c +7 -0
index c2df976ab2..dff81f6125 100644
@@ -339,6 +339,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)
+{
Quoted 8 lines

xen/arch/arm/mmu/setup.c void free_init_memory(void) · line 355

Quoted 6 lines
xen/arch/arm/mmu/setup.c +6 -0
+    void *src = (void *)FIXMAP_ADDR(FIXMAP_MISC);
+
+    while (len) {
+        unsigned long l, s;
+
+        s = paddr & (PAGE_SIZE-1);

NIT: I would take the opportunity to add a missing space before/after '-'.

Sounds good, I’ve done that in my local v8 and also mentioned this change in commit message as:

```
Take the opportunity to add a missing space before and after '-' in
"s = paddr & (PAGE_SIZE-1);" of copy_from_paddr().
```
Quoted 2 lines

Thanks!

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#17 lore raw

PATCH v7 7/8 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.

---
v7:
- No change.
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 d23ebc7df6..db6d889826 100644
@@ -204,9 +204,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 eac805c74c..f05c53a087 100644
@@ -67,7 +67,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();
 
@@ -80,7 +80,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 beb137d06e..ac451e9b3e 100644
@@ -448,7 +448,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
#18 lore raw

Re: [PATCH v7 7/8] xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm()

Hi Henry,

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

---
v7:
- No change.
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 +0 -0
index d23ebc7df6..db6d889826 100644

xen/arch/arm/include/asm/mm.h extern void setup_pagetables(unsigned long boot_phys_offset); · line 210

Quoted 10 lines
xen/arch/arm/include/asm/mm.h +4 -3
@@ -204,9 +204,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.
+ */

AFAIU, with the renaming, you are trying to make the call MMU/MPU agnostic. But the comment is still very tailored to the MPU. I would consider to move the comment to mmu/smpboot.c and replace this one with a generic comment. Something like:

/* Prepare the memory subystem to bring-up the given secondary CPU. */

Cheers,

Signature · 1 line
Julien Grall
Reviewed-by
#19 lore raw

Re: [PATCH v7 7/8] xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm()

Quoted 60 lines · Julien Grall

Hi Henry,

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

---
v7:
- No change.
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 +0 -0
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 +0 -1
index d23ebc7df6..db6d889826 100644
@@ -204,9 +204,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 */

xen/arch/arm/include/asm/mm.h extern void setup_pagetables(unsigned long boot_phys_offset); · line 210

Quoted 5 lines
xen/arch/arm/include/asm/mm.h +4 -1
-extern int init_secondary_pagetables(int cpu);
+/*
+ * Allocate and initialise pagetables for a secondary CPU.
+ * Sets init_ttbr to the new page table.
+ */

AFAIU, with the renaming, you are trying to make the call MMU/MPU agnostic. But the comment is still very tailored to the MPU. I would consider to move the comment to mmu/smpboot.c and replace this one with a generic comment. Something like:

Quoted 1 line

/* Prepare the memory subystem to bring-up the given secondary CPU. */

I forgot to mention. With that:

Cheers,

Signature · 1 line
Julien Grall
author
#20 lore raw

Re: [PATCH v7 7/8] xen/arm: Rename init_secondary_pagetables() to prepare_secondary_mm()

Hi Julien,

Quoted 51 lines · Julien Grall
Quoted 48 lines · Julien Grall

Hi Henry,

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

---
v7:
- No change.
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 +0 -1
index d23ebc7df6..db6d889826 100644
@@ -204,9 +204,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
Quoted 8 lines
Quoted 8 lines

xen/arch/arm/include/asm/mm.h extern void setup_pagetables(unsigned long boot_phys_offset); · line 210

Quoted 6 lines
xen/arch/arm/include/asm/mm.h +4 -2
- * 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.
+ */

AFAIU, with the renaming, you are trying to make the call MMU/MPU agnostic. But the comment is still very tailored to the MPU. I would consider to move the comment to mmu/smpboot.c and replace this one with a generic comment. Something like:
/* Prepare the memory subystem to bring-up the given secondary CPU. */

Good suggestion! I will follow this in v8.

Quoted 4 lines

I forgot to mention. With that:

Thanks!

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#21 lore raw

PATCH v7 8/8 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.

---
v7:
- No change.
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.

#22 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Hi Henry,

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

Quoted 8 lines

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.

Some remarks about some of the code not moved:
* struct p2m_domain: The bulk of the fields seems to be MMU specific.
So depending on the number of common fields we either want to split or move the structure to p2m_domain. I would be ok to wait until the MPU code is present.
* p2m_type_t: It is not yet clear how this will apply to the MPU. I am
ok to wait before moving it.
* p2m_cache_flush_range(): I expect the code will need some change
because you may get large chunk of memory for the MPU.
* p2m_set_way_flush()/p2m_toggle_cache(): This was a giant hack to
support cache flush operations via set/way. To make it efficient, we track the pages that have been touched and only flush them. For the MPU, this would not work. Can we attempt to not emulate the instructions?

Quoted 59 lines

---
v7:
- No change.
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 +0 -0
index 940495d42b..a9622dac9a 100644

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 19

Quoted 1 line
xen/arch/arm/include/asm/p2m.h +0 -0
@@ -19,6 +19,22 @@ extern unsigned int p2m_root_level;
  #define P2M_ROOT_ORDER    p2m_root_order

You seem to use P2M_ROOT_ORDER to allocate p2m->root in arm/p2m.c. However, as I mentioned before, I don't think the defintion of p2m->root is suitable for the MPU. I think the two functions using p2m->root should be moved in mmu/p2m.c and P2M_ROOT_ORDER should be moved in mmu/p2m.h.

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 20

Quoted 1 line
xen/arch/arm/include/asm/p2m.h +0 -0
  #define P2M_ROOT_LEVEL p2m_root_level

P2M_ROOT_LEVEL doesn't seem to make sense for the MPU. The only use in arch/arm/p2m.c seems to be in dump_p2m_lookup() which is calling an MMU specific function. So I think this wants to be moved in the MMU code.

Quoted 6 lines
xen/arch/arm/include/asm/p2m.h +5 -0
+#define MAX_VMID_8_BIT  (1UL << 8)
+#define MAX_VMID_16_BIT (1UL << 16)
+
+#define INVALID_VMID 0 /* VMID 0 is reserved */
+

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 36

Quoted 10 lines
xen/arch/arm/include/asm/p2m.h +10 -0
+#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)

See above.

Also NIT, I would suggest to take the opportunity to use 1U and add space before/after <<.

Quoted 16 lines
xen/arch/arm/include/asm/p2m.h +5 -0
+
  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)
  {
      /*

xen/arch/arm/include/asm/p2m.h void p2m_altp2m_check(struct vcpu *v, uint16_t idx) · line 207

Quoted 8 lines
xen/arch/arm/include/asm/p2m.h +4 -0
@@ -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);

I don't much like the idea to export setup_virt_paging_one(). Could we instead move cpu_virt_paging_callback() & co to mmu/p2m.c?

Cheers,

Signature · 1 line
Julien Grall
author
#23 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Hi Julien,

Quoted 20 lines · Julien Grall

Hi Henry,

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

Quoted 3 lines
Quoted 3 lines

Some remarks about some of the code not moved:
* struct p2m_domain: The bulk of the fields seems to be MMU specific. So depending on the number of common fields we either want to split or move the structure to p2m_domain. I would be ok to wait until the MPU code is present.
* p2m_type_t: It is not yet clear how this will apply to the MPU. I am ok to wait before moving it.

Agree with both here, let’s continue the discussion in the actual MPU patch for P2M then, but I am then a bit confused about...

Quoted 2 lines

* p2m_cache_flush_range(): I expect the code will need some change because you may get large chunk of memory for the MPU.
* p2m_set_way_flush()/p2m_toggle_cache(): This was a giant hack to support cache flush operations via set/way. To make it efficient, we track the pages that have been touched and only flush them. For the MPU, this would not work. Can we attempt to not emulate the instructions?

…these two remarks here, do you expect me to do some changes with these three functions in this patch? Or we can defer the required changes to the MPU patch for P2M?

I think I am highly likely to make a mistake here but I took a look at the MPU implementation [1] and it looks like the MPU code can use these tree functions without changes - probably because these functions are simply used by
(1) domctl and we only have dom0less DomUs on MPU
(2) trap handlers
which means these functions are simply not called…

So maybe moving these three functions to mmu/p2m.c would be a good idea for this patch?

Quoted 10 lines

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 19

Quoted 8 lines

---

xen/arch/arm/include/asm/p2m.h +0 -0
index 940495d42b..a9622dac9a 100644
@@ -19,6 +19,22 @@ extern unsigned int p2m_root_level;
 #define P2M_ROOT_ORDER    p2m_root_order

You seem to use P2M_ROOT_ORDER to allocate p2m->root in arm/p2m.c. However, as I mentioned before, I don't think the defintion of p2m->root is suitable for the MPU. I think the two functions using p2m->root should be moved in mmu/p2m.c and P2M_ROOT_ORDER should be moved in mmu/p2m.h.

Sure, will follow that, sorry for missing this part.

Quoted 4 lines

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 20

Quoted 2 lines
xen/arch/arm/include/asm/p2m.h +0 -0
 #define P2M_ROOT_LEVEL p2m_root_level

P2M_ROOT_LEVEL doesn't seem to make sense for the MPU. The only use in arch/arm/p2m.c seems to be in dump_p2m_lookup() which is calling an MMU specific function. So I think this wants to be moved in the MMU code.

Will do in v8.

Quoted 6 lines

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 36

Quoted 2 lines
xen/arch/arm/include/asm/p2m.h +1 -0
+#define P2M_ROOT_PAGES    (1<<P2M_ROOT_ORDER)

See above.

Also NIT, I would suggest to take the opportunity to use 1U and add space before/after <<.

Sounds good, will do in v8.

Quoted 16 lines
Quoted 16 lines
xen/arch/arm/include/asm/p2m.h +4 -0
+
 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)
  */
Quoted 8 lines

xen/arch/arm/include/asm/p2m.h void p2m_altp2m_check(struct vcpu *v, uint16_t idx) · line 207

Quoted 6 lines
xen/arch/arm/include/asm/p2m.h +3 -0
 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);

I don't much like the idea to export setup_virt_paging_one(). Could we instead move cpu_virt_paging_callback() & co to mmu/p2m.c?

Yeah good idea, thank you for this suggestion :) will do in v8.

[1] https://gitlab.com/xen-project/people/henryw/xen/-/commits/mpu_v5/?ref_type=heads

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#24 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Quoted 1 line · Henry Wang

Hi Julien,

Hi Henry,

Quoted 27 lines
Quoted 20 lines · Julien Grall

Hi Henry,

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

Quoted 3 lines
Quoted 3 lines

Some remarks about some of the code not moved:
* struct p2m_domain: The bulk of the fields seems to be MMU specific. So depending on the number of common fields we either want to split or move the structure to p2m_domain. I would be ok to wait until the MPU code is present.
* p2m_type_t: It is not yet clear how this will apply to the MPU. I am ok to wait before moving it.

Agree with both here, let’s continue the discussion in the actual MPU patch for P2M then, but I am then a bit confused about...

Quoted 6 lines
Quoted 2 lines

* p2m_cache_flush_range(): I expect the code will need some change because you may get large chunk of memory for the MPU.
* p2m_set_way_flush()/p2m_toggle_cache(): This was a giant hack to support cache flush operations via set/way. To make it efficient, we track the pages that have been touched and only flush them. For the MPU, this would not work. Can we attempt to not emulate the instructions?

…these two remarks here, do you expect me to do some changes with these three functions in this patch? Or we can defer the required changes to the MPU patch for P2M?

My original intention was to ask to move them right now. But if it is unclear whether they would be used, then it would be best to defer until we have a better understanding.

Quoted 7 lines

I think I am highly likely to make a mistake here but I took a look at the MPU implementation [1] and it looks like the MPU code can use these tree functions without changes - probably because these functions are simply used by
(1) domctl and we only have dom0less DomUs on MPU
(2) trap handlers
which means these functions are simply not called…

I am not sure I fully understand why would the trap handlers not called. Is this suggesting that a dom0less domUs can not use set/way instructions?

Cheers,

Signature · 1 line
Julien Grall
author
#25 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Hi Julien,

Quoted 31 lines · Julien Grall
Quoted 1 line · Henry Wang

Hi Julien,

Hi Henry,

Quoted 25 lines
Quoted 20 lines · Julien Grall

Hi Henry,

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

Quoted 3 lines
Quoted 3 lines

Some remarks about some of the code not moved:
* struct p2m_domain: The bulk of the fields seems to be MMU specific. So depending on the number of common fields we either want to split or move the structure to p2m_domain. I would be ok to wait until the MPU code is present.
* p2m_type_t: It is not yet clear how this will apply to the MPU. I am ok to wait before moving it.

Agree with both here, let’s continue the discussion in the actual MPU patch for P2M then, but I am then a bit confused about...

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

* p2m_cache_flush_range(): I expect the code will need some change because you may get large chunk of memory for the MPU.
* p2m_set_way_flush()/p2m_toggle_cache(): This was a giant hack to support cache flush operations via set/way. To make it efficient, we track the pages that have been touched and only flush them. For the MPU, this would not work. Can we attempt to not emulate the instructions?

…these two remarks here, do you expect me to do some changes with these three functions in this patch? Or we can defer the required changes to the MPU patch for P2M?

My original intention was to ask to move them right now. But if it is unclear whether they would be used, then it would be best to defer until we have a better understanding.

Sure, I think I will defer it then.

Quoted 9 lines
Quoted 7 lines

I think I am highly likely to make a mistake here but I took a look at the MPU implementation [1] and it looks like the MPU code can use these tree functions without changes - probably because these functions are simply used by
(1) domctl and we only have dom0less DomUs on MPU
(2) trap handlers
which means these functions are simply not called…

I am not sure I fully understand why would the trap handlers not called. Is this suggesting that a dom0less domUs can not use set/way instructions?

No I was wrong, sorry for the confusion, I somehow “mis-linked” the “trap” to “panic” :/

But as I saw your reply above, so I think probably we can defer the movement of these functions to further discussions in MPU series.

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
author
#26 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

In reply to Julien Grall

Hi Julien,

Quoted 6 lines · Julien Grall

Hi Henry,

Quoted 1 line · Henry Wang
Quoted 8 lines

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 19

Quoted 6 lines
xen/arch/arm/include/asm/p2m.h +0 -0
index 940495d42b..a9622dac9a 100644
@@ -19,6 +19,22 @@ extern unsigned int p2m_root_level;
 #define P2M_ROOT_ORDER    p2m_root_order

You seem to use P2M_ROOT_ORDER to allocate p2m->root in arm/p2m.c. However, as I mentioned before, I don't think the defintion of p2m->root is suitable for the MPU. I think the two functions using p2m->root should be moved in mmu/p2m.c and P2M_ROOT_ORDER should be moved in mmu/p2m.h.

While working on this, I noticed that if we move p2m_final_teardown() (one of the two functions you mentioned that consuming p2m->root), we also need to move the static function p2m_free_vmid(). This seems reasonable as on MPU we only have dom0less domUs so we don’t really need to free vmid. However, if p2m_free_vmid() is moved to mmu/p2m.c, we have two consumers of "static spinlock_t vmid_alloc_lock” and "static unsigned long *vmid_mask".

So I am writing this email just to confirm that are you fine with exposing these two global variables? Thanks!

The other comments seem fine and I’ve fixed all of them.

Kind regards,
Henry

#27 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Hi Henry,

Quoted 15 lines · Henry Wang
Quoted 6 lines · Julien Grall

Hi Henry,

Quoted 1 line · Henry Wang
Quoted 8 lines

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 19

Quoted 6 lines
xen/arch/arm/include/asm/p2m.h +0 -0
index 940495d42b..a9622dac9a 100644
@@ -19,6 +19,22 @@ extern unsigned int p2m_root_level;
  #define P2M_ROOT_ORDER    p2m_root_order

You seem to use P2M_ROOT_ORDER to allocate p2m->root in arm/p2m.c. However, as I mentioned before, I don't think the defintion of p2m->root is suitable for the MPU. I think the two functions using p2m->root should be moved in mmu/p2m.c and P2M_ROOT_ORDER should be moved in mmu/p2m.h.

Quoted 4 lines

While working on this, I noticed that if we move p2m_final_teardown() (one of the two functions you mentioned that consuming p2m->root), we also need to move the static function p2m_free_vmid(). This seems reasonable as on MPU we only have dom0less domUs so we don’t really need to free vmid.

Regardless on whether you need to free VMID on MPU, the allocation and free functions should live in the same unit. So if you plan to move p2m_final_teardown() in mmu/p2m.c, then p2m_free_vmid should be exported.

Cheers,

Signature · 1 line
Julien Grall
author
#28 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Hi Julien,

Quoted 19 lines · Julien Grall

Hi Henry,

Quoted 14 lines · Henry Wang
Quoted 14 lines · Julien Grall

Hi Henry,

xen/arch/arm/include/asm/p2m.h extern unsigned int p2m_root_level; · line 19

Quoted 7 lines · Henry Wang
xen/arch/arm/include/asm/p2m.h +0 -0
index 940495d42b..a9622dac9a 100644
@@ -19,6 +19,22 @@ extern unsigned int p2m_root_level;
 #define P2M_ROOT_ORDER    p2m_root_order

You seem to use P2M_ROOT_ORDER to allocate p2m->root in arm/p2m.c. However, as I mentioned before, I don't think the defintion of p2m->root is suitable for the MPU. I think the two functions using p2m->root should be moved in mmu/p2m.c and P2M_ROOT_ORDER should be moved in mmu/p2m.h.

Quoted 6 lines
Quoted 4 lines

While working on this, I noticed that if we move p2m_final_teardown() (one of the two functions you mentioned that consuming p2m->root), we also need to move the static function p2m_free_vmid(). This seems reasonable as on MPU we only have dom0less domUs so we don’t really need to free vmid.

Regardless on whether you need to free VMID on MPU, the allocation and free functions should live in the same unit. So if you plan to move p2m_final_teardown() in mmu/p2m.c, then p2m_free_vmid should be exported.

Sounds good, I will follow this way. Thanks.

Kind regards,
Henry

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#29 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

In reply to Henry Wang

Hi Henry,

some more remarks.

xen/arch/arm/mmu/p2m.c line 1231

Quoted 8 lines · Henry Wang
xen/arch/arm/mmu/p2m.c new file +8 -0
+/*
+ * Invalidate all entries in the root page-tables. This is
+ * useful to get fault on entry and do an action.
+ *
+ * p2m_invalid_root() should not be called when the P2M is shared with
+ * the IOMMU because it will cause IOMMU fault.
+ */
+void p2m_invalidate_root(struct p2m_domain *p2m)

I think this function doesn't make sense for the MPU. What would invalidate?

The only caller outside of the file is in arch_domain_creation_finished(). This was used for set/way emulation which I believe will be difficult to implement efficiently for the MPU (see my other reply to this patch).

So I would consider to move arch_domain_creation_finished() in mmu/p2m.c or creating a new function p2m_domain_creation_finished() which is called from arch_domain_creation_finished().

[...]

xen/arch/arm/mmu/p2m.c line 1545

Quoted 7 lines
xen/arch/arm/mmu/p2m.c new file +7 -0
+/*
+ * Clean & invalidate RAM associated to the guest vCPU.
+ *
+ * The function can only work with the current vCPU and should be called
+ * with IRQ enabled as the vCPU could get preempted.
+ */
+void p2m_flush_vm(struct vcpu *v)

I believe an implementation of p2m_flush_vm() would be quite expensive for the MPU. It would be better to not emulate set/way for now.

It would also feel a bit odd to leave it unimplemented and called from check_for_vcpu_work(). So maybe we want to rename it. I don't have a goood name suggestion though. Bertrand, Stefano?

Quoted 54 lines
xen/arch/arm/mmu/p2m.c new file +54 -0
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
+    int rc;
+    gfn_t start = _gfn(0);
+
+    ASSERT(v == current);
+    ASSERT(local_irq_is_enabled());
+    ASSERT(v->arch.need_flush_to_ram);
+
+    do
+    {
+        rc = p2m_cache_flush_range(v->domain, &start, _gfn(ULONG_MAX));
+        if ( rc == -ERESTART )
+            do_softirq();
+    } while ( rc == -ERESTART );
+
+    if ( rc != 0 )
+        gprintk(XENLOG_WARNING,
+                "P2M has not been correctly cleaned (rc = %d)\n",
+                rc);
+
+    /*
+     * Invalidate the p2m to track which page was modified by the guest
+     * between call of p2m_flush_vm().
+     */
+    p2m_invalidate_root(p2m);
+
+    v->arch.need_flush_to_ram = false;
+}
+
+/* VTCR value to be configured by all CPUs. Set only once by the boot CPU */
+static register_t __read_mostly vtcr;
+
+void setup_virt_paging_one(void *data)
+{
+    WRITE_SYSREG(vtcr, VTCR_EL2);
+
+    /*
+     * ARM64_WORKAROUND_AT_SPECULATE: We want to keep the TLBs free from
+     * entries related to EL1/EL0 translation regime until a guest vCPU
+     * is running. For that, we need to set-up VTTBR to point to an empty
+     * page-table and turn on stage-2 translation. The TLB entries
+     * associated with EL1/EL0 translation regime will also be flushed in case
+     * an AT instruction was speculated before hand.
+     */
+    if ( cpus_have_cap(ARM64_WORKAROUND_AT_SPECULATE) )
+    {
+        WRITE_SYSREG64(generate_vttbr(INVALID_VMID, empty_root_mfn), VTTBR_EL2);
+        WRITE_SYSREG(READ_SYSREG(HCR_EL2) | HCR_VM, HCR_EL2);
+        isb();
+
+        flush_all_guests_tlb_local();
+    }
+}

[...]

Signature · 1 line
Julien Grall
author
#30 lore raw

Re: [PATCH v7 8/8] xen/arm: mmu: move MMU specific P2M code to mmu/p2m.{c,h}

Hi Julien,

(Sending this email directly “To:” Bertrand and Stefano to catch their attention about one question that you ask them)

Quoted 18 lines · Julien Grall

Hi Henry,

some more remarks.

xen/arch/arm/mmu/p2m.c line 1231

Quoted 8 lines · Henry Wang
xen/arch/arm/mmu/p2m.c new file +8 -0
+/*
+ * Invalidate all entries in the root page-tables. This is
+ * useful to get fault on entry and do an action.
+ *
+ * p2m_invalid_root() should not be called when the P2M is shared with
+ * the IOMMU because it will cause IOMMU fault.
+ */
+void p2m_invalidate_root(struct p2m_domain *p2m)

I think this function doesn't make sense for the MPU. What would invalidate?

Quoted 3 lines

The only caller outside of the file is in arch_domain_creation_finished(). This was used for set/way emulation which I believe will be difficult to implement efficiently for the MPU (see my other reply to this patch).

So I would consider to move arch_domain_creation_finished() in mmu/p2m.c or creating a new function p2m_domain_creation_finished() which is called from arch_domain_creation_finished().

Yeah make sense, I will do the suggested change in v8.

Quoted 11 lines

[...]

xen/arch/arm/mmu/p2m.c line 1545

Quoted 7 lines
xen/arch/arm/mmu/p2m.c new file +7 -0
+/*
+ * Clean & invalidate RAM associated to the guest vCPU.
+ *
+ * The function can only work with the current vCPU and should be called
+ * with IRQ enabled as the vCPU could get preempted.
+ */
+void p2m_flush_vm(struct vcpu *v)

I believe an implementation of p2m_flush_vm() would be quite expensive for the MPU. It would be better to not emulate set/way for now.

Quoted 1 line

It would also feel a bit odd to leave it unimplemented and called from check_for_vcpu_work(). So maybe we want to rename it. I don't have a goood name suggestion though. Bertrand, Stefano?

See the beginning of this email.

Kind regards,
Henry

Quoted 5 lines

[...]

Signature · 1 line
Julien Grall