-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkvm.rs
More file actions
91 lines (84 loc) · 2.56 KB
/
kvm.rs
File metadata and controls
91 lines (84 loc) · 2.56 KB
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
use crate::msr::{self, MSRValue, MsrStore};
use super::CpuidDB;
use core::arch::x86_64::CpuidResult;
use kvm_bindings::{kvm_msr_entry, Msrs, KVM_CPUID_FLAG_SIGNIFCANT_INDEX, KVM_MAX_CPUID_ENTRIES};
use std::error::Error;
/** Wrap information from kvm
*
* Other structures such as CPUInfo will then make it accessible like the cpuid function
*/
pub struct KvmInfo {
cpuid_info: kvm_bindings::CpuId,
}
impl KvmInfo {
pub fn new(kvm: &kvm_ioctls::Kvm) -> Result<Self, kvm_ioctls::Error> {
let cpuid_info = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES)?;
Ok(Self { cpuid_info })
}
}
impl CpuidDB for KvmInfo {
fn get_cpuid(&self, leaf: u32, subleaf: u32) -> Option<CpuidResult> {
self.cpuid_info.as_slice().iter().find_map(|entry| {
if entry.function == leaf {
if (subleaf == 0 && (entry.flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) == 0)
|| (subleaf == entry.index)
{
Some(CpuidResult {
eax: entry.eax,
ebx: entry.ebx,
ecx: entry.ecx,
edx: entry.edx,
})
} else {
None
}
} else {
None
}
})
}
}
pub struct KvmMsrInfo {
msr_info: kvm_bindings::Msrs,
}
impl KvmMsrInfo {
pub fn new(kvm: &kvm_ioctls::Kvm) -> Result<Self, Box<dyn Error>> {
let msr_features = kvm.get_msr_feature_index_list()?;
let mut msrs = Msrs::from_entries(
&msr_features
.as_slice()
.iter()
.map(|&index| kvm_msr_entry {
index,
..Default::default()
})
.collect::<Vec<_>>(),
)?;
kvm.get_msrs(&mut msrs)?;
Ok(KvmMsrInfo { msr_info: msrs })
}
}
impl MsrStore for KvmMsrInfo {
fn is_empty(&self) -> bool {
false
}
fn get_value<'a>(
&self,
desc: &'a crate::msr::MSRDesc,
) -> std::result::Result<crate::msr::MSRValue<'a>, crate::msr::Error> {
self.msr_info
.as_slice()
.iter()
.find_map(|entry| {
if entry.index == desc.address {
Some(MSRValue {
desc,
value: entry.data,
})
} else {
None
}
})
.ok_or_else(|| msr::Error::NotAvailible("/dev/kvm".to_string()))
}
}