Skip to contentSkip to conversation

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

PATCH In review Julien Grall 5 participants 21 messages Apr 16, 2023 – Jun 10, 2023

lore
Share & tools
lore
#0 lore raw

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

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   |  38 ++++---
 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                   |  31 ++++--
 xen/arch/arm/smpboot.c              |   1 +
 14 files changed, 320 insertions(+), 130 deletions(-)
 create mode 100644 xen/arch/arm/arm64/mm.c
Signature · 1 line
2.39.2
#1 lore raw

PATCH v7 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.2
Reviewed-by on v7 1/5
#2 lore raw

Re: [PATCH v7 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 168 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.2
#3 lore raw

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

In reply to Julien Grall

Hi all,

Quoted 9 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).

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

I had to revert this patch a couple of months ago because Xen was not booting anymore on the Arndale. I finally managed to figure out what was the issue. It was an interesting read through the Arm Arm.

In switch_to_runtime_mapping we have the following code:

flush_xen_tlb_local r0

/* Map boot_second into boot_pgtable */ mov_w r0, XEN_VIRT_START create_table_entry boot_pgtable, boot_second, r0, 1

/* Ensure any page table updates are visible before continuing */
dsb nsh

ready_to_switch:
mov pc, lr

Per Arm Arm (Armv7 ARM DDI 0406C.d, section A3-148):

"The DMB and DSB memory barriers affect reads and writes to the memory system generated by load/store instructions and data or unified cache maintenance operations being executed by the processor. Instruction fetches or accesses caused by a hardware translation table access are not explicit accesses."

In the example above, 'lr' points to a region covered by the mapping we created just above. As the 'dsb' doesn't affect instruction fetch, it means it could happen before the page-table entry was observed and therefore result to a translation fault.

There is another situation where this could happen (taken from Linux commit d0b7a302d58a "Revert "arm64: Remove unnecessary ISBs from set_{pte,pmd,pud}"):

            MOV     X0, <valid pte>
            STR     X0, [Xptep]     // Store new PTE to page table
            DSB     ISHST
            LDR     X1, [X2]        // Translates using the new PTE

The dsb needs to be followed by an isb otherwise, a translation fault could occur.

There are a few places in where where the isb is missing. I think we didn't notice it before because we don't often create a new PTE and then directly access it.

Note that this issue is not in this patch but in fbd9b5fb4c26 ("xen/arm32: head: Remove restriction where to load Xen"). We didn't notice it because the temporary mapping wasn't much used before.

I will prepare a patch series to add the missing isb.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

PATCH v7 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. All the regions currently in L0 slot 0 will not be part of slot 4 (2TB). This requires a slight tweak of the boot code because XEN_VIRT_START (2TB + 2MB) cannot be used as an immediate.

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

Lastly, take the opportunity to check a compile time if any of the regions may overlap with the reserved area for identity mapping.

----
Changes in v7:
- Remove all tags
- Add BUILD_BUG_ON()s
- Don't forget to update FRAMETABLE_VIRT_START and VMAP_VIRT_START

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 | 38 +++++++++++++++++++++----------
 xen/arch/arm/mm.c                 | 23 +++++++++++++++----
 3 files changed, 46 insertions(+), 18 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 +26 -12
index 5df0e4c4959b..2cfe5e480256 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,14 +179,12 @@
 
 #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 IDENTITY_MAPPING_AREA_NR_L0  4
 
-#define VMAP_VIRT_START  GB(1)
+#define VMAP_VIRT_START  (SLOT0(4) + GB(1))
 #define VMAP_VIRT_SIZE   GB(1)
 
-#define FRAMETABLE_VIRT_START  GB(32)
+#define FRAMETABLE_VIRT_START  (SLOT0(4) + GB(32))
 #define FRAMETABLE_SIZE        GB(32)
 #define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
 
xen/arch/arm/mm.c +18 -5
index b99806af996c..1d09d61dd922 100644
@@ -153,7 +153,19 @@ 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));
+    /*
+     * The first few slots of the L0 table is reserved for the identity
+     * mapping. Check that none of the other regions are overlapping
+     * with it.
+     */
+#define CHECK_OVERLAP_WITH_IDMAP(virt) \
+    BUILD_BUG_ON(zeroeth_table_offset(virt) < IDENTITY_MAPPING_AREA_NR_L0)
+
+    CHECK_OVERLAP_WITH_IDMAP(XEN_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(VMAP_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(FRAMETABLE_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(DIRECTMAP_VIRT_START);
+#undef CHECK_OVERLAP_WITH_IDMAP
 #endif
     BUILD_BUG_ON(first_table_offset(XEN_VIRT_START));
 #ifdef CONFIG_ARCH_MAP_DOMAIN_PAGE
@@ -496,10 +508,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.2
Tested-by on v7 2/5
#5 lore raw

RE: [PATCH v7 2/5] xen/arm64: Rework the memory layout

Hi Julien,

Quoted 22 lines

-----Original Message-----
Subject: [PATCH v7 2/5] xen/arm64: Rework the memory layout

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. All the regions currently in L0 slot 0 will not be part of slot 4 (2TB). This requires a slight tweak of the boot code because XEN_VIRT_START (2TB + 2MB) cannot be used as an immediate.

Quoted 7 lines

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

Lastly, take the opportunity to check a compile time if any of the regions may overlap with the reserved area for identity mapping.

This time I used our CI to test this series patch by patch on top of staging today (Apr 17), so that we can see if the qemu issue reported by Bertrand in v6 still persists.

I can confirm all boards including the qemu-arm64 passed this time, so:

Kind regards,
Henry

Reviewed-by on v7 2/5
#6 lore raw

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

In reply to Julien Grall
Quoted 23 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. All the regions currently in L0 slot 0 will not be part of slot 4 (2TB). This requires a slight tweak of the boot code because XEN_VIRT_START (2TB + 2MB) cannot be used as an immediate.

Quoted 8 lines

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

Lastly, take the opportunity to check a compile time if any of the regions may overlap with the reserved area for identity mapping.

Hi Julien,

It looks fine to me.

Reviewed-by on v7 2/5
#7 lore raw

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

In reply to Julien Grall

Hi,

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. All the regions currently in L0 slot 0 will not be part of slot 4 (2TB). This requires a slight tweak of the boot code because XEN_VIRT_START (2TB + 2MB) cannot be used as an immediate.

Quoted 4 lines

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

Lastly, take the opportunity to check a compile time if any of the

s/a/at/ compile time

Quoted 75 lines

regions may overlap with the reserved area for identity mapping.

----
Changes in v7:
- Remove all tags
- Add BUILD_BUG_ON()s
- Don't forget to update FRAMETABLE_VIRT_START and VMAP_VIRT_START

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 | 38 +++++++++++++++++++++----------
 xen/arch/arm/mm.c                 | 23 +++++++++++++++----
 3 files changed, 46 insertions(+), 18 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 +1 -4
index 5df0e4c4959b..2cfe5e480256 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

xen/arch/arm/include/asm/config.h line 94

Quoted 10 lines
xen/arch/arm/include/asm/config.h +5 -2
@@ -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]

missing closing parenthesis at the end of line

This can be done on commit, so:

~Michal

#8 lore raw

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

Hi Michal,

Quoted 115 lines · Michal Orzel
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. All the regions currently in L0 slot 0 will not be part of slot 4 (2TB). This requires a slight tweak of the boot code because XEN_VIRT_START (2TB + 2MB) cannot be used as an immediate.

Quoted 4 lines

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

Lastly, take the opportunity to check a compile time if any of the

s/a/at/ compile time

Quoted 75 lines

regions may overlap with the reserved area for identity mapping.

----
Changes in v7:
- Remove all tags
- Add BUILD_BUG_ON()s
- Don't forget to update FRAMETABLE_VIRT_START and VMAP_VIRT_START

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 | 38 +++++++++++++++++++++----------
  xen/arch/arm/mm.c                 | 23 +++++++++++++++----
  3 files changed, 46 insertions(+), 18 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 +1 -4
index 5df0e4c4959b..2cfe5e480256 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

xen/arch/arm/include/asm/config.h line 94

Quoted 10 lines
xen/arch/arm/include/asm/config.h +5 -2
@@ -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]

missing closing parenthesis at the end of line

Quoted 1 line

This can be done on commit, so:

Thanks for spotting the typoes. I will update on commit.

Quoted 1 line

Cheers,

Signature · 1 line
Julien Grall
Reviewed-by on v7 2/5
#9 lore raw

Re: [PATCH v7 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. All the regions currently in L0 slot 0 will not be part of slot 4 (2TB). This requires a slight tweak of the boot code because XEN_VIRT_START (2TB + 2MB) cannot be used as an immediate.

Quoted 7 lines

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

Lastly, take the opportunity to check a compile time if any of the regions may overlap with the reserved area for identity mapping.

With the 2 typos found by Michal fixed:

Cheers
Bertrand

Quoted 178 lines

----
Changes in v7:
- Remove all tags
- Add BUILD_BUG_ON()s
- Don't forget to update FRAMETABLE_VIRT_START and VMAP_VIRT_START

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 | 38 +++++++++++++++++++++----------
xen/arch/arm/mm.c                 | 23 +++++++++++++++----
3 files changed, 46 insertions(+), 18 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 +26 -12
index 5df0e4c4959b..2cfe5e480256 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,14 +179,12 @@
 
 #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 IDENTITY_MAPPING_AREA_NR_L0  4
 
-#define VMAP_VIRT_START  GB(1)
+#define VMAP_VIRT_START  (SLOT0(4) + GB(1))
 #define VMAP_VIRT_SIZE   GB(1)
 
-#define FRAMETABLE_VIRT_START  GB(32)
+#define FRAMETABLE_VIRT_START  (SLOT0(4) + GB(32))
 #define FRAMETABLE_SIZE        GB(32)
 #define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
 
xen/arch/arm/mm.c +18 -5
index b99806af996c..1d09d61dd922 100644
@@ -153,7 +153,19 @@ 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));
+    /*
+     * The first few slots of the L0 table is reserved for the identity
+     * mapping. Check that none of the other regions are overlapping
+     * with it.
+     */
+#define CHECK_OVERLAP_WITH_IDMAP(virt) \
+    BUILD_BUG_ON(zeroeth_table_offset(virt) < IDENTITY_MAPPING_AREA_NR_L0)
+
+    CHECK_OVERLAP_WITH_IDMAP(XEN_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(VMAP_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(FRAMETABLE_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(DIRECTMAP_VIRT_START);
+#undef CHECK_OVERLAP_WITH_IDMAP
 #endif
    BUILD_BUG_ON(first_table_offset(XEN_VIRT_START));
 #ifdef CONFIG_ARCH_MAP_DOMAIN_PAGE
@@ -496,10 +508,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.2
#10 lore raw

Re: [PATCH v7 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. All the regions currently in L0 slot 0 will not be part of slot 4 (2TB). This requires a slight tweak of the boot code because XEN_VIRT_START (2TB + 2MB) cannot be used as an immediate.

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

Quoted 7 lines
Quoted 4 lines

Lastly, take the opportunity to check a compile time if any of the regions may overlap with the reserved area for identity mapping.

With the 2 typos found by Michal fixed:

You can fix them on commit definitely :-)

Cheers
Bertrand

Quoted 181 lines

Cheers
Bertrand

Quoted 177 lines

----
Changes in v7:
- Remove all tags
- Add BUILD_BUG_ON()s
- Don't forget to update FRAMETABLE_VIRT_START and VMAP_VIRT_START

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 | 38 +++++++++++++++++++++----------
xen/arch/arm/mm.c                 | 23 +++++++++++++++----
3 files changed, 46 insertions(+), 18 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 +26 -12
index 5df0e4c4959b..2cfe5e480256 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,14 +179,12 @@
 
 #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 IDENTITY_MAPPING_AREA_NR_L0  4
 
-#define VMAP_VIRT_START  GB(1)
+#define VMAP_VIRT_START  (SLOT0(4) + GB(1))
 #define VMAP_VIRT_SIZE   GB(1)
 
-#define FRAMETABLE_VIRT_START  GB(32)
+#define FRAMETABLE_VIRT_START  (SLOT0(4) + GB(32))
 #define FRAMETABLE_SIZE        GB(32)
 #define FRAMETABLE_NR          (FRAMETABLE_SIZE / sizeof(*frame_table))
 
xen/arch/arm/mm.c +18 -5
index b99806af996c..1d09d61dd922 100644
@@ -153,7 +153,19 @@ 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));
+    /*
+     * The first few slots of the L0 table is reserved for the identity
+     * mapping. Check that none of the other regions are overlapping
+     * with it.
+     */
+#define CHECK_OVERLAP_WITH_IDMAP(virt) \
+    BUILD_BUG_ON(zeroeth_table_offset(virt) < IDENTITY_MAPPING_AREA_NR_L0)
+
+    CHECK_OVERLAP_WITH_IDMAP(XEN_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(VMAP_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(FRAMETABLE_VIRT_START);
+    CHECK_OVERLAP_WITH_IDMAP(DIRECTMAP_VIRT_START);
+#undef CHECK_OVERLAP_WITH_IDMAP
 #endif
   BUILD_BUG_ON(first_table_offset(XEN_VIRT_START));
 #ifdef CONFIG_ARCH_MAP_DOMAIN_PAGE
@@ -496,10 +508,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.2
#11 lore raw

PATCH v7 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 v7:
- The definition of IDENTITY_MAPPING_AREA_NR_L0 was moved in the previous patch.

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/setup.h    |  11 +++
 xen/arch/arm/mm.c                   |   6 +-
 6 files changed, 163 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/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 1d09d61dd922..b7104d8d33ba 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
@@ -400,7 +400,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;
 
@@ -507,6 +507,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.2
Tested-by on v7 3/5
#12 lore raw

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

Hi Julien,

Quoted 18 lines

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

I used the test method described in my notes from patch#2, and this patch passed the test, so:

Kind regards,
Henry

Reviewed-by on v7 3/5
#13 lore raw

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

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

Hi Julien,

Reviewed-by on v7 3/5
#14 lore raw

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

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

~Michal

Reviewed-by on v7 3/5
#15 lore raw

Re: [PATCH v7 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

Cheers
Bertrand

Quoted 283 lines

----
Changes in v7:
- The definition of IDENTITY_MAPPING_AREA_NR_L0 was moved in the previous patch.

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/setup.h    |  11 +++
xen/arch/arm/mm.c                   |   6 +-
6 files changed, 163 insertions(+), 2 deletions(-)

create mode 100644 xen/arch/arm/arm64/mm.c

xen/arch/arm/arm64/Makefile +0 -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
new file mode 100644
index 000000000000..56b9e9b8d3ef
@@ -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);
+
+    /*
+     * 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] >= IDENTITY_MAPPING_AREA_NR_L0 )
+        panic("Cannot handle ID mapping above 2TB\n");
+
+    /* 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 */
+}
+
+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 handle ID mapping above 2TB\n");
+
+    /* Link first ID table */
+    pte = pte_of_xenaddr((vaddr_t)xen_first_id);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+
+    write_pte(&xen_pgtable[id_offsets[0]], pte);
+
+    /* Link second ID table */
+    pte = pte_of_xenaddr((vaddr_t)xen_second_id);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+
+    write_pte(&xen_first_id[id_offsets[1]], pte);
+
+    /* Link third ID table */
+    pte = pte_of_xenaddr((vaddr_t)xen_third_id);
+    pte.pt.table = 1;
+    pte.pt.xn = 0;
+
+    write_pte(&xen_second_id[id_offsets[2]], pte);
+
+    /* The mapping in the third table will be created at a later stage */
+}
+
+void __init arch_setup_page_tables(void)
+{
+    prepare_boot_identity_mapping();
+    prepare_runtime_identity_mapping();
+}
+
+void update_identity_mapping(bool enable)
+{
+    paddr_t id_addr = virt_to_maddr(_start);
+    int rc;
+
+    if ( enable )
+        rc = map_pages_to_xen(id_addr, maddr_to_mfn(id_addr), 1,
+                              PAGE_HYPERVISOR_RX);
+    else
+        rc = destroy_xen_mappings(id_addr, id_addr + PAGE_SIZE);
+
+    BUG_ON(rc);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
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/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 1d09d61dd922..b7104d8d33ba 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
@@ -400,7 +400,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;
 
@@ -507,6 +507,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.2
#16 lore raw

PATCH v7 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 v7:
- Removed the tested-by tag because of the layout reshuffle in the previous patches.

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 b7104d8d33ba..74f6ff2c6f78 100644
@@ -488,8 +488,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.2
Tested-by on v7 4/5
#17 lore raw

RE: [PATCH v7 4/5] xen/arm64: mm: Rework switch_ttbr()

Hi Julien,

Quoted 25 lines

-----Original Message-----
Subject: [PATCH v7 4/5] xen/arm64: mm: Rework switch_ttbr()

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

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

I used the test method described in my notes from patch#2, and this patch passed the test, so:

Kind regards,
Henry

#18 lore raw

PATCH v7 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 v7:
- Remove the tested-by tag because of the layout reshuffle in the previous patches.

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.2
Tested-by on v7 5/5
#19 lore raw

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

Hi Julien,

Quoted 10 lines

-----Original Message-----
Subject: [PATCH v7 5/5] xen/arm64: smpboot: Directly switch to the runtime page-tables

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.

I used the test method described in my notes from patch#2, and this patch passed the test, so:

Kind regards,
Henry

#20 lore raw
In reply to Julien Grall

Hi,

Quoted 45 lines · Julien Grall

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

Cheers,

Quoted 7 lines

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

This series is now fully committed.

Thanks for the help to review it!

Cheers,

Signature · 1 line
Julien Grall