enable MMU for RISC-V
PATCH v4 2/3 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 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.39.2
PATCH v4 3/3 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 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.39.2
v4 cover enable MMU for RISC-V
The patch series is based on top patches [1] and [2]
The patch series introduces the following things:
1. Functionality to build the page tables for Xen that map link-time to physical-time location.
2. Check that Xen is less then page size.
3. Check that load addresses don't overlap with linker addresses.
4. Prepare things for proper switch to virtual memory world.
5. Load the built page table into the SATP
6. Enable MMU.
[1] https://lore.kernel.org/xen-devel/2785518800dce64fafb3096480a5ae4c4e026bcb.1678970065.git.oleksii.kurochko@gmail.com/
[2] https://lore.kernel.org/xen-devel/7c066d6dcc0618749df04785b34b93819148087d.1678970065.git.oleksii.kurochko@gmail.com/
---
Changes in 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 (3):
xen/riscv: introduce setup_initial_pages
xen/riscv: setup initial pagetables
xen/riscv: remove dummy_bss variable
xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/config.h | 12 +- xen/arch/riscv/include/asm/mm.h | 9 + xen/arch/riscv/include/asm/page-bits.h | 10 + xen/arch/riscv/include/asm/page.h | 65 +++++ xen/arch/riscv/mm.c | 319 +++++++++++++++++++++++++ xen/arch/riscv/riscv64/head.S | 2 + xen/arch/riscv/setup.c | 22 +- xen/arch/riscv/xen.lds.S | 4 + 9 files changed, 435 insertions(+), 9 deletions(-) create mode 100644 xen/arch/riscv/include/asm/mm.h create mode 100644 xen/arch/riscv/include/asm/page.h create mode 100644 xen/arch/riscv/mm.c
Signature · 1 line
2.39.2
PATCH v4 1/3 xen/riscv: introduce setup_initial_pages
The idea was taken from xvisor but the following changes
were done:
* Use only a minimal part of the code enough to enable MMU
* rename {_}setup_initial_pagetables functions
* add an argument for setup_initial_mapping to have
an opportunity to make set PTE flags.
* update setup_initial_pagetables function to map sections with correct PTE flags.
* Rewrite enable_mmu() to C.
* map linker addresses range to load addresses range without 1:1 mapping. It will be 1:1 only in case when load_start_addr is equal to linker_start_addr.
* add safety checks such as:
* Xen size is less than page size
* linker addresses range doesn't overlap load addresses range
* Rework macros {THIRD,SECOND,FIRST,ZEROETH}_{SHIFT,MASK}
* change PTE_LEAF_DEFAULT to RW instead of RWX.
* Remove phys_offset as it is not used now
* Remove alignment of {map, pa}_start &= XEN_PT_LEVEL_MAP_MASK(0); in setup_inital_mapping() as they should be already aligned. Make a check that {map_pa}_start are aligned.
* Remove clear_pagetables() as initial pagetables will be zeroed during bss initialization
* Remove __attribute__((section(".entry")) for setup_initial_pagetables() as there is no such section in xen.lds.S
* Update the argument of pte_is_valid() to "const pte_t *p"
* Add check that Xen's load address is aligned at 4k boundary
* Refactor setup_initial_pagetables() so it is mapping linker address range to load address range. After setup needed permissions for specific section ( such as .text, .rodata, etc ) otherwise RW permission will be set by default.
* Add function to check that requested SATP_MODE is supported
Origin: [email protected]:xvisor/xvisor.git 9be2fdd7
---
Changes in 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 | 12 +- xen/arch/riscv/include/asm/mm.h | 9 + xen/arch/riscv/include/asm/page-bits.h | 10 + xen/arch/riscv/include/asm/page.h | 65 +++++ xen/arch/riscv/mm.c | 319 +++++++++++++++++++++++++ xen/arch/riscv/riscv64/head.S | 2 + xen/arch/riscv/setup.c | 11 + xen/arch/riscv/xen.lds.S | 4 + 9 files changed, 432 insertions(+), 1 deletion(-) create mode 100644 xen/arch/riscv/include/asm/mm.h create mode 100644 xen/arch/riscv/include/asm/page.h create mode 100644 xen/arch/riscv/mm.c
xen/arch/riscv/Makefile +1 -0
@@ -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 +11 -1
@@ -39,12 +39,22 @@ 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 RV_STAGE1_MODE SATP_MODE_SV39 +#else +#define RV_STAGE1_MODE SATP_MODE_SV32 +#endif +#endif /* __RISCV_CONFIG_H__ */ /* * Local variables:
xen/arch/riscv/include/asm/mm.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef _ASM_RISCV_MM_H +#define _ASM_RISCV_MM_H + +void setup_initial_pagetables (void ); + +void enable_mmu (void ); +void cont_after_mmu_is_enabled (void ); + +#endif /* _ASM_RISCV_MM_H */
xen/arch/riscv/include/asm/page-bits.h +10 -0
@@ -1,6 +1,16 @@#ifndef __RISCV_PAGE_BITS_H__#define __RISCV_PAGE_BITS_H__ +#ifdef CONFIG_RISCV_64 +#define PAGETABLE_ORDER (9) +#else /* CONFIG_RISCV_32 */ +#define PAGETABLE_ORDER (10) +#endif + +#define PAGETABLE_ENTRIES (1 << PAGETABLE_ORDER) + +#define PTE_PPN_SHIFT 10 +#define PAGE_SHIFT 12/* 4 KiB Pages */ #define PADDR_BITS 56/* 44-bit PPN */
xen/arch/riscv/include/asm/page.h new file +65 -0
65 lines not on this page · raw message
xen/arch/riscv/mm.c new file +319 -0
319 lines not on this page · raw message
xen/arch/riscv/riscv64/head.S +2 -0
@@ -1,4 +1,5 @@ #include <asm/asm.h> +#include <asm/asm-offsets.h> #include <asm/riscv_encoding.h> .section .text.header, "ax", %progbits
@@ -32,3 +33,4 @@ ENTRY(start) add sp, sp, t0 tail start_xen +
xen/arch/riscv/setup.c +11 -0
@@ -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 +4 -0
@@ -136,6 +136,7 @@ SECTIONS . = ALIGN(POINTER_ALIGN); __init_end = .; + . = ALIGN(PAGE_SIZE); .bss : { /* BSS */ __bss_start = .; *(.bss.stack_aligned)
@@ -169,3 +170,6 @@ SECTIONS ASSERT(!SIZEOF(.got), ".got non-empty") ASSERT(!SIZEOF(.got.plt), ".got.plt non-empty") + +ASSERT(_end - _start <= MB(2), "Xen too large for early-boot assumptions") +
Signature · 1 line
2.39.2
Re: [PATCH v4 1/3] xen/riscv: introduce setup_initial_pages
Quoted 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 6 lines
Origin: [email protected]:xvisor/xvisor.git
9be2fdd7Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in V4:
* useGB()macros instead of definingSZ_1G
* hardcodeXEN_VIRT_STARTand add comment (ADDRESS_SPACE_END+ 1 - GB(1))
Perhaps in a separate patch, may I ask that you add - like x86 and Arm have it - a block comment to config.h laying out virtual address space use? Knowing what is planned to be put where (even if just vaguely, i.e. keeping open the option of changing the layout) is likely going to help with figuring whether this is a good placement.
Such a comment could then also be accompanied by mentioning that virtual address space really "wraps" at certain boundaries (due to the upper address bits simply being ignored). For an x86 person like me this is certainly unexpected / unusual behavior.
Quoted 201 lines
* remove unnecessary 'asm' word at the end of #error
* encapsulatepte_tdefinition in a struct
* renameaddr_to_ppn()toppn_to_paddr().
* change type of paddr argument from const unsigned long topaddr_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 ofuint32_tas
their use isn't warranted.
* remove extern of bss_{start,end} as they aren't used inmm.canymore
* fix code style
* add argument forHANDLE_PGTBLmacros instead ofcurr_lvl_numvariable
* makeenable_mmu()as noinline to prevent under link-time optimization
because of the nature ofenable_mmu()
* add function to check thatSATP_MODEis supported.
* update the commit message
* updatesetup_initial_pagetablesto set correct PTE flags in one pass
instead of callingsetup_pte_permissionsaftersetup_initial_pagetables()
assetup_initial_pagetables()isn't used to change permission flags.
---
Changes in V3:
- update definition ofpte_tstructure to have a proper size ofpte_t
in case of RV32.
- updateasm/mm.hwith new functions and remove unnecessary 'extern'.
- remove LEVEL_* macros as only XEN_PT_LEVEL_* are enough.
- updatepaddr_to_pte()to receive permissions as an argument.
- add check thatmap_start&pa_startis properly aligned.
- move definesPAGETABLE_ORDER,PAGETABLE_ENTRIES,PTE_PPN_SHIFTto <asm/page-bits.h>
- RenamePTE_SHIFTtoPTE_PPN_SHIFT
- refactorsetup_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.
- refactorsetup_initial_mapping: allocate pagetable 'dynamically' if it is necessary.
- rewriteenable_mmuin C; add the check thatmap_startandpa_startare
aligned on 4k boundary.
- update the comment forsetup_initial_pagetablefuncion
- AddRV_STAGE1_MODEto support different MMU modes
- setXEN_VIRT_STARTvery 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_*
* Reworkpt_linear_offset()andpt_indexbased on XEN_PT_LEVEL_*()
* Removeclear_pagetables()functions as pagetables were zeroed during
.bss initialization
* Rename_setup_initial_pagetables()tosetup_initial_mapping()
* MakePTE_DEFAULTequal to RX.
* Update prototype ofsetup_initial_mapping(..., bool writable)->setup_initial_mapping(..., UL flags)
* Update calls ofsetup_initial_mappingaccording to new prototype
* Remove unnecessary call of:
_setup_initial_pagetables(..., load_addr_start, load_addr_end, load_addr_start, ...)
* Define index* in the loop ofsetup_initial_mapping
* Remove attribute "__attribute__((section(".entry")))" forsetup_initial_pagetables()as we don't have such section
* make arguments ofpaddr_to_pte()andpte_is_valid()as const.
* makexen_second_pagetablestatic.
* 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[]'
* usealigned()instead of "__attribute__((__aligned__(PAGE_SIZE)))"
* set__section(".bss.page_aligned")for page tables arrays
* fix identatations
* Change '__attribute__((section(".entry")))' to '__init'
* Removephys_offsetas it isn't used now.
* Remove alignment of {map, pa}_start &=XEN_PT_LEVEL_MAP_MASK(0); insetup_inital_mapping()as they should be already 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"
---xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/config.h | 12 +- xen/arch/riscv/include/asm/mm.h | 9 + xen/arch/riscv/include/asm/page-bits.h | 10 + xen/arch/riscv/include/asm/page.h | 65 +++++ xen/arch/riscv/mm.c | 319 +++++++++++++++++++++++++ xen/arch/riscv/riscv64/head.S | 2 + xen/arch/riscv/setup.c | 11 + xen/arch/riscv/xen.lds.S | 4 + 9 files changed, 432 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.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/config.h +11
-1@@ -39,12 +39,22 @@ 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 RV_STAGE1_MODE SATP_MODE_SV39 +#else +#define RV_STAGE1_MODE SATP_MODE_SV32 +#endif +#endif /* __RISCV_CONFIG_H__ */ /* * Local variables: xen/arch/riscv/include/asm/mm.h new file +9
-0@@ -0,0 +1,9 @@ +#ifndef _ASM_RISCV_MM_H +#define _ASM_RISCV_MM_H + +void setup_initial_pagetables (void ); + +void enable_mmu (void ); +void cont_after_mmu_is_enabled (void ); + +#endif /* _ASM_RISCV_MM_H */ xen/arch/riscv/include/asm/page-bits.h +10
-0@@ -1,6 +1,16 @@#ifndef __RISCV_PAGE_BITS_H__#define __RISCV_PAGE_BITS_H__ +#ifdef CONFIG_RISCV_64 +#define PAGETABLE_ORDER (9) +#else /* CONFIG_RISCV_32 */ +#define PAGETABLE_ORDER (10) +#endif + +#define PAGETABLE_ENTRIES (1 << PAGETABLE_ORDER) + +#define PTE_PPN_SHIFT 10 +#define PAGE_SHIFT 12/* 4 KiB Pages */ #define PADDR_BITS 56/* 44-bit PPN */ xen/arch/riscv/include/asm/page.h new file +30
-0@@ -0,0 +1,65 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#define VPN_MASK ((unsigned long)(PAGETABLE_ENTRIES - 1)) + +#define XEN_PT_LEVEL_ORDER (lvl) ((lvl) * PAGETABLE_ORDER) +#define XEN_PT_LEVEL_SHIFT (lvl) (XEN_PT_LEVEL_ORDER(lvl) + PAGE_SHIFT) +#define XEN_PT_LEVEL_SIZE (lvl) (_AT(paddr_t, 1) << XEN_PT_LEVEL_SHIFT(lvl)) +#define XEN_PT_LEVEL_MAP_MASK (lvl) (~(XEN_PT_LEVEL_SIZE(lvl) - 1)) +#define XEN_PT_LEVEL_MASK (lvl) (VPN_MASK << XEN_PT_LEVEL_SHIFT(lvl)) + +#define PTE_VALID BIT(0, UL) +#define PTE_READABLE BIT(1, UL) +#define PTE_WRITABLE BIT(2, UL) +#define PTE_EXECUTABLE BIT(3, UL) +#define PTE_USER BIT(4, UL) +#define PTE_GLOBAL BIT(5, UL) +#define PTE_ACCESSED BIT(6, UL) +#define PTE_DIRTY BIT(7, UL) +#define PTE_RSW (BIT(8, UL) | BIT(9, UL)) + +#define PTE_LEAF_DEFAULT (PTE_VALID | PTE_READABLE | PTE_WRITABLE) +#define PTE_TABLE (PTE_VALID) + +/* Calculate the offsets into the pagetables for a given VA */ +#define pt_linear_offset (lvl, va) ((va) >> XEN_PT_LEVEL_SHIFT(lvl)) +
xen/arch/riscv/include/asm/page.h line 40
Quoted 10 lines
xen/arch/riscv/include/asm/page.h new file +10
-0+#define pt_index (lvl, va) pt_linear_offset(lvl, (va) & XEN_PT_LEVEL_MASK(lvl)) + +/* Page Table entry */ +typedef struct { +#ifdef CONFIG_RISCV_64 +uint64_t pte; +#else +uint32_t pte; +#endif +}pte_t ;
Please indent both field declarations accordingly.
xen/arch/riscv/include/asm/page.h line 42
Quoted 1 line
xen/arch/riscv/include/asm/page.h new file +1
-0+#define addr_to_pte (x) (((x) >> PTE_PPN_SHIFT) << PAGE_SHIFT)
This still looks to be converting _to_ an address, not to PTE layout, ...
xen/arch/riscv/include/asm/page.h line 46
Quoted 3 lines
xen/arch/riscv/include/asm/page.h new file +3
-0+/* Shift the VPN[x] or PPN[x] fields of a virtual or physical address +* to become the shifted PPN[x] fields of a page table entry */ +#define ppn_to_paddr (x) (((x) >> PAGE_SHIFT) << PTE_PPN_SHIFT)
... while this converts an address (not a ppn) to PTE layout (not a paddr). Getting the names of these helpers right is crucial for easy following of any code using them. To be honest, I'll stop reviewing here, because the names being wrong is just going to be too confusing.
Jan
Re: [PATCH v4 1/3] xen/riscv: introduce setup_initial_pages
Hi Jan,
Quoted 54 lines · Jan Beulich
Quoted 35 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 supportedQuoted 7 lines
Origin: [email protected]:xvisor/xvisor.git
9be2fdd7Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in V4:
* useGB()macros instead of definingSZ_1G
* hardcodeXEN_VIRT_STARTand add comment (ADDRESS_SPACE_END+ 1
- GB(1))Perhaps in a separate patch, may I ask that you add - like x86 and Arm
have it - a block comment toconfig.hlaying out virtual address space
use? Knowing what is planned to be put where (even if just vaguely, i.e.
keeping open the option of changing the layout) is likely going to help
with figuring whether this is a good placement.
Quoted 6 lines
Such a comment could then also be accompanied by mentioning that virtual address space really "wraps" at certain boundaries (due to the
upper address bits simply being ignored). For an x86 person like me this is certainly unexpected / unusual behavior.
Sure, it makes sense. I'll add that to new version of the patch series.
Quoted 257 lines
Quoted 240 lines
* remove unnecessary 'asm' word at the end of #error
* encapsulatepte_tdefinition in a struct
* renameaddr_to_ppn()toppn_to_paddr().
* change type of paddr argument from const unsigned long topaddr_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 ofuint32_tas their use isn't warranted.
* remove extern of bss_{start,end} as they aren't used inmm.canymore
* fix code style
* add argument forHANDLE_PGTBLmacros instead ofcurr_lvl_numvariable
* makeenable_mmu()as noinline to prevent under link-time optimization
because of the nature ofenable_mmu()
* add function to check thatSATP_MODEis supported.
* update the commit message
* updatesetup_initial_pagetablesto set correct PTE flags in one pass
instead of callingsetup_pte_permissionsaftersetup_initial_pagetables()
assetup_initial_pagetables()isn't used to change permission flags.
---
Changes in V3:
- update definition ofpte_tstructure to have a proper size ofpte_t
in case of RV32.
- updateasm/mm.hwith new functions and remove unnecessary 'extern'.
- remove LEVEL_* macros as only XEN_PT_LEVEL_* are enough.
- updatepaddr_to_pte()to receive permissions as an argument.
- add check thatmap_start&pa_startis properly aligned.
- move definesPAGETABLE_ORDER,PAGETABLE_ENTRIES,PTE_PPN_SHIFTto
<asm/page-bits.h>
- RenamePTE_SHIFTtoPTE_PPN_SHIFT
- refactorsetup_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.
- refactorsetup_initial_mapping: allocate pagetable 'dynamically' if it is
necessary.
- rewriteenable_mmuin C; add the check thatmap_startandpa_startare
aligned on 4k boundary.
- update the comment forsetup_initial_pagetablefuncion
- AddRV_STAGE1_MODEto support different MMU modes
- setXEN_VIRT_STARTvery 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_*
* Reworkpt_linear_offset()andpt_indexbased on XEN_PT_LEVEL_*()
* Removeclear_pagetables()functions as pagetables were zeroed during
.bss initialization
* Rename_setup_initial_pagetables()tosetup_initial_mapping()
* MakePTE_DEFAULTequal to RX.
* Update prototype ofsetup_initial_mapping(..., bool writable)->setup_initial_mapping(..., UL flags)
* Update calls ofsetup_initial_mappingaccording to new prototype
* Remove unnecessary call of:
_setup_initial_pagetables(..., load_addr_start, load_addr_end, load_addr_start, ...)
* Define index* in the loop ofsetup_initial_mapping
* Remove attribute "__attribute__((section(".entry")))" forsetup_initial_pagetables()
as we don't have such section
* make arguments ofpaddr_to_pte()andpte_is_valid()as const.
* makexen_second_pagetablestatic.
* 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[]'
* usealigned()instead of "__attribute__((__aligned__(PAGE_SIZE)))"
* set__section(".bss.page_aligned")for page tables arrays
* fix identatations
* Change '__attribute__((section(".entry")))' to '__init'
* Removephys_offsetas 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.
* 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"
---xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/config.h | 12 +- xen/arch/riscv/include/asm/mm.h | 9 +
xen/arch/riscv/include/asm/page-bits.h| 10 + xen/arch/riscv/include/asm/page.h | 65 +++++ xen/arch/riscv/mm.c | 319
+++++++++++++++++++++++++
xen/arch/riscv/riscv64/head.S | 2 + xen/arch/riscv/setup.c | 11 + xen/arch/riscv/xen.lds.S | 4 +9 files changed, 432 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.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/config.h +0
-0@@ -39,12 +39,22 @@name:
#endifxen/arch/riscv/include/asm/config.h +2
-1-#define XEN_VIRT_START _AT(UL, 0x80200000) +#ifdef CONFIG_RISCV_64 +#define XEN_VIRT_START 0xFFFFFFFFC0000000/* (_AC(-1, UL) + 1 - GB(1)) */
xen/arch/riscv/include/asm/config.h +3
-0+#else +#error "RV32 isn't supported" +#endif
#defineSMP_CACHE_BYTES(1 << 6)
#defineSTACK_SIZEPAGE_SIZExen/arch/riscv/include/asm/config.h +6
-0+#ifdef CONFIG_RISCV_64 +#define RV_STAGE1_MODE SATP_MODE_SV39 +#else +#define RV_STAGE1_MODE SATP_MODE_SV32 +#endif +#endif /*
__RISCV_CONFIG_H__*/
/*
* Local variables:xen/arch/riscv/include/asm/mm.h new file +9
-0@@ -0,0 +1,9 @@ +#ifndef _ASM_RISCV_MM_H +#define _ASM_RISCV_MM_H + +void setup_initial_pagetables (void ); + +void enable_mmu (void ); +void cont_after_mmu_is_enabled (void ); + +#endif /* _ASM_RISCV_MM_H */ xen/arch/riscv/include/asm/page-bits.h +0
-0@@ -1,6 +1,16 @@ #ifndef __RISCV_PAGE_BITS_H__ #define __RISCV_PAGE_BITS_H__xen/arch/riscv/include/asm/page-bits.h +10
-0+#ifdef CONFIG_RISCV_64 +#define PAGETABLE_ORDER (9) +#else /* CONFIG_RISCV_32 */ +#define PAGETABLE_ORDER (10) +#endif + +#define PAGETABLE_ENTRIES (1 << PAGETABLE_ORDER) + +#define PTE_PPN_SHIFT 10 +#define PAGE_SHIFT 12 /* 4 KiB Pages */ #define PADDR_BITS 56 /* 44-bit PPN */
xen/arch/riscv/include/asm/page.h new file +7
-0@@ -0,0 +1,65 @@ +#ifndef _ASM_RISCV_PAGE_H +#define _ASM_RISCV_PAGE_H + +#include <xen/const.h> +#include <xen/types.h> + +#define VPN_MASK ((unsignedlong)(PAGETABLE_ENTRIES - 1))
xen/arch/riscv/include/asm/page.h new file +3
-0+ +#define XEN_PT_LEVEL_ORDER (lvl) ((lvl) * PAGETABLE_ORDER) +#define XEN_PT_LEVEL_SHIFT (lvl) (XEN_PT_LEVEL_ORDER(lvl) +
PAGE_SHIFT)
+#define XEN_PT_LEVEL_SIZE(lvl) (_AT(paddr_t, 1)<<XEN_PT_LEVEL_SHIFT(lvl))
+#define XEN_PT_LEVEL_MAP_MASK(lvl) (~(XEN_PT_LEVEL_SIZE(lvl) - 1))
+#define XEN_PT_LEVEL_MASK(lvl) (VPN_MASK<<XEN_PT_LEVEL_SHIFT(lvl))xen/arch/riscv/include/asm/page.h new file +12
-0+ +#define PTE_VALID BIT(0, UL) +#define PTE_READABLE BIT(1, UL) +#define PTE_WRITABLE BIT(2, UL) +#define PTE_EXECUTABLE BIT(3, UL) +#define PTE_USER BIT(4, UL) +#define PTE_GLOBAL BIT(5, UL) +#define PTE_ACCESSED BIT(6, UL) +#define PTE_DIRTY BIT(7, UL) +#define PTE_RSW (BIT(8, UL) | BIT(9, UL)) + +#define PTE_LEAF_DEFAULT (PTE_VALID | PTE_READABLE |
PTE_WRITABLE)xen/arch/riscv/include/asm/page.h new file +4
-0+#define PTE_TABLE (PTE_VALID) + +/* Calculate the offsets into the pagetables for a given VA */ +#define pt_linear_offset (lvl, va) ((va) >>
XEN_PT_LEVEL_SHIFT(lvl))xen/arch/riscv/include/asm/page.h new file +2
-0+ +#define pt_index (lvl, va) pt_linear_offset(lvl, (va) &
XEN_PT_LEVEL_MASK(lvl))xen/arch/riscv/include/asm/page.h line 40
Quoted 9 lines
xen/arch/riscv/include/asm/page.h new file +9
-0+ +/* Page Table entry */ +typedef struct { +#ifdef CONFIG_RISCV_64 +uint64_t pte; +#else +uint32_t pte; +#endif +}pte_t ;Please indent both field declarations accordingly.
Quoted 1 line
+#define
addr_to_pte(x)(((x) >>PTE_PPN_SHIFT) <<PAGE_SHIFT)This still looks to be converting _to_ an address, not to PTE layout,
...
Quoted 10 lines
Quoted 4 lines
+/* Shift the VPN[x] or PPN[x] fields of a virtual or physical address
+ * to become the shifted PPN[x] fields of a page table entry */ +#defineppn_to_paddr(x)(((x) >>PAGE_SHIFT) <<PTE_PPN_SHIFT)... while this converts an address (not a ppn) to PTE layout (not a paddr). Getting the names of these helpers right is crucial for easy following of any code using them. To be honest, I'll stop reviewing here, because the names being wrong is just going to be too confusing.
You are right the names are confusing and should be renamed. Thanks.
~ Oleksii