Skip to content

RFC rust: (mostly) type safe VMState

Paolo Bonzini qemu-devel qemu-rust 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest9 patches 14 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9rust: vmstate: add new type safe implementationrust/qemu-api/src/prelude.rs rust/qemu-api/src/vmstate.rs+106 -63 replies
2/9rust: vmstate: implement VMState for non-leaf typesrust/qemu-api/src/vmstate.rs+65 -11 reply
3/9rust: vmstate: add varray support to vmstate_of!rust/qemu-api/src/vmstate.rs+42 -32 replies
4/9rust: vmstate: implement Zeroable for VMStateFieldrust/qemu-api/src/vmstate.rs rust/qemu-api/src/zeroable.rs+34 -151 reply
5/9rust: vmstate: implement VMState for scalar typesrust/qemu-api/src/vmstate.rs+123 -23 replies
6/9rust: vmstate: add public utility macros to implement VMStaterust/qemu-api/src/vmstate.rs+58 -31 reply
7/9rust: qemu_api: add vmstate_struct and vmstate_cellrust/qemu-api/src/vmstate.rs+34 -01 reply
8/9rust: pl011: switch vmstate to new-style macrosrust/hw/char/pl011/src/device.rs rust/hw/char/pl011/src/device_class.rs rust/hw/char/pl011/src/lib.rs+26 -191 reply
9/9rust: vmstate: remove translation of C vmstate macrosrust/qemu-api/src/vmstate.rs+23 -2511 reply

Reviews trailers per patch

7 of 9 patches have a Reviewed-by or Acked-by.

PatchZhao Liu
1/9R-b
2/9R-b
3/9
4/9R-b
5/9R-b
6/9R-b
7/9
8/9R-b
9/9R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [RFC PATCH 0/9] rust: (mostly) type safe VMState

The existing translation of the C macros for vmstate does not make any attempt to type-check vmstate declarations against the struct, so introduce a new system that computes VMStateField based on the actual struct declaration.

Macros do not have full access to the type system, therefore a full implementation of this scheme requires a helper trait to analyze the type and produce a VMStateField from it; the trait stores the VMStateField as ana associated const. Then, a macro "vmstate_of!" accepts arguments similar to "offset_of!" and tricks the compiler into looking up the trait for the right type.

Structs and cells have their own sub-VMStateDescription. This poses a problem in that references can only be included in associated consts starting with Rust 1.83.0, when the const_refs_static was stabilized. So for now structs and cells cannot be done in a type-safe manner, but arrays, pointers and scalars can.

Technically, scalar types have the same issue in that they point to a VMStateInfo. For this case however there is onlyya limited list of VMStateInfos, so I am placing them in an enum, and going from enum to &VMStateInfo only when building the VMStateField (i.e. in a static's value rather than in an associated const, and in a macro rather than in a const fn). This isn't great, but it's easily removed when QEMU starts accepting a newer Rust version.

The series also introduces the pattern of applying changes to a struct using methods with a fn(mut self) -> Self signature, for example

    pub const fn with_version_id(mut self, version_id: i32) -> Self {
        self.version_id = version_id;
        self
    }

This is not quite the builder pattern because there is no need for a final ".build()" call, but it's similar/related.

The unfavorable diffstat comes half from the const_refs_static workaround, half from new documentation (so in that case it's a plus rather than a minus). And also, there is more functionality and extensibility in the new mechanism, whereas only a subset of the C macros was included in the previous implementation. The exception is "field_exists", which may be added later if needed as a modifier, similar to with_version_id().

Paolo

Paolo Bonzini (9):
rust: vmstate: add new type safe implementation
rust: vmstate: implement VMState for non-leaf types
rust: vmstate: add varray support to vmstate_of!
rust: vmstate: implement Zeroable for VMStateField
rust: vmstate: implement VMState for scalar types
rust: vmstate: add public utility macros to implement VMState
rust: qemu_api: add vmstate_struct and vmstate_cell
rust: pl011: switch vmstate to new-style macros
rust: vmstate: remove translation of C vmstate macros

 rust/hw/char/pl011/src/device.rs       |   3 +-
 rust/hw/char/pl011/src/device_class.rs |  36 +-
 rust/hw/char/pl011/src/lib.rs          |   6 +
 rust/qemu-api/src/prelude.rs           |   2 +
 rust/qemu-api/src/vmstate.rs           | 673 ++++++++++++++++---------
 rust/qemu-api/src/zeroable.rs          |  31 ++
 6 files changed, 481 insertions(+), 270 deletions(-)
Signature · 1 line
2.47.1