Skip to content

[dev][virtio-rng] Introduce virtio-rng driver to seed system prng#506

Merged
travisg merged 4 commits into
littlekernel:masterfrom
visitorckw:add-virtio-rng
May 9, 2026
Merged

[dev][virtio-rng] Introduce virtio-rng driver to seed system prng#506
travisg merged 4 commits into
littlekernel:masterfrom
visitorckw:add-virtio-rng

Conversation

@visitorckw
Copy link
Copy Markdown
Contributor

Introduces a new virtio-rng driver to fetch hardware entropy from the host in virtualized environments.

Additionally, an early boot hook is implemented to automatically fetch entropy and seed the system's prng via srand() after the device is initialized.

Comment thread dev/virtio/rng/virtio-rng.cpp
Copy link
Copy Markdown
Member

@travisg travisg left a comment

Choose a reason for hiding this comment

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

Looks great! just a few nits and this is lovely. Thanks!

int virtio_pci_init() {
LTRACE_ENTRY;

constexpr virtio_pci_devices devices[] = {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can you also add a virtio-mmio interface for this? Should be pretty easy, can test with the arm64 qemu since it can run with both mmio and PCI virtio interfaces (i think the -C flag switches to PCI from the default mmio)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ack. Will do.

bytes_read = virtio_rng_read(&seed, sizeof(seed));

if (bytes_read == sizeof(seed)) {
srand(seed);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

reminds me we should implement a better system RNG

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would be glad to help improve the system rng.
If there is anything specific you need help with or any direction you'd like me to explore, just let me know!

Introduces a new virtio-rng driver to fetch entropy from the host in
virtualized environments.

Additionally, an early boot hook (LK_INIT_LEVEL_TARGET + 1) is added to
automatically seed the system's PRNG via srand() when the device is
initialized.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Add initialization dispatch for the virtio-rng device. This registers
the transitional (0x1005) and non-transitional (0x1044) virtio-rng
device IDs to the virtio PCI bus driver, allowing the bus to probe
and initialize the device when discovered.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Add initialization dispatch for the virtio-rng device on the MMIO.
This registers the virtio-rng subsystem device ID (4) to the virtio
MMIO driver, allowing platforms that default to MMIO to probe and
initialize the hardware entropy source.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Add dev/virtio/rng to the module dependencies for the qemu-virt-arm
platform. This allows the system to automatically fetch hardware
entropy and seed the PRNG during the early boot process.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
@travisg travisg merged commit 61ce8e8 into littlekernel:master May 9, 2026
120 checks passed
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.

2 participants