enable MMU for RISC-V
PATCH v8 1/5 xen/riscv: add VM space layout
In reply to Oleksii KurochkoAlso it was added explanation about ignoring of top VA bits
---
Changes in V8:
- Add "#ifdef RV_STAGE1_MODE == SATP_MODE_SV39" instead of "#ifdef SV39" in the comment to VM layout description.
- Update the upper bound of direct map area in VM layout description.
---
Changes in V7:
- Fix range of frametable range in RV64 layout.
- Add ifdef SV39 to the RV64 layout comment to make it explicit that description if for SV39 mode.
- Add missed row in the RV64 layout table.
---
Changes in V6:
- update comment above the RISCV-64 layout table
- add Slot column to the table with RISCV-64 Layout
- update RV-64 layout table.
---
Changes in V5:
* the patch was introduced in the current patch series.
--- xen/arch/riscv/include/asm/config.h | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
xen/arch/riscv/include/asm/config.h +36 -0
@@ -4,6 +4,42 @@#include <xen/const.h> #include <xen/page-size.h> +/* +* RISC-V64 Layout: +* +* #ifdef RV_STAGE1_MODE == SATP_MODE_SV39 +* +* From the riscv-privileged doc: +* When mapping between narrower and wider addresses, +* RISC-V zero-extends a narrower physical address to a wider size. +* The mapping between 64-bit virtual addresses and the 39-bit usable +* address space of Sv39 is not based on zero-extension but instead +* follows an entrenched convention that allows an OS to use one or +* a few of the most-significant bits of a full-size (64-bit) virtual +* address to quickly distinguish user and supervisor address regions. +* +* It means that: +* top VA bits are simply ignored for the purpose of translating to PA. +* +* ============================================================================ +* Start addr | End addr | Size | Slot |area description +* ============================================================================ +* FFFFFFFFC0800000 | FFFFFFFFFFFFFFFF |1016 MB | L2 511 | Unused +* FFFFFFFFC0600000 | FFFFFFFFC0800000 | 2 MB | L2 511 | Fixmap +* FFFFFFFFC0200000 | FFFFFFFFC0600000 | 4 MB | L2 511 | FDT +* FFFFFFFFC0000000 | FFFFFFFFC0200000 | 2 MB | L2 511 | Xen +* ... | 1 GB | L2 510 | Unused +* 0000003200000000 | 0000007f80000000 | 309 GB | L2 200-509 | Direct map +* ... | 1 GB | L2 199 | Unused +* 0000003100000000 | 00000031C0000000 | 3 GB | L2 196-198 | Frametable +* ... | 1 GB | L2 195 | Unused +* 0000003080000000 | 00000030C0000000 | 1 GB | L2 194 | VMAP +* ... | 194 GB | L2 0 - 193 | Unused +* ============================================================================ +* +* #endif +*/ +#if defined(CONFIG_RISCV_64 )# define LONG_BYTEORDER 3# define ELFSIZE 64
Signature · 1 line
2.40.1
PATCH v8 2/5 xen/riscv: introduce setup_initial_pages
In reply to Oleksii KurochkoThe idea was taken from xvisor but the following changes
were done:
* Use only a minimal part of the code enough to enable MMU
* rename {_}setup_initial_pagetables functions
* add an argument for setup_initial_mapping to have
an opportunity to make set PTE flags.
* update setup_initial_pagetables function to map sections with correct PTE flags.
* Rewrite enable_mmu() to C.
* map linker addresses range to load addresses range without 1:1 mapping. It will be 1:1 only in case when load_start_addr is equal to linker_start_addr.
* add safety checks such as:
* Xen size is less than page size
* linker addresses range doesn't overlap load addresses range
* Rework macros {THIRD,SECOND,FIRST,ZEROETH}_{SHIFT,MASK}
* change PTE_LEAF_DEFAULT to RW instead of RWX.
* Remove phys_offset as it is not used now
* Remove alignment of {map, pa}_start &= XEN_PT_LEVEL_MAP_MASK(0); in setup_inital_mapping() as they should be already aligned. Make a check that {map_pa}_start are aligned.
* Remove clear_pagetables() as initial pagetables will be zeroed during bss initialization
* Remove __attribute__((section(".entry")) for setup_initial_pagetables() as there is no such section in xen.lds.S
* Update the argument of pte_is_valid() to "const pte_t *p"
* Add check that Xen's load address is aligned at 4k boundary
* Refactor setup_initial_pagetables() so it is mapping linker address range to load address range. After setup needed permissions for specific section ( such as .text, .rodata, etc ) otherwise RW permission will be set by default.
* Add function to check that requested SATP_MODE is supported
Origin: [email protected]:xvisor/xvisor.git 9be2fdd7
---
Changes in V8:
- Add parentheses for lvl in pt_index() macros.
- introduce macros paddr_to_pfn() and pfn_to_paddr() and use them inside paddr_to_pte()/pte_to_paddr().
- Remove "__" in sfence_vma() and add blanks inside the parentheses of
asm volatile.
- Parenthesize the two & against the || at the start of setup_initial_mapping() function.
- Code style fixes.
---
Changes in V7:
- define CONFIG_PAGING_LEVELS=2 for SATP_MODE_SV32.
- update switch_stack_and_jump macros(): add constraint 'X' for fn,
memory clobber and wrap into do {} while ( false ).
- add noreturn to definition of enable_mmu().
- update pt_index() to "(pt_linear_offset(lvl, (va)) & VPN_MASK)".
- expand macros pte_to_addr()/addr_to_pte() in paddr_to_pte() and
pte_to_paddr() functions and after drop them.
- remove inclusion of <asm/config.h>.
- update commit message around definition of PGTBL_INITIAL_COUNT.
- remove PGTBL_ENTRY_AMOUNT and use PAGETABLE_ENTRIES instead.
- code style fixes
- remove permission argument of setup_initial_mapping() function
- remove calc_pgtbl_lvls_num() as it's not needed anymore after definition
of CONFIG_PAGING_LEVELS.
- introduce sfence_vma().
- remove satp_mode argument from check_pgtbl_mode_support() and use
RV_STAGE1_MODE directly instead.
- change .align to .p2align.
- drop inclusion of <asm/asm-offsets.h> from head.S. This change isn't
necessary for the current patch series.
---
Changes in V6:
- move PAGE_SHIFT, PADDR_BITS to the top of page-bits.h
- cast argument x of pte_to_addr() macros to paddr_t to avoid risk of overflow for RV32
- update type of num_levels from 'unsigned long' to 'unsigned int'
- define PGTBL_INITIAL_COUNT as ((CONFIG_PAGING_LEVELS - 1) + 1)
- update type of permission arguments. changed it from 'unsgined long' to 'unsigned int'
- fix code style
- switch while 'loop' to 'for' loop
- undef HANDLE_PGTBL
- clean root page table after MMU is disabled in check_pgtbl_mode_support() function
- align __bss_start properly
- remove unnecesssary const for paddr_to_pte, pte_to_paddr, pte_is_valid functions
- add switch_stack_and_jump macros and use it inside enable_mmu() before jump to cont_after_mmu_is_enabled() function
---
Changes in V5:
* Indent fields of pte_t struct
* Rename addr_to_pte() and ppn_to_paddr() to match their content
---
Changes in V4:
* use GB() macros instead of defining SZ_1G
* hardcode XEN_VIRT_START and add comment (ADDRESS_SPACE_END + 1 - GB(1))
* remove unnecessary 'asm' word at the end of #error
* encapsulate pte_t definition in a struct
* rename addr_to_ppn() to ppn_to_paddr().
* change type of paddr argument from const unsigned long to paddr_t
* pte_to_paddr() update prototype.
* calculate size of Xen binary based on an amount of page tables
* use unsgined int instead of 'uint32_t' instead of uint32_t as
their use isn't warranted.
* remove extern of bss_{start,end} as they aren't used in mm.c anymore
* fix code style
* add argument for HANDLE_PGTBL macros instead of curr_lvl_num variable
* make enable_mmu() as noinline to prevent under link-time optimization
because of the nature of enable_mmu()
* add function to check that SATP_MODE is supported.
* update the commit message
* update setup_initial_pagetables to set correct PTE flags in one pass
instead of calling setup_pte_permissions after setup_initial_pagetables()
as setup_initial_pagetables() isn't used to change permission flags.
---
Changes in V3:
- update definition of pte_t structure to have a proper size of pte_t
in case of RV32.
- update asm/mm.h with new functions and remove unnecessary 'extern'.
- remove LEVEL_* macros as only XEN_PT_LEVEL_* are enough.
- update paddr_to_pte() to receive permissions as an argument.
- add check that map_start & pa_start is properly aligned.
- move defines PAGETABLE_ORDER, PAGETABLE_ENTRIES, PTE_PPN_SHIFT to <asm/page-bits.h>
- Rename PTE_SHIFT to PTE_PPN_SHIFT
- refactor setup_initial_pagetables: map all LINK addresses to LOAD addresses
and after setup PTEs permission for sections; update check that linker
and load addresses don't overlap.
- refactor setup_initial_mapping: allocate pagetable 'dynamically' if it is necessary.
- rewrite enable_mmu in C; add the check that map_start and pa_start are
aligned on 4k boundary.
- update the comment for setup_initial_pagetable funcion
- Add RV_STAGE1_MODE to support different MMU modes
- set XEN_VIRT_START very high to not overlap with load address range
- align bss section
---
Changes in V2:
* update the commit message:
* Remove {ZEROETH,FIRST,...}_{SHIFT,MASK, SIZE,...} and
introduce instead of them XEN_PT_LEVEL_*() and LEVEL_*
* Rework pt_linear_offset() and pt_index based on XEN_PT_LEVEL_*()
* Remove clear_pagetables() functions as pagetables were zeroed during
.bss initialization
* Rename _setup_initial_pagetables() to setup_initial_mapping()
* Make PTE_DEFAULT equal to RX.
* Update prototype of setup_initial_mapping(..., bool writable) -> setup_initial_mapping(..., UL flags)
* Update calls of setup_initial_mapping according to new prototype
* Remove unnecessary call of:
_setup_initial_pagetables(..., load_addr_start, load_addr_end, load_addr_start, ...)
* Define index* in the loop of setup_initial_mapping
* Remove attribute "__attribute__((section(".entry")))" for setup_initial_pagetables() as we don't have such section
* make arguments of paddr_to_pte() and pte_is_valid() as const.
* make xen_second_pagetable static.
* use <xen/kernel.h> instead of declaring extern unsigned long _stext, 0etext, _srodata, _erodata
* update 'extern unsigned long __init_begin' to 'extern unsigned long __init_begin[]'
* use aligned() instead of "__attribute__((__aligned__(PAGE_SIZE)))"
* set __section(".bss.page_aligned") for page tables arrays
* fix identatations
* Change '__attribute__((section(".entry")))' to '__init'
* Remove phys_offset as it isn't used now.
* Remove alignment of {map, pa}_start &= XEN_PT_LEVEL_MAP_MASK(0); in setup_inital_mapping() as they should be already aligned.
* Remove clear_pagetables() as initial pagetables will be
zeroed during bss initialization
* Remove __attribute__((section(".entry")) for setup_initial_pagetables()
as there is no such section in xen.lds.S
* Update the argument of pte_is_valid() to "const pte_t *p"
--- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/config.h | 14 +- xen/arch/riscv/include/asm/current.h | 11 + xen/arch/riscv/include/asm/mm.h | 14 ++ xen/arch/riscv/include/asm/page-bits.h | 10 + xen/arch/riscv/include/asm/page.h | 61 ++++++ xen/arch/riscv/include/asm/processor.h | 5 + xen/arch/riscv/mm.c | 277 +++++++++++++++++++++++++ xen/arch/riscv/setup.c | 11 + xen/arch/riscv/xen.lds.S | 3 + 10 files changed, 406 insertions(+), 1 deletion(-) create mode 100644 xen/arch/riscv/include/asm/current.h create mode 100644 xen/arch/riscv/include/asm/mm.h create mode 100644 xen/arch/riscv/include/asm/page.h create mode 100644 xen/arch/riscv/mm.c
xen/arch/riscv/Makefile +1 -0
@@ -1,5 +1,6 @@ obj-$(CONFIG_EARLY_PRINTK) += early_printk.o obj-y += entry.o +obj-y += mm.o obj-$(CONFIG_RISCV_64) += riscv64/ obj-y += sbi.o obj-y += setup.o
xen/arch/riscv/include/asm/config.h +13 -1
@@ -75,12 +75,24 @@ name:#endif -#define XEN_VIRT_START _AT(UL, 0x80200000) +#ifdef CONFIG_RISCV_64 +#define XEN_VIRT_START 0xFFFFFFFFC0000000/* (_AC(-1, UL) + 1 - GB(1)) */ +#else +#error "RV32 isn't supported" +#endif #define SMP_CACHE_BYTES (1 << 6)#define STACK_SIZE PAGE_SIZE +#ifdef CONFIG_RISCV_64 +#define CONFIG_PAGING_LEVELS 3 +#define RV_STAGE1_MODE SATP_MODE_SV39 +#else +#define CONFIG_PAGING_LEVELS 2 +#define RV_STAGE1_MODE SATP_MODE_SV32 +#endif +#endif /* __RISCV_CONFIG_H__ */ /* * Local variables:
xen/arch/riscv/include/asm/current.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef __ASM_CURRENT_H +#define __ASM_CURRENT_H + +#define switch_stack_and_jump (stack, fn) do { \ + asm volatile ( \ + "mv sp, %0\n" \ + "j " #fn :: "r" (stack), "X" (fn) : "memory" ); \ + unreachable(); \ +} while ( false ) + +#endif /* __ASM_CURRENT_H */
xen/arch/riscv/include/asm/mm.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef _ASM_RISCV_MM_H +#define _ASM_RISCV_MM_H + +#include <asm/page-bits.h> + +#define pfn_to_paddr (pfn) ((paddr_t)(pfn) << PAGE_SHIFT) +#define paddr_to_pfn (pa) ((unsigned long)((pa) >> PAGE_SHIFT)) + +void setup_initial_pagetables (void ); + +void enable_mmu (void ); +void cont_after_mmu_is_enabled (void ); + +#endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/include/asm/page-bits.h +10 -0
@@ -4,4 +4,14 @@#define PAGE_SHIFT 12/* 4 KiB Pages */ #define PADDR_BITS 56/* 44-bit PPN */ +#ifdef CONFIG_RISCV_64 +#define PAGETABLE_ORDER (9) +#else /* CONFIG_RISCV_32 */ +#define PAGETABLE_ORDER (10) +#endif + +#define PAGETABLE_ENTRIES (1 << PAGETABLE_ORDER) + +#define PTE_PPN_SHIFT 10 +#endif /* __RISCV_PAGE_BITS_H__ */
xen/arch/riscv/include/asm/page.h new file +61 -0
61 lines not on this page · raw message
xen/arch/riscv/include/asm/processor.h +5 -0
@@ -69,6 +69,11 @@ static inline void die(void)wfi (); } +static inline void sfence_vma (void ) +{ + asmvolatile ("sfence.vma" :::"memory" ); +} +#endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */
xen/arch/riscv/mm.c new file +277 -0
277 lines not on this page · raw message
xen/arch/riscv/setup.c +11 -0
@@ -2,6 +2,7 @@#include <xen/init.h> #include <asm/early_printk.h> +#include <asm/mm.h> /* Xen stack for bringing up the first CPU. */ unsigned char __initdata cpu0_boot_stack[STACK_SIZE ]
@@ -26,3 +27,13 @@ void __init noreturn start_xen(unsigned long bootcpu_id,unreachable (); } + +void __init noreturncont_after_mmu_is_enabled (void ) +{ +early_printk ("All set up\n" ); + +for ( ;; ) + asmvolatile ("wfi" ); + +unreachable (); +}
xen/arch/riscv/xen.lds.S +3 -0
@@ -172,3 +172,6 @@ ASSERT(IS_ALIGNED(__bss_end, POINTER_ALIGN), "__bss_end is misaligned") ASSERT(!SIZEOF(.got), ".got non-empty") ASSERT(!SIZEOF(.got.plt), ".got.plt non-empty") + +ASSERT(_end - _start <= MB(2), "Xen too large for early-boot assumptions") +
Signature · 1 line
2.40.1
PATCH v8 4/5 xen/riscv: setup initial pagetables
In reply to Oleksii KurochkoThe patch does two thing:
1. Setup initial pagetables.
2. Enable MMU which end up with code in
cont_after_mmu_is_enabled()
---
Changes in V8:
- Nothing changed. Only rebase
---
Changes in V7:
- Nothing changed. Only rebase
---
Changes in V6:
- Nothing changed. Only rebase
---
Changes in V5:
- Nothing changed. Only rebase
---
Changes in V4:
- Nothing changed. Only rebase
---
Changes in V3:
- update the commit message that MMU is also enabled here
- remove early_printk("All set up\n") as it was moved to cont_after_mmu_is_enabled() function after MMU is enabled.
---
Changes in V2:
* Update the commit message
--- xen/arch/riscv/setup.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
xen/arch/riscv/setup.c +4 -1
@@ -21,7 +21,10 @@ void __init noreturn start_xen(unsigned long bootcpu_id, {early_printk ("Hello from C env\n" ); -early_printk ("All set up\n" ); +setup_initial_pagetables (); + +enable_mmu (); +for ( ;; ) asmvolatile ("wfi" );
Signature · 1 line
2.40.1
v8 cover enable MMU for RISC-V
The patch series introduces the following things:
1. Functionality to build the page tables for Xen that map link-time to physical-time location.
2. Check that Xen is less then page size.
3. Check that load addresses don't overlap with linker addresses.
4. Prepare things for proper switch to virtual memory world.
5. Load the built page table into the SATP
6. Enable MMU.
---
Changes in V8:
- Add "#ifdef RV_STAGE1_MODE == SATP_MODE_SV39" instead of "#ifdef SV39" in the comment to VM layout description.
- Update the upper bound of direct map area in VM layout description.
- Add parentheses for lvl in pt_index() macros.
- introduce macros paddr_to_pfn() and pfn_to_paddr() and use them inside paddr_to_pte()/pte_to_paddr().
- Remove "__" in sfence_vma() and add blanks inside the parentheses of asm volatile.
- Parenthesize the two & against the || at the start of setup_initial_mapping() function.
- Code style fixes.
- Remove ". = ALIGN(PAGE_SIZE);" before "*(.bss.page_aligned)" in vmlinux.lds.S file as .bss.page_aligned specifies proper alignment themselves.
---
Changes in V7:
- Fix range of frametable range in RV64 layout.
- Add ifdef SV39 to the RV64 layout comment to make it explicit that
description if for SV39 mode.
- Add missed row in the RV64 layout table.
- define CONFIG_PAGING_LEVELS=2 for SATP_MODE_SV32.
- update switch_stack_and_jump macros(): add constraint 'X' for fn,
memory clobber and wrap into do {} while ( false ).
- add noreturn to definition of enable_mmu().
- update pt_index() to "(pt_linear_offset(lvl, (va)) & VPN_MASK)".
- expand macros pte_to_addr()/addr_to_pte() in paddr_to_pte() and
pte_to_paddr() functions and after drop them.
- remove inclusion of <asm/config.h>.
- update commit message around definition of PGTBL_INITIAL_COUNT.
- remove PGTBL_ENTRY_AMOUNT and use PAGETABLE_ENTRIES instead.
- code style fixes
- remove permission argument of setup_initial_mapping() function
- remove calc_pgtbl_lvls_num() as it's not needed anymore after definition
of CONFIG_PAGING_LEVELS
- introduce sfence_vma()
- remove satp_mode argument from check_pgtbl_mode_support() and use
RV_STAGE1_MODE directly instead.
- change .align to .p2align
- drop inclusion of <asm/asm-offsets.h> from head.S. This change isn't
necessary for the current patch series.
- create a separate patch for xen.lds.S.
---
Changes in V6:
- update RV VM layout and related to it things
- move PAGE_SHIFT, PADDR_BITS to the top of page-bits.h
- cast argument x of pte_to_addr() macros to paddr_t to avoid risk of overflow for RV32
- update type of num_levels from 'unsigned long' to 'unsigned int'
- define PGTBL_INITIAL_COUNT as ((CONFIG_PAGING_LEVELS - 1) + 1)
- update type of permission arguments. changed it from 'unsgined long' to 'unsigned int'
- fix code style
- switch while 'loop' to 'for' loop
- undef HANDLE_PGTBL
- clean root page table after MMU is disabled in check_pgtbl_mode_support() function
- align __bss_start properly
- remove unnecesssary const for paddr_to_pte, pte_to_paddr, pte_is_valid functions
- add switch_stack_and_jump macros and use it inside enable_mmu() before jump to cont_after_mmu_is_enabled() function
---
Changes in V5:
* rebase the patch series on top of current staging
* Update cover letter: it was removed the info about the patches on which
MMU patch series is based as they were merged to staging
* add new patch with description of VM layout for RISC-V2
* Indent fields of pte_t struct
* Rename addr_to_pte() and ppn_to_paddr() to match their content
---
Changes in V4:
* use GB() macros instead of defining SZ_1G
* hardcode XEN_VIRT_START and add comment (ADDRESS_SPACE_END + 1 - GB(1))
* remove unnecessary 'asm' word at the end of #error
* encapsulate pte_t definition in a struct
* rename addr_to_ppn() to ppn_to_paddr().
* change type of paddr argument from const unsigned long to paddr_t
* pte_to_paddr() update prototype.
* calculate size of Xen binary based on an amount of page tables
* use unsgined int instead of 'uint32_t' instead of uint32_t as
their use isn't warranted.
* remove extern of bss_{start,end} as they aren't used in mm.c anymore
* fix code style
* add argument for HANDLE_PGTBL macros instead of curr_lvl_num variable
* make enable_mmu() as noinline to prevent under link-time optimization
because of the nature of enable_mmu()
* add function to check that SATP_MODE is supported.
* update the commit message
* update setup_initial_pagetables to set correct PTE flags in one pass
instead of calling setup_pte_permissions after setup_initial_pagetables()
as setup_initial_pagetables() isn't used to change permission flags.
---
Changes in V3:
* Update the cover letter message: the patch series isn't depend on
[ RISC-V basic exception handling implementation ] as it was decied
to enable MMU before implementation of exception handling. Also MMU
patch series is based on two other patches which weren't merged [1]
and [2]
- Update the commit message for [ [PATCH v3 1/3]
xen/riscv: introduce setup_initial_pages ].
- update definition of pte_t structure to have a proper size of pte_t in case of RV32.
- update asm/mm.h with new functions and remove unnecessary 'extern'.
- remove LEVEL_* macros as only XEN_PT_LEVEL_* are enough.
- update paddr_to_pte() to receive permissions as an argument.
- add check that map_start & pa_start is properly aligned.
- move defines PAGETABLE_ORDER, PAGETABLE_ENTRIES, PTE_PPN_SHIFT to <asm/page-bits.h>
- Rename PTE_SHIFT to PTE_PPN_SHIFT
- refactor setup_initial_pagetables: map all LINK addresses to LOAD addresses and after setup PTEs permission for sections; update check that linker and load addresses don't overlap.
- refactor setup_initial_mapping: allocate pagetable 'dynamically' if it is necessary.
- rewrite enable_mmu in C; add the check that map_start and pa_start are aligned on 4k boundary.
- update the comment for setup_initial_pagetable funcion
- Add RV_STAGE1_MODE to support different MMU modes.
- update the commit message that MMU is also enabled here
- set XEN_VIRT_START very high to not overlap with load address range
- align bss section
---
Changes in V2:
* Remove {ZEROETH,FIRST,...}_{SHIFT,MASK, SIZE,...} and
introduce instead of them XEN_PT_LEVEL_*() and LEVEL_*
* Rework pt_linear_offset() and pt_index based on XEN_PT_LEVEL_*()
* Remove clear_pagetables() functions as pagetables were zeroed during
.bss initialization
* Rename _setup_initial_pagetables() to setup_initial_mapping()
* Make PTE_DEFAULT equal to RX.
* Update prototype of setup_initial_mapping(..., bool writable) -> setup_initial_mapping(..., UL flags)
* Update calls of setup_initial_mapping according to new prototype
* Remove unnecessary call of:
_setup_initial_pagetables(..., load_addr_start, load_addr_end, load_addr_start, ...)
* Define index* in the loop of setup_initial_mapping
* Remove attribute "__attribute__((section(".entry")))" for setup_initial_pagetables() as we don't have such section
* make arguments of paddr_to_pte() and pte_is_valid() as const.
* use <xen/kernel.h> instead of declaring extern unsigned long _stext, 0etext, _srodata, _erodata
* update 'extern unsigned long __init_begin' to 'extern unsigned long __init_begin[]'
* use aligned() instead of "__attribute__((__aligned__(PAGE_SIZE)))"
* set __section(".bss.page_aligned") for page tables arrays
* fix identatations
* Change '__attribute__((section(".entry")))' to '__init'
* Remove alignment of {map, pa}_start &= XEN_PT_LEVEL_MAP_MASK(0); in setup_inital_mapping() as they should be already aligned.
* Remove clear_pagetables() as initial pagetables will be
zeroed during bss initialization
* Remove __attribute__((section(".entry")) for setup_initial_pagetables()
as there is no such section in xen.lds.S
* Update the argument of pte_is_valid() to "const pte_t *p"
* Remove patch "[PATCH v1 3/3] automation: update RISC-V smoke test" from the patch series as it was introduced simplified approach for RISC-V smoke test by Andrew Cooper
* Add patch [[xen/riscv: remove dummy_bss variable] as there is no any sense in dummy_bss variable after introduction of inittial page tables.
---
Oleksii Kurochko (5):
xen/riscv: add VM space layout
xen/riscv: introduce setup_initial_pages
xen/riscv: align __bss_start
xen/riscv: setup initial pagetables
xen/riscv: remove dummy_bss variable
xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/config.h | 50 ++++- xen/arch/riscv/include/asm/current.h | 11 + xen/arch/riscv/include/asm/mm.h | 14 ++ xen/arch/riscv/include/asm/page-bits.h | 10 + xen/arch/riscv/include/asm/page.h | 61 ++++++ xen/arch/riscv/include/asm/processor.h | 5 + xen/arch/riscv/mm.c | 277 +++++++++++++++++++++++++ xen/arch/riscv/setup.c | 22 +- xen/arch/riscv/xen.lds.S | 5 +- 10 files changed, 446 insertions(+), 10 deletions(-) create mode 100644 xen/arch/riscv/include/asm/current.h create mode 100644 xen/arch/riscv/include/asm/mm.h create mode 100644 xen/arch/riscv/include/asm/page.h create mode 100644 xen/arch/riscv/mm.c
Signature · 1 line
2.40.1
PATCH v8 3/5 xen/riscv: align __bss_start
bss clear cycle requires proper alignment of __bss_start.
ALIGN(PAGE_SIZE) before "*(.bss.page_aligned)" in xen.lds.S was removed as any contribution to "*(.bss.page_aligned)" have to specify proper aligntment themselves.
---
Changes in V8:
* Remove ". = ALIGN(PAGE_SIZE);" before "*(.bss.page_aligned)" in vmlinux.lds.S file as any contribution to .bss.page_aligned have to specify proper alignment themselves.
* Add "Fixes: cfa0409f7cbb ("xen/riscv: initialize .bss section")" to the commit message
* Add "Reviewed-by: Jan Beulich <[email protected]>" to the commit message
---
Changes in V7:
* the patch was introduced in the current patch series.
--- xen/arch/riscv/xen.lds.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/riscv/xen.lds.S +1 -1
@@ -137,9 +137,9 @@ SECTIONS __init_end = .; .bss : { /* BSS */ + . = ALIGN(POINTER_ALIGN); __bss_start = .; *(.bss.stack_aligned) - . = ALIGN(PAGE_SIZE); *(.bss.page_aligned) . = ALIGN(PAGE_SIZE); __per_cpu_start = .;
Signature · 1 line
2.40.1
PATCH v8 5/5 xen/riscv: remove dummy_bss variable
In reply to Oleksii KurochkoAfter introduction of initial pagetables there is no any sense in dummy_bss variable as bss section will not be empty anymore.
---
Changes in V8:
- Nothing changed. Only rebase
---
Changes in V7:
- Nothing changed. Only rebase
---
Changes in V6:
- Nothing changed. Only rebase
---
Changes in V5:
- Nothing changed. Only rebase
---
Changes in V4:
- Nothing changed. Only rebase
---
Changes in V3:
* patch was introduced in the current one patch series (v3).
---
Changes in V2:
* patch was introduced in the current one patch series (v2).
--- xen/arch/riscv/setup.c | 8 -------- 1 file changed, 8 deletions(-)
xen/arch/riscv/setup.c +0 -8
@@ -8,14 +8,6 @@unsigned char __initdata cpu0_boot_stack[STACK_SIZE ]__aligned(STACK_SIZE) ; -/* -* To be sure that .bss isn't zero. It will simplify code of -* .bss initialization. -* TODO: -* To be deleted when the first real .bss user appears -*/ -int dummy_bss __attribute__((unused)); -void __init noreturnstart_xen (unsigned long bootcpu_id,paddr_t dtb_addr) {
Signature · 1 line
2.40.1
Re: [PATCH v8 1/5] xen/riscv: add VM space layout
In reply to Oleksii Kurochkoxen/arch/riscv/include/asm/config.h line 10
Quoted 10 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +4
-0@@ -4,6 +4,42 @@#include <xen/const.h> #include <xen/page-size.h> +/* +* RISC-V64 Layout: +* +* #ifdef RV_STAGE1_MODE == SATP_MODE_SV39
Nit: #if please, not #ifdef. Also may I stress again that ideally this would be formatted such that when e.g. grep-ing for SATP_MODE_SV39, the matching line here would _not_ give the impression of being "a comment only" (making people possibly pay less attention)? My referral to the x86 way of doing things remains.
Quoted 11 lines
xen/arch/riscv/include/asm/config.h +11
-0+* From the riscv-privileged doc: +* When mapping between narrower and wider addresses, +* RISC-V zero-extends a narrower physical address to a wider size. +* The mapping between 64-bit virtual addresses and the 39-bit usable +* address space of Sv39 is not based on zero-extension but instead +* follows an entrenched convention that allows an OS to use one or +* a few of the most-significant bits of a full-size (64-bit) virtual +* address to quickly distinguish user and supervisor address regions. +* +* It means that: +* top VA bits are simply ignored for the purpose of translating to PA.
xen/arch/riscv/include/asm/config.h line 32
Quoted 10 lines
xen/arch/riscv/include/asm/config.h +10
-0+* +* ============================================================================ +* Start addr | End addr | Size | Slot |area description +* ============================================================================ +* FFFFFFFFC0800000 | FFFFFFFFFFFFFFFF |1016 MB | L2 511 | Unused +* FFFFFFFFC0600000 | FFFFFFFFC0800000 | 2 MB | L2 511 | Fixmap +* FFFFFFFFC0200000 | FFFFFFFFC0600000 | 4 MB | L2 511 | FDT +* FFFFFFFFC0000000 | FFFFFFFFC0200000 | 2 MB | L2 511 | Xen +* ... | 1 GB | L2 510 | Unused +* 0000003200000000 | 0000007f80000000 | 309 GB | L2 200-509 | Direct map
And another, yet more minor nit: Would be nice if all addresses here were spelled uniformly, i.e. also with upper vs lower case of hex digit letter consistent.
Jan
Quoted 13 lines
xen/arch/riscv/include/asm/config.h +10
-0+* ... | 1 GB | L2 199 | Unused +* 0000003100000000 | 00000031C0000000 | 3 GB | L2 196-198 | Frametable +* ... | 1 GB | L2 195 | Unused +* 0000003080000000 | 00000030C0000000 | 1 GB | L2 194 | VMAP +* ... | 194 GB | L2 0 - 193 | Unused +* ============================================================================ +* +* #endif +*/ +#if defined(CONFIG_RISCV_64 )# define LONG_BYTEORDER 3# define ELFSIZE 64
Re: [PATCH v8 2/5] xen/riscv: introduce setup_initial_pages
In reply to Oleksii KurochkoQuoted 34 lines · Oleksii Kurochko
The idea was taken from xvisor but the following changes
were done:
* Use only a minimal part of the code enough to enable MMU
* rename {_}setup_initial_pagetables functions
* add an argument forsetup_initial_mappingto have
an opportunity to make set PTE flags.
* updatesetup_initial_pagetablesfunction to map sections with correct PTE flags.
* Rewriteenable_mmu()to C.
* map linker addresses range to load addresses range without 1:1 mapping. It will be 1:1 only in case whenload_start_addris equal tolinker_start_addr.
* add safety checks such as:
* Xen size is less than page size
* linker addresses range doesn't overlap load addresses range
* Rework macros {THIRD,SECOND,FIRST,ZEROETH}_{SHIFT,MASK}
* changePTE_LEAF_DEFAULTto RW instead of RWX.
* Removephys_offsetas it is not used now
* Remove alignment of {map, pa}_start &=XEN_PT_LEVEL_MAP_MASK(0); insetup_inital_mapping()as they should be already aligned. Make a check that {map_pa}_start are aligned.
* Removeclear_pagetables()as initial pagetables will be zeroed during bss initialization
* Remove __attribute__((section(".entry")) forsetup_initial_pagetables()as there is no such section inxen.lds.S
* Update the argument ofpte_is_valid()to "constpte_t*p"
* Add check that Xen's load address is aligned at 4k boundary
* Refactorsetup_initial_pagetables()so it is mapping linker address range to load address range. After setup needed permissions for specific section ( such as .text, .rodata, etc ) otherwise RW permission will be set by default.
* Add function to check that requestedSATP_MODEis supported
Quoted 2 lines
Origin: [email protected]:xvisor/xvisor.git
9be2fdd7Signed-off-by Oleksii Kurochko <[email protected]>
with one more nit and a remark:
xen/arch/riscv/include/asm/page.h line 10
Quoted 13 lines
xen/arch/riscv/include/asm/page.h new file +10
-0@@ -0,0 +1,61 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#include <asm/mm.h> +#include <asm/page-bits.h> + +#define VPN_MASK ((unsigned long)(PAGETABLE_ENTRIES - 1))
Just as a remark - this could also be just
#define VPN_MASK (PAGETABLE_ENTRIES - 1UL)
Quoted 19 lines
xen/arch/riscv/mm.c new file +16
-0@@ -0,0 +1,277 @@ +#include <xen/compiler.h> +#include <xen/init.h> +#include <xen/kernel.h> +#include <xen/pfn.h> + +#include <asm/early_printk.h> +#include <asm/csr.h> +#include <asm/current.h> +#include <asm/mm.h> +#include <asm/page.h> +#include <asm/processor.h> + +struct mmu_desc { +unsigned int num_levels; +unsigned int pgtbl_count; +pte_t *next_pgtbl;
xen/arch/riscv/mm.c line 26
Quoted 10 lines
xen/arch/riscv/mm.c new file +10
-0+pte_t *pgtbl_base; +}; + +extern unsigned char cpu0_boot_stack[STACK_SIZE ]; + +#define PHYS_OFFSET ((unsigned long)_start - XEN_VIRT_START) +#define LOAD_TO_LINK (addr) ((addr) - PHYS_OFFSET) +#define LINK_TO_LOAD (addr) ((addr) + PHYS_OFFSET) + +
Nit: No double blank lines please.
Jan
Re: [PATCH v8 1/5] xen/riscv: add VM space layout
In reply to Jan BeulichQuoted 12 lines · Jan Beulich
Quoted 10 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/config.h +0
-0@@ -4,6 +4,42 @@ #include <xen/const.h> #include <xen/page-size.h>
+/*
+ * RISC-V64 Layout:
+ *
+ * #ifdefRV_STAGE1_MODE==SATP_MODE_SV39
Quoted 8 lines
Nit: #if please, not #ifdef. Also may I stress again that ideally this
would be formatted such that when e.g. grep-ing forSATP_MODE_SV39, the
matching line here would _not_ give the impression of being "a comment
only" (making people possibly pay less attention)? My referral to the x86 way of doing things remains.
Oh, I double checked the x86's config.h and understood ( my editor's colour scheme draw it as a comment so I missed that there is no '*' at the start if "#ifndef CONFIG_BIGMEM" line ) what you mean:
'*' should be removed at the start of "#ifdef RV..." line.
Quoted 41 lines
Quoted 32 lines
+ * From the riscv-privileged doc:
+ * When mapping between narrower and wider addresses,
+ * RISC-V zero-extends a narrower physical address to a wider size.
+ * The mapping between 64-bit virtual addresses and the 39-bit usable
+ * address space of Sv39 is not based on zero-extension but instead
+ * follows an entrenched convention that allows an OS to use one or
+ * a few of the most-significant bits of a full-size (64-bit) virtual
+ * address to quickly distinguish user and supervisor address regions.
+ *
+ * It means that:
+ * top VA bits are simply ignored for the purpose of translating to PA.
+ *
+ *
===================================================================
=========
+ * Start addr | End addr | Size | Slot |area description
+ *
===================================================================
=========
+ * FFFFFFFFC0800000 | FFFFFFFFFFFFFFFF |1016 MB | L2 511 | Unused
+ * FFFFFFFFC0600000 | FFFFFFFFC0800000 | 2 MB | L2 511 | FixmapQuoted 8 lines
+ * FFFFFFFFC0200000 | FFFFFFFFC0600000 | 4 MB | L2 511 | FDT
+ * FFFFFFFFC0000000 | FFFFFFFFC0200000 | 2 MB | L2 511 | Xen
+ * ... | 1 GB | L2 510 | Unused
+ * 0000003200000000 |0000007f80000000| 309 GB | L2 200-509 | Direct map
Quoted 3 lines
And another, yet more minor nit: Would be nice if all addresses here were spelled uniformly, i.e. also with upper vs lower case of hex digit letter consistent.
Sure. I will make all upper case. Thanks.
Quoted 13 lines
Quoted 13 lines
+ * ... | 1 GB | L2 199 | Unused
+ * 0000003100000000 | 00000031C0000000 | 3 GB | L2 196-198 | Frametable
+ * ... | 1 GB | L2 195 | Unused
+ * 0000003080000000 | 00000030C0000000 | 1 GB | L2 194 | VMAP
+ * ... | 194 GB | L2 0 - 193 | Unused
+ *
===================================================================
Quoted 9 lines
Quoted 8 lines
=========
+ *
+ * #endif
+ */
+
#ifdefined(CONFIG_RISCV_64)# define LONG_BYTEORDER 3 # define ELFSIZE 64
~ Oleksii