Register Xen's load address as a boot module
cover Register Xen's load address as a boot module
This patch series registers Xen's load address as a boot module and introduce virt_to_maddr(), and drops LINK_TO_LOAD() to use virt_to_maddr() instead.
This patch series is based on the patch series: "device tree mapping" [1]
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
Oleksii Kurochko (3):
xen/riscv: implement virt_to_maddr() xen/riscv: switch LINK_TO_LOAD() to virt_to_maddr() xen/riscv: register Xen's load address as a boot module
xen/arch/riscv/include/asm/config.h | 4 ++++ xen/arch/riscv/include/asm/mm.h | 15 ++++++++++++++- xen/arch/riscv/mm.c | 9 ++++----- xen/arch/riscv/setup.c | 10 ++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-)
Signature · 1 line
2.46.1
PATCH 1/3 xen/riscv: implement virt_to_maddr()
Implement the virt_to_maddr() function to convert virtual addresses to machine addresses, including checks for address ranges such as the direct mapping area (DIRECTMAP_VIRT_START) and the Xen virtual address space. To implement this, the phys_offset variable is made accessible outside of riscv/mm.c.
--- xen/arch/riscv/include/asm/config.h | 4 ++++ xen/arch/riscv/include/asm/mm.h | 15 ++++++++++++++- xen/arch/riscv/mm.c | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-)
xen/arch/riscv/include/asm/config.h +4 -0
@@ -155,6 +155,10 @@#define IDENT_AREA_SIZE 64 +#ifndef __ASSEMBLY__ +extern unsigned long phys_offset; +#endif +#endif /* __RISCV_CONFIG_H__ */ /* * Local variables:
xen/arch/riscv/include/asm/mm.h +14 -1
@@ -28,7 +28,20 @@ static inline void *maddr_to_virt(paddr_t ma)return NULL ; } -#define virt_to_maddr (va) ({ BUG_ON("unimplemented"); 0; }) +static inline unsigned long virt_to_maddr (unsigned long va) +{ +ASSERT (va >= (DIRECTMAP_VIRT_START +DIRECTMAP_SIZE )); +if ((va >=DIRECTMAP_VIRT_START ) && + (va < (DIRECTMAP_VIRT_START +DIRECTMAP_SIZE ))) +return directmapoff_to_maddr (va -DIRECTMAP_VIRT_START ); + +BUILD_BUG_ON (XEN_VIRT_SIZE !=MB (2 )); +ASSERT (((long )va >> (PAGETABLE_ORDER +PAGE_SHIFT )) == + ((long )XEN_VIRT_START >> (PAGETABLE_ORDER +PAGE_SHIFT ))); + +return phys_offset + va; +} +#define virt_to_maddr (va) virt_to_maddr((unsigned long)(va))/* Convert between Xen-heap virtual addresses and machine frame numbers. */ #define __virt_to_mfn (va) mfn_x(maddr_to_mfn(virt_to_maddr(va)))
xen/arch/riscv/mm.c +1 -1
@@ -26,7 +26,7 @@ struct mmu_desc {pte_t *pgtbl_base; }; -static unsigned long __ro_after_init phys_offset; +unsigned long __ro_after_init phys_offset;#define LOAD_TO_LINK (addr) ((unsigned long)(addr) - phys_offset)#define LINK_TO_LOAD (addr) ((unsigned long)(addr) + phys_offset)
Signature · 1 line
2.46.1
Re: [PATCH v1 1/3] xen/riscv: implement virt_to_maddr()
xen/arch/riscv/include/asm/mm.h static inline void *maddr_to_virt(paddr_t ma) · line 36
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/mm.h +6
-1@@ -28,7 +28,20 @@ static inline void *maddr_to_virt(paddr_t ma)return NULL ; } -#define virt_to_maddr (va) ({ BUG_ON("unimplemented"); 0; }) +static inline unsigned long virt_to_maddr (unsigned long va) +{ +ASSERT (va >= (DIRECTMAP_VIRT_START +DIRECTMAP_SIZE )); +if ((va >=DIRECTMAP_VIRT_START ) && + (va < (DIRECTMAP_VIRT_START +DIRECTMAP_SIZE ))) +return directmapoff_to_maddr (va -DIRECTMAP_VIRT_START );
While the cover letter states a dependency on another series, I'm unable to spot directmapoff_to_maddr() in the tree or in that other series.
xen/arch/riscv/include/asm/mm.h static inline void *maddr_to_virt(paddr_t ma) · line 40
Quoted 3 lines
xen/arch/riscv/include/asm/mm.h +3
-0+BUILD_BUG_ON (XEN_VIRT_SIZE !=MB (2 )); +ASSERT (((long )va >> (PAGETABLE_ORDER +PAGE_SHIFT )) == + ((long )XEN_VIRT_START >> (PAGETABLE_ORDER +PAGE_SHIFT )));
What's the point of the casts here? va is unsigned long and XEN_VIRT_START is a literal number (which probably ought to have a UL suffix).
xen/arch/riscv/include/asm/mm.h static inline void *maddr_to_virt(paddr_t ma) · line 42
Quoted 1 line
xen/arch/riscv/include/asm/mm.h +1
-0+return phys_offset + va;
Don't you need to subtract XEN_VIRT_START here?
Jan
Re: [PATCH v1 1/3] xen/riscv: implement virt_to_maddr()
Quoted 22 lines · Jan Beulich
xen/arch/riscv/include/asm/mm.h static inline void *maddr_to_virt(paddr_t ma) · line 36
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/mm.h +6
-1@@ -28,7 +28,20 @@ static inline void *maddr_to_virt(paddr_t ma)return NULL ; } -#define virt_to_maddr (va) ({ BUG_ON("unimplemented"); 0; }) +static inline unsigned long virt_to_maddr (unsigned long va) +{ +ASSERT (va >= (DIRECTMAP_VIRT_START +DIRECTMAP_SIZE )); +if ((va >=DIRECTMAP_VIRT_START ) && + (va < (DIRECTMAP_VIRT_START +DIRECTMAP_SIZE ))) +return directmapoff_to_maddr (va -DIRECTMAP_VIRT_START );While the cover letter states a dependency on another series, I'm unable to spot
directmapoff_to_maddr()in the tree or in that other series.xen/arch/riscv/include/asm/mm.h static inline void *maddr_to_virt(paddr_t ma) · line 40
Quoted 3 lines
xen/arch/riscv/include/asm/mm.h +3
-0+BUILD_BUG_ON (XEN_VIRT_SIZE !=MB (2 )); +ASSERT (((long )va >> (PAGETABLE_ORDER +PAGE_SHIFT )) == + ((long )XEN_VIRT_START >> (PAGETABLE_ORDER +PAGE_SHIFT )));
Quoted 6 lines
What's the point of the casts here? va is unsigned long and
XEN_VIRT_STARTis a literal number (which probably ought to have a UL suffix).xen/arch/riscv/include/asm/mm.h static inline void *maddr_to_virt(paddr_t ma) · line 42
Quoted 1 line
xen/arch/riscv/include/asm/mm.h +1
-0+return phys_offset + va;Don't you need to subtract
XEN_VIRT_STARThere?
Oh, that subtraction is part of the calculation of phys_offset. The variable's name then doesn't really reflect its purpose, imo.
Jan
Re: [PATCH v1 1/3] xen/riscv: implement virt_to_maddr()
In reply to Jan BeulichQuoted 10 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/mm.h +0
-0@@ -28,7 +28,20 @@ static inline void *maddr_to_virt(paddr_t ma) return NULL; }xen/arch/riscv/include/asm/mm.h +2
-1-#define virt_to_maddr (va) ({ BUG_ON("unimplemented"); 0; }) +static inline unsigned long virt_to_maddr (unsigned long va) +{
Quoted 8 lines
Quoted 4 lines
+
ASSERT(va >= (DIRECTMAP_VIRT_START + DIRECTMAP_SIZE)); + if ((va >=DIRECTMAP_VIRT_START) &&
+ (va < (DIRECTMAP_VIRT_START+DIRECTMAP_SIZE))) + returndirectmapoff_to_maddr(va - DIRECTMAP_VIRT_START);While the cover letter states a dependency on another series, I'm unable
to spotdirectmapoff_to_maddr()in the tree or in that other series.
The definition of directmap_off_to_maddr() is in xen/pdx.h:
#ifdef CONFIG_PDX_COMPRESSION
...
/**
* Computes a machine address given a direct map offset *
* @param offset Offset into the direct map
* @return Corresponding machine address of that virtual location */
static inline paddr_t directmapoff_to_maddr(unsigned long offset)
{
return ((((paddr_t)offset << pfn_pdx_hole_shift) & ma_top_mask) |
(offset & ma_va_bottom_mask));
}...
#else /* !CONFIG_PDX_COMPRESSION */
...
/* directmap is indexed by by maddr */
#define maddr_to_directmapoff(x) (x)
#define directmapoff_to_maddr(x) (x)
...
#endif
Quoted 9 lines
Quoted 5 lines
+
BUILD_BUG_ON(XEN_VIRT_SIZE != MB(2));
+ ASSERT(((long)va >> (PAGETABLE_ORDER+PAGE_SHIFT)) == + ((long)XEN_VIRT_START >> (PAGETABLE_ORDER+PAGE_SHIFT)));What's the point of the casts here? va is unsigned long and
XEN_VIRT_START
is a literal number (which probably ought to have a UL suffix).
I thought that it could be the same as for x86 that:
/* Signed, so ((long)XEN_VIRT_START >> 30) fits in an imm32. */
But checking the generated code for RISC-V casts could be dropped as the generated code is the same.
Regarding UL, I will update to _AC(XEN_VIRT_START, UL).
Quoted 4 lines
Quoted 2 lines
+ return
phys_offset+ va;Don't you need to subtract
XEN_VIRT_STARThere?
It shouldn't as XEN_VIRT_START is taken into account during phys_offset calculation ( as you mentioned in the reply ).
Regarding the name of phys_offset variable, could it be better to rename it to load_offset?
As an option I can just add the comment above return:
/* phys_offset = load_start - XEN_VIRT_START */
~ Oleksii
PATCH 2/3 xen/riscv: switch LINK_TO_LOAD() to virt_to_maddr()
In reply to Oleksii KurochkoExcept for switching LINK_TO_LOAD() to virt_to_maddr(), LINK_TO_LOAD() is dropped, as virt_to_maddr() covers all the cases where LINK_TO_LOAD() is used.
--- xen/arch/riscv/mm.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
xen/arch/riscv/mm.c +3 -4
@@ -29,7 +29,6 @@ struct mmu_desc {unsigned long __ro_after_init phys_offset;#define LOAD_TO_LINK (addr) ((unsigned long)(addr) - phys_offset) -#define LINK_TO_LOAD (addr) ((unsigned long)(addr) + phys_offset)/* * It is expected that Xen won't be more then 2 MB.
@@ -122,7 +121,7 @@ static void __init setup_initial_mapping(struct mmu_desc *mmu_desc,unsigned long paddr = (page_addr - map_start) + pa_start;unsigned int permissions =PTE_LEAF_DEFAULT ;unsigned long addr = is_identity_mapping - ? page_addr :LINK_TO_LOAD (page_addr); + ? page_addr :virt_to_maddr (page_addr);pte_t pte_to_be_written; index =pt_index (0 , page_addr);
@@ -225,7 +224,7 @@ void __init setup_fixmap_mappings(void)BUG_ON (pte_is_valid (*pte)); - tmp =paddr_to_pte (LINK_TO_LOAD ((unsigned long )&xen_fixmap),PTE_TABLE ); + tmp =paddr_to_pte (virt_to_maddr (&xen_fixmap),PTE_TABLE );write_pte (pte, tmp);RISCV_FENCE (rw, rw);
@@ -312,7 +311,7 @@ void __init remove_identity_mapping(void)pte_t *pgtbl;unsigned int index, xen_index;unsigned long ident_start = -LINK_TO_LOAD (turn_on_mmu) &XEN_PT_LEVEL_MAP_MASK (0 ); +virt_to_maddr ((unsigned long )turn_on_mmu) &XEN_PT_LEVEL_MAP_MASK (0 );for ( pgtbl = stage1_pgtbl_root, i =CONFIG_PAGING_LEVELS ; i; i-- ) {
Signature · 1 line
2.46.1
Re: [PATCH v1 2/3] xen/riscv: switch LINK_TO_LOAD() to virt_to_maddr()
Quoted 3 lines · Oleksii Kurochko
Except for switching
LINK_TO_LOAD()tovirt_to_maddr(),LINK_TO_LOAD()is dropped, asvirt_to_maddr()covers all the cases whereLINK_TO_LOAD()is used.
Why "Except for ..."? I'm afraid I can'rt make sense of this.
xen/arch/riscv/mm.c void __init setup_fixmap_mappings(void) · line 227
Quoted 6 lines
xen/arch/riscv/mm.c +1
-1@@ -225,7 +224,7 @@ void __init setup_fixmap_mappings(void)BUG_ON (pte_is_valid (*pte)); - tmp =paddr_to_pte (LINK_TO_LOAD ((unsigned long )&xen_fixmap),PTE_TABLE ); + tmp =paddr_to_pte (virt_to_maddr (&xen_fixmap),PTE_TABLE );
Just like you don't open-code a cast here, ...
xen/arch/riscv/mm.c void __init remove_identity_mapping(void) · line 314
Quoted 6 lines
xen/arch/riscv/mm.c +1
-1@@ -312,7 +311,7 @@ void __init remove_identity_mapping(void)pte_t *pgtbl;unsigned int index, xen_index;unsigned long ident_start = -LINK_TO_LOAD (turn_on_mmu) &XEN_PT_LEVEL_MAP_MASK (0 ); +virt_to_maddr ((unsigned long )turn_on_mmu) &XEN_PT_LEVEL_MAP_MASK (0 );
... you shouldn't do so here. The virt_to_maddr() wrapper macro does so already.
Jan
Re: [PATCH v1 2/3] xen/riscv: switch LINK_TO_LOAD() to virt_to_maddr()
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksii Kurochko
Except for switching
LINK_TO_LOAD()tovirt_to_maddr(),LINK_TO_LOAD()is dropped, asvirt_to_maddr()covers all the cases whereLINK_TO_LOAD()is used.Why "Except for ..."? I'm afraid I can'rt make sense of this.
I will reword that for clarity:
Use virt_to_maddr() instead of LINK_TO_LOAD as virt_to_maddr() covers all the cases where LINK_TO_LOAD() is used.
Thanks.
~ Oleksii
Quoted 23 lines
Quoted 8 lines
(quoted diff) +0
-0@@ -225,7 +224,7 @@ void __init setup_fixmap_mappings(void)
BUG_ON(pte_is_valid(*pte));
- tmp =paddr_to_pte(LINK_TO_LOAD((unsigned long)&xen_fixmap), PTE_TABLE);
+ tmp =paddr_to_pte(virt_to_maddr(&xen_fixmap), PTE_TABLE);Just like you don't open-code a cast here, ...
Quoted 7 lines
xen/arch/riscv/mm.c +0
-0@@ -312,7 +311,7 @@ void __init remove_identity_mapping(void) pte_t *pgtbl; unsigned int index, xen_index;unsigned long
ident_start=
-LINK_TO_LOAD(turn_on_mmu)&XEN_PT_LEVEL_MAP_MASK(0); +virt_to_maddr((unsigned long)turn_on_mmu)&XEN_PT_LEVEL_MAP_MASK(0);... you shouldn't do so here. The
virt_to_maddr()wrapper macro does so already.Jan
PATCH 3/3 xen/riscv: register Xen's load address as a boot module
In reply to Oleksii KurochkoAvoid using BOOTMOD_XEN region for other purposes or boot modules which could result in memory corruption or undefined behaviour.
--- xen/arch/riscv/setup.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
xen/arch/riscv/setup.c +10 -0
@@ -1,6 +1,7 @@/* SPDX-License-Identifier: GPL-2.0-only */ #include <xen/bug.h> +#include <xen/bootfdt.h> #include <xen/compile.h> #include <xen/device_tree.h> #include <xen/init.h>
@@ -26,6 +27,8 @@ unsigned char __initdata cpu0_boot_stack[STACK_SIZE]void __init noreturnstart_xen (unsigned long bootcpu_id,paddr_t dtb_addr) { +struct bootmodule *xen_bootmodule; +remove_identity_mapping ();set_processor_id (0 );
@@ -44,6 +47,13 @@ void __init noreturn start_xen(unsigned long bootcpu_id,"Please check your bootloader.\n" , dtb_addr, BOOT_FDT_VIRT_SIZE); +/* Register Xen's load address as a boot module. */ + xen_bootmodule =add_boot_module (BOOTMOD_XEN , +virt_to_maddr (_start), + (paddr_t)(_end - _start), false); + +BUG_ON (!xen_bootmodule); +printk ("All set up\n" );machine_halt ();
Signature · 1 line
2.46.1
Re: [PATCH v1 3/3] xen/riscv: register Xen's load address as a boot module
xen/arch/riscv/setup.c unsigned char __initdata cpu0_boot_stack[STACK_SIZE] · line 30
Quoted 5 lines · Oleksii Kurochko
xen/arch/riscv/setup.c +1
-0@@ -26,6 +27,8 @@ unsigned char __initdata cpu0_boot_stack[STACK_SIZE]void __init noreturnstart_xen (unsigned long bootcpu_id,paddr_t dtb_addr) { +struct bootmodule *xen_bootmodule;
With just the uses below this can be pointer-to-const. Question of course is whether you already know of further uses.
xen/arch/riscv/setup.c void __init noreturn start_xen(unsigned long bootcpu_id, · line 53
Quoted 8 lines
xen/arch/riscv/setup.c +4
-0@@ -44,6 +47,13 @@ void __init noreturn start_xen(unsigned long bootcpu_id,"Please check your bootloader.\n" , dtb_addr, BOOT_FDT_VIRT_SIZE); +/* Register Xen's load address as a boot module. */ + xen_bootmodule =add_boot_module (BOOTMOD_XEN , +virt_to_maddr (_start), + (paddr_t)(_end - _start), false);
There's no real need for the cast, is there? Plus if anything, it would be more to size_t than to paddr_t.
Jan
Re: [PATCH v1 3/3] xen/riscv: register Xen's load address as a boot module
Quoted 11 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
(quoted diff) +0
-0@@ -26,6 +27,8 @@ unsigned char __initdatacpu0_boot_stack[STACK_SIZE]
void__initnoreturnstart_xen(unsigned long bootcpu_id, paddr_t dtb_addr){
+ struct bootmodule*xen_bootmodule;With just the uses below this can be pointer-to-const. Question of course
is whether you already know of further uses.
It could be dropped as it is used only for BUG_ON(!xen_bootmodule) as it looks to me a little bit better then:
BUG_ON(!add_boot_module(BOOTMOD_XEN, virt_to_maddr(_start),
(_end - _start), false));But I am okay to drop xen_bootmodule variable.
Quoted 15 lines
Quoted 11 lines
xen/arch/riscv/setup.c +0
-0@@ -44,6 +47,13 @@ void __init noreturn start_xen(unsigned long bootcpu_id, "Please check your bootloader.\n", dtb_addr, BOOT_FDT_VIRT_SIZE);
+ /* Register Xen's load address as a boot module. */ +xen_bootmodule= add_boot_module(BOOTMOD_XEN,
+virt_to_maddr(_start), + (paddr_t)(_end -_start), false);There's no real need for the cast, is there? Plus if anything, it would be
more tosize_tthan topaddr_t.
In this case the cast isn't really needed. I will drop it.
Thanks.
~ Oleksii
Re: [PATCH v1 3/3] xen/riscv: register Xen's load address as a boot module
Quoted 12 lines · On 02.10.2024 13:25, [email protected] wrote:
Quoted 11 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
(quoted diff) +0
-0@@ -26,6 +27,8 @@ unsigned char __initdatacpu0_boot_stack[STACK_SIZE]
void__initnoreturnstart_xen(unsigned long bootcpu_id, paddr_t dtb_addr){
+ struct bootmodule*xen_bootmodule;With just the uses below this can be pointer-to-const. Question of course
is whether you already know of further uses.
Quoted 4 lines
It could be dropped as it is used only for
BUG_ON(!xen_bootmodule)as it looks to me a little bit better then:BUG_ON(!add_boot_module(BOOTMOD_XEN, virt_to_maddr(_start), (_end - _start), false));
Yet that's undesirable for other reasons. Did you consider
if ( !add_boot_module(BOOTMOD_XEN, virt_to_maddr(_start),
_end - _start, false) )
BUG();? Maybe panic() would be even better for cases like this one.
Jan
Re: [PATCH v1 3/3] xen/riscv: register Xen's load address as a boot module
Quoted 20 lines · Jan Beulich
Quoted 13 lines · On 02.10.2024 13:25, [email protected] wrote:
Quoted 12 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
(quoted diff) +0
-0@@ -26,6 +27,8 @@ unsigned char __initdatacpu0_boot_stack[STACK_SIZE]
void__initnoreturnstart_xen(unsigned long bootcpu_id, paddr_t dtb_addr){
+ struct bootmodule*xen_bootmodule;With just the uses below this can be pointer-to-const. Question of
course
is whether you already know of further uses.Quoted 5 lines
It could be dropped as it is used only for
BUG_ON(!xen_bootmodule)as
it looks to me a little bit better then:
BUG_ON(!add_boot_module(BOOTMOD_XEN, virt_to_maddr(_start), (_end - _start), false));
Quoted 7 lines
Yet that's undesirable for other reasons. Did you consider
if ( !add_boot_module(BOOTMOD_XEN,
virt_to_maddr(_start),_end-_start, false) )BUG();? Maybe
panic()would be even better for cases like this one.
It looks fine to me. Thanks for suggestion.
~ Oleksii