rust/hw: Add the I2C and the first GPIO device
Subsystems (2)
v3 cover rust/hw: Add the I2C and the first GPIO device
From: Chen Miao <[email protected]>
We have implemented I2C and the first GPIO device in Rust for QEMU. Additionally, in the respective patches, we have shared our insights and experiences regarding the use of Rust for device modeling within QEMU.
1. The first patch implements the BusState for the I2CBus infrastructure.
2. The second patch implements the I2CBus and I2CSlave infrastructure, along with a discussion of the challenges encountered during the implementation.
3. The third patch provides a set of necessary helper functions for the PCF8574 GPIO device.
4. The fourth patch implements the PCF8574 GPIO device, along with a discussion of the issues and considerations addressed during the implementation.
---
Due to issues with the email service provider, the previous PATCH series was not properly displayed as a thread. Subsequently, the resent emails were accidentally merged into a single thread, making the review process difficult. Therefore, resending it and hope to receive review.
chenmiao (4):
rust/hw/core: Add the BusState of rust version rust/hw/core: Add rust bindings/funcs for i2c bus rust/hw/core: Provide some interfaces for the GPIO device rust/hw/gpio: Add the the first gpio device pcf8574
hw/gpio/Kconfig | 5 + hw/gpio/meson.build | 2 +- rust/Cargo.lock | 18 +- rust/Cargo.toml | 1 + rust/hw/Kconfig | 1 + rust/hw/core/meson.build | 2 + rust/hw/core/src/bus.rs | 44 +++++ rust/hw/core/src/i2c.rs | 303 +++++++++++++++++++++++++++++++ rust/hw/core/src/lib.rs | 6 + rust/hw/core/src/qdev.rs | 17 +- rust/hw/core/wrapper.h | 1 + rust/hw/gpio/Kconfig | 2 + rust/hw/gpio/meson.build | 1 + rust/hw/gpio/pcf8574/Cargo.toml | 28 +++ rust/hw/gpio/pcf8574/meson.build | 37 ++++ rust/hw/gpio/pcf8574/src/lib.rs | 178 ++++++++++++++++++ rust/hw/meson.build | 1 + 17 files changed, 642 insertions(+), 5 deletions(-) create mode 100644 rust/hw/core/src/bus.rs create mode 100644 rust/hw/core/src/i2c.rs create mode 100644 rust/hw/gpio/Kconfig create mode 100644 rust/hw/gpio/meson.build create mode 100644 rust/hw/gpio/pcf8574/Cargo.toml create mode 100644 rust/hw/gpio/pcf8574/meson.build create mode 100644 rust/hw/gpio/pcf8574/src/lib.rs
Signature · 1 line
2.43.0
PATCH v3 4/4 rust/hw/gpio: Add the the first gpio device pcf8574
From: chenmiao <[email protected]>
After implementing the I2CBus and I2CSlave, we proceeded to implement a basic GPIO device — the PCF8574 — which depends on I2CSlave.
This patch must depend on the below link to compile normally:
https://lists.nongnu.org/archive/html/qemu-devel/2025-10/msg07356.html
At present, I have summarized the general workflow for device modeling in Rust as follows:
1. Modify the configuration under hw/deviceto distinguish between C and Rust versions.
2. Create a device crate under rust/hw.
3. Add (or copy) the necessary wrappers, build.rs, and bindings.
4. Begin the device modeling process.
5. Construct the corresponding structures in Rust that mirror those in C, especially for “members that may change”.
6. Referencing the C implementation, define initialization functions and establish parent-class relationships for the Rust structure.
7. Set up ObjectImpl, DeviceImpl, and ResettablePhasesImpl.
8. Configure vmstate.
9. Implement other functional methods.
--- hw/gpio/Kconfig | 5 + hw/gpio/meson.build | 2 +- rust/Cargo.lock | 18 +++- rust/Cargo.toml | 1 + rust/hw/Kconfig | 1 + rust/hw/gpio/Kconfig | 2 + rust/hw/gpio/meson.build | 1 + rust/hw/gpio/pcf8574/Cargo.toml | 28 +++++ rust/hw/gpio/pcf8574/meson.build | 37 +++++++ rust/hw/gpio/pcf8574/src/lib.rs | 178 +++++++++++++++++++++++++++++++ rust/hw/meson.build | 1 + 11 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 rust/hw/gpio/Kconfig create mode 100644 rust/hw/gpio/meson.build create mode 100644 rust/hw/gpio/pcf8574/Cargo.toml create mode 100644 rust/hw/gpio/pcf8574/meson.build create mode 100644 rust/hw/gpio/pcf8574/src/lib.rs
hw/gpio/Kconfig +5 -0
@@ -27,6 +27,11 @@ config PCA9554 config PCF8574 bool depends on I2C + select PCF8574_C if !HAVE_RUST + select X_PCF8574_RUST if HAVE_RUST + +config PCF8574_C + bool config ZAURUS_SCOOP bool
hw/gpio/meson.build +1 -1
@@ -17,4 +17,4 @@ system_ss.add(when: 'CONFIG_RASPI', if_true: files( system_ss.add(when: 'CONFIG_STM32L4X5_SOC', if_true: files('stm32l4x5_gpio.c')) system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_gpio.c')) system_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c')) -system_ss.add(when: 'CONFIG_PCF8574', if_true: files('pcf8574.c')) +system_ss.add(when: 'CONFIG_PCF8574_C', if_true: files('pcf8574.c'))
rust/Cargo.lock +17 -1
@@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "anyhow"
@@ -204,6 +204,22 @@ dependencies = [ "util", ] +[[package]] +name = "pcf8574" +version = "0.1.0" +dependencies = [ + "bits", + "bql", + "common", + "glib-sys", + "hwcore", + "migration", + "qom", + "system", + "trace", + "util", +] + [[package]] name = "pkg-config" version = "0.3.32"
rust/Cargo.toml +1 -0
@@ -10,6 +10,7 @@ members = [ "system", "hw/core", "hw/char/pl011", + "hw/gpio/pcf8574", "hw/timer/hpet", "trace", "util",
rust/hw/Kconfig +1 -0
@@ -1,3 +1,4 @@ # devices Kconfig source char/Kconfig +source gpio/Kconfig source timer/Kconfig
rust/hw/gpio/Kconfig new file +2 -0
@@ -0,0 +1,2 @@ +config X_PCF8574_RUST + bool
rust/hw/gpio/meson.build new file +1 -0
@@ -0,0 +1 @@ +subdir('pcf8574')
rust/hw/gpio/pcf8574/Cargo.toml new file +28 -0
@@ -0,0 +1,28 @@ +[package] +name = "pcf8574" +version = "0.1.0" +authors = ["Chen Miao <[email protected]>"] +description = "pcf8574 device model for QEMU" +resolver = "2" +publish = false + +edition.workspace = true +homepage.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true + +[dependencies] +glib-sys.workspace = true +bits = { path = "../../../bits" } +common = { path = "../../../common" } +util = { path = "../../../util" } +bql = { path = "../../../bql" } +migration = { path = "../../../migration" } +qom = { path = "../../../qom" } +system = { path = "../../../system" } +hwcore = { path = "../../../hw/core" } +trace = { path = "../../../trace" } + +[lints] +workspace = true
rust/hw/gpio/pcf8574/meson.build new file +37 -0
@@ -0,0 +1,37 @@ +# TODO: Remove this comment when the clang/libclang mismatch issue is solved. +# +# Rust bindings generation with `bindgen` might fail in some cases where the +# detected `libclang` does not match the expected `clang` version/target. In +# this case you must pass the path to `clang` and `libclang` to your build +# command invocation using the environment variables CLANG_PATH and +# LIBCLANG_PATH +_libpcf8574_rs = static_library( + 'pcf8574', + structured_sources( + [ + 'src/lib.rs', + ], + ), + override_options: ['rust_std=2021', 'build.rust_std=2021'], + rust_abi: 'rust', + dependencies: [ + bilge_rs, + bilge_impl_rs, + bits_rs, + common_rs, + glib_sys_rs, + util_rs, + migration_rs, + bql_rs, + qom_rs, + chardev_rs, + system_rs, + hwcore_rs, + trace_rs + ], +) + +rust_devices_ss.add(when: 'CONFIG_X_PCF8574_RUST', if_true: [declare_dependency( + link_whole: [_libpcf8574_rs], + variables: {'crate': 'pcf8574'}, +)])
rust/hw/gpio/pcf8574/src/lib.rs new file +178 -0
178 lines not on this page · raw message
rust/hw/meson.build +1 -0
@@ -1,2 +1,3 @@ subdir('char') +subdir('gpio') subdir('timer')
Signature · 1 line
2.43.0
PATCH v3 1/4 rust/hw/core: Add the BusState of rust version
In reply to ChenMiaoFrom: chenmiao <[email protected]>
A Rust version implementation has been designed for BusState, which will be used for the subsequent I2CBus implementation.
--- rust/hw/core/meson.build | 1 + rust/hw/core/src/bus.rs | 44 ++++++++++++++++++++++++++++++++++++++++ rust/hw/core/src/lib.rs | 3 +++ 3 files changed, 48 insertions(+) create mode 100644 rust/hw/core/src/bus.rs
rust/hw/core/meson.build +1 -0
@@ -50,6 +50,7 @@ _hwcore_rs = static_library( [ 'src/lib.rs', 'src/bindings.rs', + 'src/bus.rs', 'src/irq.rs', 'src/qdev.rs', 'src/sysbus.rs',
rust/hw/core/src/bus.rs new file +44 -0
44 lines not on this page · raw message
rust/hw/core/src/lib.rs +3 -0
@@ -13,3 +13,6 @@mod sysbus;pub use sysbus::*; + +mod bus; +pub use bus::*;
Signature · 1 line
2.43.0
PATCH v3 3/4 rust/hw/core: Provide some interfaces for the GPIO device
In reply to ChenMiaoFrom: chenmiao <[email protected]>
In qdev.rs, we implemented the init_gpio_out_named function, which corresponds to the C function qdev_init_gpio_out_named. We also refactored the init_gpio_out function to reuse the init_gpio_out_named interface.
--- rust/hw/core/src/qdev.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
rust/hw/core/src/qdev.rs +14 -3
@@ -17,7 +17,7 @@pub use crate ::bindings::{ClockEvent ,DeviceClass ,Property ,ResetType };use crate ::{ - bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out,ResettableClass }, + bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out_named,ResettableClass }, irq::InterruptSource , };
@@ -399,11 +399,22 @@ fn do_init_gpio_in( }fn init_gpio_out (&self, pins: &[InterruptSource ]) { + self.init_gpio_out_named (pins,"unnamed-gpio-out" , pins.len ()); + } + +fn init_gpio_out_named (&self, pins: &[InterruptSource ], name: &str , n:usize ) { +let c_name =if name.is_empty () { +CString ::new ("unnamed-gpio-out" ).unwrap () + }else { +CString ::new (name).unwrap () + }; +unsafe { -qdev_init_gpio_out ( +qdev_init_gpio_out_named ( self.upcast ().as_mut_ptr (),InterruptSource ::slice_as_ptr (pins), - pins.len ()as c_int , + c_name.as_ptr (), + nas c_int , ); } }
Signature · 1 line
2.43.0
PATCH v3 2/4 rust/hw/core: Add rust bindings/funcs for i2c bus
In reply to ChenMiaoFrom: chenmiao <[email protected]>
We have implemented the I2CBus and I2CSlave infrastructure in Rust by referring to the SysBus device model.
Initially, we assumed that the I2CBus was at the same hierarchical level as the PL011 device. Therefore, we followed the implementation paradigm of the PL011 device as a reference. However, in the end, we discovered that the I2CBus is actually at the same level as the SysBus. As a result, we adopted the binding implementation paradigm used for SysBus devices. With this adjustment, we successfully compiled the code locally.
During the implementation process, we found that the current two paradigms in Rust — bindings and impl — are extremely complex and lack comprehensive documentation. There is no clear explanation as to why Bus and Device models need to be implemented using different approaches. Furthermore, the implementation of Bus and Device following these paradigms still has many limitations. At present, at least vmstate is not easily supported.
--- rust/hw/core/meson.build | 1 + rust/hw/core/src/i2c.rs | 303 +++++++++++++++++++++++++++++++++++++++ rust/hw/core/src/lib.rs | 3 + rust/hw/core/wrapper.h | 1 + 4 files changed, 308 insertions(+) create mode 100644 rust/hw/core/src/i2c.rs
rust/hw/core/meson.build +1 -0
@@ -52,6 +52,7 @@ _hwcore_rs = static_library( 'src/bindings.rs', 'src/bus.rs', 'src/irq.rs', + 'src/i2c.rs', 'src/qdev.rs', 'src/sysbus.rs', ],
rust/hw/core/src/i2c.rs new file +303 -0
303 lines not on this page · raw message
rust/hw/core/src/lib.rs +3 -0
@@ -16,3 +16,6 @@mod bus;pub use bus::*; + +mod i2c; +pub use i2c::*;
rust/hw/core/wrapper.h +1 -0
@@ -30,3 +30,4 @@ typedef enum memory_order {#include "hw/qdev-properties.h" #include "hw/qdev-properties-system.h" #include "hw/irq.h" +#include "hw/i2c/i2c.h"
Signature · 1 line
2.43.0