RustInQemu: Difference between revisions

From QEMU
(Update the hpet device status)
 
(36 intermediate revisions by 2 users not shown)
Line 4: Line 4:


* <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-v2.git.manos.pitsidianakis@linaro.org v2]
* 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 integration
** Meson and configure integration
** Bindings generation
** 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).
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.


== Past efforts ==
== Past efforts ==
Line 12: Line 16:
* <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]


== Minimum supported version ==
== Minimum supported versions ==
* 1.56.0: 2021 edition
1.59.0 has const <tt>CStr::from_bytes_with_nul_unchecked</tt>, not really possible to go below that.
* 1.59.0: const <tt>CStr::from_bytes_with_nul_unchecked</tt> (needed by cstr crate, see below)
 
* 1.64.0: <tt>std::ffi::c_*</tt> (can use libc or std::os::raw)
Bindgen 0.60.x has --allowlist-file. Ubuntu 22.04 has 0.59.x which is a problem.
* 1.74.0: Clippy can be configured in Cargo.toml
 
* 1.77.0: C string literals, offset_of!
In the future 1.83.0 would allow nested offset_of.
**alternative: cstr crate, [https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=10a22a9b8393abd7b541d8fc844bc0df with_offsets macro]


== TODO ==
== Done ==
[https://patchew.org/QEMU/rust-pl011-rfc-v4.git.manos.pitsidianakis@linaro.org/ Before commit wishlist]:
* 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
* only generate bindings.rs.inc once
* trait to generate all-zero structs without having to type "unsafe { MaybeUninit::zeroed().assume_init() }"
* disabling the cast_ptr_alignment lint is too broad
* avoid disabling lints that are too broad (cast_ptr_alignment, missing_safety_doc)
* update bundled meson to 1.5.0
* add support for --cargo and CARGO environment variables
* add support for --rustc and RUSTC environment variables
* use ctor instead of non-portable linker magic, and the cstr crate instead of CStr statics or c""
* remove --no-include-path-detection from bindgen invocation
* check if -Wl,--whole-archive can be replaced with link_whole:
* 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
 
* Enable use of rustc 1.63.0
** fix cfgs for proc-macro2
** core::ffi (stable in 1.64.0)
** "if let" usage in bilge-impl needs to be patched out (stable in 1.65.0)
** std::sync::OnceLock (stable in 1.70.0)
** MaybeUninit::zeroed() (stable in 1.75.0 in const context)
** c"" literals (stable in 1.77.0)
** 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
 
* feature parity for pl011
** [https://lore.kernel.org/qemu-devel/CAFEAcA9X5iBFMEw3dxsgBPZo7FBuNYp3dK5gf7v+cEyVa+pErA@mail.gmail.com/T/ support pl011-luminary]
** migration
* clean up Cargo usage via workspace


After commit:
== Posted ==
* [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
* [https://lore.kernel.org/qemu-devel/20241108180139.117112-1-pbonzini@redhat.com/T/ improved integration with cargo]
** Kconfig integration
** automatically generate rustc flags from Cargo.toml
* CI integration
** Perform clippy checks and reformat code ("ninja clippy", "ninja rustfmt") - TODO: rustfmt configuration currently requires nightly due to use of unstable features
** lints (see below)
** Rustfmt, clippy, rustdoc in CI
* lower minimum supported version (Debian needs 1.63.0)
* eliminate undefined behavior:
** no aliasing of &mut
** use MaybeUninit in instance_init (long-term: investigate [https://docs.rs/pinned-init/ pinned_init crate, originating from Linux])


other experiments at https://github.com/bonzini/rust-qemu
* [https://lore.kernel.org/qemu-devel/20241104085159.76841-1-pbonzini@redhat.com/T/ qemu_irq + interior mutability]
** Generic Rust<->C interop, Error, QOM reference counting
== Work in progress ==
** Chardev
=== Paolo ===
** MemoryRegion, SysbusDevice
* [https://gitlab.com/bonzini/qemu/-/commits/rust-next irq + errno + chardev + doctests]
* [https://gitlab.com/bonzini/qemu/-/commits/rust-qom QOM] (defining QOM classes entirely without macros, QOM casts and reference counting)
** also documentation for public APIs in qemu-api and qemu-api-macros
* Upstream Meson support for clippy, rustfmt, rustdoc
 
=== Manos ===
* [https://lore.kernel.org/qemu-devel/ml0lj.kelxgd4xtqc7@linaro.org/raw Fix functional tests]
* Documentation
=== Kevin ===
* Executing async code
* Block driver implementations
 
=== pl011 breakage ===
reproducer:
 
wget https://archives.fedoraproject.org/pub/archive/fedora/linux/releases/29/Everything/armhfp/os/images/pxeboot/vmlinuz
./qemu-system-arm -kernel vmlinuz -append 'printk.time=0 console=ttyAMA0' -M virt -nographic
 
== Work in progress ==
* hpet timer device
** timer/bitops/memattrs bindings
 
== TODO ==
* 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)
* rustfmt currently requires nightly, decide what to do about it
* Place rustdoc output for master somewhere?
* 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?
* For hpet device
** need error/irq/trace bindings.


== Ideas for lints without breaking CI ==
== Ideas for lints without breaking CI ==
Line 60: Line 116:
=== 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/hpet.c, hw/timer/i8254.c</code> (claimed, WIP)
 
** needs timer/memattrs/bitops/error/irq/trace bindgings
- <code>hw/timer/i8254.c</code> (claimed, WIP)
* <code>hw/mem/nvdimm.c</code> (suggested by [[User:Manos|Manos]])


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

Latest revision as of 09:01, 21 November 2024

For the old RustInQemu page, see RustInQemu/2022

Active efforts in 2024

  • Subject: [RFC 0/6] scripts: Rewrite simpletrace printer in Rust
    Date: Mon, 27 May 2024 16:14:15 +0800
    RFC v1
  • ARM PL011 UART device model in Rust
    Subject: [RFC PATCH v1 0/6] Implement ARM PL011 in Rust
    Date: Mon, 10 Jun 2024 21:22:35 +0300
    RFC v1 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 method = cargo 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).

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.

Past efforts

  • [RFC v3 00/32] Rust binding for QAPI and qemu-ga QMP handler examples
    on patchew on lore

Minimum supported versions

1.59.0 has const CStr::from_bytes_with_nul_unchecked, not really possible to go below that.

Bindgen 0.60.x has --allowlist-file. Ubuntu 22.04 has 0.59.x which is a problem.

In the future 1.83.0 would allow nested offset_of.

Done

  • only generate bindings.rs.inc once
  • 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 (native: true for deps of procedural macro crates)
  • fix licenses for Rust subprojects
  • Enable use of rustc 1.63.0
    • fix cfgs for proc-macro2
    • core::ffi (stable in 1.64.0)
    • "if let" usage in bilge-impl needs to be patched out (stable in 1.65.0)
    • std::sync::OnceLock (stable in 1.70.0)
    • MaybeUninit::zeroed() (stable in 1.75.0 in const context)
    • c"" literals (stable in 1.77.0)
    • 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

Posted

  • improved integration with cargo
    • automatically generate rustc flags from Cargo.toml
    • Perform clippy checks and reformat code ("ninja clippy", "ninja rustfmt") - TODO: rustfmt configuration currently requires nightly due to use of unstable features
    • Rustfmt, clippy, rustdoc in CI

Work in progress

Paolo

  • irq + errno + chardev + doctests
  • QOM (defining QOM classes entirely without macros, QOM casts and reference counting)
    • also documentation for public APIs in qemu-api and qemu-api-macros
  • Upstream Meson support for clippy, rustfmt, rustdoc

Manos

Kevin

  • Executing async code
  • Block driver implementations

pl011 breakage

reproducer:

wget https://archives.fedoraproject.org/pub/archive/fedora/linux/releases/29/Everything/armhfp/os/images/pxeboot/vmlinuz
./qemu-system-arm -kernel vmlinuz -append 'printk.time=0 console=ttyAMA0' -M virt -nographic

Work in progress

  • hpet timer device
    • timer/bitops/memattrs bindings

TODO

  • 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)
  • rustfmt currently requires nightly, decide what to do about it
  • Place rustdoc output for master somewhere?
  • feature parity for pl011
    • check for new commits
  • bindings
    • MemoryRegion (callbacks)
    • Chardev (callbacks, functions)
    • DMA (investigate vm-memory?)
  • other experiments
  • more QOM procedural macros (currently #[derive(Object)])
    • generate qdev properties?
    • generate parts of TypeInfo?
  • For hpet device
    • need error/irq/trace bindings.

Ideas for lints without breaking CI

See https://github.com/bonzini/rust-qemu/commit/95b25f7c5f4e2694a85a5503050cc98da7562c7c

  • 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 -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).

Possible project targets

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/hpet.c, hw/timer/i8254.c (claimed, WIP)
    • needs timer/memattrs/bitops/error/irq/trace bindgings
  • hw/mem/nvdimm.c (suggested by Manos)