Skip to contentSkip to conversation

xen/riscv: introduce identity mapping

PATCH Superseded Oleksii Kurochko 3 participants 18 messages Jul 17, 2023 – Jul 20, 2023 RISC-V

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover xen/riscv: introduce identity mapping

The patch series introduces things necessary to implement identity mapping:
1. Make identity mapping for the entire Xen.
2. Enable MMU.
3. Jump to the virtual address world
4. Remove identity mapping.

Also current patch series introduces the calculation of physical offset before MMU is enabled as access to physical offset will be calculated wrong after MMU will be enabled because access to phys_off variable is PC-relative and in the case when linker address != load address, it will cause MMU fault.

The reason for this patch series can be found here:
https://lore.kernel.org/xen-devel/[email protected]/

---
Changes in V3:
- Update the patch series message.
- The following patches were merged to staging so droped from the patch series:
* xen/riscv: add .sbss section to .bss
* xen/riscv: introduce reset_stack() function
* xen/riscv: move extern of cpu0_boot_stack to header
* xen/riscv: add SPDX tags
- move save/restore of a0/a1 registers from patch 4 to patch 2 ( numbers are from the previous patch series version )
- add SPDX tag in config.h
- update definition of PGTBL_INITIAL_COUNT taking into account identity mapping.
- refactor remove_identity_mapping() function.
- add explanatory comments in xen.lds.S and mm.c.
---
Changes in V2:
- update the patch series message.
- drop patches from the previous version of the patch series:
* xen/riscv: add __ASSEMBLY__ guards". ( merged )
* xen/riscv: make sure that identity mapping isn't bigger then page size ( entire Xen is 1:1 mapped so there is no need for the checks from the patch )
- add .sbss.* and put it befor .bss* .
- move out reset_stack() to .text section.
- add '__ro_after_init' for phys_offset variable.
- add '__init' for calc_phys_offset().
- declaring variable phys_off as non static as it will be used in head.S.
- update definition of PGTBL_INITIAL_COUNT and the comment above.
- code style fixes.
- remove id_addrs array becase entire Xen is mapped.
- reverse condition for cycle inside remove_identity_mapping().
- fix page table walk in remove_identity_mapping().
- save hart_id and dtb_addr before call MMU related C functions
- use phys_offset variable instead of doing calcultations to get phys offset in head.S file. ( it can be easily done as entire Xen is 1:1 mapped now )
- declare enable_muu() as __init.
- Update SPDX tags.
- Add Review-By/Suggested-By for some patches.
- code style fixes.

Oleksii Kurochko (3):
xen/riscv: add SPDX tag to config.h
xen/riscv: introduce function for physical offset calculation xen/riscv: introduce identity mapping

 xen/arch/riscv/include/asm/config.h |   2 +
 xen/arch/riscv/include/asm/mm.h     |   5 +-
 xen/arch/riscv/mm.c                 | 116 ++++++++++++++++++----------
 xen/arch/riscv/riscv64/head.S       |  36 +++++++++
 xen/arch/riscv/setup.c              |  14 +---
 xen/arch/riscv/xen.lds.S            |   4 +
 6 files changed, 125 insertions(+), 52 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v3 1/3 xen/riscv: add SPDX tag to config.h

---
Changes in V3:
- new patch

---
 xen/arch/riscv/include/asm/config.h | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/riscv/include/asm/config.h +2 -0
index 38862df0b8..fa90ae0898 100644
@@ -1,3 +1,5 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
 #ifndef __RISCV_CONFIG_H__
 #define __RISCV_CONFIG_H__
 
Signature · 1 line
2.41.0
Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] xen/riscv: add SPDX tag to config.h

Quoted 2 lines · Oleksii Kurochko

Alistair

Quoted 21 lines

---
Changes in V3:
- new patch

---
 xen/arch/riscv/include/asm/config.h | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/riscv/include/asm/config.h +2 -1
index 38862df0b8..fa90ae0898 100644
@@ -1,3 +1,5 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
 #ifndef __RISCV_CONFIG_H__
 #define __RISCV_CONFIG_H__
 
--

2.41.0

#3 lore raw

PATCH v3 2/3 xen/riscv: introduce function for physical offset calculation

In reply to Oleksii Kurochko

The function was introduced to calculate and save physical offset before MMU is enabled because access to start() is PC-relative and in case of linker_addr != load_addr it will result in incorrect value in phys_offset.

---
Changes in V3:
- save/restore of a0/a1 registers before C first function call.
---
Changes in V2:
- add __ro_after_init for phys_offset variable.
- remove double blank lines.
- add __init for calc_phys_offset().
- update the commit message.
- declaring variable phys_off as non static as it will be used in head.S.

---
 xen/arch/riscv/include/asm/mm.h |  2 ++
 xen/arch/riscv/mm.c             | 18 +++++++++++++++---
 xen/arch/riscv/riscv64/head.S   | 14 ++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
xen/arch/riscv/include/asm/mm.h +2 -0
index 5e3ac5cde3..d9c4205103 100644
@@ -15,4 +15,6 @@ void setup_initial_pagetables(void);
 void enable_mmu(void);
 void cont_after_mmu_is_enabled(void);
 
+void calc_phys_offset(void);
+
 #endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/mm.c +15 -3
index fddb3cd0bd..c84a8a7c3c 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/cache.h>
 #include <xen/compiler.h>
 #include <xen/init.h>
 #include <xen/kernel.h>
@@ -19,9 +20,10 @@ struct mmu_desc {
     pte_t *pgtbl_base;
 };
 
-#define PHYS_OFFSET ((unsigned long)_start - XEN_VIRT_START)
-#define LOAD_TO_LINK(addr) ((addr) - PHYS_OFFSET)
-#define LINK_TO_LOAD(addr) ((addr) + PHYS_OFFSET)
+unsigned long __ro_after_init phys_offset;
+
+#define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset)
+#define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset)
 
 /*
  * It is expected that Xen won't be more then 2 MB.
@@ -273,3 +275,13 @@ void __init noreturn noinline enable_mmu()
     switch_stack_and_jump((unsigned long)cpu0_boot_stack + STACK_SIZE,
                           cont_after_mmu_is_enabled);
 }
+
+/*
+ * calc_phys_offset() should be used before MMU is enabled because access to
+ * start() is PC-relative and in case when load_addr != linker_addr phys_offset
+ * will have an incorrect value
+ */
+void __init calc_phys_offset(void)
+{
+    phys_offset = (unsigned long)start - XEN_VIRT_START;
+}
xen/arch/riscv/riscv64/head.S +14 -0
index 2c0304646a..9015d06233 100644
@@ -29,6 +29,20 @@ ENTRY(start)
 
         jal     reset_stack
 
+        /*
+         * save hart_id and dtb_base as a0 and a1 register can be used
+         * by C code ( f.e. setup_initial_pagetables will update a0 and
+         * a1 )
+         */
+        mv      s0, a0
+        mv      s1, a1
+
+        jal     calc_phys_offset
+
+        /* restore bootcpu_id and dtb address */
+        mv      a0, s0
+        mv      a1, s1
+
         tail    start_xen
 
         .section .text, "ax", %progbits
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH v3 2/3] xen/riscv: introduce function for physical offset calculation

Quoted 77 lines · Oleksii Kurochko

The function was introduced to calculate and save physical offset before MMU is enabled because access to start() is PC-relative and in case of linker_addr != load_addr it will result in incorrect value in phys_offset.

---
Changes in V3:
- save/restore of a0/a1 registers before C first function call.
---
Changes in V2:
- add __ro_after_init for phys_offset variable.
- remove double blank lines.
- add __init for calc_phys_offset().
- update the commit message.
- declaring variable phys_off as non static as it will be used in head.S.

---
 xen/arch/riscv/include/asm/mm.h |  2 ++
 xen/arch/riscv/mm.c             | 18 +++++++++++++++---
 xen/arch/riscv/riscv64/head.S   | 14 ++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
xen/arch/riscv/include/asm/mm.h +2 -0
index 5e3ac5cde3..d9c4205103 100644
@@ -15,4 +15,6 @@ void setup_initial_pagetables(void);
 void enable_mmu(void);
 void cont_after_mmu_is_enabled(void);
 
+void calc_phys_offset(void);
+
 #endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/mm.c +15 -3
index fddb3cd0bd..c84a8a7c3c 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/cache.h>
 #include <xen/compiler.h>
 #include <xen/init.h>
 #include <xen/kernel.h>
@@ -19,9 +20,10 @@ struct mmu_desc {
     pte_t *pgtbl_base;
 };
 
-#define PHYS_OFFSET ((unsigned long)_start - XEN_VIRT_START)
-#define LOAD_TO_LINK(addr) ((addr) - PHYS_OFFSET)
-#define LINK_TO_LOAD(addr) ((addr) + PHYS_OFFSET)
+unsigned long __ro_after_init phys_offset;
+
+#define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset)
+#define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset)
 
 /*
  * It is expected that Xen won't be more then 2 MB.
@@ -273,3 +275,13 @@ void __init noreturn noinline enable_mmu()
     switch_stack_and_jump((unsigned long)cpu0_boot_stack + STACK_SIZE,
                           cont_after_mmu_is_enabled);
 }
+
+/*
+ * calc_phys_offset() should be used before MMU is enabled because access to
+ * start() is PC-relative and in case when load_addr != linker_addr phys_offset
+ * will have an incorrect value
+ */
+void __init calc_phys_offset(void)
+{
+    phys_offset = (unsigned long)start - XEN_VIRT_START;
+}
xen/arch/riscv/riscv64/head.S +0 -0
index 2c0304646a..9015d06233 100644

xen/arch/riscv/riscv64/head.S ENTRY(start) · line 35

Quoted 7 lines
xen/arch/riscv/riscv64/head.S +4 -0
@@ -29,6 +29,20 @@ ENTRY(start)
 
         jal     reset_stack
 
+        /*
+         * save hart_id and dtb_base as a0 and a1 register can be used
+         * by C code ( f.e. setup_initial_pagetables will update a0 and
+         * a1 )

I'd recommend dropping the part in parentheses - for one the function doesn't exist yet, and then you're merely restating what the ABI has.

xen/arch/riscv/riscv64/head.S ENTRY(start) · line 42

Quoted 7 lines
xen/arch/riscv/riscv64/head.S +7 -0
+         */
+        mv      s0, a0
+        mv      s1, a1
+
+        jal     calc_phys_offset
+
+        /* restore bootcpu_id and dtb address */

Is the first name here intentionally different from that in the other comment (where it's "hart_id")?

Jan

Quoted 6 lines
xen/arch/riscv/riscv64/head.S +3 -0
+        mv      a0, s0
+        mv      a1, s1
+
         tail    start_xen
 
         .section .text, "ax", %progbits
author
#5 lore raw

Re: [PATCH v3 2/3] xen/riscv: introduce function for physical offset calculation

Quoted 88 lines · Jan Beulich
Quoted 87 lines · Oleksii Kurochko

The function was introduced to calculate and save physical offset before MMU is enabled because access to start() is PC-relative and in case of linker_addr != load_addr it will result in incorrect value in phys_offset.

---
Changes in V3:
 - save/restore of a0/a1 registers before C first function call.
---
Changes in V2:
  - add __ro_after_init for phys_offset variable.
  - remove double blank lines.
  - add __init for calc_phys_offset().
  - update the commit message.
  - declaring variable phys_off as non static as it will be used in head.S.
---
 xen/arch/riscv/include/asm/mm.h |  2 ++ xen/arch/riscv/mm.c             | 18 +++++++++++++++--- xen/arch/riscv/riscv64/head.S   | 14 ++++++++++++++

 3 files changed, 31 insertions(+), 3 deletions(-)
xen/arch/riscv/include/asm/mm.h +0 -0
index 5e3ac5cde3..d9c4205103 100644
@@ -15,4 +15,6 @@ void setup_initial_pagetables(void);
  void enable_mmu(void);
  void cont_after_mmu_is_enabled(void);

xen/arch/riscv/include/asm/mm.h +2 -0
+void calc_phys_offset(void);
+

 #endif /* _ASM_RISCV_MM_H */

xen/arch/riscv/mm.c +0 -0
index fddb3cd0bd..c84a8a7c3c 100644
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: GPL-2.0-only */


+#include <xen/cache.h>
 #include <xen/compiler.h>
 #include <xen/init.h>
 #include <xen/kernel.h>

xen/arch/riscv/mm.c +0 -0
@@ -19,9 +20,10 @@ struct mmu_desc {
      pte_t *pgtbl_base;
  };

xen/arch/riscv/mm.c +4 -3
-#define PHYS_OFFSET ((unsigned long)_start - XEN_VIRT_START)
-#define LOAD_TO_LINK(addr) ((addr) - PHYS_OFFSET)
-#define LINK_TO_LOAD(addr) ((addr) + PHYS_OFFSET)
+unsigned long __ro_after_init phys_offset;
+
+#define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset)
+#define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset)


 /*
  * It is expected that Xen won't be more then 2 MB.

xen/arch/riscv/mm.c +0 -0
@@ -273,3 +275,13 @@ void __init noreturn noinline enable_mmu()

     switch_stack_and_jump((unsigned long)cpu0_boot_stack + STACK_SIZE,
                           cont_after_mmu_is_enabled); }

xen/arch/riscv/mm.c +3 -0
+
+/*
+ * calc_phys_offset() should be used before MMU is enabled because

access to
+ * start() is PC-relative and in case when load_addr != linker_addr phys_offset
+ * will have an incorrect value
+ */

xen/arch/riscv/mm.c +4 -0
+void __init calc_phys_offset(void)
+{
+    phys_offset = (unsigned long)start - XEN_VIRT_START;
+}
xen/arch/riscv/riscv64/head.S +0 -0
index 2c0304646a..9015d06233 100644
@@ -29,6 +29,20 @@ ENTRY(start)


         jal     reset_stack

+        /*

Quoted 8 lines
Quoted 5 lines

+         * save hart_id and dtb_base as a0 and a1 register can be used
+         * by C code ( f.e. setup_initial_pagetables will update a0 and
+         * a1 )

I'd recommend dropping the part in parentheses - for one the function doesn't exist yet, and then you're merely restating what the ABI has.

Thanks. I'll do that.

Quoted 11 lines

xen/arch/riscv/riscv64/head.S ENTRY(start) · line 42

Quoted 8 lines

+         */
+        mv      s0, a0
+        mv      s1, a1

xen/arch/riscv/riscv64/head.S +4 -0
+
+        jal     calc_phys_offset
+
+        /* restore bootcpu_id and dtb address */

Is the first name here intentionally different from that in the other comment (where it's "hart_id")?

Only one reason for that is that bootcpu_id is used an argument of start_xen() function. But generally I missed it to sync with the name above.

Probably it would be better to use 'bootcpu' everywhere instead of 'hart_id' as it is 'architecture independent way' to name CPU.

So I can update the comment above to: 'hart_id ( bootcpu_id )' ( as it was done in the comment above ENTRY(start) ) or just bootcpu_id.

~ Oleksii

#6 lore raw

PATCH v3 3/3 xen/riscv: introduce identity mapping

In reply to Oleksii Kurochko

The way how switch to virtual address was implemented in the commit e66003e7be ("xen/riscv: introduce setup_initial_pages") isn't safe enough as:
* enable_mmu() depends on hooking all exceptions
and pagefault.
* Any exception other than pagefault, or not taking a pagefault causes it to malfunction, which means you will fail to boot depending on where Xen was loaded into memory.

Instead of the proposed way of switching to virtual addresses was decided to use identity mapping of the entrire Xen and after switching to virtual addresses identity mapping is removed from page-tables.
Since it is not easy to keep track where the identity map was mapped, so we will look for the top-most entry exclusive to the identity map and remove it.

Fixes e66003e7be ("xen/riscv: introduce setup_initial_pages")

---
Changes in V3:
- remove unrelated to the patch changes ( SPDX tags in config.h ).
- update definition of PGTBL_INITIAL_COUNT taking into account identity mapping.
- refactor remove_identity_mapping() function.
- add explanatory comments in xen.lds.S and mm.c.
- update commit message.
- move save/restore of a0/a1 registers to [PATCH v2 2/3] xen/riscv: introduce function for physical offset calculation.
---
Changes in V2:
- update definition of PGTBL_INITIAL_COUNT and the comment above.
- code style fixes.
- 1:1 mapping for entire Xen.
- remove id_addrs array becase entire Xen is mapped.
- reverse condition for cycle inside remove_identity_mapping().
- fix page table walk in remove_identity_mapping().
- update the commit message.
- add Suggested-by: Andrew Cooper <[email protected]>
- save hart_id and dtb_addr before call MMU related C functions.
- use phys_offset variable instead of doing calcultations to get phys offset in head.S file. ( it can be easily done as entire Xen is 1:1 mapped )
- declare enable_muu() as __init.

---
 xen/arch/riscv/include/asm/mm.h |  3 +-
 xen/arch/riscv/mm.c             | 98 +++++++++++++++++++++------------
 xen/arch/riscv/riscv64/head.S   | 22 ++++++++
 xen/arch/riscv/setup.c          | 14 +----
 xen/arch/riscv/xen.lds.S        |  4 ++
 5 files changed, 92 insertions(+), 49 deletions(-)
xen/arch/riscv/include/asm/mm.h +2 -1
index d9c4205103..085eaab7fb 100644
@@ -13,7 +13,8 @@ extern unsigned char cpu0_boot_stack[];
 void setup_initial_pagetables(void);
 
 void enable_mmu(void);
-void cont_after_mmu_is_enabled(void);
+
+void remove_identity_mapping(void);
 
 void calc_phys_offset(void);
 
xen/arch/riscv/mm.c +62 -36

151 lines not on this page · raw message

xen/arch/riscv/riscv64/head.S +22 -0
index 9015d06233..41983ffe63 100644
@@ -39,6 +39,28 @@ ENTRY(start)
 
         jal     calc_phys_offset
 
+        jal     setup_initial_pagetables
+
+        jal     enable_mmu
+
+        la      t1, phys_offset
+        REG_L   t1, (t1)
+
+        /* Calculate proper VA after jump from 1:1 mapping */
+        la      t0, .L_primary_switched
+        sub     t0, t0, t1
+
+        /* Jump from 1:1 mapping world */
+        jr      t0
+
+.L_primary_switched:
+        /*
+         * cpu0_boot_stack address is 1:1 mapping related so it should be
+         * recalculated after jump from 1:1 mapping world as 1:1 mapping
+         * will be removed soon in start_xen().
+         */
+        jal     reset_stack
+
         /* restore bootcpu_id and dtb address */
         mv      a0, s0
         mv      a1, s1
xen/arch/riscv/setup.c +2 -12
index dde8fb898b..6593f601c1 100644
@@ -13,20 +13,10 @@ unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
 void __init noreturn start_xen(unsigned long bootcpu_id,
                                paddr_t dtb_addr)
 {
-    early_printk("Hello from C env\n");
-
-    setup_initial_pagetables();
-
-    enable_mmu();
-
-    for ( ;; )
-        asm volatile ("wfi");
+    remove_identity_mapping();
 
-    unreachable();
-}
+    early_printk("Hello from C env\n");
 
-void __init noreturn cont_after_mmu_is_enabled(void)
-{
     early_printk("All set up\n");
 
     for ( ;; )
xen/arch/riscv/xen.lds.S +4 -0
index 9064852173..31ccebadcb 100644
@@ -173,4 +173,8 @@ ASSERT(IS_ALIGNED(__bss_end,        POINTER_ALIGN), "__bss_end is misaligned")
 ASSERT(!SIZEOF(.got),      ".got non-empty")
 ASSERT(!SIZEOF(.got.plt),  ".got.plt non-empty")
 
+/*
+ * Changing the size of Xen binary can require an update of
+ * PGTBL_INITIAL_COUNT.
+ */
 ASSERT(_end - _start <= MB(2), "Xen too large for early-boot assumptions")
Signature · 1 line
2.41.0
#7 lore raw
Quoted 9 lines · Oleksii Kurochko

The way how switch to virtual address was implemented in the commit e66003e7be ("xen/riscv: introduce setup_initial_pages") isn't safe enough as:
* enable_mmu() depends on hooking all exceptions
and pagefault.
* Any exception other than pagefault, or not taking a pagefault causes it to malfunction, which means you will fail to boot depending on where Xen was loaded into memory.

Quoted 7 lines

Instead of the proposed way of switching to virtual addresses was decided to use identity mapping of the entrire Xen and after switching to virtual addresses identity mapping is removed from page-tables.
Since it is not easy to keep track where the identity map was mapped, so we will look for the top-most entry exclusive to the identity map and remove it.

Doesn't this paragraph need adjustment now?

xen/arch/riscv/mm.c unsigned long __ro_after_init phys_offset; · line 32

Quoted 11 lines
xen/arch/riscv/mm.c +5 -0
@@ -25,6 +25,12 @@ unsigned long __ro_after_init phys_offset;
 #define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset)
 #define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset)
 
+/*
+ * Should be removed as soon as enough headers will be merged for inclusion of
+ * <xen/lib.h>.
+ */
+#define ARRAY_SIZE(arr)		(sizeof(arr) / sizeof((arr)[0]))

Like said to Shawn for PPC in [1], there's now a pretty easy way to get this macro available for use here without needing to include xen/lib.h.

[1] https://lists.xen.org/archives/html/xen-devel/2023-07/msg01081.html

xen/arch/riscv/mm.c unsigned long __ro_after_init phys_offset; · line 47

Quoted 9 lines
xen/arch/riscv/mm.c +3 -1
@@ -35,8 +41,10 @@ unsigned long __ro_after_init phys_offset;
  *
  * It might be needed one more page table in case when Xen load address
  * isn't 2 MB aligned.
+ *
+ * CONFIG_PAGING_LEVELS page tables are needed for identity mapping.
  */
-#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) + 1)
+#define PGTBL_INITIAL_COUNT (CONFIG_PAGING_LEVELS * 2 + 1)

Where did the "- 1" go?

Quoted 27 lines
xen/arch/riscv/mm.c +7 -15
@@ -255,25 +266,40 @@ void __init noreturn noinline enable_mmu()
     csr_write(CSR_SATP,
               PFN_DOWN((unsigned long)stage1_pgtbl_root) |
               RV_STAGE1_MODE << SATP_MODE_SHIFT);
+}
 
-    asm volatile ( ".p2align 2" );
- mmu_is_enabled:
-    /*
-     * Stack should be re-inited as:
-     * 1. Right now an address of the stack is relative to load time
-     *    addresses what will cause an issue in case of load start address
-     *    isn't equal to linker start address.
-     * 2. Addresses in stack are all load time relative which can be an
-     *    issue in case when load start address isn't equal to linker
-     *    start address.
-     *
-     * We can't return to the caller because the stack was reseted
-     * and it may have stash some variable on the stack.
-     * Jump to a brand new function as the stack was reseted
-     */
+void __init remove_identity_mapping(void)
+{
+    unsigned int i;
+    pte_t *pgtbl;
+    unsigned int index, xen_index;
+    unsigned long load_start = LINK_TO_LOAD(_start);

xen/arch/riscv/mm.c void __init noreturn noinline enable_mmu() · line 286

Quoted 10 lines
xen/arch/riscv/mm.c +10 -0
+
+    for ( pgtbl = stage1_pgtbl_root, i = CONFIG_PAGING_LEVELS; i; i-- )
+    {
+        index = pt_index(i - 1, load_start);
+        xen_index = pt_index(i - 1, XEN_VIRT_START);
+
+        if ( index != xen_index )
+        {
+            /* remove after it will be possible to include <xen/lib.h> */
+            #define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1))

ROUNDUP() is even part of the patch that I've submitted already.

xen/arch/riscv/mm.c void __init noreturn noinline enable_mmu() · line 296

Quoted 9 lines
xen/arch/riscv/mm.c +9 -0
+            unsigned long load_end = LINK_TO_LOAD(_end);
+            unsigned long pt_level_size = XEN_PT_LEVEL_SIZE(i - 1);
+            unsigned long xen_size = ROUNDUP(load_end - load_start, pt_level_size);
+            unsigned long page_entries_num = xen_size / pt_level_size;
+
+            while ( page_entries_num-- )
+                pgtbl[index++].pte = 0;
+
+            break;

Unless there's a "not crossing a 2Mb boundary" guarantee somewhere that I've missed, this "break" is still too early afaict.

Jan

author
#8 lore raw
Quoted 19 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko

The way how switch to virtual address was implemented in the commit e66003e7be ("xen/riscv: introduce setup_initial_pages") isn't safe enough as:
* enable_mmu() depends on hooking all exceptions
  and pagefault.
* Any exception other than pagefault, or not taking a pagefault causes it to malfunction, which means you will fail to boot depending on where Xen was loaded into memory.

Quoted 8 lines

Instead of the proposed way of switching to virtual addresses was decided to use identity mapping of the entrire Xen and after switching to virtual addresses identity mapping is removed from page-tables.
Since it is not easy to keep track where the identity map was mapped,
so we will look for the top-most entry exclusive to the identity map and remove it.

Quoted 1 line

Doesn't this paragraph need adjustment now?

It should be. Thanks. Ill update it in the next patch version.

Quoted 19 lines
Quoted 6 lines
xen/arch/riscv/mm.c +0 -0
@@ -25,6 +25,12 @@ unsigned long __ro_after_init phys_offset;
  #define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset)
  #define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset)
Quoted 8 lines


+/*
+ * Should be removed as soon as enough headers will be merged for inclusion of
+ * <xen/lib.h>.
+ */
+#define ARRAY_SIZE(arr)                (sizeof(arr) / sizeof((arr)[0]))

Like said to Shawn for PPC in [1], there's now a pretty easy way to get this macro available for use here without needing to include xen/lib.h.

Quoted 2 lines

[1]
https://lists.xen.org/archives/html/xen-devel/2023-07/msg01081.html

Great. It'll be very useful for me so I'll add dependency on the patch where ARRAY_SIZE and ROUNDUP are moved to <xen/macros.h>.

Quoted 14 lines

xen/arch/riscv/mm.c unsigned long __ro_after_init phys_offset; · line 47

Quoted 12 lines
xen/arch/riscv/mm.c +0 -0
@@ -35,8 +41,10 @@ unsigned long __ro_after_init phys_offset;
   *
   * It might be needed one more page table in case when Xen load

address
  * isn't 2 MB aligned.
+ *
+ * CONFIG_PAGING_LEVELS page tables are needed for identity mapping.
  */

xen/arch/riscv/mm.c +1 -1
-#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) + 1)
+#define PGTBL_INITIAL_COUNT (CONFIG_PAGING_LEVELS * 2 + 1)

Where did the "- 1" go?

My fault. Should be:
#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS * 2 - 1) + 1)

Quoted 47 lines
Quoted 36 lines
xen/arch/riscv/mm.c +0 -0
@@ -255,25 +266,40 @@ void __init noreturn noinline enable_mmu()
      csr_write(CSR_SATP,

               PFN_DOWN((unsigned long)stage1_pgtbl_root) | RV_STAGE1_MODE << SATP_MODE_SHIFT);
+}

-    asm volatile ( ".p2align 2" );
- mmu_is_enabled:
-    /*
-     * Stack should be re-inited as:
-     * 1. Right now an address of the stack is relative to load time
-     *    addresses what will cause an issue in case of load start address
-     *    isn't equal to linker start address.
-     * 2. Addresses in stack are all load time relative which can be an
-     *    issue in case when load start address isn't equal to linker
-     *    start address.
-     *
-     * We can't return to the caller because the stack was reseted -     * and it may have stash some variable on the stack. -     * Jump to a brand new function as the stack was reseted -     */

xen/arch/riscv/mm.c +8 -0
+void __init remove_identity_mapping(void)
+{
+    unsigned int i;
+    pte_t *pgtbl;
+    unsigned int index, xen_index;
+    unsigned long load_start = LINK_TO_LOAD(_start);
+
+    for ( pgtbl = stage1_pgtbl_root, i = CONFIG_PAGING_LEVELS; i;

i-- )
+    {

Quoted 8 lines

+        index = pt_index(i - 1, load_start);
+        xen_index = pt_index(i - 1, XEN_VIRT_START);
+
+        if ( index != xen_index )
+        {
+            /* remove after it will be possible to include <xen/lib.h> */
+            #define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1))

ROUNDUP() is even part of the patch that I've submitted already.

Quoted 15 lines

xen/arch/riscv/mm.c void __init noreturn noinline enable_mmu() · line 296

Quoted 12 lines

+            unsigned long load_end = LINK_TO_LOAD(_end); +            unsigned long pt_level_size = XEN_PT_LEVEL_SIZE(i - 1);
+            unsigned long xen_size = ROUNDUP(load_end - load_start, pt_level_size);
+            unsigned long page_entries_num = xen_size / pt_level_size;

xen/arch/riscv/mm.c +5 -0
+
+            while ( page_entries_num-- )
+                pgtbl[index++].pte = 0;
+
+            break;

Unless there's a "not crossing a 2Mb boundary" guarantee somewhere that I've missed, this "break" is still too early afaict.

You are right. I have to re-write this part again. Thanks.

~ Oleksii

#9 lore raw
Quoted 15 lines · Oleksii
Quoted 14 lines · Jan Beulich

xen/arch/riscv/mm.c unsigned long __ro_after_init phys_offset; · line 47

Quoted 11 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -35,8 +41,10 @@ unsigned long __ro_after_init phys_offset;
   *
   * It might be needed one more page table in case when Xen load

address
  * isn't 2 MB aligned.
+ *
+ * CONFIG_PAGING_LEVELS page tables are needed for identity mapping.
  */

xen/arch/riscv/mm.c +1 -1
-#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) + 1)
+#define PGTBL_INITIAL_COUNT (CONFIG_PAGING_LEVELS * 2 + 1)

Where did the "- 1" go?

Quoted 2 lines

My fault. Should be:
#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS * 2 - 1) + 1)

Why? I'd have expected

#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) * 2 + 1)

Jan

author
#10 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Oleksii
Quoted 15 lines · Jan Beulich

xen/arch/riscv/mm.c unsigned long __ro_after_init phys_offset; · line 47

Quoted 12 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -35,8 +41,10 @@ unsigned long __ro_after_init phys_offset;
   *
   * It might be needed one more page table in case when Xen

load
address
  * isn't 2 MB aligned.
+ *
+ * CONFIG_PAGING_LEVELS page tables are needed for identity mapping.
  */

xen/arch/riscv/mm.c +1 -1
-#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) + 1)
+#define PGTBL_INITIAL_COUNT (CONFIG_PAGING_LEVELS * 2 + 1)

Where did the "- 1" go?

Quoted 7 lines
Quoted 2 lines

My fault. Should be:
  #define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS * 2  - 1) + 1)

Why? I'd have expected

#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) * 2 + 1)

I confused myself with definition of PGTBL_INITIAL_COUNT from another branch so your option is correct.
Thanks.

~ Oleksii

author
#11 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 7 lines

+            unsigned long load_end = LINK_TO_LOAD(_end); +            unsigned long pt_level_size = XEN_PT_LEVEL_SIZE(i - 1);
+            unsigned long xen_size = ROUNDUP(load_end - load_start, pt_level_size);
+            unsigned long page_entries_num = xen_size / pt_level_size;

Quoted 8 lines

xen/arch/riscv/mm.c void __init noreturn noinline enable_mmu() · line 296

Quoted 5 lines
xen/arch/riscv/mm.c +5 -0
+
+            while ( page_entries_num-- )
+                pgtbl[index++].pte = 0;
+
+            break;

Unless there's a "not crossing a 2Mb boundary" guarantee somewhere that I've missed, this "break" is still too early afaict.

If I will add a '2 MB boundary check' for load_start and linker_start could it be an upstreamable solution?

Something like:

    if ( !IS_ALIGNED(load_start, MB(2) )
	printk("load_start should be 2Mb algined\n");
and
    ASSERT( !IS_ALIGNED(XEN_VIRT_START, MB(2) )

in xen.lds.S.

Then we will have completely different L0 tables for identity mapping and not identity and the code above will be correct.

~ Oleksii

#12 lore raw
Quoted 19 lines · Oleksii
Quoted 7 lines · Jan Beulich
Quoted 7 lines

+            unsigned long load_end = LINK_TO_LOAD(_end); +            unsigned long pt_level_size = XEN_PT_LEVEL_SIZE(i - 1);
+            unsigned long xen_size = ROUNDUP(load_end - load_start, pt_level_size);
+            unsigned long page_entries_num = xen_size / pt_level_size;

Quoted 8 lines

xen/arch/riscv/mm.c void __init noreturn noinline enable_mmu() · line 296

Quoted 5 lines
xen/arch/riscv/mm.c +5 -0
+
+            while ( page_entries_num-- )
+                pgtbl[index++].pte = 0;
+
+            break;

Unless there's a "not crossing a 2Mb boundary" guarantee somewhere that I've missed, this "break" is still too early afaict.

If I will add a '2 MB boundary check' for load_start and linker_start could it be an upstreamable solution?

Quoted 6 lines

Something like:

    if ( !IS_ALIGNED(load_start, MB(2) )
	printk("load_start should be 2Mb algined\n");
and
    ASSERT( !IS_ALIGNED(XEN_VIRT_START, MB(2) )

in xen.lds.S.

Arranging for the linked address to be 2Mb-aligned is certainly reasonable. Whether expecting the load address to also be depends on whether that can be arranged for (which in turn depends on boot loader behavior); it cannot be left to "luck".

Quoted 2 lines

Then we will have completely different L0 tables for identity mapping and not identity and the code above will be correct.

As long as Xen won't grow beyond 2Mb total. Considering that at some point you may want to use large page mappings for .text, .data, and .rodata, that alone would grow Xen to 6 Mb (or really 8, assuming .init goes separate as well). That's leaving aside the realistic option of the mere sum of all sections being larger than
2. That said, even Arm64 with ACPI is still quite a bit below 2Mb. x86 is nearing 2.5 though in even a somewhat limited config; allyesconfig may well be beyond that already.

Of course you may legitimately leave dealing with that to the future.

Jan

author
#13 lore raw
Quoted 30 lines · Jan Beulich
Quoted 22 lines · Oleksii
Quoted 9 lines · Jan Beulich
Quoted 9 lines

+            unsigned long load_end = LINK_TO_LOAD(_end); +            unsigned long pt_level_size = XEN_PT_LEVEL_SIZE(i -
1);
+            unsigned long xen_size = ROUNDUP(load_end - load_start, pt_level_size);
+            unsigned long page_entries_num = xen_size / pt_level_size;
+

Quoted 8 lines
Quoted 4 lines

+            while ( page_entries_num-- )
+                pgtbl[index++].pte = 0;
+
+            break;

Unless there's a "not crossing a 2Mb boundary" guarantee somewhere
that I've missed, this "break" is still too early afaict.

If I will add a '2 MB boundary check' for load_start and linker_start
could it be an upstreamable solution?

Quoted 6 lines

Something like:
    if ( !IS_ALIGNED(load_start, MB(2) ) printk("load_start should be 2Mb algined\n"); and
    ASSERT( !IS_ALIGNED(XEN_VIRT_START, MB(2) ) in xen.lds.S.

Quoted 4 lines

Arranging for the linked address to be 2Mb-aligned is certainly reasonable. Whether expecting the load address to also be depends on whether that can be arranged for (which in turn depends on boot loader behavior); it cannot be left to "luck".

Maybe I didn't quite understand you here, but if Xen has an alignment check of load address then boot loader has to follow the alignment requirements of Xen. So it doesn't look as 'luck'.

Quoted 5 lines
Quoted 4 lines

Then we will have completely different L0 tables for identity mapping
and not identity and the code above will be correct.

Quoted 8 lines

As long as Xen won't grow beyond 2Mb total. Considering that at some point you may want to use large page mappings for .text, .data, and .rodata, that alone would grow Xen to 6 Mb (or really 8, assuming .init goes separate as well). That's leaving aside the realistic option of the mere sum of all sections being larger than
2. That said, even Arm64 with ACPI is still quite a bit below 2Mb. x86 is nearing 2.5 though in even a somewhat limited config; allyesconfig may well be beyond that already.

I am missing something about Xen size. Lets assume that Xen will be mapped using only 4k pagees ( like it is done now ). Then if Xen will be more then 2Mb then only what will be changed is a number of page tables so it is only question of changing of PGTBL_INITIAL_COUNT ( in case of RISC-V).

Could you please explain why Xen will grow to 6/8 MB in case of larger page mappings? In case of larger page mapping fewer tables are needed. For example, if we would like to use 2Mb pages then we will stop at L1 page table and write an physical address to L1 page table entry instead of creating new L0 page table.

Quoted 3 lines

Of course you may legitimately leave dealing with that to the future.

Then I'll send new patch series with updated alignment requirements.

~ Oleksii

#14 lore raw
Quoted 31 lines · Oleksii
Quoted 30 lines · Jan Beulich
Quoted 28 lines · Oleksii
Quoted 9 lines · Jan Beulich
Quoted 9 lines

+            unsigned long load_end = LINK_TO_LOAD(_end); +            unsigned long pt_level_size = XEN_PT_LEVEL_SIZE(i -
1);
+            unsigned long xen_size = ROUNDUP(load_end - load_start, pt_level_size);
+            unsigned long page_entries_num = xen_size / pt_level_size;
+

Quoted 8 lines
Quoted 4 lines

+            while ( page_entries_num-- )
+                pgtbl[index++].pte = 0;
+
+            break;

Unless there's a "not crossing a 2Mb boundary" guarantee somewhere
that I've missed, this "break" is still too early afaict.

If I will add a '2 MB boundary check' for load_start and linker_start
could it be an upstreamable solution?

Something like:
    if ( !IS_ALIGNED(load_start, MB(2) ) printk("load_start should be 2Mb algined\n"); and
    ASSERT( !IS_ALIGNED(XEN_VIRT_START, MB(2) ) in xen.lds.S.

Quoted 7 lines
Quoted 4 lines

Arranging for the linked address to be 2Mb-aligned is certainly reasonable. Whether expecting the load address to also be depends on whether that can be arranged for (which in turn depends on boot loader behavior); it cannot be left to "luck".

Maybe I didn't quite understand you here, but if Xen has an alignment check of load address then boot loader has to follow the alignment requirements of Xen. So it doesn't look as 'luck'.

That depends on (a) the alignment being properly expressed in the final binary and (b) the boot loader honoring it. (b) is what you double-check above, emitting a printk(), but I'm not sure about (a) being sufficiently enforced with just the ASSERT in the linker script. Maybe I'm wrong, though.

Quoted 12 lines
Quoted 4 lines
Quoted 3 lines

Then we will have completely different L0 tables for identity mapping
and not identity and the code above will be correct.

Quoted 8 lines

As long as Xen won't grow beyond 2Mb total. Considering that at some point you may want to use large page mappings for .text, .data, and .rodata, that alone would grow Xen to 6 Mb (or really 8, assuming .init goes separate as well). That's leaving aside the realistic option of the mere sum of all sections being larger than
2. That said, even Arm64 with ACPI is still quite a bit below 2Mb. x86 is nearing 2.5 though in even a somewhat limited config; allyesconfig may well be beyond that already.

Quoted 5 lines

I am missing something about Xen size. Lets assume that Xen will be mapped using only 4k pagees ( like it is done now ). Then if Xen will be more then 2Mb then only what will be changed is a number of page tables so it is only question of changing of PGTBL_INITIAL_COUNT ( in case of RISC-V).

And the way you do the tearing down of the transient 1:1 mapping.

Quoted 5 lines

Could you please explain why Xen will grow to 6/8 MB in case of larger page mappings? In case of larger page mapping fewer tables are needed. For example, if we would like to use 2Mb pages then we will stop at L1 page table and write an physical address to L1 page table entry instead of creating new L0 page table.

When you use 2Mb mappings, then you will want to use separate ones for .text, .rodata, and .data (plus perhaps .init), to express the differing permissions correctly. Consequently you'll need more virtual address space, but - yes - fewer page table pages. And of course the 1:1 unmapping logic will again be slightly different.

Jan

author
#15 lore raw
Quoted 43 lines · Jan Beulich
Quoted 37 lines · Oleksii
Quoted 31 lines · Jan Beulich
Quoted 23 lines · Oleksii
Quoted 10 lines · Jan Beulich
Quoted 10 lines

+            unsigned long load_end = LINK_TO_LOAD(_end); +            unsigned long pt_level_size = XEN_PT_LEVEL_SIZE(i
-
1);
+            unsigned long xen_size = ROUNDUP(load_end - load_start, pt_level_size);
+            unsigned long page_entries_num = xen_size / pt_level_size;
+

Quoted 8 lines
Quoted 4 lines

+            while ( page_entries_num-- )
+                pgtbl[index++].pte = 0;
+
+            break;

Unless there's a "not crossing a 2Mb boundary" guarantee somewhere
that I've missed, this "break" is still too early afaict.

If I will add a '2 MB boundary check' for load_start and linker_start
could it be an upstreamable solution?

Quoted 6 lines

Something like:
    if ( !IS_ALIGNED(load_start, MB(2) ) printk("load_start should be 2Mb algined\n"); and
    ASSERT( !IS_ALIGNED(XEN_VIRT_START, MB(2) ) in xen.lds.S.

Quoted 5 lines

Arranging for the linked address to be 2Mb-aligned is certainly reasonable. Whether expecting the load address to also be depends on whether that can be arranged for (which in turn depends on boot
loader behavior); it cannot be left to "luck".

Quoted 4 lines

Maybe I didn't quite understand you here, but if Xen has an alignment
check of load address then boot loader has to follow the alignment requirements of Xen. So it doesn't look as 'luck'.

Quoted 5 lines

That depends on (a) the alignment being properly expressed in the final binary and (b) the boot loader honoring it. (b) is what you double-check above, emitting a printk(), but I'm not sure about (a) being sufficiently enforced with just the ASSERT in the linker script. Maybe I'm wrong, though.

It should be enough for current purpose but probably I am missing something.

Quoted 24 lines
Quoted 16 lines
Quoted 5 lines
Quoted 4 lines

Then we will have completely different L0 tables for identity mapping
and not identity and the code above will be correct.

Quoted 11 lines

As long as Xen won't grow beyond 2Mb total. Considering that at some point you may want to use large page mappings for .text, .data, and .rodata, that alone would grow Xen to 6 Mb (or really 8,
assuming .init goes separate as well). That's leaving aside the realistic option of the mere sum of all sections being larger than
2. That said, even Arm64 with ACPI is still quite a bit below 2Mb.
x86 is nearing 2.5 though in even a somewhat limited config; allyesconfig may well be beyond that already.

Quoted 7 lines

I am missing something about Xen size. Lets assume that Xen will be mapped using only 4k pagees ( like it is done now ). Then if Xen will
be more then 2Mb then only what will be changed is a number of page tables so it is only question of changing of PGTBL_INITIAL_COUNT ( in
case of RISC-V).

Quoted 1 line

And the way you do the tearing down of the transient 1:1 mapping.

It looks like removing 1:1 mapping will be the same.

Let's assume that the size of Xen is 4 MB and that load and linker ranges don't overlap ( load and linker start address are 2Mb aligned ), and the difference between them isn't bigger than 1 GB. Then one L2 page table, one L1 page table and two L0 page tables for identity mapping, and two L0 page tables for non-identity mapping are needed. Then at L1, we will have different indexes for load_start and linker_start. So what will be needed is to clean two L1 page table entries started from some index.

The only issue I see now is that it won't work in case if identity mapping crosses a 1 Gb boundary. Then for identity mapping, it will be needed two L1 page tables, and only one of them identity mapping will be removed.

Do I miss anything else?
Wouldn't it be better to take into account that now?

Quoted 11 lines
Quoted 10 lines

Could you please explain why Xen will grow to 6/8 MB in case of larger
page mappings? In case of larger page mapping fewer tables are needed.
For example, if we would like to use 2Mb pages then we will stop at L1
page table and write an physical address to L1 page table entry instead
of creating new L0 page table.

Quoted 5 lines

When you use 2Mb mappings, then you will want to use separate ones for .text, .rodata, and .data (plus perhaps .init), to express the differing permissions correctly. Consequently you'll need more virtual address space, but - yes - fewer page table pages. And of course the 1:1 unmapping logic will again be slightly different.

Thanks for clarification.

~ Oleksii

#16 lore raw
Quoted 39 lines · Oleksii
Quoted 26 lines · Jan Beulich
Quoted 17 lines · Oleksii
Quoted 5 lines · Jan Beulich
Quoted 3 lines · Oleksii

Then we will have completely different L0 tables for identity mapping
and not identity and the code above will be correct.

Quoted 11 lines

As long as Xen won't grow beyond 2Mb total. Considering that at some point you may want to use large page mappings for .text, .data, and .rodata, that alone would grow Xen to 6 Mb (or really 8,
assuming .init goes separate as well). That's leaving aside the realistic option of the mere sum of all sections being larger than
2. That said, even Arm64 with ACPI is still quite a bit below 2Mb.
x86 is nearing 2.5 though in even a somewhat limited config; allyesconfig may well be beyond that already.

Quoted 7 lines

I am missing something about Xen size. Lets assume that Xen will be mapped using only 4k pagees ( like it is done now ). Then if Xen will
be more then 2Mb then only what will be changed is a number of page tables so it is only question of changing of PGTBL_INITIAL_COUNT ( in
case of RISC-V).

Quoted 1 line

And the way you do the tearing down of the transient 1:1 mapping.

It looks like removing 1:1 mapping will be the same.

Let's assume that the size of Xen is 4 MB and that load and linker ranges don't overlap ( load and linker start address are 2Mb aligned ), and the difference between them isn't bigger than 1 GB. Then one L2 page table, one L1 page table and two L0 page tables for identity mapping, and two L0 page tables for non-identity mapping are needed. Then at L1, we will have different indexes for load_start and linker_start. So what will be needed is to clean two L1 page table entries started from some index.

Quoted 6 lines

The only issue I see now is that it won't work in case if identity mapping crosses a 1 Gb boundary. Then for identity mapping, it will be needed two L1 page tables, and only one of them identity mapping will be removed.

Do I miss anything else?

Looks correct to me.

Quoted 1 line

Wouldn't it be better to take into account that now?

Sure, it's generally better to avoid leaving traps for someone to fall into later.

Jan

author
#17 lore raw
Quoted 57 lines · Jan Beulich
Quoted 56 lines · Oleksii
Quoted 34 lines · Jan Beulich
Quoted 21 lines · Oleksii
Quoted 6 lines · Jan Beulich
Quoted 4 lines · Oleksii

Then we will have completely different L0 tables for identity
mapping
and not identity and the code above will be correct.

Quoted 14 lines

As long as Xen won't grow beyond 2Mb total. Considering that at
some point you may want to use large page mappings for .text, .data, and .rodata, that alone would grow Xen to 6 Mb (or really
8,
assuming .init goes separate as well). That's leaving aside the
realistic option of the mere sum of all sections being larger than
2. That said, even Arm64 with ACPI is still quite a bit below 2Mb.
x86 is nearing 2.5 though in even a somewhat limited config; allyesconfig may well be beyond that already.

Quoted 11 lines

I am missing something about Xen size. Lets assume that Xen will be
mapped using only 4k pagees ( like it is done now ). Then if Xen
will
be more then 2Mb then only what will be changed is a number of page
tables so it is only question of changing of PGTBL_INITIAL_COUNT (
in
case of RISC-V).

Quoted 1 line

And the way you do the tearing down of the transient 1:1 mapping.

It looks like removing 1:1 mapping will be the same.

Let's assume that the size of Xen is 4 MB and that load and linker ranges don't overlap ( load and linker start address are 2Mb aligned ),
and the difference between them isn't bigger than 1 GB. Then one L2 page table, one L1 page table and two L0 page tables for identity mapping, and two L0 page tables for non-identity mapping are needed.
Then at L1, we will have different indexes for load_start and linker_start. So what will be needed is to clean two L1 page table entries started from some index.

The only issue I see now is that it won't work in case if identity mapping crosses a 1 Gb boundary. Then for identity mapping, it will be
needed two L1 page tables, and only one of them identity mapping will
be removed.

Quoted 8 lines
Quoted 1 line

Do I miss anything else?

Looks correct to me.

Quoted 1 line

Wouldn't it be better to take into account that now?

Sure, it's generally better to avoid leaving traps for someone to fall into later.

Thanks a lot. Then it make sense to update the removing identity mapping algo.

~ Oleksii