diff --git a/Cargo.toml b/Cargo.toml index 0533948..7b6cca5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,7 @@ license = "Apache-2.0" [features] kvm-v4_14_0 = [] kvm-v4_20_0 = [] +fam-wrappers = ["vmm-sys-util"] [dependencies] +vmm-sys-util = { version = ">=0.2.0", optional = true } diff --git a/README.md b/README.md index 9c16dbe..04b1872 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,20 @@ toml: ```toml kvm-bindings = { version = "0.1", features = ["kvm_v4_20_0"]} ``` -In the `kvm-bindings` crate each feature maps to exactly one Linux version -as follows: +Bindings are generated for each specific Linux kernel version based on the enabled +crate features as follows: - `kvm_v4_14_0` contains the bindings for the Linux kernel version 4.14 - `kvm_v4_20_0` contains the bindings for the Linux kernel version 4.20 + +This crate also offers safe wrappers over FAM structs - FFI structs that have +a Flexible Array Member in their definition. +These safe wrappers can be used if the `fam-wrappers` feature is enabled for +this crate. Example: +```toml +kvm-bindings = { version = "0.1", features = ["kvm_v4_20_0", "fam-wrappers"]} +``` + +# Dependencies +The crate has an `optional` dependency to +[vmm-sys-util](https://crates.io/crates/vmm-sys-util) when enabling the +`fam-wrappers` feature. diff --git a/src/lib.rs b/src/lib.rs index ba147c6..cbb8e0a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,13 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] +#[macro_use] +#[cfg(all( + feature = "fam-wrappers", + any(target_arch = "x86", target_arch = "x86_64") +))] +extern crate vmm_sys_util; + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod x86; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] diff --git a/src/x86/fam_wrappers.rs b/src/x86/fam_wrappers.rs new file mode 100644 index 0000000..61d1518 --- /dev/null +++ b/src/x86/fam_wrappers.rs @@ -0,0 +1,71 @@ +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +use vmm_sys_util::fam::{FamStruct, FamStructWrapper}; + +#[cfg(feature = "kvm-v4_14_0")] +use super::bindings_v4_14_0::*; + +#[cfg(feature = "kvm-v4_20_0")] +use super::bindings_v4_20_0::*; + +#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))] +use super::bindings_v4_20_0::*; + +/// Maximum number of CPUID entries that can be returned by a call to KVM ioctls. +/// +/// See arch/x86/include/asm/kvm_host.h +pub const KVM_MAX_CPUID_ENTRIES: usize = 80; + +/// Maximum number of MSRs KVM supports (See arch/x86/kvm/x86.c). +pub const KVM_MAX_MSR_ENTRIES: usize = 256; + +// Implement the FamStruct trait for kvm_cpuid2. +generate_fam_struct_impl!( + kvm_cpuid2, + kvm_cpuid_entry2, + entries, + u32, + nent, + KVM_MAX_CPUID_ENTRIES +); + +/// Wrapper over the `kvm_cpuid2` structure. +/// +/// The `kvm_cpuid2` structure contains a flexible array member. For details check the +/// [KVM API](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt) +/// documentation on `kvm_cpuid2`. To provide safe access to +/// the array elements, this type is implemented using +/// [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html). +pub type CpuId = FamStructWrapper; + +// Implement the FamStruct trait for kvm_msrs. +generate_fam_struct_impl!( + kvm_msrs, + kvm_msr_entry, + entries, + u32, + nmsrs, + KVM_MAX_MSR_ENTRIES +); + +/// Wrapper over the `kvm_msrs` structure. +/// +/// The `kvm_msrs` structure contains a flexible array member. For details check the +/// [KVM API](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt) +/// documentation on `kvm_msrs`. To provide safe access to +/// the array elements, this type is implemented using +/// [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html). +pub type Msrs = FamStructWrapper; + +// Implement the FamStruct trait for kvm_msr_list. +generate_fam_struct_impl!(kvm_msr_list, u32, indices, u32, nmsrs, KVM_MAX_MSR_ENTRIES); + +/// Wrapper over the `kvm_msr_list` structure. +/// +/// The `kvm_msr_list` structure contains a flexible array member. For details check the +/// [KVM API](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt) +/// documentation on `kvm_msr_list`. To provide safe access to +/// the array elements, this type is implemented using +/// [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html). +pub type MsrList = FamStructWrapper; diff --git a/src/x86/mod.rs b/src/x86/mod.rs index e83c303..efe5390 100644 --- a/src/x86/mod.rs +++ b/src/x86/mod.rs @@ -1,6 +1,9 @@ // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +#[cfg(feature = "fam-wrappers")] +mod fam_wrappers; + #[cfg(feature = "kvm-v4_14_0")] mod bindings_v4_14_0; #[cfg(feature = "kvm-v4_20_0")] @@ -21,4 +24,7 @@ pub mod bindings { #[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))] pub use super::bindings_v4_20_0::*; + + #[cfg(feature = "fam-wrappers")] + pub use super::fam_wrappers::*; }