Commit Graph

30 Commits

Author SHA1 Message Date
Marc-André Lureau
c899071b5a rust: move binding generation to bindings/
Move raw FFI bindings generation to separate crates.  This makes it
possible to reuse bindgen declarations for a header file in its
dependencies (this was not the case before this change), while keeping
multiple -sys crates to avoid rebuilding all the code whenever
something changes.

Because the -sys crates are generated in dependency order, this also
enforces that the crates are organized in something that resembles
the dependencies between C headers.

The meson.build for rust-safe crates becomes simpler, and it should be
possible in the future to let Meson's cargo support handle most of it.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
[General cleanup and Python script. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2026-01-27 08:57:52 +01:00
Paolo Bonzini
9947bf9351 rust: migration: implement ToMigrationState for Timer
Timer is a complex struct, allow adding it to a struct that
uses #[derive(ToMigrationState)]; similar to vmstate_timer, only
the expiration time has to be preserved.

In fact, because it is thread-safe, ToMigrationStateShared can
also be implemented without needing a cell or mutex that wraps
the timer.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:10 +01:00
Zhao Liu
febdab3d5d rust/migration: Check name field in VMStateDescriptionBuilder
The name field is necessary for VMStateDescription, so that it's
necessary to check if it is set when build VMStateDescription.

Since is_null()/as_ref() become rustc v1.84 and pointer cannot cast to
integer in const, use Option<> to check name with a new field in
VMStateDescriptionBuilder instead.

This can be simplified in future when QEMU bumps up rustc version.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20251113051937.4017675-4-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:10 +01:00
Zhao Liu
bb6e3e743a rust/migration: Fix missing name in the VMSD of Migratable<>
The VMStateDescription of Migratable<T> missed the name field, and this
casused segmentation fault in vmstate_save_state_v() when it tries to
write name field by json_writer_str().

Due to the limitation of const, a custom name based on type would be
more difficult. Instead, a straightforward and simple approach is to
have all Migratable<T> instances use the same VMSD name -
"migratable-wrapper".

This is availiable because Migratable<T> is always a field within a
VMSD, and its parent VMSD should have a distinct name.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20251113051937.4017675-3-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:10 +01:00
Zhao Liu
4ace4aa133 rust/migration: Add Sync implementation for Migratable<>
It's common to define MemoryRegionOps<T> and VMStateDescription<T> as
static variables, and this requires T to implement Sync.

Migratable<T> is usually embedded in device state, so it's necessary to
implement Sync for Migratable<T>.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20251113051937.4017675-2-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:10 +01:00
Paolo Bonzini
2ff035b7c7 bql: add prelude
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:10 +01:00
Paolo Bonzini
e560f257b2 migration: add prelude
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:09 +01:00
Paolo Bonzini
34f66fdfd2 rust: hide panicking default associated constants from rustdoc
Work around rustdoc issue that panics while trying to evaluate
the constants.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:09 +01:00
Paolo Bonzini
e65030ed50 rust: remove unnecessary repetitive options
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-12-27 10:11:09 +01:00
Paolo Bonzini
ac561a3050 rust: migration: allow nested offset_of
Nested offset_of was stabilized in Rust 1.82.  Since the minimum
supported version for QEMU is 1.83, allow nested field accesses
in vmstate_of!

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-11-04 17:13:25 +01:00
Paolo Bonzini
d4fbf6ff8d rust: migration: allow passing ParentField<> to vmstate_of!
The common superclass for devices could have its own migration state;
for it to be included in the subclass's VMState, ParentField<> must
implement the VMState trait.

Reported-by: Chen Miao <chenmiao@openatom.club>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-29 10:23:56 +01:00
Martin Kletzander
80a3c9c01a rust: remove useless glib_sys bindings
The definition of types needed for g_autolist(), g_autoslist(),
g_autoqueue() need the imports for GList, GSList and GQueue
to appear everything.  Rust code is never going to see those,
since they are not used in structs.  Block the types from
appearing in the bindings.

Co-authored-by: Martin Kletzander <mkletzan@redhat.com>
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-28 13:02:21 +01:00
Paolo Bonzini
7ee5875d42 rust: migration: implement ToMigrationState as part of impl_vmstate_bitsized
This is most likely desirable, and is the easiest way to migrate
a bit-sized value without peeking at the innards of the bilge crate.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 14:43:54 +02:00
Paolo Bonzini
29cf500e3b rust: qemu-macros: add ToMigrationState derive macro
Add a macro that recursively builds the "migrated" version
of a struct.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 11:04:07 +02:00
Paolo Bonzini
44a9d1b86c rust: migration: add high-level migration wrappers
Instead of dealing with pre/post callbacks, allow devices to
implement a snapshot/restore mechanism; this has two main
advantages:

- it can be easily implemented via procedural macros

- there can be generic implementations to deal with various
  kinds of interior-mutable containers, from BqlRefCell to Mutex,
  so that C code does not see Rust concepts such as Mutex<>.

Using it is easy; you can implement the snapshot/restore trait
ToMigrationState and declare your state like:

     regs: Migratable<Mutex<MyDeviceRegisters>>

Migratable<> allows dereferencing to the underlying object with
no run-time cost.

Note that Migratable<> actually does not accept ToMigrationState,
only the similar ToMigrationStateShared trait that the user will mostly
not care about.  This is required by the fact that pre/post callbacks
take a &self, and ensures that the argument is a Mutex or BqlRefCell
(including an array or Arc<> thereof).

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 11:04:02 +02:00
Paolo Bonzini
4526418aff rust: move VMState from bql to migration
The high-level wrapper Migratable<T> will contain a BqlCell,
which would introduce a circular dependency betwen the bql and
migration crates.  Move the implementation of VMState for cells
to "migration", together with the implementation for std types.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 11:04:00 +02:00
Paolo Bonzini
8999ca00a4 rust: migration: extract vmstate_fields_ref
This is useful when building a VMState for generic structs, because you have
to avoid nested statics.  Using vmstate_fields! will fail in the likely case
where the _FIELDS static uses Self from an outer item, because that is
forbidden.

The separate macros are needed because you cannot just do

                 .fields(vmstate_fields_ref! {
                      vmstate_of!(PL011State, clock),
                 })

The value returned by vmstate_fields_ref! is not promoted to static, which is
unfortunate but intentional (https://github.com/rust-lang/rust/issues/60502):

error[E0716]: temporary value dropped while borrowed
   --> rust/hw/char/pl011/libpl011.rlib.p/structured/device.rs:743:17
    |
738 | /      VMStateDescriptionBuilder::<PL011State>::new()
739 | |          .name(c"pl011/clock")
740 | |          .version_id(1)
741 | |          .minimum_version_id(1)
742 | |          .needed(&PL011State::clock_needed)
743 | |          .fields(vmstate_fields_ref! {
    | | _________________^
744 | ||              vmstate_of!(PL011State, clock),
745 | ||         })
    | ||_________^- argument requires that borrow lasts for `'static`
    |  |_________|
    |            creates a temporary value which is freed while still in use
746 |            .build();
    |                   - temporary value is freed at the end of this statement

Thus it is necessary to use the "static", whether explicitly or hidden by
vmstate_fields.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 11:03:59 +02:00
Paolo Bonzini
5b4fa97807 rust: migration: validate termination of subsection arrays
For consistency with fields(), validate the value (at least to some extent)
before passing it to C.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 11:03:59 +02:00
Paolo Bonzini
64bce66d6f rust: migration: do not store raw pointers into VMStateSubsectionsWrapper
Raw pointers were used to insert a NULL one at the end of the array.
However, Option<&...> has the same layout and does not remove Sync
from the type of the array.

As an extra benefit, this enables validation of the terminator of the
subsection array, because is_null() in const context would not be stable
until Rust 1.84.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 11:03:59 +02:00
Paolo Bonzini
5c776a7677 rust: migration: do not pass raw pointer to VMStateDescription::fields
Pass a slice instead; a function that accepts a raw pointer should
arguably be declared as unsafe.

But since it is now much easier to forget vmstate_fields!, validate the
value (at least to some extent) before passing it to C.  (Unfortunately,
doing the same for subsections would require const ptr::is_null(), which
is only stable in Rust 1.84).

Suggested-by: Zhao Liu <zhao1.liu@intel.com>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 11:03:59 +02:00
Paolo Bonzini
ceda1563d6 rust: migration: hide more warnings from call_func_with_field!
The call_func_with_field! macro uses dead code willingly to infer
the appropriate type.  This has started adding a new warning:

error: unused variable: `value__`
 79 |             break phantom__(&{ let value__: $typ; value__.$($field).+ })

So shut it up together with the existing unreachable_code warning.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-14 01:44:43 +02:00
Marc-André Lureau
9c40c1ff97 rust: use glib-sys
Don't generate FFI for glib, rely on glib-sys crate.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-10-03 15:14:48 +02:00
Marc-André Lureau
582a39beea build-sys: pass -fvisibility=default for wasm bindgen
Otherwise, no functions are generated:
https://github.com/rust-lang/rust-bindgen/issues/2989

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
Message-ID: <20250924120426.2158655-27-marcandre.lureau@redhat.com>
2025-09-30 19:33:25 +04:00
Paolo Bonzini
f87a2e5c59 rust: build: remove "protocol: rust: from doctests
It is added already by rust.doctest.

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-09-22 17:17:18 +02:00
Paolo Bonzini
75dbe618ac rust: vmstate: use "cast()" instead of "as"
Reported by clippy, fix it.

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-09-22 17:17:18 +02:00
Marc-André Lureau
e4444d71e8 rust: re-export qemu macros from common/qom/hwcore
This is just a bit nicer.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20250827104147.717203-22-marcandre.lureau@redhat.com
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-09-17 19:00:58 +02:00
Marc-André Lureau
0d93f81773 rust: rename qemu_api_macros -> qemu_macros
Since "qemu_api" is no longer the unique crate to provide APIs.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20250827104147.717203-17-marcandre.lureau@redhat.com
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-09-17 19:00:57 +02:00
Marc-André Lureau
fcf4c00b4d rust: split "qom" crate
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20250827104147.717203-13-marcandre.lureau@redhat.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-09-17 19:00:57 +02:00
Marc-André Lureau
f6b4f0dd9c rust: split "bql" crate
Unfortunately, an example had to be compile-time disabled, since it
relies on higher level crates (qdev, irq etc). The alternative is
probably to move that code to an example in qemu-api or elsewere and
make a link to it, or include_str.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20250827104147.717203-12-marcandre.lureau@redhat.com
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-09-17 19:00:57 +02:00
Marc-André Lureau
4dff343d23 rust: split "migration" crate
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20250827104147.717203-11-marcandre.lureau@redhat.com
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-09-17 19:00:57 +02:00