展示HN:将xv6移植到HiFive Unmatched板
Show HN: Porting xv6 to HiFive Unmatched board

原始链接: https://github.com/eyengin/xv6-riscv-unmatched

本文档详细介绍了将 xv6-riscv 操作系统移植到 SiFive HiFive Unmatched (FU740) 开发板的过程。该移植用 U-Boot SPL 替换了标准的 OpenSBI 引导加载程序,以实现直接的 M 模式引导,最大限度地减少内核修改,并利用 SPL 进行硬件初始化。一个由 SiFive 提供的本地 SD 卡驱动程序取代了 VirtIO 实现。 该过程包括克隆和构建 xv6 内核(专门针对“unmatched”)和 U-Boot SPL(推荐版本 v2023.01)。microSD 卡会被分区,并填充 U-Boot SPL 二进制文件、xv6 内核镜像(打包为 FIT 镜像 – `xv6-unmatched.itb`)和根文件系统镜像 (`fs.img`)。 要启动,板载的 MSEL 拨码开关必须设置为 SD 引导模式,并通过 UART 建立连接。成功启动会显示 U-Boot SPL 版本和 xv6 内核启动信息。该系统也可以使用 QEMU,并指定 `sifive_u` 机器类型进行仿真。

## xv6操作系统移植到HiFive Unmatched 一位开发者成功将教育用的xv6操作系统(基于RISC-V)移植到HiFive Unmatched开发板上,摆脱了QEMU模拟,实现在真机上运行。该项目源于开发者自学麻省理工学院的操作系统内部原理课程,并克服了多项挑战。 主要难点包括解决硬件特性问题,如页表位设置和中断处理,以及调整启动流程。开发者通过创建一个最小的U-Boot镜像来直接在M模式下启动xv6内核,绕过了标准的OpenSBI启动流程。此外,SPI SD卡驱动取代了虚拟磁盘驱动。 详细的实现说明可在GitHub上找到 ([https://github.com/eyengin/xv6-riscv-unmatched](https://github.com/eyengin/xv6-riscv-unmatched))。该移植旨在为其他学习操作系统开发,并寻求相对实惠的RISC-V实际实验平台的人们提供有价值的资源。
相关文章

原文

This is a port of xv6-riscv to the SiFive HiFive Unmatched board (FU740). See notes for the implementation details.

Example

  • Stability on actual hardware: usertests passed, resolving hardware-specific issues.
  • Direct M-mode boot via U-Boot SPL: Replaced the standard OpenSBI with xv6 kernel, leveraging SPL for hardware initialization and minimizing kernel modifications.
  • SD card driver: Ported the SPI mode SD card driver (from SiFive sources) to replace VirtIO.

Running on HiFive Unmatched

Host environment: Ubuntu 24.04

Install the required packages:

sudo apt update
sudo apt install git build-essential gdb-multiarch qemu-system-misc \
    gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu
sudo apt install flex bison libssl-dev python3-setuptools python3-dev \
    swig device-tree-compiler u-boot-tools
  1. Build xv6 kernel

Compile the xv6 kernel for the Unmatched target:

git clone https://github.com/eyengin/xv6-riscv-unmatched
cd xv6-riscv-unmatched
make unmatched

The unmatched target automatically converts the kernel ELF to a raw binary and packages it into a FIT image (xv6-unmatched.itb) using mkimage. This image includes the necessary metadata for U-Boot SPL to recognize it as a bootable firmware.

  1. Build U-Boot SPL

This port uses U-Boot SPL to load the xv6 kernel directly (replacing OpenSBI in the standard flow).

Clone and build v2023.01 (Note: newer versions failed to initialize CPU clocks correctly in the SPL phase):

git clone https://github.com/u-boot/u-boot.git
cd u-boot
git checkout v2023.01

make sifive_unmatched_defconfig
# Build SPL (only u-boot-spl.bin is needed for this project)
make CROSS_COMPILE=riscv64-linux-gnu- spl/u-boot-spl.bin

Prepare a microSD card (SDHC; tested with SanDisk Ultra 32GB UHS-I A1).

Warning: Replace /dev/sdX with your actual SD card device. Double-check the device name to avoid overwriting the wrong disk.

Use sgdisk to partition the SD card:

sudo sgdisk -g --clear -a 1 \
  --new=1:34:2081         --change-name=1:spl   --typecode=1:5B193300-FC78-40CD-8002-E86C45580B47 \
  --new=2:2082:10273      --change-name=2:xv6   --typecode=2:2E54B353-1271-4842-806F-E436D6AF6985 \
  --new=3:16384:+128M     --change-name=3:root  --typecode=3:0x0700 \
  /dev/sdX

Write the generated images to the SD card partitions:

sudo dd if=<U-Boot-dir>/spl/u-boot-spl.bin of=/dev/sdX1 bs=1M
sudo dd if=<xv6-riscv-dir>/xv6-unmatched.itb of=/dev/sdX2 bs=1M
sudo dd if=<xv6-riscv-dir>/fs.img of=/dev/sdX3 bs=1M
  1. Insert the microSD card into the HiFive Unmatched.

  2. Ensure the Boot Mode Select (MSEL) switches are set to SD Boot (1011).

  3. Connect via UART (Baud rate: 115200).

  4. Power on the board. You should see the following output:

U-Boot SPL 2023.01 (Jan 07 2026 - 23:26:31 +0900)
Trying to boot from MMC1

xv6 kernel is booting

hart 2 starting
hart 4 starting
hart 3 starting
init: starting sh
$ 

You can also run the kernel on QEMU using the sifive_u machine. Make sure to clean the build artifacts when switching between the Unmatched board and QEMU targets.

联系我们 contact @ memedata.com