2026-01-04POSSEDay 14 was about Disk I/O.
A device driver for the virtio-blk, a virtual disk device was implemented.
Virtio is one of the APIs for device drivers to control devices. It is widely used in virtualization environments such as QEMU. The virtio devices have a structure called virtqueue and it is a queue shared between the driver and the device. It consists of Discriptor Table, Available Ring and Used Ring.
First we enable virtio drivers in our buildscript which is run.sh. We use the following flags for qemu:
-drive id=drive0,file=lorem.txt,format=raw,if=none
-device virtio-blk-device,drive=drive0,bus=virtio-mmio-bus.0
Here, lorem.txt is a file made for testing purposes beforehand.
Then, the definition of c structs and macros is done in kernel.h. To access the MMIO registers we add that functionality in kernel.c.
The create_process function is modified to map the virtio-blk MMIO region to the page table so the kernel can access the MMIO registers. MMIO region to the page table so the kernel can access the MMIO registers.
Then the Virtio Device Initialization and Virtqueue Initialization is done with the process described in the spec.
I/O requests to the disk is implemented by "adding processing requests to the virtqueue".
A request is sent in the following steps:
blk_req. Specify the sector number you want to access and the type of read/write.blk_req (see below).To test, we initialize the virtio device in the kernel main and try reading and writing to the disk with the implemented functions.
Device drivers are just "glue" between the OS and devices. Drivers don't control the hardware directly; drivers communicate with other software running on the device (e.g., firmware). Devices and their software, not the OS driver, will do the rest of the heavy lifting, like moving disk read/write heads.
A Computer Engineering student who loves FOSS and is learning about privacy, the Internet and languages writing about the things he does.