Running 64-bit RISC-V Linux on QEMU

Note: This has been tested on Ubuntu 18.04.

Prerequisites

sudo apt install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
                 gawk build-essential bison flex texinfo gperf libtool patchutils bc \
                 zlib1g-dev libexpat-dev git

Getting the sources

首先,创建一个工作文件夹,将所有资源全部下载到该文件夹下;

mkdir riscv64-linux
cd riscv64-linux

然后就开始下载所需要的资源,分别包含了下面5部分:

  1. RISC-V newlib and Linux toolchain
  2. QEMU
  3. Linux
  4. BBL (Berkeley Boot Loader)
  5. Busybear Linux (RISC-V root filesystem image)
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
git clone https://github.com/qemu/qemu
git clone https://github.com/torvalds/linux
git clone https://github.com/riscv/riscv-pk
git clone https://github.com/michaeljclark/busybear-linux

Note: 本人试过,最难的是第一个riscv-gnu-toolchain,各种submodules. 尝试过用Gitee,但是有的模块没有成功,主要可能因为跟网速有关(有兴趣可以尝试一下,说不定你就成功了,哈哈)。我最后是在公司服务器上用上述源地址clone成功的。

Building

build toolchain

cd riscv-gnu-toolchain
./configure --prefix=/opt/riscv64 		// pick an install path, e.g. /opt/riscv64
make newlib -j $(nproc)
make linux -j $(nproc)
//导出环境变量(也可以在/etc/profile末尾添加下面两行,这样重启后不会丢失)
export PATH="$PATH:/opt/riscv64/bin"  
export RISCV="/opt/riscv64"

build QEMU with the RISC-V target

cd qemu
git checkout v3.0.0
./configure --target-list=riscv64-softmmu
make -j $(nproc)
sudo make install

Note:

  1. configure时可能会报错,错误原因如果是: glib-2.40 gthread-2.0 is required to compile QEMU,可以参考此文解决configure QEMU时遇到缺少库和工具的问题
  2. make如果遇到卡到一个config_all_devices的地方,可以选择耐心等待(由于它还是在clone,失败后会Retry),也可中止后重新make,直到make没有error.

build Linux for the RISC-V target

cd linux
git checkout v4.19-rc3
cp ../busybear-linux/conf/linux.config .config
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- olddefconfig
# enter kernel configuration
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- nconfig

在这里插入图片描述
确认下面三个选项被选上。

  1. ARCH_RV64I (Platform type->Base ISA)
  2. CMODEL_MEDANY (Platform type->Kernel Code Model)
  3. CONFIG_SIFIVE_PLIC (Device Drivers)
# After accepting changes in the configuration, compile the kernel
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- vmlinux -j $(nproc)

build BBL

cd riscv-pk
mkdir build && cd build
../configure --enable-logo --host=riscv64-unknown-elf --with-payload=../../linux/vmlinux
make -j $(nproc)

build Busybear Linux

cd busybear-linux
make -j $(nproc)

Note:

  1. make时会先Download些资源,在自己电脑上一直download失败,最后还是在公司服务器上成功。download完之后会自动配置。如果最后出现下图,即生成busybear.bin意味着成功。
    在这里插入图片描述2. 自己虽然貌似从服务器download成功,但配置最后会报错,问题还是缺少资源。发现busybear下的src文件夹下的riscv-pk是空文件夹。**解决方法是:**将之前编译过的riscv-pk文件夹(与busybear-linux同级目录)复制到src文件夹下,然后再重新编译,成功生成busybear.bin文件。

Running

Go back to your main working directory(riscv64-linux) and run:

sudo qemu-system-riscv64 -nographic -machine virt \
     -kernel riscv-pk/build/bbl -append "root=/dev/vda ro console=ttyS0" \
     -drive file=busybear-linux/busybear.bin,format=raw,id=hd0 \
     -device virtio-blk-device,drive=hd0

执行后,出现下面景象(kernel起来之后会要求输入用户名和密码),恭喜你,基本成功了。
username------root
password------busybear
在这里插入图片描述补充: busybear起来之后,如果遇到ntpd的问题,可暂时将/etc/inittab中下面这一行注掉:

::respawn:/usr/sbin/ntpd -n
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章