support RV32, nobios and timestamp print for ch1&ch2#4
Closed
Ivans-11 wants to merge 2 commits intorcore-os:mainfrom
Closed
support RV32, nobios and timestamp print for ch1&ch2#4Ivans-11 wants to merge 2 commits intorcore-os:mainfrom
Ivans-11 wants to merge 2 commits intorcore-os:mainfrom
Conversation
|
My suggestions:
|
Collaborator
Author
Create a new crate? Does |
chyyuu
pushed a commit
that referenced
this pull request
Mar 22, 2026
问题根因 lld 链接器的 .symtab(SHT_SYMTAB)和 .strtab(SHT_STRTAB)是链接器自行创建的特殊节,VMA=0、无 SHF_ALLOC 标志,不会被 QEMU -kernel 加载到内存。旧代码试图把它们合并进 .rodata 会报节类型冲突;即使放到独立输出节,VMA 仍为 0。运行时解引用地址 0 直接崩溃,连 Hello, world! 都无法输出。 解决方案:编译时 ELF 解析 + const 符号表嵌入 放弃运行时读 ELF .symtab,改为 build.rs 在编译前从上一次构建产物中提取函数符号,以 const 数组嵌入二进制。 改动的文件: build.rs — 新增 extract_func_symbols() 和 write_func_syms_rs():直接解析 ELF64 的节头定位 .symtab+.strtab,过滤 STT_FUNC 符号(地址在 0x80200000..0x81000000),按地址排序后生成 OUT_DIR/func_syms_generated.rs。通过 cargo:rerun-if-changed 监控产物路径,产物出现或变化时自动重新提取。同时从链接脚本中移除了无效的 .symtab/.strtab 输出节。 src/symtab_resolve.rs — 完全重写:通过 include!() 引入生成的 const FUNC_SYMS: &[(u64, u64, &str)],二分查找匹配 ra,直接用 rustc_demangle + SbiWriter(core::fmt::Write)输出到串口。不再需要 alloc、Vec、运行时 ELF 解析、unsafe extern "C" 引用。 src/main.rs — 移除了 symtab_ptrs.S 的 global_asm! 引用(不再需要汇编中转指针)。 asm/symtab_ptrs.S — 已删除。 test_lec2_lab1.sh — 在 cargo run 前加了一次 cargo build(确保符号表已填充),并增加了对 bt_depth 和 rust_main 函数名的检查(共 20 项)。 运行效果: [BACKTRACE] #0 fp=... ra=0x... [BACKTRACE] fn=tg_rcore_tutorial_ch1_show::stackwalk::print_backtrace [BACKTRACE] #1 fp=... ra=0x... [BACKTRACE] fn=tg_rcore_tutorial_ch1_show::bt_depth3 [BACKTRACE] #2 fp=... ra=0x... [BACKTRACE] fn=tg_rcore_tutorial_ch1_show::bt_depth2 [BACKTRACE] #3 fp=... ra=0x... [BACKTRACE] fn=tg_rcore_tutorial_ch1_show::bt_depth1 [BACKTRACE] #4 fp=... ra=0x... [BACKTRACE] fn=tg_rcore_tutorial_ch1_show::rust_main [BACKTRACE] end=ra_null_bottom_of_chain 从干净构建算起需要两次 cargo build(首次无上轮产物→空表;第二次提取到 554 个函数符号)。.text 在链接脚本中位于 .rodata 之前,函数地址不受符号数据大小变化影响,因此两次构建即可收敛。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
为 ch1 和 ch2 添加了以下主要功能:
println!和日志输出中自动显示时间戳主要变更
1. nobios 模式支持
命令行参数
xtask中添加--nobios参数-bios none参数0x80200000改为0x80000000(M-Mode 入口)新增文件
M-Mode 入口汇编:
ch1/src/m_entry_rv64.asm- RV64 M-Mode 入口ch1/src/m_entry_rv32.asm- RV32 M-Mode 入口ch1-lab/src/m_entry_rv64.asm/m_entry_rv32.asmch2/src/m_entry_rv64.asm/m_entry_rv32.asmSBI 相关模块:
ch1/src/sbi.rs- SBI 服务调用实现(不依赖 sbi-rt)ch1/src/msbi.rs- M-Mode SBI 处理器(处理来自 S-Mode 的 ecall)ch1-lab和ch2修改文件
xtask/src/main.rs:BuildArgs::nobios字段-bios none各章节的
build.rs:CARGO_FEATURE_NOBIOS环境变量生成不同的链接脚本各章节的
Cargo.toml:nobiosfeaturesbi-rt依赖各章节的
main.rs:sbi模块替代sbi-rtlinker/src/lib.rs:NOBIOS_SCRIPT常量,包含完整的 nobios 模式链接脚本build.rs可使用NOBIOS_SCRIPT2. RV32 架构支持
命令行参数
xtask中添加--arch riscv32参数(默认riscv64)新增功能
xtask/src/main.rs:Arch枚举(Riscv32,Riscv64)Arch::target()- 返回 Rust target tripleArch::qemu_system()- 返回 QEMU 系统名称Arch::target_dir()- 返回目标目录路径xtask/src/user.rs:build_for()和build_one()接受Arch参数.word(RV32)或.quad(RV64)指令.align 2或.align 3)架构相关修复
kernel-context/src/lib.rs:execute_naked()函数sw/lw指令和 4 字节偏移sd/ld指令和 8 字节偏移execute()函数也根据指针宽度条件编译linker/src/app.rs:AppMeta结构体字段从u64改为usizeAppIterator::i从u64改为usizeuser/src/heap.rs:ch2/src/main.rs:usize(避免栈溢出覆盖LocalContext)3. 时间戳功能
新增文件
ch1-lab/src/timer.rs:get_time()- 读取 RISC-V time CSRget_time_ms()- 返回毫秒数rdtimeh和rdtime)和 RV64(使用rdtime)ch2/src/timer.rs:同上修改文件
console/src/lib.rs:GetTimeMsFn类型和GET_TIME_MS静态变量set_timestamp()函数用于注册时间戳获取函数_print_timestamp()使用注册的函数println!宏在输出前打印时间戳各章节的
main.rs:mod timer;rcore_console::set_timestamp(timer::get_time_ms)运行
运行命令