Skip to content

Migrate virtio-rng from semu#747

Open
Charlie-Tsai1123 wants to merge 1 commit into
sysprog21:masterfrom
Charlie-Tsai1123:research/virtio
Open

Migrate virtio-rng from semu#747
Charlie-Tsai1123 wants to merge 1 commit into
sysprog21:masterfrom
Charlie-Tsai1123:research/virtio

Conversation

@Charlie-Tsai1123

@Charlie-Tsai1123 Charlie-Tsai1123 commented May 26, 2026

Copy link
Copy Markdown

Summary

This PR migrates virtio-rng support from semu while following the existing virtio-blk integration in rv32emu.

The implementation adds a virtio-rng device model, MMIO routing, PLIC interrupt integration, dynamic DTB node creation, and a new -x vrng option for system emulation mode. The guest Linux configuration is also updated to enable the virtio-rng driver so /dev/hwrng can be exposed inside the guest.

Implementation notes

  • Add virtio-rng MMIO register handling, feature negotiation, queue setup, QueueNotify handling, used ring update, interrupt status handling, and device reset handling.
  • Add MMIO_VIRTIORNG routing and update virtio-rng interrupts through the PLIC.
  • Add vrng_new() / vrng_delete() following the existing device lifecycle style.
  • Add -x vrng to enable virtio-rng at runtime.
  • Dynamically create a virtio,mmio DTB node for virtio-rng when enabled.
  • Enable the Linux hardware random framework and virtio-rng driver in the guest config.

Test

Build and basic check

make build-linux-image
make system_defconfig
make ENABLE_SYSTEM=1

build/rv32emu \
  -k build/linux-image/Image \
  -i build/linux-image/rootfs.cpio \
  -x vrng

Guest Verification

# cat /sys/bus/virtio/devices/virtio0/device
0x0004
# cat /sys/bus/virtio/devices/virtio0/vendor
0x12345678
# readlink /sys/bus/virtio/devices/virtio0/driver
../../../../../bus/virtio/drivers/virtio_rng
# ls -l /dev/hwrng
crw-------    1 root     root       10, 183 Jan  1  1970 /dev/hwrng
# dd if=/dev/hwrng bs=16 count=1 | hexdump -C
1+0 records in
1+0 records out
00000000  2c 9f e4 1a 6f ee c5 d8  1f fb ed bc e8 25 3a 59  |,...o........%:Y|
00000010
# dd if=/dev/hwrng bs=16 count=1 | hexdump -C
1+0 records in
1+0 records out
00000000  e9 f8 28 82 7b 66 ee bc  01 e4 9a 41 02 4a 3c f9  |..(.{f.....A.J<.|
00000010

Also tested virtio-rng together with virtio-blk:

mkdir -p build/disks
dd if=/dev/zero of=build/disks/disk.img bs=4M count=32
mkfs.ext4 -F build/disks/disk.img

build/rv32emu \
  -k build/linux-image/Image \
  -i build/linux-image/rootfs.cpio \
  -x vblk:build/disks/disk.img \
  -x vrng

Guest Verification:

# ls /sys/bus/virtio/devices
virtio0  virtio1
# cat /sys/bus/virtio/devices/virtio0/device
0x0004
# cat /sys/bus/virtio/devices/virtio1/device
0x0002
# readlink /sys/bus/virtio/devices/virtio0/driver
../../../../../bus/virtio/drivers/virtio_rng
# readlink /sys/bus/virtio/devices/virtio1/driver
../../../../../bus/virtio/drivers/virtio_blk

Summary by cubic

Adds virtio-rng to system emulation, exposing a guest /dev/hwrng. Implements MMIO routing, PLIC interrupts, a DTB virtio,mmio node with assigned MMIO base and IRQ, and a new -x vrng flag; coexists with virtio-blk.

  • New Features

    • Virtio RNG device model (feature negotiation, queue/notify, used ring, interrupts, reset).
    • MMIO routing via MMIO_VIRTIORNG with PLIC updates (emu_update_vrng_interrupts).
    • Dynamic DTB node for virtio-rng after existing virtio-blk nodes (assigns MMIO base and IRQ).
    • New -x vrng CLI flag; guest config enables CONFIG_HW_RANDOM and CONFIG_HW_RANDOM_VIRTIO.
  • Bug Fixes

    • Correct vblk MMIO range upper bound (vblk_mmio_max_hi = base + cnt - 1) to ensure proper address matching when virtio-rng is enabled.

Written for commit efcf2f8. Summary will update on new commits. Review in cubic

@shengwen-tw

Copy link
Copy Markdown
Contributor

Hi @Charlie-Tsai1123,

Could you add me as a co-author in the commit? AFAIK, semu may be frozen in the future.

Co-authored-by: Shengwen Cheng <shengwen1997.tw@gmail.com>

Thanks.

@Charlie-Tsai1123

Copy link
Copy Markdown
Author

Thanks for the reminder.

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Benchmarks

Details
Benchmark suite Current: efcf2f8 Previous: 60f74c0 Ratio
Dhrystone 1454.667 DMIPS 1769 DMIPS 1.22
CoreMark 1060.317 iterations/sec 1153.875 iterations/sec 1.09

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

cubic-dev-ai[bot]

This comment was marked as resolved.

This commit migrates virtio-rng support from semu and follows the
existing virtio-blk implementation, with the following modifications:

1. Implement virtio-rng device model
The virtio-rng implementation follows the VirtIO-MMIO flow used
by virtio-blk, including feature negotiation, queue setup, QueueNotify
handling, used ring update, interrupt status, and device status reset.

2. Rename virtio_rng_reg_read/write to virtio_rng_read/write
In semu, virtio_rng_read/write first checks the memory access width and
then calls virtio_rng_reg_read/write for register handling. In rv32emu,
MMIO accesses are already routed to each device by system.h, following
the existing virtio-blk model. Therefore, the migrated virtio-rng
device exposes virtio_rng_read/write as the register handlers directly.

3. Implement MMIO_VIRTIORNG
Add MMIO routing for virtio-rng and connect the device interrupt status
to the PLIC, following the existing virtio-blk interrupt update model.

4. Implement vrng_new() and vrng_delete()
These functions align virtio-rng with the allocation and cleanup.

5. Introduce new argument '-x vrng' for virtio-rng
The new option enables virtio-rng in system emulation mode. When it is
enabled, rv32emu dynamically creates a virtio-mmio node in the
generated device tree and assigns an MMIO base address and IRQ for the
device.

6. Use virtio-rng state
Unlike semu's global rng_fd, rv32emu stores the virtio-rng state in
vm_attr_t so MMIO routing, interrupt routing, and device cleanup can
access the same device instance.

7. Enable virtio-rng in the guest Linux configuration

Enable the Linux hardware random framework and virtio-rng driver so
the guest can bind the device and expose /dev/hwrng.

Co-authored-by: Shengwen Cheng <shengwen1997.tw@gmail.com>
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