Skip to content

Commit

Permalink
Merge pull request #539 from otteryc/vblk
Browse files Browse the repository at this point in the history
Implement VirtIO block device
  • Loading branch information
jserv authored Feb 1, 2025
2 parents a8b202c + fd051c2 commit 6e6cfb8
Show file tree
Hide file tree
Showing 11 changed files with 677 additions and 13 deletions.
50 changes: 41 additions & 9 deletions .ci/boot-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,63 @@ function ASSERT {

cleanup

ENABLE_VBLK=1
VBLK_IMG=build/disk.img
which dd >/dev/null 2>&1 || ENABLE_VBLK=0
which mkfs.ext4 >/dev/null 2>&1 || which $(brew --prefix e2fsprogs)/sbin/mkfs.ext4 >/dev/null 2>&1 || ENABLE_VBLK=0
which 7z >/dev/null 2>&1 || ENABLE_VBLK=0

TIMEOUT=50
OPTS=" -k build/linux-image/Image "
OPTS+=" -i build/linux-image/rootfs.cpio "
OPTS+=" -b build/minimal.dtb "
if [ "$ENABLE_VBLK" -eq "1" ]; then
dd if=/dev/zero of=$VBLK_IMG bs=4M count=32
mkfs.ext4 $VBLK_IMG || $(brew --prefix e2fsprogs)/sbin/mkfs.ext4 $VBLK_IMG
OPTS+=" -x vblk:$VBLK_IMG "
else
printf "Virtio-blk Test...Passed\n"
fi
RUN_LINUX="build/rv32emu ${OPTS}"

if [ "$ENABLE_VBLK" -eq "1" ]; then
ASSERT expect <<DONE
set timeout ${TIMEOUT}
spawn ${RUN_LINUX}
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
expect "# " { send "uname -a\n" } timeout { exit 2 }
expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 }
expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 }
expect "# " { send "sync\n" } timeout { exit 3 }
expect "# " { send "umount mnt\n" } timeout { exit 3 }
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
DONE
else
ASSERT expect <<DONE
set timeout ${TIMEOUT}
spawn ${RUN_LINUX}
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
expect "# " { send "uname -a\n" } timeout { exit 2 }
expect "riscv32 GNU/Linux" { send "\x01"; send "x" } timeout { exit 3 }
DONE

fi
ret=$?
cleanup

MESSAGES=("OK!" \
"Fail to boot" \
"Fail to login" \
"Fail to run commands" \
)

COLOR_G='\e[32;01m' # Green
COLOR_R='\e[31;01m' # Red
COLOR_N='\e[0m' # No color
printf "\n[ ${COLOR_G}${MESSAGES[$ret]}${COLOR_N} ]\n"

MESSAGES=("${COLOR_G}OK!" \
"${COLOR_R}Fail to boot" \
"${COLOR_R}Fail to login" \
"${COLOR_R}Fail to run commands" \
"${COLOR_R}Fail to find emu.txt in $VBLK_IMG"\
)

printf "\nBoot Linux Test: [ ${MESSAGES[$ret]}${COLOR_N} ]\n"
if [ "$ENABLE_VBLK" -eq "1" ]; then
7z l $VBLK_IMG | grep emu.txt >/dev/null 2>&1 || ret=4
printf "Virtio-blk Test: [ ${MESSAGES[$ret]}${COLOR_N} ]\n"
fi

exit ${ret}
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: install-dependencies
run: |
sudo apt-get update -q -y
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect bc
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect bc p7zip-full
.ci/riscv-toolchain-install.sh
echo "${{ github.workspace }}/toolchain/bin" >> $GITHUB_PATH
wget https://apt.llvm.org/llvm.sh
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ endif
# during emulator initialization.
$(call set-feature, FULL4G)
ifeq ($(call has, FULL4G), 1)
$(OUT)/main.o: CFLAGS += -DMEM_SIZE=0xFFFFFFFFULL # 2^{32} - 1
CFLAGS += -DMEM_SIZE=0xFFFFFFFFULL # 2^{32} - 1
endif

ENABLE_GDBSTUB ?= 0
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ $ make ENABLE_SYSTEM=1 system
Build using run using specified images:
```shell
$ make ENABLE_SYSTEM=1
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path>
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path> [-x vblk:<virtio_blk_img_path>]
```

Build with a larger INITRD_SIZE (e.g., 64 MiB) to run SDL-oriented application because the default 8 MiB is insufficient for SDL-oriented application artifacts:
Expand All @@ -86,6 +86,21 @@ $ make system ENABLE_SYSTEM=1 ENABLE_SDL=1 INITRD_SIZE=64
```
Once login the guestOS, run `doom-riscv` or `quake` or `smolnes`. To terminate SDL-oriented applications, use the built-in exit utility, ctrl-c or the SDL window close button(X).

#### Virtio Block Device (optional)
Generate ext4 image file for virtio block device in Unix-like system:
```shell
$ dd if=/dev/zero of=disk.img bs=4M count=32
$ mkfs.ext4 disk.img
```
Mount the virtual block device and create a test file after booting, note that root privilege is required to mount and unmount a disk:
```shell
# mkdir mnt
# mount /dev/vda mnt
# echo "rv32emu" > mnt/emu.txt
# umount mnt
```
Reboot and re-mount the virtual block device, the written file should remain existing.

#### Build Linux image
An automated build script is provided to compile the RISC-V cross-compiler, Busybox, and Linux kernel from source. Please note that it only supports the Linux host environment. It can be found at tools/build-linux-image.sh.
```
Expand Down
6 changes: 6 additions & 0 deletions src/devices/minimal.dts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@
no-loopback-test;
clock-frequency = <5000000>; /* the baudrate divisor is ignored */
};

blk0: virtio@4200000 {
compatible = "virtio,mmio";
reg = <0x4200000 0x200>;
interrupts = <3>;
};
};
};
Loading

0 comments on commit 6e6cfb8

Please sign in to comment.