Skip to contentSkip to conversation

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

PATCH Superseded Julien Grall 4 participants 21 messages Jan 27, 2023 – Feb 26, 2023

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 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-v5

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           |  85 +++------------
 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, 305 insertions(+), 128 deletions(-)
 create mode 100644 xen/arch/arm/arm64/mm.c
Signature · 1 line
2.38.1
#1 lore raw

PATCH v5 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 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 | 85 +++++++--------------------------------
 1 file changed, 15 insertions(+), 70 deletions(-)
xen/arch/arm/arm32/head.S +15 -70
index df51550baa8a..93b0af114b0c 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,12 @@ 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.38.1
Reviewed-by Tested-by on v5 1/5
#2 lore raw

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

Hi Julien,

Quoted 13 lines

-----Original Message-----
Subject: [PATCH v5 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).

Quoted 7 lines

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.

Tested on FVP in Arm32 mode, so:

Kind regards,
Henry

Reviewed-by on v5 1/5
#3 lore raw

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

In reply to Julien Grall

Hi Julien,

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

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 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 | 85 +++++++--------------------------------
 1 file changed, 15 insertions(+), 70 deletions(-)
xen/arch/arm/arm32/head.S +0 -0
index df51550baa8a..93b0af114b0c 100644

...

xen/arch/arm/arm32/head.S remove_identity_mapping: · line 681

Quoted 8 lines
xen/arch/arm/arm32/head.S +0 -4
@@ -675,33 +641,12 @@ 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.
-         */

Could you please add an empty line here to improve readability?

xen/arch/arm/arm32/head.S remove_identity_mapping: · line 644

Quoted 1 line
xen/arch/arm/arm32/head.S +1 -0
+        /* Find the first slot used and remove it */

~Michal

#4 lore raw

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

Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 52 lines · Julien Grall
Quoted 49 lines

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 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 | 85 +++++++--------------------------------
  1 file changed, 15 insertions(+), 70 deletions(-)
xen/arch/arm/arm32/head.S +0 -0
index df51550baa8a..93b0af114b0c 100644

...

Quoted 9 lines

xen/arch/arm/arm32/head.S remove_identity_mapping: · line 681

Quoted 8 lines
xen/arch/arm/arm32/head.S +0 -4
@@ -675,33 +641,12 @@ 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.
-         */

Could you please add an empty line here to improve readability?

Sure. I will do that on commit.

Quoted 4 lines

xen/arch/arm/arm32/head.S remove_identity_mapping: · line 644

Quoted 2 lines
xen/arch/arm/arm32/head.S +1 -0
+        /* Find the first slot used and remove it */

Thanks!

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

PATCH v5 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 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..0b0edf28d57a 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) < 2);
 #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.38.1
#6 lore raw

Re: [PATCH v5 2/5] xen/arm64: Rework the memory layout

Hi Julien,

Quoted 148 lines · 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 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 +0 -0
index f758cad545fa..0b0edf28d57a 100644

xen/arch/arm/mm.c static void __init __maybe_unused build_assertions(void) · line 156

Quoted 5 lines
xen/arch/arm/mm.c +1 -1
@@ -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) < 2);

I haven't spotted this before but this should be < 4.

~Michal

#7 lore raw

Re: [PATCH v5 2/5] xen/arm64: Rework the memory layout

Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 144 lines · Julien Grall
Quoted 143 lines

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 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)
 
Quoted 11 lines

xen/arch/arm/mm.c static void __init __maybe_unused build_assertions(void) · line 156

Quoted 10 lines
xen/arch/arm/mm.c +1 -1
index f758cad545fa..0b0edf28d57a 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) < 2);

I haven't spotted this before but this should be < 4.

Good catch! I am planning to handle it on commit unless there is more to fix in this patch.

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

Re: [PATCH v5 2/5] xen/arm64: Rework the memory layout

Hi Julien,

Quoted 163 lines · Julien Grall
Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 144 lines · Julien Grall
Quoted 143 lines

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 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)
 
Quoted 11 lines

xen/arch/arm/mm.c static void __init __maybe_unused build_assertions(void) · line 156

Quoted 10 lines
xen/arch/arm/mm.c +1 -1
index f758cad545fa..0b0edf28d57a 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) < 2);

I haven't spotted this before but this should be < 4.

Quoted 2 lines

Good catch! I am planning to handle it on commit unless there is more to fix in this patch.

No more remarks from my side.

~Michal

Reviewed-by on v5 2/5
#9 lore raw

Re: [PATCH v5 2/5] xen/arm64: Rework the memory layout

In reply to Julien Grall

Hi Julien,

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

Quoted 6 lines

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

With the finding for the comparison to 4 and not 2 found by Michal fixed:

Reviewed-by Bertrand Marquis <[email protected] <mailto:[email protected]>>

Cheers
Bertrand

Quoted 148 lines

---
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..0b0edf28d57a 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) < 2);
 #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.38.1
Reviewed-by
#10 lore raw

Re: [PATCH v5 2/5] xen/arm64: Rework the memory layout

Quoted 30 lines · Bertrand Marquis

Hi Julien,

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

Quoted 6 lines
Quoted 3 lines

With the finding for the comparison to 4 and not 2 found by Michal fixed:

Reviewed-by Bertrand Marquis <[email protected] <mailto:[email protected]>>

Sorry for that:

Cheers
Bertrand

Quoted 151 lines

Cheers
Bertrand

Quoted 147 lines

---
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..0b0edf28d57a 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) < 2);
 #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.38.1
#11 lore raw

PATCH v5 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 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..e7059a36bf17 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 pointer by TTBR_EL2).
+ *
+ * Note that nested a 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 0b0edf28d57a..e95843d88f37 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.38.1
Reviewed-by Tested-by on v5 3/5
#12 lore raw

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

Hi Julien,

Quoted 18 lines

-----Original Message-----
Subject: [PATCH v5 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

With some nits below that can definitely be fixed on commit.

Tested on FVP in Arm64 mode, so:

Quoted 7 lines
xen/arch/arm/arm64/mm.c new file +7 -0
+static void __init prepare_boot_identity_mapping(void)
+{
+    paddr_t id_addr = virt_to_maddr(_start);
+    lpae_t pte;
+    DECLARE_OFFSETS(id_offsets, id_addr);
+
+    /*

xen/arch/arm/arm64/mm.c line 44

Quoted 10 lines
xen/arch/arm/arm64/mm.c new file +10 -0
+     * We will be re-using the boot ID tables. They may not have been
+     * zeroed but they should be unlinked. So it is fine to use
+     * clear_page().
+     */
+    clear_page(boot_first_id);
+    clear_page(boot_second_id);
+    clear_page(boot_third_id);
+
+    if ( id_offsets[0] != 0 )
+        panic("Cannot handled ID mapping above 512GB\n");

Nit: s/handled/handle/

xen/arch/arm/arm64/mm.c line 77

Quoted 9 lines
xen/arch/arm/arm64/mm.c new file +9 -0
+
+static void __init prepare_runtime_identity_mapping(void)
+{
+    paddr_t id_addr = virt_to_maddr(_start);
+    lpae_t pte;
+    DECLARE_OFFSETS(id_offsets, id_addr);
+
+    if ( id_offsets[0] >= IDENTITY_MAPPING_AREA_NR_L0 )
+        panic("Cannot handled ID mapping above 512GB\n");

Same here.

xen/arch/arm/include/asm/arm64/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 19

Quoted 5 lines
xen/arch/arm/include/asm/arm64/mm.h +5 -0
+void arch_setup_page_tables(void);
+
+/*
+ * Enable/disable the identity mapping in the live page-tables (i.e.
+ * the one pointer by TTBR_EL2).

Nit: I might be wrong but I think s/pointer/pointed/.

Quoted 2 lines

+ *
+ * Note that nested a call (e.g. enable=true, enable=true) is not

Nit: s/nested/nesting/ or "a nested call"?

Quoted 3 lines

+ * supported.
+ */
+void update_identity_mapping(bool enable);

Kind regards,
Henry

#13 lore raw

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

Hi Henry,

Quoted 23 lines · Henry Wang
Quoted 23 lines

-----Original Message-----
Subject: [PATCH v5 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.

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

With some nits below that can definitely be fixed on commit.

I need to resend a new version of this patch. So I will fix them at the same time and...

Quoted 3 lines

Tested on FVP in Arm64 mode, so:

.. didn't keep these tags.

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

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

In reply to Julien Grall
Quoted 97 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

---
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 +34 -0
new file mode 100644
index 000000000000..f8e0887d25bc
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <xen/init.h>
+#include <xen/mm.h>
+
+#include <asm/setup.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))
+
+static DEFINE_PAGE_TABLE(xen_first_id);
+static DEFINE_PAGE_TABLE(xen_second_id);
+static DEFINE_PAGE_TABLE(xen_third_id);
+
+/*
+ * The identity mapping may start at physical address 0. So we don't want
+ * to keep it mapped longer than necessary.
+ *
+ * When this is called, we are still using the boot_pgtable.
+ *
+ * We need to prepare the identity mapping for both the boot page tables
+ * and runtime page tables.
+ *
+ * The logic to create the entry is slightly different because Xen may
+ * be running at a different location at runtime.
+ */
+static void __init prepare_boot_identity_mapping(void)
+{
+    paddr_t id_addr = virt_to_maddr(_start);
+    lpae_t pte;
+    DECLARE_OFFSETS(id_offsets, id_addr);
+
+    /*

xen/arch/arm/arm64/mm.c line 44

Quoted 10 lines
xen/arch/arm/arm64/mm.c new file +10 -0
+     * We will be re-using the boot ID tables. They may not have been
+     * zeroed but they should be unlinked. So it is fine to use
+     * clear_page().
+     */
+    clear_page(boot_first_id);
+    clear_page(boot_second_id);
+    clear_page(boot_third_id);
+
+    if ( id_offsets[0] != 0 )
+        panic("Cannot handled ID mapping above 512GB\n");

Hmmm... I forgot to update the two lines above and ...

Quoted 23 lines
xen/arch/arm/arm64/mm.c new file +23 -0
+
+    /* Link first ID table */
+    pte = mfn_to_xen_entry(virt_to_mfn(boot_first_id), MT_NORMAL);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+
+    write_pte(&boot_pgtable[id_offsets[0]], pte);
+
+    /* Link second ID table */
+    pte = mfn_to_xen_entry(virt_to_mfn(boot_second_id), MT_NORMAL);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+
+    write_pte(&boot_first_id[id_offsets[1]], pte);
+
+    /* Link third ID table */
+    pte = mfn_to_xen_entry(virt_to_mfn(boot_third_id), MT_NORMAL);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+
+    write_pte(&boot_second_id[id_offsets[2]], pte);
+
+    /* The mapping in the third table will be created at a later stage */

xen/arch/arm/arm64/mm.c line 77

Quoted 10 lines
xen/arch/arm/arm64/mm.c new file +10 -0
+}
+
+static void __init prepare_runtime_identity_mapping(void)
+{
+    paddr_t id_addr = virt_to_maddr(_start);
+    lpae_t pte;
+    DECLARE_OFFSETS(id_offsets, id_addr);
+
+    if ( id_offsets[0] >= IDENTITY_MAPPING_AREA_NR_L0 )
+        panic("Cannot handled ID mapping above 512GB\n");

... the error message here. I will send a new version of this patch.

Cheers,

Signature · 1 line
Julien Grall
#15 lore raw

PATCH v5 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 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 f8e0887d25bc..efcd5e70ecf6 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 e95843d88f37..0b2d31cc5d6c 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.38.1
Reviewed-by on v5 4/5
#16 lore raw

Re: [PATCH v5 4/5] xen/arm64: mm: Rework switch_ttbr()

Hi Julien,

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

Quoted 6 lines

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

~Michal

Reviewed-by Luca Fancellu Tested-by Luca Fancellu Reviewed-by on v5 4/5
#17 lore raw

Re: [PATCH v5 4/5] xen/arm64: mm: Rework switch_ttbr()

In reply to Julien Grall

Hi Julien,

On 27 Jan 2023, at 20:55, Julien Grall <[email protected]> wrote:

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.

Cheers
Bertrand

#18 lore raw

PATCH v5 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 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.38.1
Reviewed-by on v5 5/5
#19 lore raw

Re: [PATCH v5 5/5] xen/arm64: smpboot: Directly switch to the runtime page-tables

Hi Julien,

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

Quoted 6 lines

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

Reviewed-by Bertrand Marquis <[email protected] <mailto:[email protected]>>

Cheers
Bertrand

Quoted 159 lines

---
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 +3 -1
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:

xen/arch/arm/arm64/head.S +0 -16
-        /*
-         * 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

xen/arch/arm/arm64/head.S +4 -1
@@ -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:

xen/arch/arm/arm64/head.S +2 -2
+        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.38.1
Reviewed-by
#20 lore raw

Re: [PATCH v5 5/5] xen/arm64: smpboot: Directly switch to the runtime page-tables

Quoted 13 lines · Bertrand Marquis

Hi Julien,

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

Quoted 8 lines
Quoted 6 lines

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

Reviewed-by Bertrand Marquis <[email protected] <mailto:[email protected]>>

Sorry for that.

Cheers
Bertrand

Quoted 165 lines

Cheers
Bertrand

Quoted 159 lines

---
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 +3 -1
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:

xen/arch/arm/arm64/head.S +0 -16
-        /*
-         * 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

xen/arch/arm/arm64/head.S +4 -1
@@ -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:

xen/arch/arm/arm64/head.S +2 -2
+        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.38.1