基於RISC-V QEMU 仿真運行Linux 系統環境搭建

前言

文章詳細說明如何從堶零開始基於RISC-V QEMU 仿真運行Linux 系統環境搭建,
是Linux 小白入門教程不二之選,歡迎留言討論,轉發請注明原文出處~

1. 準備QEMU 仿真環境 --RISC-V 64bits

安裝包下載地址: https://www.qemu.org/

安裝命令及安裝成功效果如下所示,

target-list 設定爲riscv64-softmmu, 

sudo apt-get install libglib2.0-dev
sudo apt-get install libpixman-1-dev
./configure --target-list=riscv64-softmmu
make -j24
sudo make install -j24

安裝完成後,可按如下檢查是否安裝成功

$ qemu-system-riscv64 -h
QEMU emulator version 8.0.4
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers
usage: qemu-system-riscv64 [options] [disk_image]


2. 安裝RISC-V 編譯器, (過程略)


如下cmd 可檢查是否安裝成功並顯示版本號 12.2.0

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/opt/riscv/bin/riscv64-unknown-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/riscv/libexec/gcc/riscv64-unknown-linux-gnu/12.2.0/lto-wrapper
Target: riscv64-unknown-linux-gnu
Configured with: /project/riscv-gnu-toolchain/gcc/configure --target=riscv64-unknown-linux-gnu --prefix=/opt/riscv --with-sysroot=/opt/riscv/sysroot --with-pkgversion=g2ee5e430018 --with-system-zlib --enable-shared --enable-tls --enable-languages=c,c++,fortran --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libsanitizer --disable-nls --disable-bootstrap --src=.././gcc --enable-multilib --with-abi=lp64d --with-arch=rv64imafdc --with-tune=rocket --with-isa-spec=20191213 'CFLAGS_FOR_TARGET=-O2    -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-O2    -mcmodel=medlow'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.2.0 (g2ee5e430018) 

3. 準備U-Boot

Linux 啓動需要u-boot 引導載入鏡像文件. 節說明如何準備u-boot

下載路徑: U-Boot / U-Boot · GitLab

編譯安裝方法: 

export CROSS_COMPILE=/opt/riscv/bin/riscv64-unknown-linux-gnu-
make qemu-riscv64_smode_defconfig
make -j$(nproc) 2>&1 | tee build.log

 4. 準備OpenSBI

SBI (Supervisor Binary Interface), M-mode下运行的特定于平台固件,与bootloader引导加载程序,以及在S-mode或HS-mode模式下运行的hypervisor或通用操作系统之间的接口。與之對應, userspace 與 kernelspace 的接口稱爲ABI (Application Binary Interface).

OpenSBI 用於啓動引導操作系統,以及系統運行過程中,切換入M-Mode 提供一些硬件操作. 

下載路徑: https://github.com/riscv/opensbi.git

編譯安裝方法, FW_PAYLOAD_PATH 需換換爲上一小節u-boot.bin 實際路徑: 

export CROSS_COMPILE=/opt/riscv/bin/riscv64-unknown-linux-gnu-
make PLATFORM=generic FW_PAYLOAD_PATH=$1/u-boot.bin

編譯完成後,可看到生成fw_payload.elf,  至此,可以先測試openSBI + u-boot 是否可正常啓動:

build/platform/generic/firmware/fw_payload.bin
qemu-system-riscv64 -M virt -m 256M -nographic -bios build/platform/generic/firmware/fw_payload.elf
U-Boot 2023.10-rc4 (Sep 14 2023 - 22:44:30 +0800)

CPU:   rv64imafdch_zicbom_zicboz_zicsr_zifencei_zihintpause_zawrs_zba_zbb_zbc_zbs_sstc_svadu
Model: riscv-virtio,qemu
DRAM:  256 MiB
Core:  25 devices, 12 uclasses, devicetree: board
Flash: 32 MiB
Loading Environment from nowhere... OK
In:    serial,usbkbd
Out:   serial,vidconsole
Err:   serial,vidconsole
No working controllers found
Net:   No ethernet found.
Working FDT set to 8ef208b0
Hit any key to stop autoboot:  0 

Device 0: unknown device
scanning bus for devices...

Device 0: unknown device
starting USB...
No working controllers found
No ethernet found.
No ethernet found.
=> 

5. 準備Linux Kernel

下載路徑: https://github.com/torvalds/linux

編譯安裝方法: 

export ARCH=riscv
export KBUILD_OUTPUT=kbuild_out
export CROSS_COMPILE=/opt/riscv/bin/riscv64-unknown-linux-gnu-
make defconfig
make -j24 2>&1 | tee build.log
$ ls kbuild_out/arch/riscv/boot/Image
kbuild_out/arch/riscv/boot/Image

6. 準備rootfs

rootfs 作爲linux kernel 啓動阶段必不可少資源, 對於Linux 正常運行必不可少. busybox 是更爲常用的rootfs 制作方法,但是編譯相對較爲復雜,對於小白來說有一定難度.本小節基於buildroot 搭建rootfs, 提供一個更便捷的方法.

下載路徑: https://github.com/buildroot/buildroot

編譯安裝方法: 

前方已經準備好openSBI firmware 和 Linux Kernel Image, build 前先將兩個選項置爲no, 若爲yes, 自動從網絡下載編譯openSBI, Linux, 等待很久且沒必要.

$ git diff
diff --git a/configs/qemu_riscv64_virt_defconfig b/configs/qemu_riscv64_virt_defconfig
index 52ad87fa7c..ff330d3505 100644
--- a/configs/qemu_riscv64_virt_defconfig
+++ b/configs/qemu_riscv64_virt_defconfig
@@ -17,14 +17,14 @@ BR2_ROOTFS_POST_SCRIPT_ARGS="$(BR2_DEFCONFIG)"
 BR2_PACKAGE_HOST_LINUX_HEADERS_CUSTOM_6_1=y
 
 # Kernel
-BR2_LINUX_KERNEL=y
+BR2_LINUX_KERNEL=n
 BR2_LINUX_KERNEL_CUSTOM_VERSION=y
 BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.44"
 BR2_LINUX_KERNEL_USE_ARCH_DEFAULT_CONFIG=y
 BR2_LINUX_KERNEL_IMAGE=y
 
 # Bootloader
-BR2_TARGET_OPENSBI=y
+BR2_TARGET_OPENSBI=n
 BR2_TARGET_OPENSBI_CUSTOM_VERSION=y
 BR2_TARGET_OPENSBI_CUSTOM_VERSION_VALUE="1.2"
 BR2_TARGET_OPENSBI_PLAT="generic"

編譯安裝方法: 

export ARCH=riscv
export CROSS_COMPILE=/opt/riscv/bin/riscv64-unknown-linux-gnu-
make qemu_riscv64_virt_defconfig
make -j24 2>&1 | tee build.log

7. 測試

上述準備完成後, buildroot 路徑下自動生成"output/images/start-qemu.sh", 執行該文件即可.

由於沒有使用buildroot 自動生成opensbi 和 linux kernel image, 需修改fw_.elf 和 kernel Image

爲實際路徑. 如下圖所示.

exec qemu-system-riscv64 -M virt -bios fw_jump.elf -kernel $LINUX_IMG_DIR/Image -append "rootwait root=/dev/vda ro" -drive file=rootfs.ext2,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 -netd    ev user,id=net0 -device virtio-net-device,netdev=net0 -nographic  ${EXTRA_ARGS} "$@"
[    0.509136] devtmpfs: mounted
[    0.547883] Freeing unused kernel image (initmem) memory: 2192K
[    0.548763] Run /sbin/init as init process
[    0.775879] EXT4-fs (vda): warning: mounting unchecked fs, running e2fsck is recommended
[    0.802712] EXT4-fs (vda): re-mounted 677f7b94-119c-490a-94b9-9b9cb4651848 r/w. Quota mode: disabled.
Starting syslogd: OK
Starting klogd: OK
Running sysctl: OK
Seeding 256 bits and crediting
Saving 256 bits of creditable seed for next boot
Starting network: [    1.455587] test_kthread test_kthread_func
udhcpc: started, v1.36.1
udhcpc: broadcasting discover
udhcpc: broadcasting select for 10.0.2.15, server 10.0.2.2
udhcpc: lease of 10.0.2.15 obtained from 10.0.2.2, lease time 86400
deleting routers
adding dns 10.0.2.3
OK

Welcome to Buildroot
buildroot login: [   

你可能感兴趣的:(RISCV,QEMU,Linux,risc-v,linux)