remove size limit on XIP kernel
cover remove size limit on XIP kernel
Hi,
For XIP kernel, the writable data section is always at offset specified in XIP_OFFSET, which is hard-coded to 32MB.
Unfortunately, this means the read-only section (placed before the writable section) is restricted in size. This causes build failure if the kernel gets too large.
This series remove the use of XIP_OFFSET one by one, then remove this macro entirely at the end, with the goal of lifting this size restriction.
Also some cleanup and documentation along the way.
This series depends on
https://lore.kernel.org/linux-riscv/[email protected]/ to apply cleanly, and also depends on https://lore.kernel.org/linux-riscv/[email protected]/
which fixes a boot issue.
Best regards,
Nam
Nam Cao (7):
riscv: cleanup XIP_FIXUP macro
riscv: replace va_kernel_pa_offset with va_kernel_data_pa_offset on
XIP
riscv: drop the use of XIP_OFFSET in XIP_FIXUP_OFFSET
riscv: drop the use of XIP_OFFSET in XIP_FIXUP_FLASH_OFFSET
riscv: drop the use of XIP_OFFSET in kernel_mapping_va_to_pa()
riscv: drop the use of XIP_OFFSET in create_kernel_page_table()
riscv: remove limit on the size of read-only section for XIP kernel
arch/riscv/include/asm/page.h | 25 ++++++++++++++++++------ arch/riscv/include/asm/pgtable.h | 18 +++++++---------- arch/riscv/include/asm/xip_fixup.h | 30 +++++++++++++++++++++++------ arch/riscv/kernel/vmlinux-xip.lds.S | 4 ++-- arch/riscv/mm/init.c | 11 +++++++---- 5 files changed, 59 insertions(+), 29 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/7 riscv: cleanup XIP_FIXUP macro
The XIP_FIXUP macro is used to fix addresses early during boot before MMU:
generated code "thinks" the data section is in ROM while it is actually in RAM. So this macro correct the addresses in the data section.
This macro determines if the address needs to be fixed by checking if it is within the range startting of ROM address up to the size of 2 * XIP_OFFSET
This means addresses within the .text section would incorrectly get fixed. Also if the kernel size if bigger than (2 * XIP_OFFSET), some addresses would not be fixed up.
XIP kernel can still work if the above 2 cases do not happen. But this macro is obviously incorrect.
Rewrite this macro to only fix up addresses within the data section.
--- arch/riscv/include/asm/pgtable.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
arch/riscv/include/asm/pgtable.h +7 -4
@@ -139,11 +139,14 @@#ifdef CONFIG_XIP_KERNEL #define XIP_FIXUP (addr) ({ \ + extern char _sdata[], _start[], _end[]; \ + uintptr_t __rom_start_data = CONFIG_XIP_PHYS_ADDR \ + + (uintptr_t)&_sdata - (uintptr_t)&_start; \ + uintptr_t __rom_end_data = CONFIG_XIP_PHYS_ADDR \ + + (uintptr_t)&_end - (uintptr_t)&_start; \ uintptr_t __a = (uintptr_t)(addr); \ - (__a >= CONFIG_XIP_PHYS_ADDR && \ - __a < CONFIG_XIP_PHYS_ADDR + XIP_OFFSET * 2) ? \ - __a - CONFIG_XIP_PHYS_ADDR + CONFIG_PHYS_RAM_BASE - XIP_OFFSET :\ - __a; \ + (__a >= __rom_start_data && __a < __rom_end_data) ? \ + __a - __rom_start_data + CONFIG_PHYS_RAM_BASE : __a; \ })#else #define XIP_FIXUP (addr) (addr)
Signature · 1 line
2.39.2
Re: [PATCH 1/7] riscv: cleanup XIP_FIXUP macro
Hi Nam,
Quoted 6 lines · Nam Cao
The
XIP_FIXUPmacro is used to fix addresses early during boot before MMU:
generated code "thinks" the data section is in ROM while it is actually in RAM. So this macro correct the addresses in the data section.This macro determines if the address needs to be fixed by checking if it is within the range startting of ROM address up to the size of 2 *
XIP_OFFSET
s/startting/starting
And the sentence lacks the final dot.
Quoted 2 lines
This means addresses within the .text section would incorrectly get fixed.
Yes, but XIP_FIXUP() should only be applied to data symbols so I believe this ^ is not relevant.
Quoted 2 lines
Also if the kernel size if bigger than (2 *
XIP_OFFSET), some addresses would not be fixed up.
s/the kernel size if/the kernel size is
Quoted 24 lines
XIP kernel can still work if the above 2 cases do not happen. But this macro is obviously incorrect.
Rewrite this macro to only fix up addresses within the data section.
Signed-off-by Nam Cao <[email protected]>--- arch/riscv/include/asm/pgtable.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)arch/riscv/include/asm/pgtable.h +5
-0@@ -139,11 +139,14 @@#ifdef CONFIG_XIP_KERNEL #define XIP_FIXUP (addr) ({ \ + extern char _sdata[], _start[], _end[]; \ + uintptr_t __rom_start_data = CONFIG_XIP_PHYS_ADDR \ + + (uintptr_t)&_sdata - (uintptr_t)&_start; \ + uintptr_t __rom_end_data = CONFIG_XIP_PHYS_ADDR \ + + (uintptr_t)&_end - (uintptr_t)&_start; \
arch/riscv/include/asm/pgtable.h line 152
Quoted 10 lines
arch/riscv/include/asm/pgtable.h +2
-4uintptr_t __a = (uintptr_t )(addr); \ - (__a >=CONFIG_XIP_PHYS_ADDR && \ - __a <CONFIG_XIP_PHYS_ADDR +XIP_OFFSET *2 ) ? \ - __a -CONFIG_XIP_PHYS_ADDR +CONFIG_PHYS_RAM_BASE -XIP_OFFSET :\ - __a; \ + (__a >= __rom_start_data && __a < __rom_end_data) ? \ + __a - __rom_start_data +CONFIG_PHYS_RAM_BASE : __a; \ })#else #define XIP_FIXUP (addr) (addr)
Thanks,
Alex
PATCH 2/7 riscv: replace va_kernel_pa_offset with va_kernel_data_pa_offset on XIP
In reply to Nam CaoOn XIP kernel, the name "va_kernel_pa_offset" is misleading: unlike "normal" kernel, it is not the virtual-physical address offset of kernel mapping, it is the offset of kernel mapping's first virtual address to first physical address in DRAM, which is not meaningful because the kernel's first physical address is not in DRAM.
For XIP kernel, there are 2 different offsets because the read-only part of the kernel resides in ROM while the rest is in RAM. The offset to ROM is in kernel_map.va_kernel_xip_pa_offset, while the offset to RAM is not stored
anywhere: it is calculated on-the-fly.
Remove this confusing "va_kernel_pa_offset" and add "va_kernel_data_pa_offset" as its replacement. This new variable is the offset of virtual mapping of the kernel's data portion to the corresponding physical addresses.
--- arch/riscv/include/asm/page.h | 25 +++++++++++++++++++------ arch/riscv/mm/init.c | 4 +++- 2 files changed, 22 insertions(+), 7 deletions(-)
arch/riscv/include/asm/page.h +19 -6
@@ -112,11 +112,13 @@ struct kernel_mapping {/* Offset between linear mapping virtual address and kernel load address */ unsigned long va_pa_offset;/* Offset between kernel mapping virtual address and kernel load address */ -unsigned long va_kernel_pa_offset; -unsigned long va_kernel_xip_pa_offset;#ifdef CONFIG_XIP_KERNEL +unsigned long va_kernel_xip_pa_offset; +unsigned long va_kernel_data_pa_offset;uintptr_t xiprom;uintptr_t xiprom_sz; +#else +unsigned long va_kernel_pa_offset;#endif };
@@ -134,12 +136,18 @@ extern phys_addr_t phys_ram_base;#else void *linear_mapping_pa_to_va (unsigned long x);#endif + +#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_pa_to_va (y) ({ \ unsigned long _y = (unsigned long)(y); \ - (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ? \ + (_y < phys_ram_base) ? \ (void *)(_y + kernel_map.va_kernel_xip_pa_offset) : \ - (void *)(_y + kernel_map.va_kernel_pa_offset + XIP_OFFSET); \ + (void *)(_y + kernel_map.va_kernel_data_pa_offset); \ }) +#else +#define kernel_mapping_pa_to_va (y) (void *)((unsigned long)(y) + kernel_map.va_kernel_pa_offset) +#endif +#define __pa_to_va_nodebug (x) linear_mapping_pa_to_va(x)#ifndef CONFIG_DEBUG_VIRTUAL
@@ -147,12 +155,17 @@ void *linear_mapping_pa_to_va(unsigned long x);#else phys_addr_t linear_mapping_va_to_pa (unsigned long x);#endif + +#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_va_to_pa (y) ({ \ unsigned long _y = (unsigned long)(y); \ - (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \ + (_y < kernel_map.virt_addr + XIP_OFFSET) ? \ (_y - kernel_map.va_kernel_xip_pa_offset) : \ - (_y - kernel_map.va_kernel_pa_offset - XIP_OFFSET); \ + (_y - kernel_map.va_kernel_data_pa_offset); \ }) +#else +#define kernel_mapping_va_to_pa (y) ((unsigned long)(y) - kernel_map.va_kernel_pa_offset) +#endif #define __va_to_pa_nodebug (x) ({ \ unsigned long _x = x; \
arch/riscv/mm/init.c +3 -1
@@ -1089,10 +1089,13 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) kernel_map.size = (uintptr_t )(&_end) - (uintptr_t )(&_start); kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom; + kernel_map.va_kernel_data_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr + + (uintptr_t )&_sdata - (uintptr_t )&_start;#else kernel_map.page_offset =_AC (CONFIG_PAGE_OFFSET ,UL ); kernel_map.phys_addr = (uintptr_t )(&_start); kernel_map.size = (uintptr_t )(&_end) - kernel_map.phys_addr; + kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;#endif #if defined(CONFIG_64BIT ) && !defined(CONFIG_XIP_KERNEL )
@@ -1114,7 +1117,6 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)*/ kernel_map.va_pa_offset =IS_ENABLED (CONFIG_64BIT ) ?0UL :PAGE_OFFSET - kernel_map.phys_addr; - kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;/* * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit
Signature · 1 line
2.39.2
Re: [PATCH 2/7] riscv: replace va_kernel_pa_offset with va_kernel_data_pa_offset on XIP
Quoted 27 lines · Nam Cao
On XIP kernel, the name "
va_kernel_pa_offset" is misleading: unlike "normal" kernel, it is not the virtual-physical address offset of kernel mapping, it is the offset of kernel mapping's first virtual address to first physical address in DRAM, which is not meaningful because the kernel's first physical address is not in DRAM.For XIP kernel, there are 2 different offsets because the read-only part of the kernel resides in ROM while the rest is in RAM. The offset to ROM is in kernel_map.va_kernel_xip_pa_offset, while the offset to RAM is not stored
anywhere: it is calculated on-the-fly.Remove this confusing "
va_kernel_pa_offset" and add "va_kernel_data_pa_offset" as its replacement. This new variable is the offset of virtual mapping of the kernel's data portion to the corresponding physical addresses.Signed-off-by Nam Cao <[email protected]>--- arch/riscv/include/asm/page.h | 25 +++++++++++++++++++------ arch/riscv/mm/init.c | 4 +++- 2 files changed, 22 insertions(+), 7 deletions(-)arch/riscv/include/asm/page.h +0
-0
arch/riscv/include/asm/page.h struct kernel_mapping { · line 117
Quoted 8 lines
arch/riscv/include/asm/page.h +2
-2@@ -112,11 +112,13 @@ struct kernel_mapping {/* Offset between linear mapping virtual address and kernel load address */ unsigned long va_pa_offset;/* Offset between kernel mapping virtual address and kernel load address */ -unsigned long va_kernel_pa_offset; -unsigned long va_kernel_xip_pa_offset;#ifdef CONFIG_XIP_KERNEL +unsigned long va_kernel_xip_pa_offset; +unsigned long va_kernel_data_pa_offset;
I would call that new field va_kernel_xip_data_pa_offset so that we know at first sight it only applies to XIP_KERNEL (and maybe rename va_kernel_xip_pa_offset into va_kernel_xip_text_pa_offset?).
Quoted 67 lines
arch/riscv/include/asm/page.h +17
-4uintptr_t xiprom;uintptr_t xiprom_sz; +#else +unsigned long va_kernel_pa_offset;#endif };@@ -134,12 +136,18 @@ extern phys_addr_t phys_ram_base;#else void *linear_mapping_pa_to_va (unsigned long x);#endif + +#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_pa_to_va (y) ({ \ unsigned long _y = (unsigned long)(y); \ - (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ? \ + (_y < phys_ram_base) ? \ (void *)(_y + kernel_map.va_kernel_xip_pa_offset) : \ - (void *)(_y + kernel_map.va_kernel_pa_offset + XIP_OFFSET); \ + (void *)(_y + kernel_map.va_kernel_data_pa_offset); \ }) +#else +#define kernel_mapping_pa_to_va (y) (void *)((unsigned long)(y) + kernel_map.va_kernel_pa_offset) +#endif +#define __pa_to_va_nodebug (x) linear_mapping_pa_to_va(x)#ifndef CONFIG_DEBUG_VIRTUAL @@ -147,12 +155,17 @@ void *linear_mapping_pa_to_va(unsigned long x);#else phys_addr_t linear_mapping_va_to_pa (unsigned long x);#endif + +#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_va_to_pa (y) ({ \ unsigned long _y = (unsigned long)(y); \ - (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \ + (_y < kernel_map.virt_addr + XIP_OFFSET) ? \ (_y - kernel_map.va_kernel_xip_pa_offset) : \ - (_y - kernel_map.va_kernel_pa_offset - XIP_OFFSET); \ + (_y - kernel_map.va_kernel_data_pa_offset); \ }) +#else +#define kernel_mapping_va_to_pa (y) ((unsigned long)(y) - kernel_map.va_kernel_pa_offset) +#endif #define __va_to_pa_nodebug (x) ({ \ unsigned long _x = x; \arch/riscv/mm/init.c +3
-0@@ -1089,10 +1089,13 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) kernel_map.size = (uintptr_t )(&_end) - (uintptr_t )(&_start); kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom; + kernel_map.va_kernel_data_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr + + (uintptr_t )&_sdata - (uintptr_t )&_start;#else kernel_map.page_offset =_AC (CONFIG_PAGE_OFFSET ,UL ); kernel_map.phys_addr = (uintptr_t )(&_start); kernel_map.size = (uintptr_t )(&_end) - kernel_map.phys_addr; + kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;#endif #if defined(CONFIG_64BIT ) && !defined(CONFIG_XIP_KERNEL )
arch/riscv/mm/init.c asmlinkage void __init setup_vm(uintptr_t dtb_pa) · line 1122
Quoted 7 lines
arch/riscv/mm/init.c +0
-1@@ -1114,7 +1117,6 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)*/ kernel_map.va_pa_offset =IS_ENABLED (CONFIG_64BIT ) ?0UL :PAGE_OFFSET - kernel_map.phys_addr; - kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;/* * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit
I think you missed the occurence of va_kernel_pa_offset() in arch/riscv/kernel/vmcore_info.c.
Although some nits above, you can add:
Thanks,
Alex
PATCH 3/7 riscv: drop the use of XIP_OFFSET in XIP_FIXUP_OFFSET
In reply to Nam CaoXIP_OFFSET is the hard-coded offset of writable data section within the kernel.
By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded macro XIP_OFFSET entirely, stop using XIP_OFFSET in XIP_FIXUP_OFFSET. Instead, use CONFIG_PHYS_RAM_BASE and _sdata to do the same thing.
While at it, also add a description for XIP_FIXUP_OFFSET.
--- arch/riscv/include/asm/xip_fixup.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
arch/riscv/include/asm/xip_fixup.h +12 -2
@@ -9,8 +9,19 @@#ifdef CONFIG_XIP_KERNEL .macro XIP_FIXUP_OFFSET reg - REG_L t0, _xip_fixup +/* Fix-up address in Flash into address in RAM early during boot before +* MMU is up. Because generated code "thinks" data is in Flash, but it +* is actually in RAM (actually data is also in Flash, but Flash is +* read-only, thus we need to use the data residing in RAM). +* +* The start of data in Flash is _sdata and the start of data in RAM is +* CONFIG_PHYS_RAM_BASE. So this fix-up essentially does this: +* reg += CONFIG_PHYS_RAM_BASE - _start +*/ + li t0, CONFIG_PHYS_RAM_BASE add \reg, \reg, t0 + la t0, _sdata + sub \reg, \reg, t0 .endm .macro XIP_FIXUP_FLASH_OFFSET reg la t0, __data_loc
@@ -19,7 +30,6 @@ add \reg, \reg, t0 .endm -_xip_fixup: .dword CONFIG_PHYS_RAM_BASE - CONFIG_XIP_PHYS_ADDR - XIP_OFFSET _xip_phys_offset: .dword CONFIG_XIP_PHYS_ADDR + XIP_OFFSET #else .macro XIP_FIXUP_OFFSET reg
Signature · 1 line
2.39.2
Re: [PATCH 3/7] riscv: drop the use of XIP_OFFSET in XIP_FIXUP_OFFSET
Quoted 43 lines · Nam Cao
XIP_OFFSETis the hard-coded offset of writable data section within the kernel.By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded macro
XIP_OFFSETentirely, stop usingXIP_OFFSETinXIP_FIXUP_OFFSET. Instead, useCONFIG_PHYS_RAM_BASEand_sdatato do the same thing.While at it, also add a description for
XIP_FIXUP_OFFSET.Signed-off-by Nam Cao <[email protected]>--- arch/riscv/include/asm/xip_fixup.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)arch/riscv/include/asm/xip_fixup.h +12
-1@@ -9,8 +9,19 @@#ifdef CONFIG_XIP_KERNEL .macro XIP_FIXUP_OFFSET reg - REG_L t0, _xip_fixup +/* Fix-up address in Flash into address in RAM early during boot before +* MMU is up. Because generated code "thinks" data is in Flash, but it +* is actually in RAM (actually data is also in Flash, but Flash is +* read-only, thus we need to use the data residing in RAM). +* +* The start of data in Flash is _sdata and the start of data in RAM is +* CONFIG_PHYS_RAM_BASE. So this fix-up essentially does this: +* reg += CONFIG_PHYS_RAM_BASE - _start +*/ + li t0, CONFIG_PHYS_RAM_BASE add \reg, \reg, t0 + la t0, _sdata + sub \reg, \reg, t0 .endm .macro XIP_FIXUP_FLASH_OFFSET reg la t0, __data_loc
arch/riscv/include/asm/xip_fixup.h line 35
Quoted 7 lines
arch/riscv/include/asm/xip_fixup.h +0
-1@@ -19,7 +30,6 @@ add \reg, \reg, t0 .endm -_xip_fixup: .dword CONFIG_PHYS_RAM_BASE - CONFIG_XIP_PHYS_ADDR - XIP_OFFSET _xip_phys_offset: .dword CONFIG_XIP_PHYS_ADDR + XIP_OFFSET #else .macro XIP_FIXUP_OFFSET reg
Thanks,
Alex
PATCH 4/7 riscv: drop the use of XIP_OFFSET in XIP_FIXUP_FLASH_OFFSET
In reply to Nam CaoXIP_OFFSET is the hard-coded offset of writable data section within the kernel.
By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded macro XIP_OFFSET entirely, stop using XIP_OFFSET in XIP_FIXUP_FLASH_OFFSET. Instead, use __data_loc and _sdata to do the same thing.
While at it, also add a description for XIP_FIXUP_FLASH_OFFSET.
--- arch/riscv/include/asm/xip_fixup.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
arch/riscv/include/asm/xip_fixup.h +12 -4
@@ -24,13 +24,21 @@ sub \reg, \reg, t0 .endm .macro XIP_FIXUP_FLASH_OFFSET reg +/* In linker script, at the transition from read-only section to +* writable section, the VMA is increased while LMA remains the same. +* (See in linker script how _sdata, __data_loc and LOAD_OFFSET is +* changed) +* +* Consequently, early during boot before MMU is up, the generated code +* reads the "writable" section at wrong addresses, because VMA is used +* by compiler to generate code, but the data is located in Flash using +* LMA. +*/ + la t0, _sdata + sub \reg, \reg, t0 la t0, __data_loc - REG_L t1, _xip_phys_offset - sub \reg, \reg, t1 add \reg, \reg, t0 .endm - -_xip_phys_offset: .dword CONFIG_XIP_PHYS_ADDR + XIP_OFFSET #else .macro XIP_FIXUP_OFFSET reg .endm
Signature · 1 line
2.39.2
Re: [PATCH 4/7] riscv: drop the use of XIP_OFFSET in XIP_FIXUP_FLASH_OFFSET
Quoted 37 lines · Nam Cao
XIP_OFFSETis the hard-coded offset of writable data section within the kernel.By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded macro
XIP_OFFSETentirely, stop usingXIP_OFFSETinXIP_FIXUP_FLASH_OFFSET. Instead, use__data_locand_sdatato do the same thing.While at it, also add a description for
XIP_FIXUP_FLASH_OFFSET.Signed-off-by Nam Cao <[email protected]>--- arch/riscv/include/asm/xip_fixup.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)arch/riscv/include/asm/xip_fixup.h +12
-0@@ -24,13 +24,21 @@ sub \reg, \reg, t0 .endm .macro XIP_FIXUP_FLASH_OFFSET reg +/* In linker script, at the transition from read-only section to +* writable section, the VMA is increased while LMA remains the same. +* (See in linker script how _sdata, __data_loc and LOAD_OFFSET is +* changed) +* +* Consequently, early during boot before MMU is up, the generated code +* reads the "writable" section at wrong addresses, because VMA is used +* by compiler to generate code, but the data is located in Flash using +* LMA. +*/ + la t0, _sdata + sub \reg, \reg, t0
arch/riscv/include/asm/xip_fixup.h line 44
Quoted 10 lines
arch/riscv/include/asm/xip_fixup.h +0
-4la t0, __data_loc - REG_L t1, _xip_phys_offset - sub \reg, \reg, t1 add \reg, \reg, t0 .endm - -_xip_phys_offset: .dword CONFIG_XIP_PHYS_ADDR + XIP_OFFSET #else .macro XIP_FIXUP_OFFSET reg .endm
Thanks,
Alex
PATCH 5/7 riscv: drop the use of XIP_OFFSET in kernel_mapping_va_to_pa()
In reply to Nam CaoXIP_OFFSET is the hard-coded offset of writable data section within the kernel.
By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded macro XIP_OFFSET entirely, remove the use of XIP_OFFSET in kernel_mapping_va_to_pa(). The macro XIP_OFFSET is used in this case to check if the virtual address is mapped to Flash or to RAM. The same check can be done with kernel_map.xiprom_sz.
--- arch/riscv/include/asm/page.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/page.h +1 -1
@@ -159,7 +159,7 @@ phys_addr_t linear_mapping_va_to_pa(unsigned long x);#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_va_to_pa (y) ({ \ unsigned long _y = (unsigned long)(y); \ - (_y < kernel_map.virt_addr + XIP_OFFSET) ? \ + (_y < kernel_map.virt_addr + kernel_map.xiprom_sz) ? \ (_y - kernel_map.va_kernel_xip_pa_offset) : \ (_y - kernel_map.va_kernel_data_pa_offset); \ })
Signature · 1 line
2.39.2
Re: [PATCH 5/7] riscv: drop the use of XIP_OFFSET in kernel_mapping_va_to_pa()
Quoted 21 lines · Nam Cao
XIP_OFFSETis the hard-coded offset of writable data section within the kernel.By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded macro
XIP_OFFSETentirely, remove the use ofXIP_OFFSETinkernel_mapping_va_to_pa(). The macroXIP_OFFSETis used in this case to check if the virtual address is mapped to Flash or to RAM. The same check can be done with kernel_map.xiprom_sz.Signed-off-by Nam Cao <[email protected]>--- arch/riscv/include/asm/page.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/include/asm/page.h +0
-0
arch/riscv/include/asm/page.h phys_addr_t linear_mapping_va_to_pa(unsigned long x); · line 165
Quoted 8 lines
arch/riscv/include/asm/page.h +1
-1@@ -159,7 +159,7 @@ phys_addr_t linear_mapping_va_to_pa(unsigned long x);#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_va_to_pa (y) ({ \ unsigned long _y = (unsigned long)(y); \ - (_y < kernel_map.virt_addr + XIP_OFFSET) ? \ + (_y < kernel_map.virt_addr + kernel_map.xiprom_sz) ? \ (_y - kernel_map.va_kernel_xip_pa_offset) : \ (_y - kernel_map.va_kernel_data_pa_offset); \ })
Thanks,
Alex
PATCH 6/7 riscv: drop the use of XIP_OFFSET in create_kernel_page_table()
In reply to Nam CaoXIP_OFFSET is the hard-coded offset of writable data section within the kernel.
By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded value entirely, stop using XIP_OFFSET in create_kernel_page_table(). Instead use _sdata and _start to do the same thing.
--- arch/riscv/mm/init.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/riscv/mm/init.c +4 -3
@@ -907,7 +907,7 @@ static void __init relocate_kernel(void)static void __init create_kernel_page_table (pgd_t *pgdir,__always_unused bool early) { -uintptr_t va, end_va; +uintptr_t va, start_va, end_va;/* Map the flash resident part */ end_va = kernel_map.virt_addr + kernel_map.xiprom_sz;
@@ -917,10 +917,11 @@ static void __init create_kernel_page_table(pgd_t *pgdir, PMD_SIZE, PAGE_KERNEL_EXEC);/* Map the data in RAM */ + start_va = kernel_map.virt_addr + (uintptr_t )&_sdata - (uintptr_t )&_start; end_va = kernel_map.virt_addr + kernel_map.size; -for (va = kernel_map.virt_addr +XIP_OFFSET ; va < end_va; va +=PMD_SIZE ) +for (va = start_va; va < end_va; va +=PMD_SIZE )create_pgd_mapping (pgdir, va, - kernel_map.phys_addr + (va - (kernel_map.virt_addr +XIP_OFFSET )), + kernel_map.phys_addr + (va - start_va), PMD_SIZE, PAGE_KERNEL); }#else
Signature · 1 line
2.39.2
Re: [PATCH 6/7] riscv: drop the use of XIP_OFFSET in create_kernel_page_table()
Quoted 32 lines · Nam Cao
XIP_OFFSETis the hard-coded offset of writable data section within the kernel.By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size.
As a preparation to remove this hard-coded value entirely, stop using
XIP_OFFSETincreate_kernel_page_table(). Instead use_sdataand_startto do the same thing.Signed-off-by Nam Cao <[email protected]>--- arch/riscv/mm/init.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)arch/riscv/mm/init.c +1
-1@@ -907,7 +907,7 @@ static void __init relocate_kernel(void)static void __init create_kernel_page_table (pgd_t *pgdir,__always_unused bool early) { -uintptr_t va, end_va; +uintptr_t va, start_va, end_va;/* Map the flash resident part */ end_va = kernel_map.virt_addr + kernel_map.xiprom_sz;@@ -917,10 +917,11 @@ static void __init create_kernel_page_table(pgd_t *pgdir, PMD_SIZE, PAGE_KERNEL_EXEC);/* Map the data in RAM */
arch/riscv/mm/init.c static void __init create_kernel_page_table(pgd_t *pgdir, · line 927
Quoted 10 lines
arch/riscv/mm/init.c +3
-2+ start_va = kernel_map.virt_addr + (uintptr_t )&_sdata - (uintptr_t )&_start; end_va = kernel_map.virt_addr + kernel_map.size; -for (va = kernel_map.virt_addr +XIP_OFFSET ; va < end_va; va +=PMD_SIZE ) +for (va = start_va; va < end_va; va +=PMD_SIZE )create_pgd_mapping (pgdir, va, - kernel_map.phys_addr + (va - (kernel_map.virt_addr +XIP_OFFSET )), + kernel_map.phys_addr + (va - start_va), PMD_SIZE, PAGE_KERNEL); }#else
Thanks,
Alex
PATCH 7/7 riscv: remove limit on the size of read-only section for XIP kernel
In reply to Nam CaoXIP_OFFSET is the hard-coded offset of writable data section within the kernel.
By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size. This causes build failures if the kernel get too big (an example is in Closes:).
Remove this limit.
--- arch/riscv/include/asm/pgtable.h | 7 ------- arch/riscv/kernel/vmlinux-xip.lds.S | 4 ++-- 2 files changed, 2 insertions(+), 9 deletions(-)
arch/riscv/include/asm/pgtable.h +0 -7
@@ -104,13 +104,6 @@#endif -#ifdef CONFIG_XIP_KERNEL -#define XIP_OFFSET SZ_32M -#define XIP_OFFSET_MASK (SZ_32M - 1) -#else -#define XIP_OFFSET 0 -#endif -#ifndef __ASSEMBLY__#include <asm/page.h>
arch/riscv/kernel/vmlinux-xip.lds.S +2 -2
@@ -65,10 +65,10 @@ SECTIONS * From this point, stuff is considered writable and will be copied to RAM */ __data_loc = ALIGN(PAGE_SIZE); /* location in file */ - . = KERNEL_LINK_ADDR + XIP_OFFSET; /* location in memory */ + . = ALIGN(SZ_2M); /* location in memory */ #undef LOAD_OFFSET -#define LOAD_OFFSET (KERNEL_LINK_ADDR + XIP_OFFSET - (__data_loc & XIP_OFFSET_MASK)) +#define LOAD_OFFSET (KERNEL_LINK_ADDR + _sdata - __data_loc) _sdata = .; /* Start of data section */ _data = .;
Signature · 1 line
2.39.2
Re: [PATCH 7/7] riscv: remove limit on the size of read-only section for XIP kernel
Quoted 6 lines · Nam Cao
XIP_OFFSETis the hard-coded offset of writable data section within the kernel.By hard-coding this value, the read-only section of the kernel (which is placed before the writable data section) is restricted in size. This causes build failures if the kernel get too big (an example is in Closes:).
s/get/gets
I think you can use:
And instead use:
s/an example is in Closes:/[1]
Quoted 34 lines
Remove this limit.
Reported-by kernel test robot <[email protected]>Signed-off-by Nam Cao <[email protected]>--- arch/riscv/include/asm/pgtable.h | 7 ------- arch/riscv/kernel/vmlinux-xip.lds.S | 4 ++-- 2 files changed, 2 insertions(+), 9 deletions(-)arch/riscv/include/asm/pgtable.h +0
-7@@ -104,13 +104,6 @@#endif -#ifdef CONFIG_XIP_KERNEL -#define XIP_OFFSET SZ_32M -#define XIP_OFFSET_MASK (SZ_32M - 1) -#else -#define XIP_OFFSET 0 -#endif -#ifndef __ASSEMBLY__#include <asm/page.h> arch/riscv/kernel/vmlinux-xip.lds.S +0
-0
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 68
Quoted 5 lines
arch/riscv/kernel/vmlinux-xip.lds.S +1
-1@@ -65,10 +65,10 @@ SECTIONS * From this point, stuff is considered writable and will be copied to RAM */ __data_loc = ALIGN(PAGE_SIZE); /* location in file */ - . = KERNEL_LINK_ADDR + XIP_OFFSET; /* location in memory */ + . = ALIGN(SZ_2M); /* location in memory */
You can't use SZ_2M here since it corresponds to PMD_SIZE for rv64 but on rv32 (which is allowed to use xip kernels), it's 4MB. Use SECTION_ALIGN instead.
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 74
Quoted 7 lines
arch/riscv/kernel/vmlinux-xip.lds.S +1
-1#undef LOAD_OFFSET -#define LOAD_OFFSET (KERNEL_LINK_ADDR + XIP_OFFSET - (__data_loc & XIP_OFFSET_MASK)) +#define LOAD_OFFSET (KERNEL_LINK_ADDR + _sdata - __data_loc) _sdata = .; /* Start of data section */ _data = .;
When the above comment is fixed, you can add:
And many thanks for this big cleanup.
Alex
Re: [PATCH 7/7] riscv: remove limit on the size of read-only section for XIP kernel
Quoted 6 lines · Alexandre Ghiti
Quoted 6 lines
arch/riscv/kernel/vmlinux-xip.lds.S +0
-0@@ -65,10 +65,10 @@ SECTIONS * From this point, stuff is considered writable and will be copied to RAM
Quoted 8 lines
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 68
Quoted 4 lines
arch/riscv/kernel/vmlinux-xip.lds.S +1
-1*/ __data_loc = ALIGN(PAGE_SIZE); /* location in file */ - . = KERNEL_LINK_ADDR + XIP_OFFSET; /* location in memory */ + . = ALIGN(SZ_2M); /* location in memory */You can't use
SZ_2Mhere since it corresponds toPMD_SIZEfor rv64 but on rv32 (which is allowed to use xip kernels), it's 4MB. UseSECTION_ALIGNinstead.
SECTION_ALIGN doesn't work unfortunately. For XIP, SECTION_ALIGN is L1_CACHE_BYTES which is 64 bytes, but we need at least PMD_SIZE alignment to setup virtual mapping.
Ideally we use PMD_SIZE here, but I can't #include that header file. Probably we can refactor the header files so that we can #include the header file that PMD_SIZE is in. But I am not sure if it's worth it.
I'm thinking just go for:
ifdef CONFIG_64_BIT
. = ALIGN(SZ_2M); #else . = ALIGN(SZ_4M);
#endif
Or even simpler, just:
. = ALIGN(SZ_4M);
As much as I hate magic numbers, I think we can give linker script some leeway. Perhaps with an explanation why this alignment is chosen?
Or do you have a better idea?
Best regards,
Nam
Re: [PATCH 7/7] riscv: remove limit on the size of read-only section for XIP kernel
Quoted 16 lines · Nam Cao
Quoted 6 lines · Alexandre Ghiti
Quoted 6 lines
arch/riscv/kernel/vmlinux-xip.lds.S +0
-0@@ -65,10 +65,10 @@ SECTIONS * From this point, stuff is considered writable and will be copied to RAMQuoted 8 lines
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 68
Quoted 4 lines
arch/riscv/kernel/vmlinux-xip.lds.S +1
-1*/ __data_loc = ALIGN(PAGE_SIZE); /* location in file */ - . = KERNEL_LINK_ADDR + XIP_OFFSET; /* location in memory */ + . = ALIGN(SZ_2M); /* location in memory */You can't use
SZ_2Mhere since it corresponds toPMD_SIZEfor rv64 but on rv32 (which is allowed to use xip kernels), it's 4MB. UseSECTION_ALIGNinstead.
Quoted 3 lines
SECTION_ALIGNdoesn't work unfortunately. For XIP,SECTION_ALIGNisL1_CACHE_BYTESwhich is 64 bytes, but we need at leastPMD_SIZEalignment to setup virtual mapping.
Sorry, I think I had tunnel vision. The solution is so obvious.
I will send v2 shortly. Thanks so much for spending time reviewing.
Best regards,
Nam
Quoted 22 lines
Ideally we use
PMD_SIZEhere, but I can't #include that header file. Probably we can refactor the header files so that we can #include the header file thatPMD_SIZEis in. But I am not sure if it's worth it.I'm thinking just go for:
ifdefCONFIG_64_BIT. = ALIGN(SZ_2M); #else . = ALIGN(SZ_4M);#endif
Or even simpler, just:
. = ALIGN(SZ_4M);As much as I hate magic numbers, I think we can give linker script some leeway. Perhaps with an explanation why this alignment is chosen?
Or do you have a better idea?
Best regards,
Nam
Alexandre Ghiti
reviewerHi Nam,
Quoted 33 lines · Nam Cao
Hi,
For XIP kernel, the writable data section is always at offset specified in
XIP_OFFSET, which is hard-coded to 32MB.Unfortunately, this means the read-only section (placed before the writable section) is restricted in size. This causes build failure if the kernel gets too large.
This series remove the use of
XIP_OFFSETone by one, then remove this macro entirely at the end, with the goal of lifting this size restriction.Also some cleanup and documentation along the way.
This series depends on
https://lore.kernel.org/linux-riscv/[email protected]/ to apply cleanly, and also depends on https://lore.kernel.org/linux-riscv/[email protected]/which fixes a boot issue.
Best regards,
NamNam Cao (7):
riscv: cleanupXIP_FIXUPmacro
riscv: replaceva_kernel_pa_offsetwithva_kernel_data_pa_offseton
XIP
riscv: drop the use ofXIP_OFFSETinXIP_FIXUP_OFFSET
riscv: drop the use ofXIP_OFFSETinXIP_FIXUP_FLASH_OFFSET
riscv: drop the use ofXIP_OFFSETinkernel_mapping_va_to_pa()
riscv: drop the use ofXIP_OFFSETincreate_kernel_page_table()
riscv: remove limit on the size of read-only section for XIP kernel
Quoted 7 lines
arch/riscv/include/asm/page.h | 25 ++++++++++++++++++------ arch/riscv/include/asm/pgtable.h | 18 +++++++---------- arch/riscv/include/asm/xip_fixup.h | 30 +++++++++++++++++++++++------ arch/riscv/kernel/vmlinux-xip.lds.S | 4 ++-- arch/riscv/mm/init.c | 11 +++++++---- 5 files changed, 59 insertions(+), 29 deletions(-)
XIP kernels are intended for use with flash devices so the XIP_OFFSET restriction actually represents the size of the flash device: IIRC this 32MB was chosen because it would fit "most devices". I think it would be good to come up with a mechanism that allows to restrict the size at build time: a config? XIP kernels are custom kernels so the user could enter its flash size so that if kernel ends up being too large, it fails. And by default, we could a very large size to avoid kernel test robot build failures.
Thanks,
Alex
Quoted 7 lines · Alexandre Ghiti
XIP kernels are intended for use with flash devices so the
XIP_OFFSETrestriction actually represents the size of the flash device: IIRC this 32MB was chosen because it would fit "most devices". I think it would be good to come up with a mechanism that allows to restrict the size at build time: a config? XIP kernels are custom kernels so the user could enter its flash size so that if kernel ends up being too large, it fails. And by default, we could a very large size to avoid kernel test robot build failures.
I'm not sure if it is beneficial to do that. Users' flash tool would complain about the kernel not fitting in flash anyway. I think this would only make it (a bit more) complicated to build the kernel.
Furthermore XIP_OFFSET at the moment is not the flash size, it is size of .text (and some other read-only sections). Kernel's data sections are in flash too, which is copied to RAM during boot.
With the current linker setting, the first 32MB of virtual memory is occupied by .text. Then at 32MB offset, .data section starts. So if XIP_OFFSET limit is exceeded, the kernel's size is already way more than 32MB.
Instead, this series moves .data and .text right next to each other (well, almost, because we need PMD_SIZE alignment). So that if .text size exceed 32MB, the offset that .data resides would increase as well.
If we really want to set flash size during build time (which, again, I doubt the benefit of), this series would still be relevant: it is not just removing the size limit, it is also removing the fixed position of the .data section in virtual address space (in other words, removing the useless "hole" between .text section and .data section).
Best regards,
Nam