Note : Run LDD3 example on the QEMU RISC-V virt machine

吳建興
3 min readJun 2, 2024

--

Build and Install tool chain

git clone git@github.com:riscv-collab/riscv-gnu-toolchain.git
cd riscv-gnu-toolchain

# checkout to a stabler commit
git checkout 2024.03.01

# opt/riscv is the install path
./configure --prefix=/opt/riscv

# It takes a long time to do the submodule-init and submodule-update.
make linux

# Add the binary to the PATH
export PATH=/path/to/opt/riscv/bin:$PATH

Build the QEMU

git clone --recurse-submodules git@github.com:qemu/qemu.git 
cd qemu

git checkout v8.2.0
./configure --enable-debug --enable-plugins --prefix=/opt/install/path --target-list=riscv32-linux-user,riscv32-softmmu,riscv64-linux-user,riscv64-softmmu

make -j5

Build the OpenSBI

git clone git@github.com:riscv-software-src/opensbi.git
cd opensbi

git checkout v1.2
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- PLATFORM_RISCV_XLEN=64 PLATFORM=generic

Build the rootfs using buildroot

git clone git@github.com:buildroot/buildroot.git
cd buildroot

make menuconfig

# After config
make clean all

menuconfig options :

Target options
Target Architecture (RISCV)
Toolchain
Toolchain type (External toolchain)
Toolchain (Custom toolchain)
Toolchain path (/path/to/your/risc-v/toolchain)
Toolchain prefix ($(ARCH)-unknown-linux-gnu)
External toolchain gcc version (12.x)
External toolchain kernel headers series (5.10.x)
External toolchain C library (glibc)
[ ] Toolchain has RPC support?
[*] Toolchain has SSP support?
→ [*] Toolchain has SSP strong support?
[*] Toolchain has C++ support?
[*] Toolchain has Fortran support?
[*] Toolchain has OpenMP support?
Filesystem images
[*] cpio the root filesystem
→ Compression method (gzip)
# For Static Build
# generates static libraries
Build options
-> libraries (both static and shared)
make clean all

Build the Linux Kernel

git clone --recurse-submodules git@github.com:torvalds/linux.git
git checkout v5.10

export ARCH=riscv
export CROSS_COMPILE="riscv64-unknown-linux-gnu-"
make defconfig
make menuconfig

# after config
make clean
make

Build the LDD3 example

# This repo update the LDD3 examples, so that we can run the example
# in a new version Linux Kernel.
git clone git@github.com:martinezjavier/ldd3.git
cd ldd3

export ARCH=riscv
export CROSS_COMPILE="riscv64-unknown-linux-gnu-"
export KERNELDIR=/path/to/linux/repo

make

Put the LDD3 example into the rootfs

cd /path/to/buildroot/output/images
fakeroot

mkdir initrd
cd initrd
zcat ../rootfs.cpio.gz | cpio -H newc -idv

# Put all the ldd3 example into the rootfs
cp -r /path/to/ldd3 ./

find . | cpio -o -H newc | gzip > ../test.cpio.gz
exit

DEMO

Run the LDD3 example on the QEMU RISC-V virt machine

path/to/qemu-system-riscv64 -M virt -m 4G -smp 1 -serial mon:stdio -serial null -nographic -bios /path/to/opensbi/build/platform/generic/firmware/fw_jump.elf -kernel /path/to/linux/arch/riscv/boot/Image -initrd /path/to/buildroot/output/images/test.cpio.gz


# Starting syslogd: OK
# Starting klogd: OK
# Running sysctl: OK
# Starting network: OK

# Welcome to Buildroot
# buildroot login: root
# get into the shell of the guest OS in the QEMU
cd /ldd3/scull
sh ./scull.init start
ls /dev -al | grep scull

Now we can see that we load the scull module.

Remote debug the linux kernel

# shell 1
path/to/qemu-system-riscv64 -M virt -m 4G -smp 1 -serial mon:stdio -serial null -nographic -bios /path/to/opensbi/build/platform/generic/firmware/fw_jump.elf -kernel /path/to/linux/arch/riscv/boot/Image -initrd /path/to/buildroot/output/images/test.cpio.gz -s -S

# shell 2
riscv64-unknown-linux-gnu-gdb path/to/vmlinux
b soc_early_init
target remote localhost:1234
c

Reference

repo of riscv-gnu-toolchain
https://github.com/riscv-collab/riscv-gnu-toolchain

ldd3
https://lwn.net/Kernel/LDD3/

ldd3 examples
https://github.com/martinezjavier/ldd3

--

--