1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::brand_string::{BrandString, Reg as BsReg};
use super::common::get_vendor_id;
use super::{CpuId, CpuIdEntry};
use crate::VpmuFeatureLevel;
pub mod amd;
pub mod common;
pub mod intel;
pub struct VmSpec {
cpu_vendor_id: [u8; 12],
cpu_id: u8,
cpu_count: u8,
brand_string: BrandString,
threads_per_core: u8,
cores_per_die: u8,
dies_per_socket: u8,
vpmu_feature: VpmuFeatureLevel,
}
impl VmSpec {
pub fn new(
cpu_id: u8,
cpu_count: u8,
threads_per_core: u8,
cores_per_die: u8,
dies_per_socket: u8,
vpmu_feature: VpmuFeatureLevel,
) -> Result<VmSpec, Error> {
let cpu_vendor_id = get_vendor_id().map_err(Error::InternalError)?;
let brand_string =
BrandString::from_vendor_id(&cpu_vendor_id).map_err(Error::BrandString)?;
Ok(VmSpec {
cpu_vendor_id,
cpu_id,
cpu_count,
brand_string,
threads_per_core,
cores_per_die,
dies_per_socket,
vpmu_feature,
})
}
pub fn cpu_vendor_id(&self) -> &[u8; 12] {
&self.cpu_vendor_id
}
}
#[derive(Debug, Clone)]
pub enum Error {
BrandString(super::brand_string::Error),
CpuNotSupported,
FamError(vmm_sys_util::fam::Error),
InternalError(super::common::Error),
VcpuCountOverflow,
}
pub type EntryTransformerFn = fn(entry: &mut CpuIdEntry, vm_spec: &VmSpec) -> Result<(), Error>;
pub trait CpuidTransformer {
fn process_cpuid(&self, cpuid: &mut CpuId, vm_spec: &VmSpec) -> Result<(), Error> {
self.process_entries(cpuid, vm_spec)
}
fn process_entries(&self, cpuid: &mut CpuId, vm_spec: &VmSpec) -> Result<(), Error> {
for entry in cpuid.as_mut_slice().iter_mut() {
let maybe_transformer_fn = self.entry_transformer_fn(entry);
if let Some(transformer_fn) = maybe_transformer_fn {
transformer_fn(entry, vm_spec)?;
}
}
Ok(())
}
fn entry_transformer_fn(&self, _entry: &mut CpuIdEntry) -> Option<EntryTransformerFn> {
None
}
}
#[cfg(test)]
mod test {
use super::*;
use kvm_bindings::kvm_cpuid_entry2;
const PROCESSED_FN: u32 = 1;
const EXPECTED_INDEX: u32 = 100;
fn transform_entry(entry: &mut kvm_cpuid_entry2, _vm_spec: &VmSpec) -> Result<(), Error> {
entry.index = EXPECTED_INDEX;
Ok(())
}
struct MockCpuidTransformer {}
impl CpuidTransformer for MockCpuidTransformer {
fn entry_transformer_fn(&self, entry: &mut kvm_cpuid_entry2) -> Option<EntryTransformerFn> {
match entry.function {
PROCESSED_FN => Some(transform_entry),
_ => None,
}
}
}
#[test]
fn test_process_cpuid() {
let num_entries = 5;
let mut cpuid = CpuId::new(num_entries).unwrap();
let vm_spec = VmSpec::new(0, 1, 1, 1, 1, VpmuFeatureLevel::Disabled);
cpuid.as_mut_slice()[0].function = PROCESSED_FN;
assert!(MockCpuidTransformer {}
.process_cpuid(&mut cpuid, &vm_spec.unwrap())
.is_ok());
assert!(cpuid.as_mut_slice().len() == num_entries);
for entry in cpuid.as_mut_slice().iter() {
match entry.function {
PROCESSED_FN => {
assert_eq!(entry.index, EXPECTED_INDEX);
}
_ => {
assert_ne!(entry.index, EXPECTED_INDEX);
}
}
}
}
#[test]
fn test_invalid_cpu_architecture_cpuid() {
use crate::cpuid::process_cpuid;
let num_entries = 5;
let mut cpuid = CpuId::new(num_entries).unwrap();
let mut vm_spec = VmSpec::new(0, 1, 1, 1, 1, VpmuFeatureLevel::Disabled).unwrap();
vm_spec.cpu_vendor_id = [1; 12];
assert!(process_cpuid(&mut cpuid, &vm_spec).is_err());
}
}