Skip to contentSkip to conversation

RISCV device tree mapping

PATCH Superseded Oleksii Kurochko 3 participants 54 messages Jul 24, 2024 – Aug 7, 2024 RISC-V

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover RISCV device tree mapping

Current patch series introduces device tree mapping for RISC-V and necessary things for that such as:
- Fixmap mapping
- pmap
- Xen page table processing

Also, it introduces common stuff for working with fdt which is
based on the patches from [1]:
[PATCH v4 2/6] xen/device-tree: Move Arm's setup.c bootinfo functions to common [PATCH v4 3/6] xen/common: Move Arm's bootfdt.c
All changes which were done on top of Shawn's patches please find in "Changes" section of each patch.

Also there is another one depenency:
RISCV basic exception handling implementation [2]

[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] https://lore.kernel.org/xen-devel/[email protected]/T/#t

---
Changes in v3:
- Introduce SBI RFENCE extension support.
- Introduce and initialize pcpu_info[] and __cpuid_to_hartid_map[] and functionality to work with this arrays.
- Make page table handling arch specific instead of trying to make it generic.
- All other changes are patch specific so please look at the patch.
---
Changes in v2:
- Update the cover letter message
- introduce fixmap mapping
- introduce pmap
- introduce CONFIG_GENREIC_PT
- update use early_fdt_map() after MMU is enabled.
---

Oleksii Kurochko (7):
xen/riscv: enable CONFIG_HAS_DEVICE_TREE xen/riscv: setup fixmap mapping
xen/riscv: introduce asm/pmap.h header xen/riscv: introduce functionality to work with cpu info xen/riscv: introduce and init SBI RFENCE extension xen/riscv: page table handling
xen/riscv: introduce early_fdt_map()

Shawn Anastasio (2):
xen/device-tree: Move Arm's setup.c bootinfo functions to common xen/common: Move Arm's bootfdt.c to common

 MAINTAINERS                            |    2 +
 xen/arch/arm/Makefile                  |    1 -
 xen/arch/arm/bootfdt.c                 |  622 -------
 xen/arch/arm/include/asm/setup.h       |  198 +--
 xen/arch/arm/setup.c                   |  432 -----
 xen/arch/riscv/Kconfig                 |    3 +
 xen/arch/riscv/Makefile                |    3 +
 xen/arch/riscv/include/asm/config.h    |    8 +
 xen/arch/riscv/include/asm/fixmap.h    |   44 +
 xen/arch/riscv/include/asm/flushtlb.h  |   37 +
 xen/arch/riscv/include/asm/mm.h        |    6 +
 xen/arch/riscv/include/asm/page-bits.h |   36 +
 xen/arch/riscv/include/asm/page.h      |   84 +-
 xen/arch/riscv/include/asm/pmap.h      |   33 +
 xen/arch/riscv/include/asm/processor.h |   35 +-
 xen/arch/riscv/include/asm/sbi.h       |   57 +
 xen/arch/riscv/include/asm/smp.h       |   10 +
 xen/arch/riscv/mm.c                    |  120 +-
 xen/arch/riscv/pt.c                    |  410 +++++
 xen/arch/riscv/sbi.c                   |  256 +++
 xen/arch/riscv/setup.c                 |   27 +
 xen/arch/riscv/smp.c                   |    4 +
 xen/arch/riscv/smpboot.c               |   12 +
 xen/arch/riscv/xen.lds.S               |    2 +-
 xen/common/Makefile                    |    2 +-
 xen/common/device-tree/Makefile        |    3 +
 xen/common/device-tree/bootfdt.c       |  635 +++++++
 xen/common/device-tree/bootinfo.c      |  459 +++++
 xen/common/device-tree/device_tree.c   | 2253 ++++++++++++++++++++++++
 xen/common/device_tree.c               | 2253 ------------------------
 xen/include/xen/bootfdt.h              |  209 +++
 31 files changed, 4740 insertions(+), 3516 deletions(-)
 delete mode 100644 xen/arch/arm/bootfdt.c
 create mode 100644 xen/arch/riscv/include/asm/fixmap.h
 create mode 100644 xen/arch/riscv/include/asm/pmap.h
 create mode 100644 xen/arch/riscv/pt.c
 create mode 100644 xen/arch/riscv/smp.c
 create mode 100644 xen/arch/riscv/smpboot.c
 create mode 100644 xen/common/device-tree/Makefile
 create mode 100644 xen/common/device-tree/bootfdt.c
 create mode 100644 xen/common/device-tree/bootinfo.c
 create mode 100644 xen/common/device-tree/device_tree.c
 delete mode 100644 xen/common/device_tree.c
 create mode 100644 xen/include/xen/bootfdt.h
Signature · 1 line
2.45.2
#1 lore raw

PATCH v3 3/9 xen/riscv: enable CONFIG_HAS_DEVICE_TREE

Enable build of generic functionality for working with device tree for RISC-V.
Also, a collection of functions for parsing memory map and other boot information from a device tree are available now.

---
Changes in V3:
- add Acked-by: Jan Beulich <[email protected]>
- update the commit message
---
Changes in V2:
- move 'select HAS_DEVICE_TREE' to CONFIG_RISCV.
---
Changes in V1:
- new patch

---
 xen/arch/riscv/Kconfig | 1 +
 1 file changed, 1 insertion(+)
xen/arch/riscv/Kconfig +1 -0
index f531e96657..259eea8d3b 100644
@@ -2,6 +2,7 @@ config RISCV
 	def_bool y
 	select FUNCTION_ALIGNMENT_16B
 	select GENERIC_BUG_FRAME
+	select HAS_DEVICE_TREE
 
 config RISCV_64
 	def_bool y
Signature · 1 line
2.45.2
#2 lore raw

PATCH v3 4/9 xen/riscv: setup fixmap mapping

In reply to Oleksii Kurochko

Introduce a function to set up fixmap mappings and L0 page table for fixmap.

Additionally, defines were introduced in riscv/config.h to calculate the FIXMAP_BASE address.
This involved introducing BOOT_FDT_VIRT_{START, SIZE} and XEN_VIRT_SIZE, XEN_VIRT_END.

Also, the check of Xen size was updated in the riscv/lds.S script to use XEN_VIRT_SIZE instead of a hardcoded constant.

---
Changes in V3:
- s/XEN_SIZE/XEN_VIRT_SIZE
- drop usage of XEN_VIRT_END.
- sort newly introduced defines in config.h by address
- code style fixes
- drop runtime check of that pte is valid as it was checked in L1 page table finding cycle by BUG_ON().
- update implementation of write_pte() with FENCE rw, rw.
- add BUILD_BUG_ON() to check that amount of entries aren't bigger then entries in page table.
- drop set_fixmap, clear_fixmap declarations as they aren't used and defined now
- update the commit message.
- s/__ASM_FIXMAP_H/ASM_FIXMAP_H
- add SPDX-License-Identifier: GPL-2.0

---
 xen/arch/riscv/include/asm/config.h |  8 +++++
 xen/arch/riscv/include/asm/fixmap.h | 44 +++++++++++++++++++++++++++
 xen/arch/riscv/include/asm/mm.h     |  2 ++
 xen/arch/riscv/include/asm/page.h   |  9 ++++++
 xen/arch/riscv/mm.c                 | 47 +++++++++++++++++++++++++++++
 xen/arch/riscv/setup.c              |  2 ++
 xen/arch/riscv/xen.lds.S            |  2 +-
 7 files changed, 113 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/riscv/include/asm/fixmap.h
xen/arch/riscv/include/asm/config.h +8 -0
index 50583aafdc..f517684cbb 100644
@@ -66,6 +66,14 @@
 #define SLOTN_ENTRY_BITS        (HYP_PT_ROOT_LEVEL * VPN_BITS + PAGE_SHIFT)
 #define SLOTN(slot)             (_AT(vaddr_t, slot) << SLOTN_ENTRY_BITS)
 
+#define XEN_VIRT_SIZE           MB(2)
+
+#define BOOT_FDT_VIRT_START     (XEN_VIRT_START + XEN_VIRT_SIZE)
+#define BOOT_FDT_VIRT_SIZE      MB(4)
+
+#define FIXMAP_BASE             (BOOT_FDT_VIRT_START + BOOT_FDT_VIRT_SIZE)
+#define FIXMAP_ADDR(n)          (FIXMAP_BASE + (n) * PAGE_SIZE)
+
 #if RV_STAGE1_MODE == SATP_MODE_SV39
 #define XEN_VIRT_START 0xFFFFFFFFC0000000
 #elif RV_STAGE1_MODE == SATP_MODE_SV48
xen/arch/riscv/include/asm/fixmap.h new file +44 -0

44 lines not on this page · raw message

xen/arch/riscv/include/asm/mm.h +2 -0
index 25af9e1aaa..a0bdc2bc3a 100644
@@ -255,4 +255,6 @@ static inline unsigned int arch_get_dma_bitsize(void)
     return 32; /* TODO */
 }
 
+void setup_fixmap_mappings(void);
+
 #endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/include/asm/page.h +9 -0
index c831e16417..0cc2f37cf8 100644
@@ -11,6 +11,7 @@
 
 #include <asm/mm.h>
 #include <asm/page-bits.h>
+#include <asm/system.h>
 
 #define VPN_MASK                    (PAGETABLE_ENTRIES - 1UL)
 
@@ -81,6 +82,14 @@ static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache)
     BUG_ON("unimplemented");
 }
 
+/* Write a pagetable entry. */
+static inline void write_pte(pte_t *p, pte_t pte)
+{
+    RISCV_FENCE(rw, rw);
+    *p = pte;
+    RISCV_FENCE(rw, rw);
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_RISCV_PAGE_H */
xen/arch/riscv/mm.c +47 -0

65 lines not on this page · raw message

xen/arch/riscv/setup.c +2 -0
index 4defad68f4..13f0e8c77d 100644
@@ -46,6 +46,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
     test_macros_from_bug_h();
 #endif
 
+    setup_fixmap_mappings();
+
     printk("All set up\n");
 
     for ( ;; )
xen/arch/riscv/xen.lds.S +1 -1
index 070b19d915..7a683f6065 100644
@@ -181,6 +181,6 @@ 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")
+ASSERT(_end - _start <= XEN_VIRT_SIZE, "Xen too large for early-boot assumptions")
 
 ASSERT(_ident_end - _ident_start <= IDENT_AREA_SIZE, "identity region is too big");
Signature · 1 line
2.45.2
#3 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 8 lines · Oleksii Kurochko

Introduce a function to set up fixmap mappings and L0 page table for fixmap.

Additionally, defines were introduced in riscv/config.h to calculate the FIXMAP_BASE address.
This involved introducing BOOT_FDT_VIRT_{START, SIZE} and XEN_VIRT_SIZE, XEN_VIRT_END.

Quoted 4 lines

Also, the check of Xen size was updated in the riscv/lds.S script to use XEN_VIRT_SIZE instead of a hardcoded constant.

Yet set_fixmap() isn't introduced here, so effectively it's all dead code. This could do with mentioning, as I at least would expect set_fixmap() to be usable once fixmaps are properly set up.

Quoted 7 lines
xen/arch/riscv/include/asm/config.h +1 -0
@@ -66,6 +66,14 @@
 #define SLOTN_ENTRY_BITS        (HYP_PT_ROOT_LEVEL * VPN_BITS + PAGE_SHIFT)
 #define SLOTN(slot)             (_AT(vaddr_t, slot) << SLOTN_ENTRY_BITS)
 
+#define XEN_VIRT_SIZE           MB(2)

xen/arch/riscv/include/asm/config.h line 79

Quoted 10 lines
xen/arch/riscv/include/asm/config.h +7 -0
+
+#define BOOT_FDT_VIRT_START     (XEN_VIRT_START + XEN_VIRT_SIZE)
+#define BOOT_FDT_VIRT_SIZE      MB(4)
+
+#define FIXMAP_BASE             (BOOT_FDT_VIRT_START + BOOT_FDT_VIRT_SIZE)
+#define FIXMAP_ADDR(n)          (FIXMAP_BASE + (n) * PAGE_SIZE)
+
 #if RV_STAGE1_MODE == SATP_MODE_SV39
 #define XEN_VIRT_START 0xFFFFFFFFC0000000
 #elif RV_STAGE1_MODE == SATP_MODE_SV48

Related to my earlier comment: Is there a particular reason that what you add cannot live _below_ the XEN_VIRT_START definitions, so things actually appear in order?

Quoted 15 lines
xen/arch/riscv/include/asm/fixmap.h new file +12 -0
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * fixmap.h: compile-time virtual memory allocation
+ */
+#ifndef ASM_FIXMAP_H
+#define ASM_FIXMAP_H
+
+#include <xen/bug.h>
+#include <xen/page-size.h>
+#include <xen/pmap.h>
+
+#include <asm/page.h>

xen/arch/riscv/include/asm/fixmap.h line 22

Quoted 10 lines
xen/arch/riscv/include/asm/fixmap.h new file +10 -0
+
+/* Fixmap slots */
+#define FIX_PMAP_BEGIN (0) /* Start of PMAP */
+#define FIX_PMAP_END (FIX_PMAP_BEGIN + NUM_FIX_PMAP - 1) /* End of PMAP */
+#define FIX_MISC (FIX_PMAP_END + 1)  /* Ephemeral mappings of hardware */
+
+#define FIX_LAST FIX_MISC
+
+#define FIXADDR_START FIXMAP_ADDR(0)
+#define FIXADDR_TOP FIXMAP_ADDR(FIX_LAST)

This doesn't look right, even if it matches what Arm has. Following what we have for x86, the page at FIXADDR_TOP should be a guard page, i.e. not have any mapping ever put there. Whereas you put FIX_MISC's mapping there. This then results in ...

Quoted 4 lines
xen/arch/riscv/include/asm/fixmap.h new file +4 -0
+#ifndef __ASSEMBLY__
+
+/*
+ * Direct access to xen_fixmap[] should only happen when {set,

xen/arch/riscv/include/asm/fixmap.h line 37

Quoted 10 lines
xen/arch/riscv/include/asm/fixmap.h new file +10 -0
+ * clear}_fixmap() is unusable (e.g. where we would end up to
+ * recursively call the helpers).
+ */
+extern pte_t xen_fixmap[];
+
+#define fix_to_virt(slot) ((void *)FIXMAP_ADDR(slot))
+
+static inline unsigned int virt_to_fix(vaddr_t vaddr)
+{
+    BUG_ON(vaddr >= FIXADDR_TOP || vaddr < FIXADDR_START);

... this being wrong, i.e. triggering for FIX_MISC. Again, same issue on Arm afaics, just that it would trigger for FIX_PMAP_END there. (Cc-ing the other two Arm maintainers for that.)

xen/arch/riscv/include/asm/fixmap.h static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 91

Quoted 11 lines
xen/arch/riscv/include/asm/fixmap.h new file +7 -0
@@ -81,6 +82,14 @@ static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache)
     BUG_ON("unimplemented");
 }
 
+/* Write a pagetable entry. */
+static inline void write_pte(pte_t *p, pte_t pte)
+{
+    RISCV_FENCE(rw, rw);
+    *p = pte;
+    RISCV_FENCE(rw, rw);
+}

Why the first of the two fences? And isn't having the 2nd here going to badly affect batch updates of page tables?

Quoted 5 lines
xen/arch/riscv/include/asm/fixmap.h new file +1 -0
@@ -191,6 +195,49 @@ static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc,
     return is_mode_supported;
 }
 
+void __init setup_fixmap_mappings(void)

xen/arch/riscv/include/asm/fixmap.h static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 208

Quoted 10 lines
xen/arch/riscv/include/asm/fixmap.h new file +10 -0
+{
+    pte_t *pte, tmp;
+    unsigned int i;
+
+    BUILD_BUG_ON(FIX_LAST >= PAGETABLE_ENTRIES);
+
+    pte = &stage1_pgtbl_root[pt_index(HYP_PT_ROOT_LEVEL, FIXMAP_ADDR(0))];
+
+    /*
+     * In RISC-V page table levels are enumerated from Lx to L0 where

Nit: s/enumerated/numbered/ ?

xen/arch/riscv/include/asm/fixmap.h static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 218

Quoted 10 lines
xen/arch/riscv/include/asm/fixmap.h new file +10 -0
+     * x is the highest page table level for currect  MMU mode ( for example,
+     * for Sv39 has 3 page tables so the x = 2 (L2 -> L1 -> L0) ).
+     *
+     * In this cycle we want to find L1 page table because as L0 page table
+     * xen_fixmap[] will be used.
+     *
+     * i is defined ( HYP_PT_ROOT_LEVEL - 1 ) becuase pte for L2 ( in
+     * case of Sv39 ) has been recieved above.
+     */
+    for ( i = HYP_PT_ROOT_LEVEL - 1; i != 0; i-- )

I think the subtracting of 1 is unhelpful here. Think of another case where you want to walk down to L0. How would you express that with a similar for() construct. It would imo be more natural to use

    for ( i = HYP_PT_ROOT_LEVEL; i > 1; i-- )

here (and then in that hypothetical other case

    for ( i = HYP_PT_ROOT_LEVEL; i > 0; i-- )

), and then the last part of the comment likely wouldn't be needed either. However, considering ...

xen/arch/riscv/include/asm/fixmap.h static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 223

Quoted 5 lines
xen/arch/riscv/include/asm/fixmap.h new file +5 -0
+    {
+        BUG_ON(!pte_is_valid(*pte));
+
+        pte = (pte_t *)LOAD_TO_LINK(pte_to_paddr(*pte));
+        pte = &pte[pt_index(i, FIXMAP_ADDR(0))];

... the use of i here, it may instead want to be

    for ( i = HYP_PT_ROOT_LEVEL; i-- > 1; )

xen/arch/riscv/include/asm/fixmap.h static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 228

Quoted 5 lines
xen/arch/riscv/include/asm/fixmap.h new file +5 -0
+    }
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap), PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK() a little further up) here. Don't you have functioning __pa() and __va()?

xen/arch/riscv/include/asm/fixmap.h static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 233

Quoted 5 lines
xen/arch/riscv/include/asm/fixmap.h new file +5 -0
+    write_pte(pte, tmp);
+
+    sfence_vma();
+
+    printk("(XEN) fixmap is mapped\n");

Why the (XEN) prefix? And perhaps why the printk() in the first place?

Jan

#4 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 34 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko

Introduce a function to set up fixmap mappings and L0 page table for fixmap.

Additionally, defines were introduced in riscv/config.h to calculate the FIXMAP_BASE address.
This involved introducing BOOT_FDT_VIRT_{START, SIZE} and XEN_VIRT_SIZE, XEN_VIRT_END.

Quoted 4 lines

Also, the check of Xen size was updated in the riscv/lds.S script to use XEN_VIRT_SIZE instead of a hardcoded constant.

Yet set_fixmap() isn't introduced here, so effectively it's all dead code. This could do with mentioning, as I at least would expect set_fixmap() to be usable once fixmaps are properly set up.

Quoted 16 lines
xen/arch/riscv/include/asm/config.h +0 -0
@@ -66,6 +66,14 @@
  #define SLOTN_ENTRY_BITS        (HYP_PT_ROOT_LEVEL * VPN_BITS +
 PAGE_SHIFT)
  #define SLOTN(slot)             (_AT(vaddr_t, slot) <<
 SLOTN_ENTRY_BITS)

xen/arch/riscv/include/asm/config.h +6 -0
+#define XEN_VIRT_SIZE           MB(2)
+
+#define BOOT_FDT_VIRT_START     (XEN_VIRT_START + XEN_VIRT_SIZE)
+#define BOOT_FDT_VIRT_SIZE      MB(4)
+
+#define FIXMAP_BASE             (BOOT_FDT_VIRT_START +

BOOT_FDT_VIRT_SIZE)
+#define FIXMAP_ADDR(n)          (FIXMAP_BASE + (n) * PAGE_SIZE)

Quoted 8 lines
Quoted 4 lines

+
 #if RV_STAGE1_MODE == SATP_MODE_SV39
 #define XEN_VIRT_START 0xFFFFFFFFC0000000
 #elif RV_STAGE1_MODE == SATP_MODE_SV48

Related to my earlier comment: Is there a particular reason that what you add cannot live _below_ the XEN_VIRT_START definitions, so things actually appear in order?

It can leave _below_ the XEN_VIRT_START definitions, I just wanted to be in sync with table above.
But I'll move everything below the XEN_VIRT_START as it is used in newly introduced definitions.

Quoted 17 lines
Quoted 8 lines
xen/arch/riscv/include/asm/config.h +0 -0
@@ -81,6 +82,14 @@ static inline void flush_page_to_ram(unsigned
 long mfn, bool sync_icache)
      BUG_ON("unimplemented");
  }

xen/arch/riscv/include/asm/config.h

Quoted 7 lines
xen/arch/riscv/include/asm/config.h +7 -0
+/* Write a pagetable entry. */
+static inline void write_pte(pte_t *p, pte_t pte)
+{
+    RISCV_FENCE(rw, rw);
+    *p = pte;
+    RISCV_FENCE(rw, rw);
+}

Why the first of the two fences?

To ensure that writes have completed with the old mapping.

Quoted 2 lines

And isn't having the 2nd here going
to badly affect batch updates of page tables?

By batch you mean update more then one pte?
It yes, then it will definitely affect. It could be dropped here but could we do something to be sure that someone won't miss to add fence/barrier?

Quoted 15 lines
Quoted 14 lines

+     * x is the highest page table level for currect  MMU mode ( for example,
+     * for Sv39 has 3 page tables so the x = 2 (L2 -> L1 -> L0) ). +     *
+     * In this cycle we want to find L1 page table because as L0 page table
+     * xen_fixmap[] will be used.
+     *
+     * i is defined ( HYP_PT_ROOT_LEVEL - 1 ) becuase pte for L2 ( in
+     * case of Sv39 ) has been recieved above.
+     */
+    for ( i = HYP_PT_ROOT_LEVEL - 1; i != 0; i-- )

Quoted 7 lines

I think the subtracting of 1 is unhelpful here. Think of another case where
you want to walk down to L0. How would you express that with a similar for()
construct. It would imo be more natural to use

    for ( i = HYP_PT_ROOT_LEVEL; i > 1; i-- )

Then the first one _i_ will be initialized as L2, not L1. As an option we then have to use ...

Quoted 9 lines

here (and then in that hypothetical other case

    for ( i = HYP_PT_ROOT_LEVEL; i > 0; i-- )

), and then the last part of the comment likely wouldn't be needed either.
However, considering ...

Quoted 7 lines
Quoted 5 lines

+    {
+        BUG_ON(!pte_is_valid(*pte));
+
+        pte = (pte_t *)LOAD_TO_LINK(pte_to_paddr(*pte)); +        pte = &pte[pt_index(i, FIXMAP_ADDR(0))];

... the use of i here, it may instead want to be

... should be ( i - 1 ).
I am okay with such refactoring.

Quoted 14 lines

    for ( i = HYP_PT_ROOT_LEVEL; i-- > 1; )

Quoted 6 lines

+    }

xen/arch/riscv/include/asm/config.h +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap),

PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK() a
little further up) here. Don't you have functioning __pa() and __va()?

No, they haven't been introduced yet.

~ Oleksii

#5 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 16 lines · On 29.07.2024 18:11, [email protected] wrote:
Quoted 15 lines · Jan Beulich

xen/arch/riscv/include/asm/page.h static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 91

Quoted 12 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h +0 -0
@@ -81,6 +82,14 @@ static inline void flush_page_to_ram(unsigned
 long mfn, bool sync_icache)
      BUG_ON("unimplemented");
  }

xen/arch/riscv/include/asm/page.h +7 -0
+/* Write a pagetable entry. */
+static inline void write_pte(pte_t *p, pte_t pte)
+{
+    RISCV_FENCE(rw, rw);
+    *p = pte;
+    RISCV_FENCE(rw, rw);
+}

Why the first of the two fences?

Quoted 1 line

To ensure that writes have completed with the old mapping.

Wait: There can certainly be uncompleted writes, but those must have walked the page tables already, or else a (synchronous) fault could not be delivered on the originating store instruction. Or am I misunderstanding how paging (and associated faults) work on RISC-V?

Quoted 6 lines
Quoted 2 lines

And isn't having the 2nd here going
to badly affect batch updates of page tables?

By batch you mean update more then one pte?
It yes, then it will definitely affect. It could be dropped here but could we do something to be sure that someone won't miss to add fence/barrier?

Well, imo you first want to spell out where the responsibilities for fencing lies. Then follow the spelled out rules in all new code you add.

Quoted 38 lines
Quoted 14 lines
Quoted 5 lines

+     * x is the highest page table level for currect  MMU mode ( for example,
+     * for Sv39 has 3 page tables so the x = 2 (L2 -> L1 -> L0) ). +     *
+     * In this cycle we want to find L1 page table because as L0

Quoted 8 lines

page table
+     * xen_fixmap[] will be used.
+     *
+     * i is defined ( HYP_PT_ROOT_LEVEL - 1 ) becuase pte for L2 ( in
+     * case of Sv39 ) has been recieved above.
+     */
+    for ( i = HYP_PT_ROOT_LEVEL - 1; i != 0; i-- )

Quoted 7 lines

I think the subtracting of 1 is unhelpful here. Think of another case where
you want to walk down to L0. How would you express that with a similar for()
construct. It would imo be more natural to use

    for ( i = HYP_PT_ROOT_LEVEL; i > 1; i-- )

Then the first one _i_ will be initialized as L2, not L1. As an option we then have to use ...

Quoted 15 lines

here (and then in that hypothetical other case

    for ( i = HYP_PT_ROOT_LEVEL; i > 0; i-- )

), and then the last part of the comment likely wouldn't be needed either.
However, considering ...

Quoted 5 lines

+    {
+        BUG_ON(!pte_is_valid(*pte));
+
+        pte = (pte_t *)LOAD_TO_LINK(pte_to_paddr(*pte)); +        pte = &pte[pt_index(i, FIXMAP_ADDR(0))];

Quoted 3 lines
Quoted 1 line

... the use of i here, it may instead want to be

... should be ( i - 1 ).
I am okay with such refactoring.

Well, because of this use of i I specifically suggested ...

Quoted 1 line
Quoted 1 line

    for ( i = HYP_PT_ROOT_LEVEL; i-- > 1; )

... this.

Quoted 11 lines
Quoted 11 lines
Quoted 6 lines

+    }

xen/arch/riscv/mm.c +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap),

PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK() a
little further up) here. Don't you have functioning __pa() and __va()?

Quoted 1 line

No, they haven't been introduced yet.

So you're building up more technical debt, as the use of said two constructs really should be limited to very early setup. Aiui once you have functioning __va() / __pa() the code here would want changing?

Jan

#6 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 18 lines · Jan Beulich
Quoted 17 lines · On 29.07.2024 18:11, [email protected] wrote:
Quoted 16 lines · Jan Beulich

xen/arch/riscv/include/asm/page.h static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 91

Quoted 13 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -81,6 +82,14 @@ static inline void

flush_page_to_ram(unsigned
long mfn, bool sync_icache)
     BUG_ON("unimplemented");
 }

xen/arch/riscv/include/asm/page.h +7 -0
+/* Write a pagetable entry. */
+static inline void write_pte(pte_t *p, pte_t pte)
+{
+    RISCV_FENCE(rw, rw);
+    *p = pte;
+    RISCV_FENCE(rw, rw);
+}

Why the first of the two fences?

Quoted 6 lines
Quoted 1 line

To ensure that writes have completed with the old mapping.

Wait: There can certainly be uncompleted writes, but those must have walked the page tables already, or else a (synchronous) fault could not be delivered on the originating store instruction. Or am I misunderstanding how paging (and associated faults) work on RISC-V?

I am not sure that I correctly understand the part regarding ( synchronous ) fault. Could you please come up with an example?

If something during page table walk will go wrong then a fault will be raised.

My initial intension was to be sure if I will be writing to an actively in-use page table that other cores can see at the time then fences above are required. It is not the case for now as we have only one CPUs but I assume that it will be a case when SMP will be enabled and more then one CPU will be able to work with the same page table.

Quoted 10 lines
Quoted 6 lines
Quoted 1 line

And isn't having the 2nd here going

to badly affect batch updates of page tables?
By batch you mean update more then one pte?
It yes, then it will definitely affect. It could be dropped here but could we do something to be sure that someone won't miss to add fence/barrier?

Well, imo you first want to spell out where the responsibilities for fencing lies. Then follow the spelled out rules in all new code you add.

It makes sense. It would we nice if someone can help me with that.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 8 lines

+    }

xen/arch/riscv/mm.c +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned

long)&xen_fixmap),
PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK()
a
little further up) here. Don't you have functioning __pa() and __va()?

Quoted 6 lines
Quoted 1 line

No, they haven't been introduced yet.

So you're building up more technical debt, as the use of said two constructs really should be limited to very early setup. Aiui once you have functioning __va() / __pa() the code here would want changing?

Ideally yes, it would want to changed.

Would it be the better solution to define __va() and __pa() using LOAD_TO_LINK()/LINK_TO_LOAD() so when "real" __va() and __pa() will be ready so only definitions of __va() and __pa() should be changed.

~ Oleksii

#7 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 25 lines · On 30.07.2024 13:25, [email protected] wrote:
Quoted 18 lines · Jan Beulich
Quoted 17 lines · On 29.07.2024 18:11, [email protected] wrote:
Quoted 16 lines · Jan Beulich

xen/arch/riscv/include/asm/page.h static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 91

Quoted 13 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -81,6 +82,14 @@ static inline void

flush_page_to_ram(unsigned
long mfn, bool sync_icache)
     BUG_ON("unimplemented");
 }

xen/arch/riscv/include/asm/page.h +7 -0
+/* Write a pagetable entry. */
+static inline void write_pte(pte_t *p, pte_t pte)
+{
+    RISCV_FENCE(rw, rw);
+    *p = pte;
+    RISCV_FENCE(rw, rw);
+}

Why the first of the two fences?

Quoted 6 lines
Quoted 1 line

To ensure that writes have completed with the old mapping.

Wait: There can certainly be uncompleted writes, but those must have walked the page tables already, or else a (synchronous) fault could not be delivered on the originating store instruction. Or am I misunderstanding how paging (and associated faults) work on RISC-V?

Quoted 5 lines

I am not sure that I correctly understand the part regarding ( synchronous ) fault. Could you please come up with an example?

If something during page table walk will go wrong then a fault will be raised.

On the very insn, with subsequent insns not having started executing (from an architectural perspective, i.e. leaving aside speculation). That is what my use of "synchronous" meant.

Quoted 5 lines

My initial intension was to be sure if I will be writing to an actively in-use page table that other cores can see at the time then fences above are required. It is not the case for now as we have only one CPUs but I assume that it will be a case when SMP will be enabled and more then one CPU will be able to work with the same page table.

Would that first fence really help there? The other CPU could use the page tables in the window between the fence and the write. My understanding of the need for fences is for them to be used at times where ordering of memory accesses matters. For the moment I don't see this as an aspect for the 1st fence here, but I may be overlooking something.

Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned
long)&xen_fixmap),
PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK()
a
little further up) here. Don't you have functioning __pa() and __va()?

Quoted 6 lines
Quoted 1 line

No, they haven't been introduced yet.

So you're building up more technical debt, as the use of said two constructs really should be limited to very early setup. Aiui once you have functioning __va() / __pa() the code here would want changing?

Quoted 5 lines

Ideally yes, it would want to changed.

Would it be the better solution to define __va() and __pa() using LOAD_TO_LINK()/LINK_TO_LOAD() so when "real" __va() and __pa() will be ready so only definitions of __va() and __pa() should be changed.

Well, that's something you're in a better position to answer, as it depends on the ordering of subsequent work of yours.

Jan

#8 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 49 lines · Jan Beulich
Quoted 28 lines · On 30.07.2024 13:25, [email protected] wrote:
Quoted 20 lines · Jan Beulich
Quoted 17 lines · On 29.07.2024 18:11, [email protected] wrote:
Quoted 16 lines · Jan Beulich

xen/arch/riscv/include/asm/page.h static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) · line 91

Quoted 13 lines · Oleksii Kurochko
(quoted diff) +0 -0
@@ -81,6 +82,14 @@ static inline void

flush_page_to_ram(unsigned
long mfn, bool sync_icache)
     BUG_ON("unimplemented");
 }

xen/arch/riscv/include/asm/page.h +7 -0
+/* Write a pagetable entry. */
+static inline void write_pte(pte_t *p, pte_t pte)
+{
+    RISCV_FENCE(rw, rw);
+    *p = pte;
+    RISCV_FENCE(rw, rw);
+}

Why the first of the two fences?

Quoted 1 line

To ensure that writes have completed with the old mapping.

Quoted 7 lines

Wait: There can certainly be uncompleted writes, but those must have
walked the page tables already, or else a (synchronous) fault could
not be delivered on the originating store instruction. Or am I misunderstanding how paging (and associated faults) work on RISC- V?

Quoted 6 lines

I am not sure that I correctly understand the part regarding ( synchronous ) fault. Could you please come up with an example?

If something during page table walk will go wrong then a fault will be
raised.

On the very insn, with subsequent insns not having started executing (from an architectural perspective, i.e. leaving aside speculation). That is what my use of "synchronous" meant.

Quoted 8 lines

My initial intension was to be sure if I will be writing to an actively
in-use page table that other cores can see at the time then fences above are required. It is not the case for now as we have only one CPUs
but I assume that it will be a case when SMP will be enabled and more
then one CPU will be able to work with the same page table.

Quoted 6 lines

Would that first fence really help there? The other CPU could use the page tables in the window between the fence and the write. My understanding of the need for fences is for them to be used at times where ordering of memory accesses matters. For the moment I don't see this as an aspect for the 1st fence here, but I may be overlooking something.

AFAIU there are cases when we need the first fence, i.e. when superpage breakup is happening and IIUC in this case we would first do a fence ( assuming pages are already allocated ), one fence between each superpage breakup and its PTE, and one after each regular PTE write. But in this case the first fence could be outside write_pte() function.

So we could eliminate the first fence in write_pte() and place it outside the function ( if it will be needed for some cases ), thus eliminating double fences.

~ Oleksii

#9 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 6 lines

+    }

xen/arch/riscv/mm.c +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap),

PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK() a
little further up) here. Don't you have functioning __pa() and __va()?

Can __pa() and __va() be used in this case?

According to comments for other architectures, these macros are used for converting between Xen heap virtual addresses (VA) and machine addresses (MA). I may have misunderstood what is meant by the Xen heap in this context, but I'm not sure if xen_fixmap[] and page tables are considered part of the Xen heap.

~ Oleksii

#10 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 13 lines · On Mon, 2024-08-05 at 17:13 +0200, [email protected] wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines

+    }

xen/arch/riscv/mm.c +3 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
Quoted 8 lines
Quoted 2 lines

+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap), PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK()
a
little further up) here. Don't you have functioning __pa() and __va()?

Quoted 8 lines

Can __pa() and __va() be used in this case?

According to comments for other architectures, these macros are used for converting between Xen heap virtual addresses (VA) and machine addresses (MA). I may have misunderstood what is meant by the Xen heap
in this context, but I'm not sure if xen_fixmap[] and page tables are considered part of the Xen heap.

One more thing: can we use __pa() and __va() in setup_fixmap()?

As I understand it, to define __pa() and __va(), the DIRECTMAP mapping should be mapped first. However, this requires information about RAM banks, which is provided in the device tree file. But we can't map device tree without fixmap. Am I missing something?

~ Oleksii

#11 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 23 lines · On 05.08.2024 17:34, [email protected] wrote:
Quoted 13 lines · On Mon, 2024-08-05 at 17:13 +0200, [email protected] wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines

+    }

xen/arch/riscv/mm.c +3 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
Quoted 8 lines
Quoted 2 lines

+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap), PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK()
a
little further up) here. Don't you have functioning __pa() and __va()?

Quoted 8 lines

Can __pa() and __va() be used in this case?

According to comments for other architectures, these macros are used for converting between Xen heap virtual addresses (VA) and machine addresses (MA). I may have misunderstood what is meant by the Xen heap
in this context, but I'm not sure if xen_fixmap[] and page tables are considered part of the Xen heap.

Quoted 6 lines

One more thing: can we use __pa() and __va() in setup_fixmap()?

As I understand it, to define __pa() and __va(), the DIRECTMAP mapping should be mapped first. However, this requires information about RAM banks, which is provided in the device tree file. But we can't map device tree without fixmap. Am I missing something?

Depends on further plans. In principle VA <-> PA translation within the directmap range and within the Xen image range is purely arithmetic, not requiring any mappings to have been established. You don't access any of the referenced memory in the course of doing the translation, after all.

Jan

#12 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

In reply to [email protected]
Quoted 12 lines · On 05.08.2024 17:13, [email protected] wrote:
Quoted 11 lines · Jan Beulich
Quoted 6 lines

+    }

xen/arch/riscv/mm.c +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap),

PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK() a
little further up) here. Don't you have functioning __pa() and __va()?

Quoted 7 lines

Can __pa() and __va() be used in this case?

According to comments for other architectures, these macros are used for converting between Xen heap virtual addresses (VA) and machine addresses (MA). I may have misunderstood what is meant by the Xen heap in this context, but I'm not sure if xen_fixmap[] and page tables are considered part of the Xen heap.

I didn't check Arm, but on x86 virt_to_maddr() (underlying __pa()) has special case code to also allow addresses within the Xen image (area).

Jan

#13 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 26 lines · Jan Beulich
Quoted 16 lines · On 05.08.2024 17:13, [email protected] wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines

+    }

xen/arch/riscv/mm.c +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned
Quoted 8 lines
Quoted 2 lines

long)&xen_fixmap),
PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK()
a
little further up) here. Don't you have functioning __pa() and __va()?

Can __pa() and __va() be used in this case?

Quoted 8 lines

According to comments for other architectures, these macros are used
for converting between Xen heap virtual addresses (VA) and machine addresses (MA). I may have misunderstood what is meant by the Xen heap
in this context, but I'm not sure if xen_fixmap[] and page tables are
considered part of the Xen heap.

Quoted 4 lines

I didn't check Arm, but on x86 virt_to_maddr() (underlying __pa()) has
special case code to also allow addresses within the Xen image (area).

Yes, it is true for __virt_to_maddr:
static inline unsigned long __virt_to_maddr(unsigned long va)

   {
       ASSERT(va < DIRECTMAP_VIRT_END);
       if ( va >= DIRECTMAP_VIRT_START )
           va -= DIRECTMAP_VIRT_START;
       else
       {
           BUILD_BUG_ON(XEN_VIRT_END - XEN_VIRT_START != GB(1));
           /* Signed, so ((long)XEN_VIRT_START >> 30) fits in an imm32. */
           ASSERT(((long)va >> (PAGE_ORDER_1G + PAGE_SHIFT)) ==
                  ((long)XEN_VIRT_START >> (PAGE_ORDER_1G + PAGE_SHIFT)));
   
           va += xen_phys_start - XEN_VIRT_START;
       }
       return (va & ma_va_bottom_mask) |
              ((va << pfn_pdx_hole_shift) & ma_top_mask);

}

But in case of __maddr_to_virt ( __va() ) it is using directmap region:
static inline void *__maddr_to_virt(unsigned long ma)
{
ASSERT(pfn_to_pdx(ma >> PAGE_SHIFT) < (DIRECTMAP_SIZE >>

   PAGE_SHIFT));
       return (void *)(DIRECTMAP_VIRT_START +
                       ((ma & ma_va_bottom_mask) |
                        ((ma & ma_top_mask) >> pfn_pdx_hole_shift)));

}

But I have to use both __va() and __pa().
__va() inside cycle to find L1 page table:

    for ( i = HYP_PT_ROOT_LEVEL; i-- > 1; )
    {
        BUG_ON(!pte_is_valid(*pte));

        pte = (pte_t *)LOAD_TO_LINK(pte_to_paddr(*pte));
        pte = &pte[pt_index(i, FIXMAP_ADDR(0))];
    }

__pa() to set a physical address of L0 page table:
tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap),

PTE_TABLE);
    write_pte(pte, tmp);

~ Oleksii

#14 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 72 lines · On 05.08.2024 18:02, [email protected] wrote:
Quoted 26 lines · Jan Beulich
Quoted 16 lines · On 05.08.2024 17:13, [email protected] wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines

+    }

xen/arch/riscv/mm.c +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned
Quoted 8 lines
Quoted 2 lines

long)&xen_fixmap),
PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK()
a
little further up) here. Don't you have functioning __pa() and __va()?

Can __pa() and __va() be used in this case?

Quoted 8 lines

According to comments for other architectures, these macros are used
for converting between Xen heap virtual addresses (VA) and machine addresses (MA). I may have misunderstood what is meant by the Xen heap
in this context, but I'm not sure if xen_fixmap[] and page tables are
considered part of the Xen heap.

Quoted 4 lines

I didn't check Arm, but on x86 virt_to_maddr() (underlying __pa()) has
special case code to also allow addresses within the Xen image (area).

Yes, it is true for __virt_to_maddr:

   static inline unsigned long __virt_to_maddr(unsigned long va)
   {
       ASSERT(va < DIRECTMAP_VIRT_END);
       if ( va >= DIRECTMAP_VIRT_START )
           va -= DIRECTMAP_VIRT_START;
       else
       {
           BUILD_BUG_ON(XEN_VIRT_END - XEN_VIRT_START != GB(1));
           /* Signed, so ((long)XEN_VIRT_START >> 30) fits in an imm32. */
           ASSERT(((long)va >> (PAGE_ORDER_1G + PAGE_SHIFT)) ==
                  ((long)XEN_VIRT_START >> (PAGE_ORDER_1G + PAGE_SHIFT)));
   
           va += xen_phys_start - XEN_VIRT_START;
       }
       return (va & ma_va_bottom_mask) |
              ((va << pfn_pdx_hole_shift) & ma_top_mask);
   }

But in case of __maddr_to_virt ( __va() ) it is using directmap region:

   static inline void *__maddr_to_virt(unsigned long ma)
   {
       ASSERT(pfn_to_pdx(ma >> PAGE_SHIFT) < (DIRECTMAP_SIZE >>
   PAGE_SHIFT));
       return (void *)(DIRECTMAP_VIRT_START +
                       ((ma & ma_va_bottom_mask) |
                        ((ma & ma_top_mask) >> pfn_pdx_hole_shift)));
   }

But I have to use both __va() and __pa().
__va() inside cycle to find L1 page table:

    for ( i = HYP_PT_ROOT_LEVEL; i-- > 1; )
    {
        BUG_ON(!pte_is_valid(*pte));

        pte = (pte_t *)LOAD_TO_LINK(pte_to_paddr(*pte));
        pte = &pte[pt_index(i, FIXMAP_ADDR(0))];
    }
Quoted 4 lines

__pa() to set a physical address of L0 page table:
tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap),

PTE_TABLE);
    write_pte(pte, tmp);

Hmm, then using at least LINK_TO_LOAD() is going to be unavoidable for the time being, I guess. Yet midterm I think they should disappear from here.

Jan

#15 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 87 lines · Jan Beulich
Quoted 77 lines · On 05.08.2024 18:02, [email protected] wrote:
Quoted 30 lines · Jan Beulich
Quoted 17 lines · On 05.08.2024 17:13, [email protected] wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines

+    }

xen/arch/riscv/mm.c +4 -0
+
+    BUG_ON(pte_is_valid(*pte));
+
+    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned

long)&xen_fixmap),

Quoted 8 lines
Quoted 1 line

PTE_TABLE);

I'm a little puzzled by the use of LINK_TO_LOAD() (and LOAD_TO_LINK()
a
little further up) here. Don't you have functioning __pa() and
__va()?

Can __pa() and __va() be used in this case?

Quoted 11 lines

According to comments for other architectures, these macros are used
for converting between Xen heap virtual addresses (VA) and machine
addresses (MA). I may have misunderstood what is meant by the Xen
heap
in this context, but I'm not sure if xen_fixmap[] and page tables
are
considered part of the Xen heap.

Quoted 5 lines

I didn't check Arm, but on x86 virt_to_maddr() (underlying __pa())
has
special case code to also allow addresses within the Xen image (area).

Yes, it is true for __virt_to_maddr:
   static inline unsigned long __virt_to_maddr(unsigned long va) {
       ASSERT(va < DIRECTMAP_VIRT_END);
       if ( va >= DIRECTMAP_VIRT_START )
           va -= DIRECTMAP_VIRT_START;
       else
       {
           BUILD_BUG_ON(XEN_VIRT_END - XEN_VIRT_START != GB(1)); /* Signed, so ((long)XEN_VIRT_START >> 30) fits in an imm32. */
           ASSERT(((long)va >> (PAGE_ORDER_1G + PAGE_SHIFT)) == ((long)XEN_VIRT_START >> (PAGE_ORDER_1G + PAGE_SHIFT)));

           va += xen_phys_start - XEN_VIRT_START;
       }
       return (va & ma_va_bottom_mask) |
              ((va << pfn_pdx_hole_shift) & ma_top_mask); }

But in case of __maddr_to_virt ( __va() ) it is using directmap region:
   static inline void *__maddr_to_virt(unsigned long ma) {
       ASSERT(pfn_to_pdx(ma >> PAGE_SHIFT) < (DIRECTMAP_SIZE >> PAGE_SHIFT));
       return (void *)(DIRECTMAP_VIRT_START +
                       ((ma & ma_va_bottom_mask) | ((ma & ma_top_mask) >> pfn_pdx_hole_shift)));
   }

But I have to use both __va() and __pa().
__va() inside cycle to find L1 page table:

    for ( i = HYP_PT_ROOT_LEVEL; i-- > 1; )
    {
        BUG_ON(!pte_is_valid(*pte));

Quoted 8 lines

        pte = (pte_t *)LOAD_TO_LINK(pte_to_paddr(*pte)); pte = &pte[pt_index(i, FIXMAP_ADDR(0))]; }

__pa() to set a physical address of L0 page table:
    tmp = paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap), PTE_TABLE);
    write_pte(pte, tmp);

Quoted 4 lines

Hmm, then using at least LINK_TO_LOAD() is going to be unavoidable for the
time being, I guess. Yet midterm I think they should disappear from here.

I think that in this specific case both LINK_TO_LOAD() and LOAD_TO_LINK() should be used.

__va() -> __maddr_to_virt() can't be used here as it's calculation is based on DIRECTMAP_VIRT_START thereby we will receive incorrect virtual address of page table.

__pa() -> __virt_to_maddr() can't be used too because virtual address of xen_fixmap will be always bigger then DIRECTMAP_VIRT_START ( as XEN_VIRT_START > DIRECTMAP_VIRT_START ) thereby physical address (PA) will be calculated based on DIRECTMAP_VIRT_START and incorrect PA of xen_fixmap will be received.

It seems to me that it is only one choice we have at the moment ( and probably in the case of fixmap mapping ) is to use LINK_TO_LOAD()/LOAD_TO_LINK().

~ Oleksii

#16 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

xen/arch/riscv/mm.c static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 233

Quoted 5 lines

+    write_pte(pte, tmp);

xen/arch/riscv/mm.c +4 -0
+
+    sfence_vma();
+
+    printk("(XEN) fixmap is mapped\n");

Why the (XEN) prefix? And perhaps why the printk() in the first place?

printk() is available after common code started to be buildable and can be used here as we have already introduced early_puts() which uses sbi_console_putchar(). So it doesn't matter if we use printk() or early_printk() here the result will be the same the call of sbi_console_putchar().

Am I missing something?

~ Oleksii

#17 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 9 lines · On 06.08.2024 11:49, [email protected] wrote:
Quoted 8 lines · Jan Beulich

xen/arch/riscv/mm.c static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 233

Quoted 5 lines

+    write_pte(pte, tmp);

xen/arch/riscv/mm.c +4 -0
+
+    sfence_vma();
+
+    printk("(XEN) fixmap is mapped\n");

Why the (XEN) prefix? And perhaps why the printk() in the first place?

Quoted 7 lines

printk() is available after common code started to be buildable and can be used here as we have already introduced early_puts() which uses sbi_console_putchar(). So it doesn't matter if we use printk() or early_printk() here the result will be the same the call of sbi_console_putchar().

Am I missing something?

Apparently yes, as you answered neither of my questions. To put them
differently: What value does this log message have, outside of your own development activities? What value does the explicit (XEN) have when printk() prepends such a prefix already anyway?

Jan

#18 lore raw

Re: [PATCH v3 4/9] xen/riscv: setup fixmap mapping

Quoted 17 lines · Jan Beulich
Quoted 16 lines · On 06.08.2024 11:49, [email protected] wrote:
Quoted 8 lines · Jan Beulich

xen/arch/riscv/mm.c static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc, · line 233

Quoted 5 lines

+    write_pte(pte, tmp);

xen/arch/riscv/mm.c +4 -0
+
+    sfence_vma();
+
+    printk("(XEN) fixmap is mapped\n");

Why the (XEN) prefix? And perhaps why the printk() in the first place?

printk() is available after common code started to be buildable and can
be used here as we have already introduced early_puts() which uses sbi_console_putchar(). So it doesn't matter if we use printk() or early_printk() here the result will be the same the call of sbi_console_putchar().

Quoted 5 lines
Quoted 1 line

Am I missing something?

Apparently yes, as you answered neither of my questions. To put them
differently: What value does this log message have, outside of your own development activities?

Probably there is no any value. Just show that mapping of fixmap has been finished. I think that we can really this printk.

Quoted 2 lines

What value does the explicit (XEN) have
when printk() prepends such a prefix already anyway?

I thought that I answered on this question before that there is no any sense in the explicit "(XEN)".

~ Oleksii

#19 lore raw

PATCH v7 1/9 xen/device-tree: Move Arm's setup.c bootinfo functions to common

In reply to Oleksii Kurochko

From: Shawn Anastasio <[email protected]>

Arm's setup.c contains a collection of functions for parsing memory map and other boot information from a device tree. Since these routines are generally useful on any architecture that supports device tree booting, move them into xen/common/device-tree.

Also, common/device_tree.c has been moved to the device-tree folder with the corresponding updates to common/Makefile and common/device-tree/Makefile.

---
Changes in V7:
- move obj-$(CONFIG_HAS_DEVICE_TREE) += device-tree/ to proper place in common/Makefile.
- rename macros __XEN_BOOTFDT_H__ to XEN_BOOTFDT_H to not violate MISRA rule 21.1.
- drop definition of "#define MAX_FDT_SIZE SZ_2M" in xen/bootfdt.h as it is expected to be arch-specific. Back "#define MAX_FDT_SIZE SZ_2M" to arm/setup.h where it was before these changes.
- git mv xen/common/device_tree.c xen/common/device-tree/ and update correspondingly Makefiles of common/ and common/device-tree
- update the commit message
---
Changes in V6:
- update the version of the patch to v6.
---
Changes in V5:
- add xen/include/xen/bootfdt.h to MAINTAINERS file.
- drop message "Early device tree parsing and".
- After rebase on top of the current staging the following changes were done:
- init bootinfo variable in <common/device-tree/bootinfo.c> with BOOTINFO_INIT;
- update the code of dt_unreserved_regions():
CONFIG_STATIC_SHM related changes and getting of reserved_mem bootinfo_get_shmem() ??
- update the code of meminfo_overlap_check():

       add check ( INVALID_PADDR == bank_start ) to if case.

- update the code of check_reserved_regions_overlap():
CONFIG_STATIC_SHM related changes.
- struct bootinfo was updated ( CONFIG_STATIC_SHM changes )
- add shared_meminfo ( because of CONFIG_STATIC_SHM )
- struct struct membanks was update with __struct group so <xen/kernel> is neeeded to be included in bootfdt.h
- move BOOTINFO_ACPI_INIT, BOOTINFO_SHMEM_INIT, BOOTINFO_INIT to generic bootfdt.h
- bootinfo_get_reserved_mem(), bootinfo_get_mem(), bootinfo_get_acpi(), bootinfo_get_shmem() and bootinfo_get_shmem_extra() were moved to xen/bootfdt.h
- s/arm32/CONFIG_SEPARATE_XENHEAP/
- add inclusion of <xen/macros.h> because there are function in <xen/bootfdt.h> which are using container_of().
---
Changes in v4:
- create new xen/include/bootinfo.h rather than relying on arch's asm/setup.h to provide required definitions for bootinfo.c
- build bootinfo.c as .init.o
- clean up and sort bootinfo.c's #includes
- use CONFIG_SEPARATE_XENHEAP rather than CONFIG_ARM_32 to guard xenheap-specific behavior of populate_boot_allocator
- (MAINTAINERS) include all of common/device-tree rather than just bootinfo.c

---
 MAINTAINERS                          |    2 +
 xen/arch/arm/include/asm/setup.h     |  185 +--
 xen/arch/arm/setup.c                 |  432 -----
 xen/common/Makefile                  |    2 +-
 xen/common/device-tree/Makefile      |    2 +
 xen/common/device-tree/bootinfo.c    |  459 ++++++
 xen/common/device-tree/device_tree.c | 2253 ++++++++++++++++++++++++++
 xen/common/device_tree.c             | 2253 --------------------------
 xen/include/xen/bootfdt.h            |  195 +++
 9 files changed, 2913 insertions(+), 2870 deletions(-)
 create mode 100644 xen/common/device-tree/Makefile
 create mode 100644 xen/common/device-tree/bootinfo.c
 create mode 100644 xen/common/device-tree/device_tree.c
 delete mode 100644 xen/common/device_tree.c
 create mode 100644 xen/include/xen/bootfdt.h
MAINTAINERS +2 -0
index 2b0c894527..505915b6b6 100644
@@ -295,9 +295,11 @@ M:	Stefano Stabellini <[email protected]>
 M:	Julien Grall <[email protected]>
 S:	Supported
 F:	xen/common/libfdt/
+F:	xen/common/device-tree/
 F:	xen/common/device_tree.c
 F:	xen/common/dt-overlay.c
 F:	xen/include/xen/libfdt/
+F:	xen/include/xen/bootfdt.h
 F:	xen/include/xen/device_tree.h
 F:	xen/drivers/passthrough/device_tree.c
 
xen/arch/arm/include/asm/setup.h +1 -184

215 lines not on this page · raw message

xen/arch/arm/setup.c +0 -432

456 lines not on this page · raw message

xen/common/Makefile +1 -1
index f12a474d40..7e66802a9e 100644
@@ -6,7 +6,7 @@ obj-$(CONFIG_HYPFS_CONFIG) += config_data.o
 obj-$(CONFIG_CORE_PARKING) += core_parking.o
 obj-y += cpu.o
 obj-$(CONFIG_DEBUG_TRACE) += debugtrace.o
-obj-$(CONFIG_HAS_DEVICE_TREE) += device_tree.o
+obj-$(CONFIG_HAS_DEVICE_TREE) += device-tree/
 obj-$(CONFIG_IOREQ_SERVER) += dm.o
 obj-y += domain.o
 obj-$(CONFIG_OVERLAY_DTB) += dt-overlay.o
xen/common/device-tree/Makefile new file +2 -0
new file mode 100644
index 0000000000..da892dd55d
@@ -0,0 +1,2 @@
+obj-y += bootinfo.init.o
+obj-y += device_tree.o
xen/common/device-tree/bootinfo.c new file +459 -0

459 lines not on this page · raw message

xen/common/device-tree/device_tree.c new file +2253 -0

1,849 lines not on this page · raw message

xen/common/device_tree.c deleted +0 -2253

2,253 lines not on this page · raw message

xen/include/xen/bootfdt.h new file +195 -0

195 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.45.2

Part of the diff is not shown here. View the raw message.

Acked-by on v7 1/9
#20 lore raw

Re: [PATCH v7 1/9] xen/device-tree: Move Arm's setup.c bootinfo functions to common

Hi,

Quoted 12 lines · Oleksii Kurochko

Changes in V5:
- add xen/include/xen/bootfdt.h to MAINTAINERS file.
- drop message "Early device tree parsing and".
- After rebase on top of the current staging the following changes were done:
- init bootinfo variable in <common/device-tree/bootinfo.c> with BOOTINFO_INIT;
- update the code of dt_unreserved_regions():
CONFIG_STATIC_SHM related changes and getting of reserved_mem bootinfo_get_shmem() ??
- update the code of meminfo_overlap_check():
add check ( INVALID_PADDR == bank_start ) to if case.
- update the code of check_reserved_regions_overlap():
CONFIG_STATIC_SHM related changes.

Quoted 8 lines

- struct bootinfo was updated ( CONFIG_STATIC_SHM changes )
- add shared_meminfo ( because of CONFIG_STATIC_SHM )
- struct struct membanks was update with __struct group so <xen/kernel> is neeeded to be included in bootfdt.h
- move BOOTINFO_ACPI_INIT, BOOTINFO_SHMEM_INIT, BOOTINFO_INIT to generic bootfdt.h
- bootinfo_get_reserved_mem(), bootinfo_get_mem(), bootinfo_get_acpi(), bootinfo_get_shmem() and bootinfo_get_shmem_extra() were moved to xen/bootfdt.h
- s/arm32/CONFIG_SEPARATE_XENHEAP/

I think this change deserve a comment in the commit message.

[...]

xen/common/device-tree/bootinfo.c line 3

Quoted 6 lines
xen/common/device-tree/bootinfo.c new file +3 -0
@@ -0,0 +1,459 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Derived from $xen/arch/arm/setup.c.

I would suggest to mention the version of Xen this is derived from.

Signature · 1 line
Julien Grall
#21 lore raw

Re: [PATCH v7 1/9] xen/device-tree: Move Arm's setup.c bootinfo functions to common

Hi Julien,

Quoted 31 lines · Julien Grall

Hi,

Quoted 19 lines · Oleksii Kurochko

Changes in V5:
  - add xen/include/xen/bootfdt.h to MAINTAINERS file.
  - drop message "Early device tree parsing and".
  - After rebase on top of the current staging the following changes were done:
    - init bootinfo variable in <common/device-tree/bootinfo.c> with BOOTINFO_INIT;
    - update the code of dt_unreserved_regions():
        CONFIG_STATIC_SHM related changes and getting of reserved_mem
        bootinfo_get_shmem() ??
    - update the code of meminfo_overlap_check():
        add check ( INVALID_PADDR == bank_start ) to if case.
    - update the code of check_reserved_regions_overlap():
        CONFIG_STATIC_SHM related changes.
    - struct bootinfo was updated ( CONFIG_STATIC_SHM changes )
    - add shared_meminfo ( because of CONFIG_STATIC_SHM )
    - struct struct membanks was update with __struct group so <xen/kernel> is

Quoted 8 lines

      neeeded to be included in bootfdt.h
    - move BOOTINFO_ACPI_INIT, BOOTINFO_SHMEM_INIT, BOOTINFO_INIT to generic bootfdt.h
    - bootinfo_get_reserved_mem(), bootinfo_get_mem(), bootinfo_get_acpi(),
      bootinfo_get_shmem() and bootinfo_get_shmem_extra() were moved to xen/bootfdt.h
  - s/arm32/CONFIG_SEPARATE_XENHEAP/

Quoted 1 line

I think this change deserve a comment in the commit message.

Sure, I will mention that ( IIUC only this "s/arm32/CONFIG_SEPARATE_XENHEAP/" should be mentioned ).

Quoted 11 lines

[...]

xen/common/device-tree/bootinfo.c line 3

Quoted 6 lines
xen/common/device-tree/bootinfo.c new file +3 -0
@@ -0,0 +1,459 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Derived from $xen/arch/arm/setup.c.

I would suggest to mention the version of Xen this is derived from.

Will it be enough only Xen version without commit hash?

Quoted 2 lines

Thanks!

~ Oleksii

#22 lore raw

Re: [PATCH v7 1/9] xen/device-tree: Move Arm's setup.c bootinfo functions to common

Quoted 11 lines · On 05/08/2024 10:44, [email protected] wrote:
Quoted 10 lines

[...]

xen/common/device-tree/bootinfo.c line 3

Quoted 6 lines
xen/common/device-tree/bootinfo.c new file +3 -0
@@ -0,0 +1,459 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Derived from $xen/arch/arm/setup.c.

I would suggest to mention the version of Xen this is derived from.

Will it be enough only Xen version without commit hash?

I think so, the code you moved has not changed recently since 4.19.

Cheers,

Signature · 1 line
Julien Grall
#23 lore raw

PATCH v3 5/9 xen/riscv: introduce asm/pmap.h header

In reply to Oleksii Kurochko

Introduces arch_pmap_{un}map functions and select HAS_PMAP for CONFIG_RISCV.

Additionaly it was necessary to introduce functions:
- mfn_from_xen_entry
- mfn_to_pte

Also flush_xen_tlb_range_va_local() and flush_xen_tlb_one_local() are introduced and use in arch_pmap_unmap().

---
Changes in V3:
- rename argument of function mfn_to_xen_entry(..., attr -> flags ).
- update the code of mfn_to_xen_entry() to use flags argument.
- add blank in mfn_from_pte() in return line.
- introduce flush_xen_tlb_range_va_local() and use it inside arch_pmap_{un}map().
- s/__ASM_PMAP_H__/ASM_PMAP_H
- add SPDX-License-Identifier: GPL-2.0

---
 xen/arch/riscv/Kconfig                |  1 +
 xen/arch/riscv/include/asm/flushtlb.h | 22 ++++++++++++++++++
 xen/arch/riscv/include/asm/page.h     |  2 ++
 xen/arch/riscv/include/asm/pmap.h     | 33 +++++++++++++++++++++++++++
 xen/arch/riscv/mm.c                   | 15 ++++++++++++
 5 files changed, 73 insertions(+)
 create mode 100644 xen/arch/riscv/include/asm/pmap.h
xen/arch/riscv/Kconfig +1 -0
index 259eea8d3b..0112aa8778 100644
@@ -3,6 +3,7 @@ config RISCV
 	select FUNCTION_ALIGNMENT_16B
 	select GENERIC_BUG_FRAME
 	select HAS_DEVICE_TREE
+	select HAS_PMAP
 
 config RISCV_64
 	def_bool y
xen/arch/riscv/include/asm/flushtlb.h +22 -0
index 7ce32bea0b..cf66e90773 100644
@@ -5,6 +5,28 @@
 #include <xen/bug.h>
 #include <xen/cpumask.h>
 
+/* Flush TLB of local processor for address va. */
+static inline void flush_xen_tlb_one_local(vaddr_t va)
+{
+    asm volatile ( "sfence.vma %0" :: "r" (va) : "memory" );
+}
+
+/*
+ * Flush a range of VA's hypervisor mappings from the TLB of the local
+ * processor.
+ */
+static inline void flush_xen_tlb_range_va_local(vaddr_t va,
+                                                unsigned long size)
+{
+    vaddr_t end = va + size;
+
+    while ( va < end )
+    {
+        flush_xen_tlb_one_local(va);
+        va += PAGE_SIZE;
+    }
+}
+
 /*
  * Filter the given set of CPUs, removing those that definitely flushed their
  * TLB since @page_timestamp.
xen/arch/riscv/include/asm/page.h +2 -0
index 0cc2f37cf8..2308cefb0a 100644
@@ -52,6 +52,8 @@ typedef struct {
 #endif
 } pte_t;
 
+pte_t mfn_to_xen_entry(mfn_t mfn, unsigned int access_bits);
+
 static inline pte_t paddr_to_pte(paddr_t paddr,
                                  unsigned int permissions)
 {
xen/arch/riscv/include/asm/pmap.h new file +33 -0
new file mode 100644
index 0000000000..068d0794b1
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_PMAP_H
+#define ASM_PMAP_H
+
+#include <xen/bug.h>
+#include <xen/mm.h>
+#include <xen/page-size.h>
+
+#include <asm/fixmap.h>
+#include <asm/flushtlb.h>
+#include <asm/system.h>
+
+static inline void arch_pmap_map(unsigned int slot, mfn_t mfn)
+{
+    pte_t *entry = &xen_fixmap[slot];
+    pte_t pte;
+
+    ASSERT(!pte_is_valid(*entry));
+
+    pte = mfn_to_xen_entry(mfn, PAGE_HYPERVISOR_RW);
+    write_pte(entry, pte);
+}
+
+static inline void arch_pmap_unmap(unsigned int slot)
+{
+    pte_t pte = {};
+
+    write_pte(&xen_fixmap[slot], pte);
+
+    flush_xen_tlb_range_va_local(FIXMAP_ADDR(slot), PAGE_SIZE);
+}
+
+#endif /* ASM_PMAP_H */
xen/arch/riscv/mm.c +15 -0
index 35724505ec..959b6fc63e 100644
@@ -382,3 +382,18 @@ int map_pages_to_xen(unsigned long virt,
     BUG_ON("unimplemented");
     return -1;
 }
+
+static inline pte_t mfn_from_pte(mfn_t mfn)
+{
+    unsigned long pte = mfn_x(mfn) << PTE_PPN_SHIFT;
+    return (pte_t){ .pte = pte };
+}
+
+inline pte_t mfn_to_xen_entry(mfn_t mfn, unsigned int access_bits)
+{
+    pte_t pte = mfn_from_pte(mfn);
+
+    pte.pte |= access_bits;
+
+    return pte;
+}
Signature · 1 line
2.45.2
#24 lore raw

Re: [PATCH v3 5/9] xen/riscv: introduce asm/pmap.h header

Quoted 9 lines · Oleksii Kurochko

Introduces arch_pmap_{un}map functions and select HAS_PMAP for CONFIG_RISCV.

Additionaly it was necessary to introduce functions:
- mfn_from_xen_entry
- mfn_to_pte

Also flush_xen_tlb_range_va_local() and flush_xen_tlb_one_local() are introduced and use in arch_pmap_unmap().

Just flush_xen_tlb_one_local() would suffice for the purposes here. flush_xen_tlb_range_va_local() will need some kind of cutoff, to avoid looping for an excessivly long time.

Quoted 15 lines
xen/arch/riscv/include/asm/pmap.h new file +12 -0
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_PMAP_H
+#define ASM_PMAP_H
+
+#include <xen/bug.h>
+#include <xen/mm.h>
+#include <xen/page-size.h>
+
+#include <asm/fixmap.h>
+#include <asm/flushtlb.h>
+#include <asm/system.h>
+

xen/arch/riscv/include/asm/pmap.h line 22

Quoted 10 lines
xen/arch/riscv/include/asm/pmap.h new file +10 -0
+static inline void arch_pmap_map(unsigned int slot, mfn_t mfn)
+{
+    pte_t *entry = &xen_fixmap[slot];
+    pte_t pte;
+
+    ASSERT(!pte_is_valid(*entry));
+
+    pte = mfn_to_xen_entry(mfn, PAGE_HYPERVISOR_RW);
+    write_pte(entry, pte);
+}

Perhaps add a comment to clarify why it's safe to omit a TLB flush here. Note that arch_pmap_unmap() having one is a necessary but not sufficient condition to that. In principle hardware may also cache "negative" TLB entries; I have no idea how RISC-V behaves in this regard, or whether that aspect is actually left to implementations.

xen/arch/riscv/include/asm/pmap.h line 31

Quoted 8 lines
xen/arch/riscv/include/asm/pmap.h new file +8 -0
+static inline void arch_pmap_unmap(unsigned int slot)
+{
+    pte_t pte = {};
+
+    write_pte(&xen_fixmap[slot], pte);
+
+    flush_xen_tlb_range_va_local(FIXMAP_ADDR(slot), PAGE_SIZE);
+}

For both functions, even if mainly for documentation purposes, it may be desirable to mark them __init. It's again a necessary (but not sufficient) condition here, to validly use local TLB flushes only.

xen/arch/riscv/mm.c int map_pages_to_xen(unsigned long virt, · line 390

Quoted 12 lines
xen/arch/riscv/mm.c +6 -0
@@ -382,3 +382,18 @@ int map_pages_to_xen(unsigned long virt,
     BUG_ON("unimplemented");
     return -1;
 }
+
+static inline pte_t mfn_from_pte(mfn_t mfn)
+{
+    unsigned long pte = mfn_x(mfn) << PTE_PPN_SHIFT;
+    return (pte_t){ .pte = pte };
+}

My earlier naming related comment may not have been here, but it was certainly meant to apply to any similar functions: A function of this name should imo take a pte_t and produce an mfn_t. IOW I'd expect this function to be pte_from_mfn(). However, I question it being a good idea to do it this way. The resulting pte_t isn't valid yet, aiui, as it still needs at least the access controls filled in. Such a partial pte_t would better not be floating around at any time, imo. IOW the function likely wants to take a 2nd parameter.

Jan

#25 lore raw

Re: [PATCH v3 5/9] xen/riscv: introduce asm/pmap.h header

Quoted 41 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko

Introduces arch_pmap_{un}map functions and select HAS_PMAP for CONFIG_RISCV.

Additionaly it was necessary to introduce functions:
 - mfn_from_xen_entry
 - mfn_to_pte

Also flush_xen_tlb_range_va_local() and flush_xen_tlb_one_local() are introduced and use in arch_pmap_unmap().

Just flush_xen_tlb_one_local() would suffice for the purposes here. flush_xen_tlb_range_va_local() will need some kind of cutoff, to avoid looping for an excessivly long time.

Quoted 15 lines
xen/arch/riscv/include/asm/pmap.h new file +12 -0
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_PMAP_H
+#define ASM_PMAP_H
+
+#include <xen/bug.h>
+#include <xen/mm.h>
+#include <xen/page-size.h>
+
+#include <asm/fixmap.h>
+#include <asm/flushtlb.h>
+#include <asm/system.h>
+

xen/arch/riscv/include/asm/pmap.h line 22

Quoted 10 lines
xen/arch/riscv/include/asm/pmap.h new file +10 -0
+static inline void arch_pmap_map(unsigned int slot, mfn_t mfn)
+{
+    pte_t *entry = &xen_fixmap[slot];
+    pte_t pte;
+
+    ASSERT(!pte_is_valid(*entry));
+
+    pte = mfn_to_xen_entry(mfn, PAGE_HYPERVISOR_RW);
+    write_pte(entry, pte);
+}
Quoted 8 lines

Perhaps add a comment to clarify why it's safe to omit a TLB flush here.
Note that arch_pmap_unmap() having one is a necessary but not sufficient
condition to that. In principle hardware may also cache "negative" TLB
entries; I have no idea how RISC-V behaves in this regard, or whether that aspect is actually left to implementations.

what do you mean by "negative" TLB? an old TLB entry which should be invalidated?

Quoted 13 lines

xen/arch/riscv/include/asm/pmap.h line 31

Quoted 9 lines
xen/arch/riscv/include/asm/pmap.h new file +8 -0
+static inline void arch_pmap_unmap(unsigned int slot)
+{
+    pte_t pte = {};
+
+    write_pte(&xen_fixmap[slot], pte);
+
+    flush_xen_tlb_range_va_local(FIXMAP_ADDR(slot), PAGE_SIZE);
+}

For both functions, even if mainly for documentation purposes, it may be desirable to mark them __init. It's again a necessary (but not sufficient) condition here, to validly use local TLB flushes only.

Does __init in this context means that it will be called only by boot cpu at the start and that is it?

Quoted 17 lines

xen/arch/riscv/mm.c int map_pages_to_xen(unsigned long virt, · line 390

Quoted 13 lines
xen/arch/riscv/mm.c +6 -0
@@ -382,3 +382,18 @@ int map_pages_to_xen(unsigned long virt,
      BUG_ON("unimplemented");
      return -1;
  }
+
+static inline pte_t mfn_from_pte(mfn_t mfn)
+{
+    unsigned long pte = mfn_x(mfn) << PTE_PPN_SHIFT;
+    return (pte_t){ .pte = pte };
+}

My earlier naming related comment may not have been here, but it was certainly meant to apply to any similar functions: A function of this name should imo take a pte_t and produce an mfn_t. IOW I'd

Quoted 8 lines

expect this function to be pte_from_mfn(). However, I question it being
a good idea to do it this way. The resulting pte_t isn't valid yet, aiui,
as it still needs at least the access controls filled in. Such a partial
pte_t would better not be floating around at any time, imo. IOW the function likely wants to take a 2nd parameter.

Thanks. I'll double check the namings and update the prototype of this function.

~ Oleksii

#26 lore raw

Re: [PATCH v3 5/9] xen/riscv: introduce asm/pmap.h header

Quoted 36 lines · On 29.07.2024 18:22, [email protected] wrote:
Quoted 27 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/pmap.h new file +12 -0
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_PMAP_H
+#define ASM_PMAP_H
+
+#include <xen/bug.h>
+#include <xen/mm.h>
+#include <xen/page-size.h>
+
+#include <asm/fixmap.h>
+#include <asm/flushtlb.h>
+#include <asm/system.h>
+

xen/arch/riscv/include/asm/pmap.h line 22

Quoted 10 lines
xen/arch/riscv/include/asm/pmap.h new file +10 -0
+static inline void arch_pmap_map(unsigned int slot, mfn_t mfn)
+{
+    pte_t *entry = &xen_fixmap[slot];
+    pte_t pte;
+
+    ASSERT(!pte_is_valid(*entry));
+
+    pte = mfn_to_xen_entry(mfn, PAGE_HYPERVISOR_RW);
+    write_pte(entry, pte);
+}
Quoted 8 lines

Perhaps add a comment to clarify why it's safe to omit a TLB flush here.
Note that arch_pmap_unmap() having one is a necessary but not sufficient
condition to that. In principle hardware may also cache "negative" TLB
entries; I have no idea how RISC-V behaves in this regard, or whether that aspect is actually left to implementations.

Quoted 2 lines

what do you mean by "negative" TLB? an old TLB entry which should be invalidated?

No, I mean TLB entries saying "no valid translation here". I.e. avoiding recurring walks of something that was once found to have no translation.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/riscv/include/asm/pmap.h new file +4 -0
+static inline void arch_pmap_unmap(unsigned int slot)
+{
+    pte_t pte = {};
+
Quoted 8 lines

xen/arch/riscv/include/asm/pmap.h line 31

Quoted 4 lines
xen/arch/riscv/include/asm/pmap.h new file +4 -0
+    write_pte(&xen_fixmap[slot], pte);
+
+    flush_xen_tlb_range_va_local(FIXMAP_ADDR(slot), PAGE_SIZE);
+}

For both functions, even if mainly for documentation purposes, it may be desirable to mark them __init. It's again a necessary (but not sufficient) condition here, to validly use local TLB flushes only.

Quoted 2 lines

Does __init in this context means that it will be called only by boot cpu at the start and that is it?

No, and I said so in my reply. __init is indicative of the function not being suitable for runtime use, but it is not enough to indicate the function is also unsuitable for use as soon as a 2nd CPU is being brought up. Yet for the latter we have no proper annotation. Hence my suggestion to use the former to have at least a limited indicator.

An alternative might be to add ASSERT(system_state < SYS_STATE_smp_boot). That, however, exists in common/pmap.c already anyway.

Yet another alternative would be a clarifying comment.

Jan

#27 lore raw

Re: [PATCH v3 5/9] xen/riscv: introduce asm/pmap.h header

Quoted 40 lines · Jan Beulich
Quoted 39 lines · On 29.07.2024 18:22, [email protected] wrote:
Quoted 38 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/pmap.h new file +12 -0
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_PMAP_H
+#define ASM_PMAP_H
+
+#include <xen/bug.h>
+#include <xen/mm.h>
+#include <xen/page-size.h>
+
+#include <asm/fixmap.h>
+#include <asm/flushtlb.h>
+#include <asm/system.h>
+

xen/arch/riscv/include/asm/pmap.h line 22

Quoted 10 lines
xen/arch/riscv/include/asm/pmap.h new file +10 -0
+static inline void arch_pmap_map(unsigned int slot, mfn_t mfn)
+{
+    pte_t *entry = &xen_fixmap[slot];
+    pte_t pte;
+
+    ASSERT(!pte_is_valid(*entry));
+
+    pte = mfn_to_xen_entry(mfn, PAGE_HYPERVISOR_RW);
+    write_pte(entry, pte);
+}

Perhaps add a comment to clarify why it's safe to omit a TLB flush
here.
Note that arch_pmap_unmap() having one is a necessary but not sufficient
condition to that. In principle hardware may also cache "negative"
TLB
entries; I have no idea how RISC-V behaves in this regard, or whether
that aspect is actually left to implementations.

Quoted 8 lines
Quoted 3 lines

what do you mean by "negative" TLB? an old TLB entry which should be
invalidated?

No, I mean TLB entries saying "no valid translation here". I.e. avoiding
recurring walks of something that was once found to have no translation.

But we can't modify an existent entry because we have "ASSERT(!pte_is_valid(*entry))" in pmap_map() function and we are doing TLB flush during pmap_unmap(). So when we will be in pmap_map() we are sure that there is no TLB entry for the new pte.

Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
Quoted 10 lines
xen/arch/riscv/include/asm/pmap.h new file +7 -0
+static inline void arch_pmap_unmap(unsigned int slot)
+{
+    pte_t pte = {};
+
+    write_pte(&xen_fixmap[slot], pte);
+
+    flush_xen_tlb_range_va_local(FIXMAP_ADDR(slot),

PAGE_SIZE);
+}

For both functions, even if mainly for documentation purposes, it may
be desirable to mark them __init. It's again a necessary (but not sufficient) condition here, to validly use local TLB flushes only.

Quoted 3 lines

Does __init in this context means that it will be called only by boot
cpu at the start and that is it?

No, and I said so in my reply. __init is indicative of the function not
being suitable for runtime use, but it is not enough to indicate the function is also unsuitable for use as soon as a 2nd CPU is being brought
up. Yet for the latter we have no proper annotation. Hence my suggestion
to use the former to have at least a limited indicator.

Quoted 5 lines

An alternative might be to add ASSERT(system_state < SYS_STATE_smp_boot).
That, however, exists in common/pmap.c already anyway.

Yet another alternative would be a clarifying comment.

Thanks for clarifying. I will stick to the first option with __init.

~ Oleksii

#28 lore raw

Re: [PATCH v3 5/9] xen/riscv: introduce asm/pmap.h header

Quoted 45 lines · On 30.07.2024 13:39, [email protected] wrote:
Quoted 44 lines · Jan Beulich
Quoted 42 lines · On 29.07.2024 18:22, [email protected] wrote:
Quoted 30 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/pmap.h new file +12 -0
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_PMAP_H
+#define ASM_PMAP_H
+
+#include <xen/bug.h>
+#include <xen/mm.h>
+#include <xen/page-size.h>
+
+#include <asm/fixmap.h>
+#include <asm/flushtlb.h>
+#include <asm/system.h>
+

xen/arch/riscv/include/asm/pmap.h line 22

Quoted 10 lines
xen/arch/riscv/include/asm/pmap.h new file +10 -0
+static inline void arch_pmap_map(unsigned int slot, mfn_t mfn)
+{
+    pte_t *entry = &xen_fixmap[slot];
+    pte_t pte;
+
+    ASSERT(!pte_is_valid(*entry));
+
+    pte = mfn_to_xen_entry(mfn, PAGE_HYPERVISOR_RW);
+    write_pte(entry, pte);
+}

Perhaps add a comment to clarify why it's safe to omit a TLB flush
here.

Quoted 8 lines

Note that arch_pmap_unmap() having one is a necessary but not sufficient
condition to that. In principle hardware may also cache "negative"
TLB
entries; I have no idea how RISC-V behaves in this regard, or whether
that aspect is actually left to implementations.

what do you mean by "negative" TLB? an old TLB entry which should be
invalidated?

Quoted 7 lines
Quoted 4 lines

No, I mean TLB entries saying "no valid translation here". I.e. avoiding
recurring walks of something that was once found to have no translation.

But we can't modify an existent entry because we have "ASSERT(!pte_is_valid(*entry))" in pmap_map() function and we are doing TLB flush during pmap_unmap().

You _always_ modify an existing entry. That may be a not-present one, but that's still an entry. And that's part of why I think you still didn't understand what I said. A particular implementation, if permitted by the spec, may very well put in place a TLB entry when the result of a page walk was a non-present entry. So ...

Quoted 2 lines

So when we will be in pmap_map() we are
sure that there is no TLB entry for the new pte.

..., can you point me at the part of the spec saying that such "negative" TLB entries are not permitted?

Jan

#29 lore raw

Re: [PATCH v3 5/9] xen/riscv: introduce asm/pmap.h header

Quoted 67 lines · Jan Beulich
Quoted 47 lines · On 30.07.2024 13:39, [email protected] wrote:
Quoted 46 lines · Jan Beulich
Quoted 44 lines · On 29.07.2024 18:22, [email protected] wrote:
Quoted 31 lines · Jan Beulich
Quoted 17 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/pmap.h new file +13 -0
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_PMAP_H
+#define ASM_PMAP_H
+
+#include <xen/bug.h>
+#include <xen/mm.h>
+#include <xen/page-size.h>
+
+#include <asm/fixmap.h>
+#include <asm/flushtlb.h>
+#include <asm/system.h>
+
+static inline void arch_pmap_map(unsigned int slot, mfn_t

mfn)

xen/arch/riscv/include/asm/pmap.h line 22

Quoted 9 lines
xen/arch/riscv/include/asm/pmap.h new file +9 -0
+{
+    pte_t *entry = &xen_fixmap[slot];
+    pte_t pte;
+
+    ASSERT(!pte_is_valid(*entry));
+
+    pte = mfn_to_xen_entry(mfn, PAGE_HYPERVISOR_RW);
+    write_pte(entry, pte);
+}

Perhaps add a comment to clarify why it's safe to omit a TLB flush
here.

Quoted 8 lines

Note that arch_pmap_unmap() having one is a necessary but not sufficient
condition to that. In principle hardware may also cache "negative"
TLB
entries; I have no idea how RISC-V behaves in this regard, or whether
that aspect is actually left to implementations.

what do you mean by "negative" TLB? an old TLB entry which should
be
invalidated?

Quoted 8 lines
Quoted 4 lines

No, I mean TLB entries saying "no valid translation here". I.e. avoiding
recurring walks of something that was once found to have no translation.

But we can't modify an existent entry because we have "ASSERT(!pte_is_valid(*entry))" in pmap_map() function and we are doing
TLB flush during pmap_unmap().

You _always_ modify an existing entry. That may be a not-present one, but
that's still an entry. And that's part of why I think you still didn't
understand what I said. A particular implementation, if permitted by the
spec, may very well put in place a TLB entry when the result of a page
walk was a non-present entry. So ...

Quoted 6 lines
Quoted 2 lines

So when we will be in pmap_map() we are
sure that there is no TLB entry for the new pte.

..., can you point me at the part of the spec saying that such "negative"
TLB entries are not permitted?

I double-checked the spec and it seems to me you are right and it could be an issue. Absense of "negative" TLB entrues will be guaranted only in the case when Svvptc extension is present (
https://github.com/riscv/riscv-svvptc/blob/main/svvptc.adoc?plain=1#L85
):
Depending on the microarchitecture, some possible ways to facilitate implementation of Svvptc include: not having any address-translation caches, not
storing Invalid PTEs in the address-translation caches, automatically evicting
Invalid PTEs using a bounded timer, or making address-translation caches
coherent with store instructions that modify PTEs.

If the mentioned above extension isn't present. Also there is the following words in the spec
( https://github.com/riscv/riscv-isa-manual/blob/main/src/supervisor.adoc?plain=1#L1213
):
For the special cases of increasing the permissions on a leaf PTE and changing an invalid
PTE to a valid leaf, software may choose to execute the SFENCE.VMA lazily. After
modifying the PTE but before executing SFENCE.VMA, either the new or old permissions
will be used. In the latter case, a page-fault exception might occur, at which point software
should execute SFENCE.VMA in accordance with the previous bullet point.

Probably in the future we will need also similar to work Linux kernel does:
https://patchwork.kernel.org/project/linux-mips/cover/[email protected]/

So I'll add flush_xen_tlb_range_va_local(FIXMAP_ADDR(slot), PAGE_SIZE) in arch_pmap_map.

~ Oleksii

#30 lore raw

PATCH v3 6/9 xen/riscv: introduce functionality to work with cpu info

In reply to Oleksii Kurochko

struct pcpu_info is introduced to store pcpu related information. At the moment, it is only processor_id but in the fututre it will be guest cpu information and some temporary variables which will be used during save/restore of vcpu registers.

set_processor_id() and get_processor_id() are introduced to set and get processor id which is stored in pcpu_info.

__cpuid_to_hartid_map[NR_CPUS] is introduced to store the mapping between Xen logical CPU and hartid ( physical CPU id ) and auxiliary macros cpuid_to_hartid_map() for convience access to __cpuid_to_hartid_map[].

smp_processor_id() is defined properly as it is enough to information to define it now instead of seting it to "dummy" 0.

Also, tp registers is initialized to point to pcpu_info[0]; set_processor_id is set to 0 as Xen is running on logical cpu 0 and save physical CPU id for current logical CPU id in __cpuid_to_hartid_map[].

---
Changes in V3:
- new patch.

---
 xen/arch/riscv/Makefile                |  2 ++
 xen/arch/riscv/include/asm/processor.h | 35 ++++++++++++++++++++++++--
 xen/arch/riscv/include/asm/smp.h       | 10 ++++++++
 xen/arch/riscv/setup.c                 | 13 ++++++++++
 xen/arch/riscv/smp.c                   |  4 +++
 xen/arch/riscv/smpboot.c               | 12 +++++++++
 6 files changed, 74 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/riscv/smp.c
 create mode 100644 xen/arch/riscv/smpboot.c
xen/arch/riscv/Makefile +2 -0
index 81b77b13d6..334fd24547 100644
@@ -4,6 +4,8 @@ obj-y += mm.o
 obj-$(CONFIG_RISCV_64) += riscv64/
 obj-y += sbi.o
 obj-y += setup.o
+obj-y += smp.o
+obj-y += smpboot.o
 obj-y += stubs.o
 obj-y += traps.o
 obj-y += vm_event.o
xen/arch/riscv/include/asm/processor.h +33 -2
index 3ae164c265..4c3e45fd17 100644
@@ -12,8 +12,39 @@
 
 #ifndef __ASSEMBLY__
 
-/* TODO: need to be implemeted */
-#define smp_processor_id() 0
+#include <xen/bug.h>
+#include <xen/types.h>
+
+register struct pcpu_info *tp asm ("tp");
+
+struct pcpu_info {
+    unsigned int processor_id;
+};
+
+/* tp points to one of these */
+extern struct pcpu_info pcpu_info[NR_CPUS];
+
+#define get_processor_id()      (tp->processor_id)
+#define set_processor_id(id)    do { \
+    tp->processor_id = id;           \
+} while(0)
+
+static inline unsigned int smp_processor_id(void)
+{
+    unsigned int id;
+
+    id = get_processor_id();
+
+    /*
+     * Technically the hartid can be greater than what a uint can hold.
+     * If such a system were to exist, we will need to change
+     * the smp_processor_id() API to be unsigned long instead of
+     * unsigned int.
+     */
+    BUG_ON(id > UINT_MAX);
+
+    return id;
+}
 
 /* On stack VCPU state */
 struct cpu_user_regs
xen/arch/riscv/include/asm/smp.h +10 -0
index b1ea91b1eb..3fff27a8a5 100644
@@ -5,6 +5,8 @@
 #include <xen/cpumask.h>
 #include <xen/percpu.h>
 
+#define INVALID_HARTID UINT_MAX
+
 DECLARE_PER_CPU(cpumask_var_t, cpu_sibling_mask);
 DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);
 
@@ -14,6 +16,14 @@ DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);
  */
 #define park_offline_cpus false
 
+void smp_setup_processor_id(unsigned long boot_cpu_hartid);
+
+/*
+ * Mapping between linux logical cpu index and hartid.
+ */
+extern unsigned long __cpuid_to_hartid_map[NR_CPUS];
+#define cpuid_to_hartid_map(cpu) __cpuid_to_hartid_map[cpu]
+
 #endif
 
 /*
xen/arch/riscv/setup.c +13 -0
index 13f0e8c77d..37b234360c 100644
@@ -40,6 +40,19 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 {
     remove_identity_mapping();
 
+    /*
+     * tp register contains an address of physical cpu information.
+     * So write physical CPU info of boot cpu to tp register
+     * It will be used later by get_processor_id() to get process_id ( look at
+     * <asm/processor.h> ):
+     *   #define get_processor_id()    (tp->processor_id)
+     */
+    asm volatile ("mv tp, %0" : : "r"((unsigned long)&pcpu_info[0]));
+
+    set_processor_id(0);
+
+    smp_setup_processor_id(bootcpu_id);
+
     trap_init();
 
 #ifdef CONFIG_SELF_TESTS
xen/arch/riscv/smp.c new file +4 -0
new file mode 100644
index 0000000000..006a062ad7
@@ -0,0 +1,4 @@
+#include <xen/smp.h>
+
+/* tp points to one of these per cpu */
+struct pcpu_info pcpu_info[NR_CPUS];
\ No newline at end of file
xen/arch/riscv/smpboot.c new file +12 -0
new file mode 100644
index 0000000000..a90401ffd4
@@ -0,0 +1,12 @@
+#include <xen/init.h>
+#include <xen/sections.h>
+#include <xen/smp.h>
+
+unsigned long __cpuid_to_hartid_map[NR_CPUS] __ro_after_init = {
+    [0 ... NR_CPUS-1] = INVALID_HARTID
+};
+
+void __init smp_setup_processor_id(unsigned long boot_cpu_hartid)
+{
+    cpuid_to_hartid_map(0) = boot_cpu_hartid;
+}
Signature · 1 line
2.45.2
#31 lore raw

Re: [PATCH v3 6/9] xen/riscv: introduce functionality to work with cpu info

Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/processor.h +5 -2
@@ -12,8 +12,39 @@
 
 #ifndef __ASSEMBLY__
 
-/* TODO: need to be implemeted */
-#define smp_processor_id() 0
+#include <xen/bug.h>
+#include <xen/types.h>
+
+register struct pcpu_info *tp asm ("tp");
+

xen/arch/riscv/include/asm/processor.h line 29

Quoted 10 lines
xen/arch/riscv/include/asm/processor.h +10 -0
+struct pcpu_info {
+    unsigned int processor_id;
+};
+
+/* tp points to one of these */
+extern struct pcpu_info pcpu_info[NR_CPUS];
+
+#define get_processor_id()      (tp->processor_id)
+#define set_processor_id(id)    do { \
+    tp->processor_id = id;           \

Nit: Parentheses around id please.

xen/arch/riscv/include/asm/processor.h line 30

Quoted 1 line
xen/arch/riscv/include/asm/processor.h +1 -0
+} while(0)

While often we omit the blanks inside the parentheses for such constructs, the one ahead of the opening paren should still be there.

xen/arch/riscv/include/asm/processor.h line 44

Quoted 13 lines
xen/arch/riscv/include/asm/processor.h +13 -0
+static inline unsigned int smp_processor_id(void)
+{
+    unsigned int id;
+
+    id = get_processor_id();
+
+    /*
+     * Technically the hartid can be greater than what a uint can hold.
+     * If such a system were to exist, we will need to change
+     * the smp_processor_id() API to be unsigned long instead of
+     * unsigned int.
+     */
+    BUG_ON(id > UINT_MAX);

Compilers may complaing about this condition being always false. But: Why do you check against UINT_MAX, not against NR_CPUS? It's not the hart ID your retrieving get_processor_id(), but Xen's, isn't it? Even if I'm missing something here, ...

xen/arch/riscv/include/asm/smp.h line 8

Quoted 7 lines
xen/arch/riscv/include/asm/smp.h +1 -0
@@ -5,6 +5,8 @@
 #include <xen/cpumask.h>
 #include <xen/percpu.h>
 
+#define INVALID_HARTID UINT_MAX

... this implies that the check above would need to use >=.

xen/arch/riscv/include/asm/smp.h DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask); · line 25

Quoted 11 lines
xen/arch/riscv/include/asm/smp.h +7 -0
@@ -14,6 +16,14 @@ DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);
  */
 #define park_offline_cpus false
 
+void smp_setup_processor_id(unsigned long boot_cpu_hartid);
+
+/*
+ * Mapping between linux logical cpu index and hartid.
+ */
+extern unsigned long __cpuid_to_hartid_map[NR_CPUS];
+#define cpuid_to_hartid_map(cpu) __cpuid_to_hartid_map[cpu]

May I please ask that there be no new variables with double underscores at the front or any other namespacing violations?

xen/arch/riscv/setup.c void __init noreturn start_xen(unsigned long bootcpu_id, · line 46

Quoted 10 lines
xen/arch/riscv/setup.c +4 -0
@@ -40,6 +40,19 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 {
     remove_identity_mapping();
 
+    /*
+     * tp register contains an address of physical cpu information.
+     * So write physical CPU info of boot cpu to tp register
+     * It will be used later by get_processor_id() to get process_id ( look at

process_id?

xen/arch/riscv/setup.c void __init noreturn start_xen(unsigned long bootcpu_id, · line 50

Quoted 4 lines
xen/arch/riscv/setup.c +4 -0
+     * <asm/processor.h> ):
+     *   #define get_processor_id()    (tp->processor_id)
+     */
+    asm volatile ("mv tp, %0" : : "r"((unsigned long)&pcpu_info[0]));

Nit: Blanks missing.

xen/arch/riscv/smp.c line 4

Quoted 8 lines
xen/arch/riscv/smp.c new file +4 -0
@@ -0,0 +1,4 @@
+#include <xen/smp.h>
+
+/* tp points to one of these per cpu */
+struct pcpu_info pcpu_info[NR_CPUS];
\ No newline at end of file

Please correct this.

xen/arch/riscv/smpboot.c line 6

Quoted 9 lines
xen/arch/riscv/smpboot.c new file +6 -0
@@ -0,0 +1,12 @@
+#include <xen/init.h>
+#include <xen/sections.h>
+#include <xen/smp.h>
+
+unsigned long __cpuid_to_hartid_map[NR_CPUS] __ro_after_init = {
+    [0 ... NR_CPUS-1] = INVALID_HARTID

Nit: Blanks around - please.

xen/arch/riscv/smpboot.c line 12

Quoted 6 lines
xen/arch/riscv/smpboot.c new file +6 -0
+};
+
+void __init smp_setup_processor_id(unsigned long boot_cpu_hartid)
+{
+    cpuid_to_hartid_map(0) = boot_cpu_hartid;
+}

The function name suggests this can be used for all CPUs, but the code is pretty clear abut this being for the boot CPU only.

#32 lore raw

Re: [PATCH v3 6/9] xen/riscv: introduce functionality to work with cpu info

Quoted 42 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/processor.h +0 -0
@@ -12,8 +12,39 @@


 #ifndef __ASSEMBLY__

xen/arch/riscv/include/asm/processor.h +5 -2
-/* TODO: need to be implemeted */
-#define smp_processor_id() 0
+#include <xen/bug.h>
+#include <xen/types.h>
+
+register struct pcpu_info *tp asm ("tp");
+

xen/arch/riscv/include/asm/processor.h line 29

Quoted 10 lines
xen/arch/riscv/include/asm/processor.h +10 -0
+struct pcpu_info {
+    unsigned int processor_id;
+};
+
+/* tp points to one of these */
+extern struct pcpu_info pcpu_info[NR_CPUS];
+
+#define get_processor_id()      (tp->processor_id)
+#define set_processor_id(id)    do { \
+    tp->processor_id = id;           \

Nit: Parentheses around id please.

Quoted 1 line

+} while(0)

While often we omit the blanks inside the parentheses for such constructs, the one ahead of the opening paren should still be there.

Quoted 10 lines
xen/arch/riscv/include/asm/processor.h +8 -0
+static inline unsigned int smp_processor_id(void)
+{
+    unsigned int id;
+
+    id = get_processor_id();
+
+    /*
+     * Technically the hartid can be greater than what a uint can

hold.
+     * If such a system were to exist, we will need to change

Quoted 8 lines
Quoted 4 lines

+     * the smp_processor_id() API to be unsigned long instead of +     * unsigned int.
+     */
+    BUG_ON(id > UINT_MAX);

Compilers may complaing about this condition being always false. But:
Why
do you check against UINT_MAX, not against NR_CPUS?

Because HART id theoretically could be greater then what unsigned int can provide thereby NR_CPUS could be also greater then unsigned int ( or it can't ? ).
Generally I agree it would be better to compare it with NR_CPUS.

Quoted 2 lines

It's not the hart ID
your retrieving get_processor_id(), but Xen's, isn't it?

You are right it is Xen's CPU id.

Quoted 13 lines

Even if I'm
missing something here, ...

Quoted 7 lines
xen/arch/riscv/include/asm/smp.h +0 -0
@@ -5,6 +5,8 @@
  #include <xen/cpumask.h>
  #include <xen/percpu.h>


+#define INVALID_HARTID UINT_MAX

... this implies that the check above would need to use >=.

Quoted 15 lines

xen/arch/riscv/include/asm/smp.h DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask); · line 25

Quoted 11 lines
xen/arch/riscv/include/asm/smp.h +0 -0
@@ -14,6 +16,14 @@ DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);
   */
  #define park_offline_cpus false

xen/arch/riscv/include/asm/smp.h +7 -0
+void smp_setup_processor_id(unsigned long boot_cpu_hartid);
+
+/*
+ * Mapping between linux logical cpu index and hartid.
+ */
+extern unsigned long __cpuid_to_hartid_map[NR_CPUS];
+#define cpuid_to_hartid_map(cpu) __cpuid_to_hartid_map[cpu]

May I please ask that there be no new variables with double underscores
at the front or any other namespacing violations?

I just couldn't come up with better naming for __cpuid_to_hartid_map[] to show that it is private array. I will update the namings here.

Quoted 7 lines
Quoted 7 lines
xen/arch/riscv/setup.c +0 -0
@@ -40,6 +40,19 @@ void __init noreturn start_xen(unsigned long
 bootcpu_id,
  {
      remove_identity_mapping();

Quoted 8 lines
Quoted 6 lines

+    /*
+     * tp register contains an address of physical cpu information.
+     * So write physical CPU info of boot cpu to tp register +     * It will be used later by get_processor_id() to get process_id ( look at

process_id?

I meant processor_id here.

Quoted 11 lines

xen/arch/riscv/setup.c

Quoted 8 lines
xen/arch/riscv/setup.c +6 -0
+};
+
+void __init smp_setup_processor_id(unsigned long boot_cpu_hartid)
+{
+    cpuid_to_hartid_map(0) = boot_cpu_hartid;
+}

The function name suggests this can be used for all CPUs, but the code is pretty clear abut this being for the boot CPU only.

Then I will rename it to setup_bootcpu_id(...).

Thanks.

~ Oleksii

#33 lore raw

Re: [PATCH v3 6/9] xen/riscv: introduce functionality to work with cpu info

Quoted 20 lines · On 29.07.2024 18:32, [email protected] wrote:
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/processor.h +8 -0
+static inline unsigned int smp_processor_id(void)
+{
+    unsigned int id;
+
+    id = get_processor_id();
+
+    /*
+     * Technically the hartid can be greater than what a uint can

hold.
+     * If such a system were to exist, we will need to change

Quoted 8 lines
Quoted 4 lines

+     * the smp_processor_id() API to be unsigned long instead of +     * unsigned int.
+     */
+    BUG_ON(id > UINT_MAX);

Compilers may complaing about this condition being always false. But:
Why
do you check against UINT_MAX, not against NR_CPUS?

Quoted 3 lines

Because HART id theoretically could be greater then what unsigned int can provide thereby NR_CPUS could be also greater then unsigned int ( or it can't ? ).

Well, there are two aspects here. On a system with billions of harts, we wouldn't be able to bring up all of them anyway. NR_CPUS is presently limited at about 16k.

Yet than I have no idea whether hart IDs need to be all consecutive. On other hardware (x86 for example), the analog APIC IDs don't need to be. Hence I could see there being large hart IDs (unless excluded somewhere in the spec), which then you map to consecutive Xen CPU IDs, all having relatively small numbers (less than NR_CPUS).

If hart IDs can be sparse and wider than 32 bits, then I'd suggest to use an appropriately typed array right away for the Xen -> hart translation. For the hart -> Xen translation, if also needed, an array may then not be appropriate to use.

Jan

#34 lore raw

PATCH v3 7/9 xen/riscv: introduce and init SBI RFENCE extension

In reply to Oleksii Kurochko

Introduces functions to work with SBI RFENCE extenstion which will be used to do various version of fences for remote ( not local ) CPU.

Except that sbi_init() function and auxiliary functions and macros definitions are introduced to proper initialization and checking availability of SBI extenstions.
At the moment, it is only done for RFENCE.

sbi_remote_sfence_vma() is introduced to send SFENCE_VMA to a set of target HARTs and based on that flush_xen_tlb_range_va() will be implemented.

---
Changes in V3:
- new patch.

---
 xen/arch/riscv/include/asm/sbi.h |  57 +++++++
 xen/arch/riscv/sbi.c             | 256 +++++++++++++++++++++++++++++++
 xen/arch/riscv/setup.c           |   3 +
 3 files changed, 316 insertions(+)
xen/arch/riscv/include/asm/sbi.h +57 -0
index 0e6820a4ed..0985fbb1aa 100644
@@ -14,6 +14,38 @@
 
 #define SBI_EXT_0_1_CONSOLE_PUTCHAR		0x1
 
+#define SBI_EXT_BASE                    0x10
+#define SBI_EXT_RFENCE                  0x52464E43
+
+/* SBI function IDs for BASE extension */
+#define SBI_EXT_BASE_GET_SPEC_VERSION   0x0
+#define SBI_EXT_BASE_GET_IMP_ID         0x1
+#define SBI_EXT_BASE_GET_IMP_VERSION    0x2
+#define SBI_EXT_BASE_PROBE_EXT          0x3
+
+/* SBI function IDs for RFENCE extension */
+#define SBI_EXT_RFENCE_REMOTE_FENCE_I           0x0
+#define SBI_EXT_RFENCE_REMOTE_SFENCE_VMA        0x1
+#define SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID   0x2
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA       0x3
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID  0x4
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA       0x5
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID  0x6
+
+#define SBI_SPEC_VERSION_MAJOR_SHIFT    24
+#define SBI_SPEC_VERSION_MAJOR_MASK     0x7f
+#define SBI_SPEC_VERSION_MINOR_MASK     0xffffff
+
+/* SBI return error codes */
+#define SBI_SUCCESS             0
+#define SBI_ERR_FAILURE         -1
+#define SBI_ERR_NOT_SUPPORTED   -2
+#define SBI_ERR_INVALID_PARAM   -3
+#define SBI_ERR_DENIED          -4
+#define SBI_ERR_INVALID_ADDRESS -5
+
+#define SBI_SPEC_VERSION_DEFAULT        0x1
+
 struct sbiret {
     long error;
     long value;
@@ -31,4 +63,29 @@ struct sbiret sbi_ecall(unsigned long ext, unsigned long fid,
  */
 void sbi_console_putchar(int ch);
 
+/*
+ * Check underlying SBI implementation has RFENCE
+ *
+ * @return 1 for supported AND 0 for not-supported
+ */
+int sbi_has_rfence(void);
+
+/*
+ * Send SFENCE_VMA to a set of target HARTs.
+ *
+ * @param hart_mask mask representing set of target HARTs
+ * @param start virtual address start
+ * @param size virtual address size
+ */
+void sbi_remote_sfence_vma(const unsigned long *hart_mask,
+                           unsigned long start,
+                           unsigned long size);
+
+/*
+ * Initialize SBI library
+ *
+ * @return 0 on success, otherwise negative errno on failure
+ */
+int sbi_init(void);
+
 #endif /* __ASM_RISCV_SBI_H__ */
xen/arch/riscv/sbi.c +256 -0

271 lines not on this page · raw message

xen/arch/riscv/setup.c +3 -0
index 37b234360c..497e273081 100644
@@ -8,6 +8,7 @@
 #include <public/version.h>
 
 #include <asm/early_printk.h>
+#include <asm/sbi.h>
 #include <asm/traps.h>
 
 void arch_get_xen_caps(xen_capabilities_info_t *info)
@@ -55,6 +56,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     trap_init();
 
+    sbi_init();
+
 #ifdef CONFIG_SELF_TESTS
     test_macros_from_bug_h();
 #endif
Signature · 1 line
2.45.2
#35 lore raw

Re: [PATCH v3 7/9] xen/riscv: introduce and init SBI RFENCE extension

Quoted 25 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/sbi.h +19 -0
@@ -14,6 +14,38 @@
 
 #define SBI_EXT_0_1_CONSOLE_PUTCHAR		0x1
 
+#define SBI_EXT_BASE                    0x10
+#define SBI_EXT_RFENCE                  0x52464E43
+
+/* SBI function IDs for BASE extension */
+#define SBI_EXT_BASE_GET_SPEC_VERSION   0x0
+#define SBI_EXT_BASE_GET_IMP_ID         0x1
+#define SBI_EXT_BASE_GET_IMP_VERSION    0x2
+#define SBI_EXT_BASE_PROBE_EXT          0x3
+
+/* SBI function IDs for RFENCE extension */
+#define SBI_EXT_RFENCE_REMOTE_FENCE_I           0x0
+#define SBI_EXT_RFENCE_REMOTE_SFENCE_VMA        0x1
+#define SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID   0x2
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA       0x3
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID  0x4
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA       0x5
+#define SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID  0x6
+
+#define SBI_SPEC_VERSION_MAJOR_SHIFT    24

xen/arch/riscv/include/asm/sbi.h line 45

Quoted 10 lines
xen/arch/riscv/include/asm/sbi.h +10 -0
+#define SBI_SPEC_VERSION_MAJOR_MASK     0x7f
+#define SBI_SPEC_VERSION_MINOR_MASK     0xffffff
+
+/* SBI return error codes */
+#define SBI_SUCCESS             0
+#define SBI_ERR_FAILURE         -1
+#define SBI_ERR_NOT_SUPPORTED   -2
+#define SBI_ERR_INVALID_PARAM   -3
+#define SBI_ERR_DENIED          -4
+#define SBI_ERR_INVALID_ADDRESS -5

Parentheses around all the negative numbers please.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 69

Quoted 13 lines
xen/arch/riscv/include/asm/sbi.h +6 -0
+#define SBI_SPEC_VERSION_DEFAULT        0x1
+
 struct sbiret {
     long error;
     long value;
@@ -31,4 +63,29 @@ struct sbiret sbi_ecall(unsigned long ext, unsigned long fid,
  */
 void sbi_console_putchar(int ch);
 
+/*
+ * Check underlying SBI implementation has RFENCE
+ *
+ * @return 1 for supported AND 0 for not-supported

In which case ...

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 71

Quoted 2 lines
xen/arch/riscv/include/asm/sbi.h +2 -0
+ */
+int sbi_has_rfence(void);

... bool please.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 78

Quoted 6 lines
xen/arch/riscv/include/asm/sbi.h +6 -0
+/*
+ * Send SFENCE_VMA to a set of target HARTs.
+ *
+ * @param hart_mask mask representing set of target HARTs
+ * @param start virtual address start
+ * @param size virtual address size

Are these really virtual addresses, not somehow a bias and a number of bits (CPUs) or elements? From the rest of the patch I can't deduce what these two parameters express.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 79

Quoted 2 lines
xen/arch/riscv/include/asm/sbi.h +2 -0
+ */
+void sbi_remote_sfence_vma(const unsigned long *hart_mask,

Maybe better hart_mask[]? It's not clear to me though what the upper bound of the array is.

Quoted 10 lines
xen/arch/riscv/include/asm/sbi.h +6 -0
@@ -38,7 +48,253 @@ struct sbiret sbi_ecall(unsigned long ext, unsigned long fid,
     return ret;
 }
 
+static int sbi_err_map_xen_errno(int err)
+{
+    switch ( err )
+    {
+    case SBI_SUCCESS:
+        return 0;

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 66

Quoted 10 lines
xen/arch/riscv/include/asm/sbi.h +10 -0
+    case SBI_ERR_DENIED:
+        return -EACCES;
+    case SBI_ERR_INVALID_PARAM:
+        return -EINVAL;
+    case SBI_ERR_INVALID_ADDRESS:
+        return -EFAULT;
+    case SBI_ERR_NOT_SUPPORTED:
+    case SBI_ERR_FAILURE:
+    default:
+        return -EOPNOTSUPP;

Mapping FAILURE to -EOPNOTSUPP may end up confusing.

Quoted 15 lines
xen/arch/riscv/include/asm/sbi.h +11 -0
+    };
+}
+
 void sbi_console_putchar(int ch)
 {
     sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
 }
+
+static unsigned long sbi_major_version(void)
+{
+    return (sbi_spec_version >> SBI_SPEC_VERSION_MAJOR_SHIFT) &
+        SBI_SPEC_VERSION_MAJOR_MASK;
+}
+
+static unsigned long sbi_minor_version(void)

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 91

Quoted 10 lines
xen/arch/riscv/include/asm/sbi.h +10 -0
+{
+    return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
+}
+
+static long sbi_ext_base_func(long fid)
+{
+    struct sbiret ret;
+
+    ret = sbi_ecall(SBI_EXT_BASE, fid, 0, 0, 0, 0, 0, 0);
+    if (!ret.error)

Nit: Blanks missing.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 98

Quoted 7 lines
xen/arch/riscv/include/asm/sbi.h +7 -0
+        return ret.value;
+    else
+        return ret.error;
+}
+
+static void sbi_cpumask_to_hartmask(const struct cpumask *cmask,
+                 struct cpumask *hmask)

I doubt it is valud to re-use struct cpumask for hart maps.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 100

Quoted 2 lines
xen/arch/riscv/include/asm/sbi.h +2 -0
+{
+    u32 cpu;

uint32_t or yet better unsigned int please.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 111

Quoted 11 lines
xen/arch/riscv/include/asm/sbi.h +11 -0
+    unsigned long hart = INVALID_HARTID;
+
+    if (!cmask || !hmask)
+        return;
+
+    cpumask_clear(hmask);
+    for_each_cpu(cpu, cmask)
+    {
+        if ( CONFIG_NR_CPUS <= cpu )
+        {
+            printk(XENLOG_ERR "SBI: invalid hart=%lu for cpu=%d\n",

%u for the CPU please.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 112

Quoted 1 line
xen/arch/riscv/include/asm/sbi.h +1 -0
+                     hart, cpu);

"hart" wasn't set yet and hence is invalid or at least misleading to log.

Nit: Indentation.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 113

Quoted 1 line
xen/arch/riscv/include/asm/sbi.h +1 -0
+            continue;

Can you really sensibly continue in such a case?

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 116

Quoted 3 lines
xen/arch/riscv/include/asm/sbi.h +3 -0
+        }
+
+        hart = cpuid_to_hartid_map(pcpu_info[cpu].processor_id);

What does "_map" in the function/macro name signify?

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 124

Quoted 8 lines
xen/arch/riscv/include/asm/sbi.h +8 -0
+        cpumask_set_cpu(hart, hmask);
+    }
+}
+
+static int __sbi_rfence_v02_real(unsigned long fid,
+                 unsigned long hmask, unsigned long hbase,
+                 unsigned long start, unsigned long size,
+                 unsigned long arg4)

Nit: Indentation (more similar issues elsewhere).

Quoted 36 lines
xen/arch/riscv/include/asm/sbi.h +36 -0
+{
+    struct sbiret ret = {0};
+    int result = 0;
+
+    switch ( fid )
+    {
+    case SBI_EXT_RFENCE_REMOTE_FENCE_I:
+        ret = sbi_ecall(SBI_EXT_RFENCE, fid, hmask, hbase,
+                0, 0, 0, 0);
+        break;
+    case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
+        ret = sbi_ecall(SBI_EXT_RFENCE, fid, hmask, hbase,
+                start, size, 0, 0);
+        break;
+    case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
+        ret = sbi_ecall(SBI_EXT_RFENCE, fid, hmask, hbase,
+                start, size, arg4, 0);
+        break;
+    case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA:
+        ret = sbi_ecall(SBI_EXT_RFENCE, fid, hmask, hbase,
+                start, size, 0, 0);
+        break;
+    case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID:
+        ret = sbi_ecall(SBI_EXT_RFENCE, fid, hmask, hbase,
+                start, size, arg4, 0);
+        break;
+    case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA:
+        ret = sbi_ecall(SBI_EXT_RFENCE, fid, hmask, hbase,
+                start, size, 0, 0);
+        break;
+    case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID:
+        ret = sbi_ecall(SBI_EXT_RFENCE, fid, hmask, hbase,
+                start, size, arg4, 0);
+        break;
+
+    default:

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 170

Quoted 10 lines
xen/arch/riscv/include/asm/sbi.h +10 -0
+        printk("%s: unknown function ID [%lu]\n",
+               __func__, fid);
+        result = -EINVAL;
+        break;
+    };
+
+    if ( ret.error )
+    {
+        result = sbi_err_map_xen_errno(ret.error);
+        printk("%s: hbase=%lu hmask=0x%lx failed (error %d)\n",

%#lx (also elsewhere)

Quoted 6 lines
xen/arch/riscv/include/asm/sbi.h +6 -0
+               __func__, hbase, hmask, result);
+    }
+
+    return result;
+}
+

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 186

Quoted 10 lines
xen/arch/riscv/include/asm/sbi.h +10 -0
+static int __sbi_rfence_v02(unsigned long fid,
+                const unsigned long *hart_mask,
+                unsigned long start, unsigned long size,
+                unsigned long arg4, unsigned long arg5)
+{
+    struct cpumask tmask;
+    unsigned long hart, hmask, hbase;
+    int result;
+
+    if (!hart_mask)

Nit: Style (and more below). And I'm afraid I'm tired of making such remarks. Please check style yourself before submitting.

xen/arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 189

Quoted 3 lines
xen/arch/riscv/include/asm/sbi.h +3 -0
+static int sbi_spec_is_0_1(void)
+{
+    return (sbi_spec_version == SBI_SPEC_VERSION_DEFAULT) ? 1 : 0;

bool and no conditional operator

Jan

#36 lore raw

Re: [PATCH v3 7/9] xen/riscv: introduce and init SBI RFENCE extension

Quoted 10 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko

+/*
+ * Send SFENCE_VMA to a set of target HARTs.
+ *
+ * @param hart_mask mask representing set of target HARTs
+ * @param start virtual address start
+ * @param size virtual address size

Quoted 3 lines

Are these really virtual addresses, not somehow a bias and a number of bits (CPUs) or elements? From the rest of the patch I can't deduce what these two parameters express.

According to SBI doc start is an virtual address:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-rfence.adoc?plain=1#L44

and hart_mask is:
• unsigned long hart_mask is a scalar bit-vector containing hartids

Quoted 6 lines
Quoted 3 lines

+ */
+void sbi_remote_sfence_vma(const unsigned long *hart_mask,

Maybe better hart_mask[]? It's not clear to me though what the upper bound of the array is.

Theoretically it is ULONGMAX but we don't looking more then CONFIG_NR_CPUS.

Quoted 6 lines

xen/arch/riscv/sbi.c struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 98

Quoted 4 lines
xen/arch/riscv/sbi.c +3 -0
+
+static void sbi_cpumask_to_hartmask(const struct cpumask *cmask,
+                 struct cpumask *hmask)

I doubt it is valud to re-use struct cpumask for hart maps.

Why not? Would it be better to use unsigned long *hmask?

Quoted 21 lines
Quoted 3 lines

+{
+    u32 cpu;

uint32_t or yet better unsigned int please.

Quoted 11 lines

+    unsigned long hart = INVALID_HARTID;

xen/arch/riscv/sbi.c +10 -0
+
+    if (!cmask || !hmask)
+        return;
+
+    cpumask_clear(hmask);
+    for_each_cpu(cpu, cmask)
+    {
+        if ( CONFIG_NR_CPUS <= cpu )
+        {
+            printk(XENLOG_ERR "SBI: invalid hart=%lu for
Quoted 1 line

cpu=%d\n",

%u for the CPU please.

Quoted 4 lines
Quoted 1 line

+                     hart, cpu);

"hart" wasn't set yet and hence is invalid or at least misleading to log.

That why it will print INVALID_HARTID which user will identify as invalid hartid.
Do you mean that there is no any sense to message "invalid hart=%lu" as it is obviously invalid?

Quoted 7 lines

Nit: Indentation.

Quoted 1 line

+            continue;

Can you really sensibly continue in such a case?

I think yes, the worst thing we will have is that the "bad" CPU won't be used.
But it might be better to switch to BUG_ON() as if we are inised the "if CONFIG_NR_CPUS <= cpu" then it could tell us that something went wrong.

Quoted 6 lines
Quoted 4 lines

+        }
+
+        hart = cpuid_to_hartid_map(pcpu_info[cpu].processor_id);

What does "_map" in the function/macro name signify?

It is interconnections/correllation between Xen's CPU id and Hart's id.

~ Oleksii

#37 lore raw

Re: [PATCH v3 7/9] xen/riscv: introduce and init SBI RFENCE extension

Quoted 11 lines · On 30.07.2024 10:44, [email protected] wrote:
Quoted 10 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko

+/*
+ * Send SFENCE_VMA to a set of target HARTs.
+ *
+ * @param hart_mask mask representing set of target HARTs
+ * @param start virtual address start
+ * @param size virtual address size

Quoted 5 lines
Quoted 3 lines

Are these really virtual addresses, not somehow a bias and a number of bits (CPUs) or elements? From the rest of the patch I can't deduce what these two parameters express.

According to SBI doc start is an virtual address:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-rfence.adoc?plain=1#L44

Oh, so these are describing the VA range to be flushed. Okay.

Quoted 2 lines

and hart_mask is:
• unsigned long hart_mask is a scalar bit-vector containing hartids

Biased by hart_mask_base in the actual SBI call.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

+ */
+void sbi_remote_sfence_vma(const unsigned long *hart_mask,

Maybe better hart_mask[]? It's not clear to me though what the upper bound of the array is.

Theoretically it is ULONGMAX but we don't looking more then CONFIG_NR_CPUS.

See my comment elsewhere about possibly sparse numbering of hart IDs.

Quoted 6 lines
Quoted 5 lines

xen/arch/riscv/sbi.c struct sbiret sbi_ecall(unsigned long ext, unsigned long fid, · line 98

Quoted 3 lines
xen/arch/riscv/sbi.c +3 -0
+
+static void sbi_cpumask_to_hartmask(const struct cpumask *cmask,
+                 struct cpumask *hmask)

I doubt it is valud to re-use struct cpumask for hart maps.

Why not? Would it be better to use unsigned long *hmask?

It's not only better, but imo a requirement. Unless there's a guarantee by the spec that hart IDs for any subset of harts are sequential and starting from 0, you just can't assume they fall in the [0,NR_CPUS) or really [0,nr_cpu_ids) range. Yet without that you simply can't (ab)use struct cpumask (and btw it wants to be cpumask_t everywhere).

You may want to take a look at struct physid_mask that we have on x86 for the equivalent purpose.

Quoted 20 lines
Quoted 20 lines
Quoted 2 lines

+{
+    u32 cpu;

uint32_t or yet better unsigned int please.

Quoted 11 lines

+    unsigned long hart = INVALID_HARTID;

xen/arch/riscv/sbi.c +10 -0
+
+    if (!cmask || !hmask)
+        return;
+
+    cpumask_clear(hmask);
+    for_each_cpu(cpu, cmask)
+    {
+        if ( CONFIG_NR_CPUS <= cpu )
+        {
+            printk(XENLOG_ERR "SBI: invalid hart=%lu for
Quoted 1 line

cpu=%d\n",

%u for the CPU please.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+                     hart, cpu);

"hart" wasn't set yet and hence is invalid or at least misleading to log.

That why it will print INVALID_HARTID which user will identify as invalid hartid.
Do you mean that there is no any sense to message "invalid hart=%lu" as it is obviously invalid?

Yes. Plus the problem in this case isn't that there's no hart ID, but that the CPU ID is out of range. That's what the message wants to say. If such a check and message are necessary in the first place. I don't think we have anything like that on x86. And for_each_cpu() also can't possibly produce any ID at or beyond nr_cpu_ids, where nr_cpu_ids <= NR_CPUS.

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

+            continue;

Can you really sensibly continue in such a case?

I think yes, the worst thing we will have is that the "bad" CPU won't be used.
But it might be better to switch to BUG_ON() as if we are inised the "if CONFIG_NR_CPUS <= cpu" then it could tell us that something went wrong.

Again - your problem here isn't the range of "cpu". What you instead want to check is ...

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+        }
+
+        hart = cpuid_to_hartid_map(pcpu_info[cpu].processor_id);

the hart ID that you get back. If that's INVALID_HARTID, you're in fact in trouble.

Quoted 2 lines
Quoted 1 line

What does "_map" in the function/macro name signify?

It is interconnections/correllation between Xen's CPU id and Hart's id.

Well. What the function does internally is consult the map. Yet that's not relevant to any caller? They only care about the translation from one ID space to the other? No matter whether a map, radix tree, rbtree, or what not is used behind the scenes?

Jan

#38 lore raw

Re: [PATCH v3 7/9] xen/riscv: introduce and init SBI RFENCE extension

Quoted 20 lines · Jan Beulich
Quoted 11 lines · On 30.07.2024 10:44, [email protected] wrote:
Quoted 10 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko

+/*
+ * Send SFENCE_VMA to a set of target HARTs.
+ *
+ * @param hart_mask mask representing set of target HARTs
+ * @param start virtual address start
+ * @param size virtual address size

Quoted 7 lines
Quoted 5 lines

Are these really virtual addresses, not somehow a bias and a number
of bits (CPUs) or elements? From the rest of the patch I can't deduce
what these two parameters express.

According to SBI doc start is an virtual address:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-rfence.adoc?plain=1#L44

Quoted 6 lines

Oh, so these are describing the VA range to be flushed. Okay.

Quoted 2 lines

and hart_mask is:
• unsigned long hart_mask is a scalar bit-vector containing hartids

Biased by hart_mask_base in the actual SBI call.

What word "biased" mean here?

Quoted 9 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
(quoted diff) +2 -0
+
+static void sbi_cpumask_to_hartmask(const struct cpumask

*cmask,
+                 struct cpumask *hmask)

I doubt it is valud to re-use struct cpumask for hart maps.

Why not? Would it be better to use unsigned long *hmask?

Quoted 8 lines

It's not only better, but imo a requirement. Unless there's a guarantee
by the spec that hart IDs for any subset of harts are sequential and starting from 0, you just can't assume they fall in the [0,NR_CPUS) or
really [0,nr_cpu_ids) range. Yet without that you simply can't (ab)use
struct cpumask (and btw it wants to be cpumask_t everywhere).

It is guarantee that hart ID 0 will be present but not that they are numbered contiguously:

```
Hart IDs might
not necessarily be numbered contiguously in a multiprocessor system,
but at least one hart must
have a hart ID of zero. Hart IDs must be unique within the execution
environment.
```

I have to rework then this things around sbi_cpumask_to_hartmask().

Quoted 3 lines

You may want to take a look at struct physid_mask that we have on x86 for the equivalent purpose.

Thanks I will look at.

Quoted 28 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+            continue;

Can you really sensibly continue in such a case?

Quoted 8 lines

I think yes, the worst thing we will have is that the "bad" CPU won't
be used.
But it might be better to switch to BUG_ON() as if we are inised the
"if CONFIG_NR_CPUS <= cpu" then it could tell us that something went
wrong.

Again - your problem here isn't the range of "cpu". What you instead want to check is ...

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+        }
+
+        hart =
cpuid_to_hartid_map(pcpu_info[cpu].processor_id);

the hart ID that you get back. If that's INVALID_HARTID, you're in fact in trouble.

Quoted 3 lines
Quoted 1 line

What does "_map" in the function/macro name signify?

It is interconnections/correllation between Xen's CPU id and Hart's id.

Quoted 6 lines

Well. What the function does internally is consult the map. Yet that's
not relevant to any caller? They only care about the translation from one ID space to the other? No matter whether a map, radix tree, rbtree,
or what not is used behind the scenes?

Agree, the "_map" in cpuid_to_hartid_map() is useless. Thanks.

~ Oleksii

#39 lore raw

Re: [PATCH v3 7/9] xen/riscv: introduce and init SBI RFENCE extension

Quoted 21 lines · On 30.07.2024 13:57, [email protected] wrote:
Quoted 20 lines · Jan Beulich
Quoted 18 lines · On 30.07.2024 10:44, [email protected] wrote:
Quoted 10 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko

+/*
+ * Send SFENCE_VMA to a set of target HARTs.
+ *
+ * @param hart_mask mask representing set of target HARTs
+ * @param start virtual address start
+ * @param size virtual address size

Quoted 5 lines

Are these really virtual addresses, not somehow a bias and a number
of bits (CPUs) or elements? From the rest of the patch I can't deduce
what these two parameters express.

According to SBI doc start is an virtual address:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-rfence.adoc?plain=1#L44

Quoted 7 lines
Quoted 6 lines

Oh, so these are describing the VA range to be flushed. Okay.

Quoted 2 lines

and hart_mask is:
• unsigned long hart_mask is a scalar bit-vector containing hartids

Biased by hart_mask_base in the actual SBI call.

What word "biased" mean here?

The meaning of e.g. bit 0 in hart_mask will, aiui, be determined by hart_mask_base. If the latter is non-zero, the bit will not name hart 0.

Jan

#40 lore raw

Re: [PATCH v3 7/9] xen/riscv: introduce and init SBI RFENCE extension

In reply to Jan Beulich
Quoted 17 lines · Jan Beulich
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static void sbi_cpumask_to_hartmask(const struct cpumask

*cmask,
+                 struct cpumask *hmask)

I doubt it is valud to re-use struct cpumask for hart maps.

Why not? Would it be better to use unsigned long *hmask?

It's not only better, but imo a requirement. Unless there's a guarantee
by the spec that hart IDs for any subset of harts are sequential and starting from 0, you just can't assume they fall in the [0,NR_CPUS) or
really [0,nr_cpu_ids) range. Yet without that you simply can't (ab)use
struct cpumask (and btw it wants to be cpumask_t everywhere).

Quoted 2 lines

You may want to take a look at struct physid_mask that we have on x86 for the equivalent purpose.

Could you please explain me why factor 4 is used in defintion of MAX_APICS and why 256 ( is it a maximum number of APIC IDs can be supported in the system? ):
#define MAX_APICS MAX(256, 4 * NR_CPUS)

~ Oleksii

#41 lore raw

Re: [PATCH v3 7/9] xen/riscv: introduce and init SBI RFENCE extension

Quoted 18 lines · On 07.08.2024 13:29, [email protected] wrote:
Quoted 17 lines · Jan Beulich
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static void sbi_cpumask_to_hartmask(const struct cpumask

*cmask,
+                 struct cpumask *hmask)

I doubt it is valud to re-use struct cpumask for hart maps.

Why not? Would it be better to use unsigned long *hmask?

It's not only better, but imo a requirement. Unless there's a guarantee
by the spec that hart IDs for any subset of harts are sequential and starting from 0, you just can't assume they fall in the [0,NR_CPUS) or
really [0,nr_cpu_ids) range. Yet without that you simply can't (ab)use
struct cpumask (and btw it wants to be cpumask_t everywhere).

Quoted 6 lines
Quoted 2 lines

You may want to take a look at struct physid_mask that we have on x86 for the equivalent purpose.

Could you please explain me why factor 4 is used in defintion of MAX_APICS and why 256 ( is it a maximum number of APIC IDs can be supported in the system? ):
#define MAX_APICS MAX(256, 4 * NR_CPUS)

Both numbers are pretty arbitrary, I suppose. APIC IDs need not be contiguous, which is what the multiplication by 4 is trying to cover for. Plus on old (non-x2APIC) systems APIC IDs are only 8 bits wide, and allowing for all possible values in that case is cheap enough - hence the 256.

Jan

#42 lore raw

PATCH v7 2/9 xen/common: Move Arm's bootfdt.c to common

In reply to Oleksii Kurochko

From: Shawn Anastasio <[email protected]>

Move Arm's bootfdt.c to xen/common so that it can be used by other device tree architectures like PPC and RISCV.

---
Changes in V7:
- Nothing changed. Only rebase.
---
Changes in V6:
- update the version of the patch to v6.
---
Changes in V5:
- add xen/include/xen/bootfdt.h to MAINTAINERS file.
- drop message "Early device tree parsing and".
- After rebase on top of the current staging the following changes were done:
- init bootinfo variable in <common/device-tree/bootinfo.c> with BOOTINFO_INIT;
- update the code of dt_unreserved_regions():
CONFIG_STATIC_SHM related changes and getting of reserved_mem bootinfo_get_shmem() ??
- update the code of meminfo_overlap_check():

       add check ( INVALID_PADDR == bank_start ) to if case.

- update the code of check_reserved_regions_overlap():
CONFIG_STATIC_SHM related changes.
- struct bootinfo was updated ( CONFIG_STATIC_SHM changes )
- add shared_meminfo ( because of CONFIG_STATIC_SHM )
- struct struct membanks was update with __struct group so <xen/kernel> is neeeded to be included in bootfdt.h
- move BOOTINFO_ACPI_INIT, BOOTINFO_SHMEM_INIT, BOOTINFO_INIT to generic bootfdt.h
- bootinfo_get_reserved_mem(), bootinfo_get_mem(), bootinfo_get_acpi(), bootinfo_get_shmem() and bootinfo_get_shmem_extra() were moved to xen/bootfdt.h
- s/arm32/CONFIG_SEPARATE_XENHEAP/
- add inclusion of <xen/macros.h> because there are function in <xen/bootfdt.h> which are using container_of().
---
Changes in v4:
- create new xen/include/bootinfo.h rather than relying on arch's asm/setup.h to provide required definitions for bootinfo.c
- build bootinfo.c as .init.o
- clean up and sort bootinfo.c's #includes
- use CONFIG_SEPARATE_XENHEAP rather than CONFIG_ARM_32 to guard xenheap-specific behavior of populate_boot_allocator
- (MAINTAINERS) include all of common/device-tree rather than just bootinfo.c

---
 xen/arch/arm/Makefile            |   1 -
 xen/arch/arm/bootfdt.c           | 622 ------------------------------
 xen/arch/arm/include/asm/setup.h |  13 -
 xen/common/device-tree/Makefile  |   1 +
 xen/common/device-tree/bootfdt.c | 635 +++++++++++++++++++++++++++++++
 xen/include/xen/bootfdt.h        |  14 +
 6 files changed, 650 insertions(+), 636 deletions(-)
 delete mode 100644 xen/arch/arm/bootfdt.c
 create mode 100644 xen/common/device-tree/bootfdt.c
xen/arch/arm/Makefile +0 -1
index 45dc29ea53..da9c979dc4 100644
@@ -10,7 +10,6 @@ obj-$(CONFIG_TEE) += tee/
 obj-$(CONFIG_HAS_VPCI) += vpci.o
 
 obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
-obj-y += bootfdt.init.o
 obj-y += cpuerrata.o
 obj-y += cpufeature.o
 obj-y += decode.o
xen/arch/arm/bootfdt.c deleted +0 -622

622 lines not on this page · raw message

xen/arch/arm/include/asm/setup.h +0 -13
index 1748be29e5..64c227d171 100644
@@ -41,19 +41,6 @@ void fw_unreserved_regions(paddr_t s, paddr_t e,
                            void (*cb)(paddr_t ps, paddr_t pe),
                            unsigned int first);
 
-bool check_reserved_regions_overlap(paddr_t region_start, paddr_t region_size);
-
-struct bootmodule *add_boot_module(bootmodule_kind kind,
-                                   paddr_t start, paddr_t size, bool domU);
-struct bootmodule *boot_module_find_by_kind(bootmodule_kind kind);
-struct bootmodule * boot_module_find_by_addr_and_kind(bootmodule_kind kind,
-                                                             paddr_t start);
-void add_boot_cmdline(const char *name, const char *cmdline,
-                      bootmodule_kind kind, paddr_t start, bool domU);
-struct bootcmdline *boot_cmdline_find_by_kind(bootmodule_kind kind);
-struct bootcmdline * boot_cmdline_find_by_name(const char *name);
-const char *boot_module_kind_as_string(bootmodule_kind kind);
-
 void init_pdx(void);
 void setup_mm(void);
 
xen/common/device-tree/Makefile +1 -0
index da892dd55d..22a053f184 100644
@@ -1,2 +1,3 @@
+obj-y += bootfdt.init.o
 obj-y += bootinfo.init.o
 obj-y += device_tree.o
xen/common/device-tree/bootfdt.c new file +635 -0

635 lines not on this page · raw message

xen/include/xen/bootfdt.h +14 -0
index c39428d5f5..16fa05f38f 100644
@@ -157,6 +157,20 @@ struct bootinfo {
 
 extern struct bootinfo bootinfo;
 
+bool check_reserved_regions_overlap(paddr_t region_start,
+                                    paddr_t region_size);
+
+struct bootmodule *add_boot_module(bootmodule_kind kind,
+                                   paddr_t start, paddr_t size, bool domU);
+struct bootmodule *boot_module_find_by_kind(bootmodule_kind kind);
+struct bootmodule * boot_module_find_by_addr_and_kind(bootmodule_kind kind,
+                                                             paddr_t start);
+void add_boot_cmdline(const char *name, const char *cmdline,
+                      bootmodule_kind kind, paddr_t start, bool domU);
+struct bootcmdline *boot_cmdline_find_by_kind(bootmodule_kind kind);
+struct bootcmdline * boot_cmdline_find_by_name(const char *name);
+const char *boot_module_kind_as_string(bootmodule_kind kind);
+
 void populate_boot_allocator(void);
 
 size_t boot_fdt_info(const void *fdt, paddr_t paddr);
Signature · 1 line
2.45.2
#43 lore raw

Re: [PATCH v7 2/9] xen/common: Move Arm's bootfdt.c to common

Hi Oleksii,

Quoted 8 lines · Oleksii Kurochko

From: Shawn Anastasio <[email protected]>

Move Arm's bootfdt.c to xen/common so that it can be used by other device tree architectures like PPC and RISCV.

On Matrix you asked me to review this patch again. This wasn't obvious given you kept my ack. If you think a review is needed, then please either drop the ack or explain why you keep it and ask if it is fine.

Also, I tend to list in the changes where this was acked. In this case, you said I acked v4.

Anyway, before confirming my ack, I would like to ask some clarification.

Quoted 10 lines

---
Changes in V7:
- Nothing changed. Only rebase.
---
Changes in V6:
- update the version of the patch to v6.
---
Changes in V5:
- add xen/include/xen/bootfdt.h to MAINTAINERS file.

I don't see any change in MAINTAINERS within this patch. Did you happen to copy/paste all the changes made in the series?

In fact the only change related to this patch doesn't seem to be listed.

[...]

xen/common/device-tree/bootfdt.c line 448

Quoted 9 lines
xen/common/device-tree/bootfdt.c new file +9 -0
+#ifndef CONFIG_STATIC_SHM
+static inline int process_shm_node(const void *fdt, int node,
+                                   uint32_t address_cells, uint32_t size_cells)
+{
+    printk("CONFIG_STATIC_SHM must be enabled for parsing static shared"
+            " memory nodes\n");
+    return -EINVAL;
+}
+#endif

I see you duplicated the stub from arch/arm/include/static-shmem.h. But the one in static-shmem.h will now be unreachable. I think it needs to be removed.

Also, I think this change deserve an explanation in the commit message.

[...]

Quoted 19 lines
xen/common/device-tree/bootfdt.c new file +19 -0
+static void __init early_print_info(void)
+{
+    const struct membanks *mi = bootinfo_get_mem();
+    const struct membanks *mem_resv = bootinfo_get_reserved_mem();
+    struct bootmodules *mods = &bootinfo.modules;
+    struct bootcmdlines *cmds = &bootinfo.cmdlines;
+    unsigned int i;
+
+    for ( i = 0; i < mi->nr_banks; i++ )
+        printk("RAM: %"PRIpaddr" - %"PRIpaddr"\n",
+                mi->bank[i].start,
+                mi->bank[i].start + mi->bank[i].size - 1);
+    printk("\n");
+    for ( i = 0 ; i < mods->nr_mods; i++ )
+        printk("MODULE[%d]: %"PRIpaddr" - %"PRIpaddr" %-12s\n",
+                i,
+                mods->module[i].start,
+                mods->module[i].start + mods->module[i].size,
+                boot_module_kind_as_string(mods->module[i].kind));

xen/common/device-tree/bootfdt.c line 511

Quoted 10 lines
xen/common/device-tree/bootfdt.c new file +10 -0
+
+    for ( i = 0; i < mem_resv->nr_banks; i++ )
+    {
+        printk(" RESVD[%u]: %"PRIpaddr" - %"PRIpaddr"\n", i,
+               mem_resv->bank[i].start,
+               mem_resv->bank[i].start + mem_resv->bank[i].size - 1);
+    }
+#ifdef CONFIG_STATIC_SHM
+    early_print_info_shmem();
+#endif

Similar remark here.

Cheers,

Signature · 1 line
Julien Grall
#44 lore raw

Re: [PATCH v7 2/9] xen/common: Move Arm's bootfdt.c to common

Hi Julien,

Quoted 35 lines · Julien Grall

Hi Oleksii,

Quoted 8 lines · Oleksii Kurochko

From: Shawn Anastasio <[email protected]>

Move Arm's bootfdt.c to xen/common so that it can be used by other device tree architectures like PPC and RISCV.

On Matrix you asked me to review this patch again. This wasn't obvious
given you kept my ack. If you think a review is needed, then please either drop the ack or explain why you keep it and ask if it is fine.

Also, I tend to list in the changes where this was acked. In this case,
you said I acked v4.

Anyway, before confirming my ack, I would like to ask some clarification.

Quoted 10 lines

---
Changes in V7:
  - Nothing changed. Only rebase.
---
Changes in V6:
  - update the version of the patch to v6.
---
Changes in V5:
  - add xen/include/xen/bootfdt.h to MAINTAINERS file.

Quoted 3 lines

I don't see any change in MAINTAINERS within this patch. Did you happen
to copy/paste all the changes made in the series?

This change should be mentioned in this patch. It is part of the previous patch (
https://lore.kernel.org/xen-devel/102f8b60c55cdf2db5890b9fb1c2fb66e61c4a67.1721834549.git.oleksii.kurochko@gmail.com/
)

Quoted 15 lines

In fact the only change related to this patch doesn't seem to be listed.

[...]

Quoted 9 lines
xen/common/device-tree/bootfdt.c new file +3 -0
+#ifndef CONFIG_STATIC_SHM
+static inline int process_shm_node(const void *fdt, int node,
+                                   uint32_t address_cells,

uint32_t size_cells)
+{
+    printk("CONFIG_STATIC_SHM must be enabled for parsing static shared"
+            " memory nodes\n");
+    return -EINVAL;

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+#endif

I see you duplicated the stub from arch/arm/include/static-shmem.h. But
the one in static-shmem.h will now be unreachable. I think it needs to
be removed.

Overlooked that. Originally I added that to make Xen RISC-V and PPC build happy as early_scan_node() code uses process_shm_node():
static int __init early_scan_node(const void *fdt,
int node, const char *name, int
depth,
u32 address_cells, u32 size_cells, void *data)
{
...

       else if ( depth == 2 && device_tree_node_compatible(fdt, node,
   "xen,domain") )
           rc = process_domain_node(fdt, node, name, address_cells,
   size_cells);
       else if ( depth <= 3 && device_tree_node_compatible(fdt, node,
   "xen,domain-shared-memory-v1") )
           rc = process_shm_node(fdt, node, address_cells, size_cells);
   
       if ( rc < 0 )
           printk("fdt: node `%s': parsing failed\n", name);
       return rc;

}

Instead of introducing stub for process_shm_node() when CONFIG_STATIC_SHM I think it would be better to add "#ifdef CONFIG_STATIC_SHM" to early_scan_node():

static int __init early_scan_node(const void *fdt,
int node, const char *name, int
depth,
u32 address_cells, u32 size_cells, void *data)
{
...

       else if ( depth == 2 && device_tree_node_compatible(fdt, node,
   "xen,domain") )
           rc = process_domain_node(fdt, node, name, address_cells,
   size_cells);
   #ifdef CONFIG_STATIC_SHM
       else if ( depth <= 3 && device_tree_node_compatible(fdt, node,
   "xen,domain-shared-memory-v1") )
           rc = process_shm_node(fdt, node, address_cells, size_cells);

#endif

       if ( rc < 0 )
           printk("fdt: node `%s': parsing failed\n", name);
       return rc;

}

Quoted 3 lines

Also, I think this change deserve an explanation in the commit message.

If the option I mentioned above looks okay then definitely I have to mentioned that in commit message.

Thanks.

~ Oleksii

Quoted 38 lines

[...]

Quoted 27 lines
xen/common/device-tree/bootfdt.c new file +25 -0
+static void __init early_print_info(void)
+{
+    const struct membanks *mi = bootinfo_get_mem();
+    const struct membanks *mem_resv = bootinfo_get_reserved_mem();
+    struct bootmodules *mods = &bootinfo.modules;
+    struct bootcmdlines *cmds = &bootinfo.cmdlines;
+    unsigned int i;
+
+    for ( i = 0; i < mi->nr_banks; i++ )
+        printk("RAM: %"PRIpaddr" - %"PRIpaddr"\n",
+                mi->bank[i].start,
+                mi->bank[i].start + mi->bank[i].size - 1);
+    printk("\n");
+    for ( i = 0 ; i < mods->nr_mods; i++ )
+        printk("MODULE[%d]: %"PRIpaddr" - %"PRIpaddr" %-12s\n",
+                i,
+                mods->module[i].start,
+                mods->module[i].start + mods->module[i].size,
+                boot_module_kind_as_string(mods->module[i].kind));
+
+    for ( i = 0; i < mem_resv->nr_banks; i++ )
+    {
+        printk(" RESVD[%u]: %"PRIpaddr" - %"PRIpaddr"\n", i,
+               mem_resv->bank[i].start,
+               mem_resv->bank[i].start + mem_resv->bank[i].size -

1);
+    }

xen/common/device-tree/bootfdt.c line 511

Quoted 3 lines
xen/common/device-tree/bootfdt.c new file +3 -0
+#ifdef CONFIG_STATIC_SHM
+    early_print_info_shmem();
+#endif

Similar remark here.

Cheers,

#45 lore raw

Re: [PATCH v7 2/9] xen/common: Move Arm's bootfdt.c to common

Quoted 1 line · On 05/08/2024 11:04, [email protected] wrote:

Hi Julien,

Hi Oleksii,

Quoted 94 lines · Julien Grall
Quoted 35 lines

Hi Oleksii,

Quoted 8 lines · Oleksii Kurochko

From: Shawn Anastasio <[email protected]>

Move Arm's bootfdt.c to xen/common so that it can be used by other device tree architectures like PPC and RISCV.

On Matrix you asked me to review this patch again. This wasn't obvious
given you kept my ack. If you think a review is needed, then please either drop the ack or explain why you keep it and ask if it is fine.

Also, I tend to list in the changes where this was acked. In this case,
you said I acked v4.

Anyway, before confirming my ack, I would like to ask some clarification.

Quoted 10 lines

---
Changes in V7:
  - Nothing changed. Only rebase.
---
Changes in V6:
  - update the version of the patch to v6.
---
Changes in V5:
  - add xen/include/xen/bootfdt.h to MAINTAINERS file.

Quoted 3 lines

I don't see any change in MAINTAINERS within this patch. Did you happen
to copy/paste all the changes made in the series?

This change should be mentioned in this patch. It is part of the previous patch (
https://lore.kernel.org/xen-devel/102f8b60c55cdf2db5890b9fb1c2fb66e61c4a67.1721834549.git.oleksii.kurochko@gmail.com/
)

Quoted 15 lines

In fact the only change related to this patch doesn't seem to be listed.

[...]

Quoted 9 lines
xen/common/device-tree/bootfdt.c new file +3 -0
+#ifndef CONFIG_STATIC_SHM
+static inline int process_shm_node(const void *fdt, int node,
+                                   uint32_t address_cells,

uint32_t size_cells)
+{
+    printk("CONFIG_STATIC_SHM must be enabled for parsing static shared"
+            " memory nodes\n");
+    return -EINVAL;

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+#endif

I see you duplicated the stub from arch/arm/include/static-shmem.h. But
the one in static-shmem.h will now be unreachable. I think it needs to
be removed.

Overlooked that. Originally I added that to make Xen RISC-V and PPC build happy as early_scan_node() code uses process_shm_node():

    static int __init early_scan_node(const void *fdt,
                                      int node, const char *name, int
    depth,
                                      u32 address_cells, u32 size_cells,
                                      void *data)
    {
    ...
        else if ( depth == 2 && device_tree_node_compatible(fdt, node,
    "xen,domain") )
            rc = process_domain_node(fdt, node, name, address_cells,
    size_cells);
        else if ( depth <= 3 && device_tree_node_compatible(fdt, node,
    "xen,domain-shared-memory-v1") )
            rc = process_shm_node(fdt, node, address_cells, size_cells);
    
        if ( rc < 0 )
            printk("fdt: node `%s': parsing failed\n", name);
        return rc;
    }

Instead of introducing stub for process_shm_node() when CONFIG_STATIC_SHM I think it would be better to add "#ifdef CONFIG_STATIC_SHM" to early_scan_node():

Quoted 16 lines
    static int __init early_scan_node(const void *fdt,
                                      int node, const char *name, int
    depth,
                                      u32 address_cells, u32 size_cells,
                                      void *data)
    {
    ...
        else if ( depth == 2 && device_tree_node_compatible(fdt, node,
    "xen,domain") )
            rc = process_domain_node(fdt, node, name, address_cells,
    size_cells);
    #ifdef CONFIG_STATIC_SHM
        else if ( depth <= 3 && device_tree_node_compatible(fdt, node,
    "xen,domain-shared-memory-v1") )
            rc = process_shm_node(fdt, node, address_cells, size_cells);
    #endif

With this proposal, you would not throw an error if the user specify "xen,domain-shared-memory-v1" but Xen is not able to support it. This will be a change of behavior for Arm.

So my preferred approach is to stick with the existing patch and an explanation in the commit message.

Cheers,

Signature · 1 line
Julien Grall
#46 lore raw

PATCH v3 8/9 xen/riscv: page table handling

In reply to Oleksii Kurochko

Introduces an implementation of map_pages_to_xen() which requires multiple functions to work with page tables/entries:
- xen_pt_update()
- xen_pt_mapping_level()
- xen_pt_update_entry()
- xen_pt_next_level()
- xen_pt_check_entry()

During the mentioned above function it is necessary to create Xen tables or map/unmap them. For that it were introduced the following functions:
- create_xen_table()
- xen_map_table()
- xen_unmap_table()

Also it was introduced various internal macros for convience started with _PAGE_* which almost duplicate the bits in PTE except _PAGE_BLOCK which actually doesn't present in PTE which indicates that page larger then 4k is needed. RISC-V doesn't have a specific bit in PTE and it detect if it is a superpage or not only by using pte.x and pte.r. From the RISC-V spec:

```
  ...
  4. Otherwise, the PTE is valid. If pte.r = 1 or pte.x = 1, go to step 5.
     Otherwise, this PTE is a pointer to the next level of the page table.
     ... .
  5. A leaf PTE has been found.
     ...
  ...
```

Except that it was introduced flush_xen_tlb_range_va() for TLB flushing accross CPUs when PTE for requested mapping was properly updated.

---
Changes in V3:
- new patch. ( Technically it is reworked version of the generic approach which I tried to suggest in the previous version )

---
 xen/arch/riscv/Kconfig                 |   1 +
 xen/arch/riscv/Makefile                |   1 +
 xen/arch/riscv/include/asm/flushtlb.h  |  15 +
 xen/arch/riscv/include/asm/mm.h        |   2 +
 xen/arch/riscv/include/asm/page-bits.h |  36 +++
 xen/arch/riscv/include/asm/page.h      |  73 ++++-
 xen/arch/riscv/mm.c                    |   9 -
 xen/arch/riscv/pt.c                    | 410 +++++++++++++++++++++++++
 8 files changed, 537 insertions(+), 10 deletions(-)
 create mode 100644 xen/arch/riscv/pt.c
xen/arch/riscv/Kconfig +1 -0
index 0112aa8778..9827a12d34 100644
@@ -2,6 +2,7 @@ config RISCV
 	def_bool y
 	select FUNCTION_ALIGNMENT_16B
 	select GENERIC_BUG_FRAME
+	select GENERIC_PT
 	select HAS_DEVICE_TREE
 	select HAS_PMAP
 
xen/arch/riscv/Makefile +1 -0
index 334fd24547..d058ea4e95 100644
@@ -1,6 +1,7 @@
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
 obj-y += entry.o
 obj-y += mm.o
+obj-y += pt.o
 obj-$(CONFIG_RISCV_64) += riscv64/
 obj-y += sbi.o
 obj-y += setup.o
xen/arch/riscv/include/asm/flushtlb.h +15 -0
index cf66e90773..90c65b153f 100644
@@ -5,6 +5,8 @@
 #include <xen/bug.h>
 #include <xen/cpumask.h>
 
+#include <asm/sbi.h>
+
 /* Flush TLB of local processor for address va. */
 static inline void flush_xen_tlb_one_local(vaddr_t va)
 {
@@ -27,6 +29,19 @@ static inline void flush_xen_tlb_range_va_local(vaddr_t va,
     }
 }
 
+/*
+ * Flush a range of VA's hypervisor mappings from the TLB of all
+ * processors in the inner-shareable domain.
+ */
+static inline void flush_xen_tlb_range_va(vaddr_t va,
+                                          unsigned long size)
+{
+    if ( sbi_has_rfence() )
+        sbi_remote_sfence_vma(NULL, va, size);
+    else
+        BUG_ON("IPI support is need for remote TLB flush");
+}
+
 /*
  * Filter the given set of CPUs, removing those that definitely flushed their
  * TLB since @page_timestamp.
xen/arch/riscv/include/asm/mm.h +2 -0
index a0bdc2bc3a..a7550e77a7 100644
@@ -42,6 +42,8 @@ static inline void *maddr_to_virt(paddr_t ma)
 #define virt_to_mfn(va)     __virt_to_mfn(va)
 #define mfn_to_virt(mfn)    __mfn_to_virt(mfn)
 
+#define pte_get_mfn(pte) maddr_to_mfn(pte_to_paddr(pte))
+
 struct page_info
 {
     /* Each frame can be threaded onto a doubly-linked list. */
xen/arch/riscv/include/asm/page-bits.h +36 -0
index 8f1f474371..8e40a607d8 100644
@@ -3,6 +3,42 @@
 #ifndef __RISCV_PAGE_BITS_H__
 #define __RISCV_PAGE_BITS_H__
 
+/*
+ * PTE format:
+ * | XLEN-1  10 | 9             8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
+ *       PFN      reserved for SW   D   A   G   U   X   W   R   V
+ */
+
+#define _PAGE_PRESENT   BIT(0, UL)
+#define _PAGE_READ      BIT(1, UL)    /* Readable */
+#define _PAGE_WRITE     BIT(2, UL)    /* Writable */
+#define _PAGE_EXEC      BIT(3, UL)    /* Executable */
+#define _PAGE_USER      BIT(4, UL)    /* User */
+#define _PAGE_GLOBAL    BIT(5, UL)    /* Global */
+#define _PAGE_ACCESSED  BIT(6, UL)    /* Set by hardware on any access */
+#define _PAGE_DIRTY     BIT(7, UL)    /* Set by hardware on any write */
+#define _PAGE_SOFT      BIT(8, UL)    /* Reserved for software */
+
+/*
+ * There is no such bits in PTE format for RISC-V.
+ *
+ * _PAGE_BLOCK was introduced to have ability to tell that superpage
+ * should be allocated.
+ */
+#define _PAGE_BLOCK         BIT(9, UL)
+
+#define _PAGE_W_BIT     2
+#define _PAGE_XN_BIT    3
+#define _PAGE_RO_BIT    1
+
+/* TODO: move to somewhere generic part/header ? */
+#define _PAGE_XN        (1U << _PAGE_XN_BIT)
+#define _PAGE_RO        (1U << _PAGE_RO_BIT)
+#define _PAGE_W         (1U << _PAGE_W_BIT)
+#define PAGE_XN_MASK(x) (((x) >> _PAGE_XN_BIT) & 0x1U)
+#define PAGE_RO_MASK(x) (((x) >> _PAGE_RO_BIT) & 0x1U)
+#define PAGE_W_MASK(x)  (((x) >> _PAGE_W_BIT) & 0x1U)
+
 #define PAGE_SHIFT              12 /* 4 KiB Pages */
 #define PADDR_BITS              56 /* 44-bit PPN */
 
xen/arch/riscv/include/asm/page.h +72 -1

97 lines not on this page · raw message

xen/arch/riscv/mm.c +0 -9
index 959b6fc63e..ecb0f15fa8 100644
@@ -374,15 +374,6 @@ int destroy_xen_mappings(unsigned long s, unsigned long e)
     return -1;
 }
 
-int map_pages_to_xen(unsigned long virt,
-                     mfn_t mfn,
-                     unsigned long nr_mfns,
-                     unsigned int flags)
-{
-    BUG_ON("unimplemented");
-    return -1;
-}
-
 static inline pte_t mfn_from_pte(mfn_t mfn)
 {
     unsigned long pte = mfn_x(mfn) << PTE_PPN_SHIFT;
xen/arch/riscv/pt.c new file +410 -0

410 lines not on this page · raw message

Signature · 1 line
2.45.2
#47 lore raw

Re: [PATCH v3 8/9] xen/riscv: page table handling

Quoted 7 lines · Oleksii Kurochko

Introduces an implementation of map_pages_to_xen() which requires multiple functions to work with page tables/entries:
- xen_pt_update()
- xen_pt_mapping_level()
- xen_pt_update_entry()
- xen_pt_next_level()
- xen_pt_check_entry()

I question the need for xen_ prefixes here.

Quoted 2 lines

During the mentioned above function it is necessary to create Xen tables or map/unmap them. For that it were introduced the following functions:

You start the description with "Introduces ..." (yet better would be "Introduce ..."), just to switch back to past tense here again. You want to get used to describing what a patch does in imperative form. That'll make it easier to tell those parts from parts describing present / past behavior. (A patch description does specifically not describe what you did prior to submitting the patch; it describes what the patch itself does.)

Quoted 19 lines

- create_xen_table()
- xen_map_table()
- xen_unmap_table()

Also it was introduced various internal macros for convience started with _PAGE_* which almost duplicate the bits in PTE except _PAGE_BLOCK which actually doesn't present in PTE which indicates that page larger then 4k is needed. RISC-V doesn't have a specific bit in PTE and it detect if it is a superpage or not only by using pte.x and pte.r. From the RISC-V spec:

```
  ...
  4. Otherwise, the PTE is valid. If pte.r = 1 or pte.x = 1, go to step 5.
     Otherwise, this PTE is a pointer to the next level of the page table.
     ... .
  5. A leaf PTE has been found.
     ...
  ...
```
Quoted 2 lines

Except that it was introduced flush_xen_tlb_range_va() for TLB flushing accross CPUs when PTE for requested mapping was properly updated.

On top of what I said above, I think here you mean "In addition ..." or "Additionally ...".

xen/arch/riscv/Kconfig config RISCV · line 7

Quoted 9 lines
xen/arch/riscv/Kconfig +1 -0
@@ -2,6 +2,7 @@ config RISCV
 	def_bool y
 	select FUNCTION_ALIGNMENT_16B
 	select GENERIC_BUG_FRAME
+	select GENERIC_PT
 	select HAS_DEVICE_TREE
 	select HAS_PMAP

Stray leftover?

Quoted 6 lines
xen/arch/riscv/Kconfig +2 -0
@@ -27,6 +29,19 @@ static inline void flush_xen_tlb_range_va_local(vaddr_t va,
     }
 }
 
+/*
+ * Flush a range of VA's hypervisor mappings from the TLB of all

xen/arch/riscv/Kconfig static inline void flush_xen_tlb_range_va_local(vaddr_t va, · line 43

Quoted 10 lines
xen/arch/riscv/Kconfig +10 -0
+ * processors in the inner-shareable domain.
+ */
+static inline void flush_xen_tlb_range_va(vaddr_t va,
+                                          unsigned long size)
+{
+    if ( sbi_has_rfence() )
+        sbi_remote_sfence_vma(NULL, va, size);
+    else
+        BUG_ON("IPI support is need for remote TLB flush");
+}

static inline void flush_xen_tlb_range_va(vaddr_t va,
size_t size)

{
    BUG_ON(!sbi_has_rfence());
    sbi_remote_sfence_vma(NULL, va, size);
}

? Plus once again I'm uncertain about the usefulness of the "xen" infix.

xen/arch/riscv/include/asm/mm.h static inline void *maddr_to_virt(paddr_t ma) · line 45

Quoted 7 lines
xen/arch/riscv/include/asm/mm.h +1 -0
@@ -42,6 +42,8 @@ static inline void *maddr_to_virt(paddr_t ma)
 #define virt_to_mfn(va)     __virt_to_mfn(va)
 #define mfn_to_virt(mfn)    __mfn_to_virt(mfn)
 
+#define pte_get_mfn(pte) maddr_to_mfn(pte_to_paddr(pte))

Along the lines of the naming remark elsewhere: mfn_from_pte()? pte_to_paddr() is ambiguous, too: What paddr is it that the construct obtains? The one matching the address of the PTE (which may be of interest when accessing page tables), or the one encoded in the PTE? Imo paddr_from_pte() or pte_get_paddr() would again better express what is being done. You may want to take a look at x86's page.h.

xen/arch/riscv/include/asm/page-bits.h line 12

Quoted 13 lines
xen/arch/riscv/include/asm/page-bits.h +7 -0
@@ -3,6 +3,42 @@
 #ifndef __RISCV_PAGE_BITS_H__
 #define __RISCV_PAGE_BITS_H__
 
+/*
+ * PTE format:
+ * | XLEN-1  10 | 9             8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
+ *       PFN      reserved for SW   D   A   G   U   X   W   R   V
+ */
+
+#define _PAGE_PRESENT   BIT(0, UL)

The acronym being V, would _PAGE_VALID maybe be a better name? Just like it's PTE_VALID? Speaking of which: Why do you need both PTE_* and _PAGE_*? How will one determine which one to use where? Plus imo page-bits.h is the wrong header to put these in. In fact I notice abuse of this header has already proliferated: Both PPC and RISC-V include this header explicitly, when it was introduced for just xen/page-size.h to include directly (and the definitions to put there are solely ones related to what xen/page-size.h needs / provides). The underlying idea what to simplify header dependencies. With what PPC and RISC-V do, that's being undermined.

xen/arch/riscv/include/asm/page-bits.h line 20

Quoted 8 lines
xen/arch/riscv/include/asm/page-bits.h +8 -0
+#define _PAGE_READ      BIT(1, UL)    /* Readable */
+#define _PAGE_WRITE     BIT(2, UL)    /* Writable */
+#define _PAGE_EXEC      BIT(3, UL)    /* Executable */
+#define _PAGE_USER      BIT(4, UL)    /* User */
+#define _PAGE_GLOBAL    BIT(5, UL)    /* Global */
+#define _PAGE_ACCESSED  BIT(6, UL)    /* Set by hardware on any access */
+#define _PAGE_DIRTY     BIT(7, UL)    /* Set by hardware on any write */
+#define _PAGE_SOFT      BIT(8, UL)    /* Reserved for software */

The diagram says that's two bits.

xen/arch/riscv/include/asm/page-bits.h line 28

Quoted 7 lines
xen/arch/riscv/include/asm/page-bits.h +7 -0
+/*
+ * There is no such bits in PTE format for RISC-V.
+ *
+ * _PAGE_BLOCK was introduced to have ability to tell that superpage
+ * should be allocated.
+ */
+#define _PAGE_BLOCK         BIT(9, UL)

Imo, like on x86, super-page mappings should be the default whenever possible. Callers _not_ wanting such can request so - see x86'es MAP_SMALL_PAGES.

xen/arch/riscv/include/asm/page-bits.h line 40

Quoted 11 lines
xen/arch/riscv/include/asm/page-bits.h +11 -0
+#define _PAGE_W_BIT     2
+#define _PAGE_XN_BIT    3
+#define _PAGE_RO_BIT    1
+
+/* TODO: move to somewhere generic part/header ? */
+#define _PAGE_XN        (1U << _PAGE_XN_BIT)
+#define _PAGE_RO        (1U << _PAGE_RO_BIT)
+#define _PAGE_W         (1U << _PAGE_W_BIT)
+#define PAGE_XN_MASK(x) (((x) >> _PAGE_XN_BIT) & 0x1U)
+#define PAGE_RO_MASK(x) (((x) >> _PAGE_RO_BIT) & 0x1U)
+#define PAGE_W_MASK(x)  (((x) >> _PAGE_W_BIT) & 0x1U)

What are all of these about? Why PAGE_XN when you have a positive X bit in the PTEs? And why 0x1U when plain 1 would do? All the PAGE_*_MASK ones are also badly named. Constructs of that name should extract the bit in its position (i.e. not shifted to bit 0), or be the name of a constant to use to do so.

xen/arch/riscv/include/asm/page.h line 38

Quoted 8 lines
xen/arch/riscv/include/asm/page.h +1 -0
@@ -34,6 +34,7 @@
 #define PTE_LEAF_DEFAULT            (PTE_VALID | PTE_READABLE | PTE_WRITABLE)
 #define PTE_TABLE                   (PTE_VALID)
 
+#define PAGE_HYPERVISOR_RO          (PTE_VALID | PTE_READABLE)
 #define PAGE_HYPERVISOR_RW          (PTE_VALID | PTE_READABLE | PTE_WRITABLE)

No PAGE_HYPERVISOR_RX?

Quoted 16 lines
xen/arch/riscv/include/asm/page.h +11 -1
@@ -43,13 +44,68 @@
 
 #define pt_index(lvl, va) (pt_linear_offset((lvl), (va)) & VPN_MASK)
 
-/* Page Table entry */
+#define FIRST_SIZE (XEN_PT_LEVEL_SIZE(2))
+
+#define TABLE_OFFSET(offs) (_AT(unsigned int, offs) & ((_AC(1, U) << PAGETABLE_ORDER) - 1))
+
+#if RV_STAGE1_MODE > SATP_MODE_SV48
+#error "need to to update DECLARE_OFFSETS macros"
+#else
+
+#define l0_table_offset(va) TABLE_OFFSET(pt_linear_offset(0, va))
+#define l1_table_offset(va) TABLE_OFFSET(pt_linear_offset(1, va))
+#define l2_table_offset(va) TABLE_OFFSET(pt_linear_offset(2, va))

xen/arch/riscv/include/asm/page.h line 67

Quoted 10 lines
xen/arch/riscv/include/asm/page.h +10 -0
+#define l3_table_offset(va) TABLE_OFFSET(pt_linear_offset(3, va))
+
+/* Generate an array @var containing the offset for each level from @addr */
+#define DECLARE_OFFSETS(var, addr)          \
+    const unsigned int var[4] = {           \
+        l0_table_offset(addr),              \
+        l1_table_offset(addr),              \
+        l2_table_offset(addr),              \
+        l3_table_offset(addr)               \
+    }

Why would this need to have 4 entries in SV39 mode?

xen/arch/riscv/include/asm/page.h line 79

Quoted 11 lines
xen/arch/riscv/include/asm/page.h +10 -0
+#endif
+
 typedef struct {
+    unsigned long v:1;
+    unsigned long r:1;
+    unsigned long w:1;
+    unsigned long x:1;
+    unsigned long u:1;
+    unsigned long g:1;
+    unsigned long a:1;
+    unsigned long d:1;

bool for all of these?

xen/arch/riscv/include/asm/page.h line 85

Quoted 6 lines
xen/arch/riscv/include/asm/page.h +6 -0
+    unsigned long rsw:2;
+#if RV_STAGE1_MODE == SATP_MODE_SV39
+    unsigned long ppn0:9;
+    unsigned long ppn1:9;
+    unsigned long ppn2:26;
+    unsigned long rsw2:7;

"rsw" above appear to mean "reserved for software use". What does "rsw" here mean? Should this field be "rsv"?

Quoted 4 lines
xen/arch/riscv/include/asm/page.h +4 -0
+    unsigned long pbmt:2;
+    unsigned long n:1;
+#elif RV_STAGE1_MODE == SATP_MODE_SV48
+    unsigned long ppn0:9;

xen/arch/riscv/include/asm/page.h line 99

Quoted 10 lines
xen/arch/riscv/include/asm/page.h +10 -0
+    unsigned long ppn1:9;
+    unsigned long ppn2:9;
+    unsigned long ppn3:17;
+    unsigned long rsw2:7;
+    unsigned long pbmt:2;
+    unsigned long n:1;
+#else
+#error "Add proper bits for SATP_MODE"
+#endif
+} pt_t;

I can't spot a use anywhere of e.g. ppn0. Would be nice to understand in what contexts these bitfields are going to be used. I notice you specifically don't use them in e.g. pte_is_table().

xen/arch/riscv/include/asm/page.h line 109

Quoted 9 lines
xen/arch/riscv/include/asm/page.h +3 -0
+/* Page Table entry */
+typedef union {
 #ifdef CONFIG_RISCV_64
     uint64_t pte;
 #else
     uint32_t pte;
 #endif
+pt_t bits;
 } pte_t;

Struct field want indenting.

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

Quoted 9 lines
xen/arch/riscv/include/asm/page.h +5 -0
@@ -70,6 +126,21 @@ static inline bool pte_is_valid(pte_t p)
     return p.pte & PTE_VALID;
 }
 
+inline bool pte_is_table(const pte_t p, unsigned int level)
+{
+    (void) level;
+
+    return (((p.pte) & (PTE_VALID

No need to parenthesize p.pte.

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

Quoted 3 lines
xen/arch/riscv/include/asm/page.h +3 -0
+                       | PTE_READABLE
+                       | PTE_WRITABLE
+                       | PTE_EXECUTABLE)) == PTE_VALID);

Indentation of these lines looks to be off by 1.

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

Quoted 6 lines
xen/arch/riscv/include/asm/page.h +6 -0
+}
+
+static inline bool pte_is_mapping(const pte_t pte, unsigned int level)
+{
+    return !pte_is_table(pte, level);
+}

Don't you mean V=1 and (R=1 or X=1) here?

Quoted 7 lines
xen/arch/riscv/pt.c new file +4 -0
@@ -0,0 +1,410 @@
+#include <xen/bug.h>
+#include <xen/domain_page.h>
+#include <xen/errno.h>
+#include <xen/mm.h>

xen/arch/riscv/pt.c line 14

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+#include <xen/mm-frame.h>
+#include <xen/pmap.h>
+#include <xen/spinlock.h>
+
+#include <asm/flushtlb.h>
+#include <asm/page.h>
+
+static inline const mfn_t get_root_page(void)
+{
+    unsigned long root_maddr = csr_read(CSR_SATP) << PAGE_SHIFT;

You want to isolate the PPN part of the register before shifting.

xen/arch/riscv/pt.c line 26

Quoted 11 lines
xen/arch/riscv/pt.c new file +11 -0
+    return maddr_to_mfn(root_maddr);
+}
+
+static inline void set_pte_permissions(pte_t *pte, unsigned int flags)
+{
+    pte->bits.r = PAGE_RO_MASK(flags);
+    pte->bits.x = ~PAGE_XN_MASK(flags);
+    pte->bits.w = PAGE_W_MASK(flags);
+
+    pte->pte |= PTE_ACCESSED | PTE_DIRTY;
+}

As indicated elsewhere, imo objects of type pte_t should not be floating around in the system without having permissions set on them. The helper creating a pte_t should take both MFN and (permission) flags.

Further, with "flags" suitably arranged, all you need to do is to OR them into the shifted PPN; there's no need for a whapping 4 assignments, even if maybe the compiler can fold some of this.

xen/arch/riscv/pt.c line 30

Quoted 3 lines
xen/arch/riscv/pt.c new file +3 -0
+/* Sanity check of the entry */
+static bool xen_pt_check_entry(pte_t entry, mfn_t mfn, unsigned int level,
+                               unsigned int flags)

The comment wants extending to indicate what the parameters mean wrt what is going to be checked. For example, ...

xen/arch/riscv/pt.c line 33

Quoted 3 lines
xen/arch/riscv/pt.c new file +3 -0
+{
+    /* Sanity check when modifying an entry. */
+    if ( mfn_eq(mfn, INVALID_MFN) )

... it's unclear to me why incoming INVALID_MFN would indicate modification of an entry, whereas further down _PAGE_PRESENT supposedly indicates insertion.

xen/arch/riscv/pt.c line 43

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+    {
+        /* We don't allow modifying an invalid entry. */
+        if ( !pte_is_valid(entry) )
+        {
+            printk("Modifying invalid entry is not allowed.\n");
+            return false;
+        }
+
+        /* We don't allow modifying a table entry */
+        if ( !pte_is_mapping(entry, level) )

With what the comment say, why not pte_is_table()?

Quoted 13 lines
xen/arch/riscv/pt.c new file +13 -0
+        {
+            printk("Modifying a table entry is not allowed.\n");
+            return false;
+        }
+    }
+    /* Sanity check when inserting a mapping */
+    else if ( flags & _PAGE_PRESENT )
+    {
+        /* We should be here with a valid MFN. */
+        ASSERT(!mfn_eq(mfn, INVALID_MFN));
+
+        /*
+         * We don't allow replacing any valid entry.

xen/arch/riscv/pt.c line 66

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+         *
+         * Note that the function xen_pt_update() relies on this
+         * assumption and will skip the TLB flush. The function will need
+         * to be updated if the check is relaxed.
+         */
+        if ( pte_is_valid(entry) )
+        {
+            if ( pte_is_mapping(entry, level) )
+                printk("Changing MFN for a valid entry is not allowed (%#"PRI_mfn" -> %#"PRI_mfn").\n",
+                          mfn_x(pte_get_mfn(entry)), mfn_x(mfn));

Nit: Indentation. (I'm once again worried by all of these printk()s anyway.)

xen/arch/riscv/pt.c line 156

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+#define XEN_TABLE_MAP_FAILED 0
+#define XEN_TABLE_SUPER_PAGE 1
+#define XEN_TABLE_NORMAL_PAGE 2
+
+/*
+ * Take the currently mapped table, find the corresponding entry,
+ * and map the next table, if available.
+ *
+ * The read_only parameters indicates whether intermediate tables should
+ * be allocated when not present.

"read_only" would have a different meaning to me. Maybe use inverted sense with a name like "alloc"?

Quoted 19 lines
xen/arch/riscv/pt.c new file +19 -0
+ * Return values:
+ *  XEN_TABLE_MAP_FAILED: Either read_only was set and the entry
+ *  was empty, or allocating a new page failed.
+ *  XEN_TABLE_NORMAL_PAGE: next level mapped normally
+ *  XEN_TABLE_SUPER_PAGE: The next entry points to a superpage.
+ */
+static int xen_pt_next_level(bool read_only, unsigned int level,
+                             pte_t **table, unsigned int offset)
+{
+    pte_t *entry;
+    int ret;
+    mfn_t mfn;
+
+    entry = *table + offset;
+
+    if ( !pte_is_valid(*entry) )
+    {
+        if ( read_only )
+            return XEN_TABLE_MAP_FAILED;

xen/arch/riscv/pt.c line 186

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+
+        ret = create_xen_table(entry);
+        if ( ret )
+            return XEN_TABLE_MAP_FAILED;
+    }
+
+    if ( pte_is_mapping(*entry, level) )
+    {
+        return XEN_TABLE_SUPER_PAGE;
+    }

Please be consistent with braces around single statements.

xen/arch/riscv/pt.c line 194

Quoted 7 lines
xen/arch/riscv/pt.c new file +7 -0
+    mfn = pte_get_mfn(*entry);
+
+    xen_unmap_table(*table);
+    *table = xen_map_table(mfn);
+
+    return XEN_TABLE_NORMAL_PAGE;
+}

Normal page? Not normal table?

xen/arch/riscv/pt.c line 203

Quoted 8 lines
xen/arch/riscv/pt.c new file +8 -0
+/* Update an entry at the level @target. */
+static int xen_pt_update_entry(mfn_t root, unsigned long virt,
+                               mfn_t mfn, unsigned int arch_target,
+                               unsigned int flags)
+{
+    int rc;
+    unsigned int level = HYP_PT_ROOT_LEVEL;
+    unsigned int arch_level = level;

Why two level variables?

xen/arch/riscv/pt.c line 210

Quoted 7 lines
xen/arch/riscv/pt.c new file +7 -0
+    unsigned int target = arch_target;
+    pte_t *table;
+    /*
+     * The intermediate page tables are read-only when the MFN is not valid
+     * This means we either modify permissions or remove an entry.
+     */
+    bool read_only = mfn_eq(mfn, INVALID_MFN);

I'm afraid I can't make a connection between the incoming MFN being INVALID_MFN and intermediate tables being intended to remain unaltered.

xen/arch/riscv/pt.c line 214

Quoted 4 lines
xen/arch/riscv/pt.c new file +4 -0
+    pte_t pte, *entry;
+
+    /* convenience aliases */
+    DECLARE_OFFSETS(offsets, (paddr_t)virt);

Unless for a mode where translation is disabled, I don't think virtual addresses should ever be converted to paddr_t.

Quoted 14 lines
xen/arch/riscv/pt.c new file +14 -0
+    table = xen_map_table(root);
+    for ( ; level > target; level--, arch_level = level )
+    {
+        rc = xen_pt_next_level(read_only, arch_level, &table, offsets[arch_level]);
+        if ( rc == XEN_TABLE_MAP_FAILED )
+        {
+            /*
+             * We are here because xen_pt_next_level has failed to map
+             * the intermediate page table (e.g the table does not exist
+             * and the pt is read-only). It is a valid case when
+             * removing a mapping as it may not exist in the page table.
+             * In this case, just ignore it.
+             */
+            if ( flags & _PAGE_PRESENT )

xen/arch/riscv/pt.c line 239

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+            {
+                printk("%s: Unable to map level %u\n", __func__, arch_level);
+                rc = -ENOENT;
+                goto out;
+            }
+            else
+            {
+                rc = 0;
+                goto out;
+            }

Please pull out such identical "goto out".

xen/arch/riscv/pt.c line 155

Quoted 2 lines
xen/arch/riscv/pt.c new file +2 -0
+        }
+        else if ( rc != XEN_TABLE_NORMAL_PAGE ) {

Nit: Brace placement. Initially I was wondering how this would have compiled for you, until I spotted the opening brace ...

xen/arch/riscv/pt.c line 157

Quoted 2 lines
xen/arch/riscv/pt.c new file +2 -0
+            break;
+        }

... matching this closing one (both of which are really unnecessary).

Quoted 26 lines
xen/arch/riscv/pt.c new file +26 -0
+    }
+
+    if ( arch_level != arch_target )
+    {
+        printk("%s: Shattering superpage is not supported\n", __func__);
+        rc = -EOPNOTSUPP;
+        goto out;
+    }
+
+    entry = table + offsets[arch_level];
+
+    rc = -EINVAL;
+    if ( !xen_pt_check_entry(*entry, mfn, arch_level, flags) )
+        goto out;
+
+    /* We are removing the page */
+    if ( !(flags & _PAGE_PRESENT) )
+        memset(&pte, 0x00, sizeof(pte));
+    else
+    {
+        /* We are inserting a mapping => Create new pte. */
+        if ( !mfn_eq(mfn, INVALID_MFN) )
+        {
+            pte = mfn_to_xen_entry(mfn, PTE_VALID);
+        }
+        else /* We are updating the permission => Copy the current pte. */

xen/arch/riscv/pt.c line 279

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+            pte = *entry;
+
+        set_pte_permissions(&pte, flags);
+    }
+
+    write_pte(entry, pte);
+
+    rc = 0;
+
+out:

Nit: Style (label needs indenting).

Quoted 4 lines
xen/arch/riscv/pt.c new file +4 -0
+    xen_unmap_table(table);
+
+    return rc;
+}

xen/arch/riscv/pt.c line 293

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+
+static DEFINE_SPINLOCK(xen_pt_lock);
+
+/* Return the level where mapping should be done */
+static int xen_pt_mapping_level(unsigned long vfn, mfn_t mfn, unsigned long nr,
+                                unsigned int flags)
+{
+    unsigned int level = 0;
+    unsigned long mask;
+    int i;

unsigned int?

Quoted 11 lines
xen/arch/riscv/pt.c new file +11 -0
+    /*
+     * Don't take into account the MFN when removing mapping (i.e
+     * MFN_INVALID) to calculate the correct target order.
+     *
+     * `vfn` and `mfn` must be both superpage aligned.
+     * They are or-ed together and then checked against the size of
+     * each level.
+     *
+     * `left` is not included and checked separately to allow
+     * superpage mapping even if it is not properly aligned (the
+     * user may have asked to map 2MB + 4k).

xen/arch/riscv/pt.c line 315

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+     */
+    mask = !mfn_eq(mfn, INVALID_MFN) ? mfn_x(mfn) : 0;
+    mask |= vfn;
+
+    /*
+     * Always use level 0 ( 4k mapping ) mapping unless the caller request
+     * block mapping.
+     */
+    if ( likely(!(flags & _PAGE_BLOCK)) )
+        return level;

This is independent of the calculation of "mask" and hence can move up.

xen/arch/riscv/pt.c line 320

Quoted 4 lines
xen/arch/riscv/pt.c new file +4 -0
+    for ( i = HYP_PT_ROOT_LEVEL; i > 0; i-- )
+    {
+        if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) &&
+            (nr >= BIT(XEN_PT_LEVEL_ORDER(i), UL)) )

Nit: Indentation again.

xen/arch/riscv/pt.c line 333

Quoted 13 lines
xen/arch/riscv/pt.c new file +13 -0
+        {
+            level = i;
+            break;
+        }
+    }
+
+    return level;
+}
+
+static int xen_pt_update(unsigned long virt,
+                         mfn_t mfn,
+                         /* const on purpose as it is used for TLB flush */
+                         const unsigned long nr_mfns,

I'm afraid I don't see what the comment is actually trying to tell me. If this is about you wanting to make sure the function arguments aren't altered prematurely, then why would the same not apply to virt, mfn, and flags (all of which matter at the time the TLB flush is initiated)?

Quoted 54 lines
xen/arch/riscv/pt.c new file +54 -0
+                         unsigned int flags)
+{
+    int rc = 0;
+    unsigned long vfn = virt >> PAGE_SHIFT;
+    unsigned long left = nr_mfns;
+
+    const mfn_t root = get_root_page();
+
+    /*
+     * It is bad idea to have mapping both writeable and
+     * executable.
+     * When modifying/creating mapping (i.e _PAGE_PRESENT is set),
+     * prevent any update if this happen.
+     */
+    if ( (flags & _PAGE_PRESENT) && !PAGE_RO_MASK(flags) &&
+         !PAGE_XN_MASK(flags) )
+    {
+        printk("Mappings should not be both Writeable and Executable.\n");
+        return -EINVAL;
+    }
+
+    if ( !IS_ALIGNED(virt, PAGE_SIZE) )
+    {
+        printk("The virtual address is not aligned to the page-size.\n");
+        return -EINVAL;
+    }
+
+    spin_lock(&xen_pt_lock);
+
+    while ( left )
+    {
+        unsigned int order, level;
+
+        level = xen_pt_mapping_level(vfn, mfn, left, flags);
+        order = XEN_PT_LEVEL_ORDER(level);
+
+        ASSERT(left >= BIT(order, UL));
+
+        rc = xen_pt_update_entry(root, vfn << PAGE_SHIFT, mfn, level,
+                                    flags);
+        if ( rc )
+            break;
+
+        vfn += 1U << order;
+        if ( !mfn_eq(mfn, INVALID_MFN) )
+            mfn = mfn_add(mfn, 1U << order);
+
+        left -= (1U << order);
+
+        if ( rc )
+            break;
+    }
+
+    /*

xen/arch/riscv/pt.c line 397

Quoted 10 lines
xen/arch/riscv/pt.c new file +10 -0
+     * The TLBs flush can be safely skipped when a mapping is inserted
+     * as we don't allow mapping replacement (see xen_pt_check_entry()).
+     *
+     * For all the other cases, the TLBs will be flushed unconditionally
+     * even if the mapping has failed. This is because we may have
+     * partially modified the PT. This will prevent any unexpected
+     * behavior afterwards.
+     */
+    if ( !((flags & _PAGE_PRESENT) && !mfn_eq(mfn, INVALID_MFN)) )
+        flush_xen_tlb_range_va(virt, PAGE_SIZE * nr_mfns);

See my comments elsewhere towards TLB behavior on RISC-V. The indicated skipping of a flush is safe only if not-present entries cannot be put in the TLB.

xen/arch/riscv/pt.c line 410

Quoted 12 lines
xen/arch/riscv/pt.c new file +12 -0
+    spin_unlock(&xen_pt_lock);
+
+    return rc;
+}
+
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)
+{
+    return xen_pt_update(virt, mfn, nr_mfns, flags);
+}

Why this wrapping of two functions taking identical arguments?

Jan

#48 lore raw

Re: [PATCH v3 8/9] xen/riscv: page table handling

Quoted 11 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko

Introduces an implementation of map_pages_to_xen() which requires multiple
functions to work with page tables/entries:
- xen_pt_update()
- xen_pt_mapping_level()
- xen_pt_update_entry()
- xen_pt_next_level()
- xen_pt_check_entry()

I question the need for xen_ prefixes here.

xen_ prefixes could be dropped, there is no big meaning in them.

Quoted 12 lines
Quoted 10 lines
xen/arch/riscv/Kconfig +0 -0
@@ -2,6 +2,7 @@ config RISCV

  def_bool y
  select FUNCTION_ALIGNMENT_16B
  select GENERIC_BUG_FRAME
+ select GENERIC_PT
  select HAS_DEVICE_TREE
  select HAS_PMAP

Stray leftover?

Missed to drop after I tried to make these changes as a part of common code. Thanks.

Quoted 18 lines
Quoted 8 lines
xen/arch/riscv/include/asm/page-bits.h +0 -0
@@ -3,6 +3,42 @@
  #ifndef __RISCV_PAGE_BITS_H__
  #define __RISCV_PAGE_BITS_H__

xen/arch/riscv/include/asm/page-bits.h line 12

Quoted 7 lines
xen/arch/riscv/include/asm/page-bits.h +7 -0
+/*
+ * PTE format:
+ * | XLEN-1  10 | 9             8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
+ *       PFN      reserved for SW   D   A   G   U   X   W   R   V
+ */
+
+#define _PAGE_PRESENT   BIT(0, UL)

The acronym being V, would _PAGE_VALID maybe be a better name? Just like it's PTE_VALID? Speaking of which: Why do you need both PTE_*

Quoted 8 lines

and _PAGE_*? How will one determine which one to use where? Plus imo page-bits.h is the wrong header to put these in. In fact I notice abuse of this header has already proliferated: Both PPC and RISC-V include this header explicitly, when it was introduced for just xen/page-size.h to include directly (and the definitions to put there are solely ones related to what xen/page-size.h needs / provides). The underlying idea what to simplify header dependencies. With what PPC and RISC-V do, that's being undermined.

I introduced them to be aligned with ARM code as I tried to use it as a common code but after 'page table handling' was reworked to be arch- specific _PAGE_* defintions could be dropped and only PTE_* ones could be used.

Quoted 13 lines
Quoted 11 lines
xen/arch/riscv/include/asm/page-bits.h +6 -0
+#define _PAGE_READ      BIT(1, UL)    /* Readable */
+#define _PAGE_WRITE     BIT(2, UL)    /* Writable */
+#define _PAGE_EXEC      BIT(3, UL)    /* Executable */
+#define _PAGE_USER      BIT(4, UL)    /* User */
+#define _PAGE_GLOBAL    BIT(5, UL)    /* Global */
+#define _PAGE_ACCESSED  BIT(6, UL)    /* Set by hardware on any

access */
+#define _PAGE_DIRTY     BIT(7, UL)    /* Set by hardware on any write */
+#define _PAGE_SOFT      BIT(8, UL)    /* Reserved for software */

The diagram says that's two bits.

I meant here as a start bit index for _PAGE_SOFT.

Quoted 27 lines
Quoted 9 lines

+/*
+ * There is no such bits in PTE format for RISC-V.
+ *
+ * _PAGE_BLOCK was introduced to have ability to tell that superpage
+ * should be allocated.
+ */
+#define _PAGE_BLOCK         BIT(9, UL)

Imo, like on x86, super-page mappings should be the default whenever possible.
Callers _not_ wanting such can request so - see x86'es MAP_SMALL_PAGES.

xen/arch/riscv/include/asm/page-bits.h line 40

Quoted 11 lines
xen/arch/riscv/include/asm/page-bits.h +11 -0
+#define _PAGE_W_BIT     2
+#define _PAGE_XN_BIT    3
+#define _PAGE_RO_BIT    1
+
+/* TODO: move to somewhere generic part/header ? */
+#define _PAGE_XN        (1U << _PAGE_XN_BIT)
+#define _PAGE_RO        (1U << _PAGE_RO_BIT)
+#define _PAGE_W         (1U << _PAGE_W_BIT)
+#define PAGE_XN_MASK(x) (((x) >> _PAGE_XN_BIT) & 0x1U)
+#define PAGE_RO_MASK(x) (((x) >> _PAGE_RO_BIT) & 0x1U)
+#define PAGE_W_MASK(x)  (((x) >> _PAGE_W_BIT) & 0x1U)
Quoted 8 lines

What are all of these about? Why PAGE_XN when you have a positive X bit in the
PTEs? And why 0x1U when plain 1 would do? All the PAGE_*_MASK ones are also
badly named. Constructs of that name should extract the bit in its position
(i.e. not shifted to bit 0), or be the name of a constant to use to do so.

The same reason as above initially I tried to have generic code with ARM but it at the end it was a bad idea. So just need to provide proper names. Thanks.

Quoted 13 lines
Quoted 11 lines
xen/arch/riscv/include/asm/page.h +0 -0
@@ -34,6 +34,7 @@
  #define PTE_LEAF_DEFAULT            (PTE_VALID | PTE_READABLE |
 PTE_WRITABLE)
  #define PTE_TABLE                   (PTE_VALID)


+#define PAGE_HYPERVISOR_RO          (PTE_VALID | PTE_READABLE) #define PAGE_HYPERVISOR_RW          (PTE_VALID | PTE_READABLE | PTE_WRITABLE)

No PAGE_HYPERVISOR_RX?

I haven't used it at the moment, so I haven't provided it.

Quoted 24 lines
Quoted 24 lines
xen/arch/riscv/include/asm/page.h +0 -0
@@ -43,13 +44,68 @@


 #define pt_index(lvl, va) (pt_linear_offset((lvl), (va)) & VPN_MASK)

xen/arch/riscv/include/asm/page.h +3 -1
-/* Page Table entry */
+#define FIRST_SIZE (XEN_PT_LEVEL_SIZE(2))
+
+#define TABLE_OFFSET(offs) (_AT(unsigned int, offs) & ((_AC(1, U)

<< PAGETABLE_ORDER) - 1))

xen/arch/riscv/include/asm/page.h +11 -0
+
+#if RV_STAGE1_MODE > SATP_MODE_SV48
+#error "need to to update DECLARE_OFFSETS macros"
+#else
+
+#define l0_table_offset(va) TABLE_OFFSET(pt_linear_offset(0, va))
+#define l1_table_offset(va) TABLE_OFFSET(pt_linear_offset(1, va))
+#define l2_table_offset(va) TABLE_OFFSET(pt_linear_offset(2, va))
+#define l3_table_offset(va) TABLE_OFFSET(pt_linear_offset(3, va))
+
+/* Generate an array @var containing the offset for each level

from @addr */
+#define DECLARE_OFFSETS(var, addr)          \

Quoted 8 lines
Quoted 6 lines

+    const unsigned int var[4] = {           \
+        l0_table_offset(addr),              \
+        l1_table_offset(addr),              \
+        l2_table_offset(addr),              \
+        l3_table_offset(addr)               \
+    }

Why would this need to have 4 entries in SV39 mode?

I don't need for Sv39. I just wasn't sure with the better way to have universal DECLARE_OFFSETS() definition. I will add #ifdef Sv48 around l3_table_offset and drop 4 in const unsigned int var[4].

Quoted 22 lines
Quoted 4 lines
xen/arch/riscv/include/asm/page.h +2 -0
+#endif
+

 typedef struct {

Quoted 8 lines

+    unsigned long v:1;
+    unsigned long r:1;
+    unsigned long w:1;
+    unsigned long x:1;
+    unsigned long u:1;
+    unsigned long g:1;
+    unsigned long a:1;
+    unsigned long d:1;

bool for all of these?

Quoted 6 lines

+    unsigned long rsw:2;
+#if RV_STAGE1_MODE == SATP_MODE_SV39
+    unsigned long ppn0:9;
+    unsigned long ppn1:9;
+    unsigned long ppn2:26;
+    unsigned long rsw2:7;

Quoted 3 lines

"rsw" above appear to mean "reserved for software use". What does "rsw" here
mean? Should this field be "rsv"?

rsw2 means:
Bits 60–54 are reserved for future standard
use and, until their use is defined by some standard extension, must be zeroed by software for
forward compatibility. If any of these bits are set, a page-fault exception is raised.
It would be better to use "rsv" Thanks.

Quoted 13 lines
Quoted 13 lines

+    unsigned long pbmt:2;
+    unsigned long n:1;

xen/arch/riscv/include/asm/page.h +10 -0
+#elif RV_STAGE1_MODE == SATP_MODE_SV48
+    unsigned long ppn0:9;
+    unsigned long ppn1:9;
+    unsigned long ppn2:9;
+    unsigned long ppn3:17;
+    unsigned long rsw2:7;
+    unsigned long pbmt:2;
+    unsigned long n:1;
+#else
+#error "Add proper bits for SATP_MODE"
Quoted 8 lines

xen/arch/riscv/include/asm/page.h

Quoted 2 lines
xen/arch/riscv/include/asm/page.h +2 -0
+#endif
+} pt_t;

I can't spot a use anywhere of e.g. ppn0. Would be nice to understand in
what contexts these bitfields are going to be used. I notice you specifically
don't use them in e.g. pte_is_table().

I don't use them at the moment. I just introduced them for the possible future using. I can re-check what of them I am using in my private branches and come up here only with that one which are really used.

Quoted 11 lines

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

Quoted 9 lines
xen/arch/riscv/include/asm/page.h +3 -0
+}
+
+static inline bool pte_is_mapping(const pte_t pte, unsigned int

level)

xen/arch/riscv/include/asm/page.h +3 -0
+{
+    return !pte_is_table(pte, level);
+}

Don't you mean V=1 and (R=1 or X=1) here?

Agree, (R=1 or X=1) should be checked here too.

Quoted 10 lines
Quoted 5 lines
xen/arch/riscv/include/asm/page.h +2 -0
+/* Sanity check of the entry */
+static bool xen_pt_check_entry(pte_t entry, mfn_t mfn, unsigned

int level,
+                               unsigned int flags)

The comment wants extending to indicate what the parameters mean wrt what
is going to be checked. For example, ...

Quoted 8 lines
Quoted 3 lines

+{
+    /* Sanity check when modifying an entry. */
+    if ( mfn_eq(mfn, INVALID_MFN) )

... it's unclear to me why incoming INVALID_MFN would indicate modification
of an entry, whereas further down _PAGE_PRESENT supposedly indicates insertion.

The statement inside if isn't correct. It should be:
if ( (flags & _PAGE_PRESENT) && mfn_eq(mfn, INVALID_MFN) )

INVALID_MFN indicates modification because of how xen_pt_update() is used:
int map_pages_to_xen(unsigned long virt,
mfn_t mfn,
unsigned long nr_mfns,
unsigned int flags)

   {
       return xen_pt_update(virt, mfn, nr_mfns, flags);

}

   int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns)
   {
       return xen_pt_update(virt, INVALID_MFN, nr_mfns, _PAGE_PRESENT);
   }
   
   int destroy_xen_mappings(unsigned long s, unsigned long e)
   {
       ASSERT(IS_ALIGNED(s, PAGE_SIZE));
       ASSERT(IS_ALIGNED(e, PAGE_SIZE));
       ASSERT(s <= e);
       return xen_pt_update(s, INVALID_MFN, (e - s) >> PAGE_SHIFT, 0);
   }
   
   int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int
   nf)
   {
       ASSERT(IS_ALIGNED(s, PAGE_SIZE));
       ASSERT(IS_ALIGNED(e, PAGE_SIZE));
       ASSERT(s <= e);
       return xen_pt_update(s, INVALID_MFN, (e - s) >> PAGE_SHIFT, nf);
   }

The idea is the following:
the MFN is not valid and we are not populating page table. This means
we either modify entry or remove an entry.

Quoted 12 lines
Quoted 11 lines

+    {
+        /* We don't allow modifying an invalid entry. */ +        if ( !pte_is_valid(entry) )
+        {
+            printk("Modifying invalid entry is not allowed.\n"); +            return false;
+        }
+
+        /* We don't allow modifying a table entry */
+        if ( !pte_is_mapping(entry, level) )

Quoted 1 line

With what the comment say, why not pte_is_table()?

It should be pte_is_table(). I will double check, thanks.

Quoted 30 lines
Quoted 18 lines

+        {
+            printk("Modifying a table entry is not allowed.\n"); +            return false;
+        }
+    }
+    /* Sanity check when inserting a mapping */
+    else if ( flags & _PAGE_PRESENT )
+    {
+        /* We should be here with a valid MFN. */
+        ASSERT(!mfn_eq(mfn, INVALID_MFN));
+
+        /*
+         * We don't allow replacing any valid entry.
+         *
+         * Note that the function xen_pt_update() relies on this +         * assumption and will skip the TLB flush. The function will need

Quoted 8 lines

+         * to be updated if the check is relaxed.
+         */
+        if ( pte_is_valid(entry) )
+        {
+            if ( pte_is_mapping(entry, level) )
+                printk("Changing MFN for a valid entry is not allowed (%#"PRI_mfn" -> %#"PRI_mfn").\n",
+                          mfn_x(pte_get_mfn(entry)), mfn_x(mfn));

Nit: Indentation. (I'm once again worried by all of these printk()s anyway.)

Quoted 15 lines
Quoted 11 lines
xen/arch/riscv/include/asm/page.h +9 -0
+#define XEN_TABLE_MAP_FAILED 0
+#define XEN_TABLE_SUPER_PAGE 1
+#define XEN_TABLE_NORMAL_PAGE 2
+
+/*
+ * Take the currently mapped table, find the corresponding entry,
+ * and map the next table, if available.
+ *
+ * The read_only parameters indicates whether intermediate tables

should
+ * be allocated when not present.

"read_only" would have a different meaning to me. Maybe use inverted sense
with a name like "alloc"?

Sure, alloc_only would be better correspond to the comment.

Quoted 33 lines
Quoted 20 lines

+ * Return values:
+ *  XEN_TABLE_MAP_FAILED: Either read_only was set and the entry
+ *  was empty, or allocating a new page failed.
+ *  XEN_TABLE_NORMAL_PAGE: next level mapped normally
+ *  XEN_TABLE_SUPER_PAGE: The next entry points to a superpage.
+ */

xen/arch/riscv/include/asm/page.h +13 -0
+static int xen_pt_next_level(bool read_only, unsigned int level,
+                             pte_t **table, unsigned int offset)
+{
+    pte_t *entry;
+    int ret;
+    mfn_t mfn;
+
+    entry = *table + offset;
+
+    if ( !pte_is_valid(*entry) )
+    {
+        if ( read_only )
+            return XEN_TABLE_MAP_FAILED;

xen/arch/riscv/include/asm/page.h

Quoted 10 lines
xen/arch/riscv/include/asm/page.h +10 -0
+
+        ret = create_xen_table(entry);
+        if ( ret )
+            return XEN_TABLE_MAP_FAILED;
+    }
+
+    if ( pte_is_mapping(*entry, level) )
+    {
+        return XEN_TABLE_SUPER_PAGE;
+    }

Please be consistent with braces around single statements.

Quoted 9 lines

xen/arch/riscv/include/asm/page.h

Quoted 7 lines

+    mfn = pte_get_mfn(*entry);

xen/arch/riscv/include/asm/page.h +6 -0
+
+    xen_unmap_table(*table);
+    *table = xen_map_table(mfn);
+
+    return XEN_TABLE_NORMAL_PAGE;
+}

Normal page? Not normal table?

It just mean that this points not to super_page so potenially the in the next one table will have an entry that would be normal page.

Quoted 12 lines
Quoted 10 lines
xen/arch/riscv/include/asm/page.h +3 -0
+/* Update an entry at the level @target. */
+static int xen_pt_update_entry(mfn_t root, unsigned long virt,
+                               mfn_t mfn, unsigned int

arch_target,
+                               unsigned int flags) +{
+    int rc;
+    unsigned int level = HYP_PT_ROOT_LEVEL; +    unsigned int arch_level = level;

Why two level variables?

It is not needed anymore, I will drop it.

Quoted 10 lines
Quoted 9 lines

+    unsigned int target = arch_target;
+    pte_t *table;
+    /*
+     * The intermediate page tables are read-only when the MFN is not valid
+     * This means we either modify permissions or remove an entry. +     */
+    bool read_only = mfn_eq(mfn, INVALID_MFN);

Quoted 3 lines

I'm afraid I can't make a connection between the incoming MFN being INVALID_MFN and intermediate tables being intended to remain unaltered.

It is becuase of xen_pt_update() is used:
int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns)

   {
       return xen_pt_update(virt, INVALID_MFN, nr_mfns, _PAGE_PRESENT);

}
So if pt is only populated then they are read_only and so they shouldn't be allocated what means ptes are only or being removed or modified.

Quoted 91 lines
Quoted 6 lines
xen/arch/riscv/include/asm/page.h +4 -0
+
+static int xen_pt_update(unsigned long virt,
+                         mfn_t mfn,
+                         /* const on purpose as it is used for TLB

flush */
+                         const unsigned long nr_mfns,

I'm afraid I don't see what the comment is actually trying to tell me.
If this is about you wanting to make sure the function arguments aren't
altered prematurely, then why would the same not apply to virt, mfn, and flags (all of which matter at the time the TLB flush is initiated)?

Quoted 62 lines

+                         unsigned int flags)

xen/arch/riscv/include/asm/page.h +17 -0
+{
+    int rc = 0;
+    unsigned long vfn = virt >> PAGE_SHIFT;
+    unsigned long left = nr_mfns;
+
+    const mfn_t root = get_root_page();
+
+    /*
+     * It is bad idea to have mapping both writeable and
+     * executable.
+     * When modifying/creating mapping (i.e _PAGE_PRESENT is set),
+     * prevent any update if this happen.
+     */
+    if ( (flags & _PAGE_PRESENT) && !PAGE_RO_MASK(flags) &&
+         !PAGE_XN_MASK(flags) )
+    {
+        printk("Mappings should not be both Writeable and

Executable.\n");
+        return -EINVAL;
+    }
+
+    if ( !IS_ALIGNED(virt, PAGE_SIZE) )
+    {
+        printk("The virtual address is not aligned to the page- size.\n");
+        return -EINVAL;
+    }

xen/arch/riscv/include/asm/page.h +13 -0
+
+    spin_lock(&xen_pt_lock);
+
+    while ( left )
+    {
+        unsigned int order, level;
+
+        level = xen_pt_mapping_level(vfn, mfn, left, flags);
+        order = XEN_PT_LEVEL_ORDER(level);
+
+        ASSERT(left >= BIT(order, UL));
+
+        rc = xen_pt_update_entry(root, vfn << PAGE_SHIFT, mfn,

level,
+                                    flags);
+        if ( rc )
+            break;

xen/arch/riscv/include/asm/page.h +13 -0
+
+        vfn += 1U << order;
+        if ( !mfn_eq(mfn, INVALID_MFN) )
+            mfn = mfn_add(mfn, 1U << order);
+
+        left -= (1U << order);
+
+        if ( rc )
+            break;
+    }
+
+    /*
+     * The TLBs flush can be safely skipped when a mapping is

inserted
+     * as we don't allow mapping replacement (see xen_pt_check_entry()).
+     *

Quoted 8 lines

+     * For all the other cases, the TLBs will be flushed unconditionally
+     * even if the mapping has failed. This is because we may have +     * partially modified the PT. This will prevent any unexpected +     * behavior afterwards.
+     */
+    if ( !((flags & _PAGE_PRESENT) && !mfn_eq(mfn, INVALID_MFN)) ) +        flush_xen_tlb_range_va(virt, PAGE_SIZE * nr_mfns);

See my comments elsewhere towards TLB behavior on RISC-V. The indicated
skipping of a flush is safe only if not-present entries cannot be put in the TLB.

Quoted 14 lines

xen/arch/riscv/include/asm/page.h

Quoted 12 lines

+    spin_unlock(&xen_pt_lock);

xen/arch/riscv/include/asm/page.h +11 -0
+
+    return rc;
+}
+
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)
+{
+    return xen_pt_update(virt, mfn, nr_mfns, flags);
+}

Why this wrapping of two functions taking identical arguments?

map_pages_to_xen() sounds more clear regarding the way how it should be used.

xen_pt_update() will be also used inside other functions. Look at the example above.

~ Oleksii

#49 lore raw

Re: [PATCH v3 8/9] xen/riscv: page table handling

Quoted 14 lines · On 01.08.2024 11:33, [email protected] wrote:
Quoted 13 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h +0 -0
@@ -34,6 +34,7 @@
  #define PTE_LEAF_DEFAULT            (PTE_VALID | PTE_READABLE |
 PTE_WRITABLE)
  #define PTE_TABLE                   (PTE_VALID)


+#define PAGE_HYPERVISOR_RO          (PTE_VALID | PTE_READABLE) #define PAGE_HYPERVISOR_RW          (PTE_VALID | PTE_READABLE | PTE_WRITABLE)

No PAGE_HYPERVISOR_RX?

Quoted 1 line

I haven't used it at the moment, so I haven't provided it.

I'm inclined to suggest to put it there right away. You will need it rather sooner than later.

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines

+    unsigned long pbmt:2;
+    unsigned long n:1;

xen/arch/riscv/include/asm/page.h +10 -0
+#elif RV_STAGE1_MODE == SATP_MODE_SV48
+    unsigned long ppn0:9;
+    unsigned long ppn1:9;
+    unsigned long ppn2:9;
+    unsigned long ppn3:17;
+    unsigned long rsw2:7;
+    unsigned long pbmt:2;
+    unsigned long n:1;
+#else
+#error "Add proper bits for SATP_MODE"
Quoted 8 lines

xen/arch/riscv/include/asm/page.h

Quoted 2 lines
xen/arch/riscv/include/asm/page.h +2 -0
+#endif
+} pt_t;

I can't spot a use anywhere of e.g. ppn0. Would be nice to understand in
what contexts these bitfields are going to be used. I notice you specifically
don't use them in e.g. pte_is_table().

Quoted 3 lines

I don't use them at the moment. I just introduced them for the possible future using. I can re-check what of them I am using in my private branches and come up here only with that one which are really used.

Just to clarify: If you need any of the bitfields, then of course you want to introduce all of them, properly named. Yet with the PTE_* constants I'm wondering whether really you need them in addition.

Quoted 52 lines
Quoted 9 lines
Quoted 4 lines
xen/arch/riscv/include/asm/page.h +2 -0
+/* Sanity check of the entry */
+static bool xen_pt_check_entry(pte_t entry, mfn_t mfn, unsigned

int level,
+                               unsigned int flags)

The comment wants extending to indicate what the parameters mean wrt what
is going to be checked. For example, ...

Quoted 8 lines
Quoted 3 lines

+{
+    /* Sanity check when modifying an entry. */
+    if ( mfn_eq(mfn, INVALID_MFN) )

... it's unclear to me why incoming INVALID_MFN would indicate modification
of an entry, whereas further down _PAGE_PRESENT supposedly indicates insertion.

The statement inside if isn't correct. It should be:

   if ( (flags & _PAGE_PRESENT) && mfn_eq(mfn, INVALID_MFN) )

INVALID_MFN indicates modification because of how xen_pt_update() is used:

   int map_pages_to_xen(unsigned long virt,
                        mfn_t mfn,
                        unsigned long nr_mfns,
                        unsigned int flags)
   {
       return xen_pt_update(virt, mfn, nr_mfns, flags);
   }
   
   int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns)
   {
       return xen_pt_update(virt, INVALID_MFN, nr_mfns, _PAGE_PRESENT);
   }
   
   int destroy_xen_mappings(unsigned long s, unsigned long e)
   {
       ASSERT(IS_ALIGNED(s, PAGE_SIZE));
       ASSERT(IS_ALIGNED(e, PAGE_SIZE));
       ASSERT(s <= e);
       return xen_pt_update(s, INVALID_MFN, (e - s) >> PAGE_SHIFT, 0);
   }
   
   int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int
   nf)
   {
       ASSERT(IS_ALIGNED(s, PAGE_SIZE));
       ASSERT(IS_ALIGNED(e, PAGE_SIZE));
       ASSERT(s <= e);
       return xen_pt_update(s, INVALID_MFN, (e - s) >> PAGE_SHIFT, nf);
   }
Quoted 3 lines

The idea is the following:
the MFN is not valid and we are not populating page table. This means
we either modify entry or remove an entry.

Right. My request stands though: The comment ahead of the function wants extending to state how it is to be used correctly.

Quoted 11 lines
Quoted 9 lines

xen/arch/riscv/include/asm/page.h

Quoted 7 lines

+    mfn = pte_get_mfn(*entry);

xen/arch/riscv/include/asm/page.h +6 -0
+
+    xen_unmap_table(*table);
+    *table = xen_map_table(mfn);
+
+    return XEN_TABLE_NORMAL_PAGE;
+}

Normal page? Not normal table?

It just mean that this points not to super_page so potenially the in the next one table will have an entry that would be normal page.

Or a normal page table, if you haven't reached leaf level yet. IOW maybe better XEN_TABLE_NORMAL, covering both cases?

Quoted 13 lines
Quoted 9 lines
Quoted 8 lines

+    unsigned int target = arch_target;
+    pte_t *table;
+    /*
+     * The intermediate page tables are read-only when the MFN is not valid
+     * This means we either modify permissions or remove an entry. +     */
+    bool read_only = mfn_eq(mfn, INVALID_MFN);

Quoted 3 lines

I'm afraid I can't make a connection between the incoming MFN being INVALID_MFN and intermediate tables being intended to remain unaltered.

Quoted 8 lines

It is becuase of xen_pt_update() is used:

   int __init populate_pt_range(unsigned long virt, unsigned long
   nr_mfns)
   {
       return xen_pt_update(virt, INVALID_MFN, nr_mfns, _PAGE_PRESENT);
   }

So if pt is only populated then they are read_only and so they shouldn't be allocated what means ptes are only or being removed or modified.

Like above, such special assumptions would better be put in a comment.

Quoted 9 lines
Quoted 9 lines

xen/arch/riscv/include/asm/page.h

Quoted 7 lines
xen/arch/riscv/include/asm/page.h +7 -0
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)
+{
+    return xen_pt_update(virt, mfn, nr_mfns, flags);
+}

Why this wrapping of two functions taking identical arguments?

Quoted 5 lines

map_pages_to_xen() sounds more clear regarding the way how it should be used.

xen_pt_update() will be also used inside other functions. Look at the example above.

They could as well use map_pages_to_xen() then? Or else the wrapper may want to check (assert) that it is _not_ called with one of the special case arguments that xen_pt_update() knows how to deal with?

Jan

#50 lore raw

Re: [PATCH v3 8/9] xen/riscv: page table handling

Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 11 lines

xen/arch/riscv/pt.c line 194

Quoted 9 lines

+    mfn = pte_get_mfn(*entry);

xen/arch/riscv/pt.c new file +6 -0
+
+    xen_unmap_table(*table);
+    *table = xen_map_table(mfn);
+
+    return XEN_TABLE_NORMAL_PAGE;
+}

Normal page? Not normal table?

Quoted 7 lines
Quoted 3 lines

It just mean that this points not to super_page so potenially the in
the next one table will have an entry that would be normal page.

Or a normal page table, if you haven't reached leaf level yet. IOW maybe
better XEN_TABLE_NORMAL, covering both cases?

It would be better. Thanks.

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines

xen/arch/riscv/pt.c line 410

Quoted 8 lines
xen/arch/riscv/pt.c new file +7 -0
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)
+{
+    return xen_pt_update(virt, mfn, nr_mfns, flags);
+}

Why this wrapping of two functions taking identical arguments?

Quoted 7 lines

map_pages_to_xen() sounds more clear regarding the way how it should be
used.

xen_pt_update() will be also used inside other functions. Look at the
example above.

Quoted 5 lines

They could as well use map_pages_to_xen() then? Or else the wrapper may
want to check (assert) that it is _not_ called with one of the special
case arguments that xen_pt_update() knows how to deal with?

Yes, map_pages_to_xen() will be used in other functions/wrappers. At the momemnt, I don't see what should be checked additionally in map_pages_to_xen(). It seems to me that xen_pt_update() covers all the checks at the start it needs for now. ( i will double-check that ).

~ Oleksii

#51 lore raw

Re: [PATCH v3 8/9] xen/riscv: page table handling

Quoted 23 lines · On 01.08.2024 13:55, [email protected] wrote:
Quoted 17 lines · Jan Beulich
Quoted 9 lines
Quoted 9 lines

xen/arch/riscv/pt.c line 410

Quoted 7 lines
xen/arch/riscv/pt.c new file +7 -0
+int map_pages_to_xen(unsigned long virt,
+                     mfn_t mfn,
+                     unsigned long nr_mfns,
+                     unsigned int flags)
+{
+    return xen_pt_update(virt, mfn, nr_mfns, flags);
+}

Why this wrapping of two functions taking identical arguments?

Quoted 7 lines

map_pages_to_xen() sounds more clear regarding the way how it should be
used.

xen_pt_update() will be also used inside other functions. Look at the
example above.

Quoted 5 lines

They could as well use map_pages_to_xen() then? Or else the wrapper may
want to check (assert) that it is _not_ called with one of the special
case arguments that xen_pt_update() knows how to deal with?

Quoted 4 lines

Yes, map_pages_to_xen() will be used in other functions/wrappers. At the momemnt, I don't see what should be checked additionally in map_pages_to_xen(). It seems to me that xen_pt_update() covers all the checks at the start it needs for now. ( i will double-check that ).

I was referring to cases that xen_pt_update() can handle, but that map_pages_to_xen() isn't supposed to be handling (if already you want to separate both).

Jan

#52 lore raw

PATCH v3 9/9 xen/riscv: introduce early_fdt_map()

In reply to Oleksii Kurochko

Introduce function which allows to map FDT to Xen.

Also, initialization of device_tree_flattened happens using early_fdt_map.

---
Changes in V3:
- Code style fixes
- s/SZ_2M/MB(2)
- fix condition to check if early_fdt_map() in setup.c return NULL or not.
---
Changes in V2:
- rework early_fdt_map to use map_pages_to_xen()
- move call early_fdt_map() to C code after MMU is enabled.

---
 xen/arch/riscv/include/asm/mm.h |  2 ++
 xen/arch/riscv/mm.c             | 55 +++++++++++++++++++++++++++++++++
 xen/arch/riscv/setup.c          |  9 ++++++
 3 files changed, 66 insertions(+)
xen/arch/riscv/include/asm/mm.h +2 -0
index a7550e77a7..56375bf833 100644
@@ -259,4 +259,6 @@ static inline unsigned int arch_get_dma_bitsize(void)
 
 void setup_fixmap_mappings(void);
 
+void *early_fdt_map(paddr_t fdt_paddr);
+
 #endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/mm.c +55 -0

71 lines not on this page · raw message

xen/arch/riscv/setup.c +9 -0
index 497e273081..e5f32a142a 100644
@@ -2,6 +2,7 @@
 
 #include <xen/bug.h>
 #include <xen/compile.h>
+#include <xen/device_tree.h>
 #include <xen/init.h>
 #include <xen/mm.h>
 
@@ -64,6 +65,14 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     setup_fixmap_mappings();
 
+    device_tree_flattened = early_fdt_map(dtb_addr);
+    if ( !device_tree_flattened )
+        panic("Invalid device tree blob at physical address %#lx.\n"
+              "The DTB must be 8-byte aligned and must not exceed %lld "
+              "bytes in size.\n\n"
+              "Please check your bootloader.\n",
+              dtb_addr, BOOT_FDT_VIRT_SIZE);
+
     printk("All set up\n");
 
     for ( ;; )
Signature · 1 line
2.45.2
Acked-by on v3 9/9
#53 lore raw

Re: [PATCH v3 9/9] xen/riscv: introduce early_fdt_map()

Quoted 5 lines · Oleksii Kurochko

Introduce function which allows to map FDT to Xen.

Also, initialization of device_tree_flattened happens using early_fdt_map.

with ...

xen/arch/riscv/setup.c void __init noreturn start_xen(unsigned long bootcpu_id, · line 72

Quoted 9 lines
xen/arch/riscv/setup.c +5 -0
@@ -64,6 +65,14 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     setup_fixmap_mappings();
 
+    device_tree_flattened = early_fdt_map(dtb_addr);
+    if ( !device_tree_flattened )
+        panic("Invalid device tree blob at physical address %#lx.\n"
+              "The DTB must be 8-byte aligned and must not exceed %lld "
+              "bytes in size.\n\n"

... these two lines unwrapped, such that grep-ing for any part of the message line will always produce a hit. Line length restrictions don't apply in such situation, for this very reason.

Jan