Skip to contentSkip to conversation

xen/arm: Don't switch TTBR while the MMU is on

PATCH Superseded Julien Grall 3 participants 10 messages Mar 2, 2023 – Mar 25, 2023

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover xen/arm: Don't switch TTBR while the MMU is on

From: Julien Grall <[email protected]>

Hi all,

Currently, Xen on Arm will switch TTBR whilst the MMU is on. This is similar to replacing existing mappings with new ones. So we need to follow a break-before-make sequence.

When switching the TTBR, we need to temporarily disable the MMU before updating the TTBR. This means the page-tables must contain an identity mapping.

The current memory layout is not very flexible and has an higher chance to clash with the identity mapping.

On Arm64, we have plenty of unused virtual address space Therefore, we can simply reshuffle the layout to leave the first part of the virtual address space empty.

On Arm32, the virtual address space is already quite full. Even if we find space, it would be necessary to have a dynamic layout. So a different approach will be necessary. The chosen one is to have a temporary mapping that will be used to jumped from the ID mapping to the runtime mapping (or vice versa). The temporary mapping will be overlapping with the domheap area as it should not be used when switching on/off the MMU.

The Arm32 part is not yet addressed and will be handled in a follow-up series.

After this series, most of Xen page-table code should be compliant with the Arm Arm. The last two issues I am aware of are:
- domheap: Mappings are replaced without using the Break-Before-Make approach.
- The cache is not cleaned/invalidated when updating the page-tables with Data cache off (like during early boot).

The long term plan is to get rid of boot_* page tables and then directly use the runtime pages. This means for coloring, we will need to build the pages in the relocated Xen rather than the current Xen.

For convience, I pushed a branch with everything applied:

https://xenbits.xen.org/git-http/people/julieng/xen-unstable.git
branch boot-pt-rework-v6

Cheers,

Julien Grall (5):
xen/arm32: head: Widen the use of the temporary mapping xen/arm64: Rework the memory layout
xen/arm64: mm: Introduce helpers to prepare/enable/disable the
identity mapping
xen/arm64: mm: Rework switch_ttbr()
xen/arm64: smpboot: Directly switch to the runtime page-tables

 xen/arch/arm/arm32/head.S           |  86 +++------------
 xen/arch/arm/arm32/smpboot.c        |   4 +
 xen/arch/arm/arm64/Makefile         |   1 +
 xen/arch/arm/arm64/head.S           |  82 +++++++-------
 xen/arch/arm/arm64/mm.c             | 161 ++++++++++++++++++++++++++++
 xen/arch/arm/arm64/smpboot.c        |  15 ++-
 xen/arch/arm/include/asm/arm32/mm.h |   4 +
 xen/arch/arm/include/asm/arm64/mm.h |  13 +++
 xen/arch/arm/include/asm/config.h   |  34 ++++--
 xen/arch/arm/include/asm/mm.h       |   2 +
 xen/arch/arm/include/asm/setup.h    |  11 ++
 xen/arch/arm/include/asm/smp.h      |   1 +
 xen/arch/arm/mm.c                   |  19 ++--
 xen/arch/arm/smpboot.c              |   1 +
 14 files changed, 306 insertions(+), 128 deletions(-)
 create mode 100644 xen/arch/arm/arm64/mm.c
Signature · 1 line
2.39.1
#1 lore raw

PATCH v6 1/5 xen/arm32: head: Widen the use of the temporary mapping

From: Julien Grall <[email protected]>

At the moment, the temporary mapping is only used when the virtual runtime region of Xen is clashing with the physical region.

In follow-up patches, we will rework how secondary CPU bring-up works and it will be convenient to use the fixmap area for accessing the root page-table (it is per-cpu).

Rework the code to use temporary mapping when the Xen physical address is not overlapping with the temporary mapping.

This also has the advantage to simplify the logic to identity map Xen.

----

Even if this patch is rewriting part of the previous patch, I decided to keep them separated to help the review.

The "follow-up patches" are still in draft at the moment. I still haven't find a way to split them nicely and not require too much more work in the coloring side.

I have provided some medium-term goal in the cover letter.

Changes in v6:
- Add Henry's reviewed-by and tested-by tag
- Add Michal's reviewed-by
- Add newline in remove_identity_mapping for clarity

Changes in v5:
- Fix typo in a comment
- No need to link boot_{second, third}_id again if we need to create a temporary area.

Changes in v3:
- Resolve conflicts after switching from "ldr rX, <label>" to "mov_w rX, <label>" in a previous patch

Changes in v2:
- Patch added

---
 xen/arch/arm/arm32/head.S | 86 ++++++++-------------------------------
 1 file changed, 16 insertions(+), 70 deletions(-)
xen/arch/arm/arm32/head.S +16 -70
index df51550baa8a..9befffd85079 100644
@@ -459,7 +459,6 @@ ENDPROC(cpu_init)
 create_page_tables:
         /* Prepare the page-tables for mapping Xen */
         mov_w r0, XEN_VIRT_START
-        create_table_entry boot_pgtable, boot_second, r0, 1
         create_table_entry boot_second, boot_third, r0, 2
 
         /* Setup boot_third: */
@@ -479,70 +478,37 @@ create_page_tables:
         cmp   r1, #(XEN_PT_LPAE_ENTRIES<<3) /* 512*8-byte entries per page */
         blo   1b
 
-        /*
-         * If Xen is loaded at exactly XEN_VIRT_START then we don't
-         * need an additional 1:1 mapping, the virtual mapping will
-         * suffice.
-         */
-        cmp   r9, #XEN_VIRT_START
-        moveq pc, lr
-
         /*
          * Setup the 1:1 mapping so we can turn the MMU on. Note that
          * only the first page of Xen will be part of the 1:1 mapping.
-         *
-         * In all the cases, we will link boot_third_id. So create the
-         * mapping in advance.
          */
+        create_table_entry boot_pgtable, boot_second_id, r9, 1
+        create_table_entry boot_second_id, boot_third_id, r9, 2
         create_mapping_entry boot_third_id, r9, r9
 
         /*
-         * Find the first slot used. If the slot is not XEN_FIRST_SLOT,
-         * then the 1:1 mapping will use its own set of page-tables from
-         * the second level.
+         * Find the first slot used. If the slot is not the same
+         * as TEMPORARY_AREA_FIRST_SLOT, then we will want to switch
+         * to the temporary mapping before jumping to the runtime
+         * virtual mapping.
          */
         get_table_slot r1, r9, 1     /* r1 := first slot */
-        cmp   r1, #XEN_FIRST_SLOT
-        beq   1f
-        create_table_entry boot_pgtable, boot_second_id, r9, 1
-        b     link_from_second_id
-
-1:
-        /*
-         * Find the second slot used. If the slot is XEN_SECOND_SLOT, then the
-         * 1:1 mapping will use its own set of page-tables from the
-         * third level.
-         */
-        get_table_slot r1, r9, 2     /* r1 := second slot */
-        cmp   r1, #XEN_SECOND_SLOT
-        beq   virtphys_clash
-        create_table_entry boot_second, boot_third_id, r9, 2
-        b     link_from_third_id
+        cmp   r1, #TEMPORARY_AREA_FIRST_SLOT
+        bne   use_temporary_mapping
 
-link_from_second_id:
-        create_table_entry boot_second_id, boot_third_id, r9, 2
-link_from_third_id:
-        /* Good news, we are not clashing with Xen virtual mapping */
+        mov_w r0, XEN_VIRT_START
+        create_table_entry boot_pgtable, boot_second, r0, 1
         mov   r12, #0                /* r12 := temporary mapping not created */
         mov   pc, lr
 
-virtphys_clash:
+use_temporary_mapping:
         /*
-         * The identity map clashes with boot_third. Link boot_first_id and
-         * map Xen to a temporary mapping. See switch_to_runtime_mapping
-         * for more details.
+         * The identity mapping is not using the first slot
+         * TEMPORARY_AREA_FIRST_SLOT. Create a temporary mapping.
+         * See switch_to_runtime_mapping for more details.
          */
-        PRINT("- Virt and Phys addresses clash  -\r\n")
         PRINT("- Create temporary mapping -\r\n")
 
-        /*
-         * This will override the link to boot_second in XEN_FIRST_SLOT.
-         * The page-tables are not live yet. So no need to use
-         * break-before-make.
-         */
-        create_table_entry boot_pgtable, boot_second_id, r9, 1
-        create_table_entry boot_second_id, boot_third_id, r9, 2
-
         /* Map boot_second (cover Xen mappings) to the temporary 1st slot */
         mov_w r0, TEMPORARY_XEN_VIRT_START
         create_table_entry boot_pgtable, boot_second, r0, 1
@@ -675,33 +641,13 @@ remove_identity_mapping:
         /* r2:r3 := invalid page-table entry */
         mov   r2, #0x0
         mov   r3, #0x0
-        /*
-         * Find the first slot used. Remove the entry for the first
-         * table if the slot is not XEN_FIRST_SLOT.
-         */
+
+        /* Find the first slot used and remove it */
         get_table_slot r1, r9, 1     /* r1 := first slot */
-        cmp   r1, #XEN_FIRST_SLOT
-        beq   1f
-        /* It is not in slot 0, remove the entry */
         mov_w r0, boot_pgtable       /* r0 := root table */
         lsl   r1, r1, #3             /* r1 := Slot offset */
         strd  r2, r3, [r0, r1]
-        b     identity_mapping_removed
-
-1:
-        /*
-         * Find the second slot used. Remove the entry for the first
-         * table if the slot is not XEN_SECOND_SLOT.
-         */
-        get_table_slot r1, r9, 2     /* r1 := second slot */
-        cmp   r1, #XEN_SECOND_SLOT
-        beq   identity_mapping_removed
-        /* It is not in slot 1, remove the entry */
-        mov_w r0, boot_second        /* r0 := second table */
-        lsl   r1, r1, #3             /* r1 := Slot offset */
-        strd  r2, r3, [r0, r1]
 
-identity_mapping_removed:
         flush_xen_tlb_local r0
         mov   pc, lr
 ENDPROC(remove_identity_mapping)
Signature · 1 line
2.39.1
Reviewed-by on v6 1/5
#2 lore raw

Re: [PATCH v6 1/5] xen/arm32: head: Widen the use of the temporary mapping

Hi Julien,

Quoted 14 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, the temporary mapping is only used when the virtual runtime region of Xen is clashing with the physical region.

In follow-up patches, we will rework how secondary CPU bring-up works and it will be convenient to use the fixmap area for accessing the root page-table (it is per-cpu).

Rework the code to use temporary mapping when the Xen physical address is not overlapping with the temporary mapping.

Quoted 7 lines

This also has the advantage to simplify the logic to identity map Xen.

Cheers
Bertrand

Quoted 169 lines

----

Even if this patch is rewriting part of the previous patch, I decided to keep them separated to help the review.

The "follow-up patches" are still in draft at the moment. I still haven't find a way to split them nicely and not require too much more work in the coloring side.

I have provided some medium-term goal in the cover letter.

Changes in v6:
- Add Henry's reviewed-by and tested-by tag
- Add Michal's reviewed-by
- Add newline in remove_identity_mapping for clarity

Changes in v5:
- Fix typo in a comment
- No need to link boot_{second, third}_id again if we need to create a temporary area.

Changes in v3:
- Resolve conflicts after switching from "ldr rX, <label>" to "mov_w rX, <label>" in a previous patch

Changes in v2:
- Patch added
---
xen/arch/arm/arm32/head.S | 86 ++++++++-------------------------------

1 file changed, 16 insertions(+), 70 deletions(-)
xen/arch/arm/arm32/head.S +0 -0
index df51550baa8a..9befffd85079 100644
@@ -459,7 +459,6 @@ ENDPROC(cpu_init)

create_page_tables:
/* Prepare the page-tables for mapping Xen */ mov_w r0, XEN_VIRT_START

xen/arch/arm/arm32/head.S +16 -70
-        create_table_entry boot_pgtable, boot_second, r0, 1
        create_table_entry boot_second, boot_third, r0, 2
 
        /* Setup boot_third: */
@@ -479,70 +478,37 @@ create_page_tables:
        cmp   r1, #(XEN_PT_LPAE_ENTRIES<<3) /* 512*8-byte entries per page */
        blo   1b
 
-        /*
-         * If Xen is loaded at exactly XEN_VIRT_START then we don't
-         * need an additional 1:1 mapping, the virtual mapping will
-         * suffice.
-         */
-        cmp   r9, #XEN_VIRT_START
-        moveq pc, lr
-
        /*
         * Setup the 1:1 mapping so we can turn the MMU on. Note that
         * only the first page of Xen will be part of the 1:1 mapping.
-         *
-         * In all the cases, we will link boot_third_id. So create the
-         * mapping in advance.
         */
+        create_table_entry boot_pgtable, boot_second_id, r9, 1
+        create_table_entry boot_second_id, boot_third_id, r9, 2
        create_mapping_entry boot_third_id, r9, r9
 
        /*
-         * Find the first slot used. If the slot is not XEN_FIRST_SLOT,
-         * then the 1:1 mapping will use its own set of page-tables from
-         * the second level.
+         * Find the first slot used. If the slot is not the same
+         * as TEMPORARY_AREA_FIRST_SLOT, then we will want to switch
+         * to the temporary mapping before jumping to the runtime
+         * virtual mapping.
         */
        get_table_slot r1, r9, 1     /* r1 := first slot */
-        cmp   r1, #XEN_FIRST_SLOT
-        beq   1f
-        create_table_entry boot_pgtable, boot_second_id, r9, 1
-        b     link_from_second_id
-
-1:
-        /*
-         * Find the second slot used. If the slot is XEN_SECOND_SLOT, then the
-         * 1:1 mapping will use its own set of page-tables from the
-         * third level.
-         */
-        get_table_slot r1, r9, 2     /* r1 := second slot */
-        cmp   r1, #XEN_SECOND_SLOT
-        beq   virtphys_clash
-        create_table_entry boot_second, boot_third_id, r9, 2
-        b     link_from_third_id
+        cmp   r1, #TEMPORARY_AREA_FIRST_SLOT
+        bne   use_temporary_mapping
 
-link_from_second_id:
-        create_table_entry boot_second_id, boot_third_id, r9, 2
-link_from_third_id:
-        /* Good news, we are not clashing with Xen virtual mapping */
+        mov_w r0, XEN_VIRT_START
+        create_table_entry boot_pgtable, boot_second, r0, 1
        mov   r12, #0                /* r12 := temporary mapping not created */
        mov   pc, lr
 
-virtphys_clash:
+use_temporary_mapping:
        /*
-         * The identity map clashes with boot_third. Link boot_first_id and
-         * map Xen to a temporary mapping. See switch_to_runtime_mapping
-         * for more details.
+         * The identity mapping is not using the first slot
+         * TEMPORARY_AREA_FIRST_SLOT. Create a temporary mapping.
+         * See switch_to_runtime_mapping for more details.
         */
-        PRINT("- Virt and Phys addresses clash  -\r\n")
        PRINT("- Create temporary mapping -\r\n")
 
-        /*
-         * This will override the link to boot_second in XEN_FIRST_SLOT.
-         * The page-tables are not live yet. So no need to use
-         * break-before-make.
-         */
-        create_table_entry boot_pgtable, boot_second_id, r9, 1
-        create_table_entry boot_second_id, boot_third_id, r9, 2
-
        /* Map boot_second (cover Xen mappings) to the temporary 1st slot */
        mov_w r0, TEMPORARY_XEN_VIRT_START
        create_table_entry boot_pgtable, boot_second, r0, 1
@@ -675,33 +641,13 @@ remove_identity_mapping:
        /* r2:r3 := invalid page-table entry */
        mov   r2, #0x0
        mov   r3, #0x0
-        /*
-         * Find the first slot used. Remove the entry for the first
-         * table if the slot is not XEN_FIRST_SLOT.
-         */
+
+        /* Find the first slot used and remove it */
        get_table_slot r1, r9, 1     /* r1 := first slot */
-        cmp   r1, #XEN_FIRST_SLOT
-        beq   1f
-        /* It is not in slot 0, remove the entry */
        mov_w r0, boot_pgtable       /* r0 := root table */
        lsl   r1, r1, #3             /* r1 := Slot offset */
        strd  r2, r3, [r0, r1]
-        b     identity_mapping_removed
-
-1:
-        /*
-         * Find the second slot used. Remove the entry for the first
-         * table if the slot is not XEN_SECOND_SLOT.
-         */
-        get_table_slot r1, r9, 2     /* r1 := second slot */
-        cmp   r1, #XEN_SECOND_SLOT
-        beq   identity_mapping_removed
-        /* It is not in slot 1, remove the entry */
-        mov_w r0, boot_second        /* r0 := second table */
-        lsl   r1, r1, #3             /* r1 := Slot offset */
-        strd  r2, r3, [r0, r1]
 
-identity_mapping_removed:
        flush_xen_tlb_local r0
        mov   pc, lr
 ENDPROC(remove_identity_mapping)
Signature · 1 line
2.39.1
#3 lore raw

PATCH v6 2/5 xen/arm64: Rework the memory layout

In reply to Julien Grall

From: Julien Grall <[email protected]>

Xen is currently not fully compliant with the Arm Arm because it will switch the TTBR with the MMU on.

In order to be compliant, we need to disable the MMU before switching the TTBR. The implication is the page-tables should contain an identity mapping of the code switching the TTBR.

In most of the case we expect Xen to be loaded in low memory. I am aware of one platform (i.e AMD Seattle) where the memory start above 512GB. To give us some slack, consider that Xen may be loaded in the first 2TB of the physical address space.

The memory layout is reshuffled to keep the first four slots of the zeroeth level free. Xen will now be loaded at (2TB + 2MB). This requires a slight tweak of the boot code because XEN_VIRT_START cannot be used as an immediate.

This reshuffle will make trivial to create a 1:1 mapping when Xen is loaded below 2TB.

----
Changes in v6:
- Correct the BUILD_BUG_ON(), Xen virtual address should be above 2TB (i.e. slot0 > 4).
- Add Bertrand's reviewed-by

Changes in v5:
- We are reserving 4 slots rather than 2.
- Fix the addresses in the layout comment.
- Fix the size of the region in the layout comment
- Add Luca's tested-by (the reviewed-by was not added because of the changes requested by Michal
- Add Michal's reviewed-by

Changes in v4:
- Correct the documentation
- The start address is 2TB, so slot0 is 4 not 2.

Changes in v2:
- Reword the commit message
- Load Xen at 2TB + 2MB
- Update the documentation to reflect the new layout

---
 xen/arch/arm/arm64/head.S         |  3 ++-
 xen/arch/arm/include/asm/config.h | 34 +++++++++++++++++++++----------
 xen/arch/arm/mm.c                 | 11 +++++-----
 3 files changed, 31 insertions(+), 17 deletions(-)
xen/arch/arm/arm64/head.S +2 -1
index 4a3f87117c83..663f5813b12e 100644
@@ -607,7 +607,8 @@ create_page_tables:
          * need an additional 1:1 mapping, the virtual mapping will
          * suffice.
          */
-        cmp   x19, #XEN_VIRT_START
+        ldr   x0, =XEN_VIRT_START
+        cmp   x19, x0
         bne   1f
         ret
 1:
xen/arch/arm/include/asm/config.h +23 -11
index 5df0e4c4959b..e388462c23d1 100644
@@ -72,16 +72,13 @@
 #include <xen/page-size.h>
 
 /*
- * Common ARM32 and ARM64 layout:
+ * ARM32 layout:
  *   0  -   2M   Unmapped
  *   2M -   4M   Xen text, data, bss
  *   4M -   6M   Fixmap: special-purpose 4K mapping slots
  *   6M -  10M   Early boot mapping of FDT
  *   10M - 12M   Livepatch vmap (if compiled in)
  *
- * ARM32 layout:
- *   0  -  12M   <COMMON>
- *
  *  32M - 128M   Frametable: 32 bytes per page for 12GB of RAM
  * 256M -   1G   VMAP: ioremap and early_ioremap use this virtual address
  *                    space
@@ -90,14 +87,23 @@
  *   2G -   4G   Domheap: on-demand-mapped
  *
  * ARM64 layout:
- * 0x0000000000000000 - 0x0000007fffffffff (512GB, L0 slot [0])
- *   0  -  12M   <COMMON>
+ * 0x0000000000000000 - 0x000001ffffffffff (2TB, L0 slots [0..3])
+ *
+ *  Reserved to identity map Xen
+ *
+ * 0x0000020000000000 - 0x0000027fffffffff (512GB, L0 slot [4]
+ *  (Relative offsets)
+ *   0  -   2M   Unmapped
+ *   2M -   4M   Xen text, data, bss
+ *   4M -   6M   Fixmap: special-purpose 4K mapping slots
+ *   6M -  10M   Early boot mapping of FDT
+ *  10M -  12M   Livepatch vmap (if compiled in)
  *
  *   1G -   2G   VMAP: ioremap and early_ioremap
  *
  *  32G -  64G   Frametable: 56 bytes per page for 2TB of RAM
  *
- * 0x0000008000000000 - 0x00007fffffffffff (127.5TB, L0 slots [1..255])
+ * 0x0000028000000000 - 0x00007fffffffffff (125TB, L0 slots [5..255])
  *  Unused
  *
  * 0x0000800000000000 - 0x000084ffffffffff (5TB, L0 slots [256..265])
@@ -107,7 +113,17 @@
  *  Unused
  */
 
+#ifdef CONFIG_ARM_32
 #define XEN_VIRT_START          _AT(vaddr_t, MB(2))
+#else
+
+#define SLOT0_ENTRY_BITS  39
+#define SLOT0(slot) (_AT(vaddr_t,slot) << SLOT0_ENTRY_BITS)
+#define SLOT0_ENTRY_SIZE  SLOT0(1)
+
+#define XEN_VIRT_START          (SLOT0(4) + _AT(vaddr_t, MB(2)))
+#endif
+
 #define XEN_VIRT_SIZE           _AT(vaddr_t, MB(2))
 
 #define FIXMAP_VIRT_START       (XEN_VIRT_START + XEN_VIRT_SIZE)
@@ -163,10 +179,6 @@
 
 #else /* ARM_64 */
 
-#define SLOT0_ENTRY_BITS  39
-#define SLOT0(slot) (_AT(vaddr_t,slot) << SLOT0_ENTRY_BITS)
-#define SLOT0_ENTRY_SIZE  SLOT0(1)
-
 #define VMAP_VIRT_START  GB(1)
 #define VMAP_VIRT_SIZE   GB(1)
 
xen/arch/arm/mm.c +6 -5
index f758cad545fa..9263fedc3b7d 100644
@@ -153,7 +153,7 @@ static void __init __maybe_unused build_assertions(void)
 #endif
     /* Page table structure constraints */
 #ifdef CONFIG_ARM_64
-    BUILD_BUG_ON(zeroeth_table_offset(XEN_VIRT_START));
+    BUILD_BUG_ON(zeroeth_table_offset(XEN_VIRT_START) < 4);
 #endif
     BUILD_BUG_ON(first_table_offset(XEN_VIRT_START));
 #ifdef CONFIG_ARCH_MAP_DOMAIN_PAGE
@@ -496,10 +496,11 @@ void __init setup_pagetables(unsigned long boot_phys_offset)
     phys_offset = boot_phys_offset;
 
 #ifdef CONFIG_ARM_64
-    p = (void *) xen_pgtable;
-    p[0] = pte_of_xenaddr((uintptr_t)xen_first);
-    p[0].pt.table = 1;
-    p[0].pt.xn = 0;
+    pte = pte_of_xenaddr((uintptr_t)xen_first);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+    xen_pgtable[zeroeth_table_offset(XEN_VIRT_START)] = pte;
+
     p = (void *) xen_first;
 #else
     p = (void *) cpu0_pgtable;
Signature · 1 line
2.39.1
#4 lore raw

PATCH v6 3/5 xen/arm64: mm: Introduce helpers to prepare/enable/disable the identity mapping

In reply to Julien Grall

From: Julien Grall <[email protected]>

In follow-up patches we will need to have part of Xen identity mapped in order to safely switch the TTBR.

On some platform, the identity mapping may have to start at 0. If we always keep the identity region mapped, NULL pointer dereference would lead to access to valid mapping.

It would be possible to relocate Xen to avoid clashing with address 0. However the identity mapping is only meant to be used in very limited places. Therefore it would be better to keep the identity region invalid for most of the time.

Two new external helpers are introduced:
- arch_setup_page_tables() will setup the page-tables so it is easy to create the mapping afterwards.
- update_identity_mapping() will create/remove the identity mapping

----
Changes in v6:
- Correctly check the placement of the identity mapping (take 2).
- Fix typoes

Changes in v5:
- The reserved area for the identity mapping is 2TB (so 4 slots) rather than 512GB.

Changes in v4:
- Fix typo in a comment
- Clarify which page-tables are updated

Changes in v2:
- Remove the arm32 part
- Use a different logic for the boot page tables and runtime one because Xen may be running in a different place.

---
 xen/arch/arm/arm64/Makefile         |   1 +
 xen/arch/arm/arm64/mm.c             | 130 ++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/arm32/mm.h |   4 +
 xen/arch/arm/include/asm/arm64/mm.h |  13 +++
 xen/arch/arm/include/asm/config.h   |   2 +
 xen/arch/arm/include/asm/setup.h    |  11 +++
 xen/arch/arm/mm.c                   |   6 +-
 7 files changed, 165 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/arm/arm64/mm.c
xen/arch/arm/arm64/Makefile +1 -0
index 6d507da0d44d..28481393e98f 100644
@@ -10,6 +10,7 @@ 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-y += traps.o
xen/arch/arm/arm64/mm.c new file +130 -0

130 lines not on this page · raw message

xen/arch/arm/include/asm/arm32/mm.h +4 -0
index 8bfc906e7178..856f2dbec4ad 100644
@@ -18,6 +18,10 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 
 bool init_domheap_mappings(unsigned int cpu);
 
+static inline void arch_setup_page_tables(void)
+{
+}
+
 #endif /* __ARM_ARM32_MM_H__ */
 
 /*
xen/arch/arm/include/asm/arm64/mm.h +13 -0
index aa2adac63189..e0bd23a6ed0c 100644
@@ -1,6 +1,8 @@
 #ifndef __ARM_ARM64_MM_H__
 #define __ARM_ARM64_MM_H__
 
+extern DEFINE_PAGE_TABLE(xen_pgtable);
+
 /*
  * On ARM64, all the RAM is currently direct mapped in Xen.
  * Hence return always true.
@@ -10,6 +12,17 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
     return true;
 }
 
+void arch_setup_page_tables(void);
+
+/*
+ * Enable/disable the identity mapping in the live page-tables (i.e.
+ * the one pointed by TTBR_EL2).
+ *
+ * Note that nested call (e.g. enable=true, enable=true) is not
+ * supported.
+ */
+void update_identity_mapping(bool enable);
+
 #endif /* __ARM_ARM64_MM_H__ */
 
 /*
xen/arch/arm/include/asm/config.h +2 -0
index e388462c23d1..f02733e40a87 100644
@@ -179,6 +179,8 @@
 
 #else /* ARM_64 */
 
+#define IDENTITY_MAPPING_AREA_NR_L0  4
+
 #define VMAP_VIRT_START  GB(1)
 #define VMAP_VIRT_SIZE   GB(1)
 
xen/arch/arm/include/asm/setup.h +11 -0
index a926f30a2be4..66b27f2b57c1 100644
@@ -166,6 +166,17 @@ u32 device_tree_get_u32(const void *fdt, int node,
 int map_range_to_domain(const struct dt_device_node *dev,
                         u64 addr, u64 len, void *data);
 
+extern DEFINE_BOOT_PAGE_TABLE(boot_pgtable);
+
+#ifdef CONFIG_ARM_64
+extern DEFINE_BOOT_PAGE_TABLE(boot_first_id);
+#endif
+extern DEFINE_BOOT_PAGE_TABLE(boot_second_id);
+extern DEFINE_BOOT_PAGE_TABLE(boot_third_id);
+
+/* Find where Xen will be residing at runtime and return a PT entry */
+lpae_t pte_of_xenaddr(vaddr_t);
+
 extern const char __ro_after_init_start[], __ro_after_init_end[];
 
 struct init_info
xen/arch/arm/mm.c +4 -2
index 9263fedc3b7d..265bc082567e 100644
@@ -93,7 +93,7 @@ DEFINE_BOOT_PAGE_TABLE(boot_third);
 
 #ifdef CONFIG_ARM_64
 #define HYP_PT_ROOT_LEVEL 0
-static DEFINE_PAGE_TABLE(xen_pgtable);
+DEFINE_PAGE_TABLE(xen_pgtable);
 static DEFINE_PAGE_TABLE(xen_first);
 #define THIS_CPU_PGTABLE xen_pgtable
 #else
@@ -388,7 +388,7 @@ void flush_page_to_ram(unsigned long mfn, bool sync_icache)
         invalidate_icache();
 }
 
-static inline lpae_t pte_of_xenaddr(vaddr_t va)
+lpae_t pte_of_xenaddr(vaddr_t va)
 {
     paddr_t ma = va + phys_offset;
 
@@ -495,6 +495,8 @@ void __init setup_pagetables(unsigned long boot_phys_offset)
 
     phys_offset = boot_phys_offset;
 
+    arch_setup_page_tables();
+
 #ifdef CONFIG_ARM_64
     pte = pte_of_xenaddr((uintptr_t)xen_first);
     pte.pt.table = 1;
Signature · 1 line
2.39.1
Reviewed-by on v6 3/5
#5 lore raw

RE: [PATCH v6 3/5] xen/arm64: mm: Introduce helpers to prepare/enable/disable the identity mapping

Hi Julien,

Quoted 18 lines

-----Original Message-----
Subject: [PATCH v6 3/5] xen/arm64: mm: Introduce helpers to prepare/enable/disable the identity mapping

From: Julien Grall <[email protected]>

In follow-up patches we will need to have part of Xen identity mapped in order to safely switch the TTBR.

On some platform, the identity mapping may have to start at 0. If we always keep the identity region mapped, NULL pointer dereference would lead to access to valid mapping.

It would be possible to relocate Xen to avoid clashing with address 0. However the identity mapping is only meant to be used in very limited places. Therefore it would be better to keep the identity region invalid for most of the time.

Quoted 6 lines

Two new external helpers are introduced:
- arch_setup_page_tables() will setup the page-tables so it is easy to create the mapping afterwards.
- update_identity_mapping() will create/remove the identity mapping

Kind regards,
Henry

#6 lore raw

Re: [PATCH v6 3/5] xen/arm64: mm: Introduce helpers to prepare/enable/disable the identity mapping

In reply to Julien Grall

Hi Julien,

Quoted 16 lines · Julien Grall

From: Julien Grall <[email protected]>

In follow-up patches we will need to have part of Xen identity mapped in order to safely switch the TTBR.

On some platform, the identity mapping may have to start at 0. If we always keep the identity region mapped, NULL pointer dereference would lead to access to valid mapping.

It would be possible to relocate Xen to avoid clashing with address 0. However the identity mapping is only meant to be used in very limited places. Therefore it would be better to keep the identity region invalid for most of the time.

Quoted 6 lines

Two new external helpers are introduced:
- arch_setup_page_tables() will setup the page-tables so it is easy to create the mapping afterwards.
- update_identity_mapping() will create/remove the identity mapping

In Arm internal CI this patch (or maybe an other in the serie) made one of our test crash on qemu-arm64.

The error log is here after.

Cheers
Bertrand

- UART enabled -

- Boot CPU booting -

- Current EL 0000000000000008 -

- Initialize CPU -

- Turning on paging -

- Zero BSS -

- Ready -

(XEN) Checking for initrd in /chosen

(XEN) RAM: 0000000040000000 - 00000000bfffffff

(XEN)

(XEN) MODULE[0]: 0000000040200000 - 00000000403590c8 Xen

(XEN) MODULE[1]: 0000000048000000 - 0000000048008f20 Device Tree

(XEN) MODULE[2]: 0000000045000000 - 00000000463e3200 Kernel

(XEN)

(XEN)

(XEN) Command line: console=dtuart dtuart=/pl011@9000000 dom0_mem=512m

(XEN) Domain heap initialised

(XEN) Booting using Device Tree

(XEN) Platform: Generic System

(XEN) Looking for dtuart at "/pl011@9000000", options ""

Xen 4.18-unstable

(XEN) Xen version 4.18-unstable (@eu-west-1.compute.internal) (aarch64-poky-linux-gcc (GCC) 11.3.0) debug=y 2023-03-02

(XEN) Latest ChangeSet:

(XEN) build-id: cc83f93cef7a75d303680ba1f98e756c07df5497

(XEN) Processor: 00000000411fd070: "ARM Limited", variant: 0x1, part 0xd07,rev 0x0

(XEN) 64-bit Execution:

(XEN) Processor Features: 0000000001000222 0000000000000000

(XEN) Exception Levels: EL3:No EL2:64+32 EL1:64+32 EL0:64+32

(XEN) Extensions: FloatingPoint AdvancedSIMD GICv3-SysReg

(XEN) Debug Features: 0000000010305106 0000000000000000

(XEN) Auxiliary Features: 0000000000000000 0000000000000000

(XEN) Memory Model Features: 0000000000001124 0000000000000000

(XEN) ISA Features: 0000000000011120 0000000000000000

(XEN) 32-bit Execution:

(XEN) Processor Features: 0000000000000131:0000000010011001

(XEN) Instruction Sets: AArch32 A32 Thumb Thumb-2 Jazelle

(XEN) Extensions: GenericTimer

(XEN) Debug Features: 0000000003010066

(XEN) Auxiliary Features: 0000000000000000

(XEN) Memory Model Features: 0000000010101105 0000000040000000

(XEN) 0000000001260000 0000000002102211

(XEN) ISA Features: 0000000002101110 0000000013112111 0000000021232042

(XEN) 0000000001112131 0000000000011142 0000000000011121

(XEN) Using SMC Calling Convention v1.0

(XEN) Using PSCI v0.2

(XEN) SMP: Allowing 4 CPUs

(XEN) enabled workaround for: ARM erratum 832075

(XEN) enabled workaround for: ARM erratum 834220

(XEN) enabled workaround for: ARM erratum 1319367

(XEN) Generic Timer IRQ: phys=30 hyp=26 virt=27 Freq: 62500 KHz

(XEN) GICv3 initialization:

(XEN) gic_dist_addr=0x00000008000000

(XEN) gic_maintenance_irq=25

(XEN) gic_rdist_stride=0

(XEN) gic_rdist_regions=1

(XEN) redistributor regions:

(XEN) - region 0: 0x000000080a0000 - 0x00000009000000

(XEN) GICv3: 256 lines, (IID 0000043b).

(XEN) GICv3: CPU0: Found redistributor in region 0 @000000004001c000

(XEN) XSM Framework v1.0.1 initialized

(XEN) Initialising XSM SILO mode

(XEN) Using scheduler: SMP Credit Scheduler rev2 (credit2)

(XEN) Initializing Credit2 scheduler

(XEN) load_precision_shift: 18

(XEN) load_window_shift: 30

(XEN) underload_balance_tolerance: 0

(XEN) overload_balance_tolerance: -3

(XEN) runqueues arrangement: socket

(XEN) cap enforcement granularity: 10ms

(XEN) load tracking window length 1073741824 ns

(XEN) Allocated console ring of 32 KiB.

(XEN) CPU0: Guest atomics will try 1 times before pausing the domain

(XEN) Bringing up CPU1

(XEN) arch/arm/mm.c:874: Changing MFN for a valid entry is not allowed (0x8284 -> 0x40200).

(XEN) Xen WARN at arch/arm/mm.c:874

(XEN) ----[ Xen-4.18-unstable arm64 debug=y Not tainted ]----

(XEN) CPU: 0

(XEN) PC: 0000020000272438 mm.c#xen_pt_update+0x6d0/0x82c

(XEN) LR: 0000020000272438

(XEN) SP: 000002000030fc60

(XEN) CPSR: 0000000080000249 MODE:64-bit EL2h (Hypervisor, handler)

(XEN) X0: 0000020000318038 X1: 0000000000000002 X2: 0000000000000000

(XEN) X3: 0000000000000003 X4: 00000200002be956 X5: 0000000000000002

(XEN) X6: 000080007ffe9360 X7: fefefefefefefefe X8: ffffffffffffffff

(XEN) X9: 0000000000000080 X10: 7f7f7f7f7f7f7f7f X11: 0101010101010101

(XEN) X12: 0000000000000008 X13: 0000000000000030 X14: 0000000000000005

(XEN) X15: 0000000000000000 X16: fffffff800000000 X17: 0000000000000001

(XEN) X18: 0180000000000000 X19: 0000800000326000 X20: 0000000000000003

(XEN) X21: 0000800000326000 X22: 000002000030fd5c X23: 000002000034f000

(XEN) X24: 0000000000000003 X25: 0000000000040200 X26: 000002000031b074

(XEN) X27: 0000000000000000 X28: 0000000000040200 FP: 000002000030fc60

(XEN)

(XEN) VTCR_EL2: 0000000000000000

(XEN) VTTBR_EL2: 0000000000000000

(XEN)

(XEN) SCTLR_EL2: 0000000030cd183d

(XEN) HCR_EL2: 0000000000000038

(XEN) TTBR0_EL2: 000000004034e000

(XEN)

(XEN) ESR_EL2: 00000000f2000001

(XEN) HPFAR_EL2: 0000000000000000

(XEN) FAR_EL2: 0000000000000000

(XEN)

(XEN) Xen stack trace from sp=000002000030fc60:

(XEN) 000002000030fd60 00000200002727f8 ff00000040200b80 0000000000000000

(XEN) 0000000000000001 000002000034f430 000002000034f000 00000200002aef18

(XEN) 00000200002f0d38 0000000000000001 0000000048000000 0000000000008f20

(XEN) 0000000000000001 0000020000000037 0000000000000001 0000000000000001

(XEN) 000000010030fd00 00000200002b4eb8 0000000000000001 000000000004034e

(XEN) 0000000000000020 0000020000000037 0000000000000000 0000000000000001

(XEN) 0000000000000000 000002000031b074 0000000040200000 0000000000000001

(XEN) 000002000030fd60 000002000024c710 0000000100000000 0000000000000001

(XEN) 000002000030fd70 0000020000265cac 000002000030fda0 0000020000265d38

(XEN) 0000000000000001 0000000000000000 0000000000000001 00000200002aef18

(XEN) 000002000030fdc0 00000200002787f4 0000000000000001 00000200002787ac

(XEN) 000002000030fe10 0000020000201f78 0000000000000001 0000000000000000

(XEN) 00000200002f0d38 00000200002dccf8 000002000030fe10 0000020000201f50

(XEN) 0000000000000001 00000200002c1358 000002000030fe40 00000200002dc92c

(XEN) 0000000000000001 00000200002b3000 00000000c0000000 00000200002bee58

(XEN) 0000000000000000 00000200002001bc 0000000040200000 fffffe0040000000

(XEN) 0000000048000000 0000000000000000 0000020000400000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) Xen call trace:

(XEN) [<0000020000272438>] mm.c#xen_pt_update+0x6d0/0x82c (PC)

(XEN) [<0000020000272438>] mm.c#xen_pt_update+0x6d0/0x82c (LR)

(XEN) [<00000200002727f8>] map_pages_to_xen+0x10/0x1c

(XEN) [<0000020000265cac>] update_identity_mapping+0x58/0x94

(XEN) [<0000020000265d38>] arch_cpu_up+0x30/0x78

(XEN) [<00000200002787f4>] __cpu_up+0x80/0x1d0

(XEN) [<0000020000201f78>] cpu_up+0xc8/0xf8

(XEN) [<00000200002dc92c>] start_xen+0x8fc/0xcb0

(XEN) [<00000200002001bc>] head.o#primary_switched+0x10/0x30

(XEN)

(XEN) Xen BUG at arch/arm/arm64/mm.c:120

(XEN) ----[ Xen-4.18-unstable arm64 debug=y Not tainted ]----

(XEN) CPU: 0

(XEN) PC: 0000020000265ce4 update_identity_mapping+0x90/0x94

(XEN) LR: 0000020000265cac

(XEN) SP: 000002000030fd70

(XEN) CPSR: 0000000080000249 MODE:64-bit EL2h (Hypervisor, handler)

(XEN) X0: 00000000ffffffea X1: 0000000000000001 X2: 0000000000000000

(XEN) X3: 0000000000000002 X4: 00000200002bee06 X5: 0000000000000001

(XEN) X6: 000080007ffe9358 X7: fefefefefefefefe X8: ffffffffffffffff

(XEN) X9: 0000000000000080 X10: 7f7f7f7f7f7f7f7f X11: 0101010101010101

(XEN) X12: 0000000000000008 X13: 0000000000000030 X14: 0000000000000005

(XEN) X15: 0000000000000000 X16: fffffff800000000 X17: 0000000000000001

(XEN) X18: 0180000000000000 X19: ff00000040200b80 X20: 0000000000000000

(XEN) X21: 0000000000000001 X22: 000002000034f430 X23: 000002000034f000

(XEN) X24: 00000200002aef18 X25: 00000200002f0d38 X26: 0000000000000001

(XEN) X27: 0000000048000000 X28: 0000000000008f20 FP: 000002000030fd70

(XEN)

(XEN) VTCR_EL2: 0000000000000000

(XEN) VTTBR_EL2: 0000000000000000

(XEN)

(XEN) SCTLR_EL2: 0000000030cd183d

(XEN) HCR_EL2: 0000000000000038

(XEN) TTBR0_EL2: 000000004034e000

(XEN)

(XEN) ESR_EL2: 00000000f2000001

(XEN) HPFAR_EL2: 0000000000000000

(XEN) FAR_EL2: 0000000000000000

(XEN)

(XEN) Xen stack trace from sp=000002000030fd70:

(XEN) 000002000030fda0 0000020000265d38 0000000000000001 0000000000000000

(XEN) 0000000000000001 00000200002aef18 000002000030fdc0 00000200002787f4

(XEN) 0000000000000001 00000200002787ac 000002000030fe10 0000020000201f78

(XEN) 0000000000000001 0000000000000000 00000200002f0d38 00000200002dccf8

(XEN) 000002000030fe10 0000020000201f50 0000000000000001 00000200002c1358

(XEN) 000002000030fe40 00000200002dc92c 0000000000000001 00000200002b3000

(XEN) 00000000c0000000 00000200002bee58 0000000000000000 00000200002001bc

(XEN) 0000000040200000 fffffe0040000000 0000000048000000 0000000000000000

(XEN) 0000020000400000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000 0000000000000000 0000000000000000

(XEN) 0000000000000000 0000000000000000

(XEN) Xen call trace:

(XEN) [<0000020000265ce4>] update_identity_mapping+0x90/0x94 (PC)

(XEN) [<0000020000265cac>] update_identity_mapping+0x58/0x94 (LR)

(XEN) [<0000020000265d38>] arch_cpu_up+0x30/0x78

(XEN) [<00000200002787f4>] __cpu_up+0x80/0x1d0

(XEN) [<0000020000201f78>] cpu_up+0xc8/0xf8

(XEN) [<00000200002dc92c>] start_xen+0x8fc/0xcb0

(XEN) [<00000200002001bc>] head.o#primary_switched+0x10/0x30

(XEN)

(XEN)

(XEN) ****************************************

(XEN) Panic on CPU 0:

(XEN) Xen BUG at arch/arm/arm64/mm.c:120

(XEN) ****************************************

(XEN)

(XEN) Reboot in five seconds...

#7 lore raw

Re: [PATCH v6 3/5] xen/arm64: mm: Introduce helpers to prepare/enable/disable the identity mapping

Hi Bertrand,

Quoted 23 lines · Bertrand Marquis

Hi Julien,

Quoted 21 lines · Julien Grall

From: Julien Grall <[email protected]>

In follow-up patches we will need to have part of Xen identity mapped in order to safely switch the TTBR.

On some platform, the identity mapping may have to start at 0. If we always keep the identity region mapped, NULL pointer dereference would lead to access to valid mapping.

It would be possible to relocate Xen to avoid clashing with address 0. However the identity mapping is only meant to be used in very limited places. Therefore it would be better to keep the identity region invalid for most of the time.

Two new external helpers are introduced:
- arch_setup_page_tables() will setup the page-tables so it is easy to create the mapping afterwards.
- update_identity_mapping() will create/remove the identity mapping

Quoted 4 lines
Quoted 1 line

In Arm internal CI this patch (or maybe an other in the serie) made one of our test crash on qemu-arm64.

Thanks for the report. I managed to reproduce it by tweaking the QEMU command line option I was using:

42sh> qemu/build/qemu-system-aarch64 -machine virt,gic-version=3 -machine virtualization=true -cpu cortex-a57 -smp 4 -m 2048 -serial mon:stdio -serial null -nographic -kernel xen/xen/xen

The problem is in patch #2 because I didn't adjust the address of the vmap/frametable areas. So they effectively are still right in the middle of the reserved region for identity mapping.

I will update patch #2. I am also thinking to add a check in xen_pt_update() to ensure no-one can create a non 1:1 mapping in the reserved area for identity mapping.

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

PATCH v6 4/5 xen/arm64: mm: Rework switch_ttbr()

In reply to Julien Grall

From: Julien Grall <[email protected]>

At the moment, switch_ttbr() is switching the TTBR whilst the MMU is still on.

Switching TTBR is like replacing existing mappings with new ones. So we need to follow the break-before-make sequence.

In this case, it means the MMU needs to be switched off while the TTBR is updated. In order to disable the MMU, we need to first jump to an identity mapping.

Rename switch_ttbr() to switch_ttbr_id() and create an helper on top to temporary map the identity mapping and call switch_ttbr() via the identity address.

switch_ttbr_id() is now reworked to temporarily turn off the MMU before updating the TTBR.

We also need to make sure the helper switch_ttbr() is part of the identity mapping. So move _end_boot past it.

The arm32 code will use a different approach. So this issue is for now only resolved on arm64.

----
Changes in v6:
- Add Michal's reviewed-by tag
- Add Bertrand's reviewed-by tag

Changes in v5:
- Add a newline in switch_ttbr()
- Add Luca's reviewed-by and tested-by

Changes in v4:
- Don't modify setup_pagetables() as we don't handle arm32.
- Move the clearing of the boot page tables in an earlier patch
- Fix the numbering

Changes in v2:
- Remove the arm32 changes. This will be addressed differently
- Re-instate the instruct cache flush. This is not strictly necessary but kept it for safety.
- Use "dsb ish" rather than "dsb sy".

TODO:
* Handle the case where the runtime Xen is loaded at a different position for cache coloring. This will be dealt separately.

---
 xen/arch/arm/arm64/head.S     | 50 +++++++++++++++++++++++------------
 xen/arch/arm/arm64/mm.c       | 31 ++++++++++++++++++++++
 xen/arch/arm/include/asm/mm.h |  2 ++
 xen/arch/arm/mm.c             |  2 --
 4 files changed, 66 insertions(+), 19 deletions(-)
xen/arch/arm/arm64/head.S +33 -17
index 663f5813b12e..5efd442b24af 100644
@@ -816,30 +816,46 @@ ENDPROC(fail)
  * Switch TTBR
  *
  * x0    ttbr
- *
- * TODO: This code does not comply with break-before-make.
  */
-ENTRY(switch_ttbr)
-        dsb   sy                     /* Ensure the flushes happen before
-                                      * continuing */
-        isb                          /* Ensure synchronization with previous
-                                      * changes to text */
-        tlbi   alle2                 /* Flush hypervisor TLB */
-        ic     iallu                 /* Flush I-cache */
-        dsb    sy                    /* Ensure completion of TLB flush */
+ENTRY(switch_ttbr_id)
+        /* 1) Ensure any previous read/write have completed */
+        dsb    ish
+        isb
+
+        /* 2) Turn off MMU */
+        mrs    x1, SCTLR_EL2
+        bic    x1, x1, #SCTLR_Axx_ELx_M
+        msr    SCTLR_EL2, x1
+        isb
+
+        /*
+         * 3) Flush the TLBs.
+         * See asm/arm64/flushtlb.h for the explanation of the sequence.
+         */
+        dsb   nshst
+        tlbi  alle2
+        dsb   nsh
+        isb
+
+        /* 4) Update the TTBR */
+        msr   TTBR0_EL2, x0
         isb
 
-        msr    TTBR0_EL2, x0
+        /*
+         * 5) Flush I-cache
+         * This should not be necessary but it is kept for safety.
+         */
+        ic     iallu
+        isb
 
-        isb                          /* Ensure synchronization with previous
-                                      * changes to text */
-        tlbi   alle2                 /* Flush hypervisor TLB */
-        ic     iallu                 /* Flush I-cache */
-        dsb    sy                    /* Ensure completion of TLB flush */
+        /* 6) Turn on the MMU */
+        mrs   x1, SCTLR_EL2
+        orr   x1, x1, #SCTLR_Axx_ELx_M  /* Enable MMU */
+        msr   SCTLR_EL2, x1
         isb
 
         ret
-ENDPROC(switch_ttbr)
+ENDPROC(switch_ttbr_id)
 
 #ifdef CONFIG_EARLY_PRINTK
 /*
xen/arch/arm/arm64/mm.c +31 -0
index 56b9e9b8d3ef..78b7c7eb004f 100644
@@ -120,6 +120,37 @@ void update_identity_mapping(bool enable)
     BUG_ON(rc);
 }
 
+extern void switch_ttbr_id(uint64_t ttbr);
+
+typedef void (switch_ttbr_fn)(uint64_t ttbr);
+
+void __init switch_ttbr(uint64_t ttbr)
+{
+    vaddr_t id_addr = virt_to_maddr(switch_ttbr_id);
+    switch_ttbr_fn *fn = (switch_ttbr_fn *)id_addr;
+    lpae_t pte;
+
+    /* Enable the identity mapping in the boot page tables */
+    update_identity_mapping(true);
+
+    /* Enable the identity mapping in the runtime page tables */
+    pte = pte_of_xenaddr((vaddr_t)switch_ttbr_id);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+    pte.pt.ro = 1;
+    write_pte(&xen_third_id[third_table_offset(id_addr)], pte);
+
+    /* Switch TTBR */
+    fn(ttbr);
+
+    /*
+     * Disable the identity mapping in the runtime page tables.
+     * Note it is not necessary to disable it in the boot page tables
+     * because they are not going to be used by this CPU anymore.
+     */
+    update_identity_mapping(false);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/include/asm/mm.h +2 -0
index 23dec574eb31..4262165ce25e 100644
@@ -207,6 +207,8 @@ extern unsigned long total_pages;
 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
xen/arch/arm/mm.c +0 -2
index 265bc082567e..d4d376f70f50 100644
@@ -476,8 +476,6 @@ static void xen_pt_enforce_wnx(void)
     flush_xen_tlb_local();
 }
 
-extern void switch_ttbr(uint64_t ttbr);
-
 /* Clear a translation table and clean & invalidate the cache */
 static void clear_table(void *table)
 {
Signature · 1 line
2.39.1
#9 lore raw

PATCH v6 5/5 xen/arm64: smpboot: Directly switch to the runtime page-tables

In reply to Julien Grall

From: Julien Grall <[email protected]>

Switching TTBR while the MMU is on is not safe. Now that the identity mapping will not clash with the rest of the memory layout, we can avoid creating temporary page-tables every time a CPU is brought up.

The arm32 code will use a different approach. So this issue is for now only resolved on arm64.

----
Changes in v6:
- Add Bertrand's reviewed-by

Changes in v5:
- Add Luca's reviewed-by and tested-by tags.

Changes in v4:
- Somehow I forgot to send it in v3. So re-include it.

Changes in v2:
- Remove arm32 code

---
 xen/arch/arm/arm32/smpboot.c   |  4 ++++
 xen/arch/arm/arm64/head.S      | 29 +++++++++--------------------
 xen/arch/arm/arm64/smpboot.c   | 15 ++++++++++++++-
 xen/arch/arm/include/asm/smp.h |  1 +
 xen/arch/arm/smpboot.c         |  1 +
 5 files changed, 29 insertions(+), 21 deletions(-)
xen/arch/arm/arm32/smpboot.c +4 -0
index e7368665d50d..518e9f9c7e70 100644
@@ -21,6 +21,10 @@ int arch_cpu_up(int cpu)
     return platform_cpu_up(cpu);
 }
 
+void arch_cpu_up_finish(void)
+{
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/arm64/head.S +9 -20
index 5efd442b24af..a61b4d3c2738 100644
@@ -308,6 +308,7 @@ real_start_efi:
         bl    check_cpu_mode
         bl    cpu_init
         bl    create_page_tables
+        load_paddr x0, boot_pgtable
         bl    enable_mmu
 
         /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
@@ -365,29 +366,14 @@ GLOBAL(init_secondary)
 #endif
         bl    check_cpu_mode
         bl    cpu_init
-        bl    create_page_tables
+        load_paddr x0, init_ttbr
+        ldr   x0, [x0]
         bl    enable_mmu
 
         /* We are still in the 1:1 mapping. Jump to the runtime Virtual Address. */
         ldr   x0, =secondary_switched
         br    x0
 secondary_switched:
-        /*
-         * Non-boot CPUs need to move on to the proper pagetables, which were
-         * setup in init_secondary_pagetables.
-         *
-         * XXX: This is not compliant with the Arm Arm.
-         */
-        ldr   x4, =init_ttbr         /* VA of TTBR0_EL2 stashed by CPU 0 */
-        ldr   x4, [x4]               /* Actual value */
-        dsb   sy
-        msr   TTBR0_EL2, x4
-        dsb   sy
-        isb
-        tlbi  alle2
-        dsb   sy                     /* Ensure completion of TLB flush */
-        isb
-
 #ifdef CONFIG_EARLY_PRINTK
         /* Use a virtual address to access the UART. */
         ldr   x23, =EARLY_UART_VIRTUAL_ADDRESS
@@ -672,9 +658,13 @@ ENDPROC(create_page_tables)
  * mapping. In other word, the caller is responsible to switch to the runtime
  * mapping.
  *
- * Clobbers x0 - x3
+ * Inputs:
+ *   x0 : Physical address of the page tables.
+ *
+ * Clobbers x0 - x4
  */
 enable_mmu:
+        mov   x4, x0
         PRINT("- Turning on paging -\r\n")
 
         /*
@@ -685,8 +675,7 @@ enable_mmu:
         dsb   nsh
 
         /* Write Xen's PT's paddr into TTBR0_EL2 */
-        load_paddr x0, boot_pgtable
-        msr   TTBR0_EL2, x0
+        msr   TTBR0_EL2, x4
         isb
 
         mrs   x0, SCTLR_EL2
xen/arch/arm/arm64/smpboot.c +14 -1
index 694fbf67e62a..9637f424699e 100644
@@ -106,10 +106,23 @@ int __init arch_cpu_init(int cpu, struct dt_device_node *dn)
 
 int arch_cpu_up(int cpu)
 {
+    int rc;
+
     if ( !smp_enable_ops[cpu].prepare_cpu )
         return -ENODEV;
 
-    return smp_enable_ops[cpu].prepare_cpu(cpu);
+    update_identity_mapping(true);
+
+    rc = smp_enable_ops[cpu].prepare_cpu(cpu);
+    if ( rc )
+        update_identity_mapping(false);
+
+    return rc;
+}
+
+void arch_cpu_up_finish(void)
+{
+    update_identity_mapping(false);
 }
 
 /*
xen/arch/arm/include/asm/smp.h +1 -0
index 8133d5c29572..a37ca55bff2c 100644
@@ -25,6 +25,7 @@ extern void noreturn stop_cpu(void);
 extern int arch_smp_init(void);
 extern int arch_cpu_init(int cpu, struct dt_device_node *dn);
 extern int arch_cpu_up(int cpu);
+extern void arch_cpu_up_finish(void);
 
 int cpu_up_send_sgi(int cpu);
 
xen/arch/arm/smpboot.c +1 -0
index 412ae2286906..4a89b3a8345b 100644
@@ -500,6 +500,7 @@ int __cpu_up(unsigned int cpu)
     init_data.cpuid = ~0;
     smp_up_cpu = MPIDR_INVALID;
     clean_dcache(smp_up_cpu);
+    arch_cpu_up_finish();
 
     if ( !cpu_online(cpu) )
     {
Signature · 1 line
2.39.1