RustInQemu: Difference between revisions

From QEMU
 
(86 intermediate revisions by 3 users not shown)
Line 1: Line 1:
For the old RustInQemu page, see [[RustInQemu/2022]]
For the old RustInQemu page, see [[RustInQemu/2022]]


== Active efforts in 2024 ==
== Active effort ==


* <code>Subject: [RFC 0/6] scripts: Rewrite simpletrace printer in Rust</code><br /><code>Date: Mon, 27 May 2024 16:14:15 +0800</code><br />[https://lore.kernel.org/qemu-devel/20240527081421.2258624-1-zhao1.liu@intel.com/ RFC v1]
* [https://lore.kernel.org/qemu-devel/c2342e56-b6d8-4115-8318-d8047a46f1ad@redhat.com/T/ Current state]
* ARM PL011 UART device model in Rust<br /><code>Subject: [RFC PATCH v1 0/6] Implement ARM PL011 in Rust</code><br /><code>Date: Mon, 10 Jun 2024 21:22:35 +0300</code><br />[https://lore.kernel.org/qemu-devel/cover.rust-pl011-rfc-v1.git.manos.pitsidianakis@linaro.org/ RFC v1] [https://lore.kernel.org/qemu-devel/rust-pl011-rfc-v2.git.manos.pitsidianakis@linaro.org v2]
* [https://www.qemu.org/docs/master/devel/rust.html Documentation]
** Meson integration
* Past updates: [https://lore.kernel.org/qemu-devel/d3d1944e-2482-4aa7-b621-596246a08107@gnu.org/T/ April 2025] | [https://lore.kernel.org/qemu-devel/17ad81c3-98fc-44c2-8f65-f5e2cc07030b@gnu.org/T/ January 2025] [https://lore.kernel.org/qemu-devel/cc40943e-dec1-4890-a1d9-579350ce296f@pbonzini.local/T/ November 2024]
** Bindings generation


== Past efforts ==
== Past efforts ==


* <code>[RFC v3 00/32] Rust binding for QAPI and qemu-ga QMP handler examples</code><br />[https://patchew.org/QEMU/20210907121943.3498701-1-marcandre.lureau@redhat.com/ on patchew] [https://lore.kernel.org/qemu-devel/20210907121943.3498701-1-marcandre.lureau@redhat.com/ on lore]
* <code>[RFC v3 00/32] Rust binding for QAPI and qemu-ga QMP handler examples</code><br />[https://patchew.org/QEMU/20210907121943.3498701-1-marcandre.lureau@redhat.com/ on patchew] [https://lore.kernel.org/qemu-devel/20210907121943.3498701-1-marcandre.lureau@redhat.com/ on lore]
* <code>Subject: [RFC 0/6] scripts: Rewrite simpletrace printer in Rust</code><br /><code>Date: Mon, 27 May 2024 16:14:15 +0800</code><br />[https://lore.kernel.org/qemu-devel/20240527081421.2258624-1-zhao1.liu@intel.com/ RFC v1]
== Work in progress ==
=== To be merged ===
needs const_refs_static in Rust 1.83.0:
* complete vmstate bindings
* first round of typesafe qdev property generation


== Minimum supported version ==
=== Paolo ===
* 1.56.0: 2021 edition
* Improving Cargo support in Meson
* 1.59.0: const <tt>CStr::from_bytes_with_nul_unchecked</tt> (needed by cstr crate, see below)
* Tracing (summer of code 2025)
* 1.64.0: <tt>std::ffi::c_*</tt> (can use libc or std::os::raw)
* 1.74.0: Clippy can be configured in Cargo.toml
* 1.77.0: C string literals, offset_of!
**alternative: cstr crate, [https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=10a22a9b8393abd7b541d8fc844bc0df with_offsets macro]


== TODO ==
=== Manos ===
[https://patchew.org/QEMU/rust-pl011-rfc-v4.git.manos.pitsidianakis@linaro.org/ Before commit wishlist]:
* generate qdev properties via macros
* TODO comments when the code is doing potential undefined behavior
* module structure should resemble the C part of the tree?
* only generate bindings.rs.inc once
* trait to generate all-zero structs without having to type "unsafe { MaybeUninit::zeroed().assume_init() }"
* avoid disabling lints that are too broad (cast_ptr_alignment, missing_safety_doc)
* add support for --cargo and CARGO environment variables
* use ctor instead of non-portable linker magic, and the cstr crate instead of CStr statics or c""
* check if -Wl,--whole-archive can be replaced with link_whole:


After commit:
=== Zhao ===
* [https://lore.kernel.org/qemu-devel/1c53f8d2-3b33-404c-bb1c-38475087d7ae@redhat.com/ single cargo build]: using workspace instead of duplicating large parts of Cargo.toml
* vm-memory integration
** Kconfig integration
* CI integration
** lints (see below)
* lower minimum supported version (Debian needs 1.63.0)
* eliminate undefined behavior:
** no aliasing of &mut
** MaybeUninit initialization (long-term: investigate [https://docs.rs/pinned-init/ pinned_init crate, originating from Linux])


other experiments at https://github.com/bonzini/rust-qemu
=== Kevin ===
** Generic Rust<->C interop, Error, QOM reference counting
* Executing async code, block driver implementations
** Chardev
** https://lore.kernel.org/qemu-devel/20250211214328.640374-1-kwolf@redhat.com/
** MemoryRegion, SysbusDevice


== Ideas for lints without breaking CI ==
== Bugs ==
See https://github.com/bonzini/rust-qemu/commit/95b25f7c5f4e2694a85a5503050cc98da7562c7c
* Add BQL abstraction for tests - avoid running them with "--test-threads 1"
* run clippy as part of "make check", possibly only if Rust is newer than some version (1.74.0 so that clippy can be configured in Cargo.toml?)
* [https://github.com/mesonbuild/meson/issues/8828 Distros would prefer to link libstd dynamically]; Rust-enabled QEMU binaries are huge
* deny many individual lints, do not deny groups (complexity, perf, style, suspicious) on regular builds. allow unknown_lints.
* add to CI a fallible job that runs on nightly clippy with <tt>-Dclippy::complexity -Dclippy::perf -Dclippy::suspicious -Dclippy::style -Dunknown_lints</tt>. the job should generally pass, and if a new lint triggers it probably should be added to Cargo.toml as either "allow" (rare) or "deny" (possibly after adding #[allow()] to the source).


== Possible project targets ==
== Possible project targets ==
=== Projects with some code ===
* visitors/QOM properties, could be interesting for <code>hw/sensor/tmp105.c</code>...
** https://lore.kernel.org/qemu-devel/20250605101124.367270-1-pbonzini@redhat.com/
=== Check if distros package the dependent Rust crates ===
Mostly useful for distros to enable Rust when building QEMU.
* Missing in Fedora: arbitrary-int, bilge, bilge-impl, foreign
** Fedora does have bitfield-struct, which is a bit inferior to bilge in some respects but smaller
* Debian?
=== Cleanups ===
* split qemu_api into multiple crates (each with its own prelude, too)
* look into replacing pointer and BqlCell usage in HPET with [https://docs.rs/self_cell/latest/self_cell/# self_cell] (e.g. BqlRefCell&lt;SelfCell&lt;HPETRegisters, [HPETTimer; 8]&gt;&gt;</tt>).
=== Nice things to have ===
* Remove need for manual "meson subprojects update --reset" when updating packagefiles/
* Improve tool integration
** Run code checks a la "meson test --suite codecheck"? (clippy and rustfmt)
* Place rustdoc output for master somewhere?
* distros need dynamically linked libstd
** Meson 1.9.0 supports -Drust_dynamic_std=true for executables
** Add #![no_main] to output of scripts/rust/rust_root_crate.sh, and add it to the final emulator link (needs https://github.com/mesonbuild/meson/pull/14818)
* More bindings: I2C?
=== New code to write===
* more QOM procedural macros
** generate qdev properties (started)
** generate parts of TypeInfo?
** monomorphization of <tt>T: IsA&lt;Class&gt;</tt> to <tt>&T</tt> (like the momo crate)
* safe object creation with [https://docs.rs/pinned-init/ pinned_init, originating from Linux])
** https://lore.kernel.org/qemu-devel/aCysct2L8Bosqy0N@intel.com/
* Character device flow control
=== Improved clippy coverage in CI? ===
* run clippy as part of "make check"
* add to CI a fallible job that runs on nightly clippy with <tt>-Dclippy::complexity -Dclippy::perf -Dclippy::suspicious -Dclippy::style -Dunknown_lints</tt>. the job should generally pass, and if a new lint triggers it probably should be added to Cargo.toml as either "allow" (rare) or "deny" (possibly after adding #[allow()] to the source).


=== Miscellanea  ===
=== Miscellanea  ===


- <code>qemu-bridge-helper.c</code> Re-write SUID C executable with useful features.
* <code>qemu-bridge-helper.c</code> Re-write SUID C executable with useful features.


=== Devices  ===
=== Devices  ===


- <code>hw/block/pflash_cfi01.c</code>, <code>hw/block/pflash_cfi02.c</code> (claimed, WIP)
* <code>hw/block/pflash_cfi01.c</code>, <code>hw/block/pflash_cfi02.c</code> (claimed, WIP)
 
** needs block bindings
- <code>hw/mem/nvdimm.c</code> (suggested by [[User:Manos|Manos]])
* <code>hw/timer/i8254.c</code> (claimed, WIP)
 
* <code>hw/mem/nvdimm.c</code> (suggested by [[User:Manos|Manos]])
- <code>hw/timer/i8254.c</code> (claimed, WIP)


[[Category:RustInQemu]]
[[Category:RustInQemu]]

Latest revision as of 13:14, 25 July 2025

For the old RustInQemu page, see RustInQemu/2022

Active effort

Past efforts

  • [RFC v3 00/32] Rust binding for QAPI and qemu-ga QMP handler examples
    on patchew on lore
  • Subject: [RFC 0/6] scripts: Rewrite simpletrace printer in Rust
    Date: Mon, 27 May 2024 16:14:15 +0800
    RFC v1

Work in progress

To be merged

needs const_refs_static in Rust 1.83.0:

  • complete vmstate bindings
  • first round of typesafe qdev property generation

Paolo

  • Improving Cargo support in Meson
  • Tracing (summer of code 2025)

Manos

  • generate qdev properties via macros

Zhao

  • vm-memory integration

Kevin

Bugs

Possible project targets

Projects with some code

Check if distros package the dependent Rust crates

Mostly useful for distros to enable Rust when building QEMU.

  • Missing in Fedora: arbitrary-int, bilge, bilge-impl, foreign
    • Fedora does have bitfield-struct, which is a bit inferior to bilge in some respects but smaller
  • Debian?

Cleanups

  • split qemu_api into multiple crates (each with its own prelude, too)
  • look into replacing pointer and BqlCell usage in HPET with self_cell (e.g. BqlRefCell<SelfCell<HPETRegisters, [HPETTimer; 8]>>).

Nice things to have

  • Remove need for manual "meson subprojects update --reset" when updating packagefiles/
  • Improve tool integration
    • Run code checks a la "meson test --suite codecheck"? (clippy and rustfmt)
  • Place rustdoc output for master somewhere?
  • distros need dynamically linked libstd
  • More bindings: I2C?

New code to write

Improved clippy coverage in CI?

  • run clippy as part of "make check"
  • add to CI a fallible job that runs on nightly clippy with -Dclippy::complexity -Dclippy::perf -Dclippy::suspicious -Dclippy::style -Dunknown_lints. the job should generally pass, and if a new lint triggers it probably should be added to Cargo.toml as either "allow" (rare) or "deny" (possibly after adding #[allow()] to the source).

Miscellanea

  • qemu-bridge-helper.c Re-write SUID C executable with useful features.

Devices

  • hw/block/pflash_cfi01.c, hw/block/pflash_cfi02.c (claimed, WIP)
    • needs block bindings
  • hw/timer/i8254.c (claimed, WIP)
  • hw/mem/nvdimm.c (suggested by Manos)