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

Commit bbb2b52

Browse files
acatangiuSamuel Ortiz
authored andcommitted
implement CpuId as a FamStructWrapper over kvm_cpuid2
Since kvm_cpuid2 contains a flexible array member, we can implement CpuId as a FamStructWrapper<kvm_cpuid2>. This allows users of kvm-ioctls to work with safe code even when dealing with kvm_cpuid2. Signed-off-by: Adrian Catangiu <[email protected]>
1 parent dc0bd95 commit bbb2b52

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ kvm-v4_14_0 = []
1313
kvm-v4_20_0 = []
1414

1515
[dependencies]
16+
vmm-sys-util = ">=0.2.0"

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#![allow(non_camel_case_types)]
66
#![allow(non_snake_case)]
77

8+
#[macro_use]
9+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
10+
extern crate vmm_sys_util;
11+
812
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
913
mod x86;
1014
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]

src/x86/fam_wrappers.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use vmm_sys_util::fam::{FamStruct, FamStructWrapper};
5+
6+
#[cfg(feature = "kvm-v4_14_0")]
7+
use super::bindings_v4_14_0::*;
8+
9+
#[cfg(feature = "kvm-v4_20_0")]
10+
use super::bindings_v4_20_0::*;
11+
12+
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
13+
use super::bindings_v4_20_0::*;
14+
15+
/// Maximum number of CPUID entries that can be returned by a call to KVM ioctls.
16+
///
17+
/// See arch/x86/include/asm/kvm_host.h
18+
pub const KVM_MAX_CPUID_ENTRIES: usize = 80;
19+
20+
// Implement the FamStruct trait for kvm_cpuid2.
21+
generate_fam_struct_impl!(
22+
kvm_cpuid2,
23+
kvm_cpuid_entry2,
24+
entries,
25+
u32,
26+
nent,
27+
KVM_MAX_CPUID_ENTRIES
28+
);
29+
30+
/// Wrapper over the `kvm_cpuid2` structure.
31+
///
32+
/// The `kvm_cpuid2` structure contains a flexible array member. For details check the
33+
/// [KVM API](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt)
34+
/// documentation on `kvm_cpuid2`. To provide safe access to
35+
/// the array elements, this type is implemented using
36+
/// [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html).
37+
pub type CpuId = FamStructWrapper<kvm_cpuid2>;

src/x86/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
mod fam_wrappers;
5+
46
#[cfg(feature = "kvm-v4_14_0")]
57
mod bindings_v4_14_0;
68
#[cfg(feature = "kvm-v4_20_0")]
@@ -21,4 +23,6 @@ pub mod bindings {
2123

2224
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
2325
pub use super::bindings_v4_20_0::*;
26+
27+
pub use super::fam_wrappers::*;
2428
}

0 commit comments

Comments
 (0)