Skip to content

Commit

Permalink
Additional config files specified on commandline
Browse files Browse the repository at this point in the history
They are added in-order.

cpuid - later IDs replace earlier ones
msr - MSRs are added to the end of the MSR list
  • Loading branch information
andrewjj20 committed Aug 28, 2024
1 parent 630d301 commit 4f615df
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait Facter<T: From<u32> + From<bool>> {
}

///Wraps a bit flag, usually representing if a feature is present or not
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Flag {
pub name: String,
pub bit: u8,
Expand All @@ -44,7 +44,7 @@ impl Bindable for Flag {
}

///Wraps an integer value from a bit field
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Int {
pub name: String,
pub bounds: ops::Range<u8>,
Expand All @@ -69,7 +69,7 @@ impl Bindable for Int {

/// Wraps an X86Model representation
/// These can have a number of weird conditions and are always going to be a part of a bit field
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct X86Model {
pub name: String,
}
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Bindable for X86Model {
}
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct X86Family {
pub name: String,
}
Expand Down Expand Up @@ -198,7 +198,7 @@ impl<'a> fmt::Display for Bound<'a, X86Family> {
}
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum Field {
Int(Int),
Expand Down
48 changes: 42 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
use std::path::{Path, PathBuf};

type YAMLFact = GenericFact<serde_yaml::Value>;
type YAMLFactSet = FactSet<serde_yaml::Value>;
Expand All @@ -37,6 +38,8 @@ enum CommandOpts {
struct Disp {
#[arg(short, long)]
raw: bool,
#[arg(long)]
skip_cpu: bool,
#[cfg(all(target_os = "linux", feature = "kvm"))]
#[arg(long)]
skip_kvm: bool,
Expand All @@ -50,11 +53,13 @@ impl Command for Disp {
if self.raw {
display_raw()
} else {
println!("CPUID:");
let cpuid_db = cpuinfo::RunningCpuidDB::new();
for (leaf, desc) in &config.cpuids {
if let Some(bound) = desc.bind_leaf(*leaf, &cpuid_db) {
println!("{:#010x}: {}", leaf, bound);
if !self.skip_cpu {
println!("CPUID:");
let cpuid_db = cpuinfo::RunningCpuidDB::new();
for (leaf, desc) in &config.cpuids {
if let Some(bound) = desc.bind_leaf(*leaf, &cpuid_db) {
println!("{:#010x}: {}", leaf, bound);
}
}
}

Expand Down Expand Up @@ -307,6 +312,17 @@ struct Definition {
pub msrs: Vec<MSRDesc>,
}

impl Definition {
pub fn union(&mut self, b: Definition) {
let Definition {
mut cpuids,
mut msrs,
} = b;
self.cpuids.append(&mut cpuids);
self.msrs.append(&mut msrs);
}
}

fn find_read_config() -> Result<Definition, Box<dyn std::error::Error>> {
let file = include_str!("config.yaml");
Ok(serde_yaml::from_str(file)?)
Expand All @@ -332,15 +348,35 @@ fn display_raw() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn read_additional_configs<Paths, P>(
def: &mut Definition,
paths: Paths,
) -> Result<(), Box<dyn std::error::Error>>
where
Paths: Iterator<Item = P>,
P: AsRef<Path> + Sized,
{
for path in paths {
let file = std::fs::read(path)?;
let definition = serde_yaml::from_slice(&file)?;
def.union(definition);
}
Ok(())
}

#[derive(Clone, Parser)]
struct CmdLine {
#[arg(short, long)]
add_config: Vec<PathBuf>,
#[command(subcommand)]
command: CommandOpts,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = CmdLine::parse();

let config = find_read_config()?;
let mut config = find_read_config()?;

read_additional_configs(&mut config, args.add_config.iter())?;

args.command.run(&config)
}
2 changes: 1 addition & 1 deletion src/msr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub mod linux {
}

/// Wraps a general description of an MSR
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MSRDesc {
pub name: String,
pub address: u32,
Expand Down

0 comments on commit 4f615df

Please sign in to comment.