Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Add serde support for various bindings
Browse files Browse the repository at this point in the history
Add implementations of `serde::{Serialize, Deserialize}` for
kvm-bindings structures commonly used for live migration/snapshotting.
The selections of structures to add the derives to was determined by
finding the minimal set that allows both Firecracker and CloudHypervisor
to compile against this branch.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
  • Loading branch information
roypat committed Mar 6, 2024
1 parent 33a89cc commit 1c1236c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
fam-wrappers = ["vmm-sys-util"]
# If the 'fam-wrappers' feature is not enabled, the below 'vmm-sys-util/with-serde' flag will be ignored (e.g. it will not enable the vmm-sys-util dependency). Only if both 'fam-wrappers' and 'with-serde' are enaled is the 'with-serde' feature of vmm-sys-util enabled.
with-serde = ["dep:serde", "serde/derive", "dep:serde-big-array", "vmm-sys-util/with-serde"]


[dependencies]
vmm-sys-util = { version = "0.12.1", optional = true }
serde = { version = "1.0.0", optional = true, features = ["derive"] }
serde-big-array = { version = "0.5.1", optional = true}
6 changes: 6 additions & 0 deletions src/arm64/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@ pub type __wsum = __u32;
pub type __poll_t = ::std::os::raw::c_uint;
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct user_pt_regs {
pub regs: [__u64; 31usize],
pub sp: __u64,
Expand Down Expand Up @@ -1019,6 +1020,7 @@ fn bindgen_test_layout_user_pt_regs() {
#[repr(C)]
#[repr(align(16))]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct user_fpsimd_state {
pub vregs: [__uint128_t; 32usize],
pub fpsr: __u32,
Expand Down Expand Up @@ -1499,6 +1501,7 @@ fn bindgen_test_layout_user_za_header() {
#[repr(C)]
#[repr(align(16))]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_regs {
pub regs: user_pt_regs,
pub sp_el1: __u64,
Expand Down Expand Up @@ -1574,6 +1577,7 @@ fn bindgen_test_layout_kvm_regs() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_vcpu_init {
pub target: __u32,
pub features: [__u32; 7usize],
Expand Down Expand Up @@ -6511,6 +6515,7 @@ fn bindgen_test_layout_kvm_vapic_addr() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_mp_state {
pub mp_state: __u32,
}
Expand Down Expand Up @@ -8786,6 +8791,7 @@ fn bindgen_test_layout_kvm_reg_list() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_one_reg {
pub id: __u64,
pub addr: __u64,
Expand Down
50 changes: 50 additions & 0 deletions src/x86_64/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ where
}
#[repr(C)]
#[derive(Default)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
impl<T> __IncompleteArrayField<T> {
#[inline]
Expand Down Expand Up @@ -1426,6 +1427,7 @@ impl ::std::fmt::Debug for kvm_ioapic_state {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_regs {
pub rax: __u64,
pub rbx: __u64,
Expand Down Expand Up @@ -1643,7 +1645,9 @@ fn bindgen_test_layout_kvm_regs() {
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_lapic_state {
#[cfg_attr(feature = "with-serde", serde(with = "serde_big_array::BigArray"))]
pub regs: [::std::os::raw::c_char; 1024usize],
}
#[test]
Expand Down Expand Up @@ -1682,6 +1686,7 @@ impl Default for kvm_lapic_state {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_segment {
pub base: __u64,
pub limit: __u32,
Expand Down Expand Up @@ -1844,6 +1849,7 @@ fn bindgen_test_layout_kvm_segment() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_dtable {
pub base: __u64,
pub limit: __u16,
Expand Down Expand Up @@ -1896,6 +1902,7 @@ fn bindgen_test_layout_kvm_dtable() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_sregs {
pub cs: kvm_segment,
pub ds: kvm_segment,
Expand Down Expand Up @@ -2481,6 +2488,7 @@ fn bindgen_test_layout_kvm_fpu() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_msr_entry {
pub index: __u32,
pub reserved: __u32,
Expand Down Expand Up @@ -2533,6 +2541,7 @@ fn bindgen_test_layout_kvm_msr_entry() {
}
#[repr(C)]
#[derive(Debug, Default)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_msrs {
pub nmsrs: __u32,
pub pad: __u32,
Expand Down Expand Up @@ -2885,6 +2894,7 @@ fn bindgen_test_layout_kvm_cpuid() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_cpuid_entry2 {
pub function: __u32,
pub index: __u32,
Expand Down Expand Up @@ -2992,6 +3002,7 @@ fn bindgen_test_layout_kvm_cpuid_entry2() {
}
#[repr(C)]
#[derive(Debug, Default)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_cpuid2 {
pub nent: __u32,
pub padding: __u32,
Expand Down Expand Up @@ -3044,6 +3055,7 @@ fn bindgen_test_layout_kvm_cpuid2() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_pit_channel_state {
pub count: __u32,
pub latched_count: __u16,
Expand Down Expand Up @@ -3341,6 +3353,7 @@ fn bindgen_test_layout_kvm_pit_state() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_pit_state2 {
pub channels: [kvm_pit_channel_state; 3usize],
pub flags: __u32,
Expand Down Expand Up @@ -3434,6 +3447,7 @@ fn bindgen_test_layout_kvm_reinject_control() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_vcpu_events {
pub exception: kvm_vcpu_events__bindgen_ty_1,
pub interrupt: kvm_vcpu_events__bindgen_ty_2,
Expand All @@ -3448,6 +3462,7 @@ pub struct kvm_vcpu_events {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_1 {
pub injected: __u8,
pub nr: __u8,
Expand Down Expand Up @@ -3523,6 +3538,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_1() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_2 {
pub injected: __u8,
pub nr: __u8,
Expand Down Expand Up @@ -3587,6 +3603,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_2() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_3 {
pub injected: __u8,
pub pending: __u8,
Expand Down Expand Up @@ -3651,6 +3668,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_3() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_4 {
pub smm: __u8,
pub pending: __u8,
Expand Down Expand Up @@ -3715,6 +3733,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_4() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_5 {
pub pending: __u8,
}
Expand Down Expand Up @@ -3861,6 +3880,7 @@ fn bindgen_test_layout_kvm_vcpu_events() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_debugregs {
pub db: [__u64; 4usize],
pub dr6: __u64,
Expand Down Expand Up @@ -3935,7 +3955,9 @@ fn bindgen_test_layout_kvm_debugregs() {
}
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_xsave {
#[cfg_attr(feature = "with-serde", serde(with = "serde_big_array::BigArray"))]
pub region: [__u32; 1024usize],
pub extra: __IncompleteArrayField<__u32>,
}
Expand Down Expand Up @@ -3985,6 +4007,7 @@ impl Default for kvm_xsave {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_xcr {
pub xcr: __u32,
pub reserved: __u32,
Expand Down Expand Up @@ -4037,6 +4060,7 @@ fn bindgen_test_layout_kvm_xcr() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_xcrs {
pub nr_xcrs: __u32,
pub flags: __u32,
Expand Down Expand Up @@ -5032,6 +5056,7 @@ impl ::std::fmt::Debug for kvm_irq_level {
}
#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_irqchip {
pub chip_id: __u32,
pub pad: __u32,
Expand All @@ -5044,6 +5069,29 @@ pub union kvm_irqchip__bindgen_ty_1 {
pub pic: kvm_pic_state,
pub ioapic: kvm_ioapic_state,
}

#[cfg(feature = "with-serde")]
impl serde::Serialize for kvm_irqchip__bindgen_ty_1 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
// SAFETY: Safe because this is a POD struct and we're only using the read result to
// serialize the underlying bytes.
unsafe { self.dummy.serialize(serializer) }
}
}
#[cfg(feature = "with-serde")]
impl<'de> serde::Deserialize<'de> for kvm_irqchip__bindgen_ty_1 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let dummy = serde_big_array::BigArray::deserialize(deserializer)?;
assert_eq!(std::mem::size_of_val(&dummy), std::mem::size_of::<Self>());
Ok(Self { dummy })
}
}
#[test]
fn bindgen_test_layout_kvm_irqchip__bindgen_ty_1() {
const UNINIT: ::std::mem::MaybeUninit<kvm_irqchip__bindgen_ty_1> =
Expand Down Expand Up @@ -9167,6 +9215,7 @@ fn bindgen_test_layout_kvm_vapic_addr() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_mp_state {
pub mp_state: __u32,
}
Expand Down Expand Up @@ -11404,6 +11453,7 @@ fn bindgen_test_layout_kvm_irqfd() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
pub struct kvm_clock_data {
pub clock: __u64,
pub flags: __u32,
Expand Down

0 comments on commit 1c1236c

Please sign in to comment.