Skip to content

Commit 6e6cfb8

Browse files
authored
Merge pull request #539 from otteryc/vblk
Implement VirtIO block device
2 parents a8b202c + fd051c2 commit 6e6cfb8

File tree

11 files changed

+677
-13
lines changed

11 files changed

+677
-13
lines changed

.ci/boot-linux.sh

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,63 @@ function ASSERT {
1616

1717
cleanup
1818

19+
ENABLE_VBLK=1
20+
VBLK_IMG=build/disk.img
21+
which dd >/dev/null 2>&1 || ENABLE_VBLK=0
22+
which mkfs.ext4 >/dev/null 2>&1 || which $(brew --prefix e2fsprogs)/sbin/mkfs.ext4 >/dev/null 2>&1 || ENABLE_VBLK=0
23+
which 7z >/dev/null 2>&1 || ENABLE_VBLK=0
24+
1925
TIMEOUT=50
2026
OPTS=" -k build/linux-image/Image "
2127
OPTS+=" -i build/linux-image/rootfs.cpio "
22-
OPTS+=" -b build/minimal.dtb "
28+
if [ "$ENABLE_VBLK" -eq "1" ]; then
29+
dd if=/dev/zero of=$VBLK_IMG bs=4M count=32
30+
mkfs.ext4 $VBLK_IMG || $(brew --prefix e2fsprogs)/sbin/mkfs.ext4 $VBLK_IMG
31+
OPTS+=" -x vblk:$VBLK_IMG "
32+
else
33+
printf "Virtio-blk Test...Passed\n"
34+
fi
2335
RUN_LINUX="build/rv32emu ${OPTS}"
2436

37+
if [ "$ENABLE_VBLK" -eq "1" ]; then
38+
ASSERT expect <<DONE
39+
set timeout ${TIMEOUT}
40+
spawn ${RUN_LINUX}
41+
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
42+
expect "# " { send "uname -a\n" } timeout { exit 2 }
43+
expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 }
44+
expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 }
45+
expect "# " { send "sync\n" } timeout { exit 3 }
46+
expect "# " { send "umount mnt\n" } timeout { exit 3 }
47+
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
48+
DONE
49+
else
2550
ASSERT expect <<DONE
2651
set timeout ${TIMEOUT}
2752
spawn ${RUN_LINUX}
2853
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
2954
expect "# " { send "uname -a\n" } timeout { exit 2 }
3055
expect "riscv32 GNU/Linux" { send "\x01"; send "x" } timeout { exit 3 }
3156
DONE
32-
57+
fi
3358
ret=$?
3459
cleanup
3560

36-
MESSAGES=("OK!" \
37-
"Fail to boot" \
38-
"Fail to login" \
39-
"Fail to run commands" \
40-
)
41-
4261
COLOR_G='\e[32;01m' # Green
62+
COLOR_R='\e[31;01m' # Red
4363
COLOR_N='\e[0m' # No color
44-
printf "\n[ ${COLOR_G}${MESSAGES[$ret]}${COLOR_N} ]\n"
64+
65+
MESSAGES=("${COLOR_G}OK!" \
66+
"${COLOR_R}Fail to boot" \
67+
"${COLOR_R}Fail to login" \
68+
"${COLOR_R}Fail to run commands" \
69+
"${COLOR_R}Fail to find emu.txt in $VBLK_IMG"\
70+
)
71+
72+
printf "\nBoot Linux Test: [ ${MESSAGES[$ret]}${COLOR_N} ]\n"
73+
if [ "$ENABLE_VBLK" -eq "1" ]; then
74+
7z l $VBLK_IMG | grep emu.txt >/dev/null 2>&1 || ret=4
75+
printf "Virtio-blk Test: [ ${MESSAGES[$ret]}${COLOR_N} ]\n"
76+
fi
4577

4678
exit ${ret}

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
- name: install-dependencies
4747
run: |
4848
sudo apt-get update -q -y
49-
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect bc
49+
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect bc p7zip-full
5050
.ci/riscv-toolchain-install.sh
5151
echo "${{ github.workspace }}/toolchain/bin" >> $GITHUB_PATH
5252
wget https://apt.llvm.org/llvm.sh

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ endif
195195
# during emulator initialization.
196196
$(call set-feature, FULL4G)
197197
ifeq ($(call has, FULL4G), 1)
198-
$(OUT)/main.o: CFLAGS += -DMEM_SIZE=0xFFFFFFFFULL # 2^{32} - 1
198+
CFLAGS += -DMEM_SIZE=0xFFFFFFFFULL # 2^{32} - 1
199199
endif
200200

201201
ENABLE_GDBSTUB ?= 0

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ $ make ENABLE_SYSTEM=1 system
7777
Build using run using specified images:
7878
```shell
7979
$ make ENABLE_SYSTEM=1
80-
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path>
80+
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path> [-x vblk:<virtio_blk_img_path>]
8181
```
8282

8383
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:
@@ -86,6 +86,21 @@ $ make system ENABLE_SYSTEM=1 ENABLE_SDL=1 INITRD_SIZE=64
8686
```
8787
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).
8888

89+
#### Virtio Block Device (optional)
90+
Generate ext4 image file for virtio block device in Unix-like system:
91+
```shell
92+
$ dd if=/dev/zero of=disk.img bs=4M count=32
93+
$ mkfs.ext4 disk.img
94+
```
95+
Mount the virtual block device and create a test file after booting, note that root privilege is required to mount and unmount a disk:
96+
```shell
97+
# mkdir mnt
98+
# mount /dev/vda mnt
99+
# echo "rv32emu" > mnt/emu.txt
100+
# umount mnt
101+
```
102+
Reboot and re-mount the virtual block device, the written file should remain existing.
103+
89104
#### Build Linux image
90105
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.
91106
```

src/devices/minimal.dts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,11 @@
6565
no-loopback-test;
6666
clock-frequency = <5000000>; /* the baudrate divisor is ignored */
6767
};
68+
69+
blk0: virtio@4200000 {
70+
compatible = "virtio,mmio";
71+
reg = <0x4200000 0x200>;
72+
interrupts = <3>;
73+
};
6874
};
6975
};

0 commit comments

Comments
 (0)