Skip to content

bsp: cvitek: riscv: use marco for linker script #10202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025

Conversation

unicornx
Copy link
Contributor

@unicornx unicornx commented Apr 18, 2025

原先的链接脚本为支持 标准版和 smart 版之间一些内存偏移量和大小不同的问题,(譬如 "STACKSIZE", 内核起始地址,内核所占内存大小等),采用替换链接脚本和 INCLUDE 的机制,比较复杂。

本次改进,在链接脚本中利用宏替换常量。利用 gcc 提供的预处理机制,在构建过程中动态替换常量值。

本次改动还顺便改好了一个以前的老问题:

Fixed #9697

原先对于内核加载地址(smart 的是对应的 KERNEL_VADDR_START),在配置中提前加上了 offset(0x200000),反而引起麻烦,导致如果先编译了一个smart版本,切换成标准版本,再切换回 smart 版本时,由于默认的 KERNEL_VADDR_START 值和配置值不同,导致需要手动再配置一遍,很容易遗忘导致问题。现在的解决方法是配置中采用默认的值 0xffffffc000000000。具体使用时在代码和链接脚本中再加上 offset。

本次提交还刷新了 "cv18xx_riscv" bsp 的默认的 .configrtconfig.h 文件。

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

你的解决方案是什么 (what is your solution)

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/workflows/bsp_buildings.yml 详细请参考链接BSP自查

@unicornx unicornx added BSP: Cvitek BSP related with cvitek Arch: RISC-V BSP related with risc-v RT-Smart RT-Thread Smart related PR or issues labels Apr 18, 2025
@github-actions github-actions bot added the BSP label Apr 18, 2025
@unicornx unicornx requested a review from Rbb666 April 18, 2025 07:36
@supperthomas supperthomas requested a review from Copilot April 18, 2025 13:47
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the memory configuration for the CVITEK RISC-V BSP by using macro replacements in the linker script and cleaning up related memory layout configurations. Key changes include updating the linker flags to use a generated linker script, revising kernel virtual address definitions and version numbers, and adding a new script (gen_ld.py) to dynamically generate the linker script.

Reviewed Changes

Copilot reviewed 8 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
bsp/cvitek/cv18xx_risc-v/rtconfig.py Updated the linker flags to reference the generated linker script.
bsp/cvitek/cv18xx_risc-v/rtconfig.h Revised kernel virtual address and version definitions; removed redundant blocks.
bsp/cvitek/cv18xx_risc-v/gen_ld.py Introduced a new script to generate the linker script dynamically.
bsp/cvitek/cv18xx_risc-v/board/mem_layout.h Added memory layout definitions; note potential naming typos.
bsp/cvitek/cv18xx_risc-v/board/board.c Adjusted the kernel start address references to use the new memory layout macros.
bsp/cvitek/cv18xx_risc-v/SConstruct Removed the legacy stack linker script generation and integrated the new generation logic.
bsp/cvitek/README.md Updated documentation to reflect the new default kernel virtual address.
Files not reviewed (5)
  • bsp/cvitek/cv18xx_risc-v/.config: Language not supported
  • bsp/cvitek/cv18xx_risc-v/.gitignore: Language not supported
  • bsp/cvitek/cv18xx_risc-v/link.lds: Language not supported
  • bsp/cvitek/cv18xx_risc-v/link_smart.lds: Language not supported
  • bsp/cvitek/cv18xx_risc-v/link_stacksize.lds: Language not supported

Comment on lines 12 to 19
#define KERAL_OFFSETT 0x200000

#ifdef RT_USING_SMART
#define KERNEL_START KERNEL_VADDR_START + KERAL_OFFSETT
#define KERENL_SIZE 64 * 1024 * 1024
#else
#define KERNEL_START 0x80000000 + KERAL_OFFSETT
#define KERENL_SIZE 32 * 1024 * 1024
Copy link
Preview

Copilot AI Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro name 'KERAL_OFFSETT' appears to be misspelled. Consider renaming it to 'KERNEL_OFFSET' for clarity and consistency.

Suggested change
#define KERAL_OFFSETT 0x200000
#ifdef RT_USING_SMART
#define KERNEL_START KERNEL_VADDR_START + KERAL_OFFSETT
#define KERENL_SIZE 64 * 1024 * 1024
#else
#define KERNEL_START 0x80000000 + KERAL_OFFSETT
#define KERENL_SIZE 32 * 1024 * 1024
#define KERNEL_OFFSET 0x200000
#ifdef RT_USING_SMART
#define KERNEL_START KERNEL_VADDR_START + KERNEL_OFFSET
#define KERENL_SIZE 64 * 1024 * 1024
#else
#define KERNEL_START 0x80000000 + KERNEL_OFFSET

Copilot uses AI. Check for mistakes.

#ifndef MEMORY_LAYOUT_H__
#define MEMORY_LAYOUT_H__

#include "../rtconfig.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块 #include "rtconfig.h" 就可以了吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块 #include "rtconfig.h" 就可以了吧

不行哎,我发现不加 “../”, 在 “bsp/cvitek/cv18xx_risc-v/gen_ld.py" 中会找不到 ”rtconfig.h“,导致无法生成 link.lds.generated。

@unicornx unicornx requested a review from Rbb666 April 21, 2025 03:48
@supperthomas supperthomas requested a review from Copilot April 21, 2025 06:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR simplifies the linker script configuration for the CVITEK RISC-V BSP by replacing multiple script variants and hard-coded memory offsets with a macro‐based approach using gcc’s preprocessor. Key changes include updating linker flags in rtconfig.py, revising version and memory address definitions in rtconfig.h, and introducing a new ldscript generator (gen_ld.py) along with corresponding updates in memory layout and board files.

Reviewed Changes

Copilot reviewed 8 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
bsp/cvitek/cv18xx_risc-v/rtconfig.py Updated linker flags to use a generated linker script.
bsp/cvitek/cv18xx_risc-v/rtconfig.h Revised version and kernel virtual address macros.
bsp/cvitek/cv18xx_risc-v/gen_ld.py New script to generate linker scripts using the gcc preprocessor.
bsp/cvitek/cv18xx_risc-v/board/mem_layout.h New header defining memory layout constants with kernel offset.
bsp/cvitek/cv18xx_risc-v/board/board.h Removed obsolete heap and page memory definitions.
bsp/cvitek/cv18xx_risc-v/board/board.c Updated memory descriptor to use the new memory layout header.
bsp/cvitek/cv18xx_risc-v/SConstruct Integrated generation of the linker script into the build flow.
bsp/cvitek/README.md Updated documentation to reflect the new kernel virtual address.
Files not reviewed (5)
  • bsp/cvitek/cv18xx_risc-v/.config: Language not supported
  • bsp/cvitek/cv18xx_risc-v/.gitignore: Language not supported
  • bsp/cvitek/cv18xx_risc-v/link.lds: Language not supported
  • bsp/cvitek/cv18xx_risc-v/link_smart.lds: Language not supported
  • bsp/cvitek/cv18xx_risc-v/link_stacksize.lds: Language not supported

Comment on lines 15 to 19
#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
Copy link
Preview

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider wrapping the expression in parentheses, e.g., '#define KERNEL_START (KERNEL_VADDR_START + KERNEL_OFFSET)', to ensure correct operator precedence in all usage contexts.

Suggested change
#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
#define KERNEL_START (KERNEL_VADDR_START + KERNEL_OFFSET)
#define KERNEL_SIZE 64 * 1024 * 1024
#else
#define KERNEL_START (0x80000000 + KERNEL_OFFSET)

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

接受

Comment on lines +23 to +24
child = subprocess.Popen(gcc_cmd + f' -E -P -x c {input} -o {output}', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

Copy link
Preview

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using a consistent argument format for subprocess.Popen across platforms (e.g., always passing a list of arguments) to avoid potential argument parsing issues.

Suggested change
child = subprocess.Popen(gcc_cmd + f' -E -P -x c {input} -o {output}', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
child = subprocess.Popen([gcc_cmd, '-E', '-P', '-x', 'c', input, '-o', output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

尝试不成功,linux 上无法预处理生成,保持原写法。

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 <[email protected]>
@Rbb666 Rbb666 merged commit e7d870b into RT-Thread:master Apr 22, 2025
33 checks passed
@unicornx unicornx deleted the dev-gen-ld branch April 22, 2025 02:13
@unicornx unicornx mentioned this pull request Apr 23, 2025
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arch: RISC-V BSP related with risc-v BSP: Cvitek BSP related with cvitek BSP RT-Smart RT-Thread Smart related PR or issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] bsp/cvitek/cv18xx_riscv KERNEL_VADDR_START 的初始配置值丢失问题
2 participants