Skip to content

Commit

Permalink
MSR: Fixed several issues related to inclusion
Browse files Browse the repository at this point in the history
Some platforms and feature lists can end up with partial inclusions.
  • Loading branch information
andrewjj20 committed May 21, 2024
1 parent e3db94d commit 35450f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
15 changes: 8 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,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 @@ -78,9 +79,9 @@ impl Command for Disp {

#[cfg(feature = "use_msr")]
if !self.skip_msr {
#[cfg(all(target_os = "linux"))]
#[cfg(target_os = "linux")]
{
match msr::LinuxMsrStore::new() {
match msr::linux::LinuxMsrStore::new() {
Ok(linux_store) => {
println!("MSRS:");
for msr in &config.msrs {
Expand Down Expand Up @@ -174,7 +175,7 @@ impl Command for Facts {
let msr = {
#[cfg(feature = "use_msr")]
{
Box::new(msr::LinuxMsrStore::new()?) as Box<dyn MsrStore>
Box::new(msr::linux::LinuxMsrStore::new()?) as Box<dyn MsrStore>
}
#[cfg(not(feature = "use_msr"))]
{
Expand All @@ -188,12 +189,12 @@ impl Command for Facts {
{
(
CpuidType::func(),
Box::new(msr::LinuxMsrStore::new()?) as Box<dyn MsrStore>,
Box::new(msr::linux::LinuxMsrStore::new()?) as Box<dyn MsrStore>,
)
}
#[cfg(all(
not(feature = "kvm"),
any(not(target_os = "linux"), not(feature = "use_msr"))
#[cfg(any(
not(target_os = "linux"),
all(not(feature = "kvm"), not(feature = "use_msr"))
))]
{
(
Expand Down
65 changes: 34 additions & 31 deletions src/msr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use super::bitfield::{self, Facter};
use super::facts::{self, GenericFact};
use serde::{Deserialize, Serialize};
use std::fs;
use std::vec::Vec;
use std::{convert, error, fmt, io};

Expand Down Expand Up @@ -54,40 +53,44 @@ impl MsrStore for EmptyMSR {
}

#[cfg(all(target_os = "linux", feature = "use_msr"))]
pub struct LinuxMsrStore {
msr_device: fs::File,
}
pub mod linux {
use super::*;
use std::fs;
use std::io;

#[cfg(all(target_os = "linux", feature = "use_msr"))]
impl LinuxMsrStore {
pub fn new() -> Result<LinuxMsrStore> {
Ok(LinuxMsrStore {
msr_device: fs::OpenOptions::new()
.read(true)
.open("/dev/cpu/0/msr")
.map_err(|e| match e.kind() {
io::ErrorKind::NotFound => Error::NotAvailible,
io::ErrorKind::PermissionDenied => Error::NotAvailible,
_ => Error::IOError(e),
})?,
})
pub struct LinuxMsrStore {
msr_device: fs::File,
}
}

#[cfg(all(target_os = "linux", feature = "use_msr"))]
impl MsrStore for LinuxMsrStore {
fn is_empty(&self) -> bool {
false
impl LinuxMsrStore {
pub fn new() -> Result<LinuxMsrStore> {
Ok(LinuxMsrStore {
msr_device: fs::OpenOptions::new()
.read(true)
.open("/dev/cpu/0/msr")
.map_err(|e| match e.kind() {
io::ErrorKind::NotFound => Error::NotAvailible,
io::ErrorKind::PermissionDenied => Error::NotAvailible,
_ => Error::IOError(e),
})?,
})
}
}
fn get_value<'a>(&self, desc: &'a MSRDesc) -> std::result::Result<MSRValue<'a>, Error> {
use std::os::unix::fs::FileExt;
let mut msr_bytes = [u8::MIN; 8];
self.msr_device
.read_at(&mut msr_bytes, desc.address.into())?;
Ok(MSRValue {
desc,
value: u64::from_le_bytes(msr_bytes),
})

impl MsrStore for LinuxMsrStore {
fn is_empty(&self) -> bool {
false
}
fn get_value<'a>(&self, desc: &'a MSRDesc) -> std::result::Result<MSRValue<'a>, Error> {
use std::os::unix::fs::FileExt;
let mut msr_bytes = [u8::MIN; 8];
self.msr_device
.read_at(&mut msr_bytes, desc.address.into())?;
Ok(MSRValue {
desc,
value: u64::from_le_bytes(msr_bytes),
})
}
}
}

Expand Down

0 comments on commit 35450f9

Please sign in to comment.