Skip to content

Commit

Permalink
Migrate virtio-blk from semu
Browse files Browse the repository at this point in the history
This commit migrates @shengwen-tw's brilliant virtio implementation
from semu, with the following modification:

1. Rename virtio_blk_reg_read/write to virtio_blk_read/write
The original virtio_blk_read verified whether the virtio MMIO was
aligned to 4. Since rv32emu does not enforce alignment checks for UART
and PLIC devices or raise misalignment exceptions, this verification
has been omitted.

2. Implement MMIO_VIRTIOBLK

3. Implement vblk_new() and vblk_delete()
These functions align with the conventions used for UART and PLIC
devices.

4. Introduce new argument '-x vblk:<disk_image>' for virtio-blk disk
image

5. Fix the size of addr in virtq_desc structure.

Co-authored-by: Shengwen Cheng <[email protected]>
  • Loading branch information
otteryc and shengwen-tw committed Feb 1, 2025
1 parent a8b202c commit 15af061
Show file tree
Hide file tree
Showing 9 changed files with 635 additions and 3 deletions.
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 15af061

Please sign in to comment.