bsp: cvitek: riscv: use marco for linker script

Regarding link script, some memory offsets and sizes are different
between the standard version and the smart version (such as
"__STACKSIZE__", kernel start address, kernel memory size, etc.).
Original solution is replacing link scripts and use INCLUDE,
which is relatively complicated.

This improvement uses macros to replace constants in the link
script. The preprocessing mechanism provided by gcc is used
to dynamically replace constant values during the build process.

In addition, the kernel load address (the corresponding
KERNEL_VADDR_START for smart) was originally configured as
0xFFFFFFC000200000, which is default value of riscv with
kernelmap enabled (0xffffffc000000000) plus offset to skip over
bootloader (0x200000). This caused a trouble: due to
default bsp configuration is for smart, if we switched to the
standard version and build, and then switched back to the smart
version, the value of KERNEL_VADDR_START will be default back to
0xffffffc000000000, which is different from the original configuration
value, resulting in the need to manually reconfigure it, which
is easy to forget and cause problems.

The current solution is to use the default value
0xffffffc000000000 in the configuration. Add offset to the code
and link script when using it.

This patch update the default .config and rtconfig.h for cv18xx_riscv.

Signed-off-by: Chen Wang <unicorn_wang@outlook.com>
This commit is contained in:
Chen Wang 2025-04-18 12:02:59 +08:00 committed by Rbb666
parent 7371a007af
commit e7d870bb71
13 changed files with 106 additions and 261 deletions

View File

@ -163,11 +163,11 @@ RT-Thread Kernel --->
[*] Enable RT-Thread Smart (microkernel on kernel/userland)
```
**注意检查内核虚拟起始地址的配置,确保为 `0xFFFFFFC000200000`。**
**注意检查内核虚拟起始地址的配置,确保为 `0xFFFFFFC000000000`。**
```shell
RT-Thread Kernel --->
(0xFFFFFFC000200000) The virtural address of kernel start
(0xFFFFFFC000000000) The virtural address of kernel start
RT-Thread Components --->
```

View File

@ -155,7 +155,6 @@ CONFIG_RT_DEBUGING_ASSERT=y
CONFIG_RT_DEBUGING_COLOR=y
CONFIG_RT_DEBUGING_CONTEXT=y
# CONFIG_RT_DEBUGING_AUTO_INIT is not set
# CONFIG_RT_DEBUGING_PAGE_LEAK is not set
# CONFIG_RT_USING_CI_ACTION is not set
#
@ -173,7 +172,6 @@ CONFIG_RT_USING_MESSAGEQUEUE=y
#
# Memory Management
#
CONFIG_RT_PAGE_MAX_ORDER=11
# CONFIG_RT_USING_MEMPOOL is not set
# CONFIG_RT_USING_SMALL_MEM is not set
CONFIG_RT_USING_SLAB=y
@ -195,7 +193,7 @@ CONFIG_RT_USING_DEVICE_OPS=y
CONFIG_RT_USING_CONSOLE=y
CONFIG_RT_CONSOLEBUF_SIZE=256
CONFIG_RT_CONSOLE_DEVICE_NAME="uart0"
CONFIG_RT_VER_NUM=0x50200
CONFIG_RT_VER_NUM=0x50201
CONFIG_RT_USING_STDC_ATOMIC=y
CONFIG_RT_BACKTRACE_LEVEL_MAX_NR=32
# end of RT-Thread Kernel
@ -203,7 +201,7 @@ CONFIG_RT_BACKTRACE_LEVEL_MAX_NR=32
CONFIG_ARCH_CPU_64BIT=y
CONFIG_RT_USING_CACHE=y
CONFIG_ARCH_MM_MMU=y
CONFIG_KERNEL_VADDR_START=0xFFFFFFC000200000
CONFIG_KERNEL_VADDR_START=0xffffffc000000000
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_RISCV_FPU=y
CONFIG_ARCH_RISCV_FPU_D=y
@ -274,6 +272,7 @@ CONFIG_RT_DFS_ELM_MUTEX_TIMEOUT=3000
CONFIG_RT_USING_DFS_DEVFS=y
CONFIG_RT_USING_DFS_ROMFS=y
CONFIG_RT_USING_DFS_PTYFS=y
# CONFIG_RT_USING_DFS_PROCFS is not set
# CONFIG_RT_USING_DFS_CROMFS is not set
CONFIG_RT_USING_DFS_TMPFS=y
# CONFIG_RT_USING_DFS_MQUEUE is not set
@ -533,9 +532,25 @@ CONFIG_RT_USING_ADT_REF=y
# end of Utilities
# CONFIG_RT_USING_VBUS is not set
#
# Memory management
#
CONFIG_RT_PAGE_AFFINITY_BLOCK_SIZE=0x1000
CONFIG_RT_PAGE_MAX_ORDER=11
# CONFIG_RT_USING_MEMBLOCK is not set
#
# Debugging
#
# CONFIG_RT_DEBUGGING_ALIASING is not set
# CONFIG_RT_DEBUGING_PAGE_LEAK is not set
# CONFIG_RT_DEBUGGING_PAGE_POISON is not set
# end of Debugging
# end of Memory management
CONFIG_RT_USING_LWP=y
CONFIG_LWP_DEBUG=y
# CONFIG_LWP_DEBUG_INIT is not set
# CONFIG_LWP_DEBUG is not set
CONFIG_LWP_USING_RUNTIME=y
CONFIG_RT_LWP_MAX_NR=30
CONFIG_LWP_TASK_STACK_SIZE=16384
@ -548,12 +563,6 @@ CONFIG_RT_USING_LDSO=y
CONFIG_LWP_USING_TERMINAL=y
CONFIG_LWP_PTY_MAX_PARIS_LIMIT=64
#
# Memory management
#
# CONFIG_RT_USING_MEMBLOCK is not set
# end of Memory management
#
# Using USB legacy version
#

View File

@ -1,2 +1,4 @@
dtb/
Image
Image
link.lds.generated

View File

@ -29,20 +29,11 @@ drivers_path_prefix = os.path.dirname(SDK_ROOT) + '/drivers'
# prepare building environment
objs = PrepareBuilding(env, RTT_ROOT, has_libcpu = False)
if GetDepend('RT_USING_SMART'):
# use smart link.lds
env['LINKFLAGS'] = env['LINKFLAGS'].replace('link.lds', 'link_smart.lds')
# include libraries
objs.extend(SConscript(drivers_path_prefix + '/SConscript', variant_dir='build/drivers', duplicate=0))
stack_size = 4096
stack_lds = open('link_stacksize.lds', 'w')
if GetDepend('__STACKSIZE__'):
stack_size = GetDepend('__STACKSIZE__')
stack_lds.write('__STACKSIZE__ = %d;\n' % stack_size)
stack_lds.close()
from gen_ld import *
generate_ldscript('link.lds', 'link.lds.generated')
# make a building
DoBuilding(TARGET, objs)

View File

@ -14,21 +14,32 @@
#include "sbi.h"
extern unsigned int __sram_end;
#define RAM_END (rt_size_t)((void *)&__sram_end)
extern unsigned int __bss_start;
extern unsigned int __bss_end;
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
#define RT_HW_HEAP_END ((void *)((rt_size_t)RT_HW_HEAP_BEGIN + 8 * 1024 * 1024))
#define RT_HW_PAGE_START RT_HW_HEAP_END
#define RT_HW_PAGE_END ((void *)(RAM_END))
#ifdef RT_USING_SMART
#include "riscv_mmu.h"
#include "mmu.h"
#include "page.h"
#include "lwp_arch.h"
/* respect to boot loader, must be 0xFFFFFFC000200000 */
RT_STATIC_ASSERT(kmem_region, KERNEL_VADDR_START == 0xFFFFFFC000200000);
#include "mem_layout.h"
rt_region_t init_page_region = {(rt_size_t)RT_HW_PAGE_START, (rt_size_t)RT_HW_PAGE_END};
extern size_t MMUTable[];
struct mem_desc platform_mem_desc[] = {
{KERNEL_VADDR_START, (rt_size_t)RT_HW_PAGE_END - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
{KERNEL_START, (rt_size_t)RT_HW_PAGE_END - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
};
#define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))

View File

@ -15,18 +15,6 @@
#include "drv_uart.h"
#include "tick.h"
extern unsigned int __sram_end;
#define RAM_END (rt_size_t)((void *)&__sram_end)
extern unsigned int __bss_start;
extern unsigned int __bss_end;
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
#define RT_HW_HEAP_END ((void *)((rt_size_t)RT_HW_HEAP_BEGIN + 8 * 1024 * 1024))
#define RT_HW_PAGE_START RT_HW_HEAP_END
#define RT_HW_PAGE_END ((void *)(RAM_END))
void rt_hw_board_init(void);
#endif

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef MEMORY_LAYOUT_H__
#define MEMORY_LAYOUT_H__
#include "../rtconfig.h"
/* Real start address of kernel is located after bootloader */
#define KERNEL_OFFSET 0x200000
#ifdef RT_USING_SMART
#define KERNEL_START (KERNEL_VADDR_START + KERNEL_OFFSET)
#define KERNEL_SIZE 64 * 1024 * 1024
#else
#define KERNEL_START (0x80000000 + KERNEL_OFFSET)
#define KERNEL_SIZE 32 * 1024 * 1024
#endif
#endif // MEMORY_LAYOUT_H__

View File

@ -0,0 +1,27 @@
import os
import rtconfig
import platform
import subprocess
def generate_ldscript(input, output):
if not os.path.exists(input):
print('Error: file', input, 'not found')
return
if os.path.exists(output):
os.remove(output)
if rtconfig.PLATFORM == 'gcc':
gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
# gcc -E -P -x c $input -o $output
if (platform.system() == 'Windows'):
child = subprocess.Popen([gcc_cmd, '-E', '-P', '-x', 'c', input, '-o', output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
else:
child = subprocess.Popen(gcc_cmd + f' -E -P -x c {input} -o {output}', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
child.communicate()
print(output, 'is generated from', input)

View File

@ -8,20 +8,13 @@
* 2020/12/12 bernard The first version
*/
INCLUDE "link_stacksize.lds"
#include "board/mem_layout.h"
OUTPUT_ARCH( "riscv" )
/*
* Memory layout:
* 0x10200000 - 0x10201000: Bootloader
* 0x10201000 - 0x10A00000: Kernel
* 0x10A00000 - 0x11200000: Heap
*/
MEMORY
{
SRAM(wx): ORIGIN = 0x80200000, LENGTH = 32M
SRAM(wx): ORIGIN = KERNEL_START, LENGTH = KERNEL_SIZE
}
ENTRY(_start)
@ -29,7 +22,6 @@ SECTIONS
{
. = ORIGIN(SRAM) ;
/* __STACKSIZE__ = 4096; */
__sram_base = ORIGIN(SRAM);
__sram_size = LENGTH(SRAM);
__sram_end = __sram_base + __sram_size;

View File

@ -1,200 +0,0 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020/12/12 bernard The first version
*/
INCLUDE "link_stacksize.lds"
OUTPUT_ARCH( "riscv" )
/*
* Memory layout:
* 0x10200000 - 0x10201000: Bootloader
* 0x10201000 - 0x10A00000: Kernel
* 0x10A00000 - 0x11200000: Heap
*/
MEMORY
{
SRAM(wx): ORIGIN = 0xFFFFFFC000200000, LENGTH = 64M
}
ENTRY(_start)
SECTIONS
{
. = ORIGIN(SRAM) ;
/* __STACKSIZE__ = 4096; */
__sram_base = ORIGIN(SRAM);
__sram_size = LENGTH(SRAM);
__sram_end = __sram_base + __sram_size;
__text_start = .;
.start :
{
*(.start);
} > SRAM
. = ALIGN(8);
.text :
{
*(.text) /* remaining code */
*(.text.*) /* remaining code */
*(.rodata) /* read-only data (constants) */
*(.rodata*)
*(.glue_7)
*(.glue_7t)
*(.gnu.linkonce.t*)
/* section information for finsh shell */
. = ALIGN(8);
__fsymtab_start = .;
KEEP(*(FSymTab))
__fsymtab_end = .;
. = ALIGN(8);
__vsymtab_start = .;
KEEP(*(VSymTab))
__vsymtab_end = .;
. = ALIGN(8);
/* section information for initial. */
. = ALIGN(8);
__rt_init_start = .;
KEEP(*(SORT(.rti_fn*)))
__rt_init_end = .;
. = ALIGN(8);
__rt_utest_tc_tab_start = .;
KEEP(*(UtestTcTab))
__rt_utest_tc_tab_end = .;
. = ALIGN(8);
_etext = .;
} > SRAM
.eh_frame_hdr :
{
*(.eh_frame_hdr)
*(.eh_frame_entry)
} > SRAM
.eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) } > SRAM
. = ALIGN(8);
__text_end = .;
__text_size = __text_end - __text_start;
.data :
{
*(.data)
*(.data.*)
*(.data1)
*(.data1.*)
. = ALIGN(8);
PROVIDE( __global_pointer$ = . + 0x800 );
*(.sdata)
*(.sdata.*)
} > SRAM
. = ALIGN(8);
.ctors :
{
PROVIDE(__ctors_start__ = .);
KEEP(*(SORT(.init_array.*)))
KEEP(*(.init_array))
PROVIDE(__ctors_end__ = .);
} > SRAM
.dtors :
{
PROVIDE(__dtors_start__ = .);
KEEP(*(SORT(.fini_array.*)))
KEEP(*(.fini_array))
PROVIDE(__dtors_end__ = .);
} > SRAM
/* stack for dual core */
.stack :
{
. = ALIGN(64);
__stack_start__ = .;
. += __STACKSIZE__;
__stack_cpu0 = .;
. += __STACKSIZE__;
__stack_cpu1 = .;
} > SRAM
. = ALIGN(8);
.osdebug :
{
_osdebug_start = .;
. += 87K;
_osdebug_end = .;
} > SRAM
. = ALIGN(8);
.sbss :
{
__bss_start = .;
*(.sbss)
*(.sbss.*)
*(.dynsbss)
*(.scommon)
} > SRAM
.bss :
{
*(.bss)
*(.bss.*)
*(.dynbss)
*(COMMON)
__bss_end = .;
} > SRAM
_end = .;
/* Stabs debugging sections. */
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
/* DWARF debug sections.
* Symbols in the DWARF debugging sections are relative to the beginning
* of the section so we begin them at 0. */
/* DWARF 1 */
.debug 0 : { *(.debug) }
.line 0 : { *(.line) }
/* GNU DWARF 1 extensions */
.debug_srcinfo 0 : { *(.debug_srcinfo) }
.debug_sfnames 0 : { *(.debug_sfnames) }
/* DWARF 1.1 and DWARF 2 */
.debug_aranges 0 : { *(.debug_aranges) }
.debug_pubnames 0 : { *(.debug_pubnames) }
/* DWARF 2 */
.debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_line 0 : { *(.debug_line) }
.debug_frame 0 : { *(.debug_frame) }
.debug_str 0 : { *(.debug_str) }
.debug_loc 0 : { *(.debug_loc) }
.debug_macinfo 0 : { *(.debug_macinfo) }
/* SGI/MIPS DWARF 2 extensions */
.debug_weaknames 0 : { *(.debug_weaknames) }
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }
}

View File

@ -1 +0,0 @@
__STACKSIZE__ = 8192;

View File

@ -109,7 +109,6 @@
/* Memory Management */
#define RT_PAGE_MAX_ORDER 11
#define RT_USING_SLAB
#define RT_USING_SLAB_AS_HEAP
#define RT_USING_HEAP
@ -119,14 +118,14 @@
#define RT_USING_CONSOLE
#define RT_CONSOLEBUF_SIZE 256
#define RT_CONSOLE_DEVICE_NAME "uart0"
#define RT_VER_NUM 0x50200
#define RT_VER_NUM 0x50201
#define RT_USING_STDC_ATOMIC
#define RT_BACKTRACE_LEVEL_MAX_NR 32
/* end of RT-Thread Kernel */
#define ARCH_CPU_64BIT
#define RT_USING_CACHE
#define ARCH_MM_MMU
#define KERNEL_VADDR_START 0xFFFFFFC000200000
#define KERNEL_VADDR_START 0xffffffc000000000
#define ARCH_RISCV
#define ARCH_RISCV_FPU
#define ARCH_RISCV_FPU_D
@ -348,8 +347,17 @@
#define RT_USING_ADT_HASHMAP
#define RT_USING_ADT_REF
/* end of Utilities */
/* Memory management */
#define RT_PAGE_AFFINITY_BLOCK_SIZE 0x1000
#define RT_PAGE_MAX_ORDER 11
/* Debugging */
/* end of Debugging */
/* end of Memory management */
#define RT_USING_LWP
#define LWP_DEBUG
#define LWP_USING_RUNTIME
#define RT_LWP_MAX_NR 30
#define LWP_TASK_STACK_SIZE 16384
@ -360,10 +368,6 @@
#define LWP_USING_TERMINAL
#define LWP_PTY_MAX_PARIS_LIMIT 64
/* Memory management */
/* end of Memory management */
/* Using USB legacy version */
/* end of Using USB legacy version */

View File

@ -43,7 +43,7 @@ if PLATFORM == 'gcc':
DEVICE = ' -mcmodel=medany -march=rv64imafdc -mabi=lp64'
CFLAGS = DEVICE + ' -Wno-cpp -fvar-tracking -ffreestanding -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -D_POSIX_SOURCE '
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -D__ASSEMBLY__'
LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,_start -T link.lds' + ' -lsupc++ -lgcc -static'
LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,_start -T link.lds.generated' + ' -lsupc++ -lgcc -static'
CPATH = ''
LPATH = ''