Skip to contentSkip to conversation

Register Xen's load address as a boot module

PATCH In review Oleksii Kurochko 2 participants 5 messages Oct 9, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

v3 cover Register Xen's load address as a boot module

This patch series registers Xen's load address as a boot module and introduce virt_to_maddr(), and drops LINK_TO_LOAD() to use virt_to_maddr() instead.

---
Changes in V3:
- Add Acked-by: Jan Beulich <[email protected]> for patches:
- xen/riscv: switch LINK_TO_LOAD() to virt_to_maddr()
- xen/riscv: register Xen's load address as a boot module
- Minor changes for patch 1 (xen/riscv: implement virt_to_maddr())":
add an explanation comments, update the commit message and drop pointless ASSERT().
---
Changes in V2:
- Update the commit message ( drop depency from "device tree mapping" patch series as it was merged to staging )
- Other changes please look at the specific patch.
---

Oleksii Kurochko (3):
xen/riscv: implement virt_to_maddr() xen/riscv: switch LINK_TO_LOAD() to virt_to_maddr() xen/riscv: register Xen's load address as a boot module

 xen/arch/riscv/include/asm/config.h |  4 ++++
 xen/arch/riscv/include/asm/mm.h     | 22 +++++++++++++++++++++-
 xen/arch/riscv/mm.c                 |  9 ++++-----
 xen/arch/riscv/setup.c              |  6 ++++++
 4 files changed, 35 insertions(+), 6 deletions(-)
Signature · 1 line
2.46.2
#1 lore raw

PATCH v3 1/3 xen/riscv: implement virt_to_maddr()

Implement the virt_to_maddr() function to convert virtual addresses to machine addresses. The function includes checks for valid address ranges, specifically the direct mapping region (DIRECTMAP_VIRT_START) and the Xen's Linkage (XEN_VIRT_START) region. If the virtual address falls outside of these regions, an assertion will trigger. To implement this, the phys_offset variable is made accessible outside of riscv/mm.c.

---
Changes in v3:
- Drop ASSERT(va >= (DIRECTMAP_VIRT_START + DIRECTMAP_SIZE)) as it is enough to have 'if (...)' for directmap region and ASSERT() for checking that virtual address is from Xen's linkage virtual space.
- Add the comment above virt_to_maddr().
- Update the commit message.
- Add the comment for declaration and defoition of
phys_offset: /* = load_start - XEN_VIRT_START */.
---
Changes in V2:
- Drop casts in virt_to_maddr() for ASSERT which checks that VA is in the range of where Xen is located.
- Add UL suffix for or XEN_VIRT_START by using _AC(..., UL) and add inclusion of <xen/const.h>
- Add the comment above return which explains why there is no need to do " - XEN_VIRT_START.

---
 xen/arch/riscv/include/asm/config.h |  4 ++++
 xen/arch/riscv/include/asm/mm.h     | 22 +++++++++++++++++++++-
 xen/arch/riscv/mm.c                 |  2 +-
 3 files changed, 26 insertions(+), 2 deletions(-)
xen/arch/riscv/include/asm/config.h +4 -0
index ef68281653..ad75871283 100644
@@ -155,6 +155,10 @@
 
 #define IDENT_AREA_SIZE 64
 
+#ifndef __ASSEMBLY__
+extern unsigned long phys_offset; /* = load_start - XEN_VIRT_START */
+#endif
+
 #endif /* ASM__RISCV__CONFIG_H */
 /*
  * Local variables:
xen/arch/riscv/include/asm/mm.h +21 -1
index 5c79f3def3..ebb142502e 100644
@@ -5,6 +5,7 @@
 
 #include <public/xen.h>
 #include <xen/bug.h>
+#include <xen/const.h>
 #include <xen/mm-frame.h>
 #include <xen/pdx.h>
 #include <xen/types.h>
@@ -28,7 +29,26 @@ static inline void *maddr_to_virt(paddr_t ma)
     return NULL;
 }
 
-#define virt_to_maddr(va) ({ BUG_ON("unimplemented"); 0; })
+/*
+ * virt_to_maddr() is expected to work with virtual addresses from either
+ * the directmap region or Xen's linkage (XEN_VIRT_START) region.
+ * Therefore, it is sufficient to check only these regions and assert if `va`
+ * is not within the directmap or Xen's linkage region.
+ */
+static inline unsigned long virt_to_maddr(unsigned long va)
+{
+    if ((va >= DIRECTMAP_VIRT_START) &&
+        (va < (DIRECTMAP_VIRT_START + DIRECTMAP_SIZE)))
+        return directmapoff_to_maddr(va - DIRECTMAP_VIRT_START);
+
+    BUILD_BUG_ON(XEN_VIRT_SIZE != MB(2));
+    ASSERT((va >> (PAGETABLE_ORDER + PAGE_SHIFT)) ==
+           (_AC(XEN_VIRT_START, UL) >> (PAGETABLE_ORDER + PAGE_SHIFT)));
+
+    /* phys_offset = load_start - XEN_VIRT_START */
+    return phys_offset + va;
+}
+#define virt_to_maddr(va) virt_to_maddr((unsigned long)(va))
 
 /* Convert between Xen-heap virtual addresses and machine frame numbers. */
 #define __virt_to_mfn(va)  mfn_x(maddr_to_mfn(virt_to_maddr(va)))
xen/arch/riscv/mm.c +1 -1
index 4a628aef83..daa02b2c60 100644
@@ -26,7 +26,7 @@ struct mmu_desc {
     pte_t *pgtbl_base;
 };
 
-static unsigned long __ro_after_init phys_offset;
+unsigned long __ro_after_init phys_offset; /* = load_start - XEN_VIRT_START */
 
 #define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset)
 #define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset)
Signature · 1 line
2.46.2
Acked-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] xen/riscv: implement virt_to_maddr()

Quoted 9 lines · Oleksii Kurochko

Implement the virt_to_maddr() function to convert virtual addresses to machine addresses. The function includes checks for valid address ranges, specifically the direct mapping region (DIRECTMAP_VIRT_START) and the Xen's Linkage (XEN_VIRT_START) region. If the virtual address falls outside of these regions, an assertion will trigger. To implement this, the phys_offset variable is made accessible outside of riscv/mm.c.

#3 lore raw

PATCH v3 2/3 xen/riscv: switch LINK_TO_LOAD() to virt_to_maddr()

In reply to Oleksii Kurochko

Use virt_to_maddr() instead of LINK_TO_LOAD as virt_to_maddr() covers all the cases where LINK_TO_LOAD() is used.

---
Changes in V3:
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in V2:
- Drop the cast of virt_to_maddr() argument in remove_identity_mapping() as this cast is done inside virtu_to_maddr() wrapper macros.
- Update the commit message ( rewording to be more clear )

---
 xen/arch/riscv/mm.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
xen/arch/riscv/mm.c +3 -4
index daa02b2c60..27026d803b 100644
@@ -29,7 +29,6 @@ struct mmu_desc {
 unsigned long __ro_after_init phys_offset; /* = load_start - XEN_VIRT_START */
 
 #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.
@@ -122,7 +121,7 @@ static void __init setup_initial_mapping(struct mmu_desc *mmu_desc,
                 unsigned long paddr = (page_addr - map_start) + pa_start;
                 unsigned int permissions = PTE_LEAF_DEFAULT;
                 unsigned long addr = is_identity_mapping
-                                     ? page_addr : LINK_TO_LOAD(page_addr);
+                                     ? page_addr : virt_to_maddr(page_addr);
                 pte_t pte_to_be_written;
 
                 index = pt_index(0, page_addr);
@@ -225,7 +224,7 @@ void __init setup_fixmap_mappings(void)
 
     BUG_ON(pte_is_valid(*pte));
 
-    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap), PTE_TABLE);
+    tmp = paddr_to_pte(virt_to_maddr(&xen_fixmap), PTE_TABLE);
     write_pte(pte, tmp);
 
     RISCV_FENCE(rw, rw);
@@ -312,7 +311,7 @@ void __init remove_identity_mapping(void)
     pte_t *pgtbl;
     unsigned int index, xen_index;
     unsigned long ident_start =
-        LINK_TO_LOAD(turn_on_mmu) & XEN_PT_LEVEL_MAP_MASK(0);
+        virt_to_maddr(turn_on_mmu) & XEN_PT_LEVEL_MAP_MASK(0);
 
     for ( pgtbl = stage1_pgtbl_root, i = CONFIG_PAGING_LEVELS; i; i-- )
     {
Signature · 1 line
2.46.2
#4 lore raw

PATCH v3 3/3 xen/riscv: register Xen's load address as a boot module

In reply to Oleksii Kurochko

Avoid using BOOTMOD_XEN region for other purposes or boot modules which could result in memory corruption or undefined behaviour.

---
Changes in V3:
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in V2:
- Drop local variable xen_bootmodule as it won't be used after initialization.
- Drop unnecessary cast for the 3rd argument of add_boot_module() call in start_xen().
- Change BUG_ON(!xen_bootmodule) to panic().

---
 xen/arch/riscv/setup.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/riscv/setup.c +6 -0
index 6d156c3a40..f531ca38ee 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
 #include <xen/bug.h>
+#include <xen/bootfdt.h>
 #include <xen/compile.h>
 #include <xen/device_tree.h>
 #include <xen/init.h>
@@ -44,6 +45,11 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
               "Please check your bootloader.\n",
               dtb_addr, BOOT_FDT_VIRT_SIZE);
 
+    /* Register Xen's load address as a boot module. */
+    if ( !add_boot_module(BOOTMOD_XEN, virt_to_maddr(_start),
+                          _end - _start, false) )
+        panic("Failed to add BOOTMOD_XEN\n");
+
     printk("All set up\n");
 
     machine_halt();
Signature · 1 line
2.46.2