Skip to content

Conversation

ChinYikMing
Copy link
Collaborator

@ChinYikMing ChinYikMing commented Aug 30, 2025

Demo Video

multiple-vblk.mp4

Problem Statement

Previously, #539 set VBLK_DEV_CNT_MAX to 1, limiting the virtual
machine to a single block device. This commit introduces support for
multiple block devices by switching from statically allocated storage to
dynamic allocation based on CLI input.

Key changes:

  1. Dynamic Allocation
    Virtio block related fields are now dynamically allocated based on the
    number of -x vblk options provided.

  2. Device Tree Generation
    The prewritten virtio@ subnode for block device in device/minimal.dts is
    commented and it can be an example subnode for future reference.
    load_dtb() dynamically appends virtio@ subnodes under the SoC node
    based on the number of block devices.

  3. MMIO Handling
    Instead of hardcoding switch cases for each MMIO region of block device,
    the code now checks whether the accessed MMIO address falls within the
    valid range of any block device.

Note:
The /dev/vdx device order is assigned in reverse: the first -x vblk
argument corresponds to the device with the highest letter, while
subsequent arguments receive lower-lettered device names.

Testing Procedure

  1. Clone the branch
$ git clone https://github.com/ChinYikMing/rv32emu.git -b multiple-vblk --depth 1 && cd rv32emu
  1. Create 2 disk images (more than 2 is also OK, just adjusting the -x vblk option)
$ dd if=/dev/zero of=disk1.img bs=4M count=32
$ mkfs.ext4 disk1.img

$ dd if=/dev/zero of=disk2.img bs=4M count=32
$ mkfs.ext4 disk2.img
  1. Build the emulator
$ make ENABLE_SYSTEM=1 INITRD_SIZE=32 -j8
  1. Fetch Linux Image artifact
$ make artifact ENABLE_SYSTEM=1
  1. Boot the guestOS with 2 block devices
$ build/rv32emu -k build/linux-image/Image -i build/linux-image/rootfs.cpio -x vblk:disk1.img -x vblk:disk2.img
  1. Show multiple block devices in guestOS
# ls /dev/vd*
  1. Mount both block devices in guestOS
# mkdir -p mnt && mount /dev/vda mnt
# mkdir -p mnt2 && mount /dev/vdb mnt2
  1. Write some data into the block devices
# echo "rv32emu" > mnt/emu.txt
# echo "rv32emu" > mnt2/emu.txt
  1. Cat the written data in the block devices
# cat mnt/emu.txt
# cat mnt2/emu.txt

Expected Result

# ls /dev/vd*
/dev/vda  /dev/vdb
# mkdir -p mnt && mount /dev/vda mnt
[   30.911107] EXT4-fs (vda): mounted filesystem with ordered data mode. Quota mode: disabled.
# mkdir -p mnt2 && mount /dev/vdb mnt2
[   39.067705] EXT4-fs (vdb): mounted filesystem with ordered data mode. Quota mode: disabled.
# echo "rv32emu" > mnt/emu.txt
# echo "rv32emu" > mnt2/emu.txt
# cat mnt/emu.txt
rv32emu
# cat mnt2/emu.txt
rv32emu
#

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Benchmark suite Current: 9b4f2fe Previous: 678c47a Ratio
Dhrystone 1323 Average DMIPS over 10 runs 1290 Average DMIPS over 10 runs 0.98
Coremark 904.68 Average iterations/sec over 10 runs 913.951 Average iterations/sec over 10 runs 1.01

This comment was automatically generated by workflow using github-action-benchmark.

src/riscv.c Outdated
#define DTB_EXPAND_SIZE 1024 /* or more if needed */

/* Allocate enough memory for DTB + extra room */
int minimal_len = ARRAY_SIZE(minimal);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since this variable represents a length, would size_t be more appropriate than int?

Previously, sysprog21#539 set VBLK_DEV_CNT_MAX to 1, limiting the virtual
machine to a single block device. This commit introduces support for
multiple block devices by switching from statically allocated storage to
dynamic allocation based on CLI input.

Key changes:
1. Dynamic Allocation
Virtio block related fields are now dynamically allocated based on the
number of -x vblk options provided.

2. Device Tree Generation
The prewritten virtio@ subnode for block device in device/minimal.dts is
commented and it can be an example subnode for future reference.
load_dtb() dynamically appends virtio@ subnodes under the SoC node
based on the number of block devices.

3. MMIO Handling
Instead of hardcoding switch cases for each MMIO region of block device,
the code now checks whether the accessed MMIO address falls within the
valid range of any block device.

Note:
The /dev/vdx device order is assigned in reverse: the first -x vblk
argument corresponds to the device with the highest letter, while
subsequent arguments receive lower-lettered device names.
To reflect supporting multiple virtual block devices in guestOS.
This commit prevents null terminator at wrong position.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants