enable MMU for RISC-V
cover enable MMU for RISC-V
The patch series introduces the following things:
1. Functionality to build the page tables for Xen that map the following:
* The physical location of Xen (where the bootloader loaded it)
* The link-time location of Xen (where the linker expected Xen's addresses to be.
2. Load the built page table into the SATP
3. Enables MMU.
4. Updates smoke test to grep message which should be printed after MMU is enabled.
Oleksii Kurochko (3):
xen/riscv: introduce setup_initial_pages
xen/riscv: setup initial pagetables
automation: update RISC-V smoke test
automation/scripts/qemu-smoke-riscv64.sh | 2 +- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/mm.h | 9 + xen/arch/riscv/include/asm/page.h | 90 +++++++++ xen/arch/riscv/mm.c | 223 +++++++++++++++++++++++ xen/arch/riscv/setup.c | 11 ++ 6 files changed, 335 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
Signature · 1 line
2.39.0
PATCH 1/3 xen/riscv: introduce setup_initial_pages
Mostly the code for setup_initial_pages was taken from Bobby's repo except for the following changes:
* Use only a minimal part of the code enough to enable MMU
* rename {_}setup_initial_pagetables functions
* add writable argument for _setup_initial_pagetables to have an opportunity to make some sections read-only
* update setup_initial_pagetables function to make some sections read-only
* change the order of _setup_inital_pagetables()
in setup_initial_pagetable():
* first it is called for text, init, rodata sections
* after call it for ranges [link_addr_start : link_addr_end] and [load_addr_start : load_addr_end]
Before it was done first for the ranges and after for sections but in that case read-only status will be equal to 'true' and as sections' addresses can/are inside the ranges the read-only status won't be updated for them as it was set up before.
Origin: https://gitlab.com/xen-on-risc-v/xen/-/tree/riscv-rebase 4af165b468af
--- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/mm.h | 9 ++ xen/arch/riscv/include/asm/page.h | 90 ++++++++++++ xen/arch/riscv/mm.c | 223 ++++++++++++++++++++++++++++++ 4 files changed, 323 insertions(+) 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
@@ -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/mm.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef _ASM_RISCV_MM_H +#define _ASM_RISCV_MM_H + +void setup_initial_pagetables (unsigned long load_addr_start, +unsigned long load_addr_end, +unsigned long linker_addr_start, +unsigned long linker_addr_end); + +#endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/include/asm/page.h new file +90 -0
90 lines not on this page · raw message
xen/arch/riscv/mm.c new file +223 -0
223 lines not on this page · raw message
Signature · 1 line
2.39.0
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Quoted 19 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h new file +13
-0@@ -0,0 +1,90 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#define PAGE_ENTRIES 512 +#define VPN_BITS (9) +#define VPN_MASK ((unsigned long)((1 << VPN_BITS) - 1)) + +#ifdef CONFIG_RISCV_64 +/* L3 index Bit[47:39] */ +#define THIRD_SHIFT (39)
xen/arch/riscv/include/asm/page.h line 23
Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10
-0+#define THIRD_MASK (VPN_MASK << THIRD_SHIFT) +/* L2 index Bit[38:30] */ +#define SECOND_SHIFT (30) +#define SECOND_MASK (VPN_MASK << SECOND_SHIFT) +/* L1 index Bit[29:21] */ +#define FIRST_SHIFT (21) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) +/* L0 index Bit[20:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT)
Don't name these with words. That's an error ultimately inherited from an architectural mistake ARM.
These should be named L1 (4k) thru L4 (512T), and don't need separate separate masks or shifts because it looks like RISC-V designed their pagetables in a coherent and uniform way.
You'll find the code simplifies substantially if you have PAGETABLE_ORDER 9 somewhere in here.
The shift is always (PAGE_ORDER + level * PAGETABLE_ORDER), and it's rare that you need something other than "(addr >> shift) & mask". About the only time you need a virtual address masked but unshifted is for debugging.
~Andrew
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
In reply to Oleksii KurochkoHi Oleksii,
Quoted 62 lines · Oleksii Kurochko
Mostly the code for
setup_initial_pageswas taken from Bobby's repo except for the following changes:
* Use only a minimal part of the code enough to enable MMU
* rename {_}setup_initial_pagetables functions
* add writable argument for_setup_initial_pagetablesto have an opportunity to make some sections read-only
* updatesetup_initial_pagetablesfunction to make some sections read-only
* change the order of_setup_inital_pagetables()
insetup_initial_pagetable():
* first it is called for text, init, rodata sections
* after call it for ranges [link_addr_start:link_addr_end] and [load_addr_start:load_addr_end]
Before it was done first for the ranges and after for sections but in that case read-only status will be equal to 'true' and as sections' addresses can/are inside the ranges the read-only status won't be updated for them as it was set up before.Origin: https://gitlab.com/xen-on-risc-v/xen/-/tree/riscv-rebase
4af165b468afSigned-off-by Oleksii Kurochko <[email protected]>--- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/mm.h | 9 ++ xen/arch/riscv/include/asm/page.h | 90 ++++++++++++ xen/arch/riscv/mm.c | 223 ++++++++++++++++++++++++++++++ 4 files changed, 323 insertions(+)create mode 100644
xen/arch/riscv/include/asm/mm.hcreate mode 100644xen/arch/riscv/include/asm/page.hcreate mode 100644xen/arch/riscv/mm.cxen/arch/riscv/Makefile +1
-0@@ -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.oxen/arch/riscv/include/asm/mm.h new file +9
-0@@ -0,0 +1,9 @@ +#ifndef _ASM_RISCV_MM_H +#define _ASM_RISCV_MM_H + +void setup_initial_pagetables (unsigned long load_addr_start, +unsigned long load_addr_end, +unsigned long linker_addr_start, +unsigned long linker_addr_end); + +#endif /* _ASM_RISCV_MM_H */ xen/arch/riscv/include/asm/page.h new file +0
-0
xen/arch/riscv/include/asm/page.h line 7
Quoted 7 lines
xen/arch/riscv/include/asm/page.h new file +7
-0@@ -0,0 +1,90 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#define PAGE_ENTRIES 512
NIT: AFAIU, the number here is based on ...
xen/arch/riscv/include/asm/page.h line 8
Quoted 1 line
xen/arch/riscv/include/asm/page.h new file +1
-0+#define VPN_BITS (9)
... this. So I would suggest to define PAGE_ENTRIES using VPN_BITS.
xen/arch/riscv/include/asm/page.h line 9
Quoted 1 line
xen/arch/riscv/include/asm/page.h new file +1
-0+#define VPN_MASK ((unsigned long)((1 << VPN_BITS) - 1))
NIT: Use 1UL and you can avoid the cast.
Quoted 4 lines
xen/arch/riscv/include/asm/page.h new file +4
-0+ +#ifdef CONFIG_RISCV_64 +/* L3 index Bit[47:39] */ +#define THIRD_SHIFT (39)
xen/arch/riscv/include/asm/page.h line 23
Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10
-0+#define THIRD_MASK (VPN_MASK << THIRD_SHIFT) +/* L2 index Bit[38:30] */ +#define SECOND_SHIFT (30) +#define SECOND_MASK (VPN_MASK << SECOND_SHIFT) +/* L1 index Bit[29:21] */ +#define FIRST_SHIFT (21) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) +/* L0 index Bit[20:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT)
On Arm, we are trying to phase out ZEROETH_* and co because the name is too generic. Instead, we now introduce a generic macro that take a level and then compute the mask/shift (see XEN_PT_LEVEL_*).
You should be able to do in RISC-V and reduce the amount of defines introduced.
xen/arch/riscv/include/asm/page.h line 25
Quoted 2 lines
xen/arch/riscv/include/asm/page.h new file +2
-0+ +#else // CONFIG_RISCV_32
Coding style: comments in Xen are using /* ... */
Quoted 22 lines
xen/arch/riscv/include/asm/page.h new file +22
-0+ +/* L1 index Bit[31:22] */ +#define FIRST_SHIFT (22) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) + +/* L0 index Bit[21:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT) +#endif + +#define THIRD_SIZE (1 << THIRD_SHIFT) +#define THIRD_MAP_MASK (~(THIRD_SIZE - 1)) +#define SECOND_SIZE (1 << SECOND_SHIFT) +#define SECOND_MAP_MASK (~(SECOND_SIZE - 1)) +#define FIRST_SIZE (1 << FIRST_SHIFT) +#define FIRST_MAP_MASK (~(FIRST_SIZE - 1)) +#define ZEROETH_SIZE (1 << ZEROETH_SHIFT) +#define ZEROETH_MAP_MASK (~(ZEROETH_SIZE - 1)) + +#define PTE_SHIFT 10 + +#define PTE_VALID BIT(0, UL)
xen/arch/riscv/include/asm/page.h line 57
Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10
-0+#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 | PTE_EXECUTABLE)
We should avoid vulnerable default flags. So this should either be RW or RX.
Quoted 55 lines
xen/arch/riscv/include/asm/page.h new file +33
-0+#define PTE_TABLE (PTE_VALID) + +/* Calculate the offsets into the pagetables for a given VA */ +#define zeroeth_linear_offset (va) ((va) >> ZEROETH_SHIFT) +#define first_linear_offset (va) ((va) >> FIRST_SHIFT) +#define second_linear_offset (va) ((va) >> SECOND_SHIFT) +#define third_linear_offset (va) ((va) >> THIRD_SHIFT) + +#define pagetable_zeroeth_index (va) zeroeth_linear_offset((va) & ZEROETH_MASK) +#define pagetable_first_index (va) first_linear_offset((va) & FIRST_MASK) +#define pagetable_second_index (va) second_linear_offset((va) & SECOND_MASK) +#define pagetable_third_index (va) third_linear_offset((va) & THIRD_MASK) + +/* Page Table entry */ +typedef struct { +uint64_t pte; +}pte_t ; + +/* 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_SHIFT) + +static inline pte_t paddr_to_pte (unsigned long paddr) +{ +return (pte_t ) { .pte =addr_to_ppn (paddr) }; +} + +static inline bool pte_is_valid (pte_t *p) +{ +return p->pte &PTE_VALID ; +} + +#endif /* _ASM_RISCV_PAGE_H */ xen/arch/riscv/mm.c new file +16
-0@@ -0,0 +1,223 @@ +#include <xen/init.h> +#include <xen/lib.h> + +#include <asm/csr.h> +#include <asm/mm.h> +#include <asm/page.h> + +/* +* xen_second_pagetable is indexed with the VPN[2] page table entry field +* xen_first_pagetable is accessed from the VPN[1] page table entry field +* xen_zeroeth_pagetable is accessed from the VPN[0] page table entry field +*/ +pte_t xen_second_pagetable[PAGE_ENTRIES] __attribute__((__aligned__(PAGE_SIZE))); +static pte_t xen_first_pagetable[PAGE_ENTRIES] + __attribute__((__aligned__(PAGE_SIZE))); +static pte_t xen_zeroeth_pagetable[PAGE_ENTRIES ]
xen/arch/riscv/mm.c line 26
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ __attribute__((__aligned__(PAGE_SIZE))); + +extern unsigned long _stext; +extern unsigned long _etext; +extern unsigned long __init_begin; +extern unsigned long __init_end; +extern unsigned long _srodata; +extern unsigned long _erodata; + +paddr_t phys_offset;
This is defined, set but not used.
xen/arch/riscv/mm.c line 28
Quoted 2 lines
xen/arch/riscv/mm.c new file +2
-0+ +#define resolve_early_addr (x) \
This helper seems to behave the same wasy as linker_addr(). So any reason to not use it?
I will make this assumption this can be used and not comment on the implement of resolve_early_addr().
Quoted 12 lines
xen/arch/riscv/mm.c new file +12
-0+ ({ \ + unsigned long * __##x; \ + if ( load_addr_start <= x && x < load_addr_end ) \ + __##x = (unsigned long *)x; \ + else \ + __##x = (unsigned long *)(x + load_addr_start - linker_addr_start); \ + __##x; \ + }) + +static void __init clear_pagetables(unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start,
xen/arch/riscv/mm.c line 50
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ unsigned long linker_addr_end) +{ +unsigned long *p; +unsigned long page; +unsigned long i; + + page = (unsigned long )&xen_second_pagetable[0 ]; + + p =resolve_early_addr (page); +for ( i =0 ; i <ARRAY_SIZE (xen_second_pagetable); i++ )
The entries in xen_second_pagetable are a pte_t (uint64_t). But ...
xen/arch/riscv/mm.c line 52
Quoted 2 lines
xen/arch/riscv/mm.c new file +2
-0+ { + p[i] =0ULL ;
... the type here will be unsigned long. So you may not fully zero the page-table on 32-bit architecture. Therefore you want to define as pte_t.
That said, given the page table will be part of BSS, you should not need to zero again assuming you clear BSS before hand.
If you clear afterwards, then you *must* move them out of BSS.
The same applies for xen_{first, zeroeth}_pagetable below.
Quoted 11 lines
xen/arch/riscv/mm.c new file +11
-0+ } + + page = (unsigned long )&xen_first_pagetable[0 ]; + p =resolve_early_addr (page); +for ( i =0 ; i <ARRAY_SIZE (xen_first_pagetable); i++ ) + { + p[i] =0ULL ; + } + + page = (unsigned long )&xen_zeroeth_pagetable[0 ]; + p =resolve_early_addr (page);
xen/arch/riscv/mm.c line 73
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+for ( i =0 ; i <ARRAY_SIZE (xen_zeroeth_pagetable); i++ ) + { + p[i] =0ULL ; + } +} + +/* +* WARNING: load_addr() and linker_addr() are to be called only when the MMU is +* disabled and only when executed by the primary CPU. They cannot refer to +* any global variable or functions.
I find interesting you are saying when _setup_initial_pagetables() is called from setup_initial_pagetables(). Would you be able to explain how this is different?
xen/arch/riscv/mm.c line 77
Quoted 4 lines
xen/arch/riscv/mm.c new file +4
-0+*/ + +/* +* Convert an addressed layed out at link time to the address where it was loaded
Typo: s/addressed/address/ ?
xen/arch/riscv/mm.c line 79
Quoted 2 lines
xen/arch/riscv/mm.c new file +2
-0+* by the bootloader. +*/
Looking at the implementation, you seem to consider that any address not in the range [linker_addr_start, linker_addr_end[ will have a 1:1 mappings.
I am not sure this is what you want. So I would consider to throw an error if such address is passed.
Quoted 5 lines
xen/arch/riscv/mm.c new file +5
-0+#define load_addr (linker_address) \ + ({ \ + unsigned long __linker_address = (unsigned long)(linker_address); \ + if ( linker_addr_start <= __linker_address && \ + __linker_address < linker_addr_end ) \
xen/arch/riscv/mm.c line 94
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ { \ + __linker_address = \ + __linker_address - linker_addr_start + load_addr_start; \ + } \ + __linker_address; \ + }) + +/* Convert boot-time Xen address from where it was loaded by the boot loader to the address it was layed out +* at link-time. +*/
Coding style: The first line is too long and multi-line comments look like:
/*
* Foo
* Bar
*/
xen/arch/riscv/mm.c line 95
Quoted 1 line
xen/arch/riscv/mm.c new file +1
-0+#define linker_addr (load_address) \
Same remark as for load_addr() above.
xen/arch/riscv/mm.c line 108
Quoted 13 lines
xen/arch/riscv/mm.c new file +13
-0+ ({ \ +unsigned long __load_address = (unsigned long )(load_address); \ +if ( load_addr_start <= __load_address && \ + __load_address < load_addr_end ) \ + { \ + __load_address = \ + __load_address - load_addr_start + linker_addr_start; \ + } \ + __load_address; \ + }) + +static void __attribute__((section(".entry"))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t *zeroeth,
Can this be named to setup_initial_mapping() so this is clearer and avoid the one '_' different with the function below.
xen/arch/riscv/mm.c line 112
Quoted 4 lines
xen/arch/riscv/mm.c new file +4
-0+unsigned long map_start, + unsigned long map_end, + unsigned long pa_start, + bool writable)
What about the executable bit?
xen/arch/riscv/mm.c line 117
Quoted 5 lines
xen/arch/riscv/mm.c new file +5
-0+{ +unsigned long page_addr; +unsigned long index2; +unsigned long index1; +unsigned long index0;
index* could be defined in the loop below.
xen/arch/riscv/mm.c line 121
Quoted 4 lines
xen/arch/riscv/mm.c new file +4
-0+ +/* align start addresses */ + map_start &=ZEROETH_MAP_MASK ; + pa_start &=ZEROETH_MAP_MASK ;
Hmmm... I would actually expect the address to be properly aligned and therefore not require an alignment here.
Otherwise, this raise the question of what happen if you have region using the same page?
xen/arch/riscv/mm.c line 124
Quoted 3 lines
xen/arch/riscv/mm.c new file +3
-0+ + page_addr = map_start; +while ( page_addr < map_end )
Looking at the loop, it looks like you are assuming that the region will never cross a boundary of a page-table (either L0, L1, L2). I am not convinced you can make such assumption (see below).
But if you really want to make such assumption then you should add some guard (either BUILD_BUG_ON(), ASSERT(), proper check) in your code to avoid any surprise in the future.
Quoted 5 lines
xen/arch/riscv/mm.c new file +5
-0+ { + index2 =pagetable_second_index (page_addr); + index1 =pagetable_first_index (page_addr); + index0 =pagetable_zeroeth_index (page_addr); +
xen/arch/riscv/mm.c line 139
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+/* Setup level2 table */ + second[index2] =paddr_to_pte ((unsigned long )first); + second[index2].pte |=PTE_TABLE ; + +/* Setup level1 table */ + first[index1] =paddr_to_pte ((unsigned long )zeroeth); + first[index1].pte |=PTE_TABLE ; + +/* Setup level0 table */ +if ( !pte_is_valid (&zeroeth[index0]) )
Can you explain why you are checking !pte_is_valid() for the L0 entry but not the other?
xen/arch/riscv/mm.c line 144
Quoted 5 lines
xen/arch/riscv/mm.c new file +5
-0+ { +/* Update level0 table */ + zeroeth[index0] =paddr_to_pte ((page_addr - map_start) + pa_start); + zeroeth[index0].pte |=PTE_LEAF_DEFAULT ; + zeroeth[index0].pte &= ~((!writable) ?PTE_WRITABLE :0 );
Looking at the default value, it would mean that a non-writable mapping is automatically executable. This seems wrong for the section is not meant to be executable (like rodata).
Quoted 7 lines
xen/arch/riscv/mm.c new file +7
-0+ } + +/* Point to next page */ + page_addr +=ZEROETH_SIZE ; + } +} +
xen/arch/riscv/mm.c line 161
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+/* +* setup_initial_pagetables: +* +* 1) Build the page tables for Xen that map the following: +* 1.1) The physical location of Xen (where the bootloader loaded it) +* 1.2) The link-time location of Xen (where the linker expected Xen's +* addresses to be) +* 2) Load the page table into the SATP and enable the MMU +*/ +void __attribute__((section(".entry")))
I couldn't find a section ".entry" in the linker.
Quoted 6 lines
xen/arch/riscv/mm.c new file +6
-0+setup_initial_pagetables(unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start, + unsigned long linker_addr_end) +{ +pte_t *second;
xen/arch/riscv/mm.c line 177
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+pte_t *first; +pte_t *zeroeth; + +clear_pagetables (load_addr_start, load_addr_end, + linker_addr_start, linker_addr_end); + +/* Get the addresses where the page tables were loaded */ + second = (pte_t *)load_addr (&xen_second_pagetable); + first = (pte_t *)load_addr (&xen_first_pagetable); + zeroeth = (pte_t *)load_addr (&xen_zeroeth_pagetable);
I would consider to embed the type cast in load_addr() so you are adding some type safety within your code.
xen/arch/riscv/mm.c line 180
Quoted 3 lines
xen/arch/riscv/mm.c new file +3
-0+ +/* +* Create a mapping from Xen's link-time addresses to where they were actually loaded.
This is line is way long than 80 characters. Please make sure to wrap it 80 characters.
Quoted 11 lines
xen/arch/riscv/mm.c new file +11
-0+*/ +_setup_initial_pagetables (second, first, zeroeth, +linker_addr (&_stext), +linker_addr (&_etext), +load_addr (&_stext), + false); +_setup_initial_pagetables (second, first, zeroeth, +linker_addr (&__init_begin), +linker_addr (&__init_end), +load_addr (&__init_begin), + true);
xen/arch/riscv/mm.c line 201
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+_setup_initial_pagetables (second, first, zeroeth, +linker_addr (&_srodata), +linker_addr (&_erodata), +load_addr (&_srodata), + false); +_setup_initial_pagetables (second, first, zeroeth, + linker_addr_start, + linker_addr_end, + load_addr_start, + true);
Where do you guarantee that Xen will always fit in an L0 table and the start address is aligned to the size of an L0 table?
xen/arch/riscv/mm.c line 204
Quoted 3 lines
xen/arch/riscv/mm.c new file +3
-0+ +/* +* Create a mapping of the load time address range to... the load time address range.
Same about the line length here.
xen/arch/riscv/mm.c line 207
Quoted 3 lines
xen/arch/riscv/mm.c new file +3
-0+* This mapping is used at boot time only. +*/ + _setup_initial_pagetables(second, first, zeroeth,
This can only work if Xen is loaded at its linked address. So you need a separate set of L0, L1 tables for the identity mapping.
That said, this would not be sufficient because:
1) Xen may not be loaded at a 2M boundary (you can control with
U-boot, but not with EFI). So this may cross a boundary and therefore need multiple pages.
2) The load region may overlap the link address
While I think it would be good to handle those cases from the start, I would understand why are not easy to solve. So I think the minimum is to throw some errors if you are in a case you can't support.
xen/arch/riscv/mm.c line 218
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ load_addr_start, + load_addr_end, + load_addr_start, + true); > + +/* Ensure page table writes precede loading the SATP */ + asmvolatile ("sfence.vma" ); + +/* Enable the MMU and load the new pagetable for Xen */ +csr_write (CSR_SATP , + (load_addr (xen_second_pagetable) >>PAGE_SHIFT ) |SATP_MODE_SV39 <<SATP_MODE_SHIFT );
IHMO, it would make sense to introduce within the series the code to jump off the identity mapping and then remove it.
xen/arch/riscv/mm.c line 223
Quoted 5 lines
xen/arch/riscv/mm.c new file +5
-0+ + phys_offset = load_addr_start - linker_addr_start; + +return ; +}
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Quoted 82 lines · Julien Grall
Hi Oleksii,
Quoted 69 lines · Oleksii Kurochko
Mostly the code for
setup_initial_pageswas taken from Bobby's repo except for the following changes:
* Use only a minimal part of the code enough to enable MMU
* rename {_}setup_initial_pagetables functions
* add writable argument for_setup_initial_pagetablesto have an opportunity to make some sections read-only
* updatesetup_initial_pagetablesfunction to make some sections read-only
* change the order of_setup_inital_pagetables()
insetup_initial_pagetable():
* first it is called for text, init, rodata sections
* after call it for ranges [link_addr_start:link_addr_end] and [load_addr_start:load_addr_end]
Before it was done first for the ranges and after for sections but
in that case read-only status will be equal to 'true' and as sections' addresses can/are inside the ranges the read-only status
won't be updated for them as it was set up before.Origin:
https://gitlab.com/xen-on-risc-v/xen/-/tree/riscv-rebase 4af165b468
afSigned-off-by Oleksii Kurochko <[email protected]>---
xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/mm.h | 9 ++xen/arch/riscv/include/asm/page.h| 90 ++++++++++++ xen/arch/riscv/mm.c | 223
++++++++++++++++++++++++++++++4 files changed, 323 insertions(+)create mode 100644
xen/arch/riscv/include/asm/mm.hcreate mode 100644xen/arch/riscv/include/asm/page.hcreate mode 100644xen/arch/riscv/mm.cxen/arch/riscv/Makefile +0
-0@@ -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.oxen/arch/riscv/include/asm/mm.h new file +9
-0@@ -0,0 +1,9 @@ +#ifndef _ASM_RISCV_MM_H +#define _ASM_RISCV_MM_H + +void setup_initial_pagetables (unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start, + unsigned long linker_addr_end); + +#endif /* _ASM_RISCV_MM_H */ xen/arch/riscv/include/asm/page.h new file +0
-0xen/arch/riscv/include/asm/page.h line 7
Quoted 7 lines
xen/arch/riscv/include/asm/page.h new file +7
-0@@ -0,0 +1,90 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#define PAGE_ENTRIES 512NIT: AFAIU, the number here is based on ...
Quoted 3 lines
xen/arch/riscv/include/asm/page.h line 8
Quoted 1 line
xen/arch/riscv/include/asm/page.h new file +1
-0+#define VPN_BITS (9)... this. So I would suggest to define
PAGE_ENTRIESusingVPN_BITS.
Sure. It should be defined using VPN_BITS. Thanks.
Quoted 4 lines
Quoted 3 lines
xen/arch/riscv/include/asm/page.h new file +1
-0+#define VPN_MASK ((unsigned long)((1 << VPN_BITS) -1))
NIT: Use 1UL and you can avoid the cast.
Thanks. I'll update that in the next version of patch series.
Quoted 22 lines
Quoted 5 lines
xen/arch/riscv/include/asm/page.h new file +4
-0+ +#ifdef CONFIG_RISCV_64 +/* L3 index Bit[47:39] */ +#define THIRD_SHIFT (39)xen/arch/riscv/include/asm/page.h line 23
Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10
-0+#define THIRD_MASK (VPN_MASK << THIRD_SHIFT) +/* L2 index Bit[38:30] */ +#define SECOND_SHIFT (30) +#define SECOND_MASK (VPN_MASK << SECOND_SHIFT) +/* L1 index Bit[29:21] */ +#define FIRST_SHIFT (21) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) +/* L0 index Bit[20:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT)On Arm, we are trying to phase out ZEROETH_* and co because the name is
too generic. Instead, we now introduce a generic macro that take a level
and then compute the mask/shift (see XEN_PT_LEVEL_*).
Quoted 2 lines
You should be able to do in RISC-V and reduce the amount of defines introduced.
Thanks. I'll look at XEN_PT_LEVEL_*. I'll re-read Andrew's comment but as far as I understand after quick reading we can remove mostly that.
Quoted 34 lines
xen/arch/riscv/include/asm/page.h
Quoted 3 lines
xen/arch/riscv/include/asm/page.h new file +2
-0+ +#else // CONFIG_RISCV_32 Coding style: comments in Xen are using /* ... */
Quoted 28 lines
xen/arch/riscv/include/asm/page.h new file +28
-0+ +/* L1 index Bit[31:22] */ +#define FIRST_SHIFT (22) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) + +/* L0 index Bit[21:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT) +#endif + +#define THIRD_SIZE (1 << THIRD_SHIFT) +#define THIRD_MAP_MASK (~(THIRD_SIZE - 1)) +#define SECOND_SIZE (1 << SECOND_SHIFT) +#define SECOND_MAP_MASK (~(SECOND_SIZE - 1)) +#define FIRST_SIZE (1 << FIRST_SHIFT) +#define FIRST_MAP_MASK (~(FIRST_SIZE - 1)) +#define ZEROETH_SIZE (1 << ZEROETH_SHIFT) +#define ZEROETH_MAP_MASK (~(ZEROETH_SIZE - 1)) + +#define PTE_SHIFT 10 + +#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)
Quoted 8 lines
Quoted 5 lines
xen/arch/riscv/include/asm/page.h new file +4
-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|PTE_EXECUTABLE)We should avoid vulnerable default flags. So this should either be RW or RX.
Thanks. I'll take it into account.
Quoted 78 lines
Quoted 65 lines
xen/arch/riscv/include/asm/page.h new file +9
-0+#define PTE_TABLE (PTE_VALID) + +/* Calculate the offsets into the pagetables for a given VA */ +#define zeroeth_linear_offset (va) ((va) >> ZEROETH_SHIFT) +#define first_linear_offset (va) ((va) >> FIRST_SHIFT) +#define second_linear_offset (va) ((va) >> SECOND_SHIFT) +#define third_linear_offset (va) ((va) >> THIRD_SHIFT) + +#define pagetable_zeroeth_index (va) zeroeth_linear_offset((va) &
ZEROETH_MASK)
+#define pagetable_first_index(va)first_linear_offset((va) & FIRST_MASK)
+#define pagetable_second_index(va)second_linear_offset((va) & SECOND_MASK)
+#define pagetable_third_index(va)third_linear_offset((va) & THIRD_MASK)xen/arch/riscv/include/asm/page.h new file +7
-0+ +/* Page Table entry */ +typedef struct { + uint64_t pte; +}pte_t ; + +/* 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 */xen/arch/riscv/include/asm/page.h new file +13
-0+#define addr_to_ppn (x) (((x) >> PAGE_SHIFT) << PTE_SHIFT) + +static inline pte_t paddr_to_pte (unsigned long paddr) +{ + return (pte_t) { .pte = addr_to_ppn(paddr) }; +} + +static inline bool pte_is_valid (pte_t *p) +{ + return p->pte & PTE_VALID; +} + +#endif /* _ASM_RISCV_PAGE_H */ xen/arch/riscv/mm.c new file +9
-0@@ -0,0 +1,223 @@ +#include <xen/init.h> +#include <xen/lib.h> + +#include <asm/csr.h> +#include <asm/mm.h> +#include <asm/page.h> + +/* +* xen_second_pagetable is indexed with the VPN[2] page table entry field
+ *xen_first_pagetableis accessed from the VPN[1] page table entry field
+ *xen_zeroeth_pagetableis accessed from the VPN[0] page table entry field
+ */
+pte_t xen_second_pagetable[PAGE_ENTRIES] __attribute__((__aligned__(PAGE_SIZE)));xen/arch/riscv/mm.c new file +3
-0+static pte_t xen_first_pagetable[PAGE_ENTRIES] + __attribute__((__aligned__(PAGE_SIZE))); +static pte_t xen_zeroeth_pagetable[PAGE_ENTRIES ]xen/arch/riscv/mm.c line 26
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ __attribute__((__aligned__(PAGE_SIZE))); + +extern unsigned long _stext; +extern unsigned long _etext; +extern unsigned long __init_begin; +extern unsigned long __init_end; +extern unsigned long _srodata; +extern unsigned long _erodata; + +paddr_t phys_offset;This is defined, set but not used.
Quoted 5 lines
xen/arch/riscv/mm.c
Quoted 2 lines
xen/arch/riscv/mm.c new file +2
-0+ +#define resolve_early_addr (x) \This helper seems to behave the same wasy as
linker_addr(). So any reason to not use it?
linker_addr() script can be used instead. It looks I missed something before and it is spilled out into two equal macros.
Quoted 47 lines
I will make this assumption this can be used and not comment on the implement of
resolve_early_addr().Quoted 22 lines
+
({
\
+ unsigned long *
__##x; \
+ if (load_addr_start<= x && x <load_addr_end) \
+ __##x = (unsigned long
*)x; \
+
else
\
+ __##x = (unsigned long *)(x +load_addr_start-linker_addr_start); \
+
__##x;
\
+ })xen/arch/riscv/mm.c new file +4
-0+ +static void __init clear_pagetables(unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start,xen/arch/riscv/mm.c line 50
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ unsigned long linker_addr_end) +{ + unsigned long *p; + unsigned long page; + unsigned long i; + + page = (unsigned long)&xen_second_pagetable[0]; + + p = resolve_early_addr(page); + for ( i = 0; i < ARRAY_SIZE(xen_second_pagetable); i++ )The entries in
xen_second_pagetableare apte_t(uint64_t). But ...Quoted 2 lines
+ {
+ p[i] = 0ULL;... the type here will be unsigned long. So you may not fully zero the
page-table on 32-bit architecture. Therefore you want to define aspte_t.
Quoted 7 lines
That said, given the page table will be part of BSS, you should not need
to zero again assuming you clear BSS before hand.If you clear afterwards, then you *must* move them out of BSS.
The same applies for xen_{first, zeroeth}_pagetable below.
I didn't have initialized page tables so that is why I needed clear_pagetables() but I think you are right and clear_pagetables can be removed at all as page tables will be initialized during BSS initialization.
Quoted 21 lines
Quoted 21 lines
+ }
xen/arch/riscv/mm.c new file +18
-0+ + page = (unsigned long)&xen_first_pagetable[0]; + p = resolve_early_addr(page); + for ( i = 0; i < ARRAY_SIZE(xen_first_pagetable); i++ ) + { + p[i] = 0ULL; + } + + page = (unsigned long)&xen_zeroeth_pagetable[0]; + p = resolve_early_addr(page); + for ( i = 0; i < ARRAY_SIZE(xen_zeroeth_pagetable); i++ ) + { + p[i] = 0ULL; + } +} + +/* +* WARNING: load_addr() and linker_addr() are to be called only when the MMU is
Quoted 8 lines
Quoted 3 lines
+ * disabled and only when executed by the primary CPU. They cannot refer to
+ * any global variable or functions.I find interesting you are saying when
_setup_initial_pagetables()is called fromsetup_initial_pagetables(). Would you be able to explain how
this is different?
I am not sure that I understand your question correctly but _setup_initial_pagetables() was introduced to map some addresses with write/read flag. Probably I have to rename it to something that is more clear.
Quoted 8 lines
Quoted 6 lines
+ */
xen/arch/riscv/mm.c new file +3
-0+ +/* +* Convert an addressed layed out at link time to the address where it was loaded
Typo: s/addressed/address/ ?
Yes, it should be address. and 'layed out' should be changed to 'laid out'...
Quoted 11 lines
Quoted 3 lines
+ * by the bootloader.
+ */Looking at the implementation, you seem to consider that any address not
in the range [linker_addr_start, linker_addr_end[ will have a 1:1 mappings.I am not sure this is what you want. So I would consider to throw an error if such address is passed.
I thought that at this stage and if no relocation was done it is 1:1 except the case when load_addr_start != linker_addr_start.
Quoted 68 lines
Quoted 29 lines
+#define
load_addr(linker_address)
\
+
({
\
+ unsigned long__linker_address= (unsigned long)(linker_address); \
+ if (linker_addr_start<=__linker_address&& \
+__linker_address<linker_addr_end) \
+
{
\
+__linker_address
= \
+__linker_address-linker_addr_start+ load_addr_start; \
+
}
\
+__linker_address;
\
+ })xen/arch/riscv/mm.c new file +2
-0+ +/* Convert boot-time Xen address from where it was loaded by the Quoted 3 lines
boot loader to the address it was layed out
+ * at link-time.
+ */Coding style: The first line is too long and multi-line comments look like:
/*
* Foo
* Bar
*/Quoted 3 lines
+#define
linker_addr(load_address)
\Same remark as for
load_addr()above.Quoted 21 lines
+
({
\
+ unsigned long__load_address= (unsigned long)(load_address); \
+ if (load_addr_start<=__load_address&& \
+__load_address<load_addr_end
) \
+
{
\
+__load_address
= \
+__load_address-load_addr_start+ linker_addr_start; \
+
}
\
+__load_address;
Quoted 8 lines
Quoted 6 lines
\
+ })xen/arch/riscv/mm.c new file +3
-0+ +static void __attribute__((section (".entry" ))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t*zeroeth,
Can this be named to
setup_initial_mapping()so this is clearer and avoid the one '_' different with the function below.
Sure. It will be better.
Quoted 7 lines
Quoted 5 lines
+ unsigned long
map_start, + unsigned longmap_end, + unsigned longpa_start, + bool writable)What about the executable bit?
It's always executable... But as you mentioned above PTE_LEAF_DEFAULT should be either RX or RW.
I think it makes sense to add flags instead of writable.
Quoted 8 lines
Quoted 6 lines
+{
+ unsigned longpage_addr;
+ unsigned long index2;
+ unsigned long index1;
+ unsigned long index0;index* could be defined in the loop below.
It could. But I am curious why it is better?
Quoted 6 lines
Quoted 5 lines
+
+ /* align start addresses */
+map_start&=ZEROETH_MAP_MASK;
+pa_start&=ZEROETH_MAP_MASK;
Quoted 6 lines
Hmmm... I would actually expect the address to be properly aligned and
therefore not require an alignment here.Otherwise, this raise the question of what happen if you have region using the same page?
That map_start &= ZEROETH_MAP_MASK is needed to page number of address w/o page offset.
Quoted 10 lines
Quoted 4 lines
+
+page_addr=map_start;
+ while (page_addr<map_end)Looking at the loop, it looks like you are assuming that the region will
never cross a boundary of a page-table (either L0, L1, L2). I am not convinced you can make such assumption (see below).
Quoted 4 lines
But if you really want to make such assumption then you should add some
guard (eitherBUILD_BUG_ON(),ASSERT(), proper check) in your code to avoid any surprise in the future.
I am not sure that I fully understand what is the problem here. The address is aligned on (1<<12) boundary and each itearation is mapped (1<<12) page so all looks fine or I misunderstood you.
Quoted 11 lines
Quoted 11 lines
+ {
+ index2 =pagetable_second_index(page_addr); + index1 =pagetable_first_index(page_addr); + index0 =pagetable_zeroeth_index(page_addr);xen/arch/riscv/mm.c new file +6
-0+ + /* Setup level2 table */ + second[index2] = paddr_to_pte((unsigned long)first); + second[index2].pte |= PTE_TABLE; + + /* Setup level1 table */
Quoted 8 lines
Quoted 5 lines
+ first[index1] =
paddr_to_pte((unsigned long)zeroeth); + first[index1].pte |=PTE_TABLE;
+
+ /* Setup level0 table */
+ if ( !pte_is_valid(&zeroeth[index0]) )Can you explain why you are checking !pte_is_valid() for the L0 entry but not the other?
My mistake it should be checked for each level.
Quoted 9 lines
Quoted 8 lines
+ {
+ /* Update level0 table */
+ zeroeth[index0] = paddr_to_pte((page_addr -map_start)
+pa_start);
+ zeroeth[index0].pte |=PTE_LEAF_DEFAULT;
+ zeroeth[index0].pte &= ~((!writable) ?PTE_WRITABLE:
0);
Quoted 4 lines
Looking at the default value, it would mean that a non-writable mapping
is automatically executable. This seems wrong for the section is not meant to be executable (like rodata).
Yes, you are right. I'll reowrk setup_initial_mapping() to pass flags instead of write/read - only flag.
Quoted 35 lines
Quoted 13 lines
+ }
xen/arch/riscv/mm.c new file +11
-0+ + /* Point to next page */ + page_addr += ZEROETH_SIZE; + } +} + +/* +* setup_initial_pagetables: +* +* 1) Build the page tables for Xen that map the following: +* 1.1) The physical location of Xen (where the bootloader Quoted 7 lines
loaded it)
+ * 1.2) The link-time location of Xen (where the linker expected Xen's
+ * addresses to be)
+ * 2) Load the page table into the SATP and enable the MMU
+ */
+void __attribute__((section(".entry")))I couldn't find a section ".entry" in the linker.
Quoted 12 lines
xen/arch/riscv/mm.c new file +12
-0+setup_initial_pagetables(unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start, + unsigned long linker_addr_end) +{ + pte_t *second; + pte_t *first; + pte_t *zeroeth; + + clear_pagetables(load_addr_start, load_addr_end, + linker_addr_start, linker_addr_end); +
Quoted 8 lines
Quoted 4 lines
+ /* Get the addresses where the page tables were loaded */ + second = (
pte_t*)load_addr(&xen_second_pagetable); + first = (pte_t*)load_addr(&xen_first_pagetable); + zeroeth = (pte_t*)load_addr(&xen_zeroeth_pagetable);I would consider to embed the type cast in
load_addr()so you are adding
some type safety within your code.
Definitely it will be better but setup_initial_mapping() uses 'unsigned long' for passing address that should be mapped.
Quoted 32 lines
Quoted 5 lines
+
+ /*
+ * Create a mapping from Xen's link-time addresses to where they were actually loaded.This is line is way long than 80 characters. Please make sure to wrap it
80 characters.Quoted 13 lines
+ */
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&_stext), +linker_addr(&_etext), +load_addr(&_stext), + false);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&__init_begin), +linker_addr(&__init_end), +load_addr(&__init_begin), + true);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&_srodata),Quoted 8 lines
+
linker_addr(&_erodata), +load_addr(&_srodata), + false);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr_start, +linker_addr_end, +load_addr_start, + true);
Quoted 3 lines
Where do you guarantee that Xen will always fit in an L0 table and the
start address is aligned to the size of an L0 table?
I don't guarantee that it fit in an L0 table but the start address is aligned to the size of the L0 table at the start.
Quoted 22 lines
Quoted 5 lines
+
+ /*
+ * Create a mapping of the load time address range to... the load time address range.Same about the line length here.
Quoted 3 lines
+ * This mapping is used at boot time only. + */
+ _setup_initial_pagetables(second, first, zeroeth,This can only work if Xen is loaded at its linked address. So you need a
separate set of L0, L1 tables for the identity mapping.That said, this would not be sufficient because:
1) Xen may not be loaded at a 2M boundary (you can control with U-boot, but not with EFI). So this may cross a boundary and therefore need multiple pages.
2) The load region may overlap the link address
Quoted 5 lines
While I think it would be good to handle those cases from the start, I
would understand why are not easy to solve. So I think the minimum is to
throw some errors if you are in a case you can't support.
Do you mean to throw some error in load_addr()/linkder_addr()?
Quoted 13 lines
Quoted 12 lines
+
load_addr_start,
+load_addr_end,
+load_addr_start,
+ true); > +
+ /* Ensure page table writes precede loading the SATP */ + asmvolatile("sfence.vma");
+
+ /* Enable the MMU and load the new pagetable for Xen */ + csr_write(CSR_SATP,
+ (load_addr(xen_second_pagetable)>>PAGE_SHIFT) |SATP_MODE_SV39<<SATP_MODE_SHIFT);
Quoted 2 lines
IHMO, it would make sense to introduce within the series the code to jump off the identity mapping and then remove it.
Probably I have to spend more time to understand identity mapping but it looks like current one RISC-V port from Bobby's doesn't jump off and remove the identity mapping.
Quoted 9 lines
xen/arch/riscv/mm.c line 223
Quoted 6 lines
xen/arch/riscv/mm.c new file +5
-0+ + phys_offset = load_addr_start - linker_addr_start; + + return; +}Cheers,
~ Oleksii
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Hi Oleksii,
Quoted 8 lines · Oleksii
Quoted 7 lines · Julien Grall
Quoted 6 lines
+/*
+ * WARNING:load_addr()andlinker_addr()are to be called only when the MMU is
+ * disabled and only when executed by the primary CPU. They cannot refer to
+ * any global variable or functions.
Quoted 8 lines
Quoted 4 lines
I find interesting you are saying when
_setup_initial_pagetables()is called fromsetup_initial_pagetables(). Would you be able to explain how
this is different?I am not sure that I understand your question correctly but
_setup_initial_pagetables()was introduced to map some addresses with write/read flag. Probably I have to rename it to something that is more clear.
So the comment suggests that you code cannot refer to global functions/variables when the MMU is off. So I have multiple questions:
* Why only global? IOW, why static would be OK?
* setup_initial_pagetables() has a call to_setup_initial_pagetables() (IOW referring to another function). Why is it fine?
* You have code in the next patch referring to global variables
(mainly _start and _end). How is this different?
Quoted 19 lines
Quoted 8 lines
Quoted 6 lines
+ */
xen/arch/riscv/mm.c new file +3
-0+ +/* +* Convert an addressed layed out at link time to the address where it was loaded
Typo: s/addressed/address/ ?
Yes, it should be address. and 'layed out' should be changed to 'laid out'...
Quoted 9 lines
Quoted 3 lines
+ * by the bootloader.
+ */Looking at the implementation, you seem to consider that any address not
in the range [linker_addr_start, linker_addr_end[ will have a 1:1 mappings.
Quoted 4 lines
Quoted 2 lines
I am not sure this is what you want. So I would consider to throw an error if such address is passed.
I thought that at this stage and if no relocation was done it is 1:1 except the case when
load_addr_start!=linker_addr_start.
The problem is what you try to map one to one may clash with the linked region for Xen. So it is not always possible to map the region 1:1.
Therefore, I don't see any use for the else part here.
Quoted 104 lines
Quoted 70 lines
Quoted 31 lines
+#define
load_addr(linker_address)
\
+
({
\
+ unsigned long__linker_address= (unsigned long)(linker_address); \
+ if (linker_addr_start<=__linker_address&& \
+__linker_address<linker_addr_end) \
+
{
\
+__linker_address
= \
+__linker_address-linker_addr_start+ load_addr_start; \
+
}
\
+__linker_address;
\
+ })(quoted diff) +2
-0+ +/* Convert boot-time Xen address from where it was loaded by theQuoted 3 lines
boot loader to the address it was layed out
+ * at link-time.
+ */Coding style: The first line is too long and multi-line comments look like:
/*
* Foo
* Bar
*/Quoted 3 lines
+#define
linker_addr(load_address)
\Same remark as for
load_addr()above.Quoted 21 lines
+
({
\
+ unsigned long__load_address= (unsigned long)(load_address); \
+ if (load_addr_start<=__load_address&& \
+__load_address<load_addr_end
) \
+
{
\
+__load_address
= \
+__load_address-load_addr_start+ linker_addr_start; \
+
}
\
+__load_address;Quoted 8 lines
Quoted 6 lines
\
+ })xen/arch/riscv/mm.c new file +3
-0+ +static void __attribute__((section (".entry" ))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t*zeroeth,
Can this be named to
setup_initial_mapping()so this is clearer and avoid the one '_' different with the function below.Sure. It will be better.
Quoted 7 lines
Quoted 5 lines
+ unsigned long
map_start, + unsigned longmap_end, + unsigned longpa_start, + bool writable)What about the executable bit?
It's always executable... But as you mentioned above
PTE_LEAF_DEFAULTshould be either RX or RW.
I think it makes sense to add flags instead of writable.Quoted 8 lines
Quoted 6 lines
+{
+ unsigned longpage_addr;
+ unsigned long index2;
+ unsigned long index1;
+ unsigned long index0;index* could be defined in the loop below.
It could. But I am curious why it is better?
Quoted 6 lines
Quoted 5 lines
+
+ /* align start addresses */
+map_start&=ZEROETH_MAP_MASK;
+pa_start&=ZEROETH_MAP_MASK;
Quoted 8 lines
Quoted 6 lines
Hmmm... I would actually expect the address to be properly aligned and
therefore not require an alignment here.Otherwise, this raise the question of what happen if you have region using the same page?
That
map_start&=ZEROETH_MAP_MASKis needed to page number of address w/o page offset.
My point is why would the page offset be non-zero?
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
+
+page_addr=map_start;
+ while (page_addr<map_end)Looking at the loop, it looks like you are assuming that the region will
never cross a boundary of a page-table (either L0, L1, L2). I am not convinced you can make such assumption (see below).
Quoted 7 lines
Quoted 4 lines
But if you really want to make such assumption then you should add some
guard (eitherBUILD_BUG_ON(),ASSERT(), proper check) in your code to avoid any surprise in the future.I am not sure that I fully understand what is the problem here. The address is aligned on (1<<12) boundary and each itearation is mapped (1<<12) page so all looks fine or I misunderstood you.
Let's take an example, imagine the region you want to map is 4MB. AFAICT, you are only passing one L0 page-table. So your code will end up to overwrite the previous entries in the zeroeth page-table and then add another link in the L1 page-table.
Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
+ {
+ index2 =pagetable_second_index(page_addr); + index1 =pagetable_first_index(page_addr); + index0 =pagetable_zeroeth_index(page_addr);xen/arch/riscv/mm.c new file +6
-0+ + /* Setup level2 table */ + second[index2] = paddr_to_pte((unsigned long)first); + second[index2].pte |= PTE_TABLE; + + /* Setup level1 table */Quoted 8 lines
Quoted 5 lines
+ first[index1] =
paddr_to_pte((unsigned long)zeroeth); + first[index1].pte |=PTE_TABLE;
+
+ /* Setup level0 table */
+ if ( !pte_is_valid(&zeroeth[index0]) )Can you explain why you are checking !pte_is_valid() for the L0 entry but not the other?
Quoted 1 line
My mistake it should be checked for each level.
In which case, shouldn't you return an error if the entry is always valid?
Quoted 58 lines
Quoted 9 lines
Quoted 8 lines
+ {
+ /* Update level0 table */
+ zeroeth[index0] = paddr_to_pte((page_addr -map_start)
+pa_start);
+ zeroeth[index0].pte |=PTE_LEAF_DEFAULT;
+ zeroeth[index0].pte &= ~((!writable) ?PTE_WRITABLE:
0);Quoted 4 lines
Looking at the default value, it would mean that a non-writable mapping
is automatically executable. This seems wrong for the section is not meant to be executable (like rodata).Yes, you are right. I'll reowrk
setup_initial_mapping()to pass flags instead of write/read - only flag.Quoted 35 lines
Quoted 13 lines
+ }
xen/arch/riscv/mm.c new file +11
-0+ + /* Point to next page */ + page_addr += ZEROETH_SIZE; + } +} + +/* +* setup_initial_pagetables: +* +* 1) Build the page tables for Xen that map the following: +* 1.1) The physical location of Xen (where the bootloader Quoted 7 lines
loaded it)
+ * 1.2) The link-time location of Xen (where the linker expected Xen's
+ * addresses to be)
+ * 2) Load the page table into the SATP and enable the MMU
+ */
+void __attribute__((section(".entry")))I couldn't find a section ".entry" in the linker.
Quoted 12 lines
xen/arch/riscv/mm.c new file +12
-0+setup_initial_pagetables(unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start, + unsigned long linker_addr_end) +{ + pte_t *second; + pte_t *first; + pte_t *zeroeth; + + clear_pagetables(load_addr_start, load_addr_end, + linker_addr_start, linker_addr_end); +Quoted 8 lines
Quoted 4 lines
+ /* Get the addresses where the page tables were loaded */ + second = (
pte_t*)load_addr(&xen_second_pagetable); + first = (pte_t*)load_addr(&xen_first_pagetable); + zeroeth = (pte_t*)load_addr(&xen_zeroeth_pagetable);I would consider to embed the type cast in
load_addr()so you are adding
some type safety within your code.
Quoted 2 lines
Definitely it will be better but
setup_initial_mapping()uses 'unsigned long' for passing address that should be mapped.
One possibility would be to introduce a new wrapper for the typesafety. Anyway, it is not essential for now. Let's at least get the logic correct first :).
Quoted 32 lines
Quoted 32 lines
Quoted 5 lines
+
+ /*
+ * Create a mapping from Xen's link-time addresses to where they were actually loaded.This is line is way long than 80 characters. Please make sure to wrap it
80 characters.Quoted 21 lines
+ */
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&_stext), +linker_addr(&_etext), +load_addr(&_stext), + false);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&__init_begin), +linker_addr(&__init_end), +load_addr(&__init_begin), + true);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&_srodata), +linker_addr(&_erodata), +load_addr(&_srodata), + false);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr_start, +linker_addr_end,
+load_addr_start,
+ true);
Quoted 5 lines
Quoted 3 lines
Where do you guarantee that Xen will always fit in an L0 table and the
start address is aligned to the size of an L0 table?I don't guarantee that it fit in an L0 table but the start address is aligned to the size of the L0 table at the start.
Then it should be fixed.
Quoted 22 lines
Quoted 22 lines
Quoted 5 lines
+
+ /*
+ * Create a mapping of the load time address range to... the load time address range.Same about the line length here.
Quoted 3 lines
+ * This mapping is used at boot time only. + */
+ _setup_initial_pagetables(second, first, zeroeth,This can only work if Xen is loaded at its linked address. So you need a
separate set of L0, L1 tables for the identity mapping.That said, this would not be sufficient because:
1) Xen may not be loaded at a 2M boundary (you can control with
U-boot, but not with EFI). So this may cross a boundary and therefore need multiple pages.
2) The load region may overlap the link address
Quoted 6 lines
Quoted 5 lines
While I think it would be good to handle those cases from the start, I
would understand why are not easy to solve. So I think the minimum is to
throw some errors if you are in a case you can't support.Do you mean to throw some error in load_addr()/linkder_addr()?
In this case, I meant to check if load_addr != linker_addr, then throw an error.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Hi Julien,
Quoted 64 lines · Julien Grall
Hi Oleksii,
Quoted 15 lines · Oleksii
Quoted 8 lines · Julien Grall
Quoted 7 lines
+/*
+ * WARNING:load_addr()andlinker_addr()are to be called only
when the MMU is
+ * disabled and only when executed by the primary CPU. They cannot refer to
+ * any global variable or functions.Quoted 6 lines
I find interesting you are saying when
_setup_initial_pagetables()is
called fromsetup_initial_pagetables(). Would you be able to explain
how
this is different?Quoted 6 lines
I am not sure that I understand your question correctly but
_setup_initial_pagetables()was introduced to map some addresses with
write/read flag. Probably I have to rename it to something that is more
clear.So the comment suggests that you code cannot refer to global functions/variables when the MMU is off. So I have multiple questions:
* Why only global? IOW, why static would be OK?
*setup_initial_pagetables()has a call to_setup_initial_pagetables()(IOW referring to another function). Why is
it fine?
* You have code in the next patch referring to global variables (mainly_startand_end). How is this different?Quoted 21 lines
Quoted 8 lines
Quoted 6 lines
+ */
xen/arch/riscv/mm.c new file +3
-0+ +/* +* Convert an addressed layed out at link time to the address where it was loaded
Typo: s/addressed/address/ ?
Yes, it should be address. and 'layed out' should be changed to 'laid
out'...Quoted 10 lines
Quoted 3 lines
+ * by the bootloader.
+ */Looking at the implementation, you seem to consider that any address
not
in the range [linker_addr_start, linker_addr_end[ will have a 1:1 mappings.Quoted 6 lines
Quoted 3 lines
I am not sure this is what you want. So I would consider to throw an
error if such address is passed.I thought that at this stage and if no relocation was done it is 1:1
except the case whenload_addr_start!=linker_addr_start.
Quoted 5 lines
The problem is what you try to map one to one may clash with the linked
region for Xen. So it is not always possible to map the region 1:1.Therefore, I don't see any use for the else part here.
Got it. Thanks.
I am curious than what is the correct approach in general to handle this situation?
I mean that throw an error it is one option but if I would like to do that w/o throwing an error. Should it done some relocation in that case?
Quoted 114 lines
Quoted 114 lines
Quoted 74 lines
Quoted 32 lines
+#define
load_addr(linker_address)
\
+
({
\
+ unsigned long__linker_address= (unsigned long)(linker_address); \
+ if (linker_addr_start<=__linker_address&& \
+__linker_address<linker_addr_end) \
+
{
\
+__linker_address
= \
+__linker_address-linker_addr_start+ load_addr_start; \
+
}
\
+__linker_address;
\
+ })(quoted diff) +2
-0+ +/* Convert boot-time Xen address from where it was loaded byQuoted 4 lines
the
boot loader to the address it was layed out
+ * at link-time.
+ */Coding style: The first line is too long and multi-line comments look
like:/*
* Foo
* Bar
*/Quoted 3 lines
+#define
linker_addr(load_address)
\Same remark as for
load_addr()above.Quoted 22 lines
+
({
\
+ unsigned long__load_address= (unsigned long)(load_address); \
+ if (load_addr_start<=__load_address&& \
+__load_address<load_addr_end
) \
+
{
\
+__load_address
= \
+__load_address-load_addr_start+ linker_addr_start; \
+
}
\
+__load_address;
\Quoted 8 lines
Quoted 5 lines
+ })
xen/arch/riscv/mm.c new file +3
-0+ +static void __attribute__((section (".entry" ))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t*zeroeth,
Can this be named to
setup_initial_mapping()so this is clearer and
avoid the one '_' different with the function below.Sure. It will be better.
Quoted 7 lines
Quoted 5 lines
+ unsigned long
map_start, + unsigned longmap_end, + unsigned longpa_start, + bool writable)What about the executable bit?
It's always executable... But as you mentioned above
PTE_LEAF_DEFAULT
should be either RX or RW.
I think it makes sense to add flags instead of writable.Quoted 8 lines
Quoted 6 lines
+{
+ unsigned longpage_addr;
+ unsigned long index2;
+ unsigned long index1;
+ unsigned long index0;index* could be defined in the loop below.
It could. But I am curious why it is better?
Quoted 11 lines
Quoted 5 lines
+
+ /* align start addresses */
+map_start&=ZEROETH_MAP_MASK;
+pa_start&=ZEROETH_MAP_MASK;Hmmm... I would actually expect the address to be properly aligned
and
therefore not require an alignment here.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Otherwise, this raise the question of what happen if you have region
using the same page?That
map_start&=ZEROETH_MAP_MASKis needed to page number of address
w/o page offset.My point is why would the page offset be non-zero?
I checked a linker script and addresses that passed to setup_initial_mapping() and they are really always aligned so there is no any sense in additional alignment.
Quoted 23 lines
Quoted 19 lines
Quoted 13 lines
Quoted 5 lines
+
+page_addr=map_start;
+ while (page_addr<map_end)Looking at the loop, it looks like you are assuming that the region
will
never cross a boundary of a page-table (either L0, L1, L2). I am not
convinced you can make such assumption (see below).Quoted 6 lines
But if you really want to make such assumption then you should add
some
guard (eitherBUILD_BUG_ON(),ASSERT(), proper check) in your code to
avoid any surprise in the future.Quoted 3 lines
I am not sure that I fully understand what is the problem here. The address is aligned on (1<<12) boundary and each itearation is mapped (1<<12) page so all looks fine or I misunderstood you.
Quoted 6 lines
Let's take an example, imagine the region you want to map is 4MB. AFAICT, you are only passing one L0 page-table. So your code will end up
to overwrite the previous entries in the zeroeth page-table and then add
another link in the L1 page-table.
Got it. Then it looks that current approach isn't correct totally...
Quoted 88 lines
Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
+ {
+ index2 =pagetable_second_index(page_addr); + index1 =pagetable_first_index(page_addr); + index0 =pagetable_zeroeth_index(page_addr);xen/arch/riscv/mm.c new file +7
-0+ + /* Setup level2 table */ + second[index2] = paddr_to_pte((unsigned long)first); + second[index2].pte |= PTE_TABLE; + + /* Setup level1 table */ + first[index1] = paddr_to_pte((unsigned long)zeroeth);Quoted 8 lines
Quoted 4 lines
+ first[index1].pte |=
PTE_TABLE;
+
+ /* Setup level0 table */
+ if ( !pte_is_valid(&zeroeth[index0]) )Can you explain why you are checking !pte_is_valid() for the L0 entry
but not the other?Quoted 1 line
My mistake it should be checked for each level.
In which case, shouldn't you return an error if the entry is always valid?
Quoted 62 lines
Quoted 11 lines
Quoted 10 lines
+ {
+ /* Update level0 table */
+ zeroeth[index0] = paddr_to_pte((page_addr -map_start)
+pa_start);
+ zeroeth[index0].pte |=PTE_LEAF_DEFAULT; + zeroeth[index0].pte &= ~((!writable) ?PTE_WRITABLE:
0);Quoted 5 lines
Looking at the default value, it would mean that a non-writable mapping
is automatically executable. This seems wrong for the section is not
meant to be executable (like rodata).Yes, you are right. I'll reowrk
setup_initial_mapping()to pass flags
instead of write/read - only flag.Quoted 43 lines
Quoted 13 lines
+ }
xen/arch/riscv/mm.c new file +11
-0+ + /* Point to next page */ + page_addr += ZEROETH_SIZE; + } +} + +/* +* setup_initial_pagetables: +* +* 1) Build the page tables for Xen that map the following: +* 1.1) The physical location of Xen (where the bootloader Quoted 7 lines
loaded it)
+ * 1.2) The link-time location of Xen (where the linker expected Xen's
+ * addresses to be)
+ * 2) Load the page table into the SATP and enable the MMU
+ */
+void __attribute__((section(".entry")))I couldn't find a section ".entry" in the linker.
Quoted 6 lines
xen/arch/riscv/mm.c new file +6
-0+setup_initial_pagetables(unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start, + unsigned long linker_addr_end) +{ + pte_t *second;xen/arch/riscv/mm.c line 177
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ pte_t *first; + pte_t *zeroeth; + + clear_pagetables(load_addr_start, load_addr_end, + linker_addr_start, linker_addr_end); + + /* Get the addresses where the page tables were loaded */ + second = (pte_t *)load_addr(&xen_second_pagetable); + first = (pte_t *)load_addr(&xen_first_pagetable); + zeroeth = (pte_t *)load_addr(&xen_zeroeth_pagetable);I would consider to embed the type cast in
load_addr()so you are adding
some type safety within your code.
Quoted 8 lines
Quoted 3 lines
Definitely it will be better but
setup_initial_mapping()uses 'unsigned
long' for passing address that should be mapped.One possibility would be to introduce a new wrapper for the typesafety.
Anyway, it is not essential for now. Let's at least get the logic correct first :).
Agree. the logic should be fixed first.
Quoted 76 lines
Quoted 35 lines
Quoted 35 lines
Quoted 7 lines
+
+ /*
+ * Create a mapping from Xen's link-time addresses to where
they were actually loaded.This is line is way long than 80 characters. Please make sure to wrap
it
80 characters.Quoted 21 lines
+ */
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&_stext), +linker_addr(&_etext), +load_addr(&_stext), + false);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&__init_begin), +linker_addr(&__init_end), +load_addr(&__init_begin), + true);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr(&_srodata), +linker_addr(&_erodata), +load_addr(&_srodata), + false);
+ _setup_initial_pagetables(second, first, zeroeth, +linker_addr_start, +linker_addr_end,
+load_addr_start,
+ true);Quoted 7 lines
Quoted 4 lines
Where do you guarantee that Xen will always fit in an L0 table and
the
start address is aligned to the size of an L0 table?I don't guarantee that it fit in an L0 table but the start address is
aligned to the size of the L0 table at the start.Then it should be fixed.
Quoted 32 lines
Quoted 32 lines
Quoted 6 lines
+
+ /*
+ * Create a mapping of the load time address range to... the
load time address range.Same about the line length here.
Quoted 3 lines
+ * This mapping is used at boot time only. + */
+ _setup_initial_pagetables(second, first, zeroeth,This can only work if Xen is loaded at its linked address. So you need a
separate set of L0, L1 tables for the identity mapping.That said, this would not be sufficient because:
1) Xen may not be loaded at a 2M boundary (you can control with
U-boot, but not with EFI). So this may cross a boundary and therefore
need multiple pages.
2) The load region may overlap the link addressWhile I think it would be good to handle those cases from the start,
I
would understand why are not easy to solve. So I think the minimum is
to
throw some errors if you are in a case you can't support.
Quoted 5 lines
Quoted 1 line
Do you mean to throw some error in load_addr()/linkder_addr()?
In this case, I meant to check if
load_addr!=linker_addr, then throw
an error.
I am not sure that it is needed now and it is easier to throw an error but is option exist to handler situation when load_addr != linker_addr except throwing an error? relocate?
~ Oleksii
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Quoted 33 lines
Quoted 25 lines
Quoted 21 lines
Quoted 15 lines
Quoted 6 lines
+
+page_addr=map_start;
+ while (page_addr<map_end)Looking at the loop, it looks like you are assuming that the region
will
never cross a boundary of a page-table (either L0, L1, L2). I am
not
convinced you can make such assumption (see below).Quoted 6 lines
But if you really want to make such assumption then you should add
some
guard (eitherBUILD_BUG_ON(),ASSERT(), proper check) in your code to
avoid any surprise in the future.Quoted 3 lines
I am not sure that I fully understand what is the problem here. The address is aligned on (1<<12) boundary and each itearation is mapped (1<<12) page so all looks fine or I misunderstood you.
Quoted 8 lines
Let's take an example, imagine the region you want to map is 4MB. AFAICT, you are only passing one L0 page-table. So your code will end
up
to overwrite the previous entries in the zeroeth page-table and then
add
another link in the L1 page-table.
Quoted 1 line
Got it. Then it looks that current approach isn't correct totally...
Or as an option we can add to xen.lds.S something like:
ASSERT(_end - _start <= MB(L0_ENTRIES*PAGE_SIZE), "Xen too large")
~ Oleksii
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
In reply to OleksiiQuoted 1 line · Oleksii
Hi Julien,
Hi,
Sorry for the late answer. I was away for the past couple of weeks.
Quoted 69 lines · Julien Grall
Quoted 68 lines
Hi Oleksii,
Quoted 15 lines · Oleksii
Quoted 8 lines · Julien Grall
Quoted 7 lines
+/*
+ * WARNING:load_addr()andlinker_addr()are to be called only
when the MMU is
+ * disabled and only when executed by the primary CPU. They cannot refer to
+ * any global variable or functions.Quoted 6 lines
I find interesting you are saying when
_setup_initial_pagetables()is
called fromsetup_initial_pagetables(). Would you be able to explain
how
this is different?Quoted 6 lines
I am not sure that I understand your question correctly but
_setup_initial_pagetables()was introduced to map some addresses with
write/read flag. Probably I have to rename it to something that is more
clear.So the comment suggests that you code cannot refer to global functions/variables when the MMU is off. So I have multiple questions:
* Why only global? IOW, why static would be OK?
*setup_initial_pagetables()has a call to_setup_initial_pagetables()(IOW referring to another function). Why is
it fine?
* You have code in the next patch referring to global variables
(mainly_startand_end). How is this different?Quoted 21 lines
Quoted 8 lines
Quoted 6 lines
+ */
xen/arch/riscv/mm.c new file +3
-0+ +/* +* Convert an addressed layed out at link time to the address where it was loaded
Typo: s/addressed/address/ ?
Yes, it should be address. and 'layed out' should be changed to 'laid
out'...Quoted 10 lines
Quoted 3 lines
+ * by the bootloader.
+ */Looking at the implementation, you seem to consider that any address
not
in the range [linker_addr_start, linker_addr_end[ will have a 1:1 mappings.Quoted 6 lines
Quoted 3 lines
I am not sure this is what you want. So I would consider to throw an
error if such address is passed.I thought that at this stage and if no relocation was done it is 1:1
except the case whenload_addr_start!=linker_addr_start.The problem is what you try to map one to one may clash with the linked
region for Xen. So it is not always possible to map the region 1:1.
Quoted 5 lines
Quoted 1 line
Therefore, I don't see any use for the else part here.
Got it. Thanks.
I am curious than what is the correct approach in general to handle this situation?
There are multiple approach to handle it and I don't know which one would be best :). Relocation is one...
Quoted 3 lines
I mean that throw an error it is one option but if I would like to do that w/o throwing an error. Should it done some relocation in that case?
... solution. For Arm, I decided to avoid relocation it requires more work in assembly.
Let me describe what we did and you can decide what you want to do in RISC-V.
For Arm64, as we have plenty of virtual address space, I decided to reshuffle the layout so Xen is running a very high address (so it is unlikely to clash).
For Arm32, we have a smaller address space (4GB) so instead we are going through a temporary area to enable the MMU when the load and runtime region clash. The sequence is:
1) Map Xen to a temporary area
2) Enable the MMU and jump to the temporary area
3) Map Xen to the runtime area
4) Jump to the runtime area
5) Remove the temporary area
[...]
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Hmmm... I would actually expect the address to be properly aligned
and
therefore not require an alignment here.Otherwise, this raise the question of what happen if you have region
using the same page?
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
That
map_start&=ZEROETH_MAP_MASKis needed to page number of address
w/o page offset.My point is why would the page offset be non-zero?
I checked a linker script and addresses that passed to
setup_initial_mapping()and they are really always aligned so there is no any sense in additional alignment.
Ok. I would suggest to add some ASSERT()/BUG_ON() in order to confirm this is always the case.
[...]
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 6 lines
+
+ /*
+ * Create a mapping of the load time address range to... the
load time address range.Same about the line length here.
Quoted 3 lines
+ * This mapping is used at boot time only. + */
+ _setup_initial_pagetables(second, first, zeroeth,This can only work if Xen is loaded at its linked address. So you need a
separate set of L0, L1 tables for the identity mapping.That said, this would not be sufficient because:
1) Xen may not be loaded at a 2M boundary (you can control
with
U-boot, but not with EFI). So this may cross a boundary and therefore
need multiple pages.
2) The load region may overlap the link addressWhile I think it would be good to handle those cases from the start,
I
would understand why are not easy to solve. So I think the minimum is
to
throw some errors if you are in a case you can't support.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Do you mean to throw some error in load_addr()/linkder_addr()?
In this case, I meant to check if
load_addr!=linker_addr, then throw
an error.I am not sure that it is needed now and it is easier to throw an error but is option exist to handler situation when
load_addr!=linker_addrexcept throwing an error? relocate?
I believe I answered this above.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Hi Julien,
Quoted 103 lines · Julien Grall
Quoted 1 line · Oleksii
Hi Julien,
Hi,
Sorry for the late answer. I was away for the past couple of weeks.
Quoted 79 lines · Julien Grall
Quoted 78 lines
Hi Oleksii,
Quoted 16 lines · Oleksii
Quoted 9 lines · Julien Grall
Quoted 8 lines
+/*
+ * WARNING:load_addr()andlinker_addr()are to be called only
when the MMU is
+ * disabled and only when executed by the primary CPU. They
cannot refer to
+ * any global variable or functions.Quoted 6 lines
I find interesting you are saying when
_setup_initial_pagetables()is
called fromsetup_initial_pagetables(). Would you be able to explain
how
this is different?Quoted 8 lines
I am not sure that I understand your question correctly but
_setup_initial_pagetables()was introduced to map some addresses
with
write/read flag. Probably I have to rename it to something that is
more
clear.So the comment suggests that you code cannot refer to global functions/variables when the MMU is off. So I have multiple questions:
* Why only global? IOW, why static would be OK?
*setup_initial_pagetables()has a call to_setup_initial_pagetables()(IOW referring to another function). Why
is
it fine?
* You have code in the next patch referring to global variables
(mainly_startand_end). How is this different?Quoted 23 lines
Quoted 9 lines
Quoted 7 lines
+ */
xen/arch/riscv/mm.c new file +3
-0+ +/* +* Convert an addressed layed out at link time to the address
where it was loadedTypo: s/addressed/address/ ?
Yes, it should be address. and 'layed out' should be changed to 'laid
out'...Quoted 11 lines
Quoted 3 lines
+ * by the bootloader.
+ */Looking at the implementation, you seem to consider that any address
not
in the range [linker_addr_start, linker_addr_end[ will have a 1:1
mappings.Quoted 8 lines
Quoted 4 lines
I am not sure this is what you want. So I would consider to throw
an
error if such address is passed.I thought that at this stage and if no relocation was done it is
1:1
except the case whenload_addr_start!=linker_addr_start.The problem is what you try to map one to one may clash with the linked
region for Xen. So it is not always possible to map the region 1:1.Quoted 5 lines
Quoted 1 line
Therefore, I don't see any use for the else part here.
Got it. Thanks.
I am curious than what is the correct approach in general to handle this situation?
There are multiple approach to handle it and I don't know which one would be best :). Relocation is one...
Quoted 4 lines
I mean that throw an error it is one option but if I would like to do
that w/o throwing an error. Should it done some relocation in that case?... solution. For Arm, I decided to avoid relocation it requires more work in assembly.
Quoted 6 lines
Let me describe what we did and you can decide what you want to do in RISC-V.
For Arm64, as we have plenty of virtual address space, I decided to reshuffle the layout so Xen is running a very high address (so it is unlikely to clash).
I thought about running Xen very high address.
Thanks. I think it is a nice option to do the same for RISC-V64.
Quoted 6 lines
For Arm32, we have a smaller address space (4GB) so instead we are going
through a temporary area to enable the MMU when the load and runtime region clash. The sequence is:
Quoted 6 lines
1) Map Xen to a temporary area
2) Enable the MMU and jump to the temporary area
3) Map Xen to the runtime area
4) Jump to the runtime area
5) Remove the temporary area
It is the same for RV32. As we don't support RV32 I will use:
#error "Add support of MMU for RV32"
Quoted 65 lines
[...]
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
Hmmm... I would actually expect the address to be properly aligned
and
therefore not require an alignment here.Otherwise, this raise the question of what happen if you have region
using the same page?That
map_start&=ZEROETH_MAP_MASKis needed to page number of address
w/o page offset.Quoted 5 lines
Quoted 1 line
My point is why would the page offset be non-zero?
I checked a linker script and addresses that passed to
setup_initial_mapping()and they are really always aligned so there is
no any sense in additional alignment.Ok. I would suggest to add some ASSERT()/BUG_ON() in order to confirm this is always the case.
[...]
Quoted 40 lines
Quoted 40 lines
Quoted 28 lines
Quoted 28 lines
Quoted 7 lines
+
+ /*
+ * Create a mapping of the load time address range to...
the
load time address range.Same about the line length here.
Quoted 3 lines
+ * This mapping is used at boot time only. + */
+ _setup_initial_pagetables(second, first, zeroeth,This can only work if Xen is loaded at its linked address. So you
need a
separate set of L0, L1 tables for the identity mapping.That said, this would not be sufficient because:
1) Xen may not be loaded at a 2M boundary (you can control
with
U-boot, but not with EFI). So this may cross a boundary and therefore
need multiple pages.
2) The load region may overlap the link addressQuoted 8 lines
Quoted 7 lines
While I think it would be good to handle those cases from the start,
I
would understand why are not easy to solve. So I think the minimum is
to
throw some errors if you are in a case you can't support.Do you mean to throw some error in load_addr()/linkder_addr()?
In this case, I meant to check if
load_addr!=linker_addr, then throw
an error.
Quoted 7 lines
Quoted 5 lines
I am not sure that it is needed now and it is easier to throw an error
but is option exist to handler situation whenload_addr!=linker_addr
except throwing an error? relocate?I believe I answered this above.
Yeah, you answered my question completely. Thank you very much.
~ Oleksii
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
In reply to Oleksii KurochkoQuoted 78 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h new file +75
-0@@ -0,0 +1,90 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#define PAGE_ENTRIES 512 +#define VPN_BITS (9) +#define VPN_MASK ((unsigned long)((1 << VPN_BITS) - 1)) + +#ifdef CONFIG_RISCV_64 +/* L3 index Bit[47:39] */ +#define THIRD_SHIFT (39) +#define THIRD_MASK (VPN_MASK << THIRD_SHIFT) +/* L2 index Bit[38:30] */ +#define SECOND_SHIFT (30) +#define SECOND_MASK (VPN_MASK << SECOND_SHIFT) +/* L1 index Bit[29:21] */ +#define FIRST_SHIFT (21) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) +/* L0 index Bit[20:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT) + +#else // CONFIG_RISCV_32 + +/* L1 index Bit[31:22] */ +#define FIRST_SHIFT (22) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) + +/* L0 index Bit[21:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT) +#endif + +#define THIRD_SIZE (1 << THIRD_SHIFT) +#define THIRD_MAP_MASK (~(THIRD_SIZE - 1)) +#define SECOND_SIZE (1 << SECOND_SHIFT) +#define SECOND_MAP_MASK (~(SECOND_SIZE - 1)) +#define FIRST_SIZE (1 << FIRST_SHIFT) +#define FIRST_MAP_MASK (~(FIRST_SIZE - 1)) +#define ZEROETH_SIZE (1 << ZEROETH_SHIFT) +#define ZEROETH_MAP_MASK (~(ZEROETH_SIZE - 1)) + +#define PTE_SHIFT 10 + +#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 | PTE_EXECUTABLE) +#define PTE_TABLE (PTE_VALID) + +/* Calculate the offsets into the pagetables for a given VA */ +#define zeroeth_linear_offset (va) ((va) >> ZEROETH_SHIFT) +#define first_linear_offset (va) ((va) >> FIRST_SHIFT) +#define second_linear_offset (va) ((va) >> SECOND_SHIFT) +#define third_linear_offset (va) ((va) >> THIRD_SHIFT) + +#define pagetable_zeroeth_index (va) zeroeth_linear_offset((va) & ZEROETH_MASK) +#define pagetable_first_index (va) first_linear_offset((va) & FIRST_MASK) +#define pagetable_second_index (va) second_linear_offset((va) & SECOND_MASK) +#define pagetable_third_index (va) third_linear_offset((va) & THIRD_MASK) + +/* Page Table entry */ +typedef struct { +uint64_t pte; +}pte_t ; +
xen/arch/riscv/include/asm/page.h line 85
Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10
-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_SHIFT) + +static inline pte_t paddr_to_pte (unsigned long paddr) +{ +return (pte_t ) { .pte =addr_to_ppn (paddr) }; +} + +static inline bool pte_is_valid (pte_t *p)
Btw - const whenever possible please, especially in such basic helpers.
Quoted 6 lines
xen/arch/riscv/mm.c new file +3
-0@@ -0,0 +1,223 @@ +#include <xen/init.h> +#include <xen/lib.h> +
xen/arch/riscv/mm.c line 13
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+#include <asm/csr.h> +#include <asm/mm.h> +#include <asm/page.h> + +/* +* xen_second_pagetable is indexed with the VPN[2] page table entry field +* xen_first_pagetable is accessed from the VPN[1] page table entry field +* xen_zeroeth_pagetable is accessed from the VPN[0] page table entry field +*/ +pte_t xen_second_pagetable[PAGE_ENTRIES] __attribute__((__aligned__(PAGE_SIZE)));
static?
xen/arch/riscv/mm.c line 17
Quoted 4 lines
xen/arch/riscv/mm.c new file +4
-0+static pte_t xen_first_pagetable[PAGE_ENTRIES] + __attribute__((__aligned__(PAGE_SIZE))); +static pte_t xen_zeroeth_pagetable[PAGE_ENTRIES] + __attribute__((__aligned__(PAGE_SIZE)));
Please use __aligned() instead of open-coding it. You also may want to specifiy the section here explicitly, as .bss.page_aligned (as we do elsewhere).
xen/arch/riscv/mm.c line 24
Quoted 6 lines
xen/arch/riscv/mm.c new file +6
-0+extern unsigned long _stext; +extern unsigned long _etext; +extern unsigned long __init_begin; +extern unsigned long __init_end; +extern unsigned long _srodata; +extern unsigned long _erodata;
Please use kernel.h and drop then colliding declarations. For what's left please use array types, as suggested elsewhere already.
xen/arch/riscv/mm.c line 31
Quoted 6 lines
xen/arch/riscv/mm.c new file +6
-0+paddr_t phys_offset; + +#define resolve_early_addr (x) \ + ({ \ + unsigned long * __##x; \ + if ( load_addr_start <= x && x < load_addr_end ) \
Nit: Mismatched indentation.
xen/arch/riscv/mm.c line 41
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ __##x = (unsigned long *)x; \ + else \ + __##x = (unsigned long *)(x + load_addr_start - linker_addr_start); \ + __##x; \ + }) + +static void __init clear_pagetables (unsigned long load_addr_start, +unsigned long load_addr_end, +unsigned long linker_addr_start, +unsigned long linker_addr_end)
Nit (style): Indentation.
xen/arch/riscv/mm.c line 53
Quoted 12 lines
xen/arch/riscv/mm.c new file +12
-0+{ +unsigned long *p; +unsigned long page; +unsigned long i; + + page = (unsigned long )&xen_second_pagetable[0 ]; + + p =resolve_early_addr (page); +for ( i =0 ; i <ARRAY_SIZE (xen_second_pagetable); i++ ) + { + p[i] =0ULL ; + }
We typically omit braces around single-statement bodies. Here,
though: Why do you do this in the first place? These static arrays all start out zero-initialized anyway (from when you clear .bss). Plus even if they didn't - why not memset()?
Quoted 44 lines
xen/arch/riscv/mm.c new file +44
-0+ page = (unsigned long )&xen_first_pagetable[0 ]; + p =resolve_early_addr (page); +for ( i =0 ; i <ARRAY_SIZE (xen_first_pagetable); i++ ) + { + p[i] =0ULL ; + } + + page = (unsigned long )&xen_zeroeth_pagetable[0 ]; + p =resolve_early_addr (page); +for ( i =0 ; i <ARRAY_SIZE (xen_zeroeth_pagetable); i++ ) + { + p[i] =0ULL ; + } +} + +/* +* WARNING: load_addr() and linker_addr() are to be called only when the MMU is +* disabled and only when executed by the primary CPU. They cannot refer to +* any global variable or functions. +*/ + +/* +* Convert an addressed layed out at link time to the address where it was loaded +* by the bootloader. +*/ +#define load_addr (linker_address) \ + ({ \ + unsigned long __linker_address = (unsigned long)(linker_address); \ + if ( linker_addr_start <= __linker_address && \ + __linker_address < linker_addr_end ) \ + { \ + __linker_address = \ + __linker_address - linker_addr_start + load_addr_start; \ + } \ + __linker_address; \ + }) + +/* Convert boot-time Xen address from where it was loaded by the boot loader to the address it was layed out +* at link-time. +*/ +#define linker_addr (load_address) \ + ({ \ + unsigned long __load_address = (unsigned long)(load_address); \ + if ( load_addr_start <= __load_address && \
xen/arch/riscv/mm.c line 108
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+ __load_address < load_addr_end ) \ + { \ + __load_address = \ + __load_address - load_addr_start + linker_addr_start; \ + } \ + __load_address; \ + }) + +static void __attribute__((section(".entry"))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t *zeroeth,
Why the special section (also again further down)?
Jan
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Quoted 5 lines · Jan Beulich
xen/arch/riscv/mm.c line 108
Quoted 2 lines · Oleksii Kurochko
xen/arch/riscv/mm.c new file +2
-0+static void __attribute__((section(".entry"))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t *zeroeth,Why the special section (also again further down)?
Looking at patch 2 it occurred to me that you probably mean __init here.
Jan
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
Quoted 10 lines · Jan Beulich
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksii Kurochko
(quoted diff) +2
-0+static void __attribute__((section(".entry"))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t*zeroeth,
Why the special section (also again further down)?
Looking at patch 2 it occurred to me that you probably mean
__inithere.
Yes, you are right.
Quoted 2 lines
Jan
Re: [PATCH v1 1/3] xen/riscv: introduce setup_initial_pages
In reply to Jan BeulichQuoted 91 lines · Jan Beulich
Quoted 90 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h new file +9
-0@@ -0,0 +1,90 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#define PAGE_ENTRIES 512 +#define VPN_BITS (9) +#define VPN_MASK ((unsigned long)((1 << VPN_BITS) -1))
xen/arch/riscv/include/asm/page.h new file +48
-0+ +#ifdef CONFIG_RISCV_64 +/* L3 index Bit[47:39] */ +#define THIRD_SHIFT (39) +#define THIRD_MASK (VPN_MASK << THIRD_SHIFT) +/* L2 index Bit[38:30] */ +#define SECOND_SHIFT (30) +#define SECOND_MASK (VPN_MASK << SECOND_SHIFT) +/* L1 index Bit[29:21] */ +#define FIRST_SHIFT (21) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) +/* L0 index Bit[20:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT) + +#else // CONFIG_RISCV_32 + +/* L1 index Bit[31:22] */ +#define FIRST_SHIFT (22) +#define FIRST_MASK (VPN_MASK << FIRST_SHIFT) + +/* L0 index Bit[21:12] */ +#define ZEROETH_SHIFT (12) +#define ZEROETH_MASK (VPN_MASK << ZEROETH_SHIFT) +#endif + +#define THIRD_SIZE (1 << THIRD_SHIFT) +#define THIRD_MAP_MASK (~(THIRD_SIZE - 1)) +#define SECOND_SIZE (1 << SECOND_SHIFT) +#define SECOND_MAP_MASK (~(SECOND_SIZE - 1)) +#define FIRST_SIZE (1 << FIRST_SHIFT) +#define FIRST_MAP_MASK (~(FIRST_SIZE - 1)) +#define ZEROETH_SIZE (1 << ZEROETH_SHIFT) +#define ZEROETH_MAP_MASK (~(ZEROETH_SIZE - 1)) + +#define PTE_SHIFT 10 + +#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|PTE_EXECUTABLE)xen/arch/riscv/include/asm/page.h new file +9
-0+#define PTE_TABLE (PTE_VALID) + +/* Calculate the offsets into the pagetables for a given VA */ +#define zeroeth_linear_offset (va) ((va) >> ZEROETH_SHIFT) +#define first_linear_offset (va) ((va) >> FIRST_SHIFT) +#define second_linear_offset (va) ((va) >> SECOND_SHIFT) +#define third_linear_offset (va) ((va) >> THIRD_SHIFT) + +#define pagetable_zeroeth_index (va) zeroeth_linear_offset((va) &
ZEROETH_MASK)
+#define pagetable_first_index(va)first_linear_offset((va) & FIRST_MASK)
+#define pagetable_second_index(va)second_linear_offset((va) & SECOND_MASK)
+#define pagetable_third_index(va)third_linear_offset((va) & THIRD_MASK)xen/arch/riscv/include/asm/page.h new file +7
-0+ +/* Page Table entry */ +typedef struct { + uint64_t pte; +}pte_t ; + +/* 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 */xen/arch/riscv/include/asm/page.h new file +3
-0+#define addr_to_ppn (x) (((x) >> PAGE_SHIFT) << PTE_SHIFT) + +static inline pte_t paddr_to_pte (unsigned long paddr)
Quoted 8 lines
xen/arch/riscv/include/asm/page.h line 85
Quoted 5 lines
xen/arch/riscv/include/asm/page.h new file +5
-0+{ + return (pte_t) { .pte = addr_to_ppn(paddr) }; +} + +static inline bool pte_is_valid (pte_t *p)Btw - const whenever possible please, especially in such basic helpers.
Sure. Thanks.
Quoted 15 lines
Quoted 15 lines
xen/arch/riscv/mm.c new file +9
-0@@ -0,0 +1,223 @@ +#include <xen/init.h> +#include <xen/lib.h> + +#include <asm/csr.h> +#include <asm/mm.h> +#include <asm/page.h> + +/* +* xen_second_pagetable is indexed with the VPN[2] page table entry field
+ *xen_first_pagetableis accessed from the VPN[1] page table
Quoted 8 lines
Quoted 6 lines
entry field
+ *xen_zeroeth_pagetableis accessed from the VPN[0] page table entry field
+ */
+pte_t xen_second_pagetable[PAGE_ENTRIES] __attribute__((__aligned__(PAGE_SIZE)));static?
It should be static.
Thanks.
Quoted 11 lines
xen/arch/riscv/mm.c line 17
Quoted 5 lines
xen/arch/riscv/mm.c new file +4
-0+static pte_t xen_first_pagetable[PAGE_ENTRIES] + __attribute__((__aligned__(PAGE_SIZE))); +static pte_t xen_zeroeth_pagetable[PAGE_ENTRIES] + __attribute__((__aligned__(PAGE_SIZE)));Please use
__aligned()instead of open-coding it. You also may want to
specifiy the section here explicitly, as .bss.page_aligned (as we do elsewhere).
Quoted 9 lines
xen/arch/riscv/mm.c line 24
Quoted 6 lines
xen/arch/riscv/mm.c new file +6
-0+extern unsigned long _stext; +extern unsigned long _etext; +extern unsigned long __init_begin; +extern unsigned long __init_end; +extern unsigned long _srodata; +extern unsigned long _erodata;Please use
kernel.hand drop then colliding declarations. For what's left please use array types, as suggested elsewhere already.
Thanks. I'll take it into account.
Quoted 42 lines
Quoted 11 lines
xen/arch/riscv/mm.c new file +4
-0+paddr_t phys_offset; + +#define resolve_early_addr (x) \ +({
\
+ unsigned long *
__##x; \
+ if (load_addr_start<= x && x <load_addr_end) \Nit: Mismatched indentation.
Quoted 11 lines
+ __##x = (unsigned long
*)x; \
+
else
\
+ __##x = (unsigned long *)(x +load_addr_start-linker_addr_start); \
+
__##x;
\
+ })xen/arch/riscv/mm.c line 41
Quoted 5 lines
xen/arch/riscv/mm.c new file +5
-0+ +static void __init clear_pagetables (unsigned long load_addr_start, + unsigned long load_addr_end, + unsigned long linker_addr_start, + unsigned long linker_addr_end)Nit (style): Indentation.
Quoted 9 lines
xen/arch/riscv/mm.c new file +9
-0+{ + unsigned long *p; + unsigned long page; + unsigned long i; + + page = (unsigned long)&xen_second_pagetable[0]; + + p = resolve_early_addr(page); + for ( i = 0; i < ARRAY_SIZE(xen_second_pagetable); i++ )
Quoted 8 lines
Quoted 3 lines
+ {
+ p[i] = 0ULL;
+ }We typically omit braces around single-statement bodies. Here,
though: Why do you do this in the first place? These static arrays all start out zero-initialized anyway (from when you clear .bss). Plus even if they didn't - why notmemset()?
clear_pagetables() will be deleted as you mentioned it will be zeroed during initialization of .bss.
It wasn't done before because .bss wasn't initialized. Now the patches are waiting to be merged.
Quoted 84 lines
Quoted 84 lines
+ page = (unsigned long)&xen_first_pagetable[0]; + p =
resolve_early_addr(page);
+ for ( i = 0; i <ARRAY_SIZE(xen_first_pagetable); i++ ) + {
+ p[i] = 0ULL;
+ }xen/arch/riscv/mm.c new file +11
-0+ + page = (unsigned long)&xen_zeroeth_pagetable[0]; + p = resolve_early_addr(page); + for ( i = 0; i < ARRAY_SIZE(xen_zeroeth_pagetable); i++ ) + { + p[i] = 0ULL; + } +} + +/* +* WARNING: load_addr() and linker_addr() are to be called only when the MMU is
+ * disabled and only when executed by the primary CPU. They cannot refer to
+ * any global variable or functions.
+ */xen/arch/riscv/mm.c new file +3
-0+ +/* +* Convert an addressed layed out at link time to the address where it was loaded
+ * by the bootloader.
+ */
+#defineload_addr(linker_address)
\
+
({
\
+ unsigned long__linker_address= (unsigned long)(linker_address); \
+ if (linker_addr_start<=__linker_address&& \
+__linker_address<linker_addr_end) \
+
{
\
+__linker_address
= \
+__linker_address-linker_addr_start+ load_addr_start; \
+
}
\
+__linker_address;
\
+ })xen/arch/riscv/mm.c new file +2
-0+ +/* Convert boot-time Xen address from where it was loaded by the boot loader to the address it was layed out
+ * at link-time.
+ */
+#definelinker_addr(load_address)
\
+
({
\
+ unsigned long__load_address= (unsigned long)(load_address); \
+ if (load_addr_start<=__load_address&& \
+__load_address<load_addr_end
) \
+
{
\
+__load_address
= \
+__load_address-load_addr_start+ linker_addr_start; \
+
}
\
+__load_address;
Quoted 8 lines
Quoted 6 lines
\
+ })xen/arch/riscv/mm.c new file +3
-0+ +static void __attribute__((section (".entry" ))) +_setup_initial_pagetables(pte_t *second, pte_t *first, pte_t*zeroeth,
Why the special section (also again further down)?
There is no specific reason in case of Xen. I'll remove that.
~ Oleksii
PATCH 2/3 xen/riscv: setup initial pagetables
In reply to Oleksii KurochkoCalculate load and linker linker image addresses and setup initial pagetables.
--- xen/arch/riscv/setup.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
xen/arch/riscv/setup.c +11 -0
@@ -1,9 +1,11 @@#include <xen/bug.h> #include <xen/compile.h> #include <xen/init.h> +#include <xen/kernel.h> #include <asm/csr.h> #include <asm/early_printk.h> +#include <asm/mm.h> #include <asm/traps.h> /* Xen stack for bringing up the first CPU. */
@@ -43,6 +45,11 @@ static void __init disable_fpu(void)void __init noreturnstart_xen (void ) { +unsigned long load_start = (unsigned long )start; +unsigned long load_end = load_start + (unsigned long )(_end - _start); +unsigned long linker_start = (unsigned long )_start; +unsigned long linker_end = (unsigned long )_end; +/* * The following things are passed by bootloader: * a0 -> hart_id
@@ -65,6 +72,10 @@ void __init noreturn start_xen(void)test_macros_from_bug_h (); +setup_initial_pagetables (load_start, load_end, linker_start, linker_end); + +early_printk ("MMU has been enabled\n" ); +for ( ;; ) asmvolatile ("wfi" );
Signature · 1 line
2.39.0
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Hi,
Quoted 25 lines · Oleksii Kurochko
Calculate load and linker linker image addresses and setup initial pagetables.
Signed-off-by Oleksii Kurochko <[email protected]>--- xen/arch/riscv/setup.c | 11 +++++++++++ 1 file changed, 11 insertions(+)xen/arch/riscv/setup.c +2
-0@@ -1,9 +1,11 @@#include <xen/bug.h> #include <xen/compile.h> #include <xen/init.h> +#include <xen/kernel.h> #include <asm/csr.h> #include <asm/early_printk.h> +#include <asm/mm.h> #include <asm/traps.h> /* Xen stack for bringing up the first CPU. */
xen/arch/riscv/setup.c static void __init disable_fpu(void) · line 49
Quoted 5 lines
xen/arch/riscv/setup.c +2
-0@@ -43,6 +45,11 @@ static void __init disable_fpu(void)void __init noreturnstart_xen (void ) { +unsigned long load_start = (unsigned long )start; +unsigned long load_end = load_start + (unsigned long )(_end - _start);
I am a bit puzzled, on top of load_addr() and linker_addr(), you wrote it can't use global variable/function. But here... you are using them. So how is this different?
xen/arch/riscv/setup.c static void __init disable_fpu(void) · line 51
Quoted 2 lines
xen/arch/riscv/setup.c +2
-0+unsigned long linker_start = (unsigned long )_start; +unsigned long linker_end = (unsigned long )_end;
I am a bit confused with how you define the start/end for both the linker and load. In one you use _start and the other _end.
Both are fixed at compile time, so I assume the values will be a linked address rather than the load address. So how is this meant to how?
Furthermore, I would expect linker_start and load_start to point to the same symbol (the only different is one store the virtual address whereas the other the physical address). But here you are technically using two different symbol. Can you explain why?
xen/arch/riscv/setup.c void __init noreturn start_xen(void) · line 75
Quoted 9 lines
xen/arch/riscv/setup.c +2
-0+/* * The following things are passed by bootloader: * a0 -> hart_id @@ -65,6 +72,10 @@ void __init noreturn start_xen(void)test_macros_from_bug_h (); +setup_initial_pagetables (load_start, load_end, linker_start, linker_end);
Shouldn't this happen earlier in start_xen()?
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Quoted 11 lines · Julien Grall
xen/arch/riscv/setup.c static void __init disable_fpu(void) · line 49
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/setup.c +2
-0@@ -43,6 +45,11 @@ static void __init disable_fpu(void)void __init noreturnstart_xen (void ) { +unsigned long load_start = (unsigned long )start; +unsigned long load_end = load_start + (unsigned long )(_end - _start);I am a bit puzzled, on top of
load_addr()andlinker_addr(), you wrote it can't use global variable/function. But here... you are using them. So how is this different?
I guess "use" means "access" (i.e. call a function or read/write a variable). I suppose it does not mean "take the address of".
Jan
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Julien Grall
xen/arch/riscv/setup.c static void __init disable_fpu(void) · line 49
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/setup.c +2
-0@@ -43,6 +45,11 @@ static void __init disable_fpu(void)void __init noreturnstart_xen (void ) { +unsigned long load_start = (unsigned long )start; +unsigned long load_end = load_start + (unsigned long )(_end - _start);I am a bit puzzled, on top of
load_addr()andlinker_addr(), you wrote it can't use global variable/function. But here... you are using them. So how is this different?
Quoted 2 lines
I guess "use" means "access" (i.e. call a function or read/write a variable). I suppose it does not mean "take the address of".
If so, then I don't understand why we need to pass linker_start, linker_end in parameters for setup_initial_pages().
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
In reply to Julien GrallQuoted 32 lines · Julien Grall
Hi,
Quoted 29 lines · Oleksii Kurochko
Calculate load and linker linker image addresses and setup initial pagetables.
Signed-off-by Oleksii Kurochko <[email protected]>---
xen/arch/riscv/setup.c| 11 +++++++++++1 file changed, 11 insertions(+)xen/arch/riscv/setup.c +1
-0@@ -1,9 +1,11 @@ #include <xen/bug.h> #include <xen/compile.h> #include <xen/init.h> +#include <xen/kernel.h>
#include <asm/csr.h>
#include <asm/early_printk.h>
+#include <asm/mm.h>
#include <asm/traps.h>
/* Xen stack for bringing up the first CPU. */xen/arch/riscv/setup.c +0
-0@@ -43,6 +45,11 @@ static void __init disable_fpu(void)
void__initnoreturnstart_xen(void)
{
+ unsigned long load_start = (unsigned long)start;
Quoted 8 lines
Quoted 2 lines
+ unsigned long load_end =
load_start+ (unsigned long)(_end -_start);I am a bit puzzled, on top of
load_addr()andlinker_addr(), you wrote
it can't use global variable/function. But here... you are using them.
So how is this different?
I don't use load_addr() and linker_addr() macros here.
Quoted 11 lines
Quoted 3 lines
+ unsigned long linker_start = (unsigned long)_start; + unsigned long linker_end = (unsigned long)_end;
I am a bit confused with how you define the start/end for both the linker and load. In one you use
_startand the other_end.Both are fixed at compile time, so I assume the values will be a linked
address rather than the load address. So how is this meant to how?
_start, _end - it is label from linker script so I use them to define linker_start and linker_end addresses.
load_start is defined as an address of start() function from head.S and load_end is the load_start + the size (_end - _start)
Quoted 7 lines
Furthermore, I would expect
linker_startandload_startto point to the
same symbol (the only different is one store the virtual address whereas
the other the physical address). But here you are technically using two
different symbol. Can you explain why?
It is used to make identity mapping for the range [load_addr, load_end] and [linker_addr, linker_end]. It was done so because in Bobby's patches in the linker script XEN_VIRT_START is defined as _AT(vaddr_t,0x00200000) but bootloader loads Xen at 0x80200000 and so in this case loadr_addr != linker_addr.
But I have changed XEN_VIRT_START to 0x8020...00 so they are equal now.
Quoted 13 lines
Quoted 11 lines
+
/*
* The following things are passed by bootloader:
* a0 ->hart_idxen/arch/riscv/setup.c +0
-0@@ -65,6 +72,10 @@ void __init noreturn start_xen(void)
test_macros_from_bug_h();
+setup_initial_pagetables(load_start, load_end, linker_start, linker_end);Shouldn't this happen earlier in
start_xen()?
It can. If to be honest I don't know if it should. I added at the end only because it was the last thing I worked on...
Quoted 3 lines
Cheers,
~ Oleksii
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Hi,
Quoted 41 lines · Oleksii
Quoted 29 lines · Julien Grall
Hi,
Quoted 26 lines · Oleksii Kurochko
Calculate load and linker linker image addresses and setup initial pagetables.
Signed-off-by Oleksii Kurochko <[email protected]>--- xen/arch/riscv/setup.c | 11 +++++++++++ 1 file changed, 11 insertions(+)xen/arch/riscv/setup.c +2
-0@@ -1,9 +1,11 @@ #include <xen/bug.h> #include <xen/compile.h> #include <xen/init.h> +#include <xen/kernel.h> #include <asm/csr.h> #include <asm/early_printk.h> +#include <asm/mm.h> #include <asm/traps.h> /* Xen stack for bringing up the first CPU. */@@ -43,6 +45,11 @@ static void __init disable_fpu(void)Quoted 11 lines
xen/arch/riscv/setup.c static void __init disable_fpu(void)
Quoted 5 lines
xen/arch/riscv/setup.c +2
-0void __init noreturn start_xen(void) { + unsigned long load_start = (unsigned long)start; + unsigned long load_end = load_start + (unsignedlong )(_end - _start);I am a bit puzzled, on top of
load_addr()andlinker_addr(), you wrote
it can't use global variable/function. But here... you are using them.
So how is this different?
Quoted 1 line
I don't use
load_addr()andlinker_addr()macros here.
I understand that. But my comment was related to:
/*
* WARNING: load_addr() and linker_addr() are to be called only when
the MMU is
* disabled and only when executed by the primary CPU. They cannot
refer to
* any global variable or functions.
*/
_start and _end are global variables. So why can you use them here but not there?
If you could use them in load_addr() then you could simplify a lot your logic.
Quoted 11 lines
Quoted 11 lines
xen/arch/riscv/setup.c static void __init disable_fpu(void) · line 51
Quoted 3 lines
xen/arch/riscv/setup.c +2
-0+ unsigned long linker_start = (unsigned long)_start; + unsigned long linker_end = (unsigned long)_end;I am a bit confused with how you define the start/end for both the linker and load. In one you use
_startand the other_end.Both are fixed at compile time, so I assume the values will be a linked
address rather than the load address. So how is this meant to how?
Quoted 5 lines
_start,_end- it is label from linker script so I use them to definelinker_startandlinker_endaddresses.
load_startis defined as an address ofstart()function fromhead.Sandload_endis theload_start+ the size (_end-_start)
I think you misunderstood my comment. I understand what the variables are for. But I don't understand the computation because Xen could be loaded at a different address than the runtime address.
Quoted 8 lines
Quoted 8 lines
Furthermore, I would expect
linker_startandload_startto point to the
same symbol (the only different is one store the virtual address whereas
the other the physical address). But here you are technically using two
different symbol. Can you explain why?
Quoted 6 lines
It is used to make identity mapping for the range [
load_addr,load_end] and [linker_addr,linker_end]. It was done so because in Bobby's patches in the linker scriptXEN_VIRT_STARTis defined as_AT(vaddr_t,0x00200000)but bootloader loads Xen at0x80200000and so in this caseloadr_addr!=linker_addr.
But I have changedXEN_VIRT_STARTto 0x8020...00 so they are equal now.
So this will be broken as soon as this code will be tested on an hardware where there is no RAM at 0x8020...00. I would strongly recommend for you to test your code with XEN_VIRT_START != load address.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/riscv/setup.c +1
-0+ /** The following things are passed by bootloader: * a0 ->
hart_idxen/arch/riscv/setup.c +0
-0@@ -65,6 +72,10 @@ void __init noreturn start_xen(void)
Quoted 8 lines
Quoted 6 lines
xen/arch/riscv/setup.c void __init noreturn start_xen(void)
Quoted 4 lines
xen/arch/riscv/setup.c +1
-0test_macros_from_bug_h(); + setup_initial_pagetables(load_start, load_end, linker_start, linker_end);Shouldn't this happen earlier in
start_xen()?It can. If to be honest I don't know if it should. I added at the end only because it was the last thing I worked on...
I think we should enable the MMU and switch to the runtime mapping as early as possible.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Quoted 56 lines · Julien Grall
Hi,
Quoted 41 lines · Oleksii
Quoted 32 lines · Julien Grall
Hi,
Quoted 29 lines · Oleksii Kurochko
Calculate load and linker linker image addresses and setup initial pagetables.
Signed-off-by Oleksii Kurochko <[email protected]>---
xen/arch/riscv/setup.c| 11 +++++++++++1 file changed, 11 insertions(+)xen/arch/riscv/setup.c +1
-0@@ -1,9 +1,11 @@ #include <xen/bug.h> #include <xen/compile.h> #include <xen/init.h> +#include <xen/kernel.h>
#include <asm/csr.h>
#include <asm/early_printk.h>
+#include <asm/mm.h>
#include <asm/traps.h>
/* Xen stack for bringing up the first CPU. */xen/arch/riscv/setup.c +0
-0@@ -43,6 +45,11 @@ static void __init disable_fpu(void)
void__initnoreturnstart_xen(void)
{
+ unsigned long load_start = (unsigned long)start;Quoted 8 lines
Quoted 2 lines
+ unsigned long load_end =
load_start+ (unsigned long)(_end -_start);I am a bit puzzled, on top of
load_addr()andlinker_addr(), you wrote
it can't use global variable/function. But here... you are using them.
So how is this different?Quoted 1 line
I don't use
load_addr()andlinker_addr()macros here.I understand that. But my comment was related to:
/*
* WARNING:load_addr()andlinker_addr()are to be called only when the MMU is
* disabled and only when executed by the primary CPU. They cannot refer to
* any global variable or functions.
*/
Quoted 8 lines
_startand_endare global variables. So why can you use them here but
not there?If you could use them in
load_addr()then you could simplify a lot your
logic.
I experimented with it and it seems to me that the macros can be used with functions&global variables so the comment is incorrect.
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
Quoted 3 lines
+ unsigned long linker_start = (unsigned long)_start; + unsigned long linker_end = (unsigned long)_end;
Quoted 9 lines
I am a bit confused with how you define the start/end for both the
linker and load. In one you use_startand the other_end.Both are fixed at compile time, so I assume the values will be a linked
address rather than the load address. So how is this meant to how?
_start,_end- it is label from linker script so I use them to definelinker_startandlinker_endaddresses.
Quoted 8 lines
Quoted 3 lines
load_startis defined as an address ofstart()function fromhead.Sandload_endis theload_start+ the size (_end-_start)I think you misunderstood my comment. I understand what the variables are for. But I don't understand the computation because Xen could be loaded at a different address than the runtime address.
What do you mean here by the runtime address? Do you mean an address where boot loader put Xen? Or an address after relocation, or something else?
Actually after my latest experiments it looks that we don't need to calculate that things at all because for RISC-V it is used everywhere PC-relative access.
Thereby it doesn't matter what is an address where Xen was loaded and linker address.
Right now I found only to cases which aren't PC-relative. Please look at the patch below:
(quoted diff) +0 -0
b/xen/arch/riscv/include/asm/config.h
index 763a922a04..e1ba613d81 100644
xen/arch/riscv/include/asm/config.h +1 -1
@@ -39,7 +39,7 @@ name:#endif -#define XEN_VIRT_START _AT(UL, 0x80200000) +#define XEN_VIRT_START _AT(UL, 0x00200000)#define SMP_CACHE_BYTES (1 << 6)
(quoted diff) +0 -0
b/xen/arch/riscv/riscv64/head.S
index ffd95f9f89..87842632e9 100644
xen/arch/riscv/riscv64/head.S +2 -0
@@ -1,5 +1,7 @@ #include <asm/riscv_encoding.h> + .option nopic + .section .text.header, "ax", %progbits ENTRY(start)
xen/arch/riscv/traps.c +0 -0
@@ -123,8 +123,14 @@ int do_bug_frame(const struct cpu_user_regs *regs,
vaddr_t pc)
const char *filename, *predicate;
int lineno;- static const struct bug_frame* bug_frames[] = {
- &__start_bug_frames[0],
+ /*
+ * force fill bug_frames array using auipc/addi instructions to
+ * make addresses in bug_frames PC-relative.
+ */
+ const struct bug_frame * force = (const struct bug_frame *)
&__start_bug_frames[0];
+
+ const struct bug_frame* bug_frames[] = {
+ force,
&__stop_bug_frames_0[0],
&__stop_bug_frames_1[0],
&__stop_bug_frames_2[0],The changes related to <asm/config.h> are only to make linker_addr != load_address. So:
1. The first issue with cpu0_boot_stack in the head.S file. When we do:
la sp, cpu0_boot_stack
Pseudo-instruction la will be transformed to auipc/addi OR
auipc/l{w|d}.
It depends on an option: nopic, pic. [1]
So the solution can be the following:
* As it is done in the patch: add to the start of head.S ".option
nopic"
* Change la to lla thereby it will be always generated "auipc/addi"
to get an address of variable.
2. The second issue is with the code in do_bug_frame() with bug_frames array:
const struct bug_frame* bug_frames[] = {
&__start_bug_frames[0],
&__stop_bug_frames_0[0],
&__stop_bug_frames_1[0],
&__stop_bug_frames_2[0],
&__stop_bug_frames_3[0],
}; In this case &{start,stop}bug_frames{,{0-3}} will be changed to
linker address. In case of when load_addr is 0x80200000 and linker_addr is 0x00200000 then &{start,stop}bug_frames{,{0-3}} will be equal to 0x00200000 + X.
To force using addresses related to load_addr in bug_frames, it is
necessary to declare a variable with getting an address of &__{start,stop}bug_frames{,{0-3}} thereby it will generate the code:
2002c2: 00001797 auipc a5,0x1 2002c6: d3e78793 addi a5,a5,-706 # 201000 <__start_bug_frames> 2002ca: faf43c23 sd a5,-72(s0) 2002ce: 00001797 auipc a5,0x1 2002d2: d3a78793 addi a5,a5,-710 #
201008 <__stop_bug_frames_
Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
Furthermore, I would expect
linker_startandload_startto point to
the
same symbol (the only different is one store the virtual address whereas
the other the physical address). But here you are technically using
two
different symbol. Can you explain why?Quoted 9 lines
It is used to make identity mapping for the range [
load_addr,load_end]
and [linker_addr,linker_end]. It was done so because in Bobby's patches in the linker scriptXEN_VIRT_STARTis defined as_AT(vaddr_t,0x00200000)but bootloader loads Xen at0x80200000and so
in this caseloadr_addr!=linker_addr.
But I have changedXEN_VIRT_STARTto 0x8020...00 so they are equal now.
Quoted 4 lines
So this will be broken as soon as this code will be tested on an hardware where there is no RAM at 0x8020...00. I would strongly recommend for you to test your code with
XEN_VIRT_START!= load address.
I've added the check to the new version of the patch but please the comments above.
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines
+
/*
* The following things are passed by bootloader:
* a0 ->hart_idxen/arch/riscv/setup.c +0
-0@@ -65,6 +72,10 @@ void __init noreturn start_xen(void)Quoted 6 lines
test_macros_from_bug_h();
+ setup_initial_pagetables(load_start,load_end,linker_start,linker_end);Shouldn't this happen earlier in
start_xen()?
Quoted 6 lines
Quoted 3 lines
It can. If to be honest I don't know if it should. I added at the end
only because it was the last thing I worked on...I think we should enable the MMU and switch to the runtime mapping as early as possible.
I realized that during the mentioned above experiments.
~ Oleksii
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Quoted 13 lines · Oleksii
Actually after my latest experiments it looks that we don't need to calculate that things at all because for RISC-V it is used everywhere PC-relative access.
Thereby it doesn't matter what is an address where Xen was loaded and linker address.
Right now I found only to cases which aren't PC-relative. Please look at the patch below:xen/arch/riscv/include/asm/config.h +0
-0
xen/arch/riscv/include/asm/config.h line 42
Quoted 5 lines
xen/arch/riscv/include/asm/config.h +1
-1@@ -39,7 +39,7 @@ name:#endif -#define XEN_VIRT_START _AT(UL, 0x80200000) +#define XEN_VIRT_START _AT(UL, 0x00200000)
I think this wants to remain the address where Xen actually runs, and where Xen is linked to. This ...
Quoted 19 lines
xen/arch/riscv/traps.c +8
-2@@ -123,8 +123,14 @@ int do_bug_frame(const struct cpu_user_regs *regs, vaddr_t pc)const char *filename, *predicate;int lineno; -static const struct bug_frame * bug_frames[] = { - &__start_bug_frames[0 ], +/* +* force fill bug_frames array using auipc/addi instructions to +* make addresses in bug_frames PC-relative. +*/ +const struct bug_frame * force = (const struct bug_frame *) &__start_bug_frames[0 ]; + +const struct bug_frame * bug_frames[] = { + force, &__stop_bug_frames_0[0 ],
Quoted 2 lines
&__stop_bug_frames_1[0],
&__stop_bug_frames_2[0],... array would better be static anyway, and ...
Quoted 23 lines
The changes related to <
asm/config.h> are only to makelinker_addr!=load_address. So:
1. The first issue withcpu0_boot_stackin thehead.Sfile. When we do:
la sp,cpu0_boot_stack
Pseudo-instruction la will be transformed to auipc/addi OR
auipc/l{w|d}.
It depends on an option: nopic, pic. [1]So the solution can be the following:
* As it is done in the patch: add to the start ofhead.S".option
nopic"
* Change la to lla thereby it will be always generated "auipc/addi"
to get an address of variable.2. The second issue is with the code in
do_bug_frame()withbug_framesarray:const struct bug_frame* bug_frames[] = { &__start_bug_frames[0], &__stop_bug_frames_0[0], &__stop_bug_frames_1[0], &__stop_bug_frames_2[0], &__stop_bug_frames_3[0], };
Quoted 4 lines
In this case &{start,stop}bug_frames{,{0-3}} will be changed to
linker address. In case of whenload_addris0x80200000andlinker_addris0x00200000then &{start,stop}bug_frames{,{0-3}} will be equal to0x00200000+ X.
... this "solution" to a problem you introduce by wrongly modifying the linked address would then need applying to any other similar code pattern found in Xen. Which is (I hope obviously) not a viable route. Instead code running before address translation is enable needs to be extra careful in what code and data items it accesses, and how.
Jan
Quoted 10 lines
To force using addresses related to
load_addrinbug_frames, it is
necessary to declare a variable with getting an address of &__{start,stop}bug_frames{,{0-3}} thereby it will generate the code:2002c2: 00001797 auipc a5,0x1 2002c6: d3e78793 addi a5,a5,-706 # 201000 <__start_bug_frames> 2002ca: faf43c23 sd a5,-72(s0) 2002ce: 00001797 auipc a5,0x1 2002d2: d3a78793 addi a5,a5,-710 #201008 <
__stop_bug_frames_
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Quoted 86 lines · Jan Beulich
Quoted 18 lines · Oleksii
Actually after my latest experiments it looks that we don't need to calculate that things at all because for RISC-V it is used everywhere
PC-relative access.
Thereby it doesn't matter what is an address where Xen was loaded and
linker address.
Right now I found only to cases which aren't PC-relative. Please look at the patch below:xen/arch/riscv/include/asm/config.h +0
-0@@ -39,7 +39,7 @@name:
#endifxen/arch/riscv/include/asm/config.h line 42
Quoted 2 lines
xen/arch/riscv/include/asm/config.h +1
-1-#define XEN_VIRT_START _AT(UL, 0x80200000) +#define XEN_VIRT_START _AT(UL, 0x00200000)I think this wants to remain the address where Xen actually runs, and where Xen is linked to. This ...
Quoted 15 lines
xen/arch/riscv/traps.c +0
-0@@ -123,8 +123,14 @@ int do_bug_frame(const struct cpu_user_regs *regs, vaddr_t pc) const char *filename, *predicate; int lineno;
- static const struct bug_frame* bug_frames[] = {
- &__start_bug_frames[0],
+ /*
+ * force fillbug_framesarray using auipc/addi instructions to
+ * make addresses inbug_framesPC-relative.
+ */Quoted 8 lines
+ const struct
bug_frame* force = (const structbug_frame*) &__start_bug_frames[0];
+
+ const struct bug_frame* bug_frames[] = {
+ force,
&__stop_bug_frames_0[0], &__stop_bug_frames_1[0], &__stop_bug_frames_2[0],... array would better be static anyway, and ...
Quoted 18 lines
The changes related to <
asm/config.h> are only to makelinker_addr!=load_address. So:
1. The first issue withcpu0_boot_stackin thehead.Sfile. When we do:
la sp,cpu0_boot_stack
Pseudo-instruction la will be transformed to auipc/addi OR auipc/l{w|d}.
It depends on an option: nopic, pic. [1]
So the solution can be the following:
* As it is done in the patch: add to the start ofhead.S".option
nopic"
* Change la to lla thereby it will be always generated "auipc/addi"
to get an address of variable.Quoted 16 lines
2. The second issue is with the code in
do_bug_frame()withbug_frames
array:
const struct bug_frame* bug_frames[] = { &__start_bug_frames[0],
&__stop_bug_frames_0[0], &__stop_bug_frames_1[0], &__stop_bug_frames_2[0], &__stop_bug_frames_3[0],
};
In this case &{start,stop}bug_frames{,{0-3}} will be changed to
linker address. In case of whenload_addris0x80200000andlinker_addr
is0x00200000then &{start,stop}bug_frames{,{0-3}} will be equal to0x00200000+ X.
Quoted 6 lines
... this "solution" to a problem you introduce by wrongly modifying the linked address would then need applying to any other similar code pattern found in Xen. Which is (I hope obviously) not a viable route. Instead code running before address translation is enable needs to be extra careful in what code and data items it accesses, and how.
I modified the linked address only for the experiment ( when load_addr != linker_addr to emulate situation Julien told me about), so it's not something I planned to send as a part of the final patch, and I probably forgot to mention that in my previous mail.
It is only one place where we have to do a kind of 'force' and is needed to make the current state of RISC-V Xen work in case we don't have MMU enabled yet and linker_addr != load_addr. All other cases where it is used something from the range (linker_start, linker_end) will be managed by MMU.
If we can't use mentioned above solution, we still need to handle the situation when linker_addr != load_addr and MMU isn't enabled yet. Other options to do that:
1. add phys_offset ( | load_addr - linker_addr | ) everywhere where bug_frames array is used: bug_frames[id] + phys_offset
2. Check somewhere at the start if linker_addr != load_addr, then throw an error and panic().
Other options might exist. So I would appreciate it if you could suggest me some.
Could you let me know if any options are suitable for handling a case when linker_addr?
Quoted 4 lines
Quoted 4 lines
To force using addresses related to load_addr in
bug_frames, it is
necessary to declare a variable with getting an address of &__{start,stop}bug_frames{,{0-3}} thereby it will generate the
Quoted 9 lines
Quoted 8 lines
code:
2002c2: 00001797 auipc a5,0x1 2002c6: d3e78793 addi a5,a5,-706 # 201000 <__start_bug_frames>
2002ca: faf43c23 sd a5,-72(s0) 2002ce: 00001797 auipc a5,0x1 2002d2: d3a78793 addi a5,a5,-710 # 201008 <__stop_bug_frames_
~ Oleksii
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Quoted 104 lines · Oleksii
Quoted 86 lines · Jan Beulich
Quoted 18 lines · Oleksii
Actually after my latest experiments it looks that we don't need to calculate that things at all because for RISC-V it is used everywhere
PC-relative access.
Thereby it doesn't matter what is an address where Xen was loaded and
linker address.
Right now I found only to cases which aren't PC-relative. Please look at the patch below:xen/arch/riscv/include/asm/config.h +0
-0@@ -39,7 +39,7 @@name:
#endifxen/arch/riscv/include/asm/config.h line 42
Quoted 2 lines
xen/arch/riscv/include/asm/config.h +1
-1-#define XEN_VIRT_START _AT(UL, 0x80200000) +#define XEN_VIRT_START _AT(UL, 0x00200000)I think this wants to remain the address where Xen actually runs, and where Xen is linked to. This ...
Quoted 15 lines
xen/arch/riscv/traps.c +0
-0@@ -123,8 +123,14 @@ int do_bug_frame(const struct cpu_user_regs *regs, vaddr_t pc) const char *filename, *predicate; int lineno;
- static const struct bug_frame* bug_frames[] = {
- &__start_bug_frames[0],
+ /*
+ * force fillbug_framesarray using auipc/addi instructions to
+ * make addresses inbug_framesPC-relative.
+ */Quoted 8 lines
+ const struct
bug_frame* force = (const structbug_frame*) &__start_bug_frames[0];
+
+ const struct bug_frame* bug_frames[] = {
+ force,
&__stop_bug_frames_0[0], &__stop_bug_frames_1[0], &__stop_bug_frames_2[0],... array would better be static anyway, and ...
Quoted 18 lines
The changes related to <
asm/config.h> are only to makelinker_addr!=load_address. So:
1. The first issue withcpu0_boot_stackin thehead.Sfile. When we do:
la sp,cpu0_boot_stack
Pseudo-instruction la will be transformed to auipc/addi OR auipc/l{w|d}.
It depends on an option: nopic, pic. [1]
So the solution can be the following:
* As it is done in the patch: add to the start ofhead.S".option
nopic"
* Change la to lla thereby it will be always generated "auipc/addi"
to get an address of variable.Quoted 16 lines
2. The second issue is with the code in
do_bug_frame()withbug_frames
array:
const struct bug_frame* bug_frames[] = { &__start_bug_frames[0],
&__stop_bug_frames_0[0], &__stop_bug_frames_1[0], &__stop_bug_frames_2[0], &__stop_bug_frames_3[0],
};
In this case &{start,stop}bug_frames{,{0-3}} will be changed to
linker address. In case of whenload_addris0x80200000andlinker_addr
is0x00200000then &{start,stop}bug_frames{,{0-3}} will be equal to0x00200000+ X.Quoted 6 lines
... this "solution" to a problem you introduce by wrongly modifying the linked address would then need applying to any other similar code pattern found in Xen. Which is (I hope obviously) not a viable route. Instead code running before address translation is enable needs to be extra careful in what code and data items it accesses, and how.
I modified the linked address only for the experiment ( when
load_addr!=linker_addrto emulate situation Julien told me about), so it's not something I planned to send as a part of the final patch, and I probably forgot to mention that in my previous mail.It is only one place where we have to do a kind of 'force' and is needed to make the current state of RISC-V Xen work in case we don't have MMU enabled yet and
linker_addr!=load_addr. All other cases where it is used something from the range (linker_start,linker_end) will be managed by MMU.
Quoted 5 lines
If we can't use mentioned above solution, we still need to handle the situation when
linker_addr!=load_addrand MMU isn't enabled yet. Other options to do that:
1. addphys_offset( |load_addr-linker_addr| ) everywhere wherebug_framesarray is used: bug_frames[id] +phys_offset
Well, that again special cases a certain data structure. As said before, you need to be very careful with any C code involved before translation is enabled. Unless you want to retain relocations (so you can "move" from load-time to link time addresses alongside enabling translation, like we do on x86 in xen.efi), you want to constrain code paths as much as possible. One approach is to move enabling of translation to early assembly code (like we do on x86 for xen.gz). The other is to amend involved code paths with something like what you say above.
Quoted 2 lines
2. Check somewhere at the start if
linker_addr!=load_addr, then throw an error andpanic().
That's not really an option if the boot loader isn't required to place the image at its linked address (which would be odd if translation isn't expected to be enabled yet at that point). Plus no matter what linked address you choose, I guess there may be systems where that address range simply isn't (fully) populated with RAM.
Quoted 5 lines
Other options might exist. So I would appreciate it if you could suggest me some.
Could you let me know if any options are suitable for handling a case when
linker_addr?
Main question is how tied you are to doing this in C. x86 and both Arm flavors do it in assembly, with - as said - the exception of x86's xen.efi where instead we retain (or generate) and process base relocations (see efi_arch_relocate_image(), called by efi_arch_post_exit_boot() immediately before switching to the "real" page tables).
Jan
Re: [PATCH v1 2/3] xen/riscv: setup initial pagetables
Quoted 159 lines · Jan Beulich
Quoted 121 lines · Oleksii
Quoted 95 lines · Jan Beulich
Quoted 20 lines · Oleksii
Actually after my latest experiments it looks that we don't need to
calculate that things at all because for RISC-V it is used everywhere
PC-relative access.
Thereby it doesn't matter what is an address where Xen was loaded
and
linker address.
Right now I found only to cases which aren't PC-relative. Please look at the patch below:xen/arch/riscv/include/asm/config.h +0
-0@@ -39,7 +39,7 @@name:
#endifxen/arch/riscv/include/asm/config.h line 42
Quoted 2 lines
xen/arch/riscv/include/asm/config.h +1
-1-#define XEN_VIRT_START _AT(UL, 0x80200000) +#define XEN_VIRT_START _AT(UL, 0x00200000)I think this wants to remain the address where Xen actually runs, and
where Xen is linked to. This ...Quoted 18 lines
xen/arch/riscv/traps.c +0
-0@@ -123,8 +123,14 @@ int do_bug_frame(const struct
cpu_user_regs
*regs,vaddr_tpc)
const char *filename, *predicate;
int lineno;
- static const struct bug_frame* bug_frames[] = { - &__start_bug_frames[0],
+ /*
+ * force fillbug_framesarray using auipc/addi instructions
to
+ * make addresses inbug_framesPC-relative.
+ */
+ const structbug_frame* force = (const structbug_frameQuoted 8 lines
*)
&__start_bug_frames[0];
+
+ const struct bug_frame* bug_frames[] = {
+ force,
&__stop_bug_frames_0[0],
&__stop_bug_frames_1[0],
&__stop_bug_frames_2[0],... array would better be static anyway, and ...
Quoted 29 lines
The changes related to <
asm/config.h> are only to makelinker_addr
!=load_address. So:
1. The first issue withcpu0_boot_stackin thehead.Sfile. When we
do:
la sp,cpu0_boot_stackPseudo-instruction la will be transformed to auipc/addi OR auipc/l{w|d}.
It depends on an option: nopic, pic. [1]
So the solution can be the following:
* As it is done in the patch: add to the start ofhead.S".option
nopic"
* Change la to lla thereby it will be always generated "auipc/addi"
to get an address of variable.2. The second issue is with the code in
do_bug_frame()withbug_frames
array:
const struct bug_frame* bug_frames[] = { &__start_bug_frames[0], &__stop_bug_frames_0[0], &__stop_bug_frames_1[0], &__stop_bug_frames_2[0], &__stop_bug_frames_3[0],Quoted 8 lines
};
In this case &{start,stop}bug_frames{,{0-3}} will be changed to
linker address. In case of whenload_addris0x80200000andlinker_addr
is0x00200000then &{start,stop}bug_frames{,{0-3}} will be equal to0x00200000+ X.Quoted 10 lines
... this "solution" to a problem you introduce by wrongly modifying
the linked address would then need applying to any other similar code
pattern found in Xen. Which is (I hope obviously) not a viable route.
Instead code running before address translation is enable needs to be
extra careful in what code and data items it accesses, and how.I modified the linked address only for the experiment ( when
load_addr
!=linker_addrto emulate situation Julien told me about), so it's not
something I planned to send as a part of the final patch, and I probably forgot to mention that in my previous mail.It is only one place where we have to do a kind of 'force' and is needed to make the current state of RISC-V Xen work in case we don't
have MMU enabled yet andlinker_addr!=load_addr. All other cases where it is used something from the range (linker_start,linker_end)
will be managed by MMU.Quoted 6 lines
If we can't use mentioned above solution, we still need to handle the
situation whenlinker_addr!=load_addrand MMU isn't enabled yet. Other options to do that:
1. addphys_offset( |load_addr-linker_addr| ) everywhere wherebug_framesarray is used: bug_frames[id] +phys_offsetWell, that again special cases a certain data structure. As said before,
you need to be very careful with any C code involved before translation
is enabled. Unless you want to retain relocations (so you can "move" from load-time to link time addresses alongside enabling translation, like we do on x86 in xen.efi), you want to constrain code paths as much
as possible. One approach is to move enabling of translation to early assembly code (like we do on x86 for xen.gz). The other is to amend involved code paths with something like what you say above.Quoted 3 lines
2. Check somewhere at the start if
linker_addr!=load_addr, then throw
an error andpanic().That's not really an option if the boot loader isn't required to place
the image at its linked address (which would be odd if translation isn't expected to be enabled yet at that point). Plus no matter what linked address you choose, I guess there may be systems where that address range simply isn't (fully) populated with RAM.Quoted 6 lines
Other options might exist. So I would appreciate it if you could suggest me some.
Could you let me know if any options are suitable for handling a case
whenlinker_addr?
Quoted 6 lines
Main question is how tied you are to doing this in C. x86 and both Arm flavors do it in assembly, with - as said - the exception of x86's xen.efi where instead we retain (or generate) and process base relocations (see
efi_arch_relocate_image(), called byefi_arch_post_exit_boot()immediately before switching to the "real" page tables).
Thanks for the clarification.
I will look at xen.efi & xen.gz, but I think I will follow scenario 1 or similar to it ( as I did in the path diff with the introduction of force variable) for now, it is required only to make addresses relative to phys_offset in one array.
After that, I will enable MMU, and it won't be necessary to add phys_offset or make similar changes to the introduction of force variable mentioned in the patch diff.
~ Oleksii
PATCH 3/3 automation: update RISC-V smoke test
In reply to Oleksii KurochkoThe smoke test was updated to verify that MMU has been enabled.
--- automation/scripts/qemu-smoke-riscv64.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
automation/scripts/qemu-smoke-riscv64.sh +1 -1
@@ -16,5 +16,5 @@ qemu-system-riscv64 \ |& tee smoke.serial set -e -(grep -q "WARN is most likely working" smoke.serial) || exit 1 +(grep -q "MMU has been enabled" smoke.serial) || exit 1 exit 0
Signature · 1 line
2.39.0
Re: [PATCH v1 3/3] automation: update RISC-V smoke test
Quoted 12 lines · Oleksii Kurochko
The smoke test was updated to verify that MMU has been enabled.
Signed-off-by Oleksii Kurochko <[email protected]>--- automation/scripts/qemu-smoke-riscv64.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)automation/scripts/qemu-smoke-riscv64.sh +0
-0
automation/scripts/qemu-smoke-riscv64.sh qemu-system-riscv64 \ · line 20
Quoted 6 lines
automation/scripts/qemu-smoke-riscv64.sh +1
-1@@ -16,5 +16,5 @@ qemu-system-riscv64 \ |& tee smoke.serial set -e -(grep -q "WARN is most likely working" smoke.serial) || exit 1 +(grep -q "MMU has been enabled" smoke.serial) || exit 1 exit 0
There's a more simple way than this. I'll do a patch.
~Andrew
Re: [PATCH v1 3/3] automation: update RISC-V smoke test
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Oleksii Kurochko
The smoke test was updated to verify that MMU has been enabled.
Signed-off-by Oleksii Kurochko <[email protected]>---
automation/scripts/qemu-smoke-riscv64.sh | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
Quoted 11 lines
automation/scripts/qemu-smoke-riscv64.sh +0
-0@@ -16,5 +16,5 @@ qemu-system-riscv64 \|& tee smoke.serial
set -eautomation/scripts/qemu-smoke-riscv64.sh +1
-1-(grep -q "WARN is most likely working" smoke.serial) || exit 1 +(grep -q "MMU has been enabled" smoke.serial) || exit 1Quoted 1 line
exit 0
There's a more simple way than this. I'll do a patch.
Definitely your way is more simple. Thanks.
Quoted 2 lines
~Andrew
~ Oleksii
Quoted 23 lines · Oleksii Kurochko
The patch series introduces the following things:
1. Functionality to build the page tables for Xen that map the following:
* The physical location of Xen (where the bootloader loaded it)
* The link-time location of Xen (where the linker expected Xen's addresses to be.
2. Load the built page table into the SATP
3. Enables MMU.
4. Updates smoke test to grep message which should be printed after MMU is enabled.Oleksii Kurochko (3):
xen/riscv: introducesetup_initial_pages
xen/riscv: setup initial pagetables
automation: update RISC-V smoke testautomation/scripts/qemu-smoke-riscv64.sh | 2 +- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/mm.h | 9 + xen/arch/riscv/include/asm/page.h | 90 +++++++++ xen/arch/riscv/mm.c | 223
+++++++++++++++++++++++
xen/arch/riscv/setup.c | 11 ++
Quoted 5 lines
6 files changed, 335 insertions(+), 1 deletion(-)create mode 100644
xen/arch/riscv/include/asm/mm.hcreate mode 100644xen/arch/riscv/include/asm/page.hcreate mode 100644xen/arch/riscv/mm.c
I forgot to mention in cover letter that the patch series is based on top of 'introduce generic implementation of macros from bug.h' and 'RISCV basic exception handling implementation' patch series.