Use num-enum crate to make everything typesafe

This commit is contained in:
Jason Davis 2022-10-03 14:19:31 -05:00 committed by Jason
parent 1e1455688d
commit 3b2ab313de
3 changed files with 30 additions and 11 deletions

1
Cargo.lock generated
View File

@ -5022,6 +5022,7 @@ dependencies = [
"lru",
"matches",
"min-max-heap",
"num_enum",
"rand 0.7.3",
"rand_chacha 0.2.2",
"raptorq",

View File

@ -30,6 +30,7 @@ lazy_static = "1.4.0"
log = "0.4.17"
lru = "0.7.7"
min-max-heap = "1.3.0"
num_enum = "0.5.7"
rand = "0.7.0"
rand_chacha = "0.2.2"
rayon = "1.5.3"

View File

@ -2,6 +2,8 @@
use core::arch::x86::{CpuidResult, __cpuid, __cpuid_count, __get_cpuid_max};
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::{CpuidResult, __cpuid, __cpuid_count, __get_cpuid_max};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[cfg(target_os = "linux")]
use std::{fs::File, io::BufReader};
use {
@ -104,20 +106,30 @@ struct CpuInfo {
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[derive(IntoPrimitive)]
#[repr(i64)]
enum CpuManufacturer {
Other,
Intel,
AMD,
Amd,
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[derive(TryFromPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
// The value passed into cpuid via eax, to control what the result means
enum CpuidParamValue {
Manufacturer = 0,
Processor = 1,
Cache = 2,
SerialNumber = 3,
Topology = 4,
Unsupported = 5,
ThermalAndPower = 6,
Extended = 7,
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
const CPUID_PARAM_MAX_SUPPORTED_VALUE: u32 = 7;
#[derive(Default)]
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
@ -728,25 +740,26 @@ impl SystemMonitorService {
edx: 0,
};
let max_leaf = cpuid_mfr.eax;
let max_subleaf_7 = if 7 <= max_leaf {
__get_cpuid_max(7).1
} else {
0
let max_leaf = match CpuidParamValue::try_from(std::cmp::min(
cpuid_mfr.eax,
CPUID_PARAM_MAX_SUPPORTED_VALUE,
)) {
Ok(val) => val,
Err(_err) => CpuidParamValue::Manufacturer,
};
let mfr_id: i64 = if cpuid_mfr.ebx == CPUID_MANUFACTURER_EBX_INTEL
&& cpuid_mfr.edx == CPUID_MANUFACTURER_EDX_INTEL
&& cpuid_mfr.ecx == CPUID_MANUFACTURER_ECX_INTEL
{
CpuManufacturer::Intel // GenuineIntel
CpuManufacturer::Intel.into() // GenuineIntel
} else if cpuid_mfr.ebx == CPUID_MANUFACTURER_EBX_AMD
&& cpuid_mfr.edx == CPUID_MANUFACTURER_EDX_AMD
&& cpuid_mfr.ecx == CPUID_MANUFACTURER_ECX_AMD
{
CpuManufacturer::AMD // AuthenticAMD
CpuManufacturer::Amd.into() // AuthenticAMD
} else {
CpuManufacturer::Other // anything else
CpuManufacturer::Other.into() // anything else
};
let cpuid_processor = if CpuidParamValue::Processor <= max_leaf {
@ -769,8 +782,12 @@ impl SystemMonitorService {
} else {
cpuid_empty
};
let cpuid_extended_1 = if CpuidParamValue::Extended <= max_leaf && 1 <= max_subleaf_7 {
__cpuid_count(7, 1)
let cpuid_extended_1 = if CpuidParamValue::Extended <= max_leaf {
if 1 <= __get_cpuid_max(7).1 {
__cpuid_count(7, 1)
} else {
cpuid_empty
}
} else {
cpuid_empty
};