[virtio] Address review feedback - simplify documentation and fix cppcheck

- Removed TESTING.md and SPECIFICATIONS.md as requested
- Simplified README.md with essential information only
- Added Chinese version README_zh.md with language links
- Fixed cppcheck AST error by rewriting version check condition
- Changed from (version != 1 && version != 2) to ((version < 1) || (version > 2))

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-04 22:42:36 +00:00
parent 28683c8fec
commit a45ccc69cb
6 changed files with 110 additions and 571 deletions

View File

@@ -80,7 +80,7 @@ int rt_virtio_devices_init(void)
}
/* Support both legacy (0x1) and modern (0x2) versions */
if (mmio_config->version != 1 && mmio_config->version != 2)
if ((mmio_config->version < 1) || (mmio_config->version > 2))
{
continue;
}

View File

@@ -86,7 +86,7 @@ int rt_virtio_devices_init(void)
}
/* Support both legacy (0x1) and modern (0x2) versions */
if (mmio_config->version != 1 && mmio_config->version != 2)
if ((mmio_config->version < 1) || (mmio_config->version > 2))
{
continue;
}

View File

@@ -1,5 +1,7 @@
# VirtIO Driver for RT-Thread
[中文](README_zh.md) | English
## Overview
The VirtIO driver provides support for virtual I/O devices in RT-Thread, primarily used in virtualized environments like QEMU.
@@ -43,17 +45,13 @@ Options:
Enable individual VirtIO devices:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers
```
- `RT_USING_VIRTIO_BLK`: VirtIO block device support
- `RT_USING_VIRTIO_NET`: VirtIO network device support
- `RT_USING_VIRTIO_CONSOLE`: VirtIO console device support
- `RT_USING_VIRTIO_GPU`: VirtIO GPU device support
- `RT_USING_VIRTIO_INPUT`: VirtIO input device support
## Key Differences Between Legacy and Modern VirtIO
## Key Differences
### Legacy VirtIO (v0.95)
- 32-bit feature negotiation
@@ -66,7 +64,6 @@ RT-Thread Components → Device Drivers → Using VirtIO device drivers
- Separate descriptor/driver/device queue areas
- Enhanced status flow with FEATURES_OK check
- Better memory alignment and atomicity guarantees
- Config generation field for atomic configuration reads
## Migration Guide
@@ -94,74 +91,10 @@ The following BSPs have been updated to support both legacy and modern VirtIO:
## Reference Specifications
- [VirtIO Specification v1.0](https://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html)
- [VirtIO Specification v1.1](https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html)
- [VirtIO Specification v1.2](https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html)
## Implementation Details
### Feature Negotiation
Modern VirtIO uses 64-bit feature negotiation:
- Device exposes features via `device_features` register (selected by `device_features_sel`)
- Driver acknowledges features via `driver_features` register (selected by `driver_features_sel`)
- For modern devices, the driver must negotiate `VIRTIO_F_VERSION_1` (feature bit 32)
### Queue Initialization
**Legacy VirtIO:**
- Uses single `queue_pfn` register pointing to the start of the queue area
- Guest page size configured via `guest_page_size`
**Modern VirtIO:**
- Uses separate registers for descriptor, driver (avail), and device (used) areas:
- `queue_desc_low`/`queue_desc_high`: Descriptor table address
- `queue_driver_low`/`queue_driver_high`: Available ring address
- `queue_device_low`/`queue_device_high`: Used ring address
- Queue activated via `queue_ready` register
### Status Flow
**Modern VirtIO adds FEATURES_OK check:**
1. Reset device (status = 0)
2. Set ACKNOWLEDGE and DRIVER status bits
3. Read and negotiate features
4. Write negotiated features to device
5. Set FEATURES_OK status bit
6. Re-read status to verify FEATURES_OK (device may reject features)
7. If accepted, proceed with queue setup and set DRIVER_OK
## Troubleshooting
### Device Not Detected
Check that:
1. QEMU is configured with VirtIO devices
2. The VirtIO version matches your QEMU configuration
3. The device memory region is correctly mapped
### Build Errors
Ensure:
1. The Kconfig is properly configured
2. All VirtIO header files are included
3. The BSP supports VirtIO (check `BSP_USING_VIRTIO`)
### Runtime Issues
Debug tips:
1. Check device version in MMIO config
2. Verify feature negotiation succeeded
3. Check queue initialization (descriptor, avail, used ring addresses)
4. Monitor interrupt status and acknowledgment
## Contributing
When adding new VirtIO devices or features:
1. Support both legacy and modern versions
2. Use the helper functions for feature negotiation (`virtio_get_features`, `virtio_set_features`)
3. Use version checking (`dev->version`) for version-specific code
4. Test on both legacy and modern QEMU configurations
- [VirtIO Specification v1.2](https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html) (Latest, 2022)
- [VirtIO Specification v1.1](https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html) (2019)
- [VirtIO Specification v1.0](https://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html) (2016)
- [OASIS VirtIO TC](https://www.oasis-open.org/committees/virtio/)
## License

View File

@@ -0,0 +1,101 @@
# RT-Thread VirtIO 驱动
[English](README.md) | 中文
## 概述
VirtIO 驱动为 RT-Thread 提供虚拟 I/O 设备支持,主要用于 QEMU 等虚拟化环境。
## 支持的版本
驱动现已支持传统版和现代版 VirtIO 规范:
- **传统版 VirtIO (v0.95)**:版本字段为 0x1兼容较旧的 QEMU 版本
- **现代版 VirtIO (v1.0+)**:版本字段为 0x2支持 VirtIO 1.0、1.1 和 1.2 规范
## 支持的设备
- **VirtIO 块设备 (virtio-blk)**:虚拟块设备
- **VirtIO 网络设备 (virtio-net)**:虚拟网络接口
- **VirtIO 控制台 (virtio-console)**:虚拟串口控制台
- **VirtIO GPU (virtio-gpu)**:虚拟图形设备
- **VirtIO 输入设备 (virtio-input)**:虚拟输入设备(键盘、鼠标、触摸板)
## 配置
使用 `menuconfig` 配置 VirtIO 支持:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers
```
### 版本选择
在传统版和现代版 VirtIO 之间选择:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
```
选项:
- **VirtIO Legacy (v0.95)**:与较旧的 QEMU 版本兼容(默认)
- **VirtIO Modern (v1.0+)**:用于较新的 QEMU 版本2.4+),具有增强功能
### 设备选择
启用各个 VirtIO 设备:
- `RT_USING_VIRTIO_BLK`VirtIO 块设备支持
- `RT_USING_VIRTIO_NET`VirtIO 网络设备支持
- `RT_USING_VIRTIO_CONSOLE`VirtIO 控制台支持
- `RT_USING_VIRTIO_GPU`VirtIO GPU 支持
- `RT_USING_VIRTIO_INPUT`VirtIO 输入设备支持
## 主要区别
### 传统版 VirtIO (v0.95)
- 32 位特性协商
- 单一队列描述符区域
- 简单状态标志
- 客户机页面大小配置
### 现代版 VirtIO (v1.0+)
- 64 位特性协商(支持更多特性)
- 独立的描述符/驱动/设备队列区域
- 增强的状态流程,带 FEATURES_OK 检查
- 更好的内存对齐和原子性保证
## 迁移指南
### 从传统版迁移到现代版
1. 更新 QEMU 命令行以使用现代版 VirtIO 设备(最新的 QEMU 版本默认使用现代版)
2. 在 menuconfig 中更改 VirtIO 版本:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
→ 选择 "VirtIO Modern (v1.0+)"
```
3. 重新构建应用程序
4. 驱动将自动与设备协商 VERSION_1 特性
### 向后兼容性
驱动从 MMIO 配置中自动检测设备版本,并相应调整其行为。传统版(版本 0x1和现代版版本 0x2设备在同一构建中都受支持。
## BSP 支持
以下 BSP 已更新以支持传统版和现代版 VirtIO
- `qemu-virt64-riscv`QEMU RISC-V 64 位
- `qemu-virt64-aarch64`QEMU ARM64 (AArch64)
## 规范参考
- [VirtIO 规范 v1.2](https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html)最新2022
- [VirtIO 规范 v1.1](https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html)2019
- [VirtIO 规范 v1.0](https://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html)2016
- [OASIS VirtIO 技术委员会](https://www.oasis-open.org/committees/virtio/)
## 许可证
Apache-2.0

View File

@@ -1,194 +0,0 @@
# VirtIO Specification References
## Official VirtIO Specifications
### Latest Version (v1.2)
**VirtIO 1.2 Specification** (2022-07-14)
- **Official Link**: https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html
- **PDF**: https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.pdf
- **Status**: OASIS Committee Specification Draft 01
- **Key Features**:
- All features from v1.1
- New devices: Audio, File System, PMEM, RPMB, Video Encoder/Decoder, SCMI, GPIO, RDMA
- Enhanced device specifications
- Better security and performance features
### VirtIO 1.1 Specification (2019-01-29)
- **Official Link**: https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html
- **PDF**: https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.pdf
- **Status**: OASIS Committee Specification Public Review Draft 01
- **Key Features**:
- All features from v1.0
- Packed ring support (VIRTIO_F_RING_PACKED)
- New devices: Socket, Crypto, Signal Distribution Module, IOMMU, Memory
- Order-independent feature negotiation
### VirtIO 1.0 Specification (2016-03-08)
- **Official Link**: https://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html
- **PDF**: https://docs.oasis-open.org/virtio/virtio/v1.0/cs04/virtio-v1.0-cs04.pdf
- **Status**: OASIS Committee Specification 04
- **Key Features**:
- Modern interface (version field = 0x2)
- 64-bit feature negotiation
- Separate queue descriptor/driver/device areas
- FEATURES_OK status check
- Better memory alignment requirements
### Legacy VirtIO (v0.95)
- **Reference**: Part of Linux kernel documentation
- **Link**: https://wiki.libvirt.org/VirtIO.html
- **Status**: Legacy interface (version field = 0x1)
- **Note**: Used by older QEMU versions and some embedded systems
## OASIS VirtIO Technical Committee
- **Website**: https://www.oasis-open.org/committees/virtio/
- **Charter**: Defines standard interfaces for virtual I/O devices
- **Mailing List**: virtio@lists.oasis-open.org
- **Git Repository**: https://github.com/oasis-tcs/virtio-spec
## Implementation in RT-Thread
RT-Thread now supports:
-**Legacy VirtIO (v0.95)**: Version field 0x1, 32-bit features, legacy queue setup
-**Modern VirtIO (v1.0+)**: Version field 0x2, 64-bit features, modern queue setup
The implementation follows the VirtIO 1.2 specification for modern devices while maintaining backward compatibility with legacy devices.
## Device-Specific Specifications
### VirtIO Block Device
- **Section**: 5.2 in VirtIO 1.2 spec
- **Features**: Basic block I/O, multiple queues, discard support
- **Status**: Fully implemented in RT-Thread
### VirtIO Network Device
- **Section**: 5.1 in VirtIO 1.2 spec
- **Features**: Ethernet interface, checksum offload, multiple queues
- **Status**: Fully implemented in RT-Thread
### VirtIO Console Device
- **Section**: 5.3 in VirtIO 1.2 spec
- **Features**: Serial console, multiple ports, control messages
- **Status**: Fully implemented in RT-Thread
### VirtIO GPU Device
- **Section**: 5.7 in VirtIO 1.2 spec
- **Features**: 2D graphics, scanout, resource management
- **Status**: Fully implemented in RT-Thread
### VirtIO Input Device
- **Section**: 5.8 in VirtIO 1.2 spec
- **Features**: Keyboard, mouse, tablet input events
- **Status**: Fully implemented in RT-Thread
## QEMU VirtIO Implementation
### QEMU Documentation
- **VirtIO Devices**: https://qemu.readthedocs.io/en/latest/system/devices/virtio-net.html
- **MMIO Transport**: https://qemu.readthedocs.io/en/latest/specs/virtio-mmio.html
### QEMU Version Requirements
- **Legacy VirtIO**: QEMU 1.0+
- **Modern VirtIO**: QEMU 2.4+ (recommended 2.6+)
### QEMU Command Line Examples
**Legacy Mode:**
```bash
qemu-system-riscv64 -M virt \
-device virtio-blk-device,disable-modern=on,drive=blk0
```
**Modern Mode:**
```bash
qemu-system-riscv64 -M virt \
-device virtio-blk-device,disable-legacy=on,drive=blk0
```
**Auto-detect (Default):**
```bash
qemu-system-riscv64 -M virt \
-device virtio-blk-device,drive=blk0
```
## Feature Bits Reference
### Common Features (Section 6 in VirtIO 1.2 spec)
| Bit | Name | Description |
|-----|------|-------------|
| 24 | VIRTIO_F_NOTIFY_ON_EMPTY | Notify on empty available ring |
| 27 | VIRTIO_F_ANY_LAYOUT | Flexible descriptor layout |
| 28 | VIRTIO_F_RING_INDIRECT_DESC | Indirect descriptors |
| 29 | VIRTIO_F_RING_EVENT_IDX | Event index for notifications |
| 32 | VIRTIO_F_VERSION_1 | Compliance with VirtIO 1.0+ |
| 34 | VIRTIO_F_RING_PACKED | Packed ring layout (1.1+) |
### Device-Specific Features
**Block Device:**
- Bit 0: VIRTIO_BLK_F_BARRIER - Legacy barrier support
- Bit 1: VIRTIO_BLK_F_SIZE_MAX - Maximum segment size
- Bit 2: VIRTIO_BLK_F_SEG_MAX - Maximum segments
- Bit 5: VIRTIO_BLK_F_RO - Read-only device
- Bit 9: VIRTIO_BLK_F_FLUSH - Cache flush support
**Network Device:**
- Bit 0: VIRTIO_NET_F_CSUM - Checksum offload
- Bit 1: VIRTIO_NET_F_GUEST_CSUM - Guest checksum
- Bit 5: VIRTIO_NET_F_MAC - MAC address
- Bit 6: VIRTIO_NET_F_GSO - Generic segmentation offload
## Status Bits Reference
| Bit | Name | Description |
|-----|------|-------------|
| 0 | ACKNOWLEDGE | Driver has found the device |
| 1 | DRIVER | Driver knows how to drive the device |
| 2 | DRIVER_OK | Driver is set up and ready |
| 3 | FEATURES_OK | Feature negotiation complete |
| 6 | NEEDS_RESET | Device experienced unrecoverable error |
| 7 | FAILED | Driver gave up on device |
## Additional Resources
### Linux Kernel VirtIO Implementation
- **Source**: https://github.com/torvalds/linux/tree/master/drivers/virtio
- **Documentation**: https://www.kernel.org/doc/Documentation/virtual/
### KVM/QEMU VirtIO
- **KVM Forum**: https://www.linux-kvm.org/page/Virtio
- **QEMU Wiki**: https://wiki.qemu.org/Features/VirtIO
### Books and Papers
- **"Hardware Virtualization: Platforms and Applications"** - Covers VirtIO design
- **"Virtual Machines: Versatile Platforms for Systems and Processes"** - Chapter on I/O virtualization
## RT-Thread Specific Documentation
- **[README.md](README.md)**: RT-Thread VirtIO usage guide
- **[TESTING.md](TESTING.md)**: Testing procedures for both legacy and modern modes
## Version History
| Date | Version | RT-Thread Support | Notes |
|------|---------|------------------|-------|
| 2022-07-14 | 1.2 | ✅ Yes | Latest specification |
| 2019-01-29 | 1.1 | ✅ Yes | Packed ring support |
| 2016-03-08 | 1.0 | ✅ Yes | Modern interface |
| ~2013 | 0.95 | ✅ Yes | Legacy interface (default) |
## Contact and Support
- **RT-Thread Forum**: https://club.rt-thread.io/
- **GitHub Issues**: https://github.com/RT-Thread/rt-thread/issues
- **VirtIO Mailing List**: virtio@lists.oasis-open.org
## License
VirtIO specifications are licensed under:
- OASIS Open Source License (VirtIO 1.0+)
RT-Thread VirtIO driver is licensed under:
- Apache License 2.0

View File

@@ -1,301 +0,0 @@
# VirtIO Testing Guide
This guide helps you test VirtIO support on RT-Thread with both legacy and modern versions.
## Prerequisites
- QEMU installed (version 2.4+ for modern VirtIO support)
- RT-Thread build environment
- RISC-V or AArch64 toolchain
## Testing Legacy VirtIO (v0.95)
### 1. Configure RT-Thread
```bash
cd bsp/qemu-virt64-riscv
scons --menuconfig
```
Navigate to:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
→ Select "VirtIO Legacy (v0.95)" or "VirtIO Legacy (v1.0 - alias for compatibility)"
```
### 2. Build
```bash
scons
```
### 3. Run with QEMU (Legacy Mode)
Force QEMU to use legacy VirtIO:
```bash
qemu-system-riscv64 \
-M virt \
-kernel rtthread.bin \
-nographic \
-device virtio-blk-device,disable-modern=on,drive=blk0 \
-drive if=none,id=blk0,file=sd.bin \
-device virtio-net-device,disable-modern=on,netdev=net0 \
-netdev user,id=net0
```
### 4. Verify
Check that VirtIO devices are detected:
```
msh /> list_device
device type ref count
------ -------------------- ----------
virtio-blk0 Block Device 0
virtio-net0 Network Interface 0
```
## Testing Modern VirtIO (v1.0+)
### 1. Configure RT-Thread
```bash
cd bsp/qemu-virt64-riscv
scons --menuconfig
```
Navigate to:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
→ Select "VirtIO Modern (v1.0+)"
```
### 2. Build
```bash
scons
```
### 3. Run with QEMU (Modern Mode)
Use default QEMU settings (modern VirtIO):
```bash
qemu-system-riscv64 \
-M virt \
-kernel rtthread.bin \
-nographic \
-device virtio-blk-device,drive=blk0 \
-drive if=none,id=blk0,file=sd.bin \
-device virtio-net-device,netdev=net0 \
-netdev user,id=net0
```
Or explicitly enable modern mode:
```bash
qemu-system-riscv64 \
-M virt \
-kernel rtthread.bin \
-nographic \
-device virtio-blk-device,disable-legacy=on,drive=blk0 \
-drive if=none,id=blk0,file=sd.bin \
-device virtio-net-device,disable-legacy=on,netdev=net0 \
-netdev user,id=net0
```
### 4. Verify
Check that VirtIO devices are detected:
```
msh /> list_device
device type ref count
------ -------------------- ----------
virtio-blk0 Block Device 0
virtio-net0 Network Interface 0
```
## Testing Auto-Detection (Recommended)
The driver can auto-detect the VirtIO version. To test this:
### 1. Build with Default Settings
Use the existing configuration (legacy by default):
```bash
cd bsp/qemu-virt64-riscv
scons
```
### 2. Test with Both QEMU Modes
**Test 1: Legacy QEMU**
```bash
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
-device virtio-blk-device,disable-modern=on,drive=blk0 \
-drive if=none,id=blk0,file=sd.bin
```
**Test 2: Modern QEMU**
```bash
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
-device virtio-blk-device,drive=blk0 \
-drive if=none,id=blk0,file=sd.bin
```
Both should work because the driver detects the version from the MMIO config.
## Testing Individual Devices
### VirtIO Block Device
```bash
# Create a test disk image
dd if=/dev/zero of=sd.bin bs=1M count=32
# Run QEMU with block device
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
-device virtio-blk-device,drive=blk0 \
-drive if=none,id=blk0,file=sd.bin
# In RT-Thread shell
msh /> mkfs -t elm virtio-blk0
msh /> mount virtio-blk0 / elm
```
### VirtIO Network Device
```bash
# Run QEMU with network device
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
-device virtio-net-device,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::5555-:23
# In RT-Thread shell
msh /> ifconfig
```
### VirtIO Console
```bash
# Run QEMU with console device
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
-device virtio-serial-device \
-chardev stdio,id=console0 \
-device virtconsole,chardev=console0
# Check for virtio-console devices
msh /> list_device
```
### VirtIO GPU
```bash
# Run QEMU with GPU device (requires display)
qemu-system-riscv64 -M virt -kernel rtthread.bin \
-device virtio-gpu-device \
-serial stdio
# Or with VNC
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
-device virtio-gpu-device \
-vnc :0
```
### VirtIO Input (Keyboard/Mouse)
```bash
# Run QEMU with input devices
qemu-system-riscv64 -M virt -kernel rtthread.bin \
-device virtio-keyboard-device \
-device virtio-mouse-device \
-device virtio-tablet-device
# Check for input devices
msh /> list_device
```
## Debugging
### Enable Debug Output
Add debug prints to check version detection:
```c
// In your application or driver init
rt_kprintf("VirtIO version: %d\n", virtio_dev->version);
rt_kprintf("VirtIO magic: 0x%x\n", virtio_dev->mmio_config->magic);
rt_kprintf("VirtIO device_id: %d\n", virtio_dev->mmio_config->device_id);
```
### Check QEMU Version
```bash
qemu-system-riscv64 --version
```
Modern VirtIO requires QEMU 2.4+.
### Check Feature Negotiation
Add debug output in `virtio_get_features` and `virtio_set_features` to verify feature negotiation.
## Expected Results
### Legacy Mode (Version 1)
- `dev->version == 1`
- Features are 32-bit
- Queue uses `queue_pfn` register
- No FEATURES_OK check
### Modern Mode (Version 2)
- `dev->version == 2`
- Features are 64-bit
- Queue uses separate desc/driver/device registers
- FEATURES_OK status check performed
- VERSION_1 feature negotiated
## Common Issues
### Issue: Device not detected
**Solution**: Check QEMU device configuration and memory mapping
### Issue: Feature negotiation fails
**Solution**: Verify QEMU supports the features you're requesting
### Issue: Queue initialization fails
**Solution**: Check memory alignment and addresses
### Issue: Build errors
**Solution**: Ensure Kconfig is properly configured and rtconfig.h is generated
## Performance Testing
### Block Device Performance
```bash
# In RT-Thread shell (with filesystem mounted)
msh /> dd if=/dev/zero of=/test.bin bs=4096 count=1000
```
### Network Performance
```bash
# Use iperf or similar tools
# Configure network and run throughput tests
```
## Reporting Issues
When reporting issues, include:
1. QEMU version
2. RT-Thread configuration (legacy/modern)
3. QEMU command line
4. Error messages or unexpected behavior
5. Debug output (if enabled)
## Reference
- [QEMU VirtIO Documentation](https://wiki.qemu.org/Features/VirtIO)
- [VirtIO Specification](https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html)
- [RT-Thread VirtIO README](README.md)