Skip to contentSkip to conversation

enable MMU for RISC-V

PATCH Superseded Oleksii Kurochko 2 participants 10 messages May 3, 2023 – May 11, 2023 RISC-V

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#8 lore raw

PATCH v6 3/4 xen/riscv: setup initial pagetables

In reply to Oleksii Kurochko

The patch does two thing:
1. Setup initial pagetables.
2. Enable MMU which end up with code in
cont_after_mmu_is_enabled()

---
Changes in V6:
- Nothing changed. Only rebase
---
Changes in V5:
- Nothing changed. Only rebase
---
Changes in V4:
- Nothing changed. Only rebase
---
Changes in V3:
- update the commit message that MMU is also enabled here
- remove early_printk("All set up\n") as it was moved to cont_after_mmu_is_enabled() function after MMU is enabled.
---
Changes in V2:
* Update the commit message

---
 xen/arch/riscv/setup.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
xen/arch/riscv/setup.c +4 -1
index 315804aa87..cf5dc5824e 100644
@@ -21,7 +21,10 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 {
     early_printk("Hello from C env\n");
 
-    early_printk("All set up\n");
+    setup_initial_pagetables();
+
+    enable_mmu();
+
     for ( ;; )
         asm volatile ("wfi");
 
Signature · 1 line
2.40.1
#0 lore raw

v6 cover enable MMU for RISC-V

The patch series introduces the following things:
1. Functionality to build the page tables for Xen that map link-time to physical-time location.
2. Check that Xen is less then page size.
3. Check that load addresses don't overlap with linker addresses.
4. Prepare things for proper switch to virtual memory world.
5. Load the built page table into the SATP
6. Enable MMU.

---
Changes in V6:
- update RV VM layout and related to it things
- move PAGE_SHIFT, PADDR_BITS to the top of page-bits.h
- cast argument x of pte_to_addr() macros to paddr_t to avoid risk of overflow for RV32
- update type of num_levels from 'unsigned long' to 'unsigned int'
- define PGTBL_INITIAL_COUNT as ((CONFIG_PAGING_LEVELS - 1) + 1)
- update type of permission arguments. changed it from 'unsgined long' to 'unsigned int'
- fix code style
- switch while 'loop' to 'for' loop
- undef HANDLE_PGTBL
- clean root page table after MMU is disabled in check_pgtbl_mode_support() function
- align __bss_start properly
- remove unnecesssary const for paddr_to_pte, pte_to_paddr, pte_is_valid functions
- add switch_stack_and_jump macros and use it inside enable_mmu() before jump to cont_after_mmu_is_enabled() function

---
Changes in V5:
* rebase the patch series on top of current staging
* Update cover letter: it was removed the info about the patches on which
MMU patch series is based as they were merged to staging
* add new patch with description of VM layout for RISC-V2
* Indent fields of pte_t struct
* Rename addr_to_pte() and ppn_to_paddr() to match their content
---
Changes in V4:
* use GB() macros instead of defining SZ_1G
* hardcode XEN_VIRT_START and add comment (ADDRESS_SPACE_END + 1 - GB(1))
* remove unnecessary 'asm' word at the end of #error
* encapsulate pte_t definition in a struct
* rename addr_to_ppn() to ppn_to_paddr().
* change type of paddr argument from const unsigned long to paddr_t
* pte_to_paddr() update prototype.
* calculate size of Xen binary based on an amount of page tables
* use unsgined int instead of 'uint32_t' instead of uint32_t as
their use isn't warranted.
* remove extern of bss_{start,end} as they aren't used in mm.c anymore
* fix code style
* add argument for HANDLE_PGTBL macros instead of curr_lvl_num variable
* make enable_mmu() as noinline to prevent under link-time optimization
because of the nature of enable_mmu()
* add function to check that SATP_MODE is supported.
* update the commit message
* update setup_initial_pagetables to set correct PTE flags in one pass
instead of calling setup_pte_permissions after setup_initial_pagetables()
as setup_initial_pagetables() isn't used to change permission flags.
---
Changes in V3:
* Update the cover letter message: the patch series isn't depend on
[ RISC-V basic exception handling implementation ] as it was decied
to enable MMU before implementation of exception handling. Also MMU
patch series is based on two other patches which weren't merged [1]
and [2]
- Update the commit message for [ [PATCH v3 1/3]
xen/riscv: introduce setup_initial_pages ].
- update definition of pte_t structure to have a proper size of pte_t in case of RV32.
- update asm/mm.h with new functions and remove unnecessary 'extern'.
- remove LEVEL_* macros as only XEN_PT_LEVEL_* are enough.
- update paddr_to_pte() to receive permissions as an argument.
- add check that map_start & pa_start is properly aligned.
- move defines PAGETABLE_ORDER, PAGETABLE_ENTRIES, PTE_PPN_SHIFT to <asm/page-bits.h>
- Rename PTE_SHIFT to PTE_PPN_SHIFT
- refactor setup_initial_pagetables: map all LINK addresses to LOAD addresses and after setup PTEs permission for sections; update check that linker and load addresses don't overlap.
- refactor setup_initial_mapping: allocate pagetable 'dynamically' if it is necessary.
- rewrite enable_mmu in C; add the check that map_start and pa_start are aligned on 4k boundary.
- update the comment for setup_initial_pagetable funcion
- Add RV_STAGE1_MODE to support different MMU modes.
- update the commit message that MMU is also enabled here
- set XEN_VIRT_START very high to not overlap with load address range
- align bss section
---
Changes in V2:
* Remove {ZEROETH,FIRST,...}_{SHIFT,MASK, SIZE,...} and
introduce instead of them XEN_PT_LEVEL_*() and LEVEL_*
* Rework pt_linear_offset() and pt_index based on XEN_PT_LEVEL_*()
* Remove clear_pagetables() functions as pagetables were zeroed during
.bss initialization
* Rename _setup_initial_pagetables() to setup_initial_mapping()
* Make PTE_DEFAULT equal to RX.
* Update prototype of setup_initial_mapping(..., bool writable) -> setup_initial_mapping(..., UL flags)
* Update calls of setup_initial_mapping according to new prototype
* Remove unnecessary call of:
_setup_initial_pagetables(..., load_addr_start, load_addr_end, load_addr_start, ...)
* Define index* in the loop of setup_initial_mapping
* Remove attribute "__attribute__((section(".entry")))" for setup_initial_pagetables() as we don't have such section
* make arguments of paddr_to_pte() and pte_is_valid() as const.
* use <xen/kernel.h> instead of declaring extern unsigned long _stext, 0etext, _srodata, _erodata
* update 'extern unsigned long __init_begin' to 'extern unsigned long __init_begin[]'
* use aligned() instead of "__attribute__((__aligned__(PAGE_SIZE)))"
* set __section(".bss.page_aligned") for page tables arrays
* fix identatations
* Change '__attribute__((section(".entry")))' to '__init'
* Remove alignment of {map, pa}_start &= XEN_PT_LEVEL_MAP_MASK(0); in setup_inital_mapping() as they should be already aligned.
* Remove clear_pagetables() as initial pagetables will be
zeroed during bss initialization
* Remove __attribute__((section(".entry")) for setup_initial_pagetables()
as there is no such section in xen.lds.S
* Update the argument of pte_is_valid() to "const pte_t *p"
* Remove patch "[PATCH v1 3/3] automation: update RISC-V smoke test" from the patch series as it was introduced simplified approach for RISC-V smoke test by Andrew Cooper
* Add patch [[xen/riscv: remove dummy_bss variable] as there is no any sense in dummy_bss variable after introduction of inittial page tables.
---

Oleksii Kurochko (4):
xen/riscv: add VM space layout
xen/riscv: introduce setup_initial_pages
xen/riscv: setup initial pagetables
xen/riscv: remove dummy_bss variable

 xen/arch/riscv/Makefile                |   1 +
 xen/arch/riscv/include/asm/config.h    |  44 +++-
 xen/arch/riscv/include/asm/current.h   |  10 +
 xen/arch/riscv/include/asm/mm.h        |   9 +
 xen/arch/riscv/include/asm/page-bits.h |  10 +
 xen/arch/riscv/include/asm/page.h      |  62 +++++
 xen/arch/riscv/mm.c                    | 315 +++++++++++++++++++++++++
 xen/arch/riscv/riscv64/head.S          |   1 +
 xen/arch/riscv/setup.c                 |  22 +-
 xen/arch/riscv/xen.lds.S               |   4 +
 10 files changed, 469 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/riscv/include/asm/current.h
 create mode 100644 xen/arch/riscv/include/asm/mm.h
 create mode 100644 xen/arch/riscv/include/asm/page.h
 create mode 100644 xen/arch/riscv/mm.c
Signature · 1 line
2.40.1
#9 lore raw

PATCH v6 4/4 xen/riscv: remove dummy_bss variable

After introduction of initial pagetables there is no any sense in dummy_bss variable as bss section will not be empty anymore.

---
Changes in V6:
- Nothing changed. Only rebase
---
Changes in V5:
- Nothing changed. Only rebase
---
Changes in V4:
- Nothing changed. Only rebase
---
Changes in V3:
* patch was introduced in the current one patch series (v3).
---
Changes in V2:
* patch was introduced in the current one patch series (v2).

---
 xen/arch/riscv/setup.c | 8 --------
 1 file changed, 8 deletions(-)
xen/arch/riscv/setup.c +0 -8
index cf5dc5824e..845d18d86f 100644
@@ -8,14 +8,6 @@
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
     __aligned(STACK_SIZE);
 
-/*  
- * To be sure that .bss isn't zero. It will simplify code of
- * .bss initialization.
- * TODO:
- *   To be deleted when the first real .bss user appears
- */
-int dummy_bss __attribute__((unused));
-
 void __init noreturn start_xen(unsigned long bootcpu_id,
                                paddr_t dtb_addr)
 {
Signature · 1 line
2.40.1
#1 lore raw

PATCH v6 1/4 xen/riscv: add VM space layout

In reply to Oleksii Kurochko

Also it was added explanation about ignoring of top VA bits

---
Changes in V6:
- update comment above the RISCV-64 layout table
- add Slot column to the table with RISCV-64 Layout
- update RV-64 layout table.
---
Changes in V5:
* the patch was introduced in the current patch series.

---
 xen/arch/riscv/include/asm/config.h | 31 +++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
xen/arch/riscv/include/asm/config.h +31 -0
index 763a922a04..73b86ce789 100644
@@ -4,6 +4,37 @@
 #include <xen/const.h>
 #include <xen/page-size.h>
 
+/*
+ * RISC-V64 Layout:
+ *
+ * From the riscv-privileged doc:
+ *   When mapping between narrower and wider addresses,
+ *   RISC-V zero-extends a narrower physical address to a wider size.
+ *   The mapping between 64-bit virtual addresses and the 39-bit usable
+ *   address space of Sv39 is not based on zero-extension but instead
+ *   follows an entrenched convention that allows an OS to use one or
+ *   a few of the most-significant bits of a full-size (64-bit) virtual
+ *   address to quickly distinguish user and supervisor address regions.
+ *
+ * It means that:
+ *   top VA bits are simply ignored for the purpose of translating to PA.
+ *
+ * ============================================================================
+ *    Start addr    |   End addr        |  Size  | Slot       |area description
+ * ============================================================================
+ * FFFFFFFFC0000000 |  FFFFFFFFC0200000 |  2 MB  | L2 511     | Xen
+ * FFFFFFFFC0200000 |  FFFFFFFFC0600000 |  4 MB  | L2 511     | FDT
+ * FFFFFFFFC0600000 |  FFFFFFFFC0800000 |  2 MB  | L2 511     | Fixmap
+ *                 ...                  |  1 GB  | L2 510     | Unused
+ * 0000003200000000 |  0000007f40000000 | 331 GB | L2 200-609 | Direct map
+ *                 ...                  |  1 GB  | L2 199     | Unused
+ * 0000003100000000 |  0000003140000000 |  3 GB  | L2 196-198 | Frametable
+ *                 ...                  |  1 GB  | L2 195     | Unused
+ * 0000003080000000 |  00000030c0000000 |  1 GB  | L2 194     | VMAP
+ *     .................. unused ..................
+ * ============================================================================
+ */
+
 #if defined(CONFIG_RISCV_64)
 # define LONG_BYTEORDER 3
 # define ELFSIZE 64
Signature · 1 line
2.40.1
#3 lore raw

PATCH v6 2/4 xen/riscv: introduce setup_initial_pages

In reply to Oleksii Kurochko

The idea was taken from xvisor but the following changes
were done:
* Use only a minimal part of the code enough to enable MMU
* rename {_}setup_initial_pagetables functions
* add an argument for setup_initial_mapping to have
an opportunity to make set PTE flags.
* update setup_initial_pagetables function to map sections with correct PTE flags.
* Rewrite enable_mmu() to C.
* map linker addresses range to load addresses range without 1:1 mapping. It will be 1:1 only in case when load_start_addr is equal to linker_start_addr.
* add safety checks such as:
* Xen size is less than page size
* linker addresses range doesn't overlap load addresses range
* Rework macros {THIRD,SECOND,FIRST,ZEROETH}_{SHIFT,MASK}
* change PTE_LEAF_DEFAULT to RW instead of RWX.
* Remove phys_offset as it is not used now
* Remove alignment of {map, pa}_start &= XEN_PT_LEVEL_MAP_MASK(0); in setup_inital_mapping() as they should be already aligned. Make a check that {map_pa}_start are aligned.
* Remove clear_pagetables() as initial pagetables will be zeroed during bss initialization
* Remove __attribute__((section(".entry")) for setup_initial_pagetables() as there is no such section in xen.lds.S
* Update the argument of pte_is_valid() to "const pte_t *p"
* Add check that Xen's load address is aligned at 4k boundary
* Refactor setup_initial_pagetables() so it is mapping linker address range to load address range. After setup needed permissions for specific section ( such as .text, .rodata, etc ) otherwise RW permission will be set by default.
* Add function to check that requested SATP_MODE is supported

Origin: [email protected]:xvisor/xvisor.git 9be2fdd7

---
Changes in V6:
- move PAGE_SHIFT, PADDR_BITS to the top of page-bits.h
- cast argument x of pte_to_addr() macros to paddr_t to avoid risk of overflow for RV32
- update type of num_levels from 'unsigned long' to 'unsigned int'
- define PGTBL_INITIAL_COUNT as ((CONFIG_PAGING_LEVELS - 1) + 1)
- update type of permission arguments. changed it from 'unsgined long' to 'unsigned int'
- fix code style
- switch while 'loop' to 'for' loop
- undef HANDLE_PGTBL
- clean root page table after MMU is disabled in check_pgtbl_mode_support() function
- align __bss_start properly
- remove unnecesssary const for paddr_to_pte, pte_to_paddr, pte_is_valid functions
- add switch_stack_and_jump macros and use it inside enable_mmu() before jump to cont_after_mmu_is_enabled() function
---
Changes in V5:
* Indent fields of pte_t struct
* Rename addr_to_pte() and ppn_to_paddr() to match their content
---
Changes in V4:
* use GB() macros instead of defining SZ_1G
* hardcode XEN_VIRT_START and add comment (ADDRESS_SPACE_END + 1 - GB(1))
* remove unnecessary 'asm' word at the end of #error
* encapsulate pte_t definition in a struct
* rename addr_to_ppn() to ppn_to_paddr().
* change type of paddr argument from const unsigned long to paddr_t
* pte_to_paddr() update prototype.
* calculate size of Xen binary based on an amount of page tables
* use unsgined int instead of 'uint32_t' instead of uint32_t as
their use isn't warranted.
* remove extern of bss_{start,end} as they aren't used in mm.c anymore
* fix code style
* add argument for HANDLE_PGTBL macros instead of curr_lvl_num variable
* make enable_mmu() as noinline to prevent under link-time optimization
because of the nature of enable_mmu()
* add function to check that SATP_MODE is supported.
* update the commit message
* update setup_initial_pagetables to set correct PTE flags in one pass
instead of calling setup_pte_permissions after setup_initial_pagetables()
as setup_initial_pagetables() isn't used to change permission flags.
---
Changes in V3:
- update definition of pte_t structure to have a proper size of pte_t
in case of RV32.
- update asm/mm.h with new functions and remove unnecessary 'extern'.
- remove LEVEL_* macros as only XEN_PT_LEVEL_* are enough.
- update paddr_to_pte() to receive permissions as an argument.
- add check that map_start & pa_start is properly aligned.
- move defines PAGETABLE_ORDER, PAGETABLE_ENTRIES, PTE_PPN_SHIFT to <asm/page-bits.h>
- Rename PTE_SHIFT to PTE_PPN_SHIFT
- refactor setup_initial_pagetables: map all LINK addresses to LOAD addresses
and after setup PTEs permission for sections; update check that linker
and load addresses don't overlap.
- refactor setup_initial_mapping: allocate pagetable 'dynamically' if it is necessary.
- rewrite enable_mmu in C; add the check that map_start and pa_start are
aligned on 4k boundary.
- update the comment for setup_initial_pagetable funcion
- Add RV_STAGE1_MODE to support different MMU modes
- set XEN_VIRT_START very high to not overlap with load address range
- align bss section
---
Changes in V2:
* update the commit message:
* Remove {ZEROETH,FIRST,...}_{SHIFT,MASK, SIZE,...} and
introduce instead of them XEN_PT_LEVEL_*() and LEVEL_*
* Rework pt_linear_offset() and pt_index based on XEN_PT_LEVEL_*()
* Remove clear_pagetables() functions as pagetables were zeroed during
.bss initialization
* Rename _setup_initial_pagetables() to setup_initial_mapping()
* Make PTE_DEFAULT equal to RX.
* Update prototype of setup_initial_mapping(..., bool writable) -> setup_initial_mapping(..., UL flags)
* Update calls of setup_initial_mapping according to new prototype
* Remove unnecessary call of:
_setup_initial_pagetables(..., load_addr_start, load_addr_end, load_addr_start, ...)
* Define index* in the loop of setup_initial_mapping
* Remove attribute "__attribute__((section(".entry")))" for setup_initial_pagetables() as we don't have such section
* make arguments of paddr_to_pte() and pte_is_valid() as const.
* make xen_second_pagetable static.
* use <xen/kernel.h> instead of declaring extern unsigned long _stext, 0etext, _srodata, _erodata
* update 'extern unsigned long __init_begin' to 'extern unsigned long __init_begin[]'
* use aligned() instead of "__attribute__((__aligned__(PAGE_SIZE)))"
* set __section(".bss.page_aligned") for page tables arrays
* fix identatations
* Change '__attribute__((section(".entry")))' to '__init'
* Remove phys_offset as it isn't used now.
* Remove alignment of {map, pa}_start &= XEN_PT_LEVEL_MAP_MASK(0); in setup_inital_mapping() as they should be already aligned.
* Remove clear_pagetables() as initial pagetables will be
zeroed during bss initialization
* Remove __attribute__((section(".entry")) for setup_initial_pagetables()
as there is no such section in xen.lds.S
* Update the argument of pte_is_valid() to "const pte_t *p"

---
 xen/arch/riscv/Makefile                |   1 +
 xen/arch/riscv/include/asm/config.h    |  13 +-
 xen/arch/riscv/include/asm/current.h   |  10 +
 xen/arch/riscv/include/asm/mm.h        |   9 +
 xen/arch/riscv/include/asm/page-bits.h |  10 +
 xen/arch/riscv/include/asm/page.h      |  62 +++++
 xen/arch/riscv/mm.c                    | 315 +++++++++++++++++++++++++
 xen/arch/riscv/riscv64/head.S          |   1 +
 xen/arch/riscv/setup.c                 |  11 +
 xen/arch/riscv/xen.lds.S               |   4 +
 10 files changed, 435 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/riscv/include/asm/current.h
 create mode 100644 xen/arch/riscv/include/asm/mm.h
 create mode 100644 xen/arch/riscv/include/asm/page.h
 create mode 100644 xen/arch/riscv/mm.c
xen/arch/riscv/Makefile +1 -0
index 443f6bf15f..956ceb02df 100644
@@ -1,5 +1,6 @@
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
 obj-y += entry.o
+obj-y += mm.o
 obj-$(CONFIG_RISCV_64) += riscv64/
 obj-y += sbi.o
 obj-y += setup.o
xen/arch/riscv/include/asm/config.h +12 -1
index 73b86ce789..3bd206766e 100644
@@ -70,12 +70,23 @@
   name:
 #endif
 
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START 0xFFFFFFFFC0000000 /* (_AC(-1, UL) + 1 - GB(1)) */
+#else
+#error "RV32 isn't supported"
+#endif
 
 #define SMP_CACHE_BYTES (1 << 6)
 
 #define STACK_SIZE PAGE_SIZE
 
+#ifdef CONFIG_RISCV_64
+#define CONFIG_PAGING_LEVELS 3
+#define RV_STAGE1_MODE SATP_MODE_SV39
+#else
+#define RV_STAGE1_MODE SATP_MODE_SV32
+#endif
+
 #endif /* __RISCV_CONFIG_H__ */
 /*
  * Local variables:
xen/arch/riscv/include/asm/current.h new file +10 -0
new file mode 100644
index 0000000000..1cb5946fe9
@@ -0,0 +1,10 @@
+#ifndef __ASM_CURRENT_H
+#define __ASM_CURRENT_H
+
+#define switch_stack_and_jump(stack, fn)    \
+    asm volatile (                          \
+            "mv sp, %0 \n"                  \
+            "j " #fn :: "r" (stack) :       \
+    )
+
+#endif /* __ASM_CURRENT_H */
xen/arch/riscv/include/asm/mm.h new file +9 -0
new file mode 100644
index 0000000000..e16ce66fae
@@ -0,0 +1,9 @@
+#ifndef _ASM_RISCV_MM_H
+#define _ASM_RISCV_MM_H
+
+void setup_initial_pagetables(void);
+
+void enable_mmu(void);
+void cont_after_mmu_is_enabled(void);
+
+#endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/include/asm/page-bits.h +10 -0
index 1801820294..4a3e33589a 100644
@@ -4,4 +4,14 @@
 #define PAGE_SHIFT              12 /* 4 KiB Pages */
 #define PADDR_BITS              56 /* 44-bit PPN */
 
+#ifdef CONFIG_RISCV_64
+#define PAGETABLE_ORDER         (9)
+#else /* CONFIG_RISCV_32 */
+#define PAGETABLE_ORDER         (10)
+#endif
+
+#define PAGETABLE_ENTRIES       (1 << PAGETABLE_ORDER)
+
+#define PTE_PPN_SHIFT           10
+
 #endif /* __RISCV_PAGE_BITS_H__ */
xen/arch/riscv/include/asm/page.h new file +62 -0

62 lines not on this page · raw message

xen/arch/riscv/mm.c new file +315 -0

315 lines not on this page · raw message

xen/arch/riscv/riscv64/head.S +1 -0
index 8887f0cbd4..983757e498 100644
@@ -1,4 +1,5 @@
 #include <asm/asm.h>
+#include <asm/asm-offsets.h>
 #include <asm/riscv_encoding.h>
 
         .section .text.header, "ax", %progbits
xen/arch/riscv/setup.c +11 -0
index 3786f337e0..315804aa87 100644
@@ -2,6 +2,7 @@
 #include <xen/init.h>
 
 #include <asm/early_printk.h>
+#include <asm/mm.h>
 
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE]
@@ -26,3 +27,13 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     unreachable();
 }
+
+void __init noreturn cont_after_mmu_is_enabled(void)
+{
+    early_printk("All set up\n");
+
+    for ( ;; )
+        asm volatile ("wfi");
+
+    unreachable();
+}
xen/arch/riscv/xen.lds.S +4 -0
index 31e0d3576c..f9d89b69b9 100644
@@ -137,6 +137,7 @@ SECTIONS
     __init_end = .;
 
     .bss : {                     /* BSS */
+        . = ALIGN(POINTER_ALIGN);
         __bss_start = .;
         *(.bss.stack_aligned)
         . = ALIGN(PAGE_SIZE);
@@ -172,3 +173,6 @@ ASSERT(IS_ALIGNED(__bss_end,        POINTER_ALIGN), "__bss_end is misaligned")
 
 ASSERT(!SIZEOF(.got),      ".got non-empty")
 ASSERT(!SIZEOF(.got.plt),  ".got.plt non-empty")
+
+ASSERT(_end - _start <= MB(2), "Xen too large for early-boot assumptions")
+
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH v6 1/4] xen/riscv: add VM space layout

In reply to Oleksii Kurochko
Quoted 19 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +13 -0
@@ -4,6 +4,37 @@
 #include <xen/const.h>
 #include <xen/page-size.h>
 
+/*
+ * RISC-V64 Layout:
+ *
+ * From the riscv-privileged doc:
+ *   When mapping between narrower and wider addresses,
+ *   RISC-V zero-extends a narrower physical address to a wider size.
+ *   The mapping between 64-bit virtual addresses and the 39-bit usable
+ *   address space of Sv39 is not based on zero-extension but instead
+ *   follows an entrenched convention that allows an OS to use one or
+ *   a few of the most-significant bits of a full-size (64-bit) virtual
+ *   address to quickly distinguish user and supervisor address regions.
+ *
+ * It means that:

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

Quoted 10 lines
xen/arch/riscv/include/asm/config.h +10 -0
+ *   top VA bits are simply ignored for the purpose of translating to PA.
+ *
+ * ============================================================================
+ *    Start addr    |   End addr        |  Size  | Slot       |area description
+ * ============================================================================
+ * FFFFFFFFC0000000 |  FFFFFFFFC0200000 |  2 MB  | L2 511     | Xen
+ * FFFFFFFFC0200000 |  FFFFFFFFC0600000 |  4 MB  | L2 511     | FDT
+ * FFFFFFFFC0600000 |  FFFFFFFFC0800000 |  2 MB  | L2 511     | Fixmap
+ *                 ...                  |  1 GB  | L2 510     | Unused
+ * 0000003200000000 |  0000007f40000000 | 331 GB | L2 200-609 | Direct map

I guess the upper value is 509 here?

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

Quoted 2 lines
xen/arch/riscv/include/asm/config.h +2 -0
+ *                 ...                  |  1 GB  | L2 199     | Unused
+ * 0000003100000000 |  0000003140000000 |  3 GB  | L2 196-198 | Frametable

The two leftmost columns cover 1Gb only.

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

Quoted 5 lines
xen/arch/riscv/include/asm/config.h +5 -0
+ *                 ...                  |  1 GB  | L2 195     | Unused
+ * 0000003080000000 |  00000030c0000000 |  1 GB  | L2 194     | VMAP
+ *     .................. unused ..................
+ * ============================================================================
+ */

Two more remarks: This map is, aiui, Sv39-specific. The quotation from the doc doesn't really imply that, so I'd suggest to add something to make this explicit. This might be as simple as a suitable #ifdef around or inside the comment (even inside I think it'll be easily understood what it means; see e.g. the CONFIG_BIGMEM conditional in x86'es table).

The other oddity here is the sorting of entries: You sort downwards by L2 slot, but upwards within slot 511. Once suitably re-ordered it'll become apparent that there's another "unused" row missing (or perhaps even two).

Jan

#4 lore raw

Re: [PATCH v6 2/4] xen/riscv: introduce setup_initial_pages

In reply to Oleksii Kurochko
Quoted 11 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +4 -1
@@ -70,12 +70,23 @@
   name:
 #endif
 
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START 0xFFFFFFFFC0000000 /* (_AC(-1, UL) + 1 - GB(1)) */
+#else
+#error "RV32 isn't supported"

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

Quoted 10 lines
xen/arch/riscv/include/asm/config.h +5 -0
+#endif
 
 #define SMP_CACHE_BYTES (1 << 6)
 
 #define STACK_SIZE PAGE_SIZE
 
+#ifdef CONFIG_RISCV_64
+#define CONFIG_PAGING_LEVELS 3
+#define RV_STAGE1_MODE SATP_MODE_SV39
+#else

#define CONFIG_PAGING_LEVELS 2

(or else I would think ...

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

Quoted 1 line
xen/arch/riscv/include/asm/config.h +1 -0
+#define RV_STAGE1_MODE SATP_MODE_SV32

... this and hence the entire "#else" should also be omitted)

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

Quoted 11 lines
xen/arch/riscv/include/asm/current.h new file +8 -0
@@ -0,0 +1,10 @@
+#ifndef __ASM_CURRENT_H
+#define __ASM_CURRENT_H
+
+#define switch_stack_and_jump(stack, fn)    \
+    asm volatile (                          \
+            "mv sp, %0 \n"                  \
+            "j " #fn :: "r" (stack) :       \
+    )

Nit: Style. Our normal way of writing this would be

#define switch_stack_and_jump(stack, fn)    \
    asm volatile (                          \
            "mv sp, %0\n"                   \
            "j " #fn :: "r" (stack) )

i.e. unnecessary colon omitted, no trailin blank on any generated assembly line, and closing paren not placed on its own line (only closing figure braces would normally live on their own lines).

However, as to the 3rd colon: Can you really get away here without a memory clobber (i.e. the construct being a full compiler barrier)?

Further I think you want to make the use of "fn" visible to the compiler, by using an "X" constraint just like Arm does.

Finally I think you want to add unreachable(), like both Arm and x86 have it. Plus the "noreturn" on relevant functions.

Quoted 24 lines
xen/arch/riscv/include/asm/page.h new file +21 -0
@@ -0,0 +1,62 @@
+#ifndef _ASM_RISCV_PAGE_H
+#define _ASM_RISCV_PAGE_H
+
+#include <xen/const.h>
+#include <xen/types.h>
+
+#define VPN_MASK                    ((unsigned long)(PAGETABLE_ENTRIES - 1))
+
+#define XEN_PT_LEVEL_ORDER(lvl)     ((lvl) * PAGETABLE_ORDER)
+#define XEN_PT_LEVEL_SHIFT(lvl)     (XEN_PT_LEVEL_ORDER(lvl) + PAGE_SHIFT)
+#define XEN_PT_LEVEL_SIZE(lvl)      (_AT(paddr_t, 1) << XEN_PT_LEVEL_SHIFT(lvl))
+#define XEN_PT_LEVEL_MAP_MASK(lvl)  (~(XEN_PT_LEVEL_SIZE(lvl) - 1))
+#define XEN_PT_LEVEL_MASK(lvl)      (VPN_MASK << XEN_PT_LEVEL_SHIFT(lvl))
+
+#define PTE_VALID                   BIT(0, UL)
+#define PTE_READABLE                BIT(1, UL)
+#define PTE_WRITABLE                BIT(2, UL)
+#define PTE_EXECUTABLE              BIT(3, UL)
+#define PTE_USER                    BIT(4, UL)
+#define PTE_GLOBAL                  BIT(5, UL)
+#define PTE_ACCESSED                BIT(6, UL)

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

Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10 -0
+#define PTE_DIRTY                   BIT(7, UL)
+#define PTE_RSW                     (BIT(8, UL) | BIT(9, UL))
+
+#define PTE_LEAF_DEFAULT            (PTE_VALID | PTE_READABLE | PTE_WRITABLE)
+#define PTE_TABLE                   (PTE_VALID)
+
+/* Calculate the offsets into the pagetables for a given VA */
+#define pt_linear_offset(lvl, va)   ((va) >> XEN_PT_LEVEL_SHIFT(lvl))
+
+#define pt_index(lvl, va) pt_linear_offset(lvl, (va) & XEN_PT_LEVEL_MASK(lvl))

Maybe better

#define pt_index(lvl, va) (pt_linear_offset(lvl, va) & VPN_MASK)

as the involved constant will be easier to use for the compiler?

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

Quoted 12 lines
xen/arch/riscv/include/asm/page.h new file +12 -0
+/* Page Table entry */
+typedef struct {
+#ifdef CONFIG_RISCV_64
+    uint64_t pte;
+#else
+    uint32_t pte;
+#endif
+} pte_t;
+
+#define pte_to_addr(x) (((paddr_t)(x) >> PTE_PPN_SHIFT) << PAGE_SHIFT)
+
+#define addr_to_pte(x) (((x) >> PAGE_SHIFT) << PTE_PPN_SHIFT)

Are these two macros useful on their own? I ask because I still consider them somewhat misnamed, as they don't produce / consume a PTE (but the raw value). Yet generally you want to avoid any code operating on raw values, using pte_t instead. IOW I would hope to be able to convince you to ...

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

Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10 -0
+static inline pte_t paddr_to_pte(paddr_t paddr,
+                                 unsigned int permissions)
+{
+    return (pte_t) { .pte = addr_to_pte(paddr) | permissions };
+}
+
+static inline paddr_t pte_to_paddr(pte_t pte)
+{
+    return pte_to_addr(pte.pte);
+}

... expand them in the two inline functions and then drop the macros.

xen/arch/riscv/mm.c line 7

Quoted 10 lines
xen/arch/riscv/mm.c new file +7 -0
@@ -0,0 +1,315 @@
+#include <xen/compiler.h>
+#include <xen/init.h>
+#include <xen/kernel.h>
+#include <xen/pfn.h>
+
+#include <asm/early_printk.h>
+#include <asm/config.h>

No inclusion of asm/config.h (or xen/config.h) in any new code please. For quite some time xen/config.h has been forcefully included everywhere by the build system.

xen/arch/riscv/mm.c

Quoted 5 lines
xen/arch/riscv/mm.c new file +5 -0
+/*
+ * It is expected that Xen won't be more then 2 MB.
+ * The check in xen.lds.S guarantees that.
+ * At least 3 page (in case of Sv39 )
+ * tables are needed to cover 2 MB.

I guess this reads a little better as "At least 3 page tables (in case of Sv39) ..." or "At least 3 (in case of Sv39) page tables ..." Also could I talk you into using the full 80 columns instead of wrapping early in the middle of a sentence?

Quoted 8 lines

One for each page level
+ * table with PAGE_SIZE = 4 Kb
+ *
+ * One L0 page table can cover 2 MB
+ * (512 entries of one page table * PAGE_SIZE).
+ *
+ */
+#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) + 1)

Hmm, did you lose the part of the comment explaining the "+ 1"? Or did the need for that actually go away (and you should drop it here)?

Quoted 1 line

+#define PGTBL_ENTRY_AMOUNT (PAGE_SIZE / sizeof(pte_t))

Isn't this PAGETABLE_ENTRIES (and the constant hence unnecessary)?

xen/arch/riscv/mm.c line 76

Quoted 5 lines
xen/arch/riscv/mm.c new file +5 -0
+static void __init setup_initial_mapping(struct mmu_desc *mmu_desc,
+                                         unsigned long map_start,
+                                         unsigned long map_end,
+                                         unsigned long pa_start,
+                                         unsigned int permissions)

What use is the last parameter, when the sole caller passes PTE_LEAF_DEFAULT (i.e. a build-time constant)? Then ...

xen/arch/riscv/mm.c line 83

Quoted 7 lines
xen/arch/riscv/mm.c new file +7 -0
+{
+    unsigned int index;
+    pte_t *pgtbl;
+    unsigned long page_addr;
+    pte_t pte_to_be_written;
+    unsigned long paddr;
+    unsigned int tmp_permissions;

... this variable (which would better be of more narrow scope anyway, like perhaps several more of the above) could also have its tmp_ prefix dropped afaict.

Quoted 5 lines
xen/arch/riscv/mm.c new file +5 -0
+    if ( (unsigned long)_start % XEN_PT_LEVEL_SIZE(0) )
+    {
+        early_printk("(XEN) Xen should be loaded at 4k boundary\n");
+        die();
+    }

xen/arch/riscv/mm.c line 99

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+
+    if ( map_start & ~XEN_PT_LEVEL_MAP_MASK(0) ||
+         pa_start & ~XEN_PT_LEVEL_MAP_MASK(0) )
+    {
+        early_printk("(XEN) map and pa start addresses should be aligned\n");
+        /* panic(), BUG() or ASSERT() aren't ready now. */
+        die();
+    }
+
+    for ( page_addr = map_start; page_addr < map_end; page_addr += XEN_PT_LEVEL_SIZE(0) )

Nit: Style (line looks to be too long now).

    for ( page_addr = map_start; page_addr < map_end;
          page_addr += XEN_PT_LEVEL_SIZE(0) )

is the way we would typically wrap it, but

    for ( page_addr = map_start;
          page_addr < map_end;
          page_addr += XEN_PT_LEVEL_SIZE(0) )

would of course also be okay if you preferred that.

Quoted 9 lines
xen/arch/riscv/mm.c new file +9 -0
+    {
+        pgtbl = mmu_desc->pgtbl_base;
+
+        switch ( mmu_desc->num_levels )
+        {
+        case 4: /* Level 3 */
+            HANDLE_PGTBL(3);
+        case 3: /* Level 2 */
+            HANDLE_PGTBL(2);

xen/arch/riscv/mm.c line 118

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+        case 2: /* Level 1 */
+            HANDLE_PGTBL(1);
+        case 1: /* Level 0 */
+            index = pt_index(0, page_addr);
+            paddr = (page_addr - map_start) + pa_start;
+
+            tmp_permissions = permissions;
+
+            if ( is_kernel_text(LINK_TO_LOAD(page_addr)) ||
+                    is_kernel_inittext(LINK_TO_LOAD(page_addr)) )

Nit: Style (and I'm pretty sure I did comment on this kind of too deep indentation already).

Quoted 6 lines
xen/arch/riscv/mm.c new file +6 -0
+                tmp_permissions =
+                    PTE_EXECUTABLE | PTE_READABLE | PTE_VALID;
+
+            if ( is_kernel_rodata(LINK_TO_LOAD(page_addr)) )
+                tmp_permissions = PTE_READABLE | PTE_VALID;
+

xen/arch/riscv/mm.c line 134

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+            pte_to_be_written = paddr_to_pte(paddr, tmp_permissions);
+
+            if ( !pte_is_valid(pgtbl[index]) )
+                pgtbl[index] = pte_to_be_written;
+            else
+            {
+                /*
+                * get an adresses of current pte and that one to
+                * be written
+                */

Nit: Style (one missing blank each in the last three lines, and comment text wants to start with a capital letter).

xen/arch/riscv/mm.c line 136

Quoted 2 lines
xen/arch/riscv/mm.c new file +2 -0
+                unsigned long curr_pte =
+                    pgtbl[index].pte & ~(PTE_DIRTY | PTE_ACCESSED);

While technically "unsigned long" is okay to use here (afaict), I'd still recommend ...

xen/arch/riscv/mm.c line 140

Quoted 3 lines
xen/arch/riscv/mm.c new file +3 -0
+                pte_to_be_written.pte &= ~(PTE_DIRTY | PTE_ACCESSED);
+
+                if ( curr_pte != pte_to_be_written.pte )

... doing everything in terms of pte_t:

                pte_t curr_pte = pgtbl[index];

                curr_pte.pte &= ~(PTE_DIRTY | PTE_ACCESSED);
                pte_to_be_written.pte &= ~(PTE_DIRTY | PTE_ACCESSED);

                if ( curr_pte.pte != pte_to_be_written.pte )

or perhaps even simply

if ( (pgtbl[index].pte ^ pte_to_be_written.pte) &
~(PTE_DIRTY | PTE_ACCESSED) )

xen/arch/riscv/mm.c line 149

Quoted 9 lines
xen/arch/riscv/mm.c new file +9 -0
+                {
+                    early_printk("PTE overridden has occurred\n");
+                    /* panic(), <asm/bug.h> aren't ready now. */
+                    die();
+                }
+            }
+        }
+    }
+    #undef HANDLE_PGTBL

Nit: Such an #undef, even if technically okay either way, would imo better live in the same scope (and have the same indentation) as the corresponding #define. Since your #define is ahead of the function, it would ...

Quoted 1 line

+}

... then live here.

Quoted 1 line

+static void __init calc_pgtbl_lvls_num(struct mmu_desc *mmu_desc)

Nit: Double blank after "struct".

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+{
+    unsigned long satp_mode = RV_STAGE1_MODE;
+
+    /* Number of page table levels */
+    switch (satp_mode)
+    {
+    case SATP_MODE_SV32:
+        mmu_desc->num_levels = 2;
+        break;
+    case SATP_MODE_SV39:

xen/arch/riscv/mm.c line 172

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+        mmu_desc->num_levels = 3;
+        break;
+    case SATP_MODE_SV48:
+        mmu_desc->num_levels = 4;
+        break;
+    default:
+        early_printk("(XEN) Unsupported SATP_MODE\n");
+        die();
+    }
+}

Do you really need this function anymore? Isn't it now simply

mmu_desc.num_levels = CONFIG_PAGING_LEVELS?

in the caller? WHich could then even be the variable's initializer there?

Quoted 23 lines
xen/arch/riscv/mm.c new file +23 -0
+static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc,
+                                            unsigned long load_start,
+                                            unsigned long satp_mode)
+{
+    bool is_mode_supported = false;
+    unsigned int index;
+    unsigned int page_table_level = (mmu_desc->num_levels - 1);
+    unsigned level_map_mask = XEN_PT_LEVEL_MAP_MASK(page_table_level);
+
+    unsigned long aligned_load_start = load_start & level_map_mask;
+    unsigned long aligned_page_size = XEN_PT_LEVEL_SIZE(page_table_level);
+    unsigned long xen_size = (unsigned long)(_end - start);
+
+    if ( (load_start + xen_size) > (aligned_load_start + aligned_page_size) )
+    {
+        early_printk("please place Xen to be in range of PAGE_SIZE "
+                     "where PAGE_SIZE is XEN_PT_LEVEL_SIZE( {L3 | L2 | L1} ) "
+                     "depending on expected SATP_MODE \n"
+                     "XEN_PT_LEVEL_SIZE is defined in <asm/page.h>\n");
+        die();
+    }
+
+    index = pt_index(page_table_level, aligned_load_start);

xen/arch/riscv/mm.c line 206

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+    stage1_pgtbl_root[index] = paddr_to_pte(aligned_load_start,
+                                            PTE_LEAF_DEFAULT | PTE_EXECUTABLE);
+
+    asm volatile ( "sfence.vma" );
+    csr_write(CSR_SATP,
+              PFN_DOWN((unsigned long)stage1_pgtbl_root) |
+              satp_mode << SATP_MODE_SHIFT);
+
+    if ( (csr_read(CSR_SATP) >> SATP_MODE_SHIFT) == satp_mode )
+        is_mode_supported = true;

Just as a remark: While plain shift is kind of okay here, because the field is the top-most one in the register, generally you will want to get used to MASK_EXTR() (and MASK_INSR()) in cases like this one.

xen/arch/riscv/mm.c line 213

Quoted 6 lines
xen/arch/riscv/mm.c new file +6 -0
+    csr_write(CSR_SATP, 0);
+
+    /* Clean MMU root page table */
+    stage1_pgtbl_root[index] = paddr_to_pte(0x0, 0x0);
+
+    asm volatile ( "sfence.vma" );

Doesn't this want to move between the SATP write and the clearing of the root table slot? Also here and elsewhere - don't these asm()s need memory clobbers? And anyway - could I talk you into introducing an inline wrapper (e.g. named sfence_vma()) so all uses end up consistent?

xen/arch/riscv/mm.c line 237

Quoted 6 lines
xen/arch/riscv/mm.c new file +6 -0
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, NULL };
+
+    /*
+     * Access to _stard, _end is always PC-relative

Nit: Typo-ed symbol name. Also ...

xen/arch/riscv/mm.c line 242

Quoted 5 lines
xen/arch/riscv/mm.c new file +5 -0
+     * thereby when access them we will get load adresses
+     * of start and end of Xen
+     * To get linker addresses LOAD_TO_LINK() is required
+     * to use
+     */

see the earlier line wrapping remark again. Finally in multi-sentence comments full stops are required.

Quoted 5 lines
xen/arch/riscv/mm.c new file +5 -0
+    unsigned long load_start    = (unsigned long)_start;
+    unsigned long load_end      = (unsigned long)_end;
+    unsigned long linker_start  = LOAD_TO_LINK(load_start);
+    unsigned long linker_end    = LOAD_TO_LINK(load_end);
+

xen/arch/riscv/mm.c line 257

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+    if ( (linker_start != load_start) &&
+         (linker_start <= load_end) && (load_start <= linker_end) )
+    {
+        early_printk("(XEN) linker and load address ranges overlap\n");
+        die();
+    }
+
+    calc_pgtbl_lvls_num(&mmu_desc);
+
+    if ( !check_pgtbl_mode_support(&mmu_desc, load_start, RV_STAGE1_MODE) )

It is again questionable here whether passing a constant to a function at its sole call site is really useful.

Quoted 13 lines
xen/arch/riscv/mm.c new file +13 -0
+void __init noinline enable_mmu()
+{
+    /*
+     * Calculate a linker time address of the mmu_is_enabled
+     * label and update CSR_STVEC with it.
+     * MMU is configured in a way where linker addresses are mapped
+     * on load addresses so in a case when linker addresses are not equal
+     * to load addresses, after MMU is enabled, it will cause
+     * an exception and jump to linker time addresses.
+     * Otherwise if load addresses are equal to linker addresses the code
+     * after mmu_is_enabled label will be executed without exception.
+     */
+    csr_write(CSR_STVEC, LOAD_TO_LINK((unsigned long)&&mmu_is_enabled));

xen/arch/riscv/mm.c line 296

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+
+    /* Ensure page table writes precede loading the SATP */
+    asm volatile ( "sfence.vma" );
+
+    /* Enable the MMU and load the new pagetable for Xen */
+    csr_write(CSR_SATP,
+              PFN_DOWN((unsigned long)stage1_pgtbl_root) |
+              RV_STAGE1_MODE << SATP_MODE_SHIFT);
+
+    asm volatile ( ".align 2" );

May I suggest to avoid .align, and to prefer .balign and .p2align instead? This helps people coming from different architectures, as which of the two .align resolves to is arch-dependent.

xen/arch/riscv/riscv64/head.S line 5

Quoted 8 lines
xen/arch/riscv/riscv64/head.S +1 -0
@@ -1,4 +1,5 @@
 #include <asm/asm.h>
+#include <asm/asm-offsets.h>
 #include <asm/riscv_encoding.h>
 
         .section .text.header, "ax", %progbits

How does a need for this arise all of the sudden, without other changes to this file? Is this maybe a leftover which wants dropping?

xen/arch/riscv/xen.lds.S SECTIONS · line 143

Quoted 10 lines
xen/arch/riscv/xen.lds.S +1 -0
@@ -137,6 +137,7 @@ SECTIONS
     __init_end = .;
 
     .bss : {                     /* BSS */
+        . = ALIGN(POINTER_ALIGN);
         __bss_start = .;
         *(.bss.stack_aligned)
         . = ALIGN(PAGE_SIZE);

Doesn't this want to be a separate change?

Jan

author
#5 lore raw

Re: [PATCH v6 2/4] xen/riscv: introduce setup_initial_pages

Quoted 26 lines · Jan Beulich
Quoted 18 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +0 -0
@@ -70,12 +70,23 @@

   name:
 #endif

xen/arch/riscv/include/asm/config.h +2 -1
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START 0xFFFFFFFFC0000000 /* (_AC(-1, UL) + 1 -

GB(1)) */

xen/arch/riscv/include/asm/config.h +3 -0
+#else
+#error "RV32 isn't supported"
+#endif


 #define SMP_CACHE_BYTES (1 << 6)

 #define STACK_SIZE PAGE_SIZE

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

Quoted 4 lines
xen/arch/riscv/include/asm/config.h +4 -0
+#ifdef CONFIG_RISCV_64
+#define CONFIG_PAGING_LEVELS 3
+#define RV_STAGE1_MODE SATP_MODE_SV39
+#else

#define CONFIG_PAGING_LEVELS 2

Quoted 5 lines

(or else I would think ...

Quoted 1 line

+#define RV_STAGE1_MODE SATP_MODE_SV32

... this and hence the entire "#else" should also be omitted)

Agree. it will be better to set CONFIG_PAGING_LEVELS for RV32 too.

Quoted 15 lines

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

Quoted 12 lines
xen/arch/riscv/include/asm/current.h new file +8 -0
@@ -0,0 +1,10 @@
+#ifndef __ASM_CURRENT_H
+#define __ASM_CURRENT_H
+
+#define switch_stack_and_jump(stack, fn)    \
+    asm volatile (                          \
+            "mv sp, %0 \n"                  \
+            "j " #fn :: "r" (stack) :       \
+    )

Nit: Style. Our normal way of writing this would be

Quoted 8 lines

#define switch_stack_and_jump(stack, fn)    \
    asm volatile (                          \
            "mv sp, %0\n"                   \
            "j " #fn :: "r" (stack) )

i.e. unnecessary colon omitted, no trailin blank on any generated assembly line, and closing paren not placed on its own line (only closing figure braces would normally live on their own lines).

Thanks for clarification

Quoted 3 lines

However, as to the 3rd colon: Can you really get away here without a memory clobber (i.e. the construct being a full compiler barrier)?

I am not 100% sure so to be safe I would add memory clobber. Thanks.

Quoted 6 lines

Further I think you want to make the use of "fn" visible to the compiler, by using an "X" constraint just like Arm does.

Finally I think you want to add unreachable(), like both Arm and x86 have it. Plus the "noreturn" on relevant functions.

It makes sense. I'll take into account.

Quoted 46 lines
Quoted 42 lines
xen/arch/riscv/include/asm/page.h new file +5 -0
@@ -0,0 +1,62 @@
+#ifndef _ASM_RISCV_PAGE_H
+#define _ASM_RISCV_PAGE_H
+
+#include <xen/const.h>
+#include <xen/types.h>

=)+
+#define VPN_MASK                    ((unsigned
long)(PAGETABLE_ENTRIES - 1))

xen/arch/riscv/include/asm/page.h new file +3 -0
+
+#define XEN_PT_LEVEL_ORDER(lvl)     ((lvl) * PAGETABLE_ORDER)
+#define XEN_PT_LEVEL_SHIFT(lvl)     (XEN_PT_LEVEL_ORDER(lvl) +

PAGE_SHIFT)
+#define XEN_PT_LEVEL_SIZE(lvl)      (_AT(paddr_t, 1) << XEN_PT_LEVEL_SHIFT(lvl))
+#define XEN_PT_LEVEL_MAP_MASK(lvl)  (~(XEN_PT_LEVEL_SIZE(lvl) - 1))
+#define XEN_PT_LEVEL_MASK(lvl)      (VPN_MASK << XEN_PT_LEVEL_SHIFT(lvl))

xen/arch/riscv/include/asm/page.h new file +12 -0
+
+#define PTE_VALID                   BIT(0, UL)
+#define PTE_READABLE                BIT(1, UL)
+#define PTE_WRITABLE                BIT(2, UL)
+#define PTE_EXECUTABLE              BIT(3, UL)
+#define PTE_USER                    BIT(4, UL)
+#define PTE_GLOBAL                  BIT(5, UL)
+#define PTE_ACCESSED                BIT(6, UL)
+#define PTE_DIRTY                   BIT(7, UL)
+#define PTE_RSW                     (BIT(8, UL) | BIT(9, UL))
+
+#define PTE_LEAF_DEFAULT            (PTE_VALID | PTE_READABLE |

PTE_WRITABLE)

xen/arch/riscv/include/asm/page.h new file +4 -0
+#define PTE_TABLE                   (PTE_VALID)
+
+/* Calculate the offsets into the pagetables for a given VA */
+#define pt_linear_offset(lvl, va)   ((va) >>

XEN_PT_LEVEL_SHIFT(lvl))

xen/arch/riscv/include/asm/page.h new file +2 -0
+
+#define pt_index(lvl, va) pt_linear_offset(lvl, (va) &
Quoted 1 line

XEN_PT_LEVEL_MASK(lvl))

Maybe better

Quoted 3 lines

#define pt_index(lvl, va) (pt_linear_offset(lvl, va) & VPN_MASK)

as the involved constant will be easier to use for the compiler?

But VPN_MASK should be shifted by level shift value.

Or did you mean that it will be better to pre-calculate MASK for each level instead of define MASK as (VPN_MASK << XEN_PT_LEVEL_SHIFT(lvl)).

Probably I have to re-check that but taking into account that all values are defined during compile time so they will be pre-calculated so it shouldn't be big difference for the compiler.

Quoted 23 lines
Quoted 12 lines
xen/arch/riscv/include/asm/page.h new file +10 -0
+/* Page Table entry */
+typedef struct {
+#ifdef CONFIG_RISCV_64
+    uint64_t pte;
+#else
+    uint32_t pte;
+#endif
+} pte_t;
+
+#define pte_to_addr(x) (((paddr_t)(x) >> PTE_PPN_SHIFT) <<

PAGE_SHIFT)

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

Quoted 2 lines
xen/arch/riscv/include/asm/page.h new file +2 -0
+
+#define addr_to_pte(x) (((x) >> PAGE_SHIFT) << PTE_PPN_SHIFT)

Are these two macros useful on their own? I ask because I still consider
them somewhat misnamed, as they don't produce / consume a PTE (but the
raw value). Yet generally you want to avoid any code operating on raw values, using pte_t instead. IOW I would hope to be able to convince you to ...

Quoted 12 lines

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

Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10 -0
+static inline pte_t paddr_to_pte(paddr_t paddr,
+                                 unsigned int permissions)
+{
+    return (pte_t) { .pte = addr_to_pte(paddr) | permissions };
+}
+
+static inline paddr_t pte_to_paddr(pte_t pte)
+{
+    return pte_to_addr(pte.pte);
+}

... expand them in the two inline functions and then drop the macros.

I thought about drop the macros as they are used now only in the mentioned above functions. ( initially the macros were introduced for another code but that code was re-written and the macros aren't needed anymore).
So yes, lets remove them.

Quoted 17 lines

xen/arch/riscv/mm.c line 7

Quoted 11 lines
xen/arch/riscv/mm.c new file +7 -0
@@ -0,0 +1,315 @@
+#include <xen/compiler.h>
+#include <xen/init.h>
+#include <xen/kernel.h>
+#include <xen/pfn.h>
+
+#include <asm/early_printk.h>
+#include <asm/config.h>

No inclusion of asm/config.h (or xen/config.h) in any new code please.
For quite some time xen/config.h has been forcefully included everywhere
by the build system.

Sure. I'll remove the inclusion than.

Quoted 7 lines

xen/arch/riscv/mm.c

Quoted 6 lines
xen/arch/riscv/mm.c new file +5 -0
+/*
+ * It is expected that Xen won't be more then 2 MB.
+ * The check in xen.lds.S guarantees that.
+ * At least 3 page (in case of Sv39 )
+ * tables are needed to cover 2 MB.
Quoted 7 lines

I guess this reads a little better as "At least 3 page tables (in case of
Sv39) ..." or "At least 3 (in case of Sv39) page tables ..." Also could I
talk you into using the full 80 columns instead of wrapping early in the
middle of a sentence?

Sure, I'll use full 80 columns.

Quoted 10 lines
Quoted 9 lines

One for each page level
+ * table with PAGE_SIZE = 4 Kb
+ *
+ * One L0 page table can cover 2 MB
+ * (512 entries of one page table * PAGE_SIZE).
+ *
+ */
+#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) + 1)

Quoted 3 lines

Hmm, did you lose the part of the comment explaining the "+ 1"? Or did
the need for that actually go away (and you should drop it here)?

I lost it so it should be backed. Thanks.

Quoted 4 lines
Quoted 2 lines

+#define PGTBL_ENTRY_AMOUNT  (PAGE_SIZE / sizeof(pte_t))

Isn't this PAGETABLE_ENTRIES (and the constant hence unnecessary)?

It is. I'll re-use PAGETABLE_ENTRIES

Quoted 10 lines
Quoted 7 lines

+static void __init setup_initial_mapping(struct mmu_desc *mmu_desc,
+                                         unsigned long map_start, +                                         unsigned long map_end, +                                         unsigned long pa_start, +                                         unsigned int permissions)

What use is the last parameter, when the sole caller passes PTE_LEAF_DEFAULT (i.e. a build-time constant)? Then ...

It's not used anymore. and taking into account that I boot Dom0 successfully and used for setup_initial_mapping() always PTE_LEAF_DEFAULT so i'll drop it.

Quoted 39 lines
Quoted 8 lines

+{
+    unsigned int index;
+    pte_t *pgtbl;
+    unsigned long page_addr;
+    pte_t pte_to_be_written;
+    unsigned long paddr;
+    unsigned int tmp_permissions;

... this variable (which would better be of more narrow scope anyway, like
perhaps several more of the above) could also have its tmp_ prefix dropped
afaict.

Quoted 10 lines

+    if ( (unsigned long)_start % XEN_PT_LEVEL_SIZE(0) ) +    {
+        early_printk("(XEN) Xen should be loaded at 4k boundary\n");
+        die();
+    }
+
+    if ( map_start & ~XEN_PT_LEVEL_MAP_MASK(0) || +         pa_start & ~XEN_PT_LEVEL_MAP_MASK(0) ) +    {

Quoted 8 lines

+        early_printk("(XEN) map and pa start addresses should be aligned\n");
+        /* panic(), BUG() or ASSERT() aren't ready now. */ +        die();
+    }
+
+    for ( page_addr = map_start; page_addr < map_end; page_addr += XEN_PT_LEVEL_SIZE(0) )

Nit: Style (line looks to be too long now).

    for ( page_addr = map_start; page_addr < map_end; page_addr += XEN_PT_LEVEL_SIZE(0) )

Quoted 7 lines

is the way we would typically wrap it, but

    for ( page_addr = map_start;
          page_addr < map_end;
          page_addr += XEN_PT_LEVEL_SIZE(0) )

would of course also be okay if you preferred that.

Last one option looks better to me. Thanks.

Quoted 16 lines
Quoted 16 lines

+    {
+        pgtbl = mmu_desc->pgtbl_base;

xen/arch/riscv/mm.c new file +13 -0
+
+        switch ( mmu_desc->num_levels )
+        {
+        case 4: /* Level 3 */
+            HANDLE_PGTBL(3);
+        case 3: /* Level 2 */
+            HANDLE_PGTBL(2);
+        case 2: /* Level 1 */
+            HANDLE_PGTBL(1);
+        case 1: /* Level 0 */
+            index = pt_index(0, page_addr);
+            paddr = (page_addr - map_start) + pa_start;
+
Quoted 8 lines
Quoted 4 lines

+            tmp_permissions = permissions;
+
+            if ( is_kernel_text(LINK_TO_LOAD(page_addr)) || +                    is_kernel_inittext(LINK_TO_LOAD(page_addr)) )

Nit: Style (and I'm pretty sure I did comment on this kind of too deep
indentation already).

Sorry. I'll double check that and fix.

Quoted 14 lines
Quoted 14 lines

+                tmp_permissions =
+                    PTE_EXECUTABLE | PTE_READABLE | PTE_VALID;

xen/arch/riscv/mm.c new file +5 -0
+
+            if ( is_kernel_rodata(LINK_TO_LOAD(page_addr)) )
+                tmp_permissions = PTE_READABLE | PTE_VALID;
+
+            pte_to_be_written = paddr_to_pte(paddr,

tmp_permissions);
+
+            if ( !pte_is_valid(pgtbl[index]) ) +                pgtbl[index] = pte_to_be_written; +            else
+            {

Quoted 8 lines
Quoted 4 lines

+                /*
+                * get an adresses of current pte and that one to +                * be written
+                */

Nit: Style (one missing blank each in the last three lines, and comment
text wants to start with a capital letter).

Thanks.

Quoted 16 lines
Quoted 4 lines

+                unsigned long curr_pte =
+                    pgtbl[index].pte & ~(PTE_DIRTY | PTE_ACCESSED);

While technically "unsigned long" is okay to use here (afaict), I'd still
recommend ...

Quoted 4 lines

+                pte_to_be_written.pte &= ~(PTE_DIRTY | PTE_ACCESSED);
+
+                if ( curr_pte != pte_to_be_written.pte )

... doing everything in terms of pte_t:

Quoted 8 lines

                pte_t curr_pte = pgtbl[index];

                curr_pte.pte &= ~(PTE_DIRTY | PTE_ACCESSED); pte_to_be_written.pte &= ~(PTE_DIRTY | PTE_ACCESSED);

                if ( curr_pte.pte != pte_to_be_written.pte )

or perhaps even simply

It makes sense for me to use pte_t so will switch to it.

Quoted 20 lines

                if ( (pgtbl[index].pte ^ pte_to_be_written.pte) & ~(PTE_DIRTY | PTE_ACCESSED) )

Quoted 9 lines

+                {
+                    early_printk("PTE overridden has occurred\n"); +                    /* panic(), <asm/bug.h> aren't ready now. */ +                    die();
+                }
+            }
+        }
+    }
+    #undef HANDLE_PGTBL

Nit: Such an #undef, even if technically okay either way, would imo better live in the same scope (and have the same indentation) as the corresponding #define. Since your #define is ahead of the function, it
would ...

Quoted 3 lines
Quoted 1 line

+}

... then live here.

Thanks. I'll take into account.

Quoted 4 lines
Quoted 2 lines

+static void __init calc_pgtbl_lvls_num(struct  mmu_desc *mmu_desc)

Nit: Double blank after "struct".

will remove it. thanks.

Quoted 24 lines
Quoted 11 lines
xen/arch/riscv/mm.c new file +10 -0
+{
+    unsigned long satp_mode = RV_STAGE1_MODE;
+
+    /* Number of page table levels */
+    switch (satp_mode)
+    {
+    case SATP_MODE_SV32:
+        mmu_desc->num_levels = 2;
+        break;
+    case SATP_MODE_SV39:

xen/arch/riscv/mm.c line 172

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+        mmu_desc->num_levels = 3;
+        break;
+    case SATP_MODE_SV48:
+        mmu_desc->num_levels = 4;
+        break;
+    default:
+        early_printk("(XEN) Unsupported SATP_MODE\n");
+        die();
+    }
+}

Do you really need this function anymore? Isn't it now simply

Quoted 4 lines

    mmu_desc.num_levels = CONFIG_PAGING_LEVELS?

in the caller? WHich could then even be the variable's initializer there?

You are right after introduction of CONFIG_PAGING_LEVELS for RISC-V we can remove the code you mentioned.

Quoted 43 lines
Quoted 43 lines

+static bool __init check_pgtbl_mode_support(struct mmu_desc *mmu_desc,
+                                            unsigned long load_start,
+                                            unsigned long satp_mode)
+{
+    bool is_mode_supported = false;
+    unsigned int index;
+    unsigned int page_table_level = (mmu_desc->num_levels - 1); +    unsigned level_map_mask = XEN_PT_LEVEL_MAP_MASK(page_table_level);
+
+    unsigned long aligned_load_start = load_start & level_map_mask;
+    unsigned long aligned_page_size = XEN_PT_LEVEL_SIZE(page_table_level);
+    unsigned long xen_size = (unsigned long)(_end - start); +
+    if ( (load_start + xen_size) > (aligned_load_start + aligned_page_size) )
+    {
+        early_printk("please place Xen to be in range of PAGE_SIZE "
+                     "where PAGE_SIZE is XEN_PT_LEVEL_SIZE( {L3 | L2 | L1} ) "
+                     "depending on expected SATP_MODE \n" +                     "XEN_PT_LEVEL_SIZE is defined in <asm/page.h>\n");
+        die();
+    }
+
+    index = pt_index(page_table_level, aligned_load_start); +    stage1_pgtbl_root[index] = paddr_to_pte(aligned_load_start, +                                            PTE_LEAF_DEFAULT | PTE_EXECUTABLE);

xen/arch/riscv/mm.c new file +6 -0
+
+    asm volatile ( "sfence.vma" );
+    csr_write(CSR_SATP,
+              PFN_DOWN((unsigned long)stage1_pgtbl_root) |
+              satp_mode << SATP_MODE_SHIFT);
+
Quoted 8 lines
Quoted 2 lines

+    if ( (csr_read(CSR_SATP) >> SATP_MODE_SHIFT) == satp_mode ) +        is_mode_supported = true;

Just as a remark: While plain shift is kind of okay here, because the field
is the top-most one in the register, generally you will want to get used to
MASK_EXTR() (and MASK_INSR()) in cases like this one.

Thanks. I'll look at them.

Quoted 15 lines

xen/arch/riscv/mm.c line 213

Quoted 7 lines

+    csr_write(CSR_SATP, 0);

xen/arch/riscv/mm.c new file +5 -0
+
+    /* Clean MMU root page table */
+    stage1_pgtbl_root[index] = paddr_to_pte(0x0, 0x0);
+
+    asm volatile ( "sfence.vma" );

Doesn't this want to move between the SATP write and the clearing of the
root table slot? Also here and elsewhere - don't these asm()s need memory
clobbers? And anyway - could I talk you into introducing an inline wrapper
(e.g. named sfence_vma()) so all uses end up consistent?

I think clearing of root page table should be done before "sfence.vma" because we have to first clear slot of MMU's root page table and then make updating root page table visible for all. ( by usage of sfence instruction )

We can add memory clobber to be sure but it looks like it is a duplication of "sfence" instruction.

I agree that it would be nice to introduce a wrapper.

Quoted 10 lines

xen/arch/riscv/mm.c line 237

Quoted 7 lines
xen/arch/riscv/mm.c new file +6 -0
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, NULL };
+
+    /*
+     * Access to _stard, _end is always PC-relative

Nit: Typo-ed symbol name. Also ...

Quoted 8 lines
Quoted 5 lines

+     * thereby when access them we will get load adresses +     * of start and end of Xen
+     * To get linker addresses LOAD_TO_LINK() is required +     * to use
+     */

see the earlier line wrapping remark again. Finally in multi-sentence comments full stops are required.

Full stops mean '.' at the end of sentences?

Quoted 15 lines
Quoted 15 lines

+    unsigned long load_start    = (unsigned long)_start; +    unsigned long load_end      = (unsigned long)_end; +    unsigned long linker_start  = LOAD_TO_LINK(load_start); +    unsigned long linker_end    = LOAD_TO_LINK(load_end); +
+    if ( (linker_start != load_start) &&
+         (linker_start <= load_end) && (load_start <= linker_end) )
+    {
+        early_printk("(XEN) linker and load address ranges overlap\n");
+        die();
+    }
+

Quoted 8 lines
Quoted 4 lines

+    calc_pgtbl_lvls_num(&mmu_desc);
+
+    if ( !check_pgtbl_mode_support(&mmu_desc, load_start, RV_STAGE1_MODE) )

It is again questionable here whether passing a constant to a function
at its sole call site is really useful.

It looks like there is no too much sense to pass a constant directly to a function. So I'll update that.

Quoted 27 lines
Quoted 27 lines
xen/arch/riscv/mm.c new file +6 -0
+void __init noinline enable_mmu()
+{
+    /*
+     * Calculate a linker time address of the mmu_is_enabled
+     * label and update CSR_STVEC with it.
+     * MMU is configured in a way where linker addresses are

mapped
+     * on load addresses so in a case when linker addresses are not equal
+     * to load addresses, after MMU is enabled, it will cause +     * an exception and jump to linker time addresses. +     * Otherwise if load addresses are equal to linker addresses the code
+     * after mmu_is_enabled label will be executed without exception.
+     */
+    csr_write(CSR_STVEC, LOAD_TO_LINK((unsigned long)&&mmu_is_enabled));

xen/arch/riscv/mm.c new file +8 -0
+
+    /* Ensure page table writes precede loading the SATP */
+    asm volatile ( "sfence.vma" );
+
+    /* Enable the MMU and load the new pagetable for Xen */
+    csr_write(CSR_SATP,
+              PFN_DOWN((unsigned long)stage1_pgtbl_root) |
+              RV_STAGE1_MODE << SATP_MODE_SHIFT);
Quoted 8 lines
Quoted 2 lines

+
+    asm volatile ( ".align 2" );

May I suggest to avoid .align, and to prefer .balign and .p2align instead?
This helps people coming from different architectures, as which of the two
.align resolves to is arch-dependent.

Sure. Thanks. I thought that .p2align mostly used for assembly code. But yeah I'll re-use it instead of ' asm volatile ( ".align 2" );'.

Quoted 13 lines
Quoted 9 lines
xen/arch/riscv/riscv64/head.S +1 -0
@@ -1,4 +1,5 @@
  #include <asm/asm.h>
+#include <asm/asm-offsets.h>
  #include <asm/riscv_encoding.h>


         .section .text.header, "ax", %progbits

How does a need for this arise all of the sudden, without other changes
to this file? Is this maybe a leftover which wants dropping?

Yeah, it is really weird. I'll check that.

Quoted 13 lines
Quoted 11 lines
xen/arch/riscv/xen.lds.S +0 -0
@@ -137,6 +137,7 @@ SECTIONS
      __init_end = .;


     .bss : {                     /* BSS */
+        . = ALIGN(POINTER_ALIGN);
         __bss_start = .;
         *(.bss.stack_aligned)
         . = ALIGN(PAGE_SIZE);

Doesn't this want to be a separate change?

Yes, it should be. I'll do that in new verstion of the patch series.

~ Oleksii

#6 lore raw

Re: [PATCH v6 2/4] xen/riscv: introduce setup_initial_pages

Quoted 47 lines · Oleksii
Quoted 46 lines · Jan Beulich
Quoted 41 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h new file +5 -0
@@ -0,0 +1,62 @@
+#ifndef _ASM_RISCV_PAGE_H
+#define _ASM_RISCV_PAGE_H
+
+#include <xen/const.h>
+#include <xen/types.h>

=)+
+#define VPN_MASK                    ((unsigned
long)(PAGETABLE_ENTRIES - 1))

xen/arch/riscv/include/asm/page.h new file +3 -0
+
+#define XEN_PT_LEVEL_ORDER(lvl)     ((lvl) * PAGETABLE_ORDER)
+#define XEN_PT_LEVEL_SHIFT(lvl)     (XEN_PT_LEVEL_ORDER(lvl) +

PAGE_SHIFT)
+#define XEN_PT_LEVEL_SIZE(lvl)      (_AT(paddr_t, 1) << XEN_PT_LEVEL_SHIFT(lvl))
+#define XEN_PT_LEVEL_MAP_MASK(lvl)  (~(XEN_PT_LEVEL_SIZE(lvl) - 1))
+#define XEN_PT_LEVEL_MASK(lvl)      (VPN_MASK << XEN_PT_LEVEL_SHIFT(lvl))

xen/arch/riscv/include/asm/page.h new file +12 -0
+
+#define PTE_VALID                   BIT(0, UL)
+#define PTE_READABLE                BIT(1, UL)
+#define PTE_WRITABLE                BIT(2, UL)
+#define PTE_EXECUTABLE              BIT(3, UL)
+#define PTE_USER                    BIT(4, UL)
+#define PTE_GLOBAL                  BIT(5, UL)
+#define PTE_ACCESSED                BIT(6, UL)
+#define PTE_DIRTY                   BIT(7, UL)
+#define PTE_RSW                     (BIT(8, UL) | BIT(9, UL))
+
+#define PTE_LEAF_DEFAULT            (PTE_VALID | PTE_READABLE |

PTE_WRITABLE)

xen/arch/riscv/include/asm/page.h new file +4 -0
+#define PTE_TABLE                   (PTE_VALID)
+
+/* Calculate the offsets into the pagetables for a given VA */
+#define pt_linear_offset(lvl, va)   ((va) >>

XEN_PT_LEVEL_SHIFT(lvl))

xen/arch/riscv/include/asm/page.h new file +2 -0
+
+#define pt_index(lvl, va) pt_linear_offset(lvl, (va) &
Quoted 1 line

XEN_PT_LEVEL_MASK(lvl))

Maybe better

Quoted 4 lines
Quoted 3 lines

#define pt_index(lvl, va) (pt_linear_offset(lvl, va) & VPN_MASK)

as the involved constant will be easier to use for the compiler?

But VPN_MASK should be shifted by level shift value.

Why? pt_linear_offset() already does the necessary shifting.

Quoted 14 lines
Quoted 7 lines

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

Quoted 6 lines

+    csr_write(CSR_SATP, 0);

xen/arch/riscv/include/asm/page.h new file +5 -0
+
+    /* Clean MMU root page table */
+    stage1_pgtbl_root[index] = paddr_to_pte(0x0, 0x0);
+
+    asm volatile ( "sfence.vma" );
Quoted 7 lines

Doesn't this want to move between the SATP write and the clearing of the
root table slot? Also here and elsewhere - don't these asm()s need memory
clobbers? And anyway - could I talk you into introducing an inline wrapper
(e.g. named sfence_vma()) so all uses end up consistent?

Quoted 4 lines

I think clearing of root page table should be done before "sfence.vma" because we have to first clear slot of MMU's root page table and then make updating root page table visible for all. ( by usage of sfence instruction )

I disagree. The SATP write has removed the connection of the CPU to the page tables. That's the action you want to fence, not the altering of some (then) no longer referenced data structure.

Quoted 15 lines
Quoted 15 lines

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

Quoted 6 lines
xen/arch/riscv/include/asm/page.h new file +6 -0
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, NULL };
+
+    /*
+     * Access to _stard, _end is always PC-relative

Nit: Typo-ed symbol name. Also ...

Quoted 5 lines

+     * thereby when access them we will get load adresses +     * of start and end of Xen
+     * To get linker addresses LOAD_TO_LINK() is required +     * to use
+     */

Quoted 3 lines
Quoted 2 lines

see the earlier line wrapping remark again. Finally in multi-sentence comments full stops are required.

Full stops mean '.' at the end of sentences?

Yes. Please see ./CODING_STYLE.

Jan

author
#7 lore raw

Re: [PATCH v6 2/4] xen/riscv: introduce setup_initial_pages

Quoted 50 lines · Jan Beulich
Quoted 49 lines · Oleksii
Quoted 48 lines · Jan Beulich
Quoted 43 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h new file +5 -0
@@ -0,0 +1,62 @@
+#ifndef _ASM_RISCV_PAGE_H
+#define _ASM_RISCV_PAGE_H
+
+#include <xen/const.h>
+#include <xen/types.h>

=)+
+#define VPN_MASK                    ((unsigned
long)(PAGETABLE_ENTRIES - 1))

xen/arch/riscv/include/asm/page.h new file +3 -0
+
+#define XEN_PT_LEVEL_ORDER(lvl)     ((lvl) * PAGETABLE_ORDER)
+#define XEN_PT_LEVEL_SHIFT(lvl)     (XEN_PT_LEVEL_ORDER(lvl) +

PAGE_SHIFT)
+#define XEN_PT_LEVEL_SIZE(lvl)      (_AT(paddr_t, 1) << XEN_PT_LEVEL_SHIFT(lvl))

xen/arch/riscv/include/asm/page.h new file +1 -1
+#define XEN_PT_LEVEL_MAP_MASK(lvl)  (~(XEN_PT_LEVEL_SIZE(lvl)
-

1))
+#define XEN_PT_LEVEL_MASK(lvl)      (VPN_MASK <<
XEN_PT_LEVEL_SHIFT(lvl))

xen/arch/riscv/include/asm/page.h new file +12 -0
+
+#define PTE_VALID                   BIT(0, UL)
+#define PTE_READABLE                BIT(1, UL)
+#define PTE_WRITABLE                BIT(2, UL)
+#define PTE_EXECUTABLE              BIT(3, UL)
+#define PTE_USER                    BIT(4, UL)
+#define PTE_GLOBAL                  BIT(5, UL)
+#define PTE_ACCESSED                BIT(6, UL)
+#define PTE_DIRTY                   BIT(7, UL)
+#define PTE_RSW                     (BIT(8, UL) | BIT(9, UL))
+
+#define PTE_LEAF_DEFAULT            (PTE_VALID | PTE_READABLE

|
PTE_WRITABLE)

xen/arch/riscv/include/asm/page.h new file +4 -0
+#define PTE_TABLE                   (PTE_VALID)
+
+/* Calculate the offsets into the pagetables for a given VA */
+#define pt_linear_offset(lvl, va)   ((va) >>

XEN_PT_LEVEL_SHIFT(lvl))

xen/arch/riscv/include/asm/page.h new file +2 -0
+
+#define pt_index(lvl, va) pt_linear_offset(lvl, (va) &
Quoted 1 line

XEN_PT_LEVEL_MASK(lvl))

Maybe better

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

#define pt_index(lvl, va) (pt_linear_offset(lvl, va) & VPN_MASK)

as the involved constant will be easier to use for the compiler?

But VPN_MASK should be shifted by level shift value.

Why? pt_linear_offset() already does the necessary shifting.

I missed a way how you offered to define pt_index(). I thought you offered to define it as "pt_linear_offset(lvl, va & VPN_MASK)" instead of
"(pt_linear_offset(lvl, va) & VPN_MASK)".

So you are right we can re-write as you offered.

Quoted 25 lines
Quoted 18 lines
Quoted 10 lines

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

Quoted 7 lines

+    csr_write(CSR_SATP, 0);

xen/arch/riscv/include/asm/page.h new file +5 -0
+
+    /* Clean MMU root page table */
+    stage1_pgtbl_root[index] = paddr_to_pte(0x0, 0x0);
+
+    asm volatile ( "sfence.vma" );

Doesn't this want to move between the SATP write and the clearing of

Quoted 8 lines

the
root table slot? Also here and elsewhere - don't these asm()s need
memory
clobbers? And anyway - could I talk you into introducing an inline
wrapper
(e.g. named sfence_vma()) so all uses end up consistent?

Quoted 6 lines

I think clearing of root page table should be done before "sfence.vma"
because we have to first clear slot of MMU's root page table and then
make updating root page table visible for all. ( by usage of sfence instruction )

Quoted 3 lines

I disagree. The SATP write has removed the connection of the CPU to the page tables. That's the action you want to fence, not the altering of some (then) no longer referenced data structure.

From that point of view you are right. Thanks for clarification.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

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

Quoted 7 lines
xen/arch/riscv/include/asm/page.h new file +6 -0
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, NULL };
+
+    /*
+     * Access to _stard, _end is always PC-relative

Nit: Typo-ed symbol name. Also ...

Quoted 5 lines

+     * thereby when access them we will get load adresses +     * of start and end of Xen
+     * To get linker addresses LOAD_TO_LINK() is required +     * to use
+     */

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

see the earlier line wrapping remark again. Finally in multi- sentence
comments full stops are required.

Full stops mean '.' at the end of sentences?

Yes. Please see ./CODING_STYLE.

Thanks. I'll read it again.

~ Oleksii