Skip to content

Feat: Loongarch run through - #266

Merged
a6d9a6m merged 20 commits into
mainfrom
feat/loongarch-run-through
Jun 24, 2026
Merged

Feat: Loongarch run through#266
a6d9a6m merged 20 commits into
mainfrom
feat/loongarch-run-through

Conversation

@a6d9a6m

@a6d9a6m a6d9a6m commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

概述

本 PR 将 LoongArch 分支推进到可以稳定启动并跑通当前 OSComp musl 冒烟测试的状态。修复范围覆盖早期 MMIO 映射、地址空间切换、TLB refill、用户态返回、启动 trap frame、FPU 上下文保存恢复、RTC、timer 驱动网络轮询,以及官方测试镜像的挂载运行流程。

当前 LoongArch 的 rcS 仍然是明确白名单,只自动运行以下四组 musl 测试:

  • basic_testcode.sh
  • busybox_testcode.sh
  • lua_testcode.sh
  • iperf_testcode.sh

主要修改

LoongArch MMIO 与地址空间

  • 增加架构相关的 mmio_pa_to_va() 路径,让 LoongArch MMIO 使用非缓存的 DMW0 直接映射窗口,而不是走缓存 RAM direct map。
  • 克隆地址空间时不再重复映射 LoongArch 的 direct MMIO 元数据;direct window 本身已经提供有效映射。
  • 保持最终内核页表根写入 PGDH,用户地址空间使用 PGDL,避免切换用户地址空间后丢失高半区内核映射。

LoongArch Trap 与 TLB Refill

  • 修正 LoongArch TLB refill 的页表遍历层级,使其与实际启用的页表结构一致:Dir3 -> Dir2 -> Dir1 -> PTE
  • 用户态返回时让 ertnPRMD 恢复特权级,不再在 restore 路径中手动改写 CRMD
  • 将启动阶段的 SpinLock<TrapFrame> 替换为 raw UnsafeCell<TrapFrame> 存储。启动 trap frame 会被 trap_entry 异步写入,普通自旋锁无法作为这里的同步保护,并且会在早期 trap 设置中被破坏。
  • 在 trap 入口和返回路径保存恢复 LoongArch 用户 FPU 状态,包括 f0-f31fcsr0 和打包的 fcc0-fcc7。信号 mcontext.fpregs 也使用同一份状态布局。

时间、RTC 与网络轮询

  • LoongArch 毫秒时间改为基于硬件时间和 clock_freq() 换算,不再依赖调度 tick。
  • 增加 QEMU LoongArch virt 平台的 LS7A RTC 支持,即 loongson,ls7a-rtc,同时保留 goldfish RTC 支持。
  • 在 LoongArch timer tick 中轮询网络设备并唤醒 poll 等待者,解决 loopback/null-net 路径没有设备中断时 iperf 无法推进的问题。

测试镜像与运行流程

  • 官方 OSComp 测试镜像不再复制到本 worktree 内,直接作为可写 QEMU block device 挂载使用。
  • make run-laos/qemu-loongarch-run.sh 支持通过 TESTIMG_LA=/path/to/sdcard-la.img 指定测试镜像;默认也会查找 ../CCYOS/sdcard-la.img
  • LoongArch rcS 将官方 raw ext4 测试镜像从 /dev/vdb 挂载到 /tests,并在原地运行四组白名单 musl 测试,不再把测试二进制复制进 tmpfs。

ext4 目录校验和加固

  • vendor 了一处本分支依赖的 ext4_rs 目录 checksum 修复,避免目录 checksum 处理造成栈破坏。
  • 该问题会在遍历官方大测试镜像目录时触发,修复后目录遍历和测试脚本发现流程保持稳定。

验证

构建已通过:

make kernel-la disk-la.img PROFILE=debug

使用官方测试镜像直接挂载运行 LoongArch QEMU:

qemu-system-loongarch64 \
  -machine virt \
  -kernel kernel-la \
  -m 4G \
  -nographic \
  -smp 1 \
  -no-reboot \
  -rtc base=utc \
  -drive file=disk-la.img,if=none,format=raw,id=x0 \
  -device virtio-blk-pci,drive=x0 \
  -drive file=/home/littlesand/Code/CCYOS/sdcard-la.img,if=none,format=raw,id=test0 \
  -device virtio-blk-pci,drive=test0 \
  -device virtio-net-pci,netdev=net0 \
  -netdev user,id=net0,hostfwd=tcp::5555-:5555,hostfwd=udp::5555-:5555

观察到 rcS 自动完成:

  • /dev/vdb 成功挂载到 /tests
  • basic_testcode.sh 返回 rc=0
  • busybox_testcode.sh 返回 rc=0
  • lua_testcode.sh 返回 rc=0
  • iperf_testcode.sh 返回 rc=0
  • BASIC_UDPBASIC_TCPPARALLEL_UDPPARALLEL_TCPREVERSE_UDPREVERSE_TCP 均输出 success
  • 系统打印 [Tests] all tests finished; powering off,QEMU 正常退出

注意事项与风险

  • LoongArch 当前仍只运行四组 musl 白名单测试,尚未扫描并执行 /tests/musl 下所有 *_testcode.sh
  • 正常运行形态下,官方测试镜像预期是第二个 block device,即 /dev/vdb。如果在它前面插入额外 block device,设备名会变化,自动挂载路径需要同步调整。
  • 当前 LoongArch iperf 吞吐量低于 RISC-V release 模式下的最好结果;本 PR 的目标是稳定启动、自动完成当前白名单测试,而不是完成网络性能优化。
  • docs/os-myself/big-bug-record.md 是本轮排查过程记录,虽然本地可能有修改,但不属于本 PR 文档更新范围,也不会随本 PR 提交。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for local test images outside the worktree, cleans up the LoongArch musl test initialization, and adds a no-std, cross-platform ext4_rs vendor crate. It also implements floating-point state saving/restoring, LS7A RTC support, and MMIO mapping for the LoongArch architecture. Key feedback includes a critical logic error in trap_entry.S that checks the old PLV instead of the target PLV before restoring floating-point state, a deadlock risk in check_timer due to synchronous network polling in the hard interrupt context, a bug in ext4_rs where inserting an extent overwrites existing extents without shifting them, and missing boundary checks in bitmap operations and Ext4DirEntryTail::copy_to_slice that could lead to panics or undefined behavior.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread os/src/arch/loongarch/trap/trap_entry.S
Comment thread os/src/arch/loongarch/trap/trap_handler.rs Outdated
Comment thread vendor/ext4_rs/src/ext4_impls/extents.rs Outdated
Comment thread vendor/ext4_rs/src/utils/bitmap.rs
Comment thread vendor/ext4_rs/src/ext4_defs/direntry.rs Outdated
@a6d9a6m
a6d9a6m merged commit 6d556a2 into main Jun 24, 2026
1 check passed
@a6d9a6m
a6d9a6m deleted the feat/loongarch-run-through branch June 24, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant