Skip to contentSkip to conversation

enable MMU for RISC-V

PATCH Superseded Oleksii Kurochko 3 participants 13 messages Mar 27, 2023 – Apr 4, 2023 RISC-V

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover enable MMU for RISC-V

The patch series is based on top patches [1] and [2]

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.

[1] https://lore.kernel.org/xen-devel/2785518800dce64fafb3096480a5ae4c4e026bcb.1678970065.git.oleksii.kurochko@gmail.com/
[2] https://lore.kernel.org/xen-devel/7c066d6dcc0618749df04785b34b93819148087d.1678970065.git.oleksii.kurochko@gmail.com/

---
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 (3):
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    |  15 +-
 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                    | 277 +++++++++++++++++++++++++
 xen/arch/riscv/riscv64/head.S          |   2 +
 xen/arch/riscv/setup.c                 |  22 +-
 xen/arch/riscv/xen.lds.S               |   3 +
 9 files changed, 392 insertions(+), 9 deletions(-)
 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.39.2
#11 lore raw

PATCH v3 2/3 xen/riscv: setup initial pagetables

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 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.39.2
#12 lore raw

PATCH v3 3/3 xen/riscv: remove dummy_bss variable

In reply to Oleksii Kurochko

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

---
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.39.2
#1 lore raw

PATCH v3 1/3 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.

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

---
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    |  15 +-
 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                    | 277 +++++++++++++++++++++++++
 xen/arch/riscv/riscv64/head.S          |   2 +
 xen/arch/riscv/setup.c                 |  11 +
 xen/arch/riscv/xen.lds.S               |   3 +
 9 files changed, 389 insertions(+), 1 deletion(-)
 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 +14 -1
index 763a922a04..7ffe9051b2 100644
@@ -39,12 +39,25 @@
   name:
 #endif
 
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#define ADDRESS_SPACE_END (_AC(-1, UL))
+#define SZ_1G             0x40000000
+
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START    (ADDRESS_SPACE_END - SZ_1G + 1)
+#else
+#error "RV32 isn't supported"asm
+#endif
 
 #define SMP_CACHE_BYTES (1 << 6)
 
 #define STACK_SIZE PAGE_SIZE
 
+#ifdef CONFIG_RISCV_64
+#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/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..0879a527f2 100644
@@ -1,6 +1,16 @@
 #ifndef __RISCV_PAGE_BITS_H__
 #define __RISCV_PAGE_BITS_H__
 
+#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
+
 #define PAGE_SHIFT              12 /* 4 KiB Pages */
 #define PADDR_BITS              56 /* 44-bit PPN */
 
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 +277 -0

277 lines not on this page · raw message

xen/arch/riscv/riscv64/head.S +2 -0
index 8887f0cbd4..b3309d902c 100644
@@ -1,4 +1,5 @@
 #include <asm/asm.h>
+#include <asm/asm-offsets.h>
 #include <asm/riscv_encoding.h>
 
         .section .text.header, "ax", %progbits
@@ -32,3 +33,4 @@ ENTRY(start)
         add     sp, sp, t0
 
         tail    start_xen
+
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 +3 -0
index f299ea8422..54d4236866 100644
@@ -136,6 +136,7 @@ SECTIONS
     . = ALIGN(POINTER_ALIGN);
     __init_end = .;
 
+    . = ALIGN(PAGE_SIZE);
     .bss : {                     /* BSS */
         __bss_start = .;
         *(.bss.stack_aligned)
@@ -169,3 +170,5 @@ SECTIONS
 
 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.39.2
#2 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

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

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +2 -1
@@ -39,12 +39,25 @@
   name:
 #endif
 
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#define ADDRESS_SPACE_END (_AC(-1, UL))
+#define SZ_1G             0x40000000

No need for this - please use GB(1) (see xen/config.h) in its stead.

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

Quoted 2 lines
xen/arch/riscv/include/asm/config.h +2 -0
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START    (ADDRESS_SPACE_END - SZ_1G + 1)

I first thought the " + 1" would be rendering the address misaligned. May I suggest (ADDRESS_SPACE_END + 1 - SZ_1G) to help avoiding this impression?

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

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

Stray "asm" at the end of the line.

Quoted 30 lines
xen/arch/riscv/include/asm/page.h new file +27 -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)
+#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)
+

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

Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10 -0
+/* 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))
+
+#ifdef CONFIG_RISCV_64
+typedef uint64_t pte_t;
+#else
+typedef uint32_t pte_t;
+#endif

Are you sure you don't want to encapsulate these in a struct, for type safety purposes?

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

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

This looking like the inverse of ...

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

Quoted 3 lines
xen/arch/riscv/include/asm/page.h new file +3 -0
+/* Shift the VPN[x] or PPN[x] fields of a virtual or physical address
+ * to become the shifted PPN[x] fields of a page table entry */
+#define addr_to_ppn(x) (((x) >> PAGE_SHIFT) << PTE_PPN_SHIFT)

... this, does that one want to be called "ppn_to_addr()"? Otherwise I can't see how name an operation fit together.

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

Quoted 1 line
xen/arch/riscv/include/asm/page.h new file +1 -0
+static inline pte_t paddr_to_pte(const unsigned long paddr,

paddr_t?

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

Quoted 7 lines
xen/arch/riscv/include/asm/page.h new file +7 -0
+                                 const unsigned long permissions)
+{
+    pte_t tmp = (pte_t)addr_to_ppn(paddr);
+    return tmp | permissions;
+}
+
+static inline pte_t pte_to_paddr(const unsigned long paddr)

A function of this name wants to take pte_t as parameter and return paddr_t (note the type safety remark higher up, as it becomes relevant right here). It also hardly can be implemented ...

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

Quoted 2 lines
xen/arch/riscv/include/asm/page.h new file +2 -0
+{
+    return (pte_t)addr_to_pte(paddr);

... in terms of a function/macro with effectively the opposite name.

Quoted 4 lines
xen/arch/riscv/mm.c new file +1 -0
@@ -0,0 +1,277 @@
+#include <xen/init.h>

xen/arch/riscv/mm.c line 11

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+#include <xen/kernel.h>
+
+#include <asm/early_printk.h>
+#include <asm/config.h>
+#include <asm/csr.h>
+#include <asm/mm.h>
+#include <asm/page.h>
+#include <asm/processor.h>
+
+#define PGTBL_INITIAL_COUNT 8

What does this magic number derive from? (A comment may help.)

xen/arch/riscv/mm.c line 12

Quoted 1 line
xen/arch/riscv/mm.c new file +1 -0
+#define PGTBL_ENTRY_AMOUNT  (PAGE_SIZE/sizeof(pte_t))

Nit: Style (blanks around binary operators).

xen/arch/riscv/mm.c line 16

Quoted 3 lines
xen/arch/riscv/mm.c new file +3 -0
+struct mmu_desc {
+    unsigned long num_levels;
+    uint32_t pgtbl_count;

Nit: Style (as before please avoid fixed width types when their use isn't really warranted; afaics unsigned int will do fine here and elsewhere below).

xen/arch/riscv/mm.c line 22

Quoted 6 lines
xen/arch/riscv/mm.c new file +6 -0
+    pte_t *next_pgtbl;
+    pte_t *pgtbl_base;
+};
+
+extern unsigned long __bss_start[];
+extern unsigned long __bss_end[];

Why are these being added here? They aren't being used afaics.

Quoted 4 lines
xen/arch/riscv/mm.c new file +4 -0
+extern unsigned char cpu0_boot_stack[STACK_SIZE];
+
+#define PHYS_OFFSET ((unsigned long)_start - XEN_VIRT_START)
+#define LOAD_TO_LINK(addr) ((addr) - PHYS_OFFSET)

xen/arch/riscv/mm.c line 36

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+#define LINK_TO_LOAD(addr) ((addr) + PHYS_OFFSET)
+
+pte_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
+stage1_pgtbl_root[PGTBL_ENTRY_AMOUNT];
+
+pte_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
+stage1_pgtbl_nonroot[PGTBL_INITIAL_COUNT * PGTBL_ENTRY_AMOUNT];
+
+#define HANDLE_PGTBL()                                                      \
+    index =  pt_index(curr_lvl_num, page_addr);                             \

Nit: Style (stray double blanks).

xen/arch/riscv/mm.c line 37

Quoted 1 line
xen/arch/riscv/mm.c new file +1 -0
+    if ( pte_is_valid(pgtbl[index]) ) {                                     \

Nit: Style (brace placement throughout the macro - in really small macros the way you have it may be okay as an exception, but this macro isn't small).

Quoted 11 lines
xen/arch/riscv/mm.c new file +11 -0
+        /* Find L{ 0-3 } table */                                           \
+        pgtbl = (pte_t *)pte_to_paddr(pgtbl[index]);                        \
+    } else {                                                                \
+        /* Allocate new L{0-3} page table */                                \
+        if ( mmu_desc->pgtbl_count == PGTBL_INITIAL_COUNT ) {               \
+            early_printk("(XEN) No initial table available\n");             \
+            /* panic(), BUG() or ASSERT() aren't ready now. */              \
+            die();                                                          \
+        }                                                                   \
+        mmu_desc->pgtbl_count++;                                            \
+        pgtbl[index] = paddr_to_pte((unsigned long)mmu_desc->next_pgtbl,    \

xen/arch/riscv/mm.c line 58

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+                                    PTE_VALID);                             \
+        pgtbl = mmu_desc->next_pgtbl;                                       \
+        mmu_desc->next_pgtbl += PGTBL_ENTRY_AMOUNT;                         \
+    }
+
+static void __init setup_initial_mapping(struct mmu_desc *mmu_desc,
+                                  unsigned long map_start,
+                                  unsigned long map_end,
+                                  unsigned long pa_start,
+                                  unsigned long permission)

Nit: Style (indentation, and it feels like I commented on this before).

xen/arch/riscv/mm.c line 67

Quoted 9 lines
xen/arch/riscv/mm.c new file +9 -0
+{
+    uint32_t index;
+    pte_t *pgtbl;
+    unsigned long page_addr;
+    unsigned int curr_lvl_num;
+    pte_t pte_to_be_written;
+    unsigned long paddr;
+
+    if ( (unsigned long)_start % XEN_PT_LEVEL_SIZE(0) ) {

Nit: Style (brace placement).

xen/arch/riscv/mm.c line 73

Quoted 6 lines
xen/arch/riscv/mm.c new file +6 -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) ) {

Nit: Style (indentation and brace placement; more below).

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+        early_printk("(XEN) map and pa start addresses should be aligned\n");
+        /* panic(), BUG() or ASSERT() aren't ready now. */
+        die();
+    }
+
+    page_addr = map_start;
+    while ( page_addr < map_end ) {
+        pgtbl = mmu_desc->pgtbl_base;
+
+        switch (mmu_desc->num_levels)

xen/arch/riscv/mm.c line 94

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+        {
+        case 4: /* Level 3 */
+            curr_lvl_num = 3;
+            HANDLE_PGTBL();
+        case 3: /* Level 2 */
+            curr_lvl_num = 2;
+            HANDLE_PGTBL();
+        case 2: /* Level 1 */
+            curr_lvl_num = 1;
+            HANDLE_PGTBL();

Am I getting it right that curr_lvl_num solely exists to pass a parameter to the macro? If so, why doesn't the macro take an appropriate parameter instead?

Quoted 9 lines
xen/arch/riscv/mm.c new file +9 -0
+        case 1: /* Level 0 */
+            index = pt_index(0, page_addr);
+            paddr = (page_addr - map_start) + pa_start;
+            pte_to_be_written = paddr_to_pte(paddr, permission);
+
+            if ( !pte_is_valid(pgtbl[index]) )
+                pgtbl[index] = pte_to_be_written;
+            else {
+                /*

xen/arch/riscv/mm.c line 113

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+                 * get an adresses of current pte and that one to
+                 * be written without permission flags
+                 */
+                unsigned long curr_pte =
+                    (unsigned long)&pgtbl[index] & (PTE_PPN_SHIFT - 1);
+                pte_to_be_written &= (PTE_PPN_SHIFT - 1);
+
+                if ( curr_pte != pte_to_be_written ) {
+                    early_printk("PTE that is intended to write isn't the \
+                                 same that the once are overwriting\n");

Iirc there are compilers which warn about line continuation characters inside a string literal. Since adjacent string literals are concatenated by the compiler anyway, you'll find elsewhere we have the equivalent of

early_printk("PTE that is intended to write isn't the"
" same that the once are overwriting\n");

This will also avoid an excessive number of blanks in the middle of the string.

Quoted 7 lines
xen/arch/riscv/mm.c new file +7 -0
+                    /* panic(), <asm/bug.h> aren't ready now. */
+                    die();
+                }
+            }
+        }
+
+        /* Point to next page */

xen/arch/riscv/mm.c line 130

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+        page_addr += XEN_PT_LEVEL_SIZE(0);
+    }
+}
+
+static void __init calc_pgtbl_lvls_num(struct  mmu_desc *mmu_desc)
+{
+    unsigned long satp_mode = RV_STAGE1_MODE;
+
+    /* Number of page table levels */
+    switch (satp_mode) {

Nit: Style (missing blanks and brace placement).

xen/arch/riscv/mm.c line 142

Quoted 12 lines
xen/arch/riscv/mm.c new file +12 -0
+    case SATP_MODE_SV32:
+        mmu_desc->num_levels = 2;
+        break;
+    case SATP_MODE_SV39:
+        mmu_desc->num_levels = 3;
+        break;
+    case SATP_MODE_SV48:
+        mmu_desc->num_levels = 4;
+        break;
+    default:
+        early_printk("(XEN) Unsupported SATP_MODE\n");
+        while (1);

die() (until you have panic()) like above?

Quoted 4 lines
xen/arch/riscv/mm.c new file +4 -0
+    }
+}
+
+static void __init setup_ptes_permission(struct mmu_desc *mmu_desc)

xen/arch/riscv/mm.c line 156

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+{
+    pte_t *pgtbl;
+    uint32_t index;
+    uint32_t permissions = PTE_VALID;
+    unsigned long start = LOAD_TO_LINK((unsigned long)_start);
+    unsigned long end   = LOAD_TO_LINK((unsigned long)_end);
+
+    while ( start < end )
+    {
+        pgtbl = (pte_t *) mmu_desc->pgtbl_base;

Nit: Style (extra blank after closing parenthesis). But afaict you don't need a case here in the first place; as said before, please avoid casts whenever possible.

xen/arch/riscv/mm.c line 155

Quoted 1 line
xen/arch/riscv/mm.c new file +1 -0
+        switch (mmu_desc->num_levels)

Nit: Style (Missing blanks).

Quoted 11 lines
xen/arch/riscv/mm.c new file +11 -0
+        {
+        case 4: /* find Level 3 page table*/
+            index = pt_index(3, start);
+            pgtbl = (pte_t *)pte_to_paddr(pgtbl[index]);
+        case 3: /* find level 2 page table */
+            index = pt_index(2, start);
+            pgtbl = (pte_t *)pte_to_paddr(pgtbl[index]);
+        case 2: /* find level 1 page table */
+            index = pt_index(1, start);
+            pgtbl = (pte_t *)pte_to_paddr(pgtbl[index]);
+        case 1: /* find level 0 page table */

xen/arch/riscv/mm.c line 179

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+            index = pt_index(0, start);
+
+            if ( is_kernel_text(LINK_TO_LOAD(start)) ||
+                 is_kernel_inittext(LINK_TO_LOAD(start)) )
+                permissions |= PTE_EXECUTABLE | PTE_READABLE;
+
+            if ( is_kernel_rodata(LINK_TO_LOAD(start)) )
+                permissions |= PTE_READABLE;
+
+            pgtbl[index] |= permissions;

setup_initial_mapping() has installed R/W mappings. Hence ORing in PTE_READABLE is meaningless here, and neither .text nor .rodata will end up write-protected. The question is ...

Quoted 44 lines
xen/arch/riscv/mm.c new file +44 -0
+        }
+
+        start += PAGE_SIZE;
+    }
+}
+
+/*
+ * setup_initial_pagetables:
+ *
+ * Build the page tables for Xen that map the following:
+ *  1. Calculate page table's level numbers.
+ *  2. Init mmu description structure.
+ *  3. Check that linker addresses range doesn't overlap
+ *     with load addresses range
+ *  4. Map all linker addresses and load addresses ( it shouldn't
+ *     be 1:1 mapped and will be 1:1 mapped only in case if
+ *     linker address is equal to load address ) with
+ *     RW permissions by default.
+ *  5. Setup proper PTE permissions for each section.
+ */
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, 0 };
+
+    /*
+     * Access to _{stard, end } is always PC-relative
+     * 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
+     */
+    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();
+    }
+
+    calc_pgtbl_lvls_num(&mmu_desc);
+

xen/arch/riscv/mm.c line 233

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+    mmu_desc.pgtbl_base = stage1_pgtbl_root;
+    mmu_desc.next_pgtbl = stage1_pgtbl_nonroot;
+
+    setup_initial_mapping(&mmu_desc,
+                          linker_start,
+                          linker_end,
+                          load_start,
+                          PTE_LEAF_DEFAULT);
+
+    setup_ptes_permission(&mmu_desc);

...: Why does this require a 2nd pass / function in the first place?

Quoted 5 lines
xen/arch/riscv/mm.c new file +5 -0
+}
+
+void __init enable_mmu()
+{
+    /*

xen/arch/riscv/mm.c line 248

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+     * 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));

That looks like a pretty unusual way of moving from physical to virtual addresses. But if it works ...

xen/arch/riscv/mm.c line 255

Quoted 6 lines
xen/arch/riscv/mm.c new file +6 -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,
+              ((unsigned long)stage1_pgtbl_root >> PAGE_SHIFT) | RV_STAGE1_MODE << SATP_MODE_SHIFT);

Nit: Style (excessively long line).

xen/arch/riscv/mm.c line 258

Quoted 2 lines
xen/arch/riscv/mm.c new file +2 -0
+    asm volatile(".align 2");
+mmu_is_enabled:

Nit: Style (labels want indenting by at least one blank).

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

Quoted 6 lines
xen/arch/riscv/mm.c new file +2 -0
@@ -26,3 +27,13 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     unreachable();
 }
+
+void __init noreturn cont_after_mmu_is_enabled(void)

To prevent undue link-time optimization such a function also wants to be noinline.

Jan

#3 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

Hi,

Quoted 13 lines · Jan Beulich

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

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +2 -1
@@ -39,12 +39,25 @@
    name:
  #endif
 
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#define ADDRESS_SPACE_END (_AC(-1, UL))
+#define SZ_1G             0x40000000

No need for this - please use GB(1) (see xen/config.h) in its stead.

Quoted 6 lines

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

Quoted 2 lines
xen/arch/riscv/include/asm/config.h +2 -0
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START    (ADDRESS_SPACE_END - SZ_1G + 1)

I first thought the " + 1" would be rendering the address misaligned. May I suggest (ADDRESS_SPACE_END + 1 - SZ_1G) to help avoiding this impression?

I will jump on this to say that I am finding quite difficult to review code using define/variable that contains "end" in their name because it is never clear whether this is inclusive or exclusive.

In this case, it is inclusive, but within the same patch I can see map_end which is exclusive.

For this case, my suggestion would be to remove ADDRESS_SPACE_END and just hardcode the value where you want to put Xen. For others, you could use (start, size) to describe a region.

Also, are you sure that all the CPU will be able to support more full 64-bit VA space?

Cheers,

Signature · 1 line
Julien Grall
author
#7 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

In reply to Jan Beulich
Quoted 12 lines · Jan Beulich

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

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +0 -0
@@ -39,12 +39,25 @@

   name:
 #endif

xen/arch/riscv/include/asm/config.h +2 -1
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#define ADDRESS_SPACE_END (_AC(-1, UL))
+#define SZ_1G             0x40000000

No need for this - please use GB(1) (see xen/config.h) in its stead.

I'll use GB(1). Thanks.

Quoted 7 lines

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

Quoted 3 lines
xen/arch/riscv/include/asm/config.h +2 -0
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START    (ADDRESS_SPACE_END - SZ_1G + 1)

I first thought the " + 1" would be rendering the address misaligned. May I suggest (ADDRESS_SPACE_END + 1 - SZ_1G) to help avoiding this impression?

Sure. Will update it in next version of the patch series.

Quoted 5 lines

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

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

Stray "asm" at the end of the line.

My fault...

Quoted 45 lines
Quoted 45 lines
xen/arch/riscv/include/asm/page.h new file +7 -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) &

XEN_PT_LEVEL_MASK(lvl))

xen/arch/riscv/include/asm/page.h new file +2 -0
+
+#ifdef CONFIG_RISCV_64
Quoted 8 lines

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

Quoted 4 lines
xen/arch/riscv/include/asm/page.h new file +4 -0
+typedef uint64_t pte_t;
+#else
+typedef uint32_t pte_t;
+#endif

Are you sure you don't want to encapsulate these in a struct, for type
safety purposes?

Yeah, it makes sense.

Quoted 5 lines
Quoted 2 lines

+#define addr_to_pte(x) (((x) >> PTE_PPN_SHIFT) << PAGE_SHIFT)

This looking like the inverse of ...

Quoted 8 lines
Quoted 4 lines

+/* Shift the VPN[x] or PPN[x] fields of a virtual or physical address
+ * to become the shifted PPN[x] fields of a page table entry */ +#define addr_to_ppn(x) (((x) >> PAGE_SHIFT) << PTE_PPN_SHIFT)

... this, does that one want to be called "ppn_to_addr()"? Otherwise I
can't see how name an operation fit together.

ppn_to_addr() will be better so it will be update in next version of the patch series.

Quoted 4 lines
Quoted 2 lines

+static inline pte_t paddr_to_pte(const unsigned long paddr,

paddr_t?

It looks like I used paddr_t before but got a recommendation to change it to 'ul'. I will double-check.

Quoted 14 lines

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

Quoted 8 lines

+                                 const unsigned long permissions)

xen/arch/riscv/include/asm/page.h new file +6 -0
+{
+    pte_t tmp = (pte_t)addr_to_ppn(paddr);
+    return tmp | permissions;
+}
+
+static inline pte_t pte_to_paddr(const unsigned long paddr)

A function of this name wants to take pte_t as parameter and return paddr_t (note the type safety remark higher up, as it becomes relevant
right here). It also hardly can be implemented ...

Quoted 4 lines
Quoted 2 lines

+{
+    return (pte_t)addr_to_pte(paddr);

... in terms of a function/macro with effectively the opposite name.

Thanks. I'll update that.

Quoted 17 lines
Quoted 5 lines
xen/arch/riscv/mm.c new file +1 -0
@@ -0,0 +1,277 @@
+#include <xen/init.h>

xen/arch/riscv/mm.c line 11

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+#include <xen/kernel.h>
+
+#include <asm/early_printk.h>
+#include <asm/config.h>
+#include <asm/csr.h>
+#include <asm/mm.h>
+#include <asm/page.h>
+#include <asm/processor.h>
+
+#define PGTBL_INITIAL_COUNT 8

What does this magic number derive from? (A comment may help.)

It can be now 4 tables as it is enough to map 2Mb. 8 it was when I experimented with bigger sizes of Xen binary and verified that logic of working with page tables works.

Quoted 4 lines

xen/arch/riscv/mm.c line 12

Quoted 2 lines
xen/arch/riscv/mm.c new file +1 -0
+#define PGTBL_ENTRY_AMOUNT  (PAGE_SIZE/sizeof(pte_t))

Nit: Style (blanks around binary operators).

Thanks.

Quoted 8 lines

xen/arch/riscv/mm.c line 16

Quoted 4 lines
xen/arch/riscv/mm.c new file +3 -0
+struct mmu_desc {
+    unsigned long num_levels;
+    uint32_t pgtbl_count;

Nit: Style (as before please avoid fixed width types when their use isn't really warranted; afaics unsigned int will do fine here and elsewhere below).

I will change it but I am not sure that I fully understand what is an issue with uint32_t.

Quoted 9 lines

xen/arch/riscv/mm.c line 22

Quoted 7 lines
xen/arch/riscv/mm.c new file +6 -0
+    pte_t *next_pgtbl;
+    pte_t *pgtbl_base;
+};
+
+extern unsigned long __bss_start[];
+extern unsigned long __bss_end[];

Why are these being added here? They aren't being used afaics.

Misssed to remove them. They was used in the previous version of the patch.

Quoted 12 lines
Quoted 12 lines
xen/arch/riscv/mm.c new file +11 -0
+extern unsigned char cpu0_boot_stack[STACK_SIZE];
+
+#define PHYS_OFFSET ((unsigned long)_start - XEN_VIRT_START)
+#define LOAD_TO_LINK(addr) ((addr) - PHYS_OFFSET)
+#define LINK_TO_LOAD(addr) ((addr) + PHYS_OFFSET)
+
+pte_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
+stage1_pgtbl_root[PGTBL_ENTRY_AMOUNT];
+
+pte_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
+stage1_pgtbl_nonroot[PGTBL_INITIAL_COUNT * PGTBL_ENTRY_AMOUNT];
Quoted 8 lines
Quoted 6 lines
xen/arch/riscv/mm.c new file +2 -0
+
+#define

HANDLE_PGTBL()
\
+    index =  pt_index(curr_lvl_num,
page_addr);                             \

Nit: Style (stray double blanks).

Sure. Thanks.

Quoted 8 lines
Quoted 3 lines

+    if ( pte_is_valid(pgtbl[index]) )
{                                     \

Nit: Style (brace placement throughout the macro - in really small macros the way you have it may be okay as an exception, but this macro
isn't small).

It looks like I confused brace placement for if/else with LK... Anyway it will be updated.

Quoted 28 lines
Quoted 28 lines

+        /* Find L{ 0-3 } table
*/                                           \
+        pgtbl = (pte_t
*)pte_to_paddr(pgtbl[index]);                        \
+    } else
{                                                                \
+        /* Allocate new L{0-3} page table
*/                                \
+        if ( mmu_desc->pgtbl_count == PGTBL_INITIAL_COUNT ) {               \
+            early_printk("(XEN) No initial table available\n");             \
+            /* panic(), BUG() or ASSERT() aren't ready now. */              \
+
die();                                                          \
+
}
\
+        mmu_desc-

Quoted 1 line

pgtbl_count++;                                            \

+        pgtbl[index] = paddr_to_pte((unsigned long)mmu_desc-

Quoted 1 line

next_pgtbl,    \

+
PTE_VALID);                             \
+        pgtbl = mmu_desc-

Quoted 1 line

next_pgtbl;                                       \

Quoted 13 lines
Quoted 10 lines

+        mmu_desc->next_pgtbl +=
PGTBL_ENTRY_AMOUNT;                         \
+    }

xen/arch/riscv/mm.c new file +2 -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 long permission)

Nit: Style (indentation, and it feels like I commented on this before).

You commented. I will double check because I though that I fixed it.

Quoted 12 lines

xen/arch/riscv/mm.c line 67

Quoted 10 lines
xen/arch/riscv/mm.c new file +9 -0
+{
+    uint32_t index;
+    pte_t *pgtbl;
+    unsigned long page_addr;
+    unsigned int curr_lvl_num;
+    pte_t pte_to_be_written;
+    unsigned long paddr;
+
+    if ( (unsigned long)_start % XEN_PT_LEVEL_SIZE(0) ) {

Nit: Style (brace placement).

Thanks. Ill update brace placement.

Quoted 10 lines
Quoted 8 lines

+        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) ) {

Nit: Style (indentation and brace placement; more below).

Thanks. Ill update brace placement.

Quoted 20 lines
Quoted 20 lines

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

xen/arch/riscv/mm.c new file +14 -0
+
+    page_addr = map_start;
+    while ( page_addr < map_end ) {
+        pgtbl = mmu_desc->pgtbl_base;
+
+        switch (mmu_desc->num_levels)
+        {
+        case 4: /* Level 3 */
+            curr_lvl_num = 3;
+            HANDLE_PGTBL();
+        case 3: /* Level 2 */
+            curr_lvl_num = 2;
+            HANDLE_PGTBL();
+        case 2: /* Level 1 */
Quoted 8 lines
Quoted 2 lines

+            curr_lvl_num = 1;
+            HANDLE_PGTBL();

Am I getting it right that curr_lvl_num solely exists to pass a parameter
to the macro? If so, why doesn't the macro take an appropriate parameter
instead?

curr_lvl_num exists only to pass current page table level to the macro. I'll add a parameter to the macro.
Thanks.

Quoted 31 lines
Quoted 20 lines

+        case 1: /* Level 0 */
+            index = pt_index(0, page_addr);
+            paddr = (page_addr - map_start) + pa_start; +            pte_to_be_written = paddr_to_pte(paddr, permission);

xen/arch/riscv/mm.c new file +14 -1
+
+            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 without permission flags
+                 */
+                unsigned long curr_pte =
+                    (unsigned long)&pgtbl[index] & (PTE_PPN_SHIFT
- 1);
+                pte_to_be_written &= (PTE_PPN_SHIFT - 1);
+
+                if ( curr_pte != pte_to_be_written ) {
+                    early_printk("PTE that is intended to write
Quoted 3 lines

isn't the \
+                                 same that the once are overwriting\n");

Iirc there are compilers which warn about line continuation characters
inside a string literal. Since adjacent string literals are concatenated
by the compiler anyway, you'll find elsewhere we have the equivalent of

Quoted 8 lines

                    early_printk("PTE that is intended to write isn't the"
                                 " same that the once are overwriting\n");

This will also avoid an excessive number of blanks in the middle of the
string.

Agree. It will be much better.

Quoted 12 lines
Quoted 12 lines

+                    /* panic(), <asm/bug.h> aren't ready now. */ +                    die();
+                }
+            }
+        }

xen/arch/riscv/mm.c new file +6 -0
+
+        /* Point to next page */
+        page_addr += XEN_PT_LEVEL_SIZE(0);
+    }
+}
+
Quoted 8 lines

xen/arch/riscv/mm.c line 130

Quoted 6 lines
xen/arch/riscv/mm.c new file +6 -0
+static void __init calc_pgtbl_lvls_num(struct  mmu_desc *mmu_desc)
+{
+    unsigned long satp_mode = RV_STAGE1_MODE;
+
+    /* Number of page table levels */
+    switch (satp_mode) {

Nit: Style (missing blanks and brace placement).

I think that after this patch I'll remmeber about brace placement. Thank you for remind me that :)

Quoted 14 lines
Quoted 13 lines

+    case SATP_MODE_SV32:
+        mmu_desc->num_levels = 2;
+        break;
+    case SATP_MODE_SV39:
+        mmu_desc->num_levels = 3;
+        break;
+    case SATP_MODE_SV48:
+        mmu_desc->num_levels = 4;
+        break;
+    default:
+        early_printk("(XEN) Unsupported SATP_MODE\n"); +        while (1);

Quoted 1 line

die() (until you have panic()) like above?

Should be die. Thanks.

Quoted 14 lines
Quoted 14 lines

+    }

xen/arch/riscv/mm.c new file +3 -0
+}
+
+static void __init setup_ptes_permission(struct mmu_desc

*mmu_desc)

xen/arch/riscv/mm.c new file +8 -0
+{
+    pte_t *pgtbl;
+    uint32_t index;
+    uint32_t permissions = PTE_VALID;
+    unsigned long start = LOAD_TO_LINK((unsigned long)_start);
+    unsigned long end   = LOAD_TO_LINK((unsigned long)_end);
+
+    while ( start < end )
Quoted 8 lines
Quoted 2 lines

+    {
+        pgtbl = (pte_t *) mmu_desc->pgtbl_base;

Nit: Style (extra blank after closing parenthesis). But afaict you don't
need a case here in the first place; as said before, please avoid casts
whenever possible.

There is no any sense in cast. Thanks.

Quoted 81 lines
Quoted 2 lines

+        switch (mmu_desc->num_levels)

Nit: Style (Missing blanks).

Quoted 12 lines

+        {
+        case 4: /* find Level 3 page table*/
+            index = pt_index(3, start);
+            pgtbl = (pte_t *)pte_to_paddr(pgtbl[index]); +        case 3: /* find level 2 page table */
+            index = pt_index(2, start);
+            pgtbl = (pte_t *)pte_to_paddr(pgtbl[index]); +        case 2: /* find level 1 page table */
+            index = pt_index(1, start);
+            pgtbl = (pte_t *)pte_to_paddr(pgtbl[index]); +        case 1: /* find level 0 page table */
+            index = pt_index(0, start);

xen/arch/riscv/mm.c line 179

Quoted 9 lines
xen/arch/riscv/mm.c new file +9 -0
+
+            if ( is_kernel_text(LINK_TO_LOAD(start)) ||
+                 is_kernel_inittext(LINK_TO_LOAD(start)) )
+                permissions |= PTE_EXECUTABLE | PTE_READABLE;
+
+            if ( is_kernel_rodata(LINK_TO_LOAD(start)) )
+                permissions |= PTE_READABLE;
+
+            pgtbl[index] |= permissions;

setup_initial_mapping() has installed R/W mappings. Hence ORing in PTE_READABLE is meaningless here, and neither .text nor .rodata will end up write-protected. The question is ...

Quoted 50 lines

+        }

xen/arch/riscv/mm.c new file +37 -0
+
+        start += PAGE_SIZE;
+    }
+}
+
+/*
+ * setup_initial_pagetables:
+ *
+ * Build the page tables for Xen that map the following:
+ *  1. Calculate page table's level numbers.
+ *  2. Init mmu description structure.
+ *  3. Check that linker addresses range doesn't overlap
+ *     with load addresses range
+ *  4. Map all linker addresses and load addresses ( it shouldn't
+ *     be 1:1 mapped and will be 1:1 mapped only in case if
+ *     linker address is equal to load address ) with
+ *     RW permissions by default.
+ *  5. Setup proper PTE permissions for each section.
+ */
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, 0 };
+
+    /*
+     * Access to _{stard, end } is always PC-relative
+     * 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
+     */
+    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();
+    }

xen/arch/riscv/mm.c new file +7 -0
+
+    calc_pgtbl_lvls_num(&mmu_desc);
+
+    mmu_desc.pgtbl_base = stage1_pgtbl_root;
+    mmu_desc.next_pgtbl = stage1_pgtbl_nonroot;
+
+    setup_initial_mapping(&mmu_desc,
Quoted 8 lines
Quoted 6 lines

+                          linker_start,
+                          linker_end,
+                          load_start,
+                          PTE_LEAF_DEFAULT);
+
+    setup_ptes_permission(&mmu_desc);

...: Why does this require a 2nd pass / function in the first place?

Probably I misunderstood Julien and it setup_pte_permission can be done in setup_initial_mapping() but here is the reply:
https://lore.kernel.org/xen-devel/[email protected]/

Quoted 34 lines
Quoted 13 lines
xen/arch/riscv/mm.c new file +8 -0
+}
+
+void __init 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

Quoted 8 lines

+     * 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));

That looks like a pretty unusual way of moving from physical to virtual
addresses. But if it works ...

Quoted 7 lines

+    /* 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,
+              ((unsigned long)stage1_pgtbl_root >> PAGE_SHIFT) | RV_STAGE1_MODE << SATP_MODE_SHIFT);

Quoted 1 line

Nit: Style (excessively long line).

Sure. I will update it.

Quoted 5 lines
Quoted 3 lines

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

Nit: Style (labels want indenting by at least one blank).

Thanks. I will take it into account.

Quoted 11 lines

xen/arch/riscv/mm.c

Quoted 8 lines
xen/arch/riscv/mm.c new file +0 -0
@@ -26,3 +27,13 @@ void __init noreturn start_xen(unsigned long
 bootcpu_id,


     unreachable();
 }

xen/arch/riscv/mm.c new file +2 -0
+
+void __init noreturn cont_after_mmu_is_enabled(void)

To prevent undue link-time optimization such a function also wants to be noinline.

Sure. Missed that.

~ Oleksii

author
#4 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

In reply to Julien Grall

Hi Julien,

Quoted 31 lines · Julien Grall

Hi,

Quoted 14 lines · Jan Beulich

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

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +0 -0
@@ -39,12 +39,25 @@

    name:
  #endif

xen/arch/riscv/include/asm/config.h +2 -1
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#define ADDRESS_SPACE_END (_AC(-1, UL))
+#define SZ_1G             0x40000000

No need for this - please use GB(1) (see xen/config.h) in its stead.

Quoted 7 lines

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

Quoted 2 lines
xen/arch/riscv/include/asm/config.h +2 -0
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START    (ADDRESS_SPACE_END - SZ_1G + 1)

I first thought the " + 1" would be rendering the address misaligned.
May I suggest (ADDRESS_SPACE_END + 1 - SZ_1G) to help avoiding this impression?

I will jump on this to say that I am finding quite difficult to review
code using define/variable that contains "end" in their name because it
is never clear whether this is inclusive or exclusive.

Quoted 7 lines

In this case, it is inclusive, but within the same patch I can see map_end which is exclusive.

For this case, my suggestion would be to remove ADDRESS_SPACE_END and just hardcode the value where you want to put Xen. For others, you could
use (start, size) to describe a region.

Thanks for the suggestion. I'll take it into account.

Quoted 3 lines

Also, are you sure that all the CPU will be able to support more full 64-bit VA space?

I am not sure but based on Linux it looks like it is true. ( at least, they use the same definitions for RV64 ).

I am curious how that can be checked.

~ Oleksii

#8 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

In reply to Oleksii
Quoted 18 lines · Oleksii
Quoted 17 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/mm.c new file +1 -0
@@ -0,0 +1,277 @@
+#include <xen/init.h>

xen/arch/riscv/mm.c line 11

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+#include <xen/kernel.h>
+
+#include <asm/early_printk.h>
+#include <asm/config.h>
+#include <asm/csr.h>
+#include <asm/mm.h>
+#include <asm/page.h>
+#include <asm/processor.h>
+
+#define PGTBL_INITIAL_COUNT 8

What does this magic number derive from? (A comment may help.)

Quoted 3 lines

It can be now 4 tables as it is enough to map 2Mb. 8 it was when I experimented with bigger sizes of Xen binary and verified that logic of working with page tables works.

Since this is connected to Xen's image size as you say, I guess the constant wants to move to a header, and then be used in an assertion in xen.lds.S. That way one will become easily aware if/when this 2Mb assumption breaks.

Quoted 9 lines
Quoted 7 lines

xen/arch/riscv/mm.c line 16

Quoted 3 lines
xen/arch/riscv/mm.c new file +3 -0
+struct mmu_desc {
+    unsigned long num_levels;
+    uint32_t pgtbl_count;

Nit: Style (as before please avoid fixed width types when their use isn't really warranted; afaics unsigned int will do fine here and elsewhere below).

I will change it but I am not sure that I fully understand what is an issue with uint32_t.

The issue is simply that ./CODING_STYLE says to prefer basic types whenever possible.

Quoted 38 lines
Quoted 30 lines
Quoted 30 lines
xen/arch/riscv/mm.c new file +18 -0
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, 0 };
+
+    /*
+     * Access to _{stard, end } is always PC-relative
+     * 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
+     */
+    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();
+    }

xen/arch/riscv/mm.c new file +7 -0
+
+    calc_pgtbl_lvls_num(&mmu_desc);
+
+    mmu_desc.pgtbl_base = stage1_pgtbl_root;
+    mmu_desc.next_pgtbl = stage1_pgtbl_nonroot;
+
+    setup_initial_mapping(&mmu_desc,
Quoted 8 lines
Quoted 6 lines

+                          linker_start,
+                          linker_end,
+                          load_start,
+                          PTE_LEAF_DEFAULT);
+
+    setup_ptes_permission(&mmu_desc);

...: Why does this require a 2nd pass / function in the first place?

Quoted 3 lines

Probably I misunderstood Julien and it setup_pte_permission can be done in setup_initial_mapping() but here is the reply:
https://lore.kernel.org/xen-devel/[email protected]/

Hmm, yes, his option 2 looks like what you've implemented. Still I don't see why the permissions can't be got right on the first pass.

Jan

#5 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

In reply to Oleksii
Quoted 37 lines · Oleksii
Quoted 29 lines · Julien Grall
Quoted 14 lines · Jan Beulich

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

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +0 -0
@@ -39,12 +39,25 @@

    name:
  #endif

xen/arch/riscv/include/asm/config.h +2 -1
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#define ADDRESS_SPACE_END (_AC(-1, UL))
+#define SZ_1G             0x40000000

No need for this - please use GB(1) (see xen/config.h) in its stead.

Quoted 7 lines

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

Quoted 2 lines
xen/arch/riscv/include/asm/config.h +2 -0
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START    (ADDRESS_SPACE_END - SZ_1G + 1)

I first thought the " + 1" would be rendering the address misaligned.
May I suggest (ADDRESS_SPACE_END + 1 - SZ_1G) to help avoiding this impression?

I will jump on this to say that I am finding quite difficult to review
code using define/variable that contains "end" in their name because it
is never clear whether this is inclusive or exclusive.

Quoted 7 lines

In this case, it is inclusive, but within the same patch I can see map_end which is exclusive.

For this case, my suggestion would be to remove ADDRESS_SPACE_END and just hardcode the value where you want to put Xen. For others, you could
use (start, size) to describe a region.

Quoted 6 lines

Thanks for the suggestion. I'll take it into account.

Quoted 3 lines

Also, are you sure that all the CPU will be able to support more full 64-bit VA space?

I am not sure but based on Linux it looks like it is true. ( at least, they use the same definitions for RV64 ).

Hmm, the spec has this text in a rationalizing remark: "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."

Apart from that all descriptions look to assume that the top VA bits are simply ignored for the purpose of translating to PA, which would mean that any address within the 64-bit address space can be mapped, and the same mapping would appear many times in VA space. I wonder whether that's really how it's meant to be, or whether I'm overlooking something, or whether the spec would want clarifying.

Quoted 1 line

I am curious how that can be checked.

Try it out somewhere?

Jan

author
#6 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

Quoted 59 lines · Jan Beulich
Quoted 42 lines · Oleksii
Quoted 35 lines · Julien Grall
Quoted 14 lines · Jan Beulich

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

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +0 -0
@@ -39,12 +39,25 @@

    name:
  #endif

xen/arch/riscv/include/asm/config.h +2 -1
-#define XEN_VIRT_START  _AT(UL, 0x80200000)
+#define ADDRESS_SPACE_END (_AC(-1, UL))
+#define SZ_1G             0x40000000

No need for this - please use GB(1) (see xen/config.h) in its stead.

Quoted 8 lines

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

Quoted 2 lines
xen/arch/riscv/include/asm/config.h +2 -0
+#ifdef CONFIG_RISCV_64
+#define XEN_VIRT_START    (ADDRESS_SPACE_END - SZ_1G + 1)

I first thought the " + 1" would be rendering the address misaligned.
May I suggest (ADDRESS_SPACE_END + 1 - SZ_1G) to help avoiding this
impression?

I will jump on this to say that I am finding quite difficult to review
code using define/variable that contains "end" in their name because
it
is never clear whether this is inclusive or exclusive.

In this case, it is inclusive, but within the same patch I can see
map_end which is exclusive.

Quoted 6 lines

For this case, my suggestion would be to remove ADDRESS_SPACE_END and
just hardcode the value where you want to put Xen. For others, you
could
use (start, size) to describe a region.

Quoted 8 lines

Thanks for the suggestion. I'll take it into account.

Quoted 4 lines

Also, are you sure that all the CPU will be able to support more full
64-bit VA space?

I am not sure but based on Linux it looks like it is true. ( at least,
they use the same definitions for RV64 ).

Hmm, the spec has this text in a rationalizing remark: "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."

Quoted 8 lines

Apart from that all descriptions look to assume that the top VA bits are simply ignored for the purpose of translating to PA, which would mean that any address within the 64-bit address space can be mapped, and the same mapping would appear many times in VA space. I wonder whether that's really how it's meant to be, or whether I'm overlooking
something, or whether the spec would want clarifying.

You are right the top VA bits are ignored but my understanding is that you can't map any address within 64-bit address space as the top bits are ignored. Thereby address 0xFFFFFFXXXXXXXXXX are equal to 0x000000XXXXXXXXXX from HW point of view and the top VA bits are used only by developer.

Quoted 3 lines
Quoted 1 line

I am curious how that can be checked.

Try it out somewhere?

I re-read the initial question and also your answer and it looks like I misunderstood a little bit the initial question.

So if the question was about if all CPUs support all MMU addressing modes ( Sv32, Sv39, Sv48, Sv57 ) then an answer is no. There is a rule that if ,for example, CPU supports Sv57 then it must support lower modes.

If I understood the question correctly then we can add function which will write/read SATP_MODE:
csr_write(CSR_SATP,
((unsigned long)stage1_pgtbl_root >> PAGE_SHIFT) |

RV_STAGE1_MODE << SATP_MODE_SHIFT);	__sfence_vma_all();
	csr_write(CSR_SATP, satp);
	if ((csr_read(CSR_SATP) >> SATP_MODE_SHIFT) == SATP_MODE_SV57)
	{
		max_supported_mode = SATP_MODE_SV57;
		return
	}
	... check other modes ...

~ Oleksii

author
#9 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

In reply to Jan Beulich
Quoted 24 lines · Jan Beulich
Quoted 18 lines · Oleksii
Quoted 17 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/mm.c new file +1 -0
@@ -0,0 +1,277 @@
+#include <xen/init.h>

xen/arch/riscv/mm.c line 11

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+#include <xen/kernel.h>
+
+#include <asm/early_printk.h>
+#include <asm/config.h>
+#include <asm/csr.h>
+#include <asm/mm.h>
+#include <asm/page.h>
+#include <asm/processor.h>
+
+#define PGTBL_INITIAL_COUNT 8

What does this magic number derive from? (A comment may help.)

Quoted 4 lines

It can be now 4 tables as it is enough to map 2Mb. 8 it was when I experimented with bigger sizes of Xen binary and verified that logic of
working with page tables works.

Quoted 4 lines

Since this is connected to Xen's image size as you say, I guess the constant wants to move to a header, and then be used in an assertion in xen.lds.S. That way one will become easily aware if/when this 2Mb assumption breaks.

then we can not be tied to 2MB but to:
ASSERT(_end - _start <= MB((PAGE_TABLE_INIT_COUNT - 2) * PAGE_SIZE), "Xen too large for early-boot assumptions")

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

xen/arch/riscv/mm.c line 16

Quoted 4 lines
xen/arch/riscv/mm.c new file +3 -0
+struct mmu_desc {
+    unsigned long num_levels;
+    uint32_t pgtbl_count;

Nit: Style (as before please avoid fixed width types when their use
isn't really warranted; afaics unsigned int will do fine here and elsewhere below).

Quoted 6 lines
Quoted 3 lines

I will change it but I am not sure that I fully understand what is an
issue with uint32_t.

The issue is simply that ./CODING_STYLE says to prefer basic types whenever possible.

Thanks.

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 28 lines
xen/arch/riscv/mm.c new file +18 -0
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, 0 };
+
+    /*
+     * Access to _{stard, end } is always PC-relative
+     * 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
+     */
+    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();
+    }

xen/arch/riscv/mm.c new file +3 -0
+
+    calc_pgtbl_lvls_num(&mmu_desc);
+

xen/arch/riscv/mm.c line 233

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+    mmu_desc.pgtbl_base = stage1_pgtbl_root;
+    mmu_desc.next_pgtbl = stage1_pgtbl_nonroot;
+
+    setup_initial_mapping(&mmu_desc,
+                          linker_start,
+                          linker_end,
+                          load_start,
+                          PTE_LEAF_DEFAULT);
+
+    setup_ptes_permission(&mmu_desc);

...: Why does this require a 2nd pass / function in the first place?

Quoted 7 lines
Quoted 4 lines

Probably I misunderstood Julien and it setup_pte_permission can be done
in setup_initial_mapping() but here is the reply:
https://lore.kernel.org/xen-devel/[email protected]/

Hmm, yes, his option 2 looks like what you've implemented. Still I don't see why the permissions can't be got right on the first pass.

~ Oleksii

author
#10 lore raw

Re: [PATCH v3 1/3] xen/riscv: introduce setup_initial_pages

In reply to Jan Beulich

Hi Julien,

Quoted 40 lines · Jan Beulich
Quoted 40 lines
Quoted 40 lines
Quoted 27 lines
xen/arch/riscv/mm.c new file +18 -0
+void __init setup_initial_pagetables(void)
+{
+    struct mmu_desc mmu_desc = { 0, 0, NULL, 0 };
+
+    /*
+     * Access to _{stard, end } is always PC-relative
+     * 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
+     */
+    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();
+    }

xen/arch/riscv/mm.c new file +3 -0
+
+    calc_pgtbl_lvls_num(&mmu_desc);
+

xen/arch/riscv/mm.c line 233

Quoted 10 lines
xen/arch/riscv/mm.c new file +10 -0
+    mmu_desc.pgtbl_base = stage1_pgtbl_root;
+    mmu_desc.next_pgtbl = stage1_pgtbl_nonroot;
+
+    setup_initial_mapping(&mmu_desc,
+                          linker_start,
+                          linker_end,
+                          load_start,
+                          PTE_LEAF_DEFAULT);
+
+    setup_ptes_permission(&mmu_desc);

...: Why does this require a 2nd pass / function in the first place?

Quoted 7 lines
Quoted 4 lines

Probably I misunderstood Julien and it setup_pte_permission can be done
in setup_initial_mapping() but here is the reply:
https://lore.kernel.org/xen-devel/[email protected]/

Hmm, yes, his option 2 looks like what you've implemented. Still I don't see why the permissions can't be got right on the first pass.

I would like to ask you again about separation of mapping Xen and setting permission for specific sections.

I am not using setup_initial_mapping to change permission flags as it was before so do we still need to do two passes? Can't we set permission during the first pass?

It looks like there is a sense in merging setup_initial_mapping() and setup_ptes_permission().

Probably I misunderstood your opinion from the link above.

~ Oleksii