Skip to contentSkip to conversation

xen/arm64: Rework the MMU-off code (idmap) so it is self-contained

PATCH In review Julien Grall 2 participants 6 messages Jan 16, 2024

lore
Share & tools
lore
#0 lore raw

cover xen/arm64: Rework the MMU-off code (idmap) so it is self-contained

From: Julien Grall <[email protected]>

Hi all,

Right now, the MMU-off code will access may access data that are either in .rodata or .data. With the enablement of cache coloring, Xen may not be physcally contiguous anymore when secondary CPUs are brought up.

There are multiple way to solve this problem. The first is to keep a copy of Xen physically contiguous in memory. The downside is this means we are using up to 8MB (maximum size of Xen) when only a few KBs is necessary.

This series is reworking the logic so all the MMU-off code is now self-contained for secondary boot CPUs on arm64.

On arm32, this is not yet possible because secondary CPUs needs to rebuild boot page-tables.

Cheers,

Julien Grall (3):
arm/smpboot: Move smp_up_cpu to a new section .data.idmap xen/arm64: head: Use PRINT_ID() for secondary CPU MMU-off boot code [DO NOT COMMIT] xen/arm: Create a trampoline for secondary boot CPUs

 xen/arch/arm/arm64/head.S               | 14 +++----
 xen/arch/arm/arm64/mmu/head.S           |  2 +-
 xen/arch/arm/arm64/mmu/mm.c             |  7 ++++
 xen/arch/arm/include/asm/arm64/macros.h |  9 +++--
 xen/arch/arm/mmu/smpboot.c              |  4 +-
 xen/arch/arm/psci.c                     |  5 ++-
 xen/arch/arm/smpboot.c                  | 49 ++++++++++++++++++++++---
 7 files changed, 72 insertions(+), 18 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/3 arm/smpboot: Move smp_up_cpu to a new section .data.idmap

From: Julien Grall <[email protected]>

With the upcoming work to color Xen, the binary will not be anymore physically contiguous. This will be a problem during boot as the assembly code will need to work out where each piece of Xen reside.

An easy way to solve the issue is to have all code/data accessed by the secondary CPUs while the MMU is off within a single page.

Right now, smp_up_cpu is used by secondary CPUs to wait there turn for booting before the MMU is on. Yet it is currently in .data which is unlikely to be within the same page as the rest of the idmap.

Move smp_up_cpu to the recently create section .data.idmap. The idmap is currently part of the text section and therefore will be mapped read-onl executable. This means that we need to temporarily remap smp_up_cpu in order to update it.

Introduce a new function set_smp_up_cpu() for this purpose so the code is not duplicated between when opening and closing the gate.

---
 xen/arch/arm/smpboot.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
xen/arch/arm/smpboot.c +31 -5
index 7110bc11fc05..8d508a1bb258 100644
@@ -29,6 +29,10 @@
 #include <asm/psci.h>
 #include <asm/acpi.h>
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef virt_to_mfn
+#define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
+
 cpumask_t cpu_online_map;
 cpumask_t cpu_present_map;
 cpumask_t cpu_possible_map;
@@ -56,7 +60,7 @@ struct init_info init_data =
 };
 
 /* Shared state for coordinating CPU bringup */
-unsigned long smp_up_cpu = MPIDR_INVALID;
+unsigned long __section(".data.idmap") smp_up_cpu = MPIDR_INVALID;
 /* Shared state for coordinating CPU teardown */
 static bool cpu_is_dead;
 
@@ -429,6 +433,28 @@ void stop_cpu(void)
         wfi();
 }
 
+static void set_smp_up_cpu(unsigned long mpidr)
+{
+    /*
+     * smp_up_cpu is part of the identity mapping which is read-only. So
+     * We need to re-map the region so it can be updated.
+     */
+    void *ptr = map_domain_page(virt_to_mfn(&smp_up_cpu));
+
+    ptr += PAGE_OFFSET(&smp_up_cpu);
+
+    *(unsigned long *)ptr = mpidr;
+
+    /*
+     * init_ttbr will be accessed with the MMU off, so ensure the update
+     * is visible by cleaning the cache.
+     */
+    clean_dcache(ptr);
+
+    unmap_domain_page(ptr);
+
+}
+
 int __init cpu_up_send_sgi(int cpu)
 {
     /* We don't know the GIC ID of the CPU until it has woken up, so just
@@ -460,8 +486,7 @@ int __cpu_up(unsigned int cpu)
     init_data.cpuid = cpu;
 
     /* Open the gate for this CPU */
-    smp_up_cpu = cpu_logical_map(cpu);
-    clean_dcache(smp_up_cpu);
+    set_smp_up_cpu(cpu_logical_map(cpu));
 
     rc = arch_cpu_up(cpu);
 
@@ -497,8 +522,9 @@ int __cpu_up(unsigned int cpu)
      */
     init_data.stack = NULL;
     init_data.cpuid = ~0;
-    smp_up_cpu = MPIDR_INVALID;
-    clean_dcache(smp_up_cpu);
+
+    set_smp_up_cpu(MPIDR_INVALID);
+
     arch_cpu_up_finish();
 
     if ( !cpu_online(cpu) )
Signature · 1 line
2.40.1
#2 lore raw

PATCH 2/3 xen/arm64: head: Use PRINT_ID() for secondary CPU MMU-off boot code

In reply to Julien Grall

From: Julien Grall <[email protected]>

With the upcoming work to color Xen, the binary will not be anymore physically contiguous. This will be a problem during boot as the assembly code will need to work out where each piece of Xen reside.

An easy way to solve the issue is to have all code/data accessed by the secondary CPUs while the MMU is off within a single page.

Right now, most of the early printk messages are using PRINT() which will add the message in .rodata. This is unlikely to be within the same page as the rest of the idmap.

So replace all the PRINT() that can be reachable by the secondary CPU with MMU-off with PRINT_ID().

---
 xen/arch/arm/arm64/head.S               | 14 +++++++-------
 xen/arch/arm/arm64/mmu/head.S           |  2 +-
 xen/arch/arm/include/asm/arm64/macros.h |  9 ++++++---
 3 files changed, 14 insertions(+), 11 deletions(-)
xen/arch/arm/arm64/head.S +7 -7
index cfc04c755400..fa8b00b6f1db 100644
@@ -289,9 +289,9 @@ GLOBAL(init_secondary)
 
 #ifdef CONFIG_EARLY_PRINTK
         ldr   x23, =CONFIG_EARLY_UART_BASE_ADDRESS /* x23 := UART base address */
-        PRINT("- CPU ")
+        PRINT_ID("- CPU ")
         print_reg x24
-        PRINT(" booting -\r\n")
+        PRINT_ID(" booting -\r\n")
 #endif
         bl    check_cpu_mode
         bl    cpu_init
@@ -314,10 +314,10 @@ ENDPROC(init_secondary)
  * Clobbers x0 - x5
  */
 check_cpu_mode:
-        PRINT("- Current EL ")
+        PRINT_ID("- Current EL ")
         mrs   x5, CurrentEL
         print_reg x5
-        PRINT(" -\r\n")
+        PRINT_ID(" -\r\n")
 
         /* Are we in EL2 */
         cmp   x5, #PSR_MODE_EL2t
@@ -326,8 +326,8 @@ check_cpu_mode:
         ret
 1:
         /* OK, we're boned. */
-        PRINT("- Xen must be entered in NS EL2 mode -\r\n")
-        PRINT("- Please update the bootloader -\r\n")
+        PRINT_ID("- Xen must be entered in NS EL2 mode -\r\n")
+        PRINT_ID("- Please update the bootloader -\r\n")
         b fail
 ENDPROC(check_cpu_mode)
 
@@ -361,7 +361,7 @@ ENDPROC(zero_bss)
  * Clobbers x0 - x3
  */
 cpu_init:
-        PRINT("- Initialize CPU -\r\n")
+        PRINT_ID("- Initialize CPU -\r\n")
 
         /* Set up memory attribute type tables */
         ldr   x0, =MAIRVAL
xen/arch/arm/arm64/mmu/head.S +1 -1
index 92b62ae94ce5..fa40b696ddc8 100644
@@ -276,7 +276,7 @@ ENDPROC(create_page_tables)
 enable_mmu:
         mov   x4, x0
         mov   x5, x1
-        PRINT("- Turning on paging -\r\n")
+        PRINT_ID("- Turning on paging -\r\n")
 
         /*
          * The state of the TLBs is unknown before turning on the MMU.
xen/arch/arm/include/asm/arm64/macros.h +6 -3
index 10e652041f57..6a0108f778a2 100644
@@ -39,9 +39,12 @@
  * There are multiple flavors:
  *  - PRINT_SECT(section, string): The @string will be located in @section
  *  - PRINT(): The string will be located in .rodata.str.
- *  - PRINT_ID(): When Xen is running on the Identity Mapping, it is
- *    only possible to have a limited amount of Xen. This will create
- *    the string in .rodata.idmap which will always be mapped.
+ *  - PRINT_ID(): This will create the string in .rodata.idmap which
+ *    will always be accessible. This is used when:
+ *      - Xen is running on the identity mapping because not all of Xen is mapped
+ *      - Running with the MMU-off on secondary boots as Xen may not be
+ *        physically contiguous in memory (e.g. in the case of cache
+ *        coloring).
  *
  * Clobbers x0 - x3
  */
Signature · 1 line
2.40.1
#3 lore raw

PATCH 3/3 xen/arm: Create a trampoline for secondary boot CPUs

In reply to Julien Grall

From: Julien Grall <[email protected]>

In order to confirm the early boot code is self-contained, allocate a separate trampoline region for secondary to boot from it.

---
 xen/arch/arm/arm64/mmu/mm.c |  7 +++++++
 xen/arch/arm/mmu/smpboot.c  |  4 +++-
 xen/arch/arm/psci.c         |  5 ++++-
 xen/arch/arm/smpboot.c      | 15 ++++++++++++++-
 4 files changed, 28 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/mmu/mm.c +7 -0
index d2651c948698..3c4988dc75d1 100644
@@ -110,11 +110,18 @@ void __init arch_setup_page_tables(void)
     prepare_runtime_identity_mapping();
 }
 
+extern mfn_t trampoline_start;
+
 void update_identity_mapping(bool enable)
 {
     paddr_t id_addr = virt_to_maddr(_start);
     int rc;
 
+    if ( !mfn_eq(trampoline_start, INVALID_MFN) )
+    {
+        id_addr = mfn_to_maddr(trampoline_start);
+    }
+
     if ( enable )
         rc = map_pages_to_xen(id_addr, maddr_to_mfn(id_addr), 1,
                               PAGE_HYPERVISOR_RX);
xen/arch/arm/mmu/smpboot.c +3 -1
index f1cf9252710c..d768dfe065a5 100644
@@ -72,13 +72,15 @@ static void clear_boot_pagetables(void)
     clear_table(boot_third);
 }
 
+extern mfn_t trampoline_start;
+
 static void set_init_ttbr(lpae_t *root)
 {
     /*
      * init_ttbr is part of the identity mapping which is read-only. So
      * We need to re-map the region so it can be updated
      */
-    void *ptr = map_domain_page(virt_to_mfn(&init_ttbr));
+    void *ptr = map_domain_page(trampoline_start);
 
     ptr += PAGE_OFFSET(&init_ttbr);
 
xen/arch/arm/psci.c +4 -1
index 695d2fa1f1b5..a00574d559d6 100644
@@ -36,11 +36,14 @@ static uint32_t psci_cpu_on_nr;
 
 #define PSCI_RET(res)   ((int32_t)(res).a0)
 
+extern mfn_t trampoline_start;
+
 int call_psci_cpu_on(int cpu)
 {
     struct arm_smccc_res res;
 
-    arm_smccc_smc(psci_cpu_on_nr, cpu_logical_map(cpu), __pa(init_secondary),
+    arm_smccc_smc(psci_cpu_on_nr, cpu_logical_map(cpu),
+                  mfn_to_maddr(trampoline_start) + PAGE_OFFSET(init_secondary),
                   &res);
 
     return PSCI_RET(res);
xen/arch/arm/smpboot.c +14 -1
index 8d508a1bb258..ef84b73ebd46 100644
@@ -293,10 +293,13 @@ unsigned int __init smp_get_max_cpus(void)
     return cpus;
 }
 
+mfn_t trampoline_start = INVALID_MFN_INITIALIZER;
+
 void __init
 smp_prepare_cpus(void)
 {
     int rc;
+    void *trampoline;
 
     cpumask_copy(&cpu_present_map, &cpu_possible_map);
 
@@ -304,6 +307,16 @@ smp_prepare_cpus(void)
     if ( rc )
         panic("Unable to allocate CPU sibling/core maps\n");
 
+    /* Create a trampoline to confirm early boot code is self-contained */
+    trampoline = alloc_xenheap_page();
+    BUG_ON(!trampoline);
+
+    memcpy(trampoline, _start, PAGE_SIZE);
+    clean_dcache_va_range(trampoline, PAGE_SIZE);
+    invalidate_icache();
+
+    printk("Trampoline 0x%lx\n", virt_to_maddr(trampoline));
+    trampoline_start = virt_to_mfn(trampoline);
 }
 
 /* Boot the current CPU */
@@ -439,7 +452,7 @@ static void set_smp_up_cpu(unsigned long mpidr)
      * smp_up_cpu is part of the identity mapping which is read-only. So
      * We need to re-map the region so it can be updated.
      */
-    void *ptr = map_domain_page(virt_to_mfn(&smp_up_cpu));
+    void *ptr = map_domain_page(trampoline_start);
 
     ptr += PAGE_OFFSET(&smp_up_cpu);
 
Signature · 1 line
2.40.1
#4 lore raw

Re: [PATCH 3/3] [DO NOT COMMIT] xen/arm: Create a trampoline for secondary boot CPUs

Hi Julien,

Quoted 42 lines · Julien Grall

From: Julien Grall <[email protected]>

In order to confirm the early boot code is self-contained, allocate a separate trampoline region for secondary to boot from it.

---
 xen/arch/arm/arm64/mmu/mm.c |  7 +++++++
 xen/arch/arm/mmu/smpboot.c  |  4 +++-
 xen/arch/arm/psci.c         |  5 ++++-
 xen/arch/arm/smpboot.c      | 15 ++++++++++++++-
 4 files changed, 28 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/mmu/mm.c +7 -0
index d2651c948698..3c4988dc75d1 100644
@@ -110,11 +110,18 @@ void __init arch_setup_page_tables(void)
     prepare_runtime_identity_mapping();
 }
 
+extern mfn_t trampoline_start;
+
 void update_identity_mapping(bool enable)
 {
     paddr_t id_addr = virt_to_maddr(_start);
     int rc;
 
+    if ( !mfn_eq(trampoline_start, INVALID_MFN) )
+    {
+        id_addr = mfn_to_maddr(trampoline_start);
+    }
+
     if ( enable )
         rc = map_pages_to_xen(id_addr, maddr_to_mfn(id_addr), 1,
                               PAGE_HYPERVISOR_RX);
xen/arch/arm/mmu/smpboot.c +0 -0
index f1cf9252710c..d768dfe065a5 100644

xen/arch/arm/mmu/smpboot.c static void clear_boot_pagetables(void) · line 77

Quoted 6 lines
xen/arch/arm/mmu/smpboot.c +2 -0
@@ -72,13 +72,15 @@ static void clear_boot_pagetables(void)
     clear_table(boot_third);
 }
 
+extern mfn_t trampoline_start;
+
 static void set_init_ttbr(lpae_t *root)

Isn't this function not present in the patch series?

Quoted 75 lines
xen/arch/arm/mmu/smpboot.c +1 -1
 {
     /*
      * init_ttbr is part of the identity mapping which is read-only. So
      * We need to re-map the region so it can be updated
      */
-    void *ptr = map_domain_page(virt_to_mfn(&init_ttbr));
+    void *ptr = map_domain_page(trampoline_start);
 
     ptr += PAGE_OFFSET(&init_ttbr);
 
xen/arch/arm/psci.c +4 -1
index 695d2fa1f1b5..a00574d559d6 100644
@@ -36,11 +36,14 @@ static uint32_t psci_cpu_on_nr;
 
 #define PSCI_RET(res)   ((int32_t)(res).a0)
 
+extern mfn_t trampoline_start;
+
 int call_psci_cpu_on(int cpu)
 {
     struct arm_smccc_res res;
 
-    arm_smccc_smc(psci_cpu_on_nr, cpu_logical_map(cpu), __pa(init_secondary),
+    arm_smccc_smc(psci_cpu_on_nr, cpu_logical_map(cpu),
+                  mfn_to_maddr(trampoline_start) + PAGE_OFFSET(init_secondary),
                   &res);
 
     return PSCI_RET(res);
xen/arch/arm/smpboot.c +14 -2
index 8d508a1bb258..ef84b73ebd46 100644
@@ -293,10 +293,13 @@ unsigned int __init smp_get_max_cpus(void)
     return cpus;
 }
 
+mfn_t trampoline_start = INVALID_MFN_INITIALIZER;
+
 void __init
 smp_prepare_cpus(void)
 {
     int rc;
+    void *trampoline;
 
     cpumask_copy(&cpu_present_map, &cpu_possible_map);
 
@@ -304,6 +307,16 @@ smp_prepare_cpus(void)
     if ( rc )
         panic("Unable to allocate CPU sibling/core maps\n");
 
+    /* Create a trampoline to confirm early boot code is self-contained */
+    trampoline = alloc_xenheap_page();
+    BUG_ON(!trampoline);
+
+    memcpy(trampoline, _start, PAGE_SIZE);
+    clean_dcache_va_range(trampoline, PAGE_SIZE);
+    invalidate_icache();
+
+    printk("Trampoline 0x%lx\n", virt_to_maddr(trampoline));
+    trampoline_start = virt_to_mfn(trampoline);
 }
 
 /* Boot the current CPU */
@@ -439,7 +452,7 @@ static void set_smp_up_cpu(unsigned long mpidr)
      * smp_up_cpu is part of the identity mapping which is read-only. So
      * We need to re-map the region so it can be updated.
      */
-    void *ptr = map_domain_page(virt_to_mfn(&smp_up_cpu));
+    void *ptr = map_domain_page(trampoline_start);
 
     ptr += PAGE_OFFSET(&smp_up_cpu);
 
--
Quoted 2 lines

2.40.1

Thank you very much for your help.

#5 lore raw

Re: [PATCH 3/3] [DO NOT COMMIT] xen/arm: Create a trampoline for secondary boot CPUs

Quoted 48 lines · Carlo Nonato

Hi Julien,

Quoted 45 lines · Julien Grall

From: Julien Grall <[email protected]>

In order to confirm the early boot code is self-contained, allocate a separate trampoline region for secondary to boot from it.

---
  xen/arch/arm/arm64/mmu/mm.c |  7 +++++++
  xen/arch/arm/mmu/smpboot.c  |  4 +++-
  xen/arch/arm/psci.c         |  5 ++++-
  xen/arch/arm/smpboot.c      | 15 ++++++++++++++-
  4 files changed, 28 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/mmu/mm.c +7 -0
index d2651c948698..3c4988dc75d1 100644
@@ -110,11 +110,18 @@ void __init arch_setup_page_tables(void)
      prepare_runtime_identity_mapping();
  }
 
+extern mfn_t trampoline_start;
+
  void update_identity_mapping(bool enable)
  {
      paddr_t id_addr = virt_to_maddr(_start);
      int rc;
 
+    if ( !mfn_eq(trampoline_start, INVALID_MFN) )
+    {
+        id_addr = mfn_to_maddr(trampoline_start);
+    }
+
      if ( enable )
          rc = map_pages_to_xen(id_addr, maddr_to_mfn(id_addr), 1,
                                PAGE_HYPERVISOR_RX);
xen/arch/arm/mmu/smpboot.c +0 -0
index f1cf9252710c..d768dfe065a5 100644
@@ -72,13 +72,15 @@ static void clear_boot_pagetables(void)
      clear_table(boot_third);
  }
 
Quoted 5 lines

xen/arch/arm/mmu/smpboot.c static void clear_boot_pagetables(void) · line 77

Quoted 3 lines
xen/arch/arm/mmu/smpboot.c +2 -0
+extern mfn_t trampoline_start;
+
  static void set_init_ttbr(lpae_t *root)

Isn't this function not present in the patch series?

Oh. It looks like I forgot to post one patch. Let me resend it.

Cheers,

Signature · 1 line
Julien Grall