Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KVM MSR access and search #13

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ serde_yaml = "0.8"
enum_dispatch = "0.3.8"

[target.'cfg(target_os = "linux")'.dependencies]
kvm-ioctls = { version = "0.12.0", optional = true }
kvm-bindings = { version = "0.6.0", features = ["fam-wrappers"], optional = true }
kvm-ioctls = { version = "0.17", optional = true }
kvm-bindings = { version = "0.8", features = ["fam-wrappers"], optional = true }

[features]
default = ["use_msr", "kvm"]
Expand Down
52 changes: 51 additions & 1 deletion src/kvm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::msr::{self, MSRValue, MsrStore};

use super::CpuidDB;
use core::arch::x86_64::CpuidResult;
use kvm_bindings::{KVM_CPUID_FLAG_SIGNIFCANT_INDEX, KVM_MAX_CPUID_ENTRIES};
use kvm_bindings::{kvm_msr_entry, Msrs, KVM_CPUID_FLAG_SIGNIFCANT_INDEX, KVM_MAX_CPUID_ENTRIES};
use std::error::Error;

/** Wrap information from kvm
*
Expand Down Expand Up @@ -39,3 +42,50 @@ impl CpuidDB for KvmInfo {
})
}
}

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 {})
}
}
112 changes: 76 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

use cpuinfo::facts::{FactSet, Facter, GenericFact};
use cpuinfo::layout::LeafDesc;
use cpuinfo::msr::MSRValue;
use cpuinfo::msr::MsrStore;
use cpuinfo::*;
use enum_dispatch::enum_dispatch;
use msr::MSRDesc;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::error::Error;
use std::fmt;
use structopt::StructOpt;
Expand Down Expand Up @@ -41,6 +40,7 @@ struct Disp {
#[cfg(all(target_os = "linux", feature = "kvm"))]
#[structopt(long)]
skip_kvm: bool,
#[cfg(feature = "use_msr")]
#[structopt(long)]
skip_msr: bool,
}
Expand Down Expand Up @@ -77,12 +77,40 @@ impl Command for Disp {
}
}

if MSRDesc::is_available() && !self.skip_msr {
println!("MSRS:");
for msr in &config.msrs {
match msr.into_value() {
Ok(value) => println!("{}", value),
Err(err) => println!("{} Error : {}", msr, err),
#[cfg(feature = "use_msr")]
if !self.skip_msr {
#[cfg(target_os = "linux")]
{
match msr::linux::LinuxMsrStore::new() {
Ok(linux_store) => {
println!("MSRS:");
for msr in &config.msrs {
match linux_store.get_value(msr) {
Ok(value) => println!("{}", value),
Err(err) => println!("{} Error : {}", msr, err),
}
}
}
Err(e) => println!("Error checking all msrs: {}", e),
}
}
#[cfg(all(target_os = "linux", feature = "kvm"))]
if !self.skip_kvm {
use cpuinfo::kvm::KvmMsrInfo;
use kvm_ioctls::Kvm;
println!("KVM-MSR:");
if let Err(e) = {
let kvm = Kvm::new()?;
let kvm_msr = KvmMsrInfo::new(&kvm)?;
for msr in &config.msrs {
match kvm_msr.get_value(msr) {
Ok(value) => println!("{}", value),
Err(err) => println!("{} Error : {}", msr, err),
}
}
Ok::<_, Box<dyn Error>>(())
} {
println!("Error Processing KVM-MSR: {}", e);
}
}
}
Expand All @@ -101,6 +129,7 @@ struct Facts {
fn collect_facts(
config: &Definition,
cpuid_selected: CpuidType,
msr_store: Box<dyn MsrStore>,
) -> Result<Vec<YAMLFact>, Box<dyn std::error::Error>> {
let mut ret: Vec<YAMLFact> = config
.cpuids
Expand All @@ -113,15 +142,9 @@ fn collect_facts(
})
.collect();

let use_kvm = match cpuid_selected {
CpuidType::Func(_) => false,
#[cfg(all(target_os = "linux", feature = "kvm"))]
CpuidType::KvmInfo(_) => true,
};

if MSRDesc::is_available() && !use_kvm {
if !msr_store.is_empty() {
for msr in &config.msrs {
if let Ok(value) = MSRValue::try_from(msr) {
if let Ok(value) = msr_store.get_value(msr) {
let mut facts = value.collect_facts();
for fact in &mut facts {
fact.add_path("msr");
Expand All @@ -136,36 +159,53 @@ fn collect_facts(

impl Command for Facts {
fn run(&self, config: &Definition) -> Result<(), Box<dyn std::error::Error>> {
#[cfg(all(target_os = "linux", feature = "kvm"))]
let kvm_option = {
use cpuinfo::kvm::KvmInfo;
use kvm_ioctls::Kvm;
if self.use_kvm {
println!("using kvm");
let kvm = Kvm::new()?;
Some(KvmInfo::new(&kvm)?)
} else {
None
}
};

let cpuid_source = {
let (cpuid_source, msr_source): (_, Box<dyn MsrStore>) = {
#[cfg(all(target_os = "linux", feature = "kvm"))]
{
if let Some(kvm_info) = kvm_option {
kvm_info.into()
if self.use_kvm {
use cpuinfo::kvm::KvmInfo;
use kvm::KvmMsrInfo;
use kvm_ioctls::Kvm;
let kvm = Kvm::new()?;
(
KvmInfo::new(&kvm)?.into(),
Box::new(KvmMsrInfo::new(&kvm)?) as Box<dyn MsrStore>,
)
} else {
CpuidType::func()
let msr = {
#[cfg(feature = "use_msr")]
{
Box::new(msr::linux::LinuxMsrStore::new()?) as Box<dyn MsrStore>
}
#[cfg(not(feature = "use_msr"))]
{
Box::new(msr::EmptyMSR {})
}
};
(CpuidType::func(), msr)
}
}
#[cfg(any(not(target_os = "linux"), not(feature = "kvm")))]
#[cfg(all(target_os = "linux", not(feature = "kvm"), feature = "use_msr"))]
{
(
CpuidType::func(),
Box::new(msr::linux::LinuxMsrStore::new()?) as Box<dyn MsrStore>,
)
}
#[cfg(any(
not(target_os = "linux"),
all(not(feature = "kvm"), not(feature = "use_msr"))
))]
{
CpuidType::func()
(
CpuidType::func(),
Box::new(msr::EmptyMSR {}) as Box<dyn MsrStore>,
)
}
};
println!(
"{}",
serde_yaml::to_string(&collect_facts(config, cpuid_source)?)?
serde_yaml::to_string(&collect_facts(config, cpuid_source, msr_source)?)?
);
Ok(())
}
Expand Down
Loading
Loading