RustInQemu: Difference between revisions

From QEMU
 
(63 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 ==


* [https://lore.kernel.org/qemu-devel/d3d1944e-2482-4aa7-b621-596246a08107@gnu.org/ Current state]
* [https://www.qemu.org/docs/master/devel/rust.html Documentation]
* Past updates: [https://lore.kernel.org/qemu-devel/17ad81c3-98fc-44c2-8f65-f5e2cc07030b@gnu.org/ January 2025] |  [https://lore.kernel.org/all/cc40943e-dec1-4890-a1d9-579350ce296f@pbonzini.local/ November 2024]
== 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>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]
* <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]
* 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-v6.git.manos.pitsidianakis@linaro.org/ v6]
** Meson and configure integration
** Bindings generation


After some discussion we decided to use rustc directly without going through cargo and each external crate becomes a Meson subproject. Meson 1.5.0+'s support for <tt>method = cargo</tt> is pretty good for host-compiled packages, but it doesn't work for dependencies of build-compiled packages (i.e. dependencies of procedural macros). This is tracked by https://github.com/mesonbuild/meson/issues/11121. Until then, meson.build files for procedural macros and their dependencies have to be written by hand. (Currently _all_ dependencies use hand-written meson.build).
== Work in progress ==
=== Paolo ===
* Improving Cargo support in Meson


While cargo would be fine as an interim solution, using Meson to compile the Rust code removes some moving parts. The extra cost of writing meson.build files for dependencies is expected to be small and will mostly happen upfront.
=== Manos ===
* generate qdev properties via macros


== Past efforts ==
=== Zhao ===
* vm-memory?


* <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]
=== Kevin ===
* Executing async code, block driver implementations
** https://lore.kernel.org/qemu-devel/20250211214328.640374-1-kwolf@redhat.com/


== Minimum supported versions ==
== Bugs ==
1.59.0 has const <tt>CStr::from_bytes_with_nul_unchecked</tt>, not really possible to go below that.
* Add BQL abstraction for tests - avoid running them with "--test-threads 1"
* [https://github.com/mesonbuild/meson/issues/8828 Distros would prefer to link libstd dynamically]; Rust-enabled QEMU binaries are huge


Bindgen 0.60.x has --allowlist-file. Ubuntu 22.04 has 0.59.x which is a problem.
== Possible project targets ==


In the future 1.83.0 would allow nested offset_of.
=== 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/
* complete vmstate bindings (needs const_refs_static in Rust 1.83.0, Zhao+Paolo have a prototype)
** https://lore.kernel.org/qemu-devel/20250505100854.73936-1-pbonzini@redhat.com/


== Done ==
=== Cleanups ===
* only generate bindings.rs.inc once
* split qemu_api into multiple crates
* disabling the cast_ptr_alignment lint is too broad
* update bundled meson to 1.5.0
* add support for --rustc and RUSTC environment variables
* remove --no-include-path-detection from bindgen invocation
* rename subprojects to use Meson 1.5.0+ convention and add meson.override_dependency() call
* add support for cross compilation of Rust subprojects (<tt>native: true</tt> for deps of procedural macro crates)
* fix licenses for Rust subprojects


== Posted ==
=== Nice things to have ===
* Enable use of rustc 1.63.0
* Remove need for manual "meson subprojects update --reset" when updating packagefiles/
** fix cfgs for proc-macro2
* Improve tool integration
** core::ffi (stable in 1.64.0)
** Run code checks a la "meson test --suite codecheck"? (clippy and rustfmt)
** "if let" usage in bilge-impl needs to be patched out (stable in 1.65.0)
* Place rustdoc output for master somewhere?
** std::sync::OnceLock (stable in 1.70.0)
* distros need dynamically linked libstd
** MaybeUninit::zeroed() (stable in 1.75.0 in const context)
** rewrite all main()s in Rust and use [https://github.com/mesonbuild/meson/issues/14224 -C prefer-dynamic]?
** c"" literals (stable in 1.77.0)
** or should Meson be able to link C programs with rlibs?
** offset_of! (stable in 1.77.0)
* Enable use of older bindgen
** What to do about Ubuntu 22.04? Bindgen 0.59.x does not have --allowlist-file
* Install rustc/bindgen in all containers


=== New code to write===
* more QOM procedural macros
** generate qdev properties
** 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])
* DMA (investigate vm-memory?)
** https://lore.kernel.org/qemu-devel/aCysct2L8Bosqy0N@intel.com/
* feature parity for pl011
* feature parity for pl011
** [https://lore.kernel.org/qemu-devel/CAFEAcA9X5iBFMEw3dxsgBPZo7FBuNYp3dK5gf7v+cEyVa+pErA@mail.gmail.com/T/ support pl011-luminary]
** Trace/log (Summer of Code)
** migration
== To be posted ==
* qemu_irq (https://gitlab.com/bonzini/qemu/-/commit/b04e20648568f7b73de8af2bc5c6b0dadca3dbaf)
* QOM (https://gitlab.com/bonzini/qemu/-/commits/rust-qom (defining QOM classes entirely without macros, QOM casts and reference counting)
** also adds documentation for public APIs in qemu-api and qemu-api-macros
== TODO ==
* Remove need for manual "meson subprojects update --reset" when updating packagefiles/
* Remove remaining uses of Cargo, or clean up. Either all this should be driven entirely by Meson (for example via compile_commands.json, similar to scripts/check_sparse.py), or it should use a Cargo workspace so that a single invocation of "cargo clippy" will handle all crates.
** Perform clippy checks ("ninja clippy"? Maybe "meson test --suite codecheck"?)
** Reformat code ("ninja rustfmt"? Maybe also include it in the same suite as clippy?)
** Generate documentation ("ninja rustdoc"? Maybe check how Linux runs doctests?)
* More CI
** Rustfmt checks?
** Run nightly clippy
** Rustdoc
* feature parity for pl011
** check for new commits
* bindings
** MemoryRegion (callbacks)
** Chardev (callbacks, functions)
** DMA (investigate vm-memory?)
* other experiments: https://github.com/bonzini/rust-qemu (Generic Rust<->C interop, Error)
** safe object creation: use MaybeUninit in instance_init? (long-term investigate [https://docs.rs/pinned-init/ pinned_init crate, originating from Linux])
* more QOM procedural macros (currently <tt>#[derive(Object)]</tt>)
** generate qdev properties?
** generate parts of TypeInfo?


== Ideas for lints without breaking CI ==
=== Improved clippy coverage in CI? ===
See https://github.com/bonzini/rust-qemu/commit/95b25f7c5f4e2694a85a5503050cc98da7562c7c
* run clippy as part of "make check"
* 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?)
* 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).
* 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 ==


=== 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  ===
Line 92: Line 74:
** needs block bindings
** needs block bindings
* <code>hw/timer/i8254.c</code> (claimed, WIP)
* <code>hw/timer/i8254.c</code> (claimed, WIP)
** needs timer bindgings
* <code>hw/mem/nvdimm.c</code> (suggested by [[User:Manos|Manos]])
* <code>hw/mem/nvdimm.c</code> (suggested by [[User:Manos|Manos]])


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

Latest revision as of 13:35, 5 June 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

Paolo

  • Improving Cargo support in Meson

Manos

  • generate qdev properties via macros

Zhao

  • vm-memory?

Kevin

Bugs

Possible project targets

Projects with some code

Cleanups

  • split qemu_api into multiple crates

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
    • rewrite all main()s in Rust and use -C prefer-dynamic?
    • or should Meson be able to link C programs with rlibs?

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)