Skip to contentSkip to conversation

Unflattening and relocation of host device tree

PATCH In review Oleksii Kurochko 2 participants 12 messages Dec 17, 2024 – Dec 19, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

v3 cover Unflattening and relocation of host device tree

The current patch series introduces the relocation of the host device tree file to free up low memory and also it is expected that discard_initial_modules() will be called sooner or later, it will discard the FDT boot module, and remove_early_mappings() will destroy the early mappings.

In addition to relocation, unflattening is introduced to create the tree of struct device_node for the host device tree.

To implement this, several things have been introduced:
- destroy_xen_mappings() function, which removes page mappings from Xen's page tables. This is necessary for clear_fixmap().
- {set,clear}_fixmap() functions to manage mappings in the fixmap region, which are expected to be used in copy_from_paddr() to copy the FDT to Xen's heap.
- Introduce new config CONFIG_QEMU which is going to be used to cover changes connected to QEMU virtual board. It will be used during introduction of stubs for clean_and_invalidate_dcache_va_range() and clean_dcache_va_range(), which are expected to be used in copy_from_paddr() and flush_page_to_ram(), which in turn are expected to be used during the call to xvmalloc_array() in relocate_fdt().
In case of QEMU cached related functions are implemented as returning 0 as QEMU doesn't model cache ( and so CMO extensions ). For others cases, it is introduced as -ENOSUPP as h/w could support CMO extension ( or hardware specific insertions ) and it will need to update implementation of the mentioned functions.
- The introduction of copy_from_paddr() to copy the FDT to an address allocated in Xen's heap.

---
Changes in v3:
- Add some Acks for the patches. All the patches are Acked except:
[PATCH v3 1/7] xen/riscv: update layout table in config.h [PATCH v3 5/7] xen/riscv: implement data and instruction cache operations
- Other changes please look at the specific patch.
---
Changes in v2:
- Update the cover letter.
- Introduce new patch with aligning of upper bounds in the layout table in config.h with the definitions below which are inclusive.
- Other changes please look at the specific patch.
---

Oleksii Kurochko (7):
xen/riscv: update layout table in config.h
xen/riscv: add destroy_xen_mappings() to remove mappings in Xen page
tables
xen/riscv: reorder includes in asm/page.h alphabetically xen/riscv: add {set,clear}_fixmap() functions for managing fixmap
entries
xen/riscv: implement data and instruction cache operations xen/riscv: implement prereq for DTB relocation
xen/riscv: relocating and unflattening host device tree

 xen/arch/riscv/Kconfig                  |  2 +
 xen/arch/riscv/configs/tiny64_defconfig |  1 +
 xen/arch/riscv/include/asm/config.h     | 12 +++---
 xen/arch/riscv/include/asm/fixmap.h     |  5 +++
 xen/arch/riscv/include/asm/mm.h         |  8 +++-
 xen/arch/riscv/include/asm/page.h       | 36 ++++++++++++++--
 xen/arch/riscv/include/asm/setup.h      |  4 ++
 xen/arch/riscv/mm.c                     |  6 ---
 xen/arch/riscv/platforms/Kconfig        |  5 +++
 xen/arch/riscv/pt.c                     | 24 +++++++++++
 xen/arch/riscv/setup.c                  | 57 ++++++++++++++++++++++++-
 11 files changed, 140 insertions(+), 20 deletions(-)
 create mode 100644 xen/arch/riscv/platforms/Kconfig
Signature · 1 line
2.47.1
#1 lore raw

PATCH v3 1/7 xen/riscv: update layout table in config.h

Make all upper bounds (end addresses) for areas inclusive to align with the corresponding definitions.

For the Direct map region, the upper bound was calculated incorrectly in efadb18dd58aba ("xen/riscv: add VM space layout"). It should be 0x7f80000000 (considering that the value is exclusive, instead of 0x7f40000000). Therefore, the inclusive upper bound for that region is 0x7f80000000 - 1.

---
Changes in v2:
- update the commit message: add explanation about direct map upper bound.
---
Changes in v2:
- new patch

---
 xen/arch/riscv/include/asm/config.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
xen/arch/riscv/include/asm/config.h +6 -6
index 4954677aff..826e5c7172 100644
@@ -41,17 +41,17 @@
  * Start addr          | End addr         | Slot       | area description
  * ============================================================================
  *                   .....                 L2 511          Unused
- *  0xffffffffc0a00000  0xffffffffc0c00000 L2 511          Fixmap
+ *  0xffffffffc0a00000  0xffffffffc0bfffff L2 511          Fixmap
  *                   ..... ( 2 MB gap )
- *  0xffffffffc0400000  0xffffffffc0800000 L2 511          FDT
+ *  0xffffffffc0400000  0xffffffffc07fffff L2 511          FDT
  *                   ..... ( 2 MB gap )
- *  0xffffffffc0000000  0xffffffffc0200000 L2 511          Xen
+ *  0xffffffffc0000000  0xffffffffc01fffff L2 511          Xen
  *                   .....                 L2 510          Unused
- *  0x3200000000        0x7f40000000       L2 200-509      Direct map
+ *  0x3200000000        0x7f7fffffff       L2 200-509      Direct map
  *                   .....                 L2 199          Unused
- *  0x30c0000000        0x31c0000000       L2 195-198      Frametable
+ *  0x30c0000000        0x31bfffffff       L2 195-198      Frametable
  *                   .....                 L2 194          Unused
- *  0x3040000000        0x3080000000       L2 193          VMAP
+ *  0x3040000000        0x307fffffff       L2 193          VMAP
  *                   .....                 L2 0-192        Unused
 #elif RV_STAGE1_MODE == SATP_MODE_SV48
  * Memory layout is the same as for SV39 in terms of slots, so only start and
Signature · 1 line
2.47.1
Acked-by on v3 1/7
#2 lore raw

Re: [PATCH v3 1/7] xen/riscv: update layout table in config.h

Quoted 10 lines · Oleksii Kurochko

Make all upper bounds (end addresses) for areas inclusive to align with the corresponding definitions.

For the Direct map region, the upper bound was calculated incorrectly in efadb18dd58aba ("xen/riscv: add VM space layout"). It should be 0x7f80000000 (considering that the value is exclusive, instead of 0x7f40000000). Therefore, the inclusive upper bound for that region is 0x7f80000000 - 1.

#3 lore raw

PATCH v3 2/7 xen/riscv: add destroy_xen_mappings() to remove mappings in Xen page tables

In reply to Oleksii Kurochko

Introduce the destroy_xen_mappings() function, which removes page mappings in Xen's page tables between a start address s and an end address e.
The function ensures that both s and e are page-aligned
and verifies that the start address is less than or equal to the end address before calling pt_update() to invalidate the mappings. The pt_update() function is called with INVALID_MFN and PTE_VALID=0 in the flags, which tell pt_update() to remove mapping. No additional ASSERT() is required to check these arguments, as they are hardcoded in the call to pt_update() within destroy_xen_mappings().

---
Changes in V3:
- Fix code style: drop blanks in condition part of ternary operation.
---
Changes in V2:
- Drop ASSERT(s <= e).
- Update implementation of destroy_xen_mappings() to avoid calling of pt_update() when start_addr >= end_addr and return -EINVAL.
- Add Acked-by: Jan Beulich <[email protected]>.

---
 xen/arch/riscv/mm.c | 6 ------
 xen/arch/riscv/pt.c | 8 ++++++++
 2 files changed, 8 insertions(+), 6 deletions(-)
xen/arch/riscv/mm.c +0 -6
index 9359dc7f33..f2bf279bac 100644
@@ -360,12 +360,6 @@ int xenmem_add_to_physmap_one(struct domain *d, unsigned int space,
     return 0;
 }
 
-int destroy_xen_mappings(unsigned long s, unsigned long e)
-{
-    BUG_ON("unimplemented");
-    return -1;
-}
-
 void share_xen_page_with_guest(struct page_info *page, struct domain *d,
                                enum XENSHARE_flags flags)
 {
xen/arch/riscv/pt.c +8 -0
index d62aceb36c..2ca23206f4 100644
@@ -421,6 +421,14 @@ int map_pages_to_xen(unsigned long virt,
     return pt_update(virt, mfn, nr_mfns, flags);
 }
 
+int destroy_xen_mappings(unsigned long s, unsigned long e)
+{
+    ASSERT(IS_ALIGNED(s, PAGE_SIZE));
+    ASSERT(IS_ALIGNED(e, PAGE_SIZE));
+
+    return (s < e) ? pt_update(s, INVALID_MFN, PFN_DOWN(e - s), 0) : -EINVAL;
+}
+
 int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns)
 {
     return pt_update(virt, INVALID_MFN, nr_mfns, PTE_POPULATE);
Signature · 1 line
2.47.1
#4 lore raw

PATCH v3 3/7 xen/riscv: reorder includes in asm/page.h alphabetically

In reply to Oleksii Kurochko

---
Changes in V3:
- Nothing changed. Only rebase.
---
Changes in V2:
- Add Acked-by: Jan Beulich <[email protected]>

---
 xen/arch/riscv/include/asm/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/riscv/include/asm/page.h +1 -1
index 91b1194b55..bf3f75e85d 100644
@@ -5,8 +5,8 @@
 
 #ifndef __ASSEMBLY__
 
-#include <xen/const.h>
 #include <xen/bug.h>
+#include <xen/const.h>
 #include <xen/types.h>
 
 #include <asm/atomic.h>
Signature · 1 line
2.47.1
#5 lore raw

PATCH v3 4/7 xen/riscv: add {set,clear}_fixmap() functions for managing fixmap entries

In reply to Oleksii Kurochko

Introduce set_fixmap() and clear_fixmap() functions to manage mappings in the fixmap region. The set_fixmap() function maps a 4k page ( as only L0 is expected to be updated; look at setup_fixmap_mappings() ) at a specified fixmap entry using map_pages_to_xen(), while clear_fixmap() removes the mapping from a fixmap entry by calling destroy_xen_mappings().

Both functions ensure that the operations succeed by asserting that their respective calls (map_pages_to_xen() and destroy_xen_mappings()) return 0. BUG() is added to trigger a failure if any issues occur during the mapping or unmapping process.

---
Changes in V3:
- Fix code style: ident if-condition properly.
---
Changes in V2:
- Update the commit message.
- drop local variables from {set, clear}_fixmap(); use if (... != 0) + BUG().

---
 xen/arch/riscv/include/asm/fixmap.h |  5 +++++
 xen/arch/riscv/pt.c                 | 16 ++++++++++++++++
 2 files changed, 21 insertions(+)
xen/arch/riscv/include/asm/fixmap.h +5 -0
index 818c8ce07b..e399a15f53 100644
@@ -32,6 +32,11 @@
  */
 extern pte_t xen_fixmap[];
 
+/* Map a page in a fixmap entry */
+void set_fixmap(unsigned int map, mfn_t mfn, unsigned int flags);
+/* Remove a mapping from a fixmap entry */
+void clear_fixmap(unsigned int map);
+
 #define fix_to_virt(slot) ((void *)FIXMAP_ADDR(slot))
 
 static inline unsigned int virt_to_fix(vaddr_t vaddr)
xen/arch/riscv/pt.c +16 -0
index 2ca23206f4..aea9f97a5a 100644
@@ -8,6 +8,7 @@
 #include <xen/pmap.h>
 #include <xen/spinlock.h>
 
+#include <asm/fixmap.h>
 #include <asm/flushtlb.h>
 #include <asm/page.h>
 
@@ -433,3 +434,18 @@ int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns)
 {
     return pt_update(virt, INVALID_MFN, nr_mfns, PTE_POPULATE);
 }
+
+/* Map a 4k page in a fixmap entry */
+void set_fixmap(unsigned int map, mfn_t mfn, unsigned int flags)
+{
+    if ( map_pages_to_xen(FIXMAP_ADDR(map), mfn, 1, flags | PTE_SMALL) != 0 )
+        BUG();
+}
+
+/* Remove a mapping from a fixmap entry */
+void clear_fixmap(unsigned int map)
+{
+    if ( destroy_xen_mappings(FIXMAP_ADDR(map),
+                              FIXMAP_ADDR(map) + PAGE_SIZE) != 0 )
+        BUG();
+}
Signature · 1 line
2.47.1
#6 lore raw

PATCH v3 5/7 xen/riscv: implement data and instruction cache operations

In reply to Oleksii Kurochko

Implement following cache operations:
- clean_and_invalidate_dcache_va_range()
- clean_dcache_va_range()
- invalidate_icache()

The first two functions may require support for the CMO (Cache Management Operations) extension and/or hardware-specific instructions. Currently, only QEMU is supported, which does not model cache behavior. Therefore, clean_and_invalidate_dcache_va_range() and clean_dcache_va_range() are implemented to simply return 0. For other cases, generate compilation error so a user won't miss to update this function if necessery.
If hardware supports CMO or hardware-specific instructions, these functions should be updated accordingly. To support current implementation of these function CONFIG_QEMU_PLATFORM is introduced.

invalidate_icache() is implemented using fence.i instruction as mentioned in the unpriv spec:
The FENCE.I instruction was designed to support a wide variety of implementations. A simple implementation can flush the local instruction cache and the instruction pipeline when the FENCE.I is executed. A more complex implementation might snoop the instruction (data) cache on every data (instruction) cache miss, or use an inclusive unified private L2 cache to invalidate lines from the primary instruction cache when they are being written by a local store instruction.
If instruction and data caches are kept coherent in this way, or if the memory system consists of only uncached RAMs, then just the fetch pipeline needs to be flushed at a FENCE.I.
The FENCE.I instruction requires the presence of the Zifencei extension, which might not always be available. However, Xen uses the RV64G ISA, which guarantees the presence of the Zifencei extension. According to the unprivileged ISA specification (version 20240411):
One goal of the RISC-V project is that it be used as a stable software development target. For this purpose, we define a combination of a base ISA (RV32I or RV64I) plus selected standard extensions (IMAFD, Zicsr, Zifencei) as a "general-purpose" ISA, and we use the abbreviation G for the IMAFDZicsr_Zifencei combination of instruction-set extensions.

Set CONFIG_QEMU_PLATFORM=y in tiny64_defconfig to have proper implemtation of clean_and_invalidate_dcache_va_range() and clean_dcache_va_range() for CI.

---
Changes in V3:
- Updates for config QEMU:
- Rename CONFIG_QEMU to CONFIG_QEMU_PLATFORM.
- Drop "depends on RISCV_64" property for Config QEMU_PLATFORM.
- Split help string.
- Update implementation of the functions: clean_and_invalidate_dcache_va_range() and clean_dcache_va_range().
- Minimal code style fixes: move second argument of clean_and_invalidate_dcache_va_range() to next line.
- Update the commit message.
---
Changes in V2:
- Update the commit message and subject:
- drop information about HAS_CMO;
- add information about Zifencei extension;
- Introdce platforms directory and CONFIG_QEMU; update implementation of data/instruction cache operations as returning 0 for CONFIG_QEMU and for others - return -ENOTSUPP.
- Drop HAS_CMO config.

---
 xen/arch/riscv/Kconfig                  |  2 ++
 xen/arch/riscv/configs/tiny64_defconfig |  1 +
 xen/arch/riscv/include/asm/page.h       | 22 +++++++++++++++++++++-
 xen/arch/riscv/platforms/Kconfig        |  5 +++++
 4 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/riscv/platforms/Kconfig
xen/arch/riscv/Kconfig +2 -0
index 1858004676..00f329054c 100644
@@ -52,6 +52,8 @@ config RISCV_ISA_C
 
 endmenu
 
+source "arch/riscv/platforms/Kconfig"
+
 source "common/Kconfig"
 
 source "drivers/Kconfig"
xen/arch/riscv/configs/tiny64_defconfig +1 -0
index fc7a04872f..6af563bd73 100644
@@ -10,3 +10,4 @@ CONFIG_RISCV_64=y
 CONFIG_DEBUG=y
 CONFIG_DEBUG_INFO=y
 CONFIG_EXPERT=y
+CONFIG_QEMU_PLATFORM=y
xen/arch/riscv/include/asm/page.h +21 -1
index bf3f75e85d..c82432dc67 100644
@@ -7,6 +7,7 @@
 
 #include <xen/bug.h>
 #include <xen/const.h>
+#include <xen/errno.h>
 #include <xen/types.h>
 
 #include <asm/atomic.h>
@@ -148,9 +149,28 @@ static inline bool pte_is_mapping(pte_t p)
     return (p.pte & PTE_VALID) && (p.pte & PTE_ACCESS_MASK);
 }
 
+static inline int clean_and_invalidate_dcache_va_range(const void *p,
+                                                       unsigned long size)
+{
+#ifndef CONFIG_QEMU_PLATFORM
+    #error "should clean_and_invalidate_dcache_va_range() be updated?"
+#endif
+
+    return 0;
+}
+
+static inline int clean_dcache_va_range(const void *p, unsigned long size)
+{
+#ifndef CONFIG_QEMU_PLATFORM
+    #error "should clean_dcache_va_range() be updated?"
+#endif
+
+    return 0;
+}
+
 static inline void invalidate_icache(void)
 {
-    BUG_ON("unimplemented");
+    asm volatile ( "fence.i" ::: "memory" );
 }
 
 #define clear_page(page) memset((void *)(page), 0, PAGE_SIZE)
xen/arch/riscv/platforms/Kconfig new file +5 -0
new file mode 100644
index 0000000000..710423a59a
@@ -0,0 +1,5 @@
+config QEMU_PLATFORM
+	bool "QEMU based platform support"
+	help
+	  Enable all the required drivers for QEMU riscv64
+	  virt emulated machine.
Signature · 1 line
2.47.1
Acked-by on v3 5/7
#7 lore raw

Re: [PATCH v3 5/7] xen/riscv: implement data and instruction cache operations

Quoted 37 lines · Oleksii Kurochko

Implement following cache operations:
- clean_and_invalidate_dcache_va_range()
- clean_dcache_va_range()
- invalidate_icache()

The first two functions may require support for the CMO (Cache Management Operations) extension and/or hardware-specific instructions. Currently, only QEMU is supported, which does not model cache behavior. Therefore, clean_and_invalidate_dcache_va_range() and clean_dcache_va_range() are implemented to simply return 0. For other cases, generate compilation error so a user won't miss to update this function if necessery.
If hardware supports CMO or hardware-specific instructions, these functions should be updated accordingly. To support current implementation of these function CONFIG_QEMU_PLATFORM is introduced.

invalidate_icache() is implemented using fence.i instruction as mentioned in the unpriv spec:
The FENCE.I instruction was designed to support a wide variety of implementations. A simple implementation can flush the local instruction cache and the instruction pipeline when the FENCE.I is executed. A more complex implementation might snoop the instruction (data) cache on every data (instruction) cache miss, or use an inclusive unified private L2 cache to invalidate lines from the primary instruction cache when they are being written by a local store instruction.
If instruction and data caches are kept coherent in this way, or if the memory system consists of only uncached RAMs, then just the fetch pipeline needs to be flushed at a FENCE.I.
The FENCE.I instruction requires the presence of the Zifencei extension, which might not always be available. However, Xen uses the RV64G ISA, which guarantees the presence of the Zifencei extension. According to the unprivileged ISA specification (version 20240411):
One goal of the RISC-V project is that it be used as a stable software development target. For this purpose, we define a combination of a base ISA (RV32I or RV64I) plus selected standard extensions (IMAFD, Zicsr, Zifencei) as a "general-purpose" ISA, and we use the abbreviation G for the IMAFDZicsr_Zifencei combination of instruction-set extensions.

Quoted 4 lines

Set CONFIG_QEMU_PLATFORM=y in tiny64_defconfig to have proper implemtation of clean_and_invalidate_dcache_va_range() and clean_dcache_va_range() for CI.

I'm not entirely happy with this (and where it appears to be moving us), but for the time being it's perhaps good enough, so

Albeit with ...

Quoted 12 lines
xen/arch/riscv/include/asm/page.h +8 -0
@@ -148,9 +149,28 @@ static inline bool pte_is_mapping(pte_t p)
     return (p.pte & PTE_VALID) && (p.pte & PTE_ACCESS_MASK);
 }
 
+static inline int clean_and_invalidate_dcache_va_range(const void *p,
+                                                       unsigned long size)
+{
+#ifndef CONFIG_QEMU_PLATFORM
+    #error "should clean_and_invalidate_dcache_va_range() be updated?"
+#endif
+
+    return 0;

xen/arch/riscv/include/asm/page.h static inline bool pte_is_mapping(pte_t p) · line 169

Quoted 10 lines
xen/arch/riscv/include/asm/page.h +10 -0
+}
+
+static inline int clean_dcache_va_range(const void *p, unsigned long size)
+{
+#ifndef CONFIG_QEMU_PLATFORM
+    #error "should clean_dcache_va_range() be updated?"
+#endif
+
+    return 0;
+}

... the #-es moved back to the 1st column, which I'll take the liberty of doing while committing. Personally I wonder anyway why these aren't simply BUILD_BUG_ON("unimplemented").

Jan

#8 lore raw

PATCH v3 6/7 xen/riscv: implement prereq for DTB relocation

In reply to Oleksii Kurochko

DTB relocatin in Xen heap requires the following functions which are introduced in current patch:
- xvmalloc_array()
- copy_from_paddr()

For internal use of xvmalloc, the functions flush_page_to_ram() and virt_to_page() are introduced. virt_to_page() is also required for free_xenheap_pages().

---
Changes in V3:
- Rewrite part of flush_page_to_ram() to use BUG() instead of BUG_ON().
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in V2:
- Drop variable directmap_virt_end.
- Update ASSERT in virt_to_page() to use DIRECTMAP_VIRT_END instead of variable directmap_virt_end.
- Declare local varibale v as const inside flush_page_to_ram().
- Declare copy_from_paddr() in riscv/setup.h as in the future it will be used for copying kernel in guest memory.
- Code style updates for copy_from_paddr().
- Make l and s variable in copy_from_paddr() as the virables' initializers.
- Drop call of clean_dcache_va_range(dst, l) in copy_from_paddr() as the necessiry of it is Arm-specific:
After memcpy'ing the kernel in guest memory Arm need to flush the dcache to make sure that the data actually reaches the memory before we start executing guest code with caches disabled.
RISC-V has caches always enabled thereby there is no such issue for RISC-V.
- Make local variable src in copy_from_paddr() as const.
- Update the commit message and subject: drop information of relocate_fdt() introduction and rename it to "prereq for DTB relocation".
- Add BUG_ON() inside flush_page_to_ram() to check the return value of clean_and_invalidate_dcache_va_range().
- Move relocate_fdt() introduction to the next patch to make this patch compilable.

---
 xen/arch/riscv/include/asm/mm.h    |  8 ++++++--
 xen/arch/riscv/include/asm/page.h  | 12 ++++++++++--
 xen/arch/riscv/include/asm/setup.h |  4 ++++
 xen/arch/riscv/setup.c             | 26 ++++++++++++++++++++++++++
 4 files changed, 46 insertions(+), 4 deletions(-)
xen/arch/riscv/include/asm/mm.h +6 -2
index 699ed23f0d..292aa48fc1 100644
@@ -8,6 +8,7 @@
 #include <xen/const.h>
 #include <xen/mm-frame.h>
 #include <xen/pdx.h>
+#include <xen/pfn.h>
 #include <xen/types.h>
 
 #include <asm/page-bits.h>
@@ -148,8 +149,11 @@ static inline void *page_to_virt(const struct page_info *pg)
 /* Convert between Xen-heap virtual addresses and page-info structures. */
 static inline struct page_info *virt_to_page(const void *v)
 {
-    BUG_ON("unimplemented");
-    return NULL;
+    unsigned long va = (unsigned long)v;
+
+    ASSERT((va >= DIRECTMAP_VIRT_START) && (va <= DIRECTMAP_VIRT_END));
+
+    return frametable_virt_start + PFN_DOWN(va - directmap_virt_start);
 }
 
 /*
xen/arch/riscv/include/asm/page.h +10 -2
index c82432dc67..a1d54c7fb1 100644
@@ -7,6 +7,7 @@
 
 #include <xen/bug.h>
 #include <xen/const.h>
+#include <xen/domain_page.h>
 #include <xen/errno.h>
 #include <xen/types.h>
 
@@ -176,10 +177,17 @@ static inline void invalidate_icache(void)
 #define clear_page(page) memset((void *)(page), 0, PAGE_SIZE)
 #define copy_page(dp, sp) memcpy(dp, sp, PAGE_SIZE)
 
-/* TODO: Flush the dcache for an entire page. */
 static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache)
 {
-    BUG_ON("unimplemented");
+    const void *v = map_domain_page(_mfn(mfn));
+
+    if ( clean_and_invalidate_dcache_va_range(v, PAGE_SIZE) )
+        BUG();
+
+    unmap_domain_page(v);
+
+    if ( sync_icache )
+        invalidate_icache();
 }
 
 /* Write a pagetable entry. */
xen/arch/riscv/include/asm/setup.h +4 -0
index 844a2f0ef1..c9d69cdf51 100644
@@ -3,10 +3,14 @@
 #ifndef ASM__RISCV__SETUP_H
 #define ASM__RISCV__SETUP_H
 
+#include <xen/types.h>
+
 #define max_init_domid (0)
 
 void setup_mm(void);
 
+void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len);
+
 #endif /* ASM__RISCV__SETUP_H */
 
 /*
xen/arch/riscv/setup.c +26 -0
index 9680332fee..bea3f27c4d 100644
@@ -12,6 +12,7 @@
 #include <public/version.h>
 
 #include <asm/early_printk.h>
+#include <asm/fixmap.h>
 #include <asm/sbi.h>
 #include <asm/setup.h>
 #include <asm/smp.h>
@@ -26,6 +27,31 @@ void arch_get_xen_caps(xen_capabilities_info_t *info)
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
     __aligned(STACK_SIZE);
 
+/**
+ * copy_from_paddr - copy data from a physical address
+ * @dst: destination virtual address
+ * @paddr: source physical address
+ * @len: length to copy
+ */
+void __init copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)
+{
+    const void *src = (void *)FIXMAP_ADDR(FIX_MISC);
+
+    while ( len )
+    {
+        unsigned long s = paddr & (PAGE_SIZE - 1);
+        unsigned long l = min(PAGE_SIZE - s, len);
+
+        set_fixmap(FIX_MISC, maddr_to_mfn(paddr), PAGE_HYPERVISOR_RW);
+        memcpy(dst, src + s, l);
+        clear_fixmap(FIX_MISC);
+
+        paddr += l;
+        dst += l;
+        len -= l;
+    }
+}
+
 void __init noreturn start_xen(unsigned long bootcpu_id,
                                paddr_t dtb_addr)
 {
Signature · 1 line
2.47.1
#9 lore raw

PATCH v3 7/7 xen/riscv: relocating and unflattening host device tree

In reply to Oleksii Kurochko

Introduce relocate_fdt() and call it to relocate FDT to Xen heap instead of using early mapping as it is expected that discard_initial_modules() ( is supposed to call in the future ) discards the FDT boot module and remove_early_mappings() destroys the early mapping.

Unflatten a device tree, creating the tree of struct device_node. It also fills the "name" and "type" pointers of the nodes so the normal device-tree walking functions can be used.

Set device_tree_flattened to NULL in the case when acpi_disabled is equal to false.

---
Changes in V3:
- Add Acked-by: Jan Beulich <[email protected]>.
---
Changes in V2:
- Move introduction of relocate_fdt() to the current patch with the following change:
- use xvmalloc() instead of xmalloc_bytes() in relocate_fdt();
- Drop the check of returned fdt_size from boot_fdt_info() to be in sync with Arm and boot_fdt_info() will panic anyway if something wrong with DTB.
- Update the commit message.

---
 xen/arch/riscv/setup.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
xen/arch/riscv/setup.c +29 -2
index bea3f27c4d..fb6bbba684 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/acpi.h>
 #include <xen/bug.h>
 #include <xen/bootfdt.h>
 #include <xen/compile.h>
@@ -8,6 +9,7 @@
 #include <xen/mm.h>
 #include <xen/shutdown.h>
 #include <xen/vmap.h>
+#include <xen/xvmalloc.h>
 
 #include <public/version.h>
 
@@ -52,10 +54,24 @@ void __init copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)
     }
 }
 
+/* Relocate the FDT in Xen heap */
+static void * __init relocate_fdt(paddr_t dtb_paddr, size_t dtb_size)
+{
+    void *fdt = xvmalloc_array(uint8_t, dtb_size);
+
+    if ( !fdt )
+        panic("Unable to allocate memory for relocating the Device-Tree.\n");
+
+    copy_from_paddr(fdt, dtb_paddr, dtb_size);
+
+    return fdt;
+}
+
 void __init noreturn start_xen(unsigned long bootcpu_id,
                                paddr_t dtb_addr)
 {
     const char *cmdline;
+    size_t fdt_size;
 
     remove_identity_mapping();
 
@@ -80,8 +96,7 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
                           _end - _start, false) )
         panic("Failed to add BOOTMOD_XEN\n");
 
-    if ( !boot_fdt_info(device_tree_flattened, dtb_addr) )
-        BUG();
+    fdt_size = boot_fdt_info(device_tree_flattened, dtb_addr);
 
     cmdline = boot_fdt_cmdline(device_tree_flattened);
     printk("Command line: %s\n", cmdline);
@@ -99,6 +114,18 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
      */
     system_state = SYS_STATE_boot;
 
+    if ( acpi_disabled )
+    {
+        printk("Booting using Device Tree\n");
+        device_tree_flattened = relocate_fdt(dtb_addr, fdt_size);
+        dt_unflatten_host_device_tree();
+    }
+    else
+    {
+        device_tree_flattened = NULL;
+        panic("Booting using ACPI isn't supported\n");
+    }
+
     printk("All set up\n");
 
     machine_halt();
Signature · 1 line
2.47.1
#10 lore raw
In reply to Oleksii Kurochko
Quoted 42 lines · Oleksii Kurochko

The current patch series introduces the relocation of the host device tree file to free up low memory and also it is expected that discard_initial_modules() will be called sooner or later, it will discard the FDT boot module, and remove_early_mappings() will destroy the early mappings.

In addition to relocation, unflattening is introduced to create the tree of struct device_node for the host device tree.

To implement this, several things have been introduced:
- destroy_xen_mappings() function, which removes page mappings from Xen's page tables. This is necessary for clear_fixmap().
- {set,clear}_fixmap() functions to manage mappings in the fixmap region, which are expected to be used in copy_from_paddr() to copy the FDT to Xen's heap.
- Introduce new config CONFIG_QEMU which is going to be used to cover changes connected to QEMU virtual board. It will be used during introduction of stubs for clean_and_invalidate_dcache_va_range() and clean_dcache_va_range(), which are expected to be used in copy_from_paddr() and flush_page_to_ram(), which in turn are expected to be used during the call to xvmalloc_array() in relocate_fdt().
In case of QEMU cached related functions are implemented as returning 0 as QEMU doesn't model cache ( and so CMO extensions ). For others cases, it is introduced as -ENOSUPP as h/w could support CMO extension ( or hardware specific insertions ) and it will need to update implementation of the mentioned functions.
- The introduction of copy_from_paddr() to copy the FDT to an address allocated in Xen's heap.

---
Changes in v3:
- Add some Acks for the patches. All the patches are Acked except:
[PATCH v3 1/7] xen/riscv: update layout table in config.h [PATCH v3 5/7] xen/riscv: implement data and instruction cache operations
- Other changes please look at the specific patch.
---
Changes in v2:
- Update the cover letter.
- Introduce new patch with aligning of upper bounds in the layout table in config.h with the definitions below which are inclusive.
- Other changes please look at the specific patch.
---

Quoted 7 lines

Oleksii Kurochko (7):
xen/riscv: update layout table in config.h
xen/riscv: add destroy_xen_mappings() to remove mappings in Xen page
tables
xen/riscv: reorder includes in asm/page.h alphabetically xen/riscv: add {set,clear}_fixmap() functions for managing fixmap
entries

Please can you avoid re-posting patches that were already committed (the latter three)?

Jan

#11 lore raw
Quoted 50 lines · Jan Beulich
Quoted 42 lines · Oleksii Kurochko

The current patch series introduces the relocation of the host device tree file to free up low memory and also it is expected that discard_initial_modules() will be called sooner or later, it will discard the FDT boot module, and remove_early_mappings() will destroy the early mappings.

In addition to relocation, unflattening is introduced to create the tree of struct device_node for the host device tree.

To implement this, several things have been introduced:
- destroy_xen_mappings() function, which removes page mappings from Xen's page tables. This is necessary for clear_fixmap().
- {set,clear}_fixmap() functions to manage mappings in the fixmap region, which are expected to be used in copy_from_paddr() to copy the FDT to Xen's heap.
- Introduce new config CONFIG_QEMU which is going to be used to cover changes connected to QEMU virtual board. It will be used during introduction of stubs for clean_and_invalidate_dcache_va_range() and clean_dcache_va_range(), which are expected to be used in copy_from_paddr() and flush_page_to_ram(), which in turn are expected to be used during the call to xvmalloc_array() in relocate_fdt().
In case of QEMU cached related functions are implemented as returning 0 as QEMU doesn't model cache ( and so CMO extensions ). For others cases, it is introduced as -ENOSUPP as h/w could support CMO extension ( or hardware specific insertions ) and it will need to update implementation of the mentioned functions.
- The introduction of copy_from_paddr() to copy the FDT to an address allocated in Xen's heap.

---
Changes in v3:
- Add some Acks for the patches. All the patches are Acked except:
[PATCH v3 1/7] xen/riscv: update layout table in config.h [PATCH v3 5/7] xen/riscv: implement data and instruction cache operations
- Other changes please look at the specific patch.
---
Changes in v2:
- Update the cover letter.
- Introduce new patch with aligning of upper bounds in the layout table in config.h with the definitions below which are inclusive.
- Other changes please look at the specific patch.
---

Quoted 7 lines

Oleksii Kurochko (7):
xen/riscv: update layout table in config.h
xen/riscv: add destroy_xen_mappings() to remove mappings in Xen page
tables
xen/riscv: reorder includes in asm/page.h alphabetically xen/riscv: add {set,clear}_fixmap() functions for managing fixmap
entries

Quoted 2 lines

Please can you avoid re-posting patches that were already committed (the latter three)?

Sure, I confused something when fetch upstream/staging so missed that some patches were merged.

Sorry for inconvenience.

~ Oleksii